Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / gcc / dwarf2out.c
blob30bbfee90521dfe417f12c3813dcc4022eea3a57
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 ref_p = lookup_external_ref (extern_map, c);
9027 if (ref_p->stub && ref_p->stub != die)
9028 change_AT_die_ref (a, ref_p->stub);
9029 else
9030 /* We aren't changing this reference, so mark it external. */
9031 set_AT_ref_external (a, 1);
9034 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9036 dw_attr_node *die_a, *abbrev_a;
9037 unsigned ix;
9038 bool ok = true;
9040 if (abbrev_id == 0)
9041 continue;
9042 if (abbrev->die_tag != die->die_tag)
9043 continue;
9044 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9045 continue;
9047 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9048 continue;
9050 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9052 abbrev_a = &(*abbrev->die_attr)[ix];
9053 if ((abbrev_a->dw_attr != die_a->dw_attr)
9054 || (value_format (abbrev_a) != value_format (die_a)))
9056 ok = false;
9057 break;
9060 if (ok)
9061 break;
9064 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9066 vec_safe_push (abbrev_die_table, die);
9067 if (abbrev_opt_start)
9068 abbrev_usage_count.safe_push (0);
9070 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9072 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9073 sorted_abbrev_dies.safe_push (die);
9076 die->die_abbrev = abbrev_id;
9077 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9080 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9081 by die_abbrev's usage count, from the most commonly used
9082 abbreviation to the least. */
9084 static int
9085 die_abbrev_cmp (const void *p1, const void *p2)
9087 dw_die_ref die1 = *(const dw_die_ref *) p1;
9088 dw_die_ref die2 = *(const dw_die_ref *) p2;
9090 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9091 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9093 if (die1->die_abbrev >= abbrev_opt_base_type_end
9094 && die2->die_abbrev >= abbrev_opt_base_type_end)
9096 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9097 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9098 return -1;
9099 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9100 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9101 return 1;
9104 /* Stabilize the sort. */
9105 if (die1->die_abbrev < die2->die_abbrev)
9106 return -1;
9107 if (die1->die_abbrev > die2->die_abbrev)
9108 return 1;
9110 return 0;
9113 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9114 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9115 into dw_val_class_const_implicit or
9116 dw_val_class_unsigned_const_implicit. */
9118 static void
9119 optimize_implicit_const (unsigned int first_id, unsigned int end,
9120 vec<bool> &implicit_consts)
9122 /* It never makes sense if there is just one DIE using the abbreviation. */
9123 if (end < first_id + 2)
9124 return;
9126 dw_attr_node *a;
9127 unsigned ix, i;
9128 dw_die_ref die = sorted_abbrev_dies[first_id];
9129 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9130 if (implicit_consts[ix])
9132 enum dw_val_class new_class = dw_val_class_none;
9133 switch (AT_class (a))
9135 case dw_val_class_unsigned_const:
9136 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9137 continue;
9139 /* The .debug_abbrev section will grow by
9140 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9141 in all the DIEs using that abbreviation. */
9142 if (constant_size (AT_unsigned (a)) * (end - first_id)
9143 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9144 continue;
9146 new_class = dw_val_class_unsigned_const_implicit;
9147 break;
9149 case dw_val_class_const:
9150 new_class = dw_val_class_const_implicit;
9151 break;
9153 case dw_val_class_file:
9154 new_class = dw_val_class_file_implicit;
9155 break;
9157 default:
9158 continue;
9160 for (i = first_id; i < end; i++)
9161 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9162 = new_class;
9166 /* Attempt to optimize abbreviation table from abbrev_opt_start
9167 abbreviation above. */
9169 static void
9170 optimize_abbrev_table (void)
9172 if (abbrev_opt_start
9173 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9174 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9176 auto_vec<bool, 32> implicit_consts;
9177 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9179 unsigned int abbrev_id = abbrev_opt_start - 1;
9180 unsigned int first_id = ~0U;
9181 unsigned int last_abbrev_id = 0;
9182 unsigned int i;
9183 dw_die_ref die;
9184 if (abbrev_opt_base_type_end > abbrev_opt_start)
9185 abbrev_id = abbrev_opt_base_type_end - 1;
9186 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9187 most commonly used abbreviations come first. */
9188 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9190 dw_attr_node *a;
9191 unsigned ix;
9193 /* If calc_base_type_die_sizes has been called, the CU and
9194 base types after it can't be optimized, because we've already
9195 calculated their DIE offsets. We've sorted them first. */
9196 if (die->die_abbrev < abbrev_opt_base_type_end)
9197 continue;
9198 if (die->die_abbrev != last_abbrev_id)
9200 last_abbrev_id = die->die_abbrev;
9201 if (dwarf_version >= 5 && first_id != ~0U)
9202 optimize_implicit_const (first_id, i, implicit_consts);
9203 abbrev_id++;
9204 (*abbrev_die_table)[abbrev_id] = die;
9205 if (dwarf_version >= 5)
9207 first_id = i;
9208 implicit_consts.truncate (0);
9210 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9211 switch (AT_class (a))
9213 case dw_val_class_const:
9214 case dw_val_class_unsigned_const:
9215 case dw_val_class_file:
9216 implicit_consts.safe_push (true);
9217 break;
9218 default:
9219 implicit_consts.safe_push (false);
9220 break;
9224 else if (dwarf_version >= 5)
9226 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9227 if (!implicit_consts[ix])
9228 continue;
9229 else
9231 dw_attr_node *other_a
9232 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9233 if (!dw_val_equal_p (&a->dw_attr_val,
9234 &other_a->dw_attr_val))
9235 implicit_consts[ix] = false;
9238 die->die_abbrev = abbrev_id;
9240 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9241 if (dwarf_version >= 5 && first_id != ~0U)
9242 optimize_implicit_const (first_id, i, implicit_consts);
9245 abbrev_opt_start = 0;
9246 abbrev_opt_base_type_end = 0;
9247 abbrev_usage_count.release ();
9248 sorted_abbrev_dies.release ();
9251 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9253 static int
9254 constant_size (unsigned HOST_WIDE_INT value)
9256 int log;
9258 if (value == 0)
9259 log = 0;
9260 else
9261 log = floor_log2 (value);
9263 log = log / 8;
9264 log = 1 << (floor_log2 (log) + 1);
9266 return log;
9269 /* Return the size of a DIE as it is represented in the
9270 .debug_info section. */
9272 static unsigned long
9273 size_of_die (dw_die_ref die)
9275 unsigned long size = 0;
9276 dw_attr_node *a;
9277 unsigned ix;
9278 enum dwarf_form form;
9280 size += size_of_uleb128 (die->die_abbrev);
9281 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9283 switch (AT_class (a))
9285 case dw_val_class_addr:
9286 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9288 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9289 size += size_of_uleb128 (AT_index (a));
9291 else
9292 size += DWARF2_ADDR_SIZE;
9293 break;
9294 case dw_val_class_offset:
9295 size += DWARF_OFFSET_SIZE;
9296 break;
9297 case dw_val_class_loc:
9299 unsigned long lsize = size_of_locs (AT_loc (a));
9301 /* Block length. */
9302 if (dwarf_version >= 4)
9303 size += size_of_uleb128 (lsize);
9304 else
9305 size += constant_size (lsize);
9306 size += lsize;
9308 break;
9309 case dw_val_class_loc_list:
9310 case dw_val_class_view_list:
9311 if (dwarf_split_debug_info && dwarf_version >= 5)
9313 gcc_assert (AT_loc_list (a)->num_assigned);
9314 size += size_of_uleb128 (AT_loc_list (a)->hash);
9316 else
9317 size += DWARF_OFFSET_SIZE;
9318 break;
9319 case dw_val_class_range_list:
9320 if (value_format (a) == DW_FORM_rnglistx)
9322 gcc_assert (rnglist_idx);
9323 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9324 size += size_of_uleb128 (r->idx);
9326 else
9327 size += DWARF_OFFSET_SIZE;
9328 break;
9329 case dw_val_class_const:
9330 size += size_of_sleb128 (AT_int (a));
9331 break;
9332 case dw_val_class_unsigned_const:
9334 int csize = constant_size (AT_unsigned (a));
9335 if (dwarf_version == 3
9336 && a->dw_attr == DW_AT_data_member_location
9337 && csize >= 4)
9338 size += size_of_uleb128 (AT_unsigned (a));
9339 else
9340 size += csize;
9342 break;
9343 case dw_val_class_symview:
9344 if (symview_upper_bound <= 0xff)
9345 size += 1;
9346 else if (symview_upper_bound <= 0xffff)
9347 size += 2;
9348 else if (symview_upper_bound <= 0xffffffff)
9349 size += 4;
9350 else
9351 size += 8;
9352 break;
9353 case dw_val_class_const_implicit:
9354 case dw_val_class_unsigned_const_implicit:
9355 case dw_val_class_file_implicit:
9356 /* These occupy no size in the DIE, just an extra sleb128 in
9357 .debug_abbrev. */
9358 break;
9359 case dw_val_class_const_double:
9360 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9361 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9362 size++; /* block */
9363 break;
9364 case dw_val_class_wide_int:
9365 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9366 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9367 if (get_full_len (*a->dw_attr_val.v.val_wide)
9368 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9369 size++; /* block */
9370 break;
9371 case dw_val_class_vec:
9372 size += constant_size (a->dw_attr_val.v.val_vec.length
9373 * a->dw_attr_val.v.val_vec.elt_size)
9374 + a->dw_attr_val.v.val_vec.length
9375 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9376 break;
9377 case dw_val_class_flag:
9378 if (dwarf_version >= 4)
9379 /* Currently all add_AT_flag calls pass in 1 as last argument,
9380 so DW_FORM_flag_present can be used. If that ever changes,
9381 we'll need to use DW_FORM_flag and have some optimization
9382 in build_abbrev_table that will change those to
9383 DW_FORM_flag_present if it is set to 1 in all DIEs using
9384 the same abbrev entry. */
9385 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9386 else
9387 size += 1;
9388 break;
9389 case dw_val_class_die_ref:
9390 if (AT_ref_external (a))
9392 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9393 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9394 is sized by target address length, whereas in DWARF3
9395 it's always sized as an offset. */
9396 if (use_debug_types)
9397 size += DWARF_TYPE_SIGNATURE_SIZE;
9398 else if (dwarf_version == 2)
9399 size += DWARF2_ADDR_SIZE;
9400 else
9401 size += DWARF_OFFSET_SIZE;
9403 else
9404 size += DWARF_OFFSET_SIZE;
9405 break;
9406 case dw_val_class_fde_ref:
9407 size += DWARF_OFFSET_SIZE;
9408 break;
9409 case dw_val_class_lbl_id:
9410 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9412 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9413 size += size_of_uleb128 (AT_index (a));
9415 else
9416 size += DWARF2_ADDR_SIZE;
9417 break;
9418 case dw_val_class_lineptr:
9419 case dw_val_class_macptr:
9420 case dw_val_class_loclistsptr:
9421 size += DWARF_OFFSET_SIZE;
9422 break;
9423 case dw_val_class_str:
9424 form = AT_string_form (a);
9425 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9426 size += DWARF_OFFSET_SIZE;
9427 else if (form == dwarf_FORM (DW_FORM_strx))
9428 size += size_of_uleb128 (AT_index (a));
9429 else
9430 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9431 break;
9432 case dw_val_class_file:
9433 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9434 break;
9435 case dw_val_class_data8:
9436 size += 8;
9437 break;
9438 case dw_val_class_vms_delta:
9439 size += DWARF_OFFSET_SIZE;
9440 break;
9441 case dw_val_class_high_pc:
9442 size += DWARF2_ADDR_SIZE;
9443 break;
9444 case dw_val_class_discr_value:
9445 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9446 break;
9447 case dw_val_class_discr_list:
9449 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9451 /* This is a block, so we have the block length and then its
9452 data. */
9453 size += constant_size (block_size) + block_size;
9455 break;
9456 default:
9457 gcc_unreachable ();
9461 return size;
9464 /* Size the debugging information associated with a given DIE. Visits the
9465 DIE's children recursively. Updates the global variable next_die_offset, on
9466 each time through. Uses the current value of next_die_offset to update the
9467 die_offset field in each DIE. */
9469 static void
9470 calc_die_sizes (dw_die_ref die)
9472 dw_die_ref c;
9474 gcc_assert (die->die_offset == 0
9475 || (unsigned long int) die->die_offset == next_die_offset);
9476 die->die_offset = next_die_offset;
9477 next_die_offset += size_of_die (die);
9479 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9481 if (die->die_child != NULL)
9482 /* Count the null byte used to terminate sibling lists. */
9483 next_die_offset += 1;
9486 /* Size just the base type children at the start of the CU.
9487 This is needed because build_abbrev needs to size locs
9488 and sizing of type based stack ops needs to know die_offset
9489 values for the base types. */
9491 static void
9492 calc_base_type_die_sizes (void)
9494 unsigned long die_offset = (dwarf_split_debug_info
9495 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9496 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9497 unsigned int i;
9498 dw_die_ref base_type;
9499 #if ENABLE_ASSERT_CHECKING
9500 dw_die_ref prev = comp_unit_die ()->die_child;
9501 #endif
9503 die_offset += size_of_die (comp_unit_die ());
9504 for (i = 0; base_types.iterate (i, &base_type); i++)
9506 #if ENABLE_ASSERT_CHECKING
9507 gcc_assert (base_type->die_offset == 0
9508 && prev->die_sib == base_type
9509 && base_type->die_child == NULL
9510 && base_type->die_abbrev);
9511 prev = base_type;
9512 #endif
9513 if (abbrev_opt_start
9514 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9515 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9516 base_type->die_offset = die_offset;
9517 die_offset += size_of_die (base_type);
9521 /* Set the marks for a die and its children. We do this so
9522 that we know whether or not a reference needs to use FORM_ref_addr; only
9523 DIEs in the same CU will be marked. We used to clear out the offset
9524 and use that as the flag, but ran into ordering problems. */
9526 static void
9527 mark_dies (dw_die_ref die)
9529 dw_die_ref c;
9531 gcc_assert (!die->die_mark);
9533 die->die_mark = 1;
9534 FOR_EACH_CHILD (die, c, mark_dies (c));
9537 /* Clear the marks for a die and its children. */
9539 static void
9540 unmark_dies (dw_die_ref die)
9542 dw_die_ref c;
9544 if (! use_debug_types)
9545 gcc_assert (die->die_mark);
9547 die->die_mark = 0;
9548 FOR_EACH_CHILD (die, c, unmark_dies (c));
9551 /* Clear the marks for a die, its children and referred dies. */
9553 static void
9554 unmark_all_dies (dw_die_ref die)
9556 dw_die_ref c;
9557 dw_attr_node *a;
9558 unsigned ix;
9560 if (!die->die_mark)
9561 return;
9562 die->die_mark = 0;
9564 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9566 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9567 if (AT_class (a) == dw_val_class_die_ref)
9568 unmark_all_dies (AT_ref (a));
9571 /* Calculate if the entry should appear in the final output file. It may be
9572 from a pruned a type. */
9574 static bool
9575 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9577 /* By limiting gnu pubnames to definitions only, gold can generate a
9578 gdb index without entries for declarations, which don't include
9579 enough information to be useful. */
9580 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9581 return false;
9583 if (table == pubname_table)
9585 /* Enumerator names are part of the pubname table, but the
9586 parent DW_TAG_enumeration_type die may have been pruned.
9587 Don't output them if that is the case. */
9588 if (p->die->die_tag == DW_TAG_enumerator &&
9589 (p->die->die_parent == NULL
9590 || !p->die->die_parent->die_perennial_p))
9591 return false;
9593 /* Everything else in the pubname table is included. */
9594 return true;
9597 /* The pubtypes table shouldn't include types that have been
9598 pruned. */
9599 return (p->die->die_offset != 0
9600 || !flag_eliminate_unused_debug_types);
9603 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9604 generated for the compilation unit. */
9606 static unsigned long
9607 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9609 unsigned long size;
9610 unsigned i;
9611 pubname_entry *p;
9612 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9614 size = DWARF_PUBNAMES_HEADER_SIZE;
9615 FOR_EACH_VEC_ELT (*names, i, p)
9616 if (include_pubname_in_output (names, p))
9617 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9619 size += DWARF_OFFSET_SIZE;
9620 return size;
9623 /* Return the size of the information in the .debug_aranges section. */
9625 static unsigned long
9626 size_of_aranges (void)
9628 unsigned long size;
9630 size = DWARF_ARANGES_HEADER_SIZE;
9632 /* Count the address/length pair for this compilation unit. */
9633 if (text_section_used)
9634 size += 2 * DWARF2_ADDR_SIZE;
9635 if (cold_text_section_used)
9636 size += 2 * DWARF2_ADDR_SIZE;
9637 if (have_multiple_function_sections)
9639 unsigned fde_idx;
9640 dw_fde_ref fde;
9642 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9644 if (DECL_IGNORED_P (fde->decl))
9645 continue;
9646 if (!fde->in_std_section)
9647 size += 2 * DWARF2_ADDR_SIZE;
9648 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9649 size += 2 * DWARF2_ADDR_SIZE;
9653 /* Count the two zero words used to terminated the address range table. */
9654 size += 2 * DWARF2_ADDR_SIZE;
9655 return size;
9658 /* Select the encoding of an attribute value. */
9660 static enum dwarf_form
9661 value_format (dw_attr_node *a)
9663 switch (AT_class (a))
9665 case dw_val_class_addr:
9666 /* Only very few attributes allow DW_FORM_addr. */
9667 switch (a->dw_attr)
9669 case DW_AT_low_pc:
9670 case DW_AT_high_pc:
9671 case DW_AT_entry_pc:
9672 case DW_AT_trampoline:
9673 return (AT_index (a) == NOT_INDEXED
9674 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9675 default:
9676 break;
9678 switch (DWARF2_ADDR_SIZE)
9680 case 1:
9681 return DW_FORM_data1;
9682 case 2:
9683 return DW_FORM_data2;
9684 case 4:
9685 return DW_FORM_data4;
9686 case 8:
9687 return DW_FORM_data8;
9688 default:
9689 gcc_unreachable ();
9691 case dw_val_class_loc_list:
9692 case dw_val_class_view_list:
9693 if (dwarf_split_debug_info
9694 && dwarf_version >= 5
9695 && AT_loc_list (a)->num_assigned)
9696 return DW_FORM_loclistx;
9697 /* FALLTHRU */
9698 case dw_val_class_range_list:
9699 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9700 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9701 care about sizes of .debug* sections in shared libraries and
9702 executables and don't take into account relocations that affect just
9703 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9704 table in the .debug_rnglists section. */
9705 if (dwarf_split_debug_info
9706 && dwarf_version >= 5
9707 && AT_class (a) == dw_val_class_range_list
9708 && rnglist_idx
9709 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9710 return DW_FORM_rnglistx;
9711 if (dwarf_version >= 4)
9712 return DW_FORM_sec_offset;
9713 /* FALLTHRU */
9714 case dw_val_class_vms_delta:
9715 case dw_val_class_offset:
9716 switch (DWARF_OFFSET_SIZE)
9718 case 4:
9719 return DW_FORM_data4;
9720 case 8:
9721 return DW_FORM_data8;
9722 default:
9723 gcc_unreachable ();
9725 case dw_val_class_loc:
9726 if (dwarf_version >= 4)
9727 return DW_FORM_exprloc;
9728 switch (constant_size (size_of_locs (AT_loc (a))))
9730 case 1:
9731 return DW_FORM_block1;
9732 case 2:
9733 return DW_FORM_block2;
9734 case 4:
9735 return DW_FORM_block4;
9736 default:
9737 gcc_unreachable ();
9739 case dw_val_class_const:
9740 return DW_FORM_sdata;
9741 case dw_val_class_unsigned_const:
9742 switch (constant_size (AT_unsigned (a)))
9744 case 1:
9745 return DW_FORM_data1;
9746 case 2:
9747 return DW_FORM_data2;
9748 case 4:
9749 /* In DWARF3 DW_AT_data_member_location with
9750 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9751 constant, so we need to use DW_FORM_udata if we need
9752 a large constant. */
9753 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9754 return DW_FORM_udata;
9755 return DW_FORM_data4;
9756 case 8:
9757 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9758 return DW_FORM_udata;
9759 return DW_FORM_data8;
9760 default:
9761 gcc_unreachable ();
9763 case dw_val_class_const_implicit:
9764 case dw_val_class_unsigned_const_implicit:
9765 case dw_val_class_file_implicit:
9766 return DW_FORM_implicit_const;
9767 case dw_val_class_const_double:
9768 switch (HOST_BITS_PER_WIDE_INT)
9770 case 8:
9771 return DW_FORM_data2;
9772 case 16:
9773 return DW_FORM_data4;
9774 case 32:
9775 return DW_FORM_data8;
9776 case 64:
9777 if (dwarf_version >= 5)
9778 return DW_FORM_data16;
9779 /* FALLTHRU */
9780 default:
9781 return DW_FORM_block1;
9783 case dw_val_class_wide_int:
9784 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9786 case 8:
9787 return DW_FORM_data1;
9788 case 16:
9789 return DW_FORM_data2;
9790 case 32:
9791 return DW_FORM_data4;
9792 case 64:
9793 return DW_FORM_data8;
9794 case 128:
9795 if (dwarf_version >= 5)
9796 return DW_FORM_data16;
9797 /* FALLTHRU */
9798 default:
9799 return DW_FORM_block1;
9801 case dw_val_class_symview:
9802 /* ??? We might use uleb128, but then we'd have to compute
9803 .debug_info offsets in the assembler. */
9804 if (symview_upper_bound <= 0xff)
9805 return DW_FORM_data1;
9806 else if (symview_upper_bound <= 0xffff)
9807 return DW_FORM_data2;
9808 else if (symview_upper_bound <= 0xffffffff)
9809 return DW_FORM_data4;
9810 else
9811 return DW_FORM_data8;
9812 case dw_val_class_vec:
9813 switch (constant_size (a->dw_attr_val.v.val_vec.length
9814 * a->dw_attr_val.v.val_vec.elt_size))
9816 case 1:
9817 return DW_FORM_block1;
9818 case 2:
9819 return DW_FORM_block2;
9820 case 4:
9821 return DW_FORM_block4;
9822 default:
9823 gcc_unreachable ();
9825 case dw_val_class_flag:
9826 if (dwarf_version >= 4)
9828 /* Currently all add_AT_flag calls pass in 1 as last argument,
9829 so DW_FORM_flag_present can be used. If that ever changes,
9830 we'll need to use DW_FORM_flag and have some optimization
9831 in build_abbrev_table that will change those to
9832 DW_FORM_flag_present if it is set to 1 in all DIEs using
9833 the same abbrev entry. */
9834 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9835 return DW_FORM_flag_present;
9837 return DW_FORM_flag;
9838 case dw_val_class_die_ref:
9839 if (AT_ref_external (a))
9840 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9841 else
9842 return DW_FORM_ref;
9843 case dw_val_class_fde_ref:
9844 return DW_FORM_data;
9845 case dw_val_class_lbl_id:
9846 return (AT_index (a) == NOT_INDEXED
9847 ? DW_FORM_addr : dwarf_FORM (DW_FORM_addrx));
9848 case dw_val_class_lineptr:
9849 case dw_val_class_macptr:
9850 case dw_val_class_loclistsptr:
9851 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9852 case dw_val_class_str:
9853 return AT_string_form (a);
9854 case dw_val_class_file:
9855 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9857 case 1:
9858 return DW_FORM_data1;
9859 case 2:
9860 return DW_FORM_data2;
9861 case 4:
9862 return DW_FORM_data4;
9863 default:
9864 gcc_unreachable ();
9867 case dw_val_class_data8:
9868 return DW_FORM_data8;
9870 case dw_val_class_high_pc:
9871 switch (DWARF2_ADDR_SIZE)
9873 case 1:
9874 return DW_FORM_data1;
9875 case 2:
9876 return DW_FORM_data2;
9877 case 4:
9878 return DW_FORM_data4;
9879 case 8:
9880 return DW_FORM_data8;
9881 default:
9882 gcc_unreachable ();
9885 case dw_val_class_discr_value:
9886 return (a->dw_attr_val.v.val_discr_value.pos
9887 ? DW_FORM_udata
9888 : DW_FORM_sdata);
9889 case dw_val_class_discr_list:
9890 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9892 case 1:
9893 return DW_FORM_block1;
9894 case 2:
9895 return DW_FORM_block2;
9896 case 4:
9897 return DW_FORM_block4;
9898 default:
9899 gcc_unreachable ();
9902 default:
9903 gcc_unreachable ();
9907 /* Output the encoding of an attribute value. */
9909 static void
9910 output_value_format (dw_attr_node *a)
9912 enum dwarf_form form = value_format (a);
9914 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9917 /* Given a die and id, produce the appropriate abbreviations. */
9919 static void
9920 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9922 unsigned ix;
9923 dw_attr_node *a_attr;
9925 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9926 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9927 dwarf_tag_name (abbrev->die_tag));
9929 if (abbrev->die_child != NULL)
9930 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9931 else
9932 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9934 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9936 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9937 dwarf_attr_name (a_attr->dw_attr));
9938 output_value_format (a_attr);
9939 if (value_format (a_attr) == DW_FORM_implicit_const)
9941 if (AT_class (a_attr) == dw_val_class_file_implicit)
9943 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9944 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9945 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9947 else
9948 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9952 dw2_asm_output_data (1, 0, NULL);
9953 dw2_asm_output_data (1, 0, NULL);
9957 /* Output the .debug_abbrev section which defines the DIE abbreviation
9958 table. */
9960 static void
9961 output_abbrev_section (void)
9963 unsigned int abbrev_id;
9964 dw_die_ref abbrev;
9966 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9967 if (abbrev_id != 0)
9968 output_die_abbrevs (abbrev_id, abbrev);
9970 /* Terminate the table. */
9971 dw2_asm_output_data (1, 0, NULL);
9974 /* Return a new location list, given the begin and end range, and the
9975 expression. */
9977 static inline dw_loc_list_ref
9978 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9979 const char *end, var_loc_view vend,
9980 const char *section)
9982 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9984 retlist->begin = begin;
9985 retlist->begin_entry = NULL;
9986 retlist->end = end;
9987 retlist->expr = expr;
9988 retlist->section = section;
9989 retlist->vbegin = vbegin;
9990 retlist->vend = vend;
9992 return retlist;
9995 /* Return true iff there's any nonzero view number in the loc list.
9997 ??? When views are not enabled, we'll often extend a single range
9998 to the entire function, so that we emit a single location
9999 expression rather than a location list. With views, even with a
10000 single range, we'll output a list if start or end have a nonzero
10001 view. If we change this, we may want to stop splitting a single
10002 range in dw_loc_list just because of a nonzero view, even if it
10003 straddles across hot/cold partitions. */
10005 static bool
10006 loc_list_has_views (dw_loc_list_ref list)
10008 if (!debug_variable_location_views)
10009 return false;
10011 for (dw_loc_list_ref loc = list;
10012 loc != NULL; loc = loc->dw_loc_next)
10013 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10014 return true;
10016 return false;
10019 /* Generate a new internal symbol for this location list node, if it
10020 hasn't got one yet. */
10022 static inline void
10023 gen_llsym (dw_loc_list_ref list)
10025 gcc_assert (!list->ll_symbol);
10026 list->ll_symbol = gen_internal_sym ("LLST");
10028 if (!loc_list_has_views (list))
10029 return;
10031 if (dwarf2out_locviews_in_attribute ())
10033 /* Use the same label_num for the view list. */
10034 label_num--;
10035 list->vl_symbol = gen_internal_sym ("LVUS");
10037 else
10038 list->vl_symbol = list->ll_symbol;
10041 /* Generate a symbol for the list, but only if we really want to emit
10042 it as a list. */
10044 static inline void
10045 maybe_gen_llsym (dw_loc_list_ref list)
10047 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10048 return;
10050 gen_llsym (list);
10053 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10054 NULL, don't consider size of the location expression. If we're not
10055 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10056 representation in *SIZEP. */
10058 static bool
10059 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10061 /* Don't output an entry that starts and ends at the same address. */
10062 if (strcmp (curr->begin, curr->end) == 0
10063 && curr->vbegin == curr->vend && !curr->force)
10064 return true;
10066 if (!sizep)
10067 return false;
10069 unsigned long size = size_of_locs (curr->expr);
10071 /* If the expression is too large, drop it on the floor. We could
10072 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10073 in the expression, but >= 64KB expressions for a single value
10074 in a single range are unlikely very useful. */
10075 if (dwarf_version < 5 && size > 0xffff)
10076 return true;
10078 *sizep = size;
10080 return false;
10083 /* Output a view pair loclist entry for CURR, if it requires one. */
10085 static void
10086 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10088 if (!dwarf2out_locviews_in_loclist ())
10089 return;
10091 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10092 return;
10094 #ifdef DW_LLE_view_pair
10095 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10097 if (dwarf2out_as_locview_support)
10099 if (ZERO_VIEW_P (curr->vbegin))
10100 dw2_asm_output_data_uleb128 (0, "Location view begin");
10101 else
10103 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10104 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10105 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10108 if (ZERO_VIEW_P (curr->vend))
10109 dw2_asm_output_data_uleb128 (0, "Location view end");
10110 else
10112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10113 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10114 dw2_asm_output_symname_uleb128 (label, "Location view end");
10117 else
10119 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10120 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10122 #endif /* DW_LLE_view_pair */
10124 return;
10127 /* Output the location list given to us. */
10129 static void
10130 output_loc_list (dw_loc_list_ref list_head)
10132 int vcount = 0, lcount = 0;
10134 if (list_head->emitted)
10135 return;
10136 list_head->emitted = true;
10138 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10140 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10142 for (dw_loc_list_ref curr = list_head; curr != NULL;
10143 curr = curr->dw_loc_next)
10145 unsigned long size;
10147 if (skip_loc_list_entry (curr, &size))
10148 continue;
10150 vcount++;
10152 /* ?? dwarf_split_debug_info? */
10153 if (dwarf2out_as_locview_support)
10155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10157 if (!ZERO_VIEW_P (curr->vbegin))
10159 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10160 dw2_asm_output_symname_uleb128 (label,
10161 "View list begin (%s)",
10162 list_head->vl_symbol);
10164 else
10165 dw2_asm_output_data_uleb128 (0,
10166 "View list begin (%s)",
10167 list_head->vl_symbol);
10169 if (!ZERO_VIEW_P (curr->vend))
10171 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10172 dw2_asm_output_symname_uleb128 (label,
10173 "View list end (%s)",
10174 list_head->vl_symbol);
10176 else
10177 dw2_asm_output_data_uleb128 (0,
10178 "View list end (%s)",
10179 list_head->vl_symbol);
10181 else
10183 dw2_asm_output_data_uleb128 (curr->vbegin,
10184 "View list begin (%s)",
10185 list_head->vl_symbol);
10186 dw2_asm_output_data_uleb128 (curr->vend,
10187 "View list end (%s)",
10188 list_head->vl_symbol);
10193 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10195 const char *last_section = NULL;
10196 const char *base_label = NULL;
10198 /* Walk the location list, and output each range + expression. */
10199 for (dw_loc_list_ref curr = list_head; curr != NULL;
10200 curr = curr->dw_loc_next)
10202 unsigned long size;
10204 /* Skip this entry? If we skip it here, we must skip it in the
10205 view list above as well. */
10206 if (skip_loc_list_entry (curr, &size))
10207 continue;
10209 lcount++;
10211 if (dwarf_version >= 5)
10213 if (dwarf_split_debug_info)
10215 dwarf2out_maybe_output_loclist_view_pair (curr);
10216 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10217 uleb128 index into .debug_addr and uleb128 length. */
10218 dw2_asm_output_data (1, DW_LLE_startx_length,
10219 "DW_LLE_startx_length (%s)",
10220 list_head->ll_symbol);
10221 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10222 "Location list range start index "
10223 "(%s)", curr->begin);
10224 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10225 For that case we probably need to emit DW_LLE_startx_endx,
10226 but we'd need 2 .debug_addr entries rather than just one. */
10227 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10228 "Location list length (%s)",
10229 list_head->ll_symbol);
10231 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10233 dwarf2out_maybe_output_loclist_view_pair (curr);
10234 /* If all code is in .text section, the base address is
10235 already provided by the CU attributes. Use
10236 DW_LLE_offset_pair where both addresses are uleb128 encoded
10237 offsets against that base. */
10238 dw2_asm_output_data (1, DW_LLE_offset_pair,
10239 "DW_LLE_offset_pair (%s)",
10240 list_head->ll_symbol);
10241 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10242 "Location list begin address (%s)",
10243 list_head->ll_symbol);
10244 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10245 "Location list end address (%s)",
10246 list_head->ll_symbol);
10248 else if (HAVE_AS_LEB128)
10250 /* Otherwise, find out how many consecutive entries could share
10251 the same base entry. If just one, emit DW_LLE_start_length,
10252 otherwise emit DW_LLE_base_address for the base address
10253 followed by a series of DW_LLE_offset_pair. */
10254 if (last_section == NULL || curr->section != last_section)
10256 dw_loc_list_ref curr2;
10257 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10258 curr2 = curr2->dw_loc_next)
10260 if (strcmp (curr2->begin, curr2->end) == 0
10261 && !curr2->force)
10262 continue;
10263 break;
10265 if (curr2 == NULL || curr->section != curr2->section)
10266 last_section = NULL;
10267 else
10269 last_section = curr->section;
10270 base_label = curr->begin;
10271 dw2_asm_output_data (1, DW_LLE_base_address,
10272 "DW_LLE_base_address (%s)",
10273 list_head->ll_symbol);
10274 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10275 "Base address (%s)",
10276 list_head->ll_symbol);
10279 /* Only one entry with the same base address. Use
10280 DW_LLE_start_length with absolute address and uleb128
10281 length. */
10282 if (last_section == NULL)
10284 dwarf2out_maybe_output_loclist_view_pair (curr);
10285 dw2_asm_output_data (1, DW_LLE_start_length,
10286 "DW_LLE_start_length (%s)",
10287 list_head->ll_symbol);
10288 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10289 "Location list begin address (%s)",
10290 list_head->ll_symbol);
10291 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10292 "Location list length "
10293 "(%s)", list_head->ll_symbol);
10295 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10296 DW_LLE_base_address. */
10297 else
10299 dwarf2out_maybe_output_loclist_view_pair (curr);
10300 dw2_asm_output_data (1, DW_LLE_offset_pair,
10301 "DW_LLE_offset_pair (%s)",
10302 list_head->ll_symbol);
10303 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10304 "Location list begin address "
10305 "(%s)", list_head->ll_symbol);
10306 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10307 "Location list end address "
10308 "(%s)", list_head->ll_symbol);
10311 /* The assembler does not support .uleb128 directive. Emit
10312 DW_LLE_start_end with a pair of absolute addresses. */
10313 else
10315 dwarf2out_maybe_output_loclist_view_pair (curr);
10316 dw2_asm_output_data (1, DW_LLE_start_end,
10317 "DW_LLE_start_end (%s)",
10318 list_head->ll_symbol);
10319 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10320 "Location list begin address (%s)",
10321 list_head->ll_symbol);
10322 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10323 "Location list end address (%s)",
10324 list_head->ll_symbol);
10327 else if (dwarf_split_debug_info)
10329 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10330 and 4 byte length. */
10331 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10332 "Location list start/length entry (%s)",
10333 list_head->ll_symbol);
10334 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10335 "Location list range start index (%s)",
10336 curr->begin);
10337 /* The length field is 4 bytes. If we ever need to support
10338 an 8-byte length, we can add a new DW_LLE code or fall back
10339 to DW_LLE_GNU_start_end_entry. */
10340 dw2_asm_output_delta (4, curr->end, curr->begin,
10341 "Location list range length (%s)",
10342 list_head->ll_symbol);
10344 else if (!have_multiple_function_sections)
10346 /* Pair of relative addresses against start of text section. */
10347 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10348 "Location list begin address (%s)",
10349 list_head->ll_symbol);
10350 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10351 "Location list end address (%s)",
10352 list_head->ll_symbol);
10354 else
10356 /* Pair of absolute addresses. */
10357 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10358 "Location list begin address (%s)",
10359 list_head->ll_symbol);
10360 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10361 "Location list end address (%s)",
10362 list_head->ll_symbol);
10365 /* Output the block length for this list of location operations. */
10366 if (dwarf_version >= 5)
10367 dw2_asm_output_data_uleb128 (size, "Location expression size");
10368 else
10370 gcc_assert (size <= 0xffff);
10371 dw2_asm_output_data (2, size, "Location expression size");
10374 output_loc_sequence (curr->expr, -1);
10377 /* And finally list termination. */
10378 if (dwarf_version >= 5)
10379 dw2_asm_output_data (1, DW_LLE_end_of_list,
10380 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10381 else if (dwarf_split_debug_info)
10382 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10383 "Location list terminator (%s)",
10384 list_head->ll_symbol);
10385 else
10387 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10388 "Location list terminator begin (%s)",
10389 list_head->ll_symbol);
10390 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10391 "Location list terminator end (%s)",
10392 list_head->ll_symbol);
10395 gcc_assert (!list_head->vl_symbol
10396 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10399 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10400 section. Emit a relocated reference if val_entry is NULL, otherwise,
10401 emit an indirect reference. */
10403 static void
10404 output_range_list_offset (dw_attr_node *a)
10406 const char *name = dwarf_attr_name (a->dw_attr);
10408 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10410 if (dwarf_version >= 5)
10412 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10413 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10414 debug_ranges_section, "%s", name);
10416 else
10418 char *p = strchr (ranges_section_label, '\0');
10419 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10420 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10421 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10422 debug_ranges_section, "%s", name);
10423 *p = '\0';
10426 else if (dwarf_version >= 5)
10428 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10429 gcc_assert (rnglist_idx);
10430 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10432 else
10433 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10434 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10435 "%s (offset from %s)", name, ranges_section_label);
10438 /* Output the offset into the debug_loc section. */
10440 static void
10441 output_loc_list_offset (dw_attr_node *a)
10443 char *sym = AT_loc_list (a)->ll_symbol;
10445 gcc_assert (sym);
10446 if (!dwarf_split_debug_info)
10447 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10448 "%s", dwarf_attr_name (a->dw_attr));
10449 else if (dwarf_version >= 5)
10451 gcc_assert (AT_loc_list (a)->num_assigned);
10452 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10453 dwarf_attr_name (a->dw_attr),
10454 sym);
10456 else
10457 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10458 "%s", dwarf_attr_name (a->dw_attr));
10461 /* Output the offset into the debug_loc section. */
10463 static void
10464 output_view_list_offset (dw_attr_node *a)
10466 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10468 gcc_assert (sym);
10469 if (dwarf_split_debug_info)
10470 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10471 "%s", dwarf_attr_name (a->dw_attr));
10472 else
10473 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10474 "%s", dwarf_attr_name (a->dw_attr));
10477 /* Output an attribute's index or value appropriately. */
10479 static void
10480 output_attr_index_or_value (dw_attr_node *a)
10482 const char *name = dwarf_attr_name (a->dw_attr);
10484 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10486 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10487 return;
10489 switch (AT_class (a))
10491 case dw_val_class_addr:
10492 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10493 break;
10494 case dw_val_class_high_pc:
10495 case dw_val_class_lbl_id:
10496 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10497 break;
10498 default:
10499 gcc_unreachable ();
10503 /* Output a type signature. */
10505 static inline void
10506 output_signature (const char *sig, const char *name)
10508 int i;
10510 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10511 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10514 /* Output a discriminant value. */
10516 static inline void
10517 output_discr_value (dw_discr_value *discr_value, const char *name)
10519 if (discr_value->pos)
10520 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10521 else
10522 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10525 /* Output the DIE and its attributes. Called recursively to generate
10526 the definitions of each child DIE. */
10528 static void
10529 output_die (dw_die_ref die)
10531 dw_attr_node *a;
10532 dw_die_ref c;
10533 unsigned long size;
10534 unsigned ix;
10536 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10537 (unsigned long)die->die_offset,
10538 dwarf_tag_name (die->die_tag));
10540 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10542 const char *name = dwarf_attr_name (a->dw_attr);
10544 switch (AT_class (a))
10546 case dw_val_class_addr:
10547 output_attr_index_or_value (a);
10548 break;
10550 case dw_val_class_offset:
10551 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10552 "%s", name);
10553 break;
10555 case dw_val_class_range_list:
10556 output_range_list_offset (a);
10557 break;
10559 case dw_val_class_loc:
10560 size = size_of_locs (AT_loc (a));
10562 /* Output the block length for this list of location operations. */
10563 if (dwarf_version >= 4)
10564 dw2_asm_output_data_uleb128 (size, "%s", name);
10565 else
10566 dw2_asm_output_data (constant_size (size), size, "%s", name);
10568 output_loc_sequence (AT_loc (a), -1);
10569 break;
10571 case dw_val_class_const:
10572 /* ??? It would be slightly more efficient to use a scheme like is
10573 used for unsigned constants below, but gdb 4.x does not sign
10574 extend. Gdb 5.x does sign extend. */
10575 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10576 break;
10578 case dw_val_class_unsigned_const:
10580 int csize = constant_size (AT_unsigned (a));
10581 if (dwarf_version == 3
10582 && a->dw_attr == DW_AT_data_member_location
10583 && csize >= 4)
10584 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10585 else
10586 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10588 break;
10590 case dw_val_class_symview:
10592 int vsize;
10593 if (symview_upper_bound <= 0xff)
10594 vsize = 1;
10595 else if (symview_upper_bound <= 0xffff)
10596 vsize = 2;
10597 else if (symview_upper_bound <= 0xffffffff)
10598 vsize = 4;
10599 else
10600 vsize = 8;
10601 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10602 "%s", name);
10604 break;
10606 case dw_val_class_const_implicit:
10607 if (flag_debug_asm)
10608 fprintf (asm_out_file, "\t\t\t%s %s ("
10609 HOST_WIDE_INT_PRINT_DEC ")\n",
10610 ASM_COMMENT_START, name, AT_int (a));
10611 break;
10613 case dw_val_class_unsigned_const_implicit:
10614 if (flag_debug_asm)
10615 fprintf (asm_out_file, "\t\t\t%s %s ("
10616 HOST_WIDE_INT_PRINT_HEX ")\n",
10617 ASM_COMMENT_START, name, AT_unsigned (a));
10618 break;
10620 case dw_val_class_const_double:
10622 unsigned HOST_WIDE_INT first, second;
10624 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10625 dw2_asm_output_data (1,
10626 HOST_BITS_PER_DOUBLE_INT
10627 / HOST_BITS_PER_CHAR,
10628 NULL);
10630 if (WORDS_BIG_ENDIAN)
10632 first = a->dw_attr_val.v.val_double.high;
10633 second = a->dw_attr_val.v.val_double.low;
10635 else
10637 first = a->dw_attr_val.v.val_double.low;
10638 second = a->dw_attr_val.v.val_double.high;
10641 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10642 first, "%s", name);
10643 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10644 second, NULL);
10646 break;
10648 case dw_val_class_wide_int:
10650 int i;
10651 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10652 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10653 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10654 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10655 * l, NULL);
10657 if (WORDS_BIG_ENDIAN)
10658 for (i = len - 1; i >= 0; --i)
10660 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10661 "%s", name);
10662 name = "";
10664 else
10665 for (i = 0; i < len; ++i)
10667 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10668 "%s", name);
10669 name = "";
10672 break;
10674 case dw_val_class_vec:
10676 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10677 unsigned int len = a->dw_attr_val.v.val_vec.length;
10678 unsigned int i;
10679 unsigned char *p;
10681 dw2_asm_output_data (constant_size (len * elt_size),
10682 len * elt_size, "%s", name);
10683 if (elt_size > sizeof (HOST_WIDE_INT))
10685 elt_size /= 2;
10686 len *= 2;
10688 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10689 i < len;
10690 i++, p += elt_size)
10691 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10692 "fp or vector constant word %u", i);
10693 break;
10696 case dw_val_class_flag:
10697 if (dwarf_version >= 4)
10699 /* Currently all add_AT_flag calls pass in 1 as last argument,
10700 so DW_FORM_flag_present can be used. If that ever changes,
10701 we'll need to use DW_FORM_flag and have some optimization
10702 in build_abbrev_table that will change those to
10703 DW_FORM_flag_present if it is set to 1 in all DIEs using
10704 the same abbrev entry. */
10705 gcc_assert (AT_flag (a) == 1);
10706 if (flag_debug_asm)
10707 fprintf (asm_out_file, "\t\t\t%s %s\n",
10708 ASM_COMMENT_START, name);
10709 break;
10711 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10712 break;
10714 case dw_val_class_loc_list:
10715 output_loc_list_offset (a);
10716 break;
10718 case dw_val_class_view_list:
10719 output_view_list_offset (a);
10720 break;
10722 case dw_val_class_die_ref:
10723 if (AT_ref_external (a))
10725 if (AT_ref (a)->comdat_type_p)
10727 comdat_type_node *type_node
10728 = AT_ref (a)->die_id.die_type_node;
10730 gcc_assert (type_node);
10731 output_signature (type_node->signature, name);
10733 else
10735 const char *sym = AT_ref (a)->die_id.die_symbol;
10736 int size;
10738 gcc_assert (sym);
10739 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10740 length, whereas in DWARF3 it's always sized as an
10741 offset. */
10742 if (dwarf_version == 2)
10743 size = DWARF2_ADDR_SIZE;
10744 else
10745 size = DWARF_OFFSET_SIZE;
10746 /* ??? We cannot unconditionally output die_offset if
10747 non-zero - others might create references to those
10748 DIEs via symbols.
10749 And we do not clear its DIE offset after outputting it
10750 (and the label refers to the actual DIEs, not the
10751 DWARF CU unit header which is when using label + offset
10752 would be the correct thing to do).
10753 ??? This is the reason for the with_offset flag. */
10754 if (AT_ref (a)->with_offset)
10755 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10756 debug_info_section, "%s", name);
10757 else
10758 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10759 name);
10762 else
10764 gcc_assert (AT_ref (a)->die_offset);
10765 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10766 "%s", name);
10768 break;
10770 case dw_val_class_fde_ref:
10772 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10774 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10775 a->dw_attr_val.v.val_fde_index * 2);
10776 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10777 "%s", name);
10779 break;
10781 case dw_val_class_vms_delta:
10782 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10783 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10784 AT_vms_delta2 (a), AT_vms_delta1 (a),
10785 "%s", name);
10786 #else
10787 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10788 AT_vms_delta2 (a), AT_vms_delta1 (a),
10789 "%s", name);
10790 #endif
10791 break;
10793 case dw_val_class_lbl_id:
10794 output_attr_index_or_value (a);
10795 break;
10797 case dw_val_class_lineptr:
10798 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10799 debug_line_section, "%s", name);
10800 break;
10802 case dw_val_class_macptr:
10803 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10804 debug_macinfo_section, "%s", name);
10805 break;
10807 case dw_val_class_loclistsptr:
10808 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10809 debug_loc_section, "%s", name);
10810 break;
10812 case dw_val_class_str:
10813 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10814 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10815 a->dw_attr_val.v.val_str->label,
10816 debug_str_section,
10817 "%s: \"%s\"", name, AT_string (a));
10818 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10819 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10820 a->dw_attr_val.v.val_str->label,
10821 debug_line_str_section,
10822 "%s: \"%s\"", name, AT_string (a));
10823 else if (a->dw_attr_val.v.val_str->form == dwarf_FORM (DW_FORM_strx))
10824 dw2_asm_output_data_uleb128 (AT_index (a),
10825 "%s: \"%s\"", name, AT_string (a));
10826 else
10827 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10828 break;
10830 case dw_val_class_file:
10832 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10834 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10835 a->dw_attr_val.v.val_file->filename);
10836 break;
10839 case dw_val_class_file_implicit:
10840 if (flag_debug_asm)
10841 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10842 ASM_COMMENT_START, name,
10843 maybe_emit_file (a->dw_attr_val.v.val_file),
10844 a->dw_attr_val.v.val_file->filename);
10845 break;
10847 case dw_val_class_data8:
10849 int i;
10851 for (i = 0; i < 8; i++)
10852 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10853 i == 0 ? "%s" : NULL, name);
10854 break;
10857 case dw_val_class_high_pc:
10858 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10859 get_AT_low_pc (die), "DW_AT_high_pc");
10860 break;
10862 case dw_val_class_discr_value:
10863 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10864 break;
10866 case dw_val_class_discr_list:
10868 dw_discr_list_ref list = AT_discr_list (a);
10869 const int size = size_of_discr_list (list);
10871 /* This is a block, so output its length first. */
10872 dw2_asm_output_data (constant_size (size), size,
10873 "%s: block size", name);
10875 for (; list != NULL; list = list->dw_discr_next)
10877 /* One byte for the discriminant value descriptor, and then as
10878 many LEB128 numbers as required. */
10879 if (list->dw_discr_range)
10880 dw2_asm_output_data (1, DW_DSC_range,
10881 "%s: DW_DSC_range", name);
10882 else
10883 dw2_asm_output_data (1, DW_DSC_label,
10884 "%s: DW_DSC_label", name);
10886 output_discr_value (&list->dw_discr_lower_bound, name);
10887 if (list->dw_discr_range)
10888 output_discr_value (&list->dw_discr_upper_bound, name);
10890 break;
10893 default:
10894 gcc_unreachable ();
10898 FOR_EACH_CHILD (die, c, output_die (c));
10900 /* Add null byte to terminate sibling list. */
10901 if (die->die_child != NULL)
10902 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10903 (unsigned long) die->die_offset);
10906 /* Output the dwarf version number. */
10908 static void
10909 output_dwarf_version ()
10911 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10912 views in loclist. That will change eventually. */
10913 if (dwarf_version == 6)
10915 static bool once;
10916 if (!once)
10918 warning (0,
10919 "-gdwarf-6 is output as version 5 with incompatibilities");
10920 once = true;
10922 dw2_asm_output_data (2, 5, "DWARF version number");
10924 else
10925 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10928 /* Output the compilation unit that appears at the beginning of the
10929 .debug_info section, and precedes the DIE descriptions. */
10931 static void
10932 output_compilation_unit_header (enum dwarf_unit_type ut)
10934 if (!XCOFF_DEBUGGING_INFO)
10936 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10937 dw2_asm_output_data (4, 0xffffffff,
10938 "Initial length escape value indicating 64-bit DWARF extension");
10939 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10940 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10941 "Length of Compilation Unit Info");
10944 output_dwarf_version ();
10945 if (dwarf_version >= 5)
10947 const char *name;
10948 switch (ut)
10950 case DW_UT_compile: name = "DW_UT_compile"; break;
10951 case DW_UT_type: name = "DW_UT_type"; break;
10952 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10953 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10954 default: gcc_unreachable ();
10956 dw2_asm_output_data (1, ut, "%s", name);
10957 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10960 debug_abbrev_section,
10961 "Offset Into Abbrev. Section");
10962 if (dwarf_version < 5)
10963 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10966 /* Output the compilation unit DIE and its children. */
10968 static void
10969 output_comp_unit (dw_die_ref die, int output_if_empty,
10970 const unsigned char *dwo_id)
10972 const char *secname, *oldsym;
10973 char *tmp;
10975 /* Unless we are outputting main CU, we may throw away empty ones. */
10976 if (!output_if_empty && die->die_child == NULL)
10977 return;
10979 /* Even if there are no children of this DIE, we must output the information
10980 about the compilation unit. Otherwise, on an empty translation unit, we
10981 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10982 will then complain when examining the file. First mark all the DIEs in
10983 this CU so we know which get local refs. */
10984 mark_dies (die);
10986 external_ref_hash_type *extern_map = optimize_external_refs (die);
10988 /* For now, optimize only the main CU, in order to optimize the rest
10989 we'd need to see all of them earlier. Leave the rest for post-linking
10990 tools like DWZ. */
10991 if (die == comp_unit_die ())
10992 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10994 build_abbrev_table (die, extern_map);
10996 optimize_abbrev_table ();
10998 delete extern_map;
11000 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11001 next_die_offset = (dwo_id
11002 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11003 : DWARF_COMPILE_UNIT_HEADER_SIZE);
11004 calc_die_sizes (die);
11006 oldsym = die->die_id.die_symbol;
11007 if (oldsym && die->comdat_type_p)
11009 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11011 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11012 secname = tmp;
11013 die->die_id.die_symbol = NULL;
11014 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11016 else
11018 switch_to_section (debug_info_section);
11019 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11020 info_section_emitted = true;
11023 /* For LTO cross unit DIE refs we want a symbol on the start of the
11024 debuginfo section, not on the CU DIE. */
11025 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11027 /* ??? No way to get visibility assembled without a decl. */
11028 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11029 get_identifier (oldsym), char_type_node);
11030 TREE_PUBLIC (decl) = true;
11031 TREE_STATIC (decl) = true;
11032 DECL_ARTIFICIAL (decl) = true;
11033 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11034 DECL_VISIBILITY_SPECIFIED (decl) = true;
11035 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11036 #ifdef ASM_WEAKEN_LABEL
11037 /* We prefer a .weak because that handles duplicates from duplicate
11038 archive members in a graceful way. */
11039 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11040 #else
11041 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11042 #endif
11043 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11046 /* Output debugging information. */
11047 output_compilation_unit_header (dwo_id
11048 ? DW_UT_split_compile : DW_UT_compile);
11049 if (dwarf_version >= 5)
11051 if (dwo_id != NULL)
11052 for (int i = 0; i < 8; i++)
11053 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11055 output_die (die);
11057 /* Leave the marks on the main CU, so we can check them in
11058 output_pubnames. */
11059 if (oldsym)
11061 unmark_dies (die);
11062 die->die_id.die_symbol = oldsym;
11066 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11067 and .debug_pubtypes. This is configured per-target, but can be
11068 overridden by the -gpubnames or -gno-pubnames options. */
11070 static inline bool
11071 want_pubnames (void)
11073 if (debug_info_level <= DINFO_LEVEL_TERSE)
11074 return false;
11075 if (debug_generate_pub_sections != -1)
11076 return debug_generate_pub_sections;
11077 return targetm.want_debug_pub_sections;
11080 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11082 static void
11083 add_AT_pubnames (dw_die_ref die)
11085 if (want_pubnames ())
11086 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11089 /* Add a string attribute value to a skeleton DIE. */
11091 static inline void
11092 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11093 const char *str)
11095 dw_attr_node attr;
11096 struct indirect_string_node *node;
11098 if (! skeleton_debug_str_hash)
11099 skeleton_debug_str_hash
11100 = hash_table<indirect_string_hasher>::create_ggc (10);
11102 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11103 find_string_form (node);
11104 if (node->form == dwarf_FORM (DW_FORM_strx))
11105 node->form = DW_FORM_strp;
11107 attr.dw_attr = attr_kind;
11108 attr.dw_attr_val.val_class = dw_val_class_str;
11109 attr.dw_attr_val.val_entry = NULL;
11110 attr.dw_attr_val.v.val_str = node;
11111 add_dwarf_attr (die, &attr);
11114 /* Helper function to generate top-level dies for skeleton debug_info and
11115 debug_types. */
11117 static void
11118 add_top_level_skeleton_die_attrs (dw_die_ref die)
11120 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11121 const char *comp_dir = comp_dir_string ();
11123 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11124 if (comp_dir != NULL)
11125 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11126 add_AT_pubnames (die);
11127 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11130 /* Output skeleton debug sections that point to the dwo file. */
11132 static void
11133 output_skeleton_debug_sections (dw_die_ref comp_unit,
11134 const unsigned char *dwo_id)
11136 /* These attributes will be found in the full debug_info section. */
11137 remove_AT (comp_unit, DW_AT_producer);
11138 remove_AT (comp_unit, DW_AT_language);
11140 switch_to_section (debug_skeleton_info_section);
11141 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11143 /* Produce the skeleton compilation-unit header. This one differs enough from
11144 a normal CU header that it's better not to call output_compilation_unit
11145 header. */
11146 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11147 dw2_asm_output_data (4, 0xffffffff,
11148 "Initial length escape value indicating 64-bit "
11149 "DWARF extension");
11151 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11152 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11153 - DWARF_INITIAL_LENGTH_SIZE
11154 + size_of_die (comp_unit),
11155 "Length of Compilation Unit Info");
11156 output_dwarf_version ();
11157 if (dwarf_version >= 5)
11159 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11160 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11162 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11163 debug_skeleton_abbrev_section,
11164 "Offset Into Abbrev. Section");
11165 if (dwarf_version < 5)
11166 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11167 else
11168 for (int i = 0; i < 8; i++)
11169 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11171 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11172 output_die (comp_unit);
11174 /* Build the skeleton debug_abbrev section. */
11175 switch_to_section (debug_skeleton_abbrev_section);
11176 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11178 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11180 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11183 /* Output a comdat type unit DIE and its children. */
11185 static void
11186 output_comdat_type_unit (comdat_type_node *node)
11188 const char *secname;
11189 char *tmp;
11190 int i;
11191 #if defined (OBJECT_FORMAT_ELF)
11192 tree comdat_key;
11193 #endif
11195 /* First mark all the DIEs in this CU so we know which get local refs. */
11196 mark_dies (node->root_die);
11198 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11200 build_abbrev_table (node->root_die, extern_map);
11202 delete extern_map;
11203 extern_map = NULL;
11205 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11206 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11207 calc_die_sizes (node->root_die);
11209 #if defined (OBJECT_FORMAT_ELF)
11210 if (dwarf_version >= 5)
11212 if (!dwarf_split_debug_info)
11213 secname = ".debug_info";
11214 else
11215 secname = ".debug_info.dwo";
11217 else if (!dwarf_split_debug_info)
11218 secname = ".debug_types";
11219 else
11220 secname = ".debug_types.dwo";
11222 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11223 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11224 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11225 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11226 comdat_key = get_identifier (tmp);
11227 targetm.asm_out.named_section (secname,
11228 SECTION_DEBUG | SECTION_LINKONCE,
11229 comdat_key);
11230 #else
11231 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11232 sprintf (tmp, (dwarf_version >= 5
11233 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11234 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11235 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11236 secname = tmp;
11237 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11238 #endif
11240 /* Output debugging information. */
11241 output_compilation_unit_header (dwarf_split_debug_info
11242 ? DW_UT_split_type : DW_UT_type);
11243 output_signature (node->signature, "Type Signature");
11244 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11245 "Offset to Type DIE");
11246 output_die (node->root_die);
11248 unmark_dies (node->root_die);
11251 /* Return the DWARF2/3 pubname associated with a decl. */
11253 static const char *
11254 dwarf2_name (tree decl, int scope)
11256 if (DECL_NAMELESS (decl))
11257 return NULL;
11258 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11261 /* Add a new entry to .debug_pubnames if appropriate. */
11263 static void
11264 add_pubname_string (const char *str, dw_die_ref die)
11266 pubname_entry e;
11268 e.die = die;
11269 e.name = xstrdup (str);
11270 vec_safe_push (pubname_table, e);
11273 static void
11274 add_pubname (tree decl, dw_die_ref die)
11276 if (!want_pubnames ())
11277 return;
11279 /* Don't add items to the table when we expect that the consumer will have
11280 just read the enclosing die. For example, if the consumer is looking at a
11281 class_member, it will either be inside the class already, or will have just
11282 looked up the class to find the member. Either way, searching the class is
11283 faster than searching the index. */
11284 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11285 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11287 const char *name = dwarf2_name (decl, 1);
11289 if (name)
11290 add_pubname_string (name, die);
11294 /* Add an enumerator to the pubnames section. */
11296 static void
11297 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11299 pubname_entry e;
11301 gcc_assert (scope_name);
11302 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11303 e.die = die;
11304 vec_safe_push (pubname_table, e);
11307 /* Add a new entry to .debug_pubtypes if appropriate. */
11309 static void
11310 add_pubtype (tree decl, dw_die_ref die)
11312 pubname_entry e;
11314 if (!want_pubnames ())
11315 return;
11317 if ((TREE_PUBLIC (decl)
11318 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11319 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11321 tree scope = NULL;
11322 const char *scope_name = "";
11323 const char *sep = is_cxx () ? "::" : ".";
11324 const char *name;
11326 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11327 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11329 scope_name = lang_hooks.dwarf_name (scope, 1);
11330 if (scope_name != NULL && scope_name[0] != '\0')
11331 scope_name = concat (scope_name, sep, NULL);
11332 else
11333 scope_name = "";
11336 if (TYPE_P (decl))
11337 name = type_tag (decl);
11338 else
11339 name = lang_hooks.dwarf_name (decl, 1);
11341 /* If we don't have a name for the type, there's no point in adding
11342 it to the table. */
11343 if (name != NULL && name[0] != '\0')
11345 e.die = die;
11346 e.name = concat (scope_name, name, NULL);
11347 vec_safe_push (pubtype_table, e);
11350 /* Although it might be more consistent to add the pubinfo for the
11351 enumerators as their dies are created, they should only be added if the
11352 enum type meets the criteria above. So rather than re-check the parent
11353 enum type whenever an enumerator die is created, just output them all
11354 here. This isn't protected by the name conditional because anonymous
11355 enums don't have names. */
11356 if (die->die_tag == DW_TAG_enumeration_type)
11358 dw_die_ref c;
11360 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11365 /* Output a single entry in the pubnames table. */
11367 static void
11368 output_pubname (dw_offset die_offset, pubname_entry *entry)
11370 dw_die_ref die = entry->die;
11371 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11373 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11375 if (debug_generate_pub_sections == 2)
11377 /* This logic follows gdb's method for determining the value of the flag
11378 byte. */
11379 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11380 switch (die->die_tag)
11382 case DW_TAG_typedef:
11383 case DW_TAG_base_type:
11384 case DW_TAG_subrange_type:
11385 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11386 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11387 break;
11388 case DW_TAG_enumerator:
11389 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11390 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11391 if (!is_cxx ())
11392 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11393 break;
11394 case DW_TAG_subprogram:
11395 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11396 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11397 if (!is_ada ())
11398 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11399 break;
11400 case DW_TAG_constant:
11401 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11402 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11403 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11404 break;
11405 case DW_TAG_variable:
11406 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11407 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11408 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11409 break;
11410 case DW_TAG_namespace:
11411 case DW_TAG_imported_declaration:
11412 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11413 break;
11414 case DW_TAG_class_type:
11415 case DW_TAG_interface_type:
11416 case DW_TAG_structure_type:
11417 case DW_TAG_union_type:
11418 case DW_TAG_enumeration_type:
11419 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11420 if (!is_cxx ())
11421 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11422 break;
11423 default:
11424 /* An unusual tag. Leave the flag-byte empty. */
11425 break;
11427 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11428 "GDB-index flags");
11431 dw2_asm_output_nstring (entry->name, -1, "external name");
11435 /* Output the public names table used to speed up access to externally
11436 visible names; or the public types table used to find type definitions. */
11438 static void
11439 output_pubnames (vec<pubname_entry, va_gc> *names)
11441 unsigned i;
11442 unsigned long pubnames_length = size_of_pubnames (names);
11443 pubname_entry *pub;
11445 if (!XCOFF_DEBUGGING_INFO)
11447 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11448 dw2_asm_output_data (4, 0xffffffff,
11449 "Initial length escape value indicating 64-bit DWARF extension");
11450 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11451 "Pub Info Length");
11454 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11455 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11457 if (dwarf_split_debug_info)
11458 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11459 debug_skeleton_info_section,
11460 "Offset of Compilation Unit Info");
11461 else
11462 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11463 debug_info_section,
11464 "Offset of Compilation Unit Info");
11465 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11466 "Compilation Unit Length");
11468 FOR_EACH_VEC_ELT (*names, i, pub)
11470 if (include_pubname_in_output (names, pub))
11472 dw_offset die_offset = pub->die->die_offset;
11474 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11475 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11476 gcc_assert (pub->die->die_mark);
11478 /* If we're putting types in their own .debug_types sections,
11479 the .debug_pubtypes table will still point to the compile
11480 unit (not the type unit), so we want to use the offset of
11481 the skeleton DIE (if there is one). */
11482 if (pub->die->comdat_type_p && names == pubtype_table)
11484 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11486 if (type_node != NULL)
11487 die_offset = (type_node->skeleton_die != NULL
11488 ? type_node->skeleton_die->die_offset
11489 : comp_unit_die ()->die_offset);
11492 output_pubname (die_offset, pub);
11496 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11499 /* Output public names and types tables if necessary. */
11501 static void
11502 output_pubtables (void)
11504 if (!want_pubnames () || !info_section_emitted)
11505 return;
11507 switch_to_section (debug_pubnames_section);
11508 output_pubnames (pubname_table);
11509 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11510 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11511 simply won't look for the section. */
11512 switch_to_section (debug_pubtypes_section);
11513 output_pubnames (pubtype_table);
11517 /* Output the information that goes into the .debug_aranges table.
11518 Namely, define the beginning and ending address range of the
11519 text section generated for this compilation unit. */
11521 static void
11522 output_aranges (void)
11524 unsigned i;
11525 unsigned long aranges_length = size_of_aranges ();
11527 if (!XCOFF_DEBUGGING_INFO)
11529 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11530 dw2_asm_output_data (4, 0xffffffff,
11531 "Initial length escape value indicating 64-bit DWARF extension");
11532 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11533 "Length of Address Ranges Info");
11536 /* Version number for aranges is still 2, even up to DWARF5. */
11537 dw2_asm_output_data (2, 2, "DWARF aranges version");
11538 if (dwarf_split_debug_info)
11539 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11540 debug_skeleton_info_section,
11541 "Offset of Compilation Unit Info");
11542 else
11543 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11544 debug_info_section,
11545 "Offset of Compilation Unit Info");
11546 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11547 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11549 /* We need to align to twice the pointer size here. */
11550 if (DWARF_ARANGES_PAD_SIZE)
11552 /* Pad using a 2 byte words so that padding is correct for any
11553 pointer size. */
11554 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11555 2 * DWARF2_ADDR_SIZE);
11556 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11557 dw2_asm_output_data (2, 0, NULL);
11560 /* It is necessary not to output these entries if the sections were
11561 not used; if the sections were not used, the length will be 0 and
11562 the address may end up as 0 if the section is discarded by ld
11563 --gc-sections, leaving an invalid (0, 0) entry that can be
11564 confused with the terminator. */
11565 if (text_section_used)
11567 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11568 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11569 text_section_label, "Length");
11571 if (cold_text_section_used)
11573 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11574 "Address");
11575 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11576 cold_text_section_label, "Length");
11579 if (have_multiple_function_sections)
11581 unsigned fde_idx;
11582 dw_fde_ref fde;
11584 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11586 if (DECL_IGNORED_P (fde->decl))
11587 continue;
11588 if (!fde->in_std_section)
11590 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11591 "Address");
11592 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11593 fde->dw_fde_begin, "Length");
11595 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11597 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11598 "Address");
11599 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11600 fde->dw_fde_second_begin, "Length");
11605 /* Output the terminator words. */
11606 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11607 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11610 /* Add a new entry to .debug_ranges. Return its index into
11611 ranges_table vector. */
11613 static unsigned int
11614 add_ranges_num (int num, bool maybe_new_sec)
11616 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11617 vec_safe_push (ranges_table, r);
11618 return vec_safe_length (ranges_table) - 1;
11621 /* Add a new entry to .debug_ranges corresponding to a block, or a
11622 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11623 this entry might be in a different section from previous range. */
11625 static unsigned int
11626 add_ranges (const_tree block, bool maybe_new_sec)
11628 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11631 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11632 chain, or middle entry of a chain that will be directly referred to. */
11634 static void
11635 note_rnglist_head (unsigned int offset)
11637 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11638 return;
11639 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11642 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11643 When using dwarf_split_debug_info, address attributes in dies destined
11644 for the final executable should be direct references--setting the
11645 parameter force_direct ensures this behavior. */
11647 static void
11648 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11649 bool *added, bool force_direct)
11651 unsigned int in_use = vec_safe_length (ranges_by_label);
11652 unsigned int offset;
11653 dw_ranges_by_label rbl = { begin, end };
11654 vec_safe_push (ranges_by_label, rbl);
11655 offset = add_ranges_num (-(int)in_use - 1, true);
11656 if (!*added)
11658 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11659 *added = true;
11660 note_rnglist_head (offset);
11664 /* Emit .debug_ranges section. */
11666 static void
11667 output_ranges (void)
11669 unsigned i;
11670 static const char *const start_fmt = "Offset %#x";
11671 const char *fmt = start_fmt;
11672 dw_ranges *r;
11674 switch_to_section (debug_ranges_section);
11675 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11676 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11678 int block_num = r->num;
11680 if (block_num > 0)
11682 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11683 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11685 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11686 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11688 /* If all code is in the text section, then the compilation
11689 unit base address defaults to DW_AT_low_pc, which is the
11690 base of the text section. */
11691 if (!have_multiple_function_sections)
11693 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11694 text_section_label,
11695 fmt, i * 2 * DWARF2_ADDR_SIZE);
11696 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11697 text_section_label, NULL);
11700 /* Otherwise, the compilation unit base address is zero,
11701 which allows us to use absolute addresses, and not worry
11702 about whether the target supports cross-section
11703 arithmetic. */
11704 else
11706 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11707 fmt, i * 2 * DWARF2_ADDR_SIZE);
11708 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11711 fmt = NULL;
11714 /* Negative block_num stands for an index into ranges_by_label. */
11715 else if (block_num < 0)
11717 int lab_idx = - block_num - 1;
11719 if (!have_multiple_function_sections)
11721 gcc_unreachable ();
11722 #if 0
11723 /* If we ever use add_ranges_by_labels () for a single
11724 function section, all we have to do is to take out
11725 the #if 0 above. */
11726 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11727 (*ranges_by_label)[lab_idx].begin,
11728 text_section_label,
11729 fmt, i * 2 * DWARF2_ADDR_SIZE);
11730 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11731 (*ranges_by_label)[lab_idx].end,
11732 text_section_label, NULL);
11733 #endif
11735 else
11737 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11738 (*ranges_by_label)[lab_idx].begin,
11739 fmt, i * 2 * DWARF2_ADDR_SIZE);
11740 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11741 (*ranges_by_label)[lab_idx].end,
11742 NULL);
11745 else
11747 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11748 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11749 fmt = start_fmt;
11754 /* Non-zero if .debug_line_str should be used for .debug_line section
11755 strings or strings that are likely shareable with those. */
11756 #define DWARF5_USE_DEBUG_LINE_STR \
11757 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11758 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11759 /* FIXME: there is no .debug_line_str.dwo section, \
11760 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11761 && !dwarf_split_debug_info)
11763 /* Assign .debug_rnglists indexes. */
11765 static void
11766 index_rnglists (void)
11768 unsigned i;
11769 dw_ranges *r;
11771 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11772 if (r->label)
11773 r->idx = rnglist_idx++;
11776 /* Emit .debug_rnglists section. */
11778 static void
11779 output_rnglists (unsigned generation)
11781 unsigned i;
11782 dw_ranges *r;
11783 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11784 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11785 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11787 switch_to_section (debug_ranges_section);
11788 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11789 /* There are up to 4 unique ranges labels per generation.
11790 See also init_sections_and_labels. */
11791 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11792 2 + generation * 4);
11793 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11794 3 + generation * 4);
11795 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11796 dw2_asm_output_data (4, 0xffffffff,
11797 "Initial length escape value indicating "
11798 "64-bit DWARF extension");
11799 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11800 "Length of Range Lists");
11801 ASM_OUTPUT_LABEL (asm_out_file, l1);
11802 output_dwarf_version ();
11803 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11804 dw2_asm_output_data (1, 0, "Segment Size");
11805 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11806 about relocation sizes and primarily care about the size of .debug*
11807 sections in linked shared libraries and executables, then
11808 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11809 into it are usually larger than just DW_FORM_sec_offset offsets
11810 into the .debug_rnglists section. */
11811 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11812 "Offset Entry Count");
11813 if (dwarf_split_debug_info)
11815 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11816 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11817 if (r->label)
11818 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11819 ranges_base_label, NULL);
11822 const char *lab = "";
11823 unsigned int len = vec_safe_length (ranges_table);
11824 const char *base = NULL;
11825 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11827 int block_num = r->num;
11829 if (r->label)
11831 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11832 lab = r->label;
11834 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11835 base = NULL;
11836 if (block_num > 0)
11838 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11839 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11841 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11842 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11844 if (HAVE_AS_LEB128)
11846 /* If all code is in the text section, then the compilation
11847 unit base address defaults to DW_AT_low_pc, which is the
11848 base of the text section. */
11849 if (!have_multiple_function_sections)
11851 dw2_asm_output_data (1, DW_RLE_offset_pair,
11852 "DW_RLE_offset_pair (%s)", lab);
11853 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11854 "Range begin address (%s)", lab);
11855 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11856 "Range end address (%s)", lab);
11857 continue;
11859 if (base == NULL)
11861 dw_ranges *r2 = NULL;
11862 if (i < len - 1)
11863 r2 = &(*ranges_table)[i + 1];
11864 if (r2
11865 && r2->num != 0
11866 && r2->label == NULL
11867 && !r2->maybe_new_sec)
11869 dw2_asm_output_data (1, DW_RLE_base_address,
11870 "DW_RLE_base_address (%s)", lab);
11871 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11872 "Base address (%s)", lab);
11873 strcpy (basebuf, blabel);
11874 base = basebuf;
11877 if (base)
11879 dw2_asm_output_data (1, DW_RLE_offset_pair,
11880 "DW_RLE_offset_pair (%s)", lab);
11881 dw2_asm_output_delta_uleb128 (blabel, base,
11882 "Range begin address (%s)", lab);
11883 dw2_asm_output_delta_uleb128 (elabel, base,
11884 "Range end address (%s)", lab);
11885 continue;
11887 dw2_asm_output_data (1, DW_RLE_start_length,
11888 "DW_RLE_start_length (%s)", lab);
11889 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11890 "Range begin address (%s)", lab);
11891 dw2_asm_output_delta_uleb128 (elabel, blabel,
11892 "Range length (%s)", lab);
11894 else
11896 dw2_asm_output_data (1, DW_RLE_start_end,
11897 "DW_RLE_start_end (%s)", lab);
11898 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11899 "Range begin address (%s)", lab);
11900 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11901 "Range end address (%s)", lab);
11905 /* Negative block_num stands for an index into ranges_by_label. */
11906 else if (block_num < 0)
11908 int lab_idx = - block_num - 1;
11909 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11910 const char *elabel = (*ranges_by_label)[lab_idx].end;
11912 if (!have_multiple_function_sections)
11913 gcc_unreachable ();
11914 if (HAVE_AS_LEB128)
11916 dw2_asm_output_data (1, DW_RLE_start_length,
11917 "DW_RLE_start_length (%s)", lab);
11918 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11919 "Range begin address (%s)", lab);
11920 dw2_asm_output_delta_uleb128 (elabel, blabel,
11921 "Range length (%s)", lab);
11923 else
11925 dw2_asm_output_data (1, DW_RLE_start_end,
11926 "DW_RLE_start_end (%s)", lab);
11927 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11928 "Range begin address (%s)", lab);
11929 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11930 "Range end address (%s)", lab);
11933 else
11934 dw2_asm_output_data (1, DW_RLE_end_of_list,
11935 "DW_RLE_end_of_list (%s)", lab);
11937 ASM_OUTPUT_LABEL (asm_out_file, l2);
11940 /* Data structure containing information about input files. */
11941 struct file_info
11943 const char *path; /* Complete file name. */
11944 const char *fname; /* File name part. */
11945 int length; /* Length of entire string. */
11946 struct dwarf_file_data * file_idx; /* Index in input file table. */
11947 int dir_idx; /* Index in directory table. */
11950 /* Data structure containing information about directories with source
11951 files. */
11952 struct dir_info
11954 const char *path; /* Path including directory name. */
11955 int length; /* Path length. */
11956 int prefix; /* Index of directory entry which is a prefix. */
11957 int count; /* Number of files in this directory. */
11958 int dir_idx; /* Index of directory used as base. */
11961 /* Callback function for file_info comparison. We sort by looking at
11962 the directories in the path. */
11964 static int
11965 file_info_cmp (const void *p1, const void *p2)
11967 const struct file_info *const s1 = (const struct file_info *) p1;
11968 const struct file_info *const s2 = (const struct file_info *) p2;
11969 const unsigned char *cp1;
11970 const unsigned char *cp2;
11972 /* Take care of file names without directories. We need to make sure that
11973 we return consistent values to qsort since some will get confused if
11974 we return the same value when identical operands are passed in opposite
11975 orders. So if neither has a directory, return 0 and otherwise return
11976 1 or -1 depending on which one has the directory. We want the one with
11977 the directory to sort after the one without, so all no directory files
11978 are at the start (normally only the compilation unit file). */
11979 if ((s1->path == s1->fname || s2->path == s2->fname))
11980 return (s2->path == s2->fname) - (s1->path == s1->fname);
11982 cp1 = (const unsigned char *) s1->path;
11983 cp2 = (const unsigned char *) s2->path;
11985 while (1)
11987 ++cp1;
11988 ++cp2;
11989 /* Reached the end of the first path? If so, handle like above,
11990 but now we want longer directory prefixes before shorter ones. */
11991 if ((cp1 == (const unsigned char *) s1->fname)
11992 || (cp2 == (const unsigned char *) s2->fname))
11993 return ((cp1 == (const unsigned char *) s1->fname)
11994 - (cp2 == (const unsigned char *) s2->fname));
11996 /* Character of current path component the same? */
11997 else if (*cp1 != *cp2)
11998 return *cp1 - *cp2;
12002 struct file_name_acquire_data
12004 struct file_info *files;
12005 int used_files;
12006 int max_files;
12009 /* Traversal function for the hash table. */
12012 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12014 struct dwarf_file_data *d = *slot;
12015 struct file_info *fi;
12016 const char *f;
12018 gcc_assert (fnad->max_files >= d->emitted_number);
12020 if (! d->emitted_number)
12021 return 1;
12023 gcc_assert (fnad->max_files != fnad->used_files);
12025 fi = fnad->files + fnad->used_files++;
12027 /* Skip all leading "./". */
12028 f = d->filename;
12029 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12030 f += 2;
12032 /* Create a new array entry. */
12033 fi->path = f;
12034 fi->length = strlen (f);
12035 fi->file_idx = d;
12037 /* Search for the file name part. */
12038 f = strrchr (f, DIR_SEPARATOR);
12039 #if defined (DIR_SEPARATOR_2)
12041 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12043 if (g != NULL)
12045 if (f == NULL || f < g)
12046 f = g;
12049 #endif
12051 fi->fname = f == NULL ? fi->path : f + 1;
12052 return 1;
12055 /* Helper function for output_file_names. Emit a FORM encoded
12056 string STR, with assembly comment start ENTRY_KIND and
12057 index IDX */
12059 static void
12060 output_line_string (enum dwarf_form form, const char *str,
12061 const char *entry_kind, unsigned int idx)
12063 switch (form)
12065 case DW_FORM_string:
12066 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12067 break;
12068 case DW_FORM_line_strp:
12069 if (!debug_line_str_hash)
12070 debug_line_str_hash
12071 = hash_table<indirect_string_hasher>::create_ggc (10);
12073 struct indirect_string_node *node;
12074 node = find_AT_string_in_table (str, debug_line_str_hash);
12075 set_indirect_string (node);
12076 node->form = form;
12077 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12078 debug_line_str_section, "%s: %#x: \"%s\"",
12079 entry_kind, 0, node->str);
12080 break;
12081 default:
12082 gcc_unreachable ();
12086 /* Output the directory table and the file name table. We try to minimize
12087 the total amount of memory needed. A heuristic is used to avoid large
12088 slowdowns with many input files. */
12090 static void
12091 output_file_names (void)
12093 struct file_name_acquire_data fnad;
12094 int numfiles;
12095 struct file_info *files;
12096 struct dir_info *dirs;
12097 int *saved;
12098 int *savehere;
12099 int *backmap;
12100 int ndirs;
12101 int idx_offset;
12102 int i;
12104 if (!last_emitted_file)
12106 if (dwarf_version >= 5)
12108 dw2_asm_output_data (1, 0, "Directory entry format count");
12109 dw2_asm_output_data_uleb128 (0, "Directories count");
12110 dw2_asm_output_data (1, 0, "File name entry format count");
12111 dw2_asm_output_data_uleb128 (0, "File names count");
12113 else
12115 dw2_asm_output_data (1, 0, "End directory table");
12116 dw2_asm_output_data (1, 0, "End file name table");
12118 return;
12121 numfiles = last_emitted_file->emitted_number;
12123 /* Allocate the various arrays we need. */
12124 files = XALLOCAVEC (struct file_info, numfiles);
12125 dirs = XALLOCAVEC (struct dir_info, numfiles);
12127 fnad.files = files;
12128 fnad.used_files = 0;
12129 fnad.max_files = numfiles;
12130 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12131 gcc_assert (fnad.used_files == fnad.max_files);
12133 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12135 /* Find all the different directories used. */
12136 dirs[0].path = files[0].path;
12137 dirs[0].length = files[0].fname - files[0].path;
12138 dirs[0].prefix = -1;
12139 dirs[0].count = 1;
12140 dirs[0].dir_idx = 0;
12141 files[0].dir_idx = 0;
12142 ndirs = 1;
12144 for (i = 1; i < numfiles; i++)
12145 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12146 && memcmp (dirs[ndirs - 1].path, files[i].path,
12147 dirs[ndirs - 1].length) == 0)
12149 /* Same directory as last entry. */
12150 files[i].dir_idx = ndirs - 1;
12151 ++dirs[ndirs - 1].count;
12153 else
12155 int j;
12157 /* This is a new directory. */
12158 dirs[ndirs].path = files[i].path;
12159 dirs[ndirs].length = files[i].fname - files[i].path;
12160 dirs[ndirs].count = 1;
12161 dirs[ndirs].dir_idx = ndirs;
12162 files[i].dir_idx = ndirs;
12164 /* Search for a prefix. */
12165 dirs[ndirs].prefix = -1;
12166 for (j = 0; j < ndirs; j++)
12167 if (dirs[j].length < dirs[ndirs].length
12168 && dirs[j].length > 1
12169 && (dirs[ndirs].prefix == -1
12170 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12171 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12172 dirs[ndirs].prefix = j;
12174 ++ndirs;
12177 /* Now to the actual work. We have to find a subset of the directories which
12178 allow expressing the file name using references to the directory table
12179 with the least amount of characters. We do not do an exhaustive search
12180 where we would have to check out every combination of every single
12181 possible prefix. Instead we use a heuristic which provides nearly optimal
12182 results in most cases and never is much off. */
12183 saved = XALLOCAVEC (int, ndirs);
12184 savehere = XALLOCAVEC (int, ndirs);
12186 memset (saved, '\0', ndirs * sizeof (saved[0]));
12187 for (i = 0; i < ndirs; i++)
12189 int j;
12190 int total;
12192 /* We can always save some space for the current directory. But this
12193 does not mean it will be enough to justify adding the directory. */
12194 savehere[i] = dirs[i].length;
12195 total = (savehere[i] - saved[i]) * dirs[i].count;
12197 for (j = i + 1; j < ndirs; j++)
12199 savehere[j] = 0;
12200 if (saved[j] < dirs[i].length)
12202 /* Determine whether the dirs[i] path is a prefix of the
12203 dirs[j] path. */
12204 int k;
12206 k = dirs[j].prefix;
12207 while (k != -1 && k != (int) i)
12208 k = dirs[k].prefix;
12210 if (k == (int) i)
12212 /* Yes it is. We can possibly save some memory by
12213 writing the filenames in dirs[j] relative to
12214 dirs[i]. */
12215 savehere[j] = dirs[i].length;
12216 total += (savehere[j] - saved[j]) * dirs[j].count;
12221 /* Check whether we can save enough to justify adding the dirs[i]
12222 directory. */
12223 if (total > dirs[i].length + 1)
12225 /* It's worthwhile adding. */
12226 for (j = i; j < ndirs; j++)
12227 if (savehere[j] > 0)
12229 /* Remember how much we saved for this directory so far. */
12230 saved[j] = savehere[j];
12232 /* Remember the prefix directory. */
12233 dirs[j].dir_idx = i;
12238 /* Emit the directory name table. */
12239 idx_offset = dirs[0].length > 0 ? 1 : 0;
12240 enum dwarf_form str_form = DW_FORM_string;
12241 enum dwarf_form idx_form = DW_FORM_udata;
12242 if (dwarf_version >= 5)
12244 const char *comp_dir = comp_dir_string ();
12245 if (comp_dir == NULL)
12246 comp_dir = "";
12247 dw2_asm_output_data (1, 1, "Directory entry format count");
12248 if (DWARF5_USE_DEBUG_LINE_STR)
12249 str_form = DW_FORM_line_strp;
12250 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12251 dw2_asm_output_data_uleb128 (str_form, "%s",
12252 get_DW_FORM_name (str_form));
12253 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12254 if (str_form == DW_FORM_string)
12256 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12257 for (i = 1 - idx_offset; i < ndirs; i++)
12258 dw2_asm_output_nstring (dirs[i].path,
12259 dirs[i].length
12260 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12261 "Directory Entry: %#x", i + idx_offset);
12263 else
12265 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12266 for (i = 1 - idx_offset; i < ndirs; i++)
12268 const char *str
12269 = ggc_alloc_string (dirs[i].path,
12270 dirs[i].length
12271 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12272 output_line_string (str_form, str, "Directory Entry",
12273 (unsigned) i + idx_offset);
12277 else
12279 for (i = 1 - idx_offset; i < ndirs; i++)
12280 dw2_asm_output_nstring (dirs[i].path,
12281 dirs[i].length
12282 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12283 "Directory Entry: %#x", i + idx_offset);
12285 dw2_asm_output_data (1, 0, "End directory table");
12288 /* We have to emit them in the order of emitted_number since that's
12289 used in the debug info generation. To do this efficiently we
12290 generate a back-mapping of the indices first. */
12291 backmap = XALLOCAVEC (int, numfiles);
12292 for (i = 0; i < numfiles; i++)
12293 backmap[files[i].file_idx->emitted_number - 1] = i;
12295 if (dwarf_version >= 5)
12297 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12298 if (filename0 == NULL)
12299 filename0 = "";
12300 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12301 DW_FORM_data2. Choose one based on the number of directories
12302 and how much space would they occupy in each encoding.
12303 If we have at most 256 directories, all indexes fit into
12304 a single byte, so DW_FORM_data1 is most compact (if there
12305 are at most 128 directories, DW_FORM_udata would be as
12306 compact as that, but not shorter and slower to decode). */
12307 if (ndirs + idx_offset <= 256)
12308 idx_form = DW_FORM_data1;
12309 /* If there are more than 65536 directories, we have to use
12310 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12311 Otherwise, compute what space would occupy if all the indexes
12312 used DW_FORM_udata - sum - and compare that to how large would
12313 be DW_FORM_data2 encoding, and pick the more efficient one. */
12314 else if (ndirs + idx_offset <= 65536)
12316 unsigned HOST_WIDE_INT sum = 1;
12317 for (i = 0; i < numfiles; i++)
12319 int file_idx = backmap[i];
12320 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12321 sum += size_of_uleb128 (dir_idx);
12323 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12324 idx_form = DW_FORM_data2;
12326 #ifdef VMS_DEBUGGING_INFO
12327 dw2_asm_output_data (1, 4, "File name entry format count");
12328 #else
12329 dw2_asm_output_data (1, 2, "File name entry format count");
12330 #endif
12331 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12332 dw2_asm_output_data_uleb128 (str_form, "%s",
12333 get_DW_FORM_name (str_form));
12334 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12335 "DW_LNCT_directory_index");
12336 dw2_asm_output_data_uleb128 (idx_form, "%s",
12337 get_DW_FORM_name (idx_form));
12338 #ifdef VMS_DEBUGGING_INFO
12339 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12340 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12341 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12342 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12343 #endif
12344 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12346 output_line_string (str_form, filename0, "File Entry", 0);
12348 /* Include directory index. */
12349 if (idx_form != DW_FORM_udata)
12350 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12351 0, NULL);
12352 else
12353 dw2_asm_output_data_uleb128 (0, NULL);
12355 #ifdef VMS_DEBUGGING_INFO
12356 dw2_asm_output_data_uleb128 (0, NULL);
12357 dw2_asm_output_data_uleb128 (0, NULL);
12358 #endif
12361 /* Now write all the file names. */
12362 for (i = 0; i < numfiles; i++)
12364 int file_idx = backmap[i];
12365 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12367 #ifdef VMS_DEBUGGING_INFO
12368 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12370 /* Setting these fields can lead to debugger miscomparisons,
12371 but VMS Debug requires them to be set correctly. */
12373 int ver;
12374 long long cdt;
12375 long siz;
12376 int maxfilelen = (strlen (files[file_idx].path)
12377 + dirs[dir_idx].length
12378 + MAX_VMS_VERSION_LEN + 1);
12379 char *filebuf = XALLOCAVEC (char, maxfilelen);
12381 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12382 snprintf (filebuf, maxfilelen, "%s;%d",
12383 files[file_idx].path + dirs[dir_idx].length, ver);
12385 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12387 /* Include directory index. */
12388 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12389 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12390 dir_idx + idx_offset, NULL);
12391 else
12392 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12394 /* Modification time. */
12395 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12396 &cdt, 0, 0, 0) == 0)
12397 ? cdt : 0, NULL);
12399 /* File length in bytes. */
12400 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12401 0, &siz, 0, 0) == 0)
12402 ? siz : 0, NULL);
12403 #else
12404 output_line_string (str_form,
12405 files[file_idx].path + dirs[dir_idx].length,
12406 "File Entry", (unsigned) i + 1);
12408 /* Include directory index. */
12409 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12410 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12411 dir_idx + idx_offset, NULL);
12412 else
12413 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12415 if (dwarf_version >= 5)
12416 continue;
12418 /* Modification time. */
12419 dw2_asm_output_data_uleb128 (0, NULL);
12421 /* File length in bytes. */
12422 dw2_asm_output_data_uleb128 (0, NULL);
12423 #endif /* VMS_DEBUGGING_INFO */
12426 if (dwarf_version < 5)
12427 dw2_asm_output_data (1, 0, "End file name table");
12431 /* Output one line number table into the .debug_line section. */
12433 static void
12434 output_one_line_info_table (dw_line_info_table *table)
12436 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12437 unsigned int current_line = 1;
12438 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12439 dw_line_info_entry *ent, *prev_addr;
12440 size_t i;
12441 unsigned int view;
12443 view = 0;
12445 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12447 switch (ent->opcode)
12449 case LI_set_address:
12450 /* ??? Unfortunately, we have little choice here currently, and
12451 must always use the most general form. GCC does not know the
12452 address delta itself, so we can't use DW_LNS_advance_pc. Many
12453 ports do have length attributes which will give an upper bound
12454 on the address range. We could perhaps use length attributes
12455 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12456 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12458 view = 0;
12460 /* This can handle any delta. This takes
12461 4+DWARF2_ADDR_SIZE bytes. */
12462 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12463 debug_variable_location_views
12464 ? ", reset view to 0" : "");
12465 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12466 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12467 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12469 prev_addr = ent;
12470 break;
12472 case LI_adv_address:
12474 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12475 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12476 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12478 view++;
12480 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12481 dw2_asm_output_delta (2, line_label, prev_label,
12482 "from %s to %s", prev_label, line_label);
12484 prev_addr = ent;
12485 break;
12488 case LI_set_line:
12489 if (ent->val == current_line)
12491 /* We still need to start a new row, so output a copy insn. */
12492 dw2_asm_output_data (1, DW_LNS_copy,
12493 "copy line %u", current_line);
12495 else
12497 int line_offset = ent->val - current_line;
12498 int line_delta = line_offset - DWARF_LINE_BASE;
12500 current_line = ent->val;
12501 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12503 /* This can handle deltas from -10 to 234, using the current
12504 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12505 This takes 1 byte. */
12506 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12507 "line %u", current_line);
12509 else
12511 /* This can handle any delta. This takes at least 4 bytes,
12512 depending on the value being encoded. */
12513 dw2_asm_output_data (1, DW_LNS_advance_line,
12514 "advance to line %u", current_line);
12515 dw2_asm_output_data_sleb128 (line_offset, NULL);
12516 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12519 break;
12521 case LI_set_file:
12522 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12523 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12524 break;
12526 case LI_set_column:
12527 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12528 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12529 break;
12531 case LI_negate_stmt:
12532 current_is_stmt = !current_is_stmt;
12533 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12534 "is_stmt %d", current_is_stmt);
12535 break;
12537 case LI_set_prologue_end:
12538 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12539 "set prologue end");
12540 break;
12542 case LI_set_epilogue_begin:
12543 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12544 "set epilogue begin");
12545 break;
12547 case LI_set_discriminator:
12548 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12549 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12550 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12551 dw2_asm_output_data_uleb128 (ent->val, NULL);
12552 break;
12556 /* Emit debug info for the address of the end of the table. */
12557 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12558 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12559 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12560 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12562 dw2_asm_output_data (1, 0, "end sequence");
12563 dw2_asm_output_data_uleb128 (1, NULL);
12564 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12567 /* Output the source line number correspondence information. This
12568 information goes into the .debug_line section. */
12570 static void
12571 output_line_info (bool prologue_only)
12573 static unsigned int generation;
12574 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12575 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12576 bool saw_one = false;
12577 int opc;
12579 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12580 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12581 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12582 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12584 if (!XCOFF_DEBUGGING_INFO)
12586 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12587 dw2_asm_output_data (4, 0xffffffff,
12588 "Initial length escape value indicating 64-bit DWARF extension");
12589 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12590 "Length of Source Line Info");
12593 ASM_OUTPUT_LABEL (asm_out_file, l1);
12595 output_dwarf_version ();
12596 if (dwarf_version >= 5)
12598 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12599 dw2_asm_output_data (1, 0, "Segment Size");
12601 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12602 ASM_OUTPUT_LABEL (asm_out_file, p1);
12604 /* Define the architecture-dependent minimum instruction length (in bytes).
12605 In this implementation of DWARF, this field is used for information
12606 purposes only. Since GCC generates assembly language, we have no
12607 a priori knowledge of how many instruction bytes are generated for each
12608 source line, and therefore can use only the DW_LNE_set_address and
12609 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12610 this as '1', which is "correct enough" for all architectures,
12611 and don't let the target override. */
12612 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12614 if (dwarf_version >= 4)
12615 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12616 "Maximum Operations Per Instruction");
12617 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12618 "Default is_stmt_start flag");
12619 dw2_asm_output_data (1, DWARF_LINE_BASE,
12620 "Line Base Value (Special Opcodes)");
12621 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12622 "Line Range Value (Special Opcodes)");
12623 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12624 "Special Opcode Base");
12626 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12628 int n_op_args;
12629 switch (opc)
12631 case DW_LNS_advance_pc:
12632 case DW_LNS_advance_line:
12633 case DW_LNS_set_file:
12634 case DW_LNS_set_column:
12635 case DW_LNS_fixed_advance_pc:
12636 case DW_LNS_set_isa:
12637 n_op_args = 1;
12638 break;
12639 default:
12640 n_op_args = 0;
12641 break;
12644 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12645 opc, n_op_args);
12648 /* Write out the information about the files we use. */
12649 output_file_names ();
12650 ASM_OUTPUT_LABEL (asm_out_file, p2);
12651 if (prologue_only)
12653 /* Output the marker for the end of the line number info. */
12654 ASM_OUTPUT_LABEL (asm_out_file, l2);
12655 return;
12658 if (separate_line_info)
12660 dw_line_info_table *table;
12661 size_t i;
12663 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12664 if (table->in_use)
12666 output_one_line_info_table (table);
12667 saw_one = true;
12670 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12672 output_one_line_info_table (cold_text_section_line_info);
12673 saw_one = true;
12676 /* ??? Some Darwin linkers crash on a .debug_line section with no
12677 sequences. Further, merely a DW_LNE_end_sequence entry is not
12678 sufficient -- the address column must also be initialized.
12679 Make sure to output at least one set_address/end_sequence pair,
12680 choosing .text since that section is always present. */
12681 if (text_section_line_info->in_use || !saw_one)
12682 output_one_line_info_table (text_section_line_info);
12684 /* Output the marker for the end of the line number info. */
12685 ASM_OUTPUT_LABEL (asm_out_file, l2);
12688 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12690 static inline bool
12691 need_endianity_attribute_p (bool reverse)
12693 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12696 /* Given a pointer to a tree node for some base type, return a pointer to
12697 a DIE that describes the given type. REVERSE is true if the type is
12698 to be interpreted in the reverse storage order wrt the target order.
12700 This routine must only be called for GCC type nodes that correspond to
12701 Dwarf base (fundamental) types. */
12703 static dw_die_ref
12704 base_type_die (tree type, bool reverse)
12706 dw_die_ref base_type_result;
12707 enum dwarf_type encoding;
12708 bool fpt_used = false;
12709 struct fixed_point_type_info fpt_info;
12710 tree type_bias = NULL_TREE;
12712 /* If this is a subtype that should not be emitted as a subrange type,
12713 use the base type. See subrange_type_for_debug_p. */
12714 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12715 type = TREE_TYPE (type);
12717 switch (TREE_CODE (type))
12719 case INTEGER_TYPE:
12720 if ((dwarf_version >= 4 || !dwarf_strict)
12721 && TYPE_NAME (type)
12722 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12723 && DECL_IS_BUILTIN (TYPE_NAME (type))
12724 && DECL_NAME (TYPE_NAME (type)))
12726 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12727 if (strcmp (name, "char16_t") == 0
12728 || strcmp (name, "char32_t") == 0)
12730 encoding = DW_ATE_UTF;
12731 break;
12734 if ((dwarf_version >= 3 || !dwarf_strict)
12735 && lang_hooks.types.get_fixed_point_type_info)
12737 memset (&fpt_info, 0, sizeof (fpt_info));
12738 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12740 fpt_used = true;
12741 encoding = ((TYPE_UNSIGNED (type))
12742 ? DW_ATE_unsigned_fixed
12743 : DW_ATE_signed_fixed);
12744 break;
12747 if (TYPE_STRING_FLAG (type))
12749 if (TYPE_UNSIGNED (type))
12750 encoding = DW_ATE_unsigned_char;
12751 else
12752 encoding = DW_ATE_signed_char;
12754 else if (TYPE_UNSIGNED (type))
12755 encoding = DW_ATE_unsigned;
12756 else
12757 encoding = DW_ATE_signed;
12759 if (!dwarf_strict
12760 && lang_hooks.types.get_type_bias)
12761 type_bias = lang_hooks.types.get_type_bias (type);
12762 break;
12764 case REAL_TYPE:
12765 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12767 if (dwarf_version >= 3 || !dwarf_strict)
12768 encoding = DW_ATE_decimal_float;
12769 else
12770 encoding = DW_ATE_lo_user;
12772 else
12773 encoding = DW_ATE_float;
12774 break;
12776 case FIXED_POINT_TYPE:
12777 if (!(dwarf_version >= 3 || !dwarf_strict))
12778 encoding = DW_ATE_lo_user;
12779 else if (TYPE_UNSIGNED (type))
12780 encoding = DW_ATE_unsigned_fixed;
12781 else
12782 encoding = DW_ATE_signed_fixed;
12783 break;
12785 /* Dwarf2 doesn't know anything about complex ints, so use
12786 a user defined type for it. */
12787 case COMPLEX_TYPE:
12788 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12789 encoding = DW_ATE_complex_float;
12790 else
12791 encoding = DW_ATE_lo_user;
12792 break;
12794 case BOOLEAN_TYPE:
12795 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12796 encoding = DW_ATE_boolean;
12797 break;
12799 default:
12800 /* No other TREE_CODEs are Dwarf fundamental types. */
12801 gcc_unreachable ();
12804 base_type_result = new_die_raw (DW_TAG_base_type);
12806 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12807 int_size_in_bytes (type));
12808 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12810 if (need_endianity_attribute_p (reverse))
12811 add_AT_unsigned (base_type_result, DW_AT_endianity,
12812 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12814 add_alignment_attribute (base_type_result, type);
12816 if (fpt_used)
12818 switch (fpt_info.scale_factor_kind)
12820 case fixed_point_scale_factor_binary:
12821 add_AT_int (base_type_result, DW_AT_binary_scale,
12822 fpt_info.scale_factor.binary);
12823 break;
12825 case fixed_point_scale_factor_decimal:
12826 add_AT_int (base_type_result, DW_AT_decimal_scale,
12827 fpt_info.scale_factor.decimal);
12828 break;
12830 case fixed_point_scale_factor_arbitrary:
12831 /* Arbitrary scale factors cannot be described in standard DWARF,
12832 yet. */
12833 if (!dwarf_strict)
12835 /* Describe the scale factor as a rational constant. */
12836 const dw_die_ref scale_factor
12837 = new_die (DW_TAG_constant, comp_unit_die (), type);
12839 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12840 fpt_info.scale_factor.arbitrary.numerator);
12841 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12842 fpt_info.scale_factor.arbitrary.denominator);
12844 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12846 break;
12848 default:
12849 gcc_unreachable ();
12853 if (type_bias)
12854 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12855 dw_scalar_form_constant
12856 | dw_scalar_form_exprloc
12857 | dw_scalar_form_reference,
12858 NULL);
12860 return base_type_result;
12863 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12864 named 'auto' in its type: return true for it, false otherwise. */
12866 static inline bool
12867 is_cxx_auto (tree type)
12869 if (is_cxx ())
12871 tree name = TYPE_IDENTIFIER (type);
12872 if (name == get_identifier ("auto")
12873 || name == get_identifier ("decltype(auto)"))
12874 return true;
12876 return false;
12879 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12880 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12882 static inline int
12883 is_base_type (tree type)
12885 switch (TREE_CODE (type))
12887 case INTEGER_TYPE:
12888 case REAL_TYPE:
12889 case FIXED_POINT_TYPE:
12890 case COMPLEX_TYPE:
12891 case BOOLEAN_TYPE:
12892 return 1;
12894 case VOID_TYPE:
12895 case ARRAY_TYPE:
12896 case RECORD_TYPE:
12897 case UNION_TYPE:
12898 case QUAL_UNION_TYPE:
12899 case ENUMERAL_TYPE:
12900 case FUNCTION_TYPE:
12901 case METHOD_TYPE:
12902 case POINTER_TYPE:
12903 case REFERENCE_TYPE:
12904 case NULLPTR_TYPE:
12905 case OFFSET_TYPE:
12906 case LANG_TYPE:
12907 case VECTOR_TYPE:
12908 return 0;
12910 default:
12911 if (is_cxx_auto (type))
12912 return 0;
12913 gcc_unreachable ();
12916 return 0;
12919 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12920 node, return the size in bits for the type if it is a constant, or else
12921 return the alignment for the type if the type's size is not constant, or
12922 else return BITS_PER_WORD if the type actually turns out to be an
12923 ERROR_MARK node. */
12925 static inline unsigned HOST_WIDE_INT
12926 simple_type_size_in_bits (const_tree type)
12928 if (TREE_CODE (type) == ERROR_MARK)
12929 return BITS_PER_WORD;
12930 else if (TYPE_SIZE (type) == NULL_TREE)
12931 return 0;
12932 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12933 return tree_to_uhwi (TYPE_SIZE (type));
12934 else
12935 return TYPE_ALIGN (type);
12938 /* Similarly, but return an offset_int instead of UHWI. */
12940 static inline offset_int
12941 offset_int_type_size_in_bits (const_tree type)
12943 if (TREE_CODE (type) == ERROR_MARK)
12944 return BITS_PER_WORD;
12945 else if (TYPE_SIZE (type) == NULL_TREE)
12946 return 0;
12947 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12948 return wi::to_offset (TYPE_SIZE (type));
12949 else
12950 return TYPE_ALIGN (type);
12953 /* Given a pointer to a tree node for a subrange type, return a pointer
12954 to a DIE that describes the given type. */
12956 static dw_die_ref
12957 subrange_type_die (tree type, tree low, tree high, tree bias,
12958 dw_die_ref context_die)
12960 dw_die_ref subrange_die;
12961 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12963 if (context_die == NULL)
12964 context_die = comp_unit_die ();
12966 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12968 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12970 /* The size of the subrange type and its base type do not match,
12971 so we need to generate a size attribute for the subrange type. */
12972 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12975 add_alignment_attribute (subrange_die, type);
12977 if (low)
12978 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12979 if (high)
12980 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12981 if (bias && !dwarf_strict)
12982 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12983 dw_scalar_form_constant
12984 | dw_scalar_form_exprloc
12985 | dw_scalar_form_reference,
12986 NULL);
12988 return subrange_die;
12991 /* Returns the (const and/or volatile) cv_qualifiers associated with
12992 the decl node. This will normally be augmented with the
12993 cv_qualifiers of the underlying type in add_type_attribute. */
12995 static int
12996 decl_quals (const_tree decl)
12998 return ((TREE_READONLY (decl)
12999 /* The C++ front-end correctly marks reference-typed
13000 variables as readonly, but from a language (and debug
13001 info) standpoint they are not const-qualified. */
13002 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
13003 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
13004 | (TREE_THIS_VOLATILE (decl)
13005 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
13008 /* Determine the TYPE whose qualifiers match the largest strict subset
13009 of the given TYPE_QUALS, and return its qualifiers. Ignore all
13010 qualifiers outside QUAL_MASK. */
13012 static int
13013 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13015 tree t;
13016 int best_rank = 0, best_qual = 0, max_rank;
13018 type_quals &= qual_mask;
13019 max_rank = popcount_hwi (type_quals) - 1;
13021 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13022 t = TYPE_NEXT_VARIANT (t))
13024 int q = TYPE_QUALS (t) & qual_mask;
13026 if ((q & type_quals) == q && q != type_quals
13027 && check_base_type (t, type))
13029 int rank = popcount_hwi (q);
13031 if (rank > best_rank)
13033 best_rank = rank;
13034 best_qual = q;
13039 return best_qual;
13042 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13043 static const dwarf_qual_info_t dwarf_qual_info[] =
13045 { TYPE_QUAL_CONST, DW_TAG_const_type },
13046 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13047 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13048 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13050 static const unsigned int dwarf_qual_info_size
13051 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13053 /* If DIE is a qualified DIE of some base DIE with the same parent,
13054 return the base DIE, otherwise return NULL. Set MASK to the
13055 qualifiers added compared to the returned DIE. */
13057 static dw_die_ref
13058 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13060 unsigned int i;
13061 for (i = 0; i < dwarf_qual_info_size; i++)
13062 if (die->die_tag == dwarf_qual_info[i].t)
13063 break;
13064 if (i == dwarf_qual_info_size)
13065 return NULL;
13066 if (vec_safe_length (die->die_attr) != 1)
13067 return NULL;
13068 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13069 if (type == NULL || type->die_parent != die->die_parent)
13070 return NULL;
13071 *mask |= dwarf_qual_info[i].q;
13072 if (depth)
13074 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13075 if (ret)
13076 return ret;
13078 return type;
13081 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13082 entry that chains the modifiers specified by CV_QUALS in front of the
13083 given type. REVERSE is true if the type is to be interpreted in the
13084 reverse storage order wrt the target order. */
13086 static dw_die_ref
13087 modified_type_die (tree type, int cv_quals, bool reverse,
13088 dw_die_ref context_die)
13090 enum tree_code code = TREE_CODE (type);
13091 dw_die_ref mod_type_die;
13092 dw_die_ref sub_die = NULL;
13093 tree item_type = NULL;
13094 tree qualified_type;
13095 tree name, low, high;
13096 dw_die_ref mod_scope;
13097 /* Only these cv-qualifiers are currently handled. */
13098 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13099 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13100 ENCODE_QUAL_ADDR_SPACE(~0U));
13101 const bool reverse_base_type
13102 = need_endianity_attribute_p (reverse) && is_base_type (type);
13104 if (code == ERROR_MARK)
13105 return NULL;
13107 if (lang_hooks.types.get_debug_type)
13109 tree debug_type = lang_hooks.types.get_debug_type (type);
13111 if (debug_type != NULL_TREE && debug_type != type)
13112 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13115 cv_quals &= cv_qual_mask;
13117 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13118 tag modifier (and not an attribute) old consumers won't be able
13119 to handle it. */
13120 if (dwarf_version < 3)
13121 cv_quals &= ~TYPE_QUAL_RESTRICT;
13123 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13124 if (dwarf_version < 5)
13125 cv_quals &= ~TYPE_QUAL_ATOMIC;
13127 /* See if we already have the appropriately qualified variant of
13128 this type. */
13129 qualified_type = get_qualified_type (type, cv_quals);
13131 if (qualified_type == sizetype)
13133 /* Try not to expose the internal sizetype type's name. */
13134 if (TYPE_NAME (qualified_type)
13135 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13137 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13139 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13140 && (TYPE_PRECISION (t)
13141 == TYPE_PRECISION (qualified_type))
13142 && (TYPE_UNSIGNED (t)
13143 == TYPE_UNSIGNED (qualified_type)));
13144 qualified_type = t;
13146 else if (qualified_type == sizetype
13147 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13148 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13149 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13150 qualified_type = size_type_node;
13153 /* If we do, then we can just use its DIE, if it exists. */
13154 if (qualified_type)
13156 mod_type_die = lookup_type_die (qualified_type);
13158 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13159 dealt with specially: the DIE with the attribute, if it exists, is
13160 placed immediately after the regular DIE for the same base type. */
13161 if (mod_type_die
13162 && (!reverse_base_type
13163 || ((mod_type_die = mod_type_die->die_sib) != NULL
13164 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13165 return mod_type_die;
13168 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13170 /* Handle C typedef types. */
13171 if (name
13172 && TREE_CODE (name) == TYPE_DECL
13173 && DECL_ORIGINAL_TYPE (name)
13174 && !DECL_ARTIFICIAL (name))
13176 tree dtype = TREE_TYPE (name);
13178 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13179 if (qualified_type == dtype && !reverse_base_type)
13181 tree origin = decl_ultimate_origin (name);
13183 /* Typedef variants that have an abstract origin don't get their own
13184 type DIE (see gen_typedef_die), so fall back on the ultimate
13185 abstract origin instead. */
13186 if (origin != NULL && origin != name)
13187 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13188 context_die);
13190 /* For a named type, use the typedef. */
13191 gen_type_die (qualified_type, context_die);
13192 return lookup_type_die (qualified_type);
13194 else
13196 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13197 dquals &= cv_qual_mask;
13198 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13199 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13200 /* cv-unqualified version of named type. Just use
13201 the unnamed type to which it refers. */
13202 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13203 reverse, context_die);
13204 /* Else cv-qualified version of named type; fall through. */
13208 mod_scope = scope_die_for (type, context_die);
13210 if (cv_quals)
13212 int sub_quals = 0, first_quals = 0;
13213 unsigned i;
13214 dw_die_ref first = NULL, last = NULL;
13216 /* Determine a lesser qualified type that most closely matches
13217 this one. Then generate DW_TAG_* entries for the remaining
13218 qualifiers. */
13219 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13220 cv_qual_mask);
13221 if (sub_quals && use_debug_types)
13223 bool needed = false;
13224 /* If emitting type units, make sure the order of qualifiers
13225 is canonical. Thus, start from unqualified type if
13226 an earlier qualifier is missing in sub_quals, but some later
13227 one is present there. */
13228 for (i = 0; i < dwarf_qual_info_size; i++)
13229 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13230 needed = true;
13231 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13233 sub_quals = 0;
13234 break;
13237 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13238 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13240 /* As not all intermediate qualified DIEs have corresponding
13241 tree types, ensure that qualified DIEs in the same scope
13242 as their DW_AT_type are emitted after their DW_AT_type,
13243 only with other qualified DIEs for the same type possibly
13244 in between them. Determine the range of such qualified
13245 DIEs now (first being the base type, last being corresponding
13246 last qualified DIE for it). */
13247 unsigned int count = 0;
13248 first = qualified_die_p (mod_type_die, &first_quals,
13249 dwarf_qual_info_size);
13250 if (first == NULL)
13251 first = mod_type_die;
13252 gcc_assert ((first_quals & ~sub_quals) == 0);
13253 for (count = 0, last = first;
13254 count < (1U << dwarf_qual_info_size);
13255 count++, last = last->die_sib)
13257 int quals = 0;
13258 if (last == mod_scope->die_child)
13259 break;
13260 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13261 != first)
13262 break;
13266 for (i = 0; i < dwarf_qual_info_size; i++)
13267 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13269 dw_die_ref d;
13270 if (first && first != last)
13272 for (d = first->die_sib; ; d = d->die_sib)
13274 int quals = 0;
13275 qualified_die_p (d, &quals, dwarf_qual_info_size);
13276 if (quals == (first_quals | dwarf_qual_info[i].q))
13277 break;
13278 if (d == last)
13280 d = NULL;
13281 break;
13284 if (d)
13286 mod_type_die = d;
13287 continue;
13290 if (first)
13292 d = new_die_raw (dwarf_qual_info[i].t);
13293 add_child_die_after (mod_scope, d, last);
13294 last = d;
13296 else
13297 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13298 if (mod_type_die)
13299 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13300 mod_type_die = d;
13301 first_quals |= dwarf_qual_info[i].q;
13304 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13306 dwarf_tag tag = DW_TAG_pointer_type;
13307 if (code == REFERENCE_TYPE)
13309 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13310 tag = DW_TAG_rvalue_reference_type;
13311 else
13312 tag = DW_TAG_reference_type;
13314 mod_type_die = new_die (tag, mod_scope, type);
13316 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13317 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13318 add_alignment_attribute (mod_type_die, type);
13319 item_type = TREE_TYPE (type);
13321 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13322 if (!ADDR_SPACE_GENERIC_P (as))
13324 int action = targetm.addr_space.debug (as);
13325 if (action >= 0)
13327 /* Positive values indicate an address_class. */
13328 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13330 else
13332 /* Negative values indicate an (inverted) segment base reg. */
13333 dw_loc_descr_ref d
13334 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13335 add_AT_loc (mod_type_die, DW_AT_segment, d);
13339 else if (code == INTEGER_TYPE
13340 && TREE_TYPE (type) != NULL_TREE
13341 && subrange_type_for_debug_p (type, &low, &high))
13343 tree bias = NULL_TREE;
13344 if (lang_hooks.types.get_type_bias)
13345 bias = lang_hooks.types.get_type_bias (type);
13346 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13347 item_type = TREE_TYPE (type);
13349 else if (is_base_type (type))
13351 mod_type_die = base_type_die (type, reverse);
13353 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13354 if (reverse_base_type)
13356 dw_die_ref after_die
13357 = modified_type_die (type, cv_quals, false, context_die);
13358 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13360 else
13361 add_child_die (comp_unit_die (), mod_type_die);
13363 add_pubtype (type, mod_type_die);
13365 else
13367 gen_type_die (type, context_die);
13369 /* We have to get the type_main_variant here (and pass that to the
13370 `lookup_type_die' routine) because the ..._TYPE node we have
13371 might simply be a *copy* of some original type node (where the
13372 copy was created to help us keep track of typedef names) and
13373 that copy might have a different TYPE_UID from the original
13374 ..._TYPE node. */
13375 if (TREE_CODE (type) == FUNCTION_TYPE
13376 || TREE_CODE (type) == METHOD_TYPE)
13378 /* For function/method types, can't just use type_main_variant here,
13379 because that can have different ref-qualifiers for C++,
13380 but try to canonicalize. */
13381 tree main = TYPE_MAIN_VARIANT (type);
13382 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13383 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13384 && check_base_type (t, main)
13385 && check_lang_type (t, type))
13386 return lookup_type_die (t);
13387 return lookup_type_die (type);
13389 else if (TREE_CODE (type) != VECTOR_TYPE
13390 && TREE_CODE (type) != ARRAY_TYPE)
13391 return lookup_type_die (type_main_variant (type));
13392 else
13393 /* Vectors have the debugging information in the type,
13394 not the main variant. */
13395 return lookup_type_die (type);
13398 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13399 don't output a DW_TAG_typedef, since there isn't one in the
13400 user's program; just attach a DW_AT_name to the type.
13401 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13402 if the base type already has the same name. */
13403 if (name
13404 && ((TREE_CODE (name) != TYPE_DECL
13405 && (qualified_type == TYPE_MAIN_VARIANT (type)
13406 || (cv_quals == TYPE_UNQUALIFIED)))
13407 || (TREE_CODE (name) == TYPE_DECL
13408 && TREE_TYPE (name) == qualified_type
13409 && DECL_NAME (name))))
13411 if (TREE_CODE (name) == TYPE_DECL)
13412 /* Could just call add_name_and_src_coords_attributes here,
13413 but since this is a builtin type it doesn't have any
13414 useful source coordinates anyway. */
13415 name = DECL_NAME (name);
13416 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13418 /* This probably indicates a bug. */
13419 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13421 name = TYPE_IDENTIFIER (type);
13422 add_name_attribute (mod_type_die,
13423 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13426 if (qualified_type && !reverse_base_type)
13427 equate_type_number_to_die (qualified_type, mod_type_die);
13429 if (item_type)
13430 /* We must do this after the equate_type_number_to_die call, in case
13431 this is a recursive type. This ensures that the modified_type_die
13432 recursion will terminate even if the type is recursive. Recursive
13433 types are possible in Ada. */
13434 sub_die = modified_type_die (item_type,
13435 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13436 reverse,
13437 context_die);
13439 if (sub_die != NULL)
13440 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13442 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13443 if (TYPE_ARTIFICIAL (type))
13444 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13446 return mod_type_die;
13449 /* Generate DIEs for the generic parameters of T.
13450 T must be either a generic type or a generic function.
13451 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13453 static void
13454 gen_generic_params_dies (tree t)
13456 tree parms, args;
13457 int parms_num, i;
13458 dw_die_ref die = NULL;
13459 int non_default;
13461 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13462 return;
13464 if (TYPE_P (t))
13465 die = lookup_type_die (t);
13466 else if (DECL_P (t))
13467 die = lookup_decl_die (t);
13469 gcc_assert (die);
13471 parms = lang_hooks.get_innermost_generic_parms (t);
13472 if (!parms)
13473 /* T has no generic parameter. It means T is neither a generic type
13474 or function. End of story. */
13475 return;
13477 parms_num = TREE_VEC_LENGTH (parms);
13478 args = lang_hooks.get_innermost_generic_args (t);
13479 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13480 non_default = int_cst_value (TREE_CHAIN (args));
13481 else
13482 non_default = TREE_VEC_LENGTH (args);
13483 for (i = 0; i < parms_num; i++)
13485 tree parm, arg, arg_pack_elems;
13486 dw_die_ref parm_die;
13488 parm = TREE_VEC_ELT (parms, i);
13489 arg = TREE_VEC_ELT (args, i);
13490 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13491 gcc_assert (parm && TREE_VALUE (parm) && arg);
13493 if (parm && TREE_VALUE (parm) && arg)
13495 /* If PARM represents a template parameter pack,
13496 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13497 by DW_TAG_template_*_parameter DIEs for the argument
13498 pack elements of ARG. Note that ARG would then be
13499 an argument pack. */
13500 if (arg_pack_elems)
13501 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13502 arg_pack_elems,
13503 die);
13504 else
13505 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13506 true /* emit name */, die);
13507 if (i >= non_default)
13508 add_AT_flag (parm_die, DW_AT_default_value, 1);
13513 /* Create and return a DIE for PARM which should be
13514 the representation of a generic type parameter.
13515 For instance, in the C++ front end, PARM would be a template parameter.
13516 ARG is the argument to PARM.
13517 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13518 name of the PARM.
13519 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13520 as a child node. */
13522 static dw_die_ref
13523 generic_parameter_die (tree parm, tree arg,
13524 bool emit_name_p,
13525 dw_die_ref parent_die)
13527 dw_die_ref tmpl_die = NULL;
13528 const char *name = NULL;
13530 if (!parm || !DECL_NAME (parm) || !arg)
13531 return NULL;
13533 /* We support non-type generic parameters and arguments,
13534 type generic parameters and arguments, as well as
13535 generic generic parameters (a.k.a. template template parameters in C++)
13536 and arguments. */
13537 if (TREE_CODE (parm) == PARM_DECL)
13538 /* PARM is a nontype generic parameter */
13539 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13540 else if (TREE_CODE (parm) == TYPE_DECL)
13541 /* PARM is a type generic parameter. */
13542 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13543 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13544 /* PARM is a generic generic parameter.
13545 Its DIE is a GNU extension. It shall have a
13546 DW_AT_name attribute to represent the name of the template template
13547 parameter, and a DW_AT_GNU_template_name attribute to represent the
13548 name of the template template argument. */
13549 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13550 parent_die, parm);
13551 else
13552 gcc_unreachable ();
13554 if (tmpl_die)
13556 tree tmpl_type;
13558 /* If PARM is a generic parameter pack, it means we are
13559 emitting debug info for a template argument pack element.
13560 In other terms, ARG is a template argument pack element.
13561 In that case, we don't emit any DW_AT_name attribute for
13562 the die. */
13563 if (emit_name_p)
13565 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13566 gcc_assert (name);
13567 add_AT_string (tmpl_die, DW_AT_name, name);
13570 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13572 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13573 TMPL_DIE should have a child DW_AT_type attribute that is set
13574 to the type of the argument to PARM, which is ARG.
13575 If PARM is a type generic parameter, TMPL_DIE should have a
13576 child DW_AT_type that is set to ARG. */
13577 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13578 add_type_attribute (tmpl_die, tmpl_type,
13579 (TREE_THIS_VOLATILE (tmpl_type)
13580 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13581 false, parent_die);
13583 else
13585 /* So TMPL_DIE is a DIE representing a
13586 a generic generic template parameter, a.k.a template template
13587 parameter in C++ and arg is a template. */
13589 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13590 to the name of the argument. */
13591 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13592 if (name)
13593 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13596 if (TREE_CODE (parm) == PARM_DECL)
13597 /* So PARM is a non-type generic parameter.
13598 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13599 attribute of TMPL_DIE which value represents the value
13600 of ARG.
13601 We must be careful here:
13602 The value of ARG might reference some function decls.
13603 We might currently be emitting debug info for a generic
13604 type and types are emitted before function decls, we don't
13605 know if the function decls referenced by ARG will actually be
13606 emitted after cgraph computations.
13607 So must defer the generation of the DW_AT_const_value to
13608 after cgraph is ready. */
13609 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13612 return tmpl_die;
13615 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13616 PARM_PACK must be a template parameter pack. The returned DIE
13617 will be child DIE of PARENT_DIE. */
13619 static dw_die_ref
13620 template_parameter_pack_die (tree parm_pack,
13621 tree parm_pack_args,
13622 dw_die_ref parent_die)
13624 dw_die_ref die;
13625 int j;
13627 gcc_assert (parent_die && parm_pack);
13629 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13630 add_name_and_src_coords_attributes (die, parm_pack);
13631 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13632 generic_parameter_die (parm_pack,
13633 TREE_VEC_ELT (parm_pack_args, j),
13634 false /* Don't emit DW_AT_name */,
13635 die);
13636 return die;
13639 /* Return the DBX register number described by a given RTL node. */
13641 static unsigned int
13642 dbx_reg_number (const_rtx rtl)
13644 unsigned regno = REGNO (rtl);
13646 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13648 #ifdef LEAF_REG_REMAP
13649 if (crtl->uses_only_leaf_regs)
13651 int leaf_reg = LEAF_REG_REMAP (regno);
13652 if (leaf_reg != -1)
13653 regno = (unsigned) leaf_reg;
13655 #endif
13657 regno = DBX_REGISTER_NUMBER (regno);
13658 gcc_assert (regno != INVALID_REGNUM);
13659 return regno;
13662 /* Optionally add a DW_OP_piece term to a location description expression.
13663 DW_OP_piece is only added if the location description expression already
13664 doesn't end with DW_OP_piece. */
13666 static void
13667 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13669 dw_loc_descr_ref loc;
13671 if (*list_head != NULL)
13673 /* Find the end of the chain. */
13674 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13677 if (loc->dw_loc_opc != DW_OP_piece)
13678 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13682 /* Return a location descriptor that designates a machine register or
13683 zero if there is none. */
13685 static dw_loc_descr_ref
13686 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13688 rtx regs;
13690 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13691 return 0;
13693 /* We only use "frame base" when we're sure we're talking about the
13694 post-prologue local stack frame. We do this by *not* running
13695 register elimination until this point, and recognizing the special
13696 argument pointer and soft frame pointer rtx's.
13697 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13698 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13699 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13701 dw_loc_descr_ref result = NULL;
13703 if (dwarf_version >= 4 || !dwarf_strict)
13705 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13706 initialized);
13707 if (result)
13708 add_loc_descr (&result,
13709 new_loc_descr (DW_OP_stack_value, 0, 0));
13711 return result;
13714 regs = targetm.dwarf_register_span (rtl);
13716 if (REG_NREGS (rtl) > 1 || regs)
13717 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13718 else
13720 unsigned int dbx_regnum = dbx_reg_number (rtl);
13721 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13722 return 0;
13723 return one_reg_loc_descriptor (dbx_regnum, initialized);
13727 /* Return a location descriptor that designates a machine register for
13728 a given hard register number. */
13730 static dw_loc_descr_ref
13731 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13733 dw_loc_descr_ref reg_loc_descr;
13735 if (regno <= 31)
13736 reg_loc_descr
13737 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13738 else
13739 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13741 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13742 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13744 return reg_loc_descr;
13747 /* Given an RTL of a register, return a location descriptor that
13748 designates a value that spans more than one register. */
13750 static dw_loc_descr_ref
13751 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13752 enum var_init_status initialized)
13754 int size, i;
13755 dw_loc_descr_ref loc_result = NULL;
13757 /* Simple, contiguous registers. */
13758 if (regs == NULL_RTX)
13760 unsigned reg = REGNO (rtl);
13761 int nregs;
13763 #ifdef LEAF_REG_REMAP
13764 if (crtl->uses_only_leaf_regs)
13766 int leaf_reg = LEAF_REG_REMAP (reg);
13767 if (leaf_reg != -1)
13768 reg = (unsigned) leaf_reg;
13770 #endif
13772 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13773 nregs = REG_NREGS (rtl);
13775 /* At present we only track constant-sized pieces. */
13776 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13777 return NULL;
13778 size /= nregs;
13780 loc_result = NULL;
13781 while (nregs--)
13783 dw_loc_descr_ref t;
13785 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13786 VAR_INIT_STATUS_INITIALIZED);
13787 add_loc_descr (&loc_result, t);
13788 add_loc_descr_op_piece (&loc_result, size);
13789 ++reg;
13791 return loc_result;
13794 /* Now onto stupid register sets in non contiguous locations. */
13796 gcc_assert (GET_CODE (regs) == PARALLEL);
13798 /* At present we only track constant-sized pieces. */
13799 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13800 return NULL;
13801 loc_result = NULL;
13803 for (i = 0; i < XVECLEN (regs, 0); ++i)
13805 dw_loc_descr_ref t;
13807 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13808 VAR_INIT_STATUS_INITIALIZED);
13809 add_loc_descr (&loc_result, t);
13810 add_loc_descr_op_piece (&loc_result, size);
13813 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13814 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13815 return loc_result;
13818 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13820 /* Return a location descriptor that designates a constant i,
13821 as a compound operation from constant (i >> shift), constant shift
13822 and DW_OP_shl. */
13824 static dw_loc_descr_ref
13825 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13827 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13828 add_loc_descr (&ret, int_loc_descriptor (shift));
13829 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13830 return ret;
13833 /* Return a location descriptor that designates constant POLY_I. */
13835 static dw_loc_descr_ref
13836 int_loc_descriptor (poly_int64 poly_i)
13838 enum dwarf_location_atom op;
13840 HOST_WIDE_INT i;
13841 if (!poly_i.is_constant (&i))
13843 /* Create location descriptions for the non-constant part and
13844 add any constant offset at the end. */
13845 dw_loc_descr_ref ret = NULL;
13846 HOST_WIDE_INT constant = poly_i.coeffs[0];
13847 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13849 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13850 if (coeff != 0)
13852 dw_loc_descr_ref start = ret;
13853 unsigned int factor;
13854 int bias;
13855 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13856 (j, &factor, &bias);
13858 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13859 add COEFF * (REGNO / FACTOR) now and subtract
13860 COEFF * BIAS from the final constant part. */
13861 constant -= coeff * bias;
13862 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13863 if (coeff % factor == 0)
13864 coeff /= factor;
13865 else
13867 int amount = exact_log2 (factor);
13868 gcc_assert (amount >= 0);
13869 add_loc_descr (&ret, int_loc_descriptor (amount));
13870 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13872 if (coeff != 1)
13874 add_loc_descr (&ret, int_loc_descriptor (coeff));
13875 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13877 if (start)
13878 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13881 loc_descr_plus_const (&ret, constant);
13882 return ret;
13885 /* Pick the smallest representation of a constant, rather than just
13886 defaulting to the LEB encoding. */
13887 if (i >= 0)
13889 int clz = clz_hwi (i);
13890 int ctz = ctz_hwi (i);
13891 if (i <= 31)
13892 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13893 else if (i <= 0xff)
13894 op = DW_OP_const1u;
13895 else if (i <= 0xffff)
13896 op = DW_OP_const2u;
13897 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13898 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13899 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13900 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13901 while DW_OP_const4u is 5 bytes. */
13902 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13903 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13904 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13905 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13906 while DW_OP_const4u is 5 bytes. */
13907 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13909 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13910 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13911 <= 4)
13913 /* As i >= 2**31, the double cast above will yield a negative number.
13914 Since wrapping is defined in DWARF expressions we can output big
13915 positive integers as small negative ones, regardless of the size
13916 of host wide ints.
13918 Here, since the evaluator will handle 32-bit values and since i >=
13919 2**31, we know it's going to be interpreted as a negative literal:
13920 store it this way if we can do better than 5 bytes this way. */
13921 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13923 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13924 op = DW_OP_const4u;
13926 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13927 least 6 bytes: see if we can do better before falling back to it. */
13928 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13929 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13930 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13931 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13932 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13933 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13934 >= HOST_BITS_PER_WIDE_INT)
13935 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13936 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13937 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13938 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13939 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13940 && size_of_uleb128 (i) > 6)
13941 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13942 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13943 else
13944 op = DW_OP_constu;
13946 else
13948 if (i >= -0x80)
13949 op = DW_OP_const1s;
13950 else if (i >= -0x8000)
13951 op = DW_OP_const2s;
13952 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13954 if (size_of_int_loc_descriptor (i) < 5)
13956 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13957 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13958 return ret;
13960 op = DW_OP_const4s;
13962 else
13964 if (size_of_int_loc_descriptor (i)
13965 < (unsigned long) 1 + size_of_sleb128 (i))
13967 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13968 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13969 return ret;
13971 op = DW_OP_consts;
13975 return new_loc_descr (op, i, 0);
13978 /* Likewise, for unsigned constants. */
13980 static dw_loc_descr_ref
13981 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13983 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13984 const unsigned HOST_WIDE_INT max_uint
13985 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13987 /* If possible, use the clever signed constants handling. */
13988 if (i <= max_int)
13989 return int_loc_descriptor ((HOST_WIDE_INT) i);
13991 /* Here, we are left with positive numbers that cannot be represented as
13992 HOST_WIDE_INT, i.e.:
13993 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13995 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13996 whereas may be better to output a negative integer: thanks to integer
13997 wrapping, we know that:
13998 x = x - 2 ** DWARF2_ADDR_SIZE
13999 = x - 2 * (max (HOST_WIDE_INT) + 1)
14000 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
14001 small negative integers. Let's try that in cases it will clearly improve
14002 the encoding: there is no gain turning DW_OP_const4u into
14003 DW_OP_const4s. */
14004 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14005 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14006 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14008 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14010 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14011 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14012 const HOST_WIDE_INT second_shift
14013 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14015 /* So we finally have:
14016 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14017 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14018 return int_loc_descriptor (second_shift);
14021 /* Last chance: fallback to a simple constant operation. */
14022 return new_loc_descr
14023 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14024 ? DW_OP_const4u
14025 : DW_OP_const8u,
14026 i, 0);
14029 /* Generate and return a location description that computes the unsigned
14030 comparison of the two stack top entries (a OP b where b is the top-most
14031 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14032 LE_EXPR, GT_EXPR or GE_EXPR. */
14034 static dw_loc_descr_ref
14035 uint_comparison_loc_list (enum tree_code kind)
14037 enum dwarf_location_atom op, flip_op;
14038 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14040 switch (kind)
14042 case LT_EXPR:
14043 op = DW_OP_lt;
14044 break;
14045 case LE_EXPR:
14046 op = DW_OP_le;
14047 break;
14048 case GT_EXPR:
14049 op = DW_OP_gt;
14050 break;
14051 case GE_EXPR:
14052 op = DW_OP_ge;
14053 break;
14054 default:
14055 gcc_unreachable ();
14058 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14059 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14061 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14062 possible to perform unsigned comparisons: we just have to distinguish
14063 three cases:
14065 1. when a and b have the same sign (as signed integers); then we should
14066 return: a OP(signed) b;
14068 2. when a is a negative signed integer while b is a positive one, then a
14069 is a greater unsigned integer than b; likewise when a and b's roles
14070 are flipped.
14072 So first, compare the sign of the two operands. */
14073 ret = new_loc_descr (DW_OP_over, 0, 0);
14074 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14075 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14076 /* If they have different signs (i.e. they have different sign bits), then
14077 the stack top value has now the sign bit set and thus it's smaller than
14078 zero. */
14079 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14080 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14081 add_loc_descr (&ret, bra_node);
14083 /* We are in case 1. At this point, we know both operands have the same
14084 sign, to it's safe to use the built-in signed comparison. */
14085 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14086 add_loc_descr (&ret, jmp_node);
14088 /* We are in case 2. Here, we know both operands do not have the same sign,
14089 so we have to flip the signed comparison. */
14090 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14091 tmp = new_loc_descr (flip_op, 0, 0);
14092 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14093 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14094 add_loc_descr (&ret, tmp);
14096 /* This dummy operation is necessary to make the two branches join. */
14097 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14098 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14099 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14100 add_loc_descr (&ret, tmp);
14102 return ret;
14105 /* Likewise, but takes the location description lists (might be destructive on
14106 them). Return NULL if either is NULL or if concatenation fails. */
14108 static dw_loc_list_ref
14109 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14110 enum tree_code kind)
14112 if (left == NULL || right == NULL)
14113 return NULL;
14115 add_loc_list (&left, right);
14116 if (left == NULL)
14117 return NULL;
14119 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14120 return left;
14123 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14124 without actually allocating it. */
14126 static unsigned long
14127 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14129 return size_of_int_loc_descriptor (i >> shift)
14130 + size_of_int_loc_descriptor (shift)
14131 + 1;
14134 /* Return size_of_locs (int_loc_descriptor (i)) without
14135 actually allocating it. */
14137 static unsigned long
14138 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14140 unsigned long s;
14142 if (i >= 0)
14144 int clz, ctz;
14145 if (i <= 31)
14146 return 1;
14147 else if (i <= 0xff)
14148 return 2;
14149 else if (i <= 0xffff)
14150 return 3;
14151 clz = clz_hwi (i);
14152 ctz = ctz_hwi (i);
14153 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14154 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14155 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14156 - clz - 5);
14157 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14158 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14159 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14160 - clz - 8);
14161 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14162 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14163 <= 4)
14164 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14165 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14166 return 5;
14167 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14168 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14169 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14170 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14171 - clz - 8);
14172 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14173 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14174 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14175 - clz - 16);
14176 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14177 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14178 && s > 6)
14179 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14180 - clz - 32);
14181 else
14182 return 1 + s;
14184 else
14186 if (i >= -0x80)
14187 return 2;
14188 else if (i >= -0x8000)
14189 return 3;
14190 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14192 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14194 s = size_of_int_loc_descriptor (-i) + 1;
14195 if (s < 5)
14196 return s;
14198 return 5;
14200 else
14202 unsigned long r = 1 + size_of_sleb128 (i);
14203 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14205 s = size_of_int_loc_descriptor (-i) + 1;
14206 if (s < r)
14207 return s;
14209 return r;
14214 /* Return loc description representing "address" of integer value.
14215 This can appear only as toplevel expression. */
14217 static dw_loc_descr_ref
14218 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14220 int litsize;
14221 dw_loc_descr_ref loc_result = NULL;
14223 if (!(dwarf_version >= 4 || !dwarf_strict))
14224 return NULL;
14226 litsize = size_of_int_loc_descriptor (i);
14227 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14228 is more compact. For DW_OP_stack_value we need:
14229 litsize + 1 (DW_OP_stack_value)
14230 and for DW_OP_implicit_value:
14231 1 (DW_OP_implicit_value) + 1 (length) + size. */
14232 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14234 loc_result = int_loc_descriptor (i);
14235 add_loc_descr (&loc_result,
14236 new_loc_descr (DW_OP_stack_value, 0, 0));
14237 return loc_result;
14240 loc_result = new_loc_descr (DW_OP_implicit_value,
14241 size, 0);
14242 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14243 loc_result->dw_loc_oprnd2.v.val_int = i;
14244 return loc_result;
14247 /* Return a location descriptor that designates a base+offset location. */
14249 static dw_loc_descr_ref
14250 based_loc_descr (rtx reg, poly_int64 offset,
14251 enum var_init_status initialized)
14253 unsigned int regno;
14254 dw_loc_descr_ref result;
14255 dw_fde_ref fde = cfun->fde;
14257 /* We only use "frame base" when we're sure we're talking about the
14258 post-prologue local stack frame. We do this by *not* running
14259 register elimination until this point, and recognizing the special
14260 argument pointer and soft frame pointer rtx's. */
14261 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14263 rtx elim = (ira_use_lra_p
14264 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14265 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14267 if (elim != reg)
14269 /* Allow hard frame pointer here even if frame pointer
14270 isn't used since hard frame pointer is encoded with
14271 DW_OP_fbreg which uses the DW_AT_frame_base attribute,
14272 not hard frame pointer directly. */
14273 elim = strip_offset_and_add (elim, &offset);
14274 gcc_assert (elim == hard_frame_pointer_rtx
14275 || elim == stack_pointer_rtx);
14277 /* If drap register is used to align stack, use frame
14278 pointer + offset to access stack variables. If stack
14279 is aligned without drap, use stack pointer + offset to
14280 access stack variables. */
14281 if (crtl->stack_realign_tried
14282 && reg == frame_pointer_rtx)
14284 int base_reg
14285 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14286 ? HARD_FRAME_POINTER_REGNUM
14287 : REGNO (elim));
14288 return new_reg_loc_descr (base_reg, offset);
14291 gcc_assert (frame_pointer_fb_offset_valid);
14292 offset += frame_pointer_fb_offset;
14293 HOST_WIDE_INT const_offset;
14294 if (offset.is_constant (&const_offset))
14295 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14296 else
14298 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14299 loc_descr_plus_const (&ret, offset);
14300 return ret;
14305 regno = REGNO (reg);
14306 #ifdef LEAF_REG_REMAP
14307 if (crtl->uses_only_leaf_regs)
14309 int leaf_reg = LEAF_REG_REMAP (regno);
14310 if (leaf_reg != -1)
14311 regno = (unsigned) leaf_reg;
14313 #endif
14314 regno = DWARF_FRAME_REGNUM (regno);
14316 HOST_WIDE_INT const_offset;
14317 if (!optimize && fde
14318 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14319 && offset.is_constant (&const_offset))
14321 /* Use cfa+offset to represent the location of arguments passed
14322 on the stack when drap is used to align stack.
14323 Only do this when not optimizing, for optimized code var-tracking
14324 is supposed to track where the arguments live and the register
14325 used as vdrap or drap in some spot might be used for something
14326 else in other part of the routine. */
14327 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14330 result = new_reg_loc_descr (regno, offset);
14332 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14333 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14335 return result;
14338 /* Return true if this RTL expression describes a base+offset calculation. */
14340 static inline int
14341 is_based_loc (const_rtx rtl)
14343 return (GET_CODE (rtl) == PLUS
14344 && ((REG_P (XEXP (rtl, 0))
14345 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14346 && CONST_INT_P (XEXP (rtl, 1)))));
14349 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14350 failed. */
14352 static dw_loc_descr_ref
14353 tls_mem_loc_descriptor (rtx mem)
14355 tree base;
14356 dw_loc_descr_ref loc_result;
14358 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14359 return NULL;
14361 base = get_base_address (MEM_EXPR (mem));
14362 if (base == NULL
14363 || !VAR_P (base)
14364 || !DECL_THREAD_LOCAL_P (base))
14365 return NULL;
14367 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14368 if (loc_result == NULL)
14369 return NULL;
14371 if (maybe_ne (MEM_OFFSET (mem), 0))
14372 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14374 return loc_result;
14377 /* Output debug info about reason why we failed to expand expression as dwarf
14378 expression. */
14380 static void
14381 expansion_failed (tree expr, rtx rtl, char const *reason)
14383 if (dump_file && (dump_flags & TDF_DETAILS))
14385 fprintf (dump_file, "Failed to expand as dwarf: ");
14386 if (expr)
14387 print_generic_expr (dump_file, expr, dump_flags);
14388 if (rtl)
14390 fprintf (dump_file, "\n");
14391 print_rtl (dump_file, rtl);
14393 fprintf (dump_file, "\nReason: %s\n", reason);
14397 /* Helper function for const_ok_for_output. */
14399 static bool
14400 const_ok_for_output_1 (rtx rtl)
14402 if (targetm.const_not_ok_for_debug_p (rtl))
14404 if (GET_CODE (rtl) != UNSPEC)
14406 expansion_failed (NULL_TREE, rtl,
14407 "Expression rejected for debug by the backend.\n");
14408 return false;
14411 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14412 the target hook doesn't explicitly allow it in debug info, assume
14413 we can't express it in the debug info. */
14414 /* Don't complain about TLS UNSPECs, those are just too hard to
14415 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14416 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14417 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14418 if (flag_checking
14419 && (XVECLEN (rtl, 0) == 0
14420 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14421 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14422 inform (current_function_decl
14423 ? DECL_SOURCE_LOCATION (current_function_decl)
14424 : UNKNOWN_LOCATION,
14425 #if NUM_UNSPEC_VALUES > 0
14426 "non-delegitimized UNSPEC %s (%d) found in variable location",
14427 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14428 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14429 XINT (rtl, 1));
14430 #else
14431 "non-delegitimized UNSPEC %d found in variable location",
14432 XINT (rtl, 1));
14433 #endif
14434 expansion_failed (NULL_TREE, rtl,
14435 "UNSPEC hasn't been delegitimized.\n");
14436 return false;
14439 if (CONST_POLY_INT_P (rtl))
14440 return false;
14442 if (targetm.const_not_ok_for_debug_p (rtl))
14444 expansion_failed (NULL_TREE, rtl,
14445 "Expression rejected for debug by the backend.\n");
14446 return false;
14449 /* FIXME: Refer to PR60655. It is possible for simplification
14450 of rtl expressions in var tracking to produce such expressions.
14451 We should really identify / validate expressions
14452 enclosed in CONST that can be handled by assemblers on various
14453 targets and only handle legitimate cases here. */
14454 switch (GET_CODE (rtl))
14456 case SYMBOL_REF:
14457 break;
14458 case NOT:
14459 case NEG:
14460 return false;
14461 default:
14462 return true;
14465 if (CONSTANT_POOL_ADDRESS_P (rtl))
14467 bool marked;
14468 get_pool_constant_mark (rtl, &marked);
14469 /* If all references to this pool constant were optimized away,
14470 it was not output and thus we can't represent it. */
14471 if (!marked)
14473 expansion_failed (NULL_TREE, rtl,
14474 "Constant was removed from constant pool.\n");
14475 return false;
14479 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14480 return false;
14482 /* Avoid references to external symbols in debug info, on several targets
14483 the linker might even refuse to link when linking a shared library,
14484 and in many other cases the relocations for .debug_info/.debug_loc are
14485 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14486 to be defined within the same shared library or executable are fine. */
14487 if (SYMBOL_REF_EXTERNAL_P (rtl))
14489 tree decl = SYMBOL_REF_DECL (rtl);
14491 if (decl == NULL || !targetm.binds_local_p (decl))
14493 expansion_failed (NULL_TREE, rtl,
14494 "Symbol not defined in current TU.\n");
14495 return false;
14499 return true;
14502 /* Return true if constant RTL can be emitted in DW_OP_addr or
14503 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14504 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14506 static bool
14507 const_ok_for_output (rtx rtl)
14509 if (GET_CODE (rtl) == SYMBOL_REF)
14510 return const_ok_for_output_1 (rtl);
14512 if (GET_CODE (rtl) == CONST)
14514 subrtx_var_iterator::array_type array;
14515 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14516 if (!const_ok_for_output_1 (*iter))
14517 return false;
14518 return true;
14521 return true;
14524 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14525 if possible, NULL otherwise. */
14527 static dw_die_ref
14528 base_type_for_mode (machine_mode mode, bool unsignedp)
14530 dw_die_ref type_die;
14531 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14533 if (type == NULL)
14534 return NULL;
14535 switch (TREE_CODE (type))
14537 case INTEGER_TYPE:
14538 case REAL_TYPE:
14539 break;
14540 default:
14541 return NULL;
14543 type_die = lookup_type_die (type);
14544 if (!type_die)
14545 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14546 comp_unit_die ());
14547 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14548 return NULL;
14549 return type_die;
14552 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14553 type matching MODE, or, if MODE is narrower than or as wide as
14554 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14555 possible. */
14557 static dw_loc_descr_ref
14558 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14560 machine_mode outer_mode = mode;
14561 dw_die_ref type_die;
14562 dw_loc_descr_ref cvt;
14564 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14566 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14567 return op;
14569 type_die = base_type_for_mode (outer_mode, 1);
14570 if (type_die == NULL)
14571 return NULL;
14572 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14573 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14574 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14575 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14576 add_loc_descr (&op, cvt);
14577 return op;
14580 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14582 static dw_loc_descr_ref
14583 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14584 dw_loc_descr_ref op1)
14586 dw_loc_descr_ref ret = op0;
14587 add_loc_descr (&ret, op1);
14588 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14589 if (STORE_FLAG_VALUE != 1)
14591 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14594 return ret;
14597 /* Subroutine of scompare_loc_descriptor for the case in which we're
14598 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14599 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14601 static dw_loc_descr_ref
14602 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14603 scalar_int_mode op_mode,
14604 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14606 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14607 dw_loc_descr_ref cvt;
14609 if (type_die == NULL)
14610 return NULL;
14611 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14612 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14613 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14614 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14615 add_loc_descr (&op0, cvt);
14616 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14617 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14618 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14619 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14620 add_loc_descr (&op1, cvt);
14621 return compare_loc_descriptor (op, op0, op1);
14624 /* Subroutine of scompare_loc_descriptor for the case in which we're
14625 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14626 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14628 static dw_loc_descr_ref
14629 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14630 scalar_int_mode op_mode,
14631 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14633 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14634 /* For eq/ne, if the operands are known to be zero-extended,
14635 there is no need to do the fancy shifting up. */
14636 if (op == DW_OP_eq || op == DW_OP_ne)
14638 dw_loc_descr_ref last0, last1;
14639 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14641 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14643 /* deref_size zero extends, and for constants we can check
14644 whether they are zero extended or not. */
14645 if (((last0->dw_loc_opc == DW_OP_deref_size
14646 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14647 || (CONST_INT_P (XEXP (rtl, 0))
14648 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14649 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14650 && ((last1->dw_loc_opc == DW_OP_deref_size
14651 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14652 || (CONST_INT_P (XEXP (rtl, 1))
14653 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14654 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14655 return compare_loc_descriptor (op, op0, op1);
14657 /* EQ/NE comparison against constant in narrower type than
14658 DWARF2_ADDR_SIZE can be performed either as
14659 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14660 DW_OP_{eq,ne}
14662 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14663 DW_OP_{eq,ne}. Pick whatever is shorter. */
14664 if (CONST_INT_P (XEXP (rtl, 1))
14665 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14666 && (size_of_int_loc_descriptor (shift) + 1
14667 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14668 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14669 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14670 & GET_MODE_MASK (op_mode))))
14672 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14673 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14674 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14675 & GET_MODE_MASK (op_mode));
14676 return compare_loc_descriptor (op, op0, op1);
14679 add_loc_descr (&op0, int_loc_descriptor (shift));
14680 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14681 if (CONST_INT_P (XEXP (rtl, 1)))
14682 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14683 else
14685 add_loc_descr (&op1, int_loc_descriptor (shift));
14686 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14688 return compare_loc_descriptor (op, op0, op1);
14691 /* Return location descriptor for unsigned comparison OP RTL. */
14693 static dw_loc_descr_ref
14694 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14695 machine_mode mem_mode)
14697 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14698 dw_loc_descr_ref op0, op1;
14700 if (op_mode == VOIDmode)
14701 op_mode = GET_MODE (XEXP (rtl, 1));
14702 if (op_mode == VOIDmode)
14703 return NULL;
14705 scalar_int_mode int_op_mode;
14706 if (dwarf_strict
14707 && dwarf_version < 5
14708 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14709 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14710 return NULL;
14712 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14713 VAR_INIT_STATUS_INITIALIZED);
14714 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14715 VAR_INIT_STATUS_INITIALIZED);
14717 if (op0 == NULL || op1 == NULL)
14718 return NULL;
14720 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14722 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14723 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14725 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14726 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14728 return compare_loc_descriptor (op, op0, op1);
14731 /* Return location descriptor for unsigned comparison OP RTL. */
14733 static dw_loc_descr_ref
14734 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14735 machine_mode mem_mode)
14737 dw_loc_descr_ref op0, op1;
14739 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14740 if (test_op_mode == VOIDmode)
14741 test_op_mode = GET_MODE (XEXP (rtl, 1));
14743 scalar_int_mode op_mode;
14744 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14745 return NULL;
14747 if (dwarf_strict
14748 && dwarf_version < 5
14749 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14750 return NULL;
14752 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14753 VAR_INIT_STATUS_INITIALIZED);
14754 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14755 VAR_INIT_STATUS_INITIALIZED);
14757 if (op0 == NULL || op1 == NULL)
14758 return NULL;
14760 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14762 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14763 dw_loc_descr_ref last0, last1;
14764 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14766 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14768 if (CONST_INT_P (XEXP (rtl, 0)))
14769 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14770 /* deref_size zero extends, so no need to mask it again. */
14771 else if (last0->dw_loc_opc != DW_OP_deref_size
14772 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14774 add_loc_descr (&op0, int_loc_descriptor (mask));
14775 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14777 if (CONST_INT_P (XEXP (rtl, 1)))
14778 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14779 /* deref_size zero extends, so no need to mask it again. */
14780 else if (last1->dw_loc_opc != DW_OP_deref_size
14781 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14783 add_loc_descr (&op1, int_loc_descriptor (mask));
14784 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14787 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14789 HOST_WIDE_INT bias = 1;
14790 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14791 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14792 if (CONST_INT_P (XEXP (rtl, 1)))
14793 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14794 + INTVAL (XEXP (rtl, 1)));
14795 else
14796 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14797 bias, 0));
14799 return compare_loc_descriptor (op, op0, op1);
14802 /* Return location descriptor for {U,S}{MIN,MAX}. */
14804 static dw_loc_descr_ref
14805 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14806 machine_mode mem_mode)
14808 enum dwarf_location_atom op;
14809 dw_loc_descr_ref op0, op1, ret;
14810 dw_loc_descr_ref bra_node, drop_node;
14812 scalar_int_mode int_mode;
14813 if (dwarf_strict
14814 && dwarf_version < 5
14815 && (!is_a <scalar_int_mode> (mode, &int_mode)
14816 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14817 return NULL;
14819 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14820 VAR_INIT_STATUS_INITIALIZED);
14821 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14822 VAR_INIT_STATUS_INITIALIZED);
14824 if (op0 == NULL || op1 == NULL)
14825 return NULL;
14827 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14828 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14829 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14830 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14832 /* Checked by the caller. */
14833 int_mode = as_a <scalar_int_mode> (mode);
14834 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14836 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14837 add_loc_descr (&op0, int_loc_descriptor (mask));
14838 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14839 add_loc_descr (&op1, int_loc_descriptor (mask));
14840 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14842 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14844 HOST_WIDE_INT bias = 1;
14845 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14846 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14847 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14850 else if (is_a <scalar_int_mode> (mode, &int_mode)
14851 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14853 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14854 add_loc_descr (&op0, int_loc_descriptor (shift));
14855 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14856 add_loc_descr (&op1, int_loc_descriptor (shift));
14857 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14859 else if (is_a <scalar_int_mode> (mode, &int_mode)
14860 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14862 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14863 dw_loc_descr_ref cvt;
14864 if (type_die == NULL)
14865 return NULL;
14866 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14867 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14868 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14869 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14870 add_loc_descr (&op0, cvt);
14871 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14872 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14873 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14874 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14875 add_loc_descr (&op1, cvt);
14878 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14879 op = DW_OP_lt;
14880 else
14881 op = DW_OP_gt;
14882 ret = op0;
14883 add_loc_descr (&ret, op1);
14884 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14885 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14886 add_loc_descr (&ret, bra_node);
14887 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14888 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14889 add_loc_descr (&ret, drop_node);
14890 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14891 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14892 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14893 && is_a <scalar_int_mode> (mode, &int_mode)
14894 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14895 ret = convert_descriptor_to_mode (int_mode, ret);
14896 return ret;
14899 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14900 but after converting arguments to type_die, afterwards
14901 convert back to unsigned. */
14903 static dw_loc_descr_ref
14904 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14905 scalar_int_mode mode, machine_mode mem_mode)
14907 dw_loc_descr_ref cvt, op0, op1;
14909 if (type_die == NULL)
14910 return NULL;
14911 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14912 VAR_INIT_STATUS_INITIALIZED);
14913 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14914 VAR_INIT_STATUS_INITIALIZED);
14915 if (op0 == NULL || op1 == NULL)
14916 return NULL;
14917 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14918 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14919 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14920 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14921 add_loc_descr (&op0, cvt);
14922 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14923 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14924 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14925 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14926 add_loc_descr (&op1, cvt);
14927 add_loc_descr (&op0, op1);
14928 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14929 return convert_descriptor_to_mode (mode, op0);
14932 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14933 const0 is DW_OP_lit0 or corresponding typed constant,
14934 const1 is DW_OP_lit1 or corresponding typed constant
14935 and constMSB is constant with just the MSB bit set
14936 for the mode):
14937 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14938 L1: const0 DW_OP_swap
14939 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14940 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14941 L3: DW_OP_drop
14942 L4: DW_OP_nop
14944 CTZ is similar:
14945 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14946 L1: const0 DW_OP_swap
14947 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14948 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14949 L3: DW_OP_drop
14950 L4: DW_OP_nop
14952 FFS is similar:
14953 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14954 L1: const1 DW_OP_swap
14955 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14956 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14957 L3: DW_OP_drop
14958 L4: DW_OP_nop */
14960 static dw_loc_descr_ref
14961 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14962 machine_mode mem_mode)
14964 dw_loc_descr_ref op0, ret, tmp;
14965 HOST_WIDE_INT valv;
14966 dw_loc_descr_ref l1jump, l1label;
14967 dw_loc_descr_ref l2jump, l2label;
14968 dw_loc_descr_ref l3jump, l3label;
14969 dw_loc_descr_ref l4jump, l4label;
14970 rtx msb;
14972 if (GET_MODE (XEXP (rtl, 0)) != mode)
14973 return NULL;
14975 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14976 VAR_INIT_STATUS_INITIALIZED);
14977 if (op0 == NULL)
14978 return NULL;
14979 ret = op0;
14980 if (GET_CODE (rtl) == CLZ)
14982 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14983 valv = GET_MODE_BITSIZE (mode);
14985 else if (GET_CODE (rtl) == FFS)
14986 valv = 0;
14987 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14988 valv = GET_MODE_BITSIZE (mode);
14989 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14990 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14991 add_loc_descr (&ret, l1jump);
14992 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14993 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14994 VAR_INIT_STATUS_INITIALIZED);
14995 if (tmp == NULL)
14996 return NULL;
14997 add_loc_descr (&ret, tmp);
14998 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14999 add_loc_descr (&ret, l4jump);
15000 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
15001 ? const1_rtx : const0_rtx,
15002 mode, mem_mode,
15003 VAR_INIT_STATUS_INITIALIZED);
15004 if (l1label == NULL)
15005 return NULL;
15006 add_loc_descr (&ret, l1label);
15007 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15008 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15009 add_loc_descr (&ret, l2label);
15010 if (GET_CODE (rtl) != CLZ)
15011 msb = const1_rtx;
15012 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15013 msb = GEN_INT (HOST_WIDE_INT_1U
15014 << (GET_MODE_BITSIZE (mode) - 1));
15015 else
15016 msb = immed_wide_int_const
15017 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15018 GET_MODE_PRECISION (mode)), mode);
15019 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15020 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15021 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15022 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15023 else
15024 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15025 VAR_INIT_STATUS_INITIALIZED);
15026 if (tmp == NULL)
15027 return NULL;
15028 add_loc_descr (&ret, tmp);
15029 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15030 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15031 add_loc_descr (&ret, l3jump);
15032 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15033 VAR_INIT_STATUS_INITIALIZED);
15034 if (tmp == NULL)
15035 return NULL;
15036 add_loc_descr (&ret, tmp);
15037 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15038 ? DW_OP_shl : DW_OP_shr, 0, 0));
15039 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15040 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15041 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15042 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15043 add_loc_descr (&ret, l2jump);
15044 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15045 add_loc_descr (&ret, l3label);
15046 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15047 add_loc_descr (&ret, l4label);
15048 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15049 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15050 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15051 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15052 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15053 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15054 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15055 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15056 return ret;
15059 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15060 const1 is DW_OP_lit1 or corresponding typed constant):
15061 const0 DW_OP_swap
15062 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15063 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15064 L2: DW_OP_drop
15066 PARITY is similar:
15067 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15068 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15069 L2: DW_OP_drop */
15071 static dw_loc_descr_ref
15072 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15073 machine_mode mem_mode)
15075 dw_loc_descr_ref op0, ret, tmp;
15076 dw_loc_descr_ref l1jump, l1label;
15077 dw_loc_descr_ref l2jump, l2label;
15079 if (GET_MODE (XEXP (rtl, 0)) != mode)
15080 return NULL;
15082 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15083 VAR_INIT_STATUS_INITIALIZED);
15084 if (op0 == NULL)
15085 return NULL;
15086 ret = op0;
15087 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15088 VAR_INIT_STATUS_INITIALIZED);
15089 if (tmp == NULL)
15090 return NULL;
15091 add_loc_descr (&ret, tmp);
15092 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15093 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15094 add_loc_descr (&ret, l1label);
15095 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15096 add_loc_descr (&ret, l2jump);
15097 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15098 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15099 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15100 VAR_INIT_STATUS_INITIALIZED);
15101 if (tmp == NULL)
15102 return NULL;
15103 add_loc_descr (&ret, tmp);
15104 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15105 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15106 ? DW_OP_plus : DW_OP_xor, 0, 0));
15107 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15108 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15109 VAR_INIT_STATUS_INITIALIZED);
15110 add_loc_descr (&ret, tmp);
15111 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15112 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15113 add_loc_descr (&ret, l1jump);
15114 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15115 add_loc_descr (&ret, l2label);
15116 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15117 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15118 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15119 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15120 return ret;
15123 /* BSWAP (constS is initial shift count, either 56 or 24):
15124 constS const0
15125 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15126 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15127 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15128 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15129 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15131 static dw_loc_descr_ref
15132 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15133 machine_mode mem_mode)
15135 dw_loc_descr_ref op0, ret, tmp;
15136 dw_loc_descr_ref l1jump, l1label;
15137 dw_loc_descr_ref l2jump, l2label;
15139 if (BITS_PER_UNIT != 8
15140 || (GET_MODE_BITSIZE (mode) != 32
15141 && GET_MODE_BITSIZE (mode) != 64))
15142 return NULL;
15144 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15145 VAR_INIT_STATUS_INITIALIZED);
15146 if (op0 == NULL)
15147 return NULL;
15149 ret = op0;
15150 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15151 mode, mem_mode,
15152 VAR_INIT_STATUS_INITIALIZED);
15153 if (tmp == NULL)
15154 return NULL;
15155 add_loc_descr (&ret, tmp);
15156 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15157 VAR_INIT_STATUS_INITIALIZED);
15158 if (tmp == NULL)
15159 return NULL;
15160 add_loc_descr (&ret, tmp);
15161 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15162 add_loc_descr (&ret, l1label);
15163 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15164 mode, mem_mode,
15165 VAR_INIT_STATUS_INITIALIZED);
15166 add_loc_descr (&ret, tmp);
15167 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15168 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15169 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15170 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15171 VAR_INIT_STATUS_INITIALIZED);
15172 if (tmp == NULL)
15173 return NULL;
15174 add_loc_descr (&ret, tmp);
15175 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15176 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15178 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15179 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15180 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15181 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15182 VAR_INIT_STATUS_INITIALIZED);
15183 add_loc_descr (&ret, tmp);
15184 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15185 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15186 add_loc_descr (&ret, l2jump);
15187 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15188 VAR_INIT_STATUS_INITIALIZED);
15189 add_loc_descr (&ret, tmp);
15190 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15191 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15192 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15193 add_loc_descr (&ret, l1jump);
15194 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15195 add_loc_descr (&ret, l2label);
15196 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15197 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15198 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15199 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15200 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15201 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15202 return ret;
15205 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15206 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15207 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15208 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15210 ROTATERT is similar:
15211 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15212 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15213 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15215 static dw_loc_descr_ref
15216 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15217 machine_mode mem_mode)
15219 rtx rtlop1 = XEXP (rtl, 1);
15220 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15221 int i;
15223 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15224 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15225 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15226 VAR_INIT_STATUS_INITIALIZED);
15227 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15228 VAR_INIT_STATUS_INITIALIZED);
15229 if (op0 == NULL || op1 == NULL)
15230 return NULL;
15231 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15232 for (i = 0; i < 2; i++)
15234 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15235 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15236 mode, mem_mode,
15237 VAR_INIT_STATUS_INITIALIZED);
15238 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15239 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15240 ? DW_OP_const4u
15241 : HOST_BITS_PER_WIDE_INT == 64
15242 ? DW_OP_const8u : DW_OP_constu,
15243 GET_MODE_MASK (mode), 0);
15244 else
15245 mask[i] = NULL;
15246 if (mask[i] == NULL)
15247 return NULL;
15248 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15250 ret = op0;
15251 add_loc_descr (&ret, op1);
15252 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15253 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15254 if (GET_CODE (rtl) == ROTATERT)
15256 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15257 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15258 GET_MODE_BITSIZE (mode), 0));
15260 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15261 if (mask[0] != NULL)
15262 add_loc_descr (&ret, mask[0]);
15263 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15264 if (mask[1] != NULL)
15266 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15267 add_loc_descr (&ret, mask[1]);
15268 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15270 if (GET_CODE (rtl) == ROTATE)
15272 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15273 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15274 GET_MODE_BITSIZE (mode), 0));
15276 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15277 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15278 return ret;
15281 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15282 for DEBUG_PARAMETER_REF RTL. */
15284 static dw_loc_descr_ref
15285 parameter_ref_descriptor (rtx rtl)
15287 dw_loc_descr_ref ret;
15288 dw_die_ref ref;
15290 if (dwarf_strict)
15291 return NULL;
15292 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15293 /* With LTO during LTRANS we get the late DIE that refers to the early
15294 DIE, thus we add another indirection here. This seems to confuse
15295 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15296 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15297 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15298 if (ref)
15300 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15301 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15302 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15304 else
15306 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15307 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15309 return ret;
15312 /* The following routine converts the RTL for a variable or parameter
15313 (resident in memory) into an equivalent Dwarf representation of a
15314 mechanism for getting the address of that same variable onto the top of a
15315 hypothetical "address evaluation" stack.
15317 When creating memory location descriptors, we are effectively transforming
15318 the RTL for a memory-resident object into its Dwarf postfix expression
15319 equivalent. This routine recursively descends an RTL tree, turning
15320 it into Dwarf postfix code as it goes.
15322 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15324 MEM_MODE is the mode of the memory reference, needed to handle some
15325 autoincrement addressing modes.
15327 Return 0 if we can't represent the location. */
15329 dw_loc_descr_ref
15330 mem_loc_descriptor (rtx rtl, machine_mode mode,
15331 machine_mode mem_mode,
15332 enum var_init_status initialized)
15334 dw_loc_descr_ref mem_loc_result = NULL;
15335 enum dwarf_location_atom op;
15336 dw_loc_descr_ref op0, op1;
15337 rtx inner = NULL_RTX;
15338 poly_int64 offset;
15340 if (mode == VOIDmode)
15341 mode = GET_MODE (rtl);
15343 /* Note that for a dynamically sized array, the location we will generate a
15344 description of here will be the lowest numbered location which is
15345 actually within the array. That's *not* necessarily the same as the
15346 zeroth element of the array. */
15348 rtl = targetm.delegitimize_address (rtl);
15350 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15351 return NULL;
15353 scalar_int_mode int_mode, inner_mode, op1_mode;
15354 switch (GET_CODE (rtl))
15356 case POST_INC:
15357 case POST_DEC:
15358 case POST_MODIFY:
15359 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15361 case SUBREG:
15362 /* The case of a subreg may arise when we have a local (register)
15363 variable or a formal (register) parameter which doesn't quite fill
15364 up an entire register. For now, just assume that it is
15365 legitimate to make the Dwarf info refer to the whole register which
15366 contains the given subreg. */
15367 if (!subreg_lowpart_p (rtl))
15368 break;
15369 inner = SUBREG_REG (rtl);
15370 /* FALLTHRU */
15371 case TRUNCATE:
15372 if (inner == NULL_RTX)
15373 inner = XEXP (rtl, 0);
15374 if (is_a <scalar_int_mode> (mode, &int_mode)
15375 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15376 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15377 #ifdef POINTERS_EXTEND_UNSIGNED
15378 || (int_mode == Pmode && mem_mode != VOIDmode)
15379 #endif
15381 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15383 mem_loc_result = mem_loc_descriptor (inner,
15384 inner_mode,
15385 mem_mode, initialized);
15386 break;
15388 if (dwarf_strict && dwarf_version < 5)
15389 break;
15390 if (is_a <scalar_int_mode> (mode, &int_mode)
15391 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15392 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15393 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15395 dw_die_ref type_die;
15396 dw_loc_descr_ref cvt;
15398 mem_loc_result = mem_loc_descriptor (inner,
15399 GET_MODE (inner),
15400 mem_mode, initialized);
15401 if (mem_loc_result == NULL)
15402 break;
15403 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15404 if (type_die == NULL)
15406 mem_loc_result = NULL;
15407 break;
15409 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15410 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15411 else
15412 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15413 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15414 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15415 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15416 add_loc_descr (&mem_loc_result, cvt);
15417 if (is_a <scalar_int_mode> (mode, &int_mode)
15418 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15420 /* Convert it to untyped afterwards. */
15421 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15422 add_loc_descr (&mem_loc_result, cvt);
15425 break;
15427 case REG:
15428 if (!is_a <scalar_int_mode> (mode, &int_mode)
15429 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15430 && rtl != arg_pointer_rtx
15431 && rtl != frame_pointer_rtx
15432 #ifdef POINTERS_EXTEND_UNSIGNED
15433 && (int_mode != Pmode || mem_mode == VOIDmode)
15434 #endif
15437 dw_die_ref type_die;
15438 unsigned int dbx_regnum;
15440 if (dwarf_strict && dwarf_version < 5)
15441 break;
15442 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
15443 break;
15444 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15445 if (type_die == NULL)
15446 break;
15448 dbx_regnum = dbx_reg_number (rtl);
15449 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15450 break;
15451 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15452 dbx_regnum, 0);
15453 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15454 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15455 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15456 break;
15458 /* Whenever a register number forms a part of the description of the
15459 method for calculating the (dynamic) address of a memory resident
15460 object, DWARF rules require the register number be referred to as
15461 a "base register". This distinction is not based in any way upon
15462 what category of register the hardware believes the given register
15463 belongs to. This is strictly DWARF terminology we're dealing with
15464 here. Note that in cases where the location of a memory-resident
15465 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15466 OP_CONST (0)) the actual DWARF location descriptor that we generate
15467 may just be OP_BASEREG (basereg). This may look deceptively like
15468 the object in question was allocated to a register (rather than in
15469 memory) so DWARF consumers need to be aware of the subtle
15470 distinction between OP_REG and OP_BASEREG. */
15471 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15472 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15473 else if (stack_realign_drap
15474 && crtl->drap_reg
15475 && crtl->args.internal_arg_pointer == rtl
15476 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15478 /* If RTL is internal_arg_pointer, which has been optimized
15479 out, use DRAP instead. */
15480 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15481 VAR_INIT_STATUS_INITIALIZED);
15483 break;
15485 case SIGN_EXTEND:
15486 case ZERO_EXTEND:
15487 if (!is_a <scalar_int_mode> (mode, &int_mode)
15488 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15489 break;
15490 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15491 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15492 if (op0 == 0)
15493 break;
15494 else if (GET_CODE (rtl) == ZERO_EXTEND
15495 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15496 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15497 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15498 to expand zero extend as two shifts instead of
15499 masking. */
15500 && GET_MODE_SIZE (inner_mode) <= 4)
15502 mem_loc_result = op0;
15503 add_loc_descr (&mem_loc_result,
15504 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15505 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15507 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15509 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15510 shift *= BITS_PER_UNIT;
15511 if (GET_CODE (rtl) == SIGN_EXTEND)
15512 op = DW_OP_shra;
15513 else
15514 op = DW_OP_shr;
15515 mem_loc_result = op0;
15516 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15517 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15518 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15519 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15521 else if (!dwarf_strict || dwarf_version >= 5)
15523 dw_die_ref type_die1, type_die2;
15524 dw_loc_descr_ref cvt;
15526 type_die1 = base_type_for_mode (inner_mode,
15527 GET_CODE (rtl) == ZERO_EXTEND);
15528 if (type_die1 == NULL)
15529 break;
15530 type_die2 = base_type_for_mode (int_mode, 1);
15531 if (type_die2 == NULL)
15532 break;
15533 mem_loc_result = op0;
15534 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15535 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15536 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15537 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15538 add_loc_descr (&mem_loc_result, cvt);
15539 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15540 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15541 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15542 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15543 add_loc_descr (&mem_loc_result, cvt);
15545 break;
15547 case MEM:
15549 rtx new_rtl = avoid_constant_pool_reference (rtl);
15550 if (new_rtl != rtl)
15552 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15553 initialized);
15554 if (mem_loc_result != NULL)
15555 return mem_loc_result;
15558 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15559 get_address_mode (rtl), mode,
15560 VAR_INIT_STATUS_INITIALIZED);
15561 if (mem_loc_result == NULL)
15562 mem_loc_result = tls_mem_loc_descriptor (rtl);
15563 if (mem_loc_result != NULL)
15565 if (!is_a <scalar_int_mode> (mode, &int_mode)
15566 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15568 dw_die_ref type_die;
15569 dw_loc_descr_ref deref;
15570 HOST_WIDE_INT size;
15572 if (dwarf_strict && dwarf_version < 5)
15573 return NULL;
15574 if (!GET_MODE_SIZE (mode).is_constant (&size))
15575 return NULL;
15576 type_die
15577 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15578 if (type_die == NULL)
15579 return NULL;
15580 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15581 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15582 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15583 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15584 add_loc_descr (&mem_loc_result, deref);
15586 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15587 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15588 else
15589 add_loc_descr (&mem_loc_result,
15590 new_loc_descr (DW_OP_deref_size,
15591 GET_MODE_SIZE (int_mode), 0));
15593 break;
15595 case LO_SUM:
15596 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15598 case LABEL_REF:
15599 /* Some ports can transform a symbol ref into a label ref, because
15600 the symbol ref is too far away and has to be dumped into a constant
15601 pool. */
15602 case CONST:
15603 case SYMBOL_REF:
15604 if (!is_a <scalar_int_mode> (mode, &int_mode)
15605 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15606 #ifdef POINTERS_EXTEND_UNSIGNED
15607 && (int_mode != Pmode || mem_mode == VOIDmode)
15608 #endif
15610 break;
15611 if (GET_CODE (rtl) == SYMBOL_REF
15612 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15614 dw_loc_descr_ref temp;
15616 /* If this is not defined, we have no way to emit the data. */
15617 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15618 break;
15620 temp = new_addr_loc_descr (rtl, dtprel_true);
15622 /* We check for DWARF 5 here because gdb did not implement
15623 DW_OP_form_tls_address until after 7.12. */
15624 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15625 ? DW_OP_form_tls_address
15626 : DW_OP_GNU_push_tls_address),
15627 0, 0);
15628 add_loc_descr (&mem_loc_result, temp);
15630 break;
15633 if (!const_ok_for_output (rtl))
15635 if (GET_CODE (rtl) == CONST)
15636 switch (GET_CODE (XEXP (rtl, 0)))
15638 case NOT:
15639 op = DW_OP_not;
15640 goto try_const_unop;
15641 case NEG:
15642 op = DW_OP_neg;
15643 goto try_const_unop;
15644 try_const_unop:
15645 rtx arg;
15646 arg = XEXP (XEXP (rtl, 0), 0);
15647 if (!CONSTANT_P (arg))
15648 arg = gen_rtx_CONST (int_mode, arg);
15649 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15650 initialized);
15651 if (op0)
15653 mem_loc_result = op0;
15654 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15656 break;
15657 default:
15658 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15659 mem_mode, initialized);
15660 break;
15662 break;
15665 symref:
15666 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15667 vec_safe_push (used_rtx_array, rtl);
15668 break;
15670 case CONCAT:
15671 case CONCATN:
15672 case VAR_LOCATION:
15673 case DEBUG_IMPLICIT_PTR:
15674 expansion_failed (NULL_TREE, rtl,
15675 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15676 return 0;
15678 case ENTRY_VALUE:
15679 if (dwarf_strict && dwarf_version < 5)
15680 return NULL;
15681 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15683 if (!is_a <scalar_int_mode> (mode, &int_mode)
15684 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15685 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15686 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15687 else
15689 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15690 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15691 return NULL;
15692 op0 = one_reg_loc_descriptor (dbx_regnum,
15693 VAR_INIT_STATUS_INITIALIZED);
15696 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15697 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15699 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15700 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15701 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15702 return NULL;
15704 else
15705 gcc_unreachable ();
15706 if (op0 == NULL)
15707 return NULL;
15708 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15709 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15710 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15711 break;
15713 case DEBUG_PARAMETER_REF:
15714 mem_loc_result = parameter_ref_descriptor (rtl);
15715 break;
15717 case PRE_MODIFY:
15718 /* Extract the PLUS expression nested inside and fall into
15719 PLUS code below. */
15720 rtl = XEXP (rtl, 1);
15721 goto plus;
15723 case PRE_INC:
15724 case PRE_DEC:
15725 /* Turn these into a PLUS expression and fall into the PLUS code
15726 below. */
15727 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15728 gen_int_mode (GET_CODE (rtl) == PRE_INC
15729 ? GET_MODE_UNIT_SIZE (mem_mode)
15730 : -GET_MODE_UNIT_SIZE (mem_mode),
15731 mode));
15733 /* fall through */
15735 case PLUS:
15736 plus:
15737 if (is_based_loc (rtl)
15738 && is_a <scalar_int_mode> (mode, &int_mode)
15739 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15740 || XEXP (rtl, 0) == arg_pointer_rtx
15741 || XEXP (rtl, 0) == frame_pointer_rtx))
15742 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15743 INTVAL (XEXP (rtl, 1)),
15744 VAR_INIT_STATUS_INITIALIZED);
15745 else
15747 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15748 VAR_INIT_STATUS_INITIALIZED);
15749 if (mem_loc_result == 0)
15750 break;
15752 if (CONST_INT_P (XEXP (rtl, 1))
15753 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15754 <= DWARF2_ADDR_SIZE))
15755 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15756 else
15758 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15759 VAR_INIT_STATUS_INITIALIZED);
15760 if (op1 == 0)
15761 return NULL;
15762 add_loc_descr (&mem_loc_result, op1);
15763 add_loc_descr (&mem_loc_result,
15764 new_loc_descr (DW_OP_plus, 0, 0));
15767 break;
15769 /* If a pseudo-reg is optimized away, it is possible for it to
15770 be replaced with a MEM containing a multiply or shift. */
15771 case MINUS:
15772 op = DW_OP_minus;
15773 goto do_binop;
15775 case MULT:
15776 op = DW_OP_mul;
15777 goto do_binop;
15779 case DIV:
15780 if ((!dwarf_strict || dwarf_version >= 5)
15781 && is_a <scalar_int_mode> (mode, &int_mode)
15782 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15784 mem_loc_result = typed_binop (DW_OP_div, rtl,
15785 base_type_for_mode (mode, 0),
15786 int_mode, mem_mode);
15787 break;
15789 op = DW_OP_div;
15790 goto do_binop;
15792 case UMOD:
15793 op = DW_OP_mod;
15794 goto do_binop;
15796 case ASHIFT:
15797 op = DW_OP_shl;
15798 goto do_shift;
15800 case ASHIFTRT:
15801 op = DW_OP_shra;
15802 goto do_shift;
15804 case LSHIFTRT:
15805 op = DW_OP_shr;
15806 goto do_shift;
15808 do_shift:
15809 if (!is_a <scalar_int_mode> (mode, &int_mode))
15810 break;
15811 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15812 VAR_INIT_STATUS_INITIALIZED);
15814 rtx rtlop1 = XEXP (rtl, 1);
15815 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15816 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15817 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15818 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15819 VAR_INIT_STATUS_INITIALIZED);
15822 if (op0 == 0 || op1 == 0)
15823 break;
15825 mem_loc_result = op0;
15826 add_loc_descr (&mem_loc_result, op1);
15827 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15828 break;
15830 case AND:
15831 op = DW_OP_and;
15832 goto do_binop;
15834 case IOR:
15835 op = DW_OP_or;
15836 goto do_binop;
15838 case XOR:
15839 op = DW_OP_xor;
15840 goto do_binop;
15842 do_binop:
15843 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15844 VAR_INIT_STATUS_INITIALIZED);
15845 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15846 VAR_INIT_STATUS_INITIALIZED);
15848 if (op0 == 0 || op1 == 0)
15849 break;
15851 mem_loc_result = op0;
15852 add_loc_descr (&mem_loc_result, op1);
15853 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15854 break;
15856 case MOD:
15857 if ((!dwarf_strict || dwarf_version >= 5)
15858 && is_a <scalar_int_mode> (mode, &int_mode)
15859 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15861 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15862 base_type_for_mode (mode, 0),
15863 int_mode, mem_mode);
15864 break;
15867 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15868 VAR_INIT_STATUS_INITIALIZED);
15869 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15870 VAR_INIT_STATUS_INITIALIZED);
15872 if (op0 == 0 || op1 == 0)
15873 break;
15875 mem_loc_result = op0;
15876 add_loc_descr (&mem_loc_result, op1);
15877 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
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_div, 0, 0));
15880 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15881 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15882 break;
15884 case UDIV:
15885 if ((!dwarf_strict || dwarf_version >= 5)
15886 && is_a <scalar_int_mode> (mode, &int_mode))
15888 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15890 op = DW_OP_div;
15891 goto do_binop;
15893 mem_loc_result = typed_binop (DW_OP_div, rtl,
15894 base_type_for_mode (int_mode, 1),
15895 int_mode, mem_mode);
15897 break;
15899 case NOT:
15900 op = DW_OP_not;
15901 goto do_unop;
15903 case ABS:
15904 op = DW_OP_abs;
15905 goto do_unop;
15907 case NEG:
15908 op = DW_OP_neg;
15909 goto do_unop;
15911 do_unop:
15912 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15913 VAR_INIT_STATUS_INITIALIZED);
15915 if (op0 == 0)
15916 break;
15918 mem_loc_result = op0;
15919 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15920 break;
15922 case CONST_INT:
15923 if (!is_a <scalar_int_mode> (mode, &int_mode)
15924 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15925 #ifdef POINTERS_EXTEND_UNSIGNED
15926 || (int_mode == Pmode
15927 && mem_mode != VOIDmode
15928 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15929 #endif
15932 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15933 break;
15935 if ((!dwarf_strict || dwarf_version >= 5)
15936 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15937 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15939 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15940 scalar_int_mode amode;
15941 if (type_die == NULL)
15942 return NULL;
15943 if (INTVAL (rtl) >= 0
15944 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15945 .exists (&amode))
15946 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15947 /* const DW_OP_convert <XXX> vs.
15948 DW_OP_const_type <XXX, 1, const>. */
15949 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15950 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15952 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15953 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15954 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15955 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15956 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15957 add_loc_descr (&mem_loc_result, op0);
15958 return mem_loc_result;
15960 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15961 INTVAL (rtl));
15962 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15963 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15964 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15965 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15966 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15967 else
15969 mem_loc_result->dw_loc_oprnd2.val_class
15970 = dw_val_class_const_double;
15971 mem_loc_result->dw_loc_oprnd2.v.val_double
15972 = double_int::from_shwi (INTVAL (rtl));
15975 break;
15977 case CONST_DOUBLE:
15978 if (!dwarf_strict || dwarf_version >= 5)
15980 dw_die_ref type_die;
15982 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15983 CONST_DOUBLE rtx could represent either a large integer
15984 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15985 the value is always a floating point constant.
15987 When it is an integer, a CONST_DOUBLE is used whenever
15988 the constant requires 2 HWIs to be adequately represented.
15989 We output CONST_DOUBLEs as blocks. */
15990 if (mode == VOIDmode
15991 || (GET_MODE (rtl) == VOIDmode
15992 && maybe_ne (GET_MODE_BITSIZE (mode),
15993 HOST_BITS_PER_DOUBLE_INT)))
15994 break;
15995 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15996 if (type_die == NULL)
15997 return NULL;
15998 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15999 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16000 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16001 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16002 #if TARGET_SUPPORTS_WIDE_INT == 0
16003 if (!SCALAR_FLOAT_MODE_P (mode))
16005 mem_loc_result->dw_loc_oprnd2.val_class
16006 = dw_val_class_const_double;
16007 mem_loc_result->dw_loc_oprnd2.v.val_double
16008 = rtx_to_double_int (rtl);
16010 else
16011 #endif
16013 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16014 unsigned int length = GET_MODE_SIZE (float_mode);
16015 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16017 insert_float (rtl, array);
16018 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16019 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16020 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16021 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16024 break;
16026 case CONST_WIDE_INT:
16027 if (!dwarf_strict || dwarf_version >= 5)
16029 dw_die_ref type_die;
16031 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16032 if (type_die == NULL)
16033 return NULL;
16034 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16035 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16036 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16037 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16038 mem_loc_result->dw_loc_oprnd2.val_class
16039 = dw_val_class_wide_int;
16040 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16041 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16043 break;
16045 case CONST_POLY_INT:
16046 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16047 break;
16049 case EQ:
16050 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16051 break;
16053 case GE:
16054 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16055 break;
16057 case GT:
16058 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16059 break;
16061 case LE:
16062 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16063 break;
16065 case LT:
16066 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16067 break;
16069 case NE:
16070 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16071 break;
16073 case GEU:
16074 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16075 break;
16077 case GTU:
16078 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16079 break;
16081 case LEU:
16082 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16083 break;
16085 case LTU:
16086 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16087 break;
16089 case UMIN:
16090 case UMAX:
16091 if (!SCALAR_INT_MODE_P (mode))
16092 break;
16093 /* FALLTHRU */
16094 case SMIN:
16095 case SMAX:
16096 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16097 break;
16099 case ZERO_EXTRACT:
16100 case SIGN_EXTRACT:
16101 if (CONST_INT_P (XEXP (rtl, 1))
16102 && CONST_INT_P (XEXP (rtl, 2))
16103 && is_a <scalar_int_mode> (mode, &int_mode)
16104 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16105 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16106 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16107 && ((unsigned) INTVAL (XEXP (rtl, 1))
16108 + (unsigned) INTVAL (XEXP (rtl, 2))
16109 <= GET_MODE_BITSIZE (int_mode)))
16111 int shift, size;
16112 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16113 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16114 if (op0 == 0)
16115 break;
16116 if (GET_CODE (rtl) == SIGN_EXTRACT)
16117 op = DW_OP_shra;
16118 else
16119 op = DW_OP_shr;
16120 mem_loc_result = op0;
16121 size = INTVAL (XEXP (rtl, 1));
16122 shift = INTVAL (XEXP (rtl, 2));
16123 if (BITS_BIG_ENDIAN)
16124 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16125 if (shift + size != (int) DWARF2_ADDR_SIZE)
16127 add_loc_descr (&mem_loc_result,
16128 int_loc_descriptor (DWARF2_ADDR_SIZE
16129 - shift - size));
16130 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16132 if (size != (int) DWARF2_ADDR_SIZE)
16134 add_loc_descr (&mem_loc_result,
16135 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16136 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16139 break;
16141 case IF_THEN_ELSE:
16143 dw_loc_descr_ref op2, bra_node, drop_node;
16144 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16145 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16146 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16147 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16148 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16149 VAR_INIT_STATUS_INITIALIZED);
16150 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16151 VAR_INIT_STATUS_INITIALIZED);
16152 if (op0 == NULL || op1 == NULL || op2 == NULL)
16153 break;
16155 mem_loc_result = op1;
16156 add_loc_descr (&mem_loc_result, op2);
16157 add_loc_descr (&mem_loc_result, op0);
16158 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16159 add_loc_descr (&mem_loc_result, bra_node);
16160 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16161 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16162 add_loc_descr (&mem_loc_result, drop_node);
16163 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16164 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16166 break;
16168 case FLOAT_EXTEND:
16169 case FLOAT_TRUNCATE:
16170 case FLOAT:
16171 case UNSIGNED_FLOAT:
16172 case FIX:
16173 case UNSIGNED_FIX:
16174 if (!dwarf_strict || dwarf_version >= 5)
16176 dw_die_ref type_die;
16177 dw_loc_descr_ref cvt;
16179 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16180 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16181 if (op0 == NULL)
16182 break;
16183 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16184 && (GET_CODE (rtl) == FLOAT
16185 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16187 type_die = base_type_for_mode (int_mode,
16188 GET_CODE (rtl) == UNSIGNED_FLOAT);
16189 if (type_die == NULL)
16190 break;
16191 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16192 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16193 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16194 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16195 add_loc_descr (&op0, cvt);
16197 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16198 if (type_die == NULL)
16199 break;
16200 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16201 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16202 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16203 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16204 add_loc_descr (&op0, cvt);
16205 if (is_a <scalar_int_mode> (mode, &int_mode)
16206 && (GET_CODE (rtl) == FIX
16207 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16209 op0 = convert_descriptor_to_mode (int_mode, op0);
16210 if (op0 == NULL)
16211 break;
16213 mem_loc_result = op0;
16215 break;
16217 case CLZ:
16218 case CTZ:
16219 case FFS:
16220 if (is_a <scalar_int_mode> (mode, &int_mode))
16221 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16222 break;
16224 case POPCOUNT:
16225 case PARITY:
16226 if (is_a <scalar_int_mode> (mode, &int_mode))
16227 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16228 break;
16230 case BSWAP:
16231 if (is_a <scalar_int_mode> (mode, &int_mode))
16232 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16233 break;
16235 case ROTATE:
16236 case ROTATERT:
16237 if (is_a <scalar_int_mode> (mode, &int_mode))
16238 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16239 break;
16241 case COMPARE:
16242 /* In theory, we could implement the above. */
16243 /* DWARF cannot represent the unsigned compare operations
16244 natively. */
16245 case SS_MULT:
16246 case US_MULT:
16247 case SS_DIV:
16248 case US_DIV:
16249 case SS_PLUS:
16250 case US_PLUS:
16251 case SS_MINUS:
16252 case US_MINUS:
16253 case SS_NEG:
16254 case US_NEG:
16255 case SS_ABS:
16256 case SS_ASHIFT:
16257 case US_ASHIFT:
16258 case SS_TRUNCATE:
16259 case US_TRUNCATE:
16260 case UNORDERED:
16261 case ORDERED:
16262 case UNEQ:
16263 case UNGE:
16264 case UNGT:
16265 case UNLE:
16266 case UNLT:
16267 case LTGT:
16268 case FRACT_CONVERT:
16269 case UNSIGNED_FRACT_CONVERT:
16270 case SAT_FRACT:
16271 case UNSIGNED_SAT_FRACT:
16272 case SQRT:
16273 case ASM_OPERANDS:
16274 case VEC_MERGE:
16275 case VEC_SELECT:
16276 case VEC_CONCAT:
16277 case VEC_DUPLICATE:
16278 case VEC_SERIES:
16279 case UNSPEC:
16280 case HIGH:
16281 case FMA:
16282 case STRICT_LOW_PART:
16283 case CONST_VECTOR:
16284 case CONST_FIXED:
16285 case CLRSB:
16286 case CLOBBER:
16287 case CLOBBER_HIGH:
16288 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16289 can't express it in the debug info. This can happen e.g. with some
16290 TLS UNSPECs. */
16291 break;
16293 case CONST_STRING:
16294 resolve_one_addr (&rtl);
16295 goto symref;
16297 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16298 the expression. An UNSPEC rtx represents a raw DWARF operation,
16299 new_loc_descr is called for it to build the operation directly.
16300 Otherwise mem_loc_descriptor is called recursively. */
16301 case PARALLEL:
16303 int index = 0;
16304 dw_loc_descr_ref exp_result = NULL;
16306 for (; index < XVECLEN (rtl, 0); index++)
16308 rtx elem = XVECEXP (rtl, 0, index);
16309 if (GET_CODE (elem) == UNSPEC)
16311 /* Each DWARF operation UNSPEC contain two operands, if
16312 one operand is not used for the operation, const0_rtx is
16313 passed. */
16314 gcc_assert (XVECLEN (elem, 0) == 2);
16316 HOST_WIDE_INT dw_op = XINT (elem, 1);
16317 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16318 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16319 exp_result
16320 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16321 oprnd2);
16323 else
16324 exp_result
16325 = mem_loc_descriptor (elem, mode, mem_mode,
16326 VAR_INIT_STATUS_INITIALIZED);
16328 if (!mem_loc_result)
16329 mem_loc_result = exp_result;
16330 else
16331 add_loc_descr (&mem_loc_result, exp_result);
16334 break;
16337 default:
16338 if (flag_checking)
16340 print_rtl (stderr, rtl);
16341 gcc_unreachable ();
16343 break;
16346 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16347 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16349 return mem_loc_result;
16352 /* Return a descriptor that describes the concatenation of two locations.
16353 This is typically a complex variable. */
16355 static dw_loc_descr_ref
16356 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16358 /* At present we only track constant-sized pieces. */
16359 unsigned int size0, size1;
16360 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16361 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16362 return 0;
16364 dw_loc_descr_ref cc_loc_result = NULL;
16365 dw_loc_descr_ref x0_ref
16366 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16367 dw_loc_descr_ref x1_ref
16368 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16370 if (x0_ref == 0 || x1_ref == 0)
16371 return 0;
16373 cc_loc_result = x0_ref;
16374 add_loc_descr_op_piece (&cc_loc_result, size0);
16376 add_loc_descr (&cc_loc_result, x1_ref);
16377 add_loc_descr_op_piece (&cc_loc_result, size1);
16379 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16380 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16382 return cc_loc_result;
16385 /* Return a descriptor that describes the concatenation of N
16386 locations. */
16388 static dw_loc_descr_ref
16389 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16391 unsigned int i;
16392 dw_loc_descr_ref cc_loc_result = NULL;
16393 unsigned int n = XVECLEN (concatn, 0);
16394 unsigned int size;
16396 for (i = 0; i < n; ++i)
16398 dw_loc_descr_ref ref;
16399 rtx x = XVECEXP (concatn, 0, i);
16401 /* At present we only track constant-sized pieces. */
16402 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16403 return NULL;
16405 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16406 if (ref == NULL)
16407 return NULL;
16409 add_loc_descr (&cc_loc_result, ref);
16410 add_loc_descr_op_piece (&cc_loc_result, size);
16413 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16414 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16416 return cc_loc_result;
16419 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16420 for DEBUG_IMPLICIT_PTR RTL. */
16422 static dw_loc_descr_ref
16423 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16425 dw_loc_descr_ref ret;
16426 dw_die_ref ref;
16428 if (dwarf_strict && dwarf_version < 5)
16429 return NULL;
16430 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16431 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16432 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16433 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16434 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16435 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16436 if (ref)
16438 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16439 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16440 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16442 else
16444 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16445 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16447 return ret;
16450 /* Output a proper Dwarf location descriptor for a variable or parameter
16451 which is either allocated in a register or in a memory location. For a
16452 register, we just generate an OP_REG and the register number. For a
16453 memory location we provide a Dwarf postfix expression describing how to
16454 generate the (dynamic) address of the object onto the address stack.
16456 MODE is mode of the decl if this loc_descriptor is going to be used in
16457 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16458 allowed, VOIDmode otherwise.
16460 If we don't know how to describe it, return 0. */
16462 static dw_loc_descr_ref
16463 loc_descriptor (rtx rtl, machine_mode mode,
16464 enum var_init_status initialized)
16466 dw_loc_descr_ref loc_result = NULL;
16467 scalar_int_mode int_mode;
16469 switch (GET_CODE (rtl))
16471 case SUBREG:
16472 /* The case of a subreg may arise when we have a local (register)
16473 variable or a formal (register) parameter which doesn't quite fill
16474 up an entire register. For now, just assume that it is
16475 legitimate to make the Dwarf info refer to the whole register which
16476 contains the given subreg. */
16477 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16478 loc_result = loc_descriptor (SUBREG_REG (rtl),
16479 GET_MODE (SUBREG_REG (rtl)), initialized);
16480 else
16481 goto do_default;
16482 break;
16484 case REG:
16485 loc_result = reg_loc_descriptor (rtl, initialized);
16486 break;
16488 case MEM:
16489 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16490 GET_MODE (rtl), initialized);
16491 if (loc_result == NULL)
16492 loc_result = tls_mem_loc_descriptor (rtl);
16493 if (loc_result == NULL)
16495 rtx new_rtl = avoid_constant_pool_reference (rtl);
16496 if (new_rtl != rtl)
16497 loc_result = loc_descriptor (new_rtl, mode, initialized);
16499 break;
16501 case CONCAT:
16502 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16503 initialized);
16504 break;
16506 case CONCATN:
16507 loc_result = concatn_loc_descriptor (rtl, initialized);
16508 break;
16510 case VAR_LOCATION:
16511 /* Single part. */
16512 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16514 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16515 if (GET_CODE (loc) == EXPR_LIST)
16516 loc = XEXP (loc, 0);
16517 loc_result = loc_descriptor (loc, mode, initialized);
16518 break;
16521 rtl = XEXP (rtl, 1);
16522 /* FALLTHRU */
16524 case PARALLEL:
16526 rtvec par_elems = XVEC (rtl, 0);
16527 int num_elem = GET_NUM_ELEM (par_elems);
16528 machine_mode mode;
16529 int i, size;
16531 /* Create the first one, so we have something to add to. */
16532 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16533 VOIDmode, initialized);
16534 if (loc_result == NULL)
16535 return NULL;
16536 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16537 /* At present we only track constant-sized pieces. */
16538 if (!GET_MODE_SIZE (mode).is_constant (&size))
16539 return NULL;
16540 add_loc_descr_op_piece (&loc_result, size);
16541 for (i = 1; i < num_elem; i++)
16543 dw_loc_descr_ref temp;
16545 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16546 VOIDmode, initialized);
16547 if (temp == NULL)
16548 return NULL;
16549 add_loc_descr (&loc_result, temp);
16550 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16551 /* At present we only track constant-sized pieces. */
16552 if (!GET_MODE_SIZE (mode).is_constant (&size))
16553 return NULL;
16554 add_loc_descr_op_piece (&loc_result, size);
16557 break;
16559 case CONST_INT:
16560 if (mode != VOIDmode && mode != BLKmode)
16562 int_mode = as_a <scalar_int_mode> (mode);
16563 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16564 INTVAL (rtl));
16566 break;
16568 case CONST_DOUBLE:
16569 if (mode == VOIDmode)
16570 mode = GET_MODE (rtl);
16572 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16574 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16576 /* Note that a CONST_DOUBLE rtx could represent either an integer
16577 or a floating-point constant. A CONST_DOUBLE is used whenever
16578 the constant requires more than one word in order to be
16579 adequately represented. We output CONST_DOUBLEs as blocks. */
16580 scalar_mode smode = as_a <scalar_mode> (mode);
16581 loc_result = new_loc_descr (DW_OP_implicit_value,
16582 GET_MODE_SIZE (smode), 0);
16583 #if TARGET_SUPPORTS_WIDE_INT == 0
16584 if (!SCALAR_FLOAT_MODE_P (smode))
16586 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16587 loc_result->dw_loc_oprnd2.v.val_double
16588 = rtx_to_double_int (rtl);
16590 else
16591 #endif
16593 unsigned int length = GET_MODE_SIZE (smode);
16594 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16596 insert_float (rtl, array);
16597 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16598 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16599 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16600 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16603 break;
16605 case CONST_WIDE_INT:
16606 if (mode == VOIDmode)
16607 mode = GET_MODE (rtl);
16609 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16611 int_mode = as_a <scalar_int_mode> (mode);
16612 loc_result = new_loc_descr (DW_OP_implicit_value,
16613 GET_MODE_SIZE (int_mode), 0);
16614 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16615 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16616 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16618 break;
16620 case CONST_VECTOR:
16621 if (mode == VOIDmode)
16622 mode = GET_MODE (rtl);
16624 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16626 unsigned int length;
16627 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16628 return NULL;
16630 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16631 unsigned char *array
16632 = ggc_vec_alloc<unsigned char> (length * elt_size);
16633 unsigned int i;
16634 unsigned char *p;
16635 machine_mode imode = GET_MODE_INNER (mode);
16637 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16638 switch (GET_MODE_CLASS (mode))
16640 case MODE_VECTOR_INT:
16641 for (i = 0, p = array; i < length; i++, p += elt_size)
16643 rtx elt = CONST_VECTOR_ELT (rtl, i);
16644 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16646 break;
16648 case MODE_VECTOR_FLOAT:
16649 for (i = 0, p = array; i < length; i++, p += elt_size)
16651 rtx elt = CONST_VECTOR_ELT (rtl, i);
16652 insert_float (elt, p);
16654 break;
16656 default:
16657 gcc_unreachable ();
16660 loc_result = new_loc_descr (DW_OP_implicit_value,
16661 length * elt_size, 0);
16662 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16663 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16664 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16665 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16667 break;
16669 case CONST:
16670 if (mode == VOIDmode
16671 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16672 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16673 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16675 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16676 break;
16678 /* FALLTHROUGH */
16679 case SYMBOL_REF:
16680 if (!const_ok_for_output (rtl))
16681 break;
16682 /* FALLTHROUGH */
16683 case LABEL_REF:
16684 if (is_a <scalar_int_mode> (mode, &int_mode)
16685 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16686 && (dwarf_version >= 4 || !dwarf_strict))
16688 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16689 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16690 vec_safe_push (used_rtx_array, rtl);
16692 break;
16694 case DEBUG_IMPLICIT_PTR:
16695 loc_result = implicit_ptr_descriptor (rtl, 0);
16696 break;
16698 case PLUS:
16699 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16700 && CONST_INT_P (XEXP (rtl, 1)))
16702 loc_result
16703 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16704 break;
16706 /* FALLTHRU */
16707 do_default:
16708 default:
16709 if ((is_a <scalar_int_mode> (mode, &int_mode)
16710 && GET_MODE (rtl) == int_mode
16711 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16712 && dwarf_version >= 4)
16713 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16715 /* Value expression. */
16716 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16717 if (loc_result)
16718 add_loc_descr (&loc_result,
16719 new_loc_descr (DW_OP_stack_value, 0, 0));
16721 break;
16724 return loc_result;
16727 /* We need to figure out what section we should use as the base for the
16728 address ranges where a given location is valid.
16729 1. If this particular DECL has a section associated with it, use that.
16730 2. If this function has a section associated with it, use that.
16731 3. Otherwise, use the text section.
16732 XXX: If you split a variable across multiple sections, we won't notice. */
16734 static const char *
16735 secname_for_decl (const_tree decl)
16737 const char *secname;
16739 if (VAR_OR_FUNCTION_DECL_P (decl)
16740 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16741 && DECL_SECTION_NAME (decl))
16742 secname = DECL_SECTION_NAME (decl);
16743 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16744 secname = DECL_SECTION_NAME (current_function_decl);
16745 else if (cfun && in_cold_section_p)
16746 secname = crtl->subsections.cold_section_label;
16747 else
16748 secname = text_section_label;
16750 return secname;
16753 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16755 static bool
16756 decl_by_reference_p (tree decl)
16758 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16759 || VAR_P (decl))
16760 && DECL_BY_REFERENCE (decl));
16763 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16764 for VARLOC. */
16766 static dw_loc_descr_ref
16767 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16768 enum var_init_status initialized)
16770 int have_address = 0;
16771 dw_loc_descr_ref descr;
16772 machine_mode mode;
16774 if (want_address != 2)
16776 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16777 /* Single part. */
16778 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16780 varloc = PAT_VAR_LOCATION_LOC (varloc);
16781 if (GET_CODE (varloc) == EXPR_LIST)
16782 varloc = XEXP (varloc, 0);
16783 mode = GET_MODE (varloc);
16784 if (MEM_P (varloc))
16786 rtx addr = XEXP (varloc, 0);
16787 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16788 mode, initialized);
16789 if (descr)
16790 have_address = 1;
16791 else
16793 rtx x = avoid_constant_pool_reference (varloc);
16794 if (x != varloc)
16795 descr = mem_loc_descriptor (x, mode, VOIDmode,
16796 initialized);
16799 else
16800 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16802 else
16803 return 0;
16805 else
16807 if (GET_CODE (varloc) == VAR_LOCATION)
16808 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16809 else
16810 mode = DECL_MODE (loc);
16811 descr = loc_descriptor (varloc, mode, initialized);
16812 have_address = 1;
16815 if (!descr)
16816 return 0;
16818 if (want_address == 2 && !have_address
16819 && (dwarf_version >= 4 || !dwarf_strict))
16821 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16823 expansion_failed (loc, NULL_RTX,
16824 "DWARF address size mismatch");
16825 return 0;
16827 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16828 have_address = 1;
16830 /* Show if we can't fill the request for an address. */
16831 if (want_address && !have_address)
16833 expansion_failed (loc, NULL_RTX,
16834 "Want address and only have value");
16835 return 0;
16838 /* If we've got an address and don't want one, dereference. */
16839 if (!want_address && have_address)
16841 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16842 enum dwarf_location_atom op;
16844 if (size > DWARF2_ADDR_SIZE || size == -1)
16846 expansion_failed (loc, NULL_RTX,
16847 "DWARF address size mismatch");
16848 return 0;
16850 else if (size == DWARF2_ADDR_SIZE)
16851 op = DW_OP_deref;
16852 else
16853 op = DW_OP_deref_size;
16855 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16858 return descr;
16861 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16862 if it is not possible. */
16864 static dw_loc_descr_ref
16865 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16867 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16868 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16869 else if (dwarf_version >= 3 || !dwarf_strict)
16870 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16871 else
16872 return NULL;
16875 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16876 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16878 static dw_loc_descr_ref
16879 dw_sra_loc_expr (tree decl, rtx loc)
16881 rtx p;
16882 unsigned HOST_WIDE_INT padsize = 0;
16883 dw_loc_descr_ref descr, *descr_tail;
16884 unsigned HOST_WIDE_INT decl_size;
16885 rtx varloc;
16886 enum var_init_status initialized;
16888 if (DECL_SIZE (decl) == NULL
16889 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16890 return NULL;
16892 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16893 descr = NULL;
16894 descr_tail = &descr;
16896 for (p = loc; p; p = XEXP (p, 1))
16898 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16899 rtx loc_note = *decl_piece_varloc_ptr (p);
16900 dw_loc_descr_ref cur_descr;
16901 dw_loc_descr_ref *tail, last = NULL;
16902 unsigned HOST_WIDE_INT opsize = 0;
16904 if (loc_note == NULL_RTX
16905 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16907 padsize += bitsize;
16908 continue;
16910 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16911 varloc = NOTE_VAR_LOCATION (loc_note);
16912 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16913 if (cur_descr == NULL)
16915 padsize += bitsize;
16916 continue;
16919 /* Check that cur_descr either doesn't use
16920 DW_OP_*piece operations, or their sum is equal
16921 to bitsize. Otherwise we can't embed it. */
16922 for (tail = &cur_descr; *tail != NULL;
16923 tail = &(*tail)->dw_loc_next)
16924 if ((*tail)->dw_loc_opc == DW_OP_piece)
16926 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16927 * BITS_PER_UNIT;
16928 last = *tail;
16930 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16932 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16933 last = *tail;
16936 if (last != NULL && opsize != bitsize)
16938 padsize += bitsize;
16939 /* Discard the current piece of the descriptor and release any
16940 addr_table entries it uses. */
16941 remove_loc_list_addr_table_entries (cur_descr);
16942 continue;
16945 /* If there is a hole, add DW_OP_*piece after empty DWARF
16946 expression, which means that those bits are optimized out. */
16947 if (padsize)
16949 if (padsize > decl_size)
16951 remove_loc_list_addr_table_entries (cur_descr);
16952 goto discard_descr;
16954 decl_size -= padsize;
16955 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16956 if (*descr_tail == NULL)
16958 remove_loc_list_addr_table_entries (cur_descr);
16959 goto discard_descr;
16961 descr_tail = &(*descr_tail)->dw_loc_next;
16962 padsize = 0;
16964 *descr_tail = cur_descr;
16965 descr_tail = tail;
16966 if (bitsize > decl_size)
16967 goto discard_descr;
16968 decl_size -= bitsize;
16969 if (last == NULL)
16971 HOST_WIDE_INT offset = 0;
16972 if (GET_CODE (varloc) == VAR_LOCATION
16973 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16975 varloc = PAT_VAR_LOCATION_LOC (varloc);
16976 if (GET_CODE (varloc) == EXPR_LIST)
16977 varloc = XEXP (varloc, 0);
16981 if (GET_CODE (varloc) == CONST
16982 || GET_CODE (varloc) == SIGN_EXTEND
16983 || GET_CODE (varloc) == ZERO_EXTEND)
16984 varloc = XEXP (varloc, 0);
16985 else if (GET_CODE (varloc) == SUBREG)
16986 varloc = SUBREG_REG (varloc);
16987 else
16988 break;
16990 while (1);
16991 /* DW_OP_bit_size offset should be zero for register
16992 or implicit location descriptions and empty location
16993 descriptions, but for memory addresses needs big endian
16994 adjustment. */
16995 if (MEM_P (varloc))
16997 unsigned HOST_WIDE_INT memsize;
16998 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16999 goto discard_descr;
17000 memsize *= BITS_PER_UNIT;
17001 if (memsize != bitsize)
17003 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17004 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17005 goto discard_descr;
17006 if (memsize < bitsize)
17007 goto discard_descr;
17008 if (BITS_BIG_ENDIAN)
17009 offset = memsize - bitsize;
17013 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17014 if (*descr_tail == NULL)
17015 goto discard_descr;
17016 descr_tail = &(*descr_tail)->dw_loc_next;
17020 /* If there were any non-empty expressions, add padding till the end of
17021 the decl. */
17022 if (descr != NULL && decl_size != 0)
17024 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17025 if (*descr_tail == NULL)
17026 goto discard_descr;
17028 return descr;
17030 discard_descr:
17031 /* Discard the descriptor and release any addr_table entries it uses. */
17032 remove_loc_list_addr_table_entries (descr);
17033 return NULL;
17036 /* Return the dwarf representation of the location list LOC_LIST of
17037 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17038 function. */
17040 static dw_loc_list_ref
17041 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17043 const char *endname, *secname;
17044 var_loc_view endview;
17045 rtx varloc;
17046 enum var_init_status initialized;
17047 struct var_loc_node *node;
17048 dw_loc_descr_ref descr;
17049 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17050 dw_loc_list_ref list = NULL;
17051 dw_loc_list_ref *listp = &list;
17053 /* Now that we know what section we are using for a base,
17054 actually construct the list of locations.
17055 The first location information is what is passed to the
17056 function that creates the location list, and the remaining
17057 locations just get added on to that list.
17058 Note that we only know the start address for a location
17059 (IE location changes), so to build the range, we use
17060 the range [current location start, next location start].
17061 This means we have to special case the last node, and generate
17062 a range of [last location start, end of function label]. */
17064 if (cfun && crtl->has_bb_partition)
17066 bool save_in_cold_section_p = in_cold_section_p;
17067 in_cold_section_p = first_function_block_is_cold;
17068 if (loc_list->last_before_switch == NULL)
17069 in_cold_section_p = !in_cold_section_p;
17070 secname = secname_for_decl (decl);
17071 in_cold_section_p = save_in_cold_section_p;
17073 else
17074 secname = secname_for_decl (decl);
17076 for (node = loc_list->first; node; node = node->next)
17078 bool range_across_switch = false;
17079 if (GET_CODE (node->loc) == EXPR_LIST
17080 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17082 if (GET_CODE (node->loc) == EXPR_LIST)
17084 descr = NULL;
17085 /* This requires DW_OP_{,bit_}piece, which is not usable
17086 inside DWARF expressions. */
17087 if (want_address == 2)
17088 descr = dw_sra_loc_expr (decl, node->loc);
17090 else
17092 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17093 varloc = NOTE_VAR_LOCATION (node->loc);
17094 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17096 if (descr)
17098 /* If section switch happens in between node->label
17099 and node->next->label (or end of function) and
17100 we can't emit it as a single entry list,
17101 emit two ranges, first one ending at the end
17102 of first partition and second one starting at the
17103 beginning of second partition. */
17104 if (node == loc_list->last_before_switch
17105 && (node != loc_list->first || loc_list->first->next
17106 /* If we are to emit a view number, we will emit
17107 a loclist rather than a single location
17108 expression for the entire function (see
17109 loc_list_has_views), so we have to split the
17110 range that straddles across partitions. */
17111 || !ZERO_VIEW_P (node->view))
17112 && current_function_decl)
17114 endname = cfun->fde->dw_fde_end;
17115 endview = 0;
17116 range_across_switch = true;
17118 /* The variable has a location between NODE->LABEL and
17119 NODE->NEXT->LABEL. */
17120 else if (node->next)
17121 endname = node->next->label, endview = node->next->view;
17122 /* If the variable has a location at the last label
17123 it keeps its location until the end of function. */
17124 else if (!current_function_decl)
17125 endname = text_end_label, endview = 0;
17126 else
17128 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17129 current_function_funcdef_no);
17130 endname = ggc_strdup (label_id);
17131 endview = 0;
17134 *listp = new_loc_list (descr, node->label, node->view,
17135 endname, endview, secname);
17136 if (TREE_CODE (decl) == PARM_DECL
17137 && node == loc_list->first
17138 && NOTE_P (node->loc)
17139 && strcmp (node->label, endname) == 0)
17140 (*listp)->force = true;
17141 listp = &(*listp)->dw_loc_next;
17145 if (cfun
17146 && crtl->has_bb_partition
17147 && node == loc_list->last_before_switch)
17149 bool save_in_cold_section_p = in_cold_section_p;
17150 in_cold_section_p = !first_function_block_is_cold;
17151 secname = secname_for_decl (decl);
17152 in_cold_section_p = save_in_cold_section_p;
17155 if (range_across_switch)
17157 if (GET_CODE (node->loc) == EXPR_LIST)
17158 descr = dw_sra_loc_expr (decl, node->loc);
17159 else
17161 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17162 varloc = NOTE_VAR_LOCATION (node->loc);
17163 descr = dw_loc_list_1 (decl, varloc, want_address,
17164 initialized);
17166 gcc_assert (descr);
17167 /* The variable has a location between NODE->LABEL and
17168 NODE->NEXT->LABEL. */
17169 if (node->next)
17170 endname = node->next->label, endview = node->next->view;
17171 else
17172 endname = cfun->fde->dw_fde_second_end, endview = 0;
17173 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17174 endname, endview, secname);
17175 listp = &(*listp)->dw_loc_next;
17179 /* Try to avoid the overhead of a location list emitting a location
17180 expression instead, but only if we didn't have more than one
17181 location entry in the first place. If some entries were not
17182 representable, we don't want to pretend a single entry that was
17183 applies to the entire scope in which the variable is
17184 available. */
17185 if (list && loc_list->first->next)
17186 gen_llsym (list);
17187 else
17188 maybe_gen_llsym (list);
17190 return list;
17193 /* Return if the loc_list has only single element and thus can be represented
17194 as location description. */
17196 static bool
17197 single_element_loc_list_p (dw_loc_list_ref list)
17199 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17200 return !list->ll_symbol;
17203 /* Duplicate a single element of location list. */
17205 static inline dw_loc_descr_ref
17206 copy_loc_descr (dw_loc_descr_ref ref)
17208 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17209 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17210 return copy;
17213 /* To each location in list LIST append loc descr REF. */
17215 static void
17216 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17218 dw_loc_descr_ref copy;
17219 add_loc_descr (&list->expr, ref);
17220 list = list->dw_loc_next;
17221 while (list)
17223 copy = copy_loc_descr (ref);
17224 add_loc_descr (&list->expr, copy);
17225 while (copy->dw_loc_next)
17226 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17227 list = list->dw_loc_next;
17231 /* To each location in list LIST prepend loc descr REF. */
17233 static void
17234 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17236 dw_loc_descr_ref copy;
17237 dw_loc_descr_ref ref_end = list->expr;
17238 add_loc_descr (&ref, list->expr);
17239 list->expr = ref;
17240 list = list->dw_loc_next;
17241 while (list)
17243 dw_loc_descr_ref end = list->expr;
17244 list->expr = copy = copy_loc_descr (ref);
17245 while (copy->dw_loc_next != ref_end)
17246 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17247 copy->dw_loc_next = end;
17248 list = list->dw_loc_next;
17252 /* Given two lists RET and LIST
17253 produce location list that is result of adding expression in LIST
17254 to expression in RET on each position in program.
17255 Might be destructive on both RET and LIST.
17257 TODO: We handle only simple cases of RET or LIST having at most one
17258 element. General case would involve sorting the lists in program order
17259 and merging them that will need some additional work.
17260 Adding that will improve quality of debug info especially for SRA-ed
17261 structures. */
17263 static void
17264 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17266 if (!list)
17267 return;
17268 if (!*ret)
17270 *ret = list;
17271 return;
17273 if (!list->dw_loc_next)
17275 add_loc_descr_to_each (*ret, list->expr);
17276 return;
17278 if (!(*ret)->dw_loc_next)
17280 prepend_loc_descr_to_each (list, (*ret)->expr);
17281 *ret = list;
17282 return;
17284 expansion_failed (NULL_TREE, NULL_RTX,
17285 "Don't know how to merge two non-trivial"
17286 " location lists.\n");
17287 *ret = NULL;
17288 return;
17291 /* LOC is constant expression. Try a luck, look it up in constant
17292 pool and return its loc_descr of its address. */
17294 static dw_loc_descr_ref
17295 cst_pool_loc_descr (tree loc)
17297 /* Get an RTL for this, if something has been emitted. */
17298 rtx rtl = lookup_constant_def (loc);
17300 if (!rtl || !MEM_P (rtl))
17302 gcc_assert (!rtl);
17303 return 0;
17305 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17307 /* TODO: We might get more coverage if we was actually delaying expansion
17308 of all expressions till end of compilation when constant pools are fully
17309 populated. */
17310 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17312 expansion_failed (loc, NULL_RTX,
17313 "CST value in contant pool but not marked.");
17314 return 0;
17316 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17317 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17320 /* Return dw_loc_list representing address of addr_expr LOC
17321 by looking for inner INDIRECT_REF expression and turning
17322 it into simple arithmetics.
17324 See loc_list_from_tree for the meaning of CONTEXT. */
17326 static dw_loc_list_ref
17327 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17328 loc_descr_context *context)
17330 tree obj, offset;
17331 poly_int64 bitsize, bitpos, bytepos;
17332 machine_mode mode;
17333 int unsignedp, reversep, volatilep = 0;
17334 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17336 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17337 &bitsize, &bitpos, &offset, &mode,
17338 &unsignedp, &reversep, &volatilep);
17339 STRIP_NOPS (obj);
17340 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17342 expansion_failed (loc, NULL_RTX, "bitfield access");
17343 return 0;
17345 if (!INDIRECT_REF_P (obj))
17347 expansion_failed (obj,
17348 NULL_RTX, "no indirect ref in inner refrence");
17349 return 0;
17351 if (!offset && known_eq (bitpos, 0))
17352 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17353 context);
17354 else if (toplev
17355 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17356 && (dwarf_version >= 4 || !dwarf_strict))
17358 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17359 if (!list_ret)
17360 return 0;
17361 if (offset)
17363 /* Variable offset. */
17364 list_ret1 = loc_list_from_tree (offset, 0, context);
17365 if (list_ret1 == 0)
17366 return 0;
17367 add_loc_list (&list_ret, list_ret1);
17368 if (!list_ret)
17369 return 0;
17370 add_loc_descr_to_each (list_ret,
17371 new_loc_descr (DW_OP_plus, 0, 0));
17373 HOST_WIDE_INT value;
17374 if (bytepos.is_constant (&value) && value > 0)
17375 add_loc_descr_to_each (list_ret,
17376 new_loc_descr (DW_OP_plus_uconst, value, 0));
17377 else if (maybe_ne (bytepos, 0))
17378 loc_list_plus_const (list_ret, bytepos);
17379 add_loc_descr_to_each (list_ret,
17380 new_loc_descr (DW_OP_stack_value, 0, 0));
17382 return list_ret;
17385 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17386 all operations from LOC are nops, move to the last one. Insert in NOPS all
17387 operations that are skipped. */
17389 static void
17390 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17391 hash_set<dw_loc_descr_ref> &nops)
17393 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17395 nops.add (loc);
17396 loc = loc->dw_loc_next;
17400 /* Helper for loc_descr_without_nops: free the location description operation
17401 P. */
17403 bool
17404 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17406 ggc_free (loc);
17407 return true;
17410 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17411 finishes LOC. */
17413 static void
17414 loc_descr_without_nops (dw_loc_descr_ref &loc)
17416 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17417 return;
17419 /* Set of all DW_OP_nop operations we remove. */
17420 hash_set<dw_loc_descr_ref> nops;
17422 /* First, strip all prefix NOP operations in order to keep the head of the
17423 operations list. */
17424 loc_descr_to_next_no_nop (loc, nops);
17426 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17428 /* For control flow operations: strip "prefix" nops in destination
17429 labels. */
17430 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17431 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17432 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17433 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17435 /* Do the same for the operations that follow, then move to the next
17436 iteration. */
17437 if (cur->dw_loc_next != NULL)
17438 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17439 cur = cur->dw_loc_next;
17442 nops.traverse<void *, free_loc_descr> (NULL);
17446 struct dwarf_procedure_info;
17448 /* Helper structure for location descriptions generation. */
17449 struct loc_descr_context
17451 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17452 NULL_TREE if DW_OP_push_object_address in invalid for this location
17453 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17454 tree context_type;
17455 /* The ..._DECL node that should be translated as a
17456 DW_OP_push_object_address operation. */
17457 tree base_decl;
17458 /* Information about the DWARF procedure we are currently generating. NULL if
17459 we are not generating a DWARF procedure. */
17460 struct dwarf_procedure_info *dpi;
17461 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17462 by consumer. Used for DW_TAG_generic_subrange attributes. */
17463 bool placeholder_arg;
17464 /* True if PLACEHOLDER_EXPR has been seen. */
17465 bool placeholder_seen;
17468 /* DWARF procedures generation
17470 DWARF expressions (aka. location descriptions) are used to encode variable
17471 things such as sizes or offsets. Such computations can have redundant parts
17472 that can be factorized in order to reduce the size of the output debug
17473 information. This is the whole point of DWARF procedures.
17475 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17476 already factorized into functions ("size functions") in order to handle very
17477 big and complex types. Such functions are quite simple: they have integral
17478 arguments, they return an integral result and their body contains only a
17479 return statement with arithmetic expressions. This is the only kind of
17480 function we are interested in translating into DWARF procedures, here.
17482 DWARF expressions and DWARF procedure are executed using a stack, so we have
17483 to define some calling convention for them to interact. Let's say that:
17485 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17486 all arguments in reverse order (right-to-left) so that when the DWARF
17487 procedure execution starts, the first argument is the top of the stack.
17489 - Then, when returning, the DWARF procedure must have consumed all arguments
17490 on the stack, must have pushed the result and touched nothing else.
17492 - Each integral argument and the result are integral types can be hold in a
17493 single stack slot.
17495 - We call "frame offset" the number of stack slots that are "under DWARF
17496 procedure control": it includes the arguments slots, the temporaries and
17497 the result slot. Thus, it is equal to the number of arguments when the
17498 procedure execution starts and must be equal to one (the result) when it
17499 returns. */
17501 /* Helper structure used when generating operations for a DWARF procedure. */
17502 struct dwarf_procedure_info
17504 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17505 currently translated. */
17506 tree fndecl;
17507 /* The number of arguments FNDECL takes. */
17508 unsigned args_count;
17511 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17512 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17513 equate it to this DIE. */
17515 static dw_die_ref
17516 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17517 dw_die_ref parent_die)
17519 dw_die_ref dwarf_proc_die;
17521 if ((dwarf_version < 3 && dwarf_strict)
17522 || location == NULL)
17523 return NULL;
17525 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17526 if (fndecl)
17527 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17528 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17529 return dwarf_proc_die;
17532 /* Return whether TYPE is a supported type as a DWARF procedure argument
17533 type or return type (we handle only scalar types and pointer types that
17534 aren't wider than the DWARF expression evaluation stack. */
17536 static bool
17537 is_handled_procedure_type (tree type)
17539 return ((INTEGRAL_TYPE_P (type)
17540 || TREE_CODE (type) == OFFSET_TYPE
17541 || TREE_CODE (type) == POINTER_TYPE)
17542 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17545 /* Helper for resolve_args_picking: do the same but stop when coming across
17546 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17547 offset *before* evaluating the corresponding operation. */
17549 static bool
17550 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17551 struct dwarf_procedure_info *dpi,
17552 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17554 /* The "frame_offset" identifier is already used to name a macro... */
17555 unsigned frame_offset_ = initial_frame_offset;
17556 dw_loc_descr_ref l;
17558 for (l = loc; l != NULL;)
17560 bool existed;
17561 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17563 /* If we already met this node, there is nothing to compute anymore. */
17564 if (existed)
17566 /* Make sure that the stack size is consistent wherever the execution
17567 flow comes from. */
17568 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17569 break;
17571 l_frame_offset = frame_offset_;
17573 /* If needed, relocate the picking offset with respect to the frame
17574 offset. */
17575 if (l->frame_offset_rel)
17577 unsigned HOST_WIDE_INT off;
17578 switch (l->dw_loc_opc)
17580 case DW_OP_pick:
17581 off = l->dw_loc_oprnd1.v.val_unsigned;
17582 break;
17583 case DW_OP_dup:
17584 off = 0;
17585 break;
17586 case DW_OP_over:
17587 off = 1;
17588 break;
17589 default:
17590 gcc_unreachable ();
17592 /* frame_offset_ is the size of the current stack frame, including
17593 incoming arguments. Besides, the arguments are pushed
17594 right-to-left. Thus, in order to access the Nth argument from
17595 this operation node, the picking has to skip temporaries *plus*
17596 one stack slot per argument (0 for the first one, 1 for the second
17597 one, etc.).
17599 The targetted argument number (N) is already set as the operand,
17600 and the number of temporaries can be computed with:
17601 frame_offsets_ - dpi->args_count */
17602 off += frame_offset_ - dpi->args_count;
17604 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17605 if (off > 255)
17606 return false;
17608 if (off == 0)
17610 l->dw_loc_opc = DW_OP_dup;
17611 l->dw_loc_oprnd1.v.val_unsigned = 0;
17613 else if (off == 1)
17615 l->dw_loc_opc = DW_OP_over;
17616 l->dw_loc_oprnd1.v.val_unsigned = 0;
17618 else
17620 l->dw_loc_opc = DW_OP_pick;
17621 l->dw_loc_oprnd1.v.val_unsigned = off;
17625 /* Update frame_offset according to the effect the current operation has
17626 on the stack. */
17627 switch (l->dw_loc_opc)
17629 case DW_OP_deref:
17630 case DW_OP_swap:
17631 case DW_OP_rot:
17632 case DW_OP_abs:
17633 case DW_OP_neg:
17634 case DW_OP_not:
17635 case DW_OP_plus_uconst:
17636 case DW_OP_skip:
17637 case DW_OP_reg0:
17638 case DW_OP_reg1:
17639 case DW_OP_reg2:
17640 case DW_OP_reg3:
17641 case DW_OP_reg4:
17642 case DW_OP_reg5:
17643 case DW_OP_reg6:
17644 case DW_OP_reg7:
17645 case DW_OP_reg8:
17646 case DW_OP_reg9:
17647 case DW_OP_reg10:
17648 case DW_OP_reg11:
17649 case DW_OP_reg12:
17650 case DW_OP_reg13:
17651 case DW_OP_reg14:
17652 case DW_OP_reg15:
17653 case DW_OP_reg16:
17654 case DW_OP_reg17:
17655 case DW_OP_reg18:
17656 case DW_OP_reg19:
17657 case DW_OP_reg20:
17658 case DW_OP_reg21:
17659 case DW_OP_reg22:
17660 case DW_OP_reg23:
17661 case DW_OP_reg24:
17662 case DW_OP_reg25:
17663 case DW_OP_reg26:
17664 case DW_OP_reg27:
17665 case DW_OP_reg28:
17666 case DW_OP_reg29:
17667 case DW_OP_reg30:
17668 case DW_OP_reg31:
17669 case DW_OP_bregx:
17670 case DW_OP_piece:
17671 case DW_OP_deref_size:
17672 case DW_OP_nop:
17673 case DW_OP_bit_piece:
17674 case DW_OP_implicit_value:
17675 case DW_OP_stack_value:
17676 break;
17678 case DW_OP_addr:
17679 case DW_OP_const1u:
17680 case DW_OP_const1s:
17681 case DW_OP_const2u:
17682 case DW_OP_const2s:
17683 case DW_OP_const4u:
17684 case DW_OP_const4s:
17685 case DW_OP_const8u:
17686 case DW_OP_const8s:
17687 case DW_OP_constu:
17688 case DW_OP_consts:
17689 case DW_OP_dup:
17690 case DW_OP_over:
17691 case DW_OP_pick:
17692 case DW_OP_lit0:
17693 case DW_OP_lit1:
17694 case DW_OP_lit2:
17695 case DW_OP_lit3:
17696 case DW_OP_lit4:
17697 case DW_OP_lit5:
17698 case DW_OP_lit6:
17699 case DW_OP_lit7:
17700 case DW_OP_lit8:
17701 case DW_OP_lit9:
17702 case DW_OP_lit10:
17703 case DW_OP_lit11:
17704 case DW_OP_lit12:
17705 case DW_OP_lit13:
17706 case DW_OP_lit14:
17707 case DW_OP_lit15:
17708 case DW_OP_lit16:
17709 case DW_OP_lit17:
17710 case DW_OP_lit18:
17711 case DW_OP_lit19:
17712 case DW_OP_lit20:
17713 case DW_OP_lit21:
17714 case DW_OP_lit22:
17715 case DW_OP_lit23:
17716 case DW_OP_lit24:
17717 case DW_OP_lit25:
17718 case DW_OP_lit26:
17719 case DW_OP_lit27:
17720 case DW_OP_lit28:
17721 case DW_OP_lit29:
17722 case DW_OP_lit30:
17723 case DW_OP_lit31:
17724 case DW_OP_breg0:
17725 case DW_OP_breg1:
17726 case DW_OP_breg2:
17727 case DW_OP_breg3:
17728 case DW_OP_breg4:
17729 case DW_OP_breg5:
17730 case DW_OP_breg6:
17731 case DW_OP_breg7:
17732 case DW_OP_breg8:
17733 case DW_OP_breg9:
17734 case DW_OP_breg10:
17735 case DW_OP_breg11:
17736 case DW_OP_breg12:
17737 case DW_OP_breg13:
17738 case DW_OP_breg14:
17739 case DW_OP_breg15:
17740 case DW_OP_breg16:
17741 case DW_OP_breg17:
17742 case DW_OP_breg18:
17743 case DW_OP_breg19:
17744 case DW_OP_breg20:
17745 case DW_OP_breg21:
17746 case DW_OP_breg22:
17747 case DW_OP_breg23:
17748 case DW_OP_breg24:
17749 case DW_OP_breg25:
17750 case DW_OP_breg26:
17751 case DW_OP_breg27:
17752 case DW_OP_breg28:
17753 case DW_OP_breg29:
17754 case DW_OP_breg30:
17755 case DW_OP_breg31:
17756 case DW_OP_fbreg:
17757 case DW_OP_push_object_address:
17758 case DW_OP_call_frame_cfa:
17759 case DW_OP_GNU_variable_value:
17760 ++frame_offset_;
17761 break;
17763 case DW_OP_drop:
17764 case DW_OP_xderef:
17765 case DW_OP_and:
17766 case DW_OP_div:
17767 case DW_OP_minus:
17768 case DW_OP_mod:
17769 case DW_OP_mul:
17770 case DW_OP_or:
17771 case DW_OP_plus:
17772 case DW_OP_shl:
17773 case DW_OP_shr:
17774 case DW_OP_shra:
17775 case DW_OP_xor:
17776 case DW_OP_bra:
17777 case DW_OP_eq:
17778 case DW_OP_ge:
17779 case DW_OP_gt:
17780 case DW_OP_le:
17781 case DW_OP_lt:
17782 case DW_OP_ne:
17783 case DW_OP_regx:
17784 case DW_OP_xderef_size:
17785 --frame_offset_;
17786 break;
17788 case DW_OP_call2:
17789 case DW_OP_call4:
17790 case DW_OP_call_ref:
17792 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17793 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17795 if (stack_usage == NULL)
17796 return false;
17797 frame_offset_ += *stack_usage;
17798 break;
17801 case DW_OP_implicit_pointer:
17802 case DW_OP_entry_value:
17803 case DW_OP_const_type:
17804 case DW_OP_regval_type:
17805 case DW_OP_deref_type:
17806 case DW_OP_convert:
17807 case DW_OP_reinterpret:
17808 case DW_OP_form_tls_address:
17809 case DW_OP_GNU_push_tls_address:
17810 case DW_OP_GNU_uninit:
17811 case DW_OP_GNU_encoded_addr:
17812 case DW_OP_GNU_implicit_pointer:
17813 case DW_OP_GNU_entry_value:
17814 case DW_OP_GNU_const_type:
17815 case DW_OP_GNU_regval_type:
17816 case DW_OP_GNU_deref_type:
17817 case DW_OP_GNU_convert:
17818 case DW_OP_GNU_reinterpret:
17819 case DW_OP_GNU_parameter_ref:
17820 /* loc_list_from_tree will probably not output these operations for
17821 size functions, so assume they will not appear here. */
17822 /* Fall through... */
17824 default:
17825 gcc_unreachable ();
17828 /* Now, follow the control flow (except subroutine calls). */
17829 switch (l->dw_loc_opc)
17831 case DW_OP_bra:
17832 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17833 frame_offsets))
17834 return false;
17835 /* Fall through. */
17837 case DW_OP_skip:
17838 l = l->dw_loc_oprnd1.v.val_loc;
17839 break;
17841 case DW_OP_stack_value:
17842 return true;
17844 default:
17845 l = l->dw_loc_next;
17846 break;
17850 return true;
17853 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17854 operations) in order to resolve the operand of DW_OP_pick operations that
17855 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17856 offset *before* LOC is executed. Return if all relocations were
17857 successful. */
17859 static bool
17860 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17861 struct dwarf_procedure_info *dpi)
17863 /* Associate to all visited operations the frame offset *before* evaluating
17864 this operation. */
17865 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17867 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17868 frame_offsets);
17871 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17872 Return NULL if it is not possible. */
17874 static dw_die_ref
17875 function_to_dwarf_procedure (tree fndecl)
17877 struct loc_descr_context ctx;
17878 struct dwarf_procedure_info dpi;
17879 dw_die_ref dwarf_proc_die;
17880 tree tree_body = DECL_SAVED_TREE (fndecl);
17881 dw_loc_descr_ref loc_body, epilogue;
17883 tree cursor;
17884 unsigned i;
17886 /* Do not generate multiple DWARF procedures for the same function
17887 declaration. */
17888 dwarf_proc_die = lookup_decl_die (fndecl);
17889 if (dwarf_proc_die != NULL)
17890 return dwarf_proc_die;
17892 /* DWARF procedures are available starting with the DWARFv3 standard. */
17893 if (dwarf_version < 3 && dwarf_strict)
17894 return NULL;
17896 /* We handle only functions for which we still have a body, that return a
17897 supported type and that takes arguments with supported types. Note that
17898 there is no point translating functions that return nothing. */
17899 if (tree_body == NULL_TREE
17900 || DECL_RESULT (fndecl) == NULL_TREE
17901 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17902 return NULL;
17904 for (cursor = DECL_ARGUMENTS (fndecl);
17905 cursor != NULL_TREE;
17906 cursor = TREE_CHAIN (cursor))
17907 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17908 return NULL;
17910 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17911 if (TREE_CODE (tree_body) != RETURN_EXPR)
17912 return NULL;
17913 tree_body = TREE_OPERAND (tree_body, 0);
17914 if (TREE_CODE (tree_body) != MODIFY_EXPR
17915 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17916 return NULL;
17917 tree_body = TREE_OPERAND (tree_body, 1);
17919 /* Try to translate the body expression itself. Note that this will probably
17920 cause an infinite recursion if its call graph has a cycle. This is very
17921 unlikely for size functions, however, so don't bother with such things at
17922 the moment. */
17923 ctx.context_type = NULL_TREE;
17924 ctx.base_decl = NULL_TREE;
17925 ctx.dpi = &dpi;
17926 ctx.placeholder_arg = false;
17927 ctx.placeholder_seen = false;
17928 dpi.fndecl = fndecl;
17929 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17930 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17931 if (!loc_body)
17932 return NULL;
17934 /* After evaluating all operands in "loc_body", we should still have on the
17935 stack all arguments plus the desired function result (top of the stack).
17936 Generate code in order to keep only the result in our stack frame. */
17937 epilogue = NULL;
17938 for (i = 0; i < dpi.args_count; ++i)
17940 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17941 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17942 op_couple->dw_loc_next->dw_loc_next = epilogue;
17943 epilogue = op_couple;
17945 add_loc_descr (&loc_body, epilogue);
17946 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17947 return NULL;
17949 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17950 because they are considered useful. Now there is an epilogue, they are
17951 not anymore, so give it another try. */
17952 loc_descr_without_nops (loc_body);
17954 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17955 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17956 though, given that size functions do not come from source, so they should
17957 not have a dedicated DW_TAG_subprogram DIE. */
17958 dwarf_proc_die
17959 = new_dwarf_proc_die (loc_body, fndecl,
17960 get_context_die (DECL_CONTEXT (fndecl)));
17962 /* The called DWARF procedure consumes one stack slot per argument and
17963 returns one stack slot. */
17964 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17966 return dwarf_proc_die;
17970 /* Generate Dwarf location list representing LOC.
17971 If WANT_ADDRESS is false, expression computing LOC will be computed
17972 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17973 if WANT_ADDRESS is 2, expression computing address useable in location
17974 will be returned (i.e. DW_OP_reg can be used
17975 to refer to register values).
17977 CONTEXT provides information to customize the location descriptions
17978 generation. Its context_type field specifies what type is implicitly
17979 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17980 will not be generated.
17982 Its DPI field determines whether we are generating a DWARF expression for a
17983 DWARF procedure, so PARM_DECL references are processed specifically.
17985 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17986 and dpi fields were null. */
17988 static dw_loc_list_ref
17989 loc_list_from_tree_1 (tree loc, int want_address,
17990 struct loc_descr_context *context)
17992 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17993 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17994 int have_address = 0;
17995 enum dwarf_location_atom op;
17997 /* ??? Most of the time we do not take proper care for sign/zero
17998 extending the values properly. Hopefully this won't be a real
17999 problem... */
18001 if (context != NULL
18002 && context->base_decl == loc
18003 && want_address == 0)
18005 if (dwarf_version >= 3 || !dwarf_strict)
18006 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
18007 NULL, 0, NULL, 0, NULL);
18008 else
18009 return NULL;
18012 switch (TREE_CODE (loc))
18014 case ERROR_MARK:
18015 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18016 return 0;
18018 case PLACEHOLDER_EXPR:
18019 /* This case involves extracting fields from an object to determine the
18020 position of other fields. It is supposed to appear only as the first
18021 operand of COMPONENT_REF nodes and to reference precisely the type
18022 that the context allows. */
18023 if (context != NULL
18024 && TREE_TYPE (loc) == context->context_type
18025 && want_address >= 1)
18027 if (dwarf_version >= 3 || !dwarf_strict)
18029 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18030 have_address = 1;
18031 break;
18033 else
18034 return NULL;
18036 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18037 the single argument passed by consumer. */
18038 else if (context != NULL
18039 && context->placeholder_arg
18040 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18041 && want_address == 0)
18043 ret = new_loc_descr (DW_OP_pick, 0, 0);
18044 ret->frame_offset_rel = 1;
18045 context->placeholder_seen = true;
18046 break;
18048 else
18049 expansion_failed (loc, NULL_RTX,
18050 "PLACEHOLDER_EXPR for an unexpected type");
18051 break;
18053 case CALL_EXPR:
18055 const int nargs = call_expr_nargs (loc);
18056 tree callee = get_callee_fndecl (loc);
18057 int i;
18058 dw_die_ref dwarf_proc;
18060 if (callee == NULL_TREE)
18061 goto call_expansion_failed;
18063 /* We handle only functions that return an integer. */
18064 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18065 goto call_expansion_failed;
18067 dwarf_proc = function_to_dwarf_procedure (callee);
18068 if (dwarf_proc == NULL)
18069 goto call_expansion_failed;
18071 /* Evaluate arguments right-to-left so that the first argument will
18072 be the top-most one on the stack. */
18073 for (i = nargs - 1; i >= 0; --i)
18075 dw_loc_descr_ref loc_descr
18076 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18077 context);
18079 if (loc_descr == NULL)
18080 goto call_expansion_failed;
18082 add_loc_descr (&ret, loc_descr);
18085 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18086 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18087 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18088 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18089 add_loc_descr (&ret, ret1);
18090 break;
18092 call_expansion_failed:
18093 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18094 /* There are no opcodes for these operations. */
18095 return 0;
18098 case PREINCREMENT_EXPR:
18099 case PREDECREMENT_EXPR:
18100 case POSTINCREMENT_EXPR:
18101 case POSTDECREMENT_EXPR:
18102 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18103 /* There are no opcodes for these operations. */
18104 return 0;
18106 case ADDR_EXPR:
18107 /* If we already want an address, see if there is INDIRECT_REF inside
18108 e.g. for &this->field. */
18109 if (want_address)
18111 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18112 (loc, want_address == 2, context);
18113 if (list_ret)
18114 have_address = 1;
18115 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18116 && (ret = cst_pool_loc_descr (loc)))
18117 have_address = 1;
18119 /* Otherwise, process the argument and look for the address. */
18120 if (!list_ret && !ret)
18121 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18122 else
18124 if (want_address)
18125 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18126 return NULL;
18128 break;
18130 case VAR_DECL:
18131 if (DECL_THREAD_LOCAL_P (loc))
18133 rtx rtl;
18134 enum dwarf_location_atom tls_op;
18135 enum dtprel_bool dtprel = dtprel_false;
18137 if (targetm.have_tls)
18139 /* If this is not defined, we have no way to emit the
18140 data. */
18141 if (!targetm.asm_out.output_dwarf_dtprel)
18142 return 0;
18144 /* The way DW_OP_GNU_push_tls_address is specified, we
18145 can only look up addresses of objects in the current
18146 module. We used DW_OP_addr as first op, but that's
18147 wrong, because DW_OP_addr is relocated by the debug
18148 info consumer, while DW_OP_GNU_push_tls_address
18149 operand shouldn't be. */
18150 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18151 return 0;
18152 dtprel = dtprel_true;
18153 /* We check for DWARF 5 here because gdb did not implement
18154 DW_OP_form_tls_address until after 7.12. */
18155 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18156 : DW_OP_GNU_push_tls_address);
18158 else
18160 if (!targetm.emutls.debug_form_tls_address
18161 || !(dwarf_version >= 3 || !dwarf_strict))
18162 return 0;
18163 /* We stuffed the control variable into the DECL_VALUE_EXPR
18164 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18165 no longer appear in gimple code. We used the control
18166 variable in specific so that we could pick it up here. */
18167 loc = DECL_VALUE_EXPR (loc);
18168 tls_op = DW_OP_form_tls_address;
18171 rtl = rtl_for_decl_location (loc);
18172 if (rtl == NULL_RTX)
18173 return 0;
18175 if (!MEM_P (rtl))
18176 return 0;
18177 rtl = XEXP (rtl, 0);
18178 if (! CONSTANT_P (rtl))
18179 return 0;
18181 ret = new_addr_loc_descr (rtl, dtprel);
18182 ret1 = new_loc_descr (tls_op, 0, 0);
18183 add_loc_descr (&ret, ret1);
18185 have_address = 1;
18186 break;
18188 /* FALLTHRU */
18190 case PARM_DECL:
18191 if (context != NULL && context->dpi != NULL
18192 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18194 /* We are generating code for a DWARF procedure and we want to access
18195 one of its arguments: find the appropriate argument offset and let
18196 the resolve_args_picking pass compute the offset that complies
18197 with the stack frame size. */
18198 unsigned i = 0;
18199 tree cursor;
18201 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18202 cursor != NULL_TREE && cursor != loc;
18203 cursor = TREE_CHAIN (cursor), ++i)
18205 /* If we are translating a DWARF procedure, all referenced parameters
18206 must belong to the current function. */
18207 gcc_assert (cursor != NULL_TREE);
18209 ret = new_loc_descr (DW_OP_pick, i, 0);
18210 ret->frame_offset_rel = 1;
18211 break;
18213 /* FALLTHRU */
18215 case RESULT_DECL:
18216 if (DECL_HAS_VALUE_EXPR_P (loc))
18217 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18218 want_address, context);
18219 /* FALLTHRU */
18221 case FUNCTION_DECL:
18223 rtx rtl;
18224 var_loc_list *loc_list = lookup_decl_loc (loc);
18226 if (loc_list && loc_list->first)
18228 list_ret = dw_loc_list (loc_list, loc, want_address);
18229 have_address = want_address != 0;
18230 break;
18232 rtl = rtl_for_decl_location (loc);
18233 if (rtl == NULL_RTX)
18235 if (TREE_CODE (loc) != FUNCTION_DECL
18236 && early_dwarf
18237 && current_function_decl
18238 && want_address != 1
18239 && ! DECL_IGNORED_P (loc)
18240 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18241 || POINTER_TYPE_P (TREE_TYPE (loc)))
18242 && DECL_CONTEXT (loc) == current_function_decl
18243 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18244 <= DWARF2_ADDR_SIZE))
18246 dw_die_ref ref = lookup_decl_die (loc);
18247 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18248 if (ref)
18250 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18251 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18252 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18254 else
18256 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18257 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18259 break;
18261 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18262 return 0;
18264 else if (CONST_INT_P (rtl))
18266 HOST_WIDE_INT val = INTVAL (rtl);
18267 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18268 val &= GET_MODE_MASK (DECL_MODE (loc));
18269 ret = int_loc_descriptor (val);
18271 else if (GET_CODE (rtl) == CONST_STRING)
18273 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18274 return 0;
18276 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18277 ret = new_addr_loc_descr (rtl, dtprel_false);
18278 else
18280 machine_mode mode, mem_mode;
18282 /* Certain constructs can only be represented at top-level. */
18283 if (want_address == 2)
18285 ret = loc_descriptor (rtl, VOIDmode,
18286 VAR_INIT_STATUS_INITIALIZED);
18287 have_address = 1;
18289 else
18291 mode = GET_MODE (rtl);
18292 mem_mode = VOIDmode;
18293 if (MEM_P (rtl))
18295 mem_mode = mode;
18296 mode = get_address_mode (rtl);
18297 rtl = XEXP (rtl, 0);
18298 have_address = 1;
18300 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18301 VAR_INIT_STATUS_INITIALIZED);
18303 if (!ret)
18304 expansion_failed (loc, rtl,
18305 "failed to produce loc descriptor for rtl");
18308 break;
18310 case MEM_REF:
18311 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18313 have_address = 1;
18314 goto do_plus;
18316 /* Fallthru. */
18317 case INDIRECT_REF:
18318 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18319 have_address = 1;
18320 break;
18322 case TARGET_MEM_REF:
18323 case SSA_NAME:
18324 case DEBUG_EXPR_DECL:
18325 return NULL;
18327 case COMPOUND_EXPR:
18328 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18329 context);
18331 CASE_CONVERT:
18332 case VIEW_CONVERT_EXPR:
18333 case SAVE_EXPR:
18334 case MODIFY_EXPR:
18335 case NON_LVALUE_EXPR:
18336 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18337 context);
18339 case COMPONENT_REF:
18340 case BIT_FIELD_REF:
18341 case ARRAY_REF:
18342 case ARRAY_RANGE_REF:
18343 case REALPART_EXPR:
18344 case IMAGPART_EXPR:
18346 tree obj, offset;
18347 poly_int64 bitsize, bitpos, bytepos;
18348 machine_mode mode;
18349 int unsignedp, reversep, volatilep = 0;
18351 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18352 &unsignedp, &reversep, &volatilep);
18354 gcc_assert (obj != loc);
18356 list_ret = loc_list_from_tree_1 (obj,
18357 want_address == 2
18358 && known_eq (bitpos, 0)
18359 && !offset ? 2 : 1,
18360 context);
18361 /* TODO: We can extract value of the small expression via shifting even
18362 for nonzero bitpos. */
18363 if (list_ret == 0)
18364 return 0;
18365 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18366 || !multiple_p (bitsize, BITS_PER_UNIT))
18368 expansion_failed (loc, NULL_RTX,
18369 "bitfield access");
18370 return 0;
18373 if (offset != NULL_TREE)
18375 /* Variable offset. */
18376 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18377 if (list_ret1 == 0)
18378 return 0;
18379 add_loc_list (&list_ret, list_ret1);
18380 if (!list_ret)
18381 return 0;
18382 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18385 HOST_WIDE_INT value;
18386 if (bytepos.is_constant (&value) && value > 0)
18387 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18388 value, 0));
18389 else if (maybe_ne (bytepos, 0))
18390 loc_list_plus_const (list_ret, bytepos);
18392 have_address = 1;
18393 break;
18396 case INTEGER_CST:
18397 if ((want_address || !tree_fits_shwi_p (loc))
18398 && (ret = cst_pool_loc_descr (loc)))
18399 have_address = 1;
18400 else if (want_address == 2
18401 && tree_fits_shwi_p (loc)
18402 && (ret = address_of_int_loc_descriptor
18403 (int_size_in_bytes (TREE_TYPE (loc)),
18404 tree_to_shwi (loc))))
18405 have_address = 1;
18406 else if (tree_fits_shwi_p (loc))
18407 ret = int_loc_descriptor (tree_to_shwi (loc));
18408 else if (tree_fits_uhwi_p (loc))
18409 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18410 else
18412 expansion_failed (loc, NULL_RTX,
18413 "Integer operand is not host integer");
18414 return 0;
18416 break;
18418 case CONSTRUCTOR:
18419 case REAL_CST:
18420 case STRING_CST:
18421 case COMPLEX_CST:
18422 if ((ret = cst_pool_loc_descr (loc)))
18423 have_address = 1;
18424 else if (TREE_CODE (loc) == CONSTRUCTOR)
18426 tree type = TREE_TYPE (loc);
18427 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18428 unsigned HOST_WIDE_INT offset = 0;
18429 unsigned HOST_WIDE_INT cnt;
18430 constructor_elt *ce;
18432 if (TREE_CODE (type) == RECORD_TYPE)
18434 /* This is very limited, but it's enough to output
18435 pointers to member functions, as long as the
18436 referenced function is defined in the current
18437 translation unit. */
18438 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18440 tree val = ce->value;
18442 tree field = ce->index;
18444 if (val)
18445 STRIP_NOPS (val);
18447 if (!field || DECL_BIT_FIELD (field))
18449 expansion_failed (loc, NULL_RTX,
18450 "bitfield in record type constructor");
18451 size = offset = (unsigned HOST_WIDE_INT)-1;
18452 ret = NULL;
18453 break;
18456 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18457 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18458 gcc_assert (pos + fieldsize <= size);
18459 if (pos < offset)
18461 expansion_failed (loc, NULL_RTX,
18462 "out-of-order fields in record constructor");
18463 size = offset = (unsigned HOST_WIDE_INT)-1;
18464 ret = NULL;
18465 break;
18467 if (pos > offset)
18469 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18470 add_loc_descr (&ret, ret1);
18471 offset = pos;
18473 if (val && fieldsize != 0)
18475 ret1 = loc_descriptor_from_tree (val, want_address, context);
18476 if (!ret1)
18478 expansion_failed (loc, NULL_RTX,
18479 "unsupported expression in field");
18480 size = offset = (unsigned HOST_WIDE_INT)-1;
18481 ret = NULL;
18482 break;
18484 add_loc_descr (&ret, ret1);
18486 if (fieldsize)
18488 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18489 add_loc_descr (&ret, ret1);
18490 offset = pos + fieldsize;
18494 if (offset != size)
18496 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18497 add_loc_descr (&ret, ret1);
18498 offset = size;
18501 have_address = !!want_address;
18503 else
18504 expansion_failed (loc, NULL_RTX,
18505 "constructor of non-record type");
18507 else
18508 /* We can construct small constants here using int_loc_descriptor. */
18509 expansion_failed (loc, NULL_RTX,
18510 "constructor or constant not in constant pool");
18511 break;
18513 case TRUTH_AND_EXPR:
18514 case TRUTH_ANDIF_EXPR:
18515 case BIT_AND_EXPR:
18516 op = DW_OP_and;
18517 goto do_binop;
18519 case TRUTH_XOR_EXPR:
18520 case BIT_XOR_EXPR:
18521 op = DW_OP_xor;
18522 goto do_binop;
18524 case TRUTH_OR_EXPR:
18525 case TRUTH_ORIF_EXPR:
18526 case BIT_IOR_EXPR:
18527 op = DW_OP_or;
18528 goto do_binop;
18530 case FLOOR_DIV_EXPR:
18531 case CEIL_DIV_EXPR:
18532 case ROUND_DIV_EXPR:
18533 case TRUNC_DIV_EXPR:
18534 case EXACT_DIV_EXPR:
18535 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18536 return 0;
18537 op = DW_OP_div;
18538 goto do_binop;
18540 case MINUS_EXPR:
18541 op = DW_OP_minus;
18542 goto do_binop;
18544 case FLOOR_MOD_EXPR:
18545 case CEIL_MOD_EXPR:
18546 case ROUND_MOD_EXPR:
18547 case TRUNC_MOD_EXPR:
18548 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18550 op = DW_OP_mod;
18551 goto do_binop;
18553 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18554 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18555 if (list_ret == 0 || list_ret1 == 0)
18556 return 0;
18558 add_loc_list (&list_ret, list_ret1);
18559 if (list_ret == 0)
18560 return 0;
18561 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 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_div, 0, 0));
18564 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18565 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18566 break;
18568 case MULT_EXPR:
18569 op = DW_OP_mul;
18570 goto do_binop;
18572 case LSHIFT_EXPR:
18573 op = DW_OP_shl;
18574 goto do_binop;
18576 case RSHIFT_EXPR:
18577 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18578 goto do_binop;
18580 case POINTER_PLUS_EXPR:
18581 case PLUS_EXPR:
18582 do_plus:
18583 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18585 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18586 smarter to encode their opposite. The DW_OP_plus_uconst operation
18587 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18588 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18589 bytes, Y being the size of the operation that pushes the opposite
18590 of the addend. So let's choose the smallest representation. */
18591 const tree tree_addend = TREE_OPERAND (loc, 1);
18592 offset_int wi_addend;
18593 HOST_WIDE_INT shwi_addend;
18594 dw_loc_descr_ref loc_naddend;
18596 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18597 if (list_ret == 0)
18598 return 0;
18600 /* Try to get the literal to push. It is the opposite of the addend,
18601 so as we rely on wrapping during DWARF evaluation, first decode
18602 the literal as a "DWARF-sized" signed number. */
18603 wi_addend = wi::to_offset (tree_addend);
18604 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18605 shwi_addend = wi_addend.to_shwi ();
18606 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18607 ? int_loc_descriptor (-shwi_addend)
18608 : NULL;
18610 if (loc_naddend != NULL
18611 && ((unsigned) size_of_uleb128 (shwi_addend)
18612 > size_of_loc_descr (loc_naddend)))
18614 add_loc_descr_to_each (list_ret, loc_naddend);
18615 add_loc_descr_to_each (list_ret,
18616 new_loc_descr (DW_OP_minus, 0, 0));
18618 else
18620 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18622 loc_naddend = loc_cur;
18623 loc_cur = loc_cur->dw_loc_next;
18624 ggc_free (loc_naddend);
18626 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18628 break;
18631 op = DW_OP_plus;
18632 goto do_binop;
18634 case LE_EXPR:
18635 op = DW_OP_le;
18636 goto do_comp_binop;
18638 case GE_EXPR:
18639 op = DW_OP_ge;
18640 goto do_comp_binop;
18642 case LT_EXPR:
18643 op = DW_OP_lt;
18644 goto do_comp_binop;
18646 case GT_EXPR:
18647 op = DW_OP_gt;
18648 goto do_comp_binop;
18650 do_comp_binop:
18651 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18653 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18654 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18655 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18656 TREE_CODE (loc));
18657 break;
18659 else
18660 goto do_binop;
18662 case EQ_EXPR:
18663 op = DW_OP_eq;
18664 goto do_binop;
18666 case NE_EXPR:
18667 op = DW_OP_ne;
18668 goto do_binop;
18670 do_binop:
18671 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18672 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18673 if (list_ret == 0 || list_ret1 == 0)
18674 return 0;
18676 add_loc_list (&list_ret, list_ret1);
18677 if (list_ret == 0)
18678 return 0;
18679 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18680 break;
18682 case TRUTH_NOT_EXPR:
18683 case BIT_NOT_EXPR:
18684 op = DW_OP_not;
18685 goto do_unop;
18687 case ABS_EXPR:
18688 op = DW_OP_abs;
18689 goto do_unop;
18691 case NEGATE_EXPR:
18692 op = DW_OP_neg;
18693 goto do_unop;
18695 do_unop:
18696 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18697 if (list_ret == 0)
18698 return 0;
18700 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18701 break;
18703 case MIN_EXPR:
18704 case MAX_EXPR:
18706 const enum tree_code code =
18707 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18709 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18710 build2 (code, integer_type_node,
18711 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18712 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18715 /* fall through */
18717 case COND_EXPR:
18719 dw_loc_descr_ref lhs
18720 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18721 dw_loc_list_ref rhs
18722 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18723 dw_loc_descr_ref bra_node, jump_node, tmp;
18725 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18726 if (list_ret == 0 || lhs == 0 || rhs == 0)
18727 return 0;
18729 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18730 add_loc_descr_to_each (list_ret, bra_node);
18732 add_loc_list (&list_ret, rhs);
18733 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18734 add_loc_descr_to_each (list_ret, jump_node);
18736 add_loc_descr_to_each (list_ret, lhs);
18737 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18738 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18740 /* ??? Need a node to point the skip at. Use a nop. */
18741 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18742 add_loc_descr_to_each (list_ret, tmp);
18743 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18744 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18746 break;
18748 case FIX_TRUNC_EXPR:
18749 return 0;
18751 default:
18752 /* Leave front-end specific codes as simply unknown. This comes
18753 up, for instance, with the C STMT_EXPR. */
18754 if ((unsigned int) TREE_CODE (loc)
18755 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18757 expansion_failed (loc, NULL_RTX,
18758 "language specific tree node");
18759 return 0;
18762 /* Otherwise this is a generic code; we should just lists all of
18763 these explicitly. We forgot one. */
18764 if (flag_checking)
18765 gcc_unreachable ();
18767 /* In a release build, we want to degrade gracefully: better to
18768 generate incomplete debugging information than to crash. */
18769 return NULL;
18772 if (!ret && !list_ret)
18773 return 0;
18775 if (want_address == 2 && !have_address
18776 && (dwarf_version >= 4 || !dwarf_strict))
18778 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18780 expansion_failed (loc, NULL_RTX,
18781 "DWARF address size mismatch");
18782 return 0;
18784 if (ret)
18785 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18786 else
18787 add_loc_descr_to_each (list_ret,
18788 new_loc_descr (DW_OP_stack_value, 0, 0));
18789 have_address = 1;
18791 /* Show if we can't fill the request for an address. */
18792 if (want_address && !have_address)
18794 expansion_failed (loc, NULL_RTX,
18795 "Want address and only have value");
18796 return 0;
18799 gcc_assert (!ret || !list_ret);
18801 /* If we've got an address and don't want one, dereference. */
18802 if (!want_address && have_address)
18804 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18806 if (size > DWARF2_ADDR_SIZE || size == -1)
18808 expansion_failed (loc, NULL_RTX,
18809 "DWARF address size mismatch");
18810 return 0;
18812 else if (size == DWARF2_ADDR_SIZE)
18813 op = DW_OP_deref;
18814 else
18815 op = DW_OP_deref_size;
18817 if (ret)
18818 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18819 else
18820 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18822 if (ret)
18823 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18825 return list_ret;
18828 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18829 expressions. */
18831 static dw_loc_list_ref
18832 loc_list_from_tree (tree loc, int want_address,
18833 struct loc_descr_context *context)
18835 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18837 for (dw_loc_list_ref loc_cur = result;
18838 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18839 loc_descr_without_nops (loc_cur->expr);
18840 return result;
18843 /* Same as above but return only single location expression. */
18844 static dw_loc_descr_ref
18845 loc_descriptor_from_tree (tree loc, int want_address,
18846 struct loc_descr_context *context)
18848 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18849 if (!ret)
18850 return NULL;
18851 if (ret->dw_loc_next)
18853 expansion_failed (loc, NULL_RTX,
18854 "Location list where only loc descriptor needed");
18855 return NULL;
18857 return ret->expr;
18860 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18861 pointer to the declared type for the relevant field variable, or return
18862 `integer_type_node' if the given node turns out to be an
18863 ERROR_MARK node. */
18865 static inline tree
18866 field_type (const_tree decl)
18868 tree type;
18870 if (TREE_CODE (decl) == ERROR_MARK)
18871 return integer_type_node;
18873 type = DECL_BIT_FIELD_TYPE (decl);
18874 if (type == NULL_TREE)
18875 type = TREE_TYPE (decl);
18877 return type;
18880 /* Given a pointer to a tree node, return the alignment in bits for
18881 it, or else return BITS_PER_WORD if the node actually turns out to
18882 be an ERROR_MARK node. */
18884 static inline unsigned
18885 simple_type_align_in_bits (const_tree type)
18887 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18890 static inline unsigned
18891 simple_decl_align_in_bits (const_tree decl)
18893 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18896 /* Return the result of rounding T up to ALIGN. */
18898 static inline offset_int
18899 round_up_to_align (const offset_int &t, unsigned int align)
18901 return wi::udiv_trunc (t + align - 1, align) * align;
18904 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18905 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18906 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18907 if we fail to return the size in one of these two forms. */
18909 static dw_loc_descr_ref
18910 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18912 tree tree_size;
18913 struct loc_descr_context ctx;
18915 /* Return a constant integer in priority, if possible. */
18916 *cst_size = int_size_in_bytes (type);
18917 if (*cst_size != -1)
18918 return NULL;
18920 ctx.context_type = const_cast<tree> (type);
18921 ctx.base_decl = NULL_TREE;
18922 ctx.dpi = NULL;
18923 ctx.placeholder_arg = false;
18924 ctx.placeholder_seen = false;
18926 type = TYPE_MAIN_VARIANT (type);
18927 tree_size = TYPE_SIZE_UNIT (type);
18928 return ((tree_size != NULL_TREE)
18929 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18930 : NULL);
18933 /* Helper structure for RECORD_TYPE processing. */
18934 struct vlr_context
18936 /* Root RECORD_TYPE. It is needed to generate data member location
18937 descriptions in variable-length records (VLR), but also to cope with
18938 variants, which are composed of nested structures multiplexed with
18939 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18940 function processing a FIELD_DECL, it is required to be non null. */
18941 tree struct_type;
18942 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18943 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18944 this variant part as part of the root record (in storage units). For
18945 regular records, it must be NULL_TREE. */
18946 tree variant_part_offset;
18949 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18950 addressed byte of the "containing object" for the given FIELD_DECL. If
18951 possible, return a native constant through CST_OFFSET (in which case NULL is
18952 returned); otherwise return a DWARF expression that computes the offset.
18954 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18955 that offset is, either because the argument turns out to be a pointer to an
18956 ERROR_MARK node, or because the offset expression is too complex for us.
18958 CTX is required: see the comment for VLR_CONTEXT. */
18960 static dw_loc_descr_ref
18961 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18962 HOST_WIDE_INT *cst_offset)
18964 tree tree_result;
18965 dw_loc_list_ref loc_result;
18967 *cst_offset = 0;
18969 if (TREE_CODE (decl) == ERROR_MARK)
18970 return NULL;
18971 else
18972 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18974 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18975 case. */
18976 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18977 return NULL;
18979 #ifdef PCC_BITFIELD_TYPE_MATTERS
18980 /* We used to handle only constant offsets in all cases. Now, we handle
18981 properly dynamic byte offsets only when PCC bitfield type doesn't
18982 matter. */
18983 if (PCC_BITFIELD_TYPE_MATTERS
18984 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18986 offset_int object_offset_in_bits;
18987 offset_int object_offset_in_bytes;
18988 offset_int bitpos_int;
18989 tree type;
18990 tree field_size_tree;
18991 offset_int deepest_bitpos;
18992 offset_int field_size_in_bits;
18993 unsigned int type_align_in_bits;
18994 unsigned int decl_align_in_bits;
18995 offset_int type_size_in_bits;
18997 bitpos_int = wi::to_offset (bit_position (decl));
18998 type = field_type (decl);
18999 type_size_in_bits = offset_int_type_size_in_bits (type);
19000 type_align_in_bits = simple_type_align_in_bits (type);
19002 field_size_tree = DECL_SIZE (decl);
19004 /* The size could be unspecified if there was an error, or for
19005 a flexible array member. */
19006 if (!field_size_tree)
19007 field_size_tree = bitsize_zero_node;
19009 /* If the size of the field is not constant, use the type size. */
19010 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19011 field_size_in_bits = wi::to_offset (field_size_tree);
19012 else
19013 field_size_in_bits = type_size_in_bits;
19015 decl_align_in_bits = simple_decl_align_in_bits (decl);
19017 /* The GCC front-end doesn't make any attempt to keep track of the
19018 starting bit offset (relative to the start of the containing
19019 structure type) of the hypothetical "containing object" for a
19020 bit-field. Thus, when computing the byte offset value for the
19021 start of the "containing object" of a bit-field, we must deduce
19022 this information on our own. This can be rather tricky to do in
19023 some cases. For example, handling the following structure type
19024 definition when compiling for an i386/i486 target (which only
19025 aligns long long's to 32-bit boundaries) can be very tricky:
19027 struct S { int field1; long long field2:31; };
19029 Fortunately, there is a simple rule-of-thumb which can be used
19030 in such cases. When compiling for an i386/i486, GCC will
19031 allocate 8 bytes for the structure shown above. It decides to
19032 do this based upon one simple rule for bit-field allocation.
19033 GCC allocates each "containing object" for each bit-field at
19034 the first (i.e. lowest addressed) legitimate alignment boundary
19035 (based upon the required minimum alignment for the declared
19036 type of the field) which it can possibly use, subject to the
19037 condition that there is still enough available space remaining
19038 in the containing object (when allocated at the selected point)
19039 to fully accommodate all of the bits of the bit-field itself.
19041 This simple rule makes it obvious why GCC allocates 8 bytes for
19042 each object of the structure type shown above. When looking
19043 for a place to allocate the "containing object" for `field2',
19044 the compiler simply tries to allocate a 64-bit "containing
19045 object" at each successive 32-bit boundary (starting at zero)
19046 until it finds a place to allocate that 64- bit field such that
19047 at least 31 contiguous (and previously unallocated) bits remain
19048 within that selected 64 bit field. (As it turns out, for the
19049 example above, the compiler finds it is OK to allocate the
19050 "containing object" 64-bit field at bit-offset zero within the
19051 structure type.)
19053 Here we attempt to work backwards from the limited set of facts
19054 we're given, and we try to deduce from those facts, where GCC
19055 must have believed that the containing object started (within
19056 the structure type). The value we deduce is then used (by the
19057 callers of this routine) to generate DW_AT_location and
19058 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19059 the case of DW_AT_location, regular fields as well). */
19061 /* Figure out the bit-distance from the start of the structure to
19062 the "deepest" bit of the bit-field. */
19063 deepest_bitpos = bitpos_int + field_size_in_bits;
19065 /* This is the tricky part. Use some fancy footwork to deduce
19066 where the lowest addressed bit of the containing object must
19067 be. */
19068 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19070 /* Round up to type_align by default. This works best for
19071 bitfields. */
19072 object_offset_in_bits
19073 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19075 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19077 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19079 /* Round up to decl_align instead. */
19080 object_offset_in_bits
19081 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19084 object_offset_in_bytes
19085 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19086 if (ctx->variant_part_offset == NULL_TREE)
19088 *cst_offset = object_offset_in_bytes.to_shwi ();
19089 return NULL;
19091 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19093 else
19094 #endif /* PCC_BITFIELD_TYPE_MATTERS */
19095 tree_result = byte_position (decl);
19097 if (ctx->variant_part_offset != NULL_TREE)
19098 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19099 ctx->variant_part_offset, tree_result);
19101 /* If the byte offset is a constant, it's simplier to handle a native
19102 constant rather than a DWARF expression. */
19103 if (TREE_CODE (tree_result) == INTEGER_CST)
19105 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19106 return NULL;
19108 struct loc_descr_context loc_ctx = {
19109 ctx->struct_type, /* context_type */
19110 NULL_TREE, /* base_decl */
19111 NULL, /* dpi */
19112 false, /* placeholder_arg */
19113 false /* placeholder_seen */
19115 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19117 /* We want a DWARF expression: abort if we only have a location list with
19118 multiple elements. */
19119 if (!loc_result || !single_element_loc_list_p (loc_result))
19120 return NULL;
19121 else
19122 return loc_result->expr;
19125 /* The following routines define various Dwarf attributes and any data
19126 associated with them. */
19128 /* Add a location description attribute value to a DIE.
19130 This emits location attributes suitable for whole variables and
19131 whole parameters. Note that the location attributes for struct fields are
19132 generated by the routine `data_member_location_attribute' below. */
19134 static inline void
19135 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19136 dw_loc_list_ref descr)
19138 bool check_no_locviews = true;
19139 if (descr == 0)
19140 return;
19141 if (single_element_loc_list_p (descr))
19142 add_AT_loc (die, attr_kind, descr->expr);
19143 else
19145 add_AT_loc_list (die, attr_kind, descr);
19146 gcc_assert (descr->ll_symbol);
19147 if (attr_kind == DW_AT_location && descr->vl_symbol
19148 && dwarf2out_locviews_in_attribute ())
19150 add_AT_view_list (die, DW_AT_GNU_locviews);
19151 check_no_locviews = false;
19155 if (check_no_locviews)
19156 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19159 /* Add DW_AT_accessibility attribute to DIE if needed. */
19161 static void
19162 add_accessibility_attribute (dw_die_ref die, tree decl)
19164 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19165 children, otherwise the default is DW_ACCESS_public. In DWARF2
19166 the default has always been DW_ACCESS_public. */
19167 if (TREE_PROTECTED (decl))
19168 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19169 else if (TREE_PRIVATE (decl))
19171 if (dwarf_version == 2
19172 || die->die_parent == NULL
19173 || die->die_parent->die_tag != DW_TAG_class_type)
19174 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19176 else if (dwarf_version > 2
19177 && die->die_parent
19178 && die->die_parent->die_tag == DW_TAG_class_type)
19179 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19182 /* Attach the specialized form of location attribute used for data members of
19183 struct and union types. In the special case of a FIELD_DECL node which
19184 represents a bit-field, the "offset" part of this special location
19185 descriptor must indicate the distance in bytes from the lowest-addressed
19186 byte of the containing struct or union type to the lowest-addressed byte of
19187 the "containing object" for the bit-field. (See the `field_byte_offset'
19188 function above).
19190 For any given bit-field, the "containing object" is a hypothetical object
19191 (of some integral or enum type) within which the given bit-field lives. The
19192 type of this hypothetical "containing object" is always the same as the
19193 declared type of the individual bit-field itself (for GCC anyway... the
19194 DWARF spec doesn't actually mandate this). Note that it is the size (in
19195 bytes) of the hypothetical "containing object" which will be given in the
19196 DW_AT_byte_size attribute for this bit-field. (See the
19197 `byte_size_attribute' function below.) It is also used when calculating the
19198 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19199 function below.)
19201 CTX is required: see the comment for VLR_CONTEXT. */
19203 static void
19204 add_data_member_location_attribute (dw_die_ref die,
19205 tree decl,
19206 struct vlr_context *ctx)
19208 HOST_WIDE_INT offset;
19209 dw_loc_descr_ref loc_descr = 0;
19211 if (TREE_CODE (decl) == TREE_BINFO)
19213 /* We're working on the TAG_inheritance for a base class. */
19214 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19216 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19217 aren't at a fixed offset from all (sub)objects of the same
19218 type. We need to extract the appropriate offset from our
19219 vtable. The following dwarf expression means
19221 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19223 This is specific to the V3 ABI, of course. */
19225 dw_loc_descr_ref tmp;
19227 /* Make a copy of the object address. */
19228 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19229 add_loc_descr (&loc_descr, tmp);
19231 /* Extract the vtable address. */
19232 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19233 add_loc_descr (&loc_descr, tmp);
19235 /* Calculate the address of the offset. */
19236 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19237 gcc_assert (offset < 0);
19239 tmp = int_loc_descriptor (-offset);
19240 add_loc_descr (&loc_descr, tmp);
19241 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19242 add_loc_descr (&loc_descr, tmp);
19244 /* Extract the offset. */
19245 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19246 add_loc_descr (&loc_descr, tmp);
19248 /* Add it to the object address. */
19249 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19250 add_loc_descr (&loc_descr, tmp);
19252 else
19253 offset = tree_to_shwi (BINFO_OFFSET (decl));
19255 else
19257 loc_descr = field_byte_offset (decl, ctx, &offset);
19259 /* If loc_descr is available then we know the field offset is dynamic.
19260 However, GDB does not handle dynamic field offsets very well at the
19261 moment. */
19262 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19264 loc_descr = NULL;
19265 offset = 0;
19268 /* Data member location evalutation starts with the base address on the
19269 stack. Compute the field offset and add it to this base address. */
19270 else if (loc_descr != NULL)
19271 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19274 if (! loc_descr)
19276 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19277 e.g. GDB only added support to it in November 2016. For DWARF5
19278 we need newer debug info consumers anyway. We might change this
19279 to dwarf_version >= 4 once most consumers catched up. */
19280 if (dwarf_version >= 5
19281 && TREE_CODE (decl) == FIELD_DECL
19282 && DECL_BIT_FIELD_TYPE (decl))
19284 tree off = bit_position (decl);
19285 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19287 remove_AT (die, DW_AT_byte_size);
19288 remove_AT (die, DW_AT_bit_offset);
19289 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19290 return;
19293 if (dwarf_version > 2)
19295 /* Don't need to output a location expression, just the constant. */
19296 if (offset < 0)
19297 add_AT_int (die, DW_AT_data_member_location, offset);
19298 else
19299 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19300 return;
19302 else
19304 enum dwarf_location_atom op;
19306 /* The DWARF2 standard says that we should assume that the structure
19307 address is already on the stack, so we can specify a structure
19308 field address by using DW_OP_plus_uconst. */
19309 op = DW_OP_plus_uconst;
19310 loc_descr = new_loc_descr (op, offset, 0);
19314 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19317 /* Writes integer values to dw_vec_const array. */
19319 static void
19320 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19322 while (size != 0)
19324 *dest++ = val & 0xff;
19325 val >>= 8;
19326 --size;
19330 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19332 static HOST_WIDE_INT
19333 extract_int (const unsigned char *src, unsigned int size)
19335 HOST_WIDE_INT val = 0;
19337 src += size;
19338 while (size != 0)
19340 val <<= 8;
19341 val |= *--src & 0xff;
19342 --size;
19344 return val;
19347 /* Writes wide_int values to dw_vec_const array. */
19349 static void
19350 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19352 int i;
19354 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19356 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19357 return;
19360 /* We'd have to extend this code to support odd sizes. */
19361 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19363 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19365 if (WORDS_BIG_ENDIAN)
19366 for (i = n - 1; i >= 0; i--)
19368 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19369 dest += sizeof (HOST_WIDE_INT);
19371 else
19372 for (i = 0; i < n; i++)
19374 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19375 dest += sizeof (HOST_WIDE_INT);
19379 /* Writes floating point values to dw_vec_const array. */
19381 static void
19382 insert_float (const_rtx rtl, unsigned char *array)
19384 long val[4];
19385 int i;
19386 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19388 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19390 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19391 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19393 insert_int (val[i], 4, array);
19394 array += 4;
19398 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19399 does not have a "location" either in memory or in a register. These
19400 things can arise in GNU C when a constant is passed as an actual parameter
19401 to an inlined function. They can also arise in C++ where declared
19402 constants do not necessarily get memory "homes". */
19404 static bool
19405 add_const_value_attribute (dw_die_ref die, rtx rtl)
19407 switch (GET_CODE (rtl))
19409 case CONST_INT:
19411 HOST_WIDE_INT val = INTVAL (rtl);
19413 if (val < 0)
19414 add_AT_int (die, DW_AT_const_value, val);
19415 else
19416 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19418 return true;
19420 case CONST_WIDE_INT:
19422 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19423 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19424 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19425 wide_int w = wi::zext (w1, prec);
19426 add_AT_wide (die, DW_AT_const_value, w);
19428 return true;
19430 case CONST_DOUBLE:
19431 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19432 floating-point constant. A CONST_DOUBLE is used whenever the
19433 constant requires more than one word in order to be adequately
19434 represented. */
19435 if (TARGET_SUPPORTS_WIDE_INT == 0
19436 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19437 add_AT_double (die, DW_AT_const_value,
19438 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19439 else
19441 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19442 unsigned int length = GET_MODE_SIZE (mode);
19443 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19445 insert_float (rtl, array);
19446 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19448 return true;
19450 case CONST_VECTOR:
19452 unsigned int length;
19453 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19454 return false;
19456 machine_mode mode = GET_MODE (rtl);
19457 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19458 unsigned char *array
19459 = ggc_vec_alloc<unsigned char> (length * elt_size);
19460 unsigned int i;
19461 unsigned char *p;
19462 machine_mode imode = GET_MODE_INNER (mode);
19464 switch (GET_MODE_CLASS (mode))
19466 case MODE_VECTOR_INT:
19467 for (i = 0, p = array; i < length; i++, p += elt_size)
19469 rtx elt = CONST_VECTOR_ELT (rtl, i);
19470 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19472 break;
19474 case MODE_VECTOR_FLOAT:
19475 for (i = 0, p = array; i < length; i++, p += elt_size)
19477 rtx elt = CONST_VECTOR_ELT (rtl, i);
19478 insert_float (elt, p);
19480 break;
19482 default:
19483 gcc_unreachable ();
19486 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19488 return true;
19490 case CONST_STRING:
19491 if (dwarf_version >= 4 || !dwarf_strict)
19493 dw_loc_descr_ref loc_result;
19494 resolve_one_addr (&rtl);
19495 rtl_addr:
19496 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19497 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19498 add_AT_loc (die, DW_AT_location, loc_result);
19499 vec_safe_push (used_rtx_array, rtl);
19500 return true;
19502 return false;
19504 case CONST:
19505 if (CONSTANT_P (XEXP (rtl, 0)))
19506 return add_const_value_attribute (die, XEXP (rtl, 0));
19507 /* FALLTHROUGH */
19508 case SYMBOL_REF:
19509 if (!const_ok_for_output (rtl))
19510 return false;
19511 /* FALLTHROUGH */
19512 case LABEL_REF:
19513 if (dwarf_version >= 4 || !dwarf_strict)
19514 goto rtl_addr;
19515 return false;
19517 case PLUS:
19518 /* In cases where an inlined instance of an inline function is passed
19519 the address of an `auto' variable (which is local to the caller) we
19520 can get a situation where the DECL_RTL of the artificial local
19521 variable (for the inlining) which acts as a stand-in for the
19522 corresponding formal parameter (of the inline function) will look
19523 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19524 exactly a compile-time constant expression, but it isn't the address
19525 of the (artificial) local variable either. Rather, it represents the
19526 *value* which the artificial local variable always has during its
19527 lifetime. We currently have no way to represent such quasi-constant
19528 values in Dwarf, so for now we just punt and generate nothing. */
19529 return false;
19531 case HIGH:
19532 case CONST_FIXED:
19533 return false;
19535 case MEM:
19536 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19537 && MEM_READONLY_P (rtl)
19538 && GET_MODE (rtl) == BLKmode)
19540 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19541 return true;
19543 return false;
19545 default:
19546 /* No other kinds of rtx should be possible here. */
19547 gcc_unreachable ();
19549 return false;
19552 /* Determine whether the evaluation of EXPR references any variables
19553 or functions which aren't otherwise used (and therefore may not be
19554 output). */
19555 static tree
19556 reference_to_unused (tree * tp, int * walk_subtrees,
19557 void * data ATTRIBUTE_UNUSED)
19559 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19560 *walk_subtrees = 0;
19562 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19563 && ! TREE_ASM_WRITTEN (*tp))
19564 return *tp;
19565 /* ??? The C++ FE emits debug information for using decls, so
19566 putting gcc_unreachable here falls over. See PR31899. For now
19567 be conservative. */
19568 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19569 return *tp;
19570 else if (VAR_P (*tp))
19572 varpool_node *node = varpool_node::get (*tp);
19573 if (!node || !node->definition)
19574 return *tp;
19576 else if (TREE_CODE (*tp) == FUNCTION_DECL
19577 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19579 /* The call graph machinery must have finished analyzing,
19580 optimizing and gimplifying the CU by now.
19581 So if *TP has no call graph node associated
19582 to it, it means *TP will not be emitted. */
19583 if (!cgraph_node::get (*tp))
19584 return *tp;
19586 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19587 return *tp;
19589 return NULL_TREE;
19592 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19593 for use in a later add_const_value_attribute call. */
19595 static rtx
19596 rtl_for_decl_init (tree init, tree type)
19598 rtx rtl = NULL_RTX;
19600 STRIP_NOPS (init);
19602 /* If a variable is initialized with a string constant without embedded
19603 zeros, build CONST_STRING. */
19604 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19606 tree enttype = TREE_TYPE (type);
19607 tree domain = TYPE_DOMAIN (type);
19608 scalar_int_mode mode;
19610 if (is_int_mode (TYPE_MODE (enttype), &mode)
19611 && GET_MODE_SIZE (mode) == 1
19612 && domain
19613 && TYPE_MAX_VALUE (domain)
19614 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19615 && integer_zerop (TYPE_MIN_VALUE (domain))
19616 && compare_tree_int (TYPE_MAX_VALUE (domain),
19617 TREE_STRING_LENGTH (init) - 1) == 0
19618 && ((size_t) TREE_STRING_LENGTH (init)
19619 == strlen (TREE_STRING_POINTER (init)) + 1))
19621 rtl = gen_rtx_CONST_STRING (VOIDmode,
19622 ggc_strdup (TREE_STRING_POINTER (init)));
19623 rtl = gen_rtx_MEM (BLKmode, rtl);
19624 MEM_READONLY_P (rtl) = 1;
19627 /* Other aggregates, and complex values, could be represented using
19628 CONCAT: FIXME! */
19629 else if (AGGREGATE_TYPE_P (type)
19630 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19631 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19632 || TREE_CODE (type) == COMPLEX_TYPE)
19634 /* Vectors only work if their mode is supported by the target.
19635 FIXME: generic vectors ought to work too. */
19636 else if (TREE_CODE (type) == VECTOR_TYPE
19637 && !VECTOR_MODE_P (TYPE_MODE (type)))
19639 /* If the initializer is something that we know will expand into an
19640 immediate RTL constant, expand it now. We must be careful not to
19641 reference variables which won't be output. */
19642 else if (initializer_constant_valid_p (init, type)
19643 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19645 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19646 possible. */
19647 if (TREE_CODE (type) == VECTOR_TYPE)
19648 switch (TREE_CODE (init))
19650 case VECTOR_CST:
19651 break;
19652 case CONSTRUCTOR:
19653 if (TREE_CONSTANT (init))
19655 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19656 bool constant_p = true;
19657 tree value;
19658 unsigned HOST_WIDE_INT ix;
19660 /* Even when ctor is constant, it might contain non-*_CST
19661 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19662 belong into VECTOR_CST nodes. */
19663 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19664 if (!CONSTANT_CLASS_P (value))
19666 constant_p = false;
19667 break;
19670 if (constant_p)
19672 init = build_vector_from_ctor (type, elts);
19673 break;
19676 /* FALLTHRU */
19678 default:
19679 return NULL;
19682 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19684 /* If expand_expr returns a MEM, it wasn't immediate. */
19685 gcc_assert (!rtl || !MEM_P (rtl));
19688 return rtl;
19691 /* Generate RTL for the variable DECL to represent its location. */
19693 static rtx
19694 rtl_for_decl_location (tree decl)
19696 rtx rtl;
19698 /* Here we have to decide where we are going to say the parameter "lives"
19699 (as far as the debugger is concerned). We only have a couple of
19700 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19702 DECL_RTL normally indicates where the parameter lives during most of the
19703 activation of the function. If optimization is enabled however, this
19704 could be either NULL or else a pseudo-reg. Both of those cases indicate
19705 that the parameter doesn't really live anywhere (as far as the code
19706 generation parts of GCC are concerned) during most of the function's
19707 activation. That will happen (for example) if the parameter is never
19708 referenced within the function.
19710 We could just generate a location descriptor here for all non-NULL
19711 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19712 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19713 where DECL_RTL is NULL or is a pseudo-reg.
19715 Note however that we can only get away with using DECL_INCOMING_RTL as
19716 a backup substitute for DECL_RTL in certain limited cases. In cases
19717 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19718 we can be sure that the parameter was passed using the same type as it is
19719 declared to have within the function, and that its DECL_INCOMING_RTL
19720 points us to a place where a value of that type is passed.
19722 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19723 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19724 because in these cases DECL_INCOMING_RTL points us to a value of some
19725 type which is *different* from the type of the parameter itself. Thus,
19726 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19727 such cases, the debugger would end up (for example) trying to fetch a
19728 `float' from a place which actually contains the first part of a
19729 `double'. That would lead to really incorrect and confusing
19730 output at debug-time.
19732 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19733 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19734 are a couple of exceptions however. On little-endian machines we can
19735 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19736 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19737 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19738 when (on a little-endian machine) a non-prototyped function has a
19739 parameter declared to be of type `short' or `char'. In such cases,
19740 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19741 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19742 passed `int' value. If the debugger then uses that address to fetch
19743 a `short' or a `char' (on a little-endian machine) the result will be
19744 the correct data, so we allow for such exceptional cases below.
19746 Note that our goal here is to describe the place where the given formal
19747 parameter lives during most of the function's activation (i.e. between the
19748 end of the prologue and the start of the epilogue). We'll do that as best
19749 as we can. Note however that if the given formal parameter is modified
19750 sometime during the execution of the function, then a stack backtrace (at
19751 debug-time) will show the function as having been called with the *new*
19752 value rather than the value which was originally passed in. This happens
19753 rarely enough that it is not a major problem, but it *is* a problem, and
19754 I'd like to fix it.
19756 A future version of dwarf2out.c may generate two additional attributes for
19757 any given DW_TAG_formal_parameter DIE which will describe the "passed
19758 type" and the "passed location" for the given formal parameter in addition
19759 to the attributes we now generate to indicate the "declared type" and the
19760 "active location" for each parameter. This additional set of attributes
19761 could be used by debuggers for stack backtraces. Separately, note that
19762 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19763 This happens (for example) for inlined-instances of inline function formal
19764 parameters which are never referenced. This really shouldn't be
19765 happening. All PARM_DECL nodes should get valid non-NULL
19766 DECL_INCOMING_RTL values. FIXME. */
19768 /* Use DECL_RTL as the "location" unless we find something better. */
19769 rtl = DECL_RTL_IF_SET (decl);
19771 /* When generating abstract instances, ignore everything except
19772 constants, symbols living in memory, and symbols living in
19773 fixed registers. */
19774 if (! reload_completed)
19776 if (rtl
19777 && (CONSTANT_P (rtl)
19778 || (MEM_P (rtl)
19779 && CONSTANT_P (XEXP (rtl, 0)))
19780 || (REG_P (rtl)
19781 && VAR_P (decl)
19782 && TREE_STATIC (decl))))
19784 rtl = targetm.delegitimize_address (rtl);
19785 return rtl;
19787 rtl = NULL_RTX;
19789 else if (TREE_CODE (decl) == PARM_DECL)
19791 if (rtl == NULL_RTX
19792 || is_pseudo_reg (rtl)
19793 || (MEM_P (rtl)
19794 && is_pseudo_reg (XEXP (rtl, 0))
19795 && DECL_INCOMING_RTL (decl)
19796 && MEM_P (DECL_INCOMING_RTL (decl))
19797 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19799 tree declared_type = TREE_TYPE (decl);
19800 tree passed_type = DECL_ARG_TYPE (decl);
19801 machine_mode dmode = TYPE_MODE (declared_type);
19802 machine_mode pmode = TYPE_MODE (passed_type);
19804 /* This decl represents a formal parameter which was optimized out.
19805 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19806 all cases where (rtl == NULL_RTX) just below. */
19807 if (dmode == pmode)
19808 rtl = DECL_INCOMING_RTL (decl);
19809 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19810 && SCALAR_INT_MODE_P (dmode)
19811 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19812 && DECL_INCOMING_RTL (decl))
19814 rtx inc = DECL_INCOMING_RTL (decl);
19815 if (REG_P (inc))
19816 rtl = inc;
19817 else if (MEM_P (inc))
19819 if (BYTES_BIG_ENDIAN)
19820 rtl = adjust_address_nv (inc, dmode,
19821 GET_MODE_SIZE (pmode)
19822 - GET_MODE_SIZE (dmode));
19823 else
19824 rtl = inc;
19829 /* If the parm was passed in registers, but lives on the stack, then
19830 make a big endian correction if the mode of the type of the
19831 parameter is not the same as the mode of the rtl. */
19832 /* ??? This is the same series of checks that are made in dbxout.c before
19833 we reach the big endian correction code there. It isn't clear if all
19834 of these checks are necessary here, but keeping them all is the safe
19835 thing to do. */
19836 else if (MEM_P (rtl)
19837 && XEXP (rtl, 0) != const0_rtx
19838 && ! CONSTANT_P (XEXP (rtl, 0))
19839 /* Not passed in memory. */
19840 && !MEM_P (DECL_INCOMING_RTL (decl))
19841 /* Not passed by invisible reference. */
19842 && (!REG_P (XEXP (rtl, 0))
19843 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19844 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19845 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19846 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19847 #endif
19849 /* Big endian correction check. */
19850 && BYTES_BIG_ENDIAN
19851 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19852 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19853 UNITS_PER_WORD))
19855 machine_mode addr_mode = get_address_mode (rtl);
19856 poly_int64 offset = (UNITS_PER_WORD
19857 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19859 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19860 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19863 else if (VAR_P (decl)
19864 && rtl
19865 && MEM_P (rtl)
19866 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19868 machine_mode addr_mode = get_address_mode (rtl);
19869 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19870 GET_MODE (rtl));
19872 /* If a variable is declared "register" yet is smaller than
19873 a register, then if we store the variable to memory, it
19874 looks like we're storing a register-sized value, when in
19875 fact we are not. We need to adjust the offset of the
19876 storage location to reflect the actual value's bytes,
19877 else gdb will not be able to display it. */
19878 if (maybe_ne (offset, 0))
19879 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19880 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19883 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19884 and will have been substituted directly into all expressions that use it.
19885 C does not have such a concept, but C++ and other languages do. */
19886 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19887 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19889 if (rtl)
19890 rtl = targetm.delegitimize_address (rtl);
19892 /* If we don't look past the constant pool, we risk emitting a
19893 reference to a constant pool entry that isn't referenced from
19894 code, and thus is not emitted. */
19895 if (rtl)
19896 rtl = avoid_constant_pool_reference (rtl);
19898 /* Try harder to get a rtl. If this symbol ends up not being emitted
19899 in the current CU, resolve_addr will remove the expression referencing
19900 it. */
19901 if (rtl == NULL_RTX
19902 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
19903 && VAR_P (decl)
19904 && !DECL_EXTERNAL (decl)
19905 && TREE_STATIC (decl)
19906 && DECL_NAME (decl)
19907 && !DECL_HARD_REGISTER (decl)
19908 && DECL_MODE (decl) != VOIDmode)
19910 rtl = make_decl_rtl_for_debug (decl);
19911 if (!MEM_P (rtl)
19912 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19913 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19914 rtl = NULL_RTX;
19917 return rtl;
19920 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19921 returned. If so, the decl for the COMMON block is returned, and the
19922 value is the offset into the common block for the symbol. */
19924 static tree
19925 fortran_common (tree decl, HOST_WIDE_INT *value)
19927 tree val_expr, cvar;
19928 machine_mode mode;
19929 poly_int64 bitsize, bitpos;
19930 tree offset;
19931 HOST_WIDE_INT cbitpos;
19932 int unsignedp, reversep, volatilep = 0;
19934 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19935 it does not have a value (the offset into the common area), or if it
19936 is thread local (as opposed to global) then it isn't common, and shouldn't
19937 be handled as such. */
19938 if (!VAR_P (decl)
19939 || !TREE_STATIC (decl)
19940 || !DECL_HAS_VALUE_EXPR_P (decl)
19941 || !is_fortran ())
19942 return NULL_TREE;
19944 val_expr = DECL_VALUE_EXPR (decl);
19945 if (TREE_CODE (val_expr) != COMPONENT_REF)
19946 return NULL_TREE;
19948 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19949 &unsignedp, &reversep, &volatilep);
19951 if (cvar == NULL_TREE
19952 || !VAR_P (cvar)
19953 || DECL_ARTIFICIAL (cvar)
19954 || !TREE_PUBLIC (cvar)
19955 /* We don't expect to have to cope with variable offsets,
19956 since at present all static data must have a constant size. */
19957 || !bitpos.is_constant (&cbitpos))
19958 return NULL_TREE;
19960 *value = 0;
19961 if (offset != NULL)
19963 if (!tree_fits_shwi_p (offset))
19964 return NULL_TREE;
19965 *value = tree_to_shwi (offset);
19967 if (cbitpos != 0)
19968 *value += cbitpos / BITS_PER_UNIT;
19970 return cvar;
19973 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19974 data attribute for a variable or a parameter. We generate the
19975 DW_AT_const_value attribute only in those cases where the given variable
19976 or parameter does not have a true "location" either in memory or in a
19977 register. This can happen (for example) when a constant is passed as an
19978 actual argument in a call to an inline function. (It's possible that
19979 these things can crop up in other ways also.) Note that one type of
19980 constant value which can be passed into an inlined function is a constant
19981 pointer. This can happen for example if an actual argument in an inlined
19982 function call evaluates to a compile-time constant address.
19984 CACHE_P is true if it is worth caching the location list for DECL,
19985 so that future calls can reuse it rather than regenerate it from scratch.
19986 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19987 since we will need to refer to them each time the function is inlined. */
19989 static bool
19990 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19992 rtx rtl;
19993 dw_loc_list_ref list;
19994 var_loc_list *loc_list;
19995 cached_dw_loc_list *cache;
19997 if (early_dwarf)
19998 return false;
20000 if (TREE_CODE (decl) == ERROR_MARK)
20001 return false;
20003 if (get_AT (die, DW_AT_location)
20004 || get_AT (die, DW_AT_const_value))
20005 return true;
20007 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20008 || TREE_CODE (decl) == RESULT_DECL);
20010 /* Try to get some constant RTL for this decl, and use that as the value of
20011 the location. */
20013 rtl = rtl_for_decl_location (decl);
20014 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20015 && add_const_value_attribute (die, rtl))
20016 return true;
20018 /* See if we have single element location list that is equivalent to
20019 a constant value. That way we are better to use add_const_value_attribute
20020 rather than expanding constant value equivalent. */
20021 loc_list = lookup_decl_loc (decl);
20022 if (loc_list
20023 && loc_list->first
20024 && loc_list->first->next == NULL
20025 && NOTE_P (loc_list->first->loc)
20026 && NOTE_VAR_LOCATION (loc_list->first->loc)
20027 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20029 struct var_loc_node *node;
20031 node = loc_list->first;
20032 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20033 if (GET_CODE (rtl) == EXPR_LIST)
20034 rtl = XEXP (rtl, 0);
20035 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20036 && add_const_value_attribute (die, rtl))
20037 return true;
20039 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20040 list several times. See if we've already cached the contents. */
20041 list = NULL;
20042 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20043 cache_p = false;
20044 if (cache_p)
20046 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20047 if (cache)
20048 list = cache->loc_list;
20050 if (list == NULL)
20052 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20053 NULL);
20054 /* It is usually worth caching this result if the decl is from
20055 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20056 if (cache_p && list && list->dw_loc_next)
20058 cached_dw_loc_list **slot
20059 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20060 DECL_UID (decl),
20061 INSERT);
20062 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20063 cache->decl_id = DECL_UID (decl);
20064 cache->loc_list = list;
20065 *slot = cache;
20068 if (list)
20070 add_AT_location_description (die, DW_AT_location, list);
20071 return true;
20073 /* None of that worked, so it must not really have a location;
20074 try adding a constant value attribute from the DECL_INITIAL. */
20075 return tree_add_const_value_attribute_for_decl (die, decl);
20078 /* Helper function for tree_add_const_value_attribute. Natively encode
20079 initializer INIT into an array. Return true if successful. */
20081 static bool
20082 native_encode_initializer (tree init, unsigned char *array, int size)
20084 tree type;
20086 if (init == NULL_TREE)
20087 return false;
20089 STRIP_NOPS (init);
20090 switch (TREE_CODE (init))
20092 case STRING_CST:
20093 type = TREE_TYPE (init);
20094 if (TREE_CODE (type) == ARRAY_TYPE)
20096 tree enttype = TREE_TYPE (type);
20097 scalar_int_mode mode;
20099 if (!is_int_mode (TYPE_MODE (enttype), &mode)
20100 || GET_MODE_SIZE (mode) != 1)
20101 return false;
20102 if (int_size_in_bytes (type) != size)
20103 return false;
20104 if (size > TREE_STRING_LENGTH (init))
20106 memcpy (array, TREE_STRING_POINTER (init),
20107 TREE_STRING_LENGTH (init));
20108 memset (array + TREE_STRING_LENGTH (init),
20109 '\0', size - TREE_STRING_LENGTH (init));
20111 else
20112 memcpy (array, TREE_STRING_POINTER (init), size);
20113 return true;
20115 return false;
20116 case CONSTRUCTOR:
20117 type = TREE_TYPE (init);
20118 if (int_size_in_bytes (type) != size)
20119 return false;
20120 if (TREE_CODE (type) == ARRAY_TYPE)
20122 HOST_WIDE_INT min_index;
20123 unsigned HOST_WIDE_INT cnt;
20124 int curpos = 0, fieldsize;
20125 constructor_elt *ce;
20127 if (TYPE_DOMAIN (type) == NULL_TREE
20128 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20129 return false;
20131 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20132 if (fieldsize <= 0)
20133 return false;
20135 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20136 memset (array, '\0', size);
20137 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20139 tree val = ce->value;
20140 tree index = ce->index;
20141 int pos = curpos;
20142 if (index && TREE_CODE (index) == RANGE_EXPR)
20143 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20144 * fieldsize;
20145 else if (index)
20146 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20148 if (val)
20150 STRIP_NOPS (val);
20151 if (!native_encode_initializer (val, array + pos, fieldsize))
20152 return false;
20154 curpos = pos + fieldsize;
20155 if (index && TREE_CODE (index) == RANGE_EXPR)
20157 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20158 - tree_to_shwi (TREE_OPERAND (index, 0));
20159 while (count-- > 0)
20161 if (val)
20162 memcpy (array + curpos, array + pos, fieldsize);
20163 curpos += fieldsize;
20166 gcc_assert (curpos <= size);
20168 return true;
20170 else if (TREE_CODE (type) == RECORD_TYPE
20171 || TREE_CODE (type) == UNION_TYPE)
20173 tree field = NULL_TREE;
20174 unsigned HOST_WIDE_INT cnt;
20175 constructor_elt *ce;
20177 if (int_size_in_bytes (type) != size)
20178 return false;
20180 if (TREE_CODE (type) == RECORD_TYPE)
20181 field = TYPE_FIELDS (type);
20183 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20185 tree val = ce->value;
20186 int pos, fieldsize;
20188 if (ce->index != 0)
20189 field = ce->index;
20191 if (val)
20192 STRIP_NOPS (val);
20194 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20195 return false;
20197 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20198 && TYPE_DOMAIN (TREE_TYPE (field))
20199 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20200 return false;
20201 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20202 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20203 return false;
20204 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20205 pos = int_byte_position (field);
20206 gcc_assert (pos + fieldsize <= size);
20207 if (val && fieldsize != 0
20208 && !native_encode_initializer (val, array + pos, fieldsize))
20209 return false;
20211 return true;
20213 return false;
20214 case VIEW_CONVERT_EXPR:
20215 case NON_LVALUE_EXPR:
20216 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20217 default:
20218 return native_encode_expr (init, array, size) == size;
20222 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20223 attribute is the const value T. */
20225 static bool
20226 tree_add_const_value_attribute (dw_die_ref die, tree t)
20228 tree init;
20229 tree type = TREE_TYPE (t);
20230 rtx rtl;
20232 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20233 return false;
20235 init = t;
20236 gcc_assert (!DECL_P (init));
20238 if (TREE_CODE (init) == INTEGER_CST)
20240 if (tree_fits_uhwi_p (init))
20242 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20243 return true;
20245 if (tree_fits_shwi_p (init))
20247 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20248 return true;
20251 if (! early_dwarf)
20253 rtl = rtl_for_decl_init (init, type);
20254 if (rtl)
20255 return add_const_value_attribute (die, rtl);
20257 /* If the host and target are sane, try harder. */
20258 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20259 && initializer_constant_valid_p (init, type))
20261 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20262 if (size > 0 && (int) size == size)
20264 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20266 if (native_encode_initializer (init, array, size))
20268 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20269 return true;
20271 ggc_free (array);
20274 return false;
20277 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20278 attribute is the const value of T, where T is an integral constant
20279 variable with static storage duration
20280 (so it can't be a PARM_DECL or a RESULT_DECL). */
20282 static bool
20283 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20286 if (!decl
20287 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20288 || (VAR_P (decl) && !TREE_STATIC (decl)))
20289 return false;
20291 if (TREE_READONLY (decl)
20292 && ! TREE_THIS_VOLATILE (decl)
20293 && DECL_INITIAL (decl))
20294 /* OK */;
20295 else
20296 return false;
20298 /* Don't add DW_AT_const_value if abstract origin already has one. */
20299 if (get_AT (var_die, DW_AT_const_value))
20300 return false;
20302 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20305 /* Convert the CFI instructions for the current function into a
20306 location list. This is used for DW_AT_frame_base when we targeting
20307 a dwarf2 consumer that does not support the dwarf3
20308 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20309 expressions. */
20311 static dw_loc_list_ref
20312 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20314 int ix;
20315 dw_fde_ref fde;
20316 dw_loc_list_ref list, *list_tail;
20317 dw_cfi_ref cfi;
20318 dw_cfa_location last_cfa, next_cfa;
20319 const char *start_label, *last_label, *section;
20320 dw_cfa_location remember;
20322 fde = cfun->fde;
20323 gcc_assert (fde != NULL);
20325 section = secname_for_decl (current_function_decl);
20326 list_tail = &list;
20327 list = NULL;
20329 memset (&next_cfa, 0, sizeof (next_cfa));
20330 next_cfa.reg = INVALID_REGNUM;
20331 remember = next_cfa;
20333 start_label = fde->dw_fde_begin;
20335 /* ??? Bald assumption that the CIE opcode list does not contain
20336 advance opcodes. */
20337 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20338 lookup_cfa_1 (cfi, &next_cfa, &remember);
20340 last_cfa = next_cfa;
20341 last_label = start_label;
20343 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20345 /* If the first partition contained no CFI adjustments, the
20346 CIE opcodes apply to the whole first partition. */
20347 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20348 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20349 list_tail =&(*list_tail)->dw_loc_next;
20350 start_label = last_label = fde->dw_fde_second_begin;
20353 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20355 switch (cfi->dw_cfi_opc)
20357 case DW_CFA_set_loc:
20358 case DW_CFA_advance_loc1:
20359 case DW_CFA_advance_loc2:
20360 case DW_CFA_advance_loc4:
20361 if (!cfa_equal_p (&last_cfa, &next_cfa))
20363 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20364 start_label, 0, last_label, 0, section);
20366 list_tail = &(*list_tail)->dw_loc_next;
20367 last_cfa = next_cfa;
20368 start_label = last_label;
20370 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20371 break;
20373 case DW_CFA_advance_loc:
20374 /* The encoding is complex enough that we should never emit this. */
20375 gcc_unreachable ();
20377 default:
20378 lookup_cfa_1 (cfi, &next_cfa, &remember);
20379 break;
20381 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20383 if (!cfa_equal_p (&last_cfa, &next_cfa))
20385 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20386 start_label, 0, last_label, 0, section);
20388 list_tail = &(*list_tail)->dw_loc_next;
20389 last_cfa = next_cfa;
20390 start_label = last_label;
20392 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20393 start_label, 0, fde->dw_fde_end, 0, section);
20394 list_tail = &(*list_tail)->dw_loc_next;
20395 start_label = last_label = fde->dw_fde_second_begin;
20399 if (!cfa_equal_p (&last_cfa, &next_cfa))
20401 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20402 start_label, 0, last_label, 0, section);
20403 list_tail = &(*list_tail)->dw_loc_next;
20404 start_label = last_label;
20407 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20408 start_label, 0,
20409 fde->dw_fde_second_begin
20410 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20411 section);
20413 maybe_gen_llsym (list);
20415 return list;
20418 /* Compute a displacement from the "steady-state frame pointer" to the
20419 frame base (often the same as the CFA), and store it in
20420 frame_pointer_fb_offset. OFFSET is added to the displacement
20421 before the latter is negated. */
20423 static void
20424 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20426 rtx reg, elim;
20428 #ifdef FRAME_POINTER_CFA_OFFSET
20429 reg = frame_pointer_rtx;
20430 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20431 #else
20432 reg = arg_pointer_rtx;
20433 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20434 #endif
20436 elim = (ira_use_lra_p
20437 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20438 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20439 elim = strip_offset_and_add (elim, &offset);
20441 frame_pointer_fb_offset = -offset;
20443 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20444 in which to eliminate. This is because it's stack pointer isn't
20445 directly accessible as a register within the ISA. To work around
20446 this, assume that while we cannot provide a proper value for
20447 frame_pointer_fb_offset, we won't need one either. We can use
20448 hard frame pointer in debug info even if frame pointer isn't used
20449 since hard frame pointer in debug info is encoded with DW_OP_fbreg
20450 which uses the DW_AT_frame_base attribute, not hard frame pointer
20451 directly. */
20452 frame_pointer_fb_offset_valid
20453 = (elim == hard_frame_pointer_rtx || elim == stack_pointer_rtx);
20456 /* Generate a DW_AT_name attribute given some string value to be included as
20457 the value of the attribute. */
20459 static void
20460 add_name_attribute (dw_die_ref die, const char *name_string)
20462 if (name_string != NULL && *name_string != 0)
20464 if (demangle_name_func)
20465 name_string = (*demangle_name_func) (name_string);
20467 add_AT_string (die, DW_AT_name, name_string);
20471 /* Generate a DW_AT_description attribute given some string value to be included
20472 as the value of the attribute. */
20474 static void
20475 add_desc_attribute (dw_die_ref die, const char *name_string)
20477 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20478 return;
20480 if (name_string == NULL || *name_string == 0)
20481 return;
20483 if (demangle_name_func)
20484 name_string = (*demangle_name_func) (name_string);
20486 add_AT_string (die, DW_AT_description, name_string);
20489 /* Generate a DW_AT_description attribute given some decl to be included
20490 as the value of the attribute. */
20492 static void
20493 add_desc_attribute (dw_die_ref die, tree decl)
20495 tree decl_name;
20497 if (!flag_describe_dies || (dwarf_version < 3 && dwarf_strict))
20498 return;
20500 if (decl == NULL_TREE || !DECL_P (decl))
20501 return;
20502 decl_name = DECL_NAME (decl);
20504 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20506 const char *name = dwarf2_name (decl, 0);
20507 add_desc_attribute (die, name ? name : IDENTIFIER_POINTER (decl_name));
20509 else
20511 char *desc = print_generic_expr_to_str (decl);
20512 add_desc_attribute (die, desc);
20513 free (desc);
20517 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20518 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20519 of TYPE accordingly.
20521 ??? This is a temporary measure until after we're able to generate
20522 regular DWARF for the complex Ada type system. */
20524 static void
20525 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20526 dw_die_ref context_die)
20528 tree dtype;
20529 dw_die_ref dtype_die;
20531 if (!lang_hooks.types.descriptive_type)
20532 return;
20534 dtype = lang_hooks.types.descriptive_type (type);
20535 if (!dtype)
20536 return;
20538 dtype_die = lookup_type_die (dtype);
20539 if (!dtype_die)
20541 gen_type_die (dtype, context_die);
20542 dtype_die = lookup_type_die (dtype);
20543 gcc_assert (dtype_die);
20546 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20549 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20551 static const char *
20552 comp_dir_string (void)
20554 const char *wd;
20555 char *wd1;
20556 static const char *cached_wd = NULL;
20558 if (cached_wd != NULL)
20559 return cached_wd;
20561 wd = get_src_pwd ();
20562 if (wd == NULL)
20563 return NULL;
20565 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20567 int wdlen;
20569 wdlen = strlen (wd);
20570 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20571 strcpy (wd1, wd);
20572 wd1 [wdlen] = DIR_SEPARATOR;
20573 wd1 [wdlen + 1] = 0;
20574 wd = wd1;
20577 cached_wd = remap_debug_filename (wd);
20578 return cached_wd;
20581 /* Generate a DW_AT_comp_dir attribute for DIE. */
20583 static void
20584 add_comp_dir_attribute (dw_die_ref die)
20586 const char * wd = comp_dir_string ();
20587 if (wd != NULL)
20588 add_AT_string (die, DW_AT_comp_dir, wd);
20591 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20592 pointer computation, ...), output a representation for that bound according
20593 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20594 loc_list_from_tree for the meaning of CONTEXT. */
20596 static void
20597 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20598 int forms, struct loc_descr_context *context)
20600 dw_die_ref context_die, decl_die = NULL;
20601 dw_loc_list_ref list;
20602 bool strip_conversions = true;
20603 bool placeholder_seen = false;
20605 while (strip_conversions)
20606 switch (TREE_CODE (value))
20608 case ERROR_MARK:
20609 case SAVE_EXPR:
20610 return;
20612 CASE_CONVERT:
20613 case VIEW_CONVERT_EXPR:
20614 value = TREE_OPERAND (value, 0);
20615 break;
20617 default:
20618 strip_conversions = false;
20619 break;
20622 /* If possible and permitted, output the attribute as a constant. */
20623 if ((forms & dw_scalar_form_constant) != 0
20624 && TREE_CODE (value) == INTEGER_CST)
20626 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20628 /* If HOST_WIDE_INT is big enough then represent the bound as
20629 a constant value. We need to choose a form based on
20630 whether the type is signed or unsigned. We cannot just
20631 call add_AT_unsigned if the value itself is positive
20632 (add_AT_unsigned might add the unsigned value encoded as
20633 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20634 bounds type and then sign extend any unsigned values found
20635 for signed types. This is needed only for
20636 DW_AT_{lower,upper}_bound, since for most other attributes,
20637 consumers will treat DW_FORM_data[1248] as unsigned values,
20638 regardless of the underlying type. */
20639 if (prec <= HOST_BITS_PER_WIDE_INT
20640 || tree_fits_uhwi_p (value))
20642 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20643 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20644 else
20645 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20647 else
20648 /* Otherwise represent the bound as an unsigned value with
20649 the precision of its type. The precision and signedness
20650 of the type will be necessary to re-interpret it
20651 unambiguously. */
20652 add_AT_wide (die, attr, wi::to_wide (value));
20653 return;
20656 /* Otherwise, if it's possible and permitted too, output a reference to
20657 another DIE. */
20658 if ((forms & dw_scalar_form_reference) != 0)
20660 tree decl = NULL_TREE;
20662 /* Some type attributes reference an outer type. For instance, the upper
20663 bound of an array may reference an embedding record (this happens in
20664 Ada). */
20665 if (TREE_CODE (value) == COMPONENT_REF
20666 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20667 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20668 decl = TREE_OPERAND (value, 1);
20670 else if (VAR_P (value)
20671 || TREE_CODE (value) == PARM_DECL
20672 || TREE_CODE (value) == RESULT_DECL)
20673 decl = value;
20675 if (decl != NULL_TREE)
20677 decl_die = lookup_decl_die (decl);
20679 /* ??? Can this happen, or should the variable have been bound
20680 first? Probably it can, since I imagine that we try to create
20681 the types of parameters in the order in which they exist in
20682 the list, and won't have created a forward reference to a
20683 later parameter. */
20684 if (decl_die != NULL)
20686 if (get_AT (decl_die, DW_AT_location)
20687 || get_AT (decl_die, DW_AT_const_value))
20689 add_AT_die_ref (die, attr, decl_die);
20690 return;
20696 /* Last chance: try to create a stack operation procedure to evaluate the
20697 value. Do nothing if even that is not possible or permitted. */
20698 if ((forms & dw_scalar_form_exprloc) == 0)
20699 return;
20701 list = loc_list_from_tree (value, 2, context);
20702 if (context && context->placeholder_arg)
20704 placeholder_seen = context->placeholder_seen;
20705 context->placeholder_seen = false;
20707 if (list == NULL || single_element_loc_list_p (list))
20709 /* If this attribute is not a reference nor constant, it is
20710 a DWARF expression rather than location description. For that
20711 loc_list_from_tree (value, 0, &context) is needed. */
20712 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20713 if (list2 && single_element_loc_list_p (list2))
20715 if (placeholder_seen)
20717 struct dwarf_procedure_info dpi;
20718 dpi.fndecl = NULL_TREE;
20719 dpi.args_count = 1;
20720 if (!resolve_args_picking (list2->expr, 1, &dpi))
20721 return;
20723 add_AT_loc (die, attr, list2->expr);
20724 return;
20728 /* If that failed to give a single element location list, fall back to
20729 outputting this as a reference... still if permitted. */
20730 if (list == NULL
20731 || (forms & dw_scalar_form_reference) == 0
20732 || placeholder_seen)
20733 return;
20735 if (!decl_die)
20737 if (current_function_decl == 0)
20738 context_die = comp_unit_die ();
20739 else
20740 context_die = lookup_decl_die (current_function_decl);
20742 decl_die = new_die (DW_TAG_variable, context_die, value);
20743 add_AT_flag (decl_die, DW_AT_artificial, 1);
20744 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20745 context_die);
20748 add_AT_location_description (decl_die, DW_AT_location, list);
20749 add_AT_die_ref (die, attr, decl_die);
20752 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20753 default. */
20755 static int
20756 lower_bound_default (void)
20758 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20760 case DW_LANG_C:
20761 case DW_LANG_C89:
20762 case DW_LANG_C99:
20763 case DW_LANG_C11:
20764 case DW_LANG_C_plus_plus:
20765 case DW_LANG_C_plus_plus_11:
20766 case DW_LANG_C_plus_plus_14:
20767 case DW_LANG_ObjC:
20768 case DW_LANG_ObjC_plus_plus:
20769 return 0;
20770 case DW_LANG_Fortran77:
20771 case DW_LANG_Fortran90:
20772 case DW_LANG_Fortran95:
20773 case DW_LANG_Fortran03:
20774 case DW_LANG_Fortran08:
20775 return 1;
20776 case DW_LANG_UPC:
20777 case DW_LANG_D:
20778 case DW_LANG_Python:
20779 return dwarf_version >= 4 ? 0 : -1;
20780 case DW_LANG_Ada95:
20781 case DW_LANG_Ada83:
20782 case DW_LANG_Cobol74:
20783 case DW_LANG_Cobol85:
20784 case DW_LANG_Modula2:
20785 case DW_LANG_PLI:
20786 return dwarf_version >= 4 ? 1 : -1;
20787 default:
20788 return -1;
20792 /* Given a tree node describing an array bound (either lower or upper) output
20793 a representation for that bound. */
20795 static void
20796 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20797 tree bound, struct loc_descr_context *context)
20799 int dflt;
20801 while (1)
20802 switch (TREE_CODE (bound))
20804 /* Strip all conversions. */
20805 CASE_CONVERT:
20806 case VIEW_CONVERT_EXPR:
20807 bound = TREE_OPERAND (bound, 0);
20808 break;
20810 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20811 are even omitted when they are the default. */
20812 case INTEGER_CST:
20813 /* If the value for this bound is the default one, we can even omit the
20814 attribute. */
20815 if (bound_attr == DW_AT_lower_bound
20816 && tree_fits_shwi_p (bound)
20817 && (dflt = lower_bound_default ()) != -1
20818 && tree_to_shwi (bound) == dflt)
20819 return;
20821 /* FALLTHRU */
20823 default:
20824 /* Because of the complex interaction there can be with other GNAT
20825 encodings, GDB isn't ready yet to handle proper DWARF description
20826 for self-referencial subrange bounds: let GNAT encodings do the
20827 magic in such a case. */
20828 if (is_ada ()
20829 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20830 && contains_placeholder_p (bound))
20831 return;
20833 add_scalar_info (subrange_die, bound_attr, bound,
20834 dw_scalar_form_constant
20835 | dw_scalar_form_exprloc
20836 | dw_scalar_form_reference,
20837 context);
20838 return;
20842 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20843 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20844 Note that the block of subscript information for an array type also
20845 includes information about the element type of the given array type.
20847 This function reuses previously set type and bound information if
20848 available. */
20850 static void
20851 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20853 unsigned dimension_number;
20854 tree lower, upper;
20855 dw_die_ref child = type_die->die_child;
20857 for (dimension_number = 0;
20858 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20859 type = TREE_TYPE (type), dimension_number++)
20861 tree domain = TYPE_DOMAIN (type);
20863 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20864 break;
20866 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20867 and (in GNU C only) variable bounds. Handle all three forms
20868 here. */
20870 /* Find and reuse a previously generated DW_TAG_subrange_type if
20871 available.
20873 For multi-dimensional arrays, as we iterate through the
20874 various dimensions in the enclosing for loop above, we also
20875 iterate through the DIE children and pick at each
20876 DW_TAG_subrange_type previously generated (if available).
20877 Each child DW_TAG_subrange_type DIE describes the range of
20878 the current dimension. At this point we should have as many
20879 DW_TAG_subrange_type's as we have dimensions in the
20880 array. */
20881 dw_die_ref subrange_die = NULL;
20882 if (child)
20883 while (1)
20885 child = child->die_sib;
20886 if (child->die_tag == DW_TAG_subrange_type)
20887 subrange_die = child;
20888 if (child == type_die->die_child)
20890 /* If we wrapped around, stop looking next time. */
20891 child = NULL;
20892 break;
20894 if (child->die_tag == DW_TAG_subrange_type)
20895 break;
20897 if (!subrange_die)
20898 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20900 if (domain)
20902 /* We have an array type with specified bounds. */
20903 lower = TYPE_MIN_VALUE (domain);
20904 upper = TYPE_MAX_VALUE (domain);
20906 /* Define the index type. */
20907 if (TREE_TYPE (domain)
20908 && !get_AT (subrange_die, DW_AT_type))
20910 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20911 TREE_TYPE field. We can't emit debug info for this
20912 because it is an unnamed integral type. */
20913 if (TREE_CODE (domain) == INTEGER_TYPE
20914 && TYPE_NAME (domain) == NULL_TREE
20915 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20916 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20918 else
20919 add_type_attribute (subrange_die, TREE_TYPE (domain),
20920 TYPE_UNQUALIFIED, false, type_die);
20923 /* ??? If upper is NULL, the array has unspecified length,
20924 but it does have a lower bound. This happens with Fortran
20925 dimension arr(N:*)
20926 Since the debugger is definitely going to need to know N
20927 to produce useful results, go ahead and output the lower
20928 bound solo, and hope the debugger can cope. */
20930 if (!get_AT (subrange_die, DW_AT_lower_bound))
20931 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20932 if (!get_AT (subrange_die, DW_AT_upper_bound)
20933 && !get_AT (subrange_die, DW_AT_count))
20935 if (upper)
20936 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20937 else if ((is_c () || is_cxx ()) && COMPLETE_TYPE_P (type))
20938 /* Zero-length array. */
20939 add_bound_info (subrange_die, DW_AT_count,
20940 build_int_cst (TREE_TYPE (lower), 0), NULL);
20944 /* Otherwise we have an array type with an unspecified length. The
20945 DWARF-2 spec does not say how to handle this; let's just leave out the
20946 bounds. */
20950 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20952 static void
20953 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20955 dw_die_ref decl_die;
20956 HOST_WIDE_INT size;
20957 dw_loc_descr_ref size_expr = NULL;
20959 switch (TREE_CODE (tree_node))
20961 case ERROR_MARK:
20962 size = 0;
20963 break;
20964 case ENUMERAL_TYPE:
20965 case RECORD_TYPE:
20966 case UNION_TYPE:
20967 case QUAL_UNION_TYPE:
20968 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20969 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20971 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20972 return;
20974 size_expr = type_byte_size (tree_node, &size);
20975 break;
20976 case FIELD_DECL:
20977 /* For a data member of a struct or union, the DW_AT_byte_size is
20978 generally given as the number of bytes normally allocated for an
20979 object of the *declared* type of the member itself. This is true
20980 even for bit-fields. */
20981 size = int_size_in_bytes (field_type (tree_node));
20982 break;
20983 default:
20984 gcc_unreachable ();
20987 /* Support for dynamically-sized objects was introduced by DWARFv3.
20988 At the moment, GDB does not handle variable byte sizes very well,
20989 though. */
20990 if ((dwarf_version >= 3 || !dwarf_strict)
20991 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20992 && size_expr != NULL)
20993 add_AT_loc (die, DW_AT_byte_size, size_expr);
20995 /* Note that `size' might be -1 when we get to this point. If it is, that
20996 indicates that the byte size of the entity in question is variable and
20997 that we could not generate a DWARF expression that computes it. */
20998 if (size >= 0)
20999 add_AT_unsigned (die, DW_AT_byte_size, size);
21002 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
21003 alignment. */
21005 static void
21006 add_alignment_attribute (dw_die_ref die, tree tree_node)
21008 if (dwarf_version < 5 && dwarf_strict)
21009 return;
21011 unsigned align;
21013 if (DECL_P (tree_node))
21015 if (!DECL_USER_ALIGN (tree_node))
21016 return;
21018 align = DECL_ALIGN_UNIT (tree_node);
21020 else if (TYPE_P (tree_node))
21022 if (!TYPE_USER_ALIGN (tree_node))
21023 return;
21025 align = TYPE_ALIGN_UNIT (tree_node);
21027 else
21028 gcc_unreachable ();
21030 add_AT_unsigned (die, DW_AT_alignment, align);
21033 /* For a FIELD_DECL node which represents a bit-field, output an attribute
21034 which specifies the distance in bits from the highest order bit of the
21035 "containing object" for the bit-field to the highest order bit of the
21036 bit-field itself.
21038 For any given bit-field, the "containing object" is a hypothetical object
21039 (of some integral or enum type) within which the given bit-field lives. The
21040 type of this hypothetical "containing object" is always the same as the
21041 declared type of the individual bit-field itself. The determination of the
21042 exact location of the "containing object" for a bit-field is rather
21043 complicated. It's handled by the `field_byte_offset' function (above).
21045 CTX is required: see the comment for VLR_CONTEXT.
21047 Note that it is the size (in bytes) of the hypothetical "containing object"
21048 which will be given in the DW_AT_byte_size attribute for this bit-field.
21049 (See `byte_size_attribute' above). */
21051 static inline void
21052 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
21054 HOST_WIDE_INT object_offset_in_bytes;
21055 tree original_type = DECL_BIT_FIELD_TYPE (decl);
21056 HOST_WIDE_INT bitpos_int;
21057 HOST_WIDE_INT highest_order_object_bit_offset;
21058 HOST_WIDE_INT highest_order_field_bit_offset;
21059 HOST_WIDE_INT bit_offset;
21061 field_byte_offset (decl, ctx, &object_offset_in_bytes);
21063 /* Must be a field and a bit field. */
21064 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21066 /* We can't yet handle bit-fields whose offsets are variable, so if we
21067 encounter such things, just return without generating any attribute
21068 whatsoever. Likewise for variable or too large size. */
21069 if (! tree_fits_shwi_p (bit_position (decl))
21070 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21071 return;
21073 bitpos_int = int_bit_position (decl);
21075 /* Note that the bit offset is always the distance (in bits) from the
21076 highest-order bit of the "containing object" to the highest-order bit of
21077 the bit-field itself. Since the "high-order end" of any object or field
21078 is different on big-endian and little-endian machines, the computation
21079 below must take account of these differences. */
21080 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21081 highest_order_field_bit_offset = bitpos_int;
21083 if (! BYTES_BIG_ENDIAN)
21085 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21086 highest_order_object_bit_offset +=
21087 simple_type_size_in_bits (original_type);
21090 bit_offset
21091 = (! BYTES_BIG_ENDIAN
21092 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21093 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21095 if (bit_offset < 0)
21096 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21097 else
21098 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21101 /* For a FIELD_DECL node which represents a bit field, output an attribute
21102 which specifies the length in bits of the given field. */
21104 static inline void
21105 add_bit_size_attribute (dw_die_ref die, tree decl)
21107 /* Must be a field and a bit field. */
21108 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21109 && DECL_BIT_FIELD_TYPE (decl));
21111 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21112 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21115 /* If the compiled language is ANSI C, then add a 'prototyped'
21116 attribute, if arg types are given for the parameters of a function. */
21118 static inline void
21119 add_prototyped_attribute (dw_die_ref die, tree func_type)
21121 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21123 case DW_LANG_C:
21124 case DW_LANG_C89:
21125 case DW_LANG_C99:
21126 case DW_LANG_C11:
21127 case DW_LANG_ObjC:
21128 if (prototype_p (func_type))
21129 add_AT_flag (die, DW_AT_prototyped, 1);
21130 break;
21131 default:
21132 break;
21136 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21137 by looking in the type declaration, the object declaration equate table or
21138 the block mapping. */
21140 static inline void
21141 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21143 dw_die_ref origin_die = NULL;
21145 /* For late LTO debug output we want to refer directly to the abstract
21146 DIE in the early debug rather to the possibly existing concrete
21147 instance and avoid creating that just for this purpose. */
21148 sym_off_pair *desc;
21149 if (in_lto_p
21150 && external_die_map
21151 && (desc = external_die_map->get (origin)))
21153 add_AT_external_die_ref (die, DW_AT_abstract_origin,
21154 desc->sym, desc->off);
21155 return;
21158 if (DECL_P (origin))
21159 origin_die = lookup_decl_die (origin);
21160 else if (TYPE_P (origin))
21161 origin_die = lookup_type_die (origin);
21162 else if (TREE_CODE (origin) == BLOCK)
21163 origin_die = lookup_block_die (origin);
21165 /* XXX: Functions that are never lowered don't always have correct block
21166 trees (in the case of java, they simply have no block tree, in some other
21167 languages). For these functions, there is nothing we can really do to
21168 output correct debug info for inlined functions in all cases. Rather
21169 than die, we'll just produce deficient debug info now, in that we will
21170 have variables without a proper abstract origin. In the future, when all
21171 functions are lowered, we should re-add a gcc_assert (origin_die)
21172 here. */
21174 if (origin_die)
21175 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21178 /* We do not currently support the pure_virtual attribute. */
21180 static inline void
21181 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21183 if (DECL_VINDEX (func_decl))
21185 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21187 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21188 add_AT_loc (die, DW_AT_vtable_elem_location,
21189 new_loc_descr (DW_OP_constu,
21190 tree_to_shwi (DECL_VINDEX (func_decl)),
21191 0));
21193 /* GNU extension: Record what type this method came from originally. */
21194 if (debug_info_level > DINFO_LEVEL_TERSE
21195 && DECL_CONTEXT (func_decl))
21196 add_AT_die_ref (die, DW_AT_containing_type,
21197 lookup_type_die (DECL_CONTEXT (func_decl)));
21201 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21202 given decl. This used to be a vendor extension until after DWARF 4
21203 standardized it. */
21205 static void
21206 add_linkage_attr (dw_die_ref die, tree decl)
21208 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21210 /* Mimic what assemble_name_raw does with a leading '*'. */
21211 if (name[0] == '*')
21212 name = &name[1];
21214 if (dwarf_version >= 4)
21215 add_AT_string (die, DW_AT_linkage_name, name);
21216 else
21217 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21220 /* Add source coordinate attributes for the given decl. */
21222 static void
21223 add_src_coords_attributes (dw_die_ref die, tree decl)
21225 expanded_location s;
21227 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21228 return;
21229 s = expand_location (DECL_SOURCE_LOCATION (decl));
21230 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21231 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21232 if (debug_column_info && s.column)
21233 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21236 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21238 static void
21239 add_linkage_name_raw (dw_die_ref die, tree decl)
21241 /* Defer until we have an assembler name set. */
21242 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21244 limbo_die_node *asm_name;
21246 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21247 asm_name->die = die;
21248 asm_name->created_for = decl;
21249 asm_name->next = deferred_asm_name;
21250 deferred_asm_name = asm_name;
21252 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21253 add_linkage_attr (die, decl);
21256 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21258 static void
21259 add_linkage_name (dw_die_ref die, tree decl)
21261 if (debug_info_level > DINFO_LEVEL_NONE
21262 && VAR_OR_FUNCTION_DECL_P (decl)
21263 && TREE_PUBLIC (decl)
21264 && !(VAR_P (decl) && DECL_REGISTER (decl))
21265 && die->die_tag != DW_TAG_member)
21266 add_linkage_name_raw (die, decl);
21269 /* Add a DW_AT_name attribute and source coordinate attribute for the
21270 given decl, but only if it actually has a name. */
21272 static void
21273 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21274 bool no_linkage_name)
21276 tree decl_name;
21278 decl_name = DECL_NAME (decl);
21279 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21281 const char *name = dwarf2_name (decl, 0);
21282 if (name)
21283 add_name_attribute (die, name);
21284 else
21285 add_desc_attribute (die, decl);
21287 if (! DECL_ARTIFICIAL (decl))
21288 add_src_coords_attributes (die, decl);
21290 if (!no_linkage_name)
21291 add_linkage_name (die, decl);
21293 else
21294 add_desc_attribute (die, decl);
21296 #ifdef VMS_DEBUGGING_INFO
21297 /* Get the function's name, as described by its RTL. This may be different
21298 from the DECL_NAME name used in the source file. */
21299 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21301 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21302 XEXP (DECL_RTL (decl), 0), false);
21303 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21305 #endif /* VMS_DEBUGGING_INFO */
21308 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21310 static void
21311 add_discr_value (dw_die_ref die, dw_discr_value *value)
21313 dw_attr_node attr;
21315 attr.dw_attr = DW_AT_discr_value;
21316 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21317 attr.dw_attr_val.val_entry = NULL;
21318 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21319 if (value->pos)
21320 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21321 else
21322 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21323 add_dwarf_attr (die, &attr);
21326 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21328 static void
21329 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21331 dw_attr_node attr;
21333 attr.dw_attr = DW_AT_discr_list;
21334 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21335 attr.dw_attr_val.val_entry = NULL;
21336 attr.dw_attr_val.v.val_discr_list = discr_list;
21337 add_dwarf_attr (die, &attr);
21340 static inline dw_discr_list_ref
21341 AT_discr_list (dw_attr_node *attr)
21343 return attr->dw_attr_val.v.val_discr_list;
21346 #ifdef VMS_DEBUGGING_INFO
21347 /* Output the debug main pointer die for VMS */
21349 void
21350 dwarf2out_vms_debug_main_pointer (void)
21352 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21353 dw_die_ref die;
21355 /* Allocate the VMS debug main subprogram die. */
21356 die = new_die_raw (DW_TAG_subprogram);
21357 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21358 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21359 current_function_funcdef_no);
21360 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21362 /* Make it the first child of comp_unit_die (). */
21363 die->die_parent = comp_unit_die ();
21364 if (comp_unit_die ()->die_child)
21366 die->die_sib = comp_unit_die ()->die_child->die_sib;
21367 comp_unit_die ()->die_child->die_sib = die;
21369 else
21371 die->die_sib = die;
21372 comp_unit_die ()->die_child = die;
21375 #endif /* VMS_DEBUGGING_INFO */
21377 /* walk_tree helper function for uses_local_type, below. */
21379 static tree
21380 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21382 if (!TYPE_P (*tp))
21383 *walk_subtrees = 0;
21384 else
21386 tree name = TYPE_NAME (*tp);
21387 if (name && DECL_P (name) && decl_function_context (name))
21388 return *tp;
21390 return NULL_TREE;
21393 /* If TYPE involves a function-local type (including a local typedef to a
21394 non-local type), returns that type; otherwise returns NULL_TREE. */
21396 static tree
21397 uses_local_type (tree type)
21399 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21400 return used;
21403 /* Return the DIE for the scope that immediately contains this type.
21404 Non-named types that do not involve a function-local type get global
21405 scope. Named types nested in namespaces or other types get their
21406 containing scope. All other types (i.e. function-local named types) get
21407 the current active scope. */
21409 static dw_die_ref
21410 scope_die_for (tree t, dw_die_ref context_die)
21412 dw_die_ref scope_die = NULL;
21413 tree containing_scope;
21415 /* Non-types always go in the current scope. */
21416 gcc_assert (TYPE_P (t));
21418 /* Use the scope of the typedef, rather than the scope of the type
21419 it refers to. */
21420 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21421 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21422 else
21423 containing_scope = TYPE_CONTEXT (t);
21425 /* Use the containing namespace if there is one. */
21426 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21428 if (context_die == lookup_decl_die (containing_scope))
21429 /* OK */;
21430 else if (debug_info_level > DINFO_LEVEL_TERSE)
21431 context_die = get_context_die (containing_scope);
21432 else
21433 containing_scope = NULL_TREE;
21436 /* Ignore function type "scopes" from the C frontend. They mean that
21437 a tagged type is local to a parmlist of a function declarator, but
21438 that isn't useful to DWARF. */
21439 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21440 containing_scope = NULL_TREE;
21442 if (SCOPE_FILE_SCOPE_P (containing_scope))
21444 /* If T uses a local type keep it local as well, to avoid references
21445 to function-local DIEs from outside the function. */
21446 if (current_function_decl && uses_local_type (t))
21447 scope_die = context_die;
21448 else
21449 scope_die = comp_unit_die ();
21451 else if (TYPE_P (containing_scope))
21453 /* For types, we can just look up the appropriate DIE. */
21454 if (debug_info_level > DINFO_LEVEL_TERSE)
21455 scope_die = get_context_die (containing_scope);
21456 else
21458 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21459 if (scope_die == NULL)
21460 scope_die = comp_unit_die ();
21463 else
21464 scope_die = context_die;
21466 return scope_die;
21469 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21471 static inline int
21472 local_scope_p (dw_die_ref context_die)
21474 for (; context_die; context_die = context_die->die_parent)
21475 if (context_die->die_tag == DW_TAG_inlined_subroutine
21476 || context_die->die_tag == DW_TAG_subprogram)
21477 return 1;
21479 return 0;
21482 /* Returns nonzero if CONTEXT_DIE is a class. */
21484 static inline int
21485 class_scope_p (dw_die_ref context_die)
21487 return (context_die
21488 && (context_die->die_tag == DW_TAG_structure_type
21489 || context_die->die_tag == DW_TAG_class_type
21490 || context_die->die_tag == DW_TAG_interface_type
21491 || context_die->die_tag == DW_TAG_union_type));
21494 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21495 whether or not to treat a DIE in this context as a declaration. */
21497 static inline int
21498 class_or_namespace_scope_p (dw_die_ref context_die)
21500 return (class_scope_p (context_die)
21501 || (context_die && context_die->die_tag == DW_TAG_namespace));
21504 /* Many forms of DIEs require a "type description" attribute. This
21505 routine locates the proper "type descriptor" die for the type given
21506 by 'type' plus any additional qualifiers given by 'cv_quals', and
21507 adds a DW_AT_type attribute below the given die. */
21509 static void
21510 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21511 bool reverse, dw_die_ref context_die)
21513 enum tree_code code = TREE_CODE (type);
21514 dw_die_ref type_die = NULL;
21516 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21517 or fixed-point type, use the inner type. This is because we have no
21518 support for unnamed types in base_type_die. This can happen if this is
21519 an Ada subrange type. Correct solution is emit a subrange type die. */
21520 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21521 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21522 type = TREE_TYPE (type), code = TREE_CODE (type);
21524 if (code == ERROR_MARK
21525 /* Handle a special case. For functions whose return type is void, we
21526 generate *no* type attribute. (Note that no object may have type
21527 `void', so this only applies to function return types). */
21528 || code == VOID_TYPE)
21529 return;
21531 type_die = modified_type_die (type,
21532 cv_quals | TYPE_QUALS (type),
21533 reverse,
21534 context_die);
21536 if (type_die != NULL)
21537 add_AT_die_ref (object_die, DW_AT_type, type_die);
21540 /* Given an object die, add the calling convention attribute for the
21541 function call type. */
21542 static void
21543 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21545 enum dwarf_calling_convention value = DW_CC_normal;
21547 value = ((enum dwarf_calling_convention)
21548 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21550 if (is_fortran ()
21551 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21553 /* DWARF 2 doesn't provide a way to identify a program's source-level
21554 entry point. DW_AT_calling_convention attributes are only meant
21555 to describe functions' calling conventions. However, lacking a
21556 better way to signal the Fortran main program, we used this for
21557 a long time, following existing custom. Now, DWARF 4 has
21558 DW_AT_main_subprogram, which we add below, but some tools still
21559 rely on the old way, which we thus keep. */
21560 value = DW_CC_program;
21562 if (dwarf_version >= 4 || !dwarf_strict)
21563 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21566 /* Only add the attribute if the backend requests it, and
21567 is not DW_CC_normal. */
21568 if (value && (value != DW_CC_normal))
21569 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21572 /* Given a tree pointer to a struct, class, union, or enum type node, return
21573 a pointer to the (string) tag name for the given type, or zero if the type
21574 was declared without a tag. */
21576 static const char *
21577 type_tag (const_tree type)
21579 const char *name = 0;
21581 if (TYPE_NAME (type) != 0)
21583 tree t = 0;
21585 /* Find the IDENTIFIER_NODE for the type name. */
21586 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21587 && !TYPE_NAMELESS (type))
21588 t = TYPE_NAME (type);
21590 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21591 a TYPE_DECL node, regardless of whether or not a `typedef' was
21592 involved. */
21593 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21594 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21596 /* We want to be extra verbose. Don't call dwarf_name if
21597 DECL_NAME isn't set. The default hook for decl_printable_name
21598 doesn't like that, and in this context it's correct to return
21599 0, instead of "<anonymous>" or the like. */
21600 if (DECL_NAME (TYPE_NAME (type))
21601 && !DECL_NAMELESS (TYPE_NAME (type)))
21602 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21605 /* Now get the name as a string, or invent one. */
21606 if (!name && t != 0)
21607 name = IDENTIFIER_POINTER (t);
21610 return (name == 0 || *name == '\0') ? 0 : name;
21613 /* Return the type associated with a data member, make a special check
21614 for bit field types. */
21616 static inline tree
21617 member_declared_type (const_tree member)
21619 return (DECL_BIT_FIELD_TYPE (member)
21620 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21623 /* Get the decl's label, as described by its RTL. This may be different
21624 from the DECL_NAME name used in the source file. */
21626 #if 0
21627 static const char *
21628 decl_start_label (tree decl)
21630 rtx x;
21631 const char *fnname;
21633 x = DECL_RTL (decl);
21634 gcc_assert (MEM_P (x));
21636 x = XEXP (x, 0);
21637 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21639 fnname = XSTR (x, 0);
21640 return fnname;
21642 #endif
21644 /* For variable-length arrays that have been previously generated, but
21645 may be incomplete due to missing subscript info, fill the subscript
21646 info. Return TRUE if this is one of those cases. */
21647 static bool
21648 fill_variable_array_bounds (tree type)
21650 if (TREE_ASM_WRITTEN (type)
21651 && TREE_CODE (type) == ARRAY_TYPE
21652 && variably_modified_type_p (type, NULL))
21654 dw_die_ref array_die = lookup_type_die (type);
21655 if (!array_die)
21656 return false;
21657 add_subscript_info (array_die, type, !is_ada ());
21658 return true;
21660 return false;
21663 /* These routines generate the internal representation of the DIE's for
21664 the compilation unit. Debugging information is collected by walking
21665 the declaration trees passed in from dwarf2out_decl(). */
21667 static void
21668 gen_array_type_die (tree type, dw_die_ref context_die)
21670 dw_die_ref array_die;
21672 /* GNU compilers represent multidimensional array types as sequences of one
21673 dimensional array types whose element types are themselves array types.
21674 We sometimes squish that down to a single array_type DIE with multiple
21675 subscripts in the Dwarf debugging info. The draft Dwarf specification
21676 say that we are allowed to do this kind of compression in C, because
21677 there is no difference between an array of arrays and a multidimensional
21678 array. We don't do this for Ada to remain as close as possible to the
21679 actual representation, which is especially important against the language
21680 flexibilty wrt arrays of variable size. */
21682 bool collapse_nested_arrays = !is_ada ();
21684 if (fill_variable_array_bounds (type))
21685 return;
21687 dw_die_ref scope_die = scope_die_for (type, context_die);
21688 tree element_type;
21690 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21691 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21692 if (TYPE_STRING_FLAG (type)
21693 && TREE_CODE (type) == ARRAY_TYPE
21694 && is_fortran ()
21695 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21697 HOST_WIDE_INT size;
21699 array_die = new_die (DW_TAG_string_type, scope_die, type);
21700 add_name_attribute (array_die, type_tag (type));
21701 equate_type_number_to_die (type, array_die);
21702 size = int_size_in_bytes (type);
21703 if (size >= 0)
21704 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21705 /* ??? We can't annotate types late, but for LTO we may not
21706 generate a location early either (gfortran.dg/save_6.f90). */
21707 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21708 && TYPE_DOMAIN (type) != NULL_TREE
21709 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21711 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21712 tree rszdecl = szdecl;
21714 size = int_size_in_bytes (TREE_TYPE (szdecl));
21715 if (!DECL_P (szdecl))
21717 if (TREE_CODE (szdecl) == INDIRECT_REF
21718 && DECL_P (TREE_OPERAND (szdecl, 0)))
21720 rszdecl = TREE_OPERAND (szdecl, 0);
21721 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21722 != DWARF2_ADDR_SIZE)
21723 size = 0;
21725 else
21726 size = 0;
21728 if (size > 0)
21730 dw_loc_list_ref loc
21731 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21732 NULL);
21733 if (loc)
21735 add_AT_location_description (array_die, DW_AT_string_length,
21736 loc);
21737 if (size != DWARF2_ADDR_SIZE)
21738 add_AT_unsigned (array_die, dwarf_version >= 5
21739 ? DW_AT_string_length_byte_size
21740 : DW_AT_byte_size, size);
21744 return;
21747 array_die = new_die (DW_TAG_array_type, scope_die, type);
21748 add_name_attribute (array_die, type_tag (type));
21749 equate_type_number_to_die (type, array_die);
21751 if (TREE_CODE (type) == VECTOR_TYPE)
21752 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21754 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21755 if (is_fortran ()
21756 && TREE_CODE (type) == ARRAY_TYPE
21757 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21758 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21759 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21761 #if 0
21762 /* We default the array ordering. Debuggers will probably do the right
21763 things even if DW_AT_ordering is not present. It's not even an issue
21764 until we start to get into multidimensional arrays anyway. If a debugger
21765 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21766 then we'll have to put the DW_AT_ordering attribute back in. (But if
21767 and when we find out that we need to put these in, we will only do so
21768 for multidimensional arrays. */
21769 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21770 #endif
21772 if (TREE_CODE (type) == VECTOR_TYPE)
21774 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21775 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21776 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21777 add_bound_info (subrange_die, DW_AT_upper_bound,
21778 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21780 else
21781 add_subscript_info (array_die, type, collapse_nested_arrays);
21783 /* Add representation of the type of the elements of this array type and
21784 emit the corresponding DIE if we haven't done it already. */
21785 element_type = TREE_TYPE (type);
21786 if (collapse_nested_arrays)
21787 while (TREE_CODE (element_type) == ARRAY_TYPE)
21789 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21790 break;
21791 element_type = TREE_TYPE (element_type);
21794 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21795 TREE_CODE (type) == ARRAY_TYPE
21796 && TYPE_REVERSE_STORAGE_ORDER (type),
21797 context_die);
21799 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21800 if (TYPE_ARTIFICIAL (type))
21801 add_AT_flag (array_die, DW_AT_artificial, 1);
21803 if (get_AT (array_die, DW_AT_name))
21804 add_pubtype (type, array_die);
21806 add_alignment_attribute (array_die, type);
21809 /* This routine generates DIE for array with hidden descriptor, details
21810 are filled into *info by a langhook. */
21812 static void
21813 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21814 dw_die_ref context_die)
21816 const dw_die_ref scope_die = scope_die_for (type, context_die);
21817 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21818 struct loc_descr_context context = { type, info->base_decl, NULL,
21819 false, false };
21820 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21821 int dim;
21823 add_name_attribute (array_die, type_tag (type));
21824 equate_type_number_to_die (type, array_die);
21826 if (info->ndimensions > 1)
21827 switch (info->ordering)
21829 case array_descr_ordering_row_major:
21830 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21831 break;
21832 case array_descr_ordering_column_major:
21833 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21834 break;
21835 default:
21836 break;
21839 if (dwarf_version >= 3 || !dwarf_strict)
21841 if (info->data_location)
21842 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21843 dw_scalar_form_exprloc, &context);
21844 if (info->associated)
21845 add_scalar_info (array_die, DW_AT_associated, info->associated,
21846 dw_scalar_form_constant
21847 | dw_scalar_form_exprloc
21848 | dw_scalar_form_reference, &context);
21849 if (info->allocated)
21850 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21851 dw_scalar_form_constant
21852 | dw_scalar_form_exprloc
21853 | dw_scalar_form_reference, &context);
21854 if (info->stride)
21856 const enum dwarf_attribute attr
21857 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21858 const int forms
21859 = (info->stride_in_bits)
21860 ? dw_scalar_form_constant
21861 : (dw_scalar_form_constant
21862 | dw_scalar_form_exprloc
21863 | dw_scalar_form_reference);
21865 add_scalar_info (array_die, attr, info->stride, forms, &context);
21868 if (dwarf_version >= 5)
21870 if (info->rank)
21872 add_scalar_info (array_die, DW_AT_rank, info->rank,
21873 dw_scalar_form_constant
21874 | dw_scalar_form_exprloc, &context);
21875 subrange_tag = DW_TAG_generic_subrange;
21876 context.placeholder_arg = true;
21880 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21882 for (dim = 0; dim < info->ndimensions; dim++)
21884 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21886 if (info->dimen[dim].bounds_type)
21887 add_type_attribute (subrange_die,
21888 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21889 false, context_die);
21890 if (info->dimen[dim].lower_bound)
21891 add_bound_info (subrange_die, DW_AT_lower_bound,
21892 info->dimen[dim].lower_bound, &context);
21893 if (info->dimen[dim].upper_bound)
21894 add_bound_info (subrange_die, DW_AT_upper_bound,
21895 info->dimen[dim].upper_bound, &context);
21896 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21897 add_scalar_info (subrange_die, DW_AT_byte_stride,
21898 info->dimen[dim].stride,
21899 dw_scalar_form_constant
21900 | dw_scalar_form_exprloc
21901 | dw_scalar_form_reference,
21902 &context);
21905 gen_type_die (info->element_type, context_die);
21906 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21907 TREE_CODE (type) == ARRAY_TYPE
21908 && TYPE_REVERSE_STORAGE_ORDER (type),
21909 context_die);
21911 if (get_AT (array_die, DW_AT_name))
21912 add_pubtype (type, array_die);
21914 add_alignment_attribute (array_die, type);
21917 #if 0
21918 static void
21919 gen_entry_point_die (tree decl, dw_die_ref context_die)
21921 tree origin = decl_ultimate_origin (decl);
21922 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21924 if (origin != NULL)
21925 add_abstract_origin_attribute (decl_die, origin);
21926 else
21928 add_name_and_src_coords_attributes (decl_die, decl);
21929 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21930 TYPE_UNQUALIFIED, false, context_die);
21933 if (DECL_ABSTRACT_P (decl))
21934 equate_decl_number_to_die (decl, decl_die);
21935 else
21936 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21938 #endif
21940 /* Walk through the list of incomplete types again, trying once more to
21941 emit full debugging info for them. */
21943 static void
21944 retry_incomplete_types (void)
21946 set_early_dwarf s;
21947 int i;
21949 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21950 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21951 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21952 vec_safe_truncate (incomplete_types, 0);
21955 /* Determine what tag to use for a record type. */
21957 static enum dwarf_tag
21958 record_type_tag (tree type)
21960 if (! lang_hooks.types.classify_record)
21961 return DW_TAG_structure_type;
21963 switch (lang_hooks.types.classify_record (type))
21965 case RECORD_IS_STRUCT:
21966 return DW_TAG_structure_type;
21968 case RECORD_IS_CLASS:
21969 return DW_TAG_class_type;
21971 case RECORD_IS_INTERFACE:
21972 if (dwarf_version >= 3 || !dwarf_strict)
21973 return DW_TAG_interface_type;
21974 return DW_TAG_structure_type;
21976 default:
21977 gcc_unreachable ();
21981 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21982 include all of the information about the enumeration values also. Each
21983 enumerated type name/value is listed as a child of the enumerated type
21984 DIE. */
21986 static dw_die_ref
21987 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21989 dw_die_ref type_die = lookup_type_die (type);
21990 dw_die_ref orig_type_die = type_die;
21992 if (type_die == NULL)
21994 type_die = new_die (DW_TAG_enumeration_type,
21995 scope_die_for (type, context_die), type);
21996 equate_type_number_to_die (type, type_die);
21997 add_name_attribute (type_die, type_tag (type));
21998 if ((dwarf_version >= 4 || !dwarf_strict)
21999 && ENUM_IS_SCOPED (type))
22000 add_AT_flag (type_die, DW_AT_enum_class, 1);
22001 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
22002 add_AT_flag (type_die, DW_AT_declaration, 1);
22003 if (!dwarf_strict)
22004 add_AT_unsigned (type_die, DW_AT_encoding,
22005 TYPE_UNSIGNED (type)
22006 ? DW_ATE_unsigned
22007 : DW_ATE_signed);
22009 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
22010 return type_die;
22011 else
22012 remove_AT (type_die, DW_AT_declaration);
22014 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
22015 given enum type is incomplete, do not generate the DW_AT_byte_size
22016 attribute or the DW_AT_element_list attribute. */
22017 if (TYPE_SIZE (type))
22019 tree link;
22021 if (!ENUM_IS_OPAQUE (type))
22022 TREE_ASM_WRITTEN (type) = 1;
22023 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
22024 add_byte_size_attribute (type_die, type);
22025 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
22026 add_alignment_attribute (type_die, type);
22027 if ((dwarf_version >= 3 || !dwarf_strict)
22028 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
22030 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
22031 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
22032 context_die);
22034 if (TYPE_STUB_DECL (type) != NULL_TREE)
22036 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
22037 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22038 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
22039 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22042 /* If the first reference to this type was as the return type of an
22043 inline function, then it may not have a parent. Fix this now. */
22044 if (type_die->die_parent == NULL)
22045 add_child_die (scope_die_for (type, context_die), type_die);
22047 for (link = TYPE_VALUES (type);
22048 link != NULL; link = TREE_CHAIN (link))
22050 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
22051 tree value = TREE_VALUE (link);
22053 gcc_assert (!ENUM_IS_OPAQUE (type));
22054 add_name_attribute (enum_die,
22055 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22057 if (TREE_CODE (value) == CONST_DECL)
22058 value = DECL_INITIAL (value);
22060 if (simple_type_size_in_bits (TREE_TYPE (value))
22061 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22063 /* For constant forms created by add_AT_unsigned DWARF
22064 consumers (GDB, elfutils, etc.) always zero extend
22065 the value. Only when the actual value is negative
22066 do we need to use add_AT_int to generate a constant
22067 form that can represent negative values. */
22068 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22069 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22070 add_AT_unsigned (enum_die, DW_AT_const_value,
22071 (unsigned HOST_WIDE_INT) val);
22072 else
22073 add_AT_int (enum_die, DW_AT_const_value, val);
22075 else
22076 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22077 that here. TODO: This should be re-worked to use correct
22078 signed/unsigned double tags for all cases. */
22079 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22082 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22083 if (TYPE_ARTIFICIAL (type)
22084 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22085 add_AT_flag (type_die, DW_AT_artificial, 1);
22087 else
22088 add_AT_flag (type_die, DW_AT_declaration, 1);
22090 add_pubtype (type, type_die);
22092 return type_die;
22095 /* Generate a DIE to represent either a real live formal parameter decl or to
22096 represent just the type of some formal parameter position in some function
22097 type.
22099 Note that this routine is a bit unusual because its argument may be a
22100 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22101 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22102 node. If it's the former then this function is being called to output a
22103 DIE to represent a formal parameter object (or some inlining thereof). If
22104 it's the latter, then this function is only being called to output a
22105 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22106 argument type of some subprogram type.
22107 If EMIT_NAME_P is true, name and source coordinate attributes
22108 are emitted. */
22110 static dw_die_ref
22111 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22112 dw_die_ref context_die)
22114 tree node_or_origin = node ? node : origin;
22115 tree ultimate_origin;
22116 dw_die_ref parm_die = NULL;
22118 if (DECL_P (node_or_origin))
22120 parm_die = lookup_decl_die (node);
22122 /* If the contexts differ, we may not be talking about the same
22123 thing.
22124 ??? When in LTO the DIE parent is the "abstract" copy and the
22125 context_die is the specification "copy". But this whole block
22126 should eventually be no longer needed. */
22127 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
22129 if (!DECL_ABSTRACT_P (node))
22131 /* This can happen when creating an inlined instance, in
22132 which case we need to create a new DIE that will get
22133 annotated with DW_AT_abstract_origin. */
22134 parm_die = NULL;
22136 else
22137 gcc_unreachable ();
22140 if (parm_die && parm_die->die_parent == NULL)
22142 /* Check that parm_die already has the right attributes that
22143 we would have added below. If any attributes are
22144 missing, fall through to add them. */
22145 if (! DECL_ABSTRACT_P (node_or_origin)
22146 && !get_AT (parm_die, DW_AT_location)
22147 && !get_AT (parm_die, DW_AT_const_value))
22148 /* We are missing location info, and are about to add it. */
22150 else
22152 add_child_die (context_die, parm_die);
22153 return parm_die;
22158 /* If we have a previously generated DIE, use it, unless this is an
22159 concrete instance (origin != NULL), in which case we need a new
22160 DIE with a corresponding DW_AT_abstract_origin. */
22161 bool reusing_die;
22162 if (parm_die && origin == NULL)
22163 reusing_die = true;
22164 else
22166 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22167 reusing_die = false;
22170 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22172 case tcc_declaration:
22173 ultimate_origin = decl_ultimate_origin (node_or_origin);
22174 if (node || ultimate_origin)
22175 origin = ultimate_origin;
22177 if (reusing_die)
22178 goto add_location;
22180 if (origin != NULL)
22181 add_abstract_origin_attribute (parm_die, origin);
22182 else if (emit_name_p)
22183 add_name_and_src_coords_attributes (parm_die, node);
22184 if (origin == NULL
22185 || (! DECL_ABSTRACT_P (node_or_origin)
22186 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22187 decl_function_context
22188 (node_or_origin))))
22190 tree type = TREE_TYPE (node_or_origin);
22191 if (decl_by_reference_p (node_or_origin))
22192 add_type_attribute (parm_die, TREE_TYPE (type),
22193 TYPE_UNQUALIFIED,
22194 false, context_die);
22195 else
22196 add_type_attribute (parm_die, type,
22197 decl_quals (node_or_origin),
22198 false, context_die);
22200 if (origin == NULL && DECL_ARTIFICIAL (node))
22201 add_AT_flag (parm_die, DW_AT_artificial, 1);
22202 add_location:
22203 if (node && node != origin)
22204 equate_decl_number_to_die (node, parm_die);
22205 if (! DECL_ABSTRACT_P (node_or_origin))
22206 add_location_or_const_value_attribute (parm_die, node_or_origin,
22207 node == NULL);
22209 break;
22211 case tcc_type:
22212 /* We were called with some kind of a ..._TYPE node. */
22213 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22214 context_die);
22215 break;
22217 default:
22218 gcc_unreachable ();
22221 return parm_die;
22224 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22225 children DW_TAG_formal_parameter DIEs representing the arguments of the
22226 parameter pack.
22228 PARM_PACK must be a function parameter pack.
22229 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22230 must point to the subsequent arguments of the function PACK_ARG belongs to.
22231 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22232 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22233 following the last one for which a DIE was generated. */
22235 static dw_die_ref
22236 gen_formal_parameter_pack_die (tree parm_pack,
22237 tree pack_arg,
22238 dw_die_ref subr_die,
22239 tree *next_arg)
22241 tree arg;
22242 dw_die_ref parm_pack_die;
22244 gcc_assert (parm_pack
22245 && lang_hooks.function_parameter_pack_p (parm_pack)
22246 && subr_die);
22248 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22249 add_src_coords_attributes (parm_pack_die, parm_pack);
22251 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22253 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22254 parm_pack))
22255 break;
22256 gen_formal_parameter_die (arg, NULL,
22257 false /* Don't emit name attribute. */,
22258 parm_pack_die);
22260 if (next_arg)
22261 *next_arg = arg;
22262 return parm_pack_die;
22265 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22266 at the end of an (ANSI prototyped) formal parameters list. */
22268 static void
22269 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22271 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22274 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22275 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22276 parameters as specified in some function type specification (except for
22277 those which appear as part of a function *definition*). */
22279 static void
22280 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22282 tree link;
22283 tree formal_type = NULL;
22284 tree first_parm_type;
22285 tree arg;
22287 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22289 arg = DECL_ARGUMENTS (function_or_method_type);
22290 function_or_method_type = TREE_TYPE (function_or_method_type);
22292 else
22293 arg = NULL_TREE;
22295 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22297 /* Make our first pass over the list of formal parameter types and output a
22298 DW_TAG_formal_parameter DIE for each one. */
22299 for (link = first_parm_type; link; )
22301 dw_die_ref parm_die;
22303 formal_type = TREE_VALUE (link);
22304 if (formal_type == void_type_node)
22305 break;
22307 /* Output a (nameless) DIE to represent the formal parameter itself. */
22308 parm_die = gen_formal_parameter_die (formal_type, NULL,
22309 true /* Emit name attribute. */,
22310 context_die);
22311 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22312 && link == first_parm_type)
22314 add_AT_flag (parm_die, DW_AT_artificial, 1);
22315 if (dwarf_version >= 3 || !dwarf_strict)
22316 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22318 else if (arg && DECL_ARTIFICIAL (arg))
22319 add_AT_flag (parm_die, DW_AT_artificial, 1);
22321 link = TREE_CHAIN (link);
22322 if (arg)
22323 arg = DECL_CHAIN (arg);
22326 /* If this function type has an ellipsis, add a
22327 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22328 if (formal_type != void_type_node)
22329 gen_unspecified_parameters_die (function_or_method_type, context_die);
22331 /* Make our second (and final) pass over the list of formal parameter types
22332 and output DIEs to represent those types (as necessary). */
22333 for (link = TYPE_ARG_TYPES (function_or_method_type);
22334 link && TREE_VALUE (link);
22335 link = TREE_CHAIN (link))
22336 gen_type_die (TREE_VALUE (link), context_die);
22339 /* We want to generate the DIE for TYPE so that we can generate the
22340 die for MEMBER, which has been defined; we will need to refer back
22341 to the member declaration nested within TYPE. If we're trying to
22342 generate minimal debug info for TYPE, processing TYPE won't do the
22343 trick; we need to attach the member declaration by hand. */
22345 static void
22346 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22348 gen_type_die (type, context_die);
22350 /* If we're trying to avoid duplicate debug info, we may not have
22351 emitted the member decl for this function. Emit it now. */
22352 if (TYPE_STUB_DECL (type)
22353 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22354 && ! lookup_decl_die (member))
22356 dw_die_ref type_die;
22357 gcc_assert (!decl_ultimate_origin (member));
22359 type_die = lookup_type_die_strip_naming_typedef (type);
22360 if (TREE_CODE (member) == FUNCTION_DECL)
22361 gen_subprogram_die (member, type_die);
22362 else if (TREE_CODE (member) == FIELD_DECL)
22364 /* Ignore the nameless fields that are used to skip bits but handle
22365 C++ anonymous unions and structs. */
22366 if (DECL_NAME (member) != NULL_TREE
22367 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22368 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22370 struct vlr_context vlr_ctx = {
22371 DECL_CONTEXT (member), /* struct_type */
22372 NULL_TREE /* variant_part_offset */
22374 gen_type_die (member_declared_type (member), type_die);
22375 gen_field_die (member, &vlr_ctx, type_die);
22378 else
22379 gen_variable_die (member, NULL_TREE, type_die);
22383 /* Forward declare these functions, because they are mutually recursive
22384 with their set_block_* pairing functions. */
22385 static void set_decl_origin_self (tree);
22387 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22388 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22389 that it points to the node itself, thus indicating that the node is its
22390 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22391 the given node is NULL, recursively descend the decl/block tree which
22392 it is the root of, and for each other ..._DECL or BLOCK node contained
22393 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22394 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22395 values to point to themselves. */
22397 static void
22398 set_block_origin_self (tree stmt)
22400 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22402 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22405 tree local_decl;
22407 for (local_decl = BLOCK_VARS (stmt);
22408 local_decl != NULL_TREE;
22409 local_decl = DECL_CHAIN (local_decl))
22410 /* Do not recurse on nested functions since the inlining status
22411 of parent and child can be different as per the DWARF spec. */
22412 if (TREE_CODE (local_decl) != FUNCTION_DECL
22413 && !DECL_EXTERNAL (local_decl))
22414 set_decl_origin_self (local_decl);
22418 tree subblock;
22420 for (subblock = BLOCK_SUBBLOCKS (stmt);
22421 subblock != NULL_TREE;
22422 subblock = BLOCK_CHAIN (subblock))
22423 set_block_origin_self (subblock); /* Recurse. */
22428 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22429 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22430 node to so that it points to the node itself, thus indicating that the
22431 node represents its own (abstract) origin. Additionally, if the
22432 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22433 the decl/block tree of which the given node is the root of, and for
22434 each other ..._DECL or BLOCK node contained therein whose
22435 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22436 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22437 point to themselves. */
22439 static void
22440 set_decl_origin_self (tree decl)
22442 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22444 DECL_ABSTRACT_ORIGIN (decl) = decl;
22445 if (TREE_CODE (decl) == FUNCTION_DECL)
22447 tree arg;
22449 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22450 DECL_ABSTRACT_ORIGIN (arg) = arg;
22451 if (DECL_INITIAL (decl) != NULL_TREE
22452 && DECL_INITIAL (decl) != error_mark_node)
22453 set_block_origin_self (DECL_INITIAL (decl));
22458 /* Mark the early DIE for DECL as the abstract instance. */
22460 static void
22461 dwarf2out_abstract_function (tree decl)
22463 dw_die_ref old_die;
22465 /* Make sure we have the actual abstract inline, not a clone. */
22466 decl = DECL_ORIGIN (decl);
22468 if (DECL_IGNORED_P (decl))
22469 return;
22471 /* In LTO we're all set. We already created abstract instances
22472 early and we want to avoid creating a concrete instance of that
22473 if we don't output it. */
22474 if (in_lto_p)
22475 return;
22477 old_die = lookup_decl_die (decl);
22478 gcc_assert (old_die != NULL);
22479 if (get_AT (old_die, DW_AT_inline))
22480 /* We've already generated the abstract instance. */
22481 return;
22483 /* Go ahead and put DW_AT_inline on the DIE. */
22484 if (DECL_DECLARED_INLINE_P (decl))
22486 if (cgraph_function_possibly_inlined_p (decl))
22487 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22488 else
22489 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22491 else
22493 if (cgraph_function_possibly_inlined_p (decl))
22494 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22495 else
22496 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22499 if (DECL_DECLARED_INLINE_P (decl)
22500 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22501 add_AT_flag (old_die, DW_AT_artificial, 1);
22503 set_decl_origin_self (decl);
22506 /* Helper function of premark_used_types() which gets called through
22507 htab_traverse.
22509 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22510 marked as unused by prune_unused_types. */
22512 bool
22513 premark_used_types_helper (tree const &type, void *)
22515 dw_die_ref die;
22517 die = lookup_type_die (type);
22518 if (die != NULL)
22519 die->die_perennial_p = 1;
22520 return true;
22523 /* Helper function of premark_types_used_by_global_vars which gets called
22524 through htab_traverse.
22526 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22527 marked as unused by prune_unused_types. The DIE of the type is marked
22528 only if the global variable using the type will actually be emitted. */
22531 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22532 void *)
22534 struct types_used_by_vars_entry *entry;
22535 dw_die_ref die;
22537 entry = (struct types_used_by_vars_entry *) *slot;
22538 gcc_assert (entry->type != NULL
22539 && entry->var_decl != NULL);
22540 die = lookup_type_die (entry->type);
22541 if (die)
22543 /* Ask cgraph if the global variable really is to be emitted.
22544 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22545 varpool_node *node = varpool_node::get (entry->var_decl);
22546 if (node && node->definition)
22548 die->die_perennial_p = 1;
22549 /* Keep the parent DIEs as well. */
22550 while ((die = die->die_parent) && die->die_perennial_p == 0)
22551 die->die_perennial_p = 1;
22554 return 1;
22557 /* Mark all members of used_types_hash as perennial. */
22559 static void
22560 premark_used_types (struct function *fun)
22562 if (fun && fun->used_types_hash)
22563 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22566 /* Mark all members of types_used_by_vars_entry as perennial. */
22568 static void
22569 premark_types_used_by_global_vars (void)
22571 if (types_used_by_vars_hash)
22572 types_used_by_vars_hash
22573 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22576 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22577 for CA_LOC call arg loc node. */
22579 static dw_die_ref
22580 gen_call_site_die (tree decl, dw_die_ref subr_die,
22581 struct call_arg_loc_node *ca_loc)
22583 dw_die_ref stmt_die = NULL, die;
22584 tree block = ca_loc->block;
22586 while (block
22587 && block != DECL_INITIAL (decl)
22588 && TREE_CODE (block) == BLOCK)
22590 stmt_die = lookup_block_die (block);
22591 if (stmt_die)
22592 break;
22593 block = BLOCK_SUPERCONTEXT (block);
22595 if (stmt_die == NULL)
22596 stmt_die = subr_die;
22597 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22598 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22599 if (ca_loc->tail_call_p)
22600 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22601 if (ca_loc->symbol_ref)
22603 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22604 if (tdie)
22605 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22606 else
22607 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22608 false);
22610 return die;
22613 /* Generate a DIE to represent a declared function (either file-scope or
22614 block-local). */
22616 static void
22617 gen_subprogram_die (tree decl, dw_die_ref context_die)
22619 tree origin = decl_ultimate_origin (decl);
22620 dw_die_ref subr_die;
22621 dw_die_ref old_die = lookup_decl_die (decl);
22623 /* This function gets called multiple times for different stages of
22624 the debug process. For example, for func() in this code:
22626 namespace S
22628 void func() { ... }
22631 ...we get called 4 times. Twice in early debug and twice in
22632 late debug:
22634 Early debug
22635 -----------
22637 1. Once while generating func() within the namespace. This is
22638 the declaration. The declaration bit below is set, as the
22639 context is the namespace.
22641 A new DIE will be generated with DW_AT_declaration set.
22643 2. Once for func() itself. This is the specification. The
22644 declaration bit below is clear as the context is the CU.
22646 We will use the cached DIE from (1) to create a new DIE with
22647 DW_AT_specification pointing to the declaration in (1).
22649 Late debug via rest_of_handle_final()
22650 -------------------------------------
22652 3. Once generating func() within the namespace. This is also the
22653 declaration, as in (1), but this time we will early exit below
22654 as we have a cached DIE and a declaration needs no additional
22655 annotations (no locations), as the source declaration line
22656 info is enough.
22658 4. Once for func() itself. As in (2), this is the specification,
22659 but this time we will re-use the cached DIE, and just annotate
22660 it with the location information that should now be available.
22662 For something without namespaces, but with abstract instances, we
22663 are also called a multiple times:
22665 class Base
22667 public:
22668 Base (); // constructor declaration (1)
22671 Base::Base () { } // constructor specification (2)
22673 Early debug
22674 -----------
22676 1. Once for the Base() constructor by virtue of it being a
22677 member of the Base class. This is done via
22678 rest_of_type_compilation.
22680 This is a declaration, so a new DIE will be created with
22681 DW_AT_declaration.
22683 2. Once for the Base() constructor definition, but this time
22684 while generating the abstract instance of the base
22685 constructor (__base_ctor) which is being generated via early
22686 debug of reachable functions.
22688 Even though we have a cached version of the declaration (1),
22689 we will create a DW_AT_specification of the declaration DIE
22690 in (1).
22692 3. Once for the __base_ctor itself, but this time, we generate
22693 an DW_AT_abstract_origin version of the DW_AT_specification in
22694 (2).
22696 Late debug via rest_of_handle_final
22697 -----------------------------------
22699 4. One final time for the __base_ctor (which will have a cached
22700 DIE with DW_AT_abstract_origin created in (3). This time,
22701 we will just annotate the location information now
22702 available.
22704 int declaration = (current_function_decl != decl
22705 || class_or_namespace_scope_p (context_die));
22707 /* A declaration that has been previously dumped needs no
22708 additional information. */
22709 if (old_die && declaration)
22710 return;
22712 /* Now that the C++ front end lazily declares artificial member fns, we
22713 might need to retrofit the declaration into its class. */
22714 if (!declaration && !origin && !old_die
22715 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22716 && !class_or_namespace_scope_p (context_die)
22717 && debug_info_level > DINFO_LEVEL_TERSE)
22718 old_die = force_decl_die (decl);
22720 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22721 if (origin != NULL)
22723 gcc_assert (!declaration || local_scope_p (context_die));
22725 /* Fixup die_parent for the abstract instance of a nested
22726 inline function. */
22727 if (old_die && old_die->die_parent == NULL)
22728 add_child_die (context_die, old_die);
22730 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22732 /* If we have a DW_AT_abstract_origin we have a working
22733 cached version. */
22734 subr_die = old_die;
22736 else
22738 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22739 add_abstract_origin_attribute (subr_die, origin);
22740 /* This is where the actual code for a cloned function is.
22741 Let's emit linkage name attribute for it. This helps
22742 debuggers to e.g, set breakpoints into
22743 constructors/destructors when the user asks "break
22744 K::K". */
22745 add_linkage_name (subr_die, decl);
22748 /* A cached copy, possibly from early dwarf generation. Reuse as
22749 much as possible. */
22750 else if (old_die)
22752 if (!get_AT_flag (old_die, DW_AT_declaration)
22753 /* We can have a normal definition following an inline one in the
22754 case of redefinition of GNU C extern inlines.
22755 It seems reasonable to use AT_specification in this case. */
22756 && !get_AT (old_die, DW_AT_inline))
22758 /* Detect and ignore this case, where we are trying to output
22759 something we have already output. */
22760 if (get_AT (old_die, DW_AT_low_pc)
22761 || get_AT (old_die, DW_AT_ranges))
22762 return;
22764 /* If we have no location information, this must be a
22765 partially generated DIE from early dwarf generation.
22766 Fall through and generate it. */
22769 /* If the definition comes from the same place as the declaration,
22770 maybe use the old DIE. We always want the DIE for this function
22771 that has the *_pc attributes to be under comp_unit_die so the
22772 debugger can find it. We also need to do this for abstract
22773 instances of inlines, since the spec requires the out-of-line copy
22774 to have the same parent. For local class methods, this doesn't
22775 apply; we just use the old DIE. */
22776 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22777 struct dwarf_file_data * file_index = lookup_filename (s.file);
22778 if (((is_unit_die (old_die->die_parent)
22779 /* This condition fixes the inconsistency/ICE with the
22780 following Fortran test (or some derivative thereof) while
22781 building libgfortran:
22783 module some_m
22784 contains
22785 logical function funky (FLAG)
22786 funky = .true.
22787 end function
22788 end module
22790 || (old_die->die_parent
22791 && old_die->die_parent->die_tag == DW_TAG_module)
22792 || local_scope_p (old_die->die_parent)
22793 || context_die == NULL)
22794 && (DECL_ARTIFICIAL (decl)
22795 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22796 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22797 == (unsigned) s.line)
22798 && (!debug_column_info
22799 || s.column == 0
22800 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22801 == (unsigned) s.column)))))
22802 /* With LTO if there's an abstract instance for
22803 the old DIE, this is a concrete instance and
22804 thus re-use the DIE. */
22805 || get_AT (old_die, DW_AT_abstract_origin))
22807 subr_die = old_die;
22809 /* Clear out the declaration attribute, but leave the
22810 parameters so they can be augmented with location
22811 information later. Unless this was a declaration, in
22812 which case, wipe out the nameless parameters and recreate
22813 them further down. */
22814 if (remove_AT (subr_die, DW_AT_declaration))
22817 remove_AT (subr_die, DW_AT_object_pointer);
22818 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22821 /* Make a specification pointing to the previously built
22822 declaration. */
22823 else
22825 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22826 add_AT_specification (subr_die, old_die);
22827 add_pubname (decl, subr_die);
22828 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22829 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22830 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22831 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22832 if (debug_column_info
22833 && s.column
22834 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22835 != (unsigned) s.column))
22836 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22838 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22839 emit the real type on the definition die. */
22840 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22842 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22843 if (die == auto_die || die == decltype_auto_die)
22844 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22845 TYPE_UNQUALIFIED, false, context_die);
22848 /* When we process the method declaration, we haven't seen
22849 the out-of-class defaulted definition yet, so we have to
22850 recheck now. */
22851 if ((dwarf_version >= 5 || ! dwarf_strict)
22852 && !get_AT (subr_die, DW_AT_defaulted))
22854 int defaulted
22855 = lang_hooks.decls.decl_dwarf_attribute (decl,
22856 DW_AT_defaulted);
22857 if (defaulted != -1)
22859 /* Other values must have been handled before. */
22860 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22861 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22866 /* Create a fresh DIE for anything else. */
22867 else
22869 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22871 if (TREE_PUBLIC (decl))
22872 add_AT_flag (subr_die, DW_AT_external, 1);
22874 add_name_and_src_coords_attributes (subr_die, decl);
22875 add_pubname (decl, subr_die);
22876 if (debug_info_level > DINFO_LEVEL_TERSE)
22878 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22879 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22880 TYPE_UNQUALIFIED, false, context_die);
22883 add_pure_or_virtual_attribute (subr_die, decl);
22884 if (DECL_ARTIFICIAL (decl))
22885 add_AT_flag (subr_die, DW_AT_artificial, 1);
22887 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22888 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22890 add_alignment_attribute (subr_die, decl);
22892 add_accessibility_attribute (subr_die, decl);
22895 /* Unless we have an existing non-declaration DIE, equate the new
22896 DIE. */
22897 if (!old_die || is_declaration_die (old_die))
22898 equate_decl_number_to_die (decl, subr_die);
22900 if (declaration)
22902 if (!old_die || !get_AT (old_die, DW_AT_inline))
22904 add_AT_flag (subr_die, DW_AT_declaration, 1);
22906 /* If this is an explicit function declaration then generate
22907 a DW_AT_explicit attribute. */
22908 if ((dwarf_version >= 3 || !dwarf_strict)
22909 && lang_hooks.decls.decl_dwarf_attribute (decl,
22910 DW_AT_explicit) == 1)
22911 add_AT_flag (subr_die, DW_AT_explicit, 1);
22913 /* If this is a C++11 deleted special function member then generate
22914 a DW_AT_deleted attribute. */
22915 if ((dwarf_version >= 5 || !dwarf_strict)
22916 && lang_hooks.decls.decl_dwarf_attribute (decl,
22917 DW_AT_deleted) == 1)
22918 add_AT_flag (subr_die, DW_AT_deleted, 1);
22920 /* If this is a C++11 defaulted special function member then
22921 generate a DW_AT_defaulted attribute. */
22922 if (dwarf_version >= 5 || !dwarf_strict)
22924 int defaulted
22925 = lang_hooks.decls.decl_dwarf_attribute (decl,
22926 DW_AT_defaulted);
22927 if (defaulted != -1)
22928 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22931 /* If this is a C++11 non-static member function with & ref-qualifier
22932 then generate a DW_AT_reference attribute. */
22933 if ((dwarf_version >= 5 || !dwarf_strict)
22934 && lang_hooks.decls.decl_dwarf_attribute (decl,
22935 DW_AT_reference) == 1)
22936 add_AT_flag (subr_die, DW_AT_reference, 1);
22938 /* If this is a C++11 non-static member function with &&
22939 ref-qualifier then generate a DW_AT_reference attribute. */
22940 if ((dwarf_version >= 5 || !dwarf_strict)
22941 && lang_hooks.decls.decl_dwarf_attribute (decl,
22942 DW_AT_rvalue_reference)
22943 == 1)
22944 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22947 /* For non DECL_EXTERNALs, if range information is available, fill
22948 the DIE with it. */
22949 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22951 HOST_WIDE_INT cfa_fb_offset;
22953 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22955 if (!crtl->has_bb_partition)
22957 dw_fde_ref fde = fun->fde;
22958 if (fde->dw_fde_begin)
22960 /* We have already generated the labels. */
22961 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22962 fde->dw_fde_end, false);
22964 else
22966 /* Create start/end labels and add the range. */
22967 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22968 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22969 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22970 current_function_funcdef_no);
22971 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22972 current_function_funcdef_no);
22973 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22974 false);
22977 #if VMS_DEBUGGING_INFO
22978 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22979 Section 2.3 Prologue and Epilogue Attributes:
22980 When a breakpoint is set on entry to a function, it is generally
22981 desirable for execution to be suspended, not on the very first
22982 instruction of the function, but rather at a point after the
22983 function's frame has been set up, after any language defined local
22984 declaration processing has been completed, and before execution of
22985 the first statement of the function begins. Debuggers generally
22986 cannot properly determine where this point is. Similarly for a
22987 breakpoint set on exit from a function. The prologue and epilogue
22988 attributes allow a compiler to communicate the location(s) to use. */
22991 if (fde->dw_fde_vms_end_prologue)
22992 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22993 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22995 if (fde->dw_fde_vms_begin_epilogue)
22996 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22997 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22999 #endif
23002 else
23004 /* Generate pubnames entries for the split function code ranges. */
23005 dw_fde_ref fde = fun->fde;
23007 if (fde->dw_fde_second_begin)
23009 if (dwarf_version >= 3 || !dwarf_strict)
23011 /* We should use ranges for non-contiguous code section
23012 addresses. Use the actual code range for the initial
23013 section, since the HOT/COLD labels might precede an
23014 alignment offset. */
23015 bool range_list_added = false;
23016 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
23017 fde->dw_fde_end, &range_list_added,
23018 false);
23019 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
23020 fde->dw_fde_second_end,
23021 &range_list_added, false);
23022 if (range_list_added)
23023 add_ranges (NULL);
23025 else
23027 /* There is no real support in DW2 for this .. so we make
23028 a work-around. First, emit the pub name for the segment
23029 containing the function label. Then make and emit a
23030 simplified subprogram DIE for the second segment with the
23031 name pre-fixed by __hot/cold_sect_of_. We use the same
23032 linkage name for the second die so that gdb will find both
23033 sections when given "b foo". */
23034 const char *name = NULL;
23035 tree decl_name = DECL_NAME (decl);
23036 dw_die_ref seg_die;
23038 /* Do the 'primary' section. */
23039 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
23040 fde->dw_fde_end, false);
23042 /* Build a minimal DIE for the secondary section. */
23043 seg_die = new_die (DW_TAG_subprogram,
23044 subr_die->die_parent, decl);
23046 if (TREE_PUBLIC (decl))
23047 add_AT_flag (seg_die, DW_AT_external, 1);
23049 if (decl_name != NULL
23050 && IDENTIFIER_POINTER (decl_name) != NULL)
23052 name = dwarf2_name (decl, 1);
23053 if (! DECL_ARTIFICIAL (decl))
23054 add_src_coords_attributes (seg_die, decl);
23056 add_linkage_name (seg_die, decl);
23058 gcc_assert (name != NULL);
23059 add_pure_or_virtual_attribute (seg_die, decl);
23060 if (DECL_ARTIFICIAL (decl))
23061 add_AT_flag (seg_die, DW_AT_artificial, 1);
23063 name = concat ("__second_sect_of_", name, NULL);
23064 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23065 fde->dw_fde_second_end, false);
23066 add_name_attribute (seg_die, name);
23067 if (want_pubnames ())
23068 add_pubname_string (name, seg_die);
23071 else
23072 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23073 false);
23076 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23078 /* We define the "frame base" as the function's CFA. This is more
23079 convenient for several reasons: (1) It's stable across the prologue
23080 and epilogue, which makes it better than just a frame pointer,
23081 (2) With dwarf3, there exists a one-byte encoding that allows us
23082 to reference the .debug_frame data by proxy, but failing that,
23083 (3) We can at least reuse the code inspection and interpretation
23084 code that determines the CFA position at various points in the
23085 function. */
23086 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23088 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23089 add_AT_loc (subr_die, DW_AT_frame_base, op);
23091 else
23093 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23094 if (list->dw_loc_next)
23095 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23096 else
23097 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23100 /* Compute a displacement from the "steady-state frame pointer" to
23101 the CFA. The former is what all stack slots and argument slots
23102 will reference in the rtl; the latter is what we've told the
23103 debugger about. We'll need to adjust all frame_base references
23104 by this displacement. */
23105 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23107 if (fun->static_chain_decl)
23109 /* DWARF requires here a location expression that computes the
23110 address of the enclosing subprogram's frame base. The machinery
23111 in tree-nested.c is supposed to store this specific address in the
23112 last field of the FRAME record. */
23113 const tree frame_type
23114 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23115 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23117 tree fb_expr
23118 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23119 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23120 fb_expr, fb_decl, NULL_TREE);
23122 add_AT_location_description (subr_die, DW_AT_static_link,
23123 loc_list_from_tree (fb_expr, 0, NULL));
23126 resolve_variable_values ();
23129 /* Generate child dies for template paramaters. */
23130 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23131 gen_generic_params_dies (decl);
23133 /* Now output descriptions of the arguments for this function. This gets
23134 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23135 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23136 `...' at the end of the formal parameter list. In order to find out if
23137 there was a trailing ellipsis or not, we must instead look at the type
23138 associated with the FUNCTION_DECL. This will be a node of type
23139 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23140 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23141 an ellipsis at the end. */
23143 /* In the case where we are describing a mere function declaration, all we
23144 need to do here (and all we *can* do here) is to describe the *types* of
23145 its formal parameters. */
23146 if (debug_info_level <= DINFO_LEVEL_TERSE)
23148 else if (declaration)
23149 gen_formal_types_die (decl, subr_die);
23150 else
23152 /* Generate DIEs to represent all known formal parameters. */
23153 tree parm = DECL_ARGUMENTS (decl);
23154 tree generic_decl = early_dwarf
23155 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23156 tree generic_decl_parm = generic_decl
23157 ? DECL_ARGUMENTS (generic_decl)
23158 : NULL;
23160 /* Now we want to walk the list of parameters of the function and
23161 emit their relevant DIEs.
23163 We consider the case of DECL being an instance of a generic function
23164 as well as it being a normal function.
23166 If DECL is an instance of a generic function we walk the
23167 parameters of the generic function declaration _and_ the parameters of
23168 DECL itself. This is useful because we want to emit specific DIEs for
23169 function parameter packs and those are declared as part of the
23170 generic function declaration. In that particular case,
23171 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23172 That DIE has children DIEs representing the set of arguments
23173 of the pack. Note that the set of pack arguments can be empty.
23174 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23175 children DIE.
23177 Otherwise, we just consider the parameters of DECL. */
23178 while (generic_decl_parm || parm)
23180 if (generic_decl_parm
23181 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23182 gen_formal_parameter_pack_die (generic_decl_parm,
23183 parm, subr_die,
23184 &parm);
23185 else if (parm)
23187 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23189 if (early_dwarf
23190 && parm == DECL_ARGUMENTS (decl)
23191 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23192 && parm_die
23193 && (dwarf_version >= 3 || !dwarf_strict))
23194 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23196 parm = DECL_CHAIN (parm);
23198 else if (parm)
23199 parm = DECL_CHAIN (parm);
23201 if (generic_decl_parm)
23202 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23205 /* Decide whether we need an unspecified_parameters DIE at the end.
23206 There are 2 more cases to do this for: 1) the ansi ... declaration -
23207 this is detectable when the end of the arg list is not a
23208 void_type_node 2) an unprototyped function declaration (not a
23209 definition). This just means that we have no info about the
23210 parameters at all. */
23211 if (early_dwarf)
23213 if (prototype_p (TREE_TYPE (decl)))
23215 /* This is the prototyped case, check for.... */
23216 if (stdarg_p (TREE_TYPE (decl)))
23217 gen_unspecified_parameters_die (decl, subr_die);
23219 else if (DECL_INITIAL (decl) == NULL_TREE)
23220 gen_unspecified_parameters_die (decl, subr_die);
23224 if (subr_die != old_die)
23225 /* Add the calling convention attribute if requested. */
23226 add_calling_convention_attribute (subr_die, decl);
23228 /* Output Dwarf info for all of the stuff within the body of the function
23229 (if it has one - it may be just a declaration).
23231 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23232 a function. This BLOCK actually represents the outermost binding contour
23233 for the function, i.e. the contour in which the function's formal
23234 parameters and labels get declared. Curiously, it appears that the front
23235 end doesn't actually put the PARM_DECL nodes for the current function onto
23236 the BLOCK_VARS list for this outer scope, but are strung off of the
23237 DECL_ARGUMENTS list for the function instead.
23239 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23240 the LABEL_DECL nodes for the function however, and we output DWARF info
23241 for those in decls_for_scope. Just within the `outer_scope' there will be
23242 a BLOCK node representing the function's outermost pair of curly braces,
23243 and any blocks used for the base and member initializers of a C++
23244 constructor function. */
23245 tree outer_scope = DECL_INITIAL (decl);
23246 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23248 int call_site_note_count = 0;
23249 int tail_call_site_note_count = 0;
23251 /* Emit a DW_TAG_variable DIE for a named return value. */
23252 if (DECL_NAME (DECL_RESULT (decl)))
23253 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23255 /* The first time through decls_for_scope we will generate the
23256 DIEs for the locals. The second time, we fill in the
23257 location info. */
23258 decls_for_scope (outer_scope, subr_die);
23260 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23262 struct call_arg_loc_node *ca_loc;
23263 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23265 dw_die_ref die = NULL;
23266 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23267 rtx arg, next_arg;
23268 tree arg_decl = NULL_TREE;
23270 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23271 ? XEXP (ca_loc->call_arg_loc_note, 0)
23272 : NULL_RTX);
23273 arg; arg = next_arg)
23275 dw_loc_descr_ref reg, val;
23276 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23277 dw_die_ref cdie, tdie = NULL;
23279 next_arg = XEXP (arg, 1);
23280 if (REG_P (XEXP (XEXP (arg, 0), 0))
23281 && next_arg
23282 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23283 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23284 && REGNO (XEXP (XEXP (arg, 0), 0))
23285 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23286 next_arg = XEXP (next_arg, 1);
23287 if (mode == VOIDmode)
23289 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23290 if (mode == VOIDmode)
23291 mode = GET_MODE (XEXP (arg, 0));
23293 if (mode == VOIDmode || mode == BLKmode)
23294 continue;
23295 /* Get dynamic information about call target only if we
23296 have no static information: we cannot generate both
23297 DW_AT_call_origin and DW_AT_call_target
23298 attributes. */
23299 if (ca_loc->symbol_ref == NULL_RTX)
23301 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23303 tloc = XEXP (XEXP (arg, 0), 1);
23304 continue;
23306 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23307 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23309 tlocc = XEXP (XEXP (arg, 0), 1);
23310 continue;
23313 reg = NULL;
23314 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23315 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23316 VAR_INIT_STATUS_INITIALIZED);
23317 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23319 rtx mem = XEXP (XEXP (arg, 0), 0);
23320 reg = mem_loc_descriptor (XEXP (mem, 0),
23321 get_address_mode (mem),
23322 GET_MODE (mem),
23323 VAR_INIT_STATUS_INITIALIZED);
23325 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23326 == DEBUG_PARAMETER_REF)
23328 tree tdecl
23329 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23330 tdie = lookup_decl_die (tdecl);
23331 if (tdie == NULL)
23332 continue;
23333 arg_decl = tdecl;
23335 else
23336 continue;
23337 if (reg == NULL
23338 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23339 != DEBUG_PARAMETER_REF)
23340 continue;
23341 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23342 VOIDmode,
23343 VAR_INIT_STATUS_INITIALIZED);
23344 if (val == NULL)
23345 continue;
23346 if (die == NULL)
23347 die = gen_call_site_die (decl, subr_die, ca_loc);
23348 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23349 NULL_TREE);
23350 add_desc_attribute (cdie, arg_decl);
23351 if (reg != NULL)
23352 add_AT_loc (cdie, DW_AT_location, reg);
23353 else if (tdie != NULL)
23354 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23355 tdie);
23356 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23357 if (next_arg != XEXP (arg, 1))
23359 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23360 if (mode == VOIDmode)
23361 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23362 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23363 0), 1),
23364 mode, VOIDmode,
23365 VAR_INIT_STATUS_INITIALIZED);
23366 if (val != NULL)
23367 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23368 val);
23371 if (die == NULL
23372 && (ca_loc->symbol_ref || tloc))
23373 die = gen_call_site_die (decl, subr_die, ca_loc);
23374 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23376 dw_loc_descr_ref tval = NULL;
23378 if (tloc != NULL_RTX)
23379 tval = mem_loc_descriptor (tloc,
23380 GET_MODE (tloc) == VOIDmode
23381 ? Pmode : GET_MODE (tloc),
23382 VOIDmode,
23383 VAR_INIT_STATUS_INITIALIZED);
23384 if (tval)
23385 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23386 else if (tlocc != NULL_RTX)
23388 tval = mem_loc_descriptor (tlocc,
23389 GET_MODE (tlocc) == VOIDmode
23390 ? Pmode : GET_MODE (tlocc),
23391 VOIDmode,
23392 VAR_INIT_STATUS_INITIALIZED);
23393 if (tval)
23394 add_AT_loc (die,
23395 dwarf_AT (DW_AT_call_target_clobbered),
23396 tval);
23399 if (die != NULL)
23401 call_site_note_count++;
23402 if (ca_loc->tail_call_p)
23403 tail_call_site_note_count++;
23407 call_arg_locations = NULL;
23408 call_arg_loc_last = NULL;
23409 if (tail_call_site_count >= 0
23410 && tail_call_site_count == tail_call_site_note_count
23411 && (!dwarf_strict || dwarf_version >= 5))
23413 if (call_site_count >= 0
23414 && call_site_count == call_site_note_count)
23415 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23416 else
23417 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23419 call_site_count = -1;
23420 tail_call_site_count = -1;
23423 /* Mark used types after we have created DIEs for the functions scopes. */
23424 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23427 /* Returns a hash value for X (which really is a die_struct). */
23429 hashval_t
23430 block_die_hasher::hash (die_struct *d)
23432 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23435 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23436 as decl_id and die_parent of die_struct Y. */
23438 bool
23439 block_die_hasher::equal (die_struct *x, die_struct *y)
23441 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23444 /* Hold information about markers for inlined entry points. */
23445 struct GTY ((for_user)) inline_entry_data
23447 /* The block that's the inlined_function_outer_scope for an inlined
23448 function. */
23449 tree block;
23451 /* The label at the inlined entry point. */
23452 const char *label_pfx;
23453 unsigned int label_num;
23455 /* The view number to be used as the inlined entry point. */
23456 var_loc_view view;
23459 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23461 typedef tree compare_type;
23462 static inline hashval_t hash (const inline_entry_data *);
23463 static inline bool equal (const inline_entry_data *, const_tree);
23466 /* Hash table routines for inline_entry_data. */
23468 inline hashval_t
23469 inline_entry_data_hasher::hash (const inline_entry_data *data)
23471 return htab_hash_pointer (data->block);
23474 inline bool
23475 inline_entry_data_hasher::equal (const inline_entry_data *data,
23476 const_tree block)
23478 return data->block == block;
23481 /* Inlined entry points pending DIE creation in this compilation unit. */
23483 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23486 /* Return TRUE if DECL, which may have been previously generated as
23487 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23488 true if decl (or its origin) is either an extern declaration or a
23489 class/namespace scoped declaration.
23491 The declare_in_namespace support causes us to get two DIEs for one
23492 variable, both of which are declarations. We want to avoid
23493 considering one to be a specification, so we must test for
23494 DECLARATION and DW_AT_declaration. */
23495 static inline bool
23496 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23498 return (old_die && TREE_STATIC (decl) && !declaration
23499 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23502 /* Return true if DECL is a local static. */
23504 static inline bool
23505 local_function_static (tree decl)
23507 gcc_assert (VAR_P (decl));
23508 return TREE_STATIC (decl)
23509 && DECL_CONTEXT (decl)
23510 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23513 /* Generate a DIE to represent a declared data object.
23514 Either DECL or ORIGIN must be non-null. */
23516 static void
23517 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23519 HOST_WIDE_INT off = 0;
23520 tree com_decl;
23521 tree decl_or_origin = decl ? decl : origin;
23522 tree ultimate_origin;
23523 dw_die_ref var_die;
23524 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23525 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23526 || class_or_namespace_scope_p (context_die));
23527 bool specialization_p = false;
23528 bool no_linkage_name = false;
23530 /* While C++ inline static data members have definitions inside of the
23531 class, force the first DIE to be a declaration, then let gen_member_die
23532 reparent it to the class context and call gen_variable_die again
23533 to create the outside of the class DIE for the definition. */
23534 if (!declaration
23535 && old_die == NULL
23536 && decl
23537 && DECL_CONTEXT (decl)
23538 && TYPE_P (DECL_CONTEXT (decl))
23539 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23541 declaration = true;
23542 if (dwarf_version < 5)
23543 no_linkage_name = true;
23546 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23547 if (decl || ultimate_origin)
23548 origin = ultimate_origin;
23549 com_decl = fortran_common (decl_or_origin, &off);
23551 /* Symbol in common gets emitted as a child of the common block, in the form
23552 of a data member. */
23553 if (com_decl)
23555 dw_die_ref com_die;
23556 dw_loc_list_ref loc = NULL;
23557 die_node com_die_arg;
23559 var_die = lookup_decl_die (decl_or_origin);
23560 if (var_die)
23562 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23564 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23565 if (loc)
23567 if (off)
23569 /* Optimize the common case. */
23570 if (single_element_loc_list_p (loc)
23571 && loc->expr->dw_loc_opc == DW_OP_addr
23572 && loc->expr->dw_loc_next == NULL
23573 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23574 == SYMBOL_REF)
23576 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23577 loc->expr->dw_loc_oprnd1.v.val_addr
23578 = plus_constant (GET_MODE (x), x , off);
23580 else
23581 loc_list_plus_const (loc, off);
23583 add_AT_location_description (var_die, DW_AT_location, loc);
23584 remove_AT (var_die, DW_AT_declaration);
23587 return;
23590 if (common_block_die_table == NULL)
23591 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23593 com_die_arg.decl_id = DECL_UID (com_decl);
23594 com_die_arg.die_parent = context_die;
23595 com_die = common_block_die_table->find (&com_die_arg);
23596 if (! early_dwarf)
23597 loc = loc_list_from_tree (com_decl, 2, NULL);
23598 if (com_die == NULL)
23600 const char *cnam
23601 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23602 die_node **slot;
23604 com_die = new_die (DW_TAG_common_block, context_die, decl);
23605 add_name_and_src_coords_attributes (com_die, com_decl);
23606 if (loc)
23608 add_AT_location_description (com_die, DW_AT_location, loc);
23609 /* Avoid sharing the same loc descriptor between
23610 DW_TAG_common_block and DW_TAG_variable. */
23611 loc = loc_list_from_tree (com_decl, 2, NULL);
23613 else if (DECL_EXTERNAL (decl_or_origin))
23614 add_AT_flag (com_die, DW_AT_declaration, 1);
23615 if (want_pubnames ())
23616 add_pubname_string (cnam, com_die); /* ??? needed? */
23617 com_die->decl_id = DECL_UID (com_decl);
23618 slot = common_block_die_table->find_slot (com_die, INSERT);
23619 *slot = com_die;
23621 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23623 add_AT_location_description (com_die, DW_AT_location, loc);
23624 loc = loc_list_from_tree (com_decl, 2, NULL);
23625 remove_AT (com_die, DW_AT_declaration);
23627 var_die = new_die (DW_TAG_variable, com_die, decl);
23628 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23629 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23630 decl_quals (decl_or_origin), false,
23631 context_die);
23632 add_alignment_attribute (var_die, decl);
23633 add_AT_flag (var_die, DW_AT_external, 1);
23634 if (loc)
23636 if (off)
23638 /* Optimize the common case. */
23639 if (single_element_loc_list_p (loc)
23640 && loc->expr->dw_loc_opc == DW_OP_addr
23641 && loc->expr->dw_loc_next == NULL
23642 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23644 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23645 loc->expr->dw_loc_oprnd1.v.val_addr
23646 = plus_constant (GET_MODE (x), x, off);
23648 else
23649 loc_list_plus_const (loc, off);
23651 add_AT_location_description (var_die, DW_AT_location, loc);
23653 else if (DECL_EXTERNAL (decl_or_origin))
23654 add_AT_flag (var_die, DW_AT_declaration, 1);
23655 if (decl)
23656 equate_decl_number_to_die (decl, var_die);
23657 return;
23660 if (old_die)
23662 if (declaration)
23664 /* A declaration that has been previously dumped, needs no
23665 further annotations, since it doesn't need location on
23666 the second pass. */
23667 return;
23669 else if (decl_will_get_specification_p (old_die, decl, declaration)
23670 && !get_AT (old_die, DW_AT_specification))
23672 /* Fall-thru so we can make a new variable die along with a
23673 DW_AT_specification. */
23675 else if (origin && old_die->die_parent != context_die)
23677 /* If we will be creating an inlined instance, we need a
23678 new DIE that will get annotated with
23679 DW_AT_abstract_origin. */
23680 gcc_assert (!DECL_ABSTRACT_P (decl));
23682 else
23684 /* If a DIE was dumped early, it still needs location info.
23685 Skip to where we fill the location bits. */
23686 var_die = old_die;
23688 /* ??? In LTRANS we cannot annotate early created variably
23689 modified type DIEs without copying them and adjusting all
23690 references to them. Thus we dumped them again. Also add a
23691 reference to them but beware of -g0 compile and -g link
23692 in which case the reference will be already present. */
23693 tree type = TREE_TYPE (decl_or_origin);
23694 if (in_lto_p
23695 && ! get_AT (var_die, DW_AT_type)
23696 && variably_modified_type_p
23697 (type, decl_function_context (decl_or_origin)))
23699 if (decl_by_reference_p (decl_or_origin))
23700 add_type_attribute (var_die, TREE_TYPE (type),
23701 TYPE_UNQUALIFIED, false, context_die);
23702 else
23703 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23704 false, context_die);
23707 goto gen_variable_die_location;
23711 /* For static data members, the declaration in the class is supposed
23712 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23713 also in DWARF2; the specification should still be DW_TAG_variable
23714 referencing the DW_TAG_member DIE. */
23715 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23716 var_die = new_die (DW_TAG_member, context_die, decl);
23717 else
23718 var_die = new_die (DW_TAG_variable, context_die, decl);
23720 if (origin != NULL)
23721 add_abstract_origin_attribute (var_die, origin);
23723 /* Loop unrolling can create multiple blocks that refer to the same
23724 static variable, so we must test for the DW_AT_declaration flag.
23726 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23727 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23728 sharing them.
23730 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23731 else if (decl_will_get_specification_p (old_die, decl, declaration))
23733 /* This is a definition of a C++ class level static. */
23734 add_AT_specification (var_die, old_die);
23735 specialization_p = true;
23736 if (DECL_NAME (decl))
23738 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23739 struct dwarf_file_data * file_index = lookup_filename (s.file);
23741 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23742 add_AT_file (var_die, DW_AT_decl_file, file_index);
23744 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23745 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23747 if (debug_column_info
23748 && s.column
23749 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23750 != (unsigned) s.column))
23751 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23753 if (old_die->die_tag == DW_TAG_member)
23754 add_linkage_name (var_die, decl);
23757 else
23758 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23760 if ((origin == NULL && !specialization_p)
23761 || (origin != NULL
23762 && !DECL_ABSTRACT_P (decl_or_origin)
23763 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23764 decl_function_context
23765 (decl_or_origin))))
23767 tree type = TREE_TYPE (decl_or_origin);
23769 if (decl_by_reference_p (decl_or_origin))
23770 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23771 context_die);
23772 else
23773 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23774 context_die);
23777 if (origin == NULL && !specialization_p)
23779 if (TREE_PUBLIC (decl))
23780 add_AT_flag (var_die, DW_AT_external, 1);
23782 if (DECL_ARTIFICIAL (decl))
23783 add_AT_flag (var_die, DW_AT_artificial, 1);
23785 add_alignment_attribute (var_die, decl);
23787 add_accessibility_attribute (var_die, decl);
23790 if (declaration)
23791 add_AT_flag (var_die, DW_AT_declaration, 1);
23793 if (decl && (DECL_ABSTRACT_P (decl)
23794 || !old_die || is_declaration_die (old_die)))
23795 equate_decl_number_to_die (decl, var_die);
23797 gen_variable_die_location:
23798 if (! declaration
23799 && (! DECL_ABSTRACT_P (decl_or_origin)
23800 /* Local static vars are shared between all clones/inlines,
23801 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23802 already set. */
23803 || (VAR_P (decl_or_origin)
23804 && TREE_STATIC (decl_or_origin)
23805 && DECL_RTL_SET_P (decl_or_origin))))
23807 if (early_dwarf)
23808 add_pubname (decl_or_origin, var_die);
23809 else
23810 add_location_or_const_value_attribute (var_die, decl_or_origin,
23811 decl == NULL);
23813 else
23814 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23816 if ((dwarf_version >= 4 || !dwarf_strict)
23817 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23818 DW_AT_const_expr) == 1
23819 && !get_AT (var_die, DW_AT_const_expr)
23820 && !specialization_p)
23821 add_AT_flag (var_die, DW_AT_const_expr, 1);
23823 if (!dwarf_strict)
23825 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23826 DW_AT_inline);
23827 if (inl != -1
23828 && !get_AT (var_die, DW_AT_inline)
23829 && !specialization_p)
23830 add_AT_unsigned (var_die, DW_AT_inline, inl);
23834 /* Generate a DIE to represent a named constant. */
23836 static void
23837 gen_const_die (tree decl, dw_die_ref context_die)
23839 dw_die_ref const_die;
23840 tree type = TREE_TYPE (decl);
23842 const_die = lookup_decl_die (decl);
23843 if (const_die)
23844 return;
23846 const_die = new_die (DW_TAG_constant, context_die, decl);
23847 equate_decl_number_to_die (decl, const_die);
23848 add_name_and_src_coords_attributes (const_die, decl);
23849 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23850 if (TREE_PUBLIC (decl))
23851 add_AT_flag (const_die, DW_AT_external, 1);
23852 if (DECL_ARTIFICIAL (decl))
23853 add_AT_flag (const_die, DW_AT_artificial, 1);
23854 tree_add_const_value_attribute_for_decl (const_die, decl);
23857 /* Generate a DIE to represent a label identifier. */
23859 static void
23860 gen_label_die (tree decl, dw_die_ref context_die)
23862 tree origin = decl_ultimate_origin (decl);
23863 dw_die_ref lbl_die = lookup_decl_die (decl);
23864 rtx insn;
23865 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23867 if (!lbl_die)
23869 lbl_die = new_die (DW_TAG_label, context_die, decl);
23870 equate_decl_number_to_die (decl, lbl_die);
23872 if (origin != NULL)
23873 add_abstract_origin_attribute (lbl_die, origin);
23874 else
23875 add_name_and_src_coords_attributes (lbl_die, decl);
23878 if (DECL_ABSTRACT_P (decl))
23879 equate_decl_number_to_die (decl, lbl_die);
23880 else if (! early_dwarf)
23882 insn = DECL_RTL_IF_SET (decl);
23884 /* Deleted labels are programmer specified labels which have been
23885 eliminated because of various optimizations. We still emit them
23886 here so that it is possible to put breakpoints on them. */
23887 if (insn
23888 && (LABEL_P (insn)
23889 || ((NOTE_P (insn)
23890 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23892 /* When optimization is enabled (via -O) some parts of the compiler
23893 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23894 represent source-level labels which were explicitly declared by
23895 the user. This really shouldn't be happening though, so catch
23896 it if it ever does happen. */
23897 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23899 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23900 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23902 else if (insn
23903 && NOTE_P (insn)
23904 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23905 && CODE_LABEL_NUMBER (insn) != -1)
23907 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23908 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23913 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23914 attributes to the DIE for a block STMT, to describe where the inlined
23915 function was called from. This is similar to add_src_coords_attributes. */
23917 static inline void
23918 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23920 /* We can end up with BUILTINS_LOCATION here. */
23921 if (RESERVED_LOCATION_P (BLOCK_SOURCE_LOCATION (stmt)))
23922 return;
23924 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23926 if (dwarf_version >= 3 || !dwarf_strict)
23928 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23929 add_AT_unsigned (die, DW_AT_call_line, s.line);
23930 if (debug_column_info && s.column)
23931 add_AT_unsigned (die, DW_AT_call_column, s.column);
23936 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23937 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23939 static inline void
23940 add_high_low_attributes (tree stmt, dw_die_ref die)
23942 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23944 if (inline_entry_data **iedp
23945 = !inline_entry_data_table ? NULL
23946 : inline_entry_data_table->find_slot_with_hash (stmt,
23947 htab_hash_pointer (stmt),
23948 NO_INSERT))
23950 inline_entry_data *ied = *iedp;
23951 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23952 gcc_assert (debug_inline_points);
23953 gcc_assert (inlined_function_outer_scope_p (stmt));
23955 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23956 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23958 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
23959 && !dwarf_strict)
23961 if (!output_asm_line_debug_info ())
23962 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23963 else
23965 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23966 /* FIXME: this will resolve to a small number. Could we
23967 possibly emit smaller data? Ideally we'd emit a
23968 uleb128, but that would make the size of DIEs
23969 impossible for the compiler to compute, since it's
23970 the assembler that computes the value of the view
23971 label in this case. Ideally, we'd have a single form
23972 encompassing both the address and the view, and
23973 indirecting them through a table might make things
23974 easier, but even that would be more wasteful,
23975 space-wise, than what we have now. */
23976 add_AT_symview (die, DW_AT_GNU_entry_view, label);
23980 inline_entry_data_table->clear_slot (iedp);
23983 if (BLOCK_FRAGMENT_CHAIN (stmt)
23984 && (dwarf_version >= 3 || !dwarf_strict))
23986 tree chain, superblock = NULL_TREE;
23987 dw_die_ref pdie;
23988 dw_attr_node *attr = NULL;
23990 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
23992 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23993 BLOCK_NUMBER (stmt));
23994 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23997 /* Optimize duplicate .debug_ranges lists or even tails of
23998 lists. If this BLOCK has same ranges as its supercontext,
23999 lookup DW_AT_ranges attribute in the supercontext (and
24000 recursively so), verify that the ranges_table contains the
24001 right values and use it instead of adding a new .debug_range. */
24002 for (chain = stmt, pdie = die;
24003 BLOCK_SAME_RANGE (chain);
24004 chain = BLOCK_SUPERCONTEXT (chain))
24006 dw_attr_node *new_attr;
24008 pdie = pdie->die_parent;
24009 if (pdie == NULL)
24010 break;
24011 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
24012 break;
24013 new_attr = get_AT (pdie, DW_AT_ranges);
24014 if (new_attr == NULL
24015 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
24016 break;
24017 attr = new_attr;
24018 superblock = BLOCK_SUPERCONTEXT (chain);
24020 if (attr != NULL
24021 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
24022 == (int)BLOCK_NUMBER (superblock))
24023 && BLOCK_FRAGMENT_CHAIN (superblock))
24025 unsigned long off = attr->dw_attr_val.v.val_offset;
24026 unsigned long supercnt = 0, thiscnt = 0;
24027 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
24028 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24030 ++supercnt;
24031 gcc_checking_assert ((*ranges_table)[off + supercnt].num
24032 == (int)BLOCK_NUMBER (chain));
24034 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
24035 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
24036 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
24037 ++thiscnt;
24038 gcc_assert (supercnt >= thiscnt);
24039 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
24040 false);
24041 note_rnglist_head (off + supercnt - thiscnt);
24042 return;
24045 unsigned int offset = add_ranges (stmt, true);
24046 add_AT_range_list (die, DW_AT_ranges, offset, false);
24047 note_rnglist_head (offset);
24049 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
24050 chain = BLOCK_FRAGMENT_CHAIN (stmt);
24053 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
24054 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24055 chain = BLOCK_FRAGMENT_CHAIN (chain);
24057 while (chain);
24058 add_ranges (NULL);
24060 else
24062 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24063 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24064 BLOCK_NUMBER (stmt));
24065 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24066 BLOCK_NUMBER (stmt));
24067 add_AT_low_high_pc (die, label, label_high, false);
24071 /* Generate a DIE for a lexical block. */
24073 static void
24074 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24076 dw_die_ref old_die = lookup_block_die (stmt);
24077 dw_die_ref stmt_die = NULL;
24078 if (!old_die)
24080 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24081 equate_block_to_die (stmt, stmt_die);
24084 if (BLOCK_ABSTRACT_ORIGIN (stmt))
24086 /* If this is an inlined or conrecte instance, create a new lexical
24087 die for anything below to attach DW_AT_abstract_origin to. */
24088 if (old_die)
24089 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24091 tree origin = block_ultimate_origin (stmt);
24092 if (origin != NULL_TREE && (origin != stmt || old_die))
24093 add_abstract_origin_attribute (stmt_die, origin);
24095 old_die = NULL;
24098 if (old_die)
24099 stmt_die = old_die;
24101 /* A non abstract block whose blocks have already been reordered
24102 should have the instruction range for this block. If so, set the
24103 high/low attributes. */
24104 if (!early_dwarf && TREE_ASM_WRITTEN (stmt))
24106 gcc_assert (stmt_die);
24107 add_high_low_attributes (stmt, stmt_die);
24110 decls_for_scope (stmt, stmt_die);
24113 /* Generate a DIE for an inlined subprogram. */
24115 static void
24116 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24118 tree decl = block_ultimate_origin (stmt);
24120 /* Make sure any inlined functions are known to be inlineable. */
24121 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24122 || cgraph_function_possibly_inlined_p (decl));
24124 dw_die_ref subr_die = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24126 if (call_arg_locations || debug_inline_points)
24127 equate_block_to_die (stmt, subr_die);
24128 add_abstract_origin_attribute (subr_die, decl);
24129 if (TREE_ASM_WRITTEN (stmt))
24130 add_high_low_attributes (stmt, subr_die);
24131 add_call_src_coords_attributes (stmt, subr_die);
24133 /* The inliner creates an extra BLOCK for the parameter setup,
24134 we want to merge that with the actual outermost BLOCK of the
24135 inlined function to avoid duplicate locals in consumers.
24136 Do that by doing the recursion to subblocks on the single subblock
24137 of STMT. */
24138 bool unwrap_one = false;
24139 if (BLOCK_SUBBLOCKS (stmt) && !BLOCK_CHAIN (BLOCK_SUBBLOCKS (stmt)))
24141 tree origin = block_ultimate_origin (BLOCK_SUBBLOCKS (stmt));
24142 if (origin
24143 && TREE_CODE (origin) == BLOCK
24144 && BLOCK_SUPERCONTEXT (origin) == decl)
24145 unwrap_one = true;
24147 decls_for_scope (stmt, subr_die, !unwrap_one);
24148 if (unwrap_one)
24149 decls_for_scope (BLOCK_SUBBLOCKS (stmt), subr_die);
24152 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24153 the comment for VLR_CONTEXT. */
24155 static void
24156 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24158 dw_die_ref decl_die;
24160 if (TREE_TYPE (decl) == error_mark_node)
24161 return;
24163 decl_die = new_die (DW_TAG_member, context_die, decl);
24164 add_name_and_src_coords_attributes (decl_die, decl);
24165 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24166 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24167 context_die);
24169 if (DECL_BIT_FIELD_TYPE (decl))
24171 add_byte_size_attribute (decl_die, decl);
24172 add_bit_size_attribute (decl_die, decl);
24173 add_bit_offset_attribute (decl_die, decl, ctx);
24176 add_alignment_attribute (decl_die, decl);
24178 /* If we have a variant part offset, then we are supposed to process a member
24179 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24180 trees. */
24181 gcc_assert (ctx->variant_part_offset == NULL_TREE
24182 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24183 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24184 add_data_member_location_attribute (decl_die, decl, ctx);
24186 if (DECL_ARTIFICIAL (decl))
24187 add_AT_flag (decl_die, DW_AT_artificial, 1);
24189 add_accessibility_attribute (decl_die, decl);
24191 /* Equate decl number to die, so that we can look up this decl later on. */
24192 equate_decl_number_to_die (decl, decl_die);
24195 /* Generate a DIE for a pointer to a member type. TYPE can be an
24196 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24197 pointer to member function. */
24199 static void
24200 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24202 if (lookup_type_die (type))
24203 return;
24205 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24206 scope_die_for (type, context_die), type);
24208 equate_type_number_to_die (type, ptr_die);
24209 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24210 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24211 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24212 context_die);
24213 add_alignment_attribute (ptr_die, type);
24215 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24216 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24218 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24219 add_AT_loc (ptr_die, DW_AT_use_location, op);
24223 static char *producer_string;
24225 /* Return a heap allocated producer string including command line options
24226 if -grecord-gcc-switches. */
24228 static char *
24229 gen_producer_string (void)
24231 size_t j;
24232 auto_vec<const char *> switches;
24233 const char *language_string = lang_hooks.name;
24234 char *producer, *tail;
24235 const char *p;
24236 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24237 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24239 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24240 switch (save_decoded_options[j].opt_index)
24242 case OPT_o:
24243 case OPT_d:
24244 case OPT_dumpbase:
24245 case OPT_dumpdir:
24246 case OPT_auxbase:
24247 case OPT_auxbase_strip:
24248 case OPT_quiet:
24249 case OPT_version:
24250 case OPT_v:
24251 case OPT_w:
24252 case OPT_L:
24253 case OPT_D:
24254 case OPT_I:
24255 case OPT_U:
24256 case OPT_SPECIAL_unknown:
24257 case OPT_SPECIAL_ignore:
24258 case OPT_SPECIAL_deprecated:
24259 case OPT_SPECIAL_program_name:
24260 case OPT_SPECIAL_input_file:
24261 case OPT_grecord_gcc_switches:
24262 case OPT__output_pch_:
24263 case OPT_fdiagnostics_show_location_:
24264 case OPT_fdiagnostics_show_option:
24265 case OPT_fdiagnostics_show_caret:
24266 case OPT_fdiagnostics_show_labels:
24267 case OPT_fdiagnostics_show_line_numbers:
24268 case OPT_fdiagnostics_color_:
24269 case OPT_fverbose_asm:
24270 case OPT____:
24271 case OPT__sysroot_:
24272 case OPT_nostdinc:
24273 case OPT_nostdinc__:
24274 case OPT_fpreprocessed:
24275 case OPT_fltrans_output_list_:
24276 case OPT_fresolution_:
24277 case OPT_fdebug_prefix_map_:
24278 case OPT_fmacro_prefix_map_:
24279 case OPT_ffile_prefix_map_:
24280 case OPT_fcompare_debug:
24281 case OPT_fchecking:
24282 case OPT_fchecking_:
24283 /* Ignore these. */
24284 continue;
24285 default:
24286 if (cl_options[save_decoded_options[j].opt_index].flags
24287 & CL_NO_DWARF_RECORD)
24288 continue;
24289 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24290 == '-');
24291 switch (save_decoded_options[j].canonical_option[0][1])
24293 case 'M':
24294 case 'i':
24295 case 'W':
24296 continue;
24297 case 'f':
24298 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24299 "dump", 4) == 0)
24300 continue;
24301 break;
24302 default:
24303 break;
24305 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24306 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24307 break;
24310 producer = XNEWVEC (char, plen + 1 + len + 1);
24311 tail = producer;
24312 sprintf (tail, "%s %s", language_string, version_string);
24313 tail += plen;
24315 FOR_EACH_VEC_ELT (switches, j, p)
24317 len = strlen (p);
24318 *tail = ' ';
24319 memcpy (tail + 1, p, len);
24320 tail += len + 1;
24323 *tail = '\0';
24324 return producer;
24327 /* Given a C and/or C++ language/version string return the "highest".
24328 C++ is assumed to be "higher" than C in this case. Used for merging
24329 LTO translation unit languages. */
24330 static const char *
24331 highest_c_language (const char *lang1, const char *lang2)
24333 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24334 return "GNU C++17";
24335 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24336 return "GNU C++14";
24337 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24338 return "GNU C++11";
24339 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24340 return "GNU C++98";
24342 if (strcmp ("GNU C2X", lang1) == 0 || strcmp ("GNU C2X", lang2) == 0)
24343 return "GNU C2X";
24344 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24345 return "GNU C17";
24346 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24347 return "GNU C11";
24348 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24349 return "GNU C99";
24350 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24351 return "GNU C89";
24353 gcc_unreachable ();
24357 /* Generate the DIE for the compilation unit. */
24359 static dw_die_ref
24360 gen_compile_unit_die (const char *filename)
24362 dw_die_ref die;
24363 const char *language_string = lang_hooks.name;
24364 int language;
24366 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24368 if (filename)
24370 add_name_attribute (die, filename);
24371 /* Don't add cwd for <built-in>. */
24372 if (filename[0] != '<')
24373 add_comp_dir_attribute (die);
24376 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24378 /* If our producer is LTO try to figure out a common language to use
24379 from the global list of translation units. */
24380 if (strcmp (language_string, "GNU GIMPLE") == 0)
24382 unsigned i;
24383 tree t;
24384 const char *common_lang = NULL;
24386 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24388 if (!TRANSLATION_UNIT_LANGUAGE (t))
24389 continue;
24390 if (!common_lang)
24391 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24392 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24394 else if (strncmp (common_lang, "GNU C", 5) == 0
24395 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24396 /* Mixing C and C++ is ok, use C++ in that case. */
24397 common_lang = highest_c_language (common_lang,
24398 TRANSLATION_UNIT_LANGUAGE (t));
24399 else
24401 /* Fall back to C. */
24402 common_lang = NULL;
24403 break;
24407 if (common_lang)
24408 language_string = common_lang;
24411 language = DW_LANG_C;
24412 if (strncmp (language_string, "GNU C", 5) == 0
24413 && ISDIGIT (language_string[5]))
24415 language = DW_LANG_C89;
24416 if (dwarf_version >= 3 || !dwarf_strict)
24418 if (strcmp (language_string, "GNU C89") != 0)
24419 language = DW_LANG_C99;
24421 if (dwarf_version >= 5 /* || !dwarf_strict */)
24422 if (strcmp (language_string, "GNU C11") == 0
24423 || strcmp (language_string, "GNU C17") == 0
24424 || strcmp (language_string, "GNU C2X"))
24425 language = DW_LANG_C11;
24428 else if (strncmp (language_string, "GNU C++", 7) == 0)
24430 language = DW_LANG_C_plus_plus;
24431 if (dwarf_version >= 5 /* || !dwarf_strict */)
24433 if (strcmp (language_string, "GNU C++11") == 0)
24434 language = DW_LANG_C_plus_plus_11;
24435 else if (strcmp (language_string, "GNU C++14") == 0)
24436 language = DW_LANG_C_plus_plus_14;
24437 else if (strcmp (language_string, "GNU C++17") == 0)
24438 /* For now. */
24439 language = DW_LANG_C_plus_plus_14;
24442 else if (strcmp (language_string, "GNU F77") == 0)
24443 language = DW_LANG_Fortran77;
24444 else if (dwarf_version >= 3 || !dwarf_strict)
24446 if (strcmp (language_string, "GNU Ada") == 0)
24447 language = DW_LANG_Ada95;
24448 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24450 language = DW_LANG_Fortran95;
24451 if (dwarf_version >= 5 /* || !dwarf_strict */)
24453 if (strcmp (language_string, "GNU Fortran2003") == 0)
24454 language = DW_LANG_Fortran03;
24455 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24456 language = DW_LANG_Fortran08;
24459 else if (strcmp (language_string, "GNU Objective-C") == 0)
24460 language = DW_LANG_ObjC;
24461 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24462 language = DW_LANG_ObjC_plus_plus;
24463 else if (strcmp (language_string, "GNU D") == 0)
24464 language = DW_LANG_D;
24465 else if (dwarf_version >= 5 || !dwarf_strict)
24467 if (strcmp (language_string, "GNU Go") == 0)
24468 language = DW_LANG_Go;
24471 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24472 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24473 language = DW_LANG_Fortran90;
24474 /* Likewise for Ada. */
24475 else if (strcmp (language_string, "GNU Ada") == 0)
24476 language = DW_LANG_Ada83;
24478 add_AT_unsigned (die, DW_AT_language, language);
24480 switch (language)
24482 case DW_LANG_Fortran77:
24483 case DW_LANG_Fortran90:
24484 case DW_LANG_Fortran95:
24485 case DW_LANG_Fortran03:
24486 case DW_LANG_Fortran08:
24487 /* Fortran has case insensitive identifiers and the front-end
24488 lowercases everything. */
24489 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24490 break;
24491 default:
24492 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24493 break;
24495 return die;
24498 /* Generate the DIE for a base class. */
24500 static void
24501 gen_inheritance_die (tree binfo, tree access, tree type,
24502 dw_die_ref context_die)
24504 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24505 struct vlr_context ctx = { type, NULL };
24507 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24508 context_die);
24509 add_data_member_location_attribute (die, binfo, &ctx);
24511 if (BINFO_VIRTUAL_P (binfo))
24512 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24514 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24515 children, otherwise the default is DW_ACCESS_public. In DWARF2
24516 the default has always been DW_ACCESS_private. */
24517 if (access == access_public_node)
24519 if (dwarf_version == 2
24520 || context_die->die_tag == DW_TAG_class_type)
24521 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24523 else if (access == access_protected_node)
24524 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24525 else if (dwarf_version > 2
24526 && context_die->die_tag != DW_TAG_class_type)
24527 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24530 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24531 structure. */
24532 static bool
24533 is_variant_part (tree decl)
24535 return (TREE_CODE (decl) == FIELD_DECL
24536 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24539 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24540 return the FIELD_DECL. Return NULL_TREE otherwise. */
24542 static tree
24543 analyze_discr_in_predicate (tree operand, tree struct_type)
24545 bool continue_stripping = true;
24546 while (continue_stripping)
24547 switch (TREE_CODE (operand))
24549 CASE_CONVERT:
24550 operand = TREE_OPERAND (operand, 0);
24551 break;
24552 default:
24553 continue_stripping = false;
24554 break;
24557 /* Match field access to members of struct_type only. */
24558 if (TREE_CODE (operand) == COMPONENT_REF
24559 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24560 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24561 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24562 return TREE_OPERAND (operand, 1);
24563 else
24564 return NULL_TREE;
24567 /* Check that SRC is a constant integer that can be represented as a native
24568 integer constant (either signed or unsigned). If so, store it into DEST and
24569 return true. Return false otherwise. */
24571 static bool
24572 get_discr_value (tree src, dw_discr_value *dest)
24574 tree discr_type = TREE_TYPE (src);
24576 if (lang_hooks.types.get_debug_type)
24578 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24579 if (debug_type != NULL)
24580 discr_type = debug_type;
24583 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24584 return false;
24586 /* Signedness can vary between the original type and the debug type. This
24587 can happen for character types in Ada for instance: the character type
24588 used for code generation can be signed, to be compatible with the C one,
24589 but from a debugger point of view, it must be unsigned. */
24590 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24591 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24593 if (is_orig_unsigned != is_debug_unsigned)
24594 src = fold_convert (discr_type, src);
24596 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24597 return false;
24599 dest->pos = is_debug_unsigned;
24600 if (is_debug_unsigned)
24601 dest->v.uval = tree_to_uhwi (src);
24602 else
24603 dest->v.sval = tree_to_shwi (src);
24605 return true;
24608 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24609 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24610 store NULL_TREE in DISCR_DECL. Otherwise:
24612 - store the discriminant field in STRUCT_TYPE that controls the variant
24613 part to *DISCR_DECL
24615 - put in *DISCR_LISTS_P an array where for each variant, the item
24616 represents the corresponding matching list of discriminant values.
24618 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24619 the above array.
24621 Note that when the array is allocated (i.e. when the analysis is
24622 successful), it is up to the caller to free the array. */
24624 static void
24625 analyze_variants_discr (tree variant_part_decl,
24626 tree struct_type,
24627 tree *discr_decl,
24628 dw_discr_list_ref **discr_lists_p,
24629 unsigned *discr_lists_length)
24631 tree variant_part_type = TREE_TYPE (variant_part_decl);
24632 tree variant;
24633 dw_discr_list_ref *discr_lists;
24634 unsigned i;
24636 /* Compute how many variants there are in this variant part. */
24637 *discr_lists_length = 0;
24638 for (variant = TYPE_FIELDS (variant_part_type);
24639 variant != NULL_TREE;
24640 variant = DECL_CHAIN (variant))
24641 ++*discr_lists_length;
24643 *discr_decl = NULL_TREE;
24644 *discr_lists_p
24645 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24646 sizeof (**discr_lists_p));
24647 discr_lists = *discr_lists_p;
24649 /* And then analyze all variants to extract discriminant information for all
24650 of them. This analysis is conservative: as soon as we detect something we
24651 do not support, abort everything and pretend we found nothing. */
24652 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24653 variant != NULL_TREE;
24654 variant = DECL_CHAIN (variant), ++i)
24656 tree match_expr = DECL_QUALIFIER (variant);
24658 /* Now, try to analyze the predicate and deduce a discriminant for
24659 it. */
24660 if (match_expr == boolean_true_node)
24661 /* Typically happens for the default variant: it matches all cases that
24662 previous variants rejected. Don't output any matching value for
24663 this one. */
24664 continue;
24666 /* The following loop tries to iterate over each discriminant
24667 possibility: single values or ranges. */
24668 while (match_expr != NULL_TREE)
24670 tree next_round_match_expr;
24671 tree candidate_discr = NULL_TREE;
24672 dw_discr_list_ref new_node = NULL;
24674 /* Possibilities are matched one after the other by nested
24675 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24676 continue with the rest at next iteration. */
24677 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24679 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24680 match_expr = TREE_OPERAND (match_expr, 1);
24682 else
24683 next_round_match_expr = NULL_TREE;
24685 if (match_expr == boolean_false_node)
24686 /* This sub-expression matches nothing: just wait for the next
24687 one. */
24690 else if (TREE_CODE (match_expr) == EQ_EXPR)
24692 /* We are matching: <discr_field> == <integer_cst>
24693 This sub-expression matches a single value. */
24694 tree integer_cst = TREE_OPERAND (match_expr, 1);
24696 candidate_discr
24697 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24698 struct_type);
24700 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24701 if (!get_discr_value (integer_cst,
24702 &new_node->dw_discr_lower_bound))
24703 goto abort;
24704 new_node->dw_discr_range = false;
24707 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24709 /* We are matching:
24710 <discr_field> > <integer_cst>
24711 && <discr_field> < <integer_cst>.
24712 This sub-expression matches the range of values between the
24713 two matched integer constants. Note that comparisons can be
24714 inclusive or exclusive. */
24715 tree candidate_discr_1, candidate_discr_2;
24716 tree lower_cst, upper_cst;
24717 bool lower_cst_included, upper_cst_included;
24718 tree lower_op = TREE_OPERAND (match_expr, 0);
24719 tree upper_op = TREE_OPERAND (match_expr, 1);
24721 /* When the comparison is exclusive, the integer constant is not
24722 the discriminant range bound we are looking for: we will have
24723 to increment or decrement it. */
24724 if (TREE_CODE (lower_op) == GE_EXPR)
24725 lower_cst_included = true;
24726 else if (TREE_CODE (lower_op) == GT_EXPR)
24727 lower_cst_included = false;
24728 else
24729 goto abort;
24731 if (TREE_CODE (upper_op) == LE_EXPR)
24732 upper_cst_included = true;
24733 else if (TREE_CODE (upper_op) == LT_EXPR)
24734 upper_cst_included = false;
24735 else
24736 goto abort;
24738 /* Extract the discriminant from the first operand and check it
24739 is consistant with the same analysis in the second
24740 operand. */
24741 candidate_discr_1
24742 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24743 struct_type);
24744 candidate_discr_2
24745 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24746 struct_type);
24747 if (candidate_discr_1 == candidate_discr_2)
24748 candidate_discr = candidate_discr_1;
24749 else
24750 goto abort;
24752 /* Extract bounds from both. */
24753 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24754 lower_cst = TREE_OPERAND (lower_op, 1);
24755 upper_cst = TREE_OPERAND (upper_op, 1);
24757 if (!lower_cst_included)
24758 lower_cst
24759 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24760 build_int_cst (TREE_TYPE (lower_cst), 1));
24761 if (!upper_cst_included)
24762 upper_cst
24763 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24764 build_int_cst (TREE_TYPE (upper_cst), 1));
24766 if (!get_discr_value (lower_cst,
24767 &new_node->dw_discr_lower_bound)
24768 || !get_discr_value (upper_cst,
24769 &new_node->dw_discr_upper_bound))
24770 goto abort;
24772 new_node->dw_discr_range = true;
24775 else
24776 /* Unsupported sub-expression: we cannot determine the set of
24777 matching discriminant values. Abort everything. */
24778 goto abort;
24780 /* If the discriminant info is not consistant with what we saw so
24781 far, consider the analysis failed and abort everything. */
24782 if (candidate_discr == NULL_TREE
24783 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24784 goto abort;
24785 else
24786 *discr_decl = candidate_discr;
24788 if (new_node != NULL)
24790 new_node->dw_discr_next = discr_lists[i];
24791 discr_lists[i] = new_node;
24793 match_expr = next_round_match_expr;
24797 /* If we reach this point, we could match everything we were interested
24798 in. */
24799 return;
24801 abort:
24802 /* Clean all data structure and return no result. */
24803 free (*discr_lists_p);
24804 *discr_lists_p = NULL;
24805 *discr_decl = NULL_TREE;
24808 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24809 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24810 under CONTEXT_DIE.
24812 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24813 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24814 this type, which are record types, represent the available variants and each
24815 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24816 values are inferred from these attributes.
24818 In trees, the offsets for the fields inside these sub-records are relative
24819 to the variant part itself, whereas the corresponding DIEs should have
24820 offset attributes that are relative to the embedding record base address.
24821 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24822 must be an expression that computes the offset of the variant part to
24823 describe in DWARF. */
24825 static void
24826 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24827 dw_die_ref context_die)
24829 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24830 tree variant_part_offset = vlr_ctx->variant_part_offset;
24831 struct loc_descr_context ctx = {
24832 vlr_ctx->struct_type, /* context_type */
24833 NULL_TREE, /* base_decl */
24834 NULL, /* dpi */
24835 false, /* placeholder_arg */
24836 false /* placeholder_seen */
24839 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24840 NULL_TREE if there is no such field. */
24841 tree discr_decl = NULL_TREE;
24842 dw_discr_list_ref *discr_lists;
24843 unsigned discr_lists_length = 0;
24844 unsigned i;
24846 dw_die_ref dwarf_proc_die = NULL;
24847 dw_die_ref variant_part_die
24848 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24850 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24852 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24853 &discr_decl, &discr_lists, &discr_lists_length);
24855 if (discr_decl != NULL_TREE)
24857 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24859 if (discr_die)
24860 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24861 else
24862 /* We have no DIE for the discriminant, so just discard all
24863 discrimimant information in the output. */
24864 discr_decl = NULL_TREE;
24867 /* If the offset for this variant part is more complex than a constant,
24868 create a DWARF procedure for it so that we will not have to generate DWARF
24869 expressions for it for each member. */
24870 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24871 && (dwarf_version >= 3 || !dwarf_strict))
24873 const tree dwarf_proc_fndecl
24874 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24875 build_function_type (TREE_TYPE (variant_part_offset),
24876 NULL_TREE));
24877 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24878 const dw_loc_descr_ref dwarf_proc_body
24879 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24881 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24882 dwarf_proc_fndecl, context_die);
24883 if (dwarf_proc_die != NULL)
24884 variant_part_offset = dwarf_proc_call;
24887 /* Output DIEs for all variants. */
24888 i = 0;
24889 for (tree variant = TYPE_FIELDS (variant_part_type);
24890 variant != NULL_TREE;
24891 variant = DECL_CHAIN (variant), ++i)
24893 tree variant_type = TREE_TYPE (variant);
24894 dw_die_ref variant_die;
24896 /* All variants (i.e. members of a variant part) are supposed to be
24897 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24898 under these records. */
24899 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24901 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24902 equate_decl_number_to_die (variant, variant_die);
24904 /* Output discriminant values this variant matches, if any. */
24905 if (discr_decl == NULL || discr_lists[i] == NULL)
24906 /* In the case we have discriminant information at all, this is
24907 probably the default variant: as the standard says, don't
24908 output any discriminant value/list attribute. */
24910 else if (discr_lists[i]->dw_discr_next == NULL
24911 && !discr_lists[i]->dw_discr_range)
24912 /* If there is only one accepted value, don't bother outputting a
24913 list. */
24914 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24915 else
24916 add_discr_list (variant_die, discr_lists[i]);
24918 for (tree member = TYPE_FIELDS (variant_type);
24919 member != NULL_TREE;
24920 member = DECL_CHAIN (member))
24922 struct vlr_context vlr_sub_ctx = {
24923 vlr_ctx->struct_type, /* struct_type */
24924 NULL /* variant_part_offset */
24926 if (is_variant_part (member))
24928 /* All offsets for fields inside variant parts are relative to
24929 the top-level embedding RECORD_TYPE's base address. On the
24930 other hand, offsets in GCC's types are relative to the
24931 nested-most variant part. So we have to sum offsets each time
24932 we recurse. */
24934 vlr_sub_ctx.variant_part_offset
24935 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24936 variant_part_offset, byte_position (member));
24937 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24939 else
24941 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24942 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24947 free (discr_lists);
24950 /* Generate a DIE for a class member. */
24952 static void
24953 gen_member_die (tree type, dw_die_ref context_die)
24955 tree member;
24956 tree binfo = TYPE_BINFO (type);
24958 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24960 /* If this is not an incomplete type, output descriptions of each of its
24961 members. Note that as we output the DIEs necessary to represent the
24962 members of this record or union type, we will also be trying to output
24963 DIEs to represent the *types* of those members. However the `type'
24964 function (above) will specifically avoid generating type DIEs for member
24965 types *within* the list of member DIEs for this (containing) type except
24966 for those types (of members) which are explicitly marked as also being
24967 members of this (containing) type themselves. The g++ front- end can
24968 force any given type to be treated as a member of some other (containing)
24969 type by setting the TYPE_CONTEXT of the given (member) type to point to
24970 the TREE node representing the appropriate (containing) type. */
24972 /* First output info about the base classes. */
24973 if (binfo)
24975 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24976 int i;
24977 tree base;
24979 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24980 gen_inheritance_die (base,
24981 (accesses ? (*accesses)[i] : access_public_node),
24982 type,
24983 context_die);
24986 /* Now output info about the data members and type members. */
24987 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24989 struct vlr_context vlr_ctx = { type, NULL_TREE };
24990 bool static_inline_p
24991 = (TREE_STATIC (member)
24992 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24993 != -1));
24995 /* Ignore clones. */
24996 if (DECL_ABSTRACT_ORIGIN (member))
24997 continue;
24999 /* If we thought we were generating minimal debug info for TYPE
25000 and then changed our minds, some of the member declarations
25001 may have already been defined. Don't define them again, but
25002 do put them in the right order. */
25004 if (dw_die_ref child = lookup_decl_die (member))
25006 /* Handle inline static data members, which only have in-class
25007 declarations. */
25008 dw_die_ref ref = NULL;
25009 if (child->die_tag == DW_TAG_variable
25010 && child->die_parent == comp_unit_die ())
25012 ref = get_AT_ref (child, DW_AT_specification);
25013 /* For C++17 inline static data members followed by redundant
25014 out of class redeclaration, we might get here with
25015 child being the DIE created for the out of class
25016 redeclaration and with its DW_AT_specification being
25017 the DIE created for in-class definition. We want to
25018 reparent the latter, and don't want to create another
25019 DIE with DW_AT_specification in that case, because
25020 we already have one. */
25021 if (ref
25022 && static_inline_p
25023 && ref->die_tag == DW_TAG_variable
25024 && ref->die_parent == comp_unit_die ()
25025 && get_AT (ref, DW_AT_specification) == NULL)
25027 child = ref;
25028 ref = NULL;
25029 static_inline_p = false;
25033 if (child->die_tag == DW_TAG_variable
25034 && child->die_parent == comp_unit_die ()
25035 && ref == NULL)
25037 reparent_child (child, context_die);
25038 if (dwarf_version < 5)
25039 child->die_tag = DW_TAG_member;
25041 else
25042 splice_child_die (context_die, child);
25045 /* Do not generate standard DWARF for variant parts if we are generating
25046 the corresponding GNAT encodings: DIEs generated for both would
25047 conflict in our mappings. */
25048 else if (is_variant_part (member)
25049 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
25051 vlr_ctx.variant_part_offset = byte_position (member);
25052 gen_variant_part (member, &vlr_ctx, context_die);
25054 else
25056 vlr_ctx.variant_part_offset = NULL_TREE;
25057 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25060 /* For C++ inline static data members emit immediately a DW_TAG_variable
25061 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25062 DW_AT_specification. */
25063 if (static_inline_p)
25065 int old_extern = DECL_EXTERNAL (member);
25066 DECL_EXTERNAL (member) = 0;
25067 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25068 DECL_EXTERNAL (member) = old_extern;
25073 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25074 is set, we pretend that the type was never defined, so we only get the
25075 member DIEs needed by later specification DIEs. */
25077 static void
25078 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25079 enum debug_info_usage usage)
25081 if (TREE_ASM_WRITTEN (type))
25083 /* Fill in the bound of variable-length fields in late dwarf if
25084 still incomplete. */
25085 if (!early_dwarf && variably_modified_type_p (type, NULL))
25086 for (tree member = TYPE_FIELDS (type);
25087 member;
25088 member = DECL_CHAIN (member))
25089 fill_variable_array_bounds (TREE_TYPE (member));
25090 return;
25093 dw_die_ref type_die = lookup_type_die (type);
25094 dw_die_ref scope_die = 0;
25095 int nested = 0;
25096 int complete = (TYPE_SIZE (type)
25097 && (! TYPE_STUB_DECL (type)
25098 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25099 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25100 complete = complete && should_emit_struct_debug (type, usage);
25102 if (type_die && ! complete)
25103 return;
25105 if (TYPE_CONTEXT (type) != NULL_TREE
25106 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25107 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25108 nested = 1;
25110 scope_die = scope_die_for (type, context_die);
25112 /* Generate child dies for template paramaters. */
25113 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25114 schedule_generic_params_dies_gen (type);
25116 if (! type_die || (nested && is_cu_die (scope_die)))
25117 /* First occurrence of type or toplevel definition of nested class. */
25119 dw_die_ref old_die = type_die;
25121 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25122 ? record_type_tag (type) : DW_TAG_union_type,
25123 scope_die, type);
25124 equate_type_number_to_die (type, type_die);
25125 if (old_die)
25126 add_AT_specification (type_die, old_die);
25127 else
25128 add_name_attribute (type_die, type_tag (type));
25130 else
25131 remove_AT (type_die, DW_AT_declaration);
25133 /* If this type has been completed, then give it a byte_size attribute and
25134 then give a list of members. */
25135 if (complete && !ns_decl)
25137 /* Prevent infinite recursion in cases where the type of some member of
25138 this type is expressed in terms of this type itself. */
25139 TREE_ASM_WRITTEN (type) = 1;
25140 add_byte_size_attribute (type_die, type);
25141 add_alignment_attribute (type_die, type);
25142 if (TYPE_STUB_DECL (type) != NULL_TREE)
25144 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25145 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25148 /* If the first reference to this type was as the return type of an
25149 inline function, then it may not have a parent. Fix this now. */
25150 if (type_die->die_parent == NULL)
25151 add_child_die (scope_die, type_die);
25153 gen_member_die (type, type_die);
25155 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25156 if (TYPE_ARTIFICIAL (type))
25157 add_AT_flag (type_die, DW_AT_artificial, 1);
25159 /* GNU extension: Record what type our vtable lives in. */
25160 if (TYPE_VFIELD (type))
25162 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25164 gen_type_die (vtype, context_die);
25165 add_AT_die_ref (type_die, DW_AT_containing_type,
25166 lookup_type_die (vtype));
25169 else
25171 add_AT_flag (type_die, DW_AT_declaration, 1);
25173 /* We don't need to do this for function-local types. */
25174 if (TYPE_STUB_DECL (type)
25175 && ! decl_function_context (TYPE_STUB_DECL (type)))
25176 vec_safe_push (incomplete_types, type);
25179 if (get_AT (type_die, DW_AT_name))
25180 add_pubtype (type, type_die);
25183 /* Generate a DIE for a subroutine _type_. */
25185 static void
25186 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25188 tree return_type = TREE_TYPE (type);
25189 dw_die_ref subr_die
25190 = new_die (DW_TAG_subroutine_type,
25191 scope_die_for (type, context_die), type);
25193 equate_type_number_to_die (type, subr_die);
25194 add_prototyped_attribute (subr_die, type);
25195 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25196 context_die);
25197 add_alignment_attribute (subr_die, type);
25198 gen_formal_types_die (type, subr_die);
25200 if (get_AT (subr_die, DW_AT_name))
25201 add_pubtype (type, subr_die);
25202 if ((dwarf_version >= 5 || !dwarf_strict)
25203 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25204 add_AT_flag (subr_die, DW_AT_reference, 1);
25205 if ((dwarf_version >= 5 || !dwarf_strict)
25206 && lang_hooks.types.type_dwarf_attribute (type,
25207 DW_AT_rvalue_reference) != -1)
25208 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25211 /* Generate a DIE for a type definition. */
25213 static void
25214 gen_typedef_die (tree decl, dw_die_ref context_die)
25216 dw_die_ref type_die;
25217 tree type;
25219 if (TREE_ASM_WRITTEN (decl))
25221 if (DECL_ORIGINAL_TYPE (decl))
25222 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25223 return;
25226 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25227 checks in process_scope_var and modified_type_die), this should be called
25228 only for original types. */
25229 gcc_assert (decl_ultimate_origin (decl) == NULL
25230 || decl_ultimate_origin (decl) == decl);
25232 TREE_ASM_WRITTEN (decl) = 1;
25233 type_die = new_die (DW_TAG_typedef, context_die, decl);
25235 add_name_and_src_coords_attributes (type_die, decl);
25236 if (DECL_ORIGINAL_TYPE (decl))
25238 type = DECL_ORIGINAL_TYPE (decl);
25239 if (type == error_mark_node)
25240 return;
25242 gcc_assert (type != TREE_TYPE (decl));
25243 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25245 else
25247 type = TREE_TYPE (decl);
25248 if (type == error_mark_node)
25249 return;
25251 if (is_naming_typedef_decl (TYPE_NAME (type)))
25253 /* Here, we are in the case of decl being a typedef naming
25254 an anonymous type, e.g:
25255 typedef struct {...} foo;
25256 In that case TREE_TYPE (decl) is not a typedef variant
25257 type and TYPE_NAME of the anonymous type is set to the
25258 TYPE_DECL of the typedef. This construct is emitted by
25259 the C++ FE.
25261 TYPE is the anonymous struct named by the typedef
25262 DECL. As we need the DW_AT_type attribute of the
25263 DW_TAG_typedef to point to the DIE of TYPE, let's
25264 generate that DIE right away. add_type_attribute
25265 called below will then pick (via lookup_type_die) that
25266 anonymous struct DIE. */
25267 if (!TREE_ASM_WRITTEN (type))
25268 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25270 /* This is a GNU Extension. We are adding a
25271 DW_AT_linkage_name attribute to the DIE of the
25272 anonymous struct TYPE. The value of that attribute
25273 is the name of the typedef decl naming the anonymous
25274 struct. This greatly eases the work of consumers of
25275 this debug info. */
25276 add_linkage_name_raw (lookup_type_die (type), decl);
25280 add_type_attribute (type_die, type, decl_quals (decl), false,
25281 context_die);
25283 if (is_naming_typedef_decl (decl))
25284 /* We want that all subsequent calls to lookup_type_die with
25285 TYPE in argument yield the DW_TAG_typedef we have just
25286 created. */
25287 equate_type_number_to_die (type, type_die);
25289 add_alignment_attribute (type_die, TREE_TYPE (decl));
25291 add_accessibility_attribute (type_die, decl);
25293 if (DECL_ABSTRACT_P (decl))
25294 equate_decl_number_to_die (decl, type_die);
25296 if (get_AT (type_die, DW_AT_name))
25297 add_pubtype (decl, type_die);
25300 /* Generate a DIE for a struct, class, enum or union type. */
25302 static void
25303 gen_tagged_type_die (tree type,
25304 dw_die_ref context_die,
25305 enum debug_info_usage usage)
25307 if (type == NULL_TREE
25308 || !is_tagged_type (type))
25309 return;
25311 if (TREE_ASM_WRITTEN (type))
25313 /* If this is a nested type whose containing class hasn't been written
25314 out yet, writing it out will cover this one, too. This does not apply
25315 to instantiations of member class templates; they need to be added to
25316 the containing class as they are generated. FIXME: This hurts the
25317 idea of combining type decls from multiple TUs, since we can't predict
25318 what set of template instantiations we'll get. */
25319 else if (TYPE_CONTEXT (type)
25320 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25321 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25323 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25325 if (TREE_ASM_WRITTEN (type))
25326 return;
25328 /* If that failed, attach ourselves to the stub. */
25329 context_die = lookup_type_die (TYPE_CONTEXT (type));
25331 else if (TYPE_CONTEXT (type) != NULL_TREE
25332 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25334 /* If this type is local to a function that hasn't been written
25335 out yet, use a NULL context for now; it will be fixed up in
25336 decls_for_scope. */
25337 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25338 /* A declaration DIE doesn't count; nested types need to go in the
25339 specification. */
25340 if (context_die && is_declaration_die (context_die))
25341 context_die = NULL;
25343 else
25344 context_die = declare_in_namespace (type, context_die);
25346 if (TREE_CODE (type) == ENUMERAL_TYPE)
25348 /* This might have been written out by the call to
25349 declare_in_namespace. */
25350 if (!TREE_ASM_WRITTEN (type))
25351 gen_enumeration_type_die (type, context_die);
25353 else
25354 gen_struct_or_union_type_die (type, context_die, usage);
25356 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25357 it up if it is ever completed. gen_*_type_die will set it for us
25358 when appropriate. */
25361 /* Generate a type description DIE. */
25363 static void
25364 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25365 enum debug_info_usage usage)
25367 struct array_descr_info info;
25369 if (type == NULL_TREE || type == error_mark_node)
25370 return;
25372 if (flag_checking && type)
25373 verify_type (type);
25375 if (TYPE_NAME (type) != NULL_TREE
25376 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25377 && is_redundant_typedef (TYPE_NAME (type))
25378 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25379 /* The DECL of this type is a typedef we don't want to emit debug
25380 info for but we want debug info for its underlying typedef.
25381 This can happen for e.g, the injected-class-name of a C++
25382 type. */
25383 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25385 /* If TYPE is a typedef type variant, let's generate debug info
25386 for the parent typedef which TYPE is a type of. */
25387 if (typedef_variant_p (type))
25389 if (TREE_ASM_WRITTEN (type))
25390 return;
25392 tree name = TYPE_NAME (type);
25393 tree origin = decl_ultimate_origin (name);
25394 if (origin != NULL && origin != name)
25396 gen_decl_die (origin, NULL, NULL, context_die);
25397 return;
25400 /* Prevent broken recursion; we can't hand off to the same type. */
25401 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25403 /* Give typedefs the right scope. */
25404 context_die = scope_die_for (type, context_die);
25406 TREE_ASM_WRITTEN (type) = 1;
25408 gen_decl_die (name, NULL, NULL, context_die);
25409 return;
25412 /* If type is an anonymous tagged type named by a typedef, let's
25413 generate debug info for the typedef. */
25414 if (is_naming_typedef_decl (TYPE_NAME (type)))
25416 /* Give typedefs the right scope. */
25417 context_die = scope_die_for (type, context_die);
25419 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25420 return;
25423 if (lang_hooks.types.get_debug_type)
25425 tree debug_type = lang_hooks.types.get_debug_type (type);
25427 if (debug_type != NULL_TREE && debug_type != type)
25429 gen_type_die_with_usage (debug_type, context_die, usage);
25430 return;
25434 /* We are going to output a DIE to represent the unqualified version
25435 of this type (i.e. without any const or volatile qualifiers) so
25436 get the main variant (i.e. the unqualified version) of this type
25437 now. (Vectors and arrays are special because the debugging info is in the
25438 cloned type itself. Similarly function/method types can contain extra
25439 ref-qualification). */
25440 if (TREE_CODE (type) == FUNCTION_TYPE
25441 || TREE_CODE (type) == METHOD_TYPE)
25443 /* For function/method types, can't use type_main_variant here,
25444 because that can have different ref-qualifiers for C++,
25445 but try to canonicalize. */
25446 tree main = TYPE_MAIN_VARIANT (type);
25447 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25448 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25449 && check_base_type (t, main)
25450 && check_lang_type (t, type))
25452 type = t;
25453 break;
25456 else if (TREE_CODE (type) != VECTOR_TYPE
25457 && TREE_CODE (type) != ARRAY_TYPE)
25458 type = type_main_variant (type);
25460 /* If this is an array type with hidden descriptor, handle it first. */
25461 if (!TREE_ASM_WRITTEN (type)
25462 && lang_hooks.types.get_array_descr_info)
25464 memset (&info, 0, sizeof (info));
25465 if (lang_hooks.types.get_array_descr_info (type, &info))
25467 /* Fortran sometimes emits array types with no dimension. */
25468 gcc_assert (info.ndimensions >= 0
25469 && (info.ndimensions
25470 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25471 gen_descr_array_type_die (type, &info, context_die);
25472 TREE_ASM_WRITTEN (type) = 1;
25473 return;
25477 if (TREE_ASM_WRITTEN (type))
25479 /* Variable-length types may be incomplete even if
25480 TREE_ASM_WRITTEN. For such types, fall through to
25481 gen_array_type_die() and possibly fill in
25482 DW_AT_{upper,lower}_bound attributes. */
25483 if ((TREE_CODE (type) != ARRAY_TYPE
25484 && TREE_CODE (type) != RECORD_TYPE
25485 && TREE_CODE (type) != UNION_TYPE
25486 && TREE_CODE (type) != QUAL_UNION_TYPE)
25487 || !variably_modified_type_p (type, NULL))
25488 return;
25491 switch (TREE_CODE (type))
25493 case ERROR_MARK:
25494 break;
25496 case POINTER_TYPE:
25497 case REFERENCE_TYPE:
25498 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25499 ensures that the gen_type_die recursion will terminate even if the
25500 type is recursive. Recursive types are possible in Ada. */
25501 /* ??? We could perhaps do this for all types before the switch
25502 statement. */
25503 TREE_ASM_WRITTEN (type) = 1;
25505 /* For these types, all that is required is that we output a DIE (or a
25506 set of DIEs) to represent the "basis" type. */
25507 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25508 DINFO_USAGE_IND_USE);
25509 break;
25511 case OFFSET_TYPE:
25512 /* This code is used for C++ pointer-to-data-member types.
25513 Output a description of the relevant class type. */
25514 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25515 DINFO_USAGE_IND_USE);
25517 /* Output a description of the type of the object pointed to. */
25518 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25519 DINFO_USAGE_IND_USE);
25521 /* Now output a DIE to represent this pointer-to-data-member type
25522 itself. */
25523 gen_ptr_to_mbr_type_die (type, context_die);
25524 break;
25526 case FUNCTION_TYPE:
25527 /* Force out return type (in case it wasn't forced out already). */
25528 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25529 DINFO_USAGE_DIR_USE);
25530 gen_subroutine_type_die (type, context_die);
25531 break;
25533 case METHOD_TYPE:
25534 /* Force out return type (in case it wasn't forced out already). */
25535 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25536 DINFO_USAGE_DIR_USE);
25537 gen_subroutine_type_die (type, context_die);
25538 break;
25540 case ARRAY_TYPE:
25541 case VECTOR_TYPE:
25542 gen_array_type_die (type, context_die);
25543 break;
25545 case ENUMERAL_TYPE:
25546 case RECORD_TYPE:
25547 case UNION_TYPE:
25548 case QUAL_UNION_TYPE:
25549 gen_tagged_type_die (type, context_die, usage);
25550 return;
25552 case VOID_TYPE:
25553 case INTEGER_TYPE:
25554 case REAL_TYPE:
25555 case FIXED_POINT_TYPE:
25556 case COMPLEX_TYPE:
25557 case BOOLEAN_TYPE:
25558 /* No DIEs needed for fundamental types. */
25559 break;
25561 case NULLPTR_TYPE:
25562 case LANG_TYPE:
25563 /* Just use DW_TAG_unspecified_type. */
25565 dw_die_ref type_die = lookup_type_die (type);
25566 if (type_die == NULL)
25568 tree name = TYPE_IDENTIFIER (type);
25569 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25570 type);
25571 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25572 equate_type_number_to_die (type, type_die);
25575 break;
25577 default:
25578 if (is_cxx_auto (type))
25580 tree name = TYPE_IDENTIFIER (type);
25581 dw_die_ref *die = (name == get_identifier ("auto")
25582 ? &auto_die : &decltype_auto_die);
25583 if (!*die)
25585 *die = new_die (DW_TAG_unspecified_type,
25586 comp_unit_die (), NULL_TREE);
25587 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25589 equate_type_number_to_die (type, *die);
25590 break;
25592 gcc_unreachable ();
25595 TREE_ASM_WRITTEN (type) = 1;
25598 static void
25599 gen_type_die (tree type, dw_die_ref context_die)
25601 if (type != error_mark_node)
25603 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25604 if (flag_checking)
25606 dw_die_ref die = lookup_type_die (type);
25607 if (die)
25608 check_die (die);
25613 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25614 things which are local to the given block. */
25616 static void
25617 gen_block_die (tree stmt, dw_die_ref context_die)
25619 int must_output_die = 0;
25620 bool inlined_func;
25622 /* Ignore blocks that are NULL. */
25623 if (stmt == NULL_TREE)
25624 return;
25626 inlined_func = inlined_function_outer_scope_p (stmt);
25628 /* If the block is one fragment of a non-contiguous block, do not
25629 process the variables, since they will have been done by the
25630 origin block. Do process subblocks. */
25631 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25633 tree sub;
25635 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25636 gen_block_die (sub, context_die);
25638 return;
25641 /* Determine if we need to output any Dwarf DIEs at all to represent this
25642 block. */
25643 if (inlined_func)
25644 /* The outer scopes for inlinings *must* always be represented. We
25645 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25646 must_output_die = 1;
25647 else if (lookup_block_die (stmt))
25648 /* If we already have a DIE then it was filled early. Meanwhile
25649 we might have pruned all BLOCK_VARS as optimized out but we
25650 still want to generate high/low PC attributes so output it. */
25651 must_output_die = 1;
25652 else if (TREE_USED (stmt)
25653 || TREE_ASM_WRITTEN (stmt))
25655 /* Determine if this block directly contains any "significant"
25656 local declarations which we will need to output DIEs for. */
25657 if (debug_info_level > DINFO_LEVEL_TERSE)
25659 /* We are not in terse mode so any local declaration that
25660 is not ignored for debug purposes counts as being a
25661 "significant" one. */
25662 if (BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25663 must_output_die = 1;
25664 else
25665 for (tree var = BLOCK_VARS (stmt); var; var = DECL_CHAIN (var))
25666 if (!DECL_IGNORED_P (var))
25668 must_output_die = 1;
25669 break;
25672 else if (!dwarf2out_ignore_block (stmt))
25673 must_output_die = 1;
25676 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25677 DIE for any block which contains no significant local declarations at
25678 all. Rather, in such cases we just call `decls_for_scope' so that any
25679 needed Dwarf info for any sub-blocks will get properly generated. Note
25680 that in terse mode, our definition of what constitutes a "significant"
25681 local declaration gets restricted to include only inlined function
25682 instances and local (nested) function definitions. */
25683 if (must_output_die)
25685 if (inlined_func)
25686 gen_inlined_subroutine_die (stmt, context_die);
25687 else
25688 gen_lexical_block_die (stmt, context_die);
25690 else
25691 decls_for_scope (stmt, context_die);
25694 /* Process variable DECL (or variable with origin ORIGIN) within
25695 block STMT and add it to CONTEXT_DIE. */
25696 static void
25697 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25699 dw_die_ref die;
25700 tree decl_or_origin = decl ? decl : origin;
25702 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25703 die = lookup_decl_die (decl_or_origin);
25704 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25706 if (TYPE_DECL_IS_STUB (decl_or_origin))
25707 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25708 else
25709 die = lookup_decl_die (decl_or_origin);
25710 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25711 if (! die && ! early_dwarf)
25712 return;
25714 else
25715 die = NULL;
25717 /* Avoid creating DIEs for local typedefs and concrete static variables that
25718 will only be pruned later. */
25719 if ((origin || decl_ultimate_origin (decl))
25720 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25721 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25723 origin = decl_ultimate_origin (decl_or_origin);
25724 if (decl && VAR_P (decl) && die != NULL)
25726 die = lookup_decl_die (origin);
25727 if (die != NULL)
25728 equate_decl_number_to_die (decl, die);
25730 return;
25733 if (die != NULL && die->die_parent == NULL)
25734 add_child_die (context_die, die);
25735 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25737 if (early_dwarf)
25738 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25739 stmt, context_die);
25741 else
25743 if (decl && DECL_P (decl))
25745 die = lookup_decl_die (decl);
25747 /* Early created DIEs do not have a parent as the decls refer
25748 to the function as DECL_CONTEXT rather than the BLOCK. */
25749 if (die && die->die_parent == NULL)
25751 gcc_assert (in_lto_p);
25752 add_child_die (context_die, die);
25756 gen_decl_die (decl, origin, NULL, context_die);
25760 /* Generate all of the decls declared within a given scope and (recursively)
25761 all of its sub-blocks. */
25763 static void
25764 decls_for_scope (tree stmt, dw_die_ref context_die, bool recurse)
25766 tree decl;
25767 unsigned int i;
25768 tree subblocks;
25770 /* Ignore NULL blocks. */
25771 if (stmt == NULL_TREE)
25772 return;
25774 /* Output the DIEs to represent all of the data objects and typedefs
25775 declared directly within this block but not within any nested
25776 sub-blocks. Also, nested function and tag DIEs have been
25777 generated with a parent of NULL; fix that up now. We don't
25778 have to do this if we're at -g1. */
25779 if (debug_info_level > DINFO_LEVEL_TERSE)
25781 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25782 process_scope_var (stmt, decl, NULL_TREE, context_die);
25783 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25784 origin - avoid doing this twice as we have no good way to see
25785 if we've done it once already. */
25786 if (! early_dwarf)
25787 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25789 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25790 if (decl == current_function_decl)
25791 /* Ignore declarations of the current function, while they
25792 are declarations, gen_subprogram_die would treat them
25793 as definitions again, because they are equal to
25794 current_function_decl and endlessly recurse. */;
25795 else if (TREE_CODE (decl) == FUNCTION_DECL)
25796 process_scope_var (stmt, decl, NULL_TREE, context_die);
25797 else
25798 process_scope_var (stmt, NULL_TREE, decl, context_die);
25802 /* Even if we're at -g1, we need to process the subblocks in order to get
25803 inlined call information. */
25805 /* Output the DIEs to represent all sub-blocks (and the items declared
25806 therein) of this block. */
25807 if (recurse)
25808 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25809 subblocks != NULL;
25810 subblocks = BLOCK_CHAIN (subblocks))
25811 gen_block_die (subblocks, context_die);
25814 /* Is this a typedef we can avoid emitting? */
25816 static bool
25817 is_redundant_typedef (const_tree decl)
25819 if (TYPE_DECL_IS_STUB (decl))
25820 return true;
25822 if (DECL_ARTIFICIAL (decl)
25823 && DECL_CONTEXT (decl)
25824 && is_tagged_type (DECL_CONTEXT (decl))
25825 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25826 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25827 /* Also ignore the artificial member typedef for the class name. */
25828 return true;
25830 return false;
25833 /* Return TRUE if TYPE is a typedef that names a type for linkage
25834 purposes. This kind of typedefs is produced by the C++ FE for
25835 constructs like:
25837 typedef struct {...} foo;
25839 In that case, there is no typedef variant type produced for foo.
25840 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25841 struct type. */
25843 static bool
25844 is_naming_typedef_decl (const_tree decl)
25846 if (decl == NULL_TREE
25847 || TREE_CODE (decl) != TYPE_DECL
25848 || DECL_NAMELESS (decl)
25849 || !is_tagged_type (TREE_TYPE (decl))
25850 || DECL_IS_BUILTIN (decl)
25851 || is_redundant_typedef (decl)
25852 /* It looks like Ada produces TYPE_DECLs that are very similar
25853 to C++ naming typedefs but that have different
25854 semantics. Let's be specific to c++ for now. */
25855 || !is_cxx (decl))
25856 return FALSE;
25858 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25859 && TYPE_NAME (TREE_TYPE (decl)) == decl
25860 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25861 != TYPE_NAME (TREE_TYPE (decl))));
25864 /* Looks up the DIE for a context. */
25866 static inline dw_die_ref
25867 lookup_context_die (tree context)
25869 if (context)
25871 /* Find die that represents this context. */
25872 if (TYPE_P (context))
25874 context = TYPE_MAIN_VARIANT (context);
25875 dw_die_ref ctx = lookup_type_die (context);
25876 if (!ctx)
25877 return NULL;
25878 return strip_naming_typedef (context, ctx);
25880 else
25881 return lookup_decl_die (context);
25883 return comp_unit_die ();
25886 /* Returns the DIE for a context. */
25888 static inline dw_die_ref
25889 get_context_die (tree context)
25891 if (context)
25893 /* Find die that represents this context. */
25894 if (TYPE_P (context))
25896 context = TYPE_MAIN_VARIANT (context);
25897 return strip_naming_typedef (context, force_type_die (context));
25899 else
25900 return force_decl_die (context);
25902 return comp_unit_die ();
25905 /* Returns the DIE for decl. A DIE will always be returned. */
25907 static dw_die_ref
25908 force_decl_die (tree decl)
25910 dw_die_ref decl_die;
25911 unsigned saved_external_flag;
25912 tree save_fn = NULL_TREE;
25913 decl_die = lookup_decl_die (decl);
25914 if (!decl_die)
25916 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25918 decl_die = lookup_decl_die (decl);
25919 if (decl_die)
25920 return decl_die;
25922 switch (TREE_CODE (decl))
25924 case FUNCTION_DECL:
25925 /* Clear current_function_decl, so that gen_subprogram_die thinks
25926 that this is a declaration. At this point, we just want to force
25927 declaration die. */
25928 save_fn = current_function_decl;
25929 current_function_decl = NULL_TREE;
25930 gen_subprogram_die (decl, context_die);
25931 current_function_decl = save_fn;
25932 break;
25934 case VAR_DECL:
25935 /* Set external flag to force declaration die. Restore it after
25936 gen_decl_die() call. */
25937 saved_external_flag = DECL_EXTERNAL (decl);
25938 DECL_EXTERNAL (decl) = 1;
25939 gen_decl_die (decl, NULL, NULL, context_die);
25940 DECL_EXTERNAL (decl) = saved_external_flag;
25941 break;
25943 case NAMESPACE_DECL:
25944 if (dwarf_version >= 3 || !dwarf_strict)
25945 dwarf2out_decl (decl);
25946 else
25947 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25948 decl_die = comp_unit_die ();
25949 break;
25951 case TRANSLATION_UNIT_DECL:
25952 decl_die = comp_unit_die ();
25953 break;
25955 default:
25956 gcc_unreachable ();
25959 /* We should be able to find the DIE now. */
25960 if (!decl_die)
25961 decl_die = lookup_decl_die (decl);
25962 gcc_assert (decl_die);
25965 return decl_die;
25968 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25969 always returned. */
25971 static dw_die_ref
25972 force_type_die (tree type)
25974 dw_die_ref type_die;
25976 type_die = lookup_type_die (type);
25977 if (!type_die)
25979 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25981 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25982 false, context_die);
25983 gcc_assert (type_die);
25985 return type_die;
25988 /* Force out any required namespaces to be able to output DECL,
25989 and return the new context_die for it, if it's changed. */
25991 static dw_die_ref
25992 setup_namespace_context (tree thing, dw_die_ref context_die)
25994 tree context = (DECL_P (thing)
25995 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25996 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25997 /* Force out the namespace. */
25998 context_die = force_decl_die (context);
26000 return context_die;
26003 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
26004 type) within its namespace, if appropriate.
26006 For compatibility with older debuggers, namespace DIEs only contain
26007 declarations; all definitions are emitted at CU scope, with
26008 DW_AT_specification pointing to the declaration (like with class
26009 members). */
26011 static dw_die_ref
26012 declare_in_namespace (tree thing, dw_die_ref context_die)
26014 dw_die_ref ns_context;
26016 if (debug_info_level <= DINFO_LEVEL_TERSE)
26017 return context_die;
26019 /* External declarations in the local scope only need to be emitted
26020 once, not once in the namespace and once in the scope.
26022 This avoids declaring the `extern' below in the
26023 namespace DIE as well as in the innermost scope:
26025 namespace S
26027 int i=5;
26028 int foo()
26030 int i=8;
26031 extern int i;
26032 return i;
26036 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26037 return context_die;
26039 /* If this decl is from an inlined function, then don't try to emit it in its
26040 namespace, as we will get confused. It would have already been emitted
26041 when the abstract instance of the inline function was emitted anyways. */
26042 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26043 return context_die;
26045 ns_context = setup_namespace_context (thing, context_die);
26047 if (ns_context != context_die)
26049 if (is_fortran () || is_dlang ())
26050 return ns_context;
26051 if (DECL_P (thing))
26052 gen_decl_die (thing, NULL, NULL, ns_context);
26053 else
26054 gen_type_die (thing, ns_context);
26056 return context_die;
26059 /* Generate a DIE for a namespace or namespace alias. */
26061 static void
26062 gen_namespace_die (tree decl, dw_die_ref context_die)
26064 dw_die_ref namespace_die;
26066 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26067 they are an alias of. */
26068 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26070 /* Output a real namespace or module. */
26071 context_die = setup_namespace_context (decl, comp_unit_die ());
26072 namespace_die = new_die (is_fortran () || is_dlang ()
26073 ? DW_TAG_module : DW_TAG_namespace,
26074 context_die, decl);
26075 /* For Fortran modules defined in different CU don't add src coords. */
26076 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26078 const char *name = dwarf2_name (decl, 0);
26079 if (name)
26080 add_name_attribute (namespace_die, name);
26082 else
26083 add_name_and_src_coords_attributes (namespace_die, decl);
26084 if (DECL_EXTERNAL (decl))
26085 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26086 equate_decl_number_to_die (decl, namespace_die);
26088 else
26090 /* Output a namespace alias. */
26092 /* Force out the namespace we are an alias of, if necessary. */
26093 dw_die_ref origin_die
26094 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26096 if (DECL_FILE_SCOPE_P (decl)
26097 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26098 context_die = setup_namespace_context (decl, comp_unit_die ());
26099 /* Now create the namespace alias DIE. */
26100 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26101 add_name_and_src_coords_attributes (namespace_die, decl);
26102 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26103 equate_decl_number_to_die (decl, namespace_die);
26105 if ((dwarf_version >= 5 || !dwarf_strict)
26106 && lang_hooks.decls.decl_dwarf_attribute (decl,
26107 DW_AT_export_symbols) == 1)
26108 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26110 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26111 if (want_pubnames ())
26112 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26115 /* Generate Dwarf debug information for a decl described by DECL.
26116 The return value is currently only meaningful for PARM_DECLs,
26117 for all other decls it returns NULL.
26119 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26120 It can be NULL otherwise. */
26122 static dw_die_ref
26123 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26124 dw_die_ref context_die)
26126 tree decl_or_origin = decl ? decl : origin;
26127 tree class_origin = NULL, ultimate_origin;
26129 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26130 return NULL;
26132 switch (TREE_CODE (decl_or_origin))
26134 case ERROR_MARK:
26135 break;
26137 case CONST_DECL:
26138 if (!is_fortran () && !is_ada () && !is_dlang ())
26140 /* The individual enumerators of an enum type get output when we output
26141 the Dwarf representation of the relevant enum type itself. */
26142 break;
26145 /* Emit its type. */
26146 gen_type_die (TREE_TYPE (decl), context_die);
26148 /* And its containing namespace. */
26149 context_die = declare_in_namespace (decl, context_die);
26151 gen_const_die (decl, context_die);
26152 break;
26154 case FUNCTION_DECL:
26155 #if 0
26156 /* FIXME */
26157 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26158 on local redeclarations of global functions. That seems broken. */
26159 if (current_function_decl != decl)
26160 /* This is only a declaration. */;
26161 #endif
26163 /* We should have abstract copies already and should not generate
26164 stray type DIEs in late LTO dumping. */
26165 if (! early_dwarf)
26168 /* If we're emitting a clone, emit info for the abstract instance. */
26169 else if (origin || DECL_ORIGIN (decl) != decl)
26170 dwarf2out_abstract_function (origin
26171 ? DECL_ORIGIN (origin)
26172 : DECL_ABSTRACT_ORIGIN (decl));
26174 /* If we're emitting a possibly inlined function emit it as
26175 abstract instance. */
26176 else if (cgraph_function_possibly_inlined_p (decl)
26177 && ! DECL_ABSTRACT_P (decl)
26178 && ! class_or_namespace_scope_p (context_die)
26179 /* dwarf2out_abstract_function won't emit a die if this is just
26180 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26181 that case, because that works only if we have a die. */
26182 && DECL_INITIAL (decl) != NULL_TREE)
26183 dwarf2out_abstract_function (decl);
26185 /* Otherwise we're emitting the primary DIE for this decl. */
26186 else if (debug_info_level > DINFO_LEVEL_TERSE)
26188 /* Before we describe the FUNCTION_DECL itself, make sure that we
26189 have its containing type. */
26190 if (!origin)
26191 origin = decl_class_context (decl);
26192 if (origin != NULL_TREE)
26193 gen_type_die (origin, context_die);
26195 /* And its return type. */
26196 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26198 /* And its virtual context. */
26199 if (DECL_VINDEX (decl) != NULL_TREE)
26200 gen_type_die (DECL_CONTEXT (decl), context_die);
26202 /* Make sure we have a member DIE for decl. */
26203 if (origin != NULL_TREE)
26204 gen_type_die_for_member (origin, decl, context_die);
26206 /* And its containing namespace. */
26207 context_die = declare_in_namespace (decl, context_die);
26210 /* Now output a DIE to represent the function itself. */
26211 if (decl)
26212 gen_subprogram_die (decl, context_die);
26213 break;
26215 case TYPE_DECL:
26216 /* If we are in terse mode, don't generate any DIEs to represent any
26217 actual typedefs. */
26218 if (debug_info_level <= DINFO_LEVEL_TERSE)
26219 break;
26221 /* In the special case of a TYPE_DECL node representing the declaration
26222 of some type tag, if the given TYPE_DECL is marked as having been
26223 instantiated from some other (original) TYPE_DECL node (e.g. one which
26224 was generated within the original definition of an inline function) we
26225 used to generate a special (abbreviated) DW_TAG_structure_type,
26226 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26227 should be actually referencing those DIEs, as variable DIEs with that
26228 type would be emitted already in the abstract origin, so it was always
26229 removed during unused type prunning. Don't add anything in this
26230 case. */
26231 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26232 break;
26234 if (is_redundant_typedef (decl))
26235 gen_type_die (TREE_TYPE (decl), context_die);
26236 else
26237 /* Output a DIE to represent the typedef itself. */
26238 gen_typedef_die (decl, context_die);
26239 break;
26241 case LABEL_DECL:
26242 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26243 gen_label_die (decl, context_die);
26244 break;
26246 case VAR_DECL:
26247 case RESULT_DECL:
26248 /* If we are in terse mode, don't generate any DIEs to represent any
26249 variable declarations or definitions. */
26250 if (debug_info_level <= DINFO_LEVEL_TERSE)
26251 break;
26253 /* Avoid generating stray type DIEs during late dwarf dumping.
26254 All types have been dumped early. */
26255 if (early_dwarf
26256 /* ??? But in LTRANS we cannot annotate early created variably
26257 modified type DIEs without copying them and adjusting all
26258 references to them. Dump them again as happens for inlining
26259 which copies both the decl and the types. */
26260 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26261 in VLA bound information for example. */
26262 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26263 current_function_decl)))
26265 /* Output any DIEs that are needed to specify the type of this data
26266 object. */
26267 if (decl_by_reference_p (decl_or_origin))
26268 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26269 else
26270 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26273 if (early_dwarf)
26275 /* And its containing type. */
26276 class_origin = decl_class_context (decl_or_origin);
26277 if (class_origin != NULL_TREE)
26278 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26280 /* And its containing namespace. */
26281 context_die = declare_in_namespace (decl_or_origin, context_die);
26284 /* Now output the DIE to represent the data object itself. This gets
26285 complicated because of the possibility that the VAR_DECL really
26286 represents an inlined instance of a formal parameter for an inline
26287 function. */
26288 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26289 if (ultimate_origin != NULL_TREE
26290 && TREE_CODE (ultimate_origin) == PARM_DECL)
26291 gen_formal_parameter_die (decl, origin,
26292 true /* Emit name attribute. */,
26293 context_die);
26294 else
26295 gen_variable_die (decl, origin, context_die);
26296 break;
26298 case FIELD_DECL:
26299 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26300 /* Ignore the nameless fields that are used to skip bits but handle C++
26301 anonymous unions and structs. */
26302 if (DECL_NAME (decl) != NULL_TREE
26303 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26304 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26306 gen_type_die (member_declared_type (decl), context_die);
26307 gen_field_die (decl, ctx, context_die);
26309 break;
26311 case PARM_DECL:
26312 /* Avoid generating stray type DIEs during late dwarf dumping.
26313 All types have been dumped early. */
26314 if (early_dwarf
26315 /* ??? But in LTRANS we cannot annotate early created variably
26316 modified type DIEs without copying them and adjusting all
26317 references to them. Dump them again as happens for inlining
26318 which copies both the decl and the types. */
26319 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26320 in VLA bound information for example. */
26321 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26322 current_function_decl)))
26324 if (DECL_BY_REFERENCE (decl_or_origin))
26325 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26326 else
26327 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26329 return gen_formal_parameter_die (decl, origin,
26330 true /* Emit name attribute. */,
26331 context_die);
26333 case NAMESPACE_DECL:
26334 if (dwarf_version >= 3 || !dwarf_strict)
26335 gen_namespace_die (decl, context_die);
26336 break;
26338 case IMPORTED_DECL:
26339 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26340 DECL_CONTEXT (decl), context_die);
26341 break;
26343 case NAMELIST_DECL:
26344 gen_namelist_decl (DECL_NAME (decl), context_die,
26345 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26346 break;
26348 default:
26349 /* Probably some frontend-internal decl. Assume we don't care. */
26350 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26351 break;
26354 return NULL;
26357 /* Output initial debug information for global DECL. Called at the
26358 end of the parsing process.
26360 This is the initial debug generation process. As such, the DIEs
26361 generated may be incomplete. A later debug generation pass
26362 (dwarf2out_late_global_decl) will augment the information generated
26363 in this pass (e.g., with complete location info). */
26365 static void
26366 dwarf2out_early_global_decl (tree decl)
26368 set_early_dwarf s;
26370 /* gen_decl_die() will set DECL_ABSTRACT because
26371 cgraph_function_possibly_inlined_p() returns true. This is in
26372 turn will cause DW_AT_inline attributes to be set.
26374 This happens because at early dwarf generation, there is no
26375 cgraph information, causing cgraph_function_possibly_inlined_p()
26376 to return true. Trick cgraph_function_possibly_inlined_p()
26377 while we generate dwarf early. */
26378 bool save = symtab->global_info_ready;
26379 symtab->global_info_ready = true;
26381 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26382 other DECLs and they can point to template types or other things
26383 that dwarf2out can't handle when done via dwarf2out_decl. */
26384 if (TREE_CODE (decl) != TYPE_DECL
26385 && TREE_CODE (decl) != PARM_DECL)
26387 if (TREE_CODE (decl) == FUNCTION_DECL)
26389 tree save_fndecl = current_function_decl;
26391 /* For nested functions, make sure we have DIEs for the parents first
26392 so that all nested DIEs are generated at the proper scope in the
26393 first shot. */
26394 tree context = decl_function_context (decl);
26395 if (context != NULL)
26397 dw_die_ref context_die = lookup_decl_die (context);
26398 current_function_decl = context;
26400 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26401 enough so that it lands in its own context. This avoids type
26402 pruning issues later on. */
26403 if (context_die == NULL || is_declaration_die (context_die))
26404 dwarf2out_decl (context);
26407 /* Emit an abstract origin of a function first. This happens
26408 with C++ constructor clones for example and makes
26409 dwarf2out_abstract_function happy which requires the early
26410 DIE of the abstract instance to be present. */
26411 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26412 dw_die_ref origin_die;
26413 if (origin != NULL
26414 /* Do not emit the DIE multiple times but make sure to
26415 process it fully here in case we just saw a declaration. */
26416 && ((origin_die = lookup_decl_die (origin)) == NULL
26417 || is_declaration_die (origin_die)))
26419 current_function_decl = origin;
26420 dwarf2out_decl (origin);
26423 /* Emit the DIE for decl but avoid doing that multiple times. */
26424 dw_die_ref old_die;
26425 if ((old_die = lookup_decl_die (decl)) == NULL
26426 || is_declaration_die (old_die))
26428 current_function_decl = decl;
26429 dwarf2out_decl (decl);
26432 current_function_decl = save_fndecl;
26434 else
26435 dwarf2out_decl (decl);
26437 symtab->global_info_ready = save;
26440 /* Return whether EXPR is an expression with the following pattern:
26441 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26443 static bool
26444 is_trivial_indirect_ref (tree expr)
26446 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26447 return false;
26449 tree nop = TREE_OPERAND (expr, 0);
26450 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26451 return false;
26453 tree int_cst = TREE_OPERAND (nop, 0);
26454 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26457 /* Output debug information for global decl DECL. Called from
26458 toplev.c after compilation proper has finished. */
26460 static void
26461 dwarf2out_late_global_decl (tree decl)
26463 /* Fill-in any location information we were unable to determine
26464 on the first pass. */
26465 if (VAR_P (decl))
26467 dw_die_ref die = lookup_decl_die (decl);
26469 /* We may have to generate early debug late for LTO in case debug
26470 was not enabled at compile-time or the target doesn't support
26471 the LTO early debug scheme. */
26472 if (! die && in_lto_p)
26474 dwarf2out_decl (decl);
26475 die = lookup_decl_die (decl);
26478 if (die)
26480 /* We get called via the symtab code invoking late_global_decl
26481 for symbols that are optimized out.
26483 Do not add locations for those, except if they have a
26484 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26485 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26486 INDIRECT_REF expression, as this could generate relocations to
26487 text symbols in LTO object files, which is invalid. */
26488 varpool_node *node = varpool_node::get (decl);
26489 if ((! node || ! node->definition)
26490 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26491 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26492 tree_add_const_value_attribute_for_decl (die, decl);
26493 else
26494 add_location_or_const_value_attribute (die, decl, false);
26499 /* Output debug information for type decl DECL. Called from toplev.c
26500 and from language front ends (to record built-in types). */
26501 static void
26502 dwarf2out_type_decl (tree decl, int local)
26504 if (!local)
26506 set_early_dwarf s;
26507 dwarf2out_decl (decl);
26511 /* Output debug information for imported module or decl DECL.
26512 NAME is non-NULL name in the lexical block if the decl has been renamed.
26513 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26514 that DECL belongs to.
26515 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26516 static void
26517 dwarf2out_imported_module_or_decl_1 (tree decl,
26518 tree name,
26519 tree lexical_block,
26520 dw_die_ref lexical_block_die)
26522 expanded_location xloc;
26523 dw_die_ref imported_die = NULL;
26524 dw_die_ref at_import_die;
26526 if (TREE_CODE (decl) == IMPORTED_DECL)
26528 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26529 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26530 gcc_assert (decl);
26532 else
26533 xloc = expand_location (input_location);
26535 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26537 at_import_die = force_type_die (TREE_TYPE (decl));
26538 /* For namespace N { typedef void T; } using N::T; base_type_die
26539 returns NULL, but DW_TAG_imported_declaration requires
26540 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26541 if (!at_import_die)
26543 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26544 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26545 at_import_die = lookup_type_die (TREE_TYPE (decl));
26546 gcc_assert (at_import_die);
26549 else
26551 at_import_die = lookup_decl_die (decl);
26552 if (!at_import_die)
26554 /* If we're trying to avoid duplicate debug info, we may not have
26555 emitted the member decl for this field. Emit it now. */
26556 if (TREE_CODE (decl) == FIELD_DECL)
26558 tree type = DECL_CONTEXT (decl);
26560 if (TYPE_CONTEXT (type)
26561 && TYPE_P (TYPE_CONTEXT (type))
26562 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26563 DINFO_USAGE_DIR_USE))
26564 return;
26565 gen_type_die_for_member (type, decl,
26566 get_context_die (TYPE_CONTEXT (type)));
26568 if (TREE_CODE (decl) == NAMELIST_DECL)
26569 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26570 get_context_die (DECL_CONTEXT (decl)),
26571 NULL_TREE);
26572 else
26573 at_import_die = force_decl_die (decl);
26577 if (TREE_CODE (decl) == NAMESPACE_DECL)
26579 if (dwarf_version >= 3 || !dwarf_strict)
26580 imported_die = new_die (DW_TAG_imported_module,
26581 lexical_block_die,
26582 lexical_block);
26583 else
26584 return;
26586 else
26587 imported_die = new_die (DW_TAG_imported_declaration,
26588 lexical_block_die,
26589 lexical_block);
26591 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26592 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26593 if (debug_column_info && xloc.column)
26594 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26595 if (name)
26596 add_AT_string (imported_die, DW_AT_name,
26597 IDENTIFIER_POINTER (name));
26598 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26601 /* Output debug information for imported module or decl DECL.
26602 NAME is non-NULL name in context if the decl has been renamed.
26603 CHILD is true if decl is one of the renamed decls as part of
26604 importing whole module.
26605 IMPLICIT is set if this hook is called for an implicit import
26606 such as inline namespace. */
26608 static void
26609 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26610 bool child, bool implicit)
26612 /* dw_die_ref at_import_die; */
26613 dw_die_ref scope_die;
26615 if (debug_info_level <= DINFO_LEVEL_TERSE)
26616 return;
26618 gcc_assert (decl);
26620 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26621 should be enough, for DWARF4 and older even if we emit as extension
26622 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26623 for the benefit of consumers unaware of DW_AT_export_symbols. */
26624 if (implicit
26625 && dwarf_version >= 5
26626 && lang_hooks.decls.decl_dwarf_attribute (decl,
26627 DW_AT_export_symbols) == 1)
26628 return;
26630 set_early_dwarf s;
26632 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26633 We need decl DIE for reference and scope die. First, get DIE for the decl
26634 itself. */
26636 /* Get the scope die for decl context. Use comp_unit_die for global module
26637 or decl. If die is not found for non globals, force new die. */
26638 if (context
26639 && TYPE_P (context)
26640 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26641 return;
26643 scope_die = get_context_die (context);
26645 if (child)
26647 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26648 there is nothing we can do, here. */
26649 if (dwarf_version < 3 && dwarf_strict)
26650 return;
26652 gcc_assert (scope_die->die_child);
26653 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26654 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26655 scope_die = scope_die->die_child;
26658 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26659 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26662 /* Output debug information for namelists. */
26664 static dw_die_ref
26665 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26667 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26668 tree value;
26669 unsigned i;
26671 if (debug_info_level <= DINFO_LEVEL_TERSE)
26672 return NULL;
26674 gcc_assert (scope_die != NULL);
26675 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26676 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26678 /* If there are no item_decls, we have a nondefining namelist, e.g.
26679 with USE association; hence, set DW_AT_declaration. */
26680 if (item_decls == NULL_TREE)
26682 add_AT_flag (nml_die, DW_AT_declaration, 1);
26683 return nml_die;
26686 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26688 nml_item_ref_die = lookup_decl_die (value);
26689 if (!nml_item_ref_die)
26690 nml_item_ref_die = force_decl_die (value);
26692 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26693 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26695 return nml_die;
26699 /* Write the debugging output for DECL and return the DIE. */
26701 static void
26702 dwarf2out_decl (tree decl)
26704 dw_die_ref context_die = comp_unit_die ();
26706 switch (TREE_CODE (decl))
26708 case ERROR_MARK:
26709 return;
26711 case FUNCTION_DECL:
26712 /* If we're a nested function, initially use a parent of NULL; if we're
26713 a plain function, this will be fixed up in decls_for_scope. If
26714 we're a method, it will be ignored, since we already have a DIE.
26715 Avoid doing this late though since clones of class methods may
26716 otherwise end up in limbo and create type DIEs late. */
26717 if (early_dwarf
26718 && decl_function_context (decl)
26719 /* But if we're in terse mode, we don't care about scope. */
26720 && debug_info_level > DINFO_LEVEL_TERSE)
26721 context_die = NULL;
26722 break;
26724 case VAR_DECL:
26725 /* For local statics lookup proper context die. */
26726 if (local_function_static (decl))
26727 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26729 /* If we are in terse mode, don't generate any DIEs to represent any
26730 variable declarations or definitions. */
26731 if (debug_info_level <= DINFO_LEVEL_TERSE)
26732 return;
26733 break;
26735 case CONST_DECL:
26736 if (debug_info_level <= DINFO_LEVEL_TERSE)
26737 return;
26738 if (!is_fortran () && !is_ada () && !is_dlang ())
26739 return;
26740 if (TREE_STATIC (decl) && decl_function_context (decl))
26741 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26742 break;
26744 case NAMESPACE_DECL:
26745 case IMPORTED_DECL:
26746 if (debug_info_level <= DINFO_LEVEL_TERSE)
26747 return;
26748 if (lookup_decl_die (decl) != NULL)
26749 return;
26750 break;
26752 case TYPE_DECL:
26753 /* Don't emit stubs for types unless they are needed by other DIEs. */
26754 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26755 return;
26757 /* Don't bother trying to generate any DIEs to represent any of the
26758 normal built-in types for the language we are compiling. */
26759 if (DECL_IS_BUILTIN (decl))
26760 return;
26762 /* If we are in terse mode, don't generate any DIEs for types. */
26763 if (debug_info_level <= DINFO_LEVEL_TERSE)
26764 return;
26766 /* If we're a function-scope tag, initially use a parent of NULL;
26767 this will be fixed up in decls_for_scope. */
26768 if (decl_function_context (decl))
26769 context_die = NULL;
26771 break;
26773 case NAMELIST_DECL:
26774 break;
26776 default:
26777 return;
26780 gen_decl_die (decl, NULL, NULL, context_die);
26782 if (flag_checking)
26784 dw_die_ref die = lookup_decl_die (decl);
26785 if (die)
26786 check_die (die);
26790 /* Write the debugging output for DECL. */
26792 static void
26793 dwarf2out_function_decl (tree decl)
26795 dwarf2out_decl (decl);
26796 call_arg_locations = NULL;
26797 call_arg_loc_last = NULL;
26798 call_site_count = -1;
26799 tail_call_site_count = -1;
26800 decl_loc_table->empty ();
26801 cached_dw_loc_list_table->empty ();
26804 /* Output a marker (i.e. a label) for the beginning of the generated code for
26805 a lexical block. */
26807 static void
26808 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26809 unsigned int blocknum)
26811 switch_to_section (current_function_section ());
26812 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26815 /* Output a marker (i.e. a label) for the end of the generated code for a
26816 lexical block. */
26818 static void
26819 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26821 switch_to_section (current_function_section ());
26822 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26825 /* Returns nonzero if it is appropriate not to emit any debugging
26826 information for BLOCK, because it doesn't contain any instructions.
26828 Don't allow this for blocks with nested functions or local classes
26829 as we would end up with orphans, and in the presence of scheduling
26830 we may end up calling them anyway. */
26832 static bool
26833 dwarf2out_ignore_block (const_tree block)
26835 tree decl;
26836 unsigned int i;
26838 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26839 if (TREE_CODE (decl) == FUNCTION_DECL
26840 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26841 return 0;
26842 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26844 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26845 if (TREE_CODE (decl) == FUNCTION_DECL
26846 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26847 return 0;
26850 return 1;
26853 /* Hash table routines for file_hash. */
26855 bool
26856 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26858 return filename_cmp (p1->filename, p2) == 0;
26861 hashval_t
26862 dwarf_file_hasher::hash (dwarf_file_data *p)
26864 return htab_hash_string (p->filename);
26867 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26868 dwarf2out.c) and return its "index". The index of each (known) filename is
26869 just a unique number which is associated with only that one filename. We
26870 need such numbers for the sake of generating labels (in the .debug_sfnames
26871 section) and references to those files numbers (in the .debug_srcinfo
26872 and .debug_macinfo sections). If the filename given as an argument is not
26873 found in our current list, add it to the list and assign it the next
26874 available unique index number. */
26876 static struct dwarf_file_data *
26877 lookup_filename (const char *file_name)
26879 struct dwarf_file_data * created;
26881 if (!file_name)
26882 return NULL;
26884 dwarf_file_data **slot
26885 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26886 INSERT);
26887 if (*slot)
26888 return *slot;
26890 created = ggc_alloc<dwarf_file_data> ();
26891 created->filename = file_name;
26892 created->emitted_number = 0;
26893 *slot = created;
26894 return created;
26897 /* If the assembler will construct the file table, then translate the compiler
26898 internal file table number into the assembler file table number, and emit
26899 a .file directive if we haven't already emitted one yet. The file table
26900 numbers are different because we prune debug info for unused variables and
26901 types, which may include filenames. */
26903 static int
26904 maybe_emit_file (struct dwarf_file_data * fd)
26906 if (! fd->emitted_number)
26908 if (last_emitted_file)
26909 fd->emitted_number = last_emitted_file->emitted_number + 1;
26910 else
26911 fd->emitted_number = 1;
26912 last_emitted_file = fd;
26914 if (output_asm_line_debug_info ())
26916 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26917 output_quoted_string (asm_out_file,
26918 remap_debug_filename (fd->filename));
26919 fputc ('\n', asm_out_file);
26923 return fd->emitted_number;
26926 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26927 That generation should happen after function debug info has been
26928 generated. The value of the attribute is the constant value of ARG. */
26930 static void
26931 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26933 die_arg_entry entry;
26935 if (!die || !arg)
26936 return;
26938 gcc_assert (early_dwarf);
26940 if (!tmpl_value_parm_die_table)
26941 vec_alloc (tmpl_value_parm_die_table, 32);
26943 entry.die = die;
26944 entry.arg = arg;
26945 vec_safe_push (tmpl_value_parm_die_table, entry);
26948 /* Return TRUE if T is an instance of generic type, FALSE
26949 otherwise. */
26951 static bool
26952 generic_type_p (tree t)
26954 if (t == NULL_TREE || !TYPE_P (t))
26955 return false;
26956 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26959 /* Schedule the generation of the generic parameter dies for the
26960 instance of generic type T. The proper generation itself is later
26961 done by gen_scheduled_generic_parms_dies. */
26963 static void
26964 schedule_generic_params_dies_gen (tree t)
26966 if (!generic_type_p (t))
26967 return;
26969 gcc_assert (early_dwarf);
26971 if (!generic_type_instances)
26972 vec_alloc (generic_type_instances, 256);
26974 vec_safe_push (generic_type_instances, t);
26977 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26978 by append_entry_to_tmpl_value_parm_die_table. This function must
26979 be called after function DIEs have been generated. */
26981 static void
26982 gen_remaining_tmpl_value_param_die_attribute (void)
26984 if (tmpl_value_parm_die_table)
26986 unsigned i, j;
26987 die_arg_entry *e;
26989 /* We do this in two phases - first get the cases we can
26990 handle during early-finish, preserving those we cannot
26991 (containing symbolic constants where we don't yet know
26992 whether we are going to output the referenced symbols).
26993 For those we try again at late-finish. */
26994 j = 0;
26995 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26997 if (!e->die->removed
26998 && !tree_add_const_value_attribute (e->die, e->arg))
27000 dw_loc_descr_ref loc = NULL;
27001 if (! early_dwarf
27002 && (dwarf_version >= 5 || !dwarf_strict))
27003 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
27004 if (loc)
27005 add_AT_loc (e->die, DW_AT_location, loc);
27006 else
27007 (*tmpl_value_parm_die_table)[j++] = *e;
27010 tmpl_value_parm_die_table->truncate (j);
27014 /* Generate generic parameters DIEs for instances of generic types
27015 that have been previously scheduled by
27016 schedule_generic_params_dies_gen. This function must be called
27017 after all the types of the CU have been laid out. */
27019 static void
27020 gen_scheduled_generic_parms_dies (void)
27022 unsigned i;
27023 tree t;
27025 if (!generic_type_instances)
27026 return;
27028 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27029 if (COMPLETE_TYPE_P (t))
27030 gen_generic_params_dies (t);
27032 generic_type_instances = NULL;
27036 /* Replace DW_AT_name for the decl with name. */
27038 static void
27039 dwarf2out_set_name (tree decl, tree name)
27041 dw_die_ref die;
27042 dw_attr_node *attr;
27043 const char *dname;
27045 die = TYPE_SYMTAB_DIE (decl);
27046 if (!die)
27047 return;
27049 dname = dwarf2_name (name, 0);
27050 if (!dname)
27051 return;
27053 attr = get_AT (die, DW_AT_name);
27054 if (attr)
27056 struct indirect_string_node *node;
27058 node = find_AT_string (dname);
27059 /* replace the string. */
27060 attr->dw_attr_val.v.val_str = node;
27063 else
27064 add_name_attribute (die, dname);
27067 /* True if before or during processing of the first function being emitted. */
27068 static bool in_first_function_p = true;
27069 /* True if loc_note during dwarf2out_var_location call might still be
27070 before first real instruction at address equal to .Ltext0. */
27071 static bool maybe_at_text_label_p = true;
27072 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27073 static unsigned int first_loclabel_num_not_at_text_label;
27075 /* Look ahead for a real insn, or for a begin stmt marker. */
27077 static rtx_insn *
27078 dwarf2out_next_real_insn (rtx_insn *loc_note)
27080 rtx_insn *next_real = NEXT_INSN (loc_note);
27082 while (next_real)
27083 if (INSN_P (next_real))
27084 break;
27085 else
27086 next_real = NEXT_INSN (next_real);
27088 return next_real;
27091 /* Called by the final INSN scan whenever we see a var location. We
27092 use it to drop labels in the right places, and throw the location in
27093 our lookup table. */
27095 static void
27096 dwarf2out_var_location (rtx_insn *loc_note)
27098 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27099 struct var_loc_node *newloc;
27100 rtx_insn *next_real, *next_note;
27101 rtx_insn *call_insn = NULL;
27102 static const char *last_label;
27103 static const char *last_postcall_label;
27104 static bool last_in_cold_section_p;
27105 static rtx_insn *expected_next_loc_note;
27106 tree decl;
27107 bool var_loc_p;
27108 var_loc_view view = 0;
27110 if (!NOTE_P (loc_note))
27112 if (CALL_P (loc_note))
27114 maybe_reset_location_view (loc_note, cur_line_info_table);
27115 call_site_count++;
27116 if (SIBLING_CALL_P (loc_note))
27117 tail_call_site_count++;
27118 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27120 call_insn = loc_note;
27121 loc_note = NULL;
27122 var_loc_p = false;
27124 next_real = dwarf2out_next_real_insn (call_insn);
27125 next_note = NULL;
27126 cached_next_real_insn = NULL;
27127 goto create_label;
27129 if (optimize == 0 && !flag_var_tracking)
27131 /* When the var-tracking pass is not running, there is no note
27132 for indirect calls whose target is compile-time known. In this
27133 case, process such calls specifically so that we generate call
27134 sites for them anyway. */
27135 rtx x = PATTERN (loc_note);
27136 if (GET_CODE (x) == PARALLEL)
27137 x = XVECEXP (x, 0, 0);
27138 if (GET_CODE (x) == SET)
27139 x = SET_SRC (x);
27140 if (GET_CODE (x) == CALL)
27141 x = XEXP (x, 0);
27142 if (!MEM_P (x)
27143 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27144 || !SYMBOL_REF_DECL (XEXP (x, 0))
27145 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27146 != FUNCTION_DECL))
27148 call_insn = loc_note;
27149 loc_note = NULL;
27150 var_loc_p = false;
27152 next_real = dwarf2out_next_real_insn (call_insn);
27153 next_note = NULL;
27154 cached_next_real_insn = NULL;
27155 goto create_label;
27159 else if (!debug_variable_location_views)
27160 gcc_unreachable ();
27161 else
27162 maybe_reset_location_view (loc_note, cur_line_info_table);
27164 return;
27167 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27168 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27169 return;
27171 /* Optimize processing a large consecutive sequence of location
27172 notes so we don't spend too much time in next_real_insn. If the
27173 next insn is another location note, remember the next_real_insn
27174 calculation for next time. */
27175 next_real = cached_next_real_insn;
27176 if (next_real)
27178 if (expected_next_loc_note != loc_note)
27179 next_real = NULL;
27182 next_note = NEXT_INSN (loc_note);
27183 if (! next_note
27184 || next_note->deleted ()
27185 || ! NOTE_P (next_note)
27186 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27187 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27188 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27189 next_note = NULL;
27191 if (! next_real)
27192 next_real = dwarf2out_next_real_insn (loc_note);
27194 if (next_note)
27196 expected_next_loc_note = next_note;
27197 cached_next_real_insn = next_real;
27199 else
27200 cached_next_real_insn = NULL;
27202 /* If there are no instructions which would be affected by this note,
27203 don't do anything. */
27204 if (var_loc_p
27205 && next_real == NULL_RTX
27206 && !NOTE_DURING_CALL_P (loc_note))
27207 return;
27209 create_label:
27211 if (next_real == NULL_RTX)
27212 next_real = get_last_insn ();
27214 /* If there were any real insns between note we processed last time
27215 and this note (or if it is the first note), clear
27216 last_{,postcall_}label so that they are not reused this time. */
27217 if (last_var_location_insn == NULL_RTX
27218 || last_var_location_insn != next_real
27219 || last_in_cold_section_p != in_cold_section_p)
27221 last_label = NULL;
27222 last_postcall_label = NULL;
27225 if (var_loc_p)
27227 const char *label
27228 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27229 view = cur_line_info_table->view;
27230 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27231 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27232 if (newloc == NULL)
27233 return;
27235 else
27237 decl = NULL_TREE;
27238 newloc = NULL;
27241 /* If there were no real insns between note we processed last time
27242 and this note, use the label we emitted last time. Otherwise
27243 create a new label and emit it. */
27244 if (last_label == NULL)
27246 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27247 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27248 loclabel_num++;
27249 last_label = ggc_strdup (loclabel);
27250 /* See if loclabel might be equal to .Ltext0. If yes,
27251 bump first_loclabel_num_not_at_text_label. */
27252 if (!have_multiple_function_sections
27253 && in_first_function_p
27254 && maybe_at_text_label_p)
27256 static rtx_insn *last_start;
27257 rtx_insn *insn;
27258 for (insn = loc_note; insn; insn = previous_insn (insn))
27259 if (insn == last_start)
27260 break;
27261 else if (!NONDEBUG_INSN_P (insn))
27262 continue;
27263 else
27265 rtx body = PATTERN (insn);
27266 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27267 continue;
27268 /* Inline asm could occupy zero bytes. */
27269 else if (GET_CODE (body) == ASM_INPUT
27270 || asm_noperands (body) >= 0)
27271 continue;
27272 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27273 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27274 continue;
27275 #endif
27276 else
27278 /* Assume insn has non-zero length. */
27279 maybe_at_text_label_p = false;
27280 break;
27283 if (maybe_at_text_label_p)
27285 last_start = loc_note;
27286 first_loclabel_num_not_at_text_label = loclabel_num;
27291 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27292 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27294 if (!var_loc_p)
27296 struct call_arg_loc_node *ca_loc
27297 = ggc_cleared_alloc<call_arg_loc_node> ();
27298 rtx_insn *prev = call_insn;
27300 ca_loc->call_arg_loc_note
27301 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27302 ca_loc->next = NULL;
27303 ca_loc->label = last_label;
27304 gcc_assert (prev
27305 && (CALL_P (prev)
27306 || (NONJUMP_INSN_P (prev)
27307 && GET_CODE (PATTERN (prev)) == SEQUENCE
27308 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27309 if (!CALL_P (prev))
27310 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27311 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27313 /* Look for a SYMBOL_REF in the "prev" instruction. */
27314 rtx x = get_call_rtx_from (PATTERN (prev));
27315 if (x)
27317 /* Try to get the call symbol, if any. */
27318 if (MEM_P (XEXP (x, 0)))
27319 x = XEXP (x, 0);
27320 /* First, look for a memory access to a symbol_ref. */
27321 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27322 && SYMBOL_REF_DECL (XEXP (x, 0))
27323 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27324 ca_loc->symbol_ref = XEXP (x, 0);
27325 /* Otherwise, look at a compile-time known user-level function
27326 declaration. */
27327 else if (MEM_P (x)
27328 && MEM_EXPR (x)
27329 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27330 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27333 ca_loc->block = insn_scope (prev);
27334 if (call_arg_locations)
27335 call_arg_loc_last->next = ca_loc;
27336 else
27337 call_arg_locations = ca_loc;
27338 call_arg_loc_last = ca_loc;
27340 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27342 newloc->label = last_label;
27343 newloc->view = view;
27345 else
27347 if (!last_postcall_label)
27349 sprintf (loclabel, "%s-1", last_label);
27350 last_postcall_label = ggc_strdup (loclabel);
27352 newloc->label = last_postcall_label;
27353 /* ??? This view is at last_label, not last_label-1, but we
27354 could only assume view at last_label-1 is zero if we could
27355 assume calls always have length greater than one. This is
27356 probably true in general, though there might be a rare
27357 exception to this rule, e.g. if a call insn is optimized out
27358 by target magic. Then, even the -1 in the label will be
27359 wrong, which might invalidate the range. Anyway, using view,
27360 though technically possibly incorrect, will work as far as
27361 ranges go: since L-1 is in the middle of the call insn,
27362 (L-1).0 and (L-1).V shouldn't make any difference, and having
27363 the loclist entry refer to the .loc entry might be useful, so
27364 leave it like this. */
27365 newloc->view = view;
27368 if (var_loc_p && flag_debug_asm)
27370 const char *name, *sep, *patstr;
27371 if (decl && DECL_NAME (decl))
27372 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27373 else
27374 name = "";
27375 if (NOTE_VAR_LOCATION_LOC (loc_note))
27377 sep = " => ";
27378 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27380 else
27382 sep = " ";
27383 patstr = "RESET";
27385 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27386 name, sep, patstr);
27389 last_var_location_insn = next_real;
27390 last_in_cold_section_p = in_cold_section_p;
27393 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27394 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27395 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27396 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27397 BLOCK_FRAGMENT_ORIGIN links. */
27398 static bool
27399 block_within_block_p (tree block, tree outer, bool bothways)
27401 if (block == outer)
27402 return true;
27404 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27405 for (tree context = BLOCK_SUPERCONTEXT (block);
27406 context != outer;
27407 context = BLOCK_SUPERCONTEXT (context))
27408 if (!context || TREE_CODE (context) != BLOCK)
27409 return false;
27411 if (!bothways)
27412 return true;
27414 /* Now check that each block is actually referenced by its
27415 parent. */
27416 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27417 context = BLOCK_SUPERCONTEXT (context))
27419 if (BLOCK_FRAGMENT_ORIGIN (context))
27421 gcc_assert (!BLOCK_SUBBLOCKS (context));
27422 context = BLOCK_FRAGMENT_ORIGIN (context);
27424 for (tree sub = BLOCK_SUBBLOCKS (context);
27425 sub != block;
27426 sub = BLOCK_CHAIN (sub))
27427 if (!sub)
27428 return false;
27429 if (context == outer)
27430 return true;
27431 else
27432 block = context;
27436 /* Called during final while assembling the marker of the entry point
27437 for an inlined function. */
27439 static void
27440 dwarf2out_inline_entry (tree block)
27442 gcc_assert (debug_inline_points);
27444 /* If we can't represent it, don't bother. */
27445 if (!(dwarf_version >= 3 || !dwarf_strict))
27446 return;
27448 gcc_assert (DECL_P (block_ultimate_origin (block)));
27450 /* Sanity check the block tree. This would catch a case in which
27451 BLOCK got removed from the tree reachable from the outermost
27452 lexical block, but got retained in markers. It would still link
27453 back to its parents, but some ancestor would be missing a link
27454 down the path to the sub BLOCK. If the block got removed, its
27455 BLOCK_NUMBER will not be a usable value. */
27456 if (flag_checking)
27457 gcc_assert (block_within_block_p (block,
27458 DECL_INITIAL (current_function_decl),
27459 true));
27461 gcc_assert (inlined_function_outer_scope_p (block));
27462 gcc_assert (!lookup_block_die (block));
27464 if (BLOCK_FRAGMENT_ORIGIN (block))
27465 block = BLOCK_FRAGMENT_ORIGIN (block);
27466 /* Can the entry point ever not be at the beginning of an
27467 unfragmented lexical block? */
27468 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27469 || (cur_line_info_table
27470 && !ZERO_VIEW_P (cur_line_info_table->view))))
27471 return;
27473 if (!inline_entry_data_table)
27474 inline_entry_data_table
27475 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27478 inline_entry_data **iedp
27479 = inline_entry_data_table->find_slot_with_hash (block,
27480 htab_hash_pointer (block),
27481 INSERT);
27482 if (*iedp)
27483 /* ??? Ideally, we'd record all entry points for the same inlined
27484 function (some may have been duplicated by e.g. unrolling), but
27485 we have no way to represent that ATM. */
27486 return;
27488 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27489 ied->block = block;
27490 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27491 ied->label_num = BLOCK_NUMBER (block);
27492 if (cur_line_info_table)
27493 ied->view = cur_line_info_table->view;
27495 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27497 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27498 BLOCK_NUMBER (block));
27499 ASM_OUTPUT_LABEL (asm_out_file, label);
27502 /* Called from finalize_size_functions for size functions so that their body
27503 can be encoded in the debug info to describe the layout of variable-length
27504 structures. */
27506 static void
27507 dwarf2out_size_function (tree decl)
27509 function_to_dwarf_procedure (decl);
27512 /* Note in one location list that text section has changed. */
27515 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27517 var_loc_list *list = *slot;
27518 if (list->first)
27519 list->last_before_switch
27520 = list->last->next ? list->last->next : list->last;
27521 return 1;
27524 /* Note in all location lists that text section has changed. */
27526 static void
27527 var_location_switch_text_section (void)
27529 if (decl_loc_table == NULL)
27530 return;
27532 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27535 /* Create a new line number table. */
27537 static dw_line_info_table *
27538 new_line_info_table (void)
27540 dw_line_info_table *table;
27542 table = ggc_cleared_alloc<dw_line_info_table> ();
27543 table->file_num = 1;
27544 table->line_num = 1;
27545 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27546 FORCE_RESET_NEXT_VIEW (table->view);
27547 table->symviews_since_reset = 0;
27549 return table;
27552 /* Lookup the "current" table into which we emit line info, so
27553 that we don't have to do it for every source line. */
27555 static void
27556 set_cur_line_info_table (section *sec)
27558 dw_line_info_table *table;
27560 if (sec == text_section)
27561 table = text_section_line_info;
27562 else if (sec == cold_text_section)
27564 table = cold_text_section_line_info;
27565 if (!table)
27567 cold_text_section_line_info = table = new_line_info_table ();
27568 table->end_label = cold_end_label;
27571 else
27573 const char *end_label;
27575 if (crtl->has_bb_partition)
27577 if (in_cold_section_p)
27578 end_label = crtl->subsections.cold_section_end_label;
27579 else
27580 end_label = crtl->subsections.hot_section_end_label;
27582 else
27584 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27585 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27586 current_function_funcdef_no);
27587 end_label = ggc_strdup (label);
27590 table = new_line_info_table ();
27591 table->end_label = end_label;
27593 vec_safe_push (separate_line_info, table);
27596 if (output_asm_line_debug_info ())
27597 table->is_stmt = (cur_line_info_table
27598 ? cur_line_info_table->is_stmt
27599 : DWARF_LINE_DEFAULT_IS_STMT_START);
27600 cur_line_info_table = table;
27604 /* We need to reset the locations at the beginning of each
27605 function. We can't do this in the end_function hook, because the
27606 declarations that use the locations won't have been output when
27607 that hook is called. Also compute have_multiple_function_sections here. */
27609 static void
27610 dwarf2out_begin_function (tree fun)
27612 section *sec = function_section (fun);
27614 if (sec != text_section)
27615 have_multiple_function_sections = true;
27617 if (crtl->has_bb_partition && !cold_text_section)
27619 gcc_assert (current_function_decl == fun);
27620 cold_text_section = unlikely_text_section ();
27621 switch_to_section (cold_text_section);
27622 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27623 switch_to_section (sec);
27626 dwarf2out_note_section_used ();
27627 call_site_count = 0;
27628 tail_call_site_count = 0;
27630 set_cur_line_info_table (sec);
27631 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27634 /* Helper function of dwarf2out_end_function, called only after emitting
27635 the very first function into assembly. Check if some .debug_loc range
27636 might end with a .LVL* label that could be equal to .Ltext0.
27637 In that case we must force using absolute addresses in .debug_loc ranges,
27638 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27639 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27640 list terminator.
27641 Set have_multiple_function_sections to true in that case and
27642 terminate htab traversal. */
27645 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27647 var_loc_list *entry = *slot;
27648 struct var_loc_node *node;
27650 node = entry->first;
27651 if (node && node->next && node->next->label)
27653 unsigned int i;
27654 const char *label = node->next->label;
27655 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27657 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27659 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27660 if (strcmp (label, loclabel) == 0)
27662 have_multiple_function_sections = true;
27663 return 0;
27667 return 1;
27670 /* Hook called after emitting a function into assembly.
27671 This does something only for the very first function emitted. */
27673 static void
27674 dwarf2out_end_function (unsigned int)
27676 if (in_first_function_p
27677 && !have_multiple_function_sections
27678 && first_loclabel_num_not_at_text_label
27679 && decl_loc_table)
27680 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27681 in_first_function_p = false;
27682 maybe_at_text_label_p = false;
27685 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27686 front-ends register a translation unit even before dwarf2out_init is
27687 called. */
27688 static tree main_translation_unit = NULL_TREE;
27690 /* Hook called by front-ends after they built their main translation unit.
27691 Associate comp_unit_die to UNIT. */
27693 static void
27694 dwarf2out_register_main_translation_unit (tree unit)
27696 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27697 && main_translation_unit == NULL_TREE);
27698 main_translation_unit = unit;
27699 /* If dwarf2out_init has not been called yet, it will perform the association
27700 itself looking at main_translation_unit. */
27701 if (decl_die_table != NULL)
27702 equate_decl_number_to_die (unit, comp_unit_die ());
27705 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27707 static void
27708 push_dw_line_info_entry (dw_line_info_table *table,
27709 enum dw_line_info_opcode opcode, unsigned int val)
27711 dw_line_info_entry e;
27712 e.opcode = opcode;
27713 e.val = val;
27714 vec_safe_push (table->entries, e);
27717 /* Output a label to mark the beginning of a source code line entry
27718 and record information relating to this source line, in
27719 'line_info_table' for later output of the .debug_line section. */
27720 /* ??? The discriminator parameter ought to be unsigned. */
27722 static void
27723 dwarf2out_source_line (unsigned int line, unsigned int column,
27724 const char *filename,
27725 int discriminator, bool is_stmt)
27727 unsigned int file_num;
27728 dw_line_info_table *table;
27729 static var_loc_view lvugid;
27731 if (debug_info_level < DINFO_LEVEL_TERSE)
27732 return;
27734 table = cur_line_info_table;
27736 if (line == 0)
27738 if (debug_variable_location_views
27739 && output_asm_line_debug_info ()
27740 && table && !RESETTING_VIEW_P (table->view))
27742 /* If we're using the assembler to compute view numbers, we
27743 can't issue a .loc directive for line zero, so we can't
27744 get a view number at this point. We might attempt to
27745 compute it from the previous view, or equate it to a
27746 subsequent view (though it might not be there!), but
27747 since we're omitting the line number entry, we might as
27748 well omit the view number as well. That means pretending
27749 it's a view number zero, which might very well turn out
27750 to be correct. ??? Extend the assembler so that the
27751 compiler could emit e.g. ".locview .LVU#", to output a
27752 view without changing line number information. We'd then
27753 have to count it in symviews_since_reset; when it's omitted,
27754 it doesn't count. */
27755 if (!zero_view_p)
27756 zero_view_p = BITMAP_GGC_ALLOC ();
27757 bitmap_set_bit (zero_view_p, table->view);
27758 if (flag_debug_asm)
27760 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27761 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27762 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27763 ASM_COMMENT_START);
27764 assemble_name (asm_out_file, label);
27765 putc ('\n', asm_out_file);
27767 table->view = ++lvugid;
27769 return;
27772 /* The discriminator column was added in dwarf4. Simplify the below
27773 by simply removing it if we're not supposed to output it. */
27774 if (dwarf_version < 4 && dwarf_strict)
27775 discriminator = 0;
27777 if (!debug_column_info)
27778 column = 0;
27780 file_num = maybe_emit_file (lookup_filename (filename));
27782 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27783 the debugger has used the second (possibly duplicate) line number
27784 at the beginning of the function to mark the end of the prologue.
27785 We could eliminate any other duplicates within the function. For
27786 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27787 that second line number entry. */
27788 /* Recall that this end-of-prologue indication is *not* the same thing
27789 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27790 to which the hook corresponds, follows the last insn that was
27791 emitted by gen_prologue. What we need is to precede the first insn
27792 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27793 insn that corresponds to something the user wrote. These may be
27794 very different locations once scheduling is enabled. */
27796 if (0 && file_num == table->file_num
27797 && line == table->line_num
27798 && column == table->column_num
27799 && discriminator == table->discrim_num
27800 && is_stmt == table->is_stmt)
27801 return;
27803 switch_to_section (current_function_section ());
27805 /* If requested, emit something human-readable. */
27806 if (flag_debug_asm)
27808 if (debug_column_info)
27809 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27810 filename, line, column);
27811 else
27812 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27813 filename, line);
27816 if (output_asm_line_debug_info ())
27818 /* Emit the .loc directive understood by GNU as. */
27819 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27820 file_num, line, is_stmt, discriminator */
27821 fputs ("\t.loc ", asm_out_file);
27822 fprint_ul (asm_out_file, file_num);
27823 putc (' ', asm_out_file);
27824 fprint_ul (asm_out_file, line);
27825 putc (' ', asm_out_file);
27826 fprint_ul (asm_out_file, column);
27828 if (is_stmt != table->is_stmt)
27830 #if HAVE_GAS_LOC_STMT
27831 fputs (" is_stmt ", asm_out_file);
27832 putc (is_stmt ? '1' : '0', asm_out_file);
27833 #endif
27835 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27837 gcc_assert (discriminator > 0);
27838 fputs (" discriminator ", asm_out_file);
27839 fprint_ul (asm_out_file, (unsigned long) discriminator);
27841 if (debug_variable_location_views)
27843 if (!RESETTING_VIEW_P (table->view))
27845 table->symviews_since_reset++;
27846 if (table->symviews_since_reset > symview_upper_bound)
27847 symview_upper_bound = table->symviews_since_reset;
27848 /* When we're using the assembler to compute view
27849 numbers, we output symbolic labels after "view" in
27850 .loc directives, and the assembler will set them for
27851 us, so that we can refer to the view numbers in
27852 location lists. The only exceptions are when we know
27853 a view will be zero: "-0" is a forced reset, used
27854 e.g. in the beginning of functions, whereas "0" tells
27855 the assembler to check that there was a PC change
27856 since the previous view, in a way that implicitly
27857 resets the next view. */
27858 fputs (" view ", asm_out_file);
27859 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27860 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27861 assemble_name (asm_out_file, label);
27862 table->view = ++lvugid;
27864 else
27866 table->symviews_since_reset = 0;
27867 if (FORCE_RESETTING_VIEW_P (table->view))
27868 fputs (" view -0", asm_out_file);
27869 else
27870 fputs (" view 0", asm_out_file);
27871 /* Mark the present view as a zero view. Earlier debug
27872 binds may have already added its id to loclists to be
27873 emitted later, so we can't reuse the id for something
27874 else. However, it's good to know whether a view is
27875 known to be zero, because then we may be able to
27876 optimize out locviews that are all zeros, so take
27877 note of it in zero_view_p. */
27878 if (!zero_view_p)
27879 zero_view_p = BITMAP_GGC_ALLOC ();
27880 bitmap_set_bit (zero_view_p, lvugid);
27881 table->view = ++lvugid;
27884 putc ('\n', asm_out_file);
27886 else
27888 unsigned int label_num = ++line_info_label_num;
27890 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27892 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
27893 push_dw_line_info_entry (table, LI_adv_address, label_num);
27894 else
27895 push_dw_line_info_entry (table, LI_set_address, label_num);
27896 if (debug_variable_location_views)
27898 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
27899 if (resetting)
27900 table->view = 0;
27902 if (flag_debug_asm)
27903 fprintf (asm_out_file, "\t%s view %s%d\n",
27904 ASM_COMMENT_START,
27905 resetting ? "-" : "",
27906 table->view);
27908 table->view++;
27910 if (file_num != table->file_num)
27911 push_dw_line_info_entry (table, LI_set_file, file_num);
27912 if (discriminator != table->discrim_num)
27913 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27914 if (is_stmt != table->is_stmt)
27915 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27916 push_dw_line_info_entry (table, LI_set_line, line);
27917 if (debug_column_info)
27918 push_dw_line_info_entry (table, LI_set_column, column);
27921 table->file_num = file_num;
27922 table->line_num = line;
27923 table->column_num = column;
27924 table->discrim_num = discriminator;
27925 table->is_stmt = is_stmt;
27926 table->in_use = true;
27929 /* Record the beginning of a new source file. */
27931 static void
27932 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27934 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27936 macinfo_entry e;
27937 e.code = DW_MACINFO_start_file;
27938 e.lineno = lineno;
27939 e.info = ggc_strdup (filename);
27940 vec_safe_push (macinfo_table, e);
27944 /* Record the end of a source file. */
27946 static void
27947 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27949 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27951 macinfo_entry e;
27952 e.code = DW_MACINFO_end_file;
27953 e.lineno = lineno;
27954 e.info = NULL;
27955 vec_safe_push (macinfo_table, e);
27959 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27960 the tail part of the directive line, i.e. the part which is past the
27961 initial whitespace, #, whitespace, directive-name, whitespace part. */
27963 static void
27964 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27965 const char *buffer ATTRIBUTE_UNUSED)
27967 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27969 macinfo_entry e;
27970 /* Insert a dummy first entry to be able to optimize the whole
27971 predefined macro block using DW_MACRO_import. */
27972 if (macinfo_table->is_empty () && lineno <= 1)
27974 e.code = 0;
27975 e.lineno = 0;
27976 e.info = NULL;
27977 vec_safe_push (macinfo_table, e);
27979 e.code = DW_MACINFO_define;
27980 e.lineno = lineno;
27981 e.info = ggc_strdup (buffer);
27982 vec_safe_push (macinfo_table, e);
27986 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27987 the tail part of the directive line, i.e. the part which is past the
27988 initial whitespace, #, whitespace, directive-name, whitespace part. */
27990 static void
27991 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27992 const char *buffer ATTRIBUTE_UNUSED)
27994 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27996 macinfo_entry e;
27997 /* Insert a dummy first entry to be able to optimize the whole
27998 predefined macro block using DW_MACRO_import. */
27999 if (macinfo_table->is_empty () && lineno <= 1)
28001 e.code = 0;
28002 e.lineno = 0;
28003 e.info = NULL;
28004 vec_safe_push (macinfo_table, e);
28006 e.code = DW_MACINFO_undef;
28007 e.lineno = lineno;
28008 e.info = ggc_strdup (buffer);
28009 vec_safe_push (macinfo_table, e);
28013 /* Helpers to manipulate hash table of CUs. */
28015 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
28017 static inline hashval_t hash (const macinfo_entry *);
28018 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
28021 inline hashval_t
28022 macinfo_entry_hasher::hash (const macinfo_entry *entry)
28024 return htab_hash_string (entry->info);
28027 inline bool
28028 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28029 const macinfo_entry *entry2)
28031 return !strcmp (entry1->info, entry2->info);
28034 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28036 /* Output a single .debug_macinfo entry. */
28038 static void
28039 output_macinfo_op (macinfo_entry *ref)
28041 int file_num;
28042 size_t len;
28043 struct indirect_string_node *node;
28044 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28045 struct dwarf_file_data *fd;
28047 switch (ref->code)
28049 case DW_MACINFO_start_file:
28050 fd = lookup_filename (ref->info);
28051 file_num = maybe_emit_file (fd);
28052 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28053 dw2_asm_output_data_uleb128 (ref->lineno,
28054 "Included from line number %lu",
28055 (unsigned long) ref->lineno);
28056 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28057 break;
28058 case DW_MACINFO_end_file:
28059 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28060 break;
28061 case DW_MACINFO_define:
28062 case DW_MACINFO_undef:
28063 len = strlen (ref->info) + 1;
28064 if (!dwarf_strict
28065 && len > DWARF_OFFSET_SIZE
28066 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28067 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28069 ref->code = ref->code == DW_MACINFO_define
28070 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28071 output_macinfo_op (ref);
28072 return;
28074 dw2_asm_output_data (1, ref->code,
28075 ref->code == DW_MACINFO_define
28076 ? "Define macro" : "Undefine macro");
28077 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28078 (unsigned long) ref->lineno);
28079 dw2_asm_output_nstring (ref->info, -1, "The macro");
28080 break;
28081 case DW_MACRO_define_strp:
28082 case DW_MACRO_undef_strp:
28083 node = find_AT_string (ref->info);
28084 gcc_assert (node
28085 && (node->form == DW_FORM_strp
28086 || node->form == dwarf_FORM (DW_FORM_strx)));
28087 dw2_asm_output_data (1, ref->code,
28088 ref->code == DW_MACRO_define_strp
28089 ? "Define macro strp"
28090 : "Undefine macro strp");
28091 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28092 (unsigned long) ref->lineno);
28093 if (node->form == DW_FORM_strp)
28094 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28095 debug_str_section, "The macro: \"%s\"",
28096 ref->info);
28097 else
28098 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28099 ref->info);
28100 break;
28101 case DW_MACRO_import:
28102 dw2_asm_output_data (1, ref->code, "Import");
28103 ASM_GENERATE_INTERNAL_LABEL (label,
28104 DEBUG_MACRO_SECTION_LABEL,
28105 ref->lineno + macinfo_label_base);
28106 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28107 break;
28108 default:
28109 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28110 ASM_COMMENT_START, (unsigned long) ref->code);
28111 break;
28115 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28116 other compilation unit .debug_macinfo sections. IDX is the first
28117 index of a define/undef, return the number of ops that should be
28118 emitted in a comdat .debug_macinfo section and emit
28119 a DW_MACRO_import entry referencing it.
28120 If the define/undef entry should be emitted normally, return 0. */
28122 static unsigned
28123 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28124 macinfo_hash_type **macinfo_htab)
28126 macinfo_entry *first, *second, *cur, *inc;
28127 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28128 unsigned char checksum[16];
28129 struct md5_ctx ctx;
28130 char *grp_name, *tail;
28131 const char *base;
28132 unsigned int i, count, encoded_filename_len, linebuf_len;
28133 macinfo_entry **slot;
28135 first = &(*macinfo_table)[idx];
28136 second = &(*macinfo_table)[idx + 1];
28138 /* Optimize only if there are at least two consecutive define/undef ops,
28139 and either all of them are before first DW_MACINFO_start_file
28140 with lineno {0,1} (i.e. predefined macro block), or all of them are
28141 in some included header file. */
28142 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28143 return 0;
28144 if (vec_safe_is_empty (files))
28146 if (first->lineno > 1 || second->lineno > 1)
28147 return 0;
28149 else if (first->lineno == 0)
28150 return 0;
28152 /* Find the last define/undef entry that can be grouped together
28153 with first and at the same time compute md5 checksum of their
28154 codes, linenumbers and strings. */
28155 md5_init_ctx (&ctx);
28156 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28157 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28158 break;
28159 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28160 break;
28161 else
28163 unsigned char code = cur->code;
28164 md5_process_bytes (&code, 1, &ctx);
28165 checksum_uleb128 (cur->lineno, &ctx);
28166 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28168 md5_finish_ctx (&ctx, checksum);
28169 count = i - idx;
28171 /* From the containing include filename (if any) pick up just
28172 usable characters from its basename. */
28173 if (vec_safe_is_empty (files))
28174 base = "";
28175 else
28176 base = lbasename (files->last ().info);
28177 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28178 if (ISIDNUM (base[i]) || base[i] == '.')
28179 encoded_filename_len++;
28180 /* Count . at the end. */
28181 if (encoded_filename_len)
28182 encoded_filename_len++;
28184 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28185 linebuf_len = strlen (linebuf);
28187 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28188 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28189 + 16 * 2 + 1);
28190 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28191 tail = grp_name + 4;
28192 if (encoded_filename_len)
28194 for (i = 0; base[i]; i++)
28195 if (ISIDNUM (base[i]) || base[i] == '.')
28196 *tail++ = base[i];
28197 *tail++ = '.';
28199 memcpy (tail, linebuf, linebuf_len);
28200 tail += linebuf_len;
28201 *tail++ = '.';
28202 for (i = 0; i < 16; i++)
28203 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28205 /* Construct a macinfo_entry for DW_MACRO_import
28206 in the empty vector entry before the first define/undef. */
28207 inc = &(*macinfo_table)[idx - 1];
28208 inc->code = DW_MACRO_import;
28209 inc->lineno = 0;
28210 inc->info = ggc_strdup (grp_name);
28211 if (!*macinfo_htab)
28212 *macinfo_htab = new macinfo_hash_type (10);
28213 /* Avoid emitting duplicates. */
28214 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28215 if (*slot != NULL)
28217 inc->code = 0;
28218 inc->info = NULL;
28219 /* If such an entry has been used before, just emit
28220 a DW_MACRO_import op. */
28221 inc = *slot;
28222 output_macinfo_op (inc);
28223 /* And clear all macinfo_entry in the range to avoid emitting them
28224 in the second pass. */
28225 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28227 cur->code = 0;
28228 cur->info = NULL;
28231 else
28233 *slot = inc;
28234 inc->lineno = (*macinfo_htab)->elements ();
28235 output_macinfo_op (inc);
28237 return count;
28240 /* Save any strings needed by the macinfo table in the debug str
28241 table. All strings must be collected into the table by the time
28242 index_string is called. */
28244 static void
28245 save_macinfo_strings (void)
28247 unsigned len;
28248 unsigned i;
28249 macinfo_entry *ref;
28251 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28253 switch (ref->code)
28255 /* Match the logic in output_macinfo_op to decide on
28256 indirect strings. */
28257 case DW_MACINFO_define:
28258 case DW_MACINFO_undef:
28259 len = strlen (ref->info) + 1;
28260 if (!dwarf_strict
28261 && len > DWARF_OFFSET_SIZE
28262 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28263 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28264 set_indirect_string (find_AT_string (ref->info));
28265 break;
28266 case DW_MACINFO_start_file:
28267 /* -gsplit-dwarf -g3 will also output filename as indirect
28268 string. */
28269 if (!dwarf_split_debug_info)
28270 break;
28271 /* Fall through. */
28272 case DW_MACRO_define_strp:
28273 case DW_MACRO_undef_strp:
28274 set_indirect_string (find_AT_string (ref->info));
28275 break;
28276 default:
28277 break;
28282 /* Output macinfo section(s). */
28284 static void
28285 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28287 unsigned i;
28288 unsigned long length = vec_safe_length (macinfo_table);
28289 macinfo_entry *ref;
28290 vec<macinfo_entry, va_gc> *files = NULL;
28291 macinfo_hash_type *macinfo_htab = NULL;
28292 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28294 if (! length)
28295 return;
28297 /* output_macinfo* uses these interchangeably. */
28298 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28299 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28300 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28301 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28303 /* AIX Assembler inserts the length, so adjust the reference to match the
28304 offset expected by debuggers. */
28305 strcpy (dl_section_ref, debug_line_label);
28306 if (XCOFF_DEBUGGING_INFO)
28307 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28309 /* For .debug_macro emit the section header. */
28310 if (!dwarf_strict || dwarf_version >= 5)
28312 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28313 "DWARF macro version number");
28314 if (DWARF_OFFSET_SIZE == 8)
28315 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28316 else
28317 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28318 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28319 debug_line_section, NULL);
28322 /* In the first loop, it emits the primary .debug_macinfo section
28323 and after each emitted op the macinfo_entry is cleared.
28324 If a longer range of define/undef ops can be optimized using
28325 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28326 the vector before the first define/undef in the range and the
28327 whole range of define/undef ops is not emitted and kept. */
28328 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28330 switch (ref->code)
28332 case DW_MACINFO_start_file:
28333 vec_safe_push (files, *ref);
28334 break;
28335 case DW_MACINFO_end_file:
28336 if (!vec_safe_is_empty (files))
28337 files->pop ();
28338 break;
28339 case DW_MACINFO_define:
28340 case DW_MACINFO_undef:
28341 if ((!dwarf_strict || dwarf_version >= 5)
28342 && HAVE_COMDAT_GROUP
28343 && vec_safe_length (files) != 1
28344 && i > 0
28345 && i + 1 < length
28346 && (*macinfo_table)[i - 1].code == 0)
28348 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28349 if (count)
28351 i += count - 1;
28352 continue;
28355 break;
28356 case 0:
28357 /* A dummy entry may be inserted at the beginning to be able
28358 to optimize the whole block of predefined macros. */
28359 if (i == 0)
28360 continue;
28361 default:
28362 break;
28364 output_macinfo_op (ref);
28365 ref->info = NULL;
28366 ref->code = 0;
28369 if (!macinfo_htab)
28370 return;
28372 /* Save the number of transparent includes so we can adjust the
28373 label number for the fat LTO object DWARF. */
28374 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28376 delete macinfo_htab;
28377 macinfo_htab = NULL;
28379 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28380 terminate the current chain and switch to a new comdat .debug_macinfo
28381 section and emit the define/undef entries within it. */
28382 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28383 switch (ref->code)
28385 case 0:
28386 continue;
28387 case DW_MACRO_import:
28389 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28390 tree comdat_key = get_identifier (ref->info);
28391 /* Terminate the previous .debug_macinfo section. */
28392 dw2_asm_output_data (1, 0, "End compilation unit");
28393 targetm.asm_out.named_section (debug_macinfo_section_name,
28394 SECTION_DEBUG
28395 | SECTION_LINKONCE
28396 | (early_lto_debug
28397 ? SECTION_EXCLUDE : 0),
28398 comdat_key);
28399 ASM_GENERATE_INTERNAL_LABEL (label,
28400 DEBUG_MACRO_SECTION_LABEL,
28401 ref->lineno + macinfo_label_base);
28402 ASM_OUTPUT_LABEL (asm_out_file, label);
28403 ref->code = 0;
28404 ref->info = NULL;
28405 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28406 "DWARF macro version number");
28407 if (DWARF_OFFSET_SIZE == 8)
28408 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28409 else
28410 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28412 break;
28413 case DW_MACINFO_define:
28414 case DW_MACINFO_undef:
28415 output_macinfo_op (ref);
28416 ref->code = 0;
28417 ref->info = NULL;
28418 break;
28419 default:
28420 gcc_unreachable ();
28423 macinfo_label_base += macinfo_label_base_adj;
28426 /* Initialize the various sections and labels for dwarf output and prefix
28427 them with PREFIX if non-NULL. Returns the generation (zero based
28428 number of times function was called). */
28430 static unsigned
28431 init_sections_and_labels (bool early_lto_debug)
28433 /* As we may get called multiple times have a generation count for
28434 labels. */
28435 static unsigned generation = 0;
28437 if (early_lto_debug)
28439 if (!dwarf_split_debug_info)
28441 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28442 SECTION_DEBUG | SECTION_EXCLUDE,
28443 NULL);
28444 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28445 SECTION_DEBUG | SECTION_EXCLUDE,
28446 NULL);
28447 debug_macinfo_section_name
28448 = ((dwarf_strict && dwarf_version < 5)
28449 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28450 debug_macinfo_section = get_section (debug_macinfo_section_name,
28451 SECTION_DEBUG
28452 | SECTION_EXCLUDE, NULL);
28454 else
28456 /* ??? Which of the following do we need early? */
28457 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28458 SECTION_DEBUG | SECTION_EXCLUDE,
28459 NULL);
28460 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28461 SECTION_DEBUG | SECTION_EXCLUDE,
28462 NULL);
28463 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28464 SECTION_DEBUG
28465 | SECTION_EXCLUDE, NULL);
28466 debug_skeleton_abbrev_section
28467 = get_section (DEBUG_LTO_ABBREV_SECTION,
28468 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28469 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28470 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28471 generation);
28473 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28474 stay in the main .o, but the skeleton_line goes into the split
28475 off dwo. */
28476 debug_skeleton_line_section
28477 = get_section (DEBUG_LTO_LINE_SECTION,
28478 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28479 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28480 DEBUG_SKELETON_LINE_SECTION_LABEL,
28481 generation);
28482 debug_str_offsets_section
28483 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28484 SECTION_DEBUG | SECTION_EXCLUDE,
28485 NULL);
28486 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28487 DEBUG_SKELETON_INFO_SECTION_LABEL,
28488 generation);
28489 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28490 DEBUG_STR_DWO_SECTION_FLAGS,
28491 NULL);
28492 debug_macinfo_section_name
28493 = ((dwarf_strict && dwarf_version < 5)
28494 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28495 debug_macinfo_section = get_section (debug_macinfo_section_name,
28496 SECTION_DEBUG | SECTION_EXCLUDE,
28497 NULL);
28499 /* For macro info and the file table we have to refer to a
28500 debug_line section. */
28501 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28502 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28503 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28504 DEBUG_LINE_SECTION_LABEL, generation);
28506 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28507 DEBUG_STR_SECTION_FLAGS
28508 | SECTION_EXCLUDE, NULL);
28509 if (!dwarf_split_debug_info)
28510 debug_line_str_section
28511 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28512 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28514 else
28516 if (!dwarf_split_debug_info)
28518 debug_info_section = get_section (DEBUG_INFO_SECTION,
28519 SECTION_DEBUG, NULL);
28520 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28521 SECTION_DEBUG, NULL);
28522 debug_loc_section = get_section (dwarf_version >= 5
28523 ? DEBUG_LOCLISTS_SECTION
28524 : DEBUG_LOC_SECTION,
28525 SECTION_DEBUG, NULL);
28526 debug_macinfo_section_name
28527 = ((dwarf_strict && dwarf_version < 5)
28528 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28529 debug_macinfo_section = get_section (debug_macinfo_section_name,
28530 SECTION_DEBUG, NULL);
28532 else
28534 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28535 SECTION_DEBUG | SECTION_EXCLUDE,
28536 NULL);
28537 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28538 SECTION_DEBUG | SECTION_EXCLUDE,
28539 NULL);
28540 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28541 SECTION_DEBUG, NULL);
28542 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28543 SECTION_DEBUG, NULL);
28544 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28545 SECTION_DEBUG, NULL);
28546 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28547 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28548 generation);
28550 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28551 stay in the main .o, but the skeleton_line goes into the
28552 split off dwo. */
28553 debug_skeleton_line_section
28554 = get_section (DEBUG_DWO_LINE_SECTION,
28555 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28556 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28557 DEBUG_SKELETON_LINE_SECTION_LABEL,
28558 generation);
28559 debug_str_offsets_section
28560 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28561 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28562 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28563 DEBUG_SKELETON_INFO_SECTION_LABEL,
28564 generation);
28565 debug_loc_section = get_section (dwarf_version >= 5
28566 ? DEBUG_DWO_LOCLISTS_SECTION
28567 : DEBUG_DWO_LOC_SECTION,
28568 SECTION_DEBUG | SECTION_EXCLUDE,
28569 NULL);
28570 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28571 DEBUG_STR_DWO_SECTION_FLAGS,
28572 NULL);
28573 debug_macinfo_section_name
28574 = ((dwarf_strict && dwarf_version < 5)
28575 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28576 debug_macinfo_section = get_section (debug_macinfo_section_name,
28577 SECTION_DEBUG | SECTION_EXCLUDE,
28578 NULL);
28580 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28581 SECTION_DEBUG, NULL);
28582 debug_line_section = get_section (DEBUG_LINE_SECTION,
28583 SECTION_DEBUG, NULL);
28584 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28585 SECTION_DEBUG, NULL);
28586 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28587 SECTION_DEBUG, NULL);
28588 debug_str_section = get_section (DEBUG_STR_SECTION,
28589 DEBUG_STR_SECTION_FLAGS, NULL);
28590 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28591 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28592 DEBUG_STR_SECTION_FLAGS, NULL);
28594 debug_ranges_section = get_section (dwarf_version >= 5
28595 ? DEBUG_RNGLISTS_SECTION
28596 : DEBUG_RANGES_SECTION,
28597 SECTION_DEBUG, NULL);
28598 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28599 SECTION_DEBUG, NULL);
28602 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28603 DEBUG_ABBREV_SECTION_LABEL, generation);
28604 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28605 DEBUG_INFO_SECTION_LABEL, generation);
28606 info_section_emitted = false;
28607 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28608 DEBUG_LINE_SECTION_LABEL, generation);
28609 /* There are up to 4 unique ranges labels per generation.
28610 See also output_rnglists. */
28611 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28612 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28613 if (dwarf_version >= 5 && dwarf_split_debug_info)
28614 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28615 DEBUG_RANGES_SECTION_LABEL,
28616 1 + generation * 4);
28617 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28618 DEBUG_ADDR_SECTION_LABEL, generation);
28619 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28620 (dwarf_strict && dwarf_version < 5)
28621 ? DEBUG_MACINFO_SECTION_LABEL
28622 : DEBUG_MACRO_SECTION_LABEL, generation);
28623 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28624 generation);
28626 ++generation;
28627 return generation - 1;
28630 /* Set up for Dwarf output at the start of compilation. */
28632 static void
28633 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28635 /* Allocate the file_table. */
28636 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28638 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28639 /* Allocate the decl_die_table. */
28640 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28642 /* Allocate the decl_loc_table. */
28643 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28645 /* Allocate the cached_dw_loc_list_table. */
28646 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28648 /* Allocate the initial hunk of the abbrev_die_table. */
28649 vec_alloc (abbrev_die_table, 256);
28650 /* Zero-th entry is allocated, but unused. */
28651 abbrev_die_table->quick_push (NULL);
28653 /* Allocate the dwarf_proc_stack_usage_map. */
28654 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28656 /* Allocate the pubtypes and pubnames vectors. */
28657 vec_alloc (pubname_table, 32);
28658 vec_alloc (pubtype_table, 32);
28660 vec_alloc (incomplete_types, 64);
28662 vec_alloc (used_rtx_array, 32);
28664 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28665 vec_alloc (macinfo_table, 64);
28666 #endif
28668 /* If front-ends already registered a main translation unit but we were not
28669 ready to perform the association, do this now. */
28670 if (main_translation_unit != NULL_TREE)
28671 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28674 /* Called before compile () starts outputtting functions, variables
28675 and toplevel asms into assembly. */
28677 static void
28678 dwarf2out_assembly_start (void)
28680 if (text_section_line_info)
28681 return;
28683 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28684 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28685 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28686 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28687 COLD_TEXT_SECTION_LABEL, 0);
28688 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28690 switch_to_section (text_section);
28691 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28692 #endif
28694 /* Make sure the line number table for .text always exists. */
28695 text_section_line_info = new_line_info_table ();
28696 text_section_line_info->end_label = text_end_label;
28698 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28699 cur_line_info_table = text_section_line_info;
28700 #endif
28702 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28703 && dwarf2out_do_cfi_asm ()
28704 && !dwarf2out_do_eh_frame ())
28705 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28708 /* A helper function for dwarf2out_finish called through
28709 htab_traverse. Assign a string its index. All strings must be
28710 collected into the table by the time index_string is called,
28711 because the indexing code relies on htab_traverse to traverse nodes
28712 in the same order for each run. */
28715 index_string (indirect_string_node **h, unsigned int *index)
28717 indirect_string_node *node = *h;
28719 find_string_form (node);
28720 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28722 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28723 node->index = *index;
28724 *index += 1;
28726 return 1;
28729 /* A helper function for output_indirect_strings called through
28730 htab_traverse. Output the offset to a string and update the
28731 current offset. */
28734 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28736 indirect_string_node *node = *h;
28738 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28740 /* Assert that this node has been assigned an index. */
28741 gcc_assert (node->index != NO_INDEX_ASSIGNED
28742 && node->index != NOT_INDEXED);
28743 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28744 "indexed string 0x%x: %s", node->index, node->str);
28745 *offset += strlen (node->str) + 1;
28747 return 1;
28750 /* A helper function for dwarf2out_finish called through
28751 htab_traverse. Output the indexed string. */
28754 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28756 struct indirect_string_node *node = *h;
28758 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28760 /* Assert that the strings are output in the same order as their
28761 indexes were assigned. */
28762 gcc_assert (*cur_idx == node->index);
28763 assemble_string (node->str, strlen (node->str) + 1);
28764 *cur_idx += 1;
28766 return 1;
28769 /* A helper function for output_indirect_strings. Counts the number
28770 of index strings offsets. Must match the logic of the functions
28771 output_index_string[_offsets] above. */
28773 count_index_strings (indirect_string_node **h, unsigned int *last_idx)
28775 struct indirect_string_node *node = *h;
28777 if (node->form == dwarf_FORM (DW_FORM_strx) && node->refcount > 0)
28778 *last_idx += 1;
28779 return 1;
28782 /* A helper function for dwarf2out_finish called through
28783 htab_traverse. Emit one queued .debug_str string. */
28786 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28788 struct indirect_string_node *node = *h;
28790 node->form = find_string_form (node);
28791 if (node->form == form && node->refcount > 0)
28793 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28794 assemble_string (node->str, strlen (node->str) + 1);
28797 return 1;
28800 /* Output the indexed string table. */
28802 static void
28803 output_indirect_strings (void)
28805 switch_to_section (debug_str_section);
28806 if (!dwarf_split_debug_info)
28807 debug_str_hash->traverse<enum dwarf_form,
28808 output_indirect_string> (DW_FORM_strp);
28809 else
28811 unsigned int offset = 0;
28812 unsigned int cur_idx = 0;
28814 if (skeleton_debug_str_hash)
28815 skeleton_debug_str_hash->traverse<enum dwarf_form,
28816 output_indirect_string> (DW_FORM_strp);
28818 switch_to_section (debug_str_offsets_section);
28819 /* For DWARF5 the .debug_str_offsets[.dwo] section needs a unit
28820 header. Note that we don't need to generate a label to the
28821 actual index table following the header here, because this is
28822 for the split dwarf case only. In an .dwo file there is only
28823 one string offsets table (and one debug info section). But
28824 if we would start using string offset tables for the main (or
28825 skeleton) unit, then we have to add a DW_AT_str_offsets_base
28826 pointing to the actual index after the header. Split dwarf
28827 units will never have a string offsets base attribute. When
28828 a split unit is moved into a .dwp file the string offsets can
28829 be found through the .debug_cu_index section table. */
28830 if (dwarf_version >= 5)
28832 unsigned int last_idx = 0;
28833 unsigned long str_offsets_length;
28835 debug_str_hash->traverse_noresize
28836 <unsigned int *, count_index_strings> (&last_idx);
28837 str_offsets_length = last_idx * DWARF_OFFSET_SIZE + 4;
28838 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
28839 dw2_asm_output_data (4, 0xffffffff,
28840 "Escape value for 64-bit DWARF extension");
28841 dw2_asm_output_data (DWARF_OFFSET_SIZE, str_offsets_length,
28842 "Length of string offsets unit");
28843 dw2_asm_output_data (2, 5, "DWARF string offsets version");
28844 dw2_asm_output_data (2, 0, "Header zero padding");
28846 debug_str_hash->traverse_noresize
28847 <unsigned int *, output_index_string_offset> (&offset);
28848 switch_to_section (debug_str_dwo_section);
28849 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28850 (&cur_idx);
28854 /* Callback for htab_traverse to assign an index to an entry in the
28855 table, and to write that entry to the .debug_addr section. */
28858 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28860 addr_table_entry *entry = *slot;
28862 if (entry->refcount == 0)
28864 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28865 || entry->index == NOT_INDEXED);
28866 return 1;
28869 gcc_assert (entry->index == *cur_index);
28870 (*cur_index)++;
28872 switch (entry->kind)
28874 case ate_kind_rtx:
28875 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28876 "0x%x", entry->index);
28877 break;
28878 case ate_kind_rtx_dtprel:
28879 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28880 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28881 DWARF2_ADDR_SIZE,
28882 entry->addr.rtl);
28883 fputc ('\n', asm_out_file);
28884 break;
28885 case ate_kind_label:
28886 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28887 "0x%x", entry->index);
28888 break;
28889 default:
28890 gcc_unreachable ();
28892 return 1;
28895 /* A helper function for dwarf2out_finish. Counts the number
28896 of indexed addresses. Must match the logic of the functions
28897 output_addr_table_entry above. */
28899 count_index_addrs (addr_table_entry **slot, unsigned int *last_idx)
28901 addr_table_entry *entry = *slot;
28903 if (entry->refcount > 0)
28904 *last_idx += 1;
28905 return 1;
28908 /* Produce the .debug_addr section. */
28910 static void
28911 output_addr_table (void)
28913 unsigned int index = 0;
28914 if (addr_index_table == NULL || addr_index_table->size () == 0)
28915 return;
28917 switch_to_section (debug_addr_section);
28918 addr_index_table
28919 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28922 #if ENABLE_ASSERT_CHECKING
28923 /* Verify that all marks are clear. */
28925 static void
28926 verify_marks_clear (dw_die_ref die)
28928 dw_die_ref c;
28930 gcc_assert (! die->die_mark);
28931 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28933 #endif /* ENABLE_ASSERT_CHECKING */
28935 /* Clear the marks for a die and its children.
28936 Be cool if the mark isn't set. */
28938 static void
28939 prune_unmark_dies (dw_die_ref die)
28941 dw_die_ref c;
28943 if (die->die_mark)
28944 die->die_mark = 0;
28945 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28948 /* Given LOC that is referenced by a DIE we're marking as used, find all
28949 referenced DWARF procedures it references and mark them as used. */
28951 static void
28952 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28954 for (; loc != NULL; loc = loc->dw_loc_next)
28955 switch (loc->dw_loc_opc)
28957 case DW_OP_implicit_pointer:
28958 case DW_OP_convert:
28959 case DW_OP_reinterpret:
28960 case DW_OP_GNU_implicit_pointer:
28961 case DW_OP_GNU_convert:
28962 case DW_OP_GNU_reinterpret:
28963 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28964 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28965 break;
28966 case DW_OP_GNU_variable_value:
28967 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28969 dw_die_ref ref
28970 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28971 if (ref == NULL)
28972 break;
28973 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28974 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28975 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28977 /* FALLTHRU */
28978 case DW_OP_call2:
28979 case DW_OP_call4:
28980 case DW_OP_call_ref:
28981 case DW_OP_const_type:
28982 case DW_OP_GNU_const_type:
28983 case DW_OP_GNU_parameter_ref:
28984 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28985 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28986 break;
28987 case DW_OP_regval_type:
28988 case DW_OP_deref_type:
28989 case DW_OP_GNU_regval_type:
28990 case DW_OP_GNU_deref_type:
28991 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28992 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28993 break;
28994 case DW_OP_entry_value:
28995 case DW_OP_GNU_entry_value:
28996 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28997 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28998 break;
28999 default:
29000 break;
29004 /* Given DIE that we're marking as used, find any other dies
29005 it references as attributes and mark them as used. */
29007 static void
29008 prune_unused_types_walk_attribs (dw_die_ref die)
29010 dw_attr_node *a;
29011 unsigned ix;
29013 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29015 switch (AT_class (a))
29017 /* Make sure DWARF procedures referenced by location descriptions will
29018 get emitted. */
29019 case dw_val_class_loc:
29020 prune_unused_types_walk_loc_descr (AT_loc (a));
29021 break;
29022 case dw_val_class_loc_list:
29023 for (dw_loc_list_ref list = AT_loc_list (a);
29024 list != NULL;
29025 list = list->dw_loc_next)
29026 prune_unused_types_walk_loc_descr (list->expr);
29027 break;
29029 case dw_val_class_view_list:
29030 /* This points to a loc_list in another attribute, so it's
29031 already covered. */
29032 break;
29034 case dw_val_class_die_ref:
29035 /* A reference to another DIE.
29036 Make sure that it will get emitted.
29037 If it was broken out into a comdat group, don't follow it. */
29038 if (! AT_ref (a)->comdat_type_p
29039 || a->dw_attr == DW_AT_specification)
29040 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
29041 break;
29043 case dw_val_class_str:
29044 /* Set the string's refcount to 0 so that prune_unused_types_mark
29045 accounts properly for it. */
29046 a->dw_attr_val.v.val_str->refcount = 0;
29047 break;
29049 default:
29050 break;
29055 /* Mark the generic parameters and arguments children DIEs of DIE. */
29057 static void
29058 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
29060 dw_die_ref c;
29062 if (die == NULL || die->die_child == NULL)
29063 return;
29064 c = die->die_child;
29067 if (is_template_parameter (c))
29068 prune_unused_types_mark (c, 1);
29069 c = c->die_sib;
29070 } while (c && c != die->die_child);
29073 /* Mark DIE as being used. If DOKIDS is true, then walk down
29074 to DIE's children. */
29076 static void
29077 prune_unused_types_mark (dw_die_ref die, int dokids)
29079 dw_die_ref c;
29081 if (die->die_mark == 0)
29083 /* We haven't done this node yet. Mark it as used. */
29084 die->die_mark = 1;
29085 /* If this is the DIE of a generic type instantiation,
29086 mark the children DIEs that describe its generic parms and
29087 args. */
29088 prune_unused_types_mark_generic_parms_dies (die);
29090 /* We also have to mark its parents as used.
29091 (But we don't want to mark our parent's kids due to this,
29092 unless it is a class.) */
29093 if (die->die_parent)
29094 prune_unused_types_mark (die->die_parent,
29095 class_scope_p (die->die_parent));
29097 /* Mark any referenced nodes. */
29098 prune_unused_types_walk_attribs (die);
29100 /* If this node is a specification,
29101 also mark the definition, if it exists. */
29102 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29103 prune_unused_types_mark (die->die_definition, 1);
29106 if (dokids && die->die_mark != 2)
29108 /* We need to walk the children, but haven't done so yet.
29109 Remember that we've walked the kids. */
29110 die->die_mark = 2;
29112 /* If this is an array type, we need to make sure our
29113 kids get marked, even if they're types. If we're
29114 breaking out types into comdat sections, do this
29115 for all type definitions. */
29116 if (die->die_tag == DW_TAG_array_type
29117 || (use_debug_types
29118 && is_type_die (die) && ! is_declaration_die (die)))
29119 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29120 else
29121 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29125 /* For local classes, look if any static member functions were emitted
29126 and if so, mark them. */
29128 static void
29129 prune_unused_types_walk_local_classes (dw_die_ref die)
29131 dw_die_ref c;
29133 if (die->die_mark == 2)
29134 return;
29136 switch (die->die_tag)
29138 case DW_TAG_structure_type:
29139 case DW_TAG_union_type:
29140 case DW_TAG_class_type:
29141 case DW_TAG_interface_type:
29142 break;
29144 case DW_TAG_subprogram:
29145 if (!get_AT_flag (die, DW_AT_declaration)
29146 || die->die_definition != NULL)
29147 prune_unused_types_mark (die, 1);
29148 return;
29150 default:
29151 return;
29154 /* Mark children. */
29155 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29158 /* Walk the tree DIE and mark types that we actually use. */
29160 static void
29161 prune_unused_types_walk (dw_die_ref die)
29163 dw_die_ref c;
29165 /* Don't do anything if this node is already marked and
29166 children have been marked as well. */
29167 if (die->die_mark == 2)
29168 return;
29170 switch (die->die_tag)
29172 case DW_TAG_structure_type:
29173 case DW_TAG_union_type:
29174 case DW_TAG_class_type:
29175 case DW_TAG_interface_type:
29176 if (die->die_perennial_p)
29177 break;
29179 for (c = die->die_parent; c; c = c->die_parent)
29180 if (c->die_tag == DW_TAG_subprogram)
29181 break;
29183 /* Finding used static member functions inside of classes
29184 is needed just for local classes, because for other classes
29185 static member function DIEs with DW_AT_specification
29186 are emitted outside of the DW_TAG_*_type. If we ever change
29187 it, we'd need to call this even for non-local classes. */
29188 if (c)
29189 prune_unused_types_walk_local_classes (die);
29191 /* It's a type node --- don't mark it. */
29192 return;
29194 case DW_TAG_const_type:
29195 case DW_TAG_packed_type:
29196 case DW_TAG_pointer_type:
29197 case DW_TAG_reference_type:
29198 case DW_TAG_rvalue_reference_type:
29199 case DW_TAG_volatile_type:
29200 case DW_TAG_typedef:
29201 case DW_TAG_array_type:
29202 case DW_TAG_friend:
29203 case DW_TAG_enumeration_type:
29204 case DW_TAG_subroutine_type:
29205 case DW_TAG_string_type:
29206 case DW_TAG_set_type:
29207 case DW_TAG_subrange_type:
29208 case DW_TAG_ptr_to_member_type:
29209 case DW_TAG_file_type:
29210 /* Type nodes are useful only when other DIEs reference them --- don't
29211 mark them. */
29212 /* FALLTHROUGH */
29214 case DW_TAG_dwarf_procedure:
29215 /* Likewise for DWARF procedures. */
29217 if (die->die_perennial_p)
29218 break;
29220 return;
29222 default:
29223 /* Mark everything else. */
29224 break;
29227 if (die->die_mark == 0)
29229 die->die_mark = 1;
29231 /* Now, mark any dies referenced from here. */
29232 prune_unused_types_walk_attribs (die);
29235 die->die_mark = 2;
29237 /* Mark children. */
29238 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29241 /* Increment the string counts on strings referred to from DIE's
29242 attributes. */
29244 static void
29245 prune_unused_types_update_strings (dw_die_ref die)
29247 dw_attr_node *a;
29248 unsigned ix;
29250 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29251 if (AT_class (a) == dw_val_class_str)
29253 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29254 s->refcount++;
29255 /* Avoid unnecessarily putting strings that are used less than
29256 twice in the hash table. */
29257 if (s->refcount
29258 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29260 indirect_string_node **slot
29261 = debug_str_hash->find_slot_with_hash (s->str,
29262 htab_hash_string (s->str),
29263 INSERT);
29264 gcc_assert (*slot == NULL);
29265 *slot = s;
29270 /* Mark DIE and its children as removed. */
29272 static void
29273 mark_removed (dw_die_ref die)
29275 dw_die_ref c;
29276 die->removed = true;
29277 FOR_EACH_CHILD (die, c, mark_removed (c));
29280 /* Remove from the tree DIE any dies that aren't marked. */
29282 static void
29283 prune_unused_types_prune (dw_die_ref die)
29285 dw_die_ref c;
29287 gcc_assert (die->die_mark);
29288 prune_unused_types_update_strings (die);
29290 if (! die->die_child)
29291 return;
29293 c = die->die_child;
29294 do {
29295 dw_die_ref prev = c, next;
29296 for (c = c->die_sib; ! c->die_mark; c = next)
29297 if (c == die->die_child)
29299 /* No marked children between 'prev' and the end of the list. */
29300 if (prev == c)
29301 /* No marked children at all. */
29302 die->die_child = NULL;
29303 else
29305 prev->die_sib = c->die_sib;
29306 die->die_child = prev;
29308 c->die_sib = NULL;
29309 mark_removed (c);
29310 return;
29312 else
29314 next = c->die_sib;
29315 c->die_sib = NULL;
29316 mark_removed (c);
29319 if (c != prev->die_sib)
29320 prev->die_sib = c;
29321 prune_unused_types_prune (c);
29322 } while (c != die->die_child);
29325 /* Remove dies representing declarations that we never use. */
29327 static void
29328 prune_unused_types (void)
29330 unsigned int i;
29331 limbo_die_node *node;
29332 comdat_type_node *ctnode;
29333 pubname_entry *pub;
29334 dw_die_ref base_type;
29336 #if ENABLE_ASSERT_CHECKING
29337 /* All the marks should already be clear. */
29338 verify_marks_clear (comp_unit_die ());
29339 for (node = limbo_die_list; node; node = node->next)
29340 verify_marks_clear (node->die);
29341 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29342 verify_marks_clear (ctnode->root_die);
29343 #endif /* ENABLE_ASSERT_CHECKING */
29345 /* Mark types that are used in global variables. */
29346 premark_types_used_by_global_vars ();
29348 /* Set the mark on nodes that are actually used. */
29349 prune_unused_types_walk (comp_unit_die ());
29350 for (node = limbo_die_list; node; node = node->next)
29351 prune_unused_types_walk (node->die);
29352 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29354 prune_unused_types_walk (ctnode->root_die);
29355 prune_unused_types_mark (ctnode->type_die, 1);
29358 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29359 are unusual in that they are pubnames that are the children of pubtypes.
29360 They should only be marked via their parent DW_TAG_enumeration_type die,
29361 not as roots in themselves. */
29362 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29363 if (pub->die->die_tag != DW_TAG_enumerator)
29364 prune_unused_types_mark (pub->die, 1);
29365 for (i = 0; base_types.iterate (i, &base_type); i++)
29366 prune_unused_types_mark (base_type, 1);
29368 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29369 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29370 callees). */
29371 cgraph_node *cnode;
29372 FOR_EACH_FUNCTION (cnode)
29373 if (cnode->referred_to_p (false))
29375 dw_die_ref die = lookup_decl_die (cnode->decl);
29376 if (die == NULL || die->die_mark)
29377 continue;
29378 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29379 if (e->caller != cnode
29380 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29382 prune_unused_types_mark (die, 1);
29383 break;
29387 if (debug_str_hash)
29388 debug_str_hash->empty ();
29389 if (skeleton_debug_str_hash)
29390 skeleton_debug_str_hash->empty ();
29391 prune_unused_types_prune (comp_unit_die ());
29392 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29394 node = *pnode;
29395 if (!node->die->die_mark)
29396 *pnode = node->next;
29397 else
29399 prune_unused_types_prune (node->die);
29400 pnode = &node->next;
29403 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29404 prune_unused_types_prune (ctnode->root_die);
29406 /* Leave the marks clear. */
29407 prune_unmark_dies (comp_unit_die ());
29408 for (node = limbo_die_list; node; node = node->next)
29409 prune_unmark_dies (node->die);
29410 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29411 prune_unmark_dies (ctnode->root_die);
29414 /* Helpers to manipulate hash table of comdat type units. */
29416 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29418 static inline hashval_t hash (const comdat_type_node *);
29419 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29422 inline hashval_t
29423 comdat_type_hasher::hash (const comdat_type_node *type_node)
29425 hashval_t h;
29426 memcpy (&h, type_node->signature, sizeof (h));
29427 return h;
29430 inline bool
29431 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29432 const comdat_type_node *type_node_2)
29434 return (! memcmp (type_node_1->signature, type_node_2->signature,
29435 DWARF_TYPE_SIGNATURE_SIZE));
29438 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29439 to the location it would have been added, should we know its
29440 DECL_ASSEMBLER_NAME when we added other attributes. This will
29441 probably improve compactness of debug info, removing equivalent
29442 abbrevs, and hide any differences caused by deferring the
29443 computation of the assembler name, triggered by e.g. PCH. */
29445 static inline void
29446 move_linkage_attr (dw_die_ref die)
29448 unsigned ix = vec_safe_length (die->die_attr);
29449 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29451 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29452 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29454 while (--ix > 0)
29456 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29458 if (prev->dw_attr == DW_AT_decl_line
29459 || prev->dw_attr == DW_AT_decl_column
29460 || prev->dw_attr == DW_AT_name)
29461 break;
29464 if (ix != vec_safe_length (die->die_attr) - 1)
29466 die->die_attr->pop ();
29467 die->die_attr->quick_insert (ix, linkage);
29471 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29472 referenced from typed stack ops and count how often they are used. */
29474 static void
29475 mark_base_types (dw_loc_descr_ref loc)
29477 dw_die_ref base_type = NULL;
29479 for (; loc; loc = loc->dw_loc_next)
29481 switch (loc->dw_loc_opc)
29483 case DW_OP_regval_type:
29484 case DW_OP_deref_type:
29485 case DW_OP_GNU_regval_type:
29486 case DW_OP_GNU_deref_type:
29487 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29488 break;
29489 case DW_OP_convert:
29490 case DW_OP_reinterpret:
29491 case DW_OP_GNU_convert:
29492 case DW_OP_GNU_reinterpret:
29493 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29494 continue;
29495 /* FALLTHRU */
29496 case DW_OP_const_type:
29497 case DW_OP_GNU_const_type:
29498 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29499 break;
29500 case DW_OP_entry_value:
29501 case DW_OP_GNU_entry_value:
29502 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29503 continue;
29504 default:
29505 continue;
29507 gcc_assert (base_type->die_parent == comp_unit_die ());
29508 if (base_type->die_mark)
29509 base_type->die_mark++;
29510 else
29512 base_types.safe_push (base_type);
29513 base_type->die_mark = 1;
29518 /* Comparison function for sorting marked base types. */
29520 static int
29521 base_type_cmp (const void *x, const void *y)
29523 dw_die_ref dx = *(const dw_die_ref *) x;
29524 dw_die_ref dy = *(const dw_die_ref *) y;
29525 unsigned int byte_size1, byte_size2;
29526 unsigned int encoding1, encoding2;
29527 unsigned int align1, align2;
29528 if (dx->die_mark > dy->die_mark)
29529 return -1;
29530 if (dx->die_mark < dy->die_mark)
29531 return 1;
29532 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29533 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29534 if (byte_size1 < byte_size2)
29535 return 1;
29536 if (byte_size1 > byte_size2)
29537 return -1;
29538 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29539 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29540 if (encoding1 < encoding2)
29541 return 1;
29542 if (encoding1 > encoding2)
29543 return -1;
29544 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29545 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29546 if (align1 < align2)
29547 return 1;
29548 if (align1 > align2)
29549 return -1;
29550 return 0;
29553 /* Move base types marked by mark_base_types as early as possible
29554 in the CU, sorted by decreasing usage count both to make the
29555 uleb128 references as small as possible and to make sure they
29556 will have die_offset already computed by calc_die_sizes when
29557 sizes of typed stack loc ops is computed. */
29559 static void
29560 move_marked_base_types (void)
29562 unsigned int i;
29563 dw_die_ref base_type, die, c;
29565 if (base_types.is_empty ())
29566 return;
29568 /* Sort by decreasing usage count, they will be added again in that
29569 order later on. */
29570 base_types.qsort (base_type_cmp);
29571 die = comp_unit_die ();
29572 c = die->die_child;
29575 dw_die_ref prev = c;
29576 c = c->die_sib;
29577 while (c->die_mark)
29579 remove_child_with_prev (c, prev);
29580 /* As base types got marked, there must be at least
29581 one node other than DW_TAG_base_type. */
29582 gcc_assert (die->die_child != NULL);
29583 c = prev->die_sib;
29586 while (c != die->die_child);
29587 gcc_assert (die->die_child);
29588 c = die->die_child;
29589 for (i = 0; base_types.iterate (i, &base_type); i++)
29591 base_type->die_mark = 0;
29592 base_type->die_sib = c->die_sib;
29593 c->die_sib = base_type;
29594 c = base_type;
29598 /* Helper function for resolve_addr, attempt to resolve
29599 one CONST_STRING, return true if successful. Similarly verify that
29600 SYMBOL_REFs refer to variables emitted in the current CU. */
29602 static bool
29603 resolve_one_addr (rtx *addr)
29605 rtx rtl = *addr;
29607 if (GET_CODE (rtl) == CONST_STRING)
29609 size_t len = strlen (XSTR (rtl, 0)) + 1;
29610 tree t = build_string (len, XSTR (rtl, 0));
29611 tree tlen = size_int (len - 1);
29612 TREE_TYPE (t)
29613 = build_array_type (char_type_node, build_index_type (tlen));
29614 rtl = lookup_constant_def (t);
29615 if (!rtl || !MEM_P (rtl))
29616 return false;
29617 rtl = XEXP (rtl, 0);
29618 if (GET_CODE (rtl) == SYMBOL_REF
29619 && SYMBOL_REF_DECL (rtl)
29620 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29621 return false;
29622 vec_safe_push (used_rtx_array, rtl);
29623 *addr = rtl;
29624 return true;
29627 if (GET_CODE (rtl) == SYMBOL_REF
29628 && SYMBOL_REF_DECL (rtl))
29630 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29632 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29633 return false;
29635 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29636 return false;
29639 if (GET_CODE (rtl) == CONST)
29641 subrtx_ptr_iterator::array_type array;
29642 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29643 if (!resolve_one_addr (*iter))
29644 return false;
29647 return true;
29650 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29651 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29652 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29654 static rtx
29655 string_cst_pool_decl (tree t)
29657 rtx rtl = output_constant_def (t, 1);
29658 unsigned char *array;
29659 dw_loc_descr_ref l;
29660 tree decl;
29661 size_t len;
29662 dw_die_ref ref;
29664 if (!rtl || !MEM_P (rtl))
29665 return NULL_RTX;
29666 rtl = XEXP (rtl, 0);
29667 if (GET_CODE (rtl) != SYMBOL_REF
29668 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29669 return NULL_RTX;
29671 decl = SYMBOL_REF_DECL (rtl);
29672 if (!lookup_decl_die (decl))
29674 len = TREE_STRING_LENGTH (t);
29675 vec_safe_push (used_rtx_array, rtl);
29676 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29677 array = ggc_vec_alloc<unsigned char> (len);
29678 memcpy (array, TREE_STRING_POINTER (t), len);
29679 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29680 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29681 l->dw_loc_oprnd2.v.val_vec.length = len;
29682 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29683 l->dw_loc_oprnd2.v.val_vec.array = array;
29684 add_AT_loc (ref, DW_AT_location, l);
29685 equate_decl_number_to_die (decl, ref);
29687 return rtl;
29690 /* Helper function of resolve_addr_in_expr. LOC is
29691 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29692 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29693 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29694 with DW_OP_implicit_pointer if possible
29695 and return true, if unsuccessful, return false. */
29697 static bool
29698 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29700 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29701 HOST_WIDE_INT offset = 0;
29702 dw_die_ref ref = NULL;
29703 tree decl;
29705 if (GET_CODE (rtl) == CONST
29706 && GET_CODE (XEXP (rtl, 0)) == PLUS
29707 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29709 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29710 rtl = XEXP (XEXP (rtl, 0), 0);
29712 if (GET_CODE (rtl) == CONST_STRING)
29714 size_t len = strlen (XSTR (rtl, 0)) + 1;
29715 tree t = build_string (len, XSTR (rtl, 0));
29716 tree tlen = size_int (len - 1);
29718 TREE_TYPE (t)
29719 = build_array_type (char_type_node, build_index_type (tlen));
29720 rtl = string_cst_pool_decl (t);
29721 if (!rtl)
29722 return false;
29724 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29726 decl = SYMBOL_REF_DECL (rtl);
29727 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29729 ref = lookup_decl_die (decl);
29730 if (ref && (get_AT (ref, DW_AT_location)
29731 || get_AT (ref, DW_AT_const_value)))
29733 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29734 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29735 loc->dw_loc_oprnd1.val_entry = NULL;
29736 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29737 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29738 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29739 loc->dw_loc_oprnd2.v.val_int = offset;
29740 return true;
29744 return false;
29747 /* Helper function for resolve_addr, handle one location
29748 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29749 the location list couldn't be resolved. */
29751 static bool
29752 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29754 dw_loc_descr_ref keep = NULL;
29755 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29756 switch (loc->dw_loc_opc)
29758 case DW_OP_addr:
29759 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29761 if ((prev == NULL
29762 || prev->dw_loc_opc == DW_OP_piece
29763 || prev->dw_loc_opc == DW_OP_bit_piece)
29764 && loc->dw_loc_next
29765 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29766 && (!dwarf_strict || dwarf_version >= 5)
29767 && optimize_one_addr_into_implicit_ptr (loc))
29768 break;
29769 return false;
29771 break;
29772 case DW_OP_GNU_addr_index:
29773 case DW_OP_addrx:
29774 case DW_OP_GNU_const_index:
29775 case DW_OP_constx:
29776 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
29777 || loc->dw_loc_opc == DW_OP_addrx)
29778 || ((loc->dw_loc_opc == DW_OP_GNU_const_index
29779 || loc->dw_loc_opc == DW_OP_constx)
29780 && loc->dtprel))
29782 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29783 if (!resolve_one_addr (&rtl))
29784 return false;
29785 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29786 loc->dw_loc_oprnd1.val_entry
29787 = add_addr_table_entry (rtl, ate_kind_rtx);
29789 break;
29790 case DW_OP_const4u:
29791 case DW_OP_const8u:
29792 if (loc->dtprel
29793 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29794 return false;
29795 break;
29796 case DW_OP_plus_uconst:
29797 if (size_of_loc_descr (loc)
29798 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29800 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29802 dw_loc_descr_ref repl
29803 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29804 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29805 add_loc_descr (&repl, loc->dw_loc_next);
29806 *loc = *repl;
29808 break;
29809 case DW_OP_implicit_value:
29810 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29811 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29812 return false;
29813 break;
29814 case DW_OP_implicit_pointer:
29815 case DW_OP_GNU_implicit_pointer:
29816 case DW_OP_GNU_parameter_ref:
29817 case DW_OP_GNU_variable_value:
29818 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29820 dw_die_ref ref
29821 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29822 if (ref == NULL)
29823 return false;
29824 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29825 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29826 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29828 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29830 if (prev == NULL
29831 && loc->dw_loc_next == NULL
29832 && AT_class (a) == dw_val_class_loc)
29833 switch (a->dw_attr)
29835 /* Following attributes allow both exprloc and reference,
29836 so if the whole expression is DW_OP_GNU_variable_value
29837 alone we could transform it into reference. */
29838 case DW_AT_byte_size:
29839 case DW_AT_bit_size:
29840 case DW_AT_lower_bound:
29841 case DW_AT_upper_bound:
29842 case DW_AT_bit_stride:
29843 case DW_AT_count:
29844 case DW_AT_allocated:
29845 case DW_AT_associated:
29846 case DW_AT_byte_stride:
29847 a->dw_attr_val.val_class = dw_val_class_die_ref;
29848 a->dw_attr_val.val_entry = NULL;
29849 a->dw_attr_val.v.val_die_ref.die
29850 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29851 a->dw_attr_val.v.val_die_ref.external = 0;
29852 return true;
29853 default:
29854 break;
29856 if (dwarf_strict)
29857 return false;
29859 break;
29860 case DW_OP_const_type:
29861 case DW_OP_regval_type:
29862 case DW_OP_deref_type:
29863 case DW_OP_convert:
29864 case DW_OP_reinterpret:
29865 case DW_OP_GNU_const_type:
29866 case DW_OP_GNU_regval_type:
29867 case DW_OP_GNU_deref_type:
29868 case DW_OP_GNU_convert:
29869 case DW_OP_GNU_reinterpret:
29870 while (loc->dw_loc_next
29871 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29872 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29874 dw_die_ref base1, base2;
29875 unsigned enc1, enc2, size1, size2;
29876 if (loc->dw_loc_opc == DW_OP_regval_type
29877 || loc->dw_loc_opc == DW_OP_deref_type
29878 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29879 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29880 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29881 else if (loc->dw_loc_oprnd1.val_class
29882 == dw_val_class_unsigned_const)
29883 break;
29884 else
29885 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29886 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29887 == dw_val_class_unsigned_const)
29888 break;
29889 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29890 gcc_assert (base1->die_tag == DW_TAG_base_type
29891 && base2->die_tag == DW_TAG_base_type);
29892 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29893 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29894 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29895 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29896 if (size1 == size2
29897 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29898 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29899 && loc != keep)
29900 || enc1 == enc2))
29902 /* Optimize away next DW_OP_convert after
29903 adjusting LOC's base type die reference. */
29904 if (loc->dw_loc_opc == DW_OP_regval_type
29905 || loc->dw_loc_opc == DW_OP_deref_type
29906 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29907 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29908 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29909 else
29910 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29911 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29912 continue;
29914 /* Don't change integer DW_OP_convert after e.g. floating
29915 point typed stack entry. */
29916 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29917 keep = loc->dw_loc_next;
29918 break;
29920 break;
29921 default:
29922 break;
29924 return true;
29927 /* Helper function of resolve_addr. DIE had DW_AT_location of
29928 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29929 and DW_OP_addr couldn't be resolved. resolve_addr has already
29930 removed the DW_AT_location attribute. This function attempts to
29931 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29932 to it or DW_AT_const_value attribute, if possible. */
29934 static void
29935 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29937 if (!VAR_P (decl)
29938 || lookup_decl_die (decl) != die
29939 || DECL_EXTERNAL (decl)
29940 || !TREE_STATIC (decl)
29941 || DECL_INITIAL (decl) == NULL_TREE
29942 || DECL_P (DECL_INITIAL (decl))
29943 || get_AT (die, DW_AT_const_value))
29944 return;
29946 tree init = DECL_INITIAL (decl);
29947 HOST_WIDE_INT offset = 0;
29948 /* For variables that have been optimized away and thus
29949 don't have a memory location, see if we can emit
29950 DW_AT_const_value instead. */
29951 if (tree_add_const_value_attribute (die, init))
29952 return;
29953 if (dwarf_strict && dwarf_version < 5)
29954 return;
29955 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29956 and ADDR_EXPR refers to a decl that has DW_AT_location or
29957 DW_AT_const_value (but isn't addressable, otherwise
29958 resolving the original DW_OP_addr wouldn't fail), see if
29959 we can add DW_OP_implicit_pointer. */
29960 STRIP_NOPS (init);
29961 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29962 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29964 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29965 init = TREE_OPERAND (init, 0);
29966 STRIP_NOPS (init);
29968 if (TREE_CODE (init) != ADDR_EXPR)
29969 return;
29970 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29971 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29972 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29973 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29974 && TREE_OPERAND (init, 0) != decl))
29976 dw_die_ref ref;
29977 dw_loc_descr_ref l;
29979 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29981 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29982 if (!rtl)
29983 return;
29984 decl = SYMBOL_REF_DECL (rtl);
29986 else
29987 decl = TREE_OPERAND (init, 0);
29988 ref = lookup_decl_die (decl);
29989 if (ref == NULL
29990 || (!get_AT (ref, DW_AT_location)
29991 && !get_AT (ref, DW_AT_const_value)))
29992 return;
29993 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29994 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29995 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29996 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29997 add_AT_loc (die, DW_AT_location, l);
30001 /* Return NULL if l is a DWARF expression, or first op that is not
30002 valid DWARF expression. */
30004 static dw_loc_descr_ref
30005 non_dwarf_expression (dw_loc_descr_ref l)
30007 while (l)
30009 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30010 return l;
30011 switch (l->dw_loc_opc)
30013 case DW_OP_regx:
30014 case DW_OP_implicit_value:
30015 case DW_OP_stack_value:
30016 case DW_OP_implicit_pointer:
30017 case DW_OP_GNU_implicit_pointer:
30018 case DW_OP_GNU_parameter_ref:
30019 case DW_OP_piece:
30020 case DW_OP_bit_piece:
30021 return l;
30022 default:
30023 break;
30025 l = l->dw_loc_next;
30027 return NULL;
30030 /* Return adjusted copy of EXPR:
30031 If it is empty DWARF expression, return it.
30032 If it is valid non-empty DWARF expression,
30033 return copy of EXPR with DW_OP_deref appended to it.
30034 If it is DWARF expression followed by DW_OP_reg{N,x}, return
30035 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
30036 If it is DWARF expression followed by DW_OP_stack_value, return
30037 copy of the DWARF expression without anything appended.
30038 Otherwise, return NULL. */
30040 static dw_loc_descr_ref
30041 copy_deref_exprloc (dw_loc_descr_ref expr)
30043 dw_loc_descr_ref tail = NULL;
30045 if (expr == NULL)
30046 return NULL;
30048 dw_loc_descr_ref l = non_dwarf_expression (expr);
30049 if (l && l->dw_loc_next)
30050 return NULL;
30052 if (l)
30054 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
30055 tail = new_loc_descr ((enum dwarf_location_atom)
30056 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
30057 0, 0);
30058 else
30059 switch (l->dw_loc_opc)
30061 case DW_OP_regx:
30062 tail = new_loc_descr (DW_OP_bregx,
30063 l->dw_loc_oprnd1.v.val_unsigned, 0);
30064 break;
30065 case DW_OP_stack_value:
30066 break;
30067 default:
30068 return NULL;
30071 else
30072 tail = new_loc_descr (DW_OP_deref, 0, 0);
30074 dw_loc_descr_ref ret = NULL, *p = &ret;
30075 while (expr != l)
30077 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
30078 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
30079 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
30080 p = &(*p)->dw_loc_next;
30081 expr = expr->dw_loc_next;
30083 *p = tail;
30084 return ret;
30087 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
30088 reference to a variable or argument, adjust it if needed and return:
30089 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
30090 attribute if present should be removed
30091 0 keep the attribute perhaps with minor modifications, no need to rescan
30092 1 if the attribute has been successfully adjusted. */
30094 static int
30095 optimize_string_length (dw_attr_node *a)
30097 dw_loc_descr_ref l = AT_loc (a), lv;
30098 dw_die_ref die;
30099 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30101 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30102 die = lookup_decl_die (decl);
30103 if (die)
30105 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30106 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30107 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30109 else
30110 return -1;
30112 else
30113 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30115 /* DWARF5 allows reference class, so we can then reference the DIE.
30116 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30117 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30119 a->dw_attr_val.val_class = dw_val_class_die_ref;
30120 a->dw_attr_val.val_entry = NULL;
30121 a->dw_attr_val.v.val_die_ref.die = die;
30122 a->dw_attr_val.v.val_die_ref.external = 0;
30123 return 0;
30126 dw_attr_node *av = get_AT (die, DW_AT_location);
30127 dw_loc_list_ref d;
30128 bool non_dwarf_expr = false;
30130 if (av == NULL)
30131 return dwarf_strict ? -1 : 0;
30132 switch (AT_class (av))
30134 case dw_val_class_loc_list:
30135 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30136 if (d->expr && non_dwarf_expression (d->expr))
30137 non_dwarf_expr = true;
30138 break;
30139 case dw_val_class_view_list:
30140 gcc_unreachable ();
30141 case dw_val_class_loc:
30142 lv = AT_loc (av);
30143 if (lv == NULL)
30144 return dwarf_strict ? -1 : 0;
30145 if (non_dwarf_expression (lv))
30146 non_dwarf_expr = true;
30147 break;
30148 default:
30149 return dwarf_strict ? -1 : 0;
30152 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30153 into DW_OP_call4 or DW_OP_GNU_variable_value into
30154 DW_OP_call4 DW_OP_deref, do so. */
30155 if (!non_dwarf_expr
30156 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30158 l->dw_loc_opc = DW_OP_call4;
30159 if (l->dw_loc_next)
30160 l->dw_loc_next = NULL;
30161 else
30162 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30163 return 0;
30166 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30167 copy over the DW_AT_location attribute from die to a. */
30168 if (l->dw_loc_next != NULL)
30170 a->dw_attr_val = av->dw_attr_val;
30171 return 1;
30174 dw_loc_list_ref list, *p;
30175 switch (AT_class (av))
30177 case dw_val_class_loc_list:
30178 p = &list;
30179 list = NULL;
30180 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30182 lv = copy_deref_exprloc (d->expr);
30183 if (lv)
30185 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30186 p = &(*p)->dw_loc_next;
30188 else if (!dwarf_strict && d->expr)
30189 return 0;
30191 if (list == NULL)
30192 return dwarf_strict ? -1 : 0;
30193 a->dw_attr_val.val_class = dw_val_class_loc_list;
30194 gen_llsym (list);
30195 *AT_loc_list_ptr (a) = list;
30196 return 1;
30197 case dw_val_class_loc:
30198 lv = copy_deref_exprloc (AT_loc (av));
30199 if (lv == NULL)
30200 return dwarf_strict ? -1 : 0;
30201 a->dw_attr_val.v.val_loc = lv;
30202 return 1;
30203 default:
30204 gcc_unreachable ();
30208 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30209 an address in .rodata section if the string literal is emitted there,
30210 or remove the containing location list or replace DW_AT_const_value
30211 with DW_AT_location and empty location expression, if it isn't found
30212 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30213 to something that has been emitted in the current CU. */
30215 static void
30216 resolve_addr (dw_die_ref die)
30218 dw_die_ref c;
30219 dw_attr_node *a;
30220 dw_loc_list_ref *curr, *start, loc;
30221 unsigned ix;
30222 bool remove_AT_byte_size = false;
30224 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30225 switch (AT_class (a))
30227 case dw_val_class_loc_list:
30228 start = curr = AT_loc_list_ptr (a);
30229 loc = *curr;
30230 gcc_assert (loc);
30231 /* The same list can be referenced more than once. See if we have
30232 already recorded the result from a previous pass. */
30233 if (loc->replaced)
30234 *curr = loc->dw_loc_next;
30235 else if (!loc->resolved_addr)
30237 /* As things stand, we do not expect or allow one die to
30238 reference a suffix of another die's location list chain.
30239 References must be identical or completely separate.
30240 There is therefore no need to cache the result of this
30241 pass on any list other than the first; doing so
30242 would lead to unnecessary writes. */
30243 while (*curr)
30245 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30246 if (!resolve_addr_in_expr (a, (*curr)->expr))
30248 dw_loc_list_ref next = (*curr)->dw_loc_next;
30249 dw_loc_descr_ref l = (*curr)->expr;
30251 if (next && (*curr)->ll_symbol)
30253 gcc_assert (!next->ll_symbol);
30254 next->ll_symbol = (*curr)->ll_symbol;
30255 next->vl_symbol = (*curr)->vl_symbol;
30257 if (dwarf_split_debug_info)
30258 remove_loc_list_addr_table_entries (l);
30259 *curr = next;
30261 else
30263 mark_base_types ((*curr)->expr);
30264 curr = &(*curr)->dw_loc_next;
30267 if (loc == *start)
30268 loc->resolved_addr = 1;
30269 else
30271 loc->replaced = 1;
30272 loc->dw_loc_next = *start;
30275 if (!*start)
30277 remove_AT (die, a->dw_attr);
30278 ix--;
30280 break;
30281 case dw_val_class_view_list:
30283 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30284 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30285 dw_val_node *llnode
30286 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30287 /* If we no longer have a loclist, or it no longer needs
30288 views, drop this attribute. */
30289 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30291 remove_AT (die, a->dw_attr);
30292 ix--;
30294 break;
30296 case dw_val_class_loc:
30298 dw_loc_descr_ref l = AT_loc (a);
30299 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30300 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30301 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30302 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30303 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30304 with DW_FORM_ref referencing the same DIE as
30305 DW_OP_GNU_variable_value used to reference. */
30306 if (a->dw_attr == DW_AT_string_length
30307 && l
30308 && l->dw_loc_opc == DW_OP_GNU_variable_value
30309 && (l->dw_loc_next == NULL
30310 || (l->dw_loc_next->dw_loc_next == NULL
30311 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30313 switch (optimize_string_length (a))
30315 case -1:
30316 remove_AT (die, a->dw_attr);
30317 ix--;
30318 /* If we drop DW_AT_string_length, we need to drop also
30319 DW_AT_{string_length_,}byte_size. */
30320 remove_AT_byte_size = true;
30321 continue;
30322 default:
30323 break;
30324 case 1:
30325 /* Even if we keep the optimized DW_AT_string_length,
30326 it might have changed AT_class, so process it again. */
30327 ix--;
30328 continue;
30331 /* For -gdwarf-2 don't attempt to optimize
30332 DW_AT_data_member_location containing
30333 DW_OP_plus_uconst - older consumers might
30334 rely on it being that op instead of a more complex,
30335 but shorter, location description. */
30336 if ((dwarf_version > 2
30337 || a->dw_attr != DW_AT_data_member_location
30338 || l == NULL
30339 || l->dw_loc_opc != DW_OP_plus_uconst
30340 || l->dw_loc_next != NULL)
30341 && !resolve_addr_in_expr (a, l))
30343 if (dwarf_split_debug_info)
30344 remove_loc_list_addr_table_entries (l);
30345 if (l != NULL
30346 && l->dw_loc_next == NULL
30347 && l->dw_loc_opc == DW_OP_addr
30348 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30349 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30350 && a->dw_attr == DW_AT_location)
30352 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30353 remove_AT (die, a->dw_attr);
30354 ix--;
30355 optimize_location_into_implicit_ptr (die, decl);
30356 break;
30358 if (a->dw_attr == DW_AT_string_length)
30359 /* If we drop DW_AT_string_length, we need to drop also
30360 DW_AT_{string_length_,}byte_size. */
30361 remove_AT_byte_size = true;
30362 remove_AT (die, a->dw_attr);
30363 ix--;
30365 else
30366 mark_base_types (l);
30368 break;
30369 case dw_val_class_addr:
30370 if (a->dw_attr == DW_AT_const_value
30371 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30373 if (AT_index (a) != NOT_INDEXED)
30374 remove_addr_table_entry (a->dw_attr_val.val_entry);
30375 remove_AT (die, a->dw_attr);
30376 ix--;
30378 if ((die->die_tag == DW_TAG_call_site
30379 && a->dw_attr == DW_AT_call_origin)
30380 || (die->die_tag == DW_TAG_GNU_call_site
30381 && a->dw_attr == DW_AT_abstract_origin))
30383 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30384 dw_die_ref tdie = lookup_decl_die (tdecl);
30385 dw_die_ref cdie;
30386 if (tdie == NULL
30387 && DECL_EXTERNAL (tdecl)
30388 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30389 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30391 dw_die_ref pdie = cdie;
30392 /* Make sure we don't add these DIEs into type units.
30393 We could emit skeleton DIEs for context (namespaces,
30394 outer structs/classes) and a skeleton DIE for the
30395 innermost context with DW_AT_signature pointing to the
30396 type unit. See PR78835. */
30397 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30398 pdie = pdie->die_parent;
30399 if (pdie == NULL)
30401 /* Creating a full DIE for tdecl is overly expensive and
30402 at this point even wrong when in the LTO phase
30403 as it can end up generating new type DIEs we didn't
30404 output and thus optimize_external_refs will crash. */
30405 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30406 add_AT_flag (tdie, DW_AT_external, 1);
30407 add_AT_flag (tdie, DW_AT_declaration, 1);
30408 add_linkage_attr (tdie, tdecl);
30409 add_name_and_src_coords_attributes (tdie, tdecl, true);
30410 equate_decl_number_to_die (tdecl, tdie);
30413 if (tdie)
30415 a->dw_attr_val.val_class = dw_val_class_die_ref;
30416 a->dw_attr_val.v.val_die_ref.die = tdie;
30417 a->dw_attr_val.v.val_die_ref.external = 0;
30419 else
30421 if (AT_index (a) != NOT_INDEXED)
30422 remove_addr_table_entry (a->dw_attr_val.val_entry);
30423 remove_AT (die, a->dw_attr);
30424 ix--;
30427 break;
30428 default:
30429 break;
30432 if (remove_AT_byte_size)
30433 remove_AT (die, dwarf_version >= 5
30434 ? DW_AT_string_length_byte_size
30435 : DW_AT_byte_size);
30437 FOR_EACH_CHILD (die, c, resolve_addr (c));
30440 /* Helper routines for optimize_location_lists.
30441 This pass tries to share identical local lists in .debug_loc
30442 section. */
30444 /* Iteratively hash operands of LOC opcode into HSTATE. */
30446 static void
30447 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30449 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30450 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30452 switch (loc->dw_loc_opc)
30454 case DW_OP_const4u:
30455 case DW_OP_const8u:
30456 if (loc->dtprel)
30457 goto hash_addr;
30458 /* FALLTHRU */
30459 case DW_OP_const1u:
30460 case DW_OP_const1s:
30461 case DW_OP_const2u:
30462 case DW_OP_const2s:
30463 case DW_OP_const4s:
30464 case DW_OP_const8s:
30465 case DW_OP_constu:
30466 case DW_OP_consts:
30467 case DW_OP_pick:
30468 case DW_OP_plus_uconst:
30469 case DW_OP_breg0:
30470 case DW_OP_breg1:
30471 case DW_OP_breg2:
30472 case DW_OP_breg3:
30473 case DW_OP_breg4:
30474 case DW_OP_breg5:
30475 case DW_OP_breg6:
30476 case DW_OP_breg7:
30477 case DW_OP_breg8:
30478 case DW_OP_breg9:
30479 case DW_OP_breg10:
30480 case DW_OP_breg11:
30481 case DW_OP_breg12:
30482 case DW_OP_breg13:
30483 case DW_OP_breg14:
30484 case DW_OP_breg15:
30485 case DW_OP_breg16:
30486 case DW_OP_breg17:
30487 case DW_OP_breg18:
30488 case DW_OP_breg19:
30489 case DW_OP_breg20:
30490 case DW_OP_breg21:
30491 case DW_OP_breg22:
30492 case DW_OP_breg23:
30493 case DW_OP_breg24:
30494 case DW_OP_breg25:
30495 case DW_OP_breg26:
30496 case DW_OP_breg27:
30497 case DW_OP_breg28:
30498 case DW_OP_breg29:
30499 case DW_OP_breg30:
30500 case DW_OP_breg31:
30501 case DW_OP_regx:
30502 case DW_OP_fbreg:
30503 case DW_OP_piece:
30504 case DW_OP_deref_size:
30505 case DW_OP_xderef_size:
30506 hstate.add_object (val1->v.val_int);
30507 break;
30508 case DW_OP_skip:
30509 case DW_OP_bra:
30511 int offset;
30513 gcc_assert (val1->val_class == dw_val_class_loc);
30514 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30515 hstate.add_object (offset);
30517 break;
30518 case DW_OP_implicit_value:
30519 hstate.add_object (val1->v.val_unsigned);
30520 switch (val2->val_class)
30522 case dw_val_class_const:
30523 hstate.add_object (val2->v.val_int);
30524 break;
30525 case dw_val_class_vec:
30527 unsigned int elt_size = val2->v.val_vec.elt_size;
30528 unsigned int len = val2->v.val_vec.length;
30530 hstate.add_int (elt_size);
30531 hstate.add_int (len);
30532 hstate.add (val2->v.val_vec.array, len * elt_size);
30534 break;
30535 case dw_val_class_const_double:
30536 hstate.add_object (val2->v.val_double.low);
30537 hstate.add_object (val2->v.val_double.high);
30538 break;
30539 case dw_val_class_wide_int:
30540 hstate.add (val2->v.val_wide->get_val (),
30541 get_full_len (*val2->v.val_wide)
30542 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30543 break;
30544 case dw_val_class_addr:
30545 inchash::add_rtx (val2->v.val_addr, hstate);
30546 break;
30547 default:
30548 gcc_unreachable ();
30550 break;
30551 case DW_OP_bregx:
30552 case DW_OP_bit_piece:
30553 hstate.add_object (val1->v.val_int);
30554 hstate.add_object (val2->v.val_int);
30555 break;
30556 case DW_OP_addr:
30557 hash_addr:
30558 if (loc->dtprel)
30560 unsigned char dtprel = 0xd1;
30561 hstate.add_object (dtprel);
30563 inchash::add_rtx (val1->v.val_addr, hstate);
30564 break;
30565 case DW_OP_GNU_addr_index:
30566 case DW_OP_addrx:
30567 case DW_OP_GNU_const_index:
30568 case DW_OP_constx:
30570 if (loc->dtprel)
30572 unsigned char dtprel = 0xd1;
30573 hstate.add_object (dtprel);
30575 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30577 break;
30578 case DW_OP_implicit_pointer:
30579 case DW_OP_GNU_implicit_pointer:
30580 hstate.add_int (val2->v.val_int);
30581 break;
30582 case DW_OP_entry_value:
30583 case DW_OP_GNU_entry_value:
30584 hstate.add_object (val1->v.val_loc);
30585 break;
30586 case DW_OP_regval_type:
30587 case DW_OP_deref_type:
30588 case DW_OP_GNU_regval_type:
30589 case DW_OP_GNU_deref_type:
30591 unsigned int byte_size
30592 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30593 unsigned int encoding
30594 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30595 hstate.add_object (val1->v.val_int);
30596 hstate.add_object (byte_size);
30597 hstate.add_object (encoding);
30599 break;
30600 case DW_OP_convert:
30601 case DW_OP_reinterpret:
30602 case DW_OP_GNU_convert:
30603 case DW_OP_GNU_reinterpret:
30604 if (val1->val_class == dw_val_class_unsigned_const)
30606 hstate.add_object (val1->v.val_unsigned);
30607 break;
30609 /* FALLTHRU */
30610 case DW_OP_const_type:
30611 case DW_OP_GNU_const_type:
30613 unsigned int byte_size
30614 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30615 unsigned int encoding
30616 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30617 hstate.add_object (byte_size);
30618 hstate.add_object (encoding);
30619 if (loc->dw_loc_opc != DW_OP_const_type
30620 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30621 break;
30622 hstate.add_object (val2->val_class);
30623 switch (val2->val_class)
30625 case dw_val_class_const:
30626 hstate.add_object (val2->v.val_int);
30627 break;
30628 case dw_val_class_vec:
30630 unsigned int elt_size = val2->v.val_vec.elt_size;
30631 unsigned int len = val2->v.val_vec.length;
30633 hstate.add_object (elt_size);
30634 hstate.add_object (len);
30635 hstate.add (val2->v.val_vec.array, len * elt_size);
30637 break;
30638 case dw_val_class_const_double:
30639 hstate.add_object (val2->v.val_double.low);
30640 hstate.add_object (val2->v.val_double.high);
30641 break;
30642 case dw_val_class_wide_int:
30643 hstate.add (val2->v.val_wide->get_val (),
30644 get_full_len (*val2->v.val_wide)
30645 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30646 break;
30647 default:
30648 gcc_unreachable ();
30651 break;
30653 default:
30654 /* Other codes have no operands. */
30655 break;
30659 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30661 static inline void
30662 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30664 dw_loc_descr_ref l;
30665 bool sizes_computed = false;
30666 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30667 size_of_locs (loc);
30669 for (l = loc; l != NULL; l = l->dw_loc_next)
30671 enum dwarf_location_atom opc = l->dw_loc_opc;
30672 hstate.add_object (opc);
30673 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30675 size_of_locs (loc);
30676 sizes_computed = true;
30678 hash_loc_operands (l, hstate);
30682 /* Compute hash of the whole location list LIST_HEAD. */
30684 static inline void
30685 hash_loc_list (dw_loc_list_ref list_head)
30687 dw_loc_list_ref curr = list_head;
30688 inchash::hash hstate;
30690 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30692 hstate.add (curr->begin, strlen (curr->begin) + 1);
30693 hstate.add (curr->end, strlen (curr->end) + 1);
30694 hstate.add_object (curr->vbegin);
30695 hstate.add_object (curr->vend);
30696 if (curr->section)
30697 hstate.add (curr->section, strlen (curr->section) + 1);
30698 hash_locs (curr->expr, hstate);
30700 list_head->hash = hstate.end ();
30703 /* Return true if X and Y opcodes have the same operands. */
30705 static inline bool
30706 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30708 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30709 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30710 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30711 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30713 switch (x->dw_loc_opc)
30715 case DW_OP_const4u:
30716 case DW_OP_const8u:
30717 if (x->dtprel)
30718 goto hash_addr;
30719 /* FALLTHRU */
30720 case DW_OP_const1u:
30721 case DW_OP_const1s:
30722 case DW_OP_const2u:
30723 case DW_OP_const2s:
30724 case DW_OP_const4s:
30725 case DW_OP_const8s:
30726 case DW_OP_constu:
30727 case DW_OP_consts:
30728 case DW_OP_pick:
30729 case DW_OP_plus_uconst:
30730 case DW_OP_breg0:
30731 case DW_OP_breg1:
30732 case DW_OP_breg2:
30733 case DW_OP_breg3:
30734 case DW_OP_breg4:
30735 case DW_OP_breg5:
30736 case DW_OP_breg6:
30737 case DW_OP_breg7:
30738 case DW_OP_breg8:
30739 case DW_OP_breg9:
30740 case DW_OP_breg10:
30741 case DW_OP_breg11:
30742 case DW_OP_breg12:
30743 case DW_OP_breg13:
30744 case DW_OP_breg14:
30745 case DW_OP_breg15:
30746 case DW_OP_breg16:
30747 case DW_OP_breg17:
30748 case DW_OP_breg18:
30749 case DW_OP_breg19:
30750 case DW_OP_breg20:
30751 case DW_OP_breg21:
30752 case DW_OP_breg22:
30753 case DW_OP_breg23:
30754 case DW_OP_breg24:
30755 case DW_OP_breg25:
30756 case DW_OP_breg26:
30757 case DW_OP_breg27:
30758 case DW_OP_breg28:
30759 case DW_OP_breg29:
30760 case DW_OP_breg30:
30761 case DW_OP_breg31:
30762 case DW_OP_regx:
30763 case DW_OP_fbreg:
30764 case DW_OP_piece:
30765 case DW_OP_deref_size:
30766 case DW_OP_xderef_size:
30767 return valx1->v.val_int == valy1->v.val_int;
30768 case DW_OP_skip:
30769 case DW_OP_bra:
30770 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30771 can cause irrelevant differences in dw_loc_addr. */
30772 gcc_assert (valx1->val_class == dw_val_class_loc
30773 && valy1->val_class == dw_val_class_loc
30774 && (dwarf_split_debug_info
30775 || x->dw_loc_addr == y->dw_loc_addr));
30776 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30777 case DW_OP_implicit_value:
30778 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30779 || valx2->val_class != valy2->val_class)
30780 return false;
30781 switch (valx2->val_class)
30783 case dw_val_class_const:
30784 return valx2->v.val_int == valy2->v.val_int;
30785 case dw_val_class_vec:
30786 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30787 && valx2->v.val_vec.length == valy2->v.val_vec.length
30788 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30789 valx2->v.val_vec.elt_size
30790 * valx2->v.val_vec.length) == 0;
30791 case dw_val_class_const_double:
30792 return valx2->v.val_double.low == valy2->v.val_double.low
30793 && valx2->v.val_double.high == valy2->v.val_double.high;
30794 case dw_val_class_wide_int:
30795 return *valx2->v.val_wide == *valy2->v.val_wide;
30796 case dw_val_class_addr:
30797 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30798 default:
30799 gcc_unreachable ();
30801 case DW_OP_bregx:
30802 case DW_OP_bit_piece:
30803 return valx1->v.val_int == valy1->v.val_int
30804 && valx2->v.val_int == valy2->v.val_int;
30805 case DW_OP_addr:
30806 hash_addr:
30807 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30808 case DW_OP_GNU_addr_index:
30809 case DW_OP_addrx:
30810 case DW_OP_GNU_const_index:
30811 case DW_OP_constx:
30813 rtx ax1 = valx1->val_entry->addr.rtl;
30814 rtx ay1 = valy1->val_entry->addr.rtl;
30815 return rtx_equal_p (ax1, ay1);
30817 case DW_OP_implicit_pointer:
30818 case DW_OP_GNU_implicit_pointer:
30819 return valx1->val_class == dw_val_class_die_ref
30820 && valx1->val_class == valy1->val_class
30821 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30822 && valx2->v.val_int == valy2->v.val_int;
30823 case DW_OP_entry_value:
30824 case DW_OP_GNU_entry_value:
30825 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30826 case DW_OP_const_type:
30827 case DW_OP_GNU_const_type:
30828 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30829 || valx2->val_class != valy2->val_class)
30830 return false;
30831 switch (valx2->val_class)
30833 case dw_val_class_const:
30834 return valx2->v.val_int == valy2->v.val_int;
30835 case dw_val_class_vec:
30836 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30837 && valx2->v.val_vec.length == valy2->v.val_vec.length
30838 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30839 valx2->v.val_vec.elt_size
30840 * valx2->v.val_vec.length) == 0;
30841 case dw_val_class_const_double:
30842 return valx2->v.val_double.low == valy2->v.val_double.low
30843 && valx2->v.val_double.high == valy2->v.val_double.high;
30844 case dw_val_class_wide_int:
30845 return *valx2->v.val_wide == *valy2->v.val_wide;
30846 default:
30847 gcc_unreachable ();
30849 case DW_OP_regval_type:
30850 case DW_OP_deref_type:
30851 case DW_OP_GNU_regval_type:
30852 case DW_OP_GNU_deref_type:
30853 return valx1->v.val_int == valy1->v.val_int
30854 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30855 case DW_OP_convert:
30856 case DW_OP_reinterpret:
30857 case DW_OP_GNU_convert:
30858 case DW_OP_GNU_reinterpret:
30859 if (valx1->val_class != valy1->val_class)
30860 return false;
30861 if (valx1->val_class == dw_val_class_unsigned_const)
30862 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30863 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30864 case DW_OP_GNU_parameter_ref:
30865 return valx1->val_class == dw_val_class_die_ref
30866 && valx1->val_class == valy1->val_class
30867 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30868 default:
30869 /* Other codes have no operands. */
30870 return true;
30874 /* Return true if DWARF location expressions X and Y are the same. */
30876 static inline bool
30877 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30879 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30880 if (x->dw_loc_opc != y->dw_loc_opc
30881 || x->dtprel != y->dtprel
30882 || !compare_loc_operands (x, y))
30883 break;
30884 return x == NULL && y == NULL;
30887 /* Hashtable helpers. */
30889 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30891 static inline hashval_t hash (const dw_loc_list_struct *);
30892 static inline bool equal (const dw_loc_list_struct *,
30893 const dw_loc_list_struct *);
30896 /* Return precomputed hash of location list X. */
30898 inline hashval_t
30899 loc_list_hasher::hash (const dw_loc_list_struct *x)
30901 return x->hash;
30904 /* Return true if location lists A and B are the same. */
30906 inline bool
30907 loc_list_hasher::equal (const dw_loc_list_struct *a,
30908 const dw_loc_list_struct *b)
30910 if (a == b)
30911 return 1;
30912 if (a->hash != b->hash)
30913 return 0;
30914 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30915 if (strcmp (a->begin, b->begin) != 0
30916 || strcmp (a->end, b->end) != 0
30917 || (a->section == NULL) != (b->section == NULL)
30918 || (a->section && strcmp (a->section, b->section) != 0)
30919 || a->vbegin != b->vbegin || a->vend != b->vend
30920 || !compare_locs (a->expr, b->expr))
30921 break;
30922 return a == NULL && b == NULL;
30925 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30928 /* Recursively optimize location lists referenced from DIE
30929 children and share them whenever possible. */
30931 static void
30932 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30934 dw_die_ref c;
30935 dw_attr_node *a;
30936 unsigned ix;
30937 dw_loc_list_struct **slot;
30938 bool drop_locviews = false;
30939 bool has_locviews = false;
30941 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30942 if (AT_class (a) == dw_val_class_loc_list)
30944 dw_loc_list_ref list = AT_loc_list (a);
30945 /* TODO: perform some optimizations here, before hashing
30946 it and storing into the hash table. */
30947 hash_loc_list (list);
30948 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30949 if (*slot == NULL)
30951 *slot = list;
30952 if (loc_list_has_views (list))
30953 gcc_assert (list->vl_symbol);
30954 else if (list->vl_symbol)
30956 drop_locviews = true;
30957 list->vl_symbol = NULL;
30960 else
30962 if (list->vl_symbol && !(*slot)->vl_symbol)
30963 drop_locviews = true;
30964 a->dw_attr_val.v.val_loc_list = *slot;
30967 else if (AT_class (a) == dw_val_class_view_list)
30969 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30970 has_locviews = true;
30974 if (drop_locviews && has_locviews)
30975 remove_AT (die, DW_AT_GNU_locviews);
30977 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30981 /* Recursively assign each location list a unique index into the debug_addr
30982 section. */
30984 static void
30985 index_location_lists (dw_die_ref die)
30987 dw_die_ref c;
30988 dw_attr_node *a;
30989 unsigned ix;
30991 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30992 if (AT_class (a) == dw_val_class_loc_list)
30994 dw_loc_list_ref list = AT_loc_list (a);
30995 dw_loc_list_ref curr;
30996 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30998 /* Don't index an entry that has already been indexed
30999 or won't be output. Make sure skip_loc_list_entry doesn't
31000 call size_of_locs, because that might cause circular dependency,
31001 index_location_lists requiring address table indexes to be
31002 computed, but adding new indexes through add_addr_table_entry
31003 and address table index computation requiring no new additions
31004 to the hash table. In the rare case of DWARF[234] >= 64KB
31005 location expression, we'll just waste unused address table entry
31006 for it. */
31007 if (curr->begin_entry != NULL
31008 || skip_loc_list_entry (curr))
31009 continue;
31011 curr->begin_entry
31012 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
31016 FOR_EACH_CHILD (die, c, index_location_lists (c));
31019 /* Optimize location lists referenced from DIE
31020 children and share them whenever possible. */
31022 static void
31023 optimize_location_lists (dw_die_ref die)
31025 loc_list_hash_type htab (500);
31026 optimize_location_lists_1 (die, &htab);
31029 /* Traverse the limbo die list, and add parent/child links. The only
31030 dies without parents that should be here are concrete instances of
31031 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
31032 For concrete instances, we can get the parent die from the abstract
31033 instance. */
31035 static void
31036 flush_limbo_die_list (void)
31038 limbo_die_node *node;
31040 /* get_context_die calls force_decl_die, which can put new DIEs on the
31041 limbo list in LTO mode when nested functions are put in a different
31042 partition than that of their parent function. */
31043 while ((node = limbo_die_list))
31045 dw_die_ref die = node->die;
31046 limbo_die_list = node->next;
31048 if (die->die_parent == NULL)
31050 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
31052 if (origin && origin->die_parent)
31053 add_child_die (origin->die_parent, die);
31054 else if (is_cu_die (die))
31056 else if (seen_error ())
31057 /* It's OK to be confused by errors in the input. */
31058 add_child_die (comp_unit_die (), die);
31059 else
31061 /* In certain situations, the lexical block containing a
31062 nested function can be optimized away, which results
31063 in the nested function die being orphaned. Likewise
31064 with the return type of that nested function. Force
31065 this to be a child of the containing function.
31067 It may happen that even the containing function got fully
31068 inlined and optimized out. In that case we are lost and
31069 assign the empty child. This should not be big issue as
31070 the function is likely unreachable too. */
31071 gcc_assert (node->created_for);
31073 if (DECL_P (node->created_for))
31074 origin = get_context_die (DECL_CONTEXT (node->created_for));
31075 else if (TYPE_P (node->created_for))
31076 origin = scope_die_for (node->created_for, comp_unit_die ());
31077 else
31078 origin = comp_unit_die ();
31080 add_child_die (origin, die);
31086 /* Reset DIEs so we can output them again. */
31088 static void
31089 reset_dies (dw_die_ref die)
31091 dw_die_ref c;
31093 /* Remove stuff we re-generate. */
31094 die->die_mark = 0;
31095 die->die_offset = 0;
31096 die->die_abbrev = 0;
31097 remove_AT (die, DW_AT_sibling);
31099 FOR_EACH_CHILD (die, c, reset_dies (c));
31102 /* Output stuff that dwarf requires at the end of every file,
31103 and generate the DWARF-2 debugging info. */
31105 static void
31106 dwarf2out_finish (const char *filename)
31108 comdat_type_node *ctnode;
31109 dw_die_ref main_comp_unit_die;
31110 unsigned char checksum[16];
31111 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31113 /* Flush out any latecomers to the limbo party. */
31114 flush_limbo_die_list ();
31116 if (inline_entry_data_table)
31117 gcc_assert (inline_entry_data_table->elements () == 0);
31119 if (flag_checking)
31121 verify_die (comp_unit_die ());
31122 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31123 verify_die (node->die);
31126 /* We shouldn't have any symbols with delayed asm names for
31127 DIEs generated after early finish. */
31128 gcc_assert (deferred_asm_name == NULL);
31130 gen_remaining_tmpl_value_param_die_attribute ();
31132 if (flag_generate_lto || flag_generate_offload)
31134 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31136 /* Prune stuff so that dwarf2out_finish runs successfully
31137 for the fat part of the object. */
31138 reset_dies (comp_unit_die ());
31139 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31140 reset_dies (node->die);
31142 hash_table<comdat_type_hasher> comdat_type_table (100);
31143 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31145 comdat_type_node **slot
31146 = comdat_type_table.find_slot (ctnode, INSERT);
31148 /* Don't reset types twice. */
31149 if (*slot != HTAB_EMPTY_ENTRY)
31150 continue;
31152 /* Remove the pointer to the line table. */
31153 remove_AT (ctnode->root_die, DW_AT_stmt_list);
31155 if (debug_info_level >= DINFO_LEVEL_TERSE)
31156 reset_dies (ctnode->root_die);
31158 *slot = ctnode;
31161 /* Reset die CU symbol so we don't output it twice. */
31162 comp_unit_die ()->die_id.die_symbol = NULL;
31164 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31165 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31166 if (have_macinfo)
31167 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31169 /* Remove indirect string decisions. */
31170 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31171 if (debug_line_str_hash)
31173 debug_line_str_hash->traverse<void *, reset_indirect_string> (NULL);
31174 debug_line_str_hash = NULL;
31178 #if ENABLE_ASSERT_CHECKING
31180 dw_die_ref die = comp_unit_die (), c;
31181 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31183 #endif
31184 resolve_addr (comp_unit_die ());
31185 move_marked_base_types ();
31187 if (dump_file)
31189 fprintf (dump_file, "DWARF for %s\n", filename);
31190 print_die (comp_unit_die (), dump_file);
31193 /* Initialize sections and labels used for actual assembler output. */
31194 unsigned generation = init_sections_and_labels (false);
31196 /* Traverse the DIE's and add sibling attributes to those DIE's that
31197 have children. */
31198 add_sibling_attributes (comp_unit_die ());
31199 limbo_die_node *node;
31200 for (node = cu_die_list; node; node = node->next)
31201 add_sibling_attributes (node->die);
31202 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31203 add_sibling_attributes (ctnode->root_die);
31205 /* When splitting DWARF info, we put some attributes in the
31206 skeleton compile_unit DIE that remains in the .o, while
31207 most attributes go in the DWO compile_unit_die. */
31208 if (dwarf_split_debug_info)
31210 limbo_die_node *cu;
31211 main_comp_unit_die = gen_compile_unit_die (NULL);
31212 if (dwarf_version >= 5)
31213 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31214 cu = limbo_die_list;
31215 gcc_assert (cu->die == main_comp_unit_die);
31216 limbo_die_list = limbo_die_list->next;
31217 cu->next = cu_die_list;
31218 cu_die_list = cu;
31220 else
31221 main_comp_unit_die = comp_unit_die ();
31223 /* Output a terminator label for the .text section. */
31224 switch_to_section (text_section);
31225 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31226 if (cold_text_section)
31228 switch_to_section (cold_text_section);
31229 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31232 /* We can only use the low/high_pc attributes if all of the code was
31233 in .text. */
31234 if (!have_multiple_function_sections
31235 || (dwarf_version < 3 && dwarf_strict))
31237 /* Don't add if the CU has no associated code. */
31238 if (text_section_used)
31239 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31240 text_end_label, true);
31242 else
31244 unsigned fde_idx;
31245 dw_fde_ref fde;
31246 bool range_list_added = false;
31248 if (text_section_used)
31249 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31250 text_end_label, &range_list_added, true);
31251 if (cold_text_section_used)
31252 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31253 cold_end_label, &range_list_added, true);
31255 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31257 if (DECL_IGNORED_P (fde->decl))
31258 continue;
31259 if (!fde->in_std_section)
31260 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31261 fde->dw_fde_end, &range_list_added,
31262 true);
31263 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31264 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31265 fde->dw_fde_second_end, &range_list_added,
31266 true);
31269 if (range_list_added)
31271 /* We need to give .debug_loc and .debug_ranges an appropriate
31272 "base address". Use zero so that these addresses become
31273 absolute. Historically, we've emitted the unexpected
31274 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31275 Emit both to give time for other tools to adapt. */
31276 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31277 if (! dwarf_strict && dwarf_version < 4)
31278 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31280 add_ranges (NULL);
31284 /* AIX Assembler inserts the length, so adjust the reference to match the
31285 offset expected by debuggers. */
31286 strcpy (dl_section_ref, debug_line_section_label);
31287 if (XCOFF_DEBUGGING_INFO)
31288 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31290 if (debug_info_level >= DINFO_LEVEL_TERSE)
31291 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31292 dl_section_ref);
31294 if (have_macinfo)
31295 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31296 macinfo_section_label);
31298 if (dwarf_split_debug_info)
31300 if (have_location_lists)
31302 /* Since we generate the loclists in the split DWARF .dwo
31303 file itself, we don't need to generate a loclists_base
31304 attribute for the split compile unit DIE. That attribute
31305 (and using relocatable sec_offset FORMs) isn't allowed
31306 for a split compile unit. Only if the .debug_loclists
31307 section was in the main file, would we need to generate a
31308 loclists_base attribute here (for the full or skeleton
31309 unit DIE). */
31311 /* optimize_location_lists calculates the size of the lists,
31312 so index them first, and assign indices to the entries.
31313 Although optimize_location_lists will remove entries from
31314 the table, it only does so for duplicates, and therefore
31315 only reduces ref_counts to 1. */
31316 index_location_lists (comp_unit_die ());
31319 if (addr_index_table != NULL)
31321 unsigned int index = 0;
31322 addr_index_table
31323 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31324 (&index);
31328 loc_list_idx = 0;
31329 if (have_location_lists)
31331 optimize_location_lists (comp_unit_die ());
31332 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31333 if (dwarf_version >= 5 && dwarf_split_debug_info)
31334 assign_location_list_indexes (comp_unit_die ());
31337 save_macinfo_strings ();
31339 if (dwarf_split_debug_info)
31341 unsigned int index = 0;
31343 /* Add attributes common to skeleton compile_units and
31344 type_units. Because these attributes include strings, it
31345 must be done before freezing the string table. Top-level
31346 skeleton die attrs are added when the skeleton type unit is
31347 created, so ensure it is created by this point. */
31348 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31349 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31352 /* Output all of the compilation units. We put the main one last so that
31353 the offsets are available to output_pubnames. */
31354 for (node = cu_die_list; node; node = node->next)
31355 output_comp_unit (node->die, 0, NULL);
31357 hash_table<comdat_type_hasher> comdat_type_table (100);
31358 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31360 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31362 /* Don't output duplicate types. */
31363 if (*slot != HTAB_EMPTY_ENTRY)
31364 continue;
31366 /* Add a pointer to the line table for the main compilation unit
31367 so that the debugger can make sense of DW_AT_decl_file
31368 attributes. */
31369 if (debug_info_level >= DINFO_LEVEL_TERSE)
31370 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31371 (!dwarf_split_debug_info
31372 ? dl_section_ref
31373 : debug_skeleton_line_section_label));
31375 output_comdat_type_unit (ctnode);
31376 *slot = ctnode;
31379 if (dwarf_split_debug_info)
31381 int mark;
31382 struct md5_ctx ctx;
31384 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31385 index_rnglists ();
31387 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31388 md5_init_ctx (&ctx);
31389 mark = 0;
31390 die_checksum (comp_unit_die (), &ctx, &mark);
31391 unmark_all_dies (comp_unit_die ());
31392 md5_finish_ctx (&ctx, checksum);
31394 if (dwarf_version < 5)
31396 /* Use the first 8 bytes of the checksum as the dwo_id,
31397 and add it to both comp-unit DIEs. */
31398 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31399 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31402 /* Add the base offset of the ranges table to the skeleton
31403 comp-unit DIE. */
31404 if (!vec_safe_is_empty (ranges_table))
31406 if (dwarf_version >= 5)
31407 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31408 ranges_base_label);
31409 else
31410 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31411 ranges_section_label);
31414 switch_to_section (debug_addr_section);
31415 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
31416 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
31417 before DWARF5, didn't have a header for .debug_addr units.
31418 DWARF5 specifies a small header when address tables are used. */
31419 if (dwarf_version >= 5)
31421 unsigned int last_idx = 0;
31422 unsigned long addrs_length;
31424 addr_index_table->traverse_noresize
31425 <unsigned int *, count_index_addrs> (&last_idx);
31426 addrs_length = last_idx * DWARF2_ADDR_SIZE + 4;
31428 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31429 dw2_asm_output_data (4, 0xffffffff,
31430 "Escape value for 64-bit DWARF extension");
31431 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
31432 "Length of Address Unit");
31433 dw2_asm_output_data (2, 5, "DWARF addr version");
31434 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
31435 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
31437 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31438 output_addr_table ();
31441 /* Output the main compilation unit if non-empty or if .debug_macinfo
31442 or .debug_macro will be emitted. */
31443 output_comp_unit (comp_unit_die (), have_macinfo,
31444 dwarf_split_debug_info ? checksum : NULL);
31446 if (dwarf_split_debug_info && info_section_emitted)
31447 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31449 /* Output the abbreviation table. */
31450 if (vec_safe_length (abbrev_die_table) != 1)
31452 switch_to_section (debug_abbrev_section);
31453 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31454 output_abbrev_section ();
31457 /* Output location list section if necessary. */
31458 if (have_location_lists)
31460 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31461 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31462 /* Output the location lists info. */
31463 switch_to_section (debug_loc_section);
31464 if (dwarf_version >= 5)
31466 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 2);
31467 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 3);
31468 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31469 dw2_asm_output_data (4, 0xffffffff,
31470 "Initial length escape value indicating "
31471 "64-bit DWARF extension");
31472 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31473 "Length of Location Lists");
31474 ASM_OUTPUT_LABEL (asm_out_file, l1);
31475 output_dwarf_version ();
31476 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31477 dw2_asm_output_data (1, 0, "Segment Size");
31478 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31479 "Offset Entry Count");
31481 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31482 if (dwarf_version >= 5 && dwarf_split_debug_info)
31484 unsigned int save_loc_list_idx = loc_list_idx;
31485 loc_list_idx = 0;
31486 output_loclists_offsets (comp_unit_die ());
31487 gcc_assert (save_loc_list_idx == loc_list_idx);
31489 output_location_lists (comp_unit_die ());
31490 if (dwarf_version >= 5)
31491 ASM_OUTPUT_LABEL (asm_out_file, l2);
31494 output_pubtables ();
31496 /* Output the address range information if a CU (.debug_info section)
31497 was emitted. We output an empty table even if we had no functions
31498 to put in it. This because the consumer has no way to tell the
31499 difference between an empty table that we omitted and failure to
31500 generate a table that would have contained data. */
31501 if (info_section_emitted)
31503 switch_to_section (debug_aranges_section);
31504 output_aranges ();
31507 /* Output ranges section if necessary. */
31508 if (!vec_safe_is_empty (ranges_table))
31510 if (dwarf_version >= 5)
31511 output_rnglists (generation);
31512 else
31513 output_ranges ();
31516 /* Have to end the macro section. */
31517 if (have_macinfo)
31519 switch_to_section (debug_macinfo_section);
31520 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31521 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31522 : debug_skeleton_line_section_label, false);
31523 dw2_asm_output_data (1, 0, "End compilation unit");
31526 /* Output the source line correspondence table. We must do this
31527 even if there is no line information. Otherwise, on an empty
31528 translation unit, we will generate a present, but empty,
31529 .debug_info section. IRIX 6.5 `nm' will then complain when
31530 examining the file. This is done late so that any filenames
31531 used by the debug_info section are marked as 'used'. */
31532 switch_to_section (debug_line_section);
31533 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31534 if (! output_asm_line_debug_info ())
31535 output_line_info (false);
31537 if (dwarf_split_debug_info && info_section_emitted)
31539 switch_to_section (debug_skeleton_line_section);
31540 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31541 output_line_info (true);
31544 /* If we emitted any indirect strings, output the string table too. */
31545 if (debug_str_hash || skeleton_debug_str_hash)
31546 output_indirect_strings ();
31547 if (debug_line_str_hash)
31549 switch_to_section (debug_line_str_section);
31550 const enum dwarf_form form = DW_FORM_line_strp;
31551 debug_line_str_hash->traverse<enum dwarf_form,
31552 output_indirect_string> (form);
31555 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31556 symview_upper_bound = 0;
31557 if (zero_view_p)
31558 bitmap_clear (zero_view_p);
31561 /* Returns a hash value for X (which really is a variable_value_struct). */
31563 inline hashval_t
31564 variable_value_hasher::hash (variable_value_struct *x)
31566 return (hashval_t) x->decl_id;
31569 /* Return nonzero if decl_id of variable_value_struct X is the same as
31570 UID of decl Y. */
31572 inline bool
31573 variable_value_hasher::equal (variable_value_struct *x, tree y)
31575 return x->decl_id == DECL_UID (y);
31578 /* Helper function for resolve_variable_value, handle
31579 DW_OP_GNU_variable_value in one location expression.
31580 Return true if exprloc has been changed into loclist. */
31582 static bool
31583 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31585 dw_loc_descr_ref next;
31586 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31588 next = loc->dw_loc_next;
31589 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31590 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31591 continue;
31593 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31594 if (DECL_CONTEXT (decl) != current_function_decl)
31595 continue;
31597 dw_die_ref ref = lookup_decl_die (decl);
31598 if (ref)
31600 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31601 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31602 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31603 continue;
31605 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31606 if (l == NULL)
31607 continue;
31608 if (l->dw_loc_next)
31610 if (AT_class (a) != dw_val_class_loc)
31611 continue;
31612 switch (a->dw_attr)
31614 /* Following attributes allow both exprloc and loclist
31615 classes, so we can change them into a loclist. */
31616 case DW_AT_location:
31617 case DW_AT_string_length:
31618 case DW_AT_return_addr:
31619 case DW_AT_data_member_location:
31620 case DW_AT_frame_base:
31621 case DW_AT_segment:
31622 case DW_AT_static_link:
31623 case DW_AT_use_location:
31624 case DW_AT_vtable_elem_location:
31625 if (prev)
31627 prev->dw_loc_next = NULL;
31628 prepend_loc_descr_to_each (l, AT_loc (a));
31630 if (next)
31631 add_loc_descr_to_each (l, next);
31632 a->dw_attr_val.val_class = dw_val_class_loc_list;
31633 a->dw_attr_val.val_entry = NULL;
31634 a->dw_attr_val.v.val_loc_list = l;
31635 have_location_lists = true;
31636 return true;
31637 /* Following attributes allow both exprloc and reference,
31638 so if the whole expression is DW_OP_GNU_variable_value alone
31639 we could transform it into reference. */
31640 case DW_AT_byte_size:
31641 case DW_AT_bit_size:
31642 case DW_AT_lower_bound:
31643 case DW_AT_upper_bound:
31644 case DW_AT_bit_stride:
31645 case DW_AT_count:
31646 case DW_AT_allocated:
31647 case DW_AT_associated:
31648 case DW_AT_byte_stride:
31649 if (prev == NULL && next == NULL)
31650 break;
31651 /* FALLTHRU */
31652 default:
31653 if (dwarf_strict)
31654 continue;
31655 break;
31657 /* Create DW_TAG_variable that we can refer to. */
31658 gen_decl_die (decl, NULL_TREE, NULL,
31659 lookup_decl_die (current_function_decl));
31660 ref = lookup_decl_die (decl);
31661 if (ref)
31663 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31664 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31665 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31667 continue;
31669 if (prev)
31671 prev->dw_loc_next = l->expr;
31672 add_loc_descr (&prev->dw_loc_next, next);
31673 free_loc_descr (loc, NULL);
31674 next = prev->dw_loc_next;
31676 else
31678 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31679 add_loc_descr (&loc, next);
31680 next = loc;
31682 loc = prev;
31684 return false;
31687 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31689 static void
31690 resolve_variable_value (dw_die_ref die)
31692 dw_attr_node *a;
31693 dw_loc_list_ref loc;
31694 unsigned ix;
31696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31697 switch (AT_class (a))
31699 case dw_val_class_loc:
31700 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31701 break;
31702 /* FALLTHRU */
31703 case dw_val_class_loc_list:
31704 loc = AT_loc_list (a);
31705 gcc_assert (loc);
31706 for (; loc; loc = loc->dw_loc_next)
31707 resolve_variable_value_in_expr (a, loc->expr);
31708 break;
31709 default:
31710 break;
31714 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31715 temporaries in the current function. */
31717 static void
31718 resolve_variable_values (void)
31720 if (!variable_value_hash || !current_function_decl)
31721 return;
31723 struct variable_value_struct *node
31724 = variable_value_hash->find_with_hash (current_function_decl,
31725 DECL_UID (current_function_decl));
31727 if (node == NULL)
31728 return;
31730 unsigned int i;
31731 dw_die_ref die;
31732 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31733 resolve_variable_value (die);
31736 /* Helper function for note_variable_value, handle one location
31737 expression. */
31739 static void
31740 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31742 for (; loc; loc = loc->dw_loc_next)
31743 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31744 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31746 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31747 dw_die_ref ref = lookup_decl_die (decl);
31748 if (! ref && (flag_generate_lto || flag_generate_offload))
31750 /* ??? This is somewhat a hack because we do not create DIEs
31751 for variables not in BLOCK trees early but when generating
31752 early LTO output we need the dw_val_class_decl_ref to be
31753 fully resolved. For fat LTO objects we'd also like to
31754 undo this after LTO dwarf output. */
31755 gcc_assert (DECL_CONTEXT (decl));
31756 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31757 gcc_assert (ctx != NULL);
31758 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31759 ref = lookup_decl_die (decl);
31760 gcc_assert (ref != NULL);
31762 if (ref)
31764 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31765 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31766 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31767 continue;
31769 if (VAR_P (decl)
31770 && DECL_CONTEXT (decl)
31771 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31772 && lookup_decl_die (DECL_CONTEXT (decl)))
31774 if (!variable_value_hash)
31775 variable_value_hash
31776 = hash_table<variable_value_hasher>::create_ggc (10);
31778 tree fndecl = DECL_CONTEXT (decl);
31779 struct variable_value_struct *node;
31780 struct variable_value_struct **slot
31781 = variable_value_hash->find_slot_with_hash (fndecl,
31782 DECL_UID (fndecl),
31783 INSERT);
31784 if (*slot == NULL)
31786 node = ggc_cleared_alloc<variable_value_struct> ();
31787 node->decl_id = DECL_UID (fndecl);
31788 *slot = node;
31790 else
31791 node = *slot;
31793 vec_safe_push (node->dies, die);
31798 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31799 with dw_val_class_decl_ref operand. */
31801 static void
31802 note_variable_value (dw_die_ref die)
31804 dw_die_ref c;
31805 dw_attr_node *a;
31806 dw_loc_list_ref loc;
31807 unsigned ix;
31809 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31810 switch (AT_class (a))
31812 case dw_val_class_loc_list:
31813 loc = AT_loc_list (a);
31814 gcc_assert (loc);
31815 if (!loc->noted_variable_value)
31817 loc->noted_variable_value = 1;
31818 for (; loc; loc = loc->dw_loc_next)
31819 note_variable_value_in_expr (die, loc->expr);
31821 break;
31822 case dw_val_class_loc:
31823 note_variable_value_in_expr (die, AT_loc (a));
31824 break;
31825 default:
31826 break;
31829 /* Mark children. */
31830 FOR_EACH_CHILD (die, c, note_variable_value (c));
31833 /* Perform any cleanups needed after the early debug generation pass
31834 has run. */
31836 static void
31837 dwarf2out_early_finish (const char *filename)
31839 set_early_dwarf s;
31840 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31842 /* PCH might result in DW_AT_producer string being restored from the
31843 header compilation, so always fill it with empty string initially
31844 and overwrite only here. */
31845 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31846 producer_string = gen_producer_string ();
31847 producer->dw_attr_val.v.val_str->refcount--;
31848 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31850 /* Add the name for the main input file now. We delayed this from
31851 dwarf2out_init to avoid complications with PCH. */
31852 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31853 add_comp_dir_attribute (comp_unit_die ());
31855 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31856 DW_AT_comp_dir into .debug_line_str section. */
31857 if (!output_asm_line_debug_info ()
31858 && dwarf_version >= 5
31859 && DWARF5_USE_DEBUG_LINE_STR)
31861 for (int i = 0; i < 2; i++)
31863 dw_attr_node *a = get_AT (comp_unit_die (),
31864 i ? DW_AT_comp_dir : DW_AT_name);
31865 if (a == NULL
31866 || AT_class (a) != dw_val_class_str
31867 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31868 continue;
31870 if (! debug_line_str_hash)
31871 debug_line_str_hash
31872 = hash_table<indirect_string_hasher>::create_ggc (10);
31874 struct indirect_string_node *node
31875 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31876 set_indirect_string (node);
31877 node->form = DW_FORM_line_strp;
31878 a->dw_attr_val.v.val_str->refcount--;
31879 a->dw_attr_val.v.val_str = node;
31883 /* With LTO early dwarf was really finished at compile-time, so make
31884 sure to adjust the phase after annotating the LTRANS CU DIE. */
31885 if (in_lto_p)
31887 /* Force DW_TAG_imported_unit to be created now, otherwise
31888 we might end up without it or ordered after DW_TAG_inlined_subroutine
31889 referencing DIEs from it. */
31890 if (! flag_wpa && flag_incremental_link != INCREMENTAL_LINK_LTO)
31892 unsigned i;
31893 tree tu;
31894 if (external_die_map)
31895 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, tu)
31896 if (sym_off_pair *desc = external_die_map->get (tu))
31898 dw_die_ref import = new_die (DW_TAG_imported_unit,
31899 comp_unit_die (), NULL_TREE);
31900 add_AT_external_die_ref (import, DW_AT_import,
31901 desc->sym, desc->off);
31905 early_dwarf_finished = true;
31906 if (dump_file)
31908 fprintf (dump_file, "LTO EARLY DWARF for %s\n", filename);
31909 print_die (comp_unit_die (), dump_file);
31911 return;
31914 /* Walk through the list of incomplete types again, trying once more to
31915 emit full debugging info for them. */
31916 retry_incomplete_types ();
31918 /* The point here is to flush out the limbo list so that it is empty
31919 and we don't need to stream it for LTO. */
31920 flush_limbo_die_list ();
31922 gen_scheduled_generic_parms_dies ();
31923 gen_remaining_tmpl_value_param_die_attribute ();
31925 /* Add DW_AT_linkage_name for all deferred DIEs. */
31926 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31928 tree decl = node->created_for;
31929 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31930 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31931 ended up in deferred_asm_name before we knew it was
31932 constant and never written to disk. */
31933 && DECL_ASSEMBLER_NAME (decl))
31935 add_linkage_attr (node->die, decl);
31936 move_linkage_attr (node->die);
31939 deferred_asm_name = NULL;
31941 if (flag_eliminate_unused_debug_types)
31942 prune_unused_types ();
31944 /* Generate separate COMDAT sections for type DIEs. */
31945 if (use_debug_types)
31947 break_out_comdat_types (comp_unit_die ());
31949 /* Each new type_unit DIE was added to the limbo die list when created.
31950 Since these have all been added to comdat_type_list, clear the
31951 limbo die list. */
31952 limbo_die_list = NULL;
31954 /* For each new comdat type unit, copy declarations for incomplete
31955 types to make the new unit self-contained (i.e., no direct
31956 references to the main compile unit). */
31957 for (comdat_type_node *ctnode = comdat_type_list;
31958 ctnode != NULL; ctnode = ctnode->next)
31959 copy_decls_for_unworthy_types (ctnode->root_die);
31960 copy_decls_for_unworthy_types (comp_unit_die ());
31962 /* In the process of copying declarations from one unit to another,
31963 we may have left some declarations behind that are no longer
31964 referenced. Prune them. */
31965 prune_unused_types ();
31968 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31969 with dw_val_class_decl_ref operand. */
31970 note_variable_value (comp_unit_die ());
31971 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31972 note_variable_value (node->die);
31973 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31974 ctnode = ctnode->next)
31975 note_variable_value (ctnode->root_die);
31976 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31977 note_variable_value (node->die);
31979 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31980 both the main_cu and all skeleton TUs. Making this call unconditional
31981 would end up either adding a second copy of the AT_pubnames attribute, or
31982 requiring a special case in add_top_level_skeleton_die_attrs. */
31983 if (!dwarf_split_debug_info)
31984 add_AT_pubnames (comp_unit_die ());
31986 /* The early debug phase is now finished. */
31987 early_dwarf_finished = true;
31988 if (dump_file)
31990 fprintf (dump_file, "EARLY DWARF for %s\n", filename);
31991 print_die (comp_unit_die (), dump_file);
31994 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31995 if ((!flag_generate_lto && !flag_generate_offload)
31996 /* FIXME: Disable debug info generation for (PE-)COFF targets since the
31997 copy_lto_debug_sections operation of the simple object support in
31998 libiberty is not implemented for them yet. */
31999 || TARGET_PECOFF || TARGET_COFF)
32000 return;
32002 /* Now as we are going to output for LTO initialize sections and labels
32003 to the LTO variants. We don't need a random-seed postfix as other
32004 LTO sections as linking the LTO debug sections into one in a partial
32005 link is fine. */
32006 init_sections_and_labels (true);
32008 /* The output below is modeled after dwarf2out_finish with all
32009 location related output removed and some LTO specific changes.
32010 Some refactoring might make both smaller and easier to match up. */
32012 /* Traverse the DIE's and add add sibling attributes to those DIE's
32013 that have children. */
32014 add_sibling_attributes (comp_unit_die ());
32015 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32016 add_sibling_attributes (node->die);
32017 for (comdat_type_node *ctnode = comdat_type_list;
32018 ctnode != NULL; ctnode = ctnode->next)
32019 add_sibling_attributes (ctnode->root_die);
32021 /* AIX Assembler inserts the length, so adjust the reference to match the
32022 offset expected by debuggers. */
32023 strcpy (dl_section_ref, debug_line_section_label);
32024 if (XCOFF_DEBUGGING_INFO)
32025 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
32027 if (debug_info_level >= DINFO_LEVEL_TERSE)
32028 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
32030 if (have_macinfo)
32031 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
32032 macinfo_section_label);
32034 save_macinfo_strings ();
32036 if (dwarf_split_debug_info)
32038 unsigned int index = 0;
32039 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
32042 /* Output all of the compilation units. We put the main one last so that
32043 the offsets are available to output_pubnames. */
32044 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
32045 output_comp_unit (node->die, 0, NULL);
32047 hash_table<comdat_type_hasher> comdat_type_table (100);
32048 for (comdat_type_node *ctnode = comdat_type_list;
32049 ctnode != NULL; ctnode = ctnode->next)
32051 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
32053 /* Don't output duplicate types. */
32054 if (*slot != HTAB_EMPTY_ENTRY)
32055 continue;
32057 /* Add a pointer to the line table for the main compilation unit
32058 so that the debugger can make sense of DW_AT_decl_file
32059 attributes. */
32060 if (debug_info_level >= DINFO_LEVEL_TERSE)
32061 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
32062 (!dwarf_split_debug_info
32063 ? debug_line_section_label
32064 : debug_skeleton_line_section_label));
32066 output_comdat_type_unit (ctnode);
32067 *slot = ctnode;
32070 /* Stick a unique symbol to the main debuginfo section. */
32071 compute_comp_unit_symbol (comp_unit_die ());
32073 /* Output the main compilation unit. We always need it if only for
32074 the CU symbol. */
32075 output_comp_unit (comp_unit_die (), true, NULL);
32077 /* Output the abbreviation table. */
32078 if (vec_safe_length (abbrev_die_table) != 1)
32080 switch_to_section (debug_abbrev_section);
32081 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
32082 output_abbrev_section ();
32085 /* Have to end the macro section. */
32086 if (have_macinfo)
32088 /* We have to save macinfo state if we need to output it again
32089 for the FAT part of the object. */
32090 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
32091 if (flag_fat_lto_objects)
32092 macinfo_table = macinfo_table->copy ();
32094 switch_to_section (debug_macinfo_section);
32095 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
32096 output_macinfo (debug_line_section_label, true);
32097 dw2_asm_output_data (1, 0, "End compilation unit");
32099 if (flag_fat_lto_objects)
32101 vec_free (macinfo_table);
32102 macinfo_table = saved_macinfo_table;
32106 /* Emit a skeleton debug_line section. */
32107 switch_to_section (debug_line_section);
32108 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
32109 output_line_info (true);
32111 /* If we emitted any indirect strings, output the string table too. */
32112 if (debug_str_hash || skeleton_debug_str_hash)
32113 output_indirect_strings ();
32114 if (debug_line_str_hash)
32116 switch_to_section (debug_line_str_section);
32117 const enum dwarf_form form = DW_FORM_line_strp;
32118 debug_line_str_hash->traverse<enum dwarf_form,
32119 output_indirect_string> (form);
32122 /* Switch back to the text section. */
32123 switch_to_section (text_section);
32126 /* Reset all state within dwarf2out.c so that we can rerun the compiler
32127 within the same process. For use by toplev::finalize. */
32129 void
32130 dwarf2out_c_finalize (void)
32132 last_var_location_insn = NULL;
32133 cached_next_real_insn = NULL;
32134 used_rtx_array = NULL;
32135 incomplete_types = NULL;
32136 debug_info_section = NULL;
32137 debug_skeleton_info_section = NULL;
32138 debug_abbrev_section = NULL;
32139 debug_skeleton_abbrev_section = NULL;
32140 debug_aranges_section = NULL;
32141 debug_addr_section = NULL;
32142 debug_macinfo_section = NULL;
32143 debug_line_section = NULL;
32144 debug_skeleton_line_section = NULL;
32145 debug_loc_section = NULL;
32146 debug_pubnames_section = NULL;
32147 debug_pubtypes_section = NULL;
32148 debug_str_section = NULL;
32149 debug_line_str_section = NULL;
32150 debug_str_dwo_section = NULL;
32151 debug_str_offsets_section = NULL;
32152 debug_ranges_section = NULL;
32153 debug_frame_section = NULL;
32154 fde_vec = NULL;
32155 debug_str_hash = NULL;
32156 debug_line_str_hash = NULL;
32157 skeleton_debug_str_hash = NULL;
32158 dw2_string_counter = 0;
32159 have_multiple_function_sections = false;
32160 text_section_used = false;
32161 cold_text_section_used = false;
32162 cold_text_section = NULL;
32163 current_unit_personality = NULL;
32165 early_dwarf = false;
32166 early_dwarf_finished = false;
32168 next_die_offset = 0;
32169 single_comp_unit_die = NULL;
32170 comdat_type_list = NULL;
32171 limbo_die_list = NULL;
32172 file_table = NULL;
32173 decl_die_table = NULL;
32174 common_block_die_table = NULL;
32175 decl_loc_table = NULL;
32176 call_arg_locations = NULL;
32177 call_arg_loc_last = NULL;
32178 call_site_count = -1;
32179 tail_call_site_count = -1;
32180 cached_dw_loc_list_table = NULL;
32181 abbrev_die_table = NULL;
32182 delete dwarf_proc_stack_usage_map;
32183 dwarf_proc_stack_usage_map = NULL;
32184 line_info_label_num = 0;
32185 cur_line_info_table = NULL;
32186 text_section_line_info = NULL;
32187 cold_text_section_line_info = NULL;
32188 separate_line_info = NULL;
32189 info_section_emitted = false;
32190 pubname_table = NULL;
32191 pubtype_table = NULL;
32192 macinfo_table = NULL;
32193 ranges_table = NULL;
32194 ranges_by_label = NULL;
32195 rnglist_idx = 0;
32196 have_location_lists = false;
32197 loclabel_num = 0;
32198 poc_label_num = 0;
32199 last_emitted_file = NULL;
32200 label_num = 0;
32201 tmpl_value_parm_die_table = NULL;
32202 generic_type_instances = NULL;
32203 frame_pointer_fb_offset = 0;
32204 frame_pointer_fb_offset_valid = false;
32205 base_types.release ();
32206 XDELETEVEC (producer_string);
32207 producer_string = NULL;
32210 #include "gt-dwarf2out.h"