2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
blob8b478aa265fd83f0d4faf8c0f360356734128ab5
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_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3653 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3654 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3655 dw_loc_list_ref);
3656 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3657 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3658 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3659 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3660 static void remove_addr_table_entry (addr_table_entry *);
3661 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3662 static inline rtx AT_addr (dw_attr_node *);
3663 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3664 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3665 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3666 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3667 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3668 unsigned long, bool);
3669 static inline const char *AT_lbl (dw_attr_node *);
3670 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3671 static const char *get_AT_low_pc (dw_die_ref);
3672 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3673 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3674 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3675 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3676 static bool is_c (void);
3677 static bool is_cxx (void);
3678 static bool is_cxx (const_tree);
3679 static bool is_fortran (void);
3680 static bool is_ada (void);
3681 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3682 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3683 static void add_child_die (dw_die_ref, dw_die_ref);
3684 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3685 static dw_die_ref lookup_type_die (tree);
3686 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3687 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3688 static void equate_type_number_to_die (tree, dw_die_ref);
3689 static dw_die_ref lookup_decl_die (tree);
3690 static var_loc_list *lookup_decl_loc (const_tree);
3691 static void equate_decl_number_to_die (tree, dw_die_ref);
3692 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3693 static void print_spaces (FILE *);
3694 static void print_die (dw_die_ref, FILE *);
3695 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3696 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3697 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3698 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3699 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3700 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3701 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3702 struct md5_ctx *, int *);
3703 struct checksum_attributes;
3704 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3705 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3706 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3707 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3708 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3709 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3710 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3711 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3712 static int is_type_die (dw_die_ref);
3713 static inline bool is_template_instantiation (dw_die_ref);
3714 static int is_declaration_die (dw_die_ref);
3715 static int should_move_die_to_comdat (dw_die_ref);
3716 static dw_die_ref clone_as_declaration (dw_die_ref);
3717 static dw_die_ref clone_die (dw_die_ref);
3718 static dw_die_ref clone_tree (dw_die_ref);
3719 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3720 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3721 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3722 static dw_die_ref generate_skeleton (dw_die_ref);
3723 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3724 dw_die_ref,
3725 dw_die_ref);
3726 static void break_out_comdat_types (dw_die_ref);
3727 static void copy_decls_for_unworthy_types (dw_die_ref);
3729 static void add_sibling_attributes (dw_die_ref);
3730 static void output_location_lists (dw_die_ref);
3731 static int constant_size (unsigned HOST_WIDE_INT);
3732 static unsigned long size_of_die (dw_die_ref);
3733 static void calc_die_sizes (dw_die_ref);
3734 static void calc_base_type_die_sizes (void);
3735 static void mark_dies (dw_die_ref);
3736 static void unmark_dies (dw_die_ref);
3737 static void unmark_all_dies (dw_die_ref);
3738 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3739 static unsigned long size_of_aranges (void);
3740 static enum dwarf_form value_format (dw_attr_node *);
3741 static void output_value_format (dw_attr_node *);
3742 static void output_abbrev_section (void);
3743 static void output_die_abbrevs (unsigned long, dw_die_ref);
3744 static void output_die (dw_die_ref);
3745 static void output_compilation_unit_header (enum dwarf_unit_type);
3746 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3747 static void output_comdat_type_unit (comdat_type_node *);
3748 static const char *dwarf2_name (tree, int);
3749 static void add_pubname (tree, dw_die_ref);
3750 static void add_enumerator_pubname (const char *, dw_die_ref);
3751 static void add_pubname_string (const char *, dw_die_ref);
3752 static void add_pubtype (tree, dw_die_ref);
3753 static void output_pubnames (vec<pubname_entry, va_gc> *);
3754 static void output_aranges (void);
3755 static unsigned int add_ranges (const_tree, bool = false);
3756 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3757 bool *, bool);
3758 static void output_ranges (void);
3759 static dw_line_info_table *new_line_info_table (void);
3760 static void output_line_info (bool);
3761 static void output_file_names (void);
3762 static dw_die_ref base_type_die (tree, bool);
3763 static int is_base_type (tree);
3764 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3765 static int decl_quals (const_tree);
3766 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3767 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3768 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3769 static unsigned int dbx_reg_number (const_rtx);
3770 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3771 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3772 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3773 enum var_init_status);
3774 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3775 enum var_init_status);
3776 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3777 enum var_init_status);
3778 static int is_based_loc (const_rtx);
3779 static bool resolve_one_addr (rtx *);
3780 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3781 enum var_init_status);
3782 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3783 enum var_init_status);
3784 struct loc_descr_context;
3785 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3786 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3787 static dw_loc_list_ref loc_list_from_tree (tree, int,
3788 struct loc_descr_context *);
3789 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3790 struct loc_descr_context *);
3791 static tree field_type (const_tree);
3792 static unsigned int simple_type_align_in_bits (const_tree);
3793 static unsigned int simple_decl_align_in_bits (const_tree);
3794 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3795 struct vlr_context;
3796 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3797 HOST_WIDE_INT *);
3798 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3799 dw_loc_list_ref);
3800 static void add_data_member_location_attribute (dw_die_ref, tree,
3801 struct vlr_context *);
3802 static bool add_const_value_attribute (dw_die_ref, rtx);
3803 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3804 static void insert_wide_int (const wide_int &, unsigned char *, int);
3805 static void insert_float (const_rtx, unsigned char *);
3806 static rtx rtl_for_decl_location (tree);
3807 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3808 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3809 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3810 static void add_name_attribute (dw_die_ref, const char *);
3811 static void add_desc_attribute (dw_die_ref, tree);
3812 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3813 static void add_comp_dir_attribute (dw_die_ref);
3814 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3815 struct loc_descr_context *);
3816 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3817 struct loc_descr_context *);
3818 static void add_subscript_info (dw_die_ref, tree, bool);
3819 static void add_byte_size_attribute (dw_die_ref, tree);
3820 static void add_alignment_attribute (dw_die_ref, tree);
3821 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3822 struct vlr_context *);
3823 static void add_bit_size_attribute (dw_die_ref, tree);
3824 static void add_prototyped_attribute (dw_die_ref, tree);
3825 static void add_abstract_origin_attribute (dw_die_ref, tree);
3826 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3827 static void add_src_coords_attributes (dw_die_ref, tree);
3828 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3829 static void add_discr_value (dw_die_ref, dw_discr_value *);
3830 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3831 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3832 static dw_die_ref scope_die_for (tree, dw_die_ref);
3833 static inline int local_scope_p (dw_die_ref);
3834 static inline int class_scope_p (dw_die_ref);
3835 static inline int class_or_namespace_scope_p (dw_die_ref);
3836 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3837 static void add_calling_convention_attribute (dw_die_ref, tree);
3838 static const char *type_tag (const_tree);
3839 static tree member_declared_type (const_tree);
3840 #if 0
3841 static const char *decl_start_label (tree);
3842 #endif
3843 static void gen_array_type_die (tree, dw_die_ref);
3844 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3845 #if 0
3846 static void gen_entry_point_die (tree, dw_die_ref);
3847 #endif
3848 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3849 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3850 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3851 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3852 static void gen_formal_types_die (tree, dw_die_ref);
3853 static void gen_subprogram_die (tree, dw_die_ref);
3854 static void gen_variable_die (tree, tree, dw_die_ref);
3855 static void gen_const_die (tree, dw_die_ref);
3856 static void gen_label_die (tree, dw_die_ref);
3857 static void gen_lexical_block_die (tree, dw_die_ref);
3858 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3859 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3860 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3861 static dw_die_ref gen_compile_unit_die (const char *);
3862 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3863 static void gen_member_die (tree, dw_die_ref);
3864 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3865 enum debug_info_usage);
3866 static void gen_subroutine_type_die (tree, dw_die_ref);
3867 static void gen_typedef_die (tree, dw_die_ref);
3868 static void gen_type_die (tree, dw_die_ref);
3869 static void gen_block_die (tree, dw_die_ref);
3870 static void decls_for_scope (tree, dw_die_ref, bool = true);
3871 static bool is_naming_typedef_decl (const_tree);
3872 static inline dw_die_ref get_context_die (tree);
3873 static void gen_namespace_die (tree, dw_die_ref);
3874 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3875 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3876 static dw_die_ref force_decl_die (tree);
3877 static dw_die_ref force_type_die (tree);
3878 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3879 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3880 static struct dwarf_file_data * lookup_filename (const char *);
3881 static void retry_incomplete_types (void);
3882 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3883 static void gen_generic_params_dies (tree);
3884 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3885 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3886 static void splice_child_die (dw_die_ref, dw_die_ref);
3887 static int file_info_cmp (const void *, const void *);
3888 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3889 const char *, var_loc_view, const char *);
3890 static void output_loc_list (dw_loc_list_ref);
3891 static char *gen_internal_sym (const char *);
3892 static bool want_pubnames (void);
3894 static void prune_unmark_dies (dw_die_ref);
3895 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3896 static void prune_unused_types_mark (dw_die_ref, int);
3897 static void prune_unused_types_walk (dw_die_ref);
3898 static void prune_unused_types_walk_attribs (dw_die_ref);
3899 static void prune_unused_types_prune (dw_die_ref);
3900 static void prune_unused_types (void);
3901 static int maybe_emit_file (struct dwarf_file_data *fd);
3902 static inline const char *AT_vms_delta1 (dw_attr_node *);
3903 static inline const char *AT_vms_delta2 (dw_attr_node *);
3904 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3905 static void gen_remaining_tmpl_value_param_die_attribute (void);
3906 static bool generic_type_p (tree);
3907 static void schedule_generic_params_dies_gen (tree t);
3908 static void gen_scheduled_generic_parms_dies (void);
3909 static void resolve_variable_values (void);
3911 static const char *comp_dir_string (void);
3913 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3915 /* enum for tracking thread-local variables whose address is really an offset
3916 relative to the TLS pointer, which will need link-time relocation, but will
3917 not need relocation by the DWARF consumer. */
3919 enum dtprel_bool
3921 dtprel_false = 0,
3922 dtprel_true = 1
3925 /* Return the operator to use for an address of a variable. For dtprel_true, we
3926 use DW_OP_const*. For regular variables, which need both link-time
3927 relocation and consumer-level relocation (e.g., to account for shared objects
3928 loaded at a random address), we use DW_OP_addr*. */
3930 static inline enum dwarf_location_atom
3931 dw_addr_op (enum dtprel_bool dtprel)
3933 if (dtprel == dtprel_true)
3934 return (dwarf_split_debug_info ? dwarf_OP (DW_OP_constx)
3935 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3936 else
3937 return dwarf_split_debug_info ? dwarf_OP (DW_OP_addrx) : DW_OP_addr;
3940 /* Return a pointer to a newly allocated address location description. If
3941 dwarf_split_debug_info is true, then record the address with the appropriate
3942 relocation. */
3943 static inline dw_loc_descr_ref
3944 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3946 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3948 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3949 ref->dw_loc_oprnd1.v.val_addr = addr;
3950 ref->dtprel = dtprel;
3951 if (dwarf_split_debug_info)
3952 ref->dw_loc_oprnd1.val_entry
3953 = add_addr_table_entry (addr,
3954 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3955 else
3956 ref->dw_loc_oprnd1.val_entry = NULL;
3958 return ref;
3961 /* Section names used to hold DWARF debugging information. */
3963 #ifndef DEBUG_INFO_SECTION
3964 #define DEBUG_INFO_SECTION ".debug_info"
3965 #endif
3966 #ifndef DEBUG_DWO_INFO_SECTION
3967 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3968 #endif
3969 #ifndef DEBUG_LTO_INFO_SECTION
3970 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3971 #endif
3972 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3973 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3974 #endif
3975 #ifndef DEBUG_ABBREV_SECTION
3976 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3977 #endif
3978 #ifndef DEBUG_LTO_ABBREV_SECTION
3979 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3980 #endif
3981 #ifndef DEBUG_DWO_ABBREV_SECTION
3982 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3983 #endif
3984 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3985 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3986 #endif
3987 #ifndef DEBUG_ARANGES_SECTION
3988 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3989 #endif
3990 #ifndef DEBUG_ADDR_SECTION
3991 #define DEBUG_ADDR_SECTION ".debug_addr"
3992 #endif
3993 #ifndef DEBUG_MACINFO_SECTION
3994 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3995 #endif
3996 #ifndef DEBUG_LTO_MACINFO_SECTION
3997 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3998 #endif
3999 #ifndef DEBUG_DWO_MACINFO_SECTION
4000 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
4001 #endif
4002 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
4003 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
4004 #endif
4005 #ifndef DEBUG_MACRO_SECTION
4006 #define DEBUG_MACRO_SECTION ".debug_macro"
4007 #endif
4008 #ifndef DEBUG_LTO_MACRO_SECTION
4009 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
4010 #endif
4011 #ifndef DEBUG_DWO_MACRO_SECTION
4012 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
4013 #endif
4014 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
4015 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
4016 #endif
4017 #ifndef DEBUG_LINE_SECTION
4018 #define DEBUG_LINE_SECTION ".debug_line"
4019 #endif
4020 #ifndef DEBUG_LTO_LINE_SECTION
4021 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
4022 #endif
4023 #ifndef DEBUG_DWO_LINE_SECTION
4024 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
4025 #endif
4026 #ifndef DEBUG_LTO_DWO_LINE_SECTION
4027 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4028 #endif
4029 #ifndef DEBUG_LOC_SECTION
4030 #define DEBUG_LOC_SECTION ".debug_loc"
4031 #endif
4032 #ifndef DEBUG_DWO_LOC_SECTION
4033 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4034 #endif
4035 #ifndef DEBUG_LOCLISTS_SECTION
4036 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4037 #endif
4038 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4039 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4040 #endif
4041 #ifndef DEBUG_PUBNAMES_SECTION
4042 #define DEBUG_PUBNAMES_SECTION \
4043 ((debug_generate_pub_sections == 2) \
4044 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4045 #endif
4046 #ifndef DEBUG_PUBTYPES_SECTION
4047 #define DEBUG_PUBTYPES_SECTION \
4048 ((debug_generate_pub_sections == 2) \
4049 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4050 #endif
4051 #ifndef DEBUG_STR_OFFSETS_SECTION
4052 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4053 #endif
4054 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4055 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4056 #endif
4057 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4058 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4059 #endif
4060 #ifndef DEBUG_STR_SECTION
4061 #define DEBUG_STR_SECTION ".debug_str"
4062 #endif
4063 #ifndef DEBUG_LTO_STR_SECTION
4064 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4065 #endif
4066 #ifndef DEBUG_STR_DWO_SECTION
4067 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4068 #endif
4069 #ifndef DEBUG_LTO_STR_DWO_SECTION
4070 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4071 #endif
4072 #ifndef DEBUG_RANGES_SECTION
4073 #define DEBUG_RANGES_SECTION ".debug_ranges"
4074 #endif
4075 #ifndef DEBUG_RNGLISTS_SECTION
4076 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4077 #endif
4078 #ifndef DEBUG_LINE_STR_SECTION
4079 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4080 #endif
4081 #ifndef DEBUG_LTO_LINE_STR_SECTION
4082 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4083 #endif
4085 /* Standard ELF section names for compiled code and data. */
4086 #ifndef TEXT_SECTION_NAME
4087 #define TEXT_SECTION_NAME ".text"
4088 #endif
4090 /* Section flags for .debug_str section. */
4091 #define DEBUG_STR_SECTION_FLAGS \
4092 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4093 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4094 : SECTION_DEBUG)
4096 /* Section flags for .debug_str.dwo section. */
4097 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4099 /* Attribute used to refer to the macro section. */
4100 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4101 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4103 /* Labels we insert at beginning sections we can reference instead of
4104 the section names themselves. */
4106 #ifndef TEXT_SECTION_LABEL
4107 #define TEXT_SECTION_LABEL "Ltext"
4108 #endif
4109 #ifndef COLD_TEXT_SECTION_LABEL
4110 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4111 #endif
4112 #ifndef DEBUG_LINE_SECTION_LABEL
4113 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4114 #endif
4115 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4116 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4117 #endif
4118 #ifndef DEBUG_INFO_SECTION_LABEL
4119 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4120 #endif
4121 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4122 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4123 #endif
4124 #ifndef DEBUG_ABBREV_SECTION_LABEL
4125 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4126 #endif
4127 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4128 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4129 #endif
4130 #ifndef DEBUG_ADDR_SECTION_LABEL
4131 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4132 #endif
4133 #ifndef DEBUG_LOC_SECTION_LABEL
4134 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4135 #endif
4136 #ifndef DEBUG_RANGES_SECTION_LABEL
4137 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4138 #endif
4139 #ifndef DEBUG_MACINFO_SECTION_LABEL
4140 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4141 #endif
4142 #ifndef DEBUG_MACRO_SECTION_LABEL
4143 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4144 #endif
4145 #define SKELETON_COMP_DIE_ABBREV 1
4146 #define SKELETON_TYPE_DIE_ABBREV 2
4148 /* Definitions of defaults for formats and names of various special
4149 (artificial) labels which may be generated within this file (when the -g
4150 options is used and DWARF2_DEBUGGING_INFO is in effect.
4151 If necessary, these may be overridden from within the tm.h file, but
4152 typically, overriding these defaults is unnecessary. */
4154 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4155 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4156 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4157 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4158 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4159 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4160 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4161 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4162 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4163 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4164 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4165 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4166 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4167 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4170 #ifndef TEXT_END_LABEL
4171 #define TEXT_END_LABEL "Letext"
4172 #endif
4173 #ifndef COLD_END_LABEL
4174 #define COLD_END_LABEL "Letext_cold"
4175 #endif
4176 #ifndef BLOCK_BEGIN_LABEL
4177 #define BLOCK_BEGIN_LABEL "LBB"
4178 #endif
4179 #ifndef BLOCK_INLINE_ENTRY_LABEL
4180 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4181 #endif
4182 #ifndef BLOCK_END_LABEL
4183 #define BLOCK_END_LABEL "LBE"
4184 #endif
4185 #ifndef LINE_CODE_LABEL
4186 #define LINE_CODE_LABEL "LM"
4187 #endif
4190 /* Return the root of the DIE's built for the current compilation unit. */
4191 static dw_die_ref
4192 comp_unit_die (void)
4194 if (!single_comp_unit_die)
4195 single_comp_unit_die = gen_compile_unit_die (NULL);
4196 return single_comp_unit_die;
4199 /* We allow a language front-end to designate a function that is to be
4200 called to "demangle" any name before it is put into a DIE. */
4202 static const char *(*demangle_name_func) (const char *);
4204 void
4205 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4207 demangle_name_func = func;
4210 /* Test if rtl node points to a pseudo register. */
4212 static inline int
4213 is_pseudo_reg (const_rtx rtl)
4215 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4216 || (GET_CODE (rtl) == SUBREG
4217 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4220 /* Return a reference to a type, with its const and volatile qualifiers
4221 removed. */
4223 static inline tree
4224 type_main_variant (tree type)
4226 type = TYPE_MAIN_VARIANT (type);
4228 /* ??? There really should be only one main variant among any group of
4229 variants of a given type (and all of the MAIN_VARIANT values for all
4230 members of the group should point to that one type) but sometimes the C
4231 front-end messes this up for array types, so we work around that bug
4232 here. */
4233 if (TREE_CODE (type) == ARRAY_TYPE)
4234 while (type != TYPE_MAIN_VARIANT (type))
4235 type = TYPE_MAIN_VARIANT (type);
4237 return type;
4240 /* Return nonzero if the given type node represents a tagged type. */
4242 static inline int
4243 is_tagged_type (const_tree type)
4245 enum tree_code code = TREE_CODE (type);
4247 return (code == RECORD_TYPE || code == UNION_TYPE
4248 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4251 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4253 static void
4254 get_ref_die_offset_label (char *label, dw_die_ref ref)
4256 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4259 /* Return die_offset of a DIE reference to a base type. */
4261 static unsigned long int
4262 get_base_type_offset (dw_die_ref ref)
4264 if (ref->die_offset)
4265 return ref->die_offset;
4266 if (comp_unit_die ()->die_abbrev)
4268 calc_base_type_die_sizes ();
4269 gcc_assert (ref->die_offset);
4271 return ref->die_offset;
4274 /* Return die_offset of a DIE reference other than base type. */
4276 static unsigned long int
4277 get_ref_die_offset (dw_die_ref ref)
4279 gcc_assert (ref->die_offset);
4280 return ref->die_offset;
4283 /* Convert a DIE tag into its string name. */
4285 static const char *
4286 dwarf_tag_name (unsigned int tag)
4288 const char *name = get_DW_TAG_name (tag);
4290 if (name != NULL)
4291 return name;
4293 return "DW_TAG_<unknown>";
4296 /* Convert a DWARF attribute code into its string name. */
4298 static const char *
4299 dwarf_attr_name (unsigned int attr)
4301 const char *name;
4303 switch (attr)
4305 #if VMS_DEBUGGING_INFO
4306 case DW_AT_HP_prologue:
4307 return "DW_AT_HP_prologue";
4308 #else
4309 case DW_AT_MIPS_loop_unroll_factor:
4310 return "DW_AT_MIPS_loop_unroll_factor";
4311 #endif
4313 #if VMS_DEBUGGING_INFO
4314 case DW_AT_HP_epilogue:
4315 return "DW_AT_HP_epilogue";
4316 #else
4317 case DW_AT_MIPS_stride:
4318 return "DW_AT_MIPS_stride";
4319 #endif
4322 name = get_DW_AT_name (attr);
4324 if (name != NULL)
4325 return name;
4327 return "DW_AT_<unknown>";
4330 /* Convert a DWARF value form code into its string name. */
4332 static const char *
4333 dwarf_form_name (unsigned int form)
4335 const char *name = get_DW_FORM_name (form);
4337 if (name != NULL)
4338 return name;
4340 return "DW_FORM_<unknown>";
4343 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4344 instance of an inlined instance of a decl which is local to an inline
4345 function, so we have to trace all of the way back through the origin chain
4346 to find out what sort of node actually served as the original seed for the
4347 given block. */
4349 static tree
4350 decl_ultimate_origin (const_tree decl)
4352 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4353 return NULL_TREE;
4355 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4356 we're trying to output the abstract instance of this function. */
4357 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4358 return NULL_TREE;
4360 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4361 most distant ancestor, this should never happen. */
4362 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4364 return DECL_ABSTRACT_ORIGIN (decl);
4367 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4368 of a virtual function may refer to a base class, so we check the 'this'
4369 parameter. */
4371 static tree
4372 decl_class_context (tree decl)
4374 tree context = NULL_TREE;
4376 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4377 context = DECL_CONTEXT (decl);
4378 else
4379 context = TYPE_MAIN_VARIANT
4380 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4382 if (context && !TYPE_P (context))
4383 context = NULL_TREE;
4385 return context;
4388 /* Add an attribute/value pair to a DIE. */
4390 static inline void
4391 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4393 /* Maybe this should be an assert? */
4394 if (die == NULL)
4395 return;
4397 if (flag_checking)
4399 /* Check we do not add duplicate attrs. Can't use get_AT here
4400 because that recurses to the specification/abstract origin DIE. */
4401 dw_attr_node *a;
4402 unsigned ix;
4403 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4404 gcc_assert (a->dw_attr != attr->dw_attr);
4407 vec_safe_reserve (die->die_attr, 1);
4408 vec_safe_push (die->die_attr, *attr);
4411 static inline enum dw_val_class
4412 AT_class (dw_attr_node *a)
4414 return a->dw_attr_val.val_class;
4417 /* Return the index for any attribute that will be referenced with a
4418 DW_FORM_addrx/GNU_addr_index or DW_FORM_strx/GNU_str_index. String
4419 indices are stored in dw_attr_val.v.val_str for reference counting
4420 pruning. */
4422 static inline unsigned int
4423 AT_index (dw_attr_node *a)
4425 if (AT_class (a) == dw_val_class_str)
4426 return a->dw_attr_val.v.val_str->index;
4427 else if (a->dw_attr_val.val_entry != NULL)
4428 return a->dw_attr_val.val_entry->index;
4429 return NOT_INDEXED;
4432 /* Add a flag value attribute to a DIE. */
4434 static inline void
4435 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4437 dw_attr_node attr;
4439 attr.dw_attr = attr_kind;
4440 attr.dw_attr_val.val_class = dw_val_class_flag;
4441 attr.dw_attr_val.val_entry = NULL;
4442 attr.dw_attr_val.v.val_flag = flag;
4443 add_dwarf_attr (die, &attr);
4446 static inline unsigned
4447 AT_flag (dw_attr_node *a)
4449 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4450 return a->dw_attr_val.v.val_flag;
4453 /* Add a signed integer attribute value to a DIE. */
4455 static inline void
4456 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4458 dw_attr_node attr;
4460 attr.dw_attr = attr_kind;
4461 attr.dw_attr_val.val_class = dw_val_class_const;
4462 attr.dw_attr_val.val_entry = NULL;
4463 attr.dw_attr_val.v.val_int = int_val;
4464 add_dwarf_attr (die, &attr);
4467 static inline HOST_WIDE_INT
4468 AT_int (dw_attr_node *a)
4470 gcc_assert (a && (AT_class (a) == dw_val_class_const
4471 || AT_class (a) == dw_val_class_const_implicit));
4472 return a->dw_attr_val.v.val_int;
4475 /* Add an unsigned integer attribute value to a DIE. */
4477 static inline void
4478 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4479 unsigned HOST_WIDE_INT unsigned_val)
4481 dw_attr_node attr;
4483 attr.dw_attr = attr_kind;
4484 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4485 attr.dw_attr_val.val_entry = NULL;
4486 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4487 add_dwarf_attr (die, &attr);
4490 static inline unsigned HOST_WIDE_INT
4491 AT_unsigned (dw_attr_node *a)
4493 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4494 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4495 return a->dw_attr_val.v.val_unsigned;
4498 /* Add an unsigned wide integer attribute value to a DIE. */
4500 static inline void
4501 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4502 const wide_int& w)
4504 dw_attr_node attr;
4506 attr.dw_attr = attr_kind;
4507 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4508 attr.dw_attr_val.val_entry = NULL;
4509 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4510 *attr.dw_attr_val.v.val_wide = w;
4511 add_dwarf_attr (die, &attr);
4514 /* Add an unsigned double integer attribute value to a DIE. */
4516 static inline void
4517 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4518 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4520 dw_attr_node attr;
4522 attr.dw_attr = attr_kind;
4523 attr.dw_attr_val.val_class = dw_val_class_const_double;
4524 attr.dw_attr_val.val_entry = NULL;
4525 attr.dw_attr_val.v.val_double.high = high;
4526 attr.dw_attr_val.v.val_double.low = low;
4527 add_dwarf_attr (die, &attr);
4530 /* Add a floating point attribute value to a DIE and return it. */
4532 static inline void
4533 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4534 unsigned int length, unsigned int elt_size, unsigned char *array)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_vec;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_vec.length = length;
4542 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4543 attr.dw_attr_val.v.val_vec.array = array;
4544 add_dwarf_attr (die, &attr);
4547 /* Add an 8-byte data attribute value to a DIE. */
4549 static inline void
4550 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4551 unsigned char data8[8])
4553 dw_attr_node attr;
4555 attr.dw_attr = attr_kind;
4556 attr.dw_attr_val.val_class = dw_val_class_data8;
4557 attr.dw_attr_val.val_entry = NULL;
4558 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4559 add_dwarf_attr (die, &attr);
4562 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4563 dwarf_split_debug_info, address attributes in dies destined for the
4564 final executable have force_direct set to avoid using indexed
4565 references. */
4567 static inline void
4568 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4569 bool force_direct)
4571 dw_attr_node attr;
4572 char * lbl_id;
4574 lbl_id = xstrdup (lbl_low);
4575 attr.dw_attr = DW_AT_low_pc;
4576 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4577 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4578 if (dwarf_split_debug_info && !force_direct)
4579 attr.dw_attr_val.val_entry
4580 = add_addr_table_entry (lbl_id, ate_kind_label);
4581 else
4582 attr.dw_attr_val.val_entry = NULL;
4583 add_dwarf_attr (die, &attr);
4585 attr.dw_attr = DW_AT_high_pc;
4586 if (dwarf_version < 4)
4587 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4588 else
4589 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4590 lbl_id = xstrdup (lbl_high);
4591 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4592 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4593 && dwarf_split_debug_info && !force_direct)
4594 attr.dw_attr_val.val_entry
4595 = add_addr_table_entry (lbl_id, ate_kind_label);
4596 else
4597 attr.dw_attr_val.val_entry = NULL;
4598 add_dwarf_attr (die, &attr);
4601 /* Hash and equality functions for debug_str_hash. */
4603 hashval_t
4604 indirect_string_hasher::hash (indirect_string_node *x)
4606 return htab_hash_string (x->str);
4609 bool
4610 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4612 return strcmp (x1->str, x2) == 0;
4615 /* Add STR to the given string hash table. */
4617 static struct indirect_string_node *
4618 find_AT_string_in_table (const char *str,
4619 hash_table<indirect_string_hasher> *table)
4621 struct indirect_string_node *node;
4623 indirect_string_node **slot
4624 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4625 if (*slot == NULL)
4627 node = ggc_cleared_alloc<indirect_string_node> ();
4628 node->str = ggc_strdup (str);
4629 *slot = node;
4631 else
4632 node = *slot;
4634 node->refcount++;
4635 return node;
4638 /* Add STR to the indirect string hash table. */
4640 static struct indirect_string_node *
4641 find_AT_string (const char *str)
4643 if (! debug_str_hash)
4644 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4646 return find_AT_string_in_table (str, debug_str_hash);
4649 /* Add a string attribute value to a DIE. */
4651 static inline void
4652 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4654 dw_attr_node attr;
4655 struct indirect_string_node *node;
4657 node = find_AT_string (str);
4659 attr.dw_attr = attr_kind;
4660 attr.dw_attr_val.val_class = dw_val_class_str;
4661 attr.dw_attr_val.val_entry = NULL;
4662 attr.dw_attr_val.v.val_str = node;
4663 add_dwarf_attr (die, &attr);
4666 static inline const char *
4667 AT_string (dw_attr_node *a)
4669 gcc_assert (a && AT_class (a) == dw_val_class_str);
4670 return a->dw_attr_val.v.val_str->str;
4673 /* Call this function directly to bypass AT_string_form's logic to put
4674 the string inline in the die. */
4676 static void
4677 set_indirect_string (struct indirect_string_node *node)
4679 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4680 /* Already indirect is a no op. */
4681 if (node->form == DW_FORM_strp
4682 || node->form == DW_FORM_line_strp
4683 || node->form == dwarf_FORM (DW_FORM_strx))
4685 gcc_assert (node->label);
4686 return;
4688 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4689 ++dw2_string_counter;
4690 node->label = xstrdup (label);
4692 if (!dwarf_split_debug_info)
4694 node->form = DW_FORM_strp;
4695 node->index = NOT_INDEXED;
4697 else
4699 node->form = dwarf_FORM (DW_FORM_strx);
4700 node->index = NO_INDEX_ASSIGNED;
4704 /* A helper function for dwarf2out_finish, called to reset indirect
4705 string decisions done for early LTO dwarf output before fat object
4706 dwarf output. */
4709 reset_indirect_string (indirect_string_node **h, void *)
4711 struct indirect_string_node *node = *h;
4712 if (node->form == DW_FORM_strp || node->form == dwarf_FORM (DW_FORM_strx))
4714 free (node->label);
4715 node->label = NULL;
4716 node->form = (dwarf_form) 0;
4717 node->index = 0;
4719 return 1;
4722 /* Find out whether a string should be output inline in DIE
4723 or out-of-line in .debug_str section. */
4725 static enum dwarf_form
4726 find_string_form (struct indirect_string_node *node)
4728 unsigned int len;
4730 if (node->form)
4731 return node->form;
4733 len = strlen (node->str) + 1;
4735 /* If the string is shorter or equal to the size of the reference, it is
4736 always better to put it inline. */
4737 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4738 return node->form = DW_FORM_string;
4740 /* If we cannot expect the linker to merge strings in .debug_str
4741 section, only put it into .debug_str if it is worth even in this
4742 single module. */
4743 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4744 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4745 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4746 return node->form = DW_FORM_string;
4748 set_indirect_string (node);
4750 return node->form;
4753 /* Find out whether the string referenced from the attribute should be
4754 output inline in DIE or out-of-line in .debug_str section. */
4756 static enum dwarf_form
4757 AT_string_form (dw_attr_node *a)
4759 gcc_assert (a && AT_class (a) == dw_val_class_str);
4760 return find_string_form (a->dw_attr_val.v.val_str);
4763 /* Add a DIE reference attribute value to a DIE. */
4765 static inline void
4766 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4768 dw_attr_node attr;
4769 gcc_checking_assert (targ_die != NULL);
4771 /* With LTO we can end up trying to reference something we didn't create
4772 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4773 if (targ_die == NULL)
4774 return;
4776 attr.dw_attr = attr_kind;
4777 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4778 attr.dw_attr_val.val_entry = NULL;
4779 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4780 attr.dw_attr_val.v.val_die_ref.external = 0;
4781 add_dwarf_attr (die, &attr);
4784 /* Change DIE reference REF to point to NEW_DIE instead. */
4786 static inline void
4787 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4789 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4790 ref->dw_attr_val.v.val_die_ref.die = new_die;
4791 ref->dw_attr_val.v.val_die_ref.external = 0;
4794 /* Add an AT_specification attribute to a DIE, and also make the back
4795 pointer from the specification to the definition. */
4797 static inline void
4798 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4800 add_AT_die_ref (die, DW_AT_specification, targ_die);
4801 gcc_assert (!targ_die->die_definition);
4802 targ_die->die_definition = die;
4805 static inline dw_die_ref
4806 AT_ref (dw_attr_node *a)
4808 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4809 return a->dw_attr_val.v.val_die_ref.die;
4812 static inline int
4813 AT_ref_external (dw_attr_node *a)
4815 if (a && AT_class (a) == dw_val_class_die_ref)
4816 return a->dw_attr_val.v.val_die_ref.external;
4818 return 0;
4821 static inline void
4822 set_AT_ref_external (dw_attr_node *a, int i)
4824 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4825 a->dw_attr_val.v.val_die_ref.external = i;
4828 /* Add a location description attribute value to a DIE. */
4830 static inline void
4831 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4833 dw_attr_node attr;
4835 attr.dw_attr = attr_kind;
4836 attr.dw_attr_val.val_class = dw_val_class_loc;
4837 attr.dw_attr_val.val_entry = NULL;
4838 attr.dw_attr_val.v.val_loc = loc;
4839 add_dwarf_attr (die, &attr);
4842 static inline dw_loc_descr_ref
4843 AT_loc (dw_attr_node *a)
4845 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4846 return a->dw_attr_val.v.val_loc;
4849 static inline void
4850 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4852 dw_attr_node attr;
4854 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4855 return;
4857 attr.dw_attr = attr_kind;
4858 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4859 attr.dw_attr_val.val_entry = NULL;
4860 attr.dw_attr_val.v.val_loc_list = loc_list;
4861 add_dwarf_attr (die, &attr);
4862 have_location_lists = true;
4865 static inline dw_loc_list_ref
4866 AT_loc_list (dw_attr_node *a)
4868 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4869 return a->dw_attr_val.v.val_loc_list;
4872 /* Add a view list attribute to DIE. It must have a DW_AT_location
4873 attribute, because the view list complements the location list. */
4875 static inline void
4876 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4878 dw_attr_node attr;
4880 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4881 return;
4883 attr.dw_attr = attr_kind;
4884 attr.dw_attr_val.val_class = dw_val_class_view_list;
4885 attr.dw_attr_val.val_entry = NULL;
4886 attr.dw_attr_val.v.val_view_list = die;
4887 add_dwarf_attr (die, &attr);
4888 gcc_checking_assert (get_AT (die, DW_AT_location));
4889 gcc_assert (have_location_lists);
4892 /* Return a pointer to the location list referenced by the attribute.
4893 If the named attribute is a view list, look up the corresponding
4894 DW_AT_location attribute and return its location list. */
4896 static inline dw_loc_list_ref *
4897 AT_loc_list_ptr (dw_attr_node *a)
4899 gcc_assert (a);
4900 switch (AT_class (a))
4902 case dw_val_class_loc_list:
4903 return &a->dw_attr_val.v.val_loc_list;
4904 case dw_val_class_view_list:
4906 dw_attr_node *l;
4907 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4908 if (!l)
4909 return NULL;
4910 gcc_checking_assert (l + 1 == a);
4911 return AT_loc_list_ptr (l);
4913 default:
4914 gcc_unreachable ();
4918 /* Return the location attribute value associated with a view list
4919 attribute value. */
4921 static inline dw_val_node *
4922 view_list_to_loc_list_val_node (dw_val_node *val)
4924 gcc_assert (val->val_class == dw_val_class_view_list);
4925 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4926 if (!loc)
4927 return NULL;
4928 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4929 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4930 return &loc->dw_attr_val;
4933 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4935 static hashval_t hash (addr_table_entry *);
4936 static bool equal (addr_table_entry *, addr_table_entry *);
4939 /* Table of entries into the .debug_addr section. */
4941 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4943 /* Hash an address_table_entry. */
4945 hashval_t
4946 addr_hasher::hash (addr_table_entry *a)
4948 inchash::hash hstate;
4949 switch (a->kind)
4951 case ate_kind_rtx:
4952 hstate.add_int (0);
4953 break;
4954 case ate_kind_rtx_dtprel:
4955 hstate.add_int (1);
4956 break;
4957 case ate_kind_label:
4958 return htab_hash_string (a->addr.label);
4959 default:
4960 gcc_unreachable ();
4962 inchash::add_rtx (a->addr.rtl, hstate);
4963 return hstate.end ();
4966 /* Determine equality for two address_table_entries. */
4968 bool
4969 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4971 if (a1->kind != a2->kind)
4972 return 0;
4973 switch (a1->kind)
4975 case ate_kind_rtx:
4976 case ate_kind_rtx_dtprel:
4977 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4978 case ate_kind_label:
4979 return strcmp (a1->addr.label, a2->addr.label) == 0;
4980 default:
4981 gcc_unreachable ();
4985 /* Initialize an addr_table_entry. */
4987 void
4988 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4990 e->kind = kind;
4991 switch (kind)
4993 case ate_kind_rtx:
4994 case ate_kind_rtx_dtprel:
4995 e->addr.rtl = (rtx) addr;
4996 break;
4997 case ate_kind_label:
4998 e->addr.label = (char *) addr;
4999 break;
5001 e->refcount = 0;
5002 e->index = NO_INDEX_ASSIGNED;
5005 /* Add attr to the address table entry to the table. Defer setting an
5006 index until output time. */
5008 static addr_table_entry *
5009 add_addr_table_entry (void *addr, enum ate_kind kind)
5011 addr_table_entry *node;
5012 addr_table_entry finder;
5014 gcc_assert (dwarf_split_debug_info);
5015 if (! addr_index_table)
5016 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5017 init_addr_table_entry (&finder, kind, addr);
5018 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5020 if (*slot == HTAB_EMPTY_ENTRY)
5022 node = ggc_cleared_alloc<addr_table_entry> ();
5023 init_addr_table_entry (node, kind, addr);
5024 *slot = node;
5026 else
5027 node = *slot;
5029 node->refcount++;
5030 return node;
5033 /* Remove an entry from the addr table by decrementing its refcount.
5034 Strictly, decrementing the refcount would be enough, but the
5035 assertion that the entry is actually in the table has found
5036 bugs. */
5038 static void
5039 remove_addr_table_entry (addr_table_entry *entry)
5041 gcc_assert (dwarf_split_debug_info && addr_index_table);
5042 /* After an index is assigned, the table is frozen. */
5043 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5044 entry->refcount--;
5047 /* Given a location list, remove all addresses it refers to from the
5048 address_table. */
5050 static void
5051 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5053 for (; descr; descr = descr->dw_loc_next)
5054 if (descr->dw_loc_oprnd1.val_entry != NULL)
5056 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5057 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5061 /* A helper function for dwarf2out_finish called through
5062 htab_traverse. Assign an addr_table_entry its index. All entries
5063 must be collected into the table when this function is called,
5064 because the indexing code relies on htab_traverse to traverse nodes
5065 in the same order for each run. */
5068 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5070 addr_table_entry *node = *h;
5072 /* Don't index unreferenced nodes. */
5073 if (node->refcount == 0)
5074 return 1;
5076 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5077 node->index = *index;
5078 *index += 1;
5080 return 1;
5083 /* Add an address constant attribute value to a DIE. When using
5084 dwarf_split_debug_info, address attributes in dies destined for the
5085 final executable should be direct references--setting the parameter
5086 force_direct ensures this behavior. */
5088 static inline void
5089 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5090 bool force_direct)
5092 dw_attr_node attr;
5094 attr.dw_attr = attr_kind;
5095 attr.dw_attr_val.val_class = dw_val_class_addr;
5096 attr.dw_attr_val.v.val_addr = addr;
5097 if (dwarf_split_debug_info && !force_direct)
5098 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5099 else
5100 attr.dw_attr_val.val_entry = NULL;
5101 add_dwarf_attr (die, &attr);
5104 /* Get the RTX from to an address DIE attribute. */
5106 static inline rtx
5107 AT_addr (dw_attr_node *a)
5109 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5110 return a->dw_attr_val.v.val_addr;
5113 /* Add a file attribute value to a DIE. */
5115 static inline void
5116 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5117 struct dwarf_file_data *fd)
5119 dw_attr_node attr;
5121 attr.dw_attr = attr_kind;
5122 attr.dw_attr_val.val_class = dw_val_class_file;
5123 attr.dw_attr_val.val_entry = NULL;
5124 attr.dw_attr_val.v.val_file = fd;
5125 add_dwarf_attr (die, &attr);
5128 /* Get the dwarf_file_data from a file DIE attribute. */
5130 static inline struct dwarf_file_data *
5131 AT_file (dw_attr_node *a)
5133 gcc_assert (a && (AT_class (a) == dw_val_class_file
5134 || AT_class (a) == dw_val_class_file_implicit));
5135 return a->dw_attr_val.v.val_file;
5138 /* Add a symbolic view identifier attribute value to a DIE. */
5140 static inline void
5141 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5142 const char *view_label)
5144 dw_attr_node attr;
5146 attr.dw_attr = attr_kind;
5147 attr.dw_attr_val.val_class = dw_val_class_symview;
5148 attr.dw_attr_val.val_entry = NULL;
5149 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5150 add_dwarf_attr (die, &attr);
5153 /* Add a label identifier attribute value to a DIE. */
5155 static inline void
5156 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5157 const char *lbl_id)
5159 dw_attr_node attr;
5161 attr.dw_attr = attr_kind;
5162 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5163 attr.dw_attr_val.val_entry = NULL;
5164 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5165 if (dwarf_split_debug_info)
5166 attr.dw_attr_val.val_entry
5167 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5168 ate_kind_label);
5169 add_dwarf_attr (die, &attr);
5172 /* Add a section offset attribute value to a DIE, an offset into the
5173 debug_line section. */
5175 static inline void
5176 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5177 const char *label)
5179 dw_attr_node attr;
5181 attr.dw_attr = attr_kind;
5182 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5183 attr.dw_attr_val.val_entry = NULL;
5184 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5185 add_dwarf_attr (die, &attr);
5188 /* Add a section offset attribute value to a DIE, an offset into the
5189 debug_macinfo section. */
5191 static inline void
5192 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5193 const char *label)
5195 dw_attr_node attr;
5197 attr.dw_attr = attr_kind;
5198 attr.dw_attr_val.val_class = dw_val_class_macptr;
5199 attr.dw_attr_val.val_entry = NULL;
5200 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5201 add_dwarf_attr (die, &attr);
5204 /* Add a range_list attribute value to a DIE. When using
5205 dwarf_split_debug_info, address attributes in dies destined for the
5206 final executable should be direct references--setting the parameter
5207 force_direct ensures this behavior. */
5209 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5210 #define RELOCATED_OFFSET (NULL)
5212 static void
5213 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5214 long unsigned int offset, bool force_direct)
5216 dw_attr_node attr;
5218 attr.dw_attr = attr_kind;
5219 attr.dw_attr_val.val_class = dw_val_class_range_list;
5220 /* For the range_list attribute, use val_entry to store whether the
5221 offset should follow split-debug-info or normal semantics. This
5222 value is read in output_range_list_offset. */
5223 if (dwarf_split_debug_info && !force_direct)
5224 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5225 else
5226 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5227 attr.dw_attr_val.v.val_offset = offset;
5228 add_dwarf_attr (die, &attr);
5231 /* Return the start label of a delta attribute. */
5233 static inline const char *
5234 AT_vms_delta1 (dw_attr_node *a)
5236 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5237 return a->dw_attr_val.v.val_vms_delta.lbl1;
5240 /* Return the end label of a delta attribute. */
5242 static inline const char *
5243 AT_vms_delta2 (dw_attr_node *a)
5245 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5246 return a->dw_attr_val.v.val_vms_delta.lbl2;
5249 static inline const char *
5250 AT_lbl (dw_attr_node *a)
5252 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5253 || AT_class (a) == dw_val_class_lineptr
5254 || AT_class (a) == dw_val_class_macptr
5255 || AT_class (a) == dw_val_class_loclistsptr
5256 || AT_class (a) == dw_val_class_high_pc));
5257 return a->dw_attr_val.v.val_lbl_id;
5260 /* Get the attribute of type attr_kind. */
5262 static dw_attr_node *
5263 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5265 dw_attr_node *a;
5266 unsigned ix;
5267 dw_die_ref spec = NULL;
5269 if (! die)
5270 return NULL;
5272 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5273 if (a->dw_attr == attr_kind)
5274 return a;
5275 else if (a->dw_attr == DW_AT_specification
5276 || a->dw_attr == DW_AT_abstract_origin)
5277 spec = AT_ref (a);
5279 if (spec)
5280 return get_AT (spec, attr_kind);
5282 return NULL;
5285 /* Returns the parent of the declaration of DIE. */
5287 static dw_die_ref
5288 get_die_parent (dw_die_ref die)
5290 dw_die_ref t;
5292 if (!die)
5293 return NULL;
5295 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5296 || (t = get_AT_ref (die, DW_AT_specification)))
5297 die = t;
5299 return die->die_parent;
5302 /* Return the "low pc" attribute value, typically associated with a subprogram
5303 DIE. Return null if the "low pc" attribute is either not present, or if it
5304 cannot be represented as an assembler label identifier. */
5306 static inline const char *
5307 get_AT_low_pc (dw_die_ref die)
5309 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5311 return a ? AT_lbl (a) : NULL;
5314 /* Return the value of the string attribute designated by ATTR_KIND, or
5315 NULL if it is not present. */
5317 static inline const char *
5318 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5320 dw_attr_node *a = get_AT (die, attr_kind);
5322 return a ? AT_string (a) : NULL;
5325 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5326 if it is not present. */
5328 static inline int
5329 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5331 dw_attr_node *a = get_AT (die, attr_kind);
5333 return a ? AT_flag (a) : 0;
5336 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5337 if it is not present. */
5339 static inline unsigned
5340 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5342 dw_attr_node *a = get_AT (die, attr_kind);
5344 return a ? AT_unsigned (a) : 0;
5347 static inline dw_die_ref
5348 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5350 dw_attr_node *a = get_AT (die, attr_kind);
5352 return a ? AT_ref (a) : NULL;
5355 static inline struct dwarf_file_data *
5356 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5358 dw_attr_node *a = get_AT (die, attr_kind);
5360 return a ? AT_file (a) : NULL;
5363 /* Return TRUE if the language is C. */
5365 static inline bool
5366 is_c (void)
5368 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5370 return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_C99
5371 || lang == DW_LANG_C11 || lang == DW_LANG_ObjC);
5376 /* Return TRUE if the language is C++. */
5378 static inline bool
5379 is_cxx (void)
5381 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5383 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5384 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5387 /* Return TRUE if DECL was created by the C++ frontend. */
5389 static bool
5390 is_cxx (const_tree decl)
5392 if (in_lto_p)
5394 const_tree context = get_ultimate_context (decl);
5395 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5396 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5398 return is_cxx ();
5401 /* Return TRUE if the language is Fortran. */
5403 static inline bool
5404 is_fortran (void)
5406 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5408 return (lang == DW_LANG_Fortran77
5409 || lang == DW_LANG_Fortran90
5410 || lang == DW_LANG_Fortran95
5411 || lang == DW_LANG_Fortran03
5412 || lang == DW_LANG_Fortran08);
5415 static inline bool
5416 is_fortran (const_tree decl)
5418 if (in_lto_p)
5420 const_tree context = get_ultimate_context (decl);
5421 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5422 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5423 "GNU Fortran", 11) == 0
5424 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5425 "GNU F77") == 0);
5427 return is_fortran ();
5430 /* Return TRUE if the language is Ada. */
5432 static inline bool
5433 is_ada (void)
5435 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5437 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5440 /* Return TRUE if the language is D. */
5442 static inline bool
5443 is_dlang (void)
5445 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5447 return lang == DW_LANG_D;
5450 /* Remove the specified attribute if present. Return TRUE if removal
5451 was successful. */
5453 static bool
5454 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5456 dw_attr_node *a;
5457 unsigned ix;
5459 if (! die)
5460 return false;
5462 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5463 if (a->dw_attr == attr_kind)
5465 if (AT_class (a) == dw_val_class_str)
5466 if (a->dw_attr_val.v.val_str->refcount)
5467 a->dw_attr_val.v.val_str->refcount--;
5469 /* vec::ordered_remove should help reduce the number of abbrevs
5470 that are needed. */
5471 die->die_attr->ordered_remove (ix);
5472 return true;
5474 return false;
5477 /* Remove CHILD from its parent. PREV must have the property that
5478 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5480 static void
5481 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5483 gcc_assert (child->die_parent == prev->die_parent);
5484 gcc_assert (prev->die_sib == child);
5485 if (prev == child)
5487 gcc_assert (child->die_parent->die_child == child);
5488 prev = NULL;
5490 else
5491 prev->die_sib = child->die_sib;
5492 if (child->die_parent->die_child == child)
5493 child->die_parent->die_child = prev;
5494 child->die_sib = NULL;
5497 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5498 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5500 static void
5501 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5503 dw_die_ref parent = old_child->die_parent;
5505 gcc_assert (parent == prev->die_parent);
5506 gcc_assert (prev->die_sib == old_child);
5508 new_child->die_parent = parent;
5509 if (prev == old_child)
5511 gcc_assert (parent->die_child == old_child);
5512 new_child->die_sib = new_child;
5514 else
5516 prev->die_sib = new_child;
5517 new_child->die_sib = old_child->die_sib;
5519 if (old_child->die_parent->die_child == old_child)
5520 old_child->die_parent->die_child = new_child;
5521 old_child->die_sib = NULL;
5524 /* Move all children from OLD_PARENT to NEW_PARENT. */
5526 static void
5527 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5529 dw_die_ref c;
5530 new_parent->die_child = old_parent->die_child;
5531 old_parent->die_child = NULL;
5532 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5535 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5536 matches TAG. */
5538 static void
5539 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5541 dw_die_ref c;
5543 c = die->die_child;
5544 if (c) do {
5545 dw_die_ref prev = c;
5546 c = c->die_sib;
5547 while (c->die_tag == tag)
5549 remove_child_with_prev (c, prev);
5550 c->die_parent = NULL;
5551 /* Might have removed every child. */
5552 if (die->die_child == NULL)
5553 return;
5554 c = prev->die_sib;
5556 } while (c != die->die_child);
5559 /* Add a CHILD_DIE as the last child of DIE. */
5561 static void
5562 add_child_die (dw_die_ref die, dw_die_ref child_die)
5564 /* FIXME this should probably be an assert. */
5565 if (! die || ! child_die)
5566 return;
5567 gcc_assert (die != child_die);
5569 child_die->die_parent = die;
5570 if (die->die_child)
5572 child_die->die_sib = die->die_child->die_sib;
5573 die->die_child->die_sib = child_die;
5575 else
5576 child_die->die_sib = child_die;
5577 die->die_child = child_die;
5580 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5582 static void
5583 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5584 dw_die_ref after_die)
5586 gcc_assert (die
5587 && child_die
5588 && after_die
5589 && die->die_child
5590 && die != child_die);
5592 child_die->die_parent = die;
5593 child_die->die_sib = after_die->die_sib;
5594 after_die->die_sib = child_die;
5595 if (die->die_child == after_die)
5596 die->die_child = child_die;
5599 /* Unassociate CHILD from its parent, and make its parent be
5600 NEW_PARENT. */
5602 static void
5603 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5605 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5606 if (p->die_sib == child)
5608 remove_child_with_prev (child, p);
5609 break;
5611 add_child_die (new_parent, child);
5614 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5615 is the specification, to the end of PARENT's list of children.
5616 This is done by removing and re-adding it. */
5618 static void
5619 splice_child_die (dw_die_ref parent, dw_die_ref child)
5621 /* We want the declaration DIE from inside the class, not the
5622 specification DIE at toplevel. */
5623 if (child->die_parent != parent)
5625 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5627 if (tmp)
5628 child = tmp;
5631 gcc_assert (child->die_parent == parent
5632 || (child->die_parent
5633 == get_AT_ref (parent, DW_AT_specification)));
5635 reparent_child (child, parent);
5638 /* Create and return a new die with TAG_VALUE as tag. */
5640 static inline dw_die_ref
5641 new_die_raw (enum dwarf_tag tag_value)
5643 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5644 die->die_tag = tag_value;
5645 return die;
5648 /* Create and return a new die with a parent of PARENT_DIE. If
5649 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5650 associated tree T must be supplied to determine parenthood
5651 later. */
5653 static inline dw_die_ref
5654 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5656 dw_die_ref die = new_die_raw (tag_value);
5658 if (parent_die != NULL)
5659 add_child_die (parent_die, die);
5660 else
5662 limbo_die_node *limbo_node;
5664 /* No DIEs created after early dwarf should end up in limbo,
5665 because the limbo list should not persist past LTO
5666 streaming. */
5667 if (tag_value != DW_TAG_compile_unit
5668 /* These are allowed because they're generated while
5669 breaking out COMDAT units late. */
5670 && tag_value != DW_TAG_type_unit
5671 && tag_value != DW_TAG_skeleton_unit
5672 && !early_dwarf
5673 /* Allow nested functions to live in limbo because they will
5674 only temporarily live there, as decls_for_scope will fix
5675 them up. */
5676 && (TREE_CODE (t) != FUNCTION_DECL
5677 || !decl_function_context (t))
5678 /* Same as nested functions above but for types. Types that
5679 are local to a function will be fixed in
5680 decls_for_scope. */
5681 && (!RECORD_OR_UNION_TYPE_P (t)
5682 || !TYPE_CONTEXT (t)
5683 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5684 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5685 especially in the ltrans stage, but once we implement LTO
5686 dwarf streaming, we should remove this exception. */
5687 && !in_lto_p)
5689 fprintf (stderr, "symbol ended up in limbo too late:");
5690 debug_generic_stmt (t);
5691 gcc_unreachable ();
5694 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5695 limbo_node->die = die;
5696 limbo_node->created_for = t;
5697 limbo_node->next = limbo_die_list;
5698 limbo_die_list = limbo_node;
5701 return die;
5704 /* Return the DIE associated with the given type specifier. */
5706 static inline dw_die_ref
5707 lookup_type_die (tree type)
5709 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5710 if (die && die->removed)
5712 TYPE_SYMTAB_DIE (type) = NULL;
5713 return NULL;
5715 return die;
5718 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5719 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5720 anonymous type instead the one of the naming typedef. */
5722 static inline dw_die_ref
5723 strip_naming_typedef (tree type, dw_die_ref type_die)
5725 if (type
5726 && TREE_CODE (type) == RECORD_TYPE
5727 && type_die
5728 && type_die->die_tag == DW_TAG_typedef
5729 && is_naming_typedef_decl (TYPE_NAME (type)))
5730 type_die = get_AT_ref (type_die, DW_AT_type);
5731 return type_die;
5734 /* Like lookup_type_die, but if type is an anonymous type named by a
5735 typedef[1], return the DIE of the anonymous type instead the one of
5736 the naming typedef. This is because in gen_typedef_die, we did
5737 equate the anonymous struct named by the typedef with the DIE of
5738 the naming typedef. So by default, lookup_type_die on an anonymous
5739 struct yields the DIE of the naming typedef.
5741 [1]: Read the comment of is_naming_typedef_decl to learn about what
5742 a naming typedef is. */
5744 static inline dw_die_ref
5745 lookup_type_die_strip_naming_typedef (tree type)
5747 dw_die_ref die = lookup_type_die (type);
5748 return strip_naming_typedef (type, die);
5751 /* Equate a DIE to a given type specifier. */
5753 static inline void
5754 equate_type_number_to_die (tree type, dw_die_ref type_die)
5756 TYPE_SYMTAB_DIE (type) = type_die;
5759 static dw_die_ref maybe_create_die_with_external_ref (tree);
5760 struct GTY(()) sym_off_pair
5762 const char * GTY((skip)) sym;
5763 unsigned HOST_WIDE_INT off;
5765 static GTY(()) hash_map<tree, sym_off_pair> *external_die_map;
5767 /* Returns a hash value for X (which really is a die_struct). */
5769 inline hashval_t
5770 decl_die_hasher::hash (die_node *x)
5772 return (hashval_t) x->decl_id;
5775 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5777 inline bool
5778 decl_die_hasher::equal (die_node *x, tree y)
5780 return (x->decl_id == DECL_UID (y));
5783 /* Return the DIE associated with a given declaration. */
5785 static inline dw_die_ref
5786 lookup_decl_die (tree decl)
5788 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5789 NO_INSERT);
5790 if (!die)
5792 if (in_lto_p)
5793 return maybe_create_die_with_external_ref (decl);
5794 return NULL;
5796 if ((*die)->removed)
5798 decl_die_table->clear_slot (die);
5799 return NULL;
5801 return *die;
5805 /* Return the DIE associated with BLOCK. */
5807 static inline dw_die_ref
5808 lookup_block_die (tree block)
5810 dw_die_ref die = BLOCK_DIE (block);
5811 if (!die && in_lto_p)
5812 return maybe_create_die_with_external_ref (block);
5813 return die;
5816 /* Associate DIE with BLOCK. */
5818 static inline void
5819 equate_block_to_die (tree block, dw_die_ref die)
5821 BLOCK_DIE (block) = die;
5823 #undef BLOCK_DIE
5826 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5827 style reference. Return true if we found one refering to a DIE for
5828 DECL, otherwise return false. */
5830 static bool
5831 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5832 unsigned HOST_WIDE_INT *off)
5834 dw_die_ref die;
5836 if (in_lto_p)
5838 /* During WPA stage and incremental linking we use a hash-map
5839 to store the decl <-> label + offset map. */
5840 if (!external_die_map)
5841 return false;
5842 sym_off_pair *desc = external_die_map->get (decl);
5843 if (!desc)
5844 return false;
5845 *sym = desc->sym;
5846 *off = desc->off;
5847 return true;
5850 if (TREE_CODE (decl) == BLOCK)
5851 die = lookup_block_die (decl);
5852 else
5853 die = lookup_decl_die (decl);
5854 if (!die)
5855 return false;
5857 /* Similar to get_ref_die_offset_label, but using the "correct"
5858 label. */
5859 *off = die->die_offset;
5860 while (die->die_parent)
5861 die = die->die_parent;
5862 /* For the containing CU DIE we compute a die_symbol in
5863 compute_comp_unit_symbol. */
5864 gcc_assert (die->die_tag == DW_TAG_compile_unit
5865 && die->die_id.die_symbol != NULL);
5866 *sym = die->die_id.die_symbol;
5867 return true;
5870 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5872 static void
5873 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5874 const char *symbol, HOST_WIDE_INT offset)
5876 /* Create a fake DIE that contains the reference. Don't use
5877 new_die because we don't want to end up in the limbo list. */
5878 /* ??? We probably want to share these, thus put a ref to the DIE
5879 we create here to the external_die_map entry. */
5880 dw_die_ref ref = new_die_raw (die->die_tag);
5881 ref->die_id.die_symbol = symbol;
5882 ref->die_offset = offset;
5883 ref->with_offset = 1;
5884 add_AT_die_ref (die, attr_kind, ref);
5887 /* Create a DIE for DECL if required and add a reference to a DIE
5888 at SYMBOL + OFFSET which contains attributes dumped early. */
5890 static void
5891 dwarf2out_register_external_die (tree decl, const char *sym,
5892 unsigned HOST_WIDE_INT off)
5894 if (debug_info_level == DINFO_LEVEL_NONE)
5895 return;
5897 if (!external_die_map)
5898 external_die_map = hash_map<tree, sym_off_pair>::create_ggc (1000);
5899 gcc_checking_assert (!external_die_map->get (decl));
5900 sym_off_pair p = { IDENTIFIER_POINTER (get_identifier (sym)), off };
5901 external_die_map->put (decl, p);
5904 /* If we have a registered external DIE for DECL return a new DIE for
5905 the concrete instance with an appropriate abstract origin. */
5907 static dw_die_ref
5908 maybe_create_die_with_external_ref (tree decl)
5910 if (!external_die_map)
5911 return NULL;
5912 sym_off_pair *desc = external_die_map->get (decl);
5913 if (!desc)
5914 return NULL;
5916 const char *sym = desc->sym;
5917 unsigned HOST_WIDE_INT off = desc->off;
5919 in_lto_p = false;
5920 dw_die_ref die = (TREE_CODE (decl) == BLOCK
5921 ? lookup_block_die (decl) : lookup_decl_die (decl));
5922 gcc_assert (!die);
5923 in_lto_p = true;
5925 tree ctx;
5926 dw_die_ref parent = NULL;
5927 /* Need to lookup a DIE for the decls context - the containing
5928 function or translation unit. */
5929 if (TREE_CODE (decl) == BLOCK)
5931 ctx = BLOCK_SUPERCONTEXT (decl);
5932 /* ??? We do not output DIEs for all scopes thus skip as
5933 many DIEs as needed. */
5934 while (TREE_CODE (ctx) == BLOCK
5935 && !lookup_block_die (ctx))
5936 ctx = BLOCK_SUPERCONTEXT (ctx);
5938 else
5939 ctx = DECL_CONTEXT (decl);
5940 /* Peel types in the context stack. */
5941 while (ctx && TYPE_P (ctx))
5942 ctx = TYPE_CONTEXT (ctx);
5943 /* Likewise namespaces in case we do not want to emit DIEs for them. */
5944 if (debug_info_level <= DINFO_LEVEL_TERSE)
5945 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5946 ctx = DECL_CONTEXT (ctx);
5947 if (ctx)
5949 if (TREE_CODE (ctx) == BLOCK)
5950 parent = lookup_block_die (ctx);
5951 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5952 /* Keep the 1:1 association during WPA. */
5953 && !flag_wpa
5954 && flag_incremental_link != INCREMENTAL_LINK_LTO)
5955 /* Otherwise all late annotations go to the main CU which
5956 imports the original CUs. */
5957 parent = comp_unit_die ();
5958 else if (TREE_CODE (ctx) == FUNCTION_DECL
5959 && TREE_CODE (decl) != FUNCTION_DECL
5960 && TREE_CODE (decl) != PARM_DECL
5961 && TREE_CODE (decl) != RESULT_DECL
5962 && TREE_CODE (decl) != BLOCK)
5963 /* Leave function local entities parent determination to when
5964 we process scope vars. */
5966 else
5967 parent = lookup_decl_die (ctx);
5969 else
5970 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5971 Handle this case gracefully by globalizing stuff. */
5972 parent = comp_unit_die ();
5973 /* Create a DIE "stub". */
5974 switch (TREE_CODE (decl))
5976 case TRANSLATION_UNIT_DECL:
5978 die = comp_unit_die ();
5979 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5980 to create a DIE for the original CUs. */
5981 return die;
5983 case NAMESPACE_DECL:
5984 if (is_fortran (decl))
5985 die = new_die (DW_TAG_module, parent, decl);
5986 else
5987 die = new_die (DW_TAG_namespace, parent, decl);
5988 break;
5989 case FUNCTION_DECL:
5990 die = new_die (DW_TAG_subprogram, parent, decl);
5991 break;
5992 case VAR_DECL:
5993 die = new_die (DW_TAG_variable, parent, decl);
5994 break;
5995 case RESULT_DECL:
5996 die = new_die (DW_TAG_variable, parent, decl);
5997 break;
5998 case PARM_DECL:
5999 die = new_die (DW_TAG_formal_parameter, parent, decl);
6000 break;
6001 case CONST_DECL:
6002 die = new_die (DW_TAG_constant, parent, decl);
6003 break;
6004 case LABEL_DECL:
6005 die = new_die (DW_TAG_label, parent, decl);
6006 break;
6007 case BLOCK:
6008 die = new_die (DW_TAG_lexical_block, parent, decl);
6009 break;
6010 default:
6011 gcc_unreachable ();
6013 if (TREE_CODE (decl) == BLOCK)
6014 equate_block_to_die (decl, die);
6015 else
6016 equate_decl_number_to_die (decl, die);
6018 add_desc_attribute (die, decl);
6020 /* Add a reference to the DIE providing early debug at $sym + off. */
6021 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
6023 return die;
6026 /* Returns a hash value for X (which really is a var_loc_list). */
6028 inline hashval_t
6029 decl_loc_hasher::hash (var_loc_list *x)
6031 return (hashval_t) x->decl_id;
6034 /* Return nonzero if decl_id of var_loc_list X is the same as
6035 UID of decl *Y. */
6037 inline bool
6038 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6040 return (x->decl_id == DECL_UID (y));
6043 /* Return the var_loc list associated with a given declaration. */
6045 static inline var_loc_list *
6046 lookup_decl_loc (const_tree decl)
6048 if (!decl_loc_table)
6049 return NULL;
6050 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6053 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6055 inline hashval_t
6056 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6058 return (hashval_t) x->decl_id;
6061 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6062 UID of decl *Y. */
6064 inline bool
6065 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6067 return (x->decl_id == DECL_UID (y));
6070 /* Equate a DIE to a particular declaration. */
6072 static void
6073 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6075 unsigned int decl_id = DECL_UID (decl);
6077 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6078 decl_die->decl_id = decl_id;
6081 /* Return how many bits covers PIECE EXPR_LIST. */
6083 static HOST_WIDE_INT
6084 decl_piece_bitsize (rtx piece)
6086 int ret = (int) GET_MODE (piece);
6087 if (ret)
6088 return ret;
6089 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6090 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6091 return INTVAL (XEXP (XEXP (piece, 0), 0));
6094 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6096 static rtx *
6097 decl_piece_varloc_ptr (rtx piece)
6099 if ((int) GET_MODE (piece))
6100 return &XEXP (piece, 0);
6101 else
6102 return &XEXP (XEXP (piece, 0), 1);
6105 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6106 Next is the chain of following piece nodes. */
6108 static rtx_expr_list *
6109 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6111 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6112 return alloc_EXPR_LIST (bitsize, loc_note, next);
6113 else
6114 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6115 GEN_INT (bitsize),
6116 loc_note), next);
6119 /* Return rtx that should be stored into loc field for
6120 LOC_NOTE and BITPOS/BITSIZE. */
6122 static rtx
6123 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6124 HOST_WIDE_INT bitsize)
6126 if (bitsize != -1)
6128 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6129 if (bitpos != 0)
6130 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6132 return loc_note;
6135 /* This function either modifies location piece list *DEST in
6136 place (if SRC and INNER is NULL), or copies location piece list
6137 *SRC to *DEST while modifying it. Location BITPOS is modified
6138 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6139 not copied and if needed some padding around it is added.
6140 When modifying in place, DEST should point to EXPR_LIST where
6141 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6142 to the start of the whole list and INNER points to the EXPR_LIST
6143 where earlier pieces cover PIECE_BITPOS bits. */
6145 static void
6146 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6147 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6148 HOST_WIDE_INT bitsize, rtx loc_note)
6150 HOST_WIDE_INT diff;
6151 bool copy = inner != NULL;
6153 if (copy)
6155 /* First copy all nodes preceding the current bitpos. */
6156 while (src != inner)
6158 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6159 decl_piece_bitsize (*src), NULL_RTX);
6160 dest = &XEXP (*dest, 1);
6161 src = &XEXP (*src, 1);
6164 /* Add padding if needed. */
6165 if (bitpos != piece_bitpos)
6167 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6168 copy ? NULL_RTX : *dest);
6169 dest = &XEXP (*dest, 1);
6171 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6173 gcc_assert (!copy);
6174 /* A piece with correct bitpos and bitsize already exist,
6175 just update the location for it and return. */
6176 *decl_piece_varloc_ptr (*dest) = loc_note;
6177 return;
6179 /* Add the piece that changed. */
6180 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6181 dest = &XEXP (*dest, 1);
6182 /* Skip over pieces that overlap it. */
6183 diff = bitpos - piece_bitpos + bitsize;
6184 if (!copy)
6185 src = dest;
6186 while (diff > 0 && *src)
6188 rtx piece = *src;
6189 diff -= decl_piece_bitsize (piece);
6190 if (copy)
6191 src = &XEXP (piece, 1);
6192 else
6194 *src = XEXP (piece, 1);
6195 free_EXPR_LIST_node (piece);
6198 /* Add padding if needed. */
6199 if (diff < 0 && *src)
6201 if (!copy)
6202 dest = src;
6203 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6204 dest = &XEXP (*dest, 1);
6206 if (!copy)
6207 return;
6208 /* Finally copy all nodes following it. */
6209 while (*src)
6211 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6212 decl_piece_bitsize (*src), NULL_RTX);
6213 dest = &XEXP (*dest, 1);
6214 src = &XEXP (*src, 1);
6218 /* Add a variable location node to the linked list for DECL. */
6220 static struct var_loc_node *
6221 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6223 unsigned int decl_id;
6224 var_loc_list *temp;
6225 struct var_loc_node *loc = NULL;
6226 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6228 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6230 tree realdecl = DECL_DEBUG_EXPR (decl);
6231 if (handled_component_p (realdecl)
6232 || (TREE_CODE (realdecl) == MEM_REF
6233 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6235 bool reverse;
6236 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6237 &bitsize, &reverse);
6238 if (!innerdecl
6239 || !DECL_P (innerdecl)
6240 || DECL_IGNORED_P (innerdecl)
6241 || TREE_STATIC (innerdecl)
6242 || bitsize == 0
6243 || bitpos + bitsize > 256)
6244 return NULL;
6245 decl = innerdecl;
6249 decl_id = DECL_UID (decl);
6250 var_loc_list **slot
6251 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6252 if (*slot == NULL)
6254 temp = ggc_cleared_alloc<var_loc_list> ();
6255 temp->decl_id = decl_id;
6256 *slot = temp;
6258 else
6259 temp = *slot;
6261 /* For PARM_DECLs try to keep around the original incoming value,
6262 even if that means we'll emit a zero-range .debug_loc entry. */
6263 if (temp->last
6264 && temp->first == temp->last
6265 && TREE_CODE (decl) == PARM_DECL
6266 && NOTE_P (temp->first->loc)
6267 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6268 && DECL_INCOMING_RTL (decl)
6269 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6270 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6271 == GET_CODE (DECL_INCOMING_RTL (decl))
6272 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6273 && (bitsize != -1
6274 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6275 NOTE_VAR_LOCATION_LOC (loc_note))
6276 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6277 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6279 loc = ggc_cleared_alloc<var_loc_node> ();
6280 temp->first->next = loc;
6281 temp->last = loc;
6282 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6284 else if (temp->last)
6286 struct var_loc_node *last = temp->last, *unused = NULL;
6287 rtx *piece_loc = NULL, last_loc_note;
6288 HOST_WIDE_INT piece_bitpos = 0;
6289 if (last->next)
6291 last = last->next;
6292 gcc_assert (last->next == NULL);
6294 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6296 piece_loc = &last->loc;
6299 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6300 if (piece_bitpos + cur_bitsize > bitpos)
6301 break;
6302 piece_bitpos += cur_bitsize;
6303 piece_loc = &XEXP (*piece_loc, 1);
6305 while (*piece_loc);
6307 /* TEMP->LAST here is either pointer to the last but one or
6308 last element in the chained list, LAST is pointer to the
6309 last element. */
6310 if (label && strcmp (last->label, label) == 0 && last->view == view)
6312 /* For SRA optimized variables if there weren't any real
6313 insns since last note, just modify the last node. */
6314 if (piece_loc != NULL)
6316 adjust_piece_list (piece_loc, NULL, NULL,
6317 bitpos, piece_bitpos, bitsize, loc_note);
6318 return NULL;
6320 /* If the last note doesn't cover any instructions, remove it. */
6321 if (temp->last != last)
6323 temp->last->next = NULL;
6324 unused = last;
6325 last = temp->last;
6326 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6328 else
6330 gcc_assert (temp->first == temp->last
6331 || (temp->first->next == temp->last
6332 && TREE_CODE (decl) == PARM_DECL));
6333 memset (temp->last, '\0', sizeof (*temp->last));
6334 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6335 return temp->last;
6338 if (bitsize == -1 && NOTE_P (last->loc))
6339 last_loc_note = last->loc;
6340 else if (piece_loc != NULL
6341 && *piece_loc != NULL_RTX
6342 && piece_bitpos == bitpos
6343 && decl_piece_bitsize (*piece_loc) == bitsize)
6344 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6345 else
6346 last_loc_note = NULL_RTX;
6347 /* If the current location is the same as the end of the list,
6348 and either both or neither of the locations is uninitialized,
6349 we have nothing to do. */
6350 if (last_loc_note == NULL_RTX
6351 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6352 NOTE_VAR_LOCATION_LOC (loc_note)))
6353 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6354 != NOTE_VAR_LOCATION_STATUS (loc_note))
6355 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6356 == VAR_INIT_STATUS_UNINITIALIZED)
6357 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6358 == VAR_INIT_STATUS_UNINITIALIZED))))
6360 /* Add LOC to the end of list and update LAST. If the last
6361 element of the list has been removed above, reuse its
6362 memory for the new node, otherwise allocate a new one. */
6363 if (unused)
6365 loc = unused;
6366 memset (loc, '\0', sizeof (*loc));
6368 else
6369 loc = ggc_cleared_alloc<var_loc_node> ();
6370 if (bitsize == -1 || piece_loc == NULL)
6371 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6372 else
6373 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6374 bitpos, piece_bitpos, bitsize, loc_note);
6375 last->next = loc;
6376 /* Ensure TEMP->LAST will point either to the new last but one
6377 element of the chain, or to the last element in it. */
6378 if (last != temp->last)
6379 temp->last = last;
6381 else if (unused)
6382 ggc_free (unused);
6384 else
6386 loc = ggc_cleared_alloc<var_loc_node> ();
6387 temp->first = loc;
6388 temp->last = loc;
6389 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6391 return loc;
6394 /* Keep track of the number of spaces used to indent the
6395 output of the debugging routines that print the structure of
6396 the DIE internal representation. */
6397 static int print_indent;
6399 /* Indent the line the number of spaces given by print_indent. */
6401 static inline void
6402 print_spaces (FILE *outfile)
6404 fprintf (outfile, "%*s", print_indent, "");
6407 /* Print a type signature in hex. */
6409 static inline void
6410 print_signature (FILE *outfile, char *sig)
6412 int i;
6414 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6415 fprintf (outfile, "%02x", sig[i] & 0xff);
6418 static inline void
6419 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6421 if (discr_value->pos)
6422 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6423 else
6424 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6427 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6429 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6430 RECURSE, output location descriptor operations. */
6432 static void
6433 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6435 switch (val->val_class)
6437 case dw_val_class_addr:
6438 fprintf (outfile, "address");
6439 break;
6440 case dw_val_class_offset:
6441 fprintf (outfile, "offset");
6442 break;
6443 case dw_val_class_loc:
6444 fprintf (outfile, "location descriptor");
6445 if (val->v.val_loc == NULL)
6446 fprintf (outfile, " -> <null>\n");
6447 else if (recurse)
6449 fprintf (outfile, ":\n");
6450 print_indent += 4;
6451 print_loc_descr (val->v.val_loc, outfile);
6452 print_indent -= 4;
6454 else
6456 if (flag_dump_noaddr || flag_dump_unnumbered)
6457 fprintf (outfile, " #\n");
6458 else
6459 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6461 break;
6462 case dw_val_class_loc_list:
6463 fprintf (outfile, "location list -> label:%s",
6464 val->v.val_loc_list->ll_symbol);
6465 break;
6466 case dw_val_class_view_list:
6467 val = view_list_to_loc_list_val_node (val);
6468 fprintf (outfile, "location list with views -> labels:%s and %s",
6469 val->v.val_loc_list->ll_symbol,
6470 val->v.val_loc_list->vl_symbol);
6471 break;
6472 case dw_val_class_range_list:
6473 fprintf (outfile, "range list");
6474 break;
6475 case dw_val_class_const:
6476 case dw_val_class_const_implicit:
6477 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6478 break;
6479 case dw_val_class_unsigned_const:
6480 case dw_val_class_unsigned_const_implicit:
6481 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6482 break;
6483 case dw_val_class_const_double:
6484 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6485 HOST_WIDE_INT_PRINT_UNSIGNED")",
6486 val->v.val_double.high,
6487 val->v.val_double.low);
6488 break;
6489 case dw_val_class_wide_int:
6491 int i = val->v.val_wide->get_len ();
6492 fprintf (outfile, "constant (");
6493 gcc_assert (i > 0);
6494 if (val->v.val_wide->elt (i - 1) == 0)
6495 fprintf (outfile, "0x");
6496 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6497 val->v.val_wide->elt (--i));
6498 while (--i >= 0)
6499 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6500 val->v.val_wide->elt (i));
6501 fprintf (outfile, ")");
6502 break;
6504 case dw_val_class_vec:
6505 fprintf (outfile, "floating-point or vector constant");
6506 break;
6507 case dw_val_class_flag:
6508 fprintf (outfile, "%u", val->v.val_flag);
6509 break;
6510 case dw_val_class_die_ref:
6511 if (val->v.val_die_ref.die != NULL)
6513 dw_die_ref die = val->v.val_die_ref.die;
6515 if (die->comdat_type_p)
6517 fprintf (outfile, "die -> signature: ");
6518 print_signature (outfile,
6519 die->die_id.die_type_node->signature);
6521 else if (die->die_id.die_symbol)
6523 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6524 if (die->with_offset)
6525 fprintf (outfile, " + %ld", die->die_offset);
6527 else
6528 fprintf (outfile, "die -> %ld", die->die_offset);
6529 if (flag_dump_noaddr || flag_dump_unnumbered)
6530 fprintf (outfile, " #");
6531 else
6532 fprintf (outfile, " (%p)", (void *) die);
6534 else
6535 fprintf (outfile, "die -> <null>");
6536 break;
6537 case dw_val_class_vms_delta:
6538 fprintf (outfile, "delta: @slotcount(%s-%s)",
6539 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6540 break;
6541 case dw_val_class_symview:
6542 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6543 break;
6544 case dw_val_class_lbl_id:
6545 case dw_val_class_lineptr:
6546 case dw_val_class_macptr:
6547 case dw_val_class_loclistsptr:
6548 case dw_val_class_high_pc:
6549 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6550 break;
6551 case dw_val_class_str:
6552 if (val->v.val_str->str != NULL)
6553 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6554 else
6555 fprintf (outfile, "<null>");
6556 break;
6557 case dw_val_class_file:
6558 case dw_val_class_file_implicit:
6559 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6560 val->v.val_file->emitted_number);
6561 break;
6562 case dw_val_class_data8:
6564 int i;
6566 for (i = 0; i < 8; i++)
6567 fprintf (outfile, "%02x", val->v.val_data8[i]);
6568 break;
6570 case dw_val_class_discr_value:
6571 print_discr_value (outfile, &val->v.val_discr_value);
6572 break;
6573 case dw_val_class_discr_list:
6574 for (dw_discr_list_ref node = val->v.val_discr_list;
6575 node != NULL;
6576 node = node->dw_discr_next)
6578 if (node->dw_discr_range)
6580 fprintf (outfile, " .. ");
6581 print_discr_value (outfile, &node->dw_discr_lower_bound);
6582 print_discr_value (outfile, &node->dw_discr_upper_bound);
6584 else
6585 print_discr_value (outfile, &node->dw_discr_lower_bound);
6587 if (node->dw_discr_next != NULL)
6588 fprintf (outfile, " | ");
6590 default:
6591 break;
6595 /* Likewise, for a DIE attribute. */
6597 static void
6598 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6600 print_dw_val (&a->dw_attr_val, recurse, outfile);
6604 /* Print the list of operands in the LOC location description to OUTFILE. This
6605 routine is a debugging aid only. */
6607 static void
6608 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6610 dw_loc_descr_ref l = loc;
6612 if (loc == NULL)
6614 print_spaces (outfile);
6615 fprintf (outfile, "<null>\n");
6616 return;
6619 for (l = loc; l != NULL; l = l->dw_loc_next)
6621 print_spaces (outfile);
6622 if (flag_dump_noaddr || flag_dump_unnumbered)
6623 fprintf (outfile, "#");
6624 else
6625 fprintf (outfile, "(%p)", (void *) l);
6626 fprintf (outfile, " %s",
6627 dwarf_stack_op_name (l->dw_loc_opc));
6628 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6630 fprintf (outfile, " ");
6631 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6633 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6635 fprintf (outfile, ", ");
6636 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6638 fprintf (outfile, "\n");
6642 /* Print the information associated with a given DIE, and its children.
6643 This routine is a debugging aid only. */
6645 static void
6646 print_die (dw_die_ref die, FILE *outfile)
6648 dw_attr_node *a;
6649 dw_die_ref c;
6650 unsigned ix;
6652 print_spaces (outfile);
6653 fprintf (outfile, "DIE %4ld: %s ",
6654 die->die_offset, dwarf_tag_name (die->die_tag));
6655 if (flag_dump_noaddr || flag_dump_unnumbered)
6656 fprintf (outfile, "#\n");
6657 else
6658 fprintf (outfile, "(%p)\n", (void*) die);
6659 print_spaces (outfile);
6660 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6661 fprintf (outfile, " offset: %ld", die->die_offset);
6662 fprintf (outfile, " mark: %d\n", die->die_mark);
6664 if (die->comdat_type_p)
6666 print_spaces (outfile);
6667 fprintf (outfile, " signature: ");
6668 print_signature (outfile, die->die_id.die_type_node->signature);
6669 fprintf (outfile, "\n");
6672 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6674 print_spaces (outfile);
6675 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6677 print_attribute (a, true, outfile);
6678 fprintf (outfile, "\n");
6681 if (die->die_child != NULL)
6683 print_indent += 4;
6684 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6685 print_indent -= 4;
6687 if (print_indent == 0)
6688 fprintf (outfile, "\n");
6691 /* Print the list of operations in the LOC location description. */
6693 DEBUG_FUNCTION void
6694 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6696 print_loc_descr (loc, stderr);
6699 /* Print the information collected for a given DIE. */
6701 DEBUG_FUNCTION void
6702 debug_dwarf_die (dw_die_ref die)
6704 print_die (die, stderr);
6707 DEBUG_FUNCTION void
6708 debug (die_struct &ref)
6710 print_die (&ref, stderr);
6713 DEBUG_FUNCTION void
6714 debug (die_struct *ptr)
6716 if (ptr)
6717 debug (*ptr);
6718 else
6719 fprintf (stderr, "<nil>\n");
6723 /* Print all DWARF information collected for the compilation unit.
6724 This routine is a debugging aid only. */
6726 DEBUG_FUNCTION void
6727 debug_dwarf (void)
6729 print_indent = 0;
6730 print_die (comp_unit_die (), stderr);
6733 /* Verify the DIE tree structure. */
6735 DEBUG_FUNCTION void
6736 verify_die (dw_die_ref die)
6738 gcc_assert (!die->die_mark);
6739 if (die->die_parent == NULL
6740 && die->die_sib == NULL)
6741 return;
6742 /* Verify the die_sib list is cyclic. */
6743 dw_die_ref x = die;
6746 x->die_mark = 1;
6747 x = x->die_sib;
6749 while (x && !x->die_mark);
6750 gcc_assert (x == die);
6751 x = die;
6754 /* Verify all dies have the same parent. */
6755 gcc_assert (x->die_parent == die->die_parent);
6756 if (x->die_child)
6758 /* Verify the child has the proper parent and recurse. */
6759 gcc_assert (x->die_child->die_parent == x);
6760 verify_die (x->die_child);
6762 x->die_mark = 0;
6763 x = x->die_sib;
6765 while (x && x->die_mark);
6768 /* Sanity checks on DIEs. */
6770 static void
6771 check_die (dw_die_ref die)
6773 unsigned ix;
6774 dw_attr_node *a;
6775 bool inline_found = false;
6776 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6777 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6778 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6780 switch (a->dw_attr)
6782 case DW_AT_inline:
6783 if (a->dw_attr_val.v.val_unsigned)
6784 inline_found = true;
6785 break;
6786 case DW_AT_location:
6787 ++n_location;
6788 break;
6789 case DW_AT_low_pc:
6790 ++n_low_pc;
6791 break;
6792 case DW_AT_high_pc:
6793 ++n_high_pc;
6794 break;
6795 case DW_AT_artificial:
6796 ++n_artificial;
6797 break;
6798 case DW_AT_decl_column:
6799 ++n_decl_column;
6800 break;
6801 case DW_AT_decl_line:
6802 ++n_decl_line;
6803 break;
6804 case DW_AT_decl_file:
6805 ++n_decl_file;
6806 break;
6807 default:
6808 break;
6811 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6812 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6814 fprintf (stderr, "Duplicate attributes in DIE:\n");
6815 debug_dwarf_die (die);
6816 gcc_unreachable ();
6818 if (inline_found)
6820 /* A debugging information entry that is a member of an abstract
6821 instance tree [that has DW_AT_inline] should not contain any
6822 attributes which describe aspects of the subroutine which vary
6823 between distinct inlined expansions or distinct out-of-line
6824 expansions. */
6825 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6826 gcc_assert (a->dw_attr != DW_AT_low_pc
6827 && a->dw_attr != DW_AT_high_pc
6828 && a->dw_attr != DW_AT_location
6829 && a->dw_attr != DW_AT_frame_base
6830 && a->dw_attr != DW_AT_call_all_calls
6831 && a->dw_attr != DW_AT_GNU_all_call_sites);
6835 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6836 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6837 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6839 /* Calculate the checksum of a location expression. */
6841 static inline void
6842 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6844 int tem;
6845 inchash::hash hstate;
6846 hashval_t hash;
6848 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6849 CHECKSUM (tem);
6850 hash_loc_operands (loc, hstate);
6851 hash = hstate.end();
6852 CHECKSUM (hash);
6855 /* Calculate the checksum of an attribute. */
6857 static void
6858 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6860 dw_loc_descr_ref loc;
6861 rtx r;
6863 CHECKSUM (at->dw_attr);
6865 /* We don't care that this was compiled with a different compiler
6866 snapshot; if the output is the same, that's what matters. */
6867 if (at->dw_attr == DW_AT_producer)
6868 return;
6870 switch (AT_class (at))
6872 case dw_val_class_const:
6873 case dw_val_class_const_implicit:
6874 CHECKSUM (at->dw_attr_val.v.val_int);
6875 break;
6876 case dw_val_class_unsigned_const:
6877 case dw_val_class_unsigned_const_implicit:
6878 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6879 break;
6880 case dw_val_class_const_double:
6881 CHECKSUM (at->dw_attr_val.v.val_double);
6882 break;
6883 case dw_val_class_wide_int:
6884 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6885 get_full_len (*at->dw_attr_val.v.val_wide)
6886 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6887 break;
6888 case dw_val_class_vec:
6889 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6890 (at->dw_attr_val.v.val_vec.length
6891 * at->dw_attr_val.v.val_vec.elt_size));
6892 break;
6893 case dw_val_class_flag:
6894 CHECKSUM (at->dw_attr_val.v.val_flag);
6895 break;
6896 case dw_val_class_str:
6897 CHECKSUM_STRING (AT_string (at));
6898 break;
6900 case dw_val_class_addr:
6901 r = AT_addr (at);
6902 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6903 CHECKSUM_STRING (XSTR (r, 0));
6904 break;
6906 case dw_val_class_offset:
6907 CHECKSUM (at->dw_attr_val.v.val_offset);
6908 break;
6910 case dw_val_class_loc:
6911 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6912 loc_checksum (loc, ctx);
6913 break;
6915 case dw_val_class_die_ref:
6916 die_checksum (AT_ref (at), ctx, mark);
6917 break;
6919 case dw_val_class_fde_ref:
6920 case dw_val_class_vms_delta:
6921 case dw_val_class_symview:
6922 case dw_val_class_lbl_id:
6923 case dw_val_class_lineptr:
6924 case dw_val_class_macptr:
6925 case dw_val_class_loclistsptr:
6926 case dw_val_class_high_pc:
6927 break;
6929 case dw_val_class_file:
6930 case dw_val_class_file_implicit:
6931 CHECKSUM_STRING (AT_file (at)->filename);
6932 break;
6934 case dw_val_class_data8:
6935 CHECKSUM (at->dw_attr_val.v.val_data8);
6936 break;
6938 default:
6939 break;
6943 /* Calculate the checksum of a DIE. */
6945 static void
6946 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6948 dw_die_ref c;
6949 dw_attr_node *a;
6950 unsigned ix;
6952 /* To avoid infinite recursion. */
6953 if (die->die_mark)
6955 CHECKSUM (die->die_mark);
6956 return;
6958 die->die_mark = ++(*mark);
6960 CHECKSUM (die->die_tag);
6962 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6963 attr_checksum (a, ctx, mark);
6965 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6968 #undef CHECKSUM
6969 #undef CHECKSUM_BLOCK
6970 #undef CHECKSUM_STRING
6972 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6973 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6974 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6975 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6976 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6977 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6978 #define CHECKSUM_ATTR(FOO) \
6979 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6981 /* Calculate the checksum of a number in signed LEB128 format. */
6983 static void
6984 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6986 unsigned char byte;
6987 bool more;
6989 while (1)
6991 byte = (value & 0x7f);
6992 value >>= 7;
6993 more = !((value == 0 && (byte & 0x40) == 0)
6994 || (value == -1 && (byte & 0x40) != 0));
6995 if (more)
6996 byte |= 0x80;
6997 CHECKSUM (byte);
6998 if (!more)
6999 break;
7003 /* Calculate the checksum of a number in unsigned LEB128 format. */
7005 static void
7006 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
7008 while (1)
7010 unsigned char byte = (value & 0x7f);
7011 value >>= 7;
7012 if (value != 0)
7013 /* More bytes to follow. */
7014 byte |= 0x80;
7015 CHECKSUM (byte);
7016 if (value == 0)
7017 break;
7021 /* Checksum the context of the DIE. This adds the names of any
7022 surrounding namespaces or structures to the checksum. */
7024 static void
7025 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
7027 const char *name;
7028 dw_die_ref spec;
7029 int tag = die->die_tag;
7031 if (tag != DW_TAG_namespace
7032 && tag != DW_TAG_structure_type
7033 && tag != DW_TAG_class_type)
7034 return;
7036 name = get_AT_string (die, DW_AT_name);
7038 spec = get_AT_ref (die, DW_AT_specification);
7039 if (spec != NULL)
7040 die = spec;
7042 if (die->die_parent != NULL)
7043 checksum_die_context (die->die_parent, ctx);
7045 CHECKSUM_ULEB128 ('C');
7046 CHECKSUM_ULEB128 (tag);
7047 if (name != NULL)
7048 CHECKSUM_STRING (name);
7051 /* Calculate the checksum of a location expression. */
7053 static inline void
7054 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7056 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7057 were emitted as a DW_FORM_sdata instead of a location expression. */
7058 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7060 CHECKSUM_ULEB128 (DW_FORM_sdata);
7061 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7062 return;
7065 /* Otherwise, just checksum the raw location expression. */
7066 while (loc != NULL)
7068 inchash::hash hstate;
7069 hashval_t hash;
7071 CHECKSUM_ULEB128 (loc->dtprel);
7072 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7073 hash_loc_operands (loc, hstate);
7074 hash = hstate.end ();
7075 CHECKSUM (hash);
7076 loc = loc->dw_loc_next;
7080 /* Calculate the checksum of an attribute. */
7082 static void
7083 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7084 struct md5_ctx *ctx, int *mark)
7086 dw_loc_descr_ref loc;
7087 rtx r;
7089 if (AT_class (at) == dw_val_class_die_ref)
7091 dw_die_ref target_die = AT_ref (at);
7093 /* For pointer and reference types, we checksum only the (qualified)
7094 name of the target type (if there is a name). For friend entries,
7095 we checksum only the (qualified) name of the target type or function.
7096 This allows the checksum to remain the same whether the target type
7097 is complete or not. */
7098 if ((at->dw_attr == DW_AT_type
7099 && (tag == DW_TAG_pointer_type
7100 || tag == DW_TAG_reference_type
7101 || tag == DW_TAG_rvalue_reference_type
7102 || tag == DW_TAG_ptr_to_member_type))
7103 || (at->dw_attr == DW_AT_friend
7104 && tag == DW_TAG_friend))
7106 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7108 if (name_attr != NULL)
7110 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7112 if (decl == NULL)
7113 decl = target_die;
7114 CHECKSUM_ULEB128 ('N');
7115 CHECKSUM_ULEB128 (at->dw_attr);
7116 if (decl->die_parent != NULL)
7117 checksum_die_context (decl->die_parent, ctx);
7118 CHECKSUM_ULEB128 ('E');
7119 CHECKSUM_STRING (AT_string (name_attr));
7120 return;
7124 /* For all other references to another DIE, we check to see if the
7125 target DIE has already been visited. If it has, we emit a
7126 backward reference; if not, we descend recursively. */
7127 if (target_die->die_mark > 0)
7129 CHECKSUM_ULEB128 ('R');
7130 CHECKSUM_ULEB128 (at->dw_attr);
7131 CHECKSUM_ULEB128 (target_die->die_mark);
7133 else
7135 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7137 if (decl == NULL)
7138 decl = target_die;
7139 target_die->die_mark = ++(*mark);
7140 CHECKSUM_ULEB128 ('T');
7141 CHECKSUM_ULEB128 (at->dw_attr);
7142 if (decl->die_parent != NULL)
7143 checksum_die_context (decl->die_parent, ctx);
7144 die_checksum_ordered (target_die, ctx, mark);
7146 return;
7149 CHECKSUM_ULEB128 ('A');
7150 CHECKSUM_ULEB128 (at->dw_attr);
7152 switch (AT_class (at))
7154 case dw_val_class_const:
7155 case dw_val_class_const_implicit:
7156 CHECKSUM_ULEB128 (DW_FORM_sdata);
7157 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7158 break;
7160 case dw_val_class_unsigned_const:
7161 case dw_val_class_unsigned_const_implicit:
7162 CHECKSUM_ULEB128 (DW_FORM_sdata);
7163 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7164 break;
7166 case dw_val_class_const_double:
7167 CHECKSUM_ULEB128 (DW_FORM_block);
7168 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7169 CHECKSUM (at->dw_attr_val.v.val_double);
7170 break;
7172 case dw_val_class_wide_int:
7173 CHECKSUM_ULEB128 (DW_FORM_block);
7174 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7175 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7176 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7177 get_full_len (*at->dw_attr_val.v.val_wide)
7178 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7179 break;
7181 case dw_val_class_vec:
7182 CHECKSUM_ULEB128 (DW_FORM_block);
7183 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7184 * at->dw_attr_val.v.val_vec.elt_size);
7185 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7186 (at->dw_attr_val.v.val_vec.length
7187 * at->dw_attr_val.v.val_vec.elt_size));
7188 break;
7190 case dw_val_class_flag:
7191 CHECKSUM_ULEB128 (DW_FORM_flag);
7192 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7193 break;
7195 case dw_val_class_str:
7196 CHECKSUM_ULEB128 (DW_FORM_string);
7197 CHECKSUM_STRING (AT_string (at));
7198 break;
7200 case dw_val_class_addr:
7201 r = AT_addr (at);
7202 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7203 CHECKSUM_ULEB128 (DW_FORM_string);
7204 CHECKSUM_STRING (XSTR (r, 0));
7205 break;
7207 case dw_val_class_offset:
7208 CHECKSUM_ULEB128 (DW_FORM_sdata);
7209 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7210 break;
7212 case dw_val_class_loc:
7213 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7214 loc_checksum_ordered (loc, ctx);
7215 break;
7217 case dw_val_class_fde_ref:
7218 case dw_val_class_symview:
7219 case dw_val_class_lbl_id:
7220 case dw_val_class_lineptr:
7221 case dw_val_class_macptr:
7222 case dw_val_class_loclistsptr:
7223 case dw_val_class_high_pc:
7224 break;
7226 case dw_val_class_file:
7227 case dw_val_class_file_implicit:
7228 CHECKSUM_ULEB128 (DW_FORM_string);
7229 CHECKSUM_STRING (AT_file (at)->filename);
7230 break;
7232 case dw_val_class_data8:
7233 CHECKSUM (at->dw_attr_val.v.val_data8);
7234 break;
7236 default:
7237 break;
7241 struct checksum_attributes
7243 dw_attr_node *at_name;
7244 dw_attr_node *at_type;
7245 dw_attr_node *at_friend;
7246 dw_attr_node *at_accessibility;
7247 dw_attr_node *at_address_class;
7248 dw_attr_node *at_alignment;
7249 dw_attr_node *at_allocated;
7250 dw_attr_node *at_artificial;
7251 dw_attr_node *at_associated;
7252 dw_attr_node *at_binary_scale;
7253 dw_attr_node *at_bit_offset;
7254 dw_attr_node *at_bit_size;
7255 dw_attr_node *at_bit_stride;
7256 dw_attr_node *at_byte_size;
7257 dw_attr_node *at_byte_stride;
7258 dw_attr_node *at_const_value;
7259 dw_attr_node *at_containing_type;
7260 dw_attr_node *at_count;
7261 dw_attr_node *at_data_location;
7262 dw_attr_node *at_data_member_location;
7263 dw_attr_node *at_decimal_scale;
7264 dw_attr_node *at_decimal_sign;
7265 dw_attr_node *at_default_value;
7266 dw_attr_node *at_digit_count;
7267 dw_attr_node *at_discr;
7268 dw_attr_node *at_discr_list;
7269 dw_attr_node *at_discr_value;
7270 dw_attr_node *at_encoding;
7271 dw_attr_node *at_endianity;
7272 dw_attr_node *at_explicit;
7273 dw_attr_node *at_is_optional;
7274 dw_attr_node *at_location;
7275 dw_attr_node *at_lower_bound;
7276 dw_attr_node *at_mutable;
7277 dw_attr_node *at_ordering;
7278 dw_attr_node *at_picture_string;
7279 dw_attr_node *at_prototyped;
7280 dw_attr_node *at_small;
7281 dw_attr_node *at_segment;
7282 dw_attr_node *at_string_length;
7283 dw_attr_node *at_string_length_bit_size;
7284 dw_attr_node *at_string_length_byte_size;
7285 dw_attr_node *at_threads_scaled;
7286 dw_attr_node *at_upper_bound;
7287 dw_attr_node *at_use_location;
7288 dw_attr_node *at_use_UTF8;
7289 dw_attr_node *at_variable_parameter;
7290 dw_attr_node *at_virtuality;
7291 dw_attr_node *at_visibility;
7292 dw_attr_node *at_vtable_elem_location;
7295 /* Collect the attributes that we will want to use for the checksum. */
7297 static void
7298 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7300 dw_attr_node *a;
7301 unsigned ix;
7303 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7305 switch (a->dw_attr)
7307 case DW_AT_name:
7308 attrs->at_name = a;
7309 break;
7310 case DW_AT_type:
7311 attrs->at_type = a;
7312 break;
7313 case DW_AT_friend:
7314 attrs->at_friend = a;
7315 break;
7316 case DW_AT_accessibility:
7317 attrs->at_accessibility = a;
7318 break;
7319 case DW_AT_address_class:
7320 attrs->at_address_class = a;
7321 break;
7322 case DW_AT_alignment:
7323 attrs->at_alignment = a;
7324 break;
7325 case DW_AT_allocated:
7326 attrs->at_allocated = a;
7327 break;
7328 case DW_AT_artificial:
7329 attrs->at_artificial = a;
7330 break;
7331 case DW_AT_associated:
7332 attrs->at_associated = a;
7333 break;
7334 case DW_AT_binary_scale:
7335 attrs->at_binary_scale = a;
7336 break;
7337 case DW_AT_bit_offset:
7338 attrs->at_bit_offset = a;
7339 break;
7340 case DW_AT_bit_size:
7341 attrs->at_bit_size = a;
7342 break;
7343 case DW_AT_bit_stride:
7344 attrs->at_bit_stride = a;
7345 break;
7346 case DW_AT_byte_size:
7347 attrs->at_byte_size = a;
7348 break;
7349 case DW_AT_byte_stride:
7350 attrs->at_byte_stride = a;
7351 break;
7352 case DW_AT_const_value:
7353 attrs->at_const_value = a;
7354 break;
7355 case DW_AT_containing_type:
7356 attrs->at_containing_type = a;
7357 break;
7358 case DW_AT_count:
7359 attrs->at_count = a;
7360 break;
7361 case DW_AT_data_location:
7362 attrs->at_data_location = a;
7363 break;
7364 case DW_AT_data_member_location:
7365 attrs->at_data_member_location = a;
7366 break;
7367 case DW_AT_decimal_scale:
7368 attrs->at_decimal_scale = a;
7369 break;
7370 case DW_AT_decimal_sign:
7371 attrs->at_decimal_sign = a;
7372 break;
7373 case DW_AT_default_value:
7374 attrs->at_default_value = a;
7375 break;
7376 case DW_AT_digit_count:
7377 attrs->at_digit_count = a;
7378 break;
7379 case DW_AT_discr:
7380 attrs->at_discr = a;
7381 break;
7382 case DW_AT_discr_list:
7383 attrs->at_discr_list = a;
7384 break;
7385 case DW_AT_discr_value:
7386 attrs->at_discr_value = a;
7387 break;
7388 case DW_AT_encoding:
7389 attrs->at_encoding = a;
7390 break;
7391 case DW_AT_endianity:
7392 attrs->at_endianity = a;
7393 break;
7394 case DW_AT_explicit:
7395 attrs->at_explicit = a;
7396 break;
7397 case DW_AT_is_optional:
7398 attrs->at_is_optional = a;
7399 break;
7400 case DW_AT_location:
7401 attrs->at_location = a;
7402 break;
7403 case DW_AT_lower_bound:
7404 attrs->at_lower_bound = a;
7405 break;
7406 case DW_AT_mutable:
7407 attrs->at_mutable = a;
7408 break;
7409 case DW_AT_ordering:
7410 attrs->at_ordering = a;
7411 break;
7412 case DW_AT_picture_string:
7413 attrs->at_picture_string = a;
7414 break;
7415 case DW_AT_prototyped:
7416 attrs->at_prototyped = a;
7417 break;
7418 case DW_AT_small:
7419 attrs->at_small = a;
7420 break;
7421 case DW_AT_segment:
7422 attrs->at_segment = a;
7423 break;
7424 case DW_AT_string_length:
7425 attrs->at_string_length = a;
7426 break;
7427 case DW_AT_string_length_bit_size:
7428 attrs->at_string_length_bit_size = a;
7429 break;
7430 case DW_AT_string_length_byte_size:
7431 attrs->at_string_length_byte_size = a;
7432 break;
7433 case DW_AT_threads_scaled:
7434 attrs->at_threads_scaled = a;
7435 break;
7436 case DW_AT_upper_bound:
7437 attrs->at_upper_bound = a;
7438 break;
7439 case DW_AT_use_location:
7440 attrs->at_use_location = a;
7441 break;
7442 case DW_AT_use_UTF8:
7443 attrs->at_use_UTF8 = a;
7444 break;
7445 case DW_AT_variable_parameter:
7446 attrs->at_variable_parameter = a;
7447 break;
7448 case DW_AT_virtuality:
7449 attrs->at_virtuality = a;
7450 break;
7451 case DW_AT_visibility:
7452 attrs->at_visibility = a;
7453 break;
7454 case DW_AT_vtable_elem_location:
7455 attrs->at_vtable_elem_location = a;
7456 break;
7457 default:
7458 break;
7463 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7465 static void
7466 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7468 dw_die_ref c;
7469 dw_die_ref decl;
7470 struct checksum_attributes attrs;
7472 CHECKSUM_ULEB128 ('D');
7473 CHECKSUM_ULEB128 (die->die_tag);
7475 memset (&attrs, 0, sizeof (attrs));
7477 decl = get_AT_ref (die, DW_AT_specification);
7478 if (decl != NULL)
7479 collect_checksum_attributes (&attrs, decl);
7480 collect_checksum_attributes (&attrs, die);
7482 CHECKSUM_ATTR (attrs.at_name);
7483 CHECKSUM_ATTR (attrs.at_accessibility);
7484 CHECKSUM_ATTR (attrs.at_address_class);
7485 CHECKSUM_ATTR (attrs.at_allocated);
7486 CHECKSUM_ATTR (attrs.at_artificial);
7487 CHECKSUM_ATTR (attrs.at_associated);
7488 CHECKSUM_ATTR (attrs.at_binary_scale);
7489 CHECKSUM_ATTR (attrs.at_bit_offset);
7490 CHECKSUM_ATTR (attrs.at_bit_size);
7491 CHECKSUM_ATTR (attrs.at_bit_stride);
7492 CHECKSUM_ATTR (attrs.at_byte_size);
7493 CHECKSUM_ATTR (attrs.at_byte_stride);
7494 CHECKSUM_ATTR (attrs.at_const_value);
7495 CHECKSUM_ATTR (attrs.at_containing_type);
7496 CHECKSUM_ATTR (attrs.at_count);
7497 CHECKSUM_ATTR (attrs.at_data_location);
7498 CHECKSUM_ATTR (attrs.at_data_member_location);
7499 CHECKSUM_ATTR (attrs.at_decimal_scale);
7500 CHECKSUM_ATTR (attrs.at_decimal_sign);
7501 CHECKSUM_ATTR (attrs.at_default_value);
7502 CHECKSUM_ATTR (attrs.at_digit_count);
7503 CHECKSUM_ATTR (attrs.at_discr);
7504 CHECKSUM_ATTR (attrs.at_discr_list);
7505 CHECKSUM_ATTR (attrs.at_discr_value);
7506 CHECKSUM_ATTR (attrs.at_encoding);
7507 CHECKSUM_ATTR (attrs.at_endianity);
7508 CHECKSUM_ATTR (attrs.at_explicit);
7509 CHECKSUM_ATTR (attrs.at_is_optional);
7510 CHECKSUM_ATTR (attrs.at_location);
7511 CHECKSUM_ATTR (attrs.at_lower_bound);
7512 CHECKSUM_ATTR (attrs.at_mutable);
7513 CHECKSUM_ATTR (attrs.at_ordering);
7514 CHECKSUM_ATTR (attrs.at_picture_string);
7515 CHECKSUM_ATTR (attrs.at_prototyped);
7516 CHECKSUM_ATTR (attrs.at_small);
7517 CHECKSUM_ATTR (attrs.at_segment);
7518 CHECKSUM_ATTR (attrs.at_string_length);
7519 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7520 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7521 CHECKSUM_ATTR (attrs.at_threads_scaled);
7522 CHECKSUM_ATTR (attrs.at_upper_bound);
7523 CHECKSUM_ATTR (attrs.at_use_location);
7524 CHECKSUM_ATTR (attrs.at_use_UTF8);
7525 CHECKSUM_ATTR (attrs.at_variable_parameter);
7526 CHECKSUM_ATTR (attrs.at_virtuality);
7527 CHECKSUM_ATTR (attrs.at_visibility);
7528 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7529 CHECKSUM_ATTR (attrs.at_type);
7530 CHECKSUM_ATTR (attrs.at_friend);
7531 CHECKSUM_ATTR (attrs.at_alignment);
7533 /* Checksum the child DIEs. */
7534 c = die->die_child;
7535 if (c) do {
7536 dw_attr_node *name_attr;
7538 c = c->die_sib;
7539 name_attr = get_AT (c, DW_AT_name);
7540 if (is_template_instantiation (c))
7542 /* Ignore instantiations of member type and function templates. */
7544 else if (name_attr != NULL
7545 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7547 /* Use a shallow checksum for named nested types and member
7548 functions. */
7549 CHECKSUM_ULEB128 ('S');
7550 CHECKSUM_ULEB128 (c->die_tag);
7551 CHECKSUM_STRING (AT_string (name_attr));
7553 else
7555 /* Use a deep checksum for other children. */
7556 /* Mark this DIE so it gets processed when unmarking. */
7557 if (c->die_mark == 0)
7558 c->die_mark = -1;
7559 die_checksum_ordered (c, ctx, mark);
7561 } while (c != die->die_child);
7563 CHECKSUM_ULEB128 (0);
7566 /* Add a type name and tag to a hash. */
7567 static void
7568 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7570 CHECKSUM_ULEB128 (tag);
7571 CHECKSUM_STRING (name);
7574 #undef CHECKSUM
7575 #undef CHECKSUM_STRING
7576 #undef CHECKSUM_ATTR
7577 #undef CHECKSUM_LEB128
7578 #undef CHECKSUM_ULEB128
7580 /* Generate the type signature for DIE. This is computed by generating an
7581 MD5 checksum over the DIE's tag, its relevant attributes, and its
7582 children. Attributes that are references to other DIEs are processed
7583 by recursion, using the MARK field to prevent infinite recursion.
7584 If the DIE is nested inside a namespace or another type, we also
7585 need to include that context in the signature. The lower 64 bits
7586 of the resulting MD5 checksum comprise the signature. */
7588 static void
7589 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7591 int mark;
7592 const char *name;
7593 unsigned char checksum[16];
7594 struct md5_ctx ctx;
7595 dw_die_ref decl;
7596 dw_die_ref parent;
7598 name = get_AT_string (die, DW_AT_name);
7599 decl = get_AT_ref (die, DW_AT_specification);
7600 parent = get_die_parent (die);
7602 /* First, compute a signature for just the type name (and its surrounding
7603 context, if any. This is stored in the type unit DIE for link-time
7604 ODR (one-definition rule) checking. */
7606 if (is_cxx () && name != NULL)
7608 md5_init_ctx (&ctx);
7610 /* Checksum the names of surrounding namespaces and structures. */
7611 if (parent != NULL)
7612 checksum_die_context (parent, &ctx);
7614 /* Checksum the current DIE. */
7615 die_odr_checksum (die->die_tag, name, &ctx);
7616 md5_finish_ctx (&ctx, checksum);
7618 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7621 /* Next, compute the complete type signature. */
7623 md5_init_ctx (&ctx);
7624 mark = 1;
7625 die->die_mark = mark;
7627 /* Checksum the names of surrounding namespaces and structures. */
7628 if (parent != NULL)
7629 checksum_die_context (parent, &ctx);
7631 /* Checksum the DIE and its children. */
7632 die_checksum_ordered (die, &ctx, &mark);
7633 unmark_all_dies (die);
7634 md5_finish_ctx (&ctx, checksum);
7636 /* Store the signature in the type node and link the type DIE and the
7637 type node together. */
7638 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7639 DWARF_TYPE_SIGNATURE_SIZE);
7640 die->comdat_type_p = true;
7641 die->die_id.die_type_node = type_node;
7642 type_node->type_die = die;
7644 /* If the DIE is a specification, link its declaration to the type node
7645 as well. */
7646 if (decl != NULL)
7648 decl->comdat_type_p = true;
7649 decl->die_id.die_type_node = type_node;
7653 /* Do the location expressions look same? */
7654 static inline int
7655 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7657 return loc1->dw_loc_opc == loc2->dw_loc_opc
7658 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7659 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7662 /* Do the values look the same? */
7663 static int
7664 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7666 dw_loc_descr_ref loc1, loc2;
7667 rtx r1, r2;
7669 if (v1->val_class != v2->val_class)
7670 return 0;
7672 switch (v1->val_class)
7674 case dw_val_class_const:
7675 case dw_val_class_const_implicit:
7676 return v1->v.val_int == v2->v.val_int;
7677 case dw_val_class_unsigned_const:
7678 case dw_val_class_unsigned_const_implicit:
7679 return v1->v.val_unsigned == v2->v.val_unsigned;
7680 case dw_val_class_const_double:
7681 return v1->v.val_double.high == v2->v.val_double.high
7682 && v1->v.val_double.low == v2->v.val_double.low;
7683 case dw_val_class_wide_int:
7684 return *v1->v.val_wide == *v2->v.val_wide;
7685 case dw_val_class_vec:
7686 if (v1->v.val_vec.length != v2->v.val_vec.length
7687 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7688 return 0;
7689 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7690 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7691 return 0;
7692 return 1;
7693 case dw_val_class_flag:
7694 return v1->v.val_flag == v2->v.val_flag;
7695 case dw_val_class_str:
7696 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7698 case dw_val_class_addr:
7699 r1 = v1->v.val_addr;
7700 r2 = v2->v.val_addr;
7701 if (GET_CODE (r1) != GET_CODE (r2))
7702 return 0;
7703 return !rtx_equal_p (r1, r2);
7705 case dw_val_class_offset:
7706 return v1->v.val_offset == v2->v.val_offset;
7708 case dw_val_class_loc:
7709 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7710 loc1 && loc2;
7711 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7712 if (!same_loc_p (loc1, loc2, mark))
7713 return 0;
7714 return !loc1 && !loc2;
7716 case dw_val_class_die_ref:
7717 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7719 case dw_val_class_symview:
7720 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7722 case dw_val_class_fde_ref:
7723 case dw_val_class_vms_delta:
7724 case dw_val_class_lbl_id:
7725 case dw_val_class_lineptr:
7726 case dw_val_class_macptr:
7727 case dw_val_class_loclistsptr:
7728 case dw_val_class_high_pc:
7729 return 1;
7731 case dw_val_class_file:
7732 case dw_val_class_file_implicit:
7733 return v1->v.val_file == v2->v.val_file;
7735 case dw_val_class_data8:
7736 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7738 default:
7739 return 1;
7743 /* Do the attributes look the same? */
7745 static int
7746 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7748 if (at1->dw_attr != at2->dw_attr)
7749 return 0;
7751 /* We don't care that this was compiled with a different compiler
7752 snapshot; if the output is the same, that's what matters. */
7753 if (at1->dw_attr == DW_AT_producer)
7754 return 1;
7756 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7759 /* Do the dies look the same? */
7761 static int
7762 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7764 dw_die_ref c1, c2;
7765 dw_attr_node *a1;
7766 unsigned ix;
7768 /* To avoid infinite recursion. */
7769 if (die1->die_mark)
7770 return die1->die_mark == die2->die_mark;
7771 die1->die_mark = die2->die_mark = ++(*mark);
7773 if (die1->die_tag != die2->die_tag)
7774 return 0;
7776 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7777 return 0;
7779 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7780 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7781 return 0;
7783 c1 = die1->die_child;
7784 c2 = die2->die_child;
7785 if (! c1)
7787 if (c2)
7788 return 0;
7790 else
7791 for (;;)
7793 if (!same_die_p (c1, c2, mark))
7794 return 0;
7795 c1 = c1->die_sib;
7796 c2 = c2->die_sib;
7797 if (c1 == die1->die_child)
7799 if (c2 == die2->die_child)
7800 break;
7801 else
7802 return 0;
7806 return 1;
7809 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7810 children, and set die_symbol. */
7812 static void
7813 compute_comp_unit_symbol (dw_die_ref unit_die)
7815 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7816 const char *base = die_name ? lbasename (die_name) : "anonymous";
7817 char *name = XALLOCAVEC (char, strlen (base) + 64);
7818 char *p;
7819 int i, mark;
7820 unsigned char checksum[16];
7821 struct md5_ctx ctx;
7823 /* Compute the checksum of the DIE, then append part of it as hex digits to
7824 the name filename of the unit. */
7826 md5_init_ctx (&ctx);
7827 mark = 0;
7828 die_checksum (unit_die, &ctx, &mark);
7829 unmark_all_dies (unit_die);
7830 md5_finish_ctx (&ctx, checksum);
7832 /* When we this for comp_unit_die () we have a DW_AT_name that might
7833 not start with a letter but with anything valid for filenames and
7834 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7835 character is not a letter. */
7836 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7837 clean_symbol_name (name);
7839 p = name + strlen (name);
7840 for (i = 0; i < 4; i++)
7842 sprintf (p, "%.2x", checksum[i]);
7843 p += 2;
7846 unit_die->die_id.die_symbol = xstrdup (name);
7849 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7851 static int
7852 is_type_die (dw_die_ref die)
7854 switch (die->die_tag)
7856 case DW_TAG_array_type:
7857 case DW_TAG_class_type:
7858 case DW_TAG_interface_type:
7859 case DW_TAG_enumeration_type:
7860 case DW_TAG_pointer_type:
7861 case DW_TAG_reference_type:
7862 case DW_TAG_rvalue_reference_type:
7863 case DW_TAG_string_type:
7864 case DW_TAG_structure_type:
7865 case DW_TAG_subroutine_type:
7866 case DW_TAG_union_type:
7867 case DW_TAG_ptr_to_member_type:
7868 case DW_TAG_set_type:
7869 case DW_TAG_subrange_type:
7870 case DW_TAG_base_type:
7871 case DW_TAG_const_type:
7872 case DW_TAG_file_type:
7873 case DW_TAG_packed_type:
7874 case DW_TAG_volatile_type:
7875 case DW_TAG_typedef:
7876 return 1;
7877 default:
7878 return 0;
7882 /* Returns true iff C is a compile-unit DIE. */
7884 static inline bool
7885 is_cu_die (dw_die_ref c)
7887 return c && (c->die_tag == DW_TAG_compile_unit
7888 || c->die_tag == DW_TAG_skeleton_unit);
7891 /* Returns true iff C is a unit DIE of some sort. */
7893 static inline bool
7894 is_unit_die (dw_die_ref c)
7896 return c && (c->die_tag == DW_TAG_compile_unit
7897 || c->die_tag == DW_TAG_partial_unit
7898 || c->die_tag == DW_TAG_type_unit
7899 || c->die_tag == DW_TAG_skeleton_unit);
7902 /* Returns true iff C is a namespace DIE. */
7904 static inline bool
7905 is_namespace_die (dw_die_ref c)
7907 return c && c->die_tag == DW_TAG_namespace;
7910 /* Return non-zero if this DIE is a template parameter. */
7912 static inline bool
7913 is_template_parameter (dw_die_ref die)
7915 switch (die->die_tag)
7917 case DW_TAG_template_type_param:
7918 case DW_TAG_template_value_param:
7919 case DW_TAG_GNU_template_template_param:
7920 case DW_TAG_GNU_template_parameter_pack:
7921 return true;
7922 default:
7923 return false;
7927 /* Return non-zero if this DIE represents a template instantiation. */
7929 static inline bool
7930 is_template_instantiation (dw_die_ref die)
7932 dw_die_ref c;
7934 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7935 return false;
7936 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7937 return false;
7940 static char *
7941 gen_internal_sym (const char *prefix)
7943 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7945 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7946 return xstrdup (buf);
7949 /* Return non-zero if this DIE is a declaration. */
7951 static int
7952 is_declaration_die (dw_die_ref die)
7954 dw_attr_node *a;
7955 unsigned ix;
7957 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7958 if (a->dw_attr == DW_AT_declaration)
7959 return 1;
7961 return 0;
7964 /* Return non-zero if this DIE is nested inside a subprogram. */
7966 static int
7967 is_nested_in_subprogram (dw_die_ref die)
7969 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7971 if (decl == NULL)
7972 decl = die;
7973 return local_scope_p (decl);
7976 /* Return non-zero if this DIE contains a defining declaration of a
7977 subprogram. */
7979 static int
7980 contains_subprogram_definition (dw_die_ref die)
7982 dw_die_ref c;
7984 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7985 return 1;
7986 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7987 return 0;
7990 /* Return non-zero if this is a type DIE that should be moved to a
7991 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7992 unit type. */
7994 static int
7995 should_move_die_to_comdat (dw_die_ref die)
7997 switch (die->die_tag)
7999 case DW_TAG_class_type:
8000 case DW_TAG_structure_type:
8001 case DW_TAG_enumeration_type:
8002 case DW_TAG_union_type:
8003 /* Don't move declarations, inlined instances, types nested in a
8004 subprogram, or types that contain subprogram definitions. */
8005 if (is_declaration_die (die)
8006 || get_AT (die, DW_AT_abstract_origin)
8007 || is_nested_in_subprogram (die)
8008 || contains_subprogram_definition (die))
8009 return 0;
8010 return 1;
8011 case DW_TAG_array_type:
8012 case DW_TAG_interface_type:
8013 case DW_TAG_pointer_type:
8014 case DW_TAG_reference_type:
8015 case DW_TAG_rvalue_reference_type:
8016 case DW_TAG_string_type:
8017 case DW_TAG_subroutine_type:
8018 case DW_TAG_ptr_to_member_type:
8019 case DW_TAG_set_type:
8020 case DW_TAG_subrange_type:
8021 case DW_TAG_base_type:
8022 case DW_TAG_const_type:
8023 case DW_TAG_file_type:
8024 case DW_TAG_packed_type:
8025 case DW_TAG_volatile_type:
8026 case DW_TAG_typedef:
8027 default:
8028 return 0;
8032 /* Make a clone of DIE. */
8034 static dw_die_ref
8035 clone_die (dw_die_ref die)
8037 dw_die_ref clone = new_die_raw (die->die_tag);
8038 dw_attr_node *a;
8039 unsigned ix;
8041 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8042 add_dwarf_attr (clone, a);
8044 return clone;
8047 /* Make a clone of the tree rooted at DIE. */
8049 static dw_die_ref
8050 clone_tree (dw_die_ref die)
8052 dw_die_ref c;
8053 dw_die_ref clone = clone_die (die);
8055 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8057 return clone;
8060 /* Make a clone of DIE as a declaration. */
8062 static dw_die_ref
8063 clone_as_declaration (dw_die_ref die)
8065 dw_die_ref clone;
8066 dw_die_ref decl;
8067 dw_attr_node *a;
8068 unsigned ix;
8070 /* If the DIE is already a declaration, just clone it. */
8071 if (is_declaration_die (die))
8072 return clone_die (die);
8074 /* If the DIE is a specification, just clone its declaration DIE. */
8075 decl = get_AT_ref (die, DW_AT_specification);
8076 if (decl != NULL)
8078 clone = clone_die (decl);
8079 if (die->comdat_type_p)
8080 add_AT_die_ref (clone, DW_AT_signature, die);
8081 return clone;
8084 clone = new_die_raw (die->die_tag);
8086 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8088 /* We don't want to copy over all attributes.
8089 For example we don't want DW_AT_byte_size because otherwise we will no
8090 longer have a declaration and GDB will treat it as a definition. */
8092 switch (a->dw_attr)
8094 case DW_AT_abstract_origin:
8095 case DW_AT_artificial:
8096 case DW_AT_containing_type:
8097 case DW_AT_external:
8098 case DW_AT_name:
8099 case DW_AT_type:
8100 case DW_AT_virtuality:
8101 case DW_AT_linkage_name:
8102 case DW_AT_MIPS_linkage_name:
8103 add_dwarf_attr (clone, a);
8104 break;
8105 case DW_AT_byte_size:
8106 case DW_AT_alignment:
8107 default:
8108 break;
8112 if (die->comdat_type_p)
8113 add_AT_die_ref (clone, DW_AT_signature, die);
8115 add_AT_flag (clone, DW_AT_declaration, 1);
8116 return clone;
8120 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8122 struct decl_table_entry
8124 dw_die_ref orig;
8125 dw_die_ref copy;
8128 /* Helpers to manipulate hash table of copied declarations. */
8130 /* Hashtable helpers. */
8132 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8134 typedef die_struct *compare_type;
8135 static inline hashval_t hash (const decl_table_entry *);
8136 static inline bool equal (const decl_table_entry *, const die_struct *);
8139 inline hashval_t
8140 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8142 return htab_hash_pointer (entry->orig);
8145 inline bool
8146 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8147 const die_struct *entry2)
8149 return entry1->orig == entry2;
8152 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8154 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8155 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8156 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8157 to check if the ancestor has already been copied into UNIT. */
8159 static dw_die_ref
8160 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8161 decl_hash_type *decl_table)
8163 dw_die_ref parent = die->die_parent;
8164 dw_die_ref new_parent = unit;
8165 dw_die_ref copy;
8166 decl_table_entry **slot = NULL;
8167 struct decl_table_entry *entry = NULL;
8169 if (decl_table)
8171 /* Check if the entry has already been copied to UNIT. */
8172 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8173 INSERT);
8174 if (*slot != HTAB_EMPTY_ENTRY)
8176 entry = *slot;
8177 return entry->copy;
8180 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8181 entry = XCNEW (struct decl_table_entry);
8182 entry->orig = die;
8183 entry->copy = NULL;
8184 *slot = entry;
8187 if (parent != NULL)
8189 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8190 if (spec != NULL)
8191 parent = spec;
8192 if (!is_unit_die (parent))
8193 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8196 copy = clone_as_declaration (die);
8197 add_child_die (new_parent, copy);
8199 if (decl_table)
8201 /* Record the pointer to the copy. */
8202 entry->copy = copy;
8205 return copy;
8207 /* Copy the declaration context to the new type unit DIE. This includes
8208 any surrounding namespace or type declarations. If the DIE has an
8209 AT_specification attribute, it also includes attributes and children
8210 attached to the specification, and returns a pointer to the original
8211 parent of the declaration DIE. Returns NULL otherwise. */
8213 static dw_die_ref
8214 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8216 dw_die_ref decl;
8217 dw_die_ref new_decl;
8218 dw_die_ref orig_parent = NULL;
8220 decl = get_AT_ref (die, DW_AT_specification);
8221 if (decl == NULL)
8222 decl = die;
8223 else
8225 unsigned ix;
8226 dw_die_ref c;
8227 dw_attr_node *a;
8229 /* The original DIE will be changed to a declaration, and must
8230 be moved to be a child of the original declaration DIE. */
8231 orig_parent = decl->die_parent;
8233 /* Copy the type node pointer from the new DIE to the original
8234 declaration DIE so we can forward references later. */
8235 decl->comdat_type_p = true;
8236 decl->die_id.die_type_node = die->die_id.die_type_node;
8238 remove_AT (die, DW_AT_specification);
8240 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8242 if (a->dw_attr != DW_AT_name
8243 && a->dw_attr != DW_AT_declaration
8244 && a->dw_attr != DW_AT_external)
8245 add_dwarf_attr (die, a);
8248 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8251 if (decl->die_parent != NULL
8252 && !is_unit_die (decl->die_parent))
8254 new_decl = copy_ancestor_tree (unit, decl, NULL);
8255 if (new_decl != NULL)
8257 remove_AT (new_decl, DW_AT_signature);
8258 add_AT_specification (die, new_decl);
8262 return orig_parent;
8265 /* Generate the skeleton ancestor tree for the given NODE, then clone
8266 the DIE and add the clone into the tree. */
8268 static void
8269 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8271 if (node->new_die != NULL)
8272 return;
8274 node->new_die = clone_as_declaration (node->old_die);
8276 if (node->parent != NULL)
8278 generate_skeleton_ancestor_tree (node->parent);
8279 add_child_die (node->parent->new_die, node->new_die);
8283 /* Generate a skeleton tree of DIEs containing any declarations that are
8284 found in the original tree. We traverse the tree looking for declaration
8285 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8287 static void
8288 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8290 skeleton_chain_node node;
8291 dw_die_ref c;
8292 dw_die_ref first;
8293 dw_die_ref prev = NULL;
8294 dw_die_ref next = NULL;
8296 node.parent = parent;
8298 first = c = parent->old_die->die_child;
8299 if (c)
8300 next = c->die_sib;
8301 if (c) do {
8302 if (prev == NULL || prev->die_sib == c)
8303 prev = c;
8304 c = next;
8305 next = (c == first ? NULL : c->die_sib);
8306 node.old_die = c;
8307 node.new_die = NULL;
8308 if (is_declaration_die (c))
8310 if (is_template_instantiation (c))
8312 /* Instantiated templates do not need to be cloned into the
8313 type unit. Just move the DIE and its children back to
8314 the skeleton tree (in the main CU). */
8315 remove_child_with_prev (c, prev);
8316 add_child_die (parent->new_die, c);
8317 c = prev;
8319 else if (c->comdat_type_p)
8321 /* This is the skeleton of earlier break_out_comdat_types
8322 type. Clone the existing DIE, but keep the children
8323 under the original (which is in the main CU). */
8324 dw_die_ref clone = clone_die (c);
8326 replace_child (c, clone, prev);
8327 generate_skeleton_ancestor_tree (parent);
8328 add_child_die (parent->new_die, c);
8329 c = clone;
8330 continue;
8332 else
8334 /* Clone the existing DIE, move the original to the skeleton
8335 tree (which is in the main CU), and put the clone, with
8336 all the original's children, where the original came from
8337 (which is about to be moved to the type unit). */
8338 dw_die_ref clone = clone_die (c);
8339 move_all_children (c, clone);
8341 /* If the original has a DW_AT_object_pointer attribute,
8342 it would now point to a child DIE just moved to the
8343 cloned tree, so we need to remove that attribute from
8344 the original. */
8345 remove_AT (c, DW_AT_object_pointer);
8347 replace_child (c, clone, prev);
8348 generate_skeleton_ancestor_tree (parent);
8349 add_child_die (parent->new_die, c);
8350 node.old_die = clone;
8351 node.new_die = c;
8352 c = clone;
8355 generate_skeleton_bottom_up (&node);
8356 } while (next != NULL);
8359 /* Wrapper function for generate_skeleton_bottom_up. */
8361 static dw_die_ref
8362 generate_skeleton (dw_die_ref die)
8364 skeleton_chain_node node;
8366 node.old_die = die;
8367 node.new_die = NULL;
8368 node.parent = NULL;
8370 /* If this type definition is nested inside another type,
8371 and is not an instantiation of a template, always leave
8372 at least a declaration in its place. */
8373 if (die->die_parent != NULL
8374 && is_type_die (die->die_parent)
8375 && !is_template_instantiation (die))
8376 node.new_die = clone_as_declaration (die);
8378 generate_skeleton_bottom_up (&node);
8379 return node.new_die;
8382 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8383 declaration. The original DIE is moved to a new compile unit so that
8384 existing references to it follow it to the new location. If any of the
8385 original DIE's descendants is a declaration, we need to replace the
8386 original DIE with a skeleton tree and move the declarations back into the
8387 skeleton tree. */
8389 static dw_die_ref
8390 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8391 dw_die_ref prev)
8393 dw_die_ref skeleton, orig_parent;
8395 /* Copy the declaration context to the type unit DIE. If the returned
8396 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8397 that DIE. */
8398 orig_parent = copy_declaration_context (unit, child);
8400 skeleton = generate_skeleton (child);
8401 if (skeleton == NULL)
8402 remove_child_with_prev (child, prev);
8403 else
8405 skeleton->comdat_type_p = true;
8406 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8408 /* If the original DIE was a specification, we need to put
8409 the skeleton under the parent DIE of the declaration.
8410 This leaves the original declaration in the tree, but
8411 it will be pruned later since there are no longer any
8412 references to it. */
8413 if (orig_parent != NULL)
8415 remove_child_with_prev (child, prev);
8416 add_child_die (orig_parent, skeleton);
8418 else
8419 replace_child (child, skeleton, prev);
8422 return skeleton;
8425 static void
8426 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8427 comdat_type_node *type_node,
8428 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8430 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8431 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8432 DWARF procedure references in the DW_AT_location attribute. */
8434 static dw_die_ref
8435 copy_dwarf_procedure (dw_die_ref die,
8436 comdat_type_node *type_node,
8437 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8439 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8441 /* DWARF procedures are not supposed to have children... */
8442 gcc_assert (die->die_child == NULL);
8444 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8445 gcc_assert (vec_safe_length (die->die_attr) == 1
8446 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8448 /* Do not copy more than once DWARF procedures. */
8449 bool existed;
8450 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8451 if (existed)
8452 return die_copy;
8454 die_copy = clone_die (die);
8455 add_child_die (type_node->root_die, die_copy);
8456 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8457 return die_copy;
8460 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8461 procedures in DIE's attributes. */
8463 static void
8464 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8465 comdat_type_node *type_node,
8466 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8468 dw_attr_node *a;
8469 unsigned i;
8471 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8473 dw_loc_descr_ref loc;
8475 if (a->dw_attr_val.val_class != dw_val_class_loc)
8476 continue;
8478 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8480 switch (loc->dw_loc_opc)
8482 case DW_OP_call2:
8483 case DW_OP_call4:
8484 case DW_OP_call_ref:
8485 gcc_assert (loc->dw_loc_oprnd1.val_class
8486 == dw_val_class_die_ref);
8487 loc->dw_loc_oprnd1.v.val_die_ref.die
8488 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8489 type_node,
8490 copied_dwarf_procs);
8492 default:
8493 break;
8499 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8500 rewrite references to point to the copies.
8502 References are looked for in DIE's attributes and recursively in all its
8503 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8504 mapping from old DWARF procedures to their copy. It is used not to copy
8505 twice the same DWARF procedure under TYPE_NODE. */
8507 static void
8508 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8509 comdat_type_node *type_node,
8510 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8512 dw_die_ref c;
8514 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8515 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8516 type_node,
8517 copied_dwarf_procs));
8520 /* Traverse the DIE and set up additional .debug_types or .debug_info
8521 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8522 section. */
8524 static void
8525 break_out_comdat_types (dw_die_ref die)
8527 dw_die_ref c;
8528 dw_die_ref first;
8529 dw_die_ref prev = NULL;
8530 dw_die_ref next = NULL;
8531 dw_die_ref unit = NULL;
8533 first = c = die->die_child;
8534 if (c)
8535 next = c->die_sib;
8536 if (c) do {
8537 if (prev == NULL || prev->die_sib == c)
8538 prev = c;
8539 c = next;
8540 next = (c == first ? NULL : c->die_sib);
8541 if (should_move_die_to_comdat (c))
8543 dw_die_ref replacement;
8544 comdat_type_node *type_node;
8546 /* Break out nested types into their own type units. */
8547 break_out_comdat_types (c);
8549 /* Create a new type unit DIE as the root for the new tree, and
8550 add it to the list of comdat types. */
8551 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8552 add_AT_unsigned (unit, DW_AT_language,
8553 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8554 type_node = ggc_cleared_alloc<comdat_type_node> ();
8555 type_node->root_die = unit;
8556 type_node->next = comdat_type_list;
8557 comdat_type_list = type_node;
8559 /* Generate the type signature. */
8560 generate_type_signature (c, type_node);
8562 /* Copy the declaration context, attributes, and children of the
8563 declaration into the new type unit DIE, then remove this DIE
8564 from the main CU (or replace it with a skeleton if necessary). */
8565 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8566 type_node->skeleton_die = replacement;
8568 /* Add the DIE to the new compunit. */
8569 add_child_die (unit, c);
8571 /* Types can reference DWARF procedures for type size or data location
8572 expressions. Calls in DWARF expressions cannot target procedures
8573 that are not in the same section. So we must copy DWARF procedures
8574 along with this type and then rewrite references to them. */
8575 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8576 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8578 if (replacement != NULL)
8579 c = replacement;
8581 else if (c->die_tag == DW_TAG_namespace
8582 || c->die_tag == DW_TAG_class_type
8583 || c->die_tag == DW_TAG_structure_type
8584 || c->die_tag == DW_TAG_union_type)
8586 /* Look for nested types that can be broken out. */
8587 break_out_comdat_types (c);
8589 } while (next != NULL);
8592 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8593 Enter all the cloned children into the hash table decl_table. */
8595 static dw_die_ref
8596 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8598 dw_die_ref c;
8599 dw_die_ref clone;
8600 struct decl_table_entry *entry;
8601 decl_table_entry **slot;
8603 if (die->die_tag == DW_TAG_subprogram)
8604 clone = clone_as_declaration (die);
8605 else
8606 clone = clone_die (die);
8608 slot = decl_table->find_slot_with_hash (die,
8609 htab_hash_pointer (die), INSERT);
8611 /* Assert that DIE isn't in the hash table yet. If it would be there
8612 before, the ancestors would be necessarily there as well, therefore
8613 clone_tree_partial wouldn't be called. */
8614 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8616 entry = XCNEW (struct decl_table_entry);
8617 entry->orig = die;
8618 entry->copy = clone;
8619 *slot = entry;
8621 if (die->die_tag != DW_TAG_subprogram)
8622 FOR_EACH_CHILD (die, c,
8623 add_child_die (clone, clone_tree_partial (c, decl_table)));
8625 return clone;
8628 /* Walk the DIE and its children, looking for references to incomplete
8629 or trivial types that are unmarked (i.e., that are not in the current
8630 type_unit). */
8632 static void
8633 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8635 dw_die_ref c;
8636 dw_attr_node *a;
8637 unsigned ix;
8639 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8641 if (AT_class (a) == dw_val_class_die_ref)
8643 dw_die_ref targ = AT_ref (a);
8644 decl_table_entry **slot;
8645 struct decl_table_entry *entry;
8647 if (targ->die_mark != 0 || targ->comdat_type_p)
8648 continue;
8650 slot = decl_table->find_slot_with_hash (targ,
8651 htab_hash_pointer (targ),
8652 INSERT);
8654 if (*slot != HTAB_EMPTY_ENTRY)
8656 /* TARG has already been copied, so we just need to
8657 modify the reference to point to the copy. */
8658 entry = *slot;
8659 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8661 else
8663 dw_die_ref parent = unit;
8664 dw_die_ref copy = clone_die (targ);
8666 /* Record in DECL_TABLE that TARG has been copied.
8667 Need to do this now, before the recursive call,
8668 because DECL_TABLE may be expanded and SLOT
8669 would no longer be a valid pointer. */
8670 entry = XCNEW (struct decl_table_entry);
8671 entry->orig = targ;
8672 entry->copy = copy;
8673 *slot = entry;
8675 /* If TARG is not a declaration DIE, we need to copy its
8676 children. */
8677 if (!is_declaration_die (targ))
8679 FOR_EACH_CHILD (
8680 targ, c,
8681 add_child_die (copy,
8682 clone_tree_partial (c, decl_table)));
8685 /* Make sure the cloned tree is marked as part of the
8686 type unit. */
8687 mark_dies (copy);
8689 /* If TARG has surrounding context, copy its ancestor tree
8690 into the new type unit. */
8691 if (targ->die_parent != NULL
8692 && !is_unit_die (targ->die_parent))
8693 parent = copy_ancestor_tree (unit, targ->die_parent,
8694 decl_table);
8696 add_child_die (parent, copy);
8697 a->dw_attr_val.v.val_die_ref.die = copy;
8699 /* Make sure the newly-copied DIE is walked. If it was
8700 installed in a previously-added context, it won't
8701 get visited otherwise. */
8702 if (parent != unit)
8704 /* Find the highest point of the newly-added tree,
8705 mark each node along the way, and walk from there. */
8706 parent->die_mark = 1;
8707 while (parent->die_parent
8708 && parent->die_parent->die_mark == 0)
8710 parent = parent->die_parent;
8711 parent->die_mark = 1;
8713 copy_decls_walk (unit, parent, decl_table);
8719 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8722 /* Copy declarations for "unworthy" types into the new comdat section.
8723 Incomplete types, modified types, and certain other types aren't broken
8724 out into comdat sections of their own, so they don't have a signature,
8725 and we need to copy the declaration into the same section so that we
8726 don't have an external reference. */
8728 static void
8729 copy_decls_for_unworthy_types (dw_die_ref unit)
8731 mark_dies (unit);
8732 decl_hash_type decl_table (10);
8733 copy_decls_walk (unit, unit, &decl_table);
8734 unmark_dies (unit);
8737 /* Traverse the DIE and add a sibling attribute if it may have the
8738 effect of speeding up access to siblings. To save some space,
8739 avoid generating sibling attributes for DIE's without children. */
8741 static void
8742 add_sibling_attributes (dw_die_ref die)
8744 dw_die_ref c;
8746 if (! die->die_child)
8747 return;
8749 if (die->die_parent && die != die->die_parent->die_child)
8750 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8752 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8755 /* Output all location lists for the DIE and its children. */
8757 static void
8758 output_location_lists (dw_die_ref die)
8760 dw_die_ref c;
8761 dw_attr_node *a;
8762 unsigned ix;
8764 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8765 if (AT_class (a) == dw_val_class_loc_list)
8766 output_loc_list (AT_loc_list (a));
8768 FOR_EACH_CHILD (die, c, output_location_lists (c));
8771 /* During assign_location_list_indexes and output_loclists_offset the
8772 current index, after it the number of assigned indexes (i.e. how
8773 large the .debug_loclists* offset table should be). */
8774 static unsigned int loc_list_idx;
8776 /* Output all location list offsets for the DIE and its children. */
8778 static void
8779 output_loclists_offsets (dw_die_ref die)
8781 dw_die_ref c;
8782 dw_attr_node *a;
8783 unsigned ix;
8785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8786 if (AT_class (a) == dw_val_class_loc_list)
8788 dw_loc_list_ref l = AT_loc_list (a);
8789 if (l->offset_emitted)
8790 continue;
8791 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8792 loc_section_label, NULL);
8793 gcc_assert (l->hash == loc_list_idx);
8794 loc_list_idx++;
8795 l->offset_emitted = true;
8798 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8801 /* Recursively set indexes of location lists. */
8803 static void
8804 assign_location_list_indexes (dw_die_ref die)
8806 dw_die_ref c;
8807 dw_attr_node *a;
8808 unsigned ix;
8810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8811 if (AT_class (a) == dw_val_class_loc_list)
8813 dw_loc_list_ref list = AT_loc_list (a);
8814 if (!list->num_assigned)
8816 list->num_assigned = true;
8817 list->hash = loc_list_idx++;
8821 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8824 /* We want to limit the number of external references, because they are
8825 larger than local references: a relocation takes multiple words, and
8826 even a sig8 reference is always eight bytes, whereas a local reference
8827 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8828 So if we encounter multiple external references to the same type DIE, we
8829 make a local typedef stub for it and redirect all references there.
8831 This is the element of the hash table for keeping track of these
8832 references. */
8834 struct external_ref
8836 dw_die_ref type;
8837 dw_die_ref stub;
8838 unsigned n_refs;
8841 /* Hashtable helpers. */
8843 struct external_ref_hasher : free_ptr_hash <external_ref>
8845 static inline hashval_t hash (const external_ref *);
8846 static inline bool equal (const external_ref *, const external_ref *);
8849 inline hashval_t
8850 external_ref_hasher::hash (const external_ref *r)
8852 dw_die_ref die = r->type;
8853 hashval_t h = 0;
8855 /* We can't use the address of the DIE for hashing, because
8856 that will make the order of the stub DIEs non-deterministic. */
8857 if (! die->comdat_type_p)
8858 /* We have a symbol; use it to compute a hash. */
8859 h = htab_hash_string (die->die_id.die_symbol);
8860 else
8862 /* We have a type signature; use a subset of the bits as the hash.
8863 The 8-byte signature is at least as large as hashval_t. */
8864 comdat_type_node *type_node = die->die_id.die_type_node;
8865 memcpy (&h, type_node->signature, sizeof (h));
8867 return h;
8870 inline bool
8871 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8873 return r1->type == r2->type;
8876 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8878 /* Return a pointer to the external_ref for references to DIE. */
8880 static struct external_ref *
8881 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8883 struct external_ref ref, *ref_p;
8884 external_ref **slot;
8886 ref.type = die;
8887 slot = map->find_slot (&ref, INSERT);
8888 if (*slot != HTAB_EMPTY_ENTRY)
8889 return *slot;
8891 ref_p = XCNEW (struct external_ref);
8892 ref_p->type = die;
8893 *slot = ref_p;
8894 return ref_p;
8897 /* Subroutine of optimize_external_refs, below.
8899 If we see a type skeleton, record it as our stub. If we see external
8900 references, remember how many we've seen. */
8902 static void
8903 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8905 dw_die_ref c;
8906 dw_attr_node *a;
8907 unsigned ix;
8908 struct external_ref *ref_p;
8910 if (is_type_die (die)
8911 && (c = get_AT_ref (die, DW_AT_signature)))
8913 /* This is a local skeleton; use it for local references. */
8914 ref_p = lookup_external_ref (map, c);
8915 ref_p->stub = die;
8918 /* Scan the DIE references, and remember any that refer to DIEs from
8919 other CUs (i.e. those which are not marked). */
8920 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8921 if (AT_class (a) == dw_val_class_die_ref
8922 && (c = AT_ref (a))->die_mark == 0
8923 && is_type_die (c))
8925 ref_p = lookup_external_ref (map, c);
8926 ref_p->n_refs++;
8929 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8932 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8933 points to an external_ref, DATA is the CU we're processing. If we don't
8934 already have a local stub, and we have multiple refs, build a stub. */
8937 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8939 struct external_ref *ref_p = *slot;
8941 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8943 /* We have multiple references to this type, so build a small stub.
8944 Both of these forms are a bit dodgy from the perspective of the
8945 DWARF standard, since technically they should have names. */
8946 dw_die_ref cu = data;
8947 dw_die_ref type = ref_p->type;
8948 dw_die_ref stub = NULL;
8950 if (type->comdat_type_p)
8952 /* If we refer to this type via sig8, use AT_signature. */
8953 stub = new_die (type->die_tag, cu, NULL_TREE);
8954 add_AT_die_ref (stub, DW_AT_signature, type);
8956 else
8958 /* Otherwise, use a typedef with no name. */
8959 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8960 add_AT_die_ref (stub, DW_AT_type, type);
8963 stub->die_mark++;
8964 ref_p->stub = stub;
8966 return 1;
8969 /* DIE is a unit; look through all the DIE references to see if there are
8970 any external references to types, and if so, create local stubs for
8971 them which will be applied in build_abbrev_table. This is useful because
8972 references to local DIEs are smaller. */
8974 static external_ref_hash_type *
8975 optimize_external_refs (dw_die_ref die)
8977 external_ref_hash_type *map = new external_ref_hash_type (10);
8978 optimize_external_refs_1 (die, map);
8979 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8980 return map;
8983 /* The following 3 variables are temporaries that are computed only during the
8984 build_abbrev_table call and used and released during the following
8985 optimize_abbrev_table call. */
8987 /* First abbrev_id that can be optimized based on usage. */
8988 static unsigned int abbrev_opt_start;
8990 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8991 abbrev_id smaller than this, because they must be already sized
8992 during build_abbrev_table). */
8993 static unsigned int abbrev_opt_base_type_end;
8995 /* Vector of usage counts during build_abbrev_table. Indexed by
8996 abbrev_id - abbrev_opt_start. */
8997 static vec<unsigned int> abbrev_usage_count;
8999 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
9000 static vec<dw_die_ref> sorted_abbrev_dies;
9002 /* The format of each DIE (and its attribute value pairs) is encoded in an
9003 abbreviation table. This routine builds the abbreviation table and assigns
9004 a unique abbreviation id for each abbreviation entry. The children of each
9005 die are visited recursively. */
9007 static void
9008 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9010 unsigned int abbrev_id = 0;
9011 dw_die_ref c;
9012 dw_attr_node *a;
9013 unsigned ix;
9014 dw_die_ref abbrev;
9016 /* Scan the DIE references, and replace any that refer to
9017 DIEs from other CUs (i.e. those which are not marked) with
9018 the local stubs we built in optimize_external_refs. */
9019 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9020 if (AT_class (a) == dw_val_class_die_ref
9021 && (c = AT_ref (a))->die_mark == 0)
9023 struct external_ref *ref_p;
9024 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9026 if (is_type_die (c)
9027 && (ref_p = lookup_external_ref (extern_map, c))
9028 && ref_p->stub && ref_p->stub != die)
9029 change_AT_die_ref (a, ref_p->stub);
9030 else
9031 /* We aren't changing this reference, so mark it external. */
9032 set_AT_ref_external (a, 1);
9035 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9037 dw_attr_node *die_a, *abbrev_a;
9038 unsigned ix;
9039 bool ok = true;
9041 if (abbrev_id == 0)
9042 continue;
9043 if (abbrev->die_tag != die->die_tag)
9044 continue;
9045 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9046 continue;
9048 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9049 continue;
9051 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9053 abbrev_a = &(*abbrev->die_attr)[ix];
9054 if ((abbrev_a->dw_attr != die_a->dw_attr)
9055 || (value_format (abbrev_a) != value_format (die_a)))
9057 ok = false;
9058 break;
9061 if (ok)
9062 break;
9065 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9067 vec_safe_push (abbrev_die_table, die);
9068 if (abbrev_opt_start)
9069 abbrev_usage_count.safe_push (0);
9071 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9073 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9074 sorted_abbrev_dies.safe_push (die);
9077 die->die_abbrev = abbrev_id;
9078 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9081 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9082 by die_abbrev's usage count, from the most commonly used
9083 abbreviation to the least. */
9085 static int
9086 die_abbrev_cmp (const void *p1, const void *p2)
9088 dw_die_ref die1 = *(const dw_die_ref *) p1;
9089 dw_die_ref die2 = *(const dw_die_ref *) p2;
9091 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9092 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9094 if (die1->die_abbrev >= abbrev_opt_base_type_end
9095 && die2->die_abbrev >= abbrev_opt_base_type_end)
9097 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9098 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9099 return -1;
9100 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9101 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9102 return 1;
9105 /* Stabilize the sort. */
9106 if (die1->die_abbrev < die2->die_abbrev)
9107 return -1;
9108 if (die1->die_abbrev > die2->die_abbrev)
9109 return 1;
9111 return 0;
9114 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9115 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9116 into dw_val_class_const_implicit or
9117 dw_val_class_unsigned_const_implicit. */
9119 static void
9120 optimize_implicit_const (unsigned int first_id, unsigned int end,
9121 vec<bool> &implicit_consts)
9123 /* It never makes sense if there is just one DIE using the abbreviation. */
9124 if (end < first_id + 2)
9125 return;
9127 dw_attr_node *a;
9128 unsigned ix, i;
9129 dw_die_ref die = sorted_abbrev_dies[first_id];
9130 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9131 if (implicit_consts[ix])
9133 enum dw_val_class new_class = dw_val_class_none;
9134 switch (AT_class (a))
9136 case dw_val_class_unsigned_const:
9137 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9138 continue;
9140 /* The .debug_abbrev section will grow by
9141 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9142 in all the DIEs using that abbreviation. */
9143 if (constant_size (AT_unsigned (a)) * (end - first_id)
9144 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9145 continue;
9147 new_class = dw_val_class_unsigned_const_implicit;
9148 break;
9150 case dw_val_class_const:
9151 new_class = dw_val_class_const_implicit;
9152 break;
9154 case dw_val_class_file:
9155 new_class = dw_val_class_file_implicit;
9156 break;
9158 default:
9159 continue;
9161 for (i = first_id; i < end; i++)
9162 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9163 = new_class;
9167 /* Attempt to optimize abbreviation table from abbrev_opt_start
9168 abbreviation above. */
9170 static void
9171 optimize_abbrev_table (void)
9173 if (abbrev_opt_start
9174 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9175 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9177 auto_vec<bool, 32> implicit_consts;
9178 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9180 unsigned int abbrev_id = abbrev_opt_start - 1;
9181 unsigned int first_id = ~0U;
9182 unsigned int last_abbrev_id = 0;
9183 unsigned int i;
9184 dw_die_ref die;
9185 if (abbrev_opt_base_type_end > abbrev_opt_start)
9186 abbrev_id = abbrev_opt_base_type_end - 1;
9187 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9188 most commonly used abbreviations come first. */
9189 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9191 dw_attr_node *a;
9192 unsigned ix;
9194 /* If calc_base_type_die_sizes has been called, the CU and
9195 base types after it can't be optimized, because we've already
9196 calculated their DIE offsets. We've sorted them first. */
9197 if (die->die_abbrev < abbrev_opt_base_type_end)
9198 continue;
9199 if (die->die_abbrev != last_abbrev_id)
9201 last_abbrev_id = die->die_abbrev;
9202 if (dwarf_version >= 5 && first_id != ~0U)
9203 optimize_implicit_const (first_id, i, implicit_consts);
9204 abbrev_id++;
9205 (*abbrev_die_table)[abbrev_id] = die;
9206 if (dwarf_version >= 5)
9208 first_id = i;
9209 implicit_consts.truncate (0);
9211 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9212 switch (AT_class (a))
9214 case dw_val_class_const:
9215 case dw_val_class_unsigned_const:
9216 case dw_val_class_file:
9217 implicit_consts.safe_push (true);
9218 break;
9219 default:
9220 implicit_consts.safe_push (false);
9221 break;
9225 else if (dwarf_version >= 5)
9227 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9228 if (!implicit_consts[ix])
9229 continue;
9230 else
9232 dw_attr_node *other_a
9233 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9234 if (!dw_val_equal_p (&a->dw_attr_val,
9235 &other_a->dw_attr_val))
9236 implicit_consts[ix] = false;
9239 die->die_abbrev = abbrev_id;
9241 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9242 if (dwarf_version >= 5 && first_id != ~0U)
9243 optimize_implicit_const (first_id, i, implicit_consts);
9246 abbrev_opt_start = 0;
9247 abbrev_opt_base_type_end = 0;
9248 abbrev_usage_count.release ();
9249 sorted_abbrev_dies.release ();
9252 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9254 static int
9255 constant_size (unsigned HOST_WIDE_INT value)
9257 int log;
9259 if (value == 0)
9260 log = 0;
9261 else
9262 log = floor_log2 (value);
9264 log = log / 8;
9265 log = 1 << (floor_log2 (log) + 1);
9267 return log;
9270 /* Return the size of a DIE as it is represented in the
9271 .debug_info section. */
9273 static unsigned long
9274 size_of_die (dw_die_ref die)
9276 unsigned long size = 0;
9277 dw_attr_node *a;
9278 unsigned ix;
9279 enum dwarf_form form;
9281 size += size_of_uleb128 (die->die_abbrev);
9282 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9284 switch (AT_class (a))
9286 case dw_val_class_addr:
9287 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9289 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9290 size += size_of_uleb128 (AT_index (a));
9292 else
9293 size += DWARF2_ADDR_SIZE;
9294 break;
9295 case dw_val_class_offset:
9296 size += DWARF_OFFSET_SIZE;
9297 break;
9298 case dw_val_class_loc:
9300 unsigned long lsize = size_of_locs (AT_loc (a));
9302 /* Block length. */
9303 if (dwarf_version >= 4)
9304 size += size_of_uleb128 (lsize);
9305 else
9306 size += constant_size (lsize);
9307 size += lsize;
9309 break;
9310 case dw_val_class_loc_list:
9311 case dw_val_class_view_list:
9312 if (dwarf_split_debug_info && dwarf_version >= 5)
9314 gcc_assert (AT_loc_list (a)->num_assigned);
9315 size += size_of_uleb128 (AT_loc_list (a)->hash);
9317 else
9318 size += DWARF_OFFSET_SIZE;
9319 break;
9320 case dw_val_class_range_list:
9321 if (value_format (a) == DW_FORM_rnglistx)
9323 gcc_assert (rnglist_idx);
9324 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9325 size += size_of_uleb128 (r->idx);
9327 else
9328 size += DWARF_OFFSET_SIZE;
9329 break;
9330 case dw_val_class_const:
9331 size += size_of_sleb128 (AT_int (a));
9332 break;
9333 case dw_val_class_unsigned_const:
9335 int csize = constant_size (AT_unsigned (a));
9336 if (dwarf_version == 3
9337 && a->dw_attr == DW_AT_data_member_location
9338 && csize >= 4)
9339 size += size_of_uleb128 (AT_unsigned (a));
9340 else
9341 size += csize;
9343 break;
9344 case dw_val_class_symview:
9345 if (symview_upper_bound <= 0xff)
9346 size += 1;
9347 else if (symview_upper_bound <= 0xffff)
9348 size += 2;
9349 else if (symview_upper_bound <= 0xffffffff)
9350 size += 4;
9351 else
9352 size += 8;
9353 break;
9354 case dw_val_class_const_implicit:
9355 case dw_val_class_unsigned_const_implicit:
9356 case dw_val_class_file_implicit:
9357 /* These occupy no size in the DIE, just an extra sleb128 in
9358 .debug_abbrev. */
9359 break;
9360 case dw_val_class_const_double:
9361 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9362 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9363 size++; /* block */
9364 break;
9365 case dw_val_class_wide_int:
9366 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9367 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9368 if (get_full_len (*a->dw_attr_val.v.val_wide)
9369 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9370 size++; /* block */
9371 break;
9372 case dw_val_class_vec:
9373 size += constant_size (a->dw_attr_val.v.val_vec.length
9374 * a->dw_attr_val.v.val_vec.elt_size)
9375 + a->dw_attr_val.v.val_vec.length
9376 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9377 break;
9378 case dw_val_class_flag:
9379 if (dwarf_version >= 4)
9380 /* Currently all add_AT_flag calls pass in 1 as last argument,
9381 so DW_FORM_flag_present can be used. If that ever changes,
9382 we'll need to use DW_FORM_flag and have some optimization
9383 in build_abbrev_table that will change those to
9384 DW_FORM_flag_present if it is set to 1 in all DIEs using
9385 the same abbrev entry. */
9386 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9387 else
9388 size += 1;
9389 break;
9390 case dw_val_class_die_ref:
9391 if (AT_ref_external (a))
9393 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9394 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9395 is sized by target address length, whereas in DWARF3
9396 it's always sized as an offset. */
9397 if (use_debug_types)
9398 size += DWARF_TYPE_SIGNATURE_SIZE;
9399 else if (dwarf_version == 2)
9400 size += DWARF2_ADDR_SIZE;
9401 else
9402 size += DWARF_OFFSET_SIZE;
9404 else
9405 size += DWARF_OFFSET_SIZE;
9406 break;
9407 case dw_val_class_fde_ref:
9408 size += DWARF_OFFSET_SIZE;
9409 break;
9410 case dw_val_class_lbl_id:
9411 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9413 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9414 size += size_of_uleb128 (AT_index (a));
9416 else
9417 size += DWARF2_ADDR_SIZE;
9418 break;
9419 case dw_val_class_lineptr:
9420 case dw_val_class_macptr:
9421 case dw_val_class_loclistsptr:
9422 size += DWARF_OFFSET_SIZE;
9423 break;
9424 case dw_val_class_str:
9425 form = AT_string_form (a);
9426 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9427 size += DWARF_OFFSET_SIZE;
9428 else if (form == dwarf_FORM (DW_FORM_strx))
9429 size += size_of_uleb128 (AT_index (a));
9430 else
9431 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9432 break;
9433 case dw_val_class_file:
9434 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9435 break;
9436 case dw_val_class_data8:
9437 size += 8;
9438 break;
9439 case dw_val_class_vms_delta:
9440 size += DWARF_OFFSET_SIZE;
9441 break;
9442 case dw_val_class_high_pc:
9443 size += DWARF2_ADDR_SIZE;
9444 break;
9445 case dw_val_class_discr_value:
9446 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9447 break;
9448 case dw_val_class_discr_list:
9450 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9452 /* This is a block, so we have the block length and then its
9453 data. */
9454 size += constant_size (block_size) + block_size;
9456 break;
9457 default:
9458 gcc_unreachable ();
9462 return size;
9465 /* Size the debugging information associated with a given DIE. Visits the
9466 DIE's children recursively. Updates the global variable next_die_offset, on
9467 each time through. Uses the current value of next_die_offset to update the
9468 die_offset field in each DIE. */
9470 static void
9471 calc_die_sizes (dw_die_ref die)
9473 dw_die_ref c;
9475 gcc_assert (die->die_offset == 0
9476 || (unsigned long int) die->die_offset == next_die_offset);
9477 die->die_offset = next_die_offset;
9478 next_die_offset += size_of_die (die);
9480 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9482 if (die->die_child != NULL)
9483 /* Count the null byte used to terminate sibling lists. */
9484 next_die_offset += 1;
9487 /* Size just the base type children at the start of the CU.
9488 This is needed because build_abbrev needs to size locs
9489 and sizing of type based stack ops needs to know die_offset
9490 values for the base types. */
9492 static void
9493 calc_base_type_die_sizes (void)
9495 unsigned long die_offset = (dwarf_split_debug_info
9496 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9497 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9498 unsigned int i;
9499 dw_die_ref base_type;
9500 #if ENABLE_ASSERT_CHECKING
9501 dw_die_ref prev = comp_unit_die ()->die_child;
9502 #endif
9504 die_offset += size_of_die (comp_unit_die ());
9505 for (i = 0; base_types.iterate (i, &base_type); i++)
9507 #if ENABLE_ASSERT_CHECKING
9508 gcc_assert (base_type->die_offset == 0
9509 && prev->die_sib == base_type
9510 && base_type->die_child == NULL
9511 && base_type->die_abbrev);
9512 prev = base_type;
9513 #endif
9514 if (abbrev_opt_start
9515 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9516 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9517 base_type->die_offset = die_offset;
9518 die_offset += size_of_die (base_type);
9522 /* Set the marks for a die and its children. We do this so
9523 that we know whether or not a reference needs to use FORM_ref_addr; only
9524 DIEs in the same CU will be marked. We used to clear out the offset
9525 and use that as the flag, but ran into ordering problems. */
9527 static void
9528 mark_dies (dw_die_ref die)
9530 dw_die_ref c;
9532 gcc_assert (!die->die_mark);
9534 die->die_mark = 1;
9535 FOR_EACH_CHILD (die, c, mark_dies (c));
9538 /* Clear the marks for a die and its children. */
9540 static void
9541 unmark_dies (dw_die_ref die)
9543 dw_die_ref c;
9545 if (! use_debug_types)
9546 gcc_assert (die->die_mark);
9548 die->die_mark = 0;
9549 FOR_EACH_CHILD (die, c, unmark_dies (c));
9552 /* Clear the marks for a die, its children and referred dies. */
9554 static void
9555 unmark_all_dies (dw_die_ref die)
9557 dw_die_ref c;
9558 dw_attr_node *a;
9559 unsigned ix;
9561 if (!die->die_mark)
9562 return;
9563 die->die_mark = 0;
9565 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9567 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9568 if (AT_class (a) == dw_val_class_die_ref)
9569 unmark_all_dies (AT_ref (a));
9572 /* Calculate if the entry should appear in the final output file. It may be
9573 from a pruned a type. */
9575 static bool
9576 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9578 /* By limiting gnu pubnames to definitions only, gold can generate a
9579 gdb index without entries for declarations, which don't include
9580 enough information to be useful. */
9581 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9582 return false;
9584 if (table == pubname_table)
9586 /* Enumerator names are part of the pubname table, but the
9587 parent DW_TAG_enumeration_type die may have been pruned.
9588 Don't output them if that is the case. */
9589 if (p->die->die_tag == DW_TAG_enumerator &&
9590 (p->die->die_parent == NULL
9591 || !p->die->die_parent->die_perennial_p))
9592 return false;
9594 /* Everything else in the pubname table is included. */
9595 return true;
9598 /* The pubtypes table shouldn't include types that have been
9599 pruned. */
9600 return (p->die->die_offset != 0
9601 || !flag_eliminate_unused_debug_types);
9604 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9605 generated for the compilation unit. */
9607 static unsigned long
9608 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9610 unsigned long size;
9611 unsigned i;
9612 pubname_entry *p;
9613 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9615 size = DWARF_PUBNAMES_HEADER_SIZE;
9616 FOR_EACH_VEC_ELT (*names, i, p)
9617 if (include_pubname_in_output (names, p))
9618 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9620 size += DWARF_OFFSET_SIZE;
9621 return size;
9624 /* Return the size of the information in the .debug_aranges section. */
9626 static unsigned long
9627 size_of_aranges (void)
9629 unsigned long size;
9631 size = DWARF_ARANGES_HEADER_SIZE;
9633 /* Count the address/length pair for this compilation unit. */
9634 if (text_section_used)
9635 size += 2 * DWARF2_ADDR_SIZE;
9636 if (cold_text_section_used)
9637 size += 2 * DWARF2_ADDR_SIZE;
9638 if (have_multiple_function_sections)
9640 unsigned fde_idx;
9641 dw_fde_ref fde;
9643 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9645 if (DECL_IGNORED_P (fde->decl))
9646 continue;
9647 if (!fde->in_std_section)
9648 size += 2 * DWARF2_ADDR_SIZE;
9649 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9650 size += 2 * DWARF2_ADDR_SIZE;
9654 /* Count the two zero words used to terminated the address range table. */
9655 size += 2 * DWARF2_ADDR_SIZE;
9656 return size;
9659 /* Select the encoding of an attribute value. */
9661 static enum dwarf_form
9662 value_format (dw_attr_node *a)
9664 switch (AT_class (a))
9666 case dw_val_class_addr:
9667 /* Only very few attributes allow DW_FORM_addr. */
9668 switch (a->dw_attr)
9670 case DW_AT_low_pc:
9671 case DW_AT_high_pc:
9672 case DW_AT_entry_pc:
9673 case DW_AT_trampoline:
9674 return (AT_index (a) == NOT_INDEXED
9675 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9676 default:
9677 break;
9679 switch (DWARF2_ADDR_SIZE)
9681 case 1:
9682 return DW_FORM_data1;
9683 case 2:
9684 return DW_FORM_data2;
9685 case 4:
9686 return DW_FORM_data4;
9687 case 8:
9688 return DW_FORM_data8;
9689 default:
9690 gcc_unreachable ();
9692 case dw_val_class_loc_list:
9693 case dw_val_class_view_list:
9694 if (dwarf_split_debug_info
9695 && dwarf_version >= 5
9696 && AT_loc_list (a)->num_assigned)
9697 return DW_FORM_loclistx;
9698 /* FALLTHRU */
9699 case dw_val_class_range_list:
9700 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9701 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9702 care about sizes of .debug* sections in shared libraries and
9703 executables and don't take into account relocations that affect just
9704 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9705 table in the .debug_rnglists section. */
9706 if (dwarf_split_debug_info
9707 && dwarf_version >= 5
9708 && AT_class (a) == dw_val_class_range_list
9709 && rnglist_idx
9710 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9711 return DW_FORM_rnglistx;
9712 if (dwarf_version >= 4)
9713 return DW_FORM_sec_offset;
9714 /* FALLTHRU */
9715 case dw_val_class_vms_delta:
9716 case dw_val_class_offset:
9717 switch (DWARF_OFFSET_SIZE)
9719 case 4:
9720 return DW_FORM_data4;
9721 case 8:
9722 return DW_FORM_data8;
9723 default:
9724 gcc_unreachable ();
9726 case dw_val_class_loc:
9727 if (dwarf_version >= 4)
9728 return DW_FORM_exprloc;
9729 switch (constant_size (size_of_locs (AT_loc (a))))
9731 case 1:
9732 return DW_FORM_block1;
9733 case 2:
9734 return DW_FORM_block2;
9735 case 4:
9736 return DW_FORM_block4;
9737 default:
9738 gcc_unreachable ();
9740 case dw_val_class_const:
9741 return DW_FORM_sdata;
9742 case dw_val_class_unsigned_const:
9743 switch (constant_size (AT_unsigned (a)))
9745 case 1:
9746 return DW_FORM_data1;
9747 case 2:
9748 return DW_FORM_data2;
9749 case 4:
9750 /* In DWARF3 DW_AT_data_member_location with
9751 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9752 constant, so we need to use DW_FORM_udata if we need
9753 a large constant. */
9754 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9755 return DW_FORM_udata;
9756 return DW_FORM_data4;
9757 case 8:
9758 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9759 return DW_FORM_udata;
9760 return DW_FORM_data8;
9761 default:
9762 gcc_unreachable ();
9764 case dw_val_class_const_implicit:
9765 case dw_val_class_unsigned_const_implicit:
9766 case dw_val_class_file_implicit:
9767 return DW_FORM_implicit_const;
9768 case dw_val_class_const_double:
9769 switch (HOST_BITS_PER_WIDE_INT)
9771 case 8:
9772 return DW_FORM_data2;
9773 case 16:
9774 return DW_FORM_data4;
9775 case 32:
9776 return DW_FORM_data8;
9777 case 64:
9778 if (dwarf_version >= 5)
9779 return DW_FORM_data16;
9780 /* FALLTHRU */
9781 default:
9782 return DW_FORM_block1;
9784 case dw_val_class_wide_int:
9785 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9787 case 8:
9788 return DW_FORM_data1;
9789 case 16:
9790 return DW_FORM_data2;
9791 case 32:
9792 return DW_FORM_data4;
9793 case 64:
9794 return DW_FORM_data8;
9795 case 128:
9796 if (dwarf_version >= 5)
9797 return DW_FORM_data16;
9798 /* FALLTHRU */
9799 default:
9800 return DW_FORM_block1;
9802 case dw_val_class_symview:
9803 /* ??? We might use uleb128, but then we'd have to compute
9804 .debug_info offsets in the assembler. */
9805 if (symview_upper_bound <= 0xff)
9806 return DW_FORM_data1;
9807 else if (symview_upper_bound <= 0xffff)
9808 return DW_FORM_data2;
9809 else if (symview_upper_bound <= 0xffffffff)
9810 return DW_FORM_data4;
9811 else
9812 return DW_FORM_data8;
9813 case dw_val_class_vec:
9814 switch (constant_size (a->dw_attr_val.v.val_vec.length
9815 * a->dw_attr_val.v.val_vec.elt_size))
9817 case 1:
9818 return DW_FORM_block1;
9819 case 2:
9820 return DW_FORM_block2;
9821 case 4:
9822 return DW_FORM_block4;
9823 default:
9824 gcc_unreachable ();
9826 case dw_val_class_flag:
9827 if (dwarf_version >= 4)
9829 /* Currently all add_AT_flag calls pass in 1 as last argument,
9830 so DW_FORM_flag_present can be used. If that ever changes,
9831 we'll need to use DW_FORM_flag and have some optimization
9832 in build_abbrev_table that will change those to
9833 DW_FORM_flag_present if it is set to 1 in all DIEs using
9834 the same abbrev entry. */
9835 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9836 return DW_FORM_flag_present;
9838 return DW_FORM_flag;
9839 case dw_val_class_die_ref:
9840 if (AT_ref_external (a))
9841 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9842 else
9843 return DW_FORM_ref;
9844 case dw_val_class_fde_ref:
9845 return DW_FORM_data;
9846 case dw_val_class_lbl_id:
9847 return (AT_index (a) == NOT_INDEXED
9848 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9849 case dw_val_class_lineptr:
9850 case dw_val_class_macptr:
9851 case dw_val_class_loclistsptr:
9852 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9853 case dw_val_class_str:
9854 return AT_string_form (a);
9855 case dw_val_class_file:
9856 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9858 case 1:
9859 return DW_FORM_data1;
9860 case 2:
9861 return DW_FORM_data2;
9862 case 4:
9863 return DW_FORM_data4;
9864 default:
9865 gcc_unreachable ();
9868 case dw_val_class_data8:
9869 return DW_FORM_data8;
9871 case dw_val_class_high_pc:
9872 switch (DWARF2_ADDR_SIZE)
9874 case 1:
9875 return DW_FORM_data1;
9876 case 2:
9877 return DW_FORM_data2;
9878 case 4:
9879 return DW_FORM_data4;
9880 case 8:
9881 return DW_FORM_data8;
9882 default:
9883 gcc_unreachable ();
9886 case dw_val_class_discr_value:
9887 return (a->dw_attr_val.v.val_discr_value.pos
9888 ? DW_FORM_udata
9889 : DW_FORM_sdata);
9890 case dw_val_class_discr_list:
9891 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9893 case 1:
9894 return DW_FORM_block1;
9895 case 2:
9896 return DW_FORM_block2;
9897 case 4:
9898 return DW_FORM_block4;
9899 default:
9900 gcc_unreachable ();
9903 default:
9904 gcc_unreachable ();
9908 /* Output the encoding of an attribute value. */
9910 static void
9911 output_value_format (dw_attr_node *a)
9913 enum dwarf_form form = value_format (a);
9915 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9918 /* Given a die and id, produce the appropriate abbreviations. */
9920 static void
9921 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9923 unsigned ix;
9924 dw_attr_node *a_attr;
9926 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9927 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9928 dwarf_tag_name (abbrev->die_tag));
9930 if (abbrev->die_child != NULL)
9931 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9932 else
9933 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9935 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9937 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9938 dwarf_attr_name (a_attr->dw_attr));
9939 output_value_format (a_attr);
9940 if (value_format (a_attr) == DW_FORM_implicit_const)
9942 if (AT_class (a_attr) == dw_val_class_file_implicit)
9944 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9945 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9946 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9948 else
9949 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9953 dw2_asm_output_data (1, 0, NULL);
9954 dw2_asm_output_data (1, 0, NULL);
9958 /* Output the .debug_abbrev section which defines the DIE abbreviation
9959 table. */
9961 static void
9962 output_abbrev_section (void)
9964 unsigned int abbrev_id;
9965 dw_die_ref abbrev;
9967 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9968 if (abbrev_id != 0)
9969 output_die_abbrevs (abbrev_id, abbrev);
9971 /* Terminate the table. */
9972 dw2_asm_output_data (1, 0, NULL);
9975 /* Return a new location list, given the begin and end range, and the
9976 expression. */
9978 static inline dw_loc_list_ref
9979 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9980 const char *end, var_loc_view vend,
9981 const char *section)
9983 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9985 retlist->begin = begin;
9986 retlist->begin_entry = NULL;
9987 retlist->end = end;
9988 retlist->expr = expr;
9989 retlist->section = section;
9990 retlist->vbegin = vbegin;
9991 retlist->vend = vend;
9993 return retlist;
9996 /* Return true iff there's any nonzero view number in the loc list.
9998 ??? When views are not enabled, we'll often extend a single range
9999 to the entire function, so that we emit a single location
10000 expression rather than a location list. With views, even with a
10001 single range, we'll output a list if start or end have a nonzero
10002 view. If we change this, we may want to stop splitting a single
10003 range in dw_loc_list just because of a nonzero view, even if it
10004 straddles across hot/cold partitions. */
10006 static bool
10007 loc_list_has_views (dw_loc_list_ref list)
10009 if (!debug_variable_location_views)
10010 return false;
10012 for (dw_loc_list_ref loc = list;
10013 loc != NULL; loc = loc->dw_loc_next)
10014 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10015 return true;
10017 return false;
10020 /* Generate a new internal symbol for this location list node, if it
10021 hasn't got one yet. */
10023 static inline void
10024 gen_llsym (dw_loc_list_ref list)
10026 gcc_assert (!list->ll_symbol);
10027 list->ll_symbol = gen_internal_sym ("LLST");
10029 if (!loc_list_has_views (list))
10030 return;
10032 if (dwarf2out_locviews_in_attribute ())
10034 /* Use the same label_num for the view list. */
10035 label_num--;
10036 list->vl_symbol = gen_internal_sym ("LVUS");
10038 else
10039 list->vl_symbol = list->ll_symbol;
10042 /* Generate a symbol for the list, but only if we really want to emit
10043 it as a list. */
10045 static inline void
10046 maybe_gen_llsym (dw_loc_list_ref list)
10048 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10049 return;
10051 gen_llsym (list);
10054 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10055 NULL, don't consider size of the location expression. If we're not
10056 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10057 representation in *SIZEP. */
10059 static bool
10060 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10062 /* Don't output an entry that starts and ends at the same address. */
10063 if (strcmp (curr->begin, curr->end) == 0
10064 && curr->vbegin == curr->vend && !curr->force)
10065 return true;
10067 if (!sizep)
10068 return false;
10070 unsigned long size = size_of_locs (curr->expr);
10072 /* If the expression is too large, drop it on the floor. We could
10073 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10074 in the expression, but >= 64KB expressions for a single value
10075 in a single range are unlikely very useful. */
10076 if (dwarf_version < 5 && size > 0xffff)
10077 return true;
10079 *sizep = size;
10081 return false;
10084 /* Output a view pair loclist entry for CURR, if it requires one. */
10086 static void
10087 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10089 if (!dwarf2out_locviews_in_loclist ())
10090 return;
10092 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10093 return;
10095 #ifdef DW_LLE_view_pair
10096 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10098 if (dwarf2out_as_locview_support)
10100 if (ZERO_VIEW_P (curr->vbegin))
10101 dw2_asm_output_data_uleb128 (0, "Location view begin");
10102 else
10104 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10105 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10106 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10109 if (ZERO_VIEW_P (curr->vend))
10110 dw2_asm_output_data_uleb128 (0, "Location view end");
10111 else
10113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10114 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10115 dw2_asm_output_symname_uleb128 (label, "Location view end");
10118 else
10120 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10121 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10123 #endif /* DW_LLE_view_pair */
10125 return;
10128 /* Output the location list given to us. */
10130 static void
10131 output_loc_list (dw_loc_list_ref list_head)
10133 int vcount = 0, lcount = 0;
10135 if (list_head->emitted)
10136 return;
10137 list_head->emitted = true;
10139 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10141 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10143 for (dw_loc_list_ref curr = list_head; curr != NULL;
10144 curr = curr->dw_loc_next)
10146 unsigned long size;
10148 if (skip_loc_list_entry (curr, &size))
10149 continue;
10151 vcount++;
10153 /* ?? dwarf_split_debug_info? */
10154 if (dwarf2out_as_locview_support)
10156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10158 if (!ZERO_VIEW_P (curr->vbegin))
10160 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10161 dw2_asm_output_symname_uleb128 (label,
10162 "View list begin (%s)",
10163 list_head->vl_symbol);
10165 else
10166 dw2_asm_output_data_uleb128 (0,
10167 "View list begin (%s)",
10168 list_head->vl_symbol);
10170 if (!ZERO_VIEW_P (curr->vend))
10172 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10173 dw2_asm_output_symname_uleb128 (label,
10174 "View list end (%s)",
10175 list_head->vl_symbol);
10177 else
10178 dw2_asm_output_data_uleb128 (0,
10179 "View list end (%s)",
10180 list_head->vl_symbol);
10182 else
10184 dw2_asm_output_data_uleb128 (curr->vbegin,
10185 "View list begin (%s)",
10186 list_head->vl_symbol);
10187 dw2_asm_output_data_uleb128 (curr->vend,
10188 "View list end (%s)",
10189 list_head->vl_symbol);
10194 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10196 const char *last_section = NULL;
10197 const char *base_label = NULL;
10199 /* Walk the location list, and output each range + expression. */
10200 for (dw_loc_list_ref curr = list_head; curr != NULL;
10201 curr = curr->dw_loc_next)
10203 unsigned long size;
10205 /* Skip this entry? If we skip it here, we must skip it in the
10206 view list above as well. */
10207 if (skip_loc_list_entry (curr, &size))
10208 continue;
10210 lcount++;
10212 if (dwarf_version >= 5)
10214 if (dwarf_split_debug_info)
10216 dwarf2out_maybe_output_loclist_view_pair (curr);
10217 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10218 uleb128 index into .debug_addr and uleb128 length. */
10219 dw2_asm_output_data (1, DW_LLE_startx_length,
10220 "DW_LLE_startx_length (%s)",
10221 list_head->ll_symbol);
10222 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10223 "Location list range start index "
10224 "(%s)", curr->begin);
10225 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10226 For that case we probably need to emit DW_LLE_startx_endx,
10227 but we'd need 2 .debug_addr entries rather than just one. */
10228 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10229 "Location list length (%s)",
10230 list_head->ll_symbol);
10232 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10234 dwarf2out_maybe_output_loclist_view_pair (curr);
10235 /* If all code is in .text section, the base address is
10236 already provided by the CU attributes. Use
10237 DW_LLE_offset_pair where both addresses are uleb128 encoded
10238 offsets against that base. */
10239 dw2_asm_output_data (1, DW_LLE_offset_pair,
10240 "DW_LLE_offset_pair (%s)",
10241 list_head->ll_symbol);
10242 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10243 "Location list begin address (%s)",
10244 list_head->ll_symbol);
10245 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10246 "Location list end address (%s)",
10247 list_head->ll_symbol);
10249 else if (HAVE_AS_LEB128)
10251 /* Otherwise, find out how many consecutive entries could share
10252 the same base entry. If just one, emit DW_LLE_start_length,
10253 otherwise emit DW_LLE_base_address for the base address
10254 followed by a series of DW_LLE_offset_pair. */
10255 if (last_section == NULL || curr->section != last_section)
10257 dw_loc_list_ref curr2;
10258 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10259 curr2 = curr2->dw_loc_next)
10261 if (strcmp (curr2->begin, curr2->end) == 0
10262 && !curr2->force)
10263 continue;
10264 break;
10266 if (curr2 == NULL || curr->section != curr2->section)
10267 last_section = NULL;
10268 else
10270 last_section = curr->section;
10271 base_label = curr->begin;
10272 dw2_asm_output_data (1, DW_LLE_base_address,
10273 "DW_LLE_base_address (%s)",
10274 list_head->ll_symbol);
10275 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10276 "Base address (%s)",
10277 list_head->ll_symbol);
10280 /* Only one entry with the same base address. Use
10281 DW_LLE_start_length with absolute address and uleb128
10282 length. */
10283 if (last_section == NULL)
10285 dwarf2out_maybe_output_loclist_view_pair (curr);
10286 dw2_asm_output_data (1, DW_LLE_start_length,
10287 "DW_LLE_start_length (%s)",
10288 list_head->ll_symbol);
10289 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10290 "Location list begin address (%s)",
10291 list_head->ll_symbol);
10292 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10293 "Location list length "
10294 "(%s)", list_head->ll_symbol);
10296 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10297 DW_LLE_base_address. */
10298 else
10300 dwarf2out_maybe_output_loclist_view_pair (curr);
10301 dw2_asm_output_data (1, DW_LLE_offset_pair,
10302 "DW_LLE_offset_pair (%s)",
10303 list_head->ll_symbol);
10304 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10305 "Location list begin address "
10306 "(%s)", list_head->ll_symbol);
10307 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10308 "Location list end address "
10309 "(%s)", list_head->ll_symbol);
10312 /* The assembler does not support .uleb128 directive. Emit
10313 DW_LLE_start_end with a pair of absolute addresses. */
10314 else
10316 dwarf2out_maybe_output_loclist_view_pair (curr);
10317 dw2_asm_output_data (1, DW_LLE_start_end,
10318 "DW_LLE_start_end (%s)",
10319 list_head->ll_symbol);
10320 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10321 "Location list begin address (%s)",
10322 list_head->ll_symbol);
10323 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10324 "Location list end address (%s)",
10325 list_head->ll_symbol);
10328 else if (dwarf_split_debug_info)
10330 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10331 and 4 byte length. */
10332 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10333 "Location list start/length entry (%s)",
10334 list_head->ll_symbol);
10335 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10336 "Location list range start index (%s)",
10337 curr->begin);
10338 /* The length field is 4 bytes. If we ever need to support
10339 an 8-byte length, we can add a new DW_LLE code or fall back
10340 to DW_LLE_GNU_start_end_entry. */
10341 dw2_asm_output_delta (4, curr->end, curr->begin,
10342 "Location list range length (%s)",
10343 list_head->ll_symbol);
10345 else if (!have_multiple_function_sections)
10347 /* Pair of relative addresses against start of text section. */
10348 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10349 "Location list begin address (%s)",
10350 list_head->ll_symbol);
10351 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10352 "Location list end address (%s)",
10353 list_head->ll_symbol);
10355 else
10357 /* Pair of absolute addresses. */
10358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10359 "Location list begin address (%s)",
10360 list_head->ll_symbol);
10361 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10362 "Location list end address (%s)",
10363 list_head->ll_symbol);
10366 /* Output the block length for this list of location operations. */
10367 if (dwarf_version >= 5)
10368 dw2_asm_output_data_uleb128 (size, "Location expression size");
10369 else
10371 gcc_assert (size <= 0xffff);
10372 dw2_asm_output_data (2, size, "Location expression size");
10375 output_loc_sequence (curr->expr, -1);
10378 /* And finally list termination. */
10379 if (dwarf_version >= 5)
10380 dw2_asm_output_data (1, DW_LLE_end_of_list,
10381 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10382 else if (dwarf_split_debug_info)
10383 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10384 "Location list terminator (%s)",
10385 list_head->ll_symbol);
10386 else
10388 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10389 "Location list terminator begin (%s)",
10390 list_head->ll_symbol);
10391 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10392 "Location list terminator end (%s)",
10393 list_head->ll_symbol);
10396 gcc_assert (!list_head->vl_symbol
10397 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10400 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10401 section. Emit a relocated reference if val_entry is NULL, otherwise,
10402 emit an indirect reference. */
10404 static void
10405 output_range_list_offset (dw_attr_node *a)
10407 const char *name = dwarf_attr_name (a->dw_attr);
10409 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10411 if (dwarf_version >= 5)
10413 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10414 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10415 debug_ranges_section, "%s", name);
10417 else
10419 char *p = strchr (ranges_section_label, '\0');
10420 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10421 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10422 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10423 debug_ranges_section, "%s", name);
10424 *p = '\0';
10427 else if (dwarf_version >= 5)
10429 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10430 gcc_assert (rnglist_idx);
10431 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10433 else
10434 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10435 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10436 "%s (offset from %s)", name, ranges_section_label);
10439 /* Output the offset into the debug_loc section. */
10441 static void
10442 output_loc_list_offset (dw_attr_node *a)
10444 char *sym = AT_loc_list (a)->ll_symbol;
10446 gcc_assert (sym);
10447 if (!dwarf_split_debug_info)
10448 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10449 "%s", dwarf_attr_name (a->dw_attr));
10450 else if (dwarf_version >= 5)
10452 gcc_assert (AT_loc_list (a)->num_assigned);
10453 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10454 dwarf_attr_name (a->dw_attr),
10455 sym);
10457 else
10458 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10459 "%s", dwarf_attr_name (a->dw_attr));
10462 /* Output the offset into the debug_loc section. */
10464 static void
10465 output_view_list_offset (dw_attr_node *a)
10467 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10469 gcc_assert (sym);
10470 if (dwarf_split_debug_info)
10471 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10472 "%s", dwarf_attr_name (a->dw_attr));
10473 else
10474 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10475 "%s", dwarf_attr_name (a->dw_attr));
10478 /* Output an attribute's index or value appropriately. */
10480 static void
10481 output_attr_index_or_value (dw_attr_node *a)
10483 const char *name = dwarf_attr_name (a->dw_attr);
10485 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10487 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10488 return;
10490 switch (AT_class (a))
10492 case dw_val_class_addr:
10493 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10494 break;
10495 case dw_val_class_high_pc:
10496 case dw_val_class_lbl_id:
10497 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10498 break;
10499 default:
10500 gcc_unreachable ();
10504 /* Output a type signature. */
10506 static inline void
10507 output_signature (const char *sig, const char *name)
10509 int i;
10511 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10512 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10515 /* Output a discriminant value. */
10517 static inline void
10518 output_discr_value (dw_discr_value *discr_value, const char *name)
10520 if (discr_value->pos)
10521 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10522 else
10523 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10526 /* Output the DIE and its attributes. Called recursively to generate
10527 the definitions of each child DIE. */
10529 static void
10530 output_die (dw_die_ref die)
10532 dw_attr_node *a;
10533 dw_die_ref c;
10534 unsigned long size;
10535 unsigned ix;
10537 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10538 (unsigned long)die->die_offset,
10539 dwarf_tag_name (die->die_tag));
10541 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10543 const char *name = dwarf_attr_name (a->dw_attr);
10545 switch (AT_class (a))
10547 case dw_val_class_addr:
10548 output_attr_index_or_value (a);
10549 break;
10551 case dw_val_class_offset:
10552 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10553 "%s", name);
10554 break;
10556 case dw_val_class_range_list:
10557 output_range_list_offset (a);
10558 break;
10560 case dw_val_class_loc:
10561 size = size_of_locs (AT_loc (a));
10563 /* Output the block length for this list of location operations. */
10564 if (dwarf_version >= 4)
10565 dw2_asm_output_data_uleb128 (size, "%s", name);
10566 else
10567 dw2_asm_output_data (constant_size (size), size, "%s", name);
10569 output_loc_sequence (AT_loc (a), -1);
10570 break;
10572 case dw_val_class_const:
10573 /* ??? It would be slightly more efficient to use a scheme like is
10574 used for unsigned constants below, but gdb 4.x does not sign
10575 extend. Gdb 5.x does sign extend. */
10576 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10577 break;
10579 case dw_val_class_unsigned_const:
10581 int csize = constant_size (AT_unsigned (a));
10582 if (dwarf_version == 3
10583 && a->dw_attr == DW_AT_data_member_location
10584 && csize >= 4)
10585 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10586 else
10587 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10589 break;
10591 case dw_val_class_symview:
10593 int vsize;
10594 if (symview_upper_bound <= 0xff)
10595 vsize = 1;
10596 else if (symview_upper_bound <= 0xffff)
10597 vsize = 2;
10598 else if (symview_upper_bound <= 0xffffffff)
10599 vsize = 4;
10600 else
10601 vsize = 8;
10602 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10603 "%s", name);
10605 break;
10607 case dw_val_class_const_implicit:
10608 if (flag_debug_asm)
10609 fprintf (asm_out_file, "\t\t\t%s %s ("
10610 HOST_WIDE_INT_PRINT_DEC ")\n",
10611 ASM_COMMENT_START, name, AT_int (a));
10612 break;
10614 case dw_val_class_unsigned_const_implicit:
10615 if (flag_debug_asm)
10616 fprintf (asm_out_file, "\t\t\t%s %s ("
10617 HOST_WIDE_INT_PRINT_HEX ")\n",
10618 ASM_COMMENT_START, name, AT_unsigned (a));
10619 break;
10621 case dw_val_class_const_double:
10623 unsigned HOST_WIDE_INT first, second;
10625 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10626 dw2_asm_output_data (1,
10627 HOST_BITS_PER_DOUBLE_INT
10628 / HOST_BITS_PER_CHAR,
10629 NULL);
10631 if (WORDS_BIG_ENDIAN)
10633 first = a->dw_attr_val.v.val_double.high;
10634 second = a->dw_attr_val.v.val_double.low;
10636 else
10638 first = a->dw_attr_val.v.val_double.low;
10639 second = a->dw_attr_val.v.val_double.high;
10642 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10643 first, "%s", name);
10644 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10645 second, NULL);
10647 break;
10649 case dw_val_class_wide_int:
10651 int i;
10652 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10653 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10654 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10655 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10656 * l, NULL);
10658 if (WORDS_BIG_ENDIAN)
10659 for (i = len - 1; i >= 0; --i)
10661 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10662 "%s", name);
10663 name = "";
10665 else
10666 for (i = 0; i < len; ++i)
10668 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10669 "%s", name);
10670 name = "";
10673 break;
10675 case dw_val_class_vec:
10677 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10678 unsigned int len = a->dw_attr_val.v.val_vec.length;
10679 unsigned int i;
10680 unsigned char *p;
10682 dw2_asm_output_data (constant_size (len * elt_size),
10683 len * elt_size, "%s", name);
10684 if (elt_size > sizeof (HOST_WIDE_INT))
10686 elt_size /= 2;
10687 len *= 2;
10689 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10690 i < len;
10691 i++, p += elt_size)
10692 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10693 "fp or vector constant word %u", i);
10694 break;
10697 case dw_val_class_flag:
10698 if (dwarf_version >= 4)
10700 /* Currently all add_AT_flag calls pass in 1 as last argument,
10701 so DW_FORM_flag_present can be used. If that ever changes,
10702 we'll need to use DW_FORM_flag and have some optimization
10703 in build_abbrev_table that will change those to
10704 DW_FORM_flag_present if it is set to 1 in all DIEs using
10705 the same abbrev entry. */
10706 gcc_assert (AT_flag (a) == 1);
10707 if (flag_debug_asm)
10708 fprintf (asm_out_file, "\t\t\t%s %s\n",
10709 ASM_COMMENT_START, name);
10710 break;
10712 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10713 break;
10715 case dw_val_class_loc_list:
10716 output_loc_list_offset (a);
10717 break;
10719 case dw_val_class_view_list:
10720 output_view_list_offset (a);
10721 break;
10723 case dw_val_class_die_ref:
10724 if (AT_ref_external (a))
10726 if (AT_ref (a)->comdat_type_p)
10728 comdat_type_node *type_node
10729 = AT_ref (a)->die_id.die_type_node;
10731 gcc_assert (type_node);
10732 output_signature (type_node->signature, name);
10734 else
10736 const char *sym = AT_ref (a)->die_id.die_symbol;
10737 int size;
10739 gcc_assert (sym);
10740 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10741 length, whereas in DWARF3 it's always sized as an
10742 offset. */
10743 if (dwarf_version == 2)
10744 size = DWARF2_ADDR_SIZE;
10745 else
10746 size = DWARF_OFFSET_SIZE;
10747 /* ??? We cannot unconditionally output die_offset if
10748 non-zero - others might create references to those
10749 DIEs via symbols.
10750 And we do not clear its DIE offset after outputting it
10751 (and the label refers to the actual DIEs, not the
10752 DWARF CU unit header which is when using label + offset
10753 would be the correct thing to do).
10754 ??? This is the reason for the with_offset flag. */
10755 if (AT_ref (a)->with_offset)
10756 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10757 debug_info_section, "%s", name);
10758 else
10759 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10760 name);
10763 else
10765 gcc_assert (AT_ref (a)->die_offset);
10766 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10767 "%s", name);
10769 break;
10771 case dw_val_class_fde_ref:
10773 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10775 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10776 a->dw_attr_val.v.val_fde_index * 2);
10777 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10778 "%s", name);
10780 break;
10782 case dw_val_class_vms_delta:
10783 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10784 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10785 AT_vms_delta2 (a), AT_vms_delta1 (a),
10786 "%s", name);
10787 #else
10788 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10789 AT_vms_delta2 (a), AT_vms_delta1 (a),
10790 "%s", name);
10791 #endif
10792 break;
10794 case dw_val_class_lbl_id:
10795 output_attr_index_or_value (a);
10796 break;
10798 case dw_val_class_lineptr:
10799 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10800 debug_line_section, "%s", name);
10801 break;
10803 case dw_val_class_macptr:
10804 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10805 debug_macinfo_section, "%s", name);
10806 break;
10808 case dw_val_class_loclistsptr:
10809 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10810 debug_loc_section, "%s", name);
10811 break;
10813 case dw_val_class_str:
10814 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10815 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10816 a->dw_attr_val.v.val_str->label,
10817 debug_str_section,
10818 "%s: \"%s\"", name, AT_string (a));
10819 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10820 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10821 a->dw_attr_val.v.val_str->label,
10822 debug_line_str_section,
10823 "%s: \"%s\"", name, AT_string (a));
10824 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10825 dw2_asm_output_data_uleb128 (AT_index (a),
10826 "%s: \"%s\"", name, AT_string (a));
10827 else
10828 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10829 break;
10831 case dw_val_class_file:
10833 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10835 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10836 a->dw_attr_val.v.val_file->filename);
10837 break;
10840 case dw_val_class_file_implicit:
10841 if (flag_debug_asm)
10842 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10843 ASM_COMMENT_START, name,
10844 maybe_emit_file (a->dw_attr_val.v.val_file),
10845 a->dw_attr_val.v.val_file->filename);
10846 break;
10848 case dw_val_class_data8:
10850 int i;
10852 for (i = 0; i < 8; i++)
10853 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10854 i == 0 ? "%s" : NULL, name);
10855 break;
10858 case dw_val_class_high_pc:
10859 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10860 get_AT_low_pc (die), "DW_AT_high_pc");
10861 break;
10863 case dw_val_class_discr_value:
10864 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10865 break;
10867 case dw_val_class_discr_list:
10869 dw_discr_list_ref list = AT_discr_list (a);
10870 const int size = size_of_discr_list (list);
10872 /* This is a block, so output its length first. */
10873 dw2_asm_output_data (constant_size (size), size,
10874 "%s: block size", name);
10876 for (; list != NULL; list = list->dw_discr_next)
10878 /* One byte for the discriminant value descriptor, and then as
10879 many LEB128 numbers as required. */
10880 if (list->dw_discr_range)
10881 dw2_asm_output_data (1, DW_DSC_range,
10882 "%s: DW_DSC_range", name);
10883 else
10884 dw2_asm_output_data (1, DW_DSC_label,
10885 "%s: DW_DSC_label", name);
10887 output_discr_value (&list->dw_discr_lower_bound, name);
10888 if (list->dw_discr_range)
10889 output_discr_value (&list->dw_discr_upper_bound, name);
10891 break;
10894 default:
10895 gcc_unreachable ();
10899 FOR_EACH_CHILD (die, c, output_die (c));
10901 /* Add null byte to terminate sibling list. */
10902 if (die->die_child != NULL)
10903 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10904 (unsigned long) die->die_offset);
10907 /* Output the dwarf version number. */
10909 static void
10910 output_dwarf_version ()
10912 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10913 views in loclist. That will change eventually. */
10914 if (dwarf_version == 6)
10916 static bool once;
10917 if (!once)
10919 warning (0,
10920 "-gdwarf-6 is output as version 5 with incompatibilities");
10921 once = true;
10923 dw2_asm_output_data (2, 5, "DWARF version number");
10925 else
10926 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10929 /* Output the compilation unit that appears at the beginning of the
10930 .debug_info section, and precedes the DIE descriptions. */
10932 static void
10933 output_compilation_unit_header (enum dwarf_unit_type ut)
10935 if (!XCOFF_DEBUGGING_INFO)
10937 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10938 dw2_asm_output_data (4, 0xffffffff,
10939 "Initial length escape value indicating 64-bit DWARF extension");
10940 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10941 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10942 "Length of Compilation Unit Info");
10945 output_dwarf_version ();
10946 if (dwarf_version >= 5)
10948 const char *name;
10949 switch (ut)
10951 case DW_UT_compile: name = "DW_UT_compile"; break;
10952 case DW_UT_type: name = "DW_UT_type"; break;
10953 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10954 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10955 default: gcc_unreachable ();
10957 dw2_asm_output_data (1, ut, "%s", name);
10958 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10960 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10961 debug_abbrev_section,
10962 "Offset Into Abbrev. Section");
10963 if (dwarf_version < 5)
10964 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10967 /* Output the compilation unit DIE and its children. */
10969 static void
10970 output_comp_unit (dw_die_ref die, int output_if_empty,
10971 const unsigned char *dwo_id)
10973 const char *secname, *oldsym;
10974 char *tmp;
10976 /* Unless we are outputting main CU, we may throw away empty ones. */
10977 if (!output_if_empty && die->die_child == NULL)
10978 return;
10980 /* Even if there are no children of this DIE, we must output the information
10981 about the compilation unit. Otherwise, on an empty translation unit, we
10982 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10983 will then complain when examining the file. First mark all the DIEs in
10984 this CU so we know which get local refs. */
10985 mark_dies (die);
10987 external_ref_hash_type *extern_map = optimize_external_refs (die);
10989 /* For now, optimize only the main CU, in order to optimize the rest
10990 we'd need to see all of them earlier. Leave the rest for post-linking
10991 tools like DWZ. */
10992 if (die == comp_unit_die ())
10993 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10995 build_abbrev_table (die, extern_map);
10997 optimize_abbrev_table ();
10999 delete extern_map;
11001 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11002 next_die_offset = (dwo_id
11003 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11004 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11005 calc_die_sizes (die);
11007 oldsym = die->die_id.die_symbol;
11008 if (oldsym && die->comdat_type_p)
11010 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11012 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11013 secname = tmp;
11014 die->die_id.die_symbol = NULL;
11015 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11017 else
11019 switch_to_section (debug_info_section);
11020 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11021 info_section_emitted = true;
11024 /* For LTO cross unit DIE refs we want a symbol on the start of the
11025 debuginfo section, not on the CU DIE. */
11026 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11028 /* ??? No way to get visibility assembled without a decl. */
11029 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11030 get_identifier (oldsym), char_type_node);
11031 TREE_PUBLIC (decl) = true;
11032 TREE_STATIC (decl) = true;
11033 DECL_ARTIFICIAL (decl) = true;
11034 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11035 DECL_VISIBILITY_SPECIFIED (decl) = true;
11036 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11037 #ifdef ASM_WEAKEN_LABEL
11038 /* We prefer a .weak because that handles duplicates from duplicate
11039 archive members in a graceful way. */
11040 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11041 #else
11042 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11043 #endif
11044 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11047 /* Output debugging information. */
11048 output_compilation_unit_header (dwo_id
11049 ? DW_UT_split_compile : DW_UT_compile);
11050 if (dwarf_version >= 5)
11052 if (dwo_id != NULL)
11053 for (int i = 0; i < 8; i++)
11054 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11056 output_die (die);
11058 /* Leave the marks on the main CU, so we can check them in
11059 output_pubnames. */
11060 if (oldsym)
11062 unmark_dies (die);
11063 die->die_id.die_symbol = oldsym;
11067 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11068 and .debug_pubtypes. This is configured per-target, but can be
11069 overridden by the -gpubnames or -gno-pubnames options. */
11071 static inline bool
11072 want_pubnames (void)
11074 if (debug_info_level <= DINFO_LEVEL_TERSE)
11075 return false;
11076 if (debug_generate_pub_sections != -1)
11077 return debug_generate_pub_sections;
11078 return targetm.want_debug_pub_sections;
11081 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11083 static void
11084 add_AT_pubnames (dw_die_ref die)
11086 if (want_pubnames ())
11087 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11090 /* Add a string attribute value to a skeleton DIE. */
11092 static inline void
11093 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11094 const char *str)
11096 dw_attr_node attr;
11097 struct indirect_string_node *node;
11099 if (! skeleton_debug_str_hash)
11100 skeleton_debug_str_hash
11101 = hash_table<indirect_string_hasher>::create_ggc (10);
11103 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11104 find_string_form (node);
11105 if (node->form == dwarf_FORM (DW_FORM_strx))
11106 node->form = DW_FORM_strp;
11108 attr.dw_attr = attr_kind;
11109 attr.dw_attr_val.val_class = dw_val_class_str;
11110 attr.dw_attr_val.val_entry = NULL;
11111 attr.dw_attr_val.v.val_str = node;
11112 add_dwarf_attr (die, &attr);
11115 /* Helper function to generate top-level dies for skeleton debug_info and
11116 debug_types. */
11118 static void
11119 add_top_level_skeleton_die_attrs (dw_die_ref die)
11121 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11122 const char *comp_dir = comp_dir_string ();
11124 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11125 if (comp_dir != NULL)
11126 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11127 add_AT_pubnames (die);
11128 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11131 /* Output skeleton debug sections that point to the dwo file. */
11133 static void
11134 output_skeleton_debug_sections (dw_die_ref comp_unit,
11135 const unsigned char *dwo_id)
11137 /* These attributes will be found in the full debug_info section. */
11138 remove_AT (comp_unit, DW_AT_producer);
11139 remove_AT (comp_unit, DW_AT_language);
11141 switch_to_section (debug_skeleton_info_section);
11142 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11144 /* Produce the skeleton compilation-unit header. This one differs enough from
11145 a normal CU header that it's better not to call output_compilation_unit
11146 header. */
11147 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11148 dw2_asm_output_data (4, 0xffffffff,
11149 "Initial length escape value indicating 64-bit "
11150 "DWARF extension");
11152 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11153 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11154 - DWARF_INITIAL_LENGTH_SIZE
11155 + size_of_die (comp_unit),
11156 "Length of Compilation Unit Info");
11157 output_dwarf_version ();
11158 if (dwarf_version >= 5)
11160 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11161 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11163 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11164 debug_skeleton_abbrev_section,
11165 "Offset Into Abbrev. Section");
11166 if (dwarf_version < 5)
11167 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11168 else
11169 for (int i = 0; i < 8; i++)
11170 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11172 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11173 output_die (comp_unit);
11175 /* Build the skeleton debug_abbrev section. */
11176 switch_to_section (debug_skeleton_abbrev_section);
11177 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11179 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11181 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11184 /* Output a comdat type unit DIE and its children. */
11186 static void
11187 output_comdat_type_unit (comdat_type_node *node)
11189 const char *secname;
11190 char *tmp;
11191 int i;
11192 #if defined (OBJECT_FORMAT_ELF)
11193 tree comdat_key;
11194 #endif
11196 /* First mark all the DIEs in this CU so we know which get local refs. */
11197 mark_dies (node->root_die);
11199 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11201 build_abbrev_table (node->root_die, extern_map);
11203 delete extern_map;
11204 extern_map = NULL;
11206 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11207 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11208 calc_die_sizes (node->root_die);
11210 #if defined (OBJECT_FORMAT_ELF)
11211 if (dwarf_version >= 5)
11213 if (!dwarf_split_debug_info)
11214 secname = ".debug_info";
11215 else
11216 secname = ".debug_info.dwo";
11218 else if (!dwarf_split_debug_info)
11219 secname = ".debug_types";
11220 else
11221 secname = ".debug_types.dwo";
11223 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11224 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11225 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11226 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11227 comdat_key = get_identifier (tmp);
11228 targetm.asm_out.named_section (secname,
11229 SECTION_DEBUG | SECTION_LINKONCE,
11230 comdat_key);
11231 #else
11232 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11233 sprintf (tmp, (dwarf_version >= 5
11234 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11235 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11236 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11237 secname = tmp;
11238 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11239 #endif
11241 /* Output debugging information. */
11242 output_compilation_unit_header (dwarf_split_debug_info
11243 ? DW_UT_split_type : DW_UT_type);
11244 output_signature (node->signature, "Type Signature");
11245 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11246 "Offset to Type DIE");
11247 output_die (node->root_die);
11249 unmark_dies (node->root_die);
11252 /* Return the DWARF2/3 pubname associated with a decl. */
11254 static const char *
11255 dwarf2_name (tree decl, int scope)
11257 if (DECL_NAMELESS (decl))
11258 return NULL;
11259 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11262 /* Add a new entry to .debug_pubnames if appropriate. */
11264 static void
11265 add_pubname_string (const char *str, dw_die_ref die)
11267 pubname_entry e;
11269 e.die = die;
11270 e.name = xstrdup (str);
11271 vec_safe_push (pubname_table, e);
11274 static void
11275 add_pubname (tree decl, dw_die_ref die)
11277 if (!want_pubnames ())
11278 return;
11280 /* Don't add items to the table when we expect that the consumer will have
11281 just read the enclosing die. For example, if the consumer is looking at a
11282 class_member, it will either be inside the class already, or will have just
11283 looked up the class to find the member. Either way, searching the class is
11284 faster than searching the index. */
11285 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11286 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11288 const char *name = dwarf2_name (decl, 1);
11290 if (name)
11291 add_pubname_string (name, die);
11295 /* Add an enumerator to the pubnames section. */
11297 static void
11298 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11300 pubname_entry e;
11302 gcc_assert (scope_name);
11303 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11304 e.die = die;
11305 vec_safe_push (pubname_table, e);
11308 /* Add a new entry to .debug_pubtypes if appropriate. */
11310 static void
11311 add_pubtype (tree decl, dw_die_ref die)
11313 pubname_entry e;
11315 if (!want_pubnames ())
11316 return;
11318 if ((TREE_PUBLIC (decl)
11319 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11320 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11322 tree scope = NULL;
11323 const char *scope_name = "";
11324 const char *sep = is_cxx () ? "::" : ".";
11325 const char *name;
11327 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11328 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11330 scope_name = lang_hooks.dwarf_name (scope, 1);
11331 if (scope_name != NULL && scope_name[0] != '\0')
11332 scope_name = concat (scope_name, sep, NULL);
11333 else
11334 scope_name = "";
11337 if (TYPE_P (decl))
11338 name = type_tag (decl);
11339 else
11340 name = lang_hooks.dwarf_name (decl, 1);
11342 /* If we don't have a name for the type, there's no point in adding
11343 it to the table. */
11344 if (name != NULL && name[0] != '\0')
11346 e.die = die;
11347 e.name = concat (scope_name, name, NULL);
11348 vec_safe_push (pubtype_table, e);
11351 /* Although it might be more consistent to add the pubinfo for the
11352 enumerators as their dies are created, they should only be added if the
11353 enum type meets the criteria above. So rather than re-check the parent
11354 enum type whenever an enumerator die is created, just output them all
11355 here. This isn't protected by the name conditional because anonymous
11356 enums don't have names. */
11357 if (die->die_tag == DW_TAG_enumeration_type)
11359 dw_die_ref c;
11361 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11366 /* Output a single entry in the pubnames table. */
11368 static void
11369 output_pubname (dw_offset die_offset, pubname_entry *entry)
11371 dw_die_ref die = entry->die;
11372 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11374 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11376 if (debug_generate_pub_sections == 2)
11378 /* This logic follows gdb's method for determining the value of the flag
11379 byte. */
11380 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11381 switch (die->die_tag)
11383 case DW_TAG_typedef:
11384 case DW_TAG_base_type:
11385 case DW_TAG_subrange_type:
11386 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11387 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11388 break;
11389 case DW_TAG_enumerator:
11390 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11391 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11392 if (!is_cxx ())
11393 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11394 break;
11395 case DW_TAG_subprogram:
11396 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11397 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11398 if (!is_ada ())
11399 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11400 break;
11401 case DW_TAG_constant:
11402 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11403 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11404 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11405 break;
11406 case DW_TAG_variable:
11407 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11408 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11409 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11410 break;
11411 case DW_TAG_namespace:
11412 case DW_TAG_imported_declaration:
11413 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11414 break;
11415 case DW_TAG_class_type:
11416 case DW_TAG_interface_type:
11417 case DW_TAG_structure_type:
11418 case DW_TAG_union_type:
11419 case DW_TAG_enumeration_type:
11420 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11421 if (!is_cxx ())
11422 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11423 break;
11424 default:
11425 /* An unusual tag. Leave the flag-byte empty. */
11426 break;
11428 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11429 "GDB-index flags");
11432 dw2_asm_output_nstring (entry->name, -1, "external name");
11436 /* Output the public names table used to speed up access to externally
11437 visible names; or the public types table used to find type definitions. */
11439 static void
11440 output_pubnames (vec<pubname_entry, va_gc> *names)
11442 unsigned i;
11443 unsigned long pubnames_length = size_of_pubnames (names);
11444 pubname_entry *pub;
11446 if (!XCOFF_DEBUGGING_INFO)
11448 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11449 dw2_asm_output_data (4, 0xffffffff,
11450 "Initial length escape value indicating 64-bit DWARF extension");
11451 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11452 "Pub Info Length");
11455 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11456 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11458 if (dwarf_split_debug_info)
11459 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11460 debug_skeleton_info_section,
11461 "Offset of Compilation Unit Info");
11462 else
11463 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11464 debug_info_section,
11465 "Offset of Compilation Unit Info");
11466 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11467 "Compilation Unit Length");
11469 FOR_EACH_VEC_ELT (*names, i, pub)
11471 if (include_pubname_in_output (names, pub))
11473 dw_offset die_offset = pub->die->die_offset;
11475 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11476 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11477 gcc_assert (pub->die->die_mark);
11479 /* If we're putting types in their own .debug_types sections,
11480 the .debug_pubtypes table will still point to the compile
11481 unit (not the type unit), so we want to use the offset of
11482 the skeleton DIE (if there is one). */
11483 if (pub->die->comdat_type_p && names == pubtype_table)
11485 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11487 if (type_node != NULL)
11488 die_offset = (type_node->skeleton_die != NULL
11489 ? type_node->skeleton_die->die_offset
11490 : comp_unit_die ()->die_offset);
11493 output_pubname (die_offset, pub);
11497 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11500 /* Output public names and types tables if necessary. */
11502 static void
11503 output_pubtables (void)
11505 if (!want_pubnames () || !info_section_emitted)
11506 return;
11508 switch_to_section (debug_pubnames_section);
11509 output_pubnames (pubname_table);
11510 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11511 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11512 simply won't look for the section. */
11513 switch_to_section (debug_pubtypes_section);
11514 output_pubnames (pubtype_table);
11518 /* Output the information that goes into the .debug_aranges table.
11519 Namely, define the beginning and ending address range of the
11520 text section generated for this compilation unit. */
11522 static void
11523 output_aranges (void)
11525 unsigned i;
11526 unsigned long aranges_length = size_of_aranges ();
11528 if (!XCOFF_DEBUGGING_INFO)
11530 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11531 dw2_asm_output_data (4, 0xffffffff,
11532 "Initial length escape value indicating 64-bit DWARF extension");
11533 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11534 "Length of Address Ranges Info");
11537 /* Version number for aranges is still 2, even up to DWARF5. */
11538 dw2_asm_output_data (2, 2, "DWARF aranges version");
11539 if (dwarf_split_debug_info)
11540 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11541 debug_skeleton_info_section,
11542 "Offset of Compilation Unit Info");
11543 else
11544 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11545 debug_info_section,
11546 "Offset of Compilation Unit Info");
11547 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11548 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11550 /* We need to align to twice the pointer size here. */
11551 if (DWARF_ARANGES_PAD_SIZE)
11553 /* Pad using a 2 byte words so that padding is correct for any
11554 pointer size. */
11555 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11556 2 * DWARF2_ADDR_SIZE);
11557 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11558 dw2_asm_output_data (2, 0, NULL);
11561 /* It is necessary not to output these entries if the sections were
11562 not used; if the sections were not used, the length will be 0 and
11563 the address may end up as 0 if the section is discarded by ld
11564 --gc-sections, leaving an invalid (0, 0) entry that can be
11565 confused with the terminator. */
11566 if (text_section_used)
11568 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11569 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11570 text_section_label, "Length");
11572 if (cold_text_section_used)
11574 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11575 "Address");
11576 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11577 cold_text_section_label, "Length");
11580 if (have_multiple_function_sections)
11582 unsigned fde_idx;
11583 dw_fde_ref fde;
11585 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11587 if (DECL_IGNORED_P (fde->decl))
11588 continue;
11589 if (!fde->in_std_section)
11591 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11592 "Address");
11593 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11594 fde->dw_fde_begin, "Length");
11596 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11598 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11599 "Address");
11600 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11601 fde->dw_fde_second_begin, "Length");
11606 /* Output the terminator words. */
11607 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11608 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11611 /* Add a new entry to .debug_ranges. Return its index into
11612 ranges_table vector. */
11614 static unsigned int
11615 add_ranges_num (int num, bool maybe_new_sec)
11617 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11618 vec_safe_push (ranges_table, r);
11619 return vec_safe_length (ranges_table) - 1;
11622 /* Add a new entry to .debug_ranges corresponding to a block, or a
11623 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11624 this entry might be in a different section from previous range. */
11626 static unsigned int
11627 add_ranges (const_tree block, bool maybe_new_sec)
11629 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11632 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11633 chain, or middle entry of a chain that will be directly referred to. */
11635 static void
11636 note_rnglist_head (unsigned int offset)
11638 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11639 return;
11640 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11643 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11644 When using dwarf_split_debug_info, address attributes in dies destined
11645 for the final executable should be direct references--setting the
11646 parameter force_direct ensures this behavior. */
11648 static void
11649 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11650 bool *added, bool force_direct)
11652 unsigned int in_use = vec_safe_length (ranges_by_label);
11653 unsigned int offset;
11654 dw_ranges_by_label rbl = { begin, end };
11655 vec_safe_push (ranges_by_label, rbl);
11656 offset = add_ranges_num (-(int)in_use - 1, true);
11657 if (!*added)
11659 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11660 *added = true;
11661 note_rnglist_head (offset);
11665 /* Emit .debug_ranges section. */
11667 static void
11668 output_ranges (void)
11670 unsigned i;
11671 static const char *const start_fmt = "Offset %#x";
11672 const char *fmt = start_fmt;
11673 dw_ranges *r;
11675 switch_to_section (debug_ranges_section);
11676 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11677 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11679 int block_num = r->num;
11681 if (block_num > 0)
11683 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11684 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11686 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11687 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11689 /* If all code is in the text section, then the compilation
11690 unit base address defaults to DW_AT_low_pc, which is the
11691 base of the text section. */
11692 if (!have_multiple_function_sections)
11694 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11695 text_section_label,
11696 fmt, i * 2 * DWARF2_ADDR_SIZE);
11697 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11698 text_section_label, NULL);
11701 /* Otherwise, the compilation unit base address is zero,
11702 which allows us to use absolute addresses, and not worry
11703 about whether the target supports cross-section
11704 arithmetic. */
11705 else
11707 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11708 fmt, i * 2 * DWARF2_ADDR_SIZE);
11709 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11712 fmt = NULL;
11715 /* Negative block_num stands for an index into ranges_by_label. */
11716 else if (block_num < 0)
11718 int lab_idx = - block_num - 1;
11720 if (!have_multiple_function_sections)
11722 gcc_unreachable ();
11723 #if 0
11724 /* If we ever use add_ranges_by_labels () for a single
11725 function section, all we have to do is to take out
11726 the #if 0 above. */
11727 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11728 (*ranges_by_label)[lab_idx].begin,
11729 text_section_label,
11730 fmt, i * 2 * DWARF2_ADDR_SIZE);
11731 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11732 (*ranges_by_label)[lab_idx].end,
11733 text_section_label, NULL);
11734 #endif
11736 else
11738 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11739 (*ranges_by_label)[lab_idx].begin,
11740 fmt, i * 2 * DWARF2_ADDR_SIZE);
11741 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11742 (*ranges_by_label)[lab_idx].end,
11743 NULL);
11746 else
11748 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11749 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11750 fmt = start_fmt;
11755 /* Non-zero if .debug_line_str should be used for .debug_line section
11756 strings or strings that are likely shareable with those. */
11757 #define DWARF5_USE_DEBUG_LINE_STR \
11758 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11759 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11760 /* FIXME: there is no .debug_line_str.dwo section, \
11761 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11762 && !dwarf_split_debug_info)
11764 /* Assign .debug_rnglists indexes. */
11766 static void
11767 index_rnglists (void)
11769 unsigned i;
11770 dw_ranges *r;
11772 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11773 if (r->label)
11774 r->idx = rnglist_idx++;
11777 /* Emit .debug_rnglists section. */
11779 static void
11780 output_rnglists (unsigned generation)
11782 unsigned i;
11783 dw_ranges *r;
11784 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11785 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11786 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11788 switch_to_section (debug_ranges_section);
11789 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11790 /* There are up to 4 unique ranges labels per generation.
11791 See also init_sections_and_labels. */
11792 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11793 2 + generation * 4);
11794 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11795 3 + generation * 4);
11796 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11797 dw2_asm_output_data (4, 0xffffffff,
11798 "Initial length escape value indicating "
11799 "64-bit DWARF extension");
11800 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11801 "Length of Range Lists");
11802 ASM_OUTPUT_LABEL (asm_out_file, l1);
11803 output_dwarf_version ();
11804 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11805 dw2_asm_output_data (1, 0, "Segment Size");
11806 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11807 about relocation sizes and primarily care about the size of .debug*
11808 sections in linked shared libraries and executables, then
11809 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11810 into it are usually larger than just DW_FORM_sec_offset offsets
11811 into the .debug_rnglists section. */
11812 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11813 "Offset Entry Count");
11814 if (dwarf_split_debug_info)
11816 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11817 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11818 if (r->label)
11819 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11820 ranges_base_label, NULL);
11823 const char *lab = "";
11824 unsigned int len = vec_safe_length (ranges_table);
11825 const char *base = NULL;
11826 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11828 int block_num = r->num;
11830 if (r->label)
11832 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11833 lab = r->label;
11835 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11836 base = NULL;
11837 if (block_num > 0)
11839 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11840 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11842 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11843 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11845 if (HAVE_AS_LEB128)
11847 /* If all code is in the text section, then the compilation
11848 unit base address defaults to DW_AT_low_pc, which is the
11849 base of the text section. */
11850 if (!have_multiple_function_sections)
11852 dw2_asm_output_data (1, DW_RLE_offset_pair,
11853 "DW_RLE_offset_pair (%s)", lab);
11854 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11855 "Range begin address (%s)", lab);
11856 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11857 "Range end address (%s)", lab);
11858 continue;
11860 if (base == NULL)
11862 dw_ranges *r2 = NULL;
11863 if (i < len - 1)
11864 r2 = &(*ranges_table)[i + 1];
11865 if (r2
11866 && r2->num != 0
11867 && r2->label == NULL
11868 && !r2->maybe_new_sec)
11870 dw2_asm_output_data (1, DW_RLE_base_address,
11871 "DW_RLE_base_address (%s)", lab);
11872 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11873 "Base address (%s)", lab);
11874 strcpy (basebuf, blabel);
11875 base = basebuf;
11878 if (base)
11880 dw2_asm_output_data (1, DW_RLE_offset_pair,
11881 "DW_RLE_offset_pair (%s)", lab);
11882 dw2_asm_output_delta_uleb128 (blabel, base,
11883 "Range begin address (%s)", lab);
11884 dw2_asm_output_delta_uleb128 (elabel, base,
11885 "Range end address (%s)", lab);
11886 continue;
11888 dw2_asm_output_data (1, DW_RLE_start_length,
11889 "DW_RLE_start_length (%s)", lab);
11890 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11891 "Range begin address (%s)", lab);
11892 dw2_asm_output_delta_uleb128 (elabel, blabel,
11893 "Range length (%s)", lab);
11895 else
11897 dw2_asm_output_data (1, DW_RLE_start_end,
11898 "DW_RLE_start_end (%s)", lab);
11899 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11900 "Range begin address (%s)", lab);
11901 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11902 "Range end address (%s)", lab);
11906 /* Negative block_num stands for an index into ranges_by_label. */
11907 else if (block_num < 0)
11909 int lab_idx = - block_num - 1;
11910 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11911 const char *elabel = (*ranges_by_label)[lab_idx].end;
11913 if (!have_multiple_function_sections)
11914 gcc_unreachable ();
11915 if (HAVE_AS_LEB128)
11917 dw2_asm_output_data (1, DW_RLE_start_length,
11918 "DW_RLE_start_length (%s)", lab);
11919 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11920 "Range begin address (%s)", lab);
11921 dw2_asm_output_delta_uleb128 (elabel, blabel,
11922 "Range length (%s)", lab);
11924 else
11926 dw2_asm_output_data (1, DW_RLE_start_end,
11927 "DW_RLE_start_end (%s)", lab);
11928 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11929 "Range begin address (%s)", lab);
11930 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11931 "Range end address (%s)", lab);
11934 else
11935 dw2_asm_output_data (1, DW_RLE_end_of_list,
11936 "DW_RLE_end_of_list (%s)", lab);
11938 ASM_OUTPUT_LABEL (asm_out_file, l2);
11941 /* Data structure containing information about input files. */
11942 struct file_info
11944 const char *path; /* Complete file name. */
11945 const char *fname; /* File name part. */
11946 int length; /* Length of entire string. */
11947 struct dwarf_file_data * file_idx; /* Index in input file table. */
11948 int dir_idx; /* Index in directory table. */
11951 /* Data structure containing information about directories with source
11952 files. */
11953 struct dir_info
11955 const char *path; /* Path including directory name. */
11956 int length; /* Path length. */
11957 int prefix; /* Index of directory entry which is a prefix. */
11958 int count; /* Number of files in this directory. */
11959 int dir_idx; /* Index of directory used as base. */
11962 /* Callback function for file_info comparison. We sort by looking at
11963 the directories in the path. */
11965 static int
11966 file_info_cmp (const void *p1, const void *p2)
11968 const struct file_info *const s1 = (const struct file_info *) p1;
11969 const struct file_info *const s2 = (const struct file_info *) p2;
11970 const unsigned char *cp1;
11971 const unsigned char *cp2;
11973 /* Take care of file names without directories. We need to make sure that
11974 we return consistent values to qsort since some will get confused if
11975 we return the same value when identical operands are passed in opposite
11976 orders. So if neither has a directory, return 0 and otherwise return
11977 1 or -1 depending on which one has the directory. We want the one with
11978 the directory to sort after the one without, so all no directory files
11979 are at the start (normally only the compilation unit file). */
11980 if ((s1->path == s1->fname || s2->path == s2->fname))
11981 return (s2->path == s2->fname) - (s1->path == s1->fname);
11983 cp1 = (const unsigned char *) s1->path;
11984 cp2 = (const unsigned char *) s2->path;
11986 while (1)
11988 ++cp1;
11989 ++cp2;
11990 /* Reached the end of the first path? If so, handle like above,
11991 but now we want longer directory prefixes before shorter ones. */
11992 if ((cp1 == (const unsigned char *) s1->fname)
11993 || (cp2 == (const unsigned char *) s2->fname))
11994 return ((cp1 == (const unsigned char *) s1->fname)
11995 - (cp2 == (const unsigned char *) s2->fname));
11997 /* Character of current path component the same? */
11998 else if (*cp1 != *cp2)
11999 return *cp1 - *cp2;
12003 struct file_name_acquire_data
12005 struct file_info *files;
12006 int used_files;
12007 int max_files;
12010 /* Traversal function for the hash table. */
12013 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12015 struct dwarf_file_data *d = *slot;
12016 struct file_info *fi;
12017 const char *f;
12019 gcc_assert (fnad->max_files >= d->emitted_number);
12021 if (! d->emitted_number)
12022 return 1;
12024 gcc_assert (fnad->max_files != fnad->used_files);
12026 fi = fnad->files + fnad->used_files++;
12028 /* Skip all leading "./". */
12029 f = d->filename;
12030 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12031 f += 2;
12033 /* Create a new array entry. */
12034 fi->path = f;
12035 fi->length = strlen (f);
12036 fi->file_idx = d;
12038 /* Search for the file name part. */
12039 f = strrchr (f, DIR_SEPARATOR);
12040 #if defined (DIR_SEPARATOR_2)
12042 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12044 if (g != NULL)
12046 if (f == NULL || f < g)
12047 f = g;
12050 #endif
12052 fi->fname = f == NULL ? fi->path : f + 1;
12053 return 1;
12056 /* Helper function for output_file_names. Emit a FORM encoded
12057 string STR, with assembly comment start ENTRY_KIND and
12058 index IDX */
12060 static void
12061 output_line_string (enum dwarf_form form, const char *str,
12062 const char *entry_kind, unsigned int idx)
12064 switch (form)
12066 case DW_FORM_string:
12067 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12068 break;
12069 case DW_FORM_line_strp:
12070 if (!debug_line_str_hash)
12071 debug_line_str_hash
12072 = hash_table<indirect_string_hasher>::create_ggc (10);
12074 struct indirect_string_node *node;
12075 node = find_AT_string_in_table (str, debug_line_str_hash);
12076 set_indirect_string (node);
12077 node->form = form;
12078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12079 debug_line_str_section, "%s: %#x: \"%s\"",
12080 entry_kind, 0, node->str);
12081 break;
12082 default:
12083 gcc_unreachable ();
12087 /* Output the directory table and the file name table. We try to minimize
12088 the total amount of memory needed. A heuristic is used to avoid large
12089 slowdowns with many input files. */
12091 static void
12092 output_file_names (void)
12094 struct file_name_acquire_data fnad;
12095 int numfiles;
12096 struct file_info *files;
12097 struct dir_info *dirs;
12098 int *saved;
12099 int *savehere;
12100 int *backmap;
12101 int ndirs;
12102 int idx_offset;
12103 int i;
12105 if (!last_emitted_file)
12107 if (dwarf_version >= 5)
12109 dw2_asm_output_data (1, 0, "Directory entry format count");
12110 dw2_asm_output_data_uleb128 (0, "Directories count");
12111 dw2_asm_output_data (1, 0, "File name entry format count");
12112 dw2_asm_output_data_uleb128 (0, "File names count");
12114 else
12116 dw2_asm_output_data (1, 0, "End directory table");
12117 dw2_asm_output_data (1, 0, "End file name table");
12119 return;
12122 numfiles = last_emitted_file->emitted_number;
12124 /* Allocate the various arrays we need. */
12125 files = XALLOCAVEC (struct file_info, numfiles);
12126 dirs = XALLOCAVEC (struct dir_info, numfiles);
12128 fnad.files = files;
12129 fnad.used_files = 0;
12130 fnad.max_files = numfiles;
12131 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12132 gcc_assert (fnad.used_files == fnad.max_files);
12134 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12136 /* Find all the different directories used. */
12137 dirs[0].path = files[0].path;
12138 dirs[0].length = files[0].fname - files[0].path;
12139 dirs[0].prefix = -1;
12140 dirs[0].count = 1;
12141 dirs[0].dir_idx = 0;
12142 files[0].dir_idx = 0;
12143 ndirs = 1;
12145 for (i = 1; i < numfiles; i++)
12146 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12147 && memcmp (dirs[ndirs - 1].path, files[i].path,
12148 dirs[ndirs - 1].length) == 0)
12150 /* Same directory as last entry. */
12151 files[i].dir_idx = ndirs - 1;
12152 ++dirs[ndirs - 1].count;
12154 else
12156 int j;
12158 /* This is a new directory. */
12159 dirs[ndirs].path = files[i].path;
12160 dirs[ndirs].length = files[i].fname - files[i].path;
12161 dirs[ndirs].count = 1;
12162 dirs[ndirs].dir_idx = ndirs;
12163 files[i].dir_idx = ndirs;
12165 /* Search for a prefix. */
12166 dirs[ndirs].prefix = -1;
12167 for (j = 0; j < ndirs; j++)
12168 if (dirs[j].length < dirs[ndirs].length
12169 && dirs[j].length > 1
12170 && (dirs[ndirs].prefix == -1
12171 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12172 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12173 dirs[ndirs].prefix = j;
12175 ++ndirs;
12178 /* Now to the actual work. We have to find a subset of the directories which
12179 allow expressing the file name using references to the directory table
12180 with the least amount of characters. We do not do an exhaustive search
12181 where we would have to check out every combination of every single
12182 possible prefix. Instead we use a heuristic which provides nearly optimal
12183 results in most cases and never is much off. */
12184 saved = XALLOCAVEC (int, ndirs);
12185 savehere = XALLOCAVEC (int, ndirs);
12187 memset (saved, '\0', ndirs * sizeof (saved[0]));
12188 for (i = 0; i < ndirs; i++)
12190 int j;
12191 int total;
12193 /* We can always save some space for the current directory. But this
12194 does not mean it will be enough to justify adding the directory. */
12195 savehere[i] = dirs[i].length;
12196 total = (savehere[i] - saved[i]) * dirs[i].count;
12198 for (j = i + 1; j < ndirs; j++)
12200 savehere[j] = 0;
12201 if (saved[j] < dirs[i].length)
12203 /* Determine whether the dirs[i] path is a prefix of the
12204 dirs[j] path. */
12205 int k;
12207 k = dirs[j].prefix;
12208 while (k != -1 && k != (int) i)
12209 k = dirs[k].prefix;
12211 if (k == (int) i)
12213 /* Yes it is. We can possibly save some memory by
12214 writing the filenames in dirs[j] relative to
12215 dirs[i]. */
12216 savehere[j] = dirs[i].length;
12217 total += (savehere[j] - saved[j]) * dirs[j].count;
12222 /* Check whether we can save enough to justify adding the dirs[i]
12223 directory. */
12224 if (total > dirs[i].length + 1)
12226 /* It's worthwhile adding. */
12227 for (j = i; j < ndirs; j++)
12228 if (savehere[j] > 0)
12230 /* Remember how much we saved for this directory so far. */
12231 saved[j] = savehere[j];
12233 /* Remember the prefix directory. */
12234 dirs[j].dir_idx = i;
12239 /* Emit the directory name table. */
12240 idx_offset = dirs[0].length > 0 ? 1 : 0;
12241 enum dwarf_form str_form = DW_FORM_string;
12242 enum dwarf_form idx_form = DW_FORM_udata;
12243 if (dwarf_version >= 5)
12245 const char *comp_dir = comp_dir_string ();
12246 if (comp_dir == NULL)
12247 comp_dir = "";
12248 dw2_asm_output_data (1, 1, "Directory entry format count");
12249 if (DWARF5_USE_DEBUG_LINE_STR)
12250 str_form = DW_FORM_line_strp;
12251 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12252 dw2_asm_output_data_uleb128 (str_form, "%s",
12253 get_DW_FORM_name (str_form));
12254 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12255 if (str_form == DW_FORM_string)
12257 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12258 for (i = 1 - idx_offset; i < ndirs; i++)
12259 dw2_asm_output_nstring (dirs[i].path,
12260 dirs[i].length
12261 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12262 "Directory Entry: %#x", i + idx_offset);
12264 else
12266 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12267 for (i = 1 - idx_offset; i < ndirs; i++)
12269 const char *str
12270 = ggc_alloc_string (dirs[i].path,
12271 dirs[i].length
12272 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12273 output_line_string (str_form, str, "Directory Entry",
12274 (unsigned) i + idx_offset);
12278 else
12280 for (i = 1 - idx_offset; i < ndirs; i++)
12281 dw2_asm_output_nstring (dirs[i].path,
12282 dirs[i].length
12283 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12284 "Directory Entry: %#x", i + idx_offset);
12286 dw2_asm_output_data (1, 0, "End directory table");
12289 /* We have to emit them in the order of emitted_number since that's
12290 used in the debug info generation. To do this efficiently we
12291 generate a back-mapping of the indices first. */
12292 backmap = XALLOCAVEC (int, numfiles);
12293 for (i = 0; i < numfiles; i++)
12294 backmap[files[i].file_idx->emitted_number - 1] = i;
12296 if (dwarf_version >= 5)
12298 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12299 if (filename0 == NULL)
12300 filename0 = "";
12301 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12302 DW_FORM_data2. Choose one based on the number of directories
12303 and how much space would they occupy in each encoding.
12304 If we have at most 256 directories, all indexes fit into
12305 a single byte, so DW_FORM_data1 is most compact (if there
12306 are at most 128 directories, DW_FORM_udata would be as
12307 compact as that, but not shorter and slower to decode). */
12308 if (ndirs + idx_offset <= 256)
12309 idx_form = DW_FORM_data1;
12310 /* If there are more than 65536 directories, we have to use
12311 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12312 Otherwise, compute what space would occupy if all the indexes
12313 used DW_FORM_udata - sum - and compare that to how large would
12314 be DW_FORM_data2 encoding, and pick the more efficient one. */
12315 else if (ndirs + idx_offset <= 65536)
12317 unsigned HOST_WIDE_INT sum = 1;
12318 for (i = 0; i < numfiles; i++)
12320 int file_idx = backmap[i];
12321 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12322 sum += size_of_uleb128 (dir_idx);
12324 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12325 idx_form = DW_FORM_data2;
12327 #ifdef VMS_DEBUGGING_INFO
12328 dw2_asm_output_data (1, 4, "File name entry format count");
12329 #else
12330 dw2_asm_output_data (1, 2, "File name entry format count");
12331 #endif
12332 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12333 dw2_asm_output_data_uleb128 (str_form, "%s",
12334 get_DW_FORM_name (str_form));
12335 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12336 "DW_LNCT_directory_index");
12337 dw2_asm_output_data_uleb128 (idx_form, "%s",
12338 get_DW_FORM_name (idx_form));
12339 #ifdef VMS_DEBUGGING_INFO
12340 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12341 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12342 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12343 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12344 #endif
12345 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12347 output_line_string (str_form, filename0, "File Entry", 0);
12349 /* Include directory index. */
12350 if (idx_form != DW_FORM_udata)
12351 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12352 0, NULL);
12353 else
12354 dw2_asm_output_data_uleb128 (0, NULL);
12356 #ifdef VMS_DEBUGGING_INFO
12357 dw2_asm_output_data_uleb128 (0, NULL);
12358 dw2_asm_output_data_uleb128 (0, NULL);
12359 #endif
12362 /* Now write all the file names. */
12363 for (i = 0; i < numfiles; i++)
12365 int file_idx = backmap[i];
12366 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12368 #ifdef VMS_DEBUGGING_INFO
12369 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12371 /* Setting these fields can lead to debugger miscomparisons,
12372 but VMS Debug requires them to be set correctly. */
12374 int ver;
12375 long long cdt;
12376 long siz;
12377 int maxfilelen = (strlen (files[file_idx].path)
12378 + dirs[dir_idx].length
12379 + MAX_VMS_VERSION_LEN + 1);
12380 char *filebuf = XALLOCAVEC (char, maxfilelen);
12382 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12383 snprintf (filebuf, maxfilelen, "%s;%d",
12384 files[file_idx].path + dirs[dir_idx].length, ver);
12386 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12388 /* Include directory index. */
12389 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12390 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12391 dir_idx + idx_offset, NULL);
12392 else
12393 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12395 /* Modification time. */
12396 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12397 &cdt, 0, 0, 0) == 0)
12398 ? cdt : 0, NULL);
12400 /* File length in bytes. */
12401 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12402 0, &siz, 0, 0) == 0)
12403 ? siz : 0, NULL);
12404 #else
12405 output_line_string (str_form,
12406 files[file_idx].path + dirs[dir_idx].length,
12407 "File Entry", (unsigned) i + 1);
12409 /* Include directory index. */
12410 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12411 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12412 dir_idx + idx_offset, NULL);
12413 else
12414 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12416 if (dwarf_version >= 5)
12417 continue;
12419 /* Modification time. */
12420 dw2_asm_output_data_uleb128 (0, NULL);
12422 /* File length in bytes. */
12423 dw2_asm_output_data_uleb128 (0, NULL);
12424 #endif /* VMS_DEBUGGING_INFO */
12427 if (dwarf_version < 5)
12428 dw2_asm_output_data (1, 0, "End file name table");
12432 /* Output one line number table into the .debug_line section. */
12434 static void
12435 output_one_line_info_table (dw_line_info_table *table)
12437 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12438 unsigned int current_line = 1;
12439 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12440 dw_line_info_entry *ent, *prev_addr;
12441 size_t i;
12442 unsigned int view;
12444 view = 0;
12446 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12448 switch (ent->opcode)
12450 case LI_set_address:
12451 /* ??? Unfortunately, we have little choice here currently, and
12452 must always use the most general form. GCC does not know the
12453 address delta itself, so we can't use DW_LNS_advance_pc. Many
12454 ports do have length attributes which will give an upper bound
12455 on the address range. We could perhaps use length attributes
12456 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12457 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12459 view = 0;
12461 /* This can handle any delta. This takes
12462 4+DWARF2_ADDR_SIZE bytes. */
12463 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12464 debug_variable_location_views
12465 ? ", reset view to 0" : "");
12466 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12467 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12468 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12470 prev_addr = ent;
12471 break;
12473 case LI_adv_address:
12475 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12476 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12477 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12479 view++;
12481 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12482 dw2_asm_output_delta (2, line_label, prev_label,
12483 "from %s to %s", prev_label, line_label);
12485 prev_addr = ent;
12486 break;
12489 case LI_set_line:
12490 if (ent->val == current_line)
12492 /* We still need to start a new row, so output a copy insn. */
12493 dw2_asm_output_data (1, DW_LNS_copy,
12494 "copy line %u", current_line);
12496 else
12498 int line_offset = ent->val - current_line;
12499 int line_delta = line_offset - DWARF_LINE_BASE;
12501 current_line = ent->val;
12502 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12504 /* This can handle deltas from -10 to 234, using the current
12505 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12506 This takes 1 byte. */
12507 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12508 "line %u", current_line);
12510 else
12512 /* This can handle any delta. This takes at least 4 bytes,
12513 depending on the value being encoded. */
12514 dw2_asm_output_data (1, DW_LNS_advance_line,
12515 "advance to line %u", current_line);
12516 dw2_asm_output_data_sleb128 (line_offset, NULL);
12517 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12520 break;
12522 case LI_set_file:
12523 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12524 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12525 break;
12527 case LI_set_column:
12528 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12529 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12530 break;
12532 case LI_negate_stmt:
12533 current_is_stmt = !current_is_stmt;
12534 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12535 "is_stmt %d", current_is_stmt);
12536 break;
12538 case LI_set_prologue_end:
12539 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12540 "set prologue end");
12541 break;
12543 case LI_set_epilogue_begin:
12544 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12545 "set epilogue begin");
12546 break;
12548 case LI_set_discriminator:
12549 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12550 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12551 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12552 dw2_asm_output_data_uleb128 (ent->val, NULL);
12553 break;
12557 /* Emit debug info for the address of the end of the table. */
12558 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12559 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12560 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12561 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12563 dw2_asm_output_data (1, 0, "end sequence");
12564 dw2_asm_output_data_uleb128 (1, NULL);
12565 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12568 /* Output the source line number correspondence information. This
12569 information goes into the .debug_line section. */
12571 static void
12572 output_line_info (bool prologue_only)
12574 static unsigned int generation;
12575 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12576 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12577 bool saw_one = false;
12578 int opc;
12580 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12581 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12582 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12583 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12585 if (!XCOFF_DEBUGGING_INFO)
12587 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12588 dw2_asm_output_data (4, 0xffffffff,
12589 "Initial length escape value indicating 64-bit DWARF extension");
12590 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12591 "Length of Source Line Info");
12594 ASM_OUTPUT_LABEL (asm_out_file, l1);
12596 output_dwarf_version ();
12597 if (dwarf_version >= 5)
12599 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12600 dw2_asm_output_data (1, 0, "Segment Size");
12602 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12603 ASM_OUTPUT_LABEL (asm_out_file, p1);
12605 /* Define the architecture-dependent minimum instruction length (in bytes).
12606 In this implementation of DWARF, this field is used for information
12607 purposes only. Since GCC generates assembly language, we have no
12608 a priori knowledge of how many instruction bytes are generated for each
12609 source line, and therefore can use only the DW_LNE_set_address and
12610 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12611 this as '1', which is "correct enough" for all architectures,
12612 and don't let the target override. */
12613 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12615 if (dwarf_version >= 4)
12616 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12617 "Maximum Operations Per Instruction");
12618 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12619 "Default is_stmt_start flag");
12620 dw2_asm_output_data (1, DWARF_LINE_BASE,
12621 "Line Base Value (Special Opcodes)");
12622 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12623 "Line Range Value (Special Opcodes)");
12624 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12625 "Special Opcode Base");
12627 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12629 int n_op_args;
12630 switch (opc)
12632 case DW_LNS_advance_pc:
12633 case DW_LNS_advance_line:
12634 case DW_LNS_set_file:
12635 case DW_LNS_set_column:
12636 case DW_LNS_fixed_advance_pc:
12637 case DW_LNS_set_isa:
12638 n_op_args = 1;
12639 break;
12640 default:
12641 n_op_args = 0;
12642 break;
12645 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12646 opc, n_op_args);
12649 /* Write out the information about the files we use. */
12650 output_file_names ();
12651 ASM_OUTPUT_LABEL (asm_out_file, p2);
12652 if (prologue_only)
12654 /* Output the marker for the end of the line number info. */
12655 ASM_OUTPUT_LABEL (asm_out_file, l2);
12656 return;
12659 if (separate_line_info)
12661 dw_line_info_table *table;
12662 size_t i;
12664 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12665 if (table->in_use)
12667 output_one_line_info_table (table);
12668 saw_one = true;
12671 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12673 output_one_line_info_table (cold_text_section_line_info);
12674 saw_one = true;
12677 /* ??? Some Darwin linkers crash on a .debug_line section with no
12678 sequences. Further, merely a DW_LNE_end_sequence entry is not
12679 sufficient -- the address column must also be initialized.
12680 Make sure to output at least one set_address/end_sequence pair,
12681 choosing .text since that section is always present. */
12682 if (text_section_line_info->in_use || !saw_one)
12683 output_one_line_info_table (text_section_line_info);
12685 /* Output the marker for the end of the line number info. */
12686 ASM_OUTPUT_LABEL (asm_out_file, l2);
12689 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12691 static inline bool
12692 need_endianity_attribute_p (bool reverse)
12694 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12697 /* Given a pointer to a tree node for some base type, return a pointer to
12698 a DIE that describes the given type. REVERSE is true if the type is
12699 to be interpreted in the reverse storage order wrt the target order.
12701 This routine must only be called for GCC type nodes that correspond to
12702 Dwarf base (fundamental) types. */
12704 static dw_die_ref
12705 base_type_die (tree type, bool reverse)
12707 dw_die_ref base_type_result;
12708 enum dwarf_type encoding;
12709 bool fpt_used = false;
12710 struct fixed_point_type_info fpt_info;
12711 tree type_bias = NULL_TREE;
12713 /* If this is a subtype that should not be emitted as a subrange type,
12714 use the base type. See subrange_type_for_debug_p. */
12715 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12716 type = TREE_TYPE (type);
12718 switch (TREE_CODE (type))
12720 case INTEGER_TYPE:
12721 if ((dwarf_version >= 4 || !dwarf_strict)
12722 && TYPE_NAME (type)
12723 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12724 && DECL_IS_BUILTIN (TYPE_NAME (type))
12725 && DECL_NAME (TYPE_NAME (type)))
12727 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12728 if (strcmp (name, "char16_t") == 0
12729 || strcmp (name, "char32_t") == 0)
12731 encoding = DW_ATE_UTF;
12732 break;
12735 if ((dwarf_version >= 3 || !dwarf_strict)
12736 && lang_hooks.types.get_fixed_point_type_info)
12738 memset (&fpt_info, 0, sizeof (fpt_info));
12739 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12741 fpt_used = true;
12742 encoding = ((TYPE_UNSIGNED (type))
12743 ? DW_ATE_unsigned_fixed
12744 : DW_ATE_signed_fixed);
12745 break;
12748 if (TYPE_STRING_FLAG (type))
12750 if (TYPE_UNSIGNED (type))
12751 encoding = DW_ATE_unsigned_char;
12752 else
12753 encoding = DW_ATE_signed_char;
12755 else if (TYPE_UNSIGNED (type))
12756 encoding = DW_ATE_unsigned;
12757 else
12758 encoding = DW_ATE_signed;
12760 if (!dwarf_strict
12761 && lang_hooks.types.get_type_bias)
12762 type_bias = lang_hooks.types.get_type_bias (type);
12763 break;
12765 case REAL_TYPE:
12766 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12768 if (dwarf_version >= 3 || !dwarf_strict)
12769 encoding = DW_ATE_decimal_float;
12770 else
12771 encoding = DW_ATE_lo_user;
12773 else
12774 encoding = DW_ATE_float;
12775 break;
12777 case FIXED_POINT_TYPE:
12778 if (!(dwarf_version >= 3 || !dwarf_strict))
12779 encoding = DW_ATE_lo_user;
12780 else if (TYPE_UNSIGNED (type))
12781 encoding = DW_ATE_unsigned_fixed;
12782 else
12783 encoding = DW_ATE_signed_fixed;
12784 break;
12786 /* Dwarf2 doesn't know anything about complex ints, so use
12787 a user defined type for it. */
12788 case COMPLEX_TYPE:
12789 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12790 encoding = DW_ATE_complex_float;
12791 else
12792 encoding = DW_ATE_lo_user;
12793 break;
12795 case BOOLEAN_TYPE:
12796 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12797 encoding = DW_ATE_boolean;
12798 break;
12800 default:
12801 /* No other TREE_CODEs are Dwarf fundamental types. */
12802 gcc_unreachable ();
12805 base_type_result = new_die_raw (DW_TAG_base_type);
12807 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12808 int_size_in_bytes (type));
12809 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12811 if (need_endianity_attribute_p (reverse))
12812 add_AT_unsigned (base_type_result, DW_AT_endianity,
12813 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12815 add_alignment_attribute (base_type_result, type);
12817 if (fpt_used)
12819 switch (fpt_info.scale_factor_kind)
12821 case fixed_point_scale_factor_binary:
12822 add_AT_int (base_type_result, DW_AT_binary_scale,
12823 fpt_info.scale_factor.binary);
12824 break;
12826 case fixed_point_scale_factor_decimal:
12827 add_AT_int (base_type_result, DW_AT_decimal_scale,
12828 fpt_info.scale_factor.decimal);
12829 break;
12831 case fixed_point_scale_factor_arbitrary:
12832 /* Arbitrary scale factors cannot be described in standard DWARF,
12833 yet. */
12834 if (!dwarf_strict)
12836 /* Describe the scale factor as a rational constant. */
12837 const dw_die_ref scale_factor
12838 = new_die (DW_TAG_constant, comp_unit_die (), type);
12840 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12841 fpt_info.scale_factor.arbitrary.numerator);
12842 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12843 fpt_info.scale_factor.arbitrary.denominator);
12845 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12847 break;
12849 default:
12850 gcc_unreachable ();
12854 if (type_bias)
12855 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12856 dw_scalar_form_constant
12857 | dw_scalar_form_exprloc
12858 | dw_scalar_form_reference,
12859 NULL);
12861 return base_type_result;
12864 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12865 named 'auto' in its type: return true for it, false otherwise. */
12867 static inline bool
12868 is_cxx_auto (tree type)
12870 if (is_cxx ())
12872 tree name = TYPE_IDENTIFIER (type);
12873 if (name == get_identifier ("auto")
12874 || name == get_identifier ("decltype(auto)"))
12875 return true;
12877 return false;
12880 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12881 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12883 static inline int
12884 is_base_type (tree type)
12886 switch (TREE_CODE (type))
12888 case INTEGER_TYPE:
12889 case REAL_TYPE:
12890 case FIXED_POINT_TYPE:
12891 case COMPLEX_TYPE:
12892 case BOOLEAN_TYPE:
12893 return 1;
12895 case VOID_TYPE:
12896 case ARRAY_TYPE:
12897 case RECORD_TYPE:
12898 case UNION_TYPE:
12899 case QUAL_UNION_TYPE:
12900 case ENUMERAL_TYPE:
12901 case FUNCTION_TYPE:
12902 case METHOD_TYPE:
12903 case POINTER_TYPE:
12904 case REFERENCE_TYPE:
12905 case NULLPTR_TYPE:
12906 case OFFSET_TYPE:
12907 case LANG_TYPE:
12908 case VECTOR_TYPE:
12909 return 0;
12911 default:
12912 if (is_cxx_auto (type))
12913 return 0;
12914 gcc_unreachable ();
12917 return 0;
12920 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12921 node, return the size in bits for the type if it is a constant, or else
12922 return the alignment for the type if the type's size is not constant, or
12923 else return BITS_PER_WORD if the type actually turns out to be an
12924 ERROR_MARK node. */
12926 static inline unsigned HOST_WIDE_INT
12927 simple_type_size_in_bits (const_tree type)
12929 if (TREE_CODE (type) == ERROR_MARK)
12930 return BITS_PER_WORD;
12931 else if (TYPE_SIZE (type) == NULL_TREE)
12932 return 0;
12933 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12934 return tree_to_uhwi (TYPE_SIZE (type));
12935 else
12936 return TYPE_ALIGN (type);
12939 /* Similarly, but return an offset_int instead of UHWI. */
12941 static inline offset_int
12942 offset_int_type_size_in_bits (const_tree type)
12944 if (TREE_CODE (type) == ERROR_MARK)
12945 return BITS_PER_WORD;
12946 else if (TYPE_SIZE (type) == NULL_TREE)
12947 return 0;
12948 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12949 return wi::to_offset (TYPE_SIZE (type));
12950 else
12951 return TYPE_ALIGN (type);
12954 /* Given a pointer to a tree node for a subrange type, return a pointer
12955 to a DIE that describes the given type. */
12957 static dw_die_ref
12958 subrange_type_die (tree type, tree low, tree high, tree bias,
12959 dw_die_ref context_die)
12961 dw_die_ref subrange_die;
12962 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12964 if (context_die == NULL)
12965 context_die = comp_unit_die ();
12967 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12969 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12971 /* The size of the subrange type and its base type do not match,
12972 so we need to generate a size attribute for the subrange type. */
12973 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12976 add_alignment_attribute (subrange_die, type);
12978 if (low)
12979 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12980 if (high)
12981 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12982 if (bias && !dwarf_strict)
12983 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12984 dw_scalar_form_constant
12985 | dw_scalar_form_exprloc
12986 | dw_scalar_form_reference,
12987 NULL);
12989 return subrange_die;
12992 /* Returns the (const and/or volatile) cv_qualifiers associated with
12993 the decl node. This will normally be augmented with the
12994 cv_qualifiers of the underlying type in add_type_attribute. */
12996 static int
12997 decl_quals (const_tree decl)
12999 return ((TREE_READONLY (decl)
13000 /* The C++ front-end correctly marks reference-typed
13001 variables as readonly, but from a language (and debug
13002 info) standpoint they are not const-qualified. */
13003 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13004 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13005 | (TREE_THIS_VOLATILE (decl)
13006 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13009 /* Determine the TYPE whose qualifiers match the largest strict subset
13010 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13011 qualifiers outside QUAL_MASK. */
13013 static int
13014 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13016 tree t;
13017 int best_rank = 0, best_qual = 0, max_rank;
13019 type_quals &= qual_mask;
13020 max_rank = popcount_hwi (type_quals) - 1;
13022 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13023 t = TYPE_NEXT_VARIANT (t))
13025 int q = TYPE_QUALS (t) & qual_mask;
13027 if ((q & type_quals) == q && q != type_quals
13028 && check_base_type (t, type))
13030 int rank = popcount_hwi (q);
13032 if (rank > best_rank)
13034 best_rank = rank;
13035 best_qual = q;
13040 return best_qual;
13043 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13044 static const dwarf_qual_info_t dwarf_qual_info[] =
13046 { TYPE_QUAL_CONST, DW_TAG_const_type },
13047 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13048 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13049 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13051 static const unsigned int dwarf_qual_info_size
13052 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13054 /* If DIE is a qualified DIE of some base DIE with the same parent,
13055 return the base DIE, otherwise return NULL. Set MASK to the
13056 qualifiers added compared to the returned DIE. */
13058 static dw_die_ref
13059 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13061 unsigned int i;
13062 for (i = 0; i < dwarf_qual_info_size; i++)
13063 if (die->die_tag == dwarf_qual_info[i].t)
13064 break;
13065 if (i == dwarf_qual_info_size)
13066 return NULL;
13067 if (vec_safe_length (die->die_attr) != 1)
13068 return NULL;
13069 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13070 if (type == NULL || type->die_parent != die->die_parent)
13071 return NULL;
13072 *mask |= dwarf_qual_info[i].q;
13073 if (depth)
13075 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13076 if (ret)
13077 return ret;
13079 return type;
13082 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13083 entry that chains the modifiers specified by CV_QUALS in front of the
13084 given type. REVERSE is true if the type is to be interpreted in the
13085 reverse storage order wrt the target order. */
13087 static dw_die_ref
13088 modified_type_die (tree type, int cv_quals, bool reverse,
13089 dw_die_ref context_die)
13091 enum tree_code code = TREE_CODE (type);
13092 dw_die_ref mod_type_die;
13093 dw_die_ref sub_die = NULL;
13094 tree item_type = NULL;
13095 tree qualified_type;
13096 tree name, low, high;
13097 dw_die_ref mod_scope;
13098 /* Only these cv-qualifiers are currently handled. */
13099 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13100 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13101 ENCODE_QUAL_ADDR_SPACE(~0U));
13102 const bool reverse_base_type
13103 = need_endianity_attribute_p (reverse) && is_base_type (type);
13105 if (code == ERROR_MARK)
13106 return NULL;
13108 if (lang_hooks.types.get_debug_type)
13110 tree debug_type = lang_hooks.types.get_debug_type (type);
13112 if (debug_type != NULL_TREE && debug_type != type)
13113 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13116 cv_quals &= cv_qual_mask;
13118 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13119 tag modifier (and not an attribute) old consumers won't be able
13120 to handle it. */
13121 if (dwarf_version < 3)
13122 cv_quals &= ~TYPE_QUAL_RESTRICT;
13124 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13125 if (dwarf_version < 5)
13126 cv_quals &= ~TYPE_QUAL_ATOMIC;
13128 /* See if we already have the appropriately qualified variant of
13129 this type. */
13130 qualified_type = get_qualified_type (type, cv_quals);
13132 if (qualified_type == sizetype)
13134 /* Try not to expose the internal sizetype type's name. */
13135 if (TYPE_NAME (qualified_type)
13136 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13138 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13140 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13141 && (TYPE_PRECISION (t)
13142 == TYPE_PRECISION (qualified_type))
13143 && (TYPE_UNSIGNED (t)
13144 == TYPE_UNSIGNED (qualified_type)));
13145 qualified_type = t;
13147 else if (qualified_type == sizetype
13148 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13149 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13150 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13151 qualified_type = size_type_node;
13154 /* If we do, then we can just use its DIE, if it exists. */
13155 if (qualified_type)
13157 mod_type_die = lookup_type_die (qualified_type);
13159 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13160 dealt with specially: the DIE with the attribute, if it exists, is
13161 placed immediately after the regular DIE for the same base type. */
13162 if (mod_type_die
13163 && (!reverse_base_type
13164 || ((mod_type_die = mod_type_die->die_sib) != NULL
13165 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13166 return mod_type_die;
13169 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13171 /* Handle C typedef types. */
13172 if (name
13173 && TREE_CODE (name) == TYPE_DECL
13174 && DECL_ORIGINAL_TYPE (name)
13175 && !DECL_ARTIFICIAL (name))
13177 tree dtype = TREE_TYPE (name);
13179 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13180 if (qualified_type == dtype && !reverse_base_type)
13182 tree origin = decl_ultimate_origin (name);
13184 /* Typedef variants that have an abstract origin don't get their own
13185 type DIE (see gen_typedef_die), so fall back on the ultimate
13186 abstract origin instead. */
13187 if (origin != NULL && origin != name)
13188 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13189 context_die);
13191 /* For a named type, use the typedef. */
13192 gen_type_die (qualified_type, context_die);
13193 return lookup_type_die (qualified_type);
13195 else
13197 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13198 dquals &= cv_qual_mask;
13199 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13200 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13201 /* cv-unqualified version of named type. Just use
13202 the unnamed type to which it refers. */
13203 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13204 reverse, context_die);
13205 /* Else cv-qualified version of named type; fall through. */
13209 mod_scope = scope_die_for (type, context_die);
13211 if (cv_quals)
13213 int sub_quals = 0, first_quals = 0;
13214 unsigned i;
13215 dw_die_ref first = NULL, last = NULL;
13217 /* Determine a lesser qualified type that most closely matches
13218 this one. Then generate DW_TAG_* entries for the remaining
13219 qualifiers. */
13220 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13221 cv_qual_mask);
13222 if (sub_quals && use_debug_types)
13224 bool needed = false;
13225 /* If emitting type units, make sure the order of qualifiers
13226 is canonical. Thus, start from unqualified type if
13227 an earlier qualifier is missing in sub_quals, but some later
13228 one is present there. */
13229 for (i = 0; i < dwarf_qual_info_size; i++)
13230 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13231 needed = true;
13232 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13234 sub_quals = 0;
13235 break;
13238 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13239 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13241 /* As not all intermediate qualified DIEs have corresponding
13242 tree types, ensure that qualified DIEs in the same scope
13243 as their DW_AT_type are emitted after their DW_AT_type,
13244 only with other qualified DIEs for the same type possibly
13245 in between them. Determine the range of such qualified
13246 DIEs now (first being the base type, last being corresponding
13247 last qualified DIE for it). */
13248 unsigned int count = 0;
13249 first = qualified_die_p (mod_type_die, &first_quals,
13250 dwarf_qual_info_size);
13251 if (first == NULL)
13252 first = mod_type_die;
13253 gcc_assert ((first_quals & ~sub_quals) == 0);
13254 for (count = 0, last = first;
13255 count < (1U << dwarf_qual_info_size);
13256 count++, last = last->die_sib)
13258 int quals = 0;
13259 if (last == mod_scope->die_child)
13260 break;
13261 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13262 != first)
13263 break;
13267 for (i = 0; i < dwarf_qual_info_size; i++)
13268 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13270 dw_die_ref d;
13271 if (first && first != last)
13273 for (d = first->die_sib; ; d = d->die_sib)
13275 int quals = 0;
13276 qualified_die_p (d, &quals, dwarf_qual_info_size);
13277 if (quals == (first_quals | dwarf_qual_info[i].q))
13278 break;
13279 if (d == last)
13281 d = NULL;
13282 break;
13285 if (d)
13287 mod_type_die = d;
13288 continue;
13291 if (first)
13293 d = new_die_raw (dwarf_qual_info[i].t);
13294 add_child_die_after (mod_scope, d, last);
13295 last = d;
13297 else
13298 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13299 if (mod_type_die)
13300 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13301 mod_type_die = d;
13302 first_quals |= dwarf_qual_info[i].q;
13305 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13307 dwarf_tag tag = DW_TAG_pointer_type;
13308 if (code == REFERENCE_TYPE)
13310 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13311 tag = DW_TAG_rvalue_reference_type;
13312 else
13313 tag = DW_TAG_reference_type;
13315 mod_type_die = new_die (tag, mod_scope, type);
13317 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13318 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13319 add_alignment_attribute (mod_type_die, type);
13320 item_type = TREE_TYPE (type);
13322 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13323 if (!ADDR_SPACE_GENERIC_P (as))
13325 int action = targetm.addr_space.debug (as);
13326 if (action >= 0)
13328 /* Positive values indicate an address_class. */
13329 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13331 else
13333 /* Negative values indicate an (inverted) segment base reg. */
13334 dw_loc_descr_ref d
13335 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13336 add_AT_loc (mod_type_die, DW_AT_segment, d);
13340 else if (code == INTEGER_TYPE
13341 && TREE_TYPE (type) != NULL_TREE
13342 && subrange_type_for_debug_p (type, &low, &high))
13344 tree bias = NULL_TREE;
13345 if (lang_hooks.types.get_type_bias)
13346 bias = lang_hooks.types.get_type_bias (type);
13347 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13348 item_type = TREE_TYPE (type);
13350 else if (is_base_type (type))
13352 mod_type_die = base_type_die (type, reverse);
13354 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13355 if (reverse_base_type)
13357 dw_die_ref after_die
13358 = modified_type_die (type, cv_quals, false, context_die);
13359 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13361 else
13362 add_child_die (comp_unit_die (), mod_type_die);
13364 add_pubtype (type, mod_type_die);
13366 else
13368 gen_type_die (type, context_die);
13370 /* We have to get the type_main_variant here (and pass that to the
13371 `lookup_type_die' routine) because the ..._TYPE node we have
13372 might simply be a *copy* of some original type node (where the
13373 copy was created to help us keep track of typedef names) and
13374 that copy might have a different TYPE_UID from the original
13375 ..._TYPE node. */
13376 if (TREE_CODE (type) == FUNCTION_TYPE
13377 || TREE_CODE (type) == METHOD_TYPE)
13379 /* For function/method types, can't just use type_main_variant here,
13380 because that can have different ref-qualifiers for C++,
13381 but try to canonicalize. */
13382 tree main = TYPE_MAIN_VARIANT (type);
13383 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13384 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13385 && check_base_type (t, main)
13386 && check_lang_type (t, type))
13387 return lookup_type_die (t);
13388 return lookup_type_die (type);
13390 else if (TREE_CODE (type) != VECTOR_TYPE
13391 && TREE_CODE (type) != ARRAY_TYPE)
13392 return lookup_type_die (type_main_variant (type));
13393 else
13394 /* Vectors have the debugging information in the type,
13395 not the main variant. */
13396 return lookup_type_die (type);
13399 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13400 don't output a DW_TAG_typedef, since there isn't one in the
13401 user's program; just attach a DW_AT_name to the type.
13402 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13403 if the base type already has the same name. */
13404 if (name
13405 && ((TREE_CODE (name) != TYPE_DECL
13406 && (qualified_type == TYPE_MAIN_VARIANT (type)
13407 || (cv_quals == TYPE_UNQUALIFIED)))
13408 || (TREE_CODE (name) == TYPE_DECL
13409 && TREE_TYPE (name) == qualified_type
13410 && DECL_NAME (name))))
13412 if (TREE_CODE (name) == TYPE_DECL)
13413 /* Could just call add_name_and_src_coords_attributes here,
13414 but since this is a builtin type it doesn't have any
13415 useful source coordinates anyway. */
13416 name = DECL_NAME (name);
13417 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13419 /* This probably indicates a bug. */
13420 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13422 name = TYPE_IDENTIFIER (type);
13423 add_name_attribute (mod_type_die,
13424 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13427 if (qualified_type && !reverse_base_type)
13428 equate_type_number_to_die (qualified_type, mod_type_die);
13430 if (item_type)
13431 /* We must do this after the equate_type_number_to_die call, in case
13432 this is a recursive type. This ensures that the modified_type_die
13433 recursion will terminate even if the type is recursive. Recursive
13434 types are possible in Ada. */
13435 sub_die = modified_type_die (item_type,
13436 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13437 reverse,
13438 context_die);
13440 if (sub_die != NULL)
13441 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13443 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13444 if (TYPE_ARTIFICIAL (type))
13445 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13447 return mod_type_die;
13450 /* Generate DIEs for the generic parameters of T.
13451 T must be either a generic type or a generic function.
13452 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13454 static void
13455 gen_generic_params_dies (tree t)
13457 tree parms, args;
13458 int parms_num, i;
13459 dw_die_ref die = NULL;
13460 int non_default;
13462 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13463 return;
13465 if (TYPE_P (t))
13466 die = lookup_type_die (t);
13467 else if (DECL_P (t))
13468 die = lookup_decl_die (t);
13470 gcc_assert (die);
13472 parms = lang_hooks.get_innermost_generic_parms (t);
13473 if (!parms)
13474 /* T has no generic parameter. It means T is neither a generic type
13475 or function. End of story. */
13476 return;
13478 parms_num = TREE_VEC_LENGTH (parms);
13479 args = lang_hooks.get_innermost_generic_args (t);
13480 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13481 non_default = int_cst_value (TREE_CHAIN (args));
13482 else
13483 non_default = TREE_VEC_LENGTH (args);
13484 for (i = 0; i < parms_num; i++)
13486 tree parm, arg, arg_pack_elems;
13487 dw_die_ref parm_die;
13489 parm = TREE_VEC_ELT (parms, i);
13490 arg = TREE_VEC_ELT (args, i);
13491 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13492 gcc_assert (parm && TREE_VALUE (parm) && arg);
13494 if (parm && TREE_VALUE (parm) && arg)
13496 /* If PARM represents a template parameter pack,
13497 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13498 by DW_TAG_template_*_parameter DIEs for the argument
13499 pack elements of ARG. Note that ARG would then be
13500 an argument pack. */
13501 if (arg_pack_elems)
13502 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13503 arg_pack_elems,
13504 die);
13505 else
13506 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13507 true /* emit name */, die);
13508 if (i >= non_default)
13509 add_AT_flag (parm_die, DW_AT_default_value, 1);
13514 /* Create and return a DIE for PARM which should be
13515 the representation of a generic type parameter.
13516 For instance, in the C++ front end, PARM would be a template parameter.
13517 ARG is the argument to PARM.
13518 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13519 name of the PARM.
13520 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13521 as a child node. */
13523 static dw_die_ref
13524 generic_parameter_die (tree parm, tree arg,
13525 bool emit_name_p,
13526 dw_die_ref parent_die)
13528 dw_die_ref tmpl_die = NULL;
13529 const char *name = NULL;
13531 if (!parm || !DECL_NAME (parm) || !arg)
13532 return NULL;
13534 /* We support non-type generic parameters and arguments,
13535 type generic parameters and arguments, as well as
13536 generic generic parameters (a.k.a. template template parameters in C++)
13537 and arguments. */
13538 if (TREE_CODE (parm) == PARM_DECL)
13539 /* PARM is a nontype generic parameter */
13540 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13541 else if (TREE_CODE (parm) == TYPE_DECL)
13542 /* PARM is a type generic parameter. */
13543 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13544 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13545 /* PARM is a generic generic parameter.
13546 Its DIE is a GNU extension. It shall have a
13547 DW_AT_name attribute to represent the name of the template template
13548 parameter, and a DW_AT_GNU_template_name attribute to represent the
13549 name of the template template argument. */
13550 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13551 parent_die, parm);
13552 else
13553 gcc_unreachable ();
13555 if (tmpl_die)
13557 tree tmpl_type;
13559 /* If PARM is a generic parameter pack, it means we are
13560 emitting debug info for a template argument pack element.
13561 In other terms, ARG is a template argument pack element.
13562 In that case, we don't emit any DW_AT_name attribute for
13563 the die. */
13564 if (emit_name_p)
13566 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13567 gcc_assert (name);
13568 add_AT_string (tmpl_die, DW_AT_name, name);
13571 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13573 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13574 TMPL_DIE should have a child DW_AT_type attribute that is set
13575 to the type of the argument to PARM, which is ARG.
13576 If PARM is a type generic parameter, TMPL_DIE should have a
13577 child DW_AT_type that is set to ARG. */
13578 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13579 add_type_attribute (tmpl_die, tmpl_type,
13580 (TREE_THIS_VOLATILE (tmpl_type)
13581 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13582 false, parent_die);
13584 else
13586 /* So TMPL_DIE is a DIE representing a
13587 a generic generic template parameter, a.k.a template template
13588 parameter in C++ and arg is a template. */
13590 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13591 to the name of the argument. */
13592 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13593 if (name)
13594 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13597 if (TREE_CODE (parm) == PARM_DECL)
13598 /* So PARM is a non-type generic parameter.
13599 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13600 attribute of TMPL_DIE which value represents the value
13601 of ARG.
13602 We must be careful here:
13603 The value of ARG might reference some function decls.
13604 We might currently be emitting debug info for a generic
13605 type and types are emitted before function decls, we don't
13606 know if the function decls referenced by ARG will actually be
13607 emitted after cgraph computations.
13608 So must defer the generation of the DW_AT_const_value to
13609 after cgraph is ready. */
13610 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13613 return tmpl_die;
13616 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13617 PARM_PACK must be a template parameter pack. The returned DIE
13618 will be child DIE of PARENT_DIE. */
13620 static dw_die_ref
13621 template_parameter_pack_die (tree parm_pack,
13622 tree parm_pack_args,
13623 dw_die_ref parent_die)
13625 dw_die_ref die;
13626 int j;
13628 gcc_assert (parent_die && parm_pack);
13630 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13631 add_name_and_src_coords_attributes (die, parm_pack);
13632 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13633 generic_parameter_die (parm_pack,
13634 TREE_VEC_ELT (parm_pack_args, j),
13635 false /* Don't emit DW_AT_name */,
13636 die);
13637 return die;
13640 /* Return the DBX register number described by a given RTL node. */
13642 static unsigned int
13643 dbx_reg_number (const_rtx rtl)
13645 unsigned regno = REGNO (rtl);
13647 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13649 #ifdef LEAF_REG_REMAP
13650 if (crtl->uses_only_leaf_regs)
13652 int leaf_reg = LEAF_REG_REMAP (regno);
13653 if (leaf_reg != -1)
13654 regno = (unsigned) leaf_reg;
13656 #endif
13658 regno = DBX_REGISTER_NUMBER (regno);
13659 gcc_assert (regno != INVALID_REGNUM);
13660 return regno;
13663 /* Optionally add a DW_OP_piece term to a location description expression.
13664 DW_OP_piece is only added if the location description expression already
13665 doesn't end with DW_OP_piece. */
13667 static void
13668 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13670 dw_loc_descr_ref loc;
13672 if (*list_head != NULL)
13674 /* Find the end of the chain. */
13675 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13678 if (loc->dw_loc_opc != DW_OP_piece)
13679 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13683 /* Return a location descriptor that designates a machine register or
13684 zero if there is none. */
13686 static dw_loc_descr_ref
13687 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13689 rtx regs;
13691 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13692 return 0;
13694 /* We only use "frame base" when we're sure we're talking about the
13695 post-prologue local stack frame. We do this by *not* running
13696 register elimination until this point, and recognizing the special
13697 argument pointer and soft frame pointer rtx's.
13698 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13699 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13700 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13702 dw_loc_descr_ref result = NULL;
13704 if (dwarf_version >= 4 || !dwarf_strict)
13706 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13707 initialized);
13708 if (result)
13709 add_loc_descr (&result,
13710 new_loc_descr (DW_OP_stack_value, 0, 0));
13712 return result;
13715 regs = targetm.dwarf_register_span (rtl);
13717 if (REG_NREGS (rtl) > 1 || regs)
13718 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13719 else
13721 unsigned int dbx_regnum = dbx_reg_number (rtl);
13722 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13723 return 0;
13724 return one_reg_loc_descriptor (dbx_regnum, initialized);
13728 /* Return a location descriptor that designates a machine register for
13729 a given hard register number. */
13731 static dw_loc_descr_ref
13732 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13734 dw_loc_descr_ref reg_loc_descr;
13736 if (regno <= 31)
13737 reg_loc_descr
13738 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13739 else
13740 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13742 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13743 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13745 return reg_loc_descr;
13748 /* Given an RTL of a register, return a location descriptor that
13749 designates a value that spans more than one register. */
13751 static dw_loc_descr_ref
13752 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13753 enum var_init_status initialized)
13755 int size, i;
13756 dw_loc_descr_ref loc_result = NULL;
13758 /* Simple, contiguous registers. */
13759 if (regs == NULL_RTX)
13761 unsigned reg = REGNO (rtl);
13762 int nregs;
13764 #ifdef LEAF_REG_REMAP
13765 if (crtl->uses_only_leaf_regs)
13767 int leaf_reg = LEAF_REG_REMAP (reg);
13768 if (leaf_reg != -1)
13769 reg = (unsigned) leaf_reg;
13771 #endif
13773 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13774 nregs = REG_NREGS (rtl);
13776 /* At present we only track constant-sized pieces. */
13777 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13778 return NULL;
13779 size /= nregs;
13781 loc_result = NULL;
13782 while (nregs--)
13784 dw_loc_descr_ref t;
13786 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13787 VAR_INIT_STATUS_INITIALIZED);
13788 add_loc_descr (&loc_result, t);
13789 add_loc_descr_op_piece (&loc_result, size);
13790 ++reg;
13792 return loc_result;
13795 /* Now onto stupid register sets in non contiguous locations. */
13797 gcc_assert (GET_CODE (regs) == PARALLEL);
13799 /* At present we only track constant-sized pieces. */
13800 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13801 return NULL;
13802 loc_result = NULL;
13804 for (i = 0; i < XVECLEN (regs, 0); ++i)
13806 dw_loc_descr_ref t;
13808 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13809 VAR_INIT_STATUS_INITIALIZED);
13810 add_loc_descr (&loc_result, t);
13811 add_loc_descr_op_piece (&loc_result, size);
13814 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13815 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13816 return loc_result;
13819 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13821 /* Return a location descriptor that designates a constant i,
13822 as a compound operation from constant (i >> shift), constant shift
13823 and DW_OP_shl. */
13825 static dw_loc_descr_ref
13826 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13828 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13829 add_loc_descr (&ret, int_loc_descriptor (shift));
13830 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13831 return ret;
13834 /* Return a location descriptor that designates constant POLY_I. */
13836 static dw_loc_descr_ref
13837 int_loc_descriptor (poly_int64 poly_i)
13839 enum dwarf_location_atom op;
13841 HOST_WIDE_INT i;
13842 if (!poly_i.is_constant (&i))
13844 /* Create location descriptions for the non-constant part and
13845 add any constant offset at the end. */
13846 dw_loc_descr_ref ret = NULL;
13847 HOST_WIDE_INT constant = poly_i.coeffs[0];
13848 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13850 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13851 if (coeff != 0)
13853 dw_loc_descr_ref start = ret;
13854 unsigned int factor;
13855 int bias;
13856 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13857 (j, &factor, &bias);
13859 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13860 add COEFF * (REGNO / FACTOR) now and subtract
13861 COEFF * BIAS from the final constant part. */
13862 constant -= coeff * bias;
13863 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13864 if (coeff % factor == 0)
13865 coeff /= factor;
13866 else
13868 int amount = exact_log2 (factor);
13869 gcc_assert (amount >= 0);
13870 add_loc_descr (&ret, int_loc_descriptor (amount));
13871 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13873 if (coeff != 1)
13875 add_loc_descr (&ret, int_loc_descriptor (coeff));
13876 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13878 if (start)
13879 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13882 loc_descr_plus_const (&ret, constant);
13883 return ret;
13886 /* Pick the smallest representation of a constant, rather than just
13887 defaulting to the LEB encoding. */
13888 if (i >= 0)
13890 int clz = clz_hwi (i);
13891 int ctz = ctz_hwi (i);
13892 if (i <= 31)
13893 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13894 else if (i <= 0xff)
13895 op = DW_OP_const1u;
13896 else if (i <= 0xffff)
13897 op = DW_OP_const2u;
13898 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13899 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13900 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13901 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13902 while DW_OP_const4u is 5 bytes. */
13903 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13904 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13905 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13906 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13907 while DW_OP_const4u is 5 bytes. */
13908 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13910 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13911 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13912 <= 4)
13914 /* As i >= 2**31, the double cast above will yield a negative number.
13915 Since wrapping is defined in DWARF expressions we can output big
13916 positive integers as small negative ones, regardless of the size
13917 of host wide ints.
13919 Here, since the evaluator will handle 32-bit values and since i >=
13920 2**31, we know it's going to be interpreted as a negative literal:
13921 store it this way if we can do better than 5 bytes this way. */
13922 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13924 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13925 op = DW_OP_const4u;
13927 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13928 least 6 bytes: see if we can do better before falling back to it. */
13929 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13930 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13931 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13932 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13933 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13934 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13935 >= HOST_BITS_PER_WIDE_INT)
13936 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13937 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13938 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13939 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13940 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13941 && size_of_uleb128 (i) > 6)
13942 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13943 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13944 else
13945 op = DW_OP_constu;
13947 else
13949 if (i >= -0x80)
13950 op = DW_OP_const1s;
13951 else if (i >= -0x8000)
13952 op = DW_OP_const2s;
13953 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13955 if (size_of_int_loc_descriptor (i) < 5)
13957 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13958 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13959 return ret;
13961 op = DW_OP_const4s;
13963 else
13965 if (size_of_int_loc_descriptor (i)
13966 < (unsigned long) 1 + size_of_sleb128 (i))
13968 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13969 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13970 return ret;
13972 op = DW_OP_consts;
13976 return new_loc_descr (op, i, 0);
13979 /* Likewise, for unsigned constants. */
13981 static dw_loc_descr_ref
13982 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13984 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13985 const unsigned HOST_WIDE_INT max_uint
13986 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13988 /* If possible, use the clever signed constants handling. */
13989 if (i <= max_int)
13990 return int_loc_descriptor ((HOST_WIDE_INT) i);
13992 /* Here, we are left with positive numbers that cannot be represented as
13993 HOST_WIDE_INT, i.e.:
13994 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13996 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13997 whereas may be better to output a negative integer: thanks to integer
13998 wrapping, we know that:
13999 x = x - 2 ** DWARF2_ADDR_SIZE
14000 = x - 2 * (max (HOST_WIDE_INT) + 1)
14001 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14002 small negative integers. Let's try that in cases it will clearly improve
14003 the encoding: there is no gain turning DW_OP_const4u into
14004 DW_OP_const4s. */
14005 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14006 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14007 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14009 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14011 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14012 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14013 const HOST_WIDE_INT second_shift
14014 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14016 /* So we finally have:
14017 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14018 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14019 return int_loc_descriptor (second_shift);
14022 /* Last chance: fallback to a simple constant operation. */
14023 return new_loc_descr
14024 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14025 ? DW_OP_const4u
14026 : DW_OP_const8u,
14027 i, 0);
14030 /* Generate and return a location description that computes the unsigned
14031 comparison of the two stack top entries (a OP b where b is the top-most
14032 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14033 LE_EXPR, GT_EXPR or GE_EXPR. */
14035 static dw_loc_descr_ref
14036 uint_comparison_loc_list (enum tree_code kind)
14038 enum dwarf_location_atom op, flip_op;
14039 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14041 switch (kind)
14043 case LT_EXPR:
14044 op = DW_OP_lt;
14045 break;
14046 case LE_EXPR:
14047 op = DW_OP_le;
14048 break;
14049 case GT_EXPR:
14050 op = DW_OP_gt;
14051 break;
14052 case GE_EXPR:
14053 op = DW_OP_ge;
14054 break;
14055 default:
14056 gcc_unreachable ();
14059 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14060 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14062 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14063 possible to perform unsigned comparisons: we just have to distinguish
14064 three cases:
14066 1. when a and b have the same sign (as signed integers); then we should
14067 return: a OP(signed) b;
14069 2. when a is a negative signed integer while b is a positive one, then a
14070 is a greater unsigned integer than b; likewise when a and b's roles
14071 are flipped.
14073 So first, compare the sign of the two operands. */
14074 ret = new_loc_descr (DW_OP_over, 0, 0);
14075 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14076 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14077 /* If they have different signs (i.e. they have different sign bits), then
14078 the stack top value has now the sign bit set and thus it's smaller than
14079 zero. */
14080 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14081 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14082 add_loc_descr (&ret, bra_node);
14084 /* We are in case 1. At this point, we know both operands have the same
14085 sign, to it's safe to use the built-in signed comparison. */
14086 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14087 add_loc_descr (&ret, jmp_node);
14089 /* We are in case 2. Here, we know both operands do not have the same sign,
14090 so we have to flip the signed comparison. */
14091 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14092 tmp = new_loc_descr (flip_op, 0, 0);
14093 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14094 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14095 add_loc_descr (&ret, tmp);
14097 /* This dummy operation is necessary to make the two branches join. */
14098 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14099 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14100 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14101 add_loc_descr (&ret, tmp);
14103 return ret;
14106 /* Likewise, but takes the location description lists (might be destructive on
14107 them). Return NULL if either is NULL or if concatenation fails. */
14109 static dw_loc_list_ref
14110 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14111 enum tree_code kind)
14113 if (left == NULL || right == NULL)
14114 return NULL;
14116 add_loc_list (&left, right);
14117 if (left == NULL)
14118 return NULL;
14120 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14121 return left;
14124 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14125 without actually allocating it. */
14127 static unsigned long
14128 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14130 return size_of_int_loc_descriptor (i >> shift)
14131 + size_of_int_loc_descriptor (shift)
14132 + 1;
14135 /* Return size_of_locs (int_loc_descriptor (i)) without
14136 actually allocating it. */
14138 static unsigned long
14139 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14141 unsigned long s;
14143 if (i >= 0)
14145 int clz, ctz;
14146 if (i <= 31)
14147 return 1;
14148 else if (i <= 0xff)
14149 return 2;
14150 else if (i <= 0xffff)
14151 return 3;
14152 clz = clz_hwi (i);
14153 ctz = ctz_hwi (i);
14154 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14155 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14156 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14157 - clz - 5);
14158 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14159 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14160 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14161 - clz - 8);
14162 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14163 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14164 <= 4)
14165 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14166 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14167 return 5;
14168 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14169 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14170 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14171 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14172 - clz - 8);
14173 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14174 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14175 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14176 - clz - 16);
14177 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14178 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14179 && s > 6)
14180 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14181 - clz - 32);
14182 else
14183 return 1 + s;
14185 else
14187 if (i >= -0x80)
14188 return 2;
14189 else if (i >= -0x8000)
14190 return 3;
14191 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14193 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14195 s = size_of_int_loc_descriptor (-i) + 1;
14196 if (s < 5)
14197 return s;
14199 return 5;
14201 else
14203 unsigned long r = 1 + size_of_sleb128 (i);
14204 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14206 s = size_of_int_loc_descriptor (-i) + 1;
14207 if (s < r)
14208 return s;
14210 return r;
14215 /* Return loc description representing "address" of integer value.
14216 This can appear only as toplevel expression. */
14218 static dw_loc_descr_ref
14219 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14221 int litsize;
14222 dw_loc_descr_ref loc_result = NULL;
14224 if (!(dwarf_version >= 4 || !dwarf_strict))
14225 return NULL;
14227 litsize = size_of_int_loc_descriptor (i);
14228 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14229 is more compact. For DW_OP_stack_value we need:
14230 litsize + 1 (DW_OP_stack_value)
14231 and for DW_OP_implicit_value:
14232 1 (DW_OP_implicit_value) + 1 (length) + size. */
14233 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14235 loc_result = int_loc_descriptor (i);
14236 add_loc_descr (&loc_result,
14237 new_loc_descr (DW_OP_stack_value, 0, 0));
14238 return loc_result;
14241 loc_result = new_loc_descr (DW_OP_implicit_value,
14242 size, 0);
14243 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14244 loc_result->dw_loc_oprnd2.v.val_int = i;
14245 return loc_result;
14248 /* Return a location descriptor that designates a base+offset location. */
14250 static dw_loc_descr_ref
14251 based_loc_descr (rtx reg, poly_int64 offset,
14252 enum var_init_status initialized)
14254 unsigned int regno;
14255 dw_loc_descr_ref result;
14256 dw_fde_ref fde = cfun->fde;
14258 /* We only use "frame base" when we're sure we're talking about the
14259 post-prologue local stack frame. We do this by *not* running
14260 register elimination until this point, and recognizing the special
14261 argument pointer and soft frame pointer rtx's. */
14262 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14264 rtx elim = (ira_use_lra_p
14265 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14266 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14268 if (elim != reg)
14270 /* Allow hard frame pointer here even if frame pointer
14271 isn't used since hard frame pointer is encoded with
14272 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14273 not hard frame pointer directly. */
14274 elim = strip_offset_and_add (elim, &offset);
14275 gcc_assert (elim == hard_frame_pointer_rtx
14276 || elim == stack_pointer_rtx);
14278 /* If drap register is used to align stack, use frame
14279 pointer + offset to access stack variables. If stack
14280 is aligned without drap, use stack pointer + offset to
14281 access stack variables. */
14282 if (crtl->stack_realign_tried
14283 && reg == frame_pointer_rtx)
14285 int base_reg
14286 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14287 ? HARD_FRAME_POINTER_REGNUM
14288 : REGNO (elim));
14289 return new_reg_loc_descr (base_reg, offset);
14292 gcc_assert (frame_pointer_fb_offset_valid);
14293 offset += frame_pointer_fb_offset;
14294 HOST_WIDE_INT const_offset;
14295 if (offset.is_constant (&const_offset))
14296 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14297 else
14299 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14300 loc_descr_plus_const (&ret, offset);
14301 return ret;
14306 regno = REGNO (reg);
14307 #ifdef LEAF_REG_REMAP
14308 if (crtl->uses_only_leaf_regs)
14310 int leaf_reg = LEAF_REG_REMAP (regno);
14311 if (leaf_reg != -1)
14312 regno = (unsigned) leaf_reg;
14314 #endif
14315 regno = DWARF_FRAME_REGNUM (regno);
14317 HOST_WIDE_INT const_offset;
14318 if (!optimize && fde
14319 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14320 && offset.is_constant (&const_offset))
14322 /* Use cfa+offset to represent the location of arguments passed
14323 on the stack when drap is used to align stack.
14324 Only do this when not optimizing, for optimized code var-tracking
14325 is supposed to track where the arguments live and the register
14326 used as vdrap or drap in some spot might be used for something
14327 else in other part of the routine. */
14328 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14331 result = new_reg_loc_descr (regno, offset);
14333 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14334 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14336 return result;
14339 /* Return true if this RTL expression describes a base+offset calculation. */
14341 static inline int
14342 is_based_loc (const_rtx rtl)
14344 return (GET_CODE (rtl) == PLUS
14345 && ((REG_P (XEXP (rtl, 0))
14346 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14347 && CONST_INT_P (XEXP (rtl, 1)))));
14350 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14351 failed. */
14353 static dw_loc_descr_ref
14354 tls_mem_loc_descriptor (rtx mem)
14356 tree base;
14357 dw_loc_descr_ref loc_result;
14359 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14360 return NULL;
14362 base = get_base_address (MEM_EXPR (mem));
14363 if (base == NULL
14364 || !VAR_P (base)
14365 || !DECL_THREAD_LOCAL_P (base))
14366 return NULL;
14368 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14369 if (loc_result == NULL)
14370 return NULL;
14372 if (maybe_ne (MEM_OFFSET (mem), 0))
14373 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14375 return loc_result;
14378 /* Output debug info about reason why we failed to expand expression as dwarf
14379 expression. */
14381 static void
14382 expansion_failed (tree expr, rtx rtl, char const *reason)
14384 if (dump_file && (dump_flags & TDF_DETAILS))
14386 fprintf (dump_file, "Failed to expand as dwarf: ");
14387 if (expr)
14388 print_generic_expr (dump_file, expr, dump_flags);
14389 if (rtl)
14391 fprintf (dump_file, "\n");
14392 print_rtl (dump_file, rtl);
14394 fprintf (dump_file, "\nReason: %s\n", reason);
14398 /* Helper function for const_ok_for_output. */
14400 static bool
14401 const_ok_for_output_1 (rtx rtl)
14403 if (targetm.const_not_ok_for_debug_p (rtl))
14405 if (GET_CODE (rtl) != UNSPEC)
14407 expansion_failed (NULL_TREE, rtl,
14408 "Expression rejected for debug by the backend.\n");
14409 return false;
14412 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14413 the target hook doesn't explicitly allow it in debug info, assume
14414 we can't express it in the debug info. */
14415 /* Don't complain about TLS UNSPECs, those are just too hard to
14416 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14417 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14418 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14419 if (flag_checking
14420 && (XVECLEN (rtl, 0) == 0
14421 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14422 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14423 inform (current_function_decl
14424 ? DECL_SOURCE_LOCATION (current_function_decl)
14425 : UNKNOWN_LOCATION,
14426 #if NUM_UNSPEC_VALUES > 0
14427 "non-delegitimized UNSPEC %s (%d) found in variable location",
14428 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14429 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14430 XINT (rtl, 1));
14431 #else
14432 "non-delegitimized UNSPEC %d found in variable location",
14433 XINT (rtl, 1));
14434 #endif
14435 expansion_failed (NULL_TREE, rtl,
14436 "UNSPEC hasn't been delegitimized.\n");
14437 return false;
14440 if (CONST_POLY_INT_P (rtl))
14441 return false;
14443 if (targetm.const_not_ok_for_debug_p (rtl))
14445 expansion_failed (NULL_TREE, rtl,
14446 "Expression rejected for debug by the backend.\n");
14447 return false;
14450 /* FIXME: Refer to PR60655. It is possible for simplification
14451 of rtl expressions in var tracking to produce such expressions.
14452 We should really identify / validate expressions
14453 enclosed in CONST that can be handled by assemblers on various
14454 targets and only handle legitimate cases here. */
14455 switch (GET_CODE (rtl))
14457 case SYMBOL_REF:
14458 break;
14459 case NOT:
14460 case NEG:
14461 return false;
14462 default:
14463 return true;
14466 if (CONSTANT_POOL_ADDRESS_P (rtl))
14468 bool marked;
14469 get_pool_constant_mark (rtl, &marked);
14470 /* If all references to this pool constant were optimized away,
14471 it was not output and thus we can't represent it. */
14472 if (!marked)
14474 expansion_failed (NULL_TREE, rtl,
14475 "Constant was removed from constant pool.\n");
14476 return false;
14480 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14481 return false;
14483 /* Avoid references to external symbols in debug info, on several targets
14484 the linker might even refuse to link when linking a shared library,
14485 and in many other cases the relocations for .debug_info/.debug_loc are
14486 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14487 to be defined within the same shared library or executable are fine. */
14488 if (SYMBOL_REF_EXTERNAL_P (rtl))
14490 tree decl = SYMBOL_REF_DECL (rtl);
14492 if (decl == NULL || !targetm.binds_local_p (decl))
14494 expansion_failed (NULL_TREE, rtl,
14495 "Symbol not defined in current TU.\n");
14496 return false;
14500 return true;
14503 /* Return true if constant RTL can be emitted in DW_OP_addr or
14504 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14505 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14507 static bool
14508 const_ok_for_output (rtx rtl)
14510 if (GET_CODE (rtl) == SYMBOL_REF)
14511 return const_ok_for_output_1 (rtl);
14513 if (GET_CODE (rtl) == CONST)
14515 subrtx_var_iterator::array_type array;
14516 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14517 if (!const_ok_for_output_1 (*iter))
14518 return false;
14519 return true;
14522 return true;
14525 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14526 if possible, NULL otherwise. */
14528 static dw_die_ref
14529 base_type_for_mode (machine_mode mode, bool unsignedp)
14531 dw_die_ref type_die;
14532 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14534 if (type == NULL)
14535 return NULL;
14536 switch (TREE_CODE (type))
14538 case INTEGER_TYPE:
14539 case REAL_TYPE:
14540 break;
14541 default:
14542 return NULL;
14544 type_die = lookup_type_die (type);
14545 if (!type_die)
14546 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14547 comp_unit_die ());
14548 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14549 return NULL;
14550 return type_die;
14553 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14554 type matching MODE, or, if MODE is narrower than or as wide as
14555 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14556 possible. */
14558 static dw_loc_descr_ref
14559 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14561 machine_mode outer_mode = mode;
14562 dw_die_ref type_die;
14563 dw_loc_descr_ref cvt;
14565 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14567 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14568 return op;
14570 type_die = base_type_for_mode (outer_mode, 1);
14571 if (type_die == NULL)
14572 return NULL;
14573 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14574 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14575 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14576 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14577 add_loc_descr (&op, cvt);
14578 return op;
14581 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14583 static dw_loc_descr_ref
14584 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14585 dw_loc_descr_ref op1)
14587 dw_loc_descr_ref ret = op0;
14588 add_loc_descr (&ret, op1);
14589 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14590 if (STORE_FLAG_VALUE != 1)
14592 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14593 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14595 return ret;
14598 /* Subroutine of scompare_loc_descriptor for the case in which we're
14599 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14600 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14602 static dw_loc_descr_ref
14603 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14604 scalar_int_mode op_mode,
14605 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14607 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14608 dw_loc_descr_ref cvt;
14610 if (type_die == NULL)
14611 return NULL;
14612 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14613 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14614 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14615 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14616 add_loc_descr (&op0, cvt);
14617 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14618 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14619 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14620 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14621 add_loc_descr (&op1, cvt);
14622 return compare_loc_descriptor (op, op0, op1);
14625 /* Subroutine of scompare_loc_descriptor for the case in which we're
14626 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14627 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14629 static dw_loc_descr_ref
14630 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14631 scalar_int_mode op_mode,
14632 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14634 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14635 /* For eq/ne, if the operands are known to be zero-extended,
14636 there is no need to do the fancy shifting up. */
14637 if (op == DW_OP_eq || op == DW_OP_ne)
14639 dw_loc_descr_ref last0, last1;
14640 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14642 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14644 /* deref_size zero extends, and for constants we can check
14645 whether they are zero extended or not. */
14646 if (((last0->dw_loc_opc == DW_OP_deref_size
14647 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14648 || (CONST_INT_P (XEXP (rtl, 0))
14649 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14650 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14651 && ((last1->dw_loc_opc == DW_OP_deref_size
14652 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14653 || (CONST_INT_P (XEXP (rtl, 1))
14654 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14655 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14656 return compare_loc_descriptor (op, op0, op1);
14658 /* EQ/NE comparison against constant in narrower type than
14659 DWARF2_ADDR_SIZE can be performed either as
14660 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14661 DW_OP_{eq,ne}
14663 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14664 DW_OP_{eq,ne}. Pick whatever is shorter. */
14665 if (CONST_INT_P (XEXP (rtl, 1))
14666 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14667 && (size_of_int_loc_descriptor (shift) + 1
14668 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14669 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14670 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14671 & GET_MODE_MASK (op_mode))))
14673 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14674 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14675 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14676 & GET_MODE_MASK (op_mode));
14677 return compare_loc_descriptor (op, op0, op1);
14680 add_loc_descr (&op0, int_loc_descriptor (shift));
14681 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14682 if (CONST_INT_P (XEXP (rtl, 1)))
14683 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14684 else
14686 add_loc_descr (&op1, int_loc_descriptor (shift));
14687 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14689 return compare_loc_descriptor (op, op0, op1);
14692 /* Return location descriptor for unsigned comparison OP RTL. */
14694 static dw_loc_descr_ref
14695 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14696 machine_mode mem_mode)
14698 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14699 dw_loc_descr_ref op0, op1;
14701 if (op_mode == VOIDmode)
14702 op_mode = GET_MODE (XEXP (rtl, 1));
14703 if (op_mode == VOIDmode)
14704 return NULL;
14706 scalar_int_mode int_op_mode;
14707 if (dwarf_strict
14708 && dwarf_version < 5
14709 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14710 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14711 return NULL;
14713 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14714 VAR_INIT_STATUS_INITIALIZED);
14715 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14716 VAR_INIT_STATUS_INITIALIZED);
14718 if (op0 == NULL || op1 == NULL)
14719 return NULL;
14721 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14723 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14724 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14726 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14727 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14729 return compare_loc_descriptor (op, op0, op1);
14732 /* Return location descriptor for unsigned comparison OP RTL. */
14734 static dw_loc_descr_ref
14735 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14736 machine_mode mem_mode)
14738 dw_loc_descr_ref op0, op1;
14740 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14741 if (test_op_mode == VOIDmode)
14742 test_op_mode = GET_MODE (XEXP (rtl, 1));
14744 scalar_int_mode op_mode;
14745 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14746 return NULL;
14748 if (dwarf_strict
14749 && dwarf_version < 5
14750 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14751 return NULL;
14753 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14754 VAR_INIT_STATUS_INITIALIZED);
14755 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14756 VAR_INIT_STATUS_INITIALIZED);
14758 if (op0 == NULL || op1 == NULL)
14759 return NULL;
14761 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14763 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14764 dw_loc_descr_ref last0, last1;
14765 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14767 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14769 if (CONST_INT_P (XEXP (rtl, 0)))
14770 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14771 /* deref_size zero extends, so no need to mask it again. */
14772 else if (last0->dw_loc_opc != DW_OP_deref_size
14773 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14775 add_loc_descr (&op0, int_loc_descriptor (mask));
14776 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14778 if (CONST_INT_P (XEXP (rtl, 1)))
14779 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14780 /* deref_size zero extends, so no need to mask it again. */
14781 else if (last1->dw_loc_opc != DW_OP_deref_size
14782 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14784 add_loc_descr (&op1, int_loc_descriptor (mask));
14785 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14788 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14790 HOST_WIDE_INT bias = 1;
14791 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14792 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14793 if (CONST_INT_P (XEXP (rtl, 1)))
14794 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14795 + INTVAL (XEXP (rtl, 1)));
14796 else
14797 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14798 bias, 0));
14800 return compare_loc_descriptor (op, op0, op1);
14803 /* Return location descriptor for {U,S}{MIN,MAX}. */
14805 static dw_loc_descr_ref
14806 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14807 machine_mode mem_mode)
14809 enum dwarf_location_atom op;
14810 dw_loc_descr_ref op0, op1, ret;
14811 dw_loc_descr_ref bra_node, drop_node;
14813 scalar_int_mode int_mode;
14814 if (dwarf_strict
14815 && dwarf_version < 5
14816 && (!is_a <scalar_int_mode> (mode, &int_mode)
14817 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14818 return NULL;
14820 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14821 VAR_INIT_STATUS_INITIALIZED);
14822 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14823 VAR_INIT_STATUS_INITIALIZED);
14825 if (op0 == NULL || op1 == NULL)
14826 return NULL;
14828 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14829 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14830 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14831 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14833 /* Checked by the caller. */
14834 int_mode = as_a <scalar_int_mode> (mode);
14835 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14837 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14838 add_loc_descr (&op0, int_loc_descriptor (mask));
14839 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14840 add_loc_descr (&op1, int_loc_descriptor (mask));
14841 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14843 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14845 HOST_WIDE_INT bias = 1;
14846 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14847 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14848 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14851 else if (is_a <scalar_int_mode> (mode, &int_mode)
14852 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14854 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14855 add_loc_descr (&op0, int_loc_descriptor (shift));
14856 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14857 add_loc_descr (&op1, int_loc_descriptor (shift));
14858 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14860 else if (is_a <scalar_int_mode> (mode, &int_mode)
14861 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14863 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14864 dw_loc_descr_ref cvt;
14865 if (type_die == NULL)
14866 return NULL;
14867 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14868 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14869 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14870 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14871 add_loc_descr (&op0, cvt);
14872 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14873 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14874 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14875 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14876 add_loc_descr (&op1, cvt);
14879 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14880 op = DW_OP_lt;
14881 else
14882 op = DW_OP_gt;
14883 ret = op0;
14884 add_loc_descr (&ret, op1);
14885 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14886 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14887 add_loc_descr (&ret, bra_node);
14888 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14889 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14890 add_loc_descr (&ret, drop_node);
14891 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14892 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14893 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14894 && is_a <scalar_int_mode> (mode, &int_mode)
14895 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14896 ret = convert_descriptor_to_mode (int_mode, ret);
14897 return ret;
14900 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14901 but after converting arguments to type_die, afterwards
14902 convert back to unsigned. */
14904 static dw_loc_descr_ref
14905 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14906 scalar_int_mode mode, machine_mode mem_mode)
14908 dw_loc_descr_ref cvt, op0, op1;
14910 if (type_die == NULL)
14911 return NULL;
14912 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14913 VAR_INIT_STATUS_INITIALIZED);
14914 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14915 VAR_INIT_STATUS_INITIALIZED);
14916 if (op0 == NULL || op1 == NULL)
14917 return NULL;
14918 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14919 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14920 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14921 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14922 add_loc_descr (&op0, cvt);
14923 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14924 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14925 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14926 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14927 add_loc_descr (&op1, cvt);
14928 add_loc_descr (&op0, op1);
14929 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14930 return convert_descriptor_to_mode (mode, op0);
14933 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14934 const0 is DW_OP_lit0 or corresponding typed constant,
14935 const1 is DW_OP_lit1 or corresponding typed constant
14936 and constMSB is constant with just the MSB bit set
14937 for the mode):
14938 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14939 L1: const0 DW_OP_swap
14940 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14941 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14942 L3: DW_OP_drop
14943 L4: DW_OP_nop
14945 CTZ is similar:
14946 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14947 L1: const0 DW_OP_swap
14948 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14949 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14950 L3: DW_OP_drop
14951 L4: DW_OP_nop
14953 FFS is similar:
14954 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14955 L1: const1 DW_OP_swap
14956 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14957 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14958 L3: DW_OP_drop
14959 L4: DW_OP_nop */
14961 static dw_loc_descr_ref
14962 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14963 machine_mode mem_mode)
14965 dw_loc_descr_ref op0, ret, tmp;
14966 HOST_WIDE_INT valv;
14967 dw_loc_descr_ref l1jump, l1label;
14968 dw_loc_descr_ref l2jump, l2label;
14969 dw_loc_descr_ref l3jump, l3label;
14970 dw_loc_descr_ref l4jump, l4label;
14971 rtx msb;
14973 if (GET_MODE (XEXP (rtl, 0)) != mode)
14974 return NULL;
14976 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14977 VAR_INIT_STATUS_INITIALIZED);
14978 if (op0 == NULL)
14979 return NULL;
14980 ret = op0;
14981 if (GET_CODE (rtl) == CLZ)
14983 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14984 valv = GET_MODE_BITSIZE (mode);
14986 else if (GET_CODE (rtl) == FFS)
14987 valv = 0;
14988 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14989 valv = GET_MODE_BITSIZE (mode);
14990 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14991 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14992 add_loc_descr (&ret, l1jump);
14993 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14994 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14995 VAR_INIT_STATUS_INITIALIZED);
14996 if (tmp == NULL)
14997 return NULL;
14998 add_loc_descr (&ret, tmp);
14999 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
15000 add_loc_descr (&ret, l4jump);
15001 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15002 ? const1_rtx : const0_rtx,
15003 mode, mem_mode,
15004 VAR_INIT_STATUS_INITIALIZED);
15005 if (l1label == NULL)
15006 return NULL;
15007 add_loc_descr (&ret, l1label);
15008 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15009 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15010 add_loc_descr (&ret, l2label);
15011 if (GET_CODE (rtl) != CLZ)
15012 msb = const1_rtx;
15013 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15014 msb = GEN_INT (HOST_WIDE_INT_1U
15015 << (GET_MODE_BITSIZE (mode) - 1));
15016 else
15017 msb = immed_wide_int_const
15018 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15019 GET_MODE_PRECISION (mode)), mode);
15020 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15021 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15022 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15023 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15024 else
15025 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15026 VAR_INIT_STATUS_INITIALIZED);
15027 if (tmp == NULL)
15028 return NULL;
15029 add_loc_descr (&ret, tmp);
15030 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15031 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15032 add_loc_descr (&ret, l3jump);
15033 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15034 VAR_INIT_STATUS_INITIALIZED);
15035 if (tmp == NULL)
15036 return NULL;
15037 add_loc_descr (&ret, tmp);
15038 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15039 ? DW_OP_shl : DW_OP_shr, 0, 0));
15040 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15041 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15042 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15043 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15044 add_loc_descr (&ret, l2jump);
15045 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15046 add_loc_descr (&ret, l3label);
15047 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15048 add_loc_descr (&ret, l4label);
15049 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15050 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15051 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15052 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15053 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15054 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15055 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15056 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15057 return ret;
15060 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15061 const1 is DW_OP_lit1 or corresponding typed constant):
15062 const0 DW_OP_swap
15063 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15064 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15065 L2: DW_OP_drop
15067 PARITY is similar:
15068 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15069 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15070 L2: DW_OP_drop */
15072 static dw_loc_descr_ref
15073 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15074 machine_mode mem_mode)
15076 dw_loc_descr_ref op0, ret, tmp;
15077 dw_loc_descr_ref l1jump, l1label;
15078 dw_loc_descr_ref l2jump, l2label;
15080 if (GET_MODE (XEXP (rtl, 0)) != mode)
15081 return NULL;
15083 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15084 VAR_INIT_STATUS_INITIALIZED);
15085 if (op0 == NULL)
15086 return NULL;
15087 ret = op0;
15088 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15089 VAR_INIT_STATUS_INITIALIZED);
15090 if (tmp == NULL)
15091 return NULL;
15092 add_loc_descr (&ret, tmp);
15093 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15094 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15095 add_loc_descr (&ret, l1label);
15096 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15097 add_loc_descr (&ret, l2jump);
15098 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15099 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15100 tmp = mem_loc_descriptor (const1_rtx, 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 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15107 ? DW_OP_plus : DW_OP_xor, 0, 0));
15108 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15109 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15110 VAR_INIT_STATUS_INITIALIZED);
15111 add_loc_descr (&ret, tmp);
15112 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15113 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15114 add_loc_descr (&ret, l1jump);
15115 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15116 add_loc_descr (&ret, l2label);
15117 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15118 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15119 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15120 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15121 return ret;
15124 /* BSWAP (constS is initial shift count, either 56 or 24):
15125 constS const0
15126 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15127 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15128 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15129 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15130 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15132 static dw_loc_descr_ref
15133 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15134 machine_mode mem_mode)
15136 dw_loc_descr_ref op0, ret, tmp;
15137 dw_loc_descr_ref l1jump, l1label;
15138 dw_loc_descr_ref l2jump, l2label;
15140 if (BITS_PER_UNIT != 8
15141 || (GET_MODE_BITSIZE (mode) != 32
15142 && GET_MODE_BITSIZE (mode) != 64))
15143 return NULL;
15145 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15146 VAR_INIT_STATUS_INITIALIZED);
15147 if (op0 == NULL)
15148 return NULL;
15150 ret = op0;
15151 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15152 mode, mem_mode,
15153 VAR_INIT_STATUS_INITIALIZED);
15154 if (tmp == NULL)
15155 return NULL;
15156 add_loc_descr (&ret, tmp);
15157 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15158 VAR_INIT_STATUS_INITIALIZED);
15159 if (tmp == NULL)
15160 return NULL;
15161 add_loc_descr (&ret, tmp);
15162 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15163 add_loc_descr (&ret, l1label);
15164 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15165 mode, mem_mode,
15166 VAR_INIT_STATUS_INITIALIZED);
15167 add_loc_descr (&ret, tmp);
15168 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15169 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15170 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15171 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15172 VAR_INIT_STATUS_INITIALIZED);
15173 if (tmp == NULL)
15174 return NULL;
15175 add_loc_descr (&ret, tmp);
15176 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15178 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15179 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15180 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15181 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15182 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15183 VAR_INIT_STATUS_INITIALIZED);
15184 add_loc_descr (&ret, tmp);
15185 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15186 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15187 add_loc_descr (&ret, l2jump);
15188 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15189 VAR_INIT_STATUS_INITIALIZED);
15190 add_loc_descr (&ret, tmp);
15191 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15192 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15193 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15194 add_loc_descr (&ret, l1jump);
15195 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15196 add_loc_descr (&ret, l2label);
15197 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15198 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15199 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15200 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15201 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15202 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15203 return ret;
15206 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15207 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15208 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15209 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15211 ROTATERT is similar:
15212 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15213 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15214 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15216 static dw_loc_descr_ref
15217 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15218 machine_mode mem_mode)
15220 rtx rtlop1 = XEXP (rtl, 1);
15221 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15222 int i;
15224 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15225 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15226 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15227 VAR_INIT_STATUS_INITIALIZED);
15228 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15229 VAR_INIT_STATUS_INITIALIZED);
15230 if (op0 == NULL || op1 == NULL)
15231 return NULL;
15232 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15233 for (i = 0; i < 2; i++)
15235 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15236 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15237 mode, mem_mode,
15238 VAR_INIT_STATUS_INITIALIZED);
15239 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15240 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15241 ? DW_OP_const4u
15242 : HOST_BITS_PER_WIDE_INT == 64
15243 ? DW_OP_const8u : DW_OP_constu,
15244 GET_MODE_MASK (mode), 0);
15245 else
15246 mask[i] = NULL;
15247 if (mask[i] == NULL)
15248 return NULL;
15249 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15251 ret = op0;
15252 add_loc_descr (&ret, op1);
15253 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15254 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15255 if (GET_CODE (rtl) == ROTATERT)
15257 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15258 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15259 GET_MODE_BITSIZE (mode), 0));
15261 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15262 if (mask[0] != NULL)
15263 add_loc_descr (&ret, mask[0]);
15264 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15265 if (mask[1] != NULL)
15267 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15268 add_loc_descr (&ret, mask[1]);
15269 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15271 if (GET_CODE (rtl) == ROTATE)
15273 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15274 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15275 GET_MODE_BITSIZE (mode), 0));
15277 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15278 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15279 return ret;
15282 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15283 for DEBUG_PARAMETER_REF RTL. */
15285 static dw_loc_descr_ref
15286 parameter_ref_descriptor (rtx rtl)
15288 dw_loc_descr_ref ret;
15289 dw_die_ref ref;
15291 if (dwarf_strict)
15292 return NULL;
15293 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15294 /* With LTO during LTRANS we get the late DIE that refers to the early
15295 DIE, thus we add another indirection here. This seems to confuse
15296 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15297 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15298 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15299 if (ref)
15301 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15302 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15303 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15305 else
15307 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15308 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15310 return ret;
15313 /* The following routine converts the RTL for a variable or parameter
15314 (resident in memory) into an equivalent Dwarf representation of a
15315 mechanism for getting the address of that same variable onto the top of a
15316 hypothetical "address evaluation" stack.
15318 When creating memory location descriptors, we are effectively transforming
15319 the RTL for a memory-resident object into its Dwarf postfix expression
15320 equivalent. This routine recursively descends an RTL tree, turning
15321 it into Dwarf postfix code as it goes.
15323 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15325 MEM_MODE is the mode of the memory reference, needed to handle some
15326 autoincrement addressing modes.
15328 Return 0 if we can't represent the location. */
15330 dw_loc_descr_ref
15331 mem_loc_descriptor (rtx rtl, machine_mode mode,
15332 machine_mode mem_mode,
15333 enum var_init_status initialized)
15335 dw_loc_descr_ref mem_loc_result = NULL;
15336 enum dwarf_location_atom op;
15337 dw_loc_descr_ref op0, op1;
15338 rtx inner = NULL_RTX;
15339 poly_int64 offset;
15341 if (mode == VOIDmode)
15342 mode = GET_MODE (rtl);
15344 /* Note that for a dynamically sized array, the location we will generate a
15345 description of here will be the lowest numbered location which is
15346 actually within the array. That's *not* necessarily the same as the
15347 zeroth element of the array. */
15349 rtl = targetm.delegitimize_address (rtl);
15351 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15352 return NULL;
15354 scalar_int_mode int_mode, inner_mode, op1_mode;
15355 switch (GET_CODE (rtl))
15357 case POST_INC:
15358 case POST_DEC:
15359 case POST_MODIFY:
15360 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15362 case SUBREG:
15363 /* The case of a subreg may arise when we have a local (register)
15364 variable or a formal (register) parameter which doesn't quite fill
15365 up an entire register. For now, just assume that it is
15366 legitimate to make the Dwarf info refer to the whole register which
15367 contains the given subreg. */
15368 if (!subreg_lowpart_p (rtl))
15369 break;
15370 inner = SUBREG_REG (rtl);
15371 /* FALLTHRU */
15372 case TRUNCATE:
15373 if (inner == NULL_RTX)
15374 inner = XEXP (rtl, 0);
15375 if (is_a <scalar_int_mode> (mode, &int_mode)
15376 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15377 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15378 #ifdef POINTERS_EXTEND_UNSIGNED
15379 || (int_mode == Pmode && mem_mode != VOIDmode)
15380 #endif
15382 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15384 mem_loc_result = mem_loc_descriptor (inner,
15385 inner_mode,
15386 mem_mode, initialized);
15387 break;
15389 if (dwarf_strict && dwarf_version < 5)
15390 break;
15391 if (is_a <scalar_int_mode> (mode, &int_mode)
15392 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15393 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15394 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15396 dw_die_ref type_die;
15397 dw_loc_descr_ref cvt;
15399 mem_loc_result = mem_loc_descriptor (inner,
15400 GET_MODE (inner),
15401 mem_mode, initialized);
15402 if (mem_loc_result == NULL)
15403 break;
15404 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15405 if (type_die == NULL)
15407 mem_loc_result = NULL;
15408 break;
15410 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15411 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15412 else
15413 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15414 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15415 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15416 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15417 add_loc_descr (&mem_loc_result, cvt);
15418 if (is_a <scalar_int_mode> (mode, &int_mode)
15419 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15421 /* Convert it to untyped afterwards. */
15422 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15423 add_loc_descr (&mem_loc_result, cvt);
15426 break;
15428 case REG:
15429 if (!is_a <scalar_int_mode> (mode, &int_mode)
15430 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15431 && rtl != arg_pointer_rtx
15432 && rtl != frame_pointer_rtx
15433 #ifdef POINTERS_EXTEND_UNSIGNED
15434 && (int_mode != Pmode || mem_mode == VOIDmode)
15435 #endif
15438 dw_die_ref type_die;
15439 unsigned int dbx_regnum;
15441 if (dwarf_strict && dwarf_version < 5)
15442 break;
15443 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15444 break;
15445 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15446 if (type_die == NULL)
15447 break;
15449 dbx_regnum = dbx_reg_number (rtl);
15450 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15451 break;
15452 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15453 dbx_regnum, 0);
15454 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15455 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15456 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15457 break;
15459 /* Whenever a register number forms a part of the description of the
15460 method for calculating the (dynamic) address of a memory resident
15461 object, DWARF rules require the register number be referred to as
15462 a "base register". This distinction is not based in any way upon
15463 what category of register the hardware believes the given register
15464 belongs to. This is strictly DWARF terminology we're dealing with
15465 here. Note that in cases where the location of a memory-resident
15466 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15467 OP_CONST (0)) the actual DWARF location descriptor that we generate
15468 may just be OP_BASEREG (basereg). This may look deceptively like
15469 the object in question was allocated to a register (rather than in
15470 memory) so DWARF consumers need to be aware of the subtle
15471 distinction between OP_REG and OP_BASEREG. */
15472 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15473 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15474 else if (stack_realign_drap
15475 && crtl->drap_reg
15476 && crtl->args.internal_arg_pointer == rtl
15477 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15479 /* If RTL is internal_arg_pointer, which has been optimized
15480 out, use DRAP instead. */
15481 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15482 VAR_INIT_STATUS_INITIALIZED);
15484 break;
15486 case SIGN_EXTEND:
15487 case ZERO_EXTEND:
15488 if (!is_a <scalar_int_mode> (mode, &int_mode)
15489 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15490 break;
15491 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15492 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15493 if (op0 == 0)
15494 break;
15495 else if (GET_CODE (rtl) == ZERO_EXTEND
15496 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15497 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15498 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15499 to expand zero extend as two shifts instead of
15500 masking. */
15501 && GET_MODE_SIZE (inner_mode) <= 4)
15503 mem_loc_result = op0;
15504 add_loc_descr (&mem_loc_result,
15505 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15506 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15508 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15510 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15511 shift *= BITS_PER_UNIT;
15512 if (GET_CODE (rtl) == SIGN_EXTEND)
15513 op = DW_OP_shra;
15514 else
15515 op = DW_OP_shr;
15516 mem_loc_result = op0;
15517 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15518 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15519 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15520 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15522 else if (!dwarf_strict || dwarf_version >= 5)
15524 dw_die_ref type_die1, type_die2;
15525 dw_loc_descr_ref cvt;
15527 type_die1 = base_type_for_mode (inner_mode,
15528 GET_CODE (rtl) == ZERO_EXTEND);
15529 if (type_die1 == NULL)
15530 break;
15531 type_die2 = base_type_for_mode (int_mode, 1);
15532 if (type_die2 == NULL)
15533 break;
15534 mem_loc_result = op0;
15535 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15536 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15537 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15538 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15539 add_loc_descr (&mem_loc_result, cvt);
15540 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15541 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15542 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15543 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15544 add_loc_descr (&mem_loc_result, cvt);
15546 break;
15548 case MEM:
15550 rtx new_rtl = avoid_constant_pool_reference (rtl);
15551 if (new_rtl != rtl)
15553 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15554 initialized);
15555 if (mem_loc_result != NULL)
15556 return mem_loc_result;
15559 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15560 get_address_mode (rtl), mode,
15561 VAR_INIT_STATUS_INITIALIZED);
15562 if (mem_loc_result == NULL)
15563 mem_loc_result = tls_mem_loc_descriptor (rtl);
15564 if (mem_loc_result != NULL)
15566 if (!is_a <scalar_int_mode> (mode, &int_mode)
15567 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15569 dw_die_ref type_die;
15570 dw_loc_descr_ref deref;
15571 HOST_WIDE_INT size;
15573 if (dwarf_strict && dwarf_version < 5)
15574 return NULL;
15575 if (!GET_MODE_SIZE (mode).is_constant (&size))
15576 return NULL;
15577 type_die
15578 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15579 if (type_die == NULL)
15580 return NULL;
15581 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15582 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15583 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15584 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15585 add_loc_descr (&mem_loc_result, deref);
15587 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15588 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15589 else
15590 add_loc_descr (&mem_loc_result,
15591 new_loc_descr (DW_OP_deref_size,
15592 GET_MODE_SIZE (int_mode), 0));
15594 break;
15596 case LO_SUM:
15597 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15599 case LABEL_REF:
15600 /* Some ports can transform a symbol ref into a label ref, because
15601 the symbol ref is too far away and has to be dumped into a constant
15602 pool. */
15603 case CONST:
15604 case SYMBOL_REF:
15605 if (!is_a <scalar_int_mode> (mode, &int_mode)
15606 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15607 #ifdef POINTERS_EXTEND_UNSIGNED
15608 && (int_mode != Pmode || mem_mode == VOIDmode)
15609 #endif
15611 break;
15612 if (GET_CODE (rtl) == SYMBOL_REF
15613 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15615 dw_loc_descr_ref temp;
15617 /* If this is not defined, we have no way to emit the data. */
15618 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15619 break;
15621 temp = new_addr_loc_descr (rtl, dtprel_true);
15623 /* We check for DWARF 5 here because gdb did not implement
15624 DW_OP_form_tls_address until after 7.12. */
15625 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15626 ? DW_OP_form_tls_address
15627 : DW_OP_GNU_push_tls_address),
15628 0, 0);
15629 add_loc_descr (&mem_loc_result, temp);
15631 break;
15634 if (!const_ok_for_output (rtl))
15636 if (GET_CODE (rtl) == CONST)
15637 switch (GET_CODE (XEXP (rtl, 0)))
15639 case NOT:
15640 op = DW_OP_not;
15641 goto try_const_unop;
15642 case NEG:
15643 op = DW_OP_neg;
15644 goto try_const_unop;
15645 try_const_unop:
15646 rtx arg;
15647 arg = XEXP (XEXP (rtl, 0), 0);
15648 if (!CONSTANT_P (arg))
15649 arg = gen_rtx_CONST (int_mode, arg);
15650 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15651 initialized);
15652 if (op0)
15654 mem_loc_result = op0;
15655 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15657 break;
15658 default:
15659 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15660 mem_mode, initialized);
15661 break;
15663 break;
15666 symref:
15667 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15668 vec_safe_push (used_rtx_array, rtl);
15669 break;
15671 case CONCAT:
15672 case CONCATN:
15673 case VAR_LOCATION:
15674 case DEBUG_IMPLICIT_PTR:
15675 expansion_failed (NULL_TREE, rtl,
15676 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15677 return 0;
15679 case ENTRY_VALUE:
15680 if (dwarf_strict && dwarf_version < 5)
15681 return NULL;
15682 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15684 if (!is_a <scalar_int_mode> (mode, &int_mode)
15685 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15686 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15687 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15688 else
15690 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15691 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15692 return NULL;
15693 op0 = one_reg_loc_descriptor (dbx_regnum,
15694 VAR_INIT_STATUS_INITIALIZED);
15697 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15698 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15700 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15701 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15702 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15703 return NULL;
15705 else
15706 gcc_unreachable ();
15707 if (op0 == NULL)
15708 return NULL;
15709 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15710 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15711 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15712 break;
15714 case DEBUG_PARAMETER_REF:
15715 mem_loc_result = parameter_ref_descriptor (rtl);
15716 break;
15718 case PRE_MODIFY:
15719 /* Extract the PLUS expression nested inside and fall into
15720 PLUS code below. */
15721 rtl = XEXP (rtl, 1);
15722 goto plus;
15724 case PRE_INC:
15725 case PRE_DEC:
15726 /* Turn these into a PLUS expression and fall into the PLUS code
15727 below. */
15728 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15729 gen_int_mode (GET_CODE (rtl) == PRE_INC
15730 ? GET_MODE_UNIT_SIZE (mem_mode)
15731 : -GET_MODE_UNIT_SIZE (mem_mode),
15732 mode));
15734 /* fall through */
15736 case PLUS:
15737 plus:
15738 if (is_based_loc (rtl)
15739 && is_a <scalar_int_mode> (mode, &int_mode)
15740 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15741 || XEXP (rtl, 0) == arg_pointer_rtx
15742 || XEXP (rtl, 0) == frame_pointer_rtx))
15743 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15744 INTVAL (XEXP (rtl, 1)),
15745 VAR_INIT_STATUS_INITIALIZED);
15746 else
15748 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15749 VAR_INIT_STATUS_INITIALIZED);
15750 if (mem_loc_result == 0)
15751 break;
15753 if (CONST_INT_P (XEXP (rtl, 1))
15754 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15755 <= DWARF2_ADDR_SIZE))
15756 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15757 else
15759 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15760 VAR_INIT_STATUS_INITIALIZED);
15761 if (op1 == 0)
15762 return NULL;
15763 add_loc_descr (&mem_loc_result, op1);
15764 add_loc_descr (&mem_loc_result,
15765 new_loc_descr (DW_OP_plus, 0, 0));
15768 break;
15770 /* If a pseudo-reg is optimized away, it is possible for it to
15771 be replaced with a MEM containing a multiply or shift. */
15772 case MINUS:
15773 op = DW_OP_minus;
15774 goto do_binop;
15776 case MULT:
15777 op = DW_OP_mul;
15778 goto do_binop;
15780 case DIV:
15781 if ((!dwarf_strict || dwarf_version >= 5)
15782 && is_a <scalar_int_mode> (mode, &int_mode)
15783 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15785 mem_loc_result = typed_binop (DW_OP_div, rtl,
15786 base_type_for_mode (mode, 0),
15787 int_mode, mem_mode);
15788 break;
15790 op = DW_OP_div;
15791 goto do_binop;
15793 case UMOD:
15794 op = DW_OP_mod;
15795 goto do_binop;
15797 case ASHIFT:
15798 op = DW_OP_shl;
15799 goto do_shift;
15801 case ASHIFTRT:
15802 op = DW_OP_shra;
15803 goto do_shift;
15805 case LSHIFTRT:
15806 op = DW_OP_shr;
15807 goto do_shift;
15809 do_shift:
15810 if (!is_a <scalar_int_mode> (mode, &int_mode))
15811 break;
15812 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15813 VAR_INIT_STATUS_INITIALIZED);
15815 rtx rtlop1 = XEXP (rtl, 1);
15816 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15817 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15818 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15819 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15820 VAR_INIT_STATUS_INITIALIZED);
15823 if (op0 == 0 || op1 == 0)
15824 break;
15826 mem_loc_result = op0;
15827 add_loc_descr (&mem_loc_result, op1);
15828 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15829 break;
15831 case AND:
15832 op = DW_OP_and;
15833 goto do_binop;
15835 case IOR:
15836 op = DW_OP_or;
15837 goto do_binop;
15839 case XOR:
15840 op = DW_OP_xor;
15841 goto do_binop;
15843 do_binop:
15844 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15845 VAR_INIT_STATUS_INITIALIZED);
15846 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15847 VAR_INIT_STATUS_INITIALIZED);
15849 if (op0 == 0 || op1 == 0)
15850 break;
15852 mem_loc_result = op0;
15853 add_loc_descr (&mem_loc_result, op1);
15854 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15855 break;
15857 case MOD:
15858 if ((!dwarf_strict || dwarf_version >= 5)
15859 && is_a <scalar_int_mode> (mode, &int_mode)
15860 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15862 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15863 base_type_for_mode (mode, 0),
15864 int_mode, mem_mode);
15865 break;
15868 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15869 VAR_INIT_STATUS_INITIALIZED);
15870 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15871 VAR_INIT_STATUS_INITIALIZED);
15873 if (op0 == 0 || op1 == 0)
15874 break;
15876 mem_loc_result = op0;
15877 add_loc_descr (&mem_loc_result, op1);
15878 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15879 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15880 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15881 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15882 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15883 break;
15885 case UDIV:
15886 if ((!dwarf_strict || dwarf_version >= 5)
15887 && is_a <scalar_int_mode> (mode, &int_mode))
15889 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15891 op = DW_OP_div;
15892 goto do_binop;
15894 mem_loc_result = typed_binop (DW_OP_div, rtl,
15895 base_type_for_mode (int_mode, 1),
15896 int_mode, mem_mode);
15898 break;
15900 case NOT:
15901 op = DW_OP_not;
15902 goto do_unop;
15904 case ABS:
15905 op = DW_OP_abs;
15906 goto do_unop;
15908 case NEG:
15909 op = DW_OP_neg;
15910 goto do_unop;
15912 do_unop:
15913 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15914 VAR_INIT_STATUS_INITIALIZED);
15916 if (op0 == 0)
15917 break;
15919 mem_loc_result = op0;
15920 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15921 break;
15923 case CONST_INT:
15924 if (!is_a <scalar_int_mode> (mode, &int_mode)
15925 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15926 #ifdef POINTERS_EXTEND_UNSIGNED
15927 || (int_mode == Pmode
15928 && mem_mode != VOIDmode
15929 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15930 #endif
15933 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15934 break;
15936 if ((!dwarf_strict || dwarf_version >= 5)
15937 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15938 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15940 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15941 scalar_int_mode amode;
15942 if (type_die == NULL)
15943 return NULL;
15944 if (INTVAL (rtl) >= 0
15945 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15946 .exists (&amode))
15947 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15948 /* const DW_OP_convert <XXX> vs.
15949 DW_OP_const_type <XXX, 1, const>. */
15950 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15951 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15953 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15954 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15955 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15956 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15957 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15958 add_loc_descr (&mem_loc_result, op0);
15959 return mem_loc_result;
15961 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15962 INTVAL (rtl));
15963 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15964 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15965 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15966 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15967 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15968 else
15970 mem_loc_result->dw_loc_oprnd2.val_class
15971 = dw_val_class_const_double;
15972 mem_loc_result->dw_loc_oprnd2.v.val_double
15973 = double_int::from_shwi (INTVAL (rtl));
15976 break;
15978 case CONST_DOUBLE:
15979 if (!dwarf_strict || dwarf_version >= 5)
15981 dw_die_ref type_die;
15983 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15984 CONST_DOUBLE rtx could represent either a large integer
15985 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15986 the value is always a floating point constant.
15988 When it is an integer, a CONST_DOUBLE is used whenever
15989 the constant requires 2 HWIs to be adequately represented.
15990 We output CONST_DOUBLEs as blocks. */
15991 if (mode == VOIDmode
15992 || (GET_MODE (rtl) == VOIDmode
15993 && maybe_ne (GET_MODE_BITSIZE (mode),
15994 HOST_BITS_PER_DOUBLE_INT)))
15995 break;
15996 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15997 if (type_die == NULL)
15998 return NULL;
15999 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16000 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16001 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16002 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16003 #if TARGET_SUPPORTS_WIDE_INT == 0
16004 if (!SCALAR_FLOAT_MODE_P (mode))
16006 mem_loc_result->dw_loc_oprnd2.val_class
16007 = dw_val_class_const_double;
16008 mem_loc_result->dw_loc_oprnd2.v.val_double
16009 = rtx_to_double_int (rtl);
16011 else
16012 #endif
16014 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16015 unsigned int length = GET_MODE_SIZE (float_mode);
16016 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16018 insert_float (rtl, array);
16019 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16020 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16021 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16022 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16025 break;
16027 case CONST_WIDE_INT:
16028 if (!dwarf_strict || dwarf_version >= 5)
16030 dw_die_ref type_die;
16032 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16033 if (type_die == NULL)
16034 return NULL;
16035 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16036 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16037 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16038 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16039 mem_loc_result->dw_loc_oprnd2.val_class
16040 = dw_val_class_wide_int;
16041 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16042 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16044 break;
16046 case CONST_POLY_INT:
16047 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16048 break;
16050 case EQ:
16051 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16052 break;
16054 case GE:
16055 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16056 break;
16058 case GT:
16059 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16060 break;
16062 case LE:
16063 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16064 break;
16066 case LT:
16067 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16068 break;
16070 case NE:
16071 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16072 break;
16074 case GEU:
16075 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16076 break;
16078 case GTU:
16079 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16080 break;
16082 case LEU:
16083 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16084 break;
16086 case LTU:
16087 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16088 break;
16090 case UMIN:
16091 case UMAX:
16092 if (!SCALAR_INT_MODE_P (mode))
16093 break;
16094 /* FALLTHRU */
16095 case SMIN:
16096 case SMAX:
16097 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16098 break;
16100 case ZERO_EXTRACT:
16101 case SIGN_EXTRACT:
16102 if (CONST_INT_P (XEXP (rtl, 1))
16103 && CONST_INT_P (XEXP (rtl, 2))
16104 && is_a <scalar_int_mode> (mode, &int_mode)
16105 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16106 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16107 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16108 && ((unsigned) INTVAL (XEXP (rtl, 1))
16109 + (unsigned) INTVAL (XEXP (rtl, 2))
16110 <= GET_MODE_BITSIZE (int_mode)))
16112 int shift, size;
16113 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16114 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16115 if (op0 == 0)
16116 break;
16117 if (GET_CODE (rtl) == SIGN_EXTRACT)
16118 op = DW_OP_shra;
16119 else
16120 op = DW_OP_shr;
16121 mem_loc_result = op0;
16122 size = INTVAL (XEXP (rtl, 1));
16123 shift = INTVAL (XEXP (rtl, 2));
16124 if (BITS_BIG_ENDIAN)
16125 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16126 if (shift + size != (int) DWARF2_ADDR_SIZE)
16128 add_loc_descr (&mem_loc_result,
16129 int_loc_descriptor (DWARF2_ADDR_SIZE
16130 - shift - size));
16131 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16133 if (size != (int) DWARF2_ADDR_SIZE)
16135 add_loc_descr (&mem_loc_result,
16136 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16137 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16140 break;
16142 case IF_THEN_ELSE:
16144 dw_loc_descr_ref op2, bra_node, drop_node;
16145 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16146 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16147 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16148 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16149 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16150 VAR_INIT_STATUS_INITIALIZED);
16151 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16152 VAR_INIT_STATUS_INITIALIZED);
16153 if (op0 == NULL || op1 == NULL || op2 == NULL)
16154 break;
16156 mem_loc_result = op1;
16157 add_loc_descr (&mem_loc_result, op2);
16158 add_loc_descr (&mem_loc_result, op0);
16159 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16160 add_loc_descr (&mem_loc_result, bra_node);
16161 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16162 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16163 add_loc_descr (&mem_loc_result, drop_node);
16164 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16165 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16167 break;
16169 case FLOAT_EXTEND:
16170 case FLOAT_TRUNCATE:
16171 case FLOAT:
16172 case UNSIGNED_FLOAT:
16173 case FIX:
16174 case UNSIGNED_FIX:
16175 if (!dwarf_strict || dwarf_version >= 5)
16177 dw_die_ref type_die;
16178 dw_loc_descr_ref cvt;
16180 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16181 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16182 if (op0 == NULL)
16183 break;
16184 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16185 && (GET_CODE (rtl) == FLOAT
16186 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16188 type_die = base_type_for_mode (int_mode,
16189 GET_CODE (rtl) == UNSIGNED_FLOAT);
16190 if (type_die == NULL)
16191 break;
16192 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16193 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16194 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16195 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16196 add_loc_descr (&op0, cvt);
16198 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16199 if (type_die == NULL)
16200 break;
16201 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16202 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16203 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16204 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16205 add_loc_descr (&op0, cvt);
16206 if (is_a <scalar_int_mode> (mode, &int_mode)
16207 && (GET_CODE (rtl) == FIX
16208 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16210 op0 = convert_descriptor_to_mode (int_mode, op0);
16211 if (op0 == NULL)
16212 break;
16214 mem_loc_result = op0;
16216 break;
16218 case CLZ:
16219 case CTZ:
16220 case FFS:
16221 if (is_a <scalar_int_mode> (mode, &int_mode))
16222 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16223 break;
16225 case POPCOUNT:
16226 case PARITY:
16227 if (is_a <scalar_int_mode> (mode, &int_mode))
16228 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16229 break;
16231 case BSWAP:
16232 if (is_a <scalar_int_mode> (mode, &int_mode))
16233 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16234 break;
16236 case ROTATE:
16237 case ROTATERT:
16238 if (is_a <scalar_int_mode> (mode, &int_mode))
16239 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16240 break;
16242 case COMPARE:
16243 /* In theory, we could implement the above. */
16244 /* DWARF cannot represent the unsigned compare operations
16245 natively. */
16246 case SS_MULT:
16247 case US_MULT:
16248 case SS_DIV:
16249 case US_DIV:
16250 case SS_PLUS:
16251 case US_PLUS:
16252 case SS_MINUS:
16253 case US_MINUS:
16254 case SS_NEG:
16255 case US_NEG:
16256 case SS_ABS:
16257 case SS_ASHIFT:
16258 case US_ASHIFT:
16259 case SS_TRUNCATE:
16260 case US_TRUNCATE:
16261 case UNORDERED:
16262 case ORDERED:
16263 case UNEQ:
16264 case UNGE:
16265 case UNGT:
16266 case UNLE:
16267 case UNLT:
16268 case LTGT:
16269 case FRACT_CONVERT:
16270 case UNSIGNED_FRACT_CONVERT:
16271 case SAT_FRACT:
16272 case UNSIGNED_SAT_FRACT:
16273 case SQRT:
16274 case ASM_OPERANDS:
16275 case VEC_MERGE:
16276 case VEC_SELECT:
16277 case VEC_CONCAT:
16278 case VEC_DUPLICATE:
16279 case VEC_SERIES:
16280 case UNSPEC:
16281 case HIGH:
16282 case FMA:
16283 case STRICT_LOW_PART:
16284 case CONST_VECTOR:
16285 case CONST_FIXED:
16286 case CLRSB:
16287 case CLOBBER:
16288 case CLOBBER_HIGH:
16289 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16290 can't express it in the debug info. This can happen e.g. with some
16291 TLS UNSPECs. */
16292 break;
16294 case CONST_STRING:
16295 resolve_one_addr (&rtl);
16296 goto symref;
16298 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16299 the expression. An UNSPEC rtx represents a raw DWARF operation,
16300 new_loc_descr is called for it to build the operation directly.
16301 Otherwise mem_loc_descriptor is called recursively. */
16302 case PARALLEL:
16304 int index = 0;
16305 dw_loc_descr_ref exp_result = NULL;
16307 for (; index < XVECLEN (rtl, 0); index++)
16309 rtx elem = XVECEXP (rtl, 0, index);
16310 if (GET_CODE (elem) == UNSPEC)
16312 /* Each DWARF operation UNSPEC contain two operands, if
16313 one operand is not used for the operation, const0_rtx is
16314 passed. */
16315 gcc_assert (XVECLEN (elem, 0) == 2);
16317 HOST_WIDE_INT dw_op = XINT (elem, 1);
16318 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16319 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16320 exp_result
16321 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16322 oprnd2);
16324 else
16325 exp_result
16326 = mem_loc_descriptor (elem, mode, mem_mode,
16327 VAR_INIT_STATUS_INITIALIZED);
16329 if (!mem_loc_result)
16330 mem_loc_result = exp_result;
16331 else
16332 add_loc_descr (&mem_loc_result, exp_result);
16335 break;
16338 default:
16339 if (flag_checking)
16341 print_rtl (stderr, rtl);
16342 gcc_unreachable ();
16344 break;
16347 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16348 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16350 return mem_loc_result;
16353 /* Return a descriptor that describes the concatenation of two locations.
16354 This is typically a complex variable. */
16356 static dw_loc_descr_ref
16357 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16359 /* At present we only track constant-sized pieces. */
16360 unsigned int size0, size1;
16361 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16362 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16363 return 0;
16365 dw_loc_descr_ref cc_loc_result = NULL;
16366 dw_loc_descr_ref x0_ref
16367 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16368 dw_loc_descr_ref x1_ref
16369 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16371 if (x0_ref == 0 || x1_ref == 0)
16372 return 0;
16374 cc_loc_result = x0_ref;
16375 add_loc_descr_op_piece (&cc_loc_result, size0);
16377 add_loc_descr (&cc_loc_result, x1_ref);
16378 add_loc_descr_op_piece (&cc_loc_result, size1);
16380 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16381 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16383 return cc_loc_result;
16386 /* Return a descriptor that describes the concatenation of N
16387 locations. */
16389 static dw_loc_descr_ref
16390 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16392 unsigned int i;
16393 dw_loc_descr_ref cc_loc_result = NULL;
16394 unsigned int n = XVECLEN (concatn, 0);
16395 unsigned int size;
16397 for (i = 0; i < n; ++i)
16399 dw_loc_descr_ref ref;
16400 rtx x = XVECEXP (concatn, 0, i);
16402 /* At present we only track constant-sized pieces. */
16403 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16404 return NULL;
16406 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16407 if (ref == NULL)
16408 return NULL;
16410 add_loc_descr (&cc_loc_result, ref);
16411 add_loc_descr_op_piece (&cc_loc_result, size);
16414 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16415 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16417 return cc_loc_result;
16420 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16421 for DEBUG_IMPLICIT_PTR RTL. */
16423 static dw_loc_descr_ref
16424 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16426 dw_loc_descr_ref ret;
16427 dw_die_ref ref;
16429 if (dwarf_strict && dwarf_version < 5)
16430 return NULL;
16431 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16432 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16433 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16434 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16435 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16436 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16437 if (ref)
16439 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16440 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16441 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16443 else
16445 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16446 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16448 return ret;
16451 /* Output a proper Dwarf location descriptor for a variable or parameter
16452 which is either allocated in a register or in a memory location. For a
16453 register, we just generate an OP_REG and the register number. For a
16454 memory location we provide a Dwarf postfix expression describing how to
16455 generate the (dynamic) address of the object onto the address stack.
16457 MODE is mode of the decl if this loc_descriptor is going to be used in
16458 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16459 allowed, VOIDmode otherwise.
16461 If we don't know how to describe it, return 0. */
16463 static dw_loc_descr_ref
16464 loc_descriptor (rtx rtl, machine_mode mode,
16465 enum var_init_status initialized)
16467 dw_loc_descr_ref loc_result = NULL;
16468 scalar_int_mode int_mode;
16470 switch (GET_CODE (rtl))
16472 case SUBREG:
16473 /* The case of a subreg may arise when we have a local (register)
16474 variable or a formal (register) parameter which doesn't quite fill
16475 up an entire register. For now, just assume that it is
16476 legitimate to make the Dwarf info refer to the whole register which
16477 contains the given subreg. */
16478 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16479 loc_result = loc_descriptor (SUBREG_REG (rtl),
16480 GET_MODE (SUBREG_REG (rtl)), initialized);
16481 else
16482 goto do_default;
16483 break;
16485 case REG:
16486 loc_result = reg_loc_descriptor (rtl, initialized);
16487 break;
16489 case MEM:
16490 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16491 GET_MODE (rtl), initialized);
16492 if (loc_result == NULL)
16493 loc_result = tls_mem_loc_descriptor (rtl);
16494 if (loc_result == NULL)
16496 rtx new_rtl = avoid_constant_pool_reference (rtl);
16497 if (new_rtl != rtl)
16498 loc_result = loc_descriptor (new_rtl, mode, initialized);
16500 break;
16502 case CONCAT:
16503 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16504 initialized);
16505 break;
16507 case CONCATN:
16508 loc_result = concatn_loc_descriptor (rtl, initialized);
16509 break;
16511 case VAR_LOCATION:
16512 /* Single part. */
16513 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16515 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16516 if (GET_CODE (loc) == EXPR_LIST)
16517 loc = XEXP (loc, 0);
16518 loc_result = loc_descriptor (loc, mode, initialized);
16519 break;
16522 rtl = XEXP (rtl, 1);
16523 /* FALLTHRU */
16525 case PARALLEL:
16527 rtvec par_elems = XVEC (rtl, 0);
16528 int num_elem = GET_NUM_ELEM (par_elems);
16529 machine_mode mode;
16530 int i, size;
16532 /* Create the first one, so we have something to add to. */
16533 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16534 VOIDmode, initialized);
16535 if (loc_result == NULL)
16536 return NULL;
16537 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16538 /* At present we only track constant-sized pieces. */
16539 if (!GET_MODE_SIZE (mode).is_constant (&size))
16540 return NULL;
16541 add_loc_descr_op_piece (&loc_result, size);
16542 for (i = 1; i < num_elem; i++)
16544 dw_loc_descr_ref temp;
16546 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16547 VOIDmode, initialized);
16548 if (temp == NULL)
16549 return NULL;
16550 add_loc_descr (&loc_result, temp);
16551 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16552 /* At present we only track constant-sized pieces. */
16553 if (!GET_MODE_SIZE (mode).is_constant (&size))
16554 return NULL;
16555 add_loc_descr_op_piece (&loc_result, size);
16558 break;
16560 case CONST_INT:
16561 if (mode != VOIDmode && mode != BLKmode)
16563 int_mode = as_a <scalar_int_mode> (mode);
16564 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16565 INTVAL (rtl));
16567 break;
16569 case CONST_DOUBLE:
16570 if (mode == VOIDmode)
16571 mode = GET_MODE (rtl);
16573 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16575 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16577 /* Note that a CONST_DOUBLE rtx could represent either an integer
16578 or a floating-point constant. A CONST_DOUBLE is used whenever
16579 the constant requires more than one word in order to be
16580 adequately represented. We output CONST_DOUBLEs as blocks. */
16581 scalar_mode smode = as_a <scalar_mode> (mode);
16582 loc_result = new_loc_descr (DW_OP_implicit_value,
16583 GET_MODE_SIZE (smode), 0);
16584 #if TARGET_SUPPORTS_WIDE_INT == 0
16585 if (!SCALAR_FLOAT_MODE_P (smode))
16587 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16588 loc_result->dw_loc_oprnd2.v.val_double
16589 = rtx_to_double_int (rtl);
16591 else
16592 #endif
16594 unsigned int length = GET_MODE_SIZE (smode);
16595 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16597 insert_float (rtl, array);
16598 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16599 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16600 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16601 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16604 break;
16606 case CONST_WIDE_INT:
16607 if (mode == VOIDmode)
16608 mode = GET_MODE (rtl);
16610 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16612 int_mode = as_a <scalar_int_mode> (mode);
16613 loc_result = new_loc_descr (DW_OP_implicit_value,
16614 GET_MODE_SIZE (int_mode), 0);
16615 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16616 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16617 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16619 break;
16621 case CONST_VECTOR:
16622 if (mode == VOIDmode)
16623 mode = GET_MODE (rtl);
16625 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16627 unsigned int length;
16628 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16629 return NULL;
16631 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16632 unsigned char *array
16633 = ggc_vec_alloc<unsigned char> (length * elt_size);
16634 unsigned int i;
16635 unsigned char *p;
16636 machine_mode imode = GET_MODE_INNER (mode);
16638 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16639 switch (GET_MODE_CLASS (mode))
16641 case MODE_VECTOR_INT:
16642 for (i = 0, p = array; i < length; i++, p += elt_size)
16644 rtx elt = CONST_VECTOR_ELT (rtl, i);
16645 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16647 break;
16649 case MODE_VECTOR_FLOAT:
16650 for (i = 0, p = array; i < length; i++, p += elt_size)
16652 rtx elt = CONST_VECTOR_ELT (rtl, i);
16653 insert_float (elt, p);
16655 break;
16657 default:
16658 gcc_unreachable ();
16661 loc_result = new_loc_descr (DW_OP_implicit_value,
16662 length * elt_size, 0);
16663 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16664 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16665 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16666 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16668 break;
16670 case CONST:
16671 if (mode == VOIDmode
16672 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16673 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16674 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16676 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16677 break;
16679 /* FALLTHROUGH */
16680 case SYMBOL_REF:
16681 if (!const_ok_for_output (rtl))
16682 break;
16683 /* FALLTHROUGH */
16684 case LABEL_REF:
16685 if (is_a <scalar_int_mode> (mode, &int_mode)
16686 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16687 && (dwarf_version >= 4 || !dwarf_strict))
16689 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16690 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16691 vec_safe_push (used_rtx_array, rtl);
16693 break;
16695 case DEBUG_IMPLICIT_PTR:
16696 loc_result = implicit_ptr_descriptor (rtl, 0);
16697 break;
16699 case PLUS:
16700 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16701 && CONST_INT_P (XEXP (rtl, 1)))
16703 loc_result
16704 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16705 break;
16707 /* FALLTHRU */
16708 do_default:
16709 default:
16710 if ((is_a <scalar_int_mode> (mode, &int_mode)
16711 && GET_MODE (rtl) == int_mode
16712 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16713 && dwarf_version >= 4)
16714 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16716 /* Value expression. */
16717 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16718 if (loc_result)
16719 add_loc_descr (&loc_result,
16720 new_loc_descr (DW_OP_stack_value, 0, 0));
16722 break;
16725 return loc_result;
16728 /* We need to figure out what section we should use as the base for the
16729 address ranges where a given location is valid.
16730 1. If this particular DECL has a section associated with it, use that.
16731 2. If this function has a section associated with it, use that.
16732 3. Otherwise, use the text section.
16733 XXX: If you split a variable across multiple sections, we won't notice. */
16735 static const char *
16736 secname_for_decl (const_tree decl)
16738 const char *secname;
16740 if (VAR_OR_FUNCTION_DECL_P (decl)
16741 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16742 && DECL_SECTION_NAME (decl))
16743 secname = DECL_SECTION_NAME (decl);
16744 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16745 secname = DECL_SECTION_NAME (current_function_decl);
16746 else if (cfun && in_cold_section_p)
16747 secname = crtl->subsections.cold_section_label;
16748 else
16749 secname = text_section_label;
16751 return secname;
16754 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16756 static bool
16757 decl_by_reference_p (tree decl)
16759 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16760 || VAR_P (decl))
16761 && DECL_BY_REFERENCE (decl));
16764 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16765 for VARLOC. */
16767 static dw_loc_descr_ref
16768 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16769 enum var_init_status initialized)
16771 int have_address = 0;
16772 dw_loc_descr_ref descr;
16773 machine_mode mode;
16775 if (want_address != 2)
16777 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16778 /* Single part. */
16779 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16781 varloc = PAT_VAR_LOCATION_LOC (varloc);
16782 if (GET_CODE (varloc) == EXPR_LIST)
16783 varloc = XEXP (varloc, 0);
16784 mode = GET_MODE (varloc);
16785 if (MEM_P (varloc))
16787 rtx addr = XEXP (varloc, 0);
16788 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16789 mode, initialized);
16790 if (descr)
16791 have_address = 1;
16792 else
16794 rtx x = avoid_constant_pool_reference (varloc);
16795 if (x != varloc)
16796 descr = mem_loc_descriptor (x, mode, VOIDmode,
16797 initialized);
16800 else
16801 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16803 else
16804 return 0;
16806 else
16808 if (GET_CODE (varloc) == VAR_LOCATION)
16809 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16810 else
16811 mode = DECL_MODE (loc);
16812 descr = loc_descriptor (varloc, mode, initialized);
16813 have_address = 1;
16816 if (!descr)
16817 return 0;
16819 if (want_address == 2 && !have_address
16820 && (dwarf_version >= 4 || !dwarf_strict))
16822 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16824 expansion_failed (loc, NULL_RTX,
16825 "DWARF address size mismatch");
16826 return 0;
16828 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16829 have_address = 1;
16831 /* Show if we can't fill the request for an address. */
16832 if (want_address && !have_address)
16834 expansion_failed (loc, NULL_RTX,
16835 "Want address and only have value");
16836 return 0;
16839 /* If we've got an address and don't want one, dereference. */
16840 if (!want_address && have_address)
16842 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16843 enum dwarf_location_atom op;
16845 if (size > DWARF2_ADDR_SIZE || size == -1)
16847 expansion_failed (loc, NULL_RTX,
16848 "DWARF address size mismatch");
16849 return 0;
16851 else if (size == DWARF2_ADDR_SIZE)
16852 op = DW_OP_deref;
16853 else
16854 op = DW_OP_deref_size;
16856 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16859 return descr;
16862 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16863 if it is not possible. */
16865 static dw_loc_descr_ref
16866 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16868 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16869 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16870 else if (dwarf_version >= 3 || !dwarf_strict)
16871 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16872 else
16873 return NULL;
16876 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16877 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16879 static dw_loc_descr_ref
16880 dw_sra_loc_expr (tree decl, rtx loc)
16882 rtx p;
16883 unsigned HOST_WIDE_INT padsize = 0;
16884 dw_loc_descr_ref descr, *descr_tail;
16885 unsigned HOST_WIDE_INT decl_size;
16886 rtx varloc;
16887 enum var_init_status initialized;
16889 if (DECL_SIZE (decl) == NULL
16890 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16891 return NULL;
16893 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16894 descr = NULL;
16895 descr_tail = &descr;
16897 for (p = loc; p; p = XEXP (p, 1))
16899 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16900 rtx loc_note = *decl_piece_varloc_ptr (p);
16901 dw_loc_descr_ref cur_descr;
16902 dw_loc_descr_ref *tail, last = NULL;
16903 unsigned HOST_WIDE_INT opsize = 0;
16905 if (loc_note == NULL_RTX
16906 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16908 padsize += bitsize;
16909 continue;
16911 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16912 varloc = NOTE_VAR_LOCATION (loc_note);
16913 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16914 if (cur_descr == NULL)
16916 padsize += bitsize;
16917 continue;
16920 /* Check that cur_descr either doesn't use
16921 DW_OP_*piece operations, or their sum is equal
16922 to bitsize. Otherwise we can't embed it. */
16923 for (tail = &cur_descr; *tail != NULL;
16924 tail = &(*tail)->dw_loc_next)
16925 if ((*tail)->dw_loc_opc == DW_OP_piece)
16927 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16928 * BITS_PER_UNIT;
16929 last = *tail;
16931 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16933 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16934 last = *tail;
16937 if (last != NULL && opsize != bitsize)
16939 padsize += bitsize;
16940 /* Discard the current piece of the descriptor and release any
16941 addr_table entries it uses. */
16942 remove_loc_list_addr_table_entries (cur_descr);
16943 continue;
16946 /* If there is a hole, add DW_OP_*piece after empty DWARF
16947 expression, which means that those bits are optimized out. */
16948 if (padsize)
16950 if (padsize > decl_size)
16952 remove_loc_list_addr_table_entries (cur_descr);
16953 goto discard_descr;
16955 decl_size -= padsize;
16956 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16957 if (*descr_tail == NULL)
16959 remove_loc_list_addr_table_entries (cur_descr);
16960 goto discard_descr;
16962 descr_tail = &(*descr_tail)->dw_loc_next;
16963 padsize = 0;
16965 *descr_tail = cur_descr;
16966 descr_tail = tail;
16967 if (bitsize > decl_size)
16968 goto discard_descr;
16969 decl_size -= bitsize;
16970 if (last == NULL)
16972 HOST_WIDE_INT offset = 0;
16973 if (GET_CODE (varloc) == VAR_LOCATION
16974 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16976 varloc = PAT_VAR_LOCATION_LOC (varloc);
16977 if (GET_CODE (varloc) == EXPR_LIST)
16978 varloc = XEXP (varloc, 0);
16982 if (GET_CODE (varloc) == CONST
16983 || GET_CODE (varloc) == SIGN_EXTEND
16984 || GET_CODE (varloc) == ZERO_EXTEND)
16985 varloc = XEXP (varloc, 0);
16986 else if (GET_CODE (varloc) == SUBREG)
16987 varloc = SUBREG_REG (varloc);
16988 else
16989 break;
16991 while (1);
16992 /* DW_OP_bit_size offset should be zero for register
16993 or implicit location descriptions and empty location
16994 descriptions, but for memory addresses needs big endian
16995 adjustment. */
16996 if (MEM_P (varloc))
16998 unsigned HOST_WIDE_INT memsize;
16999 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
17000 goto discard_descr;
17001 memsize *= BITS_PER_UNIT;
17002 if (memsize != bitsize)
17004 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17005 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17006 goto discard_descr;
17007 if (memsize < bitsize)
17008 goto discard_descr;
17009 if (BITS_BIG_ENDIAN)
17010 offset = memsize - bitsize;
17014 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17015 if (*descr_tail == NULL)
17016 goto discard_descr;
17017 descr_tail = &(*descr_tail)->dw_loc_next;
17021 /* If there were any non-empty expressions, add padding till the end of
17022 the decl. */
17023 if (descr != NULL && decl_size != 0)
17025 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17026 if (*descr_tail == NULL)
17027 goto discard_descr;
17029 return descr;
17031 discard_descr:
17032 /* Discard the descriptor and release any addr_table entries it uses. */
17033 remove_loc_list_addr_table_entries (descr);
17034 return NULL;
17037 /* Return the dwarf representation of the location list LOC_LIST of
17038 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17039 function. */
17041 static dw_loc_list_ref
17042 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17044 const char *endname, *secname;
17045 var_loc_view endview;
17046 rtx varloc;
17047 enum var_init_status initialized;
17048 struct var_loc_node *node;
17049 dw_loc_descr_ref descr;
17050 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17051 dw_loc_list_ref list = NULL;
17052 dw_loc_list_ref *listp = &list;
17054 /* Now that we know what section we are using for a base,
17055 actually construct the list of locations.
17056 The first location information is what is passed to the
17057 function that creates the location list, and the remaining
17058 locations just get added on to that list.
17059 Note that we only know the start address for a location
17060 (IE location changes), so to build the range, we use
17061 the range [current location start, next location start].
17062 This means we have to special case the last node, and generate
17063 a range of [last location start, end of function label]. */
17065 if (cfun && crtl->has_bb_partition)
17067 bool save_in_cold_section_p = in_cold_section_p;
17068 in_cold_section_p = first_function_block_is_cold;
17069 if (loc_list->last_before_switch == NULL)
17070 in_cold_section_p = !in_cold_section_p;
17071 secname = secname_for_decl (decl);
17072 in_cold_section_p = save_in_cold_section_p;
17074 else
17075 secname = secname_for_decl (decl);
17077 for (node = loc_list->first; node; node = node->next)
17079 bool range_across_switch = false;
17080 if (GET_CODE (node->loc) == EXPR_LIST
17081 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17083 if (GET_CODE (node->loc) == EXPR_LIST)
17085 descr = NULL;
17086 /* This requires DW_OP_{,bit_}piece, which is not usable
17087 inside DWARF expressions. */
17088 if (want_address == 2)
17089 descr = dw_sra_loc_expr (decl, node->loc);
17091 else
17093 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17094 varloc = NOTE_VAR_LOCATION (node->loc);
17095 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17097 if (descr)
17099 /* If section switch happens in between node->label
17100 and node->next->label (or end of function) and
17101 we can't emit it as a single entry list,
17102 emit two ranges, first one ending at the end
17103 of first partition and second one starting at the
17104 beginning of second partition. */
17105 if (node == loc_list->last_before_switch
17106 && (node != loc_list->first || loc_list->first->next
17107 /* If we are to emit a view number, we will emit
17108 a loclist rather than a single location
17109 expression for the entire function (see
17110 loc_list_has_views), so we have to split the
17111 range that straddles across partitions. */
17112 || !ZERO_VIEW_P (node->view))
17113 && current_function_decl)
17115 endname = cfun->fde->dw_fde_end;
17116 endview = 0;
17117 range_across_switch = true;
17119 /* The variable has a location between NODE->LABEL and
17120 NODE->NEXT->LABEL. */
17121 else if (node->next)
17122 endname = node->next->label, endview = node->next->view;
17123 /* If the variable has a location at the last label
17124 it keeps its location until the end of function. */
17125 else if (!current_function_decl)
17126 endname = text_end_label, endview = 0;
17127 else
17129 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17130 current_function_funcdef_no);
17131 endname = ggc_strdup (label_id);
17132 endview = 0;
17135 *listp = new_loc_list (descr, node->label, node->view,
17136 endname, endview, secname);
17137 if (TREE_CODE (decl) == PARM_DECL
17138 && node == loc_list->first
17139 && NOTE_P (node->loc)
17140 && strcmp (node->label, endname) == 0)
17141 (*listp)->force = true;
17142 listp = &(*listp)->dw_loc_next;
17146 if (cfun
17147 && crtl->has_bb_partition
17148 && node == loc_list->last_before_switch)
17150 bool save_in_cold_section_p = in_cold_section_p;
17151 in_cold_section_p = !first_function_block_is_cold;
17152 secname = secname_for_decl (decl);
17153 in_cold_section_p = save_in_cold_section_p;
17156 if (range_across_switch)
17158 if (GET_CODE (node->loc) == EXPR_LIST)
17159 descr = dw_sra_loc_expr (decl, node->loc);
17160 else
17162 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17163 varloc = NOTE_VAR_LOCATION (node->loc);
17164 descr = dw_loc_list_1 (decl, varloc, want_address,
17165 initialized);
17167 gcc_assert (descr);
17168 /* The variable has a location between NODE->LABEL and
17169 NODE->NEXT->LABEL. */
17170 if (node->next)
17171 endname = node->next->label, endview = node->next->view;
17172 else
17173 endname = cfun->fde->dw_fde_second_end, endview = 0;
17174 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17175 endname, endview, secname);
17176 listp = &(*listp)->dw_loc_next;
17180 /* Try to avoid the overhead of a location list emitting a location
17181 expression instead, but only if we didn't have more than one
17182 location entry in the first place. If some entries were not
17183 representable, we don't want to pretend a single entry that was
17184 applies to the entire scope in which the variable is
17185 available. */
17186 if (list && loc_list->first->next)
17187 gen_llsym (list);
17188 else
17189 maybe_gen_llsym (list);
17191 return list;
17194 /* Return if the loc_list has only single element and thus can be represented
17195 as location description. */
17197 static bool
17198 single_element_loc_list_p (dw_loc_list_ref list)
17200 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17201 return !list->ll_symbol;
17204 /* Duplicate a single element of location list. */
17206 static inline dw_loc_descr_ref
17207 copy_loc_descr (dw_loc_descr_ref ref)
17209 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17210 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17211 return copy;
17214 /* To each location in list LIST append loc descr REF. */
17216 static void
17217 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17219 dw_loc_descr_ref copy;
17220 add_loc_descr (&list->expr, ref);
17221 list = list->dw_loc_next;
17222 while (list)
17224 copy = copy_loc_descr (ref);
17225 add_loc_descr (&list->expr, copy);
17226 while (copy->dw_loc_next)
17227 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17228 list = list->dw_loc_next;
17232 /* To each location in list LIST prepend loc descr REF. */
17234 static void
17235 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17237 dw_loc_descr_ref copy;
17238 dw_loc_descr_ref ref_end = list->expr;
17239 add_loc_descr (&ref, list->expr);
17240 list->expr = ref;
17241 list = list->dw_loc_next;
17242 while (list)
17244 dw_loc_descr_ref end = list->expr;
17245 list->expr = copy = copy_loc_descr (ref);
17246 while (copy->dw_loc_next != ref_end)
17247 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17248 copy->dw_loc_next = end;
17249 list = list->dw_loc_next;
17253 /* Given two lists RET and LIST
17254 produce location list that is result of adding expression in LIST
17255 to expression in RET on each position in program.
17256 Might be destructive on both RET and LIST.
17258 TODO: We handle only simple cases of RET or LIST having at most one
17259 element. General case would involve sorting the lists in program order
17260 and merging them that will need some additional work.
17261 Adding that will improve quality of debug info especially for SRA-ed
17262 structures. */
17264 static void
17265 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17267 if (!list)
17268 return;
17269 if (!*ret)
17271 *ret = list;
17272 return;
17274 if (!list->dw_loc_next)
17276 add_loc_descr_to_each (*ret, list->expr);
17277 return;
17279 if (!(*ret)->dw_loc_next)
17281 prepend_loc_descr_to_each (list, (*ret)->expr);
17282 *ret = list;
17283 return;
17285 expansion_failed (NULL_TREE, NULL_RTX,
17286 "Don't know how to merge two non-trivial"
17287 " location lists.\n");
17288 *ret = NULL;
17289 return;
17292 /* LOC is constant expression. Try a luck, look it up in constant
17293 pool and return its loc_descr of its address. */
17295 static dw_loc_descr_ref
17296 cst_pool_loc_descr (tree loc)
17298 /* Get an RTL for this, if something has been emitted. */
17299 rtx rtl = lookup_constant_def (loc);
17301 if (!rtl || !MEM_P (rtl))
17303 gcc_assert (!rtl);
17304 return 0;
17306 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17308 /* TODO: We might get more coverage if we was actually delaying expansion
17309 of all expressions till end of compilation when constant pools are fully
17310 populated. */
17311 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17313 expansion_failed (loc, NULL_RTX,
17314 "CST value in contant pool but not marked.");
17315 return 0;
17317 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17318 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17321 /* Return dw_loc_list representing address of addr_expr LOC
17322 by looking for inner INDIRECT_REF expression and turning
17323 it into simple arithmetics.
17325 See loc_list_from_tree for the meaning of CONTEXT. */
17327 static dw_loc_list_ref
17328 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17329 loc_descr_context *context)
17331 tree obj, offset;
17332 poly_int64 bitsize, bitpos, bytepos;
17333 machine_mode mode;
17334 int unsignedp, reversep, volatilep = 0;
17335 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17337 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17338 &bitsize, &bitpos, &offset, &mode,
17339 &unsignedp, &reversep, &volatilep);
17340 STRIP_NOPS (obj);
17341 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17343 expansion_failed (loc, NULL_RTX, "bitfield access");
17344 return 0;
17346 if (!INDIRECT_REF_P (obj))
17348 expansion_failed (obj,
17349 NULL_RTX, "no indirect ref in inner refrence");
17350 return 0;
17352 if (!offset && known_eq (bitpos, 0))
17353 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17354 context);
17355 else if (toplev
17356 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17357 && (dwarf_version >= 4 || !dwarf_strict))
17359 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17360 if (!list_ret)
17361 return 0;
17362 if (offset)
17364 /* Variable offset. */
17365 list_ret1 = loc_list_from_tree (offset, 0, context);
17366 if (list_ret1 == 0)
17367 return 0;
17368 add_loc_list (&list_ret, list_ret1);
17369 if (!list_ret)
17370 return 0;
17371 add_loc_descr_to_each (list_ret,
17372 new_loc_descr (DW_OP_plus, 0, 0));
17374 HOST_WIDE_INT value;
17375 if (bytepos.is_constant (&value) && value > 0)
17376 add_loc_descr_to_each (list_ret,
17377 new_loc_descr (DW_OP_plus_uconst, value, 0));
17378 else if (maybe_ne (bytepos, 0))
17379 loc_list_plus_const (list_ret, bytepos);
17380 add_loc_descr_to_each (list_ret,
17381 new_loc_descr (DW_OP_stack_value, 0, 0));
17383 return list_ret;
17386 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17387 all operations from LOC are nops, move to the last one. Insert in NOPS all
17388 operations that are skipped. */
17390 static void
17391 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17392 hash_set<dw_loc_descr_ref> &nops)
17394 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17396 nops.add (loc);
17397 loc = loc->dw_loc_next;
17401 /* Helper for loc_descr_without_nops: free the location description operation
17402 P. */
17404 bool
17405 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17407 ggc_free (loc);
17408 return true;
17411 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17412 finishes LOC. */
17414 static void
17415 loc_descr_without_nops (dw_loc_descr_ref &loc)
17417 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17418 return;
17420 /* Set of all DW_OP_nop operations we remove. */
17421 hash_set<dw_loc_descr_ref> nops;
17423 /* First, strip all prefix NOP operations in order to keep the head of the
17424 operations list. */
17425 loc_descr_to_next_no_nop (loc, nops);
17427 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17429 /* For control flow operations: strip "prefix" nops in destination
17430 labels. */
17431 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17432 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17433 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17434 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17436 /* Do the same for the operations that follow, then move to the next
17437 iteration. */
17438 if (cur->dw_loc_next != NULL)
17439 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17440 cur = cur->dw_loc_next;
17443 nops.traverse<void *, free_loc_descr> (NULL);
17447 struct dwarf_procedure_info;
17449 /* Helper structure for location descriptions generation. */
17450 struct loc_descr_context
17452 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17453 NULL_TREE if DW_OP_push_object_address in invalid for this location
17454 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17455 tree context_type;
17456 /* The ..._DECL node that should be translated as a
17457 DW_OP_push_object_address operation. */
17458 tree base_decl;
17459 /* Information about the DWARF procedure we are currently generating. NULL if
17460 we are not generating a DWARF procedure. */
17461 struct dwarf_procedure_info *dpi;
17462 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17463 by consumer. Used for DW_TAG_generic_subrange attributes. */
17464 bool placeholder_arg;
17465 /* True if PLACEHOLDER_EXPR has been seen. */
17466 bool placeholder_seen;
17469 /* DWARF procedures generation
17471 DWARF expressions (aka. location descriptions) are used to encode variable
17472 things such as sizes or offsets. Such computations can have redundant parts
17473 that can be factorized in order to reduce the size of the output debug
17474 information. This is the whole point of DWARF procedures.
17476 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17477 already factorized into functions ("size functions") in order to handle very
17478 big and complex types. Such functions are quite simple: they have integral
17479 arguments, they return an integral result and their body contains only a
17480 return statement with arithmetic expressions. This is the only kind of
17481 function we are interested in translating into DWARF procedures, here.
17483 DWARF expressions and DWARF procedure are executed using a stack, so we have
17484 to define some calling convention for them to interact. Let's say that:
17486 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17487 all arguments in reverse order (right-to-left) so that when the DWARF
17488 procedure execution starts, the first argument is the top of the stack.
17490 - Then, when returning, the DWARF procedure must have consumed all arguments
17491 on the stack, must have pushed the result and touched nothing else.
17493 - Each integral argument and the result are integral types can be hold in a
17494 single stack slot.
17496 - We call "frame offset" the number of stack slots that are "under DWARF
17497 procedure control": it includes the arguments slots, the temporaries and
17498 the result slot. Thus, it is equal to the number of arguments when the
17499 procedure execution starts and must be equal to one (the result) when it
17500 returns. */
17502 /* Helper structure used when generating operations for a DWARF procedure. */
17503 struct dwarf_procedure_info
17505 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17506 currently translated. */
17507 tree fndecl;
17508 /* The number of arguments FNDECL takes. */
17509 unsigned args_count;
17512 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17513 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17514 equate it to this DIE. */
17516 static dw_die_ref
17517 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17518 dw_die_ref parent_die)
17520 dw_die_ref dwarf_proc_die;
17522 if ((dwarf_version < 3 && dwarf_strict)
17523 || location == NULL)
17524 return NULL;
17526 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17527 if (fndecl)
17528 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17529 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17530 return dwarf_proc_die;
17533 /* Return whether TYPE is a supported type as a DWARF procedure argument
17534 type or return type (we handle only scalar types and pointer types that
17535 aren't wider than the DWARF expression evaluation stack. */
17537 static bool
17538 is_handled_procedure_type (tree type)
17540 return ((INTEGRAL_TYPE_P (type)
17541 || TREE_CODE (type) == OFFSET_TYPE
17542 || TREE_CODE (type) == POINTER_TYPE)
17543 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17546 /* Helper for resolve_args_picking: do the same but stop when coming across
17547 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17548 offset *before* evaluating the corresponding operation. */
17550 static bool
17551 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17552 struct dwarf_procedure_info *dpi,
17553 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17555 /* The "frame_offset" identifier is already used to name a macro... */
17556 unsigned frame_offset_ = initial_frame_offset;
17557 dw_loc_descr_ref l;
17559 for (l = loc; l != NULL;)
17561 bool existed;
17562 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17564 /* If we already met this node, there is nothing to compute anymore. */
17565 if (existed)
17567 /* Make sure that the stack size is consistent wherever the execution
17568 flow comes from. */
17569 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17570 break;
17572 l_frame_offset = frame_offset_;
17574 /* If needed, relocate the picking offset with respect to the frame
17575 offset. */
17576 if (l->frame_offset_rel)
17578 unsigned HOST_WIDE_INT off;
17579 switch (l->dw_loc_opc)
17581 case DW_OP_pick:
17582 off = l->dw_loc_oprnd1.v.val_unsigned;
17583 break;
17584 case DW_OP_dup:
17585 off = 0;
17586 break;
17587 case DW_OP_over:
17588 off = 1;
17589 break;
17590 default:
17591 gcc_unreachable ();
17593 /* frame_offset_ is the size of the current stack frame, including
17594 incoming arguments. Besides, the arguments are pushed
17595 right-to-left. Thus, in order to access the Nth argument from
17596 this operation node, the picking has to skip temporaries *plus*
17597 one stack slot per argument (0 for the first one, 1 for the second
17598 one, etc.).
17600 The targetted argument number (N) is already set as the operand,
17601 and the number of temporaries can be computed with:
17602 frame_offsets_ - dpi->args_count */
17603 off += frame_offset_ - dpi->args_count;
17605 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17606 if (off > 255)
17607 return false;
17609 if (off == 0)
17611 l->dw_loc_opc = DW_OP_dup;
17612 l->dw_loc_oprnd1.v.val_unsigned = 0;
17614 else if (off == 1)
17616 l->dw_loc_opc = DW_OP_over;
17617 l->dw_loc_oprnd1.v.val_unsigned = 0;
17619 else
17621 l->dw_loc_opc = DW_OP_pick;
17622 l->dw_loc_oprnd1.v.val_unsigned = off;
17626 /* Update frame_offset according to the effect the current operation has
17627 on the stack. */
17628 switch (l->dw_loc_opc)
17630 case DW_OP_deref:
17631 case DW_OP_swap:
17632 case DW_OP_rot:
17633 case DW_OP_abs:
17634 case DW_OP_neg:
17635 case DW_OP_not:
17636 case DW_OP_plus_uconst:
17637 case DW_OP_skip:
17638 case DW_OP_reg0:
17639 case DW_OP_reg1:
17640 case DW_OP_reg2:
17641 case DW_OP_reg3:
17642 case DW_OP_reg4:
17643 case DW_OP_reg5:
17644 case DW_OP_reg6:
17645 case DW_OP_reg7:
17646 case DW_OP_reg8:
17647 case DW_OP_reg9:
17648 case DW_OP_reg10:
17649 case DW_OP_reg11:
17650 case DW_OP_reg12:
17651 case DW_OP_reg13:
17652 case DW_OP_reg14:
17653 case DW_OP_reg15:
17654 case DW_OP_reg16:
17655 case DW_OP_reg17:
17656 case DW_OP_reg18:
17657 case DW_OP_reg19:
17658 case DW_OP_reg20:
17659 case DW_OP_reg21:
17660 case DW_OP_reg22:
17661 case DW_OP_reg23:
17662 case DW_OP_reg24:
17663 case DW_OP_reg25:
17664 case DW_OP_reg26:
17665 case DW_OP_reg27:
17666 case DW_OP_reg28:
17667 case DW_OP_reg29:
17668 case DW_OP_reg30:
17669 case DW_OP_reg31:
17670 case DW_OP_bregx:
17671 case DW_OP_piece:
17672 case DW_OP_deref_size:
17673 case DW_OP_nop:
17674 case DW_OP_bit_piece:
17675 case DW_OP_implicit_value:
17676 case DW_OP_stack_value:
17677 break;
17679 case DW_OP_addr:
17680 case DW_OP_const1u:
17681 case DW_OP_const1s:
17682 case DW_OP_const2u:
17683 case DW_OP_const2s:
17684 case DW_OP_const4u:
17685 case DW_OP_const4s:
17686 case DW_OP_const8u:
17687 case DW_OP_const8s:
17688 case DW_OP_constu:
17689 case DW_OP_consts:
17690 case DW_OP_dup:
17691 case DW_OP_over:
17692 case DW_OP_pick:
17693 case DW_OP_lit0:
17694 case DW_OP_lit1:
17695 case DW_OP_lit2:
17696 case DW_OP_lit3:
17697 case DW_OP_lit4:
17698 case DW_OP_lit5:
17699 case DW_OP_lit6:
17700 case DW_OP_lit7:
17701 case DW_OP_lit8:
17702 case DW_OP_lit9:
17703 case DW_OP_lit10:
17704 case DW_OP_lit11:
17705 case DW_OP_lit12:
17706 case DW_OP_lit13:
17707 case DW_OP_lit14:
17708 case DW_OP_lit15:
17709 case DW_OP_lit16:
17710 case DW_OP_lit17:
17711 case DW_OP_lit18:
17712 case DW_OP_lit19:
17713 case DW_OP_lit20:
17714 case DW_OP_lit21:
17715 case DW_OP_lit22:
17716 case DW_OP_lit23:
17717 case DW_OP_lit24:
17718 case DW_OP_lit25:
17719 case DW_OP_lit26:
17720 case DW_OP_lit27:
17721 case DW_OP_lit28:
17722 case DW_OP_lit29:
17723 case DW_OP_lit30:
17724 case DW_OP_lit31:
17725 case DW_OP_breg0:
17726 case DW_OP_breg1:
17727 case DW_OP_breg2:
17728 case DW_OP_breg3:
17729 case DW_OP_breg4:
17730 case DW_OP_breg5:
17731 case DW_OP_breg6:
17732 case DW_OP_breg7:
17733 case DW_OP_breg8:
17734 case DW_OP_breg9:
17735 case DW_OP_breg10:
17736 case DW_OP_breg11:
17737 case DW_OP_breg12:
17738 case DW_OP_breg13:
17739 case DW_OP_breg14:
17740 case DW_OP_breg15:
17741 case DW_OP_breg16:
17742 case DW_OP_breg17:
17743 case DW_OP_breg18:
17744 case DW_OP_breg19:
17745 case DW_OP_breg20:
17746 case DW_OP_breg21:
17747 case DW_OP_breg22:
17748 case DW_OP_breg23:
17749 case DW_OP_breg24:
17750 case DW_OP_breg25:
17751 case DW_OP_breg26:
17752 case DW_OP_breg27:
17753 case DW_OP_breg28:
17754 case DW_OP_breg29:
17755 case DW_OP_breg30:
17756 case DW_OP_breg31:
17757 case DW_OP_fbreg:
17758 case DW_OP_push_object_address:
17759 case DW_OP_call_frame_cfa:
17760 case DW_OP_GNU_variable_value:
17761 ++frame_offset_;
17762 break;
17764 case DW_OP_drop:
17765 case DW_OP_xderef:
17766 case DW_OP_and:
17767 case DW_OP_div:
17768 case DW_OP_minus:
17769 case DW_OP_mod:
17770 case DW_OP_mul:
17771 case DW_OP_or:
17772 case DW_OP_plus:
17773 case DW_OP_shl:
17774 case DW_OP_shr:
17775 case DW_OP_shra:
17776 case DW_OP_xor:
17777 case DW_OP_bra:
17778 case DW_OP_eq:
17779 case DW_OP_ge:
17780 case DW_OP_gt:
17781 case DW_OP_le:
17782 case DW_OP_lt:
17783 case DW_OP_ne:
17784 case DW_OP_regx:
17785 case DW_OP_xderef_size:
17786 --frame_offset_;
17787 break;
17789 case DW_OP_call2:
17790 case DW_OP_call4:
17791 case DW_OP_call_ref:
17793 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17794 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17796 if (stack_usage == NULL)
17797 return false;
17798 frame_offset_ += *stack_usage;
17799 break;
17802 case DW_OP_implicit_pointer:
17803 case DW_OP_entry_value:
17804 case DW_OP_const_type:
17805 case DW_OP_regval_type:
17806 case DW_OP_deref_type:
17807 case DW_OP_convert:
17808 case DW_OP_reinterpret:
17809 case DW_OP_form_tls_address:
17810 case DW_OP_GNU_push_tls_address:
17811 case DW_OP_GNU_uninit:
17812 case DW_OP_GNU_encoded_addr:
17813 case DW_OP_GNU_implicit_pointer:
17814 case DW_OP_GNU_entry_value:
17815 case DW_OP_GNU_const_type:
17816 case DW_OP_GNU_regval_type:
17817 case DW_OP_GNU_deref_type:
17818 case DW_OP_GNU_convert:
17819 case DW_OP_GNU_reinterpret:
17820 case DW_OP_GNU_parameter_ref:
17821 /* loc_list_from_tree will probably not output these operations for
17822 size functions, so assume they will not appear here. */
17823 /* Fall through... */
17825 default:
17826 gcc_unreachable ();
17829 /* Now, follow the control flow (except subroutine calls). */
17830 switch (l->dw_loc_opc)
17832 case DW_OP_bra:
17833 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17834 frame_offsets))
17835 return false;
17836 /* Fall through. */
17838 case DW_OP_skip:
17839 l = l->dw_loc_oprnd1.v.val_loc;
17840 break;
17842 case DW_OP_stack_value:
17843 return true;
17845 default:
17846 l = l->dw_loc_next;
17847 break;
17851 return true;
17854 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17855 operations) in order to resolve the operand of DW_OP_pick operations that
17856 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17857 offset *before* LOC is executed. Return if all relocations were
17858 successful. */
17860 static bool
17861 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17862 struct dwarf_procedure_info *dpi)
17864 /* Associate to all visited operations the frame offset *before* evaluating
17865 this operation. */
17866 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17868 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17869 frame_offsets);
17872 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17873 Return NULL if it is not possible. */
17875 static dw_die_ref
17876 function_to_dwarf_procedure (tree fndecl)
17878 struct loc_descr_context ctx;
17879 struct dwarf_procedure_info dpi;
17880 dw_die_ref dwarf_proc_die;
17881 tree tree_body = DECL_SAVED_TREE (fndecl);
17882 dw_loc_descr_ref loc_body, epilogue;
17884 tree cursor;
17885 unsigned i;
17887 /* Do not generate multiple DWARF procedures for the same function
17888 declaration. */
17889 dwarf_proc_die = lookup_decl_die (fndecl);
17890 if (dwarf_proc_die != NULL)
17891 return dwarf_proc_die;
17893 /* DWARF procedures are available starting with the DWARFv3 standard. */
17894 if (dwarf_version < 3 && dwarf_strict)
17895 return NULL;
17897 /* We handle only functions for which we still have a body, that return a
17898 supported type and that takes arguments with supported types. Note that
17899 there is no point translating functions that return nothing. */
17900 if (tree_body == NULL_TREE
17901 || DECL_RESULT (fndecl) == NULL_TREE
17902 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17903 return NULL;
17905 for (cursor = DECL_ARGUMENTS (fndecl);
17906 cursor != NULL_TREE;
17907 cursor = TREE_CHAIN (cursor))
17908 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17909 return NULL;
17911 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17912 if (TREE_CODE (tree_body) != RETURN_EXPR)
17913 return NULL;
17914 tree_body = TREE_OPERAND (tree_body, 0);
17915 if (TREE_CODE (tree_body) != MODIFY_EXPR
17916 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17917 return NULL;
17918 tree_body = TREE_OPERAND (tree_body, 1);
17920 /* Try to translate the body expression itself. Note that this will probably
17921 cause an infinite recursion if its call graph has a cycle. This is very
17922 unlikely for size functions, however, so don't bother with such things at
17923 the moment. */
17924 ctx.context_type = NULL_TREE;
17925 ctx.base_decl = NULL_TREE;
17926 ctx.dpi = &dpi;
17927 ctx.placeholder_arg = false;
17928 ctx.placeholder_seen = false;
17929 dpi.fndecl = fndecl;
17930 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17931 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17932 if (!loc_body)
17933 return NULL;
17935 /* After evaluating all operands in "loc_body", we should still have on the
17936 stack all arguments plus the desired function result (top of the stack).
17937 Generate code in order to keep only the result in our stack frame. */
17938 epilogue = NULL;
17939 for (i = 0; i < dpi.args_count; ++i)
17941 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17942 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17943 op_couple->dw_loc_next->dw_loc_next = epilogue;
17944 epilogue = op_couple;
17946 add_loc_descr (&loc_body, epilogue);
17947 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17948 return NULL;
17950 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17951 because they are considered useful. Now there is an epilogue, they are
17952 not anymore, so give it another try. */
17953 loc_descr_without_nops (loc_body);
17955 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17956 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17957 though, given that size functions do not come from source, so they should
17958 not have a dedicated DW_TAG_subprogram DIE. */
17959 dwarf_proc_die
17960 = new_dwarf_proc_die (loc_body, fndecl,
17961 get_context_die (DECL_CONTEXT (fndecl)));
17963 /* The called DWARF procedure consumes one stack slot per argument and
17964 returns one stack slot. */
17965 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17967 return dwarf_proc_die;
17971 /* Generate Dwarf location list representing LOC.
17972 If WANT_ADDRESS is false, expression computing LOC will be computed
17973 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17974 if WANT_ADDRESS is 2, expression computing address useable in location
17975 will be returned (i.e. DW_OP_reg can be used
17976 to refer to register values).
17978 CONTEXT provides information to customize the location descriptions
17979 generation. Its context_type field specifies what type is implicitly
17980 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17981 will not be generated.
17983 Its DPI field determines whether we are generating a DWARF expression for a
17984 DWARF procedure, so PARM_DECL references are processed specifically.
17986 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17987 and dpi fields were null. */
17989 static dw_loc_list_ref
17990 loc_list_from_tree_1 (tree loc, int want_address,
17991 struct loc_descr_context *context)
17993 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17994 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17995 int have_address = 0;
17996 enum dwarf_location_atom op;
17998 /* ??? Most of the time we do not take proper care for sign/zero
17999 extending the values properly. Hopefully this won't be a real
18000 problem... */
18002 if (context != NULL
18003 && context->base_decl == loc
18004 && want_address == 0)
18006 if (dwarf_version >= 3 || !dwarf_strict)
18007 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18008 NULL, 0, NULL, 0, NULL);
18009 else
18010 return NULL;
18013 switch (TREE_CODE (loc))
18015 case ERROR_MARK:
18016 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18017 return 0;
18019 case PLACEHOLDER_EXPR:
18020 /* This case involves extracting fields from an object to determine the
18021 position of other fields. It is supposed to appear only as the first
18022 operand of COMPONENT_REF nodes and to reference precisely the type
18023 that the context allows. */
18024 if (context != NULL
18025 && TREE_TYPE (loc) == context->context_type
18026 && want_address >= 1)
18028 if (dwarf_version >= 3 || !dwarf_strict)
18030 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18031 have_address = 1;
18032 break;
18034 else
18035 return NULL;
18037 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18038 the single argument passed by consumer. */
18039 else if (context != NULL
18040 && context->placeholder_arg
18041 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18042 && want_address == 0)
18044 ret = new_loc_descr (DW_OP_pick, 0, 0);
18045 ret->frame_offset_rel = 1;
18046 context->placeholder_seen = true;
18047 break;
18049 else
18050 expansion_failed (loc, NULL_RTX,
18051 "PLACEHOLDER_EXPR for an unexpected type");
18052 break;
18054 case CALL_EXPR:
18056 const int nargs = call_expr_nargs (loc);
18057 tree callee = get_callee_fndecl (loc);
18058 int i;
18059 dw_die_ref dwarf_proc;
18061 if (callee == NULL_TREE)
18062 goto call_expansion_failed;
18064 /* We handle only functions that return an integer. */
18065 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18066 goto call_expansion_failed;
18068 dwarf_proc = function_to_dwarf_procedure (callee);
18069 if (dwarf_proc == NULL)
18070 goto call_expansion_failed;
18072 /* Evaluate arguments right-to-left so that the first argument will
18073 be the top-most one on the stack. */
18074 for (i = nargs - 1; i >= 0; --i)
18076 dw_loc_descr_ref loc_descr
18077 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18078 context);
18080 if (loc_descr == NULL)
18081 goto call_expansion_failed;
18083 add_loc_descr (&ret, loc_descr);
18086 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18087 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18088 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18089 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18090 add_loc_descr (&ret, ret1);
18091 break;
18093 call_expansion_failed:
18094 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18095 /* There are no opcodes for these operations. */
18096 return 0;
18099 case PREINCREMENT_EXPR:
18100 case PREDECREMENT_EXPR:
18101 case POSTINCREMENT_EXPR:
18102 case POSTDECREMENT_EXPR:
18103 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18104 /* There are no opcodes for these operations. */
18105 return 0;
18107 case ADDR_EXPR:
18108 /* If we already want an address, see if there is INDIRECT_REF inside
18109 e.g. for &this->field. */
18110 if (want_address)
18112 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18113 (loc, want_address == 2, context);
18114 if (list_ret)
18115 have_address = 1;
18116 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18117 && (ret = cst_pool_loc_descr (loc)))
18118 have_address = 1;
18120 /* Otherwise, process the argument and look for the address. */
18121 if (!list_ret && !ret)
18122 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18123 else
18125 if (want_address)
18126 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18127 return NULL;
18129 break;
18131 case VAR_DECL:
18132 if (DECL_THREAD_LOCAL_P (loc))
18134 rtx rtl;
18135 enum dwarf_location_atom tls_op;
18136 enum dtprel_bool dtprel = dtprel_false;
18138 if (targetm.have_tls)
18140 /* If this is not defined, we have no way to emit the
18141 data. */
18142 if (!targetm.asm_out.output_dwarf_dtprel)
18143 return 0;
18145 /* The way DW_OP_GNU_push_tls_address is specified, we
18146 can only look up addresses of objects in the current
18147 module. We used DW_OP_addr as first op, but that's
18148 wrong, because DW_OP_addr is relocated by the debug
18149 info consumer, while DW_OP_GNU_push_tls_address
18150 operand shouldn't be. */
18151 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18152 return 0;
18153 dtprel = dtprel_true;
18154 /* We check for DWARF 5 here because gdb did not implement
18155 DW_OP_form_tls_address until after 7.12. */
18156 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18157 : DW_OP_GNU_push_tls_address);
18159 else
18161 if (!targetm.emutls.debug_form_tls_address
18162 || !(dwarf_version >= 3 || !dwarf_strict))
18163 return 0;
18164 /* We stuffed the control variable into the DECL_VALUE_EXPR
18165 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18166 no longer appear in gimple code. We used the control
18167 variable in specific so that we could pick it up here. */
18168 loc = DECL_VALUE_EXPR (loc);
18169 tls_op = DW_OP_form_tls_address;
18172 rtl = rtl_for_decl_location (loc);
18173 if (rtl == NULL_RTX)
18174 return 0;
18176 if (!MEM_P (rtl))
18177 return 0;
18178 rtl = XEXP (rtl, 0);
18179 if (! CONSTANT_P (rtl))
18180 return 0;
18182 ret = new_addr_loc_descr (rtl, dtprel);
18183 ret1 = new_loc_descr (tls_op, 0, 0);
18184 add_loc_descr (&ret, ret1);
18186 have_address = 1;
18187 break;
18189 /* FALLTHRU */
18191 case PARM_DECL:
18192 if (context != NULL && context->dpi != NULL
18193 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18195 /* We are generating code for a DWARF procedure and we want to access
18196 one of its arguments: find the appropriate argument offset and let
18197 the resolve_args_picking pass compute the offset that complies
18198 with the stack frame size. */
18199 unsigned i = 0;
18200 tree cursor;
18202 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18203 cursor != NULL_TREE && cursor != loc;
18204 cursor = TREE_CHAIN (cursor), ++i)
18206 /* If we are translating a DWARF procedure, all referenced parameters
18207 must belong to the current function. */
18208 gcc_assert (cursor != NULL_TREE);
18210 ret = new_loc_descr (DW_OP_pick, i, 0);
18211 ret->frame_offset_rel = 1;
18212 break;
18214 /* FALLTHRU */
18216 case RESULT_DECL:
18217 if (DECL_HAS_VALUE_EXPR_P (loc))
18218 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18219 want_address, context);
18220 /* FALLTHRU */
18222 case FUNCTION_DECL:
18224 rtx rtl;
18225 var_loc_list *loc_list = lookup_decl_loc (loc);
18227 if (loc_list && loc_list->first)
18229 list_ret = dw_loc_list (loc_list, loc, want_address);
18230 have_address = want_address != 0;
18231 break;
18233 rtl = rtl_for_decl_location (loc);
18234 if (rtl == NULL_RTX)
18236 if (TREE_CODE (loc) != FUNCTION_DECL
18237 && early_dwarf
18238 && current_function_decl
18239 && want_address != 1
18240 && ! DECL_IGNORED_P (loc)
18241 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18242 || POINTER_TYPE_P (TREE_TYPE (loc)))
18243 && DECL_CONTEXT (loc) == current_function_decl
18244 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18245 <= DWARF2_ADDR_SIZE))
18247 dw_die_ref ref = lookup_decl_die (loc);
18248 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18249 if (ref)
18251 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18252 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18253 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18255 else
18257 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18258 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18260 break;
18262 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18263 return 0;
18265 else if (CONST_INT_P (rtl))
18267 HOST_WIDE_INT val = INTVAL (rtl);
18268 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18269 val &= GET_MODE_MASK (DECL_MODE (loc));
18270 ret = int_loc_descriptor (val);
18272 else if (GET_CODE (rtl) == CONST_STRING)
18274 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18275 return 0;
18277 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18278 ret = new_addr_loc_descr (rtl, dtprel_false);
18279 else
18281 machine_mode mode, mem_mode;
18283 /* Certain constructs can only be represented at top-level. */
18284 if (want_address == 2)
18286 ret = loc_descriptor (rtl, VOIDmode,
18287 VAR_INIT_STATUS_INITIALIZED);
18288 have_address = 1;
18290 else
18292 mode = GET_MODE (rtl);
18293 mem_mode = VOIDmode;
18294 if (MEM_P (rtl))
18296 mem_mode = mode;
18297 mode = get_address_mode (rtl);
18298 rtl = XEXP (rtl, 0);
18299 have_address = 1;
18301 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18302 VAR_INIT_STATUS_INITIALIZED);
18304 if (!ret)
18305 expansion_failed (loc, rtl,
18306 "failed to produce loc descriptor for rtl");
18309 break;
18311 case MEM_REF:
18312 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18314 have_address = 1;
18315 goto do_plus;
18317 /* Fallthru. */
18318 case INDIRECT_REF:
18319 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18320 have_address = 1;
18321 break;
18323 case TARGET_MEM_REF:
18324 case SSA_NAME:
18325 case DEBUG_EXPR_DECL:
18326 return NULL;
18328 case COMPOUND_EXPR:
18329 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18330 context);
18332 CASE_CONVERT:
18333 case VIEW_CONVERT_EXPR:
18334 case SAVE_EXPR:
18335 case MODIFY_EXPR:
18336 case NON_LVALUE_EXPR:
18337 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18338 context);
18340 case COMPONENT_REF:
18341 case BIT_FIELD_REF:
18342 case ARRAY_REF:
18343 case ARRAY_RANGE_REF:
18344 case REALPART_EXPR:
18345 case IMAGPART_EXPR:
18347 tree obj, offset;
18348 poly_int64 bitsize, bitpos, bytepos;
18349 machine_mode mode;
18350 int unsignedp, reversep, volatilep = 0;
18352 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18353 &unsignedp, &reversep, &volatilep);
18355 gcc_assert (obj != loc);
18357 list_ret = loc_list_from_tree_1 (obj,
18358 want_address == 2
18359 && known_eq (bitpos, 0)
18360 && !offset ? 2 : 1,
18361 context);
18362 /* TODO: We can extract value of the small expression via shifting even
18363 for nonzero bitpos. */
18364 if (list_ret == 0)
18365 return 0;
18366 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18367 || !multiple_p (bitsize, BITS_PER_UNIT))
18369 expansion_failed (loc, NULL_RTX,
18370 "bitfield access");
18371 return 0;
18374 if (offset != NULL_TREE)
18376 /* Variable offset. */
18377 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18378 if (list_ret1 == 0)
18379 return 0;
18380 add_loc_list (&list_ret, list_ret1);
18381 if (!list_ret)
18382 return 0;
18383 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18386 HOST_WIDE_INT value;
18387 if (bytepos.is_constant (&value) && value > 0)
18388 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18389 value, 0));
18390 else if (maybe_ne (bytepos, 0))
18391 loc_list_plus_const (list_ret, bytepos);
18393 have_address = 1;
18394 break;
18397 case INTEGER_CST:
18398 if ((want_address || !tree_fits_shwi_p (loc))
18399 && (ret = cst_pool_loc_descr (loc)))
18400 have_address = 1;
18401 else if (want_address == 2
18402 && tree_fits_shwi_p (loc)
18403 && (ret = address_of_int_loc_descriptor
18404 (int_size_in_bytes (TREE_TYPE (loc)),
18405 tree_to_shwi (loc))))
18406 have_address = 1;
18407 else if (tree_fits_shwi_p (loc))
18408 ret = int_loc_descriptor (tree_to_shwi (loc));
18409 else if (tree_fits_uhwi_p (loc))
18410 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18411 else
18413 expansion_failed (loc, NULL_RTX,
18414 "Integer operand is not host integer");
18415 return 0;
18417 break;
18419 case CONSTRUCTOR:
18420 case REAL_CST:
18421 case STRING_CST:
18422 case COMPLEX_CST:
18423 if ((ret = cst_pool_loc_descr (loc)))
18424 have_address = 1;
18425 else if (TREE_CODE (loc) == CONSTRUCTOR)
18427 tree type = TREE_TYPE (loc);
18428 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18429 unsigned HOST_WIDE_INT offset = 0;
18430 unsigned HOST_WIDE_INT cnt;
18431 constructor_elt *ce;
18433 if (TREE_CODE (type) == RECORD_TYPE)
18435 /* This is very limited, but it's enough to output
18436 pointers to member functions, as long as the
18437 referenced function is defined in the current
18438 translation unit. */
18439 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18441 tree val = ce->value;
18443 tree field = ce->index;
18445 if (val)
18446 STRIP_NOPS (val);
18448 if (!field || DECL_BIT_FIELD (field))
18450 expansion_failed (loc, NULL_RTX,
18451 "bitfield in record type constructor");
18452 size = offset = (unsigned HOST_WIDE_INT)-1;
18453 ret = NULL;
18454 break;
18457 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18458 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18459 gcc_assert (pos + fieldsize <= size);
18460 if (pos < offset)
18462 expansion_failed (loc, NULL_RTX,
18463 "out-of-order fields in record constructor");
18464 size = offset = (unsigned HOST_WIDE_INT)-1;
18465 ret = NULL;
18466 break;
18468 if (pos > offset)
18470 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18471 add_loc_descr (&ret, ret1);
18472 offset = pos;
18474 if (val && fieldsize != 0)
18476 ret1 = loc_descriptor_from_tree (val, want_address, context);
18477 if (!ret1)
18479 expansion_failed (loc, NULL_RTX,
18480 "unsupported expression in field");
18481 size = offset = (unsigned HOST_WIDE_INT)-1;
18482 ret = NULL;
18483 break;
18485 add_loc_descr (&ret, ret1);
18487 if (fieldsize)
18489 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18490 add_loc_descr (&ret, ret1);
18491 offset = pos + fieldsize;
18495 if (offset != size)
18497 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18498 add_loc_descr (&ret, ret1);
18499 offset = size;
18502 have_address = !!want_address;
18504 else
18505 expansion_failed (loc, NULL_RTX,
18506 "constructor of non-record type");
18508 else
18509 /* We can construct small constants here using int_loc_descriptor. */
18510 expansion_failed (loc, NULL_RTX,
18511 "constructor or constant not in constant pool");
18512 break;
18514 case TRUTH_AND_EXPR:
18515 case TRUTH_ANDIF_EXPR:
18516 case BIT_AND_EXPR:
18517 op = DW_OP_and;
18518 goto do_binop;
18520 case TRUTH_XOR_EXPR:
18521 case BIT_XOR_EXPR:
18522 op = DW_OP_xor;
18523 goto do_binop;
18525 case TRUTH_OR_EXPR:
18526 case TRUTH_ORIF_EXPR:
18527 case BIT_IOR_EXPR:
18528 op = DW_OP_or;
18529 goto do_binop;
18531 case FLOOR_DIV_EXPR:
18532 case CEIL_DIV_EXPR:
18533 case ROUND_DIV_EXPR:
18534 case TRUNC_DIV_EXPR:
18535 case EXACT_DIV_EXPR:
18536 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18537 return 0;
18538 op = DW_OP_div;
18539 goto do_binop;
18541 case MINUS_EXPR:
18542 op = DW_OP_minus;
18543 goto do_binop;
18545 case FLOOR_MOD_EXPR:
18546 case CEIL_MOD_EXPR:
18547 case ROUND_MOD_EXPR:
18548 case TRUNC_MOD_EXPR:
18549 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18551 op = DW_OP_mod;
18552 goto do_binop;
18554 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18555 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18556 if (list_ret == 0 || list_ret1 == 0)
18557 return 0;
18559 add_loc_list (&list_ret, list_ret1);
18560 if (list_ret == 0)
18561 return 0;
18562 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18563 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18564 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18565 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18566 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18567 break;
18569 case MULT_EXPR:
18570 op = DW_OP_mul;
18571 goto do_binop;
18573 case LSHIFT_EXPR:
18574 op = DW_OP_shl;
18575 goto do_binop;
18577 case RSHIFT_EXPR:
18578 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18579 goto do_binop;
18581 case POINTER_PLUS_EXPR:
18582 case PLUS_EXPR:
18583 do_plus:
18584 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18586 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18587 smarter to encode their opposite. The DW_OP_plus_uconst operation
18588 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18589 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18590 bytes, Y being the size of the operation that pushes the opposite
18591 of the addend. So let's choose the smallest representation. */
18592 const tree tree_addend = TREE_OPERAND (loc, 1);
18593 offset_int wi_addend;
18594 HOST_WIDE_INT shwi_addend;
18595 dw_loc_descr_ref loc_naddend;
18597 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18598 if (list_ret == 0)
18599 return 0;
18601 /* Try to get the literal to push. It is the opposite of the addend,
18602 so as we rely on wrapping during DWARF evaluation, first decode
18603 the literal as a "DWARF-sized" signed number. */
18604 wi_addend = wi::to_offset (tree_addend);
18605 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18606 shwi_addend = wi_addend.to_shwi ();
18607 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18608 ? int_loc_descriptor (-shwi_addend)
18609 : NULL;
18611 if (loc_naddend != NULL
18612 && ((unsigned) size_of_uleb128 (shwi_addend)
18613 > size_of_loc_descr (loc_naddend)))
18615 add_loc_descr_to_each (list_ret, loc_naddend);
18616 add_loc_descr_to_each (list_ret,
18617 new_loc_descr (DW_OP_minus, 0, 0));
18619 else
18621 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18623 loc_naddend = loc_cur;
18624 loc_cur = loc_cur->dw_loc_next;
18625 ggc_free (loc_naddend);
18627 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18629 break;
18632 op = DW_OP_plus;
18633 goto do_binop;
18635 case LE_EXPR:
18636 op = DW_OP_le;
18637 goto do_comp_binop;
18639 case GE_EXPR:
18640 op = DW_OP_ge;
18641 goto do_comp_binop;
18643 case LT_EXPR:
18644 op = DW_OP_lt;
18645 goto do_comp_binop;
18647 case GT_EXPR:
18648 op = DW_OP_gt;
18649 goto do_comp_binop;
18651 do_comp_binop:
18652 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18654 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18655 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18656 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18657 TREE_CODE (loc));
18658 break;
18660 else
18661 goto do_binop;
18663 case EQ_EXPR:
18664 op = DW_OP_eq;
18665 goto do_binop;
18667 case NE_EXPR:
18668 op = DW_OP_ne;
18669 goto do_binop;
18671 do_binop:
18672 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18673 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18674 if (list_ret == 0 || list_ret1 == 0)
18675 return 0;
18677 add_loc_list (&list_ret, list_ret1);
18678 if (list_ret == 0)
18679 return 0;
18680 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18681 break;
18683 case TRUTH_NOT_EXPR:
18684 case BIT_NOT_EXPR:
18685 op = DW_OP_not;
18686 goto do_unop;
18688 case ABS_EXPR:
18689 op = DW_OP_abs;
18690 goto do_unop;
18692 case NEGATE_EXPR:
18693 op = DW_OP_neg;
18694 goto do_unop;
18696 do_unop:
18697 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18698 if (list_ret == 0)
18699 return 0;
18701 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18702 break;
18704 case MIN_EXPR:
18705 case MAX_EXPR:
18707 const enum tree_code code =
18708 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18710 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18711 build2 (code, integer_type_node,
18712 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18713 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18716 /* fall through */
18718 case COND_EXPR:
18720 dw_loc_descr_ref lhs
18721 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18722 dw_loc_list_ref rhs
18723 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18724 dw_loc_descr_ref bra_node, jump_node, tmp;
18726 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18727 if (list_ret == 0 || lhs == 0 || rhs == 0)
18728 return 0;
18730 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18731 add_loc_descr_to_each (list_ret, bra_node);
18733 add_loc_list (&list_ret, rhs);
18734 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18735 add_loc_descr_to_each (list_ret, jump_node);
18737 add_loc_descr_to_each (list_ret, lhs);
18738 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18739 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18741 /* ??? Need a node to point the skip at. Use a nop. */
18742 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18743 add_loc_descr_to_each (list_ret, tmp);
18744 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18745 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18747 break;
18749 case FIX_TRUNC_EXPR:
18750 return 0;
18752 default:
18753 /* Leave front-end specific codes as simply unknown. This comes
18754 up, for instance, with the C STMT_EXPR. */
18755 if ((unsigned int) TREE_CODE (loc)
18756 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18758 expansion_failed (loc, NULL_RTX,
18759 "language specific tree node");
18760 return 0;
18763 /* Otherwise this is a generic code; we should just lists all of
18764 these explicitly. We forgot one. */
18765 if (flag_checking)
18766 gcc_unreachable ();
18768 /* In a release build, we want to degrade gracefully: better to
18769 generate incomplete debugging information than to crash. */
18770 return NULL;
18773 if (!ret && !list_ret)
18774 return 0;
18776 if (want_address == 2 && !have_address
18777 && (dwarf_version >= 4 || !dwarf_strict))
18779 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18781 expansion_failed (loc, NULL_RTX,
18782 "DWARF address size mismatch");
18783 return 0;
18785 if (ret)
18786 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18787 else
18788 add_loc_descr_to_each (list_ret,
18789 new_loc_descr (DW_OP_stack_value, 0, 0));
18790 have_address = 1;
18792 /* Show if we can't fill the request for an address. */
18793 if (want_address && !have_address)
18795 expansion_failed (loc, NULL_RTX,
18796 "Want address and only have value");
18797 return 0;
18800 gcc_assert (!ret || !list_ret);
18802 /* If we've got an address and don't want one, dereference. */
18803 if (!want_address && have_address)
18805 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18807 if (size > DWARF2_ADDR_SIZE || size == -1)
18809 expansion_failed (loc, NULL_RTX,
18810 "DWARF address size mismatch");
18811 return 0;
18813 else if (size == DWARF2_ADDR_SIZE)
18814 op = DW_OP_deref;
18815 else
18816 op = DW_OP_deref_size;
18818 if (ret)
18819 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18820 else
18821 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18823 if (ret)
18824 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18826 return list_ret;
18829 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18830 expressions. */
18832 static dw_loc_list_ref
18833 loc_list_from_tree (tree loc, int want_address,
18834 struct loc_descr_context *context)
18836 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18838 for (dw_loc_list_ref loc_cur = result;
18839 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18840 loc_descr_without_nops (loc_cur->expr);
18841 return result;
18844 /* Same as above but return only single location expression. */
18845 static dw_loc_descr_ref
18846 loc_descriptor_from_tree (tree loc, int want_address,
18847 struct loc_descr_context *context)
18849 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18850 if (!ret)
18851 return NULL;
18852 if (ret->dw_loc_next)
18854 expansion_failed (loc, NULL_RTX,
18855 "Location list where only loc descriptor needed");
18856 return NULL;
18858 return ret->expr;
18861 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18862 pointer to the declared type for the relevant field variable, or return
18863 `integer_type_node' if the given node turns out to be an
18864 ERROR_MARK node. */
18866 static inline tree
18867 field_type (const_tree decl)
18869 tree type;
18871 if (TREE_CODE (decl) == ERROR_MARK)
18872 return integer_type_node;
18874 type = DECL_BIT_FIELD_TYPE (decl);
18875 if (type == NULL_TREE)
18876 type = TREE_TYPE (decl);
18878 return type;
18881 /* Given a pointer to a tree node, return the alignment in bits for
18882 it, or else return BITS_PER_WORD if the node actually turns out to
18883 be an ERROR_MARK node. */
18885 static inline unsigned
18886 simple_type_align_in_bits (const_tree type)
18888 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18891 static inline unsigned
18892 simple_decl_align_in_bits (const_tree decl)
18894 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18897 /* Return the result of rounding T up to ALIGN. */
18899 static inline offset_int
18900 round_up_to_align (const offset_int &t, unsigned int align)
18902 return wi::udiv_trunc (t + align - 1, align) * align;
18905 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18906 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18907 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18908 if we fail to return the size in one of these two forms. */
18910 static dw_loc_descr_ref
18911 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18913 tree tree_size;
18914 struct loc_descr_context ctx;
18916 /* Return a constant integer in priority, if possible. */
18917 *cst_size = int_size_in_bytes (type);
18918 if (*cst_size != -1)
18919 return NULL;
18921 ctx.context_type = const_cast<tree> (type);
18922 ctx.base_decl = NULL_TREE;
18923 ctx.dpi = NULL;
18924 ctx.placeholder_arg = false;
18925 ctx.placeholder_seen = false;
18927 type = TYPE_MAIN_VARIANT (type);
18928 tree_size = TYPE_SIZE_UNIT (type);
18929 return ((tree_size != NULL_TREE)
18930 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18931 : NULL);
18934 /* Helper structure for RECORD_TYPE processing. */
18935 struct vlr_context
18937 /* Root RECORD_TYPE. It is needed to generate data member location
18938 descriptions in variable-length records (VLR), but also to cope with
18939 variants, which are composed of nested structures multiplexed with
18940 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18941 function processing a FIELD_DECL, it is required to be non null. */
18942 tree struct_type;
18943 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18944 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18945 this variant part as part of the root record (in storage units). For
18946 regular records, it must be NULL_TREE. */
18947 tree variant_part_offset;
18950 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18951 addressed byte of the "containing object" for the given FIELD_DECL. If
18952 possible, return a native constant through CST_OFFSET (in which case NULL is
18953 returned); otherwise return a DWARF expression that computes the offset.
18955 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18956 that offset is, either because the argument turns out to be a pointer to an
18957 ERROR_MARK node, or because the offset expression is too complex for us.
18959 CTX is required: see the comment for VLR_CONTEXT. */
18961 static dw_loc_descr_ref
18962 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18963 HOST_WIDE_INT *cst_offset)
18965 tree tree_result;
18966 dw_loc_list_ref loc_result;
18968 *cst_offset = 0;
18970 if (TREE_CODE (decl) == ERROR_MARK)
18971 return NULL;
18972 else
18973 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18975 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18976 case. */
18977 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18978 return NULL;
18980 #ifdef PCC_BITFIELD_TYPE_MATTERS
18981 /* We used to handle only constant offsets in all cases. Now, we handle
18982 properly dynamic byte offsets only when PCC bitfield type doesn't
18983 matter. */
18984 if (PCC_BITFIELD_TYPE_MATTERS
18985 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18987 offset_int object_offset_in_bits;
18988 offset_int object_offset_in_bytes;
18989 offset_int bitpos_int;
18990 tree type;
18991 tree field_size_tree;
18992 offset_int deepest_bitpos;
18993 offset_int field_size_in_bits;
18994 unsigned int type_align_in_bits;
18995 unsigned int decl_align_in_bits;
18996 offset_int type_size_in_bits;
18998 bitpos_int = wi::to_offset (bit_position (decl));
18999 type = field_type (decl);
19000 type_size_in_bits = offset_int_type_size_in_bits (type);
19001 type_align_in_bits = simple_type_align_in_bits (type);
19003 field_size_tree = DECL_SIZE (decl);
19005 /* The size could be unspecified if there was an error, or for
19006 a flexible array member. */
19007 if (!field_size_tree)
19008 field_size_tree = bitsize_zero_node;
19010 /* If the size of the field is not constant, use the type size. */
19011 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19012 field_size_in_bits = wi::to_offset (field_size_tree);
19013 else
19014 field_size_in_bits = type_size_in_bits;
19016 decl_align_in_bits = simple_decl_align_in_bits (decl);
19018 /* The GCC front-end doesn't make any attempt to keep track of the
19019 starting bit offset (relative to the start of the containing
19020 structure type) of the hypothetical "containing object" for a
19021 bit-field. Thus, when computing the byte offset value for the
19022 start of the "containing object" of a bit-field, we must deduce
19023 this information on our own. This can be rather tricky to do in
19024 some cases. For example, handling the following structure type
19025 definition when compiling for an i386/i486 target (which only
19026 aligns long long's to 32-bit boundaries) can be very tricky:
19028 struct S { int field1; long long field2:31; };
19030 Fortunately, there is a simple rule-of-thumb which can be used
19031 in such cases. When compiling for an i386/i486, GCC will
19032 allocate 8 bytes for the structure shown above. It decides to
19033 do this based upon one simple rule for bit-field allocation.
19034 GCC allocates each "containing object" for each bit-field at
19035 the first (i.e. lowest addressed) legitimate alignment boundary
19036 (based upon the required minimum alignment for the declared
19037 type of the field) which it can possibly use, subject to the
19038 condition that there is still enough available space remaining
19039 in the containing object (when allocated at the selected point)
19040 to fully accommodate all of the bits of the bit-field itself.
19042 This simple rule makes it obvious why GCC allocates 8 bytes for
19043 each object of the structure type shown above. When looking
19044 for a place to allocate the "containing object" for `field2',
19045 the compiler simply tries to allocate a 64-bit "containing
19046 object" at each successive 32-bit boundary (starting at zero)
19047 until it finds a place to allocate that 64- bit field such that
19048 at least 31 contiguous (and previously unallocated) bits remain
19049 within that selected 64 bit field. (As it turns out, for the
19050 example above, the compiler finds it is OK to allocate the
19051 "containing object" 64-bit field at bit-offset zero within the
19052 structure type.)
19054 Here we attempt to work backwards from the limited set of facts
19055 we're given, and we try to deduce from those facts, where GCC
19056 must have believed that the containing object started (within
19057 the structure type). The value we deduce is then used (by the
19058 callers of this routine) to generate DW_AT_location and
19059 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19060 the case of DW_AT_location, regular fields as well). */
19062 /* Figure out the bit-distance from the start of the structure to
19063 the "deepest" bit of the bit-field. */
19064 deepest_bitpos = bitpos_int + field_size_in_bits;
19066 /* This is the tricky part. Use some fancy footwork to deduce
19067 where the lowest addressed bit of the containing object must
19068 be. */
19069 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19071 /* Round up to type_align by default. This works best for
19072 bitfields. */
19073 object_offset_in_bits
19074 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19076 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19078 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19080 /* Round up to decl_align instead. */
19081 object_offset_in_bits
19082 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19085 object_offset_in_bytes
19086 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19087 if (ctx->variant_part_offset == NULL_TREE)
19089 *cst_offset = object_offset_in_bytes.to_shwi ();
19090 return NULL;
19092 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19094 else
19095 #endif /* PCC_BITFIELD_TYPE_MATTERS */
19096 tree_result = byte_position (decl);
19098 if (ctx->variant_part_offset != NULL_TREE)
19099 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19100 ctx->variant_part_offset, tree_result);
19102 /* If the byte offset is a constant, it's simplier to handle a native
19103 constant rather than a DWARF expression. */
19104 if (TREE_CODE (tree_result) == INTEGER_CST)
19106 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19107 return NULL;
19109 struct loc_descr_context loc_ctx = {
19110 ctx->struct_type, /* context_type */
19111 NULL_TREE, /* base_decl */
19112 NULL, /* dpi */
19113 false, /* placeholder_arg */
19114 false /* placeholder_seen */
19116 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19118 /* We want a DWARF expression: abort if we only have a location list with
19119 multiple elements. */
19120 if (!loc_result || !single_element_loc_list_p (loc_result))
19121 return NULL;
19122 else
19123 return loc_result->expr;
19126 /* The following routines define various Dwarf attributes and any data
19127 associated with them. */
19129 /* Add a location description attribute value to a DIE.
19131 This emits location attributes suitable for whole variables and
19132 whole parameters. Note that the location attributes for struct fields are
19133 generated by the routine `data_member_location_attribute' below. */
19135 static inline void
19136 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19137 dw_loc_list_ref descr)
19139 bool check_no_locviews = true;
19140 if (descr == 0)
19141 return;
19142 if (single_element_loc_list_p (descr))
19143 add_AT_loc (die, attr_kind, descr->expr);
19144 else
19146 add_AT_loc_list (die, attr_kind, descr);
19147 gcc_assert (descr->ll_symbol);
19148 if (attr_kind == DW_AT_location && descr->vl_symbol
19149 && dwarf2out_locviews_in_attribute ())
19151 add_AT_view_list (die, DW_AT_GNU_locviews);
19152 check_no_locviews = false;
19156 if (check_no_locviews)
19157 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19160 /* Add DW_AT_accessibility attribute to DIE if needed. */
19162 static void
19163 add_accessibility_attribute (dw_die_ref die, tree decl)
19165 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19166 children, otherwise the default is DW_ACCESS_public. In DWARF2
19167 the default has always been DW_ACCESS_public. */
19168 if (TREE_PROTECTED (decl))
19169 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19170 else if (TREE_PRIVATE (decl))
19172 if (dwarf_version == 2
19173 || die->die_parent == NULL
19174 || die->die_parent->die_tag != DW_TAG_class_type)
19175 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19177 else if (dwarf_version > 2
19178 && die->die_parent
19179 && die->die_parent->die_tag == DW_TAG_class_type)
19180 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19183 /* Attach the specialized form of location attribute used for data members of
19184 struct and union types. In the special case of a FIELD_DECL node which
19185 represents a bit-field, the "offset" part of this special location
19186 descriptor must indicate the distance in bytes from the lowest-addressed
19187 byte of the containing struct or union type to the lowest-addressed byte of
19188 the "containing object" for the bit-field. (See the `field_byte_offset'
19189 function above).
19191 For any given bit-field, the "containing object" is a hypothetical object
19192 (of some integral or enum type) within which the given bit-field lives. The
19193 type of this hypothetical "containing object" is always the same as the
19194 declared type of the individual bit-field itself (for GCC anyway... the
19195 DWARF spec doesn't actually mandate this). Note that it is the size (in
19196 bytes) of the hypothetical "containing object" which will be given in the
19197 DW_AT_byte_size attribute for this bit-field. (See the
19198 `byte_size_attribute' function below.) It is also used when calculating the
19199 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19200 function below.)
19202 CTX is required: see the comment for VLR_CONTEXT. */
19204 static void
19205 add_data_member_location_attribute (dw_die_ref die,
19206 tree decl,
19207 struct vlr_context *ctx)
19209 HOST_WIDE_INT offset;
19210 dw_loc_descr_ref loc_descr = 0;
19212 if (TREE_CODE (decl) == TREE_BINFO)
19214 /* We're working on the TAG_inheritance for a base class. */
19215 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19217 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19218 aren't at a fixed offset from all (sub)objects of the same
19219 type. We need to extract the appropriate offset from our
19220 vtable. The following dwarf expression means
19222 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19224 This is specific to the V3 ABI, of course. */
19226 dw_loc_descr_ref tmp;
19228 /* Make a copy of the object address. */
19229 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19230 add_loc_descr (&loc_descr, tmp);
19232 /* Extract the vtable address. */
19233 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19234 add_loc_descr (&loc_descr, tmp);
19236 /* Calculate the address of the offset. */
19237 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19238 gcc_assert (offset < 0);
19240 tmp = int_loc_descriptor (-offset);
19241 add_loc_descr (&loc_descr, tmp);
19242 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19243 add_loc_descr (&loc_descr, tmp);
19245 /* Extract the offset. */
19246 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19247 add_loc_descr (&loc_descr, tmp);
19249 /* Add it to the object address. */
19250 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19251 add_loc_descr (&loc_descr, tmp);
19253 else
19254 offset = tree_to_shwi (BINFO_OFFSET (decl));
19256 else
19258 loc_descr = field_byte_offset (decl, ctx, &offset);
19260 /* If loc_descr is available then we know the field offset is dynamic.
19261 However, GDB does not handle dynamic field offsets very well at the
19262 moment. */
19263 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19265 loc_descr = NULL;
19266 offset = 0;
19269 /* Data member location evalutation starts with the base address on the
19270 stack. Compute the field offset and add it to this base address. */
19271 else if (loc_descr != NULL)
19272 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19275 if (! loc_descr)
19277 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19278 e.g. GDB only added support to it in November 2016. For DWARF5
19279 we need newer debug info consumers anyway. We might change this
19280 to dwarf_version >= 4 once most consumers catched up. */
19281 if (dwarf_version >= 5
19282 && TREE_CODE (decl) == FIELD_DECL
19283 && DECL_BIT_FIELD_TYPE (decl))
19285 tree off = bit_position (decl);
19286 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19288 remove_AT (die, DW_AT_byte_size);
19289 remove_AT (die, DW_AT_bit_offset);
19290 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19291 return;
19294 if (dwarf_version > 2)
19296 /* Don't need to output a location expression, just the constant. */
19297 if (offset < 0)
19298 add_AT_int (die, DW_AT_data_member_location, offset);
19299 else
19300 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19301 return;
19303 else
19305 enum dwarf_location_atom op;
19307 /* The DWARF2 standard says that we should assume that the structure
19308 address is already on the stack, so we can specify a structure
19309 field address by using DW_OP_plus_uconst. */
19310 op = DW_OP_plus_uconst;
19311 loc_descr = new_loc_descr (op, offset, 0);
19315 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19318 /* Writes integer values to dw_vec_const array. */
19320 static void
19321 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19323 while (size != 0)
19325 *dest++ = val & 0xff;
19326 val >>= 8;
19327 --size;
19331 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19333 static HOST_WIDE_INT
19334 extract_int (const unsigned char *src, unsigned int size)
19336 HOST_WIDE_INT val = 0;
19338 src += size;
19339 while (size != 0)
19341 val <<= 8;
19342 val |= *--src & 0xff;
19343 --size;
19345 return val;
19348 /* Writes wide_int values to dw_vec_const array. */
19350 static void
19351 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19353 int i;
19355 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19357 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19358 return;
19361 /* We'd have to extend this code to support odd sizes. */
19362 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19364 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19366 if (WORDS_BIG_ENDIAN)
19367 for (i = n - 1; i >= 0; i--)
19369 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19370 dest += sizeof (HOST_WIDE_INT);
19372 else
19373 for (i = 0; i < n; i++)
19375 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19376 dest += sizeof (HOST_WIDE_INT);
19380 /* Writes floating point values to dw_vec_const array. */
19382 static void
19383 insert_float (const_rtx rtl, unsigned char *array)
19385 long val[4];
19386 int i;
19387 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19389 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19391 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19392 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19394 insert_int (val[i], 4, array);
19395 array += 4;
19399 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19400 does not have a "location" either in memory or in a register. These
19401 things can arise in GNU C when a constant is passed as an actual parameter
19402 to an inlined function. They can also arise in C++ where declared
19403 constants do not necessarily get memory "homes". */
19405 static bool
19406 add_const_value_attribute (dw_die_ref die, rtx rtl)
19408 switch (GET_CODE (rtl))
19410 case CONST_INT:
19412 HOST_WIDE_INT val = INTVAL (rtl);
19414 if (val < 0)
19415 add_AT_int (die, DW_AT_const_value, val);
19416 else
19417 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19419 return true;
19421 case CONST_WIDE_INT:
19423 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19424 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19425 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19426 wide_int w = wi::zext (w1, prec);
19427 add_AT_wide (die, DW_AT_const_value, w);
19429 return true;
19431 case CONST_DOUBLE:
19432 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19433 floating-point constant. A CONST_DOUBLE is used whenever the
19434 constant requires more than one word in order to be adequately
19435 represented. */
19436 if (TARGET_SUPPORTS_WIDE_INT == 0
19437 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19438 add_AT_double (die, DW_AT_const_value,
19439 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19440 else
19442 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19443 unsigned int length = GET_MODE_SIZE (mode);
19444 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19446 insert_float (rtl, array);
19447 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19449 return true;
19451 case CONST_VECTOR:
19453 unsigned int length;
19454 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19455 return false;
19457 machine_mode mode = GET_MODE (rtl);
19458 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19459 unsigned char *array
19460 = ggc_vec_alloc<unsigned char> (length * elt_size);
19461 unsigned int i;
19462 unsigned char *p;
19463 machine_mode imode = GET_MODE_INNER (mode);
19465 switch (GET_MODE_CLASS (mode))
19467 case MODE_VECTOR_INT:
19468 for (i = 0, p = array; i < length; i++, p += elt_size)
19470 rtx elt = CONST_VECTOR_ELT (rtl, i);
19471 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19473 break;
19475 case MODE_VECTOR_FLOAT:
19476 for (i = 0, p = array; i < length; i++, p += elt_size)
19478 rtx elt = CONST_VECTOR_ELT (rtl, i);
19479 insert_float (elt, p);
19481 break;
19483 default:
19484 gcc_unreachable ();
19487 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19489 return true;
19491 case CONST_STRING:
19492 if (dwarf_version >= 4 || !dwarf_strict)
19494 dw_loc_descr_ref loc_result;
19495 resolve_one_addr (&rtl);
19496 rtl_addr:
19497 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19498 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19499 add_AT_loc (die, DW_AT_location, loc_result);
19500 vec_safe_push (used_rtx_array, rtl);
19501 return true;
19503 return false;
19505 case CONST:
19506 if (CONSTANT_P (XEXP (rtl, 0)))
19507 return add_const_value_attribute (die, XEXP (rtl, 0));
19508 /* FALLTHROUGH */
19509 case SYMBOL_REF:
19510 if (!const_ok_for_output (rtl))
19511 return false;
19512 /* FALLTHROUGH */
19513 case LABEL_REF:
19514 if (dwarf_version >= 4 || !dwarf_strict)
19515 goto rtl_addr;
19516 return false;
19518 case PLUS:
19519 /* In cases where an inlined instance of an inline function is passed
19520 the address of an `auto' variable (which is local to the caller) we
19521 can get a situation where the DECL_RTL of the artificial local
19522 variable (for the inlining) which acts as a stand-in for the
19523 corresponding formal parameter (of the inline function) will look
19524 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19525 exactly a compile-time constant expression, but it isn't the address
19526 of the (artificial) local variable either. Rather, it represents the
19527 *value* which the artificial local variable always has during its
19528 lifetime. We currently have no way to represent such quasi-constant
19529 values in Dwarf, so for now we just punt and generate nothing. */
19530 return false;
19532 case HIGH:
19533 case CONST_FIXED:
19534 return false;
19536 case MEM:
19537 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19538 && MEM_READONLY_P (rtl)
19539 && GET_MODE (rtl) == BLKmode)
19541 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19542 return true;
19544 return false;
19546 default:
19547 /* No other kinds of rtx should be possible here. */
19548 gcc_unreachable ();
19550 return false;
19553 /* Determine whether the evaluation of EXPR references any variables
19554 or functions which aren't otherwise used (and therefore may not be
19555 output). */
19556 static tree
19557 reference_to_unused (tree * tp, int * walk_subtrees,
19558 void * data ATTRIBUTE_UNUSED)
19560 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19561 *walk_subtrees = 0;
19563 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19564 && ! TREE_ASM_WRITTEN (*tp))
19565 return *tp;
19566 /* ??? The C++ FE emits debug information for using decls, so
19567 putting gcc_unreachable here falls over. See PR31899. For now
19568 be conservative. */
19569 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19570 return *tp;
19571 else if (VAR_P (*tp))
19573 varpool_node *node = varpool_node::get (*tp);
19574 if (!node || !node->definition)
19575 return *tp;
19577 else if (TREE_CODE (*tp) == FUNCTION_DECL
19578 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19580 /* The call graph machinery must have finished analyzing,
19581 optimizing and gimplifying the CU by now.
19582 So if *TP has no call graph node associated
19583 to it, it means *TP will not be emitted. */
19584 if (!cgraph_node::get (*tp))
19585 return *tp;
19587 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19588 return *tp;
19590 return NULL_TREE;
19593 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19594 for use in a later add_const_value_attribute call. */
19596 static rtx
19597 rtl_for_decl_init (tree init, tree type)
19599 rtx rtl = NULL_RTX;
19601 STRIP_NOPS (init);
19603 /* If a variable is initialized with a string constant without embedded
19604 zeros, build CONST_STRING. */
19605 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19607 tree enttype = TREE_TYPE (type);
19608 tree domain = TYPE_DOMAIN (type);
19609 scalar_int_mode mode;
19611 if (is_int_mode (TYPE_MODE (enttype), &mode)
19612 && GET_MODE_SIZE (mode) == 1
19613 && domain
19614 && TYPE_MAX_VALUE (domain)
19615 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19616 && integer_zerop (TYPE_MIN_VALUE (domain))
19617 && compare_tree_int (TYPE_MAX_VALUE (domain),
19618 TREE_STRING_LENGTH (init) - 1) == 0
19619 && ((size_t) TREE_STRING_LENGTH (init)
19620 == strlen (TREE_STRING_POINTER (init)) + 1))
19622 rtl = gen_rtx_CONST_STRING (VOIDmode,
19623 ggc_strdup (TREE_STRING_POINTER (init)));
19624 rtl = gen_rtx_MEM (BLKmode, rtl);
19625 MEM_READONLY_P (rtl) = 1;
19628 /* Other aggregates, and complex values, could be represented using
19629 CONCAT: FIXME! */
19630 else if (AGGREGATE_TYPE_P (type)
19631 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19632 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19633 || TREE_CODE (type) == COMPLEX_TYPE)
19635 /* Vectors only work if their mode is supported by the target.
19636 FIXME: generic vectors ought to work too. */
19637 else if (TREE_CODE (type) == VECTOR_TYPE
19638 && !VECTOR_MODE_P (TYPE_MODE (type)))
19640 /* If the initializer is something that we know will expand into an
19641 immediate RTL constant, expand it now. We must be careful not to
19642 reference variables which won't be output. */
19643 else if (initializer_constant_valid_p (init, type)
19644 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19646 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19647 possible. */
19648 if (TREE_CODE (type) == VECTOR_TYPE)
19649 switch (TREE_CODE (init))
19651 case VECTOR_CST:
19652 break;
19653 case CONSTRUCTOR:
19654 if (TREE_CONSTANT (init))
19656 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19657 bool constant_p = true;
19658 tree value;
19659 unsigned HOST_WIDE_INT ix;
19661 /* Even when ctor is constant, it might contain non-*_CST
19662 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19663 belong into VECTOR_CST nodes. */
19664 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19665 if (!CONSTANT_CLASS_P (value))
19667 constant_p = false;
19668 break;
19671 if (constant_p)
19673 init = build_vector_from_ctor (type, elts);
19674 break;
19677 /* FALLTHRU */
19679 default:
19680 return NULL;
19683 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19685 /* If expand_expr returns a MEM, it wasn't immediate. */
19686 gcc_assert (!rtl || !MEM_P (rtl));
19689 return rtl;
19692 /* Generate RTL for the variable DECL to represent its location. */
19694 static rtx
19695 rtl_for_decl_location (tree decl)
19697 rtx rtl;
19699 /* Here we have to decide where we are going to say the parameter "lives"
19700 (as far as the debugger is concerned). We only have a couple of
19701 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19703 DECL_RTL normally indicates where the parameter lives during most of the
19704 activation of the function. If optimization is enabled however, this
19705 could be either NULL or else a pseudo-reg. Both of those cases indicate
19706 that the parameter doesn't really live anywhere (as far as the code
19707 generation parts of GCC are concerned) during most of the function's
19708 activation. That will happen (for example) if the parameter is never
19709 referenced within the function.
19711 We could just generate a location descriptor here for all non-NULL
19712 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19713 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19714 where DECL_RTL is NULL or is a pseudo-reg.
19716 Note however that we can only get away with using DECL_INCOMING_RTL as
19717 a backup substitute for DECL_RTL in certain limited cases. In cases
19718 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19719 we can be sure that the parameter was passed using the same type as it is
19720 declared to have within the function, and that its DECL_INCOMING_RTL
19721 points us to a place where a value of that type is passed.
19723 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19724 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19725 because in these cases DECL_INCOMING_RTL points us to a value of some
19726 type which is *different* from the type of the parameter itself. Thus,
19727 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19728 such cases, the debugger would end up (for example) trying to fetch a
19729 `float' from a place which actually contains the first part of a
19730 `double'. That would lead to really incorrect and confusing
19731 output at debug-time.
19733 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19734 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19735 are a couple of exceptions however. On little-endian machines we can
19736 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19737 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19738 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19739 when (on a little-endian machine) a non-prototyped function has a
19740 parameter declared to be of type `short' or `char'. In such cases,
19741 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19742 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19743 passed `int' value. If the debugger then uses that address to fetch
19744 a `short' or a `char' (on a little-endian machine) the result will be
19745 the correct data, so we allow for such exceptional cases below.
19747 Note that our goal here is to describe the place where the given formal
19748 parameter lives during most of the function's activation (i.e. between the
19749 end of the prologue and the start of the epilogue). We'll do that as best
19750 as we can. Note however that if the given formal parameter is modified
19751 sometime during the execution of the function, then a stack backtrace (at
19752 debug-time) will show the function as having been called with the *new*
19753 value rather than the value which was originally passed in. This happens
19754 rarely enough that it is not a major problem, but it *is* a problem, and
19755 I'd like to fix it.
19757 A future version of dwarf2out.c may generate two additional attributes for
19758 any given DW_TAG_formal_parameter DIE which will describe the "passed
19759 type" and the "passed location" for the given formal parameter in addition
19760 to the attributes we now generate to indicate the "declared type" and the
19761 "active location" for each parameter. This additional set of attributes
19762 could be used by debuggers for stack backtraces. Separately, note that
19763 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19764 This happens (for example) for inlined-instances of inline function formal
19765 parameters which are never referenced. This really shouldn't be
19766 happening. All PARM_DECL nodes should get valid non-NULL
19767 DECL_INCOMING_RTL values. FIXME. */
19769 /* Use DECL_RTL as the "location" unless we find something better. */
19770 rtl = DECL_RTL_IF_SET (decl);
19772 /* When generating abstract instances, ignore everything except
19773 constants, symbols living in memory, and symbols living in
19774 fixed registers. */
19775 if (! reload_completed)
19777 if (rtl
19778 && (CONSTANT_P (rtl)
19779 || (MEM_P (rtl)
19780 && CONSTANT_P (XEXP (rtl, 0)))
19781 || (REG_P (rtl)
19782 && VAR_P (decl)
19783 && TREE_STATIC (decl))))
19785 rtl = targetm.delegitimize_address (rtl);
19786 return rtl;
19788 rtl = NULL_RTX;
19790 else if (TREE_CODE (decl) == PARM_DECL)
19792 if (rtl == NULL_RTX
19793 || is_pseudo_reg (rtl)
19794 || (MEM_P (rtl)
19795 && is_pseudo_reg (XEXP (rtl, 0))
19796 && DECL_INCOMING_RTL (decl)
19797 && MEM_P (DECL_INCOMING_RTL (decl))
19798 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19800 tree declared_type = TREE_TYPE (decl);
19801 tree passed_type = DECL_ARG_TYPE (decl);
19802 machine_mode dmode = TYPE_MODE (declared_type);
19803 machine_mode pmode = TYPE_MODE (passed_type);
19805 /* This decl represents a formal parameter which was optimized out.
19806 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19807 all cases where (rtl == NULL_RTX) just below. */
19808 if (dmode == pmode)
19809 rtl = DECL_INCOMING_RTL (decl);
19810 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19811 && SCALAR_INT_MODE_P (dmode)
19812 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19813 && DECL_INCOMING_RTL (decl))
19815 rtx inc = DECL_INCOMING_RTL (decl);
19816 if (REG_P (inc))
19817 rtl = inc;
19818 else if (MEM_P (inc))
19820 if (BYTES_BIG_ENDIAN)
19821 rtl = adjust_address_nv (inc, dmode,
19822 GET_MODE_SIZE (pmode)
19823 - GET_MODE_SIZE (dmode));
19824 else
19825 rtl = inc;
19830 /* If the parm was passed in registers, but lives on the stack, then
19831 make a big endian correction if the mode of the type of the
19832 parameter is not the same as the mode of the rtl. */
19833 /* ??? This is the same series of checks that are made in dbxout.c before
19834 we reach the big endian correction code there. It isn't clear if all
19835 of these checks are necessary here, but keeping them all is the safe
19836 thing to do. */
19837 else if (MEM_P (rtl)
19838 && XEXP (rtl, 0) != const0_rtx
19839 && ! CONSTANT_P (XEXP (rtl, 0))
19840 /* Not passed in memory. */
19841 && !MEM_P (DECL_INCOMING_RTL (decl))
19842 /* Not passed by invisible reference. */
19843 && (!REG_P (XEXP (rtl, 0))
19844 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19845 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19846 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19847 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19848 #endif
19850 /* Big endian correction check. */
19851 && BYTES_BIG_ENDIAN
19852 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19853 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19854 UNITS_PER_WORD))
19856 machine_mode addr_mode = get_address_mode (rtl);
19857 poly_int64 offset = (UNITS_PER_WORD
19858 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19860 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19861 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19864 else if (VAR_P (decl)
19865 && rtl
19866 && MEM_P (rtl)
19867 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19869 machine_mode addr_mode = get_address_mode (rtl);
19870 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19871 GET_MODE (rtl));
19873 /* If a variable is declared "register" yet is smaller than
19874 a register, then if we store the variable to memory, it
19875 looks like we're storing a register-sized value, when in
19876 fact we are not. We need to adjust the offset of the
19877 storage location to reflect the actual value's bytes,
19878 else gdb will not be able to display it. */
19879 if (maybe_ne (offset, 0))
19880 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19881 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19884 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19885 and will have been substituted directly into all expressions that use it.
19886 C does not have such a concept, but C++ and other languages do. */
19887 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19888 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19890 if (rtl)
19891 rtl = targetm.delegitimize_address (rtl);
19893 /* If we don't look past the constant pool, we risk emitting a
19894 reference to a constant pool entry that isn't referenced from
19895 code, and thus is not emitted. */
19896 if (rtl)
19897 rtl = avoid_constant_pool_reference (rtl);
19899 /* Try harder to get a rtl. If this symbol ends up not being emitted
19900 in the current CU, resolve_addr will remove the expression referencing
19901 it. */
19902 if (rtl == NULL_RTX
19903 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
19904 && VAR_P (decl)
19905 && !DECL_EXTERNAL (decl)
19906 && TREE_STATIC (decl)
19907 && DECL_NAME (decl)
19908 && !DECL_HARD_REGISTER (decl)
19909 && DECL_MODE (decl) != VOIDmode)
19911 rtl = make_decl_rtl_for_debug (decl);
19912 if (!MEM_P (rtl)
19913 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19914 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19915 rtl = NULL_RTX;
19918 return rtl;
19921 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19922 returned. If so, the decl for the COMMON block is returned, and the
19923 value is the offset into the common block for the symbol. */
19925 static tree
19926 fortran_common (tree decl, HOST_WIDE_INT *value)
19928 tree val_expr, cvar;
19929 machine_mode mode;
19930 poly_int64 bitsize, bitpos;
19931 tree offset;
19932 HOST_WIDE_INT cbitpos;
19933 int unsignedp, reversep, volatilep = 0;
19935 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19936 it does not have a value (the offset into the common area), or if it
19937 is thread local (as opposed to global) then it isn't common, and shouldn't
19938 be handled as such. */
19939 if (!VAR_P (decl)
19940 || !TREE_STATIC (decl)
19941 || !DECL_HAS_VALUE_EXPR_P (decl)
19942 || !is_fortran ())
19943 return NULL_TREE;
19945 val_expr = DECL_VALUE_EXPR (decl);
19946 if (TREE_CODE (val_expr) != COMPONENT_REF)
19947 return NULL_TREE;
19949 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19950 &unsignedp, &reversep, &volatilep);
19952 if (cvar == NULL_TREE
19953 || !VAR_P (cvar)
19954 || DECL_ARTIFICIAL (cvar)
19955 || !TREE_PUBLIC (cvar)
19956 /* We don't expect to have to cope with variable offsets,
19957 since at present all static data must have a constant size. */
19958 || !bitpos.is_constant (&cbitpos))
19959 return NULL_TREE;
19961 *value = 0;
19962 if (offset != NULL)
19964 if (!tree_fits_shwi_p (offset))
19965 return NULL_TREE;
19966 *value = tree_to_shwi (offset);
19968 if (cbitpos != 0)
19969 *value += cbitpos / BITS_PER_UNIT;
19971 return cvar;
19974 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19975 data attribute for a variable or a parameter. We generate the
19976 DW_AT_const_value attribute only in those cases where the given variable
19977 or parameter does not have a true "location" either in memory or in a
19978 register. This can happen (for example) when a constant is passed as an
19979 actual argument in a call to an inline function. (It's possible that
19980 these things can crop up in other ways also.) Note that one type of
19981 constant value which can be passed into an inlined function is a constant
19982 pointer. This can happen for example if an actual argument in an inlined
19983 function call evaluates to a compile-time constant address.
19985 CACHE_P is true if it is worth caching the location list for DECL,
19986 so that future calls can reuse it rather than regenerate it from scratch.
19987 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19988 since we will need to refer to them each time the function is inlined. */
19990 static bool
19991 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19993 rtx rtl;
19994 dw_loc_list_ref list;
19995 var_loc_list *loc_list;
19996 cached_dw_loc_list *cache;
19998 if (early_dwarf)
19999 return false;
20001 if (TREE_CODE (decl) == ERROR_MARK)
20002 return false;
20004 if (get_AT (die, DW_AT_location)
20005 || get_AT (die, DW_AT_const_value))
20006 return true;
20008 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20009 || TREE_CODE (decl) == RESULT_DECL);
20011 /* Try to get some constant RTL for this decl, and use that as the value of
20012 the location. */
20014 rtl = rtl_for_decl_location (decl);
20015 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20016 && add_const_value_attribute (die, rtl))
20017 return true;
20019 /* See if we have single element location list that is equivalent to
20020 a constant value. That way we are better to use add_const_value_attribute
20021 rather than expanding constant value equivalent. */
20022 loc_list = lookup_decl_loc (decl);
20023 if (loc_list
20024 && loc_list->first
20025 && loc_list->first->next == NULL
20026 && NOTE_P (loc_list->first->loc)
20027 && NOTE_VAR_LOCATION (loc_list->first->loc)
20028 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20030 struct var_loc_node *node;
20032 node = loc_list->first;
20033 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20034 if (GET_CODE (rtl) == EXPR_LIST)
20035 rtl = XEXP (rtl, 0);
20036 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20037 && add_const_value_attribute (die, rtl))
20038 return true;
20040 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20041 list several times. See if we've already cached the contents. */
20042 list = NULL;
20043 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20044 cache_p = false;
20045 if (cache_p)
20047 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20048 if (cache)
20049 list = cache->loc_list;
20051 if (list == NULL)
20053 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20054 NULL);
20055 /* It is usually worth caching this result if the decl is from
20056 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20057 if (cache_p && list && list->dw_loc_next)
20059 cached_dw_loc_list **slot
20060 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20061 DECL_UID (decl),
20062 INSERT);
20063 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20064 cache->decl_id = DECL_UID (decl);
20065 cache->loc_list = list;
20066 *slot = cache;
20069 if (list)
20071 add_AT_location_description (die, DW_AT_location, list);
20072 return true;
20074 /* None of that worked, so it must not really have a location;
20075 try adding a constant value attribute from the DECL_INITIAL. */
20076 return tree_add_const_value_attribute_for_decl (die, decl);
20079 /* Helper function for tree_add_const_value_attribute. Natively encode
20080 initializer INIT into an array. Return true if successful. */
20082 static bool
20083 native_encode_initializer (tree init, unsigned char *array, int size)
20085 tree type;
20087 if (init == NULL_TREE)
20088 return false;
20090 STRIP_NOPS (init);
20091 switch (TREE_CODE (init))
20093 case STRING_CST:
20094 type = TREE_TYPE (init);
20095 if (TREE_CODE (type) == ARRAY_TYPE)
20097 tree enttype = TREE_TYPE (type);
20098 scalar_int_mode mode;
20100 if (!is_int_mode (TYPE_MODE (enttype), &mode)
20101 || GET_MODE_SIZE (mode) != 1)
20102 return false;
20103 if (int_size_in_bytes (type) != size)
20104 return false;
20105 if (size > TREE_STRING_LENGTH (init))
20107 memcpy (array, TREE_STRING_POINTER (init),
20108 TREE_STRING_LENGTH (init));
20109 memset (array + TREE_STRING_LENGTH (init),
20110 '\0', size - TREE_STRING_LENGTH (init));
20112 else
20113 memcpy (array, TREE_STRING_POINTER (init), size);
20114 return true;
20116 return false;
20117 case CONSTRUCTOR:
20118 type = TREE_TYPE (init);
20119 if (int_size_in_bytes (type) != size)
20120 return false;
20121 if (TREE_CODE (type) == ARRAY_TYPE)
20123 HOST_WIDE_INT min_index;
20124 unsigned HOST_WIDE_INT cnt;
20125 int curpos = 0, fieldsize;
20126 constructor_elt *ce;
20128 if (TYPE_DOMAIN (type) == NULL_TREE
20129 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20130 return false;
20132 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20133 if (fieldsize <= 0)
20134 return false;
20136 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20137 memset (array, '\0', size);
20138 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20140 tree val = ce->value;
20141 tree index = ce->index;
20142 int pos = curpos;
20143 if (index && TREE_CODE (index) == RANGE_EXPR)
20144 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20145 * fieldsize;
20146 else if (index)
20147 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20149 if (val)
20151 STRIP_NOPS (val);
20152 if (!native_encode_initializer (val, array + pos, fieldsize))
20153 return false;
20155 curpos = pos + fieldsize;
20156 if (index && TREE_CODE (index) == RANGE_EXPR)
20158 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20159 - tree_to_shwi (TREE_OPERAND (index, 0));
20160 while (count-- > 0)
20162 if (val)
20163 memcpy (array + curpos, array + pos, fieldsize);
20164 curpos += fieldsize;
20167 gcc_assert (curpos <= size);
20169 return true;
20171 else if (TREE_CODE (type) == RECORD_TYPE
20172 || TREE_CODE (type) == UNION_TYPE)
20174 tree field = NULL_TREE;
20175 unsigned HOST_WIDE_INT cnt;
20176 constructor_elt *ce;
20178 if (int_size_in_bytes (type) != size)
20179 return false;
20181 if (TREE_CODE (type) == RECORD_TYPE)
20182 field = TYPE_FIELDS (type);
20184 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20186 tree val = ce->value;
20187 int pos, fieldsize;
20189 if (ce->index != 0)
20190 field = ce->index;
20192 if (val)
20193 STRIP_NOPS (val);
20195 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20196 return false;
20198 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20199 && TYPE_DOMAIN (TREE_TYPE (field))
20200 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20201 return false;
20202 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20203 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20204 return false;
20205 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20206 pos = int_byte_position (field);
20207 gcc_assert (pos + fieldsize <= size);
20208 if (val && fieldsize != 0
20209 && !native_encode_initializer (val, array + pos, fieldsize))
20210 return false;
20212 return true;
20214 return false;
20215 case VIEW_CONVERT_EXPR:
20216 case NON_LVALUE_EXPR:
20217 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20218 default:
20219 return native_encode_expr (init, array, size) == size;
20223 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20224 attribute is the const value T. */
20226 static bool
20227 tree_add_const_value_attribute (dw_die_ref die, tree t)
20229 tree init;
20230 tree type = TREE_TYPE (t);
20231 rtx rtl;
20233 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20234 return false;
20236 init = t;
20237 gcc_assert (!DECL_P (init));
20239 if (TREE_CODE (init) == INTEGER_CST)
20241 if (tree_fits_uhwi_p (init))
20243 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20244 return true;
20246 if (tree_fits_shwi_p (init))
20248 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20249 return true;
20252 if (! early_dwarf)
20254 rtl = rtl_for_decl_init (init, type);
20255 if (rtl)
20256 return add_const_value_attribute (die, rtl);
20258 /* If the host and target are sane, try harder. */
20259 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20260 && initializer_constant_valid_p (init, type))
20262 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20263 if (size > 0 && (int) size == size)
20265 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20267 if (native_encode_initializer (init, array, size))
20269 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20270 return true;
20272 ggc_free (array);
20275 return false;
20278 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20279 attribute is the const value of T, where T is an integral constant
20280 variable with static storage duration
20281 (so it can't be a PARM_DECL or a RESULT_DECL). */
20283 static bool
20284 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20287 if (!decl
20288 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20289 || (VAR_P (decl) && !TREE_STATIC (decl)))
20290 return false;
20292 if (TREE_READONLY (decl)
20293 && ! TREE_THIS_VOLATILE (decl)
20294 && DECL_INITIAL (decl))
20295 /* OK */;
20296 else
20297 return false;
20299 /* Don't add DW_AT_const_value if abstract origin already has one. */
20300 if (get_AT (var_die, DW_AT_const_value))
20301 return false;
20303 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20306 /* Convert the CFI instructions for the current function into a
20307 location list. This is used for DW_AT_frame_base when we targeting
20308 a dwarf2 consumer that does not support the dwarf3
20309 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20310 expressions. */
20312 static dw_loc_list_ref
20313 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20315 int ix;
20316 dw_fde_ref fde;
20317 dw_loc_list_ref list, *list_tail;
20318 dw_cfi_ref cfi;
20319 dw_cfa_location last_cfa, next_cfa;
20320 const char *start_label, *last_label, *section;
20321 dw_cfa_location remember;
20323 fde = cfun->fde;
20324 gcc_assert (fde != NULL);
20326 section = secname_for_decl (current_function_decl);
20327 list_tail = &list;
20328 list = NULL;
20330 memset (&next_cfa, 0, sizeof (next_cfa));
20331 next_cfa.reg = INVALID_REGNUM;
20332 remember = next_cfa;
20334 start_label = fde->dw_fde_begin;
20336 /* ??? Bald assumption that the CIE opcode list does not contain
20337 advance opcodes. */
20338 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20339 lookup_cfa_1 (cfi, &next_cfa, &remember);
20341 last_cfa = next_cfa;
20342 last_label = start_label;
20344 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20346 /* If the first partition contained no CFI adjustments, the
20347 CIE opcodes apply to the whole first partition. */
20348 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20349 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20350 list_tail =&(*list_tail)->dw_loc_next;
20351 start_label = last_label = fde->dw_fde_second_begin;
20354 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20356 switch (cfi->dw_cfi_opc)
20358 case DW_CFA_set_loc:
20359 case DW_CFA_advance_loc1:
20360 case DW_CFA_advance_loc2:
20361 case DW_CFA_advance_loc4:
20362 if (!cfa_equal_p (&last_cfa, &next_cfa))
20364 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20365 start_label, 0, last_label, 0, section);
20367 list_tail = &(*list_tail)->dw_loc_next;
20368 last_cfa = next_cfa;
20369 start_label = last_label;
20371 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20372 break;
20374 case DW_CFA_advance_loc:
20375 /* The encoding is complex enough that we should never emit this. */
20376 gcc_unreachable ();
20378 default:
20379 lookup_cfa_1 (cfi, &next_cfa, &remember);
20380 break;
20382 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20384 if (!cfa_equal_p (&last_cfa, &next_cfa))
20386 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20387 start_label, 0, last_label, 0, section);
20389 list_tail = &(*list_tail)->dw_loc_next;
20390 last_cfa = next_cfa;
20391 start_label = last_label;
20393 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20394 start_label, 0, fde->dw_fde_end, 0, section);
20395 list_tail = &(*list_tail)->dw_loc_next;
20396 start_label = last_label = fde->dw_fde_second_begin;
20400 if (!cfa_equal_p (&last_cfa, &next_cfa))
20402 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20403 start_label, 0, last_label, 0, section);
20404 list_tail = &(*list_tail)->dw_loc_next;
20405 start_label = last_label;
20408 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20409 start_label, 0,
20410 fde->dw_fde_second_begin
20411 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20412 section);
20414 maybe_gen_llsym (list);
20416 return list;
20419 /* Compute a displacement from the "steady-state frame pointer" to the
20420 frame base (often the same as the CFA), and store it in
20421 frame_pointer_fb_offset. OFFSET is added to the displacement
20422 before the latter is negated. */
20424 static void
20425 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20427 rtx reg, elim;
20429 #ifdef FRAME_POINTER_CFA_OFFSET
20430 reg = frame_pointer_rtx;
20431 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20432 #else
20433 reg = arg_pointer_rtx;
20434 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20435 #endif
20437 elim = (ira_use_lra_p
20438 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20439 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20440 elim = strip_offset_and_add (elim, &offset);
20442 frame_pointer_fb_offset = -offset;
20444 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20445 in which to eliminate. This is because it's stack pointer isn't
20446 directly accessible as a register within the ISA. To work around
20447 this, assume that while we cannot provide a proper value for
20448 frame_pointer_fb_offset, we won't need one either. We can use
20449 hard frame pointer in debug info even if frame pointer isn't used
20450 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20451 which uses the DW_AT_frame_base attribute, not hard frame pointer
20452 directly. */
20453 frame_pointer_fb_offset_valid
20454 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20457 /* Generate a DW_AT_name attribute given some string value to be included as
20458 the value of the attribute. */
20460 static void
20461 add_name_attribute (dw_die_ref die, const char *name_string)
20463 if (name_string != NULL && *name_string != 0)
20465 if (demangle_name_func)
20466 name_string = (*demangle_name_func) (name_string);
20468 add_AT_string (die, DW_AT_name, name_string);
20472 /* Generate a DW_AT_description attribute given some string value to be included
20473 as the value of the attribute. */
20475 static void
20476 add_desc_attribute (dw_die_ref die, const char *name_string)
20478 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20479 return;
20481 if (name_string == NULL || *name_string == 0)
20482 return;
20484 if (demangle_name_func)
20485 name_string = (*demangle_name_func) (name_string);
20487 add_AT_string (die, DW_AT_description, name_string);
20490 /* Generate a DW_AT_description attribute given some decl to be included
20491 as the value of the attribute. */
20493 static void
20494 add_desc_attribute (dw_die_ref die, tree decl)
20496 tree decl_name;
20498 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20499 return;
20501 if (decl == NULL_TREE || !DECL_P (decl))
20502 return;
20503 decl_name = DECL_NAME (decl);
20505 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20507 const char *name = dwarf2_name (decl, 0);
20508 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20510 else
20512 char *desc = print_generic_expr_to_str (decl);
20513 add_desc_attribute (die, desc);
20514 free (desc);
20518 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20519 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20520 of TYPE accordingly.
20522 ??? This is a temporary measure until after we're able to generate
20523 regular DWARF for the complex Ada type system. */
20525 static void
20526 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20527 dw_die_ref context_die)
20529 tree dtype;
20530 dw_die_ref dtype_die;
20532 if (!lang_hooks.types.descriptive_type)
20533 return;
20535 dtype = lang_hooks.types.descriptive_type (type);
20536 if (!dtype)
20537 return;
20539 dtype_die = lookup_type_die (dtype);
20540 if (!dtype_die)
20542 gen_type_die (dtype, context_die);
20543 dtype_die = lookup_type_die (dtype);
20544 gcc_assert (dtype_die);
20547 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20550 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20552 static const char *
20553 comp_dir_string (void)
20555 const char *wd;
20556 char *wd1;
20557 static const char *cached_wd = NULL;
20559 if (cached_wd != NULL)
20560 return cached_wd;
20562 wd = get_src_pwd ();
20563 if (wd == NULL)
20564 return NULL;
20566 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20568 int wdlen;
20570 wdlen = strlen (wd);
20571 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20572 strcpy (wd1, wd);
20573 wd1 [wdlen] = DIR_SEPARATOR;
20574 wd1 [wdlen + 1] = 0;
20575 wd = wd1;
20578 cached_wd = remap_debug_filename (wd);
20579 return cached_wd;
20582 /* Generate a DW_AT_comp_dir attribute for DIE. */
20584 static void
20585 add_comp_dir_attribute (dw_die_ref die)
20587 const char * wd = comp_dir_string ();
20588 if (wd != NULL)
20589 add_AT_string (die, DW_AT_comp_dir, wd);
20592 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20593 pointer computation, ...), output a representation for that bound according
20594 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20595 loc_list_from_tree for the meaning of CONTEXT. */
20597 static void
20598 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20599 int forms, struct loc_descr_context *context)
20601 dw_die_ref context_die, decl_die = NULL;
20602 dw_loc_list_ref list;
20603 bool strip_conversions = true;
20604 bool placeholder_seen = false;
20606 while (strip_conversions)
20607 switch (TREE_CODE (value))
20609 case ERROR_MARK:
20610 case SAVE_EXPR:
20611 return;
20613 CASE_CONVERT:
20614 case VIEW_CONVERT_EXPR:
20615 value = TREE_OPERAND (value, 0);
20616 break;
20618 default:
20619 strip_conversions = false;
20620 break;
20623 /* If possible and permitted, output the attribute as a constant. */
20624 if ((forms & dw_scalar_form_constant) != 0
20625 && TREE_CODE (value) == INTEGER_CST)
20627 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20629 /* If HOST_WIDE_INT is big enough then represent the bound as
20630 a constant value. We need to choose a form based on
20631 whether the type is signed or unsigned. We cannot just
20632 call add_AT_unsigned if the value itself is positive
20633 (add_AT_unsigned might add the unsigned value encoded as
20634 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20635 bounds type and then sign extend any unsigned values found
20636 for signed types. This is needed only for
20637 DW_AT_{lower,upper}_bound, since for most other attributes,
20638 consumers will treat DW_FORM_data[1248] as unsigned values,
20639 regardless of the underlying type. */
20640 if (prec <= HOST_BITS_PER_WIDE_INT
20641 || tree_fits_uhwi_p (value))
20643 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20644 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20645 else
20646 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20648 else
20649 /* Otherwise represent the bound as an unsigned value with
20650 the precision of its type. The precision and signedness
20651 of the type will be necessary to re-interpret it
20652 unambiguously. */
20653 add_AT_wide (die, attr, wi::to_wide (value));
20654 return;
20657 /* Otherwise, if it's possible and permitted too, output a reference to
20658 another DIE. */
20659 if ((forms & dw_scalar_form_reference) != 0)
20661 tree decl = NULL_TREE;
20663 /* Some type attributes reference an outer type. For instance, the upper
20664 bound of an array may reference an embedding record (this happens in
20665 Ada). */
20666 if (TREE_CODE (value) == COMPONENT_REF
20667 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20668 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20669 decl = TREE_OPERAND (value, 1);
20671 else if (VAR_P (value)
20672 || TREE_CODE (value) == PARM_DECL
20673 || TREE_CODE (value) == RESULT_DECL)
20674 decl = value;
20676 if (decl != NULL_TREE)
20678 decl_die = lookup_decl_die (decl);
20680 /* ??? Can this happen, or should the variable have been bound
20681 first? Probably it can, since I imagine that we try to create
20682 the types of parameters in the order in which they exist in
20683 the list, and won't have created a forward reference to a
20684 later parameter. */
20685 if (decl_die != NULL)
20687 if (get_AT (decl_die, DW_AT_location)
20688 || get_AT (decl_die, DW_AT_const_value))
20690 add_AT_die_ref (die, attr, decl_die);
20691 return;
20697 /* Last chance: try to create a stack operation procedure to evaluate the
20698 value. Do nothing if even that is not possible or permitted. */
20699 if ((forms & dw_scalar_form_exprloc) == 0)
20700 return;
20702 list = loc_list_from_tree (value, 2, context);
20703 if (context && context->placeholder_arg)
20705 placeholder_seen = context->placeholder_seen;
20706 context->placeholder_seen = false;
20708 if (list == NULL || single_element_loc_list_p (list))
20710 /* If this attribute is not a reference nor constant, it is
20711 a DWARF expression rather than location description. For that
20712 loc_list_from_tree (value, 0, &context) is needed. */
20713 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20714 if (list2 && single_element_loc_list_p (list2))
20716 if (placeholder_seen)
20718 struct dwarf_procedure_info dpi;
20719 dpi.fndecl = NULL_TREE;
20720 dpi.args_count = 1;
20721 if (!resolve_args_picking (list2->expr, 1, &dpi))
20722 return;
20724 add_AT_loc (die, attr, list2->expr);
20725 return;
20729 /* If that failed to give a single element location list, fall back to
20730 outputting this as a reference... still if permitted. */
20731 if (list == NULL
20732 || (forms & dw_scalar_form_reference) == 0
20733 || placeholder_seen)
20734 return;
20736 if (!decl_die)
20738 if (current_function_decl == 0)
20739 context_die = comp_unit_die ();
20740 else
20741 context_die = lookup_decl_die (current_function_decl);
20743 decl_die = new_die (DW_TAG_variable, context_die, value);
20744 add_AT_flag (decl_die, DW_AT_artificial, 1);
20745 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20746 context_die);
20749 add_AT_location_description (decl_die, DW_AT_location, list);
20750 add_AT_die_ref (die, attr, decl_die);
20753 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20754 default. */
20756 static int
20757 lower_bound_default (void)
20759 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20761 case DW_LANG_C:
20762 case DW_LANG_C89:
20763 case DW_LANG_C99:
20764 case DW_LANG_C11:
20765 case DW_LANG_C_plus_plus:
20766 case DW_LANG_C_plus_plus_11:
20767 case DW_LANG_C_plus_plus_14:
20768 case DW_LANG_ObjC:
20769 case DW_LANG_ObjC_plus_plus:
20770 return 0;
20771 case DW_LANG_Fortran77:
20772 case DW_LANG_Fortran90:
20773 case DW_LANG_Fortran95:
20774 case DW_LANG_Fortran03:
20775 case DW_LANG_Fortran08:
20776 return 1;
20777 case DW_LANG_UPC:
20778 case DW_LANG_D:
20779 case DW_LANG_Python:
20780 return dwarf_version >= 4 ? 0 : -1;
20781 case DW_LANG_Ada95:
20782 case DW_LANG_Ada83:
20783 case DW_LANG_Cobol74:
20784 case DW_LANG_Cobol85:
20785 case DW_LANG_Modula2:
20786 case DW_LANG_PLI:
20787 return dwarf_version >= 4 ? 1 : -1;
20788 default:
20789 return -1;
20793 /* Given a tree node describing an array bound (either lower or upper) output
20794 a representation for that bound. */
20796 static void
20797 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20798 tree bound, struct loc_descr_context *context)
20800 int dflt;
20802 while (1)
20803 switch (TREE_CODE (bound))
20805 /* Strip all conversions. */
20806 CASE_CONVERT:
20807 case VIEW_CONVERT_EXPR:
20808 bound = TREE_OPERAND (bound, 0);
20809 break;
20811 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20812 are even omitted when they are the default. */
20813 case INTEGER_CST:
20814 /* If the value for this bound is the default one, we can even omit the
20815 attribute. */
20816 if (bound_attr == DW_AT_lower_bound
20817 && tree_fits_shwi_p (bound)
20818 && (dflt = lower_bound_default ()) != -1
20819 && tree_to_shwi (bound) == dflt)
20820 return;
20822 /* FALLTHRU */
20824 default:
20825 /* Because of the complex interaction there can be with other GNAT
20826 encodings, GDB isn't ready yet to handle proper DWARF description
20827 for self-referencial subrange bounds: let GNAT encodings do the
20828 magic in such a case. */
20829 if (is_ada ()
20830 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20831 && contains_placeholder_p (bound))
20832 return;
20834 add_scalar_info (subrange_die, bound_attr, bound,
20835 dw_scalar_form_constant
20836 | dw_scalar_form_exprloc
20837 | dw_scalar_form_reference,
20838 context);
20839 return;
20843 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20844 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20845 Note that the block of subscript information for an array type also
20846 includes information about the element type of the given array type.
20848 This function reuses previously set type and bound information if
20849 available. */
20851 static void
20852 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20854 unsigned dimension_number;
20855 tree lower, upper;
20856 dw_die_ref child = type_die->die_child;
20858 for (dimension_number = 0;
20859 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20860 type = TREE_TYPE (type), dimension_number++)
20862 tree domain = TYPE_DOMAIN (type);
20864 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20865 break;
20867 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20868 and (in GNU C only) variable bounds. Handle all three forms
20869 here. */
20871 /* Find and reuse a previously generated DW_TAG_subrange_type if
20872 available.
20874 For multi-dimensional arrays, as we iterate through the
20875 various dimensions in the enclosing for loop above, we also
20876 iterate through the DIE children and pick at each
20877 DW_TAG_subrange_type previously generated (if available).
20878 Each child DW_TAG_subrange_type DIE describes the range of
20879 the current dimension. At this point we should have as many
20880 DW_TAG_subrange_type's as we have dimensions in the
20881 array. */
20882 dw_die_ref subrange_die = NULL;
20883 if (child)
20884 while (1)
20886 child = child->die_sib;
20887 if (child->die_tag == DW_TAG_subrange_type)
20888 subrange_die = child;
20889 if (child == type_die->die_child)
20891 /* If we wrapped around, stop looking next time. */
20892 child = NULL;
20893 break;
20895 if (child->die_tag == DW_TAG_subrange_type)
20896 break;
20898 if (!subrange_die)
20899 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20901 if (domain)
20903 /* We have an array type with specified bounds. */
20904 lower = TYPE_MIN_VALUE (domain);
20905 upper = TYPE_MAX_VALUE (domain);
20907 /* Define the index type. */
20908 if (TREE_TYPE (domain)
20909 && !get_AT (subrange_die, DW_AT_type))
20911 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20912 TREE_TYPE field. We can't emit debug info for this
20913 because it is an unnamed integral type. */
20914 if (TREE_CODE (domain) == INTEGER_TYPE
20915 && TYPE_NAME (domain) == NULL_TREE
20916 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20917 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20919 else
20920 add_type_attribute (subrange_die, TREE_TYPE (domain),
20921 TYPE_UNQUALIFIED, false, type_die);
20924 /* ??? If upper is NULL, the array has unspecified length,
20925 but it does have a lower bound. This happens with Fortran
20926 dimension arr(N:*)
20927 Since the debugger is definitely going to need to know N
20928 to produce useful results, go ahead and output the lower
20929 bound solo, and hope the debugger can cope. */
20931 if (!get_AT (subrange_die, DW_AT_lower_bound))
20932 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20933 if (!get_AT (subrange_die, DW_AT_upper_bound)
20934 && !get_AT (subrange_die, DW_AT_count))
20936 if (upper)
20937 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20938 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
20939 /* Zero-length array. */
20940 add_bound_info (subrange_die, DW_AT_count,
20941 build_int_cst (TREE_TYPE (lower), 0), NULL);
20945 /* Otherwise we have an array type with an unspecified length. The
20946 DWARF-2 spec does not say how to handle this; let's just leave out the
20947 bounds. */
20951 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20953 static void
20954 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20956 dw_die_ref decl_die;
20957 HOST_WIDE_INT size;
20958 dw_loc_descr_ref size_expr = NULL;
20960 switch (TREE_CODE (tree_node))
20962 case ERROR_MARK:
20963 size = 0;
20964 break;
20965 case ENUMERAL_TYPE:
20966 case RECORD_TYPE:
20967 case UNION_TYPE:
20968 case QUAL_UNION_TYPE:
20969 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20970 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20972 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20973 return;
20975 size_expr = type_byte_size (tree_node, &size);
20976 break;
20977 case FIELD_DECL:
20978 /* For a data member of a struct or union, the DW_AT_byte_size is
20979 generally given as the number of bytes normally allocated for an
20980 object of the *declared* type of the member itself. This is true
20981 even for bit-fields. */
20982 size = int_size_in_bytes (field_type (tree_node));
20983 break;
20984 default:
20985 gcc_unreachable ();
20988 /* Support for dynamically-sized objects was introduced by DWARFv3.
20989 At the moment, GDB does not handle variable byte sizes very well,
20990 though. */
20991 if ((dwarf_version >= 3 || !dwarf_strict)
20992 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20993 && size_expr != NULL)
20994 add_AT_loc (die, DW_AT_byte_size, size_expr);
20996 /* Note that `size' might be -1 when we get to this point. If it is, that
20997 indicates that the byte size of the entity in question is variable and
20998 that we could not generate a DWARF expression that computes it. */
20999 if (size >= 0)
21000 add_AT_unsigned (die, DW_AT_byte_size, size);
21003 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21004 alignment. */
21006 static void
21007 add_alignment_attribute (dw_die_ref die, tree tree_node)
21009 if (dwarf_version < 5 && dwarf_strict)
21010 return;
21012 unsigned align;
21014 if (DECL_P (tree_node))
21016 if (!DECL_USER_ALIGN (tree_node))
21017 return;
21019 align = DECL_ALIGN_UNIT (tree_node);
21021 else if (TYPE_P (tree_node))
21023 if (!TYPE_USER_ALIGN (tree_node))
21024 return;
21026 align = TYPE_ALIGN_UNIT (tree_node);
21028 else
21029 gcc_unreachable ();
21031 add_AT_unsigned (die, DW_AT_alignment, align);
21034 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21035 which specifies the distance in bits from the highest order bit of the
21036 "containing object" for the bit-field to the highest order bit of the
21037 bit-field itself.
21039 For any given bit-field, the "containing object" is a hypothetical object
21040 (of some integral or enum type) within which the given bit-field lives. The
21041 type of this hypothetical "containing object" is always the same as the
21042 declared type of the individual bit-field itself. The determination of the
21043 exact location of the "containing object" for a bit-field is rather
21044 complicated. It's handled by the `field_byte_offset' function (above).
21046 CTX is required: see the comment for VLR_CONTEXT.
21048 Note that it is the size (in bytes) of the hypothetical "containing object"
21049 which will be given in the DW_AT_byte_size attribute for this bit-field.
21050 (See `byte_size_attribute' above). */
21052 static inline void
21053 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
21055 HOST_WIDE_INT object_offset_in_bytes;
21056 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21057 HOST_WIDE_INT bitpos_int;
21058 HOST_WIDE_INT highest_order_object_bit_offset;
21059 HOST_WIDE_INT highest_order_field_bit_offset;
21060 HOST_WIDE_INT bit_offset;
21062 field_byte_offset (decl, ctx, &object_offset_in_bytes);
21064 /* Must be a field and a bit field. */
21065 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21067 /* We can't yet handle bit-fields whose offsets are variable, so if we
21068 encounter such things, just return without generating any attribute
21069 whatsoever. Likewise for variable or too large size. */
21070 if (! tree_fits_shwi_p (bit_position (decl))
21071 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21072 return;
21074 bitpos_int = int_bit_position (decl);
21076 /* Note that the bit offset is always the distance (in bits) from the
21077 highest-order bit of the "containing object" to the highest-order bit of
21078 the bit-field itself. Since the "high-order end" of any object or field
21079 is different on big-endian and little-endian machines, the computation
21080 below must take account of these differences. */
21081 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21082 highest_order_field_bit_offset = bitpos_int;
21084 if (! BYTES_BIG_ENDIAN)
21086 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21087 highest_order_object_bit_offset +=
21088 simple_type_size_in_bits (original_type);
21091 bit_offset
21092 = (! BYTES_BIG_ENDIAN
21093 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21094 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21096 if (bit_offset < 0)
21097 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21098 else
21099 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21102 /* For a FIELD_DECL node which represents a bit field, output an attribute
21103 which specifies the length in bits of the given field. */
21105 static inline void
21106 add_bit_size_attribute (dw_die_ref die, tree decl)
21108 /* Must be a field and a bit field. */
21109 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21110 && DECL_BIT_FIELD_TYPE (decl));
21112 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21113 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21116 /* If the compiled language is ANSI C, then add a 'prototyped'
21117 attribute, if arg types are given for the parameters of a function. */
21119 static inline void
21120 add_prototyped_attribute (dw_die_ref die, tree func_type)
21122 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21124 case DW_LANG_C:
21125 case DW_LANG_C89:
21126 case DW_LANG_C99:
21127 case DW_LANG_C11:
21128 case DW_LANG_ObjC:
21129 if (prototype_p (func_type))
21130 add_AT_flag (die, DW_AT_prototyped, 1);
21131 break;
21132 default:
21133 break;
21137 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21138 by looking in the type declaration, the object declaration equate table or
21139 the block mapping. */
21141 static inline void
21142 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21144 dw_die_ref origin_die = NULL;
21146 /* For late LTO debug output we want to refer directly to the abstract
21147 DIE in the early debug rather to the possibly existing concrete
21148 instance and avoid creating that just for this purpose. */
21149 sym_off_pair *desc;
21150 if (in_lto_p
21151 && external_die_map
21152 && (desc = external_die_map->get (origin)))
21154 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21155 desc->sym, desc->off);
21156 return;
21159 if (DECL_P (origin))
21160 origin_die = lookup_decl_die (origin);
21161 else if (TYPE_P (origin))
21162 origin_die = lookup_type_die (origin);
21163 else if (TREE_CODE (origin) == BLOCK)
21164 origin_die = lookup_block_die (origin);
21166 /* XXX: Functions that are never lowered don't always have correct block
21167 trees (in the case of java, they simply have no block tree, in some other
21168 languages). For these functions, there is nothing we can really do to
21169 output correct debug info for inlined functions in all cases. Rather
21170 than die, we'll just produce deficient debug info now, in that we will
21171 have variables without a proper abstract origin. In the future, when all
21172 functions are lowered, we should re-add a gcc_assert (origin_die)
21173 here. */
21175 if (origin_die)
21176 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21179 /* We do not currently support the pure_virtual attribute. */
21181 static inline void
21182 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21184 if (DECL_VINDEX (func_decl))
21186 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21188 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21189 add_AT_loc (die, DW_AT_vtable_elem_location,
21190 new_loc_descr (DW_OP_constu,
21191 tree_to_shwi (DECL_VINDEX (func_decl)),
21192 0));
21194 /* GNU extension: Record what type this method came from originally. */
21195 if (debug_info_level > DINFO_LEVEL_TERSE
21196 && DECL_CONTEXT (func_decl))
21197 add_AT_die_ref (die, DW_AT_containing_type,
21198 lookup_type_die (DECL_CONTEXT (func_decl)));
21202 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21203 given decl. This used to be a vendor extension until after DWARF 4
21204 standardized it. */
21206 static void
21207 add_linkage_attr (dw_die_ref die, tree decl)
21209 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21211 /* Mimic what assemble_name_raw does with a leading '*'. */
21212 if (name[0] == '*')
21213 name = &name[1];
21215 if (dwarf_version >= 4)
21216 add_AT_string (die, DW_AT_linkage_name, name);
21217 else
21218 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21221 /* Add source coordinate attributes for the given decl. */
21223 static void
21224 add_src_coords_attributes (dw_die_ref die, tree decl)
21226 expanded_location s;
21228 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21229 return;
21230 s = expand_location (DECL_SOURCE_LOCATION (decl));
21231 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21232 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21233 if (debug_column_info && s.column)
21234 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21237 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21239 static void
21240 add_linkage_name_raw (dw_die_ref die, tree decl)
21242 /* Defer until we have an assembler name set. */
21243 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21245 limbo_die_node *asm_name;
21247 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21248 asm_name->die = die;
21249 asm_name->created_for = decl;
21250 asm_name->next = deferred_asm_name;
21251 deferred_asm_name = asm_name;
21253 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21254 add_linkage_attr (die, decl);
21257 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21259 static void
21260 add_linkage_name (dw_die_ref die, tree decl)
21262 if (debug_info_level > DINFO_LEVEL_NONE
21263 && VAR_OR_FUNCTION_DECL_P (decl)
21264 && TREE_PUBLIC (decl)
21265 && !(VAR_P (decl) && DECL_REGISTER (decl))
21266 && die->die_tag != DW_TAG_member)
21267 add_linkage_name_raw (die, decl);
21270 /* Add a DW_AT_name attribute and source coordinate attribute for the
21271 given decl, but only if it actually has a name. */
21273 static void
21274 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21275 bool no_linkage_name)
21277 tree decl_name;
21279 decl_name = DECL_NAME (decl);
21280 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21282 const char *name = dwarf2_name (decl, 0);
21283 if (name)
21284 add_name_attribute (die, name);
21285 else
21286 add_desc_attribute (die, decl);
21288 if (! DECL_ARTIFICIAL (decl))
21289 add_src_coords_attributes (die, decl);
21291 if (!no_linkage_name)
21292 add_linkage_name (die, decl);
21294 else
21295 add_desc_attribute (die, decl);
21297 #ifdef VMS_DEBUGGING_INFO
21298 /* Get the function's name, as described by its RTL. This may be different
21299 from the DECL_NAME name used in the source file. */
21300 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21302 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21303 XEXP (DECL_RTL (decl), 0), false);
21304 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21306 #endif /* VMS_DEBUGGING_INFO */
21309 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21311 static void
21312 add_discr_value (dw_die_ref die, dw_discr_value *value)
21314 dw_attr_node attr;
21316 attr.dw_attr = DW_AT_discr_value;
21317 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21318 attr.dw_attr_val.val_entry = NULL;
21319 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21320 if (value->pos)
21321 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21322 else
21323 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21324 add_dwarf_attr (die, &attr);
21327 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21329 static void
21330 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21332 dw_attr_node attr;
21334 attr.dw_attr = DW_AT_discr_list;
21335 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21336 attr.dw_attr_val.val_entry = NULL;
21337 attr.dw_attr_val.v.val_discr_list = discr_list;
21338 add_dwarf_attr (die, &attr);
21341 static inline dw_discr_list_ref
21342 AT_discr_list (dw_attr_node *attr)
21344 return attr->dw_attr_val.v.val_discr_list;
21347 #ifdef VMS_DEBUGGING_INFO
21348 /* Output the debug main pointer die for VMS */
21350 void
21351 dwarf2out_vms_debug_main_pointer (void)
21353 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21354 dw_die_ref die;
21356 /* Allocate the VMS debug main subprogram die. */
21357 die = new_die_raw (DW_TAG_subprogram);
21358 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21359 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21360 current_function_funcdef_no);
21361 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21363 /* Make it the first child of comp_unit_die (). */
21364 die->die_parent = comp_unit_die ();
21365 if (comp_unit_die ()->die_child)
21367 die->die_sib = comp_unit_die ()->die_child->die_sib;
21368 comp_unit_die ()->die_child->die_sib = die;
21370 else
21372 die->die_sib = die;
21373 comp_unit_die ()->die_child = die;
21376 #endif /* VMS_DEBUGGING_INFO */
21378 /* walk_tree helper function for uses_local_type, below. */
21380 static tree
21381 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21383 if (!TYPE_P (*tp))
21384 *walk_subtrees = 0;
21385 else
21387 tree name = TYPE_NAME (*tp);
21388 if (name && DECL_P (name) && decl_function_context (name))
21389 return *tp;
21391 return NULL_TREE;
21394 /* If TYPE involves a function-local type (including a local typedef to a
21395 non-local type), returns that type; otherwise returns NULL_TREE. */
21397 static tree
21398 uses_local_type (tree type)
21400 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21401 return used;
21404 /* Return the DIE for the scope that immediately contains this type.
21405 Non-named types that do not involve a function-local type get global
21406 scope. Named types nested in namespaces or other types get their
21407 containing scope. All other types (i.e. function-local named types) get
21408 the current active scope. */
21410 static dw_die_ref
21411 scope_die_for (tree t, dw_die_ref context_die)
21413 dw_die_ref scope_die = NULL;
21414 tree containing_scope;
21416 /* Non-types always go in the current scope. */
21417 gcc_assert (TYPE_P (t));
21419 /* Use the scope of the typedef, rather than the scope of the type
21420 it refers to. */
21421 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21422 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21423 else
21424 containing_scope = TYPE_CONTEXT (t);
21426 /* Use the containing namespace if there is one. */
21427 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21429 if (context_die == lookup_decl_die (containing_scope))
21430 /* OK */;
21431 else if (debug_info_level > DINFO_LEVEL_TERSE)
21432 context_die = get_context_die (containing_scope);
21433 else
21434 containing_scope = NULL_TREE;
21437 /* Ignore function type "scopes" from the C frontend. They mean that
21438 a tagged type is local to a parmlist of a function declarator, but
21439 that isn't useful to DWARF. */
21440 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21441 containing_scope = NULL_TREE;
21443 if (SCOPE_FILE_SCOPE_P (containing_scope))
21445 /* If T uses a local type keep it local as well, to avoid references
21446 to function-local DIEs from outside the function. */
21447 if (current_function_decl && uses_local_type (t))
21448 scope_die = context_die;
21449 else
21450 scope_die = comp_unit_die ();
21452 else if (TYPE_P (containing_scope))
21454 /* For types, we can just look up the appropriate DIE. */
21455 if (debug_info_level > DINFO_LEVEL_TERSE)
21456 scope_die = get_context_die (containing_scope);
21457 else
21459 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21460 if (scope_die == NULL)
21461 scope_die = comp_unit_die ();
21464 else
21465 scope_die = context_die;
21467 return scope_die;
21470 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21472 static inline int
21473 local_scope_p (dw_die_ref context_die)
21475 for (; context_die; context_die = context_die->die_parent)
21476 if (context_die->die_tag == DW_TAG_inlined_subroutine
21477 || context_die->die_tag == DW_TAG_subprogram)
21478 return 1;
21480 return 0;
21483 /* Returns nonzero if CONTEXT_DIE is a class. */
21485 static inline int
21486 class_scope_p (dw_die_ref context_die)
21488 return (context_die
21489 && (context_die->die_tag == DW_TAG_structure_type
21490 || context_die->die_tag == DW_TAG_class_type
21491 || context_die->die_tag == DW_TAG_interface_type
21492 || context_die->die_tag == DW_TAG_union_type));
21495 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21496 whether or not to treat a DIE in this context as a declaration. */
21498 static inline int
21499 class_or_namespace_scope_p (dw_die_ref context_die)
21501 return (class_scope_p (context_die)
21502 || (context_die && context_die->die_tag == DW_TAG_namespace));
21505 /* Many forms of DIEs require a "type description" attribute. This
21506 routine locates the proper "type descriptor" die for the type given
21507 by 'type' plus any additional qualifiers given by 'cv_quals', and
21508 adds a DW_AT_type attribute below the given die. */
21510 static void
21511 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21512 bool reverse, dw_die_ref context_die)
21514 enum tree_code code = TREE_CODE (type);
21515 dw_die_ref type_die = NULL;
21517 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21518 or fixed-point type, use the inner type. This is because we have no
21519 support for unnamed types in base_type_die. This can happen if this is
21520 an Ada subrange type. Correct solution is emit a subrange type die. */
21521 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21522 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21523 type = TREE_TYPE (type), code = TREE_CODE (type);
21525 if (code == ERROR_MARK
21526 /* Handle a special case. For functions whose return type is void, we
21527 generate *no* type attribute. (Note that no object may have type
21528 `void', so this only applies to function return types). */
21529 || code == VOID_TYPE)
21530 return;
21532 type_die = modified_type_die (type,
21533 cv_quals | TYPE_QUALS (type),
21534 reverse,
21535 context_die);
21537 if (type_die != NULL)
21538 add_AT_die_ref (object_die, DW_AT_type, type_die);
21541 /* Given an object die, add the calling convention attribute for the
21542 function call type. */
21543 static void
21544 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21546 enum dwarf_calling_convention value = DW_CC_normal;
21548 value = ((enum dwarf_calling_convention)
21549 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21551 if (is_fortran ()
21552 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21554 /* DWARF 2 doesn't provide a way to identify a program's source-level
21555 entry point. DW_AT_calling_convention attributes are only meant
21556 to describe functions' calling conventions. However, lacking a
21557 better way to signal the Fortran main program, we used this for
21558 a long time, following existing custom. Now, DWARF 4 has
21559 DW_AT_main_subprogram, which we add below, but some tools still
21560 rely on the old way, which we thus keep. */
21561 value = DW_CC_program;
21563 if (dwarf_version >= 4 || !dwarf_strict)
21564 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21567 /* Only add the attribute if the backend requests it, and
21568 is not DW_CC_normal. */
21569 if (value && (value != DW_CC_normal))
21570 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21573 /* Given a tree pointer to a struct, class, union, or enum type node, return
21574 a pointer to the (string) tag name for the given type, or zero if the type
21575 was declared without a tag. */
21577 static const char *
21578 type_tag (const_tree type)
21580 const char *name = 0;
21582 if (TYPE_NAME (type) != 0)
21584 tree t = 0;
21586 /* Find the IDENTIFIER_NODE for the type name. */
21587 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21588 && !TYPE_NAMELESS (type))
21589 t = TYPE_NAME (type);
21591 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21592 a TYPE_DECL node, regardless of whether or not a `typedef' was
21593 involved. */
21594 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21595 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21597 /* We want to be extra verbose. Don't call dwarf_name if
21598 DECL_NAME isn't set. The default hook for decl_printable_name
21599 doesn't like that, and in this context it's correct to return
21600 0, instead of "<anonymous>" or the like. */
21601 if (DECL_NAME (TYPE_NAME (type))
21602 && !DECL_NAMELESS (TYPE_NAME (type)))
21603 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21606 /* Now get the name as a string, or invent one. */
21607 if (!name && t != 0)
21608 name = IDENTIFIER_POINTER (t);
21611 return (name == 0 || *name == '\0') ? 0 : name;
21614 /* Return the type associated with a data member, make a special check
21615 for bit field types. */
21617 static inline tree
21618 member_declared_type (const_tree member)
21620 return (DECL_BIT_FIELD_TYPE (member)
21621 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21624 /* Get the decl's label, as described by its RTL. This may be different
21625 from the DECL_NAME name used in the source file. */
21627 #if 0
21628 static const char *
21629 decl_start_label (tree decl)
21631 rtx x;
21632 const char *fnname;
21634 x = DECL_RTL (decl);
21635 gcc_assert (MEM_P (x));
21637 x = XEXP (x, 0);
21638 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21640 fnname = XSTR (x, 0);
21641 return fnname;
21643 #endif
21645 /* For variable-length arrays that have been previously generated, but
21646 may be incomplete due to missing subscript info, fill the subscript
21647 info. Return TRUE if this is one of those cases. */
21648 static bool
21649 fill_variable_array_bounds (tree type)
21651 if (TREE_ASM_WRITTEN (type)
21652 && TREE_CODE (type) == ARRAY_TYPE
21653 && variably_modified_type_p (type, NULL))
21655 dw_die_ref array_die = lookup_type_die (type);
21656 if (!array_die)
21657 return false;
21658 add_subscript_info (array_die, type, !is_ada ());
21659 return true;
21661 return false;
21664 /* These routines generate the internal representation of the DIE's for
21665 the compilation unit. Debugging information is collected by walking
21666 the declaration trees passed in from dwarf2out_decl(). */
21668 static void
21669 gen_array_type_die (tree type, dw_die_ref context_die)
21671 dw_die_ref array_die;
21673 /* GNU compilers represent multidimensional array types as sequences of one
21674 dimensional array types whose element types are themselves array types.
21675 We sometimes squish that down to a single array_type DIE with multiple
21676 subscripts in the Dwarf debugging info. The draft Dwarf specification
21677 say that we are allowed to do this kind of compression in C, because
21678 there is no difference between an array of arrays and a multidimensional
21679 array. We don't do this for Ada to remain as close as possible to the
21680 actual representation, which is especially important against the language
21681 flexibilty wrt arrays of variable size. */
21683 bool collapse_nested_arrays = !is_ada ();
21685 if (fill_variable_array_bounds (type))
21686 return;
21688 dw_die_ref scope_die = scope_die_for (type, context_die);
21689 tree element_type;
21691 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21692 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21693 if (TYPE_STRING_FLAG (type)
21694 && TREE_CODE (type) == ARRAY_TYPE
21695 && is_fortran ()
21696 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21698 HOST_WIDE_INT size;
21700 array_die = new_die (DW_TAG_string_type, scope_die, type);
21701 add_name_attribute (array_die, type_tag (type));
21702 equate_type_number_to_die (type, array_die);
21703 size = int_size_in_bytes (type);
21704 if (size >= 0)
21705 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21706 /* ??? We can't annotate types late, but for LTO we may not
21707 generate a location early either (gfortran.dg/save_6.f90). */
21708 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21709 && TYPE_DOMAIN (type) != NULL_TREE
21710 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21712 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21713 tree rszdecl = szdecl;
21715 size = int_size_in_bytes (TREE_TYPE (szdecl));
21716 if (!DECL_P (szdecl))
21718 if (TREE_CODE (szdecl) == INDIRECT_REF
21719 && DECL_P (TREE_OPERAND (szdecl, 0)))
21721 rszdecl = TREE_OPERAND (szdecl, 0);
21722 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21723 != DWARF2_ADDR_SIZE)
21724 size = 0;
21726 else
21727 size = 0;
21729 if (size > 0)
21731 dw_loc_list_ref loc
21732 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21733 NULL);
21734 if (loc)
21736 add_AT_location_description (array_die, DW_AT_string_length,
21737 loc);
21738 if (size != DWARF2_ADDR_SIZE)
21739 add_AT_unsigned (array_die, dwarf_version >= 5
21740 ? DW_AT_string_length_byte_size
21741 : DW_AT_byte_size, size);
21745 return;
21748 array_die = new_die (DW_TAG_array_type, scope_die, type);
21749 add_name_attribute (array_die, type_tag (type));
21750 equate_type_number_to_die (type, array_die);
21752 if (TREE_CODE (type) == VECTOR_TYPE)
21753 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21755 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21756 if (is_fortran ()
21757 && TREE_CODE (type) == ARRAY_TYPE
21758 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21759 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21760 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21762 #if 0
21763 /* We default the array ordering. Debuggers will probably do the right
21764 things even if DW_AT_ordering is not present. It's not even an issue
21765 until we start to get into multidimensional arrays anyway. If a debugger
21766 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21767 then we'll have to put the DW_AT_ordering attribute back in. (But if
21768 and when we find out that we need to put these in, we will only do so
21769 for multidimensional arrays. */
21770 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21771 #endif
21773 if (TREE_CODE (type) == VECTOR_TYPE)
21775 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21776 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21777 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21778 add_bound_info (subrange_die, DW_AT_upper_bound,
21779 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21781 else
21782 add_subscript_info (array_die, type, collapse_nested_arrays);
21784 /* Add representation of the type of the elements of this array type and
21785 emit the corresponding DIE if we haven't done it already. */
21786 element_type = TREE_TYPE (type);
21787 if (collapse_nested_arrays)
21788 while (TREE_CODE (element_type) == ARRAY_TYPE)
21790 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21791 break;
21792 element_type = TREE_TYPE (element_type);
21795 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21796 TREE_CODE (type) == ARRAY_TYPE
21797 && TYPE_REVERSE_STORAGE_ORDER (type),
21798 context_die);
21800 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21801 if (TYPE_ARTIFICIAL (type))
21802 add_AT_flag (array_die, DW_AT_artificial, 1);
21804 if (get_AT (array_die, DW_AT_name))
21805 add_pubtype (type, array_die);
21807 add_alignment_attribute (array_die, type);
21810 /* This routine generates DIE for array with hidden descriptor, details
21811 are filled into *info by a langhook. */
21813 static void
21814 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21815 dw_die_ref context_die)
21817 const dw_die_ref scope_die = scope_die_for (type, context_die);
21818 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21819 struct loc_descr_context context = { type, info->base_decl, NULL,
21820 false, false };
21821 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21822 int dim;
21824 add_name_attribute (array_die, type_tag (type));
21825 equate_type_number_to_die (type, array_die);
21827 if (info->ndimensions > 1)
21828 switch (info->ordering)
21830 case array_descr_ordering_row_major:
21831 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21832 break;
21833 case array_descr_ordering_column_major:
21834 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21835 break;
21836 default:
21837 break;
21840 if (dwarf_version >= 3 || !dwarf_strict)
21842 if (info->data_location)
21843 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21844 dw_scalar_form_exprloc, &context);
21845 if (info->associated)
21846 add_scalar_info (array_die, DW_AT_associated, info->associated,
21847 dw_scalar_form_constant
21848 | dw_scalar_form_exprloc
21849 | dw_scalar_form_reference, &context);
21850 if (info->allocated)
21851 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21852 dw_scalar_form_constant
21853 | dw_scalar_form_exprloc
21854 | dw_scalar_form_reference, &context);
21855 if (info->stride)
21857 const enum dwarf_attribute attr
21858 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21859 const int forms
21860 = (info->stride_in_bits)
21861 ? dw_scalar_form_constant
21862 : (dw_scalar_form_constant
21863 | dw_scalar_form_exprloc
21864 | dw_scalar_form_reference);
21866 add_scalar_info (array_die, attr, info->stride, forms, &context);
21869 if (dwarf_version >= 5)
21871 if (info->rank)
21873 add_scalar_info (array_die, DW_AT_rank, info->rank,
21874 dw_scalar_form_constant
21875 | dw_scalar_form_exprloc, &context);
21876 subrange_tag = DW_TAG_generic_subrange;
21877 context.placeholder_arg = true;
21881 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21883 for (dim = 0; dim < info->ndimensions; dim++)
21885 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21887 if (info->dimen[dim].bounds_type)
21888 add_type_attribute (subrange_die,
21889 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21890 false, context_die);
21891 if (info->dimen[dim].lower_bound)
21892 add_bound_info (subrange_die, DW_AT_lower_bound,
21893 info->dimen[dim].lower_bound, &context);
21894 if (info->dimen[dim].upper_bound)
21895 add_bound_info (subrange_die, DW_AT_upper_bound,
21896 info->dimen[dim].upper_bound, &context);
21897 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21898 add_scalar_info (subrange_die, DW_AT_byte_stride,
21899 info->dimen[dim].stride,
21900 dw_scalar_form_constant
21901 | dw_scalar_form_exprloc
21902 | dw_scalar_form_reference,
21903 &context);
21906 gen_type_die (info->element_type, context_die);
21907 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21908 TREE_CODE (type) == ARRAY_TYPE
21909 && TYPE_REVERSE_STORAGE_ORDER (type),
21910 context_die);
21912 if (get_AT (array_die, DW_AT_name))
21913 add_pubtype (type, array_die);
21915 add_alignment_attribute (array_die, type);
21918 #if 0
21919 static void
21920 gen_entry_point_die (tree decl, dw_die_ref context_die)
21922 tree origin = decl_ultimate_origin (decl);
21923 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21925 if (origin != NULL)
21926 add_abstract_origin_attribute (decl_die, origin);
21927 else
21929 add_name_and_src_coords_attributes (decl_die, decl);
21930 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21931 TYPE_UNQUALIFIED, false, context_die);
21934 if (DECL_ABSTRACT_P (decl))
21935 equate_decl_number_to_die (decl, decl_die);
21936 else
21937 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21939 #endif
21941 /* Walk through the list of incomplete types again, trying once more to
21942 emit full debugging info for them. */
21944 static void
21945 retry_incomplete_types (void)
21947 set_early_dwarf s;
21948 int i;
21950 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21951 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21952 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21953 vec_safe_truncate (incomplete_types, 0);
21956 /* Determine what tag to use for a record type. */
21958 static enum dwarf_tag
21959 record_type_tag (tree type)
21961 if (! lang_hooks.types.classify_record)
21962 return DW_TAG_structure_type;
21964 switch (lang_hooks.types.classify_record (type))
21966 case RECORD_IS_STRUCT:
21967 return DW_TAG_structure_type;
21969 case RECORD_IS_CLASS:
21970 return DW_TAG_class_type;
21972 case RECORD_IS_INTERFACE:
21973 if (dwarf_version >= 3 || !dwarf_strict)
21974 return DW_TAG_interface_type;
21975 return DW_TAG_structure_type;
21977 default:
21978 gcc_unreachable ();
21982 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21983 include all of the information about the enumeration values also. Each
21984 enumerated type name/value is listed as a child of the enumerated type
21985 DIE. */
21987 static dw_die_ref
21988 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21990 dw_die_ref type_die = lookup_type_die (type);
21991 dw_die_ref orig_type_die = type_die;
21993 if (type_die == NULL)
21995 type_die = new_die (DW_TAG_enumeration_type,
21996 scope_die_for (type, context_die), type);
21997 equate_type_number_to_die (type, type_die);
21998 add_name_attribute (type_die, type_tag (type));
21999 if ((dwarf_version >= 4 || !dwarf_strict)
22000 && ENUM_IS_SCOPED (type))
22001 add_AT_flag (type_die, DW_AT_enum_class, 1);
22002 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22003 add_AT_flag (type_die, DW_AT_declaration, 1);
22004 if (!dwarf_strict)
22005 add_AT_unsigned (type_die, DW_AT_encoding,
22006 TYPE_UNSIGNED (type)
22007 ? DW_ATE_unsigned
22008 : DW_ATE_signed);
22010 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22011 return type_die;
22012 else
22013 remove_AT (type_die, DW_AT_declaration);
22015 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22016 given enum type is incomplete, do not generate the DW_AT_byte_size
22017 attribute or the DW_AT_element_list attribute. */
22018 if (TYPE_SIZE (type))
22020 tree link;
22022 if (!ENUM_IS_OPAQUE (type))
22023 TREE_ASM_WRITTEN (type) = 1;
22024 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22025 add_byte_size_attribute (type_die, type);
22026 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22027 add_alignment_attribute (type_die, type);
22028 if ((dwarf_version >= 3 || !dwarf_strict)
22029 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22031 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22032 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22033 context_die);
22035 if (TYPE_STUB_DECL (type) != NULL_TREE)
22037 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22038 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22039 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22040 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22043 /* If the first reference to this type was as the return type of an
22044 inline function, then it may not have a parent. Fix this now. */
22045 if (type_die->die_parent == NULL)
22046 add_child_die (scope_die_for (type, context_die), type_die);
22048 for (link = TYPE_VALUES (type);
22049 link != NULL; link = TREE_CHAIN (link))
22051 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22052 tree value = TREE_VALUE (link);
22054 gcc_assert (!ENUM_IS_OPAQUE (type));
22055 add_name_attribute (enum_die,
22056 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22058 if (TREE_CODE (value) == CONST_DECL)
22059 value = DECL_INITIAL (value);
22061 if (simple_type_size_in_bits (TREE_TYPE (value))
22062 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22064 /* For constant forms created by add_AT_unsigned DWARF
22065 consumers (GDB, elfutils, etc.) always zero extend
22066 the value. Only when the actual value is negative
22067 do we need to use add_AT_int to generate a constant
22068 form that can represent negative values. */
22069 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22070 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22071 add_AT_unsigned (enum_die, DW_AT_const_value,
22072 (unsigned HOST_WIDE_INT) val);
22073 else
22074 add_AT_int (enum_die, DW_AT_const_value, val);
22076 else
22077 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22078 that here. TODO: This should be re-worked to use correct
22079 signed/unsigned double tags for all cases. */
22080 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22083 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22084 if (TYPE_ARTIFICIAL (type)
22085 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22086 add_AT_flag (type_die, DW_AT_artificial, 1);
22088 else
22089 add_AT_flag (type_die, DW_AT_declaration, 1);
22091 add_pubtype (type, type_die);
22093 return type_die;
22096 /* Generate a DIE to represent either a real live formal parameter decl or to
22097 represent just the type of some formal parameter position in some function
22098 type.
22100 Note that this routine is a bit unusual because its argument may be a
22101 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22102 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22103 node. If it's the former then this function is being called to output a
22104 DIE to represent a formal parameter object (or some inlining thereof). If
22105 it's the latter, then this function is only being called to output a
22106 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22107 argument type of some subprogram type.
22108 If EMIT_NAME_P is true, name and source coordinate attributes
22109 are emitted. */
22111 static dw_die_ref
22112 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22113 dw_die_ref context_die)
22115 tree node_or_origin = node ? node : origin;
22116 tree ultimate_origin;
22117 dw_die_ref parm_die = NULL;
22119 if (DECL_P (node_or_origin))
22121 parm_die = lookup_decl_die (node);
22123 /* If the contexts differ, we may not be talking about the same
22124 thing.
22125 ??? When in LTO the DIE parent is the "abstract" copy and the
22126 context_die is the specification "copy". But this whole block
22127 should eventually be no longer needed. */
22128 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
22130 if (!DECL_ABSTRACT_P (node))
22132 /* This can happen when creating an inlined instance, in
22133 which case we need to create a new DIE that will get
22134 annotated with DW_AT_abstract_origin. */
22135 parm_die = NULL;
22137 else
22138 gcc_unreachable ();
22141 if (parm_die && parm_die->die_parent == NULL)
22143 /* Check that parm_die already has the right attributes that
22144 we would have added below. If any attributes are
22145 missing, fall through to add them. */
22146 if (! DECL_ABSTRACT_P (node_or_origin)
22147 && !get_AT (parm_die, DW_AT_location)
22148 && !get_AT (parm_die, DW_AT_const_value))
22149 /* We are missing location info, and are about to add it. */
22151 else
22153 add_child_die (context_die, parm_die);
22154 return parm_die;
22159 /* If we have a previously generated DIE, use it, unless this is an
22160 concrete instance (origin != NULL), in which case we need a new
22161 DIE with a corresponding DW_AT_abstract_origin. */
22162 bool reusing_die;
22163 if (parm_die && origin == NULL)
22164 reusing_die = true;
22165 else
22167 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22168 reusing_die = false;
22171 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22173 case tcc_declaration:
22174 ultimate_origin = decl_ultimate_origin (node_or_origin);
22175 if (node || ultimate_origin)
22176 origin = ultimate_origin;
22178 if (reusing_die)
22179 goto add_location;
22181 if (origin != NULL)
22182 add_abstract_origin_attribute (parm_die, origin);
22183 else if (emit_name_p)
22184 add_name_and_src_coords_attributes (parm_die, node);
22185 if (origin == NULL
22186 || (! DECL_ABSTRACT_P (node_or_origin)
22187 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22188 decl_function_context
22189 (node_or_origin))))
22191 tree type = TREE_TYPE (node_or_origin);
22192 if (decl_by_reference_p (node_or_origin))
22193 add_type_attribute (parm_die, TREE_TYPE (type),
22194 TYPE_UNQUALIFIED,
22195 false, context_die);
22196 else
22197 add_type_attribute (parm_die, type,
22198 decl_quals (node_or_origin),
22199 false, context_die);
22201 if (origin == NULL && DECL_ARTIFICIAL (node))
22202 add_AT_flag (parm_die, DW_AT_artificial, 1);
22203 add_location:
22204 if (node && node != origin)
22205 equate_decl_number_to_die (node, parm_die);
22206 if (! DECL_ABSTRACT_P (node_or_origin))
22207 add_location_or_const_value_attribute (parm_die, node_or_origin,
22208 node == NULL);
22210 break;
22212 case tcc_type:
22213 /* We were called with some kind of a ..._TYPE node. */
22214 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22215 context_die);
22216 break;
22218 default:
22219 gcc_unreachable ();
22222 return parm_die;
22225 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22226 children DW_TAG_formal_parameter DIEs representing the arguments of the
22227 parameter pack.
22229 PARM_PACK must be a function parameter pack.
22230 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22231 must point to the subsequent arguments of the function PACK_ARG belongs to.
22232 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22233 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22234 following the last one for which a DIE was generated. */
22236 static dw_die_ref
22237 gen_formal_parameter_pack_die (tree parm_pack,
22238 tree pack_arg,
22239 dw_die_ref subr_die,
22240 tree *next_arg)
22242 tree arg;
22243 dw_die_ref parm_pack_die;
22245 gcc_assert (parm_pack
22246 && lang_hooks.function_parameter_pack_p (parm_pack)
22247 && subr_die);
22249 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22250 add_src_coords_attributes (parm_pack_die, parm_pack);
22252 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22254 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22255 parm_pack))
22256 break;
22257 gen_formal_parameter_die (arg, NULL,
22258 false /* Don't emit name attribute. */,
22259 parm_pack_die);
22261 if (next_arg)
22262 *next_arg = arg;
22263 return parm_pack_die;
22266 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22267 at the end of an (ANSI prototyped) formal parameters list. */
22269 static void
22270 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22272 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22275 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22276 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22277 parameters as specified in some function type specification (except for
22278 those which appear as part of a function *definition*). */
22280 static void
22281 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22283 tree link;
22284 tree formal_type = NULL;
22285 tree first_parm_type;
22286 tree arg;
22288 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22290 arg = DECL_ARGUMENTS (function_or_method_type);
22291 function_or_method_type = TREE_TYPE (function_or_method_type);
22293 else
22294 arg = NULL_TREE;
22296 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22298 /* Make our first pass over the list of formal parameter types and output a
22299 DW_TAG_formal_parameter DIE for each one. */
22300 for (link = first_parm_type; link; )
22302 dw_die_ref parm_die;
22304 formal_type = TREE_VALUE (link);
22305 if (formal_type == void_type_node)
22306 break;
22308 /* Output a (nameless) DIE to represent the formal parameter itself. */
22309 parm_die = gen_formal_parameter_die (formal_type, NULL,
22310 true /* Emit name attribute. */,
22311 context_die);
22312 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22313 && link == first_parm_type)
22315 add_AT_flag (parm_die, DW_AT_artificial, 1);
22316 if (dwarf_version >= 3 || !dwarf_strict)
22317 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22319 else if (arg && DECL_ARTIFICIAL (arg))
22320 add_AT_flag (parm_die, DW_AT_artificial, 1);
22322 link = TREE_CHAIN (link);
22323 if (arg)
22324 arg = DECL_CHAIN (arg);
22327 /* If this function type has an ellipsis, add a
22328 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22329 if (formal_type != void_type_node)
22330 gen_unspecified_parameters_die (function_or_method_type, context_die);
22332 /* Make our second (and final) pass over the list of formal parameter types
22333 and output DIEs to represent those types (as necessary). */
22334 for (link = TYPE_ARG_TYPES (function_or_method_type);
22335 link && TREE_VALUE (link);
22336 link = TREE_CHAIN (link))
22337 gen_type_die (TREE_VALUE (link), context_die);
22340 /* We want to generate the DIE for TYPE so that we can generate the
22341 die for MEMBER, which has been defined; we will need to refer back
22342 to the member declaration nested within TYPE. If we're trying to
22343 generate minimal debug info for TYPE, processing TYPE won't do the
22344 trick; we need to attach the member declaration by hand. */
22346 static void
22347 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22349 gen_type_die (type, context_die);
22351 /* If we're trying to avoid duplicate debug info, we may not have
22352 emitted the member decl for this function. Emit it now. */
22353 if (TYPE_STUB_DECL (type)
22354 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22355 && ! lookup_decl_die (member))
22357 dw_die_ref type_die;
22358 gcc_assert (!decl_ultimate_origin (member));
22360 type_die = lookup_type_die_strip_naming_typedef (type);
22361 if (TREE_CODE (member) == FUNCTION_DECL)
22362 gen_subprogram_die (member, type_die);
22363 else if (TREE_CODE (member) == FIELD_DECL)
22365 /* Ignore the nameless fields that are used to skip bits but handle
22366 C++ anonymous unions and structs. */
22367 if (DECL_NAME (member) != NULL_TREE
22368 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22369 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22371 struct vlr_context vlr_ctx = {
22372 DECL_CONTEXT (member), /* struct_type */
22373 NULL_TREE /* variant_part_offset */
22375 gen_type_die (member_declared_type (member), type_die);
22376 gen_field_die (member, &vlr_ctx, type_die);
22379 else
22380 gen_variable_die (member, NULL_TREE, type_die);
22384 /* Forward declare these functions, because they are mutually recursive
22385 with their set_block_* pairing functions. */
22386 static void set_decl_origin_self (tree);
22388 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22389 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22390 that it points to the node itself, thus indicating that the node is its
22391 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22392 the given node is NULL, recursively descend the decl/block tree which
22393 it is the root of, and for each other ..._DECL or BLOCK node contained
22394 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22395 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22396 values to point to themselves. */
22398 static void
22399 set_block_origin_self (tree stmt)
22401 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22403 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22406 tree local_decl;
22408 for (local_decl = BLOCK_VARS (stmt);
22409 local_decl != NULL_TREE;
22410 local_decl = DECL_CHAIN (local_decl))
22411 /* Do not recurse on nested functions since the inlining status
22412 of parent and child can be different as per the DWARF spec. */
22413 if (TREE_CODE (local_decl) != FUNCTION_DECL
22414 && !DECL_EXTERNAL (local_decl))
22415 set_decl_origin_self (local_decl);
22419 tree subblock;
22421 for (subblock = BLOCK_SUBBLOCKS (stmt);
22422 subblock != NULL_TREE;
22423 subblock = BLOCK_CHAIN (subblock))
22424 set_block_origin_self (subblock); /* Recurse. */
22429 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22430 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22431 node to so that it points to the node itself, thus indicating that the
22432 node represents its own (abstract) origin. Additionally, if the
22433 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22434 the decl/block tree of which the given node is the root of, and for
22435 each other ..._DECL or BLOCK node contained therein whose
22436 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22437 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22438 point to themselves. */
22440 static void
22441 set_decl_origin_self (tree decl)
22443 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22445 DECL_ABSTRACT_ORIGIN (decl) = decl;
22446 if (TREE_CODE (decl) == FUNCTION_DECL)
22448 tree arg;
22450 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22451 DECL_ABSTRACT_ORIGIN (arg) = arg;
22452 if (DECL_INITIAL (decl) != NULL_TREE
22453 && DECL_INITIAL (decl) != error_mark_node)
22454 set_block_origin_self (DECL_INITIAL (decl));
22459 /* Mark the early DIE for DECL as the abstract instance. */
22461 static void
22462 dwarf2out_abstract_function (tree decl)
22464 dw_die_ref old_die;
22466 /* Make sure we have the actual abstract inline, not a clone. */
22467 decl = DECL_ORIGIN (decl);
22469 if (DECL_IGNORED_P (decl))
22470 return;
22472 /* In LTO we're all set. We already created abstract instances
22473 early and we want to avoid creating a concrete instance of that
22474 if we don't output it. */
22475 if (in_lto_p)
22476 return;
22478 old_die = lookup_decl_die (decl);
22479 gcc_assert (old_die != NULL);
22480 if (get_AT (old_die, DW_AT_inline))
22481 /* We've already generated the abstract instance. */
22482 return;
22484 /* Go ahead and put DW_AT_inline on the DIE. */
22485 if (DECL_DECLARED_INLINE_P (decl))
22487 if (cgraph_function_possibly_inlined_p (decl))
22488 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22489 else
22490 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22492 else
22494 if (cgraph_function_possibly_inlined_p (decl))
22495 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22496 else
22497 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22500 if (DECL_DECLARED_INLINE_P (decl)
22501 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22502 add_AT_flag (old_die, DW_AT_artificial, 1);
22504 set_decl_origin_self (decl);
22507 /* Helper function of premark_used_types() which gets called through
22508 htab_traverse.
22510 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22511 marked as unused by prune_unused_types. */
22513 bool
22514 premark_used_types_helper (tree const &type, void *)
22516 dw_die_ref die;
22518 die = lookup_type_die (type);
22519 if (die != NULL)
22520 die->die_perennial_p = 1;
22521 return true;
22524 /* Helper function of premark_types_used_by_global_vars which gets called
22525 through htab_traverse.
22527 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22528 marked as unused by prune_unused_types. The DIE of the type is marked
22529 only if the global variable using the type will actually be emitted. */
22532 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22533 void *)
22535 struct types_used_by_vars_entry *entry;
22536 dw_die_ref die;
22538 entry = (struct types_used_by_vars_entry *) *slot;
22539 gcc_assert (entry->type != NULL
22540 && entry->var_decl != NULL);
22541 die = lookup_type_die (entry->type);
22542 if (die)
22544 /* Ask cgraph if the global variable really is to be emitted.
22545 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22546 varpool_node *node = varpool_node::get (entry->var_decl);
22547 if (node && node->definition)
22549 die->die_perennial_p = 1;
22550 /* Keep the parent DIEs as well. */
22551 while ((die = die->die_parent) && die->die_perennial_p == 0)
22552 die->die_perennial_p = 1;
22555 return 1;
22558 /* Mark all members of used_types_hash as perennial. */
22560 static void
22561 premark_used_types (struct function *fun)
22563 if (fun && fun->used_types_hash)
22564 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22567 /* Mark all members of types_used_by_vars_entry as perennial. */
22569 static void
22570 premark_types_used_by_global_vars (void)
22572 if (types_used_by_vars_hash)
22573 types_used_by_vars_hash
22574 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22577 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22578 for CA_LOC call arg loc node. */
22580 static dw_die_ref
22581 gen_call_site_die (tree decl, dw_die_ref subr_die,
22582 struct call_arg_loc_node *ca_loc)
22584 dw_die_ref stmt_die = NULL, die;
22585 tree block = ca_loc->block;
22587 while (block
22588 && block != DECL_INITIAL (decl)
22589 && TREE_CODE (block) == BLOCK)
22591 stmt_die = lookup_block_die (block);
22592 if (stmt_die)
22593 break;
22594 block = BLOCK_SUPERCONTEXT (block);
22596 if (stmt_die == NULL)
22597 stmt_die = subr_die;
22598 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22599 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22600 if (ca_loc->tail_call_p)
22601 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22602 if (ca_loc->symbol_ref)
22604 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22605 if (tdie)
22606 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22607 else
22608 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22609 false);
22611 return die;
22614 /* Generate a DIE to represent a declared function (either file-scope or
22615 block-local). */
22617 static void
22618 gen_subprogram_die (tree decl, dw_die_ref context_die)
22620 tree origin = decl_ultimate_origin (decl);
22621 dw_die_ref subr_die;
22622 dw_die_ref old_die = lookup_decl_die (decl);
22624 /* This function gets called multiple times for different stages of
22625 the debug process. For example, for func() in this code:
22627 namespace S
22629 void func() { ... }
22632 ...we get called 4 times. Twice in early debug and twice in
22633 late debug:
22635 Early debug
22636 -----------
22638 1. Once while generating func() within the namespace. This is
22639 the declaration. The declaration bit below is set, as the
22640 context is the namespace.
22642 A new DIE will be generated with DW_AT_declaration set.
22644 2. Once for func() itself. This is the specification. The
22645 declaration bit below is clear as the context is the CU.
22647 We will use the cached DIE from (1) to create a new DIE with
22648 DW_AT_specification pointing to the declaration in (1).
22650 Late debug via rest_of_handle_final()
22651 -------------------------------------
22653 3. Once generating func() within the namespace. This is also the
22654 declaration, as in (1), but this time we will early exit below
22655 as we have a cached DIE and a declaration needs no additional
22656 annotations (no locations), as the source declaration line
22657 info is enough.
22659 4. Once for func() itself. As in (2), this is the specification,
22660 but this time we will re-use the cached DIE, and just annotate
22661 it with the location information that should now be available.
22663 For something without namespaces, but with abstract instances, we
22664 are also called a multiple times:
22666 class Base
22668 public:
22669 Base (); // constructor declaration (1)
22672 Base::Base () { } // constructor specification (2)
22674 Early debug
22675 -----------
22677 1. Once for the Base() constructor by virtue of it being a
22678 member of the Base class. This is done via
22679 rest_of_type_compilation.
22681 This is a declaration, so a new DIE will be created with
22682 DW_AT_declaration.
22684 2. Once for the Base() constructor definition, but this time
22685 while generating the abstract instance of the base
22686 constructor (__base_ctor) which is being generated via early
22687 debug of reachable functions.
22689 Even though we have a cached version of the declaration (1),
22690 we will create a DW_AT_specification of the declaration DIE
22691 in (1).
22693 3. Once for the __base_ctor itself, but this time, we generate
22694 an DW_AT_abstract_origin version of the DW_AT_specification in
22695 (2).
22697 Late debug via rest_of_handle_final
22698 -----------------------------------
22700 4. One final time for the __base_ctor (which will have a cached
22701 DIE with DW_AT_abstract_origin created in (3). This time,
22702 we will just annotate the location information now
22703 available.
22705 int declaration = (current_function_decl != decl
22706 || class_or_namespace_scope_p (context_die));
22708 /* A declaration that has been previously dumped needs no
22709 additional information. */
22710 if (old_die && declaration)
22711 return;
22713 /* Now that the C++ front end lazily declares artificial member fns, we
22714 might need to retrofit the declaration into its class. */
22715 if (!declaration && !origin && !old_die
22716 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22717 && !class_or_namespace_scope_p (context_die)
22718 && debug_info_level > DINFO_LEVEL_TERSE)
22719 old_die = force_decl_die (decl);
22721 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22722 if (origin != NULL)
22724 gcc_assert (!declaration || local_scope_p (context_die));
22726 /* Fixup die_parent for the abstract instance of a nested
22727 inline function. */
22728 if (old_die && old_die->die_parent == NULL)
22729 add_child_die (context_die, old_die);
22731 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22733 /* If we have a DW_AT_abstract_origin we have a working
22734 cached version. */
22735 subr_die = old_die;
22737 else
22739 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22740 add_abstract_origin_attribute (subr_die, origin);
22741 /* This is where the actual code for a cloned function is.
22742 Let's emit linkage name attribute for it. This helps
22743 debuggers to e.g, set breakpoints into
22744 constructors/destructors when the user asks "break
22745 K::K". */
22746 add_linkage_name (subr_die, decl);
22749 /* A cached copy, possibly from early dwarf generation. Reuse as
22750 much as possible. */
22751 else if (old_die)
22753 if (!get_AT_flag (old_die, DW_AT_declaration)
22754 /* We can have a normal definition following an inline one in the
22755 case of redefinition of GNU C extern inlines.
22756 It seems reasonable to use AT_specification in this case. */
22757 && !get_AT (old_die, DW_AT_inline))
22759 /* Detect and ignore this case, where we are trying to output
22760 something we have already output. */
22761 if (get_AT (old_die, DW_AT_low_pc)
22762 || get_AT (old_die, DW_AT_ranges))
22763 return;
22765 /* If we have no location information, this must be a
22766 partially generated DIE from early dwarf generation.
22767 Fall through and generate it. */
22770 /* If the definition comes from the same place as the declaration,
22771 maybe use the old DIE. We always want the DIE for this function
22772 that has the *_pc attributes to be under comp_unit_die so the
22773 debugger can find it. We also need to do this for abstract
22774 instances of inlines, since the spec requires the out-of-line copy
22775 to have the same parent. For local class methods, this doesn't
22776 apply; we just use the old DIE. */
22777 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22778 struct dwarf_file_data * file_index = lookup_filename (s.file);
22779 if (((is_unit_die (old_die->die_parent)
22780 /* This condition fixes the inconsistency/ICE with the
22781 following Fortran test (or some derivative thereof) while
22782 building libgfortran:
22784 module some_m
22785 contains
22786 logical function funky (FLAG)
22787 funky = .true.
22788 end function
22789 end module
22791 || (old_die->die_parent
22792 && old_die->die_parent->die_tag == DW_TAG_module)
22793 || local_scope_p (old_die->die_parent)
22794 || context_die == NULL)
22795 && (DECL_ARTIFICIAL (decl)
22796 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22797 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22798 == (unsigned) s.line)
22799 && (!debug_column_info
22800 || s.column == 0
22801 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22802 == (unsigned) s.column)))))
22803 /* With LTO if there's an abstract instance for
22804 the old DIE, this is a concrete instance and
22805 thus re-use the DIE. */
22806 || get_AT (old_die, DW_AT_abstract_origin))
22808 subr_die = old_die;
22810 /* Clear out the declaration attribute, but leave the
22811 parameters so they can be augmented with location
22812 information later. Unless this was a declaration, in
22813 which case, wipe out the nameless parameters and recreate
22814 them further down. */
22815 if (remove_AT (subr_die, DW_AT_declaration))
22818 remove_AT (subr_die, DW_AT_object_pointer);
22819 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22822 /* Make a specification pointing to the previously built
22823 declaration. */
22824 else
22826 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22827 add_AT_specification (subr_die, old_die);
22828 add_pubname (decl, subr_die);
22829 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22830 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22831 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22832 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22833 if (debug_column_info
22834 && s.column
22835 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22836 != (unsigned) s.column))
22837 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22839 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22840 emit the real type on the definition die. */
22841 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22843 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22844 if (die == auto_die || die == decltype_auto_die)
22845 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22846 TYPE_UNQUALIFIED, false, context_die);
22849 /* When we process the method declaration, we haven't seen
22850 the out-of-class defaulted definition yet, so we have to
22851 recheck now. */
22852 if ((dwarf_version >= 5 || ! dwarf_strict)
22853 && !get_AT (subr_die, DW_AT_defaulted))
22855 int defaulted
22856 = lang_hooks.decls.decl_dwarf_attribute (decl,
22857 DW_AT_defaulted);
22858 if (defaulted != -1)
22860 /* Other values must have been handled before. */
22861 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22862 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22867 /* Create a fresh DIE for anything else. */
22868 else
22870 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22872 if (TREE_PUBLIC (decl))
22873 add_AT_flag (subr_die, DW_AT_external, 1);
22875 add_name_and_src_coords_attributes (subr_die, decl);
22876 add_pubname (decl, subr_die);
22877 if (debug_info_level > DINFO_LEVEL_TERSE)
22879 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22880 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22881 TYPE_UNQUALIFIED, false, context_die);
22884 add_pure_or_virtual_attribute (subr_die, decl);
22885 if (DECL_ARTIFICIAL (decl))
22886 add_AT_flag (subr_die, DW_AT_artificial, 1);
22888 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22889 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22891 add_alignment_attribute (subr_die, decl);
22893 add_accessibility_attribute (subr_die, decl);
22896 /* Unless we have an existing non-declaration DIE, equate the new
22897 DIE. */
22898 if (!old_die || is_declaration_die (old_die))
22899 equate_decl_number_to_die (decl, subr_die);
22901 if (declaration)
22903 if (!old_die || !get_AT (old_die, DW_AT_inline))
22905 add_AT_flag (subr_die, DW_AT_declaration, 1);
22907 /* If this is an explicit function declaration then generate
22908 a DW_AT_explicit attribute. */
22909 if ((dwarf_version >= 3 || !dwarf_strict)
22910 && lang_hooks.decls.decl_dwarf_attribute (decl,
22911 DW_AT_explicit) == 1)
22912 add_AT_flag (subr_die, DW_AT_explicit, 1);
22914 /* If this is a C++11 deleted special function member then generate
22915 a DW_AT_deleted attribute. */
22916 if ((dwarf_version >= 5 || !dwarf_strict)
22917 && lang_hooks.decls.decl_dwarf_attribute (decl,
22918 DW_AT_deleted) == 1)
22919 add_AT_flag (subr_die, DW_AT_deleted, 1);
22921 /* If this is a C++11 defaulted special function member then
22922 generate a DW_AT_defaulted attribute. */
22923 if (dwarf_version >= 5 || !dwarf_strict)
22925 int defaulted
22926 = lang_hooks.decls.decl_dwarf_attribute (decl,
22927 DW_AT_defaulted);
22928 if (defaulted != -1)
22929 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22932 /* If this is a C++11 non-static member function with & ref-qualifier
22933 then generate a DW_AT_reference attribute. */
22934 if ((dwarf_version >= 5 || !dwarf_strict)
22935 && lang_hooks.decls.decl_dwarf_attribute (decl,
22936 DW_AT_reference) == 1)
22937 add_AT_flag (subr_die, DW_AT_reference, 1);
22939 /* If this is a C++11 non-static member function with &&
22940 ref-qualifier then generate a DW_AT_reference attribute. */
22941 if ((dwarf_version >= 5 || !dwarf_strict)
22942 && lang_hooks.decls.decl_dwarf_attribute (decl,
22943 DW_AT_rvalue_reference)
22944 == 1)
22945 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22948 /* For non DECL_EXTERNALs, if range information is available, fill
22949 the DIE with it. */
22950 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22952 HOST_WIDE_INT cfa_fb_offset;
22954 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22956 if (!crtl->has_bb_partition)
22958 dw_fde_ref fde = fun->fde;
22959 if (fde->dw_fde_begin)
22961 /* We have already generated the labels. */
22962 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22963 fde->dw_fde_end, false);
22965 else
22967 /* Create start/end labels and add the range. */
22968 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22969 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22970 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22971 current_function_funcdef_no);
22972 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22973 current_function_funcdef_no);
22974 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22975 false);
22978 #if VMS_DEBUGGING_INFO
22979 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22980 Section 2.3 Prologue and Epilogue Attributes:
22981 When a breakpoint is set on entry to a function, it is generally
22982 desirable for execution to be suspended, not on the very first
22983 instruction of the function, but rather at a point after the
22984 function's frame has been set up, after any language defined local
22985 declaration processing has been completed, and before execution of
22986 the first statement of the function begins. Debuggers generally
22987 cannot properly determine where this point is. Similarly for a
22988 breakpoint set on exit from a function. The prologue and epilogue
22989 attributes allow a compiler to communicate the location(s) to use. */
22992 if (fde->dw_fde_vms_end_prologue)
22993 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22994 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22996 if (fde->dw_fde_vms_begin_epilogue)
22997 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22998 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
23000 #endif
23003 else
23005 /* Generate pubnames entries for the split function code ranges. */
23006 dw_fde_ref fde = fun->fde;
23008 if (fde->dw_fde_second_begin)
23010 if (dwarf_version >= 3 || !dwarf_strict)
23012 /* We should use ranges for non-contiguous code section
23013 addresses. Use the actual code range for the initial
23014 section, since the HOT/COLD labels might precede an
23015 alignment offset. */
23016 bool range_list_added = false;
23017 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23018 fde->dw_fde_end, &range_list_added,
23019 false);
23020 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23021 fde->dw_fde_second_end,
23022 &range_list_added, false);
23023 if (range_list_added)
23024 add_ranges (NULL);
23026 else
23028 /* There is no real support in DW2 for this .. so we make
23029 a work-around. First, emit the pub name for the segment
23030 containing the function label. Then make and emit a
23031 simplified subprogram DIE for the second segment with the
23032 name pre-fixed by __hot/cold_sect_of_. We use the same
23033 linkage name for the second die so that gdb will find both
23034 sections when given "b foo". */
23035 const char *name = NULL;
23036 tree decl_name = DECL_NAME (decl);
23037 dw_die_ref seg_die;
23039 /* Do the 'primary' section. */
23040 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23041 fde->dw_fde_end, false);
23043 /* Build a minimal DIE for the secondary section. */
23044 seg_die = new_die (DW_TAG_subprogram,
23045 subr_die->die_parent, decl);
23047 if (TREE_PUBLIC (decl))
23048 add_AT_flag (seg_die, DW_AT_external, 1);
23050 if (decl_name != NULL
23051 && IDENTIFIER_POINTER (decl_name) != NULL)
23053 name = dwarf2_name (decl, 1);
23054 if (! DECL_ARTIFICIAL (decl))
23055 add_src_coords_attributes (seg_die, decl);
23057 add_linkage_name (seg_die, decl);
23059 gcc_assert (name != NULL);
23060 add_pure_or_virtual_attribute (seg_die, decl);
23061 if (DECL_ARTIFICIAL (decl))
23062 add_AT_flag (seg_die, DW_AT_artificial, 1);
23064 name = concat ("__second_sect_of_", name, NULL);
23065 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23066 fde->dw_fde_second_end, false);
23067 add_name_attribute (seg_die, name);
23068 if (want_pubnames ())
23069 add_pubname_string (name, seg_die);
23072 else
23073 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23074 false);
23077 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23079 /* We define the "frame base" as the function's CFA. This is more
23080 convenient for several reasons: (1) It's stable across the prologue
23081 and epilogue, which makes it better than just a frame pointer,
23082 (2) With dwarf3, there exists a one-byte encoding that allows us
23083 to reference the .debug_frame data by proxy, but failing that,
23084 (3) We can at least reuse the code inspection and interpretation
23085 code that determines the CFA position at various points in the
23086 function. */
23087 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23089 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23090 add_AT_loc (subr_die, DW_AT_frame_base, op);
23092 else
23094 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23095 if (list->dw_loc_next)
23096 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23097 else
23098 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23101 /* Compute a displacement from the "steady-state frame pointer" to
23102 the CFA. The former is what all stack slots and argument slots
23103 will reference in the rtl; the latter is what we've told the
23104 debugger about. We'll need to adjust all frame_base references
23105 by this displacement. */
23106 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23108 if (fun->static_chain_decl)
23110 /* DWARF requires here a location expression that computes the
23111 address of the enclosing subprogram's frame base. The machinery
23112 in tree-nested.c is supposed to store this specific address in the
23113 last field of the FRAME record. */
23114 const tree frame_type
23115 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23116 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23118 tree fb_expr
23119 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23120 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23121 fb_expr, fb_decl, NULL_TREE);
23123 add_AT_location_description (subr_die, DW_AT_static_link,
23124 loc_list_from_tree (fb_expr, 0, NULL));
23127 resolve_variable_values ();
23130 /* Generate child dies for template paramaters. */
23131 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23132 gen_generic_params_dies (decl);
23134 /* Now output descriptions of the arguments for this function. This gets
23135 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23136 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23137 `...' at the end of the formal parameter list. In order to find out if
23138 there was a trailing ellipsis or not, we must instead look at the type
23139 associated with the FUNCTION_DECL. This will be a node of type
23140 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23141 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23142 an ellipsis at the end. */
23144 /* In the case where we are describing a mere function declaration, all we
23145 need to do here (and all we *can* do here) is to describe the *types* of
23146 its formal parameters. */
23147 if (debug_info_level <= DINFO_LEVEL_TERSE)
23149 else if (declaration)
23150 gen_formal_types_die (decl, subr_die);
23151 else
23153 /* Generate DIEs to represent all known formal parameters. */
23154 tree parm = DECL_ARGUMENTS (decl);
23155 tree generic_decl = early_dwarf
23156 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23157 tree generic_decl_parm = generic_decl
23158 ? DECL_ARGUMENTS (generic_decl)
23159 : NULL;
23161 /* Now we want to walk the list of parameters of the function and
23162 emit their relevant DIEs.
23164 We consider the case of DECL being an instance of a generic function
23165 as well as it being a normal function.
23167 If DECL is an instance of a generic function we walk the
23168 parameters of the generic function declaration _and_ the parameters of
23169 DECL itself. This is useful because we want to emit specific DIEs for
23170 function parameter packs and those are declared as part of the
23171 generic function declaration. In that particular case,
23172 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23173 That DIE has children DIEs representing the set of arguments
23174 of the pack. Note that the set of pack arguments can be empty.
23175 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23176 children DIE.
23178 Otherwise, we just consider the parameters of DECL. */
23179 while (generic_decl_parm || parm)
23181 if (generic_decl_parm
23182 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23183 gen_formal_parameter_pack_die (generic_decl_parm,
23184 parm, subr_die,
23185 &parm);
23186 else if (parm)
23188 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23190 if (early_dwarf
23191 && parm == DECL_ARGUMENTS (decl)
23192 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23193 && parm_die
23194 && (dwarf_version >= 3 || !dwarf_strict))
23195 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23197 parm = DECL_CHAIN (parm);
23199 else if (parm)
23200 parm = DECL_CHAIN (parm);
23202 if (generic_decl_parm)
23203 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23206 /* Decide whether we need an unspecified_parameters DIE at the end.
23207 There are 2 more cases to do this for: 1) the ansi ... declaration -
23208 this is detectable when the end of the arg list is not a
23209 void_type_node 2) an unprototyped function declaration (not a
23210 definition). This just means that we have no info about the
23211 parameters at all. */
23212 if (early_dwarf)
23214 if (prototype_p (TREE_TYPE (decl)))
23216 /* This is the prototyped case, check for.... */
23217 if (stdarg_p (TREE_TYPE (decl)))
23218 gen_unspecified_parameters_die (decl, subr_die);
23220 else if (DECL_INITIAL (decl) == NULL_TREE)
23221 gen_unspecified_parameters_die (decl, subr_die);
23225 if (subr_die != old_die)
23226 /* Add the calling convention attribute if requested. */
23227 add_calling_convention_attribute (subr_die, decl);
23229 /* Output Dwarf info for all of the stuff within the body of the function
23230 (if it has one - it may be just a declaration).
23232 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23233 a function. This BLOCK actually represents the outermost binding contour
23234 for the function, i.e. the contour in which the function's formal
23235 parameters and labels get declared. Curiously, it appears that the front
23236 end doesn't actually put the PARM_DECL nodes for the current function onto
23237 the BLOCK_VARS list for this outer scope, but are strung off of the
23238 DECL_ARGUMENTS list for the function instead.
23240 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23241 the LABEL_DECL nodes for the function however, and we output DWARF info
23242 for those in decls_for_scope. Just within the `outer_scope' there will be
23243 a BLOCK node representing the function's outermost pair of curly braces,
23244 and any blocks used for the base and member initializers of a C++
23245 constructor function. */
23246 tree outer_scope = DECL_INITIAL (decl);
23247 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23249 int call_site_note_count = 0;
23250 int tail_call_site_note_count = 0;
23252 /* Emit a DW_TAG_variable DIE for a named return value. */
23253 if (DECL_NAME (DECL_RESULT (decl)))
23254 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23256 /* The first time through decls_for_scope we will generate the
23257 DIEs for the locals. The second time, we fill in the
23258 location info. */
23259 decls_for_scope (outer_scope, subr_die);
23261 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23263 struct call_arg_loc_node *ca_loc;
23264 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23266 dw_die_ref die = NULL;
23267 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23268 rtx arg, next_arg;
23269 tree arg_decl = NULL_TREE;
23271 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23272 ? XEXP (ca_loc->call_arg_loc_note, 0)
23273 : NULL_RTX);
23274 arg; arg = next_arg)
23276 dw_loc_descr_ref reg, val;
23277 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23278 dw_die_ref cdie, tdie = NULL;
23280 next_arg = XEXP (arg, 1);
23281 if (REG_P (XEXP (XEXP (arg, 0), 0))
23282 && next_arg
23283 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23284 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23285 && REGNO (XEXP (XEXP (arg, 0), 0))
23286 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23287 next_arg = XEXP (next_arg, 1);
23288 if (mode == VOIDmode)
23290 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23291 if (mode == VOIDmode)
23292 mode = GET_MODE (XEXP (arg, 0));
23294 if (mode == VOIDmode || mode == BLKmode)
23295 continue;
23296 /* Get dynamic information about call target only if we
23297 have no static information: we cannot generate both
23298 DW_AT_call_origin and DW_AT_call_target
23299 attributes. */
23300 if (ca_loc->symbol_ref == NULL_RTX)
23302 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23304 tloc = XEXP (XEXP (arg, 0), 1);
23305 continue;
23307 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23308 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23310 tlocc = XEXP (XEXP (arg, 0), 1);
23311 continue;
23314 reg = NULL;
23315 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23316 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23317 VAR_INIT_STATUS_INITIALIZED);
23318 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23320 rtx mem = XEXP (XEXP (arg, 0), 0);
23321 reg = mem_loc_descriptor (XEXP (mem, 0),
23322 get_address_mode (mem),
23323 GET_MODE (mem),
23324 VAR_INIT_STATUS_INITIALIZED);
23326 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23327 == DEBUG_PARAMETER_REF)
23329 tree tdecl
23330 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23331 tdie = lookup_decl_die (tdecl);
23332 if (tdie == NULL)
23333 continue;
23334 arg_decl = tdecl;
23336 else
23337 continue;
23338 if (reg == NULL
23339 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23340 != DEBUG_PARAMETER_REF)
23341 continue;
23342 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23343 VOIDmode,
23344 VAR_INIT_STATUS_INITIALIZED);
23345 if (val == NULL)
23346 continue;
23347 if (die == NULL)
23348 die = gen_call_site_die (decl, subr_die, ca_loc);
23349 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23350 NULL_TREE);
23351 add_desc_attribute (cdie, arg_decl);
23352 if (reg != NULL)
23353 add_AT_loc (cdie, DW_AT_location, reg);
23354 else if (tdie != NULL)
23355 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23356 tdie);
23357 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23358 if (next_arg != XEXP (arg, 1))
23360 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23361 if (mode == VOIDmode)
23362 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23363 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23364 0), 1),
23365 mode, VOIDmode,
23366 VAR_INIT_STATUS_INITIALIZED);
23367 if (val != NULL)
23368 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23369 val);
23372 if (die == NULL
23373 && (ca_loc->symbol_ref || tloc))
23374 die = gen_call_site_die (decl, subr_die, ca_loc);
23375 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23377 dw_loc_descr_ref tval = NULL;
23379 if (tloc != NULL_RTX)
23380 tval = mem_loc_descriptor (tloc,
23381 GET_MODE (tloc) == VOIDmode
23382 ? Pmode : GET_MODE (tloc),
23383 VOIDmode,
23384 VAR_INIT_STATUS_INITIALIZED);
23385 if (tval)
23386 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23387 else if (tlocc != NULL_RTX)
23389 tval = mem_loc_descriptor (tlocc,
23390 GET_MODE (tlocc) == VOIDmode
23391 ? Pmode : GET_MODE (tlocc),
23392 VOIDmode,
23393 VAR_INIT_STATUS_INITIALIZED);
23394 if (tval)
23395 add_AT_loc (die,
23396 dwarf_AT (DW_AT_call_target_clobbered),
23397 tval);
23400 if (die != NULL)
23402 call_site_note_count++;
23403 if (ca_loc->tail_call_p)
23404 tail_call_site_note_count++;
23408 call_arg_locations = NULL;
23409 call_arg_loc_last = NULL;
23410 if (tail_call_site_count >= 0
23411 && tail_call_site_count == tail_call_site_note_count
23412 && (!dwarf_strict || dwarf_version >= 5))
23414 if (call_site_count >= 0
23415 && call_site_count == call_site_note_count)
23416 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23417 else
23418 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23420 call_site_count = -1;
23421 tail_call_site_count = -1;
23424 /* Mark used types after we have created DIEs for the functions scopes. */
23425 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23428 /* Returns a hash value for X (which really is a die_struct). */
23430 hashval_t
23431 block_die_hasher::hash (die_struct *d)
23433 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23436 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23437 as decl_id and die_parent of die_struct Y. */
23439 bool
23440 block_die_hasher::equal (die_struct *x, die_struct *y)
23442 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23445 /* Hold information about markers for inlined entry points. */
23446 struct GTY ((for_user)) inline_entry_data
23448 /* The block that's the inlined_function_outer_scope for an inlined
23449 function. */
23450 tree block;
23452 /* The label at the inlined entry point. */
23453 const char *label_pfx;
23454 unsigned int label_num;
23456 /* The view number to be used as the inlined entry point. */
23457 var_loc_view view;
23460 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23462 typedef tree compare_type;
23463 static inline hashval_t hash (const inline_entry_data *);
23464 static inline bool equal (const inline_entry_data *, const_tree);
23467 /* Hash table routines for inline_entry_data. */
23469 inline hashval_t
23470 inline_entry_data_hasher::hash (const inline_entry_data *data)
23472 return htab_hash_pointer (data->block);
23475 inline bool
23476 inline_entry_data_hasher::equal (const inline_entry_data *data,
23477 const_tree block)
23479 return data->block == block;
23482 /* Inlined entry points pending DIE creation in this compilation unit. */
23484 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23487 /* Return TRUE if DECL, which may have been previously generated as
23488 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23489 true if decl (or its origin) is either an extern declaration or a
23490 class/namespace scoped declaration.
23492 The declare_in_namespace support causes us to get two DIEs for one
23493 variable, both of which are declarations. We want to avoid
23494 considering one to be a specification, so we must test for
23495 DECLARATION and DW_AT_declaration. */
23496 static inline bool
23497 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23499 return (old_die && TREE_STATIC (decl) && !declaration
23500 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23503 /* Return true if DECL is a local static. */
23505 static inline bool
23506 local_function_static (tree decl)
23508 gcc_assert (VAR_P (decl));
23509 return TREE_STATIC (decl)
23510 && DECL_CONTEXT (decl)
23511 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23514 /* Generate a DIE to represent a declared data object.
23515 Either DECL or ORIGIN must be non-null. */
23517 static void
23518 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23520 HOST_WIDE_INT off = 0;
23521 tree com_decl;
23522 tree decl_or_origin = decl ? decl : origin;
23523 tree ultimate_origin;
23524 dw_die_ref var_die;
23525 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23526 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23527 || class_or_namespace_scope_p (context_die));
23528 bool specialization_p = false;
23529 bool no_linkage_name = false;
23531 /* While C++ inline static data members have definitions inside of the
23532 class, force the first DIE to be a declaration, then let gen_member_die
23533 reparent it to the class context and call gen_variable_die again
23534 to create the outside of the class DIE for the definition. */
23535 if (!declaration
23536 && old_die == NULL
23537 && decl
23538 && DECL_CONTEXT (decl)
23539 && TYPE_P (DECL_CONTEXT (decl))
23540 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23542 declaration = true;
23543 if (dwarf_version < 5)
23544 no_linkage_name = true;
23547 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23548 if (decl || ultimate_origin)
23549 origin = ultimate_origin;
23550 com_decl = fortran_common (decl_or_origin, &off);
23552 /* Symbol in common gets emitted as a child of the common block, in the form
23553 of a data member. */
23554 if (com_decl)
23556 dw_die_ref com_die;
23557 dw_loc_list_ref loc = NULL;
23558 die_node com_die_arg;
23560 var_die = lookup_decl_die (decl_or_origin);
23561 if (var_die)
23563 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23565 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23566 if (loc)
23568 if (off)
23570 /* Optimize the common case. */
23571 if (single_element_loc_list_p (loc)
23572 && loc->expr->dw_loc_opc == DW_OP_addr
23573 && loc->expr->dw_loc_next == NULL
23574 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23575 == SYMBOL_REF)
23577 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23578 loc->expr->dw_loc_oprnd1.v.val_addr
23579 = plus_constant (GET_MODE (x), x , off);
23581 else
23582 loc_list_plus_const (loc, off);
23584 add_AT_location_description (var_die, DW_AT_location, loc);
23585 remove_AT (var_die, DW_AT_declaration);
23588 return;
23591 if (common_block_die_table == NULL)
23592 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23594 com_die_arg.decl_id = DECL_UID (com_decl);
23595 com_die_arg.die_parent = context_die;
23596 com_die = common_block_die_table->find (&com_die_arg);
23597 if (! early_dwarf)
23598 loc = loc_list_from_tree (com_decl, 2, NULL);
23599 if (com_die == NULL)
23601 const char *cnam
23602 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23603 die_node **slot;
23605 com_die = new_die (DW_TAG_common_block, context_die, decl);
23606 add_name_and_src_coords_attributes (com_die, com_decl);
23607 if (loc)
23609 add_AT_location_description (com_die, DW_AT_location, loc);
23610 /* Avoid sharing the same loc descriptor between
23611 DW_TAG_common_block and DW_TAG_variable. */
23612 loc = loc_list_from_tree (com_decl, 2, NULL);
23614 else if (DECL_EXTERNAL (decl_or_origin))
23615 add_AT_flag (com_die, DW_AT_declaration, 1);
23616 if (want_pubnames ())
23617 add_pubname_string (cnam, com_die); /* ??? needed? */
23618 com_die->decl_id = DECL_UID (com_decl);
23619 slot = common_block_die_table->find_slot (com_die, INSERT);
23620 *slot = com_die;
23622 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23624 add_AT_location_description (com_die, DW_AT_location, loc);
23625 loc = loc_list_from_tree (com_decl, 2, NULL);
23626 remove_AT (com_die, DW_AT_declaration);
23628 var_die = new_die (DW_TAG_variable, com_die, decl);
23629 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23630 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23631 decl_quals (decl_or_origin), false,
23632 context_die);
23633 add_alignment_attribute (var_die, decl);
23634 add_AT_flag (var_die, DW_AT_external, 1);
23635 if (loc)
23637 if (off)
23639 /* Optimize the common case. */
23640 if (single_element_loc_list_p (loc)
23641 && loc->expr->dw_loc_opc == DW_OP_addr
23642 && loc->expr->dw_loc_next == NULL
23643 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23645 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23646 loc->expr->dw_loc_oprnd1.v.val_addr
23647 = plus_constant (GET_MODE (x), x, off);
23649 else
23650 loc_list_plus_const (loc, off);
23652 add_AT_location_description (var_die, DW_AT_location, loc);
23654 else if (DECL_EXTERNAL (decl_or_origin))
23655 add_AT_flag (var_die, DW_AT_declaration, 1);
23656 if (decl)
23657 equate_decl_number_to_die (decl, var_die);
23658 return;
23661 if (old_die)
23663 if (declaration)
23665 /* A declaration that has been previously dumped, needs no
23666 further annotations, since it doesn't need location on
23667 the second pass. */
23668 return;
23670 else if (decl_will_get_specification_p (old_die, decl, declaration)
23671 && !get_AT (old_die, DW_AT_specification))
23673 /* Fall-thru so we can make a new variable die along with a
23674 DW_AT_specification. */
23676 else if (origin && old_die->die_parent != context_die)
23678 /* If we will be creating an inlined instance, we need a
23679 new DIE that will get annotated with
23680 DW_AT_abstract_origin. */
23681 gcc_assert (!DECL_ABSTRACT_P (decl));
23683 else
23685 /* If a DIE was dumped early, it still needs location info.
23686 Skip to where we fill the location bits. */
23687 var_die = old_die;
23689 /* ??? In LTRANS we cannot annotate early created variably
23690 modified type DIEs without copying them and adjusting all
23691 references to them. Thus we dumped them again. Also add a
23692 reference to them but beware of -g0 compile and -g link
23693 in which case the reference will be already present. */
23694 tree type = TREE_TYPE (decl_or_origin);
23695 if (in_lto_p
23696 && ! get_AT (var_die, DW_AT_type)
23697 && variably_modified_type_p
23698 (type, decl_function_context (decl_or_origin)))
23700 if (decl_by_reference_p (decl_or_origin))
23701 add_type_attribute (var_die, TREE_TYPE (type),
23702 TYPE_UNQUALIFIED, false, context_die);
23703 else
23704 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23705 false, context_die);
23708 goto gen_variable_die_location;
23712 /* For static data members, the declaration in the class is supposed
23713 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23714 also in DWARF2; the specification should still be DW_TAG_variable
23715 referencing the DW_TAG_member DIE. */
23716 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23717 var_die = new_die (DW_TAG_member, context_die, decl);
23718 else
23719 var_die = new_die (DW_TAG_variable, context_die, decl);
23721 if (origin != NULL)
23722 add_abstract_origin_attribute (var_die, origin);
23724 /* Loop unrolling can create multiple blocks that refer to the same
23725 static variable, so we must test for the DW_AT_declaration flag.
23727 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23728 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23729 sharing them.
23731 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23732 else if (decl_will_get_specification_p (old_die, decl, declaration))
23734 /* This is a definition of a C++ class level static. */
23735 add_AT_specification (var_die, old_die);
23736 specialization_p = true;
23737 if (DECL_NAME (decl))
23739 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23740 struct dwarf_file_data * file_index = lookup_filename (s.file);
23742 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23743 add_AT_file (var_die, DW_AT_decl_file, file_index);
23745 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23746 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23748 if (debug_column_info
23749 && s.column
23750 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23751 != (unsigned) s.column))
23752 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23754 if (old_die->die_tag == DW_TAG_member)
23755 add_linkage_name (var_die, decl);
23758 else
23759 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23761 if ((origin == NULL && !specialization_p)
23762 || (origin != NULL
23763 && !DECL_ABSTRACT_P (decl_or_origin)
23764 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23765 decl_function_context
23766 (decl_or_origin))))
23768 tree type = TREE_TYPE (decl_or_origin);
23770 if (decl_by_reference_p (decl_or_origin))
23771 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23772 context_die);
23773 else
23774 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23775 context_die);
23778 if (origin == NULL && !specialization_p)
23780 if (TREE_PUBLIC (decl))
23781 add_AT_flag (var_die, DW_AT_external, 1);
23783 if (DECL_ARTIFICIAL (decl))
23784 add_AT_flag (var_die, DW_AT_artificial, 1);
23786 add_alignment_attribute (var_die, decl);
23788 add_accessibility_attribute (var_die, decl);
23791 if (declaration)
23792 add_AT_flag (var_die, DW_AT_declaration, 1);
23794 if (decl && (DECL_ABSTRACT_P (decl)
23795 || !old_die || is_declaration_die (old_die)))
23796 equate_decl_number_to_die (decl, var_die);
23798 gen_variable_die_location:
23799 if (! declaration
23800 && (! DECL_ABSTRACT_P (decl_or_origin)
23801 /* Local static vars are shared between all clones/inlines,
23802 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23803 already set. */
23804 || (VAR_P (decl_or_origin)
23805 && TREE_STATIC (decl_or_origin)
23806 && DECL_RTL_SET_P (decl_or_origin))))
23808 if (early_dwarf)
23809 add_pubname (decl_or_origin, var_die);
23810 else
23811 add_location_or_const_value_attribute (var_die, decl_or_origin,
23812 decl == NULL);
23814 else
23815 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23817 if ((dwarf_version >= 4 || !dwarf_strict)
23818 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23819 DW_AT_const_expr) == 1
23820 && !get_AT (var_die, DW_AT_const_expr)
23821 && !specialization_p)
23822 add_AT_flag (var_die, DW_AT_const_expr, 1);
23824 if (!dwarf_strict)
23826 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23827 DW_AT_inline);
23828 if (inl != -1
23829 && !get_AT (var_die, DW_AT_inline)
23830 && !specialization_p)
23831 add_AT_unsigned (var_die, DW_AT_inline, inl);
23835 /* Generate a DIE to represent a named constant. */
23837 static void
23838 gen_const_die (tree decl, dw_die_ref context_die)
23840 dw_die_ref const_die;
23841 tree type = TREE_TYPE (decl);
23843 const_die = lookup_decl_die (decl);
23844 if (const_die)
23845 return;
23847 const_die = new_die (DW_TAG_constant, context_die, decl);
23848 equate_decl_number_to_die (decl, const_die);
23849 add_name_and_src_coords_attributes (const_die, decl);
23850 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23851 if (TREE_PUBLIC (decl))
23852 add_AT_flag (const_die, DW_AT_external, 1);
23853 if (DECL_ARTIFICIAL (decl))
23854 add_AT_flag (const_die, DW_AT_artificial, 1);
23855 tree_add_const_value_attribute_for_decl (const_die, decl);
23858 /* Generate a DIE to represent a label identifier. */
23860 static void
23861 gen_label_die (tree decl, dw_die_ref context_die)
23863 tree origin = decl_ultimate_origin (decl);
23864 dw_die_ref lbl_die = lookup_decl_die (decl);
23865 rtx insn;
23866 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23868 if (!lbl_die)
23870 lbl_die = new_die (DW_TAG_label, context_die, decl);
23871 equate_decl_number_to_die (decl, lbl_die);
23873 if (origin != NULL)
23874 add_abstract_origin_attribute (lbl_die, origin);
23875 else
23876 add_name_and_src_coords_attributes (lbl_die, decl);
23879 if (DECL_ABSTRACT_P (decl))
23880 equate_decl_number_to_die (decl, lbl_die);
23881 else if (! early_dwarf)
23883 insn = DECL_RTL_IF_SET (decl);
23885 /* Deleted labels are programmer specified labels which have been
23886 eliminated because of various optimizations. We still emit them
23887 here so that it is possible to put breakpoints on them. */
23888 if (insn
23889 && (LABEL_P (insn)
23890 || ((NOTE_P (insn)
23891 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23893 /* When optimization is enabled (via -O) some parts of the compiler
23894 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23895 represent source-level labels which were explicitly declared by
23896 the user. This really shouldn't be happening though, so catch
23897 it if it ever does happen. */
23898 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23900 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23901 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23903 else if (insn
23904 && NOTE_P (insn)
23905 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23906 && CODE_LABEL_NUMBER (insn) != -1)
23908 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23909 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23914 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23915 attributes to the DIE for a block STMT, to describe where the inlined
23916 function was called from. This is similar to add_src_coords_attributes. */
23918 static inline void
23919 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23921 /* We can end up with BUILTINS_LOCATION here. */
23922 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
23923 return;
23925 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23927 if (dwarf_version >= 3 || !dwarf_strict)
23929 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23930 add_AT_unsigned (die, DW_AT_call_line, s.line);
23931 if (debug_column_info && s.column)
23932 add_AT_unsigned (die, DW_AT_call_column, s.column);
23937 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23938 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23940 static inline void
23941 add_high_low_attributes (tree stmt, dw_die_ref die)
23943 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23945 if (inline_entry_data **iedp
23946 = !inline_entry_data_table ? NULL
23947 : inline_entry_data_table->find_slot_with_hash (stmt,
23948 htab_hash_pointer (stmt),
23949 NO_INSERT))
23951 inline_entry_data *ied = *iedp;
23952 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23953 gcc_assert (debug_inline_points);
23954 gcc_assert (inlined_function_outer_scope_p (stmt));
23956 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23957 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23959 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
23960 && !dwarf_strict)
23962 if (!output_asm_line_debug_info ())
23963 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23964 else
23966 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23967 /* FIXME: this will resolve to a small number. Could we
23968 possibly emit smaller data? Ideally we'd emit a
23969 uleb128, but that would make the size of DIEs
23970 impossible for the compiler to compute, since it's
23971 the assembler that computes the value of the view
23972 label in this case. Ideally, we'd have a single form
23973 encompassing both the address and the view, and
23974 indirecting them through a table might make things
23975 easier, but even that would be more wasteful,
23976 space-wise, than what we have now. */
23977 add_AT_symview (die, DW_AT_GNU_entry_view, label);
23981 inline_entry_data_table->clear_slot (iedp);
23984 if (BLOCK_FRAGMENT_CHAIN (stmt)
23985 && (dwarf_version >= 3 || !dwarf_strict))
23987 tree chain, superblock = NULL_TREE;
23988 dw_die_ref pdie;
23989 dw_attr_node *attr = NULL;
23991 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
23993 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23994 BLOCK_NUMBER (stmt));
23995 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23998 /* Optimize duplicate .debug_ranges lists or even tails of
23999 lists. If this BLOCK has same ranges as its supercontext,
24000 lookup DW_AT_ranges attribute in the supercontext (and
24001 recursively so), verify that the ranges_table contains the
24002 right values and use it instead of adding a new .debug_range. */
24003 for (chain = stmt, pdie = die;
24004 BLOCK_SAME_RANGE (chain);
24005 chain = BLOCK_SUPERCONTEXT (chain))
24007 dw_attr_node *new_attr;
24009 pdie = pdie->die_parent;
24010 if (pdie == NULL)
24011 break;
24012 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24013 break;
24014 new_attr = get_AT (pdie, DW_AT_ranges);
24015 if (new_attr == NULL
24016 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24017 break;
24018 attr = new_attr;
24019 superblock = BLOCK_SUPERCONTEXT (chain);
24021 if (attr != NULL
24022 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24023 == (int)BLOCK_NUMBER (superblock))
24024 && BLOCK_FRAGMENT_CHAIN (superblock))
24026 unsigned long off = attr->dw_attr_val.v.val_offset;
24027 unsigned long supercnt = 0, thiscnt = 0;
24028 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24029 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24031 ++supercnt;
24032 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24033 == (int)BLOCK_NUMBER (chain));
24035 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24036 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24037 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24038 ++thiscnt;
24039 gcc_assert (supercnt >= thiscnt);
24040 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24041 false);
24042 note_rnglist_head (off + supercnt - thiscnt);
24043 return;
24046 unsigned int offset = add_ranges (stmt, true);
24047 add_AT_range_list (die, DW_AT_ranges, offset, false);
24048 note_rnglist_head (offset);
24050 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24051 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24054 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24055 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24056 chain = BLOCK_FRAGMENT_CHAIN (chain);
24058 while (chain);
24059 add_ranges (NULL);
24061 else
24063 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24064 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24065 BLOCK_NUMBER (stmt));
24066 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24067 BLOCK_NUMBER (stmt));
24068 add_AT_low_high_pc (die, label, label_high, false);
24072 /* Generate a DIE for a lexical block. */
24074 static void
24075 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24077 dw_die_ref old_die = lookup_block_die (stmt);
24078 dw_die_ref stmt_die = NULL;
24079 if (!old_die)
24081 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24082 equate_block_to_die (stmt, stmt_die);
24085 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24087 /* If this is an inlined or conrecte instance, create a new lexical
24088 die for anything below to attach DW_AT_abstract_origin to. */
24089 if (old_die)
24090 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24092 tree origin = block_ultimate_origin (stmt);
24093 if (origin != NULL_TREE && (origin != stmt || old_die))
24094 add_abstract_origin_attribute (stmt_die, origin);
24096 old_die = NULL;
24099 if (old_die)
24100 stmt_die = old_die;
24102 /* A non abstract block whose blocks have already been reordered
24103 should have the instruction range for this block. If so, set the
24104 high/low attributes. */
24105 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24107 gcc_assert (stmt_die);
24108 add_high_low_attributes (stmt, stmt_die);
24111 decls_for_scope (stmt, stmt_die);
24114 /* Generate a DIE for an inlined subprogram. */
24116 static void
24117 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24119 tree decl = block_ultimate_origin (stmt);
24121 /* Make sure any inlined functions are known to be inlineable. */
24122 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24123 || cgraph_function_possibly_inlined_p (decl));
24125 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24127 if (call_arg_locations || debug_inline_points)
24128 equate_block_to_die (stmt, subr_die);
24129 add_abstract_origin_attribute (subr_die, decl);
24130 if (TREE_ASM_WRITTEN (stmt))
24131 add_high_low_attributes (stmt, subr_die);
24132 add_call_src_coords_attributes (stmt, subr_die);
24134 /* The inliner creates an extra BLOCK for the parameter setup,
24135 we want to merge that with the actual outermost BLOCK of the
24136 inlined function to avoid duplicate locals in consumers.
24137 Do that by doing the recursion to subblocks on the single subblock
24138 of STMT. */
24139 bool unwrap_one = false;
24140 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24142 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24143 if (origin
24144 && TREE_CODE (origin) == BLOCK
24145 && BLOCK_SUPERCONTEXT (origin) == decl)
24146 unwrap_one = true;
24148 decls_for_scope (stmt, subr_die, !unwrap_one);
24149 if (unwrap_one)
24150 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24153 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24154 the comment for VLR_CONTEXT. */
24156 static void
24157 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24159 dw_die_ref decl_die;
24161 if (TREE_TYPE (decl) == error_mark_node)
24162 return;
24164 decl_die = new_die (DW_TAG_member, context_die, decl);
24165 add_name_and_src_coords_attributes (decl_die, decl);
24166 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24167 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24168 context_die);
24170 if (DECL_BIT_FIELD_TYPE (decl))
24172 add_byte_size_attribute (decl_die, decl);
24173 add_bit_size_attribute (decl_die, decl);
24174 add_bit_offset_attribute (decl_die, decl, ctx);
24177 add_alignment_attribute (decl_die, decl);
24179 /* If we have a variant part offset, then we are supposed to process a member
24180 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24181 trees. */
24182 gcc_assert (ctx->variant_part_offset == NULL_TREE
24183 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24184 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24185 add_data_member_location_attribute (decl_die, decl, ctx);
24187 if (DECL_ARTIFICIAL (decl))
24188 add_AT_flag (decl_die, DW_AT_artificial, 1);
24190 add_accessibility_attribute (decl_die, decl);
24192 /* Equate decl number to die, so that we can look up this decl later on. */
24193 equate_decl_number_to_die (decl, decl_die);
24196 /* Generate a DIE for a pointer to a member type. TYPE can be an
24197 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24198 pointer to member function. */
24200 static void
24201 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24203 if (lookup_type_die (type))
24204 return;
24206 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24207 scope_die_for (type, context_die), type);
24209 equate_type_number_to_die (type, ptr_die);
24210 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24211 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24212 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24213 context_die);
24214 add_alignment_attribute (ptr_die, type);
24216 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24217 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24219 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24220 add_AT_loc (ptr_die, DW_AT_use_location, op);
24224 static char *producer_string;
24226 /* Return a heap allocated producer string including command line options
24227 if -grecord-gcc-switches. */
24229 static char *
24230 gen_producer_string (void)
24232 size_t j;
24233 auto_vec<const char *> switches;
24234 const char *language_string = lang_hooks.name;
24235 char *producer, *tail;
24236 const char *p;
24237 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24238 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24240 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24241 switch (save_decoded_options[j].opt_index)
24243 case OPT_o:
24244 case OPT_d:
24245 case OPT_dumpbase:
24246 case OPT_dumpdir:
24247 case OPT_auxbase:
24248 case OPT_auxbase_strip:
24249 case OPT_quiet:
24250 case OPT_version:
24251 case OPT_v:
24252 case OPT_w:
24253 case OPT_L:
24254 case OPT_D:
24255 case OPT_I:
24256 case OPT_U:
24257 case OPT_SPECIAL_unknown:
24258 case OPT_SPECIAL_ignore:
24259 case OPT_SPECIAL_deprecated:
24260 case OPT_SPECIAL_program_name:
24261 case OPT_SPECIAL_input_file:
24262 case OPT_grecord_gcc_switches:
24263 case OPT__output_pch_:
24264 case OPT_fdiagnostics_show_location_:
24265 case OPT_fdiagnostics_show_option:
24266 case OPT_fdiagnostics_show_caret:
24267 case OPT_fdiagnostics_show_labels:
24268 case OPT_fdiagnostics_show_line_numbers:
24269 case OPT_fdiagnostics_color_:
24270 case OPT_fverbose_asm:
24271 case OPT____:
24272 case OPT__sysroot_:
24273 case OPT_nostdinc:
24274 case OPT_nostdinc__:
24275 case OPT_fpreprocessed:
24276 case OPT_fltrans_output_list_:
24277 case OPT_fresolution_:
24278 case OPT_fdebug_prefix_map_:
24279 case OPT_fmacro_prefix_map_:
24280 case OPT_ffile_prefix_map_:
24281 case OPT_fcompare_debug:
24282 case OPT_fchecking:
24283 case OPT_fchecking_:
24284 /* Ignore these. */
24285 continue;
24286 default:
24287 if (cl_options[save_decoded_options[j].opt_index].flags
24288 & CL_NO_DWARF_RECORD)
24289 continue;
24290 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24291 == '-');
24292 switch (save_decoded_options[j].canonical_option[0][1])
24294 case 'M':
24295 case 'i':
24296 case 'W':
24297 continue;
24298 case 'f':
24299 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24300 "dump", 4) == 0)
24301 continue;
24302 break;
24303 default:
24304 break;
24306 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24307 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24308 break;
24311 producer = XNEWVEC (char, plen + 1 + len + 1);
24312 tail = producer;
24313 sprintf (tail, "%s %s", language_string, version_string);
24314 tail += plen;
24316 FOR_EACH_VEC_ELT (switches, j, p)
24318 len = strlen (p);
24319 *tail = ' ';
24320 memcpy (tail + 1, p, len);
24321 tail += len + 1;
24324 *tail = '\0';
24325 return producer;
24328 /* Given a C and/or C++ language/version string return the "highest".
24329 C++ is assumed to be "higher" than C in this case. Used for merging
24330 LTO translation unit languages. */
24331 static const char *
24332 highest_c_language (const char *lang1, const char *lang2)
24334 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24335 return "GNU C++17";
24336 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24337 return "GNU C++14";
24338 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24339 return "GNU C++11";
24340 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24341 return "GNU C++98";
24343 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24344 return "GNU C2X";
24345 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24346 return "GNU C17";
24347 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24348 return "GNU C11";
24349 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24350 return "GNU C99";
24351 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24352 return "GNU C89";
24354 gcc_unreachable ();
24358 /* Generate the DIE for the compilation unit. */
24360 static dw_die_ref
24361 gen_compile_unit_die (const char *filename)
24363 dw_die_ref die;
24364 const char *language_string = lang_hooks.name;
24365 int language;
24367 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24369 if (filename)
24371 add_name_attribute (die, filename);
24372 /* Don't add cwd for <built-in>. */
24373 if (filename[0] != '<')
24374 add_comp_dir_attribute (die);
24377 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24379 /* If our producer is LTO try to figure out a common language to use
24380 from the global list of translation units. */
24381 if (strcmp (language_string, "GNU GIMPLE") == 0)
24383 unsigned i;
24384 tree t;
24385 const char *common_lang = NULL;
24387 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24389 if (!TRANSLATION_UNIT_LANGUAGE (t))
24390 continue;
24391 if (!common_lang)
24392 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24393 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24395 else if (strncmp (common_lang, "GNU C", 5) == 0
24396 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24397 /* Mixing C and C++ is ok, use C++ in that case. */
24398 common_lang = highest_c_language (common_lang,
24399 TRANSLATION_UNIT_LANGUAGE (t));
24400 else
24402 /* Fall back to C. */
24403 common_lang = NULL;
24404 break;
24408 if (common_lang)
24409 language_string = common_lang;
24412 language = DW_LANG_C;
24413 if (strncmp (language_string, "GNU C", 5) == 0
24414 && ISDIGIT (language_string[5]))
24416 language = DW_LANG_C89;
24417 if (dwarf_version >= 3 || !dwarf_strict)
24419 if (strcmp (language_string, "GNU C89") != 0)
24420 language = DW_LANG_C99;
24422 if (dwarf_version >= 5 /* || !dwarf_strict */)
24423 if (strcmp (language_string, "GNU C11") == 0
24424 || strcmp (language_string, "GNU C17") == 0
24425 || strcmp (language_string, "GNU C2X"))
24426 language = DW_LANG_C11;
24429 else if (strncmp (language_string, "GNU C++", 7) == 0)
24431 language = DW_LANG_C_plus_plus;
24432 if (dwarf_version >= 5 /* || !dwarf_strict */)
24434 if (strcmp (language_string, "GNU C++11") == 0)
24435 language = DW_LANG_C_plus_plus_11;
24436 else if (strcmp (language_string, "GNU C++14") == 0)
24437 language = DW_LANG_C_plus_plus_14;
24438 else if (strcmp (language_string, "GNU C++17") == 0)
24439 /* For now. */
24440 language = DW_LANG_C_plus_plus_14;
24443 else if (strcmp (language_string, "GNU F77") == 0)
24444 language = DW_LANG_Fortran77;
24445 else if (dwarf_version >= 3 || !dwarf_strict)
24447 if (strcmp (language_string, "GNU Ada") == 0)
24448 language = DW_LANG_Ada95;
24449 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24451 language = DW_LANG_Fortran95;
24452 if (dwarf_version >= 5 /* || !dwarf_strict */)
24454 if (strcmp (language_string, "GNU Fortran2003") == 0)
24455 language = DW_LANG_Fortran03;
24456 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24457 language = DW_LANG_Fortran08;
24460 else if (strcmp (language_string, "GNU Objective-C") == 0)
24461 language = DW_LANG_ObjC;
24462 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24463 language = DW_LANG_ObjC_plus_plus;
24464 else if (strcmp (language_string, "GNU D") == 0)
24465 language = DW_LANG_D;
24466 else if (dwarf_version >= 5 || !dwarf_strict)
24468 if (strcmp (language_string, "GNU Go") == 0)
24469 language = DW_LANG_Go;
24472 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24473 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24474 language = DW_LANG_Fortran90;
24475 /* Likewise for Ada. */
24476 else if (strcmp (language_string, "GNU Ada") == 0)
24477 language = DW_LANG_Ada83;
24479 add_AT_unsigned (die, DW_AT_language, language);
24481 switch (language)
24483 case DW_LANG_Fortran77:
24484 case DW_LANG_Fortran90:
24485 case DW_LANG_Fortran95:
24486 case DW_LANG_Fortran03:
24487 case DW_LANG_Fortran08:
24488 /* Fortran has case insensitive identifiers and the front-end
24489 lowercases everything. */
24490 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24491 break;
24492 default:
24493 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24494 break;
24496 return die;
24499 /* Generate the DIE for a base class. */
24501 static void
24502 gen_inheritance_die (tree binfo, tree access, tree type,
24503 dw_die_ref context_die)
24505 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24506 struct vlr_context ctx = { type, NULL };
24508 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24509 context_die);
24510 add_data_member_location_attribute (die, binfo, &ctx);
24512 if (BINFO_VIRTUAL_P (binfo))
24513 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24515 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24516 children, otherwise the default is DW_ACCESS_public. In DWARF2
24517 the default has always been DW_ACCESS_private. */
24518 if (access == access_public_node)
24520 if (dwarf_version == 2
24521 || context_die->die_tag == DW_TAG_class_type)
24522 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24524 else if (access == access_protected_node)
24525 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24526 else if (dwarf_version > 2
24527 && context_die->die_tag != DW_TAG_class_type)
24528 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24531 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24532 structure. */
24533 static bool
24534 is_variant_part (tree decl)
24536 return (TREE_CODE (decl) == FIELD_DECL
24537 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24540 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24541 return the FIELD_DECL. Return NULL_TREE otherwise. */
24543 static tree
24544 analyze_discr_in_predicate (tree operand, tree struct_type)
24546 bool continue_stripping = true;
24547 while (continue_stripping)
24548 switch (TREE_CODE (operand))
24550 CASE_CONVERT:
24551 operand = TREE_OPERAND (operand, 0);
24552 break;
24553 default:
24554 continue_stripping = false;
24555 break;
24558 /* Match field access to members of struct_type only. */
24559 if (TREE_CODE (operand) == COMPONENT_REF
24560 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24561 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24562 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24563 return TREE_OPERAND (operand, 1);
24564 else
24565 return NULL_TREE;
24568 /* Check that SRC is a constant integer that can be represented as a native
24569 integer constant (either signed or unsigned). If so, store it into DEST and
24570 return true. Return false otherwise. */
24572 static bool
24573 get_discr_value (tree src, dw_discr_value *dest)
24575 tree discr_type = TREE_TYPE (src);
24577 if (lang_hooks.types.get_debug_type)
24579 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24580 if (debug_type != NULL)
24581 discr_type = debug_type;
24584 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24585 return false;
24587 /* Signedness can vary between the original type and the debug type. This
24588 can happen for character types in Ada for instance: the character type
24589 used for code generation can be signed, to be compatible with the C one,
24590 but from a debugger point of view, it must be unsigned. */
24591 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24592 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24594 if (is_orig_unsigned != is_debug_unsigned)
24595 src = fold_convert (discr_type, src);
24597 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24598 return false;
24600 dest->pos = is_debug_unsigned;
24601 if (is_debug_unsigned)
24602 dest->v.uval = tree_to_uhwi (src);
24603 else
24604 dest->v.sval = tree_to_shwi (src);
24606 return true;
24609 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24610 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24611 store NULL_TREE in DISCR_DECL. Otherwise:
24613 - store the discriminant field in STRUCT_TYPE that controls the variant
24614 part to *DISCR_DECL
24616 - put in *DISCR_LISTS_P an array where for each variant, the item
24617 represents the corresponding matching list of discriminant values.
24619 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24620 the above array.
24622 Note that when the array is allocated (i.e. when the analysis is
24623 successful), it is up to the caller to free the array. */
24625 static void
24626 analyze_variants_discr (tree variant_part_decl,
24627 tree struct_type,
24628 tree *discr_decl,
24629 dw_discr_list_ref **discr_lists_p,
24630 unsigned *discr_lists_length)
24632 tree variant_part_type = TREE_TYPE (variant_part_decl);
24633 tree variant;
24634 dw_discr_list_ref *discr_lists;
24635 unsigned i;
24637 /* Compute how many variants there are in this variant part. */
24638 *discr_lists_length = 0;
24639 for (variant = TYPE_FIELDS (variant_part_type);
24640 variant != NULL_TREE;
24641 variant = DECL_CHAIN (variant))
24642 ++*discr_lists_length;
24644 *discr_decl = NULL_TREE;
24645 *discr_lists_p
24646 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24647 sizeof (**discr_lists_p));
24648 discr_lists = *discr_lists_p;
24650 /* And then analyze all variants to extract discriminant information for all
24651 of them. This analysis is conservative: as soon as we detect something we
24652 do not support, abort everything and pretend we found nothing. */
24653 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24654 variant != NULL_TREE;
24655 variant = DECL_CHAIN (variant), ++i)
24657 tree match_expr = DECL_QUALIFIER (variant);
24659 /* Now, try to analyze the predicate and deduce a discriminant for
24660 it. */
24661 if (match_expr == boolean_true_node)
24662 /* Typically happens for the default variant: it matches all cases that
24663 previous variants rejected. Don't output any matching value for
24664 this one. */
24665 continue;
24667 /* The following loop tries to iterate over each discriminant
24668 possibility: single values or ranges. */
24669 while (match_expr != NULL_TREE)
24671 tree next_round_match_expr;
24672 tree candidate_discr = NULL_TREE;
24673 dw_discr_list_ref new_node = NULL;
24675 /* Possibilities are matched one after the other by nested
24676 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24677 continue with the rest at next iteration. */
24678 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24680 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24681 match_expr = TREE_OPERAND (match_expr, 1);
24683 else
24684 next_round_match_expr = NULL_TREE;
24686 if (match_expr == boolean_false_node)
24687 /* This sub-expression matches nothing: just wait for the next
24688 one. */
24691 else if (TREE_CODE (match_expr) == EQ_EXPR)
24693 /* We are matching: <discr_field> == <integer_cst>
24694 This sub-expression matches a single value. */
24695 tree integer_cst = TREE_OPERAND (match_expr, 1);
24697 candidate_discr
24698 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24699 struct_type);
24701 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24702 if (!get_discr_value (integer_cst,
24703 &new_node->dw_discr_lower_bound))
24704 goto abort;
24705 new_node->dw_discr_range = false;
24708 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24710 /* We are matching:
24711 <discr_field> > <integer_cst>
24712 && <discr_field> < <integer_cst>.
24713 This sub-expression matches the range of values between the
24714 two matched integer constants. Note that comparisons can be
24715 inclusive or exclusive. */
24716 tree candidate_discr_1, candidate_discr_2;
24717 tree lower_cst, upper_cst;
24718 bool lower_cst_included, upper_cst_included;
24719 tree lower_op = TREE_OPERAND (match_expr, 0);
24720 tree upper_op = TREE_OPERAND (match_expr, 1);
24722 /* When the comparison is exclusive, the integer constant is not
24723 the discriminant range bound we are looking for: we will have
24724 to increment or decrement it. */
24725 if (TREE_CODE (lower_op) == GE_EXPR)
24726 lower_cst_included = true;
24727 else if (TREE_CODE (lower_op) == GT_EXPR)
24728 lower_cst_included = false;
24729 else
24730 goto abort;
24732 if (TREE_CODE (upper_op) == LE_EXPR)
24733 upper_cst_included = true;
24734 else if (TREE_CODE (upper_op) == LT_EXPR)
24735 upper_cst_included = false;
24736 else
24737 goto abort;
24739 /* Extract the discriminant from the first operand and check it
24740 is consistant with the same analysis in the second
24741 operand. */
24742 candidate_discr_1
24743 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24744 struct_type);
24745 candidate_discr_2
24746 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24747 struct_type);
24748 if (candidate_discr_1 == candidate_discr_2)
24749 candidate_discr = candidate_discr_1;
24750 else
24751 goto abort;
24753 /* Extract bounds from both. */
24754 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24755 lower_cst = TREE_OPERAND (lower_op, 1);
24756 upper_cst = TREE_OPERAND (upper_op, 1);
24758 if (!lower_cst_included)
24759 lower_cst
24760 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24761 build_int_cst (TREE_TYPE (lower_cst), 1));
24762 if (!upper_cst_included)
24763 upper_cst
24764 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24765 build_int_cst (TREE_TYPE (upper_cst), 1));
24767 if (!get_discr_value (lower_cst,
24768 &new_node->dw_discr_lower_bound)
24769 || !get_discr_value (upper_cst,
24770 &new_node->dw_discr_upper_bound))
24771 goto abort;
24773 new_node->dw_discr_range = true;
24776 else
24777 /* Unsupported sub-expression: we cannot determine the set of
24778 matching discriminant values. Abort everything. */
24779 goto abort;
24781 /* If the discriminant info is not consistant with what we saw so
24782 far, consider the analysis failed and abort everything. */
24783 if (candidate_discr == NULL_TREE
24784 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24785 goto abort;
24786 else
24787 *discr_decl = candidate_discr;
24789 if (new_node != NULL)
24791 new_node->dw_discr_next = discr_lists[i];
24792 discr_lists[i] = new_node;
24794 match_expr = next_round_match_expr;
24798 /* If we reach this point, we could match everything we were interested
24799 in. */
24800 return;
24802 abort:
24803 /* Clean all data structure and return no result. */
24804 free (*discr_lists_p);
24805 *discr_lists_p = NULL;
24806 *discr_decl = NULL_TREE;
24809 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24810 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24811 under CONTEXT_DIE.
24813 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24814 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24815 this type, which are record types, represent the available variants and each
24816 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24817 values are inferred from these attributes.
24819 In trees, the offsets for the fields inside these sub-records are relative
24820 to the variant part itself, whereas the corresponding DIEs should have
24821 offset attributes that are relative to the embedding record base address.
24822 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24823 must be an expression that computes the offset of the variant part to
24824 describe in DWARF. */
24826 static void
24827 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24828 dw_die_ref context_die)
24830 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24831 tree variant_part_offset = vlr_ctx->variant_part_offset;
24832 struct loc_descr_context ctx = {
24833 vlr_ctx->struct_type, /* context_type */
24834 NULL_TREE, /* base_decl */
24835 NULL, /* dpi */
24836 false, /* placeholder_arg */
24837 false /* placeholder_seen */
24840 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24841 NULL_TREE if there is no such field. */
24842 tree discr_decl = NULL_TREE;
24843 dw_discr_list_ref *discr_lists;
24844 unsigned discr_lists_length = 0;
24845 unsigned i;
24847 dw_die_ref dwarf_proc_die = NULL;
24848 dw_die_ref variant_part_die
24849 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24851 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24853 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24854 &discr_decl, &discr_lists, &discr_lists_length);
24856 if (discr_decl != NULL_TREE)
24858 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24860 if (discr_die)
24861 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24862 else
24863 /* We have no DIE for the discriminant, so just discard all
24864 discrimimant information in the output. */
24865 discr_decl = NULL_TREE;
24868 /* If the offset for this variant part is more complex than a constant,
24869 create a DWARF procedure for it so that we will not have to generate DWARF
24870 expressions for it for each member. */
24871 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24872 && (dwarf_version >= 3 || !dwarf_strict))
24874 const tree dwarf_proc_fndecl
24875 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24876 build_function_type (TREE_TYPE (variant_part_offset),
24877 NULL_TREE));
24878 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24879 const dw_loc_descr_ref dwarf_proc_body
24880 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24882 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24883 dwarf_proc_fndecl, context_die);
24884 if (dwarf_proc_die != NULL)
24885 variant_part_offset = dwarf_proc_call;
24888 /* Output DIEs for all variants. */
24889 i = 0;
24890 for (tree variant = TYPE_FIELDS (variant_part_type);
24891 variant != NULL_TREE;
24892 variant = DECL_CHAIN (variant), ++i)
24894 tree variant_type = TREE_TYPE (variant);
24895 dw_die_ref variant_die;
24897 /* All variants (i.e. members of a variant part) are supposed to be
24898 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24899 under these records. */
24900 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24902 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24903 equate_decl_number_to_die (variant, variant_die);
24905 /* Output discriminant values this variant matches, if any. */
24906 if (discr_decl == NULL || discr_lists[i] == NULL)
24907 /* In the case we have discriminant information at all, this is
24908 probably the default variant: as the standard says, don't
24909 output any discriminant value/list attribute. */
24911 else if (discr_lists[i]->dw_discr_next == NULL
24912 && !discr_lists[i]->dw_discr_range)
24913 /* If there is only one accepted value, don't bother outputting a
24914 list. */
24915 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24916 else
24917 add_discr_list (variant_die, discr_lists[i]);
24919 for (tree member = TYPE_FIELDS (variant_type);
24920 member != NULL_TREE;
24921 member = DECL_CHAIN (member))
24923 struct vlr_context vlr_sub_ctx = {
24924 vlr_ctx->struct_type, /* struct_type */
24925 NULL /* variant_part_offset */
24927 if (is_variant_part (member))
24929 /* All offsets for fields inside variant parts are relative to
24930 the top-level embedding RECORD_TYPE's base address. On the
24931 other hand, offsets in GCC's types are relative to the
24932 nested-most variant part. So we have to sum offsets each time
24933 we recurse. */
24935 vlr_sub_ctx.variant_part_offset
24936 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24937 variant_part_offset, byte_position (member));
24938 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24940 else
24942 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24943 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24948 free (discr_lists);
24951 /* Generate a DIE for a class member. */
24953 static void
24954 gen_member_die (tree type, dw_die_ref context_die)
24956 tree member;
24957 tree binfo = TYPE_BINFO (type);
24959 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24961 /* If this is not an incomplete type, output descriptions of each of its
24962 members. Note that as we output the DIEs necessary to represent the
24963 members of this record or union type, we will also be trying to output
24964 DIEs to represent the *types* of those members. However the `type'
24965 function (above) will specifically avoid generating type DIEs for member
24966 types *within* the list of member DIEs for this (containing) type except
24967 for those types (of members) which are explicitly marked as also being
24968 members of this (containing) type themselves. The g++ front- end can
24969 force any given type to be treated as a member of some other (containing)
24970 type by setting the TYPE_CONTEXT of the given (member) type to point to
24971 the TREE node representing the appropriate (containing) type. */
24973 /* First output info about the base classes. */
24974 if (binfo)
24976 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24977 int i;
24978 tree base;
24980 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24981 gen_inheritance_die (base,
24982 (accesses ? (*accesses)[i] : access_public_node),
24983 type,
24984 context_die);
24987 /* Now output info about the data members and type members. */
24988 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24990 struct vlr_context vlr_ctx = { type, NULL_TREE };
24991 bool static_inline_p
24992 = (TREE_STATIC (member)
24993 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24994 != -1));
24996 /* Ignore clones. */
24997 if (DECL_ABSTRACT_ORIGIN (member))
24998 continue;
25000 /* If we thought we were generating minimal debug info for TYPE
25001 and then changed our minds, some of the member declarations
25002 may have already been defined. Don't define them again, but
25003 do put them in the right order. */
25005 if (dw_die_ref child = lookup_decl_die (member))
25007 /* Handle inline static data members, which only have in-class
25008 declarations. */
25009 dw_die_ref ref = NULL;
25010 if (child->die_tag == DW_TAG_variable
25011 && child->die_parent == comp_unit_die ())
25013 ref = get_AT_ref (child, DW_AT_specification);
25014 /* For C++17 inline static data members followed by redundant
25015 out of class redeclaration, we might get here with
25016 child being the DIE created for the out of class
25017 redeclaration and with its DW_AT_specification being
25018 the DIE created for in-class definition. We want to
25019 reparent the latter, and don't want to create another
25020 DIE with DW_AT_specification in that case, because
25021 we already have one. */
25022 if (ref
25023 && static_inline_p
25024 && ref->die_tag == DW_TAG_variable
25025 && ref->die_parent == comp_unit_die ()
25026 && get_AT (ref, DW_AT_specification) == NULL)
25028 child = ref;
25029 ref = NULL;
25030 static_inline_p = false;
25034 if (child->die_tag == DW_TAG_variable
25035 && child->die_parent == comp_unit_die ()
25036 && ref == NULL)
25038 reparent_child (child, context_die);
25039 if (dwarf_version < 5)
25040 child->die_tag = DW_TAG_member;
25042 else
25043 splice_child_die (context_die, child);
25046 /* Do not generate standard DWARF for variant parts if we are generating
25047 the corresponding GNAT encodings: DIEs generated for both would
25048 conflict in our mappings. */
25049 else if (is_variant_part (member)
25050 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25052 vlr_ctx.variant_part_offset = byte_position (member);
25053 gen_variant_part (member, &vlr_ctx, context_die);
25055 else
25057 vlr_ctx.variant_part_offset = NULL_TREE;
25058 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25061 /* For C++ inline static data members emit immediately a DW_TAG_variable
25062 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25063 DW_AT_specification. */
25064 if (static_inline_p)
25066 int old_extern = DECL_EXTERNAL (member);
25067 DECL_EXTERNAL (member) = 0;
25068 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25069 DECL_EXTERNAL (member) = old_extern;
25074 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25075 is set, we pretend that the type was never defined, so we only get the
25076 member DIEs needed by later specification DIEs. */
25078 static void
25079 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25080 enum debug_info_usage usage)
25082 if (TREE_ASM_WRITTEN (type))
25084 /* Fill in the bound of variable-length fields in late dwarf if
25085 still incomplete. */
25086 if (!early_dwarf && variably_modified_type_p (type, NULL))
25087 for (tree member = TYPE_FIELDS (type);
25088 member;
25089 member = DECL_CHAIN (member))
25090 fill_variable_array_bounds (TREE_TYPE (member));
25091 return;
25094 dw_die_ref type_die = lookup_type_die (type);
25095 dw_die_ref scope_die = 0;
25096 int nested = 0;
25097 int complete = (TYPE_SIZE (type)
25098 && (! TYPE_STUB_DECL (type)
25099 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25100 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25101 complete = complete && should_emit_struct_debug (type, usage);
25103 if (type_die && ! complete)
25104 return;
25106 if (TYPE_CONTEXT (type) != NULL_TREE
25107 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25108 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25109 nested = 1;
25111 scope_die = scope_die_for (type, context_die);
25113 /* Generate child dies for template paramaters. */
25114 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25115 schedule_generic_params_dies_gen (type);
25117 if (! type_die || (nested && is_cu_die (scope_die)))
25118 /* First occurrence of type or toplevel definition of nested class. */
25120 dw_die_ref old_die = type_die;
25122 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25123 ? record_type_tag (type) : DW_TAG_union_type,
25124 scope_die, type);
25125 equate_type_number_to_die (type, type_die);
25126 if (old_die)
25127 add_AT_specification (type_die, old_die);
25128 else
25129 add_name_attribute (type_die, type_tag (type));
25131 else
25132 remove_AT (type_die, DW_AT_declaration);
25134 /* If this type has been completed, then give it a byte_size attribute and
25135 then give a list of members. */
25136 if (complete && !ns_decl)
25138 /* Prevent infinite recursion in cases where the type of some member of
25139 this type is expressed in terms of this type itself. */
25140 TREE_ASM_WRITTEN (type) = 1;
25141 add_byte_size_attribute (type_die, type);
25142 add_alignment_attribute (type_die, type);
25143 if (TYPE_STUB_DECL (type) != NULL_TREE)
25145 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25146 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25149 /* If the first reference to this type was as the return type of an
25150 inline function, then it may not have a parent. Fix this now. */
25151 if (type_die->die_parent == NULL)
25152 add_child_die (scope_die, type_die);
25154 gen_member_die (type, type_die);
25156 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25157 if (TYPE_ARTIFICIAL (type))
25158 add_AT_flag (type_die, DW_AT_artificial, 1);
25160 /* GNU extension: Record what type our vtable lives in. */
25161 if (TYPE_VFIELD (type))
25163 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25165 gen_type_die (vtype, context_die);
25166 add_AT_die_ref (type_die, DW_AT_containing_type,
25167 lookup_type_die (vtype));
25170 else
25172 add_AT_flag (type_die, DW_AT_declaration, 1);
25174 /* We don't need to do this for function-local types. */
25175 if (TYPE_STUB_DECL (type)
25176 && ! decl_function_context (TYPE_STUB_DECL (type)))
25177 vec_safe_push (incomplete_types, type);
25180 if (get_AT (type_die, DW_AT_name))
25181 add_pubtype (type, type_die);
25184 /* Generate a DIE for a subroutine _type_. */
25186 static void
25187 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25189 tree return_type = TREE_TYPE (type);
25190 dw_die_ref subr_die
25191 = new_die (DW_TAG_subroutine_type,
25192 scope_die_for (type, context_die), type);
25194 equate_type_number_to_die (type, subr_die);
25195 add_prototyped_attribute (subr_die, type);
25196 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25197 context_die);
25198 add_alignment_attribute (subr_die, type);
25199 gen_formal_types_die (type, subr_die);
25201 if (get_AT (subr_die, DW_AT_name))
25202 add_pubtype (type, subr_die);
25203 if ((dwarf_version >= 5 || !dwarf_strict)
25204 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25205 add_AT_flag (subr_die, DW_AT_reference, 1);
25206 if ((dwarf_version >= 5 || !dwarf_strict)
25207 && lang_hooks.types.type_dwarf_attribute (type,
25208 DW_AT_rvalue_reference) != -1)
25209 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25212 /* Generate a DIE for a type definition. */
25214 static void
25215 gen_typedef_die (tree decl, dw_die_ref context_die)
25217 dw_die_ref type_die;
25218 tree type;
25220 if (TREE_ASM_WRITTEN (decl))
25222 if (DECL_ORIGINAL_TYPE (decl))
25223 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25224 return;
25227 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25228 checks in process_scope_var and modified_type_die), this should be called
25229 only for original types. */
25230 gcc_assert (decl_ultimate_origin (decl) == NULL
25231 || decl_ultimate_origin (decl) == decl);
25233 TREE_ASM_WRITTEN (decl) = 1;
25234 type_die = new_die (DW_TAG_typedef, context_die, decl);
25236 add_name_and_src_coords_attributes (type_die, decl);
25237 if (DECL_ORIGINAL_TYPE (decl))
25239 type = DECL_ORIGINAL_TYPE (decl);
25240 if (type == error_mark_node)
25241 return;
25243 gcc_assert (type != TREE_TYPE (decl));
25244 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25246 else
25248 type = TREE_TYPE (decl);
25249 if (type == error_mark_node)
25250 return;
25252 if (is_naming_typedef_decl (TYPE_NAME (type)))
25254 /* Here, we are in the case of decl being a typedef naming
25255 an anonymous type, e.g:
25256 typedef struct {...} foo;
25257 In that case TREE_TYPE (decl) is not a typedef variant
25258 type and TYPE_NAME of the anonymous type is set to the
25259 TYPE_DECL of the typedef. This construct is emitted by
25260 the C++ FE.
25262 TYPE is the anonymous struct named by the typedef
25263 DECL. As we need the DW_AT_type attribute of the
25264 DW_TAG_typedef to point to the DIE of TYPE, let's
25265 generate that DIE right away. add_type_attribute
25266 called below will then pick (via lookup_type_die) that
25267 anonymous struct DIE. */
25268 if (!TREE_ASM_WRITTEN (type))
25269 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25271 /* This is a GNU Extension. We are adding a
25272 DW_AT_linkage_name attribute to the DIE of the
25273 anonymous struct TYPE. The value of that attribute
25274 is the name of the typedef decl naming the anonymous
25275 struct. This greatly eases the work of consumers of
25276 this debug info. */
25277 add_linkage_name_raw (lookup_type_die (type), decl);
25281 add_type_attribute (type_die, type, decl_quals (decl), false,
25282 context_die);
25284 if (is_naming_typedef_decl (decl))
25285 /* We want that all subsequent calls to lookup_type_die with
25286 TYPE in argument yield the DW_TAG_typedef we have just
25287 created. */
25288 equate_type_number_to_die (type, type_die);
25290 add_alignment_attribute (type_die, TREE_TYPE (decl));
25292 add_accessibility_attribute (type_die, decl);
25294 if (DECL_ABSTRACT_P (decl))
25295 equate_decl_number_to_die (decl, type_die);
25297 if (get_AT (type_die, DW_AT_name))
25298 add_pubtype (decl, type_die);
25301 /* Generate a DIE for a struct, class, enum or union type. */
25303 static void
25304 gen_tagged_type_die (tree type,
25305 dw_die_ref context_die,
25306 enum debug_info_usage usage)
25308 if (type == NULL_TREE
25309 || !is_tagged_type (type))
25310 return;
25312 if (TREE_ASM_WRITTEN (type))
25314 /* If this is a nested type whose containing class hasn't been written
25315 out yet, writing it out will cover this one, too. This does not apply
25316 to instantiations of member class templates; they need to be added to
25317 the containing class as they are generated. FIXME: This hurts the
25318 idea of combining type decls from multiple TUs, since we can't predict
25319 what set of template instantiations we'll get. */
25320 else if (TYPE_CONTEXT (type)
25321 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25322 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25324 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25326 if (TREE_ASM_WRITTEN (type))
25327 return;
25329 /* If that failed, attach ourselves to the stub. */
25330 context_die = lookup_type_die (TYPE_CONTEXT (type));
25332 else if (TYPE_CONTEXT (type) != NULL_TREE
25333 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25335 /* If this type is local to a function that hasn't been written
25336 out yet, use a NULL context for now; it will be fixed up in
25337 decls_for_scope. */
25338 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25339 /* A declaration DIE doesn't count; nested types need to go in the
25340 specification. */
25341 if (context_die && is_declaration_die (context_die))
25342 context_die = NULL;
25344 else
25345 context_die = declare_in_namespace (type, context_die);
25347 if (TREE_CODE (type) == ENUMERAL_TYPE)
25349 /* This might have been written out by the call to
25350 declare_in_namespace. */
25351 if (!TREE_ASM_WRITTEN (type))
25352 gen_enumeration_type_die (type, context_die);
25354 else
25355 gen_struct_or_union_type_die (type, context_die, usage);
25357 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25358 it up if it is ever completed. gen_*_type_die will set it for us
25359 when appropriate. */
25362 /* Generate a type description DIE. */
25364 static void
25365 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25366 enum debug_info_usage usage)
25368 struct array_descr_info info;
25370 if (type == NULL_TREE || type == error_mark_node)
25371 return;
25373 if (flag_checking && type)
25374 verify_type (type);
25376 if (TYPE_NAME (type) != NULL_TREE
25377 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25378 && is_redundant_typedef (TYPE_NAME (type))
25379 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25380 /* The DECL of this type is a typedef we don't want to emit debug
25381 info for but we want debug info for its underlying typedef.
25382 This can happen for e.g, the injected-class-name of a C++
25383 type. */
25384 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25386 /* If TYPE is a typedef type variant, let's generate debug info
25387 for the parent typedef which TYPE is a type of. */
25388 if (typedef_variant_p (type))
25390 if (TREE_ASM_WRITTEN (type))
25391 return;
25393 tree name = TYPE_NAME (type);
25394 tree origin = decl_ultimate_origin (name);
25395 if (origin != NULL && origin != name)
25397 gen_decl_die (origin, NULL, NULL, context_die);
25398 return;
25401 /* Prevent broken recursion; we can't hand off to the same type. */
25402 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25404 /* Give typedefs the right scope. */
25405 context_die = scope_die_for (type, context_die);
25407 TREE_ASM_WRITTEN (type) = 1;
25409 gen_decl_die (name, NULL, NULL, context_die);
25410 return;
25413 /* If type is an anonymous tagged type named by a typedef, let's
25414 generate debug info for the typedef. */
25415 if (is_naming_typedef_decl (TYPE_NAME (type)))
25417 /* Give typedefs the right scope. */
25418 context_die = scope_die_for (type, context_die);
25420 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25421 return;
25424 if (lang_hooks.types.get_debug_type)
25426 tree debug_type = lang_hooks.types.get_debug_type (type);
25428 if (debug_type != NULL_TREE && debug_type != type)
25430 gen_type_die_with_usage (debug_type, context_die, usage);
25431 return;
25435 /* We are going to output a DIE to represent the unqualified version
25436 of this type (i.e. without any const or volatile qualifiers) so
25437 get the main variant (i.e. the unqualified version) of this type
25438 now. (Vectors and arrays are special because the debugging info is in the
25439 cloned type itself. Similarly function/method types can contain extra
25440 ref-qualification). */
25441 if (TREE_CODE (type) == FUNCTION_TYPE
25442 || TREE_CODE (type) == METHOD_TYPE)
25444 /* For function/method types, can't use type_main_variant here,
25445 because that can have different ref-qualifiers for C++,
25446 but try to canonicalize. */
25447 tree main = TYPE_MAIN_VARIANT (type);
25448 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25449 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25450 && check_base_type (t, main)
25451 && check_lang_type (t, type))
25453 type = t;
25454 break;
25457 else if (TREE_CODE (type) != VECTOR_TYPE
25458 && TREE_CODE (type) != ARRAY_TYPE)
25459 type = type_main_variant (type);
25461 /* If this is an array type with hidden descriptor, handle it first. */
25462 if (!TREE_ASM_WRITTEN (type)
25463 && lang_hooks.types.get_array_descr_info)
25465 memset (&info, 0, sizeof (info));
25466 if (lang_hooks.types.get_array_descr_info (type, &info))
25468 /* Fortran sometimes emits array types with no dimension. */
25469 gcc_assert (info.ndimensions >= 0
25470 && (info.ndimensions
25471 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25472 gen_descr_array_type_die (type, &info, context_die);
25473 TREE_ASM_WRITTEN (type) = 1;
25474 return;
25478 if (TREE_ASM_WRITTEN (type))
25480 /* Variable-length types may be incomplete even if
25481 TREE_ASM_WRITTEN. For such types, fall through to
25482 gen_array_type_die() and possibly fill in
25483 DW_AT_{upper,lower}_bound attributes. */
25484 if ((TREE_CODE (type) != ARRAY_TYPE
25485 && TREE_CODE (type) != RECORD_TYPE
25486 && TREE_CODE (type) != UNION_TYPE
25487 && TREE_CODE (type) != QUAL_UNION_TYPE)
25488 || !variably_modified_type_p (type, NULL))
25489 return;
25492 switch (TREE_CODE (type))
25494 case ERROR_MARK:
25495 break;
25497 case POINTER_TYPE:
25498 case REFERENCE_TYPE:
25499 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25500 ensures that the gen_type_die recursion will terminate even if the
25501 type is recursive. Recursive types are possible in Ada. */
25502 /* ??? We could perhaps do this for all types before the switch
25503 statement. */
25504 TREE_ASM_WRITTEN (type) = 1;
25506 /* For these types, all that is required is that we output a DIE (or a
25507 set of DIEs) to represent the "basis" type. */
25508 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25509 DINFO_USAGE_IND_USE);
25510 break;
25512 case OFFSET_TYPE:
25513 /* This code is used for C++ pointer-to-data-member types.
25514 Output a description of the relevant class type. */
25515 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25516 DINFO_USAGE_IND_USE);
25518 /* Output a description of the type of the object pointed to. */
25519 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25520 DINFO_USAGE_IND_USE);
25522 /* Now output a DIE to represent this pointer-to-data-member type
25523 itself. */
25524 gen_ptr_to_mbr_type_die (type, context_die);
25525 break;
25527 case FUNCTION_TYPE:
25528 /* Force out return type (in case it wasn't forced out already). */
25529 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25530 DINFO_USAGE_DIR_USE);
25531 gen_subroutine_type_die (type, context_die);
25532 break;
25534 case METHOD_TYPE:
25535 /* Force out return type (in case it wasn't forced out already). */
25536 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25537 DINFO_USAGE_DIR_USE);
25538 gen_subroutine_type_die (type, context_die);
25539 break;
25541 case ARRAY_TYPE:
25542 case VECTOR_TYPE:
25543 gen_array_type_die (type, context_die);
25544 break;
25546 case ENUMERAL_TYPE:
25547 case RECORD_TYPE:
25548 case UNION_TYPE:
25549 case QUAL_UNION_TYPE:
25550 gen_tagged_type_die (type, context_die, usage);
25551 return;
25553 case VOID_TYPE:
25554 case INTEGER_TYPE:
25555 case REAL_TYPE:
25556 case FIXED_POINT_TYPE:
25557 case COMPLEX_TYPE:
25558 case BOOLEAN_TYPE:
25559 /* No DIEs needed for fundamental types. */
25560 break;
25562 case NULLPTR_TYPE:
25563 case LANG_TYPE:
25564 /* Just use DW_TAG_unspecified_type. */
25566 dw_die_ref type_die = lookup_type_die (type);
25567 if (type_die == NULL)
25569 tree name = TYPE_IDENTIFIER (type);
25570 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25571 type);
25572 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25573 equate_type_number_to_die (type, type_die);
25576 break;
25578 default:
25579 if (is_cxx_auto (type))
25581 tree name = TYPE_IDENTIFIER (type);
25582 dw_die_ref *die = (name == get_identifier ("auto")
25583 ? &auto_die : &decltype_auto_die);
25584 if (!*die)
25586 *die = new_die (DW_TAG_unspecified_type,
25587 comp_unit_die (), NULL_TREE);
25588 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25590 equate_type_number_to_die (type, *die);
25591 break;
25593 gcc_unreachable ();
25596 TREE_ASM_WRITTEN (type) = 1;
25599 static void
25600 gen_type_die (tree type, dw_die_ref context_die)
25602 if (type != error_mark_node)
25604 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25605 if (flag_checking)
25607 dw_die_ref die = lookup_type_die (type);
25608 if (die)
25609 check_die (die);
25614 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25615 things which are local to the given block. */
25617 static void
25618 gen_block_die (tree stmt, dw_die_ref context_die)
25620 int must_output_die = 0;
25621 bool inlined_func;
25623 /* Ignore blocks that are NULL. */
25624 if (stmt == NULL_TREE)
25625 return;
25627 inlined_func = inlined_function_outer_scope_p (stmt);
25629 /* If the block is one fragment of a non-contiguous block, do not
25630 process the variables, since they will have been done by the
25631 origin block. Do process subblocks. */
25632 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25634 tree sub;
25636 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25637 gen_block_die (sub, context_die);
25639 return;
25642 /* Determine if we need to output any Dwarf DIEs at all to represent this
25643 block. */
25644 if (inlined_func)
25645 /* The outer scopes for inlinings *must* always be represented. We
25646 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25647 must_output_die = 1;
25648 else if (lookup_block_die (stmt))
25649 /* If we already have a DIE then it was filled early. Meanwhile
25650 we might have pruned all BLOCK_VARS as optimized out but we
25651 still want to generate high/low PC attributes so output it. */
25652 must_output_die = 1;
25653 else if (TREE_USED (stmt)
25654 || TREE_ASM_WRITTEN (stmt))
25656 /* Determine if this block directly contains any "significant"
25657 local declarations which we will need to output DIEs for. */
25658 if (debug_info_level > DINFO_LEVEL_TERSE)
25660 /* We are not in terse mode so any local declaration that
25661 is not ignored for debug purposes counts as being a
25662 "significant" one. */
25663 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25664 must_output_die = 1;
25665 else
25666 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25667 if (!DECL_IGNORED_P (var))
25669 must_output_die = 1;
25670 break;
25673 else if (!dwarf2out_ignore_block (stmt))
25674 must_output_die = 1;
25677 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25678 DIE for any block which contains no significant local declarations at
25679 all. Rather, in such cases we just call `decls_for_scope' so that any
25680 needed Dwarf info for any sub-blocks will get properly generated. Note
25681 that in terse mode, our definition of what constitutes a "significant"
25682 local declaration gets restricted to include only inlined function
25683 instances and local (nested) function definitions. */
25684 if (must_output_die)
25686 if (inlined_func)
25687 gen_inlined_subroutine_die (stmt, context_die);
25688 else
25689 gen_lexical_block_die (stmt, context_die);
25691 else
25692 decls_for_scope (stmt, context_die);
25695 /* Process variable DECL (or variable with origin ORIGIN) within
25696 block STMT and add it to CONTEXT_DIE. */
25697 static void
25698 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25700 dw_die_ref die;
25701 tree decl_or_origin = decl ? decl : origin;
25703 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25704 die = lookup_decl_die (decl_or_origin);
25705 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25707 if (TYPE_DECL_IS_STUB (decl_or_origin))
25708 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25709 else
25710 die = lookup_decl_die (decl_or_origin);
25711 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25712 if (! die && ! early_dwarf)
25713 return;
25715 else
25716 die = NULL;
25718 /* Avoid creating DIEs for local typedefs and concrete static variables that
25719 will only be pruned later. */
25720 if ((origin || decl_ultimate_origin (decl))
25721 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25722 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25724 origin = decl_ultimate_origin (decl_or_origin);
25725 if (decl && VAR_P (decl) && die != NULL)
25727 die = lookup_decl_die (origin);
25728 if (die != NULL)
25729 equate_decl_number_to_die (decl, die);
25731 return;
25734 if (die != NULL && die->die_parent == NULL)
25735 add_child_die (context_die, die);
25736 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25738 if (early_dwarf)
25739 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25740 stmt, context_die);
25742 else
25744 if (decl && DECL_P (decl))
25746 die = lookup_decl_die (decl);
25748 /* Early created DIEs do not have a parent as the decls refer
25749 to the function as DECL_CONTEXT rather than the BLOCK. */
25750 if (die && die->die_parent == NULL)
25752 gcc_assert (in_lto_p);
25753 add_child_die (context_die, die);
25757 gen_decl_die (decl, origin, NULL, context_die);
25761 /* Generate all of the decls declared within a given scope and (recursively)
25762 all of its sub-blocks. */
25764 static void
25765 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
25767 tree decl;
25768 unsigned int i;
25769 tree subblocks;
25771 /* Ignore NULL blocks. */
25772 if (stmt == NULL_TREE)
25773 return;
25775 /* Output the DIEs to represent all of the data objects and typedefs
25776 declared directly within this block but not within any nested
25777 sub-blocks. Also, nested function and tag DIEs have been
25778 generated with a parent of NULL; fix that up now. We don't
25779 have to do this if we're at -g1. */
25780 if (debug_info_level > DINFO_LEVEL_TERSE)
25782 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25783 process_scope_var (stmt, decl, NULL_TREE, context_die);
25784 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25785 origin - avoid doing this twice as we have no good way to see
25786 if we've done it once already. */
25787 if (! early_dwarf)
25788 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25790 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25791 if (decl == current_function_decl)
25792 /* Ignore declarations of the current function, while they
25793 are declarations, gen_subprogram_die would treat them
25794 as definitions again, because they are equal to
25795 current_function_decl and endlessly recurse. */;
25796 else if (TREE_CODE (decl) == FUNCTION_DECL)
25797 process_scope_var (stmt, decl, NULL_TREE, context_die);
25798 else
25799 process_scope_var (stmt, NULL_TREE, decl, context_die);
25803 /* Even if we're at -g1, we need to process the subblocks in order to get
25804 inlined call information. */
25806 /* Output the DIEs to represent all sub-blocks (and the items declared
25807 therein) of this block. */
25808 if (recurse)
25809 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25810 subblocks != NULL;
25811 subblocks = BLOCK_CHAIN (subblocks))
25812 gen_block_die (subblocks, context_die);
25815 /* Is this a typedef we can avoid emitting? */
25817 static bool
25818 is_redundant_typedef (const_tree decl)
25820 if (TYPE_DECL_IS_STUB (decl))
25821 return true;
25823 if (DECL_ARTIFICIAL (decl)
25824 && DECL_CONTEXT (decl)
25825 && is_tagged_type (DECL_CONTEXT (decl))
25826 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25827 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25828 /* Also ignore the artificial member typedef for the class name. */
25829 return true;
25831 return false;
25834 /* Return TRUE if TYPE is a typedef that names a type for linkage
25835 purposes. This kind of typedefs is produced by the C++ FE for
25836 constructs like:
25838 typedef struct {...} foo;
25840 In that case, there is no typedef variant type produced for foo.
25841 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25842 struct type. */
25844 static bool
25845 is_naming_typedef_decl (const_tree decl)
25847 if (decl == NULL_TREE
25848 || TREE_CODE (decl) != TYPE_DECL
25849 || DECL_NAMELESS (decl)
25850 || !is_tagged_type (TREE_TYPE (decl))
25851 || DECL_IS_BUILTIN (decl)
25852 || is_redundant_typedef (decl)
25853 /* It looks like Ada produces TYPE_DECLs that are very similar
25854 to C++ naming typedefs but that have different
25855 semantics. Let's be specific to c++ for now. */
25856 || !is_cxx (decl))
25857 return FALSE;
25859 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25860 && TYPE_NAME (TREE_TYPE (decl)) == decl
25861 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25862 != TYPE_NAME (TREE_TYPE (decl))));
25865 /* Looks up the DIE for a context. */
25867 static inline dw_die_ref
25868 lookup_context_die (tree context)
25870 if (context)
25872 /* Find die that represents this context. */
25873 if (TYPE_P (context))
25875 context = TYPE_MAIN_VARIANT (context);
25876 dw_die_ref ctx = lookup_type_die (context);
25877 if (!ctx)
25878 return NULL;
25879 return strip_naming_typedef (context, ctx);
25881 else
25882 return lookup_decl_die (context);
25884 return comp_unit_die ();
25887 /* Returns the DIE for a context. */
25889 static inline dw_die_ref
25890 get_context_die (tree context)
25892 if (context)
25894 /* Find die that represents this context. */
25895 if (TYPE_P (context))
25897 context = TYPE_MAIN_VARIANT (context);
25898 return strip_naming_typedef (context, force_type_die (context));
25900 else
25901 return force_decl_die (context);
25903 return comp_unit_die ();
25906 /* Returns the DIE for decl. A DIE will always be returned. */
25908 static dw_die_ref
25909 force_decl_die (tree decl)
25911 dw_die_ref decl_die;
25912 unsigned saved_external_flag;
25913 tree save_fn = NULL_TREE;
25914 decl_die = lookup_decl_die (decl);
25915 if (!decl_die)
25917 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25919 decl_die = lookup_decl_die (decl);
25920 if (decl_die)
25921 return decl_die;
25923 switch (TREE_CODE (decl))
25925 case FUNCTION_DECL:
25926 /* Clear current_function_decl, so that gen_subprogram_die thinks
25927 that this is a declaration. At this point, we just want to force
25928 declaration die. */
25929 save_fn = current_function_decl;
25930 current_function_decl = NULL_TREE;
25931 gen_subprogram_die (decl, context_die);
25932 current_function_decl = save_fn;
25933 break;
25935 case VAR_DECL:
25936 /* Set external flag to force declaration die. Restore it after
25937 gen_decl_die() call. */
25938 saved_external_flag = DECL_EXTERNAL (decl);
25939 DECL_EXTERNAL (decl) = 1;
25940 gen_decl_die (decl, NULL, NULL, context_die);
25941 DECL_EXTERNAL (decl) = saved_external_flag;
25942 break;
25944 case NAMESPACE_DECL:
25945 if (dwarf_version >= 3 || !dwarf_strict)
25946 dwarf2out_decl (decl);
25947 else
25948 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25949 decl_die = comp_unit_die ();
25950 break;
25952 case TRANSLATION_UNIT_DECL:
25953 decl_die = comp_unit_die ();
25954 break;
25956 default:
25957 gcc_unreachable ();
25960 /* We should be able to find the DIE now. */
25961 if (!decl_die)
25962 decl_die = lookup_decl_die (decl);
25963 gcc_assert (decl_die);
25966 return decl_die;
25969 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25970 always returned. */
25972 static dw_die_ref
25973 force_type_die (tree type)
25975 dw_die_ref type_die;
25977 type_die = lookup_type_die (type);
25978 if (!type_die)
25980 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25982 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25983 false, context_die);
25984 gcc_assert (type_die);
25986 return type_die;
25989 /* Force out any required namespaces to be able to output DECL,
25990 and return the new context_die for it, if it's changed. */
25992 static dw_die_ref
25993 setup_namespace_context (tree thing, dw_die_ref context_die)
25995 tree context = (DECL_P (thing)
25996 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25997 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25998 /* Force out the namespace. */
25999 context_die = force_decl_die (context);
26001 return context_die;
26004 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26005 type) within its namespace, if appropriate.
26007 For compatibility with older debuggers, namespace DIEs only contain
26008 declarations; all definitions are emitted at CU scope, with
26009 DW_AT_specification pointing to the declaration (like with class
26010 members). */
26012 static dw_die_ref
26013 declare_in_namespace (tree thing, dw_die_ref context_die)
26015 dw_die_ref ns_context;
26017 if (debug_info_level <= DINFO_LEVEL_TERSE)
26018 return context_die;
26020 /* External declarations in the local scope only need to be emitted
26021 once, not once in the namespace and once in the scope.
26023 This avoids declaring the `extern' below in the
26024 namespace DIE as well as in the innermost scope:
26026 namespace S
26028 int i=5;
26029 int foo()
26031 int i=8;
26032 extern int i;
26033 return i;
26037 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26038 return context_die;
26040 /* If this decl is from an inlined function, then don't try to emit it in its
26041 namespace, as we will get confused. It would have already been emitted
26042 when the abstract instance of the inline function was emitted anyways. */
26043 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26044 return context_die;
26046 ns_context = setup_namespace_context (thing, context_die);
26048 if (ns_context != context_die)
26050 if (is_fortran () || is_dlang ())
26051 return ns_context;
26052 if (DECL_P (thing))
26053 gen_decl_die (thing, NULL, NULL, ns_context);
26054 else
26055 gen_type_die (thing, ns_context);
26057 return context_die;
26060 /* Generate a DIE for a namespace or namespace alias. */
26062 static void
26063 gen_namespace_die (tree decl, dw_die_ref context_die)
26065 dw_die_ref namespace_die;
26067 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26068 they are an alias of. */
26069 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26071 /* Output a real namespace or module. */
26072 context_die = setup_namespace_context (decl, comp_unit_die ());
26073 namespace_die = new_die (is_fortran () || is_dlang ()
26074 ? DW_TAG_module : DW_TAG_namespace,
26075 context_die, decl);
26076 /* For Fortran modules defined in different CU don't add src coords. */
26077 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26079 const char *name = dwarf2_name (decl, 0);
26080 if (name)
26081 add_name_attribute (namespace_die, name);
26083 else
26084 add_name_and_src_coords_attributes (namespace_die, decl);
26085 if (DECL_EXTERNAL (decl))
26086 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26087 equate_decl_number_to_die (decl, namespace_die);
26089 else
26091 /* Output a namespace alias. */
26093 /* Force out the namespace we are an alias of, if necessary. */
26094 dw_die_ref origin_die
26095 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26097 if (DECL_FILE_SCOPE_P (decl)
26098 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26099 context_die = setup_namespace_context (decl, comp_unit_die ());
26100 /* Now create the namespace alias DIE. */
26101 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26102 add_name_and_src_coords_attributes (namespace_die, decl);
26103 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26104 equate_decl_number_to_die (decl, namespace_die);
26106 if ((dwarf_version >= 5 || !dwarf_strict)
26107 && lang_hooks.decls.decl_dwarf_attribute (decl,
26108 DW_AT_export_symbols) == 1)
26109 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26111 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26112 if (want_pubnames ())
26113 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26116 /* Generate Dwarf debug information for a decl described by DECL.
26117 The return value is currently only meaningful for PARM_DECLs,
26118 for all other decls it returns NULL.
26120 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26121 It can be NULL otherwise. */
26123 static dw_die_ref
26124 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26125 dw_die_ref context_die)
26127 tree decl_or_origin = decl ? decl : origin;
26128 tree class_origin = NULL, ultimate_origin;
26130 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26131 return NULL;
26133 switch (TREE_CODE (decl_or_origin))
26135 case ERROR_MARK:
26136 break;
26138 case CONST_DECL:
26139 if (!is_fortran () && !is_ada () && !is_dlang ())
26141 /* The individual enumerators of an enum type get output when we output
26142 the Dwarf representation of the relevant enum type itself. */
26143 break;
26146 /* Emit its type. */
26147 gen_type_die (TREE_TYPE (decl), context_die);
26149 /* And its containing namespace. */
26150 context_die = declare_in_namespace (decl, context_die);
26152 gen_const_die (decl, context_die);
26153 break;
26155 case FUNCTION_DECL:
26156 #if 0
26157 /* FIXME */
26158 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26159 on local redeclarations of global functions. That seems broken. */
26160 if (current_function_decl != decl)
26161 /* This is only a declaration. */;
26162 #endif
26164 /* We should have abstract copies already and should not generate
26165 stray type DIEs in late LTO dumping. */
26166 if (! early_dwarf)
26169 /* If we're emitting a clone, emit info for the abstract instance. */
26170 else if (origin || DECL_ORIGIN (decl) != decl)
26171 dwarf2out_abstract_function (origin
26172 ? DECL_ORIGIN (origin)
26173 : DECL_ABSTRACT_ORIGIN (decl));
26175 /* If we're emitting a possibly inlined function emit it as
26176 abstract instance. */
26177 else if (cgraph_function_possibly_inlined_p (decl)
26178 && ! DECL_ABSTRACT_P (decl)
26179 && ! class_or_namespace_scope_p (context_die)
26180 /* dwarf2out_abstract_function won't emit a die if this is just
26181 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26182 that case, because that works only if we have a die. */
26183 && DECL_INITIAL (decl) != NULL_TREE)
26184 dwarf2out_abstract_function (decl);
26186 /* Otherwise we're emitting the primary DIE for this decl. */
26187 else if (debug_info_level > DINFO_LEVEL_TERSE)
26189 /* Before we describe the FUNCTION_DECL itself, make sure that we
26190 have its containing type. */
26191 if (!origin)
26192 origin = decl_class_context (decl);
26193 if (origin != NULL_TREE)
26194 gen_type_die (origin, context_die);
26196 /* And its return type. */
26197 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26199 /* And its virtual context. */
26200 if (DECL_VINDEX (decl) != NULL_TREE)
26201 gen_type_die (DECL_CONTEXT (decl), context_die);
26203 /* Make sure we have a member DIE for decl. */
26204 if (origin != NULL_TREE)
26205 gen_type_die_for_member (origin, decl, context_die);
26207 /* And its containing namespace. */
26208 context_die = declare_in_namespace (decl, context_die);
26211 /* Now output a DIE to represent the function itself. */
26212 if (decl)
26213 gen_subprogram_die (decl, context_die);
26214 break;
26216 case TYPE_DECL:
26217 /* If we are in terse mode, don't generate any DIEs to represent any
26218 actual typedefs. */
26219 if (debug_info_level <= DINFO_LEVEL_TERSE)
26220 break;
26222 /* In the special case of a TYPE_DECL node representing the declaration
26223 of some type tag, if the given TYPE_DECL is marked as having been
26224 instantiated from some other (original) TYPE_DECL node (e.g. one which
26225 was generated within the original definition of an inline function) we
26226 used to generate a special (abbreviated) DW_TAG_structure_type,
26227 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26228 should be actually referencing those DIEs, as variable DIEs with that
26229 type would be emitted already in the abstract origin, so it was always
26230 removed during unused type prunning. Don't add anything in this
26231 case. */
26232 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26233 break;
26235 if (is_redundant_typedef (decl))
26236 gen_type_die (TREE_TYPE (decl), context_die);
26237 else
26238 /* Output a DIE to represent the typedef itself. */
26239 gen_typedef_die (decl, context_die);
26240 break;
26242 case LABEL_DECL:
26243 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26244 gen_label_die (decl, context_die);
26245 break;
26247 case VAR_DECL:
26248 case RESULT_DECL:
26249 /* If we are in terse mode, don't generate any DIEs to represent any
26250 variable declarations or definitions. */
26251 if (debug_info_level <= DINFO_LEVEL_TERSE)
26252 break;
26254 /* Avoid generating stray type DIEs during late dwarf dumping.
26255 All types have been dumped early. */
26256 if (early_dwarf
26257 /* ??? But in LTRANS we cannot annotate early created variably
26258 modified type DIEs without copying them and adjusting all
26259 references to them. Dump them again as happens for inlining
26260 which copies both the decl and the types. */
26261 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26262 in VLA bound information for example. */
26263 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26264 current_function_decl)))
26266 /* Output any DIEs that are needed to specify the type of this data
26267 object. */
26268 if (decl_by_reference_p (decl_or_origin))
26269 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26270 else
26271 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26274 if (early_dwarf)
26276 /* And its containing type. */
26277 class_origin = decl_class_context (decl_or_origin);
26278 if (class_origin != NULL_TREE)
26279 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26281 /* And its containing namespace. */
26282 context_die = declare_in_namespace (decl_or_origin, context_die);
26285 /* Now output the DIE to represent the data object itself. This gets
26286 complicated because of the possibility that the VAR_DECL really
26287 represents an inlined instance of a formal parameter for an inline
26288 function. */
26289 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26290 if (ultimate_origin != NULL_TREE
26291 && TREE_CODE (ultimate_origin) == PARM_DECL)
26292 gen_formal_parameter_die (decl, origin,
26293 true /* Emit name attribute. */,
26294 context_die);
26295 else
26296 gen_variable_die (decl, origin, context_die);
26297 break;
26299 case FIELD_DECL:
26300 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26301 /* Ignore the nameless fields that are used to skip bits but handle C++
26302 anonymous unions and structs. */
26303 if (DECL_NAME (decl) != NULL_TREE
26304 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26305 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26307 gen_type_die (member_declared_type (decl), context_die);
26308 gen_field_die (decl, ctx, context_die);
26310 break;
26312 case PARM_DECL:
26313 /* Avoid generating stray type DIEs during late dwarf dumping.
26314 All types have been dumped early. */
26315 if (early_dwarf
26316 /* ??? But in LTRANS we cannot annotate early created variably
26317 modified type DIEs without copying them and adjusting all
26318 references to them. Dump them again as happens for inlining
26319 which copies both the decl and the types. */
26320 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26321 in VLA bound information for example. */
26322 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26323 current_function_decl)))
26325 if (DECL_BY_REFERENCE (decl_or_origin))
26326 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26327 else
26328 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26330 return gen_formal_parameter_die (decl, origin,
26331 true /* Emit name attribute. */,
26332 context_die);
26334 case NAMESPACE_DECL:
26335 if (dwarf_version >= 3 || !dwarf_strict)
26336 gen_namespace_die (decl, context_die);
26337 break;
26339 case IMPORTED_DECL:
26340 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26341 DECL_CONTEXT (decl), context_die);
26342 break;
26344 case NAMELIST_DECL:
26345 gen_namelist_decl (DECL_NAME (decl), context_die,
26346 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26347 break;
26349 default:
26350 /* Probably some frontend-internal decl. Assume we don't care. */
26351 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26352 break;
26355 return NULL;
26358 /* Output initial debug information for global DECL. Called at the
26359 end of the parsing process.
26361 This is the initial debug generation process. As such, the DIEs
26362 generated may be incomplete. A later debug generation pass
26363 (dwarf2out_late_global_decl) will augment the information generated
26364 in this pass (e.g., with complete location info). */
26366 static void
26367 dwarf2out_early_global_decl (tree decl)
26369 set_early_dwarf s;
26371 /* gen_decl_die() will set DECL_ABSTRACT because
26372 cgraph_function_possibly_inlined_p() returns true. This is in
26373 turn will cause DW_AT_inline attributes to be set.
26375 This happens because at early dwarf generation, there is no
26376 cgraph information, causing cgraph_function_possibly_inlined_p()
26377 to return true. Trick cgraph_function_possibly_inlined_p()
26378 while we generate dwarf early. */
26379 bool save = symtab->global_info_ready;
26380 symtab->global_info_ready = true;
26382 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26383 other DECLs and they can point to template types or other things
26384 that dwarf2out can't handle when done via dwarf2out_decl. */
26385 if (TREE_CODE (decl) != TYPE_DECL
26386 && TREE_CODE (decl) != PARM_DECL)
26388 if (TREE_CODE (decl) == FUNCTION_DECL)
26390 tree save_fndecl = current_function_decl;
26392 /* For nested functions, make sure we have DIEs for the parents first
26393 so that all nested DIEs are generated at the proper scope in the
26394 first shot. */
26395 tree context = decl_function_context (decl);
26396 if (context != NULL)
26398 dw_die_ref context_die = lookup_decl_die (context);
26399 current_function_decl = context;
26401 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26402 enough so that it lands in its own context. This avoids type
26403 pruning issues later on. */
26404 if (context_die == NULL || is_declaration_die (context_die))
26405 dwarf2out_decl (context);
26408 /* Emit an abstract origin of a function first. This happens
26409 with C++ constructor clones for example and makes
26410 dwarf2out_abstract_function happy which requires the early
26411 DIE of the abstract instance to be present. */
26412 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26413 dw_die_ref origin_die;
26414 if (origin != NULL
26415 /* Do not emit the DIE multiple times but make sure to
26416 process it fully here in case we just saw a declaration. */
26417 && ((origin_die = lookup_decl_die (origin)) == NULL
26418 || is_declaration_die (origin_die)))
26420 current_function_decl = origin;
26421 dwarf2out_decl (origin);
26424 /* Emit the DIE for decl but avoid doing that multiple times. */
26425 dw_die_ref old_die;
26426 if ((old_die = lookup_decl_die (decl)) == NULL
26427 || is_declaration_die (old_die))
26429 current_function_decl = decl;
26430 dwarf2out_decl (decl);
26433 current_function_decl = save_fndecl;
26435 else
26436 dwarf2out_decl (decl);
26438 symtab->global_info_ready = save;
26441 /* Return whether EXPR is an expression with the following pattern:
26442 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26444 static bool
26445 is_trivial_indirect_ref (tree expr)
26447 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26448 return false;
26450 tree nop = TREE_OPERAND (expr, 0);
26451 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26452 return false;
26454 tree int_cst = TREE_OPERAND (nop, 0);
26455 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26458 /* Output debug information for global decl DECL. Called from
26459 toplev.c after compilation proper has finished. */
26461 static void
26462 dwarf2out_late_global_decl (tree decl)
26464 /* Fill-in any location information we were unable to determine
26465 on the first pass. */
26466 if (VAR_P (decl))
26468 dw_die_ref die = lookup_decl_die (decl);
26470 /* We may have to generate early debug late for LTO in case debug
26471 was not enabled at compile-time or the target doesn't support
26472 the LTO early debug scheme. */
26473 if (! die && in_lto_p)
26475 dwarf2out_decl (decl);
26476 die = lookup_decl_die (decl);
26479 if (die)
26481 /* We get called via the symtab code invoking late_global_decl
26482 for symbols that are optimized out.
26484 Do not add locations for those, except if they have a
26485 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26486 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26487 INDIRECT_REF expression, as this could generate relocations to
26488 text symbols in LTO object files, which is invalid. */
26489 varpool_node *node = varpool_node::get (decl);
26490 if ((! node || ! node->definition)
26491 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26492 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26493 tree_add_const_value_attribute_for_decl (die, decl);
26494 else
26495 add_location_or_const_value_attribute (die, decl, false);
26500 /* Output debug information for type decl DECL. Called from toplev.c
26501 and from language front ends (to record built-in types). */
26502 static void
26503 dwarf2out_type_decl (tree decl, int local)
26505 if (!local)
26507 set_early_dwarf s;
26508 dwarf2out_decl (decl);
26512 /* Output debug information for imported module or decl DECL.
26513 NAME is non-NULL name in the lexical block if the decl has been renamed.
26514 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26515 that DECL belongs to.
26516 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26517 static void
26518 dwarf2out_imported_module_or_decl_1 (tree decl,
26519 tree name,
26520 tree lexical_block,
26521 dw_die_ref lexical_block_die)
26523 expanded_location xloc;
26524 dw_die_ref imported_die = NULL;
26525 dw_die_ref at_import_die;
26527 if (TREE_CODE (decl) == IMPORTED_DECL)
26529 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26530 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26531 gcc_assert (decl);
26533 else
26534 xloc = expand_location (input_location);
26536 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26538 at_import_die = force_type_die (TREE_TYPE (decl));
26539 /* For namespace N { typedef void T; } using N::T; base_type_die
26540 returns NULL, but DW_TAG_imported_declaration requires
26541 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26542 if (!at_import_die)
26544 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26545 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26546 at_import_die = lookup_type_die (TREE_TYPE (decl));
26547 gcc_assert (at_import_die);
26550 else
26552 at_import_die = lookup_decl_die (decl);
26553 if (!at_import_die)
26555 /* If we're trying to avoid duplicate debug info, we may not have
26556 emitted the member decl for this field. Emit it now. */
26557 if (TREE_CODE (decl) == FIELD_DECL)
26559 tree type = DECL_CONTEXT (decl);
26561 if (TYPE_CONTEXT (type)
26562 && TYPE_P (TYPE_CONTEXT (type))
26563 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26564 DINFO_USAGE_DIR_USE))
26565 return;
26566 gen_type_die_for_member (type, decl,
26567 get_context_die (TYPE_CONTEXT (type)));
26569 if (TREE_CODE (decl) == NAMELIST_DECL)
26570 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26571 get_context_die (DECL_CONTEXT (decl)),
26572 NULL_TREE);
26573 else
26574 at_import_die = force_decl_die (decl);
26578 if (TREE_CODE (decl) == NAMESPACE_DECL)
26580 if (dwarf_version >= 3 || !dwarf_strict)
26581 imported_die = new_die (DW_TAG_imported_module,
26582 lexical_block_die,
26583 lexical_block);
26584 else
26585 return;
26587 else
26588 imported_die = new_die (DW_TAG_imported_declaration,
26589 lexical_block_die,
26590 lexical_block);
26592 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26593 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26594 if (debug_column_info && xloc.column)
26595 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26596 if (name)
26597 add_AT_string (imported_die, DW_AT_name,
26598 IDENTIFIER_POINTER (name));
26599 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26602 /* Output debug information for imported module or decl DECL.
26603 NAME is non-NULL name in context if the decl has been renamed.
26604 CHILD is true if decl is one of the renamed decls as part of
26605 importing whole module.
26606 IMPLICIT is set if this hook is called for an implicit import
26607 such as inline namespace. */
26609 static void
26610 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26611 bool child, bool implicit)
26613 /* dw_die_ref at_import_die; */
26614 dw_die_ref scope_die;
26616 if (debug_info_level <= DINFO_LEVEL_TERSE)
26617 return;
26619 gcc_assert (decl);
26621 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26622 should be enough, for DWARF4 and older even if we emit as extension
26623 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26624 for the benefit of consumers unaware of DW_AT_export_symbols. */
26625 if (implicit
26626 && dwarf_version >= 5
26627 && lang_hooks.decls.decl_dwarf_attribute (decl,
26628 DW_AT_export_symbols) == 1)
26629 return;
26631 set_early_dwarf s;
26633 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26634 We need decl DIE for reference and scope die. First, get DIE for the decl
26635 itself. */
26637 /* Get the scope die for decl context. Use comp_unit_die for global module
26638 or decl. If die is not found for non globals, force new die. */
26639 if (context
26640 && TYPE_P (context)
26641 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26642 return;
26644 scope_die = get_context_die (context);
26646 if (child)
26648 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26649 there is nothing we can do, here. */
26650 if (dwarf_version < 3 && dwarf_strict)
26651 return;
26653 gcc_assert (scope_die->die_child);
26654 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26655 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26656 scope_die = scope_die->die_child;
26659 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26660 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26663 /* Output debug information for namelists. */
26665 static dw_die_ref
26666 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26668 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26669 tree value;
26670 unsigned i;
26672 if (debug_info_level <= DINFO_LEVEL_TERSE)
26673 return NULL;
26675 gcc_assert (scope_die != NULL);
26676 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26677 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26679 /* If there are no item_decls, we have a nondefining namelist, e.g.
26680 with USE association; hence, set DW_AT_declaration. */
26681 if (item_decls == NULL_TREE)
26683 add_AT_flag (nml_die, DW_AT_declaration, 1);
26684 return nml_die;
26687 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26689 nml_item_ref_die = lookup_decl_die (value);
26690 if (!nml_item_ref_die)
26691 nml_item_ref_die = force_decl_die (value);
26693 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26694 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26696 return nml_die;
26700 /* Write the debugging output for DECL and return the DIE. */
26702 static void
26703 dwarf2out_decl (tree decl)
26705 dw_die_ref context_die = comp_unit_die ();
26707 switch (TREE_CODE (decl))
26709 case ERROR_MARK:
26710 return;
26712 case FUNCTION_DECL:
26713 /* If we're a nested function, initially use a parent of NULL; if we're
26714 a plain function, this will be fixed up in decls_for_scope. If
26715 we're a method, it will be ignored, since we already have a DIE.
26716 Avoid doing this late though since clones of class methods may
26717 otherwise end up in limbo and create type DIEs late. */
26718 if (early_dwarf
26719 && decl_function_context (decl)
26720 /* But if we're in terse mode, we don't care about scope. */
26721 && debug_info_level > DINFO_LEVEL_TERSE)
26722 context_die = NULL;
26723 break;
26725 case VAR_DECL:
26726 /* For local statics lookup proper context die. */
26727 if (local_function_static (decl))
26728 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26730 /* If we are in terse mode, don't generate any DIEs to represent any
26731 variable declarations or definitions. */
26732 if (debug_info_level <= DINFO_LEVEL_TERSE)
26733 return;
26734 break;
26736 case CONST_DECL:
26737 if (debug_info_level <= DINFO_LEVEL_TERSE)
26738 return;
26739 if (!is_fortran () && !is_ada () && !is_dlang ())
26740 return;
26741 if (TREE_STATIC (decl) && decl_function_context (decl))
26742 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26743 break;
26745 case NAMESPACE_DECL:
26746 case IMPORTED_DECL:
26747 if (debug_info_level <= DINFO_LEVEL_TERSE)
26748 return;
26749 if (lookup_decl_die (decl) != NULL)
26750 return;
26751 break;
26753 case TYPE_DECL:
26754 /* Don't emit stubs for types unless they are needed by other DIEs. */
26755 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26756 return;
26758 /* Don't bother trying to generate any DIEs to represent any of the
26759 normal built-in types for the language we are compiling. */
26760 if (DECL_IS_BUILTIN (decl))
26761 return;
26763 /* If we are in terse mode, don't generate any DIEs for types. */
26764 if (debug_info_level <= DINFO_LEVEL_TERSE)
26765 return;
26767 /* If we're a function-scope tag, initially use a parent of NULL;
26768 this will be fixed up in decls_for_scope. */
26769 if (decl_function_context (decl))
26770 context_die = NULL;
26772 break;
26774 case NAMELIST_DECL:
26775 break;
26777 default:
26778 return;
26781 gen_decl_die (decl, NULL, NULL, context_die);
26783 if (flag_checking)
26785 dw_die_ref die = lookup_decl_die (decl);
26786 if (die)
26787 check_die (die);
26791 /* Write the debugging output for DECL. */
26793 static void
26794 dwarf2out_function_decl (tree decl)
26796 dwarf2out_decl (decl);
26797 call_arg_locations = NULL;
26798 call_arg_loc_last = NULL;
26799 call_site_count = -1;
26800 tail_call_site_count = -1;
26801 decl_loc_table->empty ();
26802 cached_dw_loc_list_table->empty ();
26805 /* Output a marker (i.e. a label) for the beginning of the generated code for
26806 a lexical block. */
26808 static void
26809 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26810 unsigned int blocknum)
26812 switch_to_section (current_function_section ());
26813 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26816 /* Output a marker (i.e. a label) for the end of the generated code for a
26817 lexical block. */
26819 static void
26820 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26822 switch_to_section (current_function_section ());
26823 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26826 /* Returns nonzero if it is appropriate not to emit any debugging
26827 information for BLOCK, because it doesn't contain any instructions.
26829 Don't allow this for blocks with nested functions or local classes
26830 as we would end up with orphans, and in the presence of scheduling
26831 we may end up calling them anyway. */
26833 static bool
26834 dwarf2out_ignore_block (const_tree block)
26836 tree decl;
26837 unsigned int i;
26839 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26840 if (TREE_CODE (decl) == FUNCTION_DECL
26841 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26842 return 0;
26843 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26845 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26846 if (TREE_CODE (decl) == FUNCTION_DECL
26847 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26848 return 0;
26851 return 1;
26854 /* Hash table routines for file_hash. */
26856 bool
26857 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26859 return filename_cmp (p1->filename, p2) == 0;
26862 hashval_t
26863 dwarf_file_hasher::hash (dwarf_file_data *p)
26865 return htab_hash_string (p->filename);
26868 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26869 dwarf2out.c) and return its "index". The index of each (known) filename is
26870 just a unique number which is associated with only that one filename. We
26871 need such numbers for the sake of generating labels (in the .debug_sfnames
26872 section) and references to those files numbers (in the .debug_srcinfo
26873 and .debug_macinfo sections). If the filename given as an argument is not
26874 found in our current list, add it to the list and assign it the next
26875 available unique index number. */
26877 static struct dwarf_file_data *
26878 lookup_filename (const char *file_name)
26880 struct dwarf_file_data * created;
26882 if (!file_name)
26883 return NULL;
26885 dwarf_file_data **slot
26886 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26887 INSERT);
26888 if (*slot)
26889 return *slot;
26891 created = ggc_alloc<dwarf_file_data> ();
26892 created->filename = file_name;
26893 created->emitted_number = 0;
26894 *slot = created;
26895 return created;
26898 /* If the assembler will construct the file table, then translate the compiler
26899 internal file table number into the assembler file table number, and emit
26900 a .file directive if we haven't already emitted one yet. The file table
26901 numbers are different because we prune debug info for unused variables and
26902 types, which may include filenames. */
26904 static int
26905 maybe_emit_file (struct dwarf_file_data * fd)
26907 if (! fd->emitted_number)
26909 if (last_emitted_file)
26910 fd->emitted_number = last_emitted_file->emitted_number + 1;
26911 else
26912 fd->emitted_number = 1;
26913 last_emitted_file = fd;
26915 if (output_asm_line_debug_info ())
26917 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26918 output_quoted_string (asm_out_file,
26919 remap_debug_filename (fd->filename));
26920 fputc ('\n', asm_out_file);
26924 return fd->emitted_number;
26927 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26928 That generation should happen after function debug info has been
26929 generated. The value of the attribute is the constant value of ARG. */
26931 static void
26932 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26934 die_arg_entry entry;
26936 if (!die || !arg)
26937 return;
26939 gcc_assert (early_dwarf);
26941 if (!tmpl_value_parm_die_table)
26942 vec_alloc (tmpl_value_parm_die_table, 32);
26944 entry.die = die;
26945 entry.arg = arg;
26946 vec_safe_push (tmpl_value_parm_die_table, entry);
26949 /* Return TRUE if T is an instance of generic type, FALSE
26950 otherwise. */
26952 static bool
26953 generic_type_p (tree t)
26955 if (t == NULL_TREE || !TYPE_P (t))
26956 return false;
26957 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26960 /* Schedule the generation of the generic parameter dies for the
26961 instance of generic type T. The proper generation itself is later
26962 done by gen_scheduled_generic_parms_dies. */
26964 static void
26965 schedule_generic_params_dies_gen (tree t)
26967 if (!generic_type_p (t))
26968 return;
26970 gcc_assert (early_dwarf);
26972 if (!generic_type_instances)
26973 vec_alloc (generic_type_instances, 256);
26975 vec_safe_push (generic_type_instances, t);
26978 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26979 by append_entry_to_tmpl_value_parm_die_table. This function must
26980 be called after function DIEs have been generated. */
26982 static void
26983 gen_remaining_tmpl_value_param_die_attribute (void)
26985 if (tmpl_value_parm_die_table)
26987 unsigned i, j;
26988 die_arg_entry *e;
26990 /* We do this in two phases - first get the cases we can
26991 handle during early-finish, preserving those we cannot
26992 (containing symbolic constants where we don't yet know
26993 whether we are going to output the referenced symbols).
26994 For those we try again at late-finish. */
26995 j = 0;
26996 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26998 if (!e->die->removed
26999 && !tree_add_const_value_attribute (e->die, e->arg))
27001 dw_loc_descr_ref loc = NULL;
27002 if (! early_dwarf
27003 && (dwarf_version >= 5 || !dwarf_strict))
27004 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27005 if (loc)
27006 add_AT_loc (e->die, DW_AT_location, loc);
27007 else
27008 (*tmpl_value_parm_die_table)[j++] = *e;
27011 tmpl_value_parm_die_table->truncate (j);
27015 /* Generate generic parameters DIEs for instances of generic types
27016 that have been previously scheduled by
27017 schedule_generic_params_dies_gen. This function must be called
27018 after all the types of the CU have been laid out. */
27020 static void
27021 gen_scheduled_generic_parms_dies (void)
27023 unsigned i;
27024 tree t;
27026 if (!generic_type_instances)
27027 return;
27029 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27030 if (COMPLETE_TYPE_P (t))
27031 gen_generic_params_dies (t);
27033 generic_type_instances = NULL;
27037 /* Replace DW_AT_name for the decl with name. */
27039 static void
27040 dwarf2out_set_name (tree decl, tree name)
27042 dw_die_ref die;
27043 dw_attr_node *attr;
27044 const char *dname;
27046 die = TYPE_SYMTAB_DIE (decl);
27047 if (!die)
27048 return;
27050 dname = dwarf2_name (name, 0);
27051 if (!dname)
27052 return;
27054 attr = get_AT (die, DW_AT_name);
27055 if (attr)
27057 struct indirect_string_node *node;
27059 node = find_AT_string (dname);
27060 /* replace the string. */
27061 attr->dw_attr_val.v.val_str = node;
27064 else
27065 add_name_attribute (die, dname);
27068 /* True if before or during processing of the first function being emitted. */
27069 static bool in_first_function_p = true;
27070 /* True if loc_note during dwarf2out_var_location call might still be
27071 before first real instruction at address equal to .Ltext0. */
27072 static bool maybe_at_text_label_p = true;
27073 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27074 static unsigned int first_loclabel_num_not_at_text_label;
27076 /* Look ahead for a real insn, or for a begin stmt marker. */
27078 static rtx_insn *
27079 dwarf2out_next_real_insn (rtx_insn *loc_note)
27081 rtx_insn *next_real = NEXT_INSN (loc_note);
27083 while (next_real)
27084 if (INSN_P (next_real))
27085 break;
27086 else
27087 next_real = NEXT_INSN (next_real);
27089 return next_real;
27092 /* Called by the final INSN scan whenever we see a var location. We
27093 use it to drop labels in the right places, and throw the location in
27094 our lookup table. */
27096 static void
27097 dwarf2out_var_location (rtx_insn *loc_note)
27099 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27100 struct var_loc_node *newloc;
27101 rtx_insn *next_real, *next_note;
27102 rtx_insn *call_insn = NULL;
27103 static const char *last_label;
27104 static const char *last_postcall_label;
27105 static bool last_in_cold_section_p;
27106 static rtx_insn *expected_next_loc_note;
27107 tree decl;
27108 bool var_loc_p;
27109 var_loc_view view = 0;
27111 if (!NOTE_P (loc_note))
27113 if (CALL_P (loc_note))
27115 maybe_reset_location_view (loc_note, cur_line_info_table);
27116 call_site_count++;
27117 if (SIBLING_CALL_P (loc_note))
27118 tail_call_site_count++;
27119 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27121 call_insn = loc_note;
27122 loc_note = NULL;
27123 var_loc_p = false;
27125 next_real = dwarf2out_next_real_insn (call_insn);
27126 next_note = NULL;
27127 cached_next_real_insn = NULL;
27128 goto create_label;
27130 if (optimize == 0 && !flag_var_tracking)
27132 /* When the var-tracking pass is not running, there is no note
27133 for indirect calls whose target is compile-time known. In this
27134 case, process such calls specifically so that we generate call
27135 sites for them anyway. */
27136 rtx x = PATTERN (loc_note);
27137 if (GET_CODE (x) == PARALLEL)
27138 x = XVECEXP (x, 0, 0);
27139 if (GET_CODE (x) == SET)
27140 x = SET_SRC (x);
27141 if (GET_CODE (x) == CALL)
27142 x = XEXP (x, 0);
27143 if (!MEM_P (x)
27144 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27145 || !SYMBOL_REF_DECL (XEXP (x, 0))
27146 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27147 != FUNCTION_DECL))
27149 call_insn = loc_note;
27150 loc_note = NULL;
27151 var_loc_p = false;
27153 next_real = dwarf2out_next_real_insn (call_insn);
27154 next_note = NULL;
27155 cached_next_real_insn = NULL;
27156 goto create_label;
27160 else if (!debug_variable_location_views)
27161 gcc_unreachable ();
27162 else
27163 maybe_reset_location_view (loc_note, cur_line_info_table);
27165 return;
27168 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27169 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27170 return;
27172 /* Optimize processing a large consecutive sequence of location
27173 notes so we don't spend too much time in next_real_insn. If the
27174 next insn is another location note, remember the next_real_insn
27175 calculation for next time. */
27176 next_real = cached_next_real_insn;
27177 if (next_real)
27179 if (expected_next_loc_note != loc_note)
27180 next_real = NULL;
27183 next_note = NEXT_INSN (loc_note);
27184 if (! next_note
27185 || next_note->deleted ()
27186 || ! NOTE_P (next_note)
27187 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27188 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27189 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27190 next_note = NULL;
27192 if (! next_real)
27193 next_real = dwarf2out_next_real_insn (loc_note);
27195 if (next_note)
27197 expected_next_loc_note = next_note;
27198 cached_next_real_insn = next_real;
27200 else
27201 cached_next_real_insn = NULL;
27203 /* If there are no instructions which would be affected by this note,
27204 don't do anything. */
27205 if (var_loc_p
27206 && next_real == NULL_RTX
27207 && !NOTE_DURING_CALL_P (loc_note))
27208 return;
27210 create_label:
27212 if (next_real == NULL_RTX)
27213 next_real = get_last_insn ();
27215 /* If there were any real insns between note we processed last time
27216 and this note (or if it is the first note), clear
27217 last_{,postcall_}label so that they are not reused this time. */
27218 if (last_var_location_insn == NULL_RTX
27219 || last_var_location_insn != next_real
27220 || last_in_cold_section_p != in_cold_section_p)
27222 last_label = NULL;
27223 last_postcall_label = NULL;
27226 if (var_loc_p)
27228 const char *label
27229 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27230 view = cur_line_info_table->view;
27231 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27232 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27233 if (newloc == NULL)
27234 return;
27236 else
27238 decl = NULL_TREE;
27239 newloc = NULL;
27242 /* If there were no real insns between note we processed last time
27243 and this note, use the label we emitted last time. Otherwise
27244 create a new label and emit it. */
27245 if (last_label == NULL)
27247 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27248 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27249 loclabel_num++;
27250 last_label = ggc_strdup (loclabel);
27251 /* See if loclabel might be equal to .Ltext0. If yes,
27252 bump first_loclabel_num_not_at_text_label. */
27253 if (!have_multiple_function_sections
27254 && in_first_function_p
27255 && maybe_at_text_label_p)
27257 static rtx_insn *last_start;
27258 rtx_insn *insn;
27259 for (insn = loc_note; insn; insn = previous_insn (insn))
27260 if (insn == last_start)
27261 break;
27262 else if (!NONDEBUG_INSN_P (insn))
27263 continue;
27264 else
27266 rtx body = PATTERN (insn);
27267 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27268 continue;
27269 /* Inline asm could occupy zero bytes. */
27270 else if (GET_CODE (body) == ASM_INPUT
27271 || asm_noperands (body) >= 0)
27272 continue;
27273 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27274 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27275 continue;
27276 #endif
27277 else
27279 /* Assume insn has non-zero length. */
27280 maybe_at_text_label_p = false;
27281 break;
27284 if (maybe_at_text_label_p)
27286 last_start = loc_note;
27287 first_loclabel_num_not_at_text_label = loclabel_num;
27292 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27293 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27295 if (!var_loc_p)
27297 struct call_arg_loc_node *ca_loc
27298 = ggc_cleared_alloc<call_arg_loc_node> ();
27299 rtx_insn *prev = call_insn;
27301 ca_loc->call_arg_loc_note
27302 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27303 ca_loc->next = NULL;
27304 ca_loc->label = last_label;
27305 gcc_assert (prev
27306 && (CALL_P (prev)
27307 || (NONJUMP_INSN_P (prev)
27308 && GET_CODE (PATTERN (prev)) == SEQUENCE
27309 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27310 if (!CALL_P (prev))
27311 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27312 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27314 /* Look for a SYMBOL_REF in the "prev" instruction. */
27315 rtx x = get_call_rtx_from (PATTERN (prev));
27316 if (x)
27318 /* Try to get the call symbol, if any. */
27319 if (MEM_P (XEXP (x, 0)))
27320 x = XEXP (x, 0);
27321 /* First, look for a memory access to a symbol_ref. */
27322 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27323 && SYMBOL_REF_DECL (XEXP (x, 0))
27324 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27325 ca_loc->symbol_ref = XEXP (x, 0);
27326 /* Otherwise, look at a compile-time known user-level function
27327 declaration. */
27328 else if (MEM_P (x)
27329 && MEM_EXPR (x)
27330 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27331 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27334 ca_loc->block = insn_scope (prev);
27335 if (call_arg_locations)
27336 call_arg_loc_last->next = ca_loc;
27337 else
27338 call_arg_locations = ca_loc;
27339 call_arg_loc_last = ca_loc;
27341 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27343 newloc->label = last_label;
27344 newloc->view = view;
27346 else
27348 if (!last_postcall_label)
27350 sprintf (loclabel, "%s-1", last_label);
27351 last_postcall_label = ggc_strdup (loclabel);
27353 newloc->label = last_postcall_label;
27354 /* ??? This view is at last_label, not last_label-1, but we
27355 could only assume view at last_label-1 is zero if we could
27356 assume calls always have length greater than one. This is
27357 probably true in general, though there might be a rare
27358 exception to this rule, e.g. if a call insn is optimized out
27359 by target magic. Then, even the -1 in the label will be
27360 wrong, which might invalidate the range. Anyway, using view,
27361 though technically possibly incorrect, will work as far as
27362 ranges go: since L-1 is in the middle of the call insn,
27363 (L-1).0 and (L-1).V shouldn't make any difference, and having
27364 the loclist entry refer to the .loc entry might be useful, so
27365 leave it like this. */
27366 newloc->view = view;
27369 if (var_loc_p && flag_debug_asm)
27371 const char *name, *sep, *patstr;
27372 if (decl && DECL_NAME (decl))
27373 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27374 else
27375 name = "";
27376 if (NOTE_VAR_LOCATION_LOC (loc_note))
27378 sep = " => ";
27379 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27381 else
27383 sep = " ";
27384 patstr = "RESET";
27386 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27387 name, sep, patstr);
27390 last_var_location_insn = next_real;
27391 last_in_cold_section_p = in_cold_section_p;
27394 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27395 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27396 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27397 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27398 BLOCK_FRAGMENT_ORIGIN links. */
27399 static bool
27400 block_within_block_p (tree block, tree outer, bool bothways)
27402 if (block == outer)
27403 return true;
27405 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27406 for (tree context = BLOCK_SUPERCONTEXT (block);
27407 context != outer;
27408 context = BLOCK_SUPERCONTEXT (context))
27409 if (!context || TREE_CODE (context) != BLOCK)
27410 return false;
27412 if (!bothways)
27413 return true;
27415 /* Now check that each block is actually referenced by its
27416 parent. */
27417 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27418 context = BLOCK_SUPERCONTEXT (context))
27420 if (BLOCK_FRAGMENT_ORIGIN (context))
27422 gcc_assert (!BLOCK_SUBBLOCKS (context));
27423 context = BLOCK_FRAGMENT_ORIGIN (context);
27425 for (tree sub = BLOCK_SUBBLOCKS (context);
27426 sub != block;
27427 sub = BLOCK_CHAIN (sub))
27428 if (!sub)
27429 return false;
27430 if (context == outer)
27431 return true;
27432 else
27433 block = context;
27437 /* Called during final while assembling the marker of the entry point
27438 for an inlined function. */
27440 static void
27441 dwarf2out_inline_entry (tree block)
27443 gcc_assert (debug_inline_points);
27445 /* If we can't represent it, don't bother. */
27446 if (!(dwarf_version >= 3 || !dwarf_strict))
27447 return;
27449 gcc_assert (DECL_P (block_ultimate_origin (block)));
27451 /* Sanity check the block tree. This would catch a case in which
27452 BLOCK got removed from the tree reachable from the outermost
27453 lexical block, but got retained in markers. It would still link
27454 back to its parents, but some ancestor would be missing a link
27455 down the path to the sub BLOCK. If the block got removed, its
27456 BLOCK_NUMBER will not be a usable value. */
27457 if (flag_checking)
27458 gcc_assert (block_within_block_p (block,
27459 DECL_INITIAL (current_function_decl),
27460 true));
27462 gcc_assert (inlined_function_outer_scope_p (block));
27463 gcc_assert (!lookup_block_die (block));
27465 if (BLOCK_FRAGMENT_ORIGIN (block))
27466 block = BLOCK_FRAGMENT_ORIGIN (block);
27467 /* Can the entry point ever not be at the beginning of an
27468 unfragmented lexical block? */
27469 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27470 || (cur_line_info_table
27471 && !ZERO_VIEW_P (cur_line_info_table->view))))
27472 return;
27474 if (!inline_entry_data_table)
27475 inline_entry_data_table
27476 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27479 inline_entry_data **iedp
27480 = inline_entry_data_table->find_slot_with_hash (block,
27481 htab_hash_pointer (block),
27482 INSERT);
27483 if (*iedp)
27484 /* ??? Ideally, we'd record all entry points for the same inlined
27485 function (some may have been duplicated by e.g. unrolling), but
27486 we have no way to represent that ATM. */
27487 return;
27489 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27490 ied->block = block;
27491 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27492 ied->label_num = BLOCK_NUMBER (block);
27493 if (cur_line_info_table)
27494 ied->view = cur_line_info_table->view;
27496 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27498 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27499 BLOCK_NUMBER (block));
27500 ASM_OUTPUT_LABEL (asm_out_file, label);
27503 /* Called from finalize_size_functions for size functions so that their body
27504 can be encoded in the debug info to describe the layout of variable-length
27505 structures. */
27507 static void
27508 dwarf2out_size_function (tree decl)
27510 function_to_dwarf_procedure (decl);
27513 /* Note in one location list that text section has changed. */
27516 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27518 var_loc_list *list = *slot;
27519 if (list->first)
27520 list->last_before_switch
27521 = list->last->next ? list->last->next : list->last;
27522 return 1;
27525 /* Note in all location lists that text section has changed. */
27527 static void
27528 var_location_switch_text_section (void)
27530 if (decl_loc_table == NULL)
27531 return;
27533 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27536 /* Create a new line number table. */
27538 static dw_line_info_table *
27539 new_line_info_table (void)
27541 dw_line_info_table *table;
27543 table = ggc_cleared_alloc<dw_line_info_table> ();
27544 table->file_num = 1;
27545 table->line_num = 1;
27546 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27547 FORCE_RESET_NEXT_VIEW (table->view);
27548 table->symviews_since_reset = 0;
27550 return table;
27553 /* Lookup the "current" table into which we emit line info, so
27554 that we don't have to do it for every source line. */
27556 static void
27557 set_cur_line_info_table (section *sec)
27559 dw_line_info_table *table;
27561 if (sec == text_section)
27562 table = text_section_line_info;
27563 else if (sec == cold_text_section)
27565 table = cold_text_section_line_info;
27566 if (!table)
27568 cold_text_section_line_info = table = new_line_info_table ();
27569 table->end_label = cold_end_label;
27572 else
27574 const char *end_label;
27576 if (crtl->has_bb_partition)
27578 if (in_cold_section_p)
27579 end_label = crtl->subsections.cold_section_end_label;
27580 else
27581 end_label = crtl->subsections.hot_section_end_label;
27583 else
27585 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27586 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27587 current_function_funcdef_no);
27588 end_label = ggc_strdup (label);
27591 table = new_line_info_table ();
27592 table->end_label = end_label;
27594 vec_safe_push (separate_line_info, table);
27597 if (output_asm_line_debug_info ())
27598 table->is_stmt = (cur_line_info_table
27599 ? cur_line_info_table->is_stmt
27600 : DWARF_LINE_DEFAULT_IS_STMT_START);
27601 cur_line_info_table = table;
27605 /* We need to reset the locations at the beginning of each
27606 function. We can't do this in the end_function hook, because the
27607 declarations that use the locations won't have been output when
27608 that hook is called. Also compute have_multiple_function_sections here. */
27610 static void
27611 dwarf2out_begin_function (tree fun)
27613 section *sec = function_section (fun);
27615 if (sec != text_section)
27616 have_multiple_function_sections = true;
27618 if (crtl->has_bb_partition && !cold_text_section)
27620 gcc_assert (current_function_decl == fun);
27621 cold_text_section = unlikely_text_section ();
27622 switch_to_section (cold_text_section);
27623 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27624 switch_to_section (sec);
27627 dwarf2out_note_section_used ();
27628 call_site_count = 0;
27629 tail_call_site_count = 0;
27631 set_cur_line_info_table (sec);
27632 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27635 /* Helper function of dwarf2out_end_function, called only after emitting
27636 the very first function into assembly. Check if some .debug_loc range
27637 might end with a .LVL* label that could be equal to .Ltext0.
27638 In that case we must force using absolute addresses in .debug_loc ranges,
27639 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27640 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27641 list terminator.
27642 Set have_multiple_function_sections to true in that case and
27643 terminate htab traversal. */
27646 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27648 var_loc_list *entry = *slot;
27649 struct var_loc_node *node;
27651 node = entry->first;
27652 if (node && node->next && node->next->label)
27654 unsigned int i;
27655 const char *label = node->next->label;
27656 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27658 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27660 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27661 if (strcmp (label, loclabel) == 0)
27663 have_multiple_function_sections = true;
27664 return 0;
27668 return 1;
27671 /* Hook called after emitting a function into assembly.
27672 This does something only for the very first function emitted. */
27674 static void
27675 dwarf2out_end_function (unsigned int)
27677 if (in_first_function_p
27678 && !have_multiple_function_sections
27679 && first_loclabel_num_not_at_text_label
27680 && decl_loc_table)
27681 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27682 in_first_function_p = false;
27683 maybe_at_text_label_p = false;
27686 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27687 front-ends register a translation unit even before dwarf2out_init is
27688 called. */
27689 static tree main_translation_unit = NULL_TREE;
27691 /* Hook called by front-ends after they built their main translation unit.
27692 Associate comp_unit_die to UNIT. */
27694 static void
27695 dwarf2out_register_main_translation_unit (tree unit)
27697 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27698 && main_translation_unit == NULL_TREE);
27699 main_translation_unit = unit;
27700 /* If dwarf2out_init has not been called yet, it will perform the association
27701 itself looking at main_translation_unit. */
27702 if (decl_die_table != NULL)
27703 equate_decl_number_to_die (unit, comp_unit_die ());
27706 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27708 static void
27709 push_dw_line_info_entry (dw_line_info_table *table,
27710 enum dw_line_info_opcode opcode, unsigned int val)
27712 dw_line_info_entry e;
27713 e.opcode = opcode;
27714 e.val = val;
27715 vec_safe_push (table->entries, e);
27718 /* Output a label to mark the beginning of a source code line entry
27719 and record information relating to this source line, in
27720 'line_info_table' for later output of the .debug_line section. */
27721 /* ??? The discriminator parameter ought to be unsigned. */
27723 static void
27724 dwarf2out_source_line (unsigned int line, unsigned int column,
27725 const char *filename,
27726 int discriminator, bool is_stmt)
27728 unsigned int file_num;
27729 dw_line_info_table *table;
27730 static var_loc_view lvugid;
27732 if (debug_info_level < DINFO_LEVEL_TERSE)
27733 return;
27735 table = cur_line_info_table;
27737 if (line == 0)
27739 if (debug_variable_location_views
27740 && output_asm_line_debug_info ()
27741 && table && !RESETTING_VIEW_P (table->view))
27743 /* If we're using the assembler to compute view numbers, we
27744 can't issue a .loc directive for line zero, so we can't
27745 get a view number at this point. We might attempt to
27746 compute it from the previous view, or equate it to a
27747 subsequent view (though it might not be there!), but
27748 since we're omitting the line number entry, we might as
27749 well omit the view number as well. That means pretending
27750 it's a view number zero, which might very well turn out
27751 to be correct. ??? Extend the assembler so that the
27752 compiler could emit e.g. ".locview .LVU#", to output a
27753 view without changing line number information. We'd then
27754 have to count it in symviews_since_reset; when it's omitted,
27755 it doesn't count. */
27756 if (!zero_view_p)
27757 zero_view_p = BITMAP_GGC_ALLOC ();
27758 bitmap_set_bit (zero_view_p, table->view);
27759 if (flag_debug_asm)
27761 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27762 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27763 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27764 ASM_COMMENT_START);
27765 assemble_name (asm_out_file, label);
27766 putc ('\n', asm_out_file);
27768 table->view = ++lvugid;
27770 return;
27773 /* The discriminator column was added in dwarf4. Simplify the below
27774 by simply removing it if we're not supposed to output it. */
27775 if (dwarf_version < 4 && dwarf_strict)
27776 discriminator = 0;
27778 if (!debug_column_info)
27779 column = 0;
27781 file_num = maybe_emit_file (lookup_filename (filename));
27783 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27784 the debugger has used the second (possibly duplicate) line number
27785 at the beginning of the function to mark the end of the prologue.
27786 We could eliminate any other duplicates within the function. For
27787 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27788 that second line number entry. */
27789 /* Recall that this end-of-prologue indication is *not* the same thing
27790 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27791 to which the hook corresponds, follows the last insn that was
27792 emitted by gen_prologue. What we need is to precede the first insn
27793 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27794 insn that corresponds to something the user wrote. These may be
27795 very different locations once scheduling is enabled. */
27797 if (0 && file_num == table->file_num
27798 && line == table->line_num
27799 && column == table->column_num
27800 && discriminator == table->discrim_num
27801 && is_stmt == table->is_stmt)
27802 return;
27804 switch_to_section (current_function_section ());
27806 /* If requested, emit something human-readable. */
27807 if (flag_debug_asm)
27809 if (debug_column_info)
27810 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27811 filename, line, column);
27812 else
27813 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27814 filename, line);
27817 if (output_asm_line_debug_info ())
27819 /* Emit the .loc directive understood by GNU as. */
27820 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27821 file_num, line, is_stmt, discriminator */
27822 fputs ("\t.loc ", asm_out_file);
27823 fprint_ul (asm_out_file, file_num);
27824 putc (' ', asm_out_file);
27825 fprint_ul (asm_out_file, line);
27826 putc (' ', asm_out_file);
27827 fprint_ul (asm_out_file, column);
27829 if (is_stmt != table->is_stmt)
27831 #if HAVE_GAS_LOC_STMT
27832 fputs (" is_stmt ", asm_out_file);
27833 putc (is_stmt ? '1' : '0', asm_out_file);
27834 #endif
27836 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27838 gcc_assert (discriminator > 0);
27839 fputs (" discriminator ", asm_out_file);
27840 fprint_ul (asm_out_file, (unsigned long) discriminator);
27842 if (debug_variable_location_views)
27844 if (!RESETTING_VIEW_P (table->view))
27846 table->symviews_since_reset++;
27847 if (table->symviews_since_reset > symview_upper_bound)
27848 symview_upper_bound = table->symviews_since_reset;
27849 /* When we're using the assembler to compute view
27850 numbers, we output symbolic labels after "view" in
27851 .loc directives, and the assembler will set them for
27852 us, so that we can refer to the view numbers in
27853 location lists. The only exceptions are when we know
27854 a view will be zero: "-0" is a forced reset, used
27855 e.g. in the beginning of functions, whereas "0" tells
27856 the assembler to check that there was a PC change
27857 since the previous view, in a way that implicitly
27858 resets the next view. */
27859 fputs (" view ", asm_out_file);
27860 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27861 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27862 assemble_name (asm_out_file, label);
27863 table->view = ++lvugid;
27865 else
27867 table->symviews_since_reset = 0;
27868 if (FORCE_RESETTING_VIEW_P (table->view))
27869 fputs (" view -0", asm_out_file);
27870 else
27871 fputs (" view 0", asm_out_file);
27872 /* Mark the present view as a zero view. Earlier debug
27873 binds may have already added its id to loclists to be
27874 emitted later, so we can't reuse the id for something
27875 else. However, it's good to know whether a view is
27876 known to be zero, because then we may be able to
27877 optimize out locviews that are all zeros, so take
27878 note of it in zero_view_p. */
27879 if (!zero_view_p)
27880 zero_view_p = BITMAP_GGC_ALLOC ();
27881 bitmap_set_bit (zero_view_p, lvugid);
27882 table->view = ++lvugid;
27885 putc ('\n', asm_out_file);
27887 else
27889 unsigned int label_num = ++line_info_label_num;
27891 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27893 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
27894 push_dw_line_info_entry (table, LI_adv_address, label_num);
27895 else
27896 push_dw_line_info_entry (table, LI_set_address, label_num);
27897 if (debug_variable_location_views)
27899 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
27900 if (resetting)
27901 table->view = 0;
27903 if (flag_debug_asm)
27904 fprintf (asm_out_file, "\t%s view %s%d\n",
27905 ASM_COMMENT_START,
27906 resetting ? "-" : "",
27907 table->view);
27909 table->view++;
27911 if (file_num != table->file_num)
27912 push_dw_line_info_entry (table, LI_set_file, file_num);
27913 if (discriminator != table->discrim_num)
27914 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27915 if (is_stmt != table->is_stmt)
27916 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27917 push_dw_line_info_entry (table, LI_set_line, line);
27918 if (debug_column_info)
27919 push_dw_line_info_entry (table, LI_set_column, column);
27922 table->file_num = file_num;
27923 table->line_num = line;
27924 table->column_num = column;
27925 table->discrim_num = discriminator;
27926 table->is_stmt = is_stmt;
27927 table->in_use = true;
27930 /* Record the beginning of a new source file. */
27932 static void
27933 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27935 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27937 macinfo_entry e;
27938 e.code = DW_MACINFO_start_file;
27939 e.lineno = lineno;
27940 e.info = ggc_strdup (filename);
27941 vec_safe_push (macinfo_table, e);
27945 /* Record the end of a source file. */
27947 static void
27948 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27950 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27952 macinfo_entry e;
27953 e.code = DW_MACINFO_end_file;
27954 e.lineno = lineno;
27955 e.info = NULL;
27956 vec_safe_push (macinfo_table, e);
27960 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27961 the tail part of the directive line, i.e. the part which is past the
27962 initial whitespace, #, whitespace, directive-name, whitespace part. */
27964 static void
27965 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27966 const char *buffer ATTRIBUTE_UNUSED)
27968 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27970 macinfo_entry e;
27971 /* Insert a dummy first entry to be able to optimize the whole
27972 predefined macro block using DW_MACRO_import. */
27973 if (macinfo_table->is_empty () && lineno <= 1)
27975 e.code = 0;
27976 e.lineno = 0;
27977 e.info = NULL;
27978 vec_safe_push (macinfo_table, e);
27980 e.code = DW_MACINFO_define;
27981 e.lineno = lineno;
27982 e.info = ggc_strdup (buffer);
27983 vec_safe_push (macinfo_table, e);
27987 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27988 the tail part of the directive line, i.e. the part which is past the
27989 initial whitespace, #, whitespace, directive-name, whitespace part. */
27991 static void
27992 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27993 const char *buffer ATTRIBUTE_UNUSED)
27995 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27997 macinfo_entry e;
27998 /* Insert a dummy first entry to be able to optimize the whole
27999 predefined macro block using DW_MACRO_import. */
28000 if (macinfo_table->is_empty () && lineno <= 1)
28002 e.code = 0;
28003 e.lineno = 0;
28004 e.info = NULL;
28005 vec_safe_push (macinfo_table, e);
28007 e.code = DW_MACINFO_undef;
28008 e.lineno = lineno;
28009 e.info = ggc_strdup (buffer);
28010 vec_safe_push (macinfo_table, e);
28014 /* Helpers to manipulate hash table of CUs. */
28016 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28018 static inline hashval_t hash (const macinfo_entry *);
28019 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28022 inline hashval_t
28023 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28025 return htab_hash_string (entry->info);
28028 inline bool
28029 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28030 const macinfo_entry *entry2)
28032 return !strcmp (entry1->info, entry2->info);
28035 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28037 /* Output a single .debug_macinfo entry. */
28039 static void
28040 output_macinfo_op (macinfo_entry *ref)
28042 int file_num;
28043 size_t len;
28044 struct indirect_string_node *node;
28045 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28046 struct dwarf_file_data *fd;
28048 switch (ref->code)
28050 case DW_MACINFO_start_file:
28051 fd = lookup_filename (ref->info);
28052 file_num = maybe_emit_file (fd);
28053 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28054 dw2_asm_output_data_uleb128 (ref->lineno,
28055 "Included from line number %lu",
28056 (unsigned long) ref->lineno);
28057 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28058 break;
28059 case DW_MACINFO_end_file:
28060 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28061 break;
28062 case DW_MACINFO_define:
28063 case DW_MACINFO_undef:
28064 len = strlen (ref->info) + 1;
28065 if (!dwarf_strict
28066 && len > DWARF_OFFSET_SIZE
28067 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28068 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28070 ref->code = ref->code == DW_MACINFO_define
28071 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28072 output_macinfo_op (ref);
28073 return;
28075 dw2_asm_output_data (1, ref->code,
28076 ref->code == DW_MACINFO_define
28077 ? "Define macro" : "Undefine macro");
28078 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28079 (unsigned long) ref->lineno);
28080 dw2_asm_output_nstring (ref->info, -1, "The macro");
28081 break;
28082 case DW_MACRO_define_strp:
28083 case DW_MACRO_undef_strp:
28084 node = find_AT_string (ref->info);
28085 gcc_assert (node
28086 && (node->form == DW_FORM_strp
28087 || node->form == dwarf_FORM (DW_FORM_strx)));
28088 dw2_asm_output_data (1, ref->code,
28089 ref->code == DW_MACRO_define_strp
28090 ? "Define macro strp"
28091 : "Undefine macro strp");
28092 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28093 (unsigned long) ref->lineno);
28094 if (node->form == DW_FORM_strp)
28095 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28096 debug_str_section, "The macro: \"%s\"",
28097 ref->info);
28098 else
28099 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28100 ref->info);
28101 break;
28102 case DW_MACRO_import:
28103 dw2_asm_output_data (1, ref->code, "Import");
28104 ASM_GENERATE_INTERNAL_LABEL (label,
28105 DEBUG_MACRO_SECTION_LABEL,
28106 ref->lineno + macinfo_label_base);
28107 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28108 break;
28109 default:
28110 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28111 ASM_COMMENT_START, (unsigned long) ref->code);
28112 break;
28116 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28117 other compilation unit .debug_macinfo sections. IDX is the first
28118 index of a define/undef, return the number of ops that should be
28119 emitted in a comdat .debug_macinfo section and emit
28120 a DW_MACRO_import entry referencing it.
28121 If the define/undef entry should be emitted normally, return 0. */
28123 static unsigned
28124 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28125 macinfo_hash_type **macinfo_htab)
28127 macinfo_entry *first, *second, *cur, *inc;
28128 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28129 unsigned char checksum[16];
28130 struct md5_ctx ctx;
28131 char *grp_name, *tail;
28132 const char *base;
28133 unsigned int i, count, encoded_filename_len, linebuf_len;
28134 macinfo_entry **slot;
28136 first = &(*macinfo_table)[idx];
28137 second = &(*macinfo_table)[idx + 1];
28139 /* Optimize only if there are at least two consecutive define/undef ops,
28140 and either all of them are before first DW_MACINFO_start_file
28141 with lineno {0,1} (i.e. predefined macro block), or all of them are
28142 in some included header file. */
28143 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28144 return 0;
28145 if (vec_safe_is_empty (files))
28147 if (first->lineno > 1 || second->lineno > 1)
28148 return 0;
28150 else if (first->lineno == 0)
28151 return 0;
28153 /* Find the last define/undef entry that can be grouped together
28154 with first and at the same time compute md5 checksum of their
28155 codes, linenumbers and strings. */
28156 md5_init_ctx (&ctx);
28157 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28158 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28159 break;
28160 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28161 break;
28162 else
28164 unsigned char code = cur->code;
28165 md5_process_bytes (&code, 1, &ctx);
28166 checksum_uleb128 (cur->lineno, &ctx);
28167 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28169 md5_finish_ctx (&ctx, checksum);
28170 count = i - idx;
28172 /* From the containing include filename (if any) pick up just
28173 usable characters from its basename. */
28174 if (vec_safe_is_empty (files))
28175 base = "";
28176 else
28177 base = lbasename (files->last ().info);
28178 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28179 if (ISIDNUM (base[i]) || base[i] == '.')
28180 encoded_filename_len++;
28181 /* Count . at the end. */
28182 if (encoded_filename_len)
28183 encoded_filename_len++;
28185 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28186 linebuf_len = strlen (linebuf);
28188 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28189 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28190 + 16 * 2 + 1);
28191 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28192 tail = grp_name + 4;
28193 if (encoded_filename_len)
28195 for (i = 0; base[i]; i++)
28196 if (ISIDNUM (base[i]) || base[i] == '.')
28197 *tail++ = base[i];
28198 *tail++ = '.';
28200 memcpy (tail, linebuf, linebuf_len);
28201 tail += linebuf_len;
28202 *tail++ = '.';
28203 for (i = 0; i < 16; i++)
28204 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28206 /* Construct a macinfo_entry for DW_MACRO_import
28207 in the empty vector entry before the first define/undef. */
28208 inc = &(*macinfo_table)[idx - 1];
28209 inc->code = DW_MACRO_import;
28210 inc->lineno = 0;
28211 inc->info = ggc_strdup (grp_name);
28212 if (!*macinfo_htab)
28213 *macinfo_htab = new macinfo_hash_type (10);
28214 /* Avoid emitting duplicates. */
28215 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28216 if (*slot != NULL)
28218 inc->code = 0;
28219 inc->info = NULL;
28220 /* If such an entry has been used before, just emit
28221 a DW_MACRO_import op. */
28222 inc = *slot;
28223 output_macinfo_op (inc);
28224 /* And clear all macinfo_entry in the range to avoid emitting them
28225 in the second pass. */
28226 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28228 cur->code = 0;
28229 cur->info = NULL;
28232 else
28234 *slot = inc;
28235 inc->lineno = (*macinfo_htab)->elements ();
28236 output_macinfo_op (inc);
28238 return count;
28241 /* Save any strings needed by the macinfo table in the debug str
28242 table. All strings must be collected into the table by the time
28243 index_string is called. */
28245 static void
28246 save_macinfo_strings (void)
28248 unsigned len;
28249 unsigned i;
28250 macinfo_entry *ref;
28252 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28254 switch (ref->code)
28256 /* Match the logic in output_macinfo_op to decide on
28257 indirect strings. */
28258 case DW_MACINFO_define:
28259 case DW_MACINFO_undef:
28260 len = strlen (ref->info) + 1;
28261 if (!dwarf_strict
28262 && len > DWARF_OFFSET_SIZE
28263 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28264 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28265 set_indirect_string (find_AT_string (ref->info));
28266 break;
28267 case DW_MACINFO_start_file:
28268 /* -gsplit-dwarf -g3 will also output filename as indirect
28269 string. */
28270 if (!dwarf_split_debug_info)
28271 break;
28272 /* Fall through. */
28273 case DW_MACRO_define_strp:
28274 case DW_MACRO_undef_strp:
28275 set_indirect_string (find_AT_string (ref->info));
28276 break;
28277 default:
28278 break;
28283 /* Output macinfo section(s). */
28285 static void
28286 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28288 unsigned i;
28289 unsigned long length = vec_safe_length (macinfo_table);
28290 macinfo_entry *ref;
28291 vec<macinfo_entry, va_gc> *files = NULL;
28292 macinfo_hash_type *macinfo_htab = NULL;
28293 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28295 if (! length)
28296 return;
28298 /* output_macinfo* uses these interchangeably. */
28299 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28300 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28301 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28302 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28304 /* AIX Assembler inserts the length, so adjust the reference to match the
28305 offset expected by debuggers. */
28306 strcpy (dl_section_ref, debug_line_label);
28307 if (XCOFF_DEBUGGING_INFO)
28308 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28310 /* For .debug_macro emit the section header. */
28311 if (!dwarf_strict || dwarf_version >= 5)
28313 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28314 "DWARF macro version number");
28315 if (DWARF_OFFSET_SIZE == 8)
28316 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28317 else
28318 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28319 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28320 debug_line_section, NULL);
28323 /* In the first loop, it emits the primary .debug_macinfo section
28324 and after each emitted op the macinfo_entry is cleared.
28325 If a longer range of define/undef ops can be optimized using
28326 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28327 the vector before the first define/undef in the range and the
28328 whole range of define/undef ops is not emitted and kept. */
28329 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28331 switch (ref->code)
28333 case DW_MACINFO_start_file:
28334 vec_safe_push (files, *ref);
28335 break;
28336 case DW_MACINFO_end_file:
28337 if (!vec_safe_is_empty (files))
28338 files->pop ();
28339 break;
28340 case DW_MACINFO_define:
28341 case DW_MACINFO_undef:
28342 if ((!dwarf_strict || dwarf_version >= 5)
28343 && HAVE_COMDAT_GROUP
28344 && vec_safe_length (files) != 1
28345 && i > 0
28346 && i + 1 < length
28347 && (*macinfo_table)[i - 1].code == 0)
28349 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28350 if (count)
28352 i += count - 1;
28353 continue;
28356 break;
28357 case 0:
28358 /* A dummy entry may be inserted at the beginning to be able
28359 to optimize the whole block of predefined macros. */
28360 if (i == 0)
28361 continue;
28362 default:
28363 break;
28365 output_macinfo_op (ref);
28366 ref->info = NULL;
28367 ref->code = 0;
28370 if (!macinfo_htab)
28371 return;
28373 /* Save the number of transparent includes so we can adjust the
28374 label number for the fat LTO object DWARF. */
28375 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28377 delete macinfo_htab;
28378 macinfo_htab = NULL;
28380 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28381 terminate the current chain and switch to a new comdat .debug_macinfo
28382 section and emit the define/undef entries within it. */
28383 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28384 switch (ref->code)
28386 case 0:
28387 continue;
28388 case DW_MACRO_import:
28390 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28391 tree comdat_key = get_identifier (ref->info);
28392 /* Terminate the previous .debug_macinfo section. */
28393 dw2_asm_output_data (1, 0, "End compilation unit");
28394 targetm.asm_out.named_section (debug_macinfo_section_name,
28395 SECTION_DEBUG
28396 | SECTION_LINKONCE
28397 | (early_lto_debug
28398 ? SECTION_EXCLUDE : 0),
28399 comdat_key);
28400 ASM_GENERATE_INTERNAL_LABEL (label,
28401 DEBUG_MACRO_SECTION_LABEL,
28402 ref->lineno + macinfo_label_base);
28403 ASM_OUTPUT_LABEL (asm_out_file, label);
28404 ref->code = 0;
28405 ref->info = NULL;
28406 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28407 "DWARF macro version number");
28408 if (DWARF_OFFSET_SIZE == 8)
28409 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28410 else
28411 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28413 break;
28414 case DW_MACINFO_define:
28415 case DW_MACINFO_undef:
28416 output_macinfo_op (ref);
28417 ref->code = 0;
28418 ref->info = NULL;
28419 break;
28420 default:
28421 gcc_unreachable ();
28424 macinfo_label_base += macinfo_label_base_adj;
28427 /* Initialize the various sections and labels for dwarf output and prefix
28428 them with PREFIX if non-NULL. Returns the generation (zero based
28429 number of times function was called). */
28431 static unsigned
28432 init_sections_and_labels (bool early_lto_debug)
28434 /* As we may get called multiple times have a generation count for
28435 labels. */
28436 static unsigned generation = 0;
28438 if (early_lto_debug)
28440 if (!dwarf_split_debug_info)
28442 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28443 SECTION_DEBUG | SECTION_EXCLUDE,
28444 NULL);
28445 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28446 SECTION_DEBUG | SECTION_EXCLUDE,
28447 NULL);
28448 debug_macinfo_section_name
28449 = ((dwarf_strict && dwarf_version < 5)
28450 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28451 debug_macinfo_section = get_section (debug_macinfo_section_name,
28452 SECTION_DEBUG
28453 | SECTION_EXCLUDE, NULL);
28455 else
28457 /* ??? Which of the following do we need early? */
28458 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28459 SECTION_DEBUG | SECTION_EXCLUDE,
28460 NULL);
28461 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28462 SECTION_DEBUG | SECTION_EXCLUDE,
28463 NULL);
28464 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28465 SECTION_DEBUG
28466 | SECTION_EXCLUDE, NULL);
28467 debug_skeleton_abbrev_section
28468 = get_section (DEBUG_LTO_ABBREV_SECTION,
28469 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28470 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28471 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28472 generation);
28474 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28475 stay in the main .o, but the skeleton_line goes into the split
28476 off dwo. */
28477 debug_skeleton_line_section
28478 = get_section (DEBUG_LTO_LINE_SECTION,
28479 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28480 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28481 DEBUG_SKELETON_LINE_SECTION_LABEL,
28482 generation);
28483 debug_str_offsets_section
28484 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28485 SECTION_DEBUG | SECTION_EXCLUDE,
28486 NULL);
28487 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28488 DEBUG_SKELETON_INFO_SECTION_LABEL,
28489 generation);
28490 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28491 DEBUG_STR_DWO_SECTION_FLAGS,
28492 NULL);
28493 debug_macinfo_section_name
28494 = ((dwarf_strict && dwarf_version < 5)
28495 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28496 debug_macinfo_section = get_section (debug_macinfo_section_name,
28497 SECTION_DEBUG | SECTION_EXCLUDE,
28498 NULL);
28500 /* For macro info and the file table we have to refer to a
28501 debug_line section. */
28502 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28503 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28504 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28505 DEBUG_LINE_SECTION_LABEL, generation);
28507 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28508 DEBUG_STR_SECTION_FLAGS
28509 | SECTION_EXCLUDE, NULL);
28510 if (!dwarf_split_debug_info)
28511 debug_line_str_section
28512 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28513 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28515 else
28517 if (!dwarf_split_debug_info)
28519 debug_info_section = get_section (DEBUG_INFO_SECTION,
28520 SECTION_DEBUG, NULL);
28521 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28522 SECTION_DEBUG, NULL);
28523 debug_loc_section = get_section (dwarf_version >= 5
28524 ? DEBUG_LOCLISTS_SECTION
28525 : DEBUG_LOC_SECTION,
28526 SECTION_DEBUG, NULL);
28527 debug_macinfo_section_name
28528 = ((dwarf_strict && dwarf_version < 5)
28529 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28530 debug_macinfo_section = get_section (debug_macinfo_section_name,
28531 SECTION_DEBUG, NULL);
28533 else
28535 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28536 SECTION_DEBUG | SECTION_EXCLUDE,
28537 NULL);
28538 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28539 SECTION_DEBUG | SECTION_EXCLUDE,
28540 NULL);
28541 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28542 SECTION_DEBUG, NULL);
28543 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28544 SECTION_DEBUG, NULL);
28545 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28546 SECTION_DEBUG, NULL);
28547 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28548 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28549 generation);
28551 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28552 stay in the main .o, but the skeleton_line goes into the
28553 split off dwo. */
28554 debug_skeleton_line_section
28555 = get_section (DEBUG_DWO_LINE_SECTION,
28556 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28557 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28558 DEBUG_SKELETON_LINE_SECTION_LABEL,
28559 generation);
28560 debug_str_offsets_section
28561 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28562 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28563 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28564 DEBUG_SKELETON_INFO_SECTION_LABEL,
28565 generation);
28566 debug_loc_section = get_section (dwarf_version >= 5
28567 ? DEBUG_DWO_LOCLISTS_SECTION
28568 : DEBUG_DWO_LOC_SECTION,
28569 SECTION_DEBUG | SECTION_EXCLUDE,
28570 NULL);
28571 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28572 DEBUG_STR_DWO_SECTION_FLAGS,
28573 NULL);
28574 debug_macinfo_section_name
28575 = ((dwarf_strict && dwarf_version < 5)
28576 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28577 debug_macinfo_section = get_section (debug_macinfo_section_name,
28578 SECTION_DEBUG | SECTION_EXCLUDE,
28579 NULL);
28581 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28582 SECTION_DEBUG, NULL);
28583 debug_line_section = get_section (DEBUG_LINE_SECTION,
28584 SECTION_DEBUG, NULL);
28585 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28586 SECTION_DEBUG, NULL);
28587 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28588 SECTION_DEBUG, NULL);
28589 debug_str_section = get_section (DEBUG_STR_SECTION,
28590 DEBUG_STR_SECTION_FLAGS, NULL);
28591 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28592 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28593 DEBUG_STR_SECTION_FLAGS, NULL);
28595 debug_ranges_section = get_section (dwarf_version >= 5
28596 ? DEBUG_RNGLISTS_SECTION
28597 : DEBUG_RANGES_SECTION,
28598 SECTION_DEBUG, NULL);
28599 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28600 SECTION_DEBUG, NULL);
28603 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28604 DEBUG_ABBREV_SECTION_LABEL, generation);
28605 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28606 DEBUG_INFO_SECTION_LABEL, generation);
28607 info_section_emitted = false;
28608 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28609 DEBUG_LINE_SECTION_LABEL, generation);
28610 /* There are up to 4 unique ranges labels per generation.
28611 See also output_rnglists. */
28612 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28613 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28614 if (dwarf_version >= 5 && dwarf_split_debug_info)
28615 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28616 DEBUG_RANGES_SECTION_LABEL,
28617 1 + generation * 4);
28618 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28619 DEBUG_ADDR_SECTION_LABEL, generation);
28620 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28621 (dwarf_strict && dwarf_version < 5)
28622 ? DEBUG_MACINFO_SECTION_LABEL
28623 : DEBUG_MACRO_SECTION_LABEL, generation);
28624 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28625 generation);
28627 ++generation;
28628 return generation - 1;
28631 /* Set up for Dwarf output at the start of compilation. */
28633 static void
28634 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28636 /* Allocate the file_table. */
28637 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28639 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28640 /* Allocate the decl_die_table. */
28641 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28643 /* Allocate the decl_loc_table. */
28644 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28646 /* Allocate the cached_dw_loc_list_table. */
28647 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28649 /* Allocate the initial hunk of the abbrev_die_table. */
28650 vec_alloc (abbrev_die_table, 256);
28651 /* Zero-th entry is allocated, but unused. */
28652 abbrev_die_table->quick_push (NULL);
28654 /* Allocate the dwarf_proc_stack_usage_map. */
28655 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28657 /* Allocate the pubtypes and pubnames vectors. */
28658 vec_alloc (pubname_table, 32);
28659 vec_alloc (pubtype_table, 32);
28661 vec_alloc (incomplete_types, 64);
28663 vec_alloc (used_rtx_array, 32);
28665 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28666 vec_alloc (macinfo_table, 64);
28667 #endif
28669 /* If front-ends already registered a main translation unit but we were not
28670 ready to perform the association, do this now. */
28671 if (main_translation_unit != NULL_TREE)
28672 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28675 /* Called before compile () starts outputtting functions, variables
28676 and toplevel asms into assembly. */
28678 static void
28679 dwarf2out_assembly_start (void)
28681 if (text_section_line_info)
28682 return;
28684 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28685 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28686 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28687 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28688 COLD_TEXT_SECTION_LABEL, 0);
28689 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28691 switch_to_section (text_section);
28692 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28693 #endif
28695 /* Make sure the line number table for .text always exists. */
28696 text_section_line_info = new_line_info_table ();
28697 text_section_line_info->end_label = text_end_label;
28699 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28700 cur_line_info_table = text_section_line_info;
28701 #endif
28703 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28704 && dwarf2out_do_cfi_asm ()
28705 && !dwarf2out_do_eh_frame ())
28706 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28709 /* A helper function for dwarf2out_finish called through
28710 htab_traverse. Assign a string its index. All strings must be
28711 collected into the table by the time index_string is called,
28712 because the indexing code relies on htab_traverse to traverse nodes
28713 in the same order for each run. */
28716 index_string (indirect_string_node **h, unsigned int *index)
28718 indirect_string_node *node = *h;
28720 find_string_form (node);
28721 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28723 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28724 node->index = *index;
28725 *index += 1;
28727 return 1;
28730 /* A helper function for output_indirect_strings called through
28731 htab_traverse. Output the offset to a string and update the
28732 current offset. */
28735 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28737 indirect_string_node *node = *h;
28739 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28741 /* Assert that this node has been assigned an index. */
28742 gcc_assert (node->index != NO_INDEX_ASSIGNED
28743 && node->index != NOT_INDEXED);
28744 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28745 "indexed string 0x%x: %s", node->index, node->str);
28746 *offset += strlen (node->str) + 1;
28748 return 1;
28751 /* A helper function for dwarf2out_finish called through
28752 htab_traverse. Output the indexed string. */
28755 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28757 struct indirect_string_node *node = *h;
28759 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28761 /* Assert that the strings are output in the same order as their
28762 indexes were assigned. */
28763 gcc_assert (*cur_idx == node->index);
28764 assemble_string (node->str, strlen (node->str) + 1);
28765 *cur_idx += 1;
28767 return 1;
28770 /* A helper function for output_indirect_strings. Counts the number
28771 of index strings offsets. Must match the logic of the functions
28772 output_index_string[_offsets] above. */
28774 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28776 struct indirect_string_node *node = *h;
28778 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28779 *last_idx += 1;
28780 return 1;
28783 /* A helper function for dwarf2out_finish called through
28784 htab_traverse. Emit one queued .debug_str string. */
28787 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28789 struct indirect_string_node *node = *h;
28791 node->form = find_string_form (node);
28792 if (node->form == form && node->refcount > 0)
28794 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28795 assemble_string (node->str, strlen (node->str) + 1);
28798 return 1;
28801 /* Output the indexed string table. */
28803 static void
28804 output_indirect_strings (void)
28806 switch_to_section (debug_str_section);
28807 if (!dwarf_split_debug_info)
28808 debug_str_hash->traverse<enum dwarf_form,
28809 output_indirect_string> (DW_FORM_strp);
28810 else
28812 unsigned int offset = 0;
28813 unsigned int cur_idx = 0;
28815 if (skeleton_debug_str_hash)
28816 skeleton_debug_str_hash->traverse<enum dwarf_form,
28817 output_indirect_string> (DW_FORM_strp);
28819 switch_to_section (debug_str_offsets_section);
28820 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
28821 header. Note that we don't need to generate a label to the
28822 actual index table following the header here, because this is
28823 for the split dwarf case only. In an .dwo file there is only
28824 one string offsets table (and one debug info section). But
28825 if we would start using string offset tables for the main (or
28826 skeleton) unit, then we have to add a DW_AT_str_offsets_base
28827 pointing to the actual index after the header. Split dwarf
28828 units will never have a string offsets base attribute. When
28829 a split unit is moved into a .dwp file the string offsets can
28830 be found through the .debug_cu_index section table. */
28831 if (dwarf_version >= 5)
28833 unsigned int last_idx = 0;
28834 unsigned long str_offsets_length;
28836 debug_str_hash->traverse_noresize
28837 <unsigned int *, count_index_strings> (&last_idx);
28838 str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
28839 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
28840 dw2_asm_output_data (4, 0xffffffff,
28841 "Escape value for 64-bit DWARF extension");
28842 dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
28843 "Length of string offsets unit");
28844 dw2_asm_output_data (2, 5, "DWARF string offsets version");
28845 dw2_asm_output_data (2, 0, "Header zero padding");
28847 debug_str_hash->traverse_noresize
28848 <unsigned int *, output_index_string_offset> (&offset);
28849 switch_to_section (debug_str_dwo_section);
28850 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28851 (&cur_idx);
28855 /* Callback for htab_traverse to assign an index to an entry in the
28856 table, and to write that entry to the .debug_addr section. */
28859 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28861 addr_table_entry *entry = *slot;
28863 if (entry->refcount == 0)
28865 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28866 || entry->index == NOT_INDEXED);
28867 return 1;
28870 gcc_assert (entry->index == *cur_index);
28871 (*cur_index)++;
28873 switch (entry->kind)
28875 case ate_kind_rtx:
28876 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28877 "0x%x", entry->index);
28878 break;
28879 case ate_kind_rtx_dtprel:
28880 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28881 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28882 DWARF2_ADDR_SIZE,
28883 entry->addr.rtl);
28884 fputc ('\n', asm_out_file);
28885 break;
28886 case ate_kind_label:
28887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28888 "0x%x", entry->index);
28889 break;
28890 default:
28891 gcc_unreachable ();
28893 return 1;
28896 /* A helper function for dwarf2out_finish. Counts the number
28897 of indexed addresses. Must match the logic of the functions
28898 output_addr_table_entry above. */
28900 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
28902 addr_table_entry *entry = *slot;
28904 if (entry->refcount > 0)
28905 *last_idx += 1;
28906 return 1;
28909 /* Produce the .debug_addr section. */
28911 static void
28912 output_addr_table (void)
28914 unsigned int index = 0;
28915 if (addr_index_table == NULL || addr_index_table->size () == 0)
28916 return;
28918 switch_to_section (debug_addr_section);
28919 addr_index_table
28920 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28923 #if ENABLE_ASSERT_CHECKING
28924 /* Verify that all marks are clear. */
28926 static void
28927 verify_marks_clear (dw_die_ref die)
28929 dw_die_ref c;
28931 gcc_assert (! die->die_mark);
28932 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28934 #endif /* ENABLE_ASSERT_CHECKING */
28936 /* Clear the marks for a die and its children.
28937 Be cool if the mark isn't set. */
28939 static void
28940 prune_unmark_dies (dw_die_ref die)
28942 dw_die_ref c;
28944 if (die->die_mark)
28945 die->die_mark = 0;
28946 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28949 /* Given LOC that is referenced by a DIE we're marking as used, find all
28950 referenced DWARF procedures it references and mark them as used. */
28952 static void
28953 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28955 for (; loc != NULL; loc = loc->dw_loc_next)
28956 switch (loc->dw_loc_opc)
28958 case DW_OP_implicit_pointer:
28959 case DW_OP_convert:
28960 case DW_OP_reinterpret:
28961 case DW_OP_GNU_implicit_pointer:
28962 case DW_OP_GNU_convert:
28963 case DW_OP_GNU_reinterpret:
28964 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28965 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28966 break;
28967 case DW_OP_GNU_variable_value:
28968 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28970 dw_die_ref ref
28971 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28972 if (ref == NULL)
28973 break;
28974 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28975 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28976 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28978 /* FALLTHRU */
28979 case DW_OP_call2:
28980 case DW_OP_call4:
28981 case DW_OP_call_ref:
28982 case DW_OP_const_type:
28983 case DW_OP_GNU_const_type:
28984 case DW_OP_GNU_parameter_ref:
28985 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28986 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28987 break;
28988 case DW_OP_regval_type:
28989 case DW_OP_deref_type:
28990 case DW_OP_GNU_regval_type:
28991 case DW_OP_GNU_deref_type:
28992 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28993 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28994 break;
28995 case DW_OP_entry_value:
28996 case DW_OP_GNU_entry_value:
28997 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28998 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28999 break;
29000 default:
29001 break;
29005 /* Given DIE that we're marking as used, find any other dies
29006 it references as attributes and mark them as used. */
29008 static void
29009 prune_unused_types_walk_attribs (dw_die_ref die)
29011 dw_attr_node *a;
29012 unsigned ix;
29014 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29016 switch (AT_class (a))
29018 /* Make sure DWARF procedures referenced by location descriptions will
29019 get emitted. */
29020 case dw_val_class_loc:
29021 prune_unused_types_walk_loc_descr (AT_loc (a));
29022 break;
29023 case dw_val_class_loc_list:
29024 for (dw_loc_list_ref list = AT_loc_list (a);
29025 list != NULL;
29026 list = list->dw_loc_next)
29027 prune_unused_types_walk_loc_descr (list->expr);
29028 break;
29030 case dw_val_class_view_list:
29031 /* This points to a loc_list in another attribute, so it's
29032 already covered. */
29033 break;
29035 case dw_val_class_die_ref:
29036 /* A reference to another DIE.
29037 Make sure that it will get emitted.
29038 If it was broken out into a comdat group, don't follow it. */
29039 if (! AT_ref (a)->comdat_type_p
29040 || a->dw_attr == DW_AT_specification)
29041 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29042 break;
29044 case dw_val_class_str:
29045 /* Set the string's refcount to 0 so that prune_unused_types_mark
29046 accounts properly for it. */
29047 a->dw_attr_val.v.val_str->refcount = 0;
29048 break;
29050 default:
29051 break;
29056 /* Mark the generic parameters and arguments children DIEs of DIE. */
29058 static void
29059 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29061 dw_die_ref c;
29063 if (die == NULL || die->die_child == NULL)
29064 return;
29065 c = die->die_child;
29068 if (is_template_parameter (c))
29069 prune_unused_types_mark (c, 1);
29070 c = c->die_sib;
29071 } while (c && c != die->die_child);
29074 /* Mark DIE as being used. If DOKIDS is true, then walk down
29075 to DIE's children. */
29077 static void
29078 prune_unused_types_mark (dw_die_ref die, int dokids)
29080 dw_die_ref c;
29082 if (die->die_mark == 0)
29084 /* We haven't done this node yet. Mark it as used. */
29085 die->die_mark = 1;
29086 /* If this is the DIE of a generic type instantiation,
29087 mark the children DIEs that describe its generic parms and
29088 args. */
29089 prune_unused_types_mark_generic_parms_dies (die);
29091 /* We also have to mark its parents as used.
29092 (But we don't want to mark our parent's kids due to this,
29093 unless it is a class.) */
29094 if (die->die_parent)
29095 prune_unused_types_mark (die->die_parent,
29096 class_scope_p (die->die_parent));
29098 /* Mark any referenced nodes. */
29099 prune_unused_types_walk_attribs (die);
29101 /* If this node is a specification,
29102 also mark the definition, if it exists. */
29103 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29104 prune_unused_types_mark (die->die_definition, 1);
29107 if (dokids && die->die_mark != 2)
29109 /* We need to walk the children, but haven't done so yet.
29110 Remember that we've walked the kids. */
29111 die->die_mark = 2;
29113 /* If this is an array type, we need to make sure our
29114 kids get marked, even if they're types. If we're
29115 breaking out types into comdat sections, do this
29116 for all type definitions. */
29117 if (die->die_tag == DW_TAG_array_type
29118 || (use_debug_types
29119 && is_type_die (die) && ! is_declaration_die (die)))
29120 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29121 else
29122 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29126 /* For local classes, look if any static member functions were emitted
29127 and if so, mark them. */
29129 static void
29130 prune_unused_types_walk_local_classes (dw_die_ref die)
29132 dw_die_ref c;
29134 if (die->die_mark == 2)
29135 return;
29137 switch (die->die_tag)
29139 case DW_TAG_structure_type:
29140 case DW_TAG_union_type:
29141 case DW_TAG_class_type:
29142 case DW_TAG_interface_type:
29143 break;
29145 case DW_TAG_subprogram:
29146 if (!get_AT_flag (die, DW_AT_declaration)
29147 || die->die_definition != NULL)
29148 prune_unused_types_mark (die, 1);
29149 return;
29151 default:
29152 return;
29155 /* Mark children. */
29156 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29159 /* Walk the tree DIE and mark types that we actually use. */
29161 static void
29162 prune_unused_types_walk (dw_die_ref die)
29164 dw_die_ref c;
29166 /* Don't do anything if this node is already marked and
29167 children have been marked as well. */
29168 if (die->die_mark == 2)
29169 return;
29171 switch (die->die_tag)
29173 case DW_TAG_structure_type:
29174 case DW_TAG_union_type:
29175 case DW_TAG_class_type:
29176 case DW_TAG_interface_type:
29177 if (die->die_perennial_p)
29178 break;
29180 for (c = die->die_parent; c; c = c->die_parent)
29181 if (c->die_tag == DW_TAG_subprogram)
29182 break;
29184 /* Finding used static member functions inside of classes
29185 is needed just for local classes, because for other classes
29186 static member function DIEs with DW_AT_specification
29187 are emitted outside of the DW_TAG_*_type. If we ever change
29188 it, we'd need to call this even for non-local classes. */
29189 if (c)
29190 prune_unused_types_walk_local_classes (die);
29192 /* It's a type node --- don't mark it. */
29193 return;
29195 case DW_TAG_const_type:
29196 case DW_TAG_packed_type:
29197 case DW_TAG_pointer_type:
29198 case DW_TAG_reference_type:
29199 case DW_TAG_rvalue_reference_type:
29200 case DW_TAG_volatile_type:
29201 case DW_TAG_typedef:
29202 case DW_TAG_array_type:
29203 case DW_TAG_friend:
29204 case DW_TAG_enumeration_type:
29205 case DW_TAG_subroutine_type:
29206 case DW_TAG_string_type:
29207 case DW_TAG_set_type:
29208 case DW_TAG_subrange_type:
29209 case DW_TAG_ptr_to_member_type:
29210 case DW_TAG_file_type:
29211 /* Type nodes are useful only when other DIEs reference them --- don't
29212 mark them. */
29213 /* FALLTHROUGH */
29215 case DW_TAG_dwarf_procedure:
29216 /* Likewise for DWARF procedures. */
29218 if (die->die_perennial_p)
29219 break;
29221 return;
29223 default:
29224 /* Mark everything else. */
29225 break;
29228 if (die->die_mark == 0)
29230 die->die_mark = 1;
29232 /* Now, mark any dies referenced from here. */
29233 prune_unused_types_walk_attribs (die);
29236 die->die_mark = 2;
29238 /* Mark children. */
29239 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29242 /* Increment the string counts on strings referred to from DIE's
29243 attributes. */
29245 static void
29246 prune_unused_types_update_strings (dw_die_ref die)
29248 dw_attr_node *a;
29249 unsigned ix;
29251 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29252 if (AT_class (a) == dw_val_class_str)
29254 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29255 s->refcount++;
29256 /* Avoid unnecessarily putting strings that are used less than
29257 twice in the hash table. */
29258 if (s->refcount
29259 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29261 indirect_string_node **slot
29262 = debug_str_hash->find_slot_with_hash (s->str,
29263 htab_hash_string (s->str),
29264 INSERT);
29265 gcc_assert (*slot == NULL);
29266 *slot = s;
29271 /* Mark DIE and its children as removed. */
29273 static void
29274 mark_removed (dw_die_ref die)
29276 dw_die_ref c;
29277 die->removed = true;
29278 FOR_EACH_CHILD (die, c, mark_removed (c));
29281 /* Remove from the tree DIE any dies that aren't marked. */
29283 static void
29284 prune_unused_types_prune (dw_die_ref die)
29286 dw_die_ref c;
29288 gcc_assert (die->die_mark);
29289 prune_unused_types_update_strings (die);
29291 if (! die->die_child)
29292 return;
29294 c = die->die_child;
29295 do {
29296 dw_die_ref prev = c, next;
29297 for (c = c->die_sib; ! c->die_mark; c = next)
29298 if (c == die->die_child)
29300 /* No marked children between 'prev' and the end of the list. */
29301 if (prev == c)
29302 /* No marked children at all. */
29303 die->die_child = NULL;
29304 else
29306 prev->die_sib = c->die_sib;
29307 die->die_child = prev;
29309 c->die_sib = NULL;
29310 mark_removed (c);
29311 return;
29313 else
29315 next = c->die_sib;
29316 c->die_sib = NULL;
29317 mark_removed (c);
29320 if (c != prev->die_sib)
29321 prev->die_sib = c;
29322 prune_unused_types_prune (c);
29323 } while (c != die->die_child);
29326 /* Remove dies representing declarations that we never use. */
29328 static void
29329 prune_unused_types (void)
29331 unsigned int i;
29332 limbo_die_node *node;
29333 comdat_type_node *ctnode;
29334 pubname_entry *pub;
29335 dw_die_ref base_type;
29337 #if ENABLE_ASSERT_CHECKING
29338 /* All the marks should already be clear. */
29339 verify_marks_clear (comp_unit_die ());
29340 for (node = limbo_die_list; node; node = node->next)
29341 verify_marks_clear (node->die);
29342 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29343 verify_marks_clear (ctnode->root_die);
29344 #endif /* ENABLE_ASSERT_CHECKING */
29346 /* Mark types that are used in global variables. */
29347 premark_types_used_by_global_vars ();
29349 /* Set the mark on nodes that are actually used. */
29350 prune_unused_types_walk (comp_unit_die ());
29351 for (node = limbo_die_list; node; node = node->next)
29352 prune_unused_types_walk (node->die);
29353 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29355 prune_unused_types_walk (ctnode->root_die);
29356 prune_unused_types_mark (ctnode->type_die, 1);
29359 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29360 are unusual in that they are pubnames that are the children of pubtypes.
29361 They should only be marked via their parent DW_TAG_enumeration_type die,
29362 not as roots in themselves. */
29363 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29364 if (pub->die->die_tag != DW_TAG_enumerator)
29365 prune_unused_types_mark (pub->die, 1);
29366 for (i = 0; base_types.iterate (i, &base_type); i++)
29367 prune_unused_types_mark (base_type, 1);
29369 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29370 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29371 callees). */
29372 cgraph_node *cnode;
29373 FOR_EACH_FUNCTION (cnode)
29374 if (cnode->referred_to_p (false))
29376 dw_die_ref die = lookup_decl_die (cnode->decl);
29377 if (die == NULL || die->die_mark)
29378 continue;
29379 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29380 if (e->caller != cnode
29381 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29383 prune_unused_types_mark (die, 1);
29384 break;
29388 if (debug_str_hash)
29389 debug_str_hash->empty ();
29390 if (skeleton_debug_str_hash)
29391 skeleton_debug_str_hash->empty ();
29392 prune_unused_types_prune (comp_unit_die ());
29393 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29395 node = *pnode;
29396 if (!node->die->die_mark)
29397 *pnode = node->next;
29398 else
29400 prune_unused_types_prune (node->die);
29401 pnode = &node->next;
29404 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29405 prune_unused_types_prune (ctnode->root_die);
29407 /* Leave the marks clear. */
29408 prune_unmark_dies (comp_unit_die ());
29409 for (node = limbo_die_list; node; node = node->next)
29410 prune_unmark_dies (node->die);
29411 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29412 prune_unmark_dies (ctnode->root_die);
29415 /* Helpers to manipulate hash table of comdat type units. */
29417 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29419 static inline hashval_t hash (const comdat_type_node *);
29420 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29423 inline hashval_t
29424 comdat_type_hasher::hash (const comdat_type_node *type_node)
29426 hashval_t h;
29427 memcpy (&h, type_node->signature, sizeof (h));
29428 return h;
29431 inline bool
29432 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29433 const comdat_type_node *type_node_2)
29435 return (! memcmp (type_node_1->signature, type_node_2->signature,
29436 DWARF_TYPE_SIGNATURE_SIZE));
29439 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29440 to the location it would have been added, should we know its
29441 DECL_ASSEMBLER_NAME when we added other attributes. This will
29442 probably improve compactness of debug info, removing equivalent
29443 abbrevs, and hide any differences caused by deferring the
29444 computation of the assembler name, triggered by e.g. PCH. */
29446 static inline void
29447 move_linkage_attr (dw_die_ref die)
29449 unsigned ix = vec_safe_length (die->die_attr);
29450 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29452 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29453 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29455 while (--ix > 0)
29457 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29459 if (prev->dw_attr == DW_AT_decl_line
29460 || prev->dw_attr == DW_AT_decl_column
29461 || prev->dw_attr == DW_AT_name)
29462 break;
29465 if (ix != vec_safe_length (die->die_attr) - 1)
29467 die->die_attr->pop ();
29468 die->die_attr->quick_insert (ix, linkage);
29472 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29473 referenced from typed stack ops and count how often they are used. */
29475 static void
29476 mark_base_types (dw_loc_descr_ref loc)
29478 dw_die_ref base_type = NULL;
29480 for (; loc; loc = loc->dw_loc_next)
29482 switch (loc->dw_loc_opc)
29484 case DW_OP_regval_type:
29485 case DW_OP_deref_type:
29486 case DW_OP_GNU_regval_type:
29487 case DW_OP_GNU_deref_type:
29488 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29489 break;
29490 case DW_OP_convert:
29491 case DW_OP_reinterpret:
29492 case DW_OP_GNU_convert:
29493 case DW_OP_GNU_reinterpret:
29494 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29495 continue;
29496 /* FALLTHRU */
29497 case DW_OP_const_type:
29498 case DW_OP_GNU_const_type:
29499 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29500 break;
29501 case DW_OP_entry_value:
29502 case DW_OP_GNU_entry_value:
29503 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29504 continue;
29505 default:
29506 continue;
29508 gcc_assert (base_type->die_parent == comp_unit_die ());
29509 if (base_type->die_mark)
29510 base_type->die_mark++;
29511 else
29513 base_types.safe_push (base_type);
29514 base_type->die_mark = 1;
29519 /* Comparison function for sorting marked base types. */
29521 static int
29522 base_type_cmp (const void *x, const void *y)
29524 dw_die_ref dx = *(const dw_die_ref *) x;
29525 dw_die_ref dy = *(const dw_die_ref *) y;
29526 unsigned int byte_size1, byte_size2;
29527 unsigned int encoding1, encoding2;
29528 unsigned int align1, align2;
29529 if (dx->die_mark > dy->die_mark)
29530 return -1;
29531 if (dx->die_mark < dy->die_mark)
29532 return 1;
29533 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29534 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29535 if (byte_size1 < byte_size2)
29536 return 1;
29537 if (byte_size1 > byte_size2)
29538 return -1;
29539 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29540 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29541 if (encoding1 < encoding2)
29542 return 1;
29543 if (encoding1 > encoding2)
29544 return -1;
29545 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29546 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29547 if (align1 < align2)
29548 return 1;
29549 if (align1 > align2)
29550 return -1;
29551 return 0;
29554 /* Move base types marked by mark_base_types as early as possible
29555 in the CU, sorted by decreasing usage count both to make the
29556 uleb128 references as small as possible and to make sure they
29557 will have die_offset already computed by calc_die_sizes when
29558 sizes of typed stack loc ops is computed. */
29560 static void
29561 move_marked_base_types (void)
29563 unsigned int i;
29564 dw_die_ref base_type, die, c;
29566 if (base_types.is_empty ())
29567 return;
29569 /* Sort by decreasing usage count, they will be added again in that
29570 order later on. */
29571 base_types.qsort (base_type_cmp);
29572 die = comp_unit_die ();
29573 c = die->die_child;
29576 dw_die_ref prev = c;
29577 c = c->die_sib;
29578 while (c->die_mark)
29580 remove_child_with_prev (c, prev);
29581 /* As base types got marked, there must be at least
29582 one node other than DW_TAG_base_type. */
29583 gcc_assert (die->die_child != NULL);
29584 c = prev->die_sib;
29587 while (c != die->die_child);
29588 gcc_assert (die->die_child);
29589 c = die->die_child;
29590 for (i = 0; base_types.iterate (i, &base_type); i++)
29592 base_type->die_mark = 0;
29593 base_type->die_sib = c->die_sib;
29594 c->die_sib = base_type;
29595 c = base_type;
29599 /* Helper function for resolve_addr, attempt to resolve
29600 one CONST_STRING, return true if successful. Similarly verify that
29601 SYMBOL_REFs refer to variables emitted in the current CU. */
29603 static bool
29604 resolve_one_addr (rtx *addr)
29606 rtx rtl = *addr;
29608 if (GET_CODE (rtl) == CONST_STRING)
29610 size_t len = strlen (XSTR (rtl, 0)) + 1;
29611 tree t = build_string (len, XSTR (rtl, 0));
29612 tree tlen = size_int (len - 1);
29613 TREE_TYPE (t)
29614 = build_array_type (char_type_node, build_index_type (tlen));
29615 rtl = lookup_constant_def (t);
29616 if (!rtl || !MEM_P (rtl))
29617 return false;
29618 rtl = XEXP (rtl, 0);
29619 if (GET_CODE (rtl) == SYMBOL_REF
29620 && SYMBOL_REF_DECL (rtl)
29621 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29622 return false;
29623 vec_safe_push (used_rtx_array, rtl);
29624 *addr = rtl;
29625 return true;
29628 if (GET_CODE (rtl) == SYMBOL_REF
29629 && SYMBOL_REF_DECL (rtl))
29631 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29633 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29634 return false;
29636 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29637 return false;
29640 if (GET_CODE (rtl) == CONST)
29642 subrtx_ptr_iterator::array_type array;
29643 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29644 if (!resolve_one_addr (*iter))
29645 return false;
29648 return true;
29651 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29652 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29653 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29655 static rtx
29656 string_cst_pool_decl (tree t)
29658 rtx rtl = output_constant_def (t, 1);
29659 unsigned char *array;
29660 dw_loc_descr_ref l;
29661 tree decl;
29662 size_t len;
29663 dw_die_ref ref;
29665 if (!rtl || !MEM_P (rtl))
29666 return NULL_RTX;
29667 rtl = XEXP (rtl, 0);
29668 if (GET_CODE (rtl) != SYMBOL_REF
29669 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29670 return NULL_RTX;
29672 decl = SYMBOL_REF_DECL (rtl);
29673 if (!lookup_decl_die (decl))
29675 len = TREE_STRING_LENGTH (t);
29676 vec_safe_push (used_rtx_array, rtl);
29677 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29678 array = ggc_vec_alloc<unsigned char> (len);
29679 memcpy (array, TREE_STRING_POINTER (t), len);
29680 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29681 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29682 l->dw_loc_oprnd2.v.val_vec.length = len;
29683 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29684 l->dw_loc_oprnd2.v.val_vec.array = array;
29685 add_AT_loc (ref, DW_AT_location, l);
29686 equate_decl_number_to_die (decl, ref);
29688 return rtl;
29691 /* Helper function of resolve_addr_in_expr. LOC is
29692 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29693 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29694 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29695 with DW_OP_implicit_pointer if possible
29696 and return true, if unsuccessful, return false. */
29698 static bool
29699 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29701 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29702 HOST_WIDE_INT offset = 0;
29703 dw_die_ref ref = NULL;
29704 tree decl;
29706 if (GET_CODE (rtl) == CONST
29707 && GET_CODE (XEXP (rtl, 0)) == PLUS
29708 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29710 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29711 rtl = XEXP (XEXP (rtl, 0), 0);
29713 if (GET_CODE (rtl) == CONST_STRING)
29715 size_t len = strlen (XSTR (rtl, 0)) + 1;
29716 tree t = build_string (len, XSTR (rtl, 0));
29717 tree tlen = size_int (len - 1);
29719 TREE_TYPE (t)
29720 = build_array_type (char_type_node, build_index_type (tlen));
29721 rtl = string_cst_pool_decl (t);
29722 if (!rtl)
29723 return false;
29725 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29727 decl = SYMBOL_REF_DECL (rtl);
29728 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29730 ref = lookup_decl_die (decl);
29731 if (ref && (get_AT (ref, DW_AT_location)
29732 || get_AT (ref, DW_AT_const_value)))
29734 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29735 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29736 loc->dw_loc_oprnd1.val_entry = NULL;
29737 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29738 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29739 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29740 loc->dw_loc_oprnd2.v.val_int = offset;
29741 return true;
29745 return false;
29748 /* Helper function for resolve_addr, handle one location
29749 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29750 the location list couldn't be resolved. */
29752 static bool
29753 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29755 dw_loc_descr_ref keep = NULL;
29756 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29757 switch (loc->dw_loc_opc)
29759 case DW_OP_addr:
29760 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29762 if ((prev == NULL
29763 || prev->dw_loc_opc == DW_OP_piece
29764 || prev->dw_loc_opc == DW_OP_bit_piece)
29765 && loc->dw_loc_next
29766 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29767 && (!dwarf_strict || dwarf_version >= 5)
29768 && optimize_one_addr_into_implicit_ptr (loc))
29769 break;
29770 return false;
29772 break;
29773 case DW_OP_GNU_addr_index:
29774 case DW_OP_addrx:
29775 case DW_OP_GNU_const_index:
29776 case DW_OP_constx:
29777 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
29778 || loc->dw_loc_opc == DW_OP_addrx)
29779 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
29780 || loc->dw_loc_opc == DW_OP_constx)
29781 && loc->dtprel))
29783 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29784 if (!resolve_one_addr (&rtl))
29785 return false;
29786 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29787 loc->dw_loc_oprnd1.val_entry
29788 = add_addr_table_entry (rtl, ate_kind_rtx);
29790 break;
29791 case DW_OP_const4u:
29792 case DW_OP_const8u:
29793 if (loc->dtprel
29794 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29795 return false;
29796 break;
29797 case DW_OP_plus_uconst:
29798 if (size_of_loc_descr (loc)
29799 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29801 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29803 dw_loc_descr_ref repl
29804 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29805 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29806 add_loc_descr (&repl, loc->dw_loc_next);
29807 *loc = *repl;
29809 break;
29810 case DW_OP_implicit_value:
29811 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29812 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29813 return false;
29814 break;
29815 case DW_OP_implicit_pointer:
29816 case DW_OP_GNU_implicit_pointer:
29817 case DW_OP_GNU_parameter_ref:
29818 case DW_OP_GNU_variable_value:
29819 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29821 dw_die_ref ref
29822 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29823 if (ref == NULL)
29824 return false;
29825 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29826 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29827 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29829 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29831 if (prev == NULL
29832 && loc->dw_loc_next == NULL
29833 && AT_class (a) == dw_val_class_loc)
29834 switch (a->dw_attr)
29836 /* Following attributes allow both exprloc and reference,
29837 so if the whole expression is DW_OP_GNU_variable_value
29838 alone we could transform it into reference. */
29839 case DW_AT_byte_size:
29840 case DW_AT_bit_size:
29841 case DW_AT_lower_bound:
29842 case DW_AT_upper_bound:
29843 case DW_AT_bit_stride:
29844 case DW_AT_count:
29845 case DW_AT_allocated:
29846 case DW_AT_associated:
29847 case DW_AT_byte_stride:
29848 a->dw_attr_val.val_class = dw_val_class_die_ref;
29849 a->dw_attr_val.val_entry = NULL;
29850 a->dw_attr_val.v.val_die_ref.die
29851 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29852 a->dw_attr_val.v.val_die_ref.external = 0;
29853 return true;
29854 default:
29855 break;
29857 if (dwarf_strict)
29858 return false;
29860 break;
29861 case DW_OP_const_type:
29862 case DW_OP_regval_type:
29863 case DW_OP_deref_type:
29864 case DW_OP_convert:
29865 case DW_OP_reinterpret:
29866 case DW_OP_GNU_const_type:
29867 case DW_OP_GNU_regval_type:
29868 case DW_OP_GNU_deref_type:
29869 case DW_OP_GNU_convert:
29870 case DW_OP_GNU_reinterpret:
29871 while (loc->dw_loc_next
29872 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29873 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29875 dw_die_ref base1, base2;
29876 unsigned enc1, enc2, size1, size2;
29877 if (loc->dw_loc_opc == DW_OP_regval_type
29878 || loc->dw_loc_opc == DW_OP_deref_type
29879 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29880 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29881 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29882 else if (loc->dw_loc_oprnd1.val_class
29883 == dw_val_class_unsigned_const)
29884 break;
29885 else
29886 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29887 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29888 == dw_val_class_unsigned_const)
29889 break;
29890 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29891 gcc_assert (base1->die_tag == DW_TAG_base_type
29892 && base2->die_tag == DW_TAG_base_type);
29893 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29894 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29895 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29896 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29897 if (size1 == size2
29898 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29899 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29900 && loc != keep)
29901 || enc1 == enc2))
29903 /* Optimize away next DW_OP_convert after
29904 adjusting LOC's base type die reference. */
29905 if (loc->dw_loc_opc == DW_OP_regval_type
29906 || loc->dw_loc_opc == DW_OP_deref_type
29907 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29908 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29909 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29910 else
29911 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29912 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29913 continue;
29915 /* Don't change integer DW_OP_convert after e.g. floating
29916 point typed stack entry. */
29917 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29918 keep = loc->dw_loc_next;
29919 break;
29921 break;
29922 default:
29923 break;
29925 return true;
29928 /* Helper function of resolve_addr. DIE had DW_AT_location of
29929 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29930 and DW_OP_addr couldn't be resolved. resolve_addr has already
29931 removed the DW_AT_location attribute. This function attempts to
29932 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29933 to it or DW_AT_const_value attribute, if possible. */
29935 static void
29936 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29938 if (!VAR_P (decl)
29939 || lookup_decl_die (decl) != die
29940 || DECL_EXTERNAL (decl)
29941 || !TREE_STATIC (decl)
29942 || DECL_INITIAL (decl) == NULL_TREE
29943 || DECL_P (DECL_INITIAL (decl))
29944 || get_AT (die, DW_AT_const_value))
29945 return;
29947 tree init = DECL_INITIAL (decl);
29948 HOST_WIDE_INT offset = 0;
29949 /* For variables that have been optimized away and thus
29950 don't have a memory location, see if we can emit
29951 DW_AT_const_value instead. */
29952 if (tree_add_const_value_attribute (die, init))
29953 return;
29954 if (dwarf_strict && dwarf_version < 5)
29955 return;
29956 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29957 and ADDR_EXPR refers to a decl that has DW_AT_location or
29958 DW_AT_const_value (but isn't addressable, otherwise
29959 resolving the original DW_OP_addr wouldn't fail), see if
29960 we can add DW_OP_implicit_pointer. */
29961 STRIP_NOPS (init);
29962 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29963 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29965 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29966 init = TREE_OPERAND (init, 0);
29967 STRIP_NOPS (init);
29969 if (TREE_CODE (init) != ADDR_EXPR)
29970 return;
29971 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29972 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29973 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29974 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29975 && TREE_OPERAND (init, 0) != decl))
29977 dw_die_ref ref;
29978 dw_loc_descr_ref l;
29980 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29982 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29983 if (!rtl)
29984 return;
29985 decl = SYMBOL_REF_DECL (rtl);
29987 else
29988 decl = TREE_OPERAND (init, 0);
29989 ref = lookup_decl_die (decl);
29990 if (ref == NULL
29991 || (!get_AT (ref, DW_AT_location)
29992 && !get_AT (ref, DW_AT_const_value)))
29993 return;
29994 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29995 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29996 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29997 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29998 add_AT_loc (die, DW_AT_location, l);
30002 /* Return NULL if l is a DWARF expression, or first op that is not
30003 valid DWARF expression. */
30005 static dw_loc_descr_ref
30006 non_dwarf_expression (dw_loc_descr_ref l)
30008 while (l)
30010 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30011 return l;
30012 switch (l->dw_loc_opc)
30014 case DW_OP_regx:
30015 case DW_OP_implicit_value:
30016 case DW_OP_stack_value:
30017 case DW_OP_implicit_pointer:
30018 case DW_OP_GNU_implicit_pointer:
30019 case DW_OP_GNU_parameter_ref:
30020 case DW_OP_piece:
30021 case DW_OP_bit_piece:
30022 return l;
30023 default:
30024 break;
30026 l = l->dw_loc_next;
30028 return NULL;
30031 /* Return adjusted copy of EXPR:
30032 If it is empty DWARF expression, return it.
30033 If it is valid non-empty DWARF expression,
30034 return copy of EXPR with DW_OP_deref appended to it.
30035 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30036 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30037 If it is DWARF expression followed by DW_OP_stack_value, return
30038 copy of the DWARF expression without anything appended.
30039 Otherwise, return NULL. */
30041 static dw_loc_descr_ref
30042 copy_deref_exprloc (dw_loc_descr_ref expr)
30044 dw_loc_descr_ref tail = NULL;
30046 if (expr == NULL)
30047 return NULL;
30049 dw_loc_descr_ref l = non_dwarf_expression (expr);
30050 if (l && l->dw_loc_next)
30051 return NULL;
30053 if (l)
30055 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30056 tail = new_loc_descr ((enum dwarf_location_atom)
30057 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30058 0, 0);
30059 else
30060 switch (l->dw_loc_opc)
30062 case DW_OP_regx:
30063 tail = new_loc_descr (DW_OP_bregx,
30064 l->dw_loc_oprnd1.v.val_unsigned, 0);
30065 break;
30066 case DW_OP_stack_value:
30067 break;
30068 default:
30069 return NULL;
30072 else
30073 tail = new_loc_descr (DW_OP_deref, 0, 0);
30075 dw_loc_descr_ref ret = NULL, *p = &ret;
30076 while (expr != l)
30078 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30079 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30080 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30081 p = &(*p)->dw_loc_next;
30082 expr = expr->dw_loc_next;
30084 *p = tail;
30085 return ret;
30088 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30089 reference to a variable or argument, adjust it if needed and return:
30090 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30091 attribute if present should be removed
30092 0 keep the attribute perhaps with minor modifications, no need to rescan
30093 1 if the attribute has been successfully adjusted. */
30095 static int
30096 optimize_string_length (dw_attr_node *a)
30098 dw_loc_descr_ref l = AT_loc (a), lv;
30099 dw_die_ref die;
30100 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30102 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30103 die = lookup_decl_die (decl);
30104 if (die)
30106 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30107 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30108 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30110 else
30111 return -1;
30113 else
30114 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30116 /* DWARF5 allows reference class, so we can then reference the DIE.
30117 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30118 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30120 a->dw_attr_val.val_class = dw_val_class_die_ref;
30121 a->dw_attr_val.val_entry = NULL;
30122 a->dw_attr_val.v.val_die_ref.die = die;
30123 a->dw_attr_val.v.val_die_ref.external = 0;
30124 return 0;
30127 dw_attr_node *av = get_AT (die, DW_AT_location);
30128 dw_loc_list_ref d;
30129 bool non_dwarf_expr = false;
30131 if (av == NULL)
30132 return dwarf_strict ? -1 : 0;
30133 switch (AT_class (av))
30135 case dw_val_class_loc_list:
30136 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30137 if (d->expr && non_dwarf_expression (d->expr))
30138 non_dwarf_expr = true;
30139 break;
30140 case dw_val_class_view_list:
30141 gcc_unreachable ();
30142 case dw_val_class_loc:
30143 lv = AT_loc (av);
30144 if (lv == NULL)
30145 return dwarf_strict ? -1 : 0;
30146 if (non_dwarf_expression (lv))
30147 non_dwarf_expr = true;
30148 break;
30149 default:
30150 return dwarf_strict ? -1 : 0;
30153 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30154 into DW_OP_call4 or DW_OP_GNU_variable_value into
30155 DW_OP_call4 DW_OP_deref, do so. */
30156 if (!non_dwarf_expr
30157 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30159 l->dw_loc_opc = DW_OP_call4;
30160 if (l->dw_loc_next)
30161 l->dw_loc_next = NULL;
30162 else
30163 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30164 return 0;
30167 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30168 copy over the DW_AT_location attribute from die to a. */
30169 if (l->dw_loc_next != NULL)
30171 a->dw_attr_val = av->dw_attr_val;
30172 return 1;
30175 dw_loc_list_ref list, *p;
30176 switch (AT_class (av))
30178 case dw_val_class_loc_list:
30179 p = &list;
30180 list = NULL;
30181 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30183 lv = copy_deref_exprloc (d->expr);
30184 if (lv)
30186 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30187 p = &(*p)->dw_loc_next;
30189 else if (!dwarf_strict && d->expr)
30190 return 0;
30192 if (list == NULL)
30193 return dwarf_strict ? -1 : 0;
30194 a->dw_attr_val.val_class = dw_val_class_loc_list;
30195 gen_llsym (list);
30196 *AT_loc_list_ptr (a) = list;
30197 return 1;
30198 case dw_val_class_loc:
30199 lv = copy_deref_exprloc (AT_loc (av));
30200 if (lv == NULL)
30201 return dwarf_strict ? -1 : 0;
30202 a->dw_attr_val.v.val_loc = lv;
30203 return 1;
30204 default:
30205 gcc_unreachable ();
30209 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30210 an address in .rodata section if the string literal is emitted there,
30211 or remove the containing location list or replace DW_AT_const_value
30212 with DW_AT_location and empty location expression, if it isn't found
30213 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30214 to something that has been emitted in the current CU. */
30216 static void
30217 resolve_addr (dw_die_ref die)
30219 dw_die_ref c;
30220 dw_attr_node *a;
30221 dw_loc_list_ref *curr, *start, loc;
30222 unsigned ix;
30223 bool remove_AT_byte_size = false;
30225 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30226 switch (AT_class (a))
30228 case dw_val_class_loc_list:
30229 start = curr = AT_loc_list_ptr (a);
30230 loc = *curr;
30231 gcc_assert (loc);
30232 /* The same list can be referenced more than once. See if we have
30233 already recorded the result from a previous pass. */
30234 if (loc->replaced)
30235 *curr = loc->dw_loc_next;
30236 else if (!loc->resolved_addr)
30238 /* As things stand, we do not expect or allow one die to
30239 reference a suffix of another die's location list chain.
30240 References must be identical or completely separate.
30241 There is therefore no need to cache the result of this
30242 pass on any list other than the first; doing so
30243 would lead to unnecessary writes. */
30244 while (*curr)
30246 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30247 if (!resolve_addr_in_expr (a, (*curr)->expr))
30249 dw_loc_list_ref next = (*curr)->dw_loc_next;
30250 dw_loc_descr_ref l = (*curr)->expr;
30252 if (next && (*curr)->ll_symbol)
30254 gcc_assert (!next->ll_symbol);
30255 next->ll_symbol = (*curr)->ll_symbol;
30256 next->vl_symbol = (*curr)->vl_symbol;
30258 if (dwarf_split_debug_info)
30259 remove_loc_list_addr_table_entries (l);
30260 *curr = next;
30262 else
30264 mark_base_types ((*curr)->expr);
30265 curr = &(*curr)->dw_loc_next;
30268 if (loc == *start)
30269 loc->resolved_addr = 1;
30270 else
30272 loc->replaced = 1;
30273 loc->dw_loc_next = *start;
30276 if (!*start)
30278 remove_AT (die, a->dw_attr);
30279 ix--;
30281 break;
30282 case dw_val_class_view_list:
30284 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30285 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30286 dw_val_node *llnode
30287 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30288 /* If we no longer have a loclist, or it no longer needs
30289 views, drop this attribute. */
30290 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30292 remove_AT (die, a->dw_attr);
30293 ix--;
30295 break;
30297 case dw_val_class_loc:
30299 dw_loc_descr_ref l = AT_loc (a);
30300 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30301 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30302 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30303 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30304 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30305 with DW_FORM_ref referencing the same DIE as
30306 DW_OP_GNU_variable_value used to reference. */
30307 if (a->dw_attr == DW_AT_string_length
30308 && l
30309 && l->dw_loc_opc == DW_OP_GNU_variable_value
30310 && (l->dw_loc_next == NULL
30311 || (l->dw_loc_next->dw_loc_next == NULL
30312 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30314 switch (optimize_string_length (a))
30316 case -1:
30317 remove_AT (die, a->dw_attr);
30318 ix--;
30319 /* If we drop DW_AT_string_length, we need to drop also
30320 DW_AT_{string_length_,}byte_size. */
30321 remove_AT_byte_size = true;
30322 continue;
30323 default:
30324 break;
30325 case 1:
30326 /* Even if we keep the optimized DW_AT_string_length,
30327 it might have changed AT_class, so process it again. */
30328 ix--;
30329 continue;
30332 /* For -gdwarf-2 don't attempt to optimize
30333 DW_AT_data_member_location containing
30334 DW_OP_plus_uconst - older consumers might
30335 rely on it being that op instead of a more complex,
30336 but shorter, location description. */
30337 if ((dwarf_version > 2
30338 || a->dw_attr != DW_AT_data_member_location
30339 || l == NULL
30340 || l->dw_loc_opc != DW_OP_plus_uconst
30341 || l->dw_loc_next != NULL)
30342 && !resolve_addr_in_expr (a, l))
30344 if (dwarf_split_debug_info)
30345 remove_loc_list_addr_table_entries (l);
30346 if (l != NULL
30347 && l->dw_loc_next == NULL
30348 && l->dw_loc_opc == DW_OP_addr
30349 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30350 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30351 && a->dw_attr == DW_AT_location)
30353 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30354 remove_AT (die, a->dw_attr);
30355 ix--;
30356 optimize_location_into_implicit_ptr (die, decl);
30357 break;
30359 if (a->dw_attr == DW_AT_string_length)
30360 /* If we drop DW_AT_string_length, we need to drop also
30361 DW_AT_{string_length_,}byte_size. */
30362 remove_AT_byte_size = true;
30363 remove_AT (die, a->dw_attr);
30364 ix--;
30366 else
30367 mark_base_types (l);
30369 break;
30370 case dw_val_class_addr:
30371 if (a->dw_attr == DW_AT_const_value
30372 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30374 if (AT_index (a) != NOT_INDEXED)
30375 remove_addr_table_entry (a->dw_attr_val.val_entry);
30376 remove_AT (die, a->dw_attr);
30377 ix--;
30379 if ((die->die_tag == DW_TAG_call_site
30380 && a->dw_attr == DW_AT_call_origin)
30381 || (die->die_tag == DW_TAG_GNU_call_site
30382 && a->dw_attr == DW_AT_abstract_origin))
30384 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30385 dw_die_ref tdie = lookup_decl_die (tdecl);
30386 dw_die_ref cdie;
30387 if (tdie == NULL
30388 && DECL_EXTERNAL (tdecl)
30389 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30390 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30392 dw_die_ref pdie = cdie;
30393 /* Make sure we don't add these DIEs into type units.
30394 We could emit skeleton DIEs for context (namespaces,
30395 outer structs/classes) and a skeleton DIE for the
30396 innermost context with DW_AT_signature pointing to the
30397 type unit. See PR78835. */
30398 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30399 pdie = pdie->die_parent;
30400 if (pdie == NULL)
30402 /* Creating a full DIE for tdecl is overly expensive and
30403 at this point even wrong when in the LTO phase
30404 as it can end up generating new type DIEs we didn't
30405 output and thus optimize_external_refs will crash. */
30406 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30407 add_AT_flag (tdie, DW_AT_external, 1);
30408 add_AT_flag (tdie, DW_AT_declaration, 1);
30409 add_linkage_attr (tdie, tdecl);
30410 add_name_and_src_coords_attributes (tdie, tdecl, true);
30411 equate_decl_number_to_die (tdecl, tdie);
30414 if (tdie)
30416 a->dw_attr_val.val_class = dw_val_class_die_ref;
30417 a->dw_attr_val.v.val_die_ref.die = tdie;
30418 a->dw_attr_val.v.val_die_ref.external = 0;
30420 else
30422 if (AT_index (a) != NOT_INDEXED)
30423 remove_addr_table_entry (a->dw_attr_val.val_entry);
30424 remove_AT (die, a->dw_attr);
30425 ix--;
30428 break;
30429 default:
30430 break;
30433 if (remove_AT_byte_size)
30434 remove_AT (die, dwarf_version >= 5
30435 ? DW_AT_string_length_byte_size
30436 : DW_AT_byte_size);
30438 FOR_EACH_CHILD (die, c, resolve_addr (c));
30441 /* Helper routines for optimize_location_lists.
30442 This pass tries to share identical local lists in .debug_loc
30443 section. */
30445 /* Iteratively hash operands of LOC opcode into HSTATE. */
30447 static void
30448 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30450 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30451 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30453 switch (loc->dw_loc_opc)
30455 case DW_OP_const4u:
30456 case DW_OP_const8u:
30457 if (loc->dtprel)
30458 goto hash_addr;
30459 /* FALLTHRU */
30460 case DW_OP_const1u:
30461 case DW_OP_const1s:
30462 case DW_OP_const2u:
30463 case DW_OP_const2s:
30464 case DW_OP_const4s:
30465 case DW_OP_const8s:
30466 case DW_OP_constu:
30467 case DW_OP_consts:
30468 case DW_OP_pick:
30469 case DW_OP_plus_uconst:
30470 case DW_OP_breg0:
30471 case DW_OP_breg1:
30472 case DW_OP_breg2:
30473 case DW_OP_breg3:
30474 case DW_OP_breg4:
30475 case DW_OP_breg5:
30476 case DW_OP_breg6:
30477 case DW_OP_breg7:
30478 case DW_OP_breg8:
30479 case DW_OP_breg9:
30480 case DW_OP_breg10:
30481 case DW_OP_breg11:
30482 case DW_OP_breg12:
30483 case DW_OP_breg13:
30484 case DW_OP_breg14:
30485 case DW_OP_breg15:
30486 case DW_OP_breg16:
30487 case DW_OP_breg17:
30488 case DW_OP_breg18:
30489 case DW_OP_breg19:
30490 case DW_OP_breg20:
30491 case DW_OP_breg21:
30492 case DW_OP_breg22:
30493 case DW_OP_breg23:
30494 case DW_OP_breg24:
30495 case DW_OP_breg25:
30496 case DW_OP_breg26:
30497 case DW_OP_breg27:
30498 case DW_OP_breg28:
30499 case DW_OP_breg29:
30500 case DW_OP_breg30:
30501 case DW_OP_breg31:
30502 case DW_OP_regx:
30503 case DW_OP_fbreg:
30504 case DW_OP_piece:
30505 case DW_OP_deref_size:
30506 case DW_OP_xderef_size:
30507 hstate.add_object (val1->v.val_int);
30508 break;
30509 case DW_OP_skip:
30510 case DW_OP_bra:
30512 int offset;
30514 gcc_assert (val1->val_class == dw_val_class_loc);
30515 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30516 hstate.add_object (offset);
30518 break;
30519 case DW_OP_implicit_value:
30520 hstate.add_object (val1->v.val_unsigned);
30521 switch (val2->val_class)
30523 case dw_val_class_const:
30524 hstate.add_object (val2->v.val_int);
30525 break;
30526 case dw_val_class_vec:
30528 unsigned int elt_size = val2->v.val_vec.elt_size;
30529 unsigned int len = val2->v.val_vec.length;
30531 hstate.add_int (elt_size);
30532 hstate.add_int (len);
30533 hstate.add (val2->v.val_vec.array, len * elt_size);
30535 break;
30536 case dw_val_class_const_double:
30537 hstate.add_object (val2->v.val_double.low);
30538 hstate.add_object (val2->v.val_double.high);
30539 break;
30540 case dw_val_class_wide_int:
30541 hstate.add (val2->v.val_wide->get_val (),
30542 get_full_len (*val2->v.val_wide)
30543 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30544 break;
30545 case dw_val_class_addr:
30546 inchash::add_rtx (val2->v.val_addr, hstate);
30547 break;
30548 default:
30549 gcc_unreachable ();
30551 break;
30552 case DW_OP_bregx:
30553 case DW_OP_bit_piece:
30554 hstate.add_object (val1->v.val_int);
30555 hstate.add_object (val2->v.val_int);
30556 break;
30557 case DW_OP_addr:
30558 hash_addr:
30559 if (loc->dtprel)
30561 unsigned char dtprel = 0xd1;
30562 hstate.add_object (dtprel);
30564 inchash::add_rtx (val1->v.val_addr, hstate);
30565 break;
30566 case DW_OP_GNU_addr_index:
30567 case DW_OP_addrx:
30568 case DW_OP_GNU_const_index:
30569 case DW_OP_constx:
30571 if (loc->dtprel)
30573 unsigned char dtprel = 0xd1;
30574 hstate.add_object (dtprel);
30576 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30578 break;
30579 case DW_OP_implicit_pointer:
30580 case DW_OP_GNU_implicit_pointer:
30581 hstate.add_int (val2->v.val_int);
30582 break;
30583 case DW_OP_entry_value:
30584 case DW_OP_GNU_entry_value:
30585 hstate.add_object (val1->v.val_loc);
30586 break;
30587 case DW_OP_regval_type:
30588 case DW_OP_deref_type:
30589 case DW_OP_GNU_regval_type:
30590 case DW_OP_GNU_deref_type:
30592 unsigned int byte_size
30593 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30594 unsigned int encoding
30595 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30596 hstate.add_object (val1->v.val_int);
30597 hstate.add_object (byte_size);
30598 hstate.add_object (encoding);
30600 break;
30601 case DW_OP_convert:
30602 case DW_OP_reinterpret:
30603 case DW_OP_GNU_convert:
30604 case DW_OP_GNU_reinterpret:
30605 if (val1->val_class == dw_val_class_unsigned_const)
30607 hstate.add_object (val1->v.val_unsigned);
30608 break;
30610 /* FALLTHRU */
30611 case DW_OP_const_type:
30612 case DW_OP_GNU_const_type:
30614 unsigned int byte_size
30615 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30616 unsigned int encoding
30617 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30618 hstate.add_object (byte_size);
30619 hstate.add_object (encoding);
30620 if (loc->dw_loc_opc != DW_OP_const_type
30621 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30622 break;
30623 hstate.add_object (val2->val_class);
30624 switch (val2->val_class)
30626 case dw_val_class_const:
30627 hstate.add_object (val2->v.val_int);
30628 break;
30629 case dw_val_class_vec:
30631 unsigned int elt_size = val2->v.val_vec.elt_size;
30632 unsigned int len = val2->v.val_vec.length;
30634 hstate.add_object (elt_size);
30635 hstate.add_object (len);
30636 hstate.add (val2->v.val_vec.array, len * elt_size);
30638 break;
30639 case dw_val_class_const_double:
30640 hstate.add_object (val2->v.val_double.low);
30641 hstate.add_object (val2->v.val_double.high);
30642 break;
30643 case dw_val_class_wide_int:
30644 hstate.add (val2->v.val_wide->get_val (),
30645 get_full_len (*val2->v.val_wide)
30646 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30647 break;
30648 default:
30649 gcc_unreachable ();
30652 break;
30654 default:
30655 /* Other codes have no operands. */
30656 break;
30660 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30662 static inline void
30663 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30665 dw_loc_descr_ref l;
30666 bool sizes_computed = false;
30667 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30668 size_of_locs (loc);
30670 for (l = loc; l != NULL; l = l->dw_loc_next)
30672 enum dwarf_location_atom opc = l->dw_loc_opc;
30673 hstate.add_object (opc);
30674 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30676 size_of_locs (loc);
30677 sizes_computed = true;
30679 hash_loc_operands (l, hstate);
30683 /* Compute hash of the whole location list LIST_HEAD. */
30685 static inline void
30686 hash_loc_list (dw_loc_list_ref list_head)
30688 dw_loc_list_ref curr = list_head;
30689 inchash::hash hstate;
30691 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30693 hstate.add (curr->begin, strlen (curr->begin) + 1);
30694 hstate.add (curr->end, strlen (curr->end) + 1);
30695 hstate.add_object (curr->vbegin);
30696 hstate.add_object (curr->vend);
30697 if (curr->section)
30698 hstate.add (curr->section, strlen (curr->section) + 1);
30699 hash_locs (curr->expr, hstate);
30701 list_head->hash = hstate.end ();
30704 /* Return true if X and Y opcodes have the same operands. */
30706 static inline bool
30707 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30709 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30710 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30711 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30712 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30714 switch (x->dw_loc_opc)
30716 case DW_OP_const4u:
30717 case DW_OP_const8u:
30718 if (x->dtprel)
30719 goto hash_addr;
30720 /* FALLTHRU */
30721 case DW_OP_const1u:
30722 case DW_OP_const1s:
30723 case DW_OP_const2u:
30724 case DW_OP_const2s:
30725 case DW_OP_const4s:
30726 case DW_OP_const8s:
30727 case DW_OP_constu:
30728 case DW_OP_consts:
30729 case DW_OP_pick:
30730 case DW_OP_plus_uconst:
30731 case DW_OP_breg0:
30732 case DW_OP_breg1:
30733 case DW_OP_breg2:
30734 case DW_OP_breg3:
30735 case DW_OP_breg4:
30736 case DW_OP_breg5:
30737 case DW_OP_breg6:
30738 case DW_OP_breg7:
30739 case DW_OP_breg8:
30740 case DW_OP_breg9:
30741 case DW_OP_breg10:
30742 case DW_OP_breg11:
30743 case DW_OP_breg12:
30744 case DW_OP_breg13:
30745 case DW_OP_breg14:
30746 case DW_OP_breg15:
30747 case DW_OP_breg16:
30748 case DW_OP_breg17:
30749 case DW_OP_breg18:
30750 case DW_OP_breg19:
30751 case DW_OP_breg20:
30752 case DW_OP_breg21:
30753 case DW_OP_breg22:
30754 case DW_OP_breg23:
30755 case DW_OP_breg24:
30756 case DW_OP_breg25:
30757 case DW_OP_breg26:
30758 case DW_OP_breg27:
30759 case DW_OP_breg28:
30760 case DW_OP_breg29:
30761 case DW_OP_breg30:
30762 case DW_OP_breg31:
30763 case DW_OP_regx:
30764 case DW_OP_fbreg:
30765 case DW_OP_piece:
30766 case DW_OP_deref_size:
30767 case DW_OP_xderef_size:
30768 return valx1->v.val_int == valy1->v.val_int;
30769 case DW_OP_skip:
30770 case DW_OP_bra:
30771 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30772 can cause irrelevant differences in dw_loc_addr. */
30773 gcc_assert (valx1->val_class == dw_val_class_loc
30774 && valy1->val_class == dw_val_class_loc
30775 && (dwarf_split_debug_info
30776 || x->dw_loc_addr == y->dw_loc_addr));
30777 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30778 case DW_OP_implicit_value:
30779 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30780 || valx2->val_class != valy2->val_class)
30781 return false;
30782 switch (valx2->val_class)
30784 case dw_val_class_const:
30785 return valx2->v.val_int == valy2->v.val_int;
30786 case dw_val_class_vec:
30787 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30788 && valx2->v.val_vec.length == valy2->v.val_vec.length
30789 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30790 valx2->v.val_vec.elt_size
30791 * valx2->v.val_vec.length) == 0;
30792 case dw_val_class_const_double:
30793 return valx2->v.val_double.low == valy2->v.val_double.low
30794 && valx2->v.val_double.high == valy2->v.val_double.high;
30795 case dw_val_class_wide_int:
30796 return *valx2->v.val_wide == *valy2->v.val_wide;
30797 case dw_val_class_addr:
30798 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30799 default:
30800 gcc_unreachable ();
30802 case DW_OP_bregx:
30803 case DW_OP_bit_piece:
30804 return valx1->v.val_int == valy1->v.val_int
30805 && valx2->v.val_int == valy2->v.val_int;
30806 case DW_OP_addr:
30807 hash_addr:
30808 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30809 case DW_OP_GNU_addr_index:
30810 case DW_OP_addrx:
30811 case DW_OP_GNU_const_index:
30812 case DW_OP_constx:
30814 rtx ax1 = valx1->val_entry->addr.rtl;
30815 rtx ay1 = valy1->val_entry->addr.rtl;
30816 return rtx_equal_p (ax1, ay1);
30818 case DW_OP_implicit_pointer:
30819 case DW_OP_GNU_implicit_pointer:
30820 return valx1->val_class == dw_val_class_die_ref
30821 && valx1->val_class == valy1->val_class
30822 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30823 && valx2->v.val_int == valy2->v.val_int;
30824 case DW_OP_entry_value:
30825 case DW_OP_GNU_entry_value:
30826 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30827 case DW_OP_const_type:
30828 case DW_OP_GNU_const_type:
30829 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30830 || valx2->val_class != valy2->val_class)
30831 return false;
30832 switch (valx2->val_class)
30834 case dw_val_class_const:
30835 return valx2->v.val_int == valy2->v.val_int;
30836 case dw_val_class_vec:
30837 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30838 && valx2->v.val_vec.length == valy2->v.val_vec.length
30839 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30840 valx2->v.val_vec.elt_size
30841 * valx2->v.val_vec.length) == 0;
30842 case dw_val_class_const_double:
30843 return valx2->v.val_double.low == valy2->v.val_double.low
30844 && valx2->v.val_double.high == valy2->v.val_double.high;
30845 case dw_val_class_wide_int:
30846 return *valx2->v.val_wide == *valy2->v.val_wide;
30847 default:
30848 gcc_unreachable ();
30850 case DW_OP_regval_type:
30851 case DW_OP_deref_type:
30852 case DW_OP_GNU_regval_type:
30853 case DW_OP_GNU_deref_type:
30854 return valx1->v.val_int == valy1->v.val_int
30855 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30856 case DW_OP_convert:
30857 case DW_OP_reinterpret:
30858 case DW_OP_GNU_convert:
30859 case DW_OP_GNU_reinterpret:
30860 if (valx1->val_class != valy1->val_class)
30861 return false;
30862 if (valx1->val_class == dw_val_class_unsigned_const)
30863 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30864 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30865 case DW_OP_GNU_parameter_ref:
30866 return valx1->val_class == dw_val_class_die_ref
30867 && valx1->val_class == valy1->val_class
30868 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30869 default:
30870 /* Other codes have no operands. */
30871 return true;
30875 /* Return true if DWARF location expressions X and Y are the same. */
30877 static inline bool
30878 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30880 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30881 if (x->dw_loc_opc != y->dw_loc_opc
30882 || x->dtprel != y->dtprel
30883 || !compare_loc_operands (x, y))
30884 break;
30885 return x == NULL && y == NULL;
30888 /* Hashtable helpers. */
30890 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30892 static inline hashval_t hash (const dw_loc_list_struct *);
30893 static inline bool equal (const dw_loc_list_struct *,
30894 const dw_loc_list_struct *);
30897 /* Return precomputed hash of location list X. */
30899 inline hashval_t
30900 loc_list_hasher::hash (const dw_loc_list_struct *x)
30902 return x->hash;
30905 /* Return true if location lists A and B are the same. */
30907 inline bool
30908 loc_list_hasher::equal (const dw_loc_list_struct *a,
30909 const dw_loc_list_struct *b)
30911 if (a == b)
30912 return 1;
30913 if (a->hash != b->hash)
30914 return 0;
30915 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30916 if (strcmp (a->begin, b->begin) != 0
30917 || strcmp (a->end, b->end) != 0
30918 || (a->section == NULL) != (b->section == NULL)
30919 || (a->section && strcmp (a->section, b->section) != 0)
30920 || a->vbegin != b->vbegin || a->vend != b->vend
30921 || !compare_locs (a->expr, b->expr))
30922 break;
30923 return a == NULL && b == NULL;
30926 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30929 /* Recursively optimize location lists referenced from DIE
30930 children and share them whenever possible. */
30932 static void
30933 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30935 dw_die_ref c;
30936 dw_attr_node *a;
30937 unsigned ix;
30938 dw_loc_list_struct **slot;
30939 bool drop_locviews = false;
30940 bool has_locviews = false;
30942 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30943 if (AT_class (a) == dw_val_class_loc_list)
30945 dw_loc_list_ref list = AT_loc_list (a);
30946 /* TODO: perform some optimizations here, before hashing
30947 it and storing into the hash table. */
30948 hash_loc_list (list);
30949 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30950 if (*slot == NULL)
30952 *slot = list;
30953 if (loc_list_has_views (list))
30954 gcc_assert (list->vl_symbol);
30955 else if (list->vl_symbol)
30957 drop_locviews = true;
30958 list->vl_symbol = NULL;
30961 else
30963 if (list->vl_symbol && !(*slot)->vl_symbol)
30964 drop_locviews = true;
30965 a->dw_attr_val.v.val_loc_list = *slot;
30968 else if (AT_class (a) == dw_val_class_view_list)
30970 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30971 has_locviews = true;
30975 if (drop_locviews && has_locviews)
30976 remove_AT (die, DW_AT_GNU_locviews);
30978 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30982 /* Recursively assign each location list a unique index into the debug_addr
30983 section. */
30985 static void
30986 index_location_lists (dw_die_ref die)
30988 dw_die_ref c;
30989 dw_attr_node *a;
30990 unsigned ix;
30992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30993 if (AT_class (a) == dw_val_class_loc_list)
30995 dw_loc_list_ref list = AT_loc_list (a);
30996 dw_loc_list_ref curr;
30997 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30999 /* Don't index an entry that has already been indexed
31000 or won't be output. Make sure skip_loc_list_entry doesn't
31001 call size_of_locs, because that might cause circular dependency,
31002 index_location_lists requiring address table indexes to be
31003 computed, but adding new indexes through add_addr_table_entry
31004 and address table index computation requiring no new additions
31005 to the hash table. In the rare case of DWARF[234] >= 64KB
31006 location expression, we'll just waste unused address table entry
31007 for it. */
31008 if (curr->begin_entry != NULL
31009 || skip_loc_list_entry (curr))
31010 continue;
31012 curr->begin_entry
31013 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31017 FOR_EACH_CHILD (die, c, index_location_lists (c));
31020 /* Optimize location lists referenced from DIE
31021 children and share them whenever possible. */
31023 static void
31024 optimize_location_lists (dw_die_ref die)
31026 loc_list_hash_type htab (500);
31027 optimize_location_lists_1 (die, &htab);
31030 /* Traverse the limbo die list, and add parent/child links. The only
31031 dies without parents that should be here are concrete instances of
31032 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31033 For concrete instances, we can get the parent die from the abstract
31034 instance. */
31036 static void
31037 flush_limbo_die_list (void)
31039 limbo_die_node *node;
31041 /* get_context_die calls force_decl_die, which can put new DIEs on the
31042 limbo list in LTO mode when nested functions are put in a different
31043 partition than that of their parent function. */
31044 while ((node = limbo_die_list))
31046 dw_die_ref die = node->die;
31047 limbo_die_list = node->next;
31049 if (die->die_parent == NULL)
31051 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31053 if (origin && origin->die_parent)
31054 add_child_die (origin->die_parent, die);
31055 else if (is_cu_die (die))
31057 else if (seen_error ())
31058 /* It's OK to be confused by errors in the input. */
31059 add_child_die (comp_unit_die (), die);
31060 else
31062 /* In certain situations, the lexical block containing a
31063 nested function can be optimized away, which results
31064 in the nested function die being orphaned. Likewise
31065 with the return type of that nested function. Force
31066 this to be a child of the containing function.
31068 It may happen that even the containing function got fully
31069 inlined and optimized out. In that case we are lost and
31070 assign the empty child. This should not be big issue as
31071 the function is likely unreachable too. */
31072 gcc_assert (node->created_for);
31074 if (DECL_P (node->created_for))
31075 origin = get_context_die (DECL_CONTEXT (node->created_for));
31076 else if (TYPE_P (node->created_for))
31077 origin = scope_die_for (node->created_for, comp_unit_die ());
31078 else
31079 origin = comp_unit_die ();
31081 add_child_die (origin, die);
31087 /* Reset DIEs so we can output them again. */
31089 static void
31090 reset_dies (dw_die_ref die)
31092 dw_die_ref c;
31094 /* Remove stuff we re-generate. */
31095 die->die_mark = 0;
31096 die->die_offset = 0;
31097 die->die_abbrev = 0;
31098 remove_AT (die, DW_AT_sibling);
31100 FOR_EACH_CHILD (die, c, reset_dies (c));
31103 /* Output stuff that dwarf requires at the end of every file,
31104 and generate the DWARF-2 debugging info. */
31106 static void
31107 dwarf2out_finish (const char *filename)
31109 comdat_type_node *ctnode;
31110 dw_die_ref main_comp_unit_die;
31111 unsigned char checksum[16];
31112 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31114 /* Flush out any latecomers to the limbo party. */
31115 flush_limbo_die_list ();
31117 if (inline_entry_data_table)
31118 gcc_assert (inline_entry_data_table->elements () == 0);
31120 if (flag_checking)
31122 verify_die (comp_unit_die ());
31123 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31124 verify_die (node->die);
31127 /* We shouldn't have any symbols with delayed asm names for
31128 DIEs generated after early finish. */
31129 gcc_assert (deferred_asm_name == NULL);
31131 gen_remaining_tmpl_value_param_die_attribute ();
31133 if (flag_generate_lto || flag_generate_offload)
31135 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31137 /* Prune stuff so that dwarf2out_finish runs successfully
31138 for the fat part of the object. */
31139 reset_dies (comp_unit_die ());
31140 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31141 reset_dies (node->die);
31143 hash_table<comdat_type_hasher> comdat_type_table (100);
31144 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31146 comdat_type_node **slot
31147 = comdat_type_table.find_slot (ctnode, INSERT);
31149 /* Don't reset types twice. */
31150 if (*slot != HTAB_EMPTY_ENTRY)
31151 continue;
31153 /* Remove the pointer to the line table. */
31154 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31156 if (debug_info_level >= DINFO_LEVEL_TERSE)
31157 reset_dies (ctnode->root_die);
31159 *slot = ctnode;
31162 /* Reset die CU symbol so we don't output it twice. */
31163 comp_unit_die ()->die_id.die_symbol = NULL;
31165 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31166 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31167 if (have_macinfo)
31168 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31170 /* Remove indirect string decisions. */
31171 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31172 if (debug_line_str_hash)
31174 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31175 debug_line_str_hash = NULL;
31179 #if ENABLE_ASSERT_CHECKING
31181 dw_die_ref die = comp_unit_die (), c;
31182 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31184 #endif
31185 resolve_addr (comp_unit_die ());
31186 move_marked_base_types ();
31188 if (dump_file)
31190 fprintf (dump_file, "DWARF for %s\n", filename);
31191 print_die (comp_unit_die (), dump_file);
31194 /* Initialize sections and labels used for actual assembler output. */
31195 unsigned generation = init_sections_and_labels (false);
31197 /* Traverse the DIE's and add sibling attributes to those DIE's that
31198 have children. */
31199 add_sibling_attributes (comp_unit_die ());
31200 limbo_die_node *node;
31201 for (node = cu_die_list; node; node = node->next)
31202 add_sibling_attributes (node->die);
31203 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31204 add_sibling_attributes (ctnode->root_die);
31206 /* When splitting DWARF info, we put some attributes in the
31207 skeleton compile_unit DIE that remains in the .o, while
31208 most attributes go in the DWO compile_unit_die. */
31209 if (dwarf_split_debug_info)
31211 limbo_die_node *cu;
31212 main_comp_unit_die = gen_compile_unit_die (NULL);
31213 if (dwarf_version >= 5)
31214 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31215 cu = limbo_die_list;
31216 gcc_assert (cu->die == main_comp_unit_die);
31217 limbo_die_list = limbo_die_list->next;
31218 cu->next = cu_die_list;
31219 cu_die_list = cu;
31221 else
31222 main_comp_unit_die = comp_unit_die ();
31224 /* Output a terminator label for the .text section. */
31225 switch_to_section (text_section);
31226 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31227 if (cold_text_section)
31229 switch_to_section (cold_text_section);
31230 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31233 /* We can only use the low/high_pc attributes if all of the code was
31234 in .text. */
31235 if (!have_multiple_function_sections
31236 || (dwarf_version < 3 && dwarf_strict))
31238 /* Don't add if the CU has no associated code. */
31239 if (text_section_used)
31240 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31241 text_end_label, true);
31243 else
31245 unsigned fde_idx;
31246 dw_fde_ref fde;
31247 bool range_list_added = false;
31249 if (text_section_used)
31250 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31251 text_end_label, &range_list_added, true);
31252 if (cold_text_section_used)
31253 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31254 cold_end_label, &range_list_added, true);
31256 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31258 if (DECL_IGNORED_P (fde->decl))
31259 continue;
31260 if (!fde->in_std_section)
31261 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31262 fde->dw_fde_end, &range_list_added,
31263 true);
31264 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31265 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31266 fde->dw_fde_second_end, &range_list_added,
31267 true);
31270 if (range_list_added)
31272 /* We need to give .debug_loc and .debug_ranges an appropriate
31273 "base address". Use zero so that these addresses become
31274 absolute. Historically, we've emitted the unexpected
31275 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31276 Emit both to give time for other tools to adapt. */
31277 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31278 if (! dwarf_strict && dwarf_version < 4)
31279 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31281 add_ranges (NULL);
31285 /* AIX Assembler inserts the length, so adjust the reference to match the
31286 offset expected by debuggers. */
31287 strcpy (dl_section_ref, debug_line_section_label);
31288 if (XCOFF_DEBUGGING_INFO)
31289 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31291 if (debug_info_level >= DINFO_LEVEL_TERSE)
31292 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31293 dl_section_ref);
31295 if (have_macinfo)
31296 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31297 macinfo_section_label);
31299 if (dwarf_split_debug_info)
31301 if (have_location_lists)
31303 /* Since we generate the loclists in the split DWARF .dwo
31304 file itself, we don't need to generate a loclists_base
31305 attribute for the split compile unit DIE. That attribute
31306 (and using relocatable sec_offset FORMs) isn't allowed
31307 for a split compile unit. Only if the .debug_loclists
31308 section was in the main file, would we need to generate a
31309 loclists_base attribute here (for the full or skeleton
31310 unit DIE). */
31312 /* optimize_location_lists calculates the size of the lists,
31313 so index them first, and assign indices to the entries.
31314 Although optimize_location_lists will remove entries from
31315 the table, it only does so for duplicates, and therefore
31316 only reduces ref_counts to 1. */
31317 index_location_lists (comp_unit_die ());
31320 if (addr_index_table != NULL)
31322 unsigned int index = 0;
31323 addr_index_table
31324 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31325 (&index);
31329 loc_list_idx = 0;
31330 if (have_location_lists)
31332 optimize_location_lists (comp_unit_die ());
31333 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31334 if (dwarf_version >= 5 && dwarf_split_debug_info)
31335 assign_location_list_indexes (comp_unit_die ());
31338 save_macinfo_strings ();
31340 if (dwarf_split_debug_info)
31342 unsigned int index = 0;
31344 /* Add attributes common to skeleton compile_units and
31345 type_units. Because these attributes include strings, it
31346 must be done before freezing the string table. Top-level
31347 skeleton die attrs are added when the skeleton type unit is
31348 created, so ensure it is created by this point. */
31349 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31350 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31353 /* Output all of the compilation units. We put the main one last so that
31354 the offsets are available to output_pubnames. */
31355 for (node = cu_die_list; node; node = node->next)
31356 output_comp_unit (node->die, 0, NULL);
31358 hash_table<comdat_type_hasher> comdat_type_table (100);
31359 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31361 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31363 /* Don't output duplicate types. */
31364 if (*slot != HTAB_EMPTY_ENTRY)
31365 continue;
31367 /* Add a pointer to the line table for the main compilation unit
31368 so that the debugger can make sense of DW_AT_decl_file
31369 attributes. */
31370 if (debug_info_level >= DINFO_LEVEL_TERSE)
31371 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31372 (!dwarf_split_debug_info
31373 ? dl_section_ref
31374 : debug_skeleton_line_section_label));
31376 output_comdat_type_unit (ctnode);
31377 *slot = ctnode;
31380 if (dwarf_split_debug_info)
31382 int mark;
31383 struct md5_ctx ctx;
31385 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31386 index_rnglists ();
31388 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31389 md5_init_ctx (&ctx);
31390 mark = 0;
31391 die_checksum (comp_unit_die (), &ctx, &mark);
31392 unmark_all_dies (comp_unit_die ());
31393 md5_finish_ctx (&ctx, checksum);
31395 if (dwarf_version < 5)
31397 /* Use the first 8 bytes of the checksum as the dwo_id,
31398 and add it to both comp-unit DIEs. */
31399 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31400 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31403 /* Add the base offset of the ranges table to the skeleton
31404 comp-unit DIE. */
31405 if (!vec_safe_is_empty (ranges_table))
31407 if (dwarf_version >= 5)
31408 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31409 ranges_base_label);
31410 else
31411 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31412 ranges_section_label);
31415 switch_to_section (debug_addr_section);
31416 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
31417 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
31418 before DWARF5, didn't have a header for .debug_addr units.
31419 DWARF5 specifies a small header when address tables are used. */
31420 if (dwarf_version >= 5)
31422 unsigned int last_idx = 0;
31423 unsigned long addrs_length;
31425 addr_index_table->traverse_noresize
31426 <unsigned int *, count_index_addrs> (&last_idx);
31427 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
31429 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31430 dw2_asm_output_data (4, 0xffffffff,
31431 "Escape value for 64-bit DWARF extension");
31432 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
31433 "Length of Address Unit");
31434 dw2_asm_output_data (2, 5, "DWARF addr version");
31435 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
31436 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
31438 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31439 output_addr_table ();
31442 /* Output the main compilation unit if non-empty or if .debug_macinfo
31443 or .debug_macro will be emitted. */
31444 output_comp_unit (comp_unit_die (), have_macinfo,
31445 dwarf_split_debug_info ? checksum : NULL);
31447 if (dwarf_split_debug_info && info_section_emitted)
31448 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31450 /* Output the abbreviation table. */
31451 if (vec_safe_length (abbrev_die_table) != 1)
31453 switch_to_section (debug_abbrev_section);
31454 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31455 output_abbrev_section ();
31458 /* Output location list section if necessary. */
31459 if (have_location_lists)
31461 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31462 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31463 /* Output the location lists info. */
31464 switch_to_section (debug_loc_section);
31465 if (dwarf_version >= 5)
31467 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31468 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31469 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31470 dw2_asm_output_data (4, 0xffffffff,
31471 "Initial length escape value indicating "
31472 "64-bit DWARF extension");
31473 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31474 "Length of Location Lists");
31475 ASM_OUTPUT_LABEL (asm_out_file, l1);
31476 output_dwarf_version ();
31477 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31478 dw2_asm_output_data (1, 0, "Segment Size");
31479 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31480 "Offset Entry Count");
31482 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31483 if (dwarf_version >= 5 && dwarf_split_debug_info)
31485 unsigned int save_loc_list_idx = loc_list_idx;
31486 loc_list_idx = 0;
31487 output_loclists_offsets (comp_unit_die ());
31488 gcc_assert (save_loc_list_idx == loc_list_idx);
31490 output_location_lists (comp_unit_die ());
31491 if (dwarf_version >= 5)
31492 ASM_OUTPUT_LABEL (asm_out_file, l2);
31495 output_pubtables ();
31497 /* Output the address range information if a CU (.debug_info section)
31498 was emitted. We output an empty table even if we had no functions
31499 to put in it. This because the consumer has no way to tell the
31500 difference between an empty table that we omitted and failure to
31501 generate a table that would have contained data. */
31502 if (info_section_emitted)
31504 switch_to_section (debug_aranges_section);
31505 output_aranges ();
31508 /* Output ranges section if necessary. */
31509 if (!vec_safe_is_empty (ranges_table))
31511 if (dwarf_version >= 5)
31512 output_rnglists (generation);
31513 else
31514 output_ranges ();
31517 /* Have to end the macro section. */
31518 if (have_macinfo)
31520 switch_to_section (debug_macinfo_section);
31521 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31522 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31523 : debug_skeleton_line_section_label, false);
31524 dw2_asm_output_data (1, 0, "End compilation unit");
31527 /* Output the source line correspondence table. We must do this
31528 even if there is no line information. Otherwise, on an empty
31529 translation unit, we will generate a present, but empty,
31530 .debug_info section. IRIX 6.5 `nm' will then complain when
31531 examining the file. This is done late so that any filenames
31532 used by the debug_info section are marked as 'used'. */
31533 switch_to_section (debug_line_section);
31534 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31535 if (! output_asm_line_debug_info ())
31536 output_line_info (false);
31538 if (dwarf_split_debug_info && info_section_emitted)
31540 switch_to_section (debug_skeleton_line_section);
31541 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31542 output_line_info (true);
31545 /* If we emitted any indirect strings, output the string table too. */
31546 if (debug_str_hash || skeleton_debug_str_hash)
31547 output_indirect_strings ();
31548 if (debug_line_str_hash)
31550 switch_to_section (debug_line_str_section);
31551 const enum dwarf_form form = DW_FORM_line_strp;
31552 debug_line_str_hash->traverse<enum dwarf_form,
31553 output_indirect_string> (form);
31556 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31557 symview_upper_bound = 0;
31558 if (zero_view_p)
31559 bitmap_clear (zero_view_p);
31562 /* Returns a hash value for X (which really is a variable_value_struct). */
31564 inline hashval_t
31565 variable_value_hasher::hash (variable_value_struct *x)
31567 return (hashval_t) x->decl_id;
31570 /* Return nonzero if decl_id of variable_value_struct X is the same as
31571 UID of decl Y. */
31573 inline bool
31574 variable_value_hasher::equal (variable_value_struct *x, tree y)
31576 return x->decl_id == DECL_UID (y);
31579 /* Helper function for resolve_variable_value, handle
31580 DW_OP_GNU_variable_value in one location expression.
31581 Return true if exprloc has been changed into loclist. */
31583 static bool
31584 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31586 dw_loc_descr_ref next;
31587 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31589 next = loc->dw_loc_next;
31590 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31591 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31592 continue;
31594 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31595 if (DECL_CONTEXT (decl) != current_function_decl)
31596 continue;
31598 dw_die_ref ref = lookup_decl_die (decl);
31599 if (ref)
31601 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31602 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31603 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31604 continue;
31606 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31607 if (l == NULL)
31608 continue;
31609 if (l->dw_loc_next)
31611 if (AT_class (a) != dw_val_class_loc)
31612 continue;
31613 switch (a->dw_attr)
31615 /* Following attributes allow both exprloc and loclist
31616 classes, so we can change them into a loclist. */
31617 case DW_AT_location:
31618 case DW_AT_string_length:
31619 case DW_AT_return_addr:
31620 case DW_AT_data_member_location:
31621 case DW_AT_frame_base:
31622 case DW_AT_segment:
31623 case DW_AT_static_link:
31624 case DW_AT_use_location:
31625 case DW_AT_vtable_elem_location:
31626 if (prev)
31628 prev->dw_loc_next = NULL;
31629 prepend_loc_descr_to_each (l, AT_loc (a));
31631 if (next)
31632 add_loc_descr_to_each (l, next);
31633 a->dw_attr_val.val_class = dw_val_class_loc_list;
31634 a->dw_attr_val.val_entry = NULL;
31635 a->dw_attr_val.v.val_loc_list = l;
31636 have_location_lists = true;
31637 return true;
31638 /* Following attributes allow both exprloc and reference,
31639 so if the whole expression is DW_OP_GNU_variable_value alone
31640 we could transform it into reference. */
31641 case DW_AT_byte_size:
31642 case DW_AT_bit_size:
31643 case DW_AT_lower_bound:
31644 case DW_AT_upper_bound:
31645 case DW_AT_bit_stride:
31646 case DW_AT_count:
31647 case DW_AT_allocated:
31648 case DW_AT_associated:
31649 case DW_AT_byte_stride:
31650 if (prev == NULL && next == NULL)
31651 break;
31652 /* FALLTHRU */
31653 default:
31654 if (dwarf_strict)
31655 continue;
31656 break;
31658 /* Create DW_TAG_variable that we can refer to. */
31659 gen_decl_die (decl, NULL_TREE, NULL,
31660 lookup_decl_die (current_function_decl));
31661 ref = lookup_decl_die (decl);
31662 if (ref)
31664 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31665 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31666 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31668 continue;
31670 if (prev)
31672 prev->dw_loc_next = l->expr;
31673 add_loc_descr (&prev->dw_loc_next, next);
31674 free_loc_descr (loc, NULL);
31675 next = prev->dw_loc_next;
31677 else
31679 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31680 add_loc_descr (&loc, next);
31681 next = loc;
31683 loc = prev;
31685 return false;
31688 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31690 static void
31691 resolve_variable_value (dw_die_ref die)
31693 dw_attr_node *a;
31694 dw_loc_list_ref loc;
31695 unsigned ix;
31697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31698 switch (AT_class (a))
31700 case dw_val_class_loc:
31701 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31702 break;
31703 /* FALLTHRU */
31704 case dw_val_class_loc_list:
31705 loc = AT_loc_list (a);
31706 gcc_assert (loc);
31707 for (; loc; loc = loc->dw_loc_next)
31708 resolve_variable_value_in_expr (a, loc->expr);
31709 break;
31710 default:
31711 break;
31715 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31716 temporaries in the current function. */
31718 static void
31719 resolve_variable_values (void)
31721 if (!variable_value_hash || !current_function_decl)
31722 return;
31724 struct variable_value_struct *node
31725 = variable_value_hash->find_with_hash (current_function_decl,
31726 DECL_UID (current_function_decl));
31728 if (node == NULL)
31729 return;
31731 unsigned int i;
31732 dw_die_ref die;
31733 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31734 resolve_variable_value (die);
31737 /* Helper function for note_variable_value, handle one location
31738 expression. */
31740 static void
31741 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31743 for (; loc; loc = loc->dw_loc_next)
31744 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31745 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31747 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31748 dw_die_ref ref = lookup_decl_die (decl);
31749 if (! ref && (flag_generate_lto || flag_generate_offload))
31751 /* ??? This is somewhat a hack because we do not create DIEs
31752 for variables not in BLOCK trees early but when generating
31753 early LTO output we need the dw_val_class_decl_ref to be
31754 fully resolved. For fat LTO objects we'd also like to
31755 undo this after LTO dwarf output. */
31756 gcc_assert (DECL_CONTEXT (decl));
31757 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31758 gcc_assert (ctx != NULL);
31759 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31760 ref = lookup_decl_die (decl);
31761 gcc_assert (ref != NULL);
31763 if (ref)
31765 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31766 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31767 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31768 continue;
31770 if (VAR_P (decl)
31771 && DECL_CONTEXT (decl)
31772 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31773 && lookup_decl_die (DECL_CONTEXT (decl)))
31775 if (!variable_value_hash)
31776 variable_value_hash
31777 = hash_table<variable_value_hasher>::create_ggc (10);
31779 tree fndecl = DECL_CONTEXT (decl);
31780 struct variable_value_struct *node;
31781 struct variable_value_struct **slot
31782 = variable_value_hash->find_slot_with_hash (fndecl,
31783 DECL_UID (fndecl),
31784 INSERT);
31785 if (*slot == NULL)
31787 node = ggc_cleared_alloc<variable_value_struct> ();
31788 node->decl_id = DECL_UID (fndecl);
31789 *slot = node;
31791 else
31792 node = *slot;
31794 vec_safe_push (node->dies, die);
31799 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31800 with dw_val_class_decl_ref operand. */
31802 static void
31803 note_variable_value (dw_die_ref die)
31805 dw_die_ref c;
31806 dw_attr_node *a;
31807 dw_loc_list_ref loc;
31808 unsigned ix;
31810 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31811 switch (AT_class (a))
31813 case dw_val_class_loc_list:
31814 loc = AT_loc_list (a);
31815 gcc_assert (loc);
31816 if (!loc->noted_variable_value)
31818 loc->noted_variable_value = 1;
31819 for (; loc; loc = loc->dw_loc_next)
31820 note_variable_value_in_expr (die, loc->expr);
31822 break;
31823 case dw_val_class_loc:
31824 note_variable_value_in_expr (die, AT_loc (a));
31825 break;
31826 default:
31827 break;
31830 /* Mark children. */
31831 FOR_EACH_CHILD (die, c, note_variable_value (c));
31834 /* Perform any cleanups needed after the early debug generation pass
31835 has run. */
31837 static void
31838 dwarf2out_early_finish (const char *filename)
31840 set_early_dwarf s;
31841 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31843 /* PCH might result in DW_AT_producer string being restored from the
31844 header compilation, so always fill it with empty string initially
31845 and overwrite only here. */
31846 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31847 producer_string = gen_producer_string ();
31848 producer->dw_attr_val.v.val_str->refcount--;
31849 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31851 /* Add the name for the main input file now. We delayed this from
31852 dwarf2out_init to avoid complications with PCH. */
31853 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31854 add_comp_dir_attribute (comp_unit_die ());
31856 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31857 DW_AT_comp_dir into .debug_line_str section. */
31858 if (!output_asm_line_debug_info ()
31859 && dwarf_version >= 5
31860 && DWARF5_USE_DEBUG_LINE_STR)
31862 for (int i = 0; i < 2; i++)
31864 dw_attr_node *a = get_AT (comp_unit_die (),
31865 i ? DW_AT_comp_dir : DW_AT_name);
31866 if (a == NULL
31867 || AT_class (a) != dw_val_class_str
31868 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31869 continue;
31871 if (! debug_line_str_hash)
31872 debug_line_str_hash
31873 = hash_table<indirect_string_hasher>::create_ggc (10);
31875 struct indirect_string_node *node
31876 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31877 set_indirect_string (node);
31878 node->form = DW_FORM_line_strp;
31879 a->dw_attr_val.v.val_str->refcount--;
31880 a->dw_attr_val.v.val_str = node;
31884 /* With LTO early dwarf was really finished at compile-time, so make
31885 sure to adjust the phase after annotating the LTRANS CU DIE. */
31886 if (in_lto_p)
31888 /* Force DW_TAG_imported_unit to be created now, otherwise
31889 we might end up without it or ordered after DW_TAG_inlined_subroutine
31890 referencing DIEs from it. */
31891 if (! flag_wpa && flag_incremental_link != INCREMENTAL_LINK_LTO)
31893 unsigned i;
31894 tree tu;
31895 if (external_die_map)
31896 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, tu)
31897 if (sym_off_pair *desc = external_die_map->get (tu))
31899 dw_die_ref import = new_die (DW_TAG_imported_unit,
31900 comp_unit_die (), NULL_TREE);
31901 add_AT_external_die_ref (import, DW_AT_import,
31902 desc->sym, desc->off);
31906 early_dwarf_finished = true;
31907 if (dump_file)
31909 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
31910 print_die (comp_unit_die (), dump_file);
31912 return;
31915 /* Walk through the list of incomplete types again, trying once more to
31916 emit full debugging info for them. */
31917 retry_incomplete_types ();
31919 /* The point here is to flush out the limbo list so that it is empty
31920 and we don't need to stream it for LTO. */
31921 flush_limbo_die_list ();
31923 gen_scheduled_generic_parms_dies ();
31924 gen_remaining_tmpl_value_param_die_attribute ();
31926 /* Add DW_AT_linkage_name for all deferred DIEs. */
31927 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31929 tree decl = node->created_for;
31930 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31931 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31932 ended up in deferred_asm_name before we knew it was
31933 constant and never written to disk. */
31934 && DECL_ASSEMBLER_NAME (decl))
31936 add_linkage_attr (node->die, decl);
31937 move_linkage_attr (node->die);
31940 deferred_asm_name = NULL;
31942 if (flag_eliminate_unused_debug_types)
31943 prune_unused_types ();
31945 /* Generate separate COMDAT sections for type DIEs. */
31946 if (use_debug_types)
31948 break_out_comdat_types (comp_unit_die ());
31950 /* Each new type_unit DIE was added to the limbo die list when created.
31951 Since these have all been added to comdat_type_list, clear the
31952 limbo die list. */
31953 limbo_die_list = NULL;
31955 /* For each new comdat type unit, copy declarations for incomplete
31956 types to make the new unit self-contained (i.e., no direct
31957 references to the main compile unit). */
31958 for (comdat_type_node *ctnode = comdat_type_list;
31959 ctnode != NULL; ctnode = ctnode->next)
31960 copy_decls_for_unworthy_types (ctnode->root_die);
31961 copy_decls_for_unworthy_types (comp_unit_die ());
31963 /* In the process of copying declarations from one unit to another,
31964 we may have left some declarations behind that are no longer
31965 referenced. Prune them. */
31966 prune_unused_types ();
31969 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31970 with dw_val_class_decl_ref operand. */
31971 note_variable_value (comp_unit_die ());
31972 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31973 note_variable_value (node->die);
31974 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31975 ctnode = ctnode->next)
31976 note_variable_value (ctnode->root_die);
31977 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31978 note_variable_value (node->die);
31980 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31981 both the main_cu and all skeleton TUs. Making this call unconditional
31982 would end up either adding a second copy of the AT_pubnames attribute, or
31983 requiring a special case in add_top_level_skeleton_die_attrs. */
31984 if (!dwarf_split_debug_info)
31985 add_AT_pubnames (comp_unit_die ());
31987 /* The early debug phase is now finished. */
31988 early_dwarf_finished = true;
31989 if (dump_file)
31991 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
31992 print_die (comp_unit_die (), dump_file);
31995 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31996 if ((!flag_generate_lto && !flag_generate_offload)
31997 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
31998 copy_lto_debug_sections operation of the simple object support in
31999 libiberty is not implemented for them yet. */
32000 || TARGET_PECOFF || TARGET_COFF)
32001 return;
32003 /* Now as we are going to output for LTO initialize sections and labels
32004 to the LTO variants. We don't need a random-seed postfix as other
32005 LTO sections as linking the LTO debug sections into one in a partial
32006 link is fine. */
32007 init_sections_and_labels (true);
32009 /* The output below is modeled after dwarf2out_finish with all
32010 location related output removed and some LTO specific changes.
32011 Some refactoring might make both smaller and easier to match up. */
32013 /* Traverse the DIE's and add add sibling attributes to those DIE's
32014 that have children. */
32015 add_sibling_attributes (comp_unit_die ());
32016 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32017 add_sibling_attributes (node->die);
32018 for (comdat_type_node *ctnode = comdat_type_list;
32019 ctnode != NULL; ctnode = ctnode->next)
32020 add_sibling_attributes (ctnode->root_die);
32022 /* AIX Assembler inserts the length, so adjust the reference to match the
32023 offset expected by debuggers. */
32024 strcpy (dl_section_ref, debug_line_section_label);
32025 if (XCOFF_DEBUGGING_INFO)
32026 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32028 if (debug_info_level >= DINFO_LEVEL_TERSE)
32029 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32031 if (have_macinfo)
32032 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32033 macinfo_section_label);
32035 save_macinfo_strings ();
32037 if (dwarf_split_debug_info)
32039 unsigned int index = 0;
32040 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32043 /* Output all of the compilation units. We put the main one last so that
32044 the offsets are available to output_pubnames. */
32045 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32046 output_comp_unit (node->die, 0, NULL);
32048 hash_table<comdat_type_hasher> comdat_type_table (100);
32049 for (comdat_type_node *ctnode = comdat_type_list;
32050 ctnode != NULL; ctnode = ctnode->next)
32052 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32054 /* Don't output duplicate types. */
32055 if (*slot != HTAB_EMPTY_ENTRY)
32056 continue;
32058 /* Add a pointer to the line table for the main compilation unit
32059 so that the debugger can make sense of DW_AT_decl_file
32060 attributes. */
32061 if (debug_info_level >= DINFO_LEVEL_TERSE)
32062 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32063 (!dwarf_split_debug_info
32064 ? debug_line_section_label
32065 : debug_skeleton_line_section_label));
32067 output_comdat_type_unit (ctnode);
32068 *slot = ctnode;
32071 /* Stick a unique symbol to the main debuginfo section. */
32072 compute_comp_unit_symbol (comp_unit_die ());
32074 /* Output the main compilation unit. We always need it if only for
32075 the CU symbol. */
32076 output_comp_unit (comp_unit_die (), true, NULL);
32078 /* Output the abbreviation table. */
32079 if (vec_safe_length (abbrev_die_table) != 1)
32081 switch_to_section (debug_abbrev_section);
32082 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32083 output_abbrev_section ();
32086 /* Have to end the macro section. */
32087 if (have_macinfo)
32089 /* We have to save macinfo state if we need to output it again
32090 for the FAT part of the object. */
32091 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32092 if (flag_fat_lto_objects)
32093 macinfo_table = macinfo_table->copy ();
32095 switch_to_section (debug_macinfo_section);
32096 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32097 output_macinfo (debug_line_section_label, true);
32098 dw2_asm_output_data (1, 0, "End compilation unit");
32100 if (flag_fat_lto_objects)
32102 vec_free (macinfo_table);
32103 macinfo_table = saved_macinfo_table;
32107 /* Emit a skeleton debug_line section. */
32108 switch_to_section (debug_line_section);
32109 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32110 output_line_info (true);
32112 /* If we emitted any indirect strings, output the string table too. */
32113 if (debug_str_hash || skeleton_debug_str_hash)
32114 output_indirect_strings ();
32115 if (debug_line_str_hash)
32117 switch_to_section (debug_line_str_section);
32118 const enum dwarf_form form = DW_FORM_line_strp;
32119 debug_line_str_hash->traverse<enum dwarf_form,
32120 output_indirect_string> (form);
32123 /* Switch back to the text section. */
32124 switch_to_section (text_section);
32127 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32128 within the same process. For use by toplev::finalize. */
32130 void
32131 dwarf2out_c_finalize (void)
32133 last_var_location_insn = NULL;
32134 cached_next_real_insn = NULL;
32135 used_rtx_array = NULL;
32136 incomplete_types = NULL;
32137 debug_info_section = NULL;
32138 debug_skeleton_info_section = NULL;
32139 debug_abbrev_section = NULL;
32140 debug_skeleton_abbrev_section = NULL;
32141 debug_aranges_section = NULL;
32142 debug_addr_section = NULL;
32143 debug_macinfo_section = NULL;
32144 debug_line_section = NULL;
32145 debug_skeleton_line_section = NULL;
32146 debug_loc_section = NULL;
32147 debug_pubnames_section = NULL;
32148 debug_pubtypes_section = NULL;
32149 debug_str_section = NULL;
32150 debug_line_str_section = NULL;
32151 debug_str_dwo_section = NULL;
32152 debug_str_offsets_section = NULL;
32153 debug_ranges_section = NULL;
32154 debug_frame_section = NULL;
32155 fde_vec = NULL;
32156 debug_str_hash = NULL;
32157 debug_line_str_hash = NULL;
32158 skeleton_debug_str_hash = NULL;
32159 dw2_string_counter = 0;
32160 have_multiple_function_sections = false;
32161 text_section_used = false;
32162 cold_text_section_used = false;
32163 cold_text_section = NULL;
32164 current_unit_personality = NULL;
32166 early_dwarf = false;
32167 early_dwarf_finished = false;
32169 next_die_offset = 0;
32170 single_comp_unit_die = NULL;
32171 comdat_type_list = NULL;
32172 limbo_die_list = NULL;
32173 file_table = NULL;
32174 decl_die_table = NULL;
32175 common_block_die_table = NULL;
32176 decl_loc_table = NULL;
32177 call_arg_locations = NULL;
32178 call_arg_loc_last = NULL;
32179 call_site_count = -1;
32180 tail_call_site_count = -1;
32181 cached_dw_loc_list_table = NULL;
32182 abbrev_die_table = NULL;
32183 delete dwarf_proc_stack_usage_map;
32184 dwarf_proc_stack_usage_map = NULL;
32185 line_info_label_num = 0;
32186 cur_line_info_table = NULL;
32187 text_section_line_info = NULL;
32188 cold_text_section_line_info = NULL;
32189 separate_line_info = NULL;
32190 info_section_emitted = false;
32191 pubname_table = NULL;
32192 pubtype_table = NULL;
32193 macinfo_table = NULL;
32194 ranges_table = NULL;
32195 ranges_by_label = NULL;
32196 rnglist_idx = 0;
32197 have_location_lists = false;
32198 loclabel_num = 0;
32199 poc_label_num = 0;
32200 last_emitted_file = NULL;
32201 label_num = 0;
32202 tmpl_value_parm_die_table = NULL;
32203 generic_type_instances = NULL;
32204 frame_pointer_fb_offset = 0;
32205 frame_pointer_fb_offset_valid = false;
32206 base_types.release ();
32207 XDELETEVEC (producer_string);
32208 producer_string = NULL;
32211 #include "gt-dwarf2out.h"