Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
blob9fc32fefc59dd84b55aec62d360fa7a5592866d2
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 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 "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260 static GTY(()) int dw2_string_counter;
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
265 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
279 /* Forward declarations for functions defined in this file. */
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
288 /* .debug_rnglists next index. */
289 static unsigned int rnglist_idx;
291 /* Data and reference forms for relocatable data. */
292 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
293 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
295 #ifndef DEBUG_FRAME_SECTION
296 #define DEBUG_FRAME_SECTION ".debug_frame"
297 #endif
299 #ifndef FUNC_BEGIN_LABEL
300 #define FUNC_BEGIN_LABEL "LFB"
301 #endif
303 #ifndef FUNC_END_LABEL
304 #define FUNC_END_LABEL "LFE"
305 #endif
307 #ifndef PROLOGUE_END_LABEL
308 #define PROLOGUE_END_LABEL "LPE"
309 #endif
311 #ifndef EPILOGUE_BEGIN_LABEL
312 #define EPILOGUE_BEGIN_LABEL "LEB"
313 #endif
315 #ifndef FRAME_BEGIN_LABEL
316 #define FRAME_BEGIN_LABEL "Lframe"
317 #endif
318 #define CIE_AFTER_SIZE_LABEL "LSCIE"
319 #define CIE_END_LABEL "LECIE"
320 #define FDE_LABEL "LSFDE"
321 #define FDE_AFTER_SIZE_LABEL "LASFDE"
322 #define FDE_END_LABEL "LEFDE"
323 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
324 #define LINE_NUMBER_END_LABEL "LELT"
325 #define LN_PROLOG_AS_LABEL "LASLTP"
326 #define LN_PROLOG_END_LABEL "LELTP"
327 #define DIE_LABEL_PREFIX "DW"
329 /* Match the base name of a file to the base name of a compilation unit. */
331 static int
332 matches_main_base (const char *path)
334 /* Cache the last query. */
335 static const char *last_path = NULL;
336 static int last_match = 0;
337 if (path != last_path)
339 const char *base;
340 int length = base_of_path (path, &base);
341 last_path = path;
342 last_match = (length == main_input_baselength
343 && memcmp (base, main_input_basename, length) == 0);
345 return last_match;
348 #ifdef DEBUG_DEBUG_STRUCT
350 static int
351 dump_struct_debug (tree type, enum debug_info_usage usage,
352 enum debug_struct_file criterion, int generic,
353 int matches, int result)
355 /* Find the type name. */
356 tree type_decl = TYPE_STUB_DECL (type);
357 tree t = type_decl;
358 const char *name = 0;
359 if (TREE_CODE (t) == TYPE_DECL)
360 t = DECL_NAME (t);
361 if (t)
362 name = IDENTIFIER_POINTER (t);
364 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
365 criterion,
366 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
367 matches ? "bas" : "hdr",
368 generic ? "gen" : "ord",
369 usage == DINFO_USAGE_DFN ? ";" :
370 usage == DINFO_USAGE_DIR_USE ? "." : "*",
371 result,
372 (void*) type_decl, name);
373 return result;
375 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
376 dump_struct_debug (type, usage, criterion, generic, matches, result)
378 #else
380 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
381 (result)
383 #endif
385 /* Get the number of HOST_WIDE_INTs needed to represent the precision
386 of the number. Some constants have a large uniform precision, so
387 we get the precision needed for the actual value of the number. */
389 static unsigned int
390 get_full_len (const wide_int &op)
392 int prec = wi::min_precision (op, UNSIGNED);
393 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
394 / HOST_BITS_PER_WIDE_INT);
397 static bool
398 should_emit_struct_debug (tree type, enum debug_info_usage usage)
400 enum debug_struct_file criterion;
401 tree type_decl;
402 bool generic = lang_hooks.types.generic_p (type);
404 if (generic)
405 criterion = debug_struct_generic[usage];
406 else
407 criterion = debug_struct_ordinary[usage];
409 if (criterion == DINFO_STRUCT_FILE_NONE)
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 if (criterion == DINFO_STRUCT_FILE_ANY)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
416 if (type_decl != NULL)
418 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
421 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
429 switch to the data section instead, and write out a synthetic start label
430 for collect2 the first time around. */
432 static void
433 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
435 if (eh_frame_section == 0)
437 int flags;
439 if (EH_TABLES_CAN_BE_READ_ONLY)
441 int fde_encoding;
442 int per_encoding;
443 int lsda_encoding;
445 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
446 /*global=*/0);
447 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
448 /*global=*/1);
449 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
450 /*global=*/0);
451 flags = ((! flag_pic
452 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
453 && (fde_encoding & 0x70) != DW_EH_PE_aligned
454 && (per_encoding & 0x70) != DW_EH_PE_absptr
455 && (per_encoding & 0x70) != DW_EH_PE_aligned
456 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
457 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
458 ? 0 : SECTION_WRITE);
460 else
461 flags = SECTION_WRITE;
463 #ifdef EH_FRAME_SECTION_NAME
464 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
465 #else
466 eh_frame_section = ((flags == SECTION_WRITE)
467 ? data_section : readonly_data_section);
468 #endif /* EH_FRAME_SECTION_NAME */
471 switch_to_section (eh_frame_section);
473 #ifdef EH_FRAME_THROUGH_COLLECT2
474 /* We have no special eh_frame section. Emit special labels to guide
475 collect2. */
476 if (!back)
478 tree label = get_file_function_name ("F");
479 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
480 targetm.asm_out.globalize_label (asm_out_file,
481 IDENTIFIER_POINTER (label));
482 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
484 #endif
487 /* Switch [BACK] to the eh or debug frame table section, depending on
488 FOR_EH. */
490 static void
491 switch_to_frame_table_section (int for_eh, bool back)
493 if (for_eh)
494 switch_to_eh_frame_section (back);
495 else
497 if (!debug_frame_section)
498 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
499 SECTION_DEBUG, NULL);
500 switch_to_section (debug_frame_section);
504 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
506 enum dw_cfi_oprnd_type
507 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
509 switch (cfi)
511 case DW_CFA_nop:
512 case DW_CFA_GNU_window_save:
513 case DW_CFA_remember_state:
514 case DW_CFA_restore_state:
515 return dw_cfi_oprnd_unused;
517 case DW_CFA_set_loc:
518 case DW_CFA_advance_loc1:
519 case DW_CFA_advance_loc2:
520 case DW_CFA_advance_loc4:
521 case DW_CFA_MIPS_advance_loc8:
522 return dw_cfi_oprnd_addr;
524 case DW_CFA_offset:
525 case DW_CFA_offset_extended:
526 case DW_CFA_def_cfa:
527 case DW_CFA_offset_extended_sf:
528 case DW_CFA_def_cfa_sf:
529 case DW_CFA_restore:
530 case DW_CFA_restore_extended:
531 case DW_CFA_undefined:
532 case DW_CFA_same_value:
533 case DW_CFA_def_cfa_register:
534 case DW_CFA_register:
535 case DW_CFA_expression:
536 case DW_CFA_val_expression:
537 return dw_cfi_oprnd_reg_num;
539 case DW_CFA_def_cfa_offset:
540 case DW_CFA_GNU_args_size:
541 case DW_CFA_def_cfa_offset_sf:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_def_cfa_expression:
545 return dw_cfi_oprnd_loc;
547 default:
548 gcc_unreachable ();
552 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
554 enum dw_cfi_oprnd_type
555 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
557 switch (cfi)
559 case DW_CFA_def_cfa:
560 case DW_CFA_def_cfa_sf:
561 case DW_CFA_offset:
562 case DW_CFA_offset_extended_sf:
563 case DW_CFA_offset_extended:
564 return dw_cfi_oprnd_offset;
566 case DW_CFA_register:
567 return dw_cfi_oprnd_reg_num;
569 case DW_CFA_expression:
570 case DW_CFA_val_expression:
571 return dw_cfi_oprnd_loc;
573 default:
574 return dw_cfi_oprnd_unused;
578 /* Output one FDE. */
580 static void
581 output_fde (dw_fde_ref fde, bool for_eh, bool second,
582 char *section_start_label, int fde_encoding, char *augmentation,
583 bool any_lsda_needed, int lsda_encoding)
585 const char *begin, *end;
586 static unsigned int j;
587 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
589 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
590 /* empty */ 0);
591 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
592 for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
595 if (!XCOFF_DEBUGGING_INFO || for_eh)
597 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
598 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
599 " indicating 64-bit DWARF extension");
600 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
601 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
743 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 unsigned int column ATTRIBUTE_UNUSED,
1035 const char *file ATTRIBUTE_UNUSED)
1037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1038 char * dup_label;
1039 dw_fde_ref fde;
1040 section *fnsec;
1041 bool do_frame;
1043 current_function_func_begin_label = NULL;
1045 do_frame = dwarf2out_do_frame ();
1047 /* ??? current_function_func_begin_label is also used by except.c for
1048 call-site information. We must emit this label if it might be used. */
1049 if (!do_frame
1050 && (!flag_exceptions
1051 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1052 return;
1054 fnsec = function_section (current_function_decl);
1055 switch_to_section (fnsec);
1056 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1057 current_function_funcdef_no);
1058 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 dup_label = xstrdup (label);
1061 current_function_func_begin_label = dup_label;
1063 /* We can elide the fde allocation if we're not emitting debug info. */
1064 if (!do_frame)
1065 return;
1067 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1068 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1069 would include pass_dwarf2_frame. If we've not created the FDE yet,
1070 do so now. */
1071 fde = cfun->fde;
1072 if (fde == NULL)
1073 fde = dwarf2out_alloc_current_fde ();
1075 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1076 fde->dw_fde_begin = dup_label;
1077 fde->dw_fde_current_label = dup_label;
1078 fde->in_std_section = (fnsec == text_section
1079 || (cold_text_section && fnsec == cold_text_section));
1081 /* We only want to output line number information for the genuine dwarf2
1082 prologue case, not the eh frame case. */
1083 #ifdef DWARF2_DEBUGGING_INFO
1084 if (file)
1085 dwarf2out_source_line (line, column, file, 0, true);
1086 #endif
1088 if (dwarf2out_do_cfi_asm ())
1089 dwarf2out_do_cfi_startproc (false);
1090 else
1092 rtx personality = get_personality_function (current_function_decl);
1093 if (!current_unit_personality)
1094 current_unit_personality = personality;
1096 /* We cannot keep a current personality per function as without CFI
1097 asm, at the point where we emit the CFI data, there is no current
1098 function anymore. */
1099 if (personality && current_unit_personality != personality)
1100 sorry ("multiple EH personalities are supported only with assemblers "
1101 "supporting .cfi_personality directive");
1105 /* Output a marker (i.e. a label) for the end of the generated code
1106 for a function prologue. This gets called *after* the prologue code has
1107 been generated. */
1109 void
1110 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1111 const char *file ATTRIBUTE_UNUSED)
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 /* Output a label to mark the endpoint of the code generated for this
1116 function. */
1117 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1118 current_function_funcdef_no);
1119 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1120 current_function_funcdef_no);
1121 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1124 /* Output a marker (i.e. a label) for the beginning of the generated code
1125 for a function epilogue. This gets called *before* the prologue code has
1126 been generated. */
1128 void
1129 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1130 const char *file ATTRIBUTE_UNUSED)
1132 dw_fde_ref fde = cfun->fde;
1133 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135 if (fde->dw_fde_vms_begin_epilogue)
1136 return;
1138 /* Output a label to mark the endpoint of the code generated for this
1139 function. */
1140 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1141 current_function_funcdef_no);
1142 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1143 current_function_funcdef_no);
1144 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1147 /* Output a marker (i.e. a label) for the absolute end of the generated code
1148 for a function definition. This gets called *after* the epilogue code has
1149 been generated. */
1151 void
1152 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1153 const char *file ATTRIBUTE_UNUSED)
1155 dw_fde_ref fde;
1156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158 last_var_location_insn = NULL;
1159 cached_next_real_insn = NULL;
1161 if (dwarf2out_do_cfi_asm ())
1162 fprintf (asm_out_file, "\t.cfi_endproc\n");
1164 /* Output a label to mark the endpoint of the code generated for this
1165 function. */
1166 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1167 current_function_funcdef_no);
1168 ASM_OUTPUT_LABEL (asm_out_file, label);
1169 fde = cfun->fde;
1170 gcc_assert (fde != NULL);
1171 if (fde->dw_fde_second_begin == NULL)
1172 fde->dw_fde_end = xstrdup (label);
1175 void
1176 dwarf2out_frame_finish (void)
1178 /* Output call frame information. */
1179 if (targetm.debug_unwind_info () == UI_DWARF2)
1180 output_call_frame_info (0);
1182 /* Output another copy for the unwinder. */
1183 if ((flag_unwind_tables || flag_exceptions)
1184 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1185 output_call_frame_info (1);
1188 /* Note that the current function section is being used for code. */
1190 static void
1191 dwarf2out_note_section_used (void)
1193 section *sec = current_function_section ();
1194 if (sec == text_section)
1195 text_section_used = true;
1196 else if (sec == cold_text_section)
1197 cold_text_section_used = true;
1200 static void var_location_switch_text_section (void);
1201 static void set_cur_line_info_table (section *);
1203 void
1204 dwarf2out_switch_text_section (void)
1206 section *sect;
1207 dw_fde_ref fde = cfun->fde;
1209 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1211 if (!in_cold_section_p)
1213 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1214 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1215 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1217 else
1219 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1220 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1221 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1223 have_multiple_function_sections = true;
1225 /* There is no need to mark used sections when not debugging. */
1226 if (cold_text_section != NULL)
1227 dwarf2out_note_section_used ();
1229 if (dwarf2out_do_cfi_asm ())
1230 fprintf (asm_out_file, "\t.cfi_endproc\n");
1232 /* Now do the real section switch. */
1233 sect = current_function_section ();
1234 switch_to_section (sect);
1236 fde->second_in_std_section
1237 = (sect == text_section
1238 || (cold_text_section && sect == cold_text_section));
1240 if (dwarf2out_do_cfi_asm ())
1241 dwarf2out_do_cfi_startproc (true);
1243 var_location_switch_text_section ();
1245 if (cold_text_section != NULL)
1246 set_cur_line_info_table (sect);
1249 /* And now, the subset of the debugging information support code necessary
1250 for emitting location expressions. */
1252 /* Data about a single source file. */
1253 struct GTY((for_user)) dwarf_file_data {
1254 const char * filename;
1255 int emitted_number;
1258 /* Describe an entry into the .debug_addr section. */
1260 enum ate_kind {
1261 ate_kind_rtx,
1262 ate_kind_rtx_dtprel,
1263 ate_kind_label
1266 struct GTY((for_user)) addr_table_entry {
1267 enum ate_kind kind;
1268 unsigned int refcount;
1269 unsigned int index;
1270 union addr_table_entry_struct_union
1272 rtx GTY ((tag ("0"))) rtl;
1273 char * GTY ((tag ("1"))) label;
1275 GTY ((desc ("%1.kind"))) addr;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1297 section. */
1298 unsigned char emitted : 1;
1299 /* True if hash field is index rather than hash value. */
1300 unsigned char num_assigned : 1;
1301 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1302 unsigned char offset_emitted : 1;
1303 /* True if note_variable_value_in_expr has been called on it. */
1304 unsigned char noted_variable_value : 1;
1305 /* True if the range should be emitted even if begin and end
1306 are the same. */
1307 bool force;
1308 } dw_loc_list_node;
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1311 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1313 /* Convert a DWARF stack opcode into its string name. */
1315 static const char *
1316 dwarf_stack_op_name (unsigned int op)
1318 const char *name = get_DW_OP_name (op);
1320 if (name != NULL)
1321 return name;
1323 return "OP_<unknown>";
1326 /* Return a pointer to a newly allocated location description. Location
1327 descriptions are simple expression terms that can be strung
1328 together to form more complicated location (address) descriptions. */
1330 static inline dw_loc_descr_ref
1331 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1332 unsigned HOST_WIDE_INT oprnd2)
1334 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1336 descr->dw_loc_opc = op;
1337 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd1.val_entry = NULL;
1339 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1340 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1341 descr->dw_loc_oprnd2.val_entry = NULL;
1342 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1344 return descr;
1347 /* Return a pointer to a newly allocated location description for
1348 REG and OFFSET. */
1350 static inline dw_loc_descr_ref
1351 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1353 if (reg <= 31)
1354 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1355 offset, 0);
1356 else
1357 return new_loc_descr (DW_OP_bregx, reg, offset);
1360 /* Add a location description term to a location description expression. */
1362 static inline void
1363 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1365 dw_loc_descr_ref *d;
1367 /* Find the end of the chain. */
1368 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1371 *d = descr;
1374 /* Compare two location operands for exact equality. */
1376 static bool
1377 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1379 if (a->val_class != b->val_class)
1380 return false;
1381 switch (a->val_class)
1383 case dw_val_class_none:
1384 return true;
1385 case dw_val_class_addr:
1386 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1388 case dw_val_class_offset:
1389 case dw_val_class_unsigned_const:
1390 case dw_val_class_const:
1391 case dw_val_class_unsigned_const_implicit:
1392 case dw_val_class_const_implicit:
1393 case dw_val_class_range_list:
1394 /* These are all HOST_WIDE_INT, signed or unsigned. */
1395 return a->v.val_unsigned == b->v.val_unsigned;
1397 case dw_val_class_loc:
1398 return a->v.val_loc == b->v.val_loc;
1399 case dw_val_class_loc_list:
1400 return a->v.val_loc_list == b->v.val_loc_list;
1401 case dw_val_class_die_ref:
1402 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1403 case dw_val_class_fde_ref:
1404 return a->v.val_fde_index == b->v.val_fde_index;
1405 case dw_val_class_lbl_id:
1406 case dw_val_class_lineptr:
1407 case dw_val_class_macptr:
1408 case dw_val_class_loclistsptr:
1409 case dw_val_class_high_pc:
1410 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1411 case dw_val_class_str:
1412 return a->v.val_str == b->v.val_str;
1413 case dw_val_class_flag:
1414 return a->v.val_flag == b->v.val_flag;
1415 case dw_val_class_file:
1416 case dw_val_class_file_implicit:
1417 return a->v.val_file == b->v.val_file;
1418 case dw_val_class_decl_ref:
1419 return a->v.val_decl_ref == b->v.val_decl_ref;
1421 case dw_val_class_const_double:
1422 return (a->v.val_double.high == b->v.val_double.high
1423 && a->v.val_double.low == b->v.val_double.low);
1425 case dw_val_class_wide_int:
1426 return *a->v.val_wide == *b->v.val_wide;
1428 case dw_val_class_vec:
1430 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1431 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1433 return (a_len == b_len
1434 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1437 case dw_val_class_data8:
1438 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1440 case dw_val_class_vms_delta:
1441 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1442 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1444 case dw_val_class_discr_value:
1445 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1446 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1447 case dw_val_class_discr_list:
1448 /* It makes no sense comparing two discriminant value lists. */
1449 return false;
1451 gcc_unreachable ();
1454 /* Compare two location atoms for exact equality. */
1456 static bool
1457 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1459 if (a->dw_loc_opc != b->dw_loc_opc)
1460 return false;
1462 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1463 address size, but since we always allocate cleared storage it
1464 should be zero for other types of locations. */
1465 if (a->dtprel != b->dtprel)
1466 return false;
1468 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1469 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1472 /* Compare two complete location expressions for exact equality. */
1474 bool
1475 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1477 while (1)
1479 if (a == b)
1480 return true;
1481 if (a == NULL || b == NULL)
1482 return false;
1483 if (!loc_descr_equal_p_1 (a, b))
1484 return false;
1486 a = a->dw_loc_next;
1487 b = b->dw_loc_next;
1492 /* Add a constant OFFSET to a location expression. */
1494 static void
1495 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1497 dw_loc_descr_ref loc;
1498 HOST_WIDE_INT *p;
1500 gcc_assert (*list_head != NULL);
1502 if (!offset)
1503 return;
1505 /* Find the end of the chain. */
1506 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1509 p = NULL;
1510 if (loc->dw_loc_opc == DW_OP_fbreg
1511 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1512 p = &loc->dw_loc_oprnd1.v.val_int;
1513 else if (loc->dw_loc_opc == DW_OP_bregx)
1514 p = &loc->dw_loc_oprnd2.v.val_int;
1516 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1517 offset. Don't optimize if an signed integer overflow would happen. */
1518 if (p != NULL
1519 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1520 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1521 *p += offset;
1523 else if (offset > 0)
1524 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1526 else
1528 loc->dw_loc_next
1529 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1530 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 /* Add a constant OFFSET to a location list. */
1536 static void
1537 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1539 dw_loc_list_ref d;
1540 for (d = list_head; d != NULL; d = d->dw_loc_next)
1541 loc_descr_plus_const (&d->expr, offset);
1544 #define DWARF_REF_SIZE \
1545 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1547 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1548 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1549 DW_FORM_data16 with 128 bits. */
1550 #define DWARF_LARGEST_DATA_FORM_BITS \
1551 (dwarf_version >= 5 ? 128 : 64)
1553 /* Utility inline function for construction of ops that were GNU extension
1554 before DWARF 5. */
1555 static inline enum dwarf_location_atom
1556 dwarf_OP (enum dwarf_location_atom op)
1558 switch (op)
1560 case DW_OP_implicit_pointer:
1561 if (dwarf_version < 5)
1562 return DW_OP_GNU_implicit_pointer;
1563 break;
1565 case DW_OP_entry_value:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_entry_value;
1568 break;
1570 case DW_OP_const_type:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_const_type;
1573 break;
1575 case DW_OP_regval_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_regval_type;
1578 break;
1580 case DW_OP_deref_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_deref_type;
1583 break;
1585 case DW_OP_convert:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_convert;
1588 break;
1590 case DW_OP_reinterpret:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_reinterpret;
1593 break;
1595 default:
1596 break;
1598 return op;
1601 /* Similarly for attributes. */
1602 static inline enum dwarf_attribute
1603 dwarf_AT (enum dwarf_attribute at)
1605 switch (at)
1607 case DW_AT_call_return_pc:
1608 if (dwarf_version < 5)
1609 return DW_AT_low_pc;
1610 break;
1612 case DW_AT_call_tail_call:
1613 if (dwarf_version < 5)
1614 return DW_AT_GNU_tail_call;
1615 break;
1617 case DW_AT_call_origin:
1618 if (dwarf_version < 5)
1619 return DW_AT_abstract_origin;
1620 break;
1622 case DW_AT_call_target:
1623 if (dwarf_version < 5)
1624 return DW_AT_GNU_call_site_target;
1625 break;
1627 case DW_AT_call_target_clobbered:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target_clobbered;
1630 break;
1632 case DW_AT_call_parameter:
1633 if (dwarf_version < 5)
1634 return DW_AT_abstract_origin;
1635 break;
1637 case DW_AT_call_value:
1638 if (dwarf_version < 5)
1639 return DW_AT_GNU_call_site_value;
1640 break;
1642 case DW_AT_call_data_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_data_value;
1645 break;
1647 case DW_AT_call_all_calls:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_all_call_sites;
1650 break;
1652 case DW_AT_call_all_tail_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_tail_call_sites;
1655 break;
1657 case DW_AT_dwo_name:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_dwo_name;
1660 break;
1662 default:
1663 break;
1665 return at;
1668 /* And similarly for tags. */
1669 static inline enum dwarf_tag
1670 dwarf_TAG (enum dwarf_tag tag)
1672 switch (tag)
1674 case DW_TAG_call_site:
1675 if (dwarf_version < 5)
1676 return DW_TAG_GNU_call_site;
1677 break;
1679 case DW_TAG_call_site_parameter:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site_parameter;
1682 break;
1684 default:
1685 break;
1687 return tag;
1690 static unsigned long int get_base_type_offset (dw_die_ref);
1692 /* Return the size of a location descriptor. */
1694 static unsigned long
1695 size_of_loc_descr (dw_loc_descr_ref loc)
1697 unsigned long size = 1;
1699 switch (loc->dw_loc_opc)
1701 case DW_OP_addr:
1702 size += DWARF2_ADDR_SIZE;
1703 break;
1704 case DW_OP_GNU_addr_index:
1705 case DW_OP_GNU_const_index:
1706 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1707 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1708 break;
1709 case DW_OP_const1u:
1710 case DW_OP_const1s:
1711 size += 1;
1712 break;
1713 case DW_OP_const2u:
1714 case DW_OP_const2s:
1715 size += 2;
1716 break;
1717 case DW_OP_const4u:
1718 case DW_OP_const4s:
1719 size += 4;
1720 break;
1721 case DW_OP_const8u:
1722 case DW_OP_const8s:
1723 size += 8;
1724 break;
1725 case DW_OP_constu:
1726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727 break;
1728 case DW_OP_consts:
1729 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730 break;
1731 case DW_OP_pick:
1732 size += 1;
1733 break;
1734 case DW_OP_plus_uconst:
1735 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736 break;
1737 case DW_OP_skip:
1738 case DW_OP_bra:
1739 size += 2;
1740 break;
1741 case DW_OP_breg0:
1742 case DW_OP_breg1:
1743 case DW_OP_breg2:
1744 case DW_OP_breg3:
1745 case DW_OP_breg4:
1746 case DW_OP_breg5:
1747 case DW_OP_breg6:
1748 case DW_OP_breg7:
1749 case DW_OP_breg8:
1750 case DW_OP_breg9:
1751 case DW_OP_breg10:
1752 case DW_OP_breg11:
1753 case DW_OP_breg12:
1754 case DW_OP_breg13:
1755 case DW_OP_breg14:
1756 case DW_OP_breg15:
1757 case DW_OP_breg16:
1758 case DW_OP_breg17:
1759 case DW_OP_breg18:
1760 case DW_OP_breg19:
1761 case DW_OP_breg20:
1762 case DW_OP_breg21:
1763 case DW_OP_breg22:
1764 case DW_OP_breg23:
1765 case DW_OP_breg24:
1766 case DW_OP_breg25:
1767 case DW_OP_breg26:
1768 case DW_OP_breg27:
1769 case DW_OP_breg28:
1770 case DW_OP_breg29:
1771 case DW_OP_breg30:
1772 case DW_OP_breg31:
1773 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774 break;
1775 case DW_OP_regx:
1776 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777 break;
1778 case DW_OP_fbreg:
1779 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780 break;
1781 case DW_OP_bregx:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784 break;
1785 case DW_OP_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 break;
1788 case DW_OP_bit_piece:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791 break;
1792 case DW_OP_deref_size:
1793 case DW_OP_xderef_size:
1794 size += 1;
1795 break;
1796 case DW_OP_call2:
1797 size += 2;
1798 break;
1799 case DW_OP_call4:
1800 size += 4;
1801 break;
1802 case DW_OP_call_ref:
1803 case DW_OP_GNU_variable_value:
1804 size += DWARF_REF_SIZE;
1805 break;
1806 case DW_OP_implicit_value:
1807 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1808 + loc->dw_loc_oprnd1.v.val_unsigned;
1809 break;
1810 case DW_OP_implicit_pointer:
1811 case DW_OP_GNU_implicit_pointer:
1812 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1813 break;
1814 case DW_OP_entry_value:
1815 case DW_OP_GNU_entry_value:
1817 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1818 size += size_of_uleb128 (op_size) + op_size;
1819 break;
1821 case DW_OP_const_type:
1822 case DW_OP_GNU_const_type:
1824 unsigned long o
1825 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1826 size += size_of_uleb128 (o) + 1;
1827 switch (loc->dw_loc_oprnd2.val_class)
1829 case dw_val_class_vec:
1830 size += loc->dw_loc_oprnd2.v.val_vec.length
1831 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1832 break;
1833 case dw_val_class_const:
1834 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_const_double:
1837 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1838 break;
1839 case dw_val_class_wide_int:
1840 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1841 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1842 break;
1843 default:
1844 gcc_unreachable ();
1846 break;
1848 case DW_OP_regval_type:
1849 case DW_OP_GNU_regval_type:
1851 unsigned long o
1852 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1854 + size_of_uleb128 (o);
1856 break;
1857 case DW_OP_deref_type:
1858 case DW_OP_GNU_deref_type:
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1862 size += 1 + size_of_uleb128 (o);
1864 break;
1865 case DW_OP_convert:
1866 case DW_OP_reinterpret:
1867 case DW_OP_GNU_convert:
1868 case DW_OP_GNU_reinterpret:
1869 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 else
1873 unsigned long o
1874 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1875 size += size_of_uleb128 (o);
1877 break;
1878 case DW_OP_GNU_parameter_ref:
1879 size += 4;
1880 break;
1881 default:
1882 break;
1885 return size;
1888 /* Return the size of a series of location descriptors. */
1890 unsigned long
1891 size_of_locs (dw_loc_descr_ref loc)
1893 dw_loc_descr_ref l;
1894 unsigned long size;
1896 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1897 field, to avoid writing to a PCH file. */
1898 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1900 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1901 break;
1902 size += size_of_loc_descr (l);
1904 if (! l)
1905 return size;
1907 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1909 l->dw_loc_addr = size;
1910 size += size_of_loc_descr (l);
1913 return size;
1916 /* Return the size of the value in a DW_AT_discr_value attribute. */
1918 static int
1919 size_of_discr_value (dw_discr_value *discr_value)
1921 if (discr_value->pos)
1922 return size_of_uleb128 (discr_value->v.uval);
1923 else
1924 return size_of_sleb128 (discr_value->v.sval);
1927 /* Return the size of the value in a DW_AT_discr_list attribute. */
1929 static int
1930 size_of_discr_list (dw_discr_list_ref discr_list)
1932 int size = 0;
1934 for (dw_discr_list_ref list = discr_list;
1935 list != NULL;
1936 list = list->dw_discr_next)
1938 /* One byte for the discriminant value descriptor, and then one or two
1939 LEB128 numbers, depending on whether it's a single case label or a
1940 range label. */
1941 size += 1;
1942 size += size_of_discr_value (&list->dw_discr_lower_bound);
1943 if (list->dw_discr_range != 0)
1944 size += size_of_discr_value (&list->dw_discr_upper_bound);
1946 return size;
1949 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1950 static void get_ref_die_offset_label (char *, dw_die_ref);
1951 static unsigned long int get_ref_die_offset (dw_die_ref);
1953 /* Output location description stack opcode's operands (if any).
1954 The for_eh_or_skip parameter controls whether register numbers are
1955 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1956 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1957 info). This should be suppressed for the cases that have not been converted
1958 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1960 static void
1961 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1963 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1964 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1966 switch (loc->dw_loc_opc)
1968 #ifdef DWARF2_DEBUGGING_INFO
1969 case DW_OP_const2u:
1970 case DW_OP_const2s:
1971 dw2_asm_output_data (2, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const4u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const4s:
1984 dw2_asm_output_data (4, val1->v.val_int, NULL);
1985 break;
1986 case DW_OP_const8u:
1987 if (loc->dtprel)
1989 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1990 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1991 val1->v.val_addr);
1992 fputc ('\n', asm_out_file);
1993 break;
1995 /* FALLTHRU */
1996 case DW_OP_const8s:
1997 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1998 dw2_asm_output_data (8, val1->v.val_int, NULL);
1999 break;
2000 case DW_OP_skip:
2001 case DW_OP_bra:
2003 int offset;
2005 gcc_assert (val1->val_class == dw_val_class_loc);
2006 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2008 dw2_asm_output_data (2, offset, NULL);
2010 break;
2011 case DW_OP_implicit_value:
2012 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2013 switch (val2->val_class)
2015 case dw_val_class_const:
2016 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2017 break;
2018 case dw_val_class_vec:
2020 unsigned int elt_size = val2->v.val_vec.elt_size;
2021 unsigned int len = val2->v.val_vec.length;
2022 unsigned int i;
2023 unsigned char *p;
2025 if (elt_size > sizeof (HOST_WIDE_INT))
2027 elt_size /= 2;
2028 len *= 2;
2030 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2031 i < len;
2032 i++, p += elt_size)
2033 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2034 "fp or vector constant word %u", i);
2036 break;
2037 case dw_val_class_const_double:
2039 unsigned HOST_WIDE_INT first, second;
2041 if (WORDS_BIG_ENDIAN)
2043 first = val2->v.val_double.high;
2044 second = val2->v.val_double.low;
2046 else
2048 first = val2->v.val_double.low;
2049 second = val2->v.val_double.high;
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 first, NULL);
2053 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2054 second, NULL);
2056 break;
2057 case dw_val_class_wide_int:
2059 int i;
2060 int len = get_full_len (*val2->v.val_wide);
2061 if (WORDS_BIG_ENDIAN)
2062 for (i = len - 1; i >= 0; --i)
2063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2064 val2->v.val_wide->elt (i), NULL);
2065 else
2066 for (i = 0; i < len; ++i)
2067 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2068 val2->v.val_wide->elt (i), NULL);
2070 break;
2071 case dw_val_class_addr:
2072 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2073 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2074 break;
2075 default:
2076 gcc_unreachable ();
2078 break;
2079 #else
2080 case DW_OP_const2u:
2081 case DW_OP_const2s:
2082 case DW_OP_const4u:
2083 case DW_OP_const4s:
2084 case DW_OP_const8u:
2085 case DW_OP_const8s:
2086 case DW_OP_skip:
2087 case DW_OP_bra:
2088 case DW_OP_implicit_value:
2089 /* We currently don't make any attempt to make sure these are
2090 aligned properly like we do for the main unwind info, so
2091 don't support emitting things larger than a byte if we're
2092 only doing unwinding. */
2093 gcc_unreachable ();
2094 #endif
2095 case DW_OP_const1u:
2096 case DW_OP_const1s:
2097 dw2_asm_output_data (1, val1->v.val_int, NULL);
2098 break;
2099 case DW_OP_constu:
2100 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2101 break;
2102 case DW_OP_consts:
2103 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_pick:
2106 dw2_asm_output_data (1, val1->v.val_int, NULL);
2107 break;
2108 case DW_OP_plus_uconst:
2109 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2110 break;
2111 case DW_OP_breg0:
2112 case DW_OP_breg1:
2113 case DW_OP_breg2:
2114 case DW_OP_breg3:
2115 case DW_OP_breg4:
2116 case DW_OP_breg5:
2117 case DW_OP_breg6:
2118 case DW_OP_breg7:
2119 case DW_OP_breg8:
2120 case DW_OP_breg9:
2121 case DW_OP_breg10:
2122 case DW_OP_breg11:
2123 case DW_OP_breg12:
2124 case DW_OP_breg13:
2125 case DW_OP_breg14:
2126 case DW_OP_breg15:
2127 case DW_OP_breg16:
2128 case DW_OP_breg17:
2129 case DW_OP_breg18:
2130 case DW_OP_breg19:
2131 case DW_OP_breg20:
2132 case DW_OP_breg21:
2133 case DW_OP_breg22:
2134 case DW_OP_breg23:
2135 case DW_OP_breg24:
2136 case DW_OP_breg25:
2137 case DW_OP_breg26:
2138 case DW_OP_breg27:
2139 case DW_OP_breg28:
2140 case DW_OP_breg29:
2141 case DW_OP_breg30:
2142 case DW_OP_breg31:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_regx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2154 break;
2155 case DW_OP_fbreg:
2156 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2157 break;
2158 case DW_OP_bregx:
2160 unsigned r = val1->v.val_unsigned;
2161 if (for_eh_or_skip >= 0)
2162 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2163 gcc_assert (size_of_uleb128 (r)
2164 == size_of_uleb128 (val1->v.val_unsigned));
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2168 break;
2169 case DW_OP_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 break;
2172 case DW_OP_bit_piece:
2173 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2174 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2175 break;
2176 case DW_OP_deref_size:
2177 case DW_OP_xderef_size:
2178 dw2_asm_output_data (1, val1->v.val_int, NULL);
2179 break;
2181 case DW_OP_addr:
2182 if (loc->dtprel)
2184 if (targetm.asm_out.output_dwarf_dtprel)
2186 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2187 DWARF2_ADDR_SIZE,
2188 val1->v.val_addr);
2189 fputc ('\n', asm_out_file);
2191 else
2192 gcc_unreachable ();
2194 else
2196 #ifdef DWARF2_DEBUGGING_INFO
2197 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2198 #else
2199 gcc_unreachable ();
2200 #endif
2202 break;
2204 case DW_OP_GNU_addr_index:
2205 case DW_OP_GNU_const_index:
2206 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2207 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2208 "(index into .debug_addr)");
2209 break;
2211 case DW_OP_call2:
2212 case DW_OP_call4:
2214 unsigned long die_offset
2215 = get_ref_die_offset (val1->v.val_die_ref.die);
2216 /* Make sure the offset has been computed and that we can encode it as
2217 an operand. */
2218 gcc_assert (die_offset > 0
2219 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2220 ? 0xffff
2221 : 0xffffffff));
2222 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2223 die_offset, NULL);
2225 break;
2227 case DW_OP_call_ref:
2228 case DW_OP_GNU_variable_value:
2230 char label[MAX_ARTIFICIAL_LABEL_BYTES
2231 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2232 gcc_assert (val1->val_class == dw_val_class_die_ref);
2233 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2234 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2236 break;
2238 case DW_OP_implicit_pointer:
2239 case DW_OP_GNU_implicit_pointer:
2241 char label[MAX_ARTIFICIAL_LABEL_BYTES
2242 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2243 gcc_assert (val1->val_class == dw_val_class_die_ref);
2244 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2245 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2246 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2248 break;
2250 case DW_OP_entry_value:
2251 case DW_OP_GNU_entry_value:
2252 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2253 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2254 break;
2256 case DW_OP_const_type:
2257 case DW_OP_GNU_const_type:
2259 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2260 gcc_assert (o);
2261 dw2_asm_output_data_uleb128 (o, NULL);
2262 switch (val2->val_class)
2264 case dw_val_class_const:
2265 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2266 dw2_asm_output_data (1, l, NULL);
2267 dw2_asm_output_data (l, val2->v.val_int, NULL);
2268 break;
2269 case dw_val_class_vec:
2271 unsigned int elt_size = val2->v.val_vec.elt_size;
2272 unsigned int len = val2->v.val_vec.length;
2273 unsigned int i;
2274 unsigned char *p;
2276 l = len * elt_size;
2277 dw2_asm_output_data (1, l, NULL);
2278 if (elt_size > sizeof (HOST_WIDE_INT))
2280 elt_size /= 2;
2281 len *= 2;
2283 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2284 i < len;
2285 i++, p += elt_size)
2286 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2287 "fp or vector constant word %u", i);
2289 break;
2290 case dw_val_class_const_double:
2292 unsigned HOST_WIDE_INT first, second;
2293 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2295 dw2_asm_output_data (1, 2 * l, NULL);
2296 if (WORDS_BIG_ENDIAN)
2298 first = val2->v.val_double.high;
2299 second = val2->v.val_double.low;
2301 else
2303 first = val2->v.val_double.low;
2304 second = val2->v.val_double.high;
2306 dw2_asm_output_data (l, first, NULL);
2307 dw2_asm_output_data (l, second, NULL);
2309 break;
2310 case dw_val_class_wide_int:
2312 int i;
2313 int len = get_full_len (*val2->v.val_wide);
2314 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2316 dw2_asm_output_data (1, len * l, NULL);
2317 if (WORDS_BIG_ENDIAN)
2318 for (i = len - 1; i >= 0; --i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2320 else
2321 for (i = 0; i < len; ++i)
2322 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2324 break;
2325 default:
2326 gcc_unreachable ();
2329 break;
2330 case DW_OP_regval_type:
2331 case DW_OP_GNU_regval_type:
2333 unsigned r = val1->v.val_unsigned;
2334 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2335 gcc_assert (o);
2336 if (for_eh_or_skip >= 0)
2338 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2339 gcc_assert (size_of_uleb128 (r)
2340 == size_of_uleb128 (val1->v.val_unsigned));
2342 dw2_asm_output_data_uleb128 (r, NULL);
2343 dw2_asm_output_data_uleb128 (o, NULL);
2345 break;
2346 case DW_OP_deref_type:
2347 case DW_OP_GNU_deref_type:
2349 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2350 gcc_assert (o);
2351 dw2_asm_output_data (1, val1->v.val_int, NULL);
2352 dw2_asm_output_data_uleb128 (o, NULL);
2354 break;
2355 case DW_OP_convert:
2356 case DW_OP_reinterpret:
2357 case DW_OP_GNU_convert:
2358 case DW_OP_GNU_reinterpret:
2359 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2360 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2361 else
2363 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2364 gcc_assert (o);
2365 dw2_asm_output_data_uleb128 (o, NULL);
2367 break;
2369 case DW_OP_GNU_parameter_ref:
2371 unsigned long o;
2372 gcc_assert (val1->val_class == dw_val_class_die_ref);
2373 o = get_ref_die_offset (val1->v.val_die_ref.die);
2374 dw2_asm_output_data (4, o, NULL);
2376 break;
2378 default:
2379 /* Other codes have no operands. */
2380 break;
2384 /* Output a sequence of location operations.
2385 The for_eh_or_skip parameter controls whether register numbers are
2386 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2387 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2388 info). This should be suppressed for the cases that have not been converted
2389 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2391 void
2392 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2394 for (; loc != NULL; loc = loc->dw_loc_next)
2396 enum dwarf_location_atom opc = loc->dw_loc_opc;
2397 /* Output the opcode. */
2398 if (for_eh_or_skip >= 0
2399 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2401 unsigned r = (opc - DW_OP_breg0);
2402 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2403 gcc_assert (r <= 31);
2404 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2406 else if (for_eh_or_skip >= 0
2407 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2409 unsigned r = (opc - DW_OP_reg0);
2410 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2411 gcc_assert (r <= 31);
2412 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2415 dw2_asm_output_data (1, opc,
2416 "%s", dwarf_stack_op_name (opc));
2418 /* Output the operand(s) (if any). */
2419 output_loc_operands (loc, for_eh_or_skip);
2423 /* Output location description stack opcode's operands (if any).
2424 The output is single bytes on a line, suitable for .cfi_escape. */
2426 static void
2427 output_loc_operands_raw (dw_loc_descr_ref loc)
2429 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2430 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2432 switch (loc->dw_loc_opc)
2434 case DW_OP_addr:
2435 case DW_OP_GNU_addr_index:
2436 case DW_OP_GNU_const_index:
2437 case DW_OP_implicit_value:
2438 /* We cannot output addresses in .cfi_escape, only bytes. */
2439 gcc_unreachable ();
2441 case DW_OP_const1u:
2442 case DW_OP_const1s:
2443 case DW_OP_pick:
2444 case DW_OP_deref_size:
2445 case DW_OP_xderef_size:
2446 fputc (',', asm_out_file);
2447 dw2_asm_output_data_raw (1, val1->v.val_int);
2448 break;
2450 case DW_OP_const2u:
2451 case DW_OP_const2s:
2452 fputc (',', asm_out_file);
2453 dw2_asm_output_data_raw (2, val1->v.val_int);
2454 break;
2456 case DW_OP_const4u:
2457 case DW_OP_const4s:
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (4, val1->v.val_int);
2460 break;
2462 case DW_OP_const8u:
2463 case DW_OP_const8s:
2464 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2465 fputc (',', asm_out_file);
2466 dw2_asm_output_data_raw (8, val1->v.val_int);
2467 break;
2469 case DW_OP_skip:
2470 case DW_OP_bra:
2472 int offset;
2474 gcc_assert (val1->val_class == dw_val_class_loc);
2475 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_raw (2, offset);
2480 break;
2482 case DW_OP_regx:
2484 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 fputc (',', asm_out_file);
2488 dw2_asm_output_data_uleb128_raw (r);
2490 break;
2492 case DW_OP_constu:
2493 case DW_OP_plus_uconst:
2494 case DW_OP_piece:
2495 fputc (',', asm_out_file);
2496 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2497 break;
2499 case DW_OP_bit_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2503 break;
2505 case DW_OP_consts:
2506 case DW_OP_breg0:
2507 case DW_OP_breg1:
2508 case DW_OP_breg2:
2509 case DW_OP_breg3:
2510 case DW_OP_breg4:
2511 case DW_OP_breg5:
2512 case DW_OP_breg6:
2513 case DW_OP_breg7:
2514 case DW_OP_breg8:
2515 case DW_OP_breg9:
2516 case DW_OP_breg10:
2517 case DW_OP_breg11:
2518 case DW_OP_breg12:
2519 case DW_OP_breg13:
2520 case DW_OP_breg14:
2521 case DW_OP_breg15:
2522 case DW_OP_breg16:
2523 case DW_OP_breg17:
2524 case DW_OP_breg18:
2525 case DW_OP_breg19:
2526 case DW_OP_breg20:
2527 case DW_OP_breg21:
2528 case DW_OP_breg22:
2529 case DW_OP_breg23:
2530 case DW_OP_breg24:
2531 case DW_OP_breg25:
2532 case DW_OP_breg26:
2533 case DW_OP_breg27:
2534 case DW_OP_breg28:
2535 case DW_OP_breg29:
2536 case DW_OP_breg30:
2537 case DW_OP_breg31:
2538 case DW_OP_fbreg:
2539 fputc (',', asm_out_file);
2540 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2541 break;
2543 case DW_OP_bregx:
2545 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2546 gcc_assert (size_of_uleb128 (r)
2547 == size_of_uleb128 (val1->v.val_unsigned));
2548 fputc (',', asm_out_file);
2549 dw2_asm_output_data_uleb128_raw (r);
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2553 break;
2555 case DW_OP_implicit_pointer:
2556 case DW_OP_entry_value:
2557 case DW_OP_const_type:
2558 case DW_OP_regval_type:
2559 case DW_OP_deref_type:
2560 case DW_OP_convert:
2561 case DW_OP_reinterpret:
2562 case DW_OP_GNU_implicit_pointer:
2563 case DW_OP_GNU_entry_value:
2564 case DW_OP_GNU_const_type:
2565 case DW_OP_GNU_regval_type:
2566 case DW_OP_GNU_deref_type:
2567 case DW_OP_GNU_convert:
2568 case DW_OP_GNU_reinterpret:
2569 case DW_OP_GNU_parameter_ref:
2570 gcc_unreachable ();
2571 break;
2573 default:
2574 /* Other codes have no operands. */
2575 break;
2579 void
2580 output_loc_sequence_raw (dw_loc_descr_ref loc)
2582 while (1)
2584 enum dwarf_location_atom opc = loc->dw_loc_opc;
2585 /* Output the opcode. */
2586 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2588 unsigned r = (opc - DW_OP_breg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2593 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2595 unsigned r = (opc - DW_OP_reg0);
2596 r = DWARF2_FRAME_REG_OUT (r, 1);
2597 gcc_assert (r <= 31);
2598 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2600 /* Output the opcode. */
2601 fprintf (asm_out_file, "%#x", opc);
2602 output_loc_operands_raw (loc);
2604 if (!loc->dw_loc_next)
2605 break;
2606 loc = loc->dw_loc_next;
2608 fputc (',', asm_out_file);
2612 /* This function builds a dwarf location descriptor sequence from a
2613 dw_cfa_location, adding the given OFFSET to the result of the
2614 expression. */
2616 struct dw_loc_descr_node *
2617 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2619 struct dw_loc_descr_node *head, *tmp;
2621 offset += cfa->offset;
2623 if (cfa->indirect)
2625 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2626 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2627 head->dw_loc_oprnd1.val_entry = NULL;
2628 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2629 add_loc_descr (&head, tmp);
2630 if (offset != 0)
2632 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2633 add_loc_descr (&head, tmp);
2636 else
2637 head = new_reg_loc_descr (cfa->reg, offset);
2639 return head;
2642 /* This function builds a dwarf location descriptor sequence for
2643 the address at OFFSET from the CFA when stack is aligned to
2644 ALIGNMENT byte. */
2646 struct dw_loc_descr_node *
2647 build_cfa_aligned_loc (dw_cfa_location *cfa,
2648 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2650 struct dw_loc_descr_node *head;
2651 unsigned int dwarf_fp
2652 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2654 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2655 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2657 head = new_reg_loc_descr (dwarf_fp, 0);
2658 add_loc_descr (&head, int_loc_descriptor (alignment));
2659 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2660 loc_descr_plus_const (&head, offset);
2662 else
2663 head = new_reg_loc_descr (dwarf_fp, offset);
2664 return head;
2667 /* And now, the support for symbolic debugging information. */
2669 /* .debug_str support. */
2671 static void dwarf2out_init (const char *);
2672 static void dwarf2out_finish (const char *);
2673 static void dwarf2out_early_finish (const char *);
2674 static void dwarf2out_assembly_start (void);
2675 static void dwarf2out_define (unsigned int, const char *);
2676 static void dwarf2out_undef (unsigned int, const char *);
2677 static void dwarf2out_start_source_file (unsigned, const char *);
2678 static void dwarf2out_end_source_file (unsigned);
2679 static void dwarf2out_function_decl (tree);
2680 static void dwarf2out_begin_block (unsigned, unsigned);
2681 static void dwarf2out_end_block (unsigned, unsigned);
2682 static bool dwarf2out_ignore_block (const_tree);
2683 static void dwarf2out_early_global_decl (tree);
2684 static void dwarf2out_late_global_decl (tree);
2685 static void dwarf2out_type_decl (tree, int);
2686 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2687 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2688 dw_die_ref);
2689 static void dwarf2out_abstract_function (tree);
2690 static void dwarf2out_var_location (rtx_insn *);
2691 static void dwarf2out_size_function (tree);
2692 static void dwarf2out_begin_function (tree);
2693 static void dwarf2out_end_function (unsigned int);
2694 static void dwarf2out_register_main_translation_unit (tree unit);
2695 static void dwarf2out_set_name (tree, tree);
2696 static void dwarf2out_register_external_die (tree decl, const char *sym,
2697 unsigned HOST_WIDE_INT off);
2698 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2699 unsigned HOST_WIDE_INT *off);
2701 /* The debug hooks structure. */
2703 const struct gcc_debug_hooks dwarf2_debug_hooks =
2705 dwarf2out_init,
2706 dwarf2out_finish,
2707 dwarf2out_early_finish,
2708 dwarf2out_assembly_start,
2709 dwarf2out_define,
2710 dwarf2out_undef,
2711 dwarf2out_start_source_file,
2712 dwarf2out_end_source_file,
2713 dwarf2out_begin_block,
2714 dwarf2out_end_block,
2715 dwarf2out_ignore_block,
2716 dwarf2out_source_line,
2717 dwarf2out_begin_prologue,
2718 #if VMS_DEBUGGING_INFO
2719 dwarf2out_vms_end_prologue,
2720 dwarf2out_vms_begin_epilogue,
2721 #else
2722 debug_nothing_int_charstar,
2723 debug_nothing_int_charstar,
2724 #endif
2725 dwarf2out_end_epilogue,
2726 dwarf2out_begin_function,
2727 dwarf2out_end_function, /* end_function */
2728 dwarf2out_register_main_translation_unit,
2729 dwarf2out_function_decl, /* function_decl */
2730 dwarf2out_early_global_decl,
2731 dwarf2out_late_global_decl,
2732 dwarf2out_type_decl, /* type_decl */
2733 dwarf2out_imported_module_or_decl,
2734 dwarf2out_die_ref_for_decl,
2735 dwarf2out_register_external_die,
2736 debug_nothing_tree, /* deferred_inline_function */
2737 /* The DWARF 2 backend tries to reduce debugging bloat by not
2738 emitting the abstract description of inline functions until
2739 something tries to reference them. */
2740 dwarf2out_abstract_function, /* outlining_inline_function */
2741 debug_nothing_rtx_code_label, /* label */
2742 debug_nothing_int, /* handle_pch */
2743 dwarf2out_var_location,
2744 dwarf2out_size_function, /* size_function */
2745 dwarf2out_switch_text_section,
2746 dwarf2out_set_name,
2747 1, /* start_end_main_source_file */
2748 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2751 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2753 dwarf2out_init,
2754 debug_nothing_charstar,
2755 debug_nothing_charstar,
2756 dwarf2out_assembly_start,
2757 debug_nothing_int_charstar,
2758 debug_nothing_int_charstar,
2759 debug_nothing_int_charstar,
2760 debug_nothing_int,
2761 debug_nothing_int_int, /* begin_block */
2762 debug_nothing_int_int, /* end_block */
2763 debug_true_const_tree, /* ignore_block */
2764 dwarf2out_source_line, /* source_line */
2765 debug_nothing_int_int_charstar, /* begin_prologue */
2766 debug_nothing_int_charstar, /* end_prologue */
2767 debug_nothing_int_charstar, /* begin_epilogue */
2768 debug_nothing_int_charstar, /* end_epilogue */
2769 debug_nothing_tree, /* begin_function */
2770 debug_nothing_int, /* end_function */
2771 debug_nothing_tree, /* register_main_translation_unit */
2772 debug_nothing_tree, /* function_decl */
2773 debug_nothing_tree, /* early_global_decl */
2774 debug_nothing_tree, /* late_global_decl */
2775 debug_nothing_tree_int, /* type_decl */
2776 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2777 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2778 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2779 debug_nothing_tree, /* deferred_inline_function */
2780 debug_nothing_tree, /* outlining_inline_function */
2781 debug_nothing_rtx_code_label, /* label */
2782 debug_nothing_int, /* handle_pch */
2783 debug_nothing_rtx_insn, /* var_location */
2784 debug_nothing_tree, /* size_function */
2785 debug_nothing_void, /* switch_text_section */
2786 debug_nothing_tree_tree, /* set_name */
2787 0, /* start_end_main_source_file */
2788 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2791 /* NOTE: In the comments in this file, many references are made to
2792 "Debugging Information Entries". This term is abbreviated as `DIE'
2793 throughout the remainder of this file. */
2795 /* An internal representation of the DWARF output is built, and then
2796 walked to generate the DWARF debugging info. The walk of the internal
2797 representation is done after the entire program has been compiled.
2798 The types below are used to describe the internal representation. */
2800 /* Whether to put type DIEs into their own section .debug_types instead
2801 of making them part of the .debug_info section. Only supported for
2802 Dwarf V4 or higher and the user didn't disable them through
2803 -fno-debug-types-section. It is more efficient to put them in a
2804 separate comdat sections since the linker will then be able to
2805 remove duplicates. But not all tools support .debug_types sections
2806 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2807 it is DW_UT_type unit type in .debug_info section. */
2809 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2811 /* Various DIE's use offsets relative to the beginning of the
2812 .debug_info section to refer to each other. */
2814 typedef long int dw_offset;
2816 struct comdat_type_node;
2818 /* The entries in the line_info table more-or-less mirror the opcodes
2819 that are used in the real dwarf line table. Arrays of these entries
2820 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2821 supported. */
2823 enum dw_line_info_opcode {
2824 /* Emit DW_LNE_set_address; the operand is the label index. */
2825 LI_set_address,
2827 /* Emit a row to the matrix with the given line. This may be done
2828 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2829 special opcodes. */
2830 LI_set_line,
2832 /* Emit a DW_LNS_set_file. */
2833 LI_set_file,
2835 /* Emit a DW_LNS_set_column. */
2836 LI_set_column,
2838 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2839 LI_negate_stmt,
2841 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2842 LI_set_prologue_end,
2843 LI_set_epilogue_begin,
2845 /* Emit a DW_LNE_set_discriminator. */
2846 LI_set_discriminator
2849 typedef struct GTY(()) dw_line_info_struct {
2850 enum dw_line_info_opcode opcode;
2851 unsigned int val;
2852 } dw_line_info_entry;
2855 struct GTY(()) dw_line_info_table {
2856 /* The label that marks the end of this section. */
2857 const char *end_label;
2859 /* The values for the last row of the matrix, as collected in the table.
2860 These are used to minimize the changes to the next row. */
2861 unsigned int file_num;
2862 unsigned int line_num;
2863 unsigned int column_num;
2864 int discrim_num;
2865 bool is_stmt;
2866 bool in_use;
2868 vec<dw_line_info_entry, va_gc> *entries;
2872 /* Each DIE attribute has a field specifying the attribute kind,
2873 a link to the next attribute in the chain, and an attribute value.
2874 Attributes are typically linked below the DIE they modify. */
2876 typedef struct GTY(()) dw_attr_struct {
2877 enum dwarf_attribute dw_attr;
2878 dw_val_node dw_attr_val;
2880 dw_attr_node;
2883 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2884 The children of each node form a circular list linked by
2885 die_sib. die_child points to the node *before* the "first" child node. */
2887 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2888 union die_symbol_or_type_node
2890 const char * GTY ((tag ("0"))) die_symbol;
2891 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2893 GTY ((desc ("%0.comdat_type_p"))) die_id;
2894 vec<dw_attr_node, va_gc> *die_attr;
2895 dw_die_ref die_parent;
2896 dw_die_ref die_child;
2897 dw_die_ref die_sib;
2898 dw_die_ref die_definition; /* ref from a specification to its definition */
2899 dw_offset die_offset;
2900 unsigned long die_abbrev;
2901 int die_mark;
2902 unsigned int decl_id;
2903 enum dwarf_tag die_tag;
2904 /* Die is used and must not be pruned as unused. */
2905 BOOL_BITFIELD die_perennial_p : 1;
2906 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2907 /* For an external ref to die_symbol if die_offset contains an extra
2908 offset to that symbol. */
2909 BOOL_BITFIELD with_offset : 1;
2910 /* Whether this DIE was removed from the DIE tree, for example via
2911 prune_unused_types. We don't consider those present from the
2912 DIE lookup routines. */
2913 BOOL_BITFIELD removed : 1;
2914 /* Lots of spare bits. */
2916 die_node;
2918 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2919 static bool early_dwarf;
2920 static bool early_dwarf_finished;
2921 struct set_early_dwarf {
2922 bool saved;
2923 set_early_dwarf () : saved(early_dwarf)
2925 gcc_assert (! early_dwarf_finished);
2926 early_dwarf = true;
2928 ~set_early_dwarf () { early_dwarf = saved; }
2931 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2932 #define FOR_EACH_CHILD(die, c, expr) do { \
2933 c = die->die_child; \
2934 if (c) do { \
2935 c = c->die_sib; \
2936 expr; \
2937 } while (c != die->die_child); \
2938 } while (0)
2940 /* The pubname structure */
2942 typedef struct GTY(()) pubname_struct {
2943 dw_die_ref die;
2944 const char *name;
2946 pubname_entry;
2949 struct GTY(()) dw_ranges {
2950 const char *label;
2951 /* If this is positive, it's a block number, otherwise it's a
2952 bitwise-negated index into dw_ranges_by_label. */
2953 int num;
2954 /* Index for the range list for DW_FORM_rnglistx. */
2955 unsigned int idx : 31;
2956 /* True if this range might be possibly in a different section
2957 from previous entry. */
2958 unsigned int maybe_new_sec : 1;
2961 /* A structure to hold a macinfo entry. */
2963 typedef struct GTY(()) macinfo_struct {
2964 unsigned char code;
2965 unsigned HOST_WIDE_INT lineno;
2966 const char *info;
2968 macinfo_entry;
2971 struct GTY(()) dw_ranges_by_label {
2972 const char *begin;
2973 const char *end;
2976 /* The comdat type node structure. */
2977 struct GTY(()) comdat_type_node
2979 dw_die_ref root_die;
2980 dw_die_ref type_die;
2981 dw_die_ref skeleton_die;
2982 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2983 comdat_type_node *next;
2986 /* A list of DIEs for which we can't determine ancestry (parent_die
2987 field) just yet. Later in dwarf2out_finish we will fill in the
2988 missing bits. */
2989 typedef struct GTY(()) limbo_die_struct {
2990 dw_die_ref die;
2991 /* The tree for which this DIE was created. We use this to
2992 determine ancestry later. */
2993 tree created_for;
2994 struct limbo_die_struct *next;
2996 limbo_die_node;
2998 typedef struct skeleton_chain_struct
3000 dw_die_ref old_die;
3001 dw_die_ref new_die;
3002 struct skeleton_chain_struct *parent;
3004 skeleton_chain_node;
3006 /* Define a macro which returns nonzero for a TYPE_DECL which was
3007 implicitly generated for a type.
3009 Note that, unlike the C front-end (which generates a NULL named
3010 TYPE_DECL node for each complete tagged type, each array type,
3011 and each function type node created) the C++ front-end generates
3012 a _named_ TYPE_DECL node for each tagged type node created.
3013 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3014 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3015 front-end, but for each type, tagged or not. */
3017 #define TYPE_DECL_IS_STUB(decl) \
3018 (DECL_NAME (decl) == NULL_TREE \
3019 || (DECL_ARTIFICIAL (decl) \
3020 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3021 /* This is necessary for stub decls that \
3022 appear in nested inline functions. */ \
3023 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3024 && (decl_ultimate_origin (decl) \
3025 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3027 /* Information concerning the compilation unit's programming
3028 language, and compiler version. */
3030 /* Fixed size portion of the DWARF compilation unit header. */
3031 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3032 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3033 + (dwarf_version >= 5 ? 4 : 3))
3035 /* Fixed size portion of the DWARF comdat type unit header. */
3036 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3037 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3038 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3040 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3041 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3044 /* Fixed size portion of public names info. */
3045 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3047 /* Fixed size portion of the address range info. */
3048 #define DWARF_ARANGES_HEADER_SIZE \
3049 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3050 DWARF2_ADDR_SIZE * 2) \
3051 - DWARF_INITIAL_LENGTH_SIZE)
3053 /* Size of padding portion in the address range info. It must be
3054 aligned to twice the pointer size. */
3055 #define DWARF_ARANGES_PAD_SIZE \
3056 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3057 DWARF2_ADDR_SIZE * 2) \
3058 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3060 /* Use assembler line directives if available. */
3061 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3062 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3063 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3064 #else
3065 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3066 #endif
3067 #endif
3069 /* Minimum line offset in a special line info. opcode.
3070 This value was chosen to give a reasonable range of values. */
3071 #define DWARF_LINE_BASE -10
3073 /* First special line opcode - leave room for the standard opcodes. */
3074 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3076 /* Range of line offsets in a special line info. opcode. */
3077 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3079 /* Flag that indicates the initial value of the is_stmt_start flag.
3080 In the present implementation, we do not mark any lines as
3081 the beginning of a source statement, because that information
3082 is not made available by the GCC front-end. */
3083 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3085 /* Maximum number of operations per instruction bundle. */
3086 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3087 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3088 #endif
3090 /* This location is used by calc_die_sizes() to keep track
3091 the offset of each DIE within the .debug_info section. */
3092 static unsigned long next_die_offset;
3094 /* Record the root of the DIE's built for the current compilation unit. */
3095 static GTY(()) dw_die_ref single_comp_unit_die;
3097 /* A list of type DIEs that have been separated into comdat sections. */
3098 static GTY(()) comdat_type_node *comdat_type_list;
3100 /* A list of CU DIEs that have been separated. */
3101 static GTY(()) limbo_die_node *cu_die_list;
3103 /* A list of DIEs with a NULL parent waiting to be relocated. */
3104 static GTY(()) limbo_die_node *limbo_die_list;
3106 /* A list of DIEs for which we may have to generate
3107 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3108 static GTY(()) limbo_die_node *deferred_asm_name;
3110 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3112 typedef const char *compare_type;
3114 static hashval_t hash (dwarf_file_data *);
3115 static bool equal (dwarf_file_data *, const char *);
3118 /* Filenames referenced by this compilation unit. */
3119 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3121 struct decl_die_hasher : ggc_ptr_hash<die_node>
3123 typedef tree compare_type;
3125 static hashval_t hash (die_node *);
3126 static bool equal (die_node *, tree);
3128 /* A hash table of references to DIE's that describe declarations.
3129 The key is a DECL_UID() which is a unique number identifying each decl. */
3130 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3132 struct GTY ((for_user)) variable_value_struct {
3133 unsigned int decl_id;
3134 vec<dw_die_ref, va_gc> *dies;
3137 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3139 typedef tree compare_type;
3141 static hashval_t hash (variable_value_struct *);
3142 static bool equal (variable_value_struct *, tree);
3144 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3145 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3146 DECL_CONTEXT of the referenced VAR_DECLs. */
3147 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3149 struct block_die_hasher : ggc_ptr_hash<die_struct>
3151 static hashval_t hash (die_struct *);
3152 static bool equal (die_struct *, die_struct *);
3155 /* A hash table of references to DIE's that describe COMMON blocks.
3156 The key is DECL_UID() ^ die_parent. */
3157 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3159 typedef struct GTY(()) die_arg_entry_struct {
3160 dw_die_ref die;
3161 tree arg;
3162 } die_arg_entry;
3165 /* Node of the variable location list. */
3166 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3167 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3168 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3169 in mode of the EXPR_LIST node and first EXPR_LIST operand
3170 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3171 location or NULL for padding. For larger bitsizes,
3172 mode is 0 and first operand is a CONCAT with bitsize
3173 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3174 NULL as second operand. */
3175 rtx GTY (()) loc;
3176 const char * GTY (()) label;
3177 struct var_loc_node * GTY (()) next;
3180 /* Variable location list. */
3181 struct GTY ((for_user)) var_loc_list_def {
3182 struct var_loc_node * GTY (()) first;
3184 /* Pointer to the last but one or last element of the
3185 chained list. If the list is empty, both first and
3186 last are NULL, if the list contains just one node
3187 or the last node certainly is not redundant, it points
3188 to the last node, otherwise points to the last but one.
3189 Do not mark it for GC because it is marked through the chain. */
3190 struct var_loc_node * GTY ((skip ("%h"))) last;
3192 /* Pointer to the last element before section switch,
3193 if NULL, either sections weren't switched or first
3194 is after section switch. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3197 /* DECL_UID of the variable decl. */
3198 unsigned int decl_id;
3200 typedef struct var_loc_list_def var_loc_list;
3202 /* Call argument location list. */
3203 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3204 rtx GTY (()) call_arg_loc_note;
3205 const char * GTY (()) label;
3206 tree GTY (()) block;
3207 bool tail_call_p;
3208 rtx GTY (()) symbol_ref;
3209 struct call_arg_loc_node * GTY (()) next;
3213 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3215 typedef const_tree compare_type;
3217 static hashval_t hash (var_loc_list *);
3218 static bool equal (var_loc_list *, const_tree);
3221 /* Table of decl location linked lists. */
3222 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3224 /* Head and tail of call_arg_loc chain. */
3225 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3226 static struct call_arg_loc_node *call_arg_loc_last;
3228 /* Number of call sites in the current function. */
3229 static int call_site_count = -1;
3230 /* Number of tail call sites in the current function. */
3231 static int tail_call_site_count = -1;
3233 /* A cached location list. */
3234 struct GTY ((for_user)) cached_dw_loc_list_def {
3235 /* The DECL_UID of the decl that this entry describes. */
3236 unsigned int decl_id;
3238 /* The cached location list. */
3239 dw_loc_list_ref loc_list;
3241 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3243 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3246 typedef const_tree compare_type;
3248 static hashval_t hash (cached_dw_loc_list *);
3249 static bool equal (cached_dw_loc_list *, const_tree);
3252 /* Table of cached location lists. */
3253 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3255 /* A vector of references to DIE's that are uniquely identified by their tag,
3256 presence/absence of children DIE's, and list of attribute/value pairs. */
3257 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3259 /* A hash map to remember the stack usage for DWARF procedures. The value
3260 stored is the stack size difference between before the DWARF procedure
3261 invokation and after it returned. In other words, for a DWARF procedure
3262 that consumes N stack slots and that pushes M ones, this stores M - N. */
3263 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3265 /* A global counter for generating labels for line number data. */
3266 static unsigned int line_info_label_num;
3268 /* The current table to which we should emit line number information
3269 for the current function. This will be set up at the beginning of
3270 assembly for the function. */
3271 static GTY(()) dw_line_info_table *cur_line_info_table;
3273 /* The two default tables of line number info. */
3274 static GTY(()) dw_line_info_table *text_section_line_info;
3275 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3277 /* The set of all non-default tables of line number info. */
3278 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3280 /* A flag to tell pubnames/types export if there is an info section to
3281 refer to. */
3282 static bool info_section_emitted;
3284 /* A pointer to the base of a table that contains a list of publicly
3285 accessible names. */
3286 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3288 /* A pointer to the base of a table that contains a list of publicly
3289 accessible types. */
3290 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3292 /* A pointer to the base of a table that contains a list of macro
3293 defines/undefines (and file start/end markers). */
3294 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3296 /* True if .debug_macinfo or .debug_macros section is going to be
3297 emitted. */
3298 #define have_macinfo \
3299 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3300 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3301 && !macinfo_table->is_empty ())
3303 /* Vector of dies for which we should generate .debug_ranges info. */
3304 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3306 /* Vector of pairs of labels referenced in ranges_table. */
3307 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3309 /* Whether we have location lists that need outputting */
3310 static GTY(()) bool have_location_lists;
3312 /* Unique label counter. */
3313 static GTY(()) unsigned int loclabel_num;
3315 /* Unique label counter for point-of-call tables. */
3316 static GTY(()) unsigned int poc_label_num;
3318 /* The last file entry emitted by maybe_emit_file(). */
3319 static GTY(()) struct dwarf_file_data * last_emitted_file;
3321 /* Number of internal labels generated by gen_internal_sym(). */
3322 static GTY(()) int label_num;
3324 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3326 /* Instances of generic types for which we need to generate debug
3327 info that describe their generic parameters and arguments. That
3328 generation needs to happen once all types are properly laid out so
3329 we do it at the end of compilation. */
3330 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3332 /* Offset from the "steady-state frame pointer" to the frame base,
3333 within the current function. */
3334 static HOST_WIDE_INT frame_pointer_fb_offset;
3335 static bool frame_pointer_fb_offset_valid;
3337 static vec<dw_die_ref> base_types;
3339 /* Flags to represent a set of attribute classes for attributes that represent
3340 a scalar value (bounds, pointers, ...). */
3341 enum dw_scalar_form
3343 dw_scalar_form_constant = 0x01,
3344 dw_scalar_form_exprloc = 0x02,
3345 dw_scalar_form_reference = 0x04
3348 /* Forward declarations for functions defined in this file. */
3350 static int is_pseudo_reg (const_rtx);
3351 static tree type_main_variant (tree);
3352 static int is_tagged_type (const_tree);
3353 static const char *dwarf_tag_name (unsigned);
3354 static const char *dwarf_attr_name (unsigned);
3355 static const char *dwarf_form_name (unsigned);
3356 static tree decl_ultimate_origin (const_tree);
3357 static tree decl_class_context (tree);
3358 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3359 static inline enum dw_val_class AT_class (dw_attr_node *);
3360 static inline unsigned int AT_index (dw_attr_node *);
3361 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3362 static inline unsigned AT_flag (dw_attr_node *);
3363 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3364 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3365 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3366 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3367 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3368 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3369 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3370 unsigned int, unsigned char *);
3371 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3372 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3373 static inline const char *AT_string (dw_attr_node *);
3374 static enum dwarf_form AT_string_form (dw_attr_node *);
3375 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3376 static void add_AT_specification (dw_die_ref, dw_die_ref);
3377 static inline dw_die_ref AT_ref (dw_attr_node *);
3378 static inline int AT_ref_external (dw_attr_node *);
3379 static inline void set_AT_ref_external (dw_attr_node *, int);
3380 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3381 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3382 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3383 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3384 dw_loc_list_ref);
3385 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3386 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3387 static void remove_addr_table_entry (addr_table_entry *);
3388 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3389 static inline rtx AT_addr (dw_attr_node *);
3390 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3391 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3392 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3393 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3394 const char *);
3395 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3396 unsigned HOST_WIDE_INT);
3397 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3398 unsigned long, bool);
3399 static inline const char *AT_lbl (dw_attr_node *);
3400 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3401 static const char *get_AT_low_pc (dw_die_ref);
3402 static const char *get_AT_hi_pc (dw_die_ref);
3403 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3404 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3405 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3406 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3407 static bool is_cxx (void);
3408 static bool is_cxx (const_tree);
3409 static bool is_fortran (void);
3410 static bool is_ada (void);
3411 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3412 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3413 static void add_child_die (dw_die_ref, dw_die_ref);
3414 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3415 static dw_die_ref lookup_type_die (tree);
3416 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3417 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3418 static void equate_type_number_to_die (tree, dw_die_ref);
3419 static dw_die_ref lookup_decl_die (tree);
3420 static var_loc_list *lookup_decl_loc (const_tree);
3421 static void equate_decl_number_to_die (tree, dw_die_ref);
3422 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3423 static void print_spaces (FILE *);
3424 static void print_die (dw_die_ref, FILE *);
3425 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3426 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3427 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3428 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3429 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3430 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3431 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3432 struct md5_ctx *, int *);
3433 struct checksum_attributes;
3434 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3435 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3436 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3437 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3438 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3439 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3440 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3441 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3442 static int is_type_die (dw_die_ref);
3443 static int is_comdat_die (dw_die_ref);
3444 static inline bool is_template_instantiation (dw_die_ref);
3445 static int is_declaration_die (dw_die_ref);
3446 static int should_move_die_to_comdat (dw_die_ref);
3447 static dw_die_ref clone_as_declaration (dw_die_ref);
3448 static dw_die_ref clone_die (dw_die_ref);
3449 static dw_die_ref clone_tree (dw_die_ref);
3450 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3451 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3452 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3453 static dw_die_ref generate_skeleton (dw_die_ref);
3454 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3455 dw_die_ref,
3456 dw_die_ref);
3457 static void break_out_comdat_types (dw_die_ref);
3458 static void copy_decls_for_unworthy_types (dw_die_ref);
3460 static void add_sibling_attributes (dw_die_ref);
3461 static void output_location_lists (dw_die_ref);
3462 static int constant_size (unsigned HOST_WIDE_INT);
3463 static unsigned long size_of_die (dw_die_ref);
3464 static void calc_die_sizes (dw_die_ref);
3465 static void calc_base_type_die_sizes (void);
3466 static void mark_dies (dw_die_ref);
3467 static void unmark_dies (dw_die_ref);
3468 static void unmark_all_dies (dw_die_ref);
3469 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3470 static unsigned long size_of_aranges (void);
3471 static enum dwarf_form value_format (dw_attr_node *);
3472 static void output_value_format (dw_attr_node *);
3473 static void output_abbrev_section (void);
3474 static void output_die_abbrevs (unsigned long, dw_die_ref);
3475 static void output_die_symbol (dw_die_ref);
3476 static void output_die (dw_die_ref);
3477 static void output_compilation_unit_header (enum dwarf_unit_type);
3478 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3479 static void output_comdat_type_unit (comdat_type_node *);
3480 static const char *dwarf2_name (tree, int);
3481 static void add_pubname (tree, dw_die_ref);
3482 static void add_enumerator_pubname (const char *, dw_die_ref);
3483 static void add_pubname_string (const char *, dw_die_ref);
3484 static void add_pubtype (tree, dw_die_ref);
3485 static void output_pubnames (vec<pubname_entry, va_gc> *);
3486 static void output_aranges (void);
3487 static unsigned int add_ranges (const_tree, bool = false);
3488 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3489 bool *, bool);
3490 static void output_ranges (void);
3491 static dw_line_info_table *new_line_info_table (void);
3492 static void output_line_info (bool);
3493 static void output_file_names (void);
3494 static dw_die_ref base_type_die (tree, bool);
3495 static int is_base_type (tree);
3496 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3497 static int decl_quals (const_tree);
3498 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3499 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3500 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3501 static int type_is_enum (const_tree);
3502 static unsigned int dbx_reg_number (const_rtx);
3503 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3504 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3505 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3506 enum var_init_status);
3507 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3508 enum var_init_status);
3509 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3510 enum var_init_status);
3511 static int is_based_loc (const_rtx);
3512 static bool resolve_one_addr (rtx *);
3513 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3514 enum var_init_status);
3515 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3516 enum var_init_status);
3517 struct loc_descr_context;
3518 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3519 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3520 static dw_loc_list_ref loc_list_from_tree (tree, int,
3521 struct loc_descr_context *);
3522 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3523 struct loc_descr_context *);
3524 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3525 static tree field_type (const_tree);
3526 static unsigned int simple_type_align_in_bits (const_tree);
3527 static unsigned int simple_decl_align_in_bits (const_tree);
3528 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3529 struct vlr_context;
3530 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3531 HOST_WIDE_INT *);
3532 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3533 dw_loc_list_ref);
3534 static void add_data_member_location_attribute (dw_die_ref, tree,
3535 struct vlr_context *);
3536 static bool add_const_value_attribute (dw_die_ref, rtx);
3537 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3538 static void insert_wide_int (const wide_int &, unsigned char *, int);
3539 static void insert_float (const_rtx, unsigned char *);
3540 static rtx rtl_for_decl_location (tree);
3541 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3542 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3543 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3544 static void add_name_attribute (dw_die_ref, const char *);
3545 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3546 static void add_comp_dir_attribute (dw_die_ref);
3547 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3548 struct loc_descr_context *);
3549 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3550 struct loc_descr_context *);
3551 static void add_subscript_info (dw_die_ref, tree, bool);
3552 static void add_byte_size_attribute (dw_die_ref, tree);
3553 static void add_alignment_attribute (dw_die_ref, tree);
3554 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3555 struct vlr_context *);
3556 static void add_bit_size_attribute (dw_die_ref, tree);
3557 static void add_prototyped_attribute (dw_die_ref, tree);
3558 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3559 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3560 static void add_src_coords_attributes (dw_die_ref, tree);
3561 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3562 static void add_discr_value (dw_die_ref, dw_discr_value *);
3563 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3564 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3565 static void push_decl_scope (tree);
3566 static void pop_decl_scope (void);
3567 static dw_die_ref scope_die_for (tree, dw_die_ref);
3568 static inline int local_scope_p (dw_die_ref);
3569 static inline int class_scope_p (dw_die_ref);
3570 static inline int class_or_namespace_scope_p (dw_die_ref);
3571 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3572 static void add_calling_convention_attribute (dw_die_ref, tree);
3573 static const char *type_tag (const_tree);
3574 static tree member_declared_type (const_tree);
3575 #if 0
3576 static const char *decl_start_label (tree);
3577 #endif
3578 static void gen_array_type_die (tree, dw_die_ref);
3579 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3580 #if 0
3581 static void gen_entry_point_die (tree, dw_die_ref);
3582 #endif
3583 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3584 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3585 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3586 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3587 static void gen_formal_types_die (tree, dw_die_ref);
3588 static void gen_subprogram_die (tree, dw_die_ref);
3589 static void gen_variable_die (tree, tree, dw_die_ref);
3590 static void gen_const_die (tree, dw_die_ref);
3591 static void gen_label_die (tree, dw_die_ref);
3592 static void gen_lexical_block_die (tree, dw_die_ref);
3593 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3594 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3595 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3596 static dw_die_ref gen_compile_unit_die (const char *);
3597 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3598 static void gen_member_die (tree, dw_die_ref);
3599 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3600 enum debug_info_usage);
3601 static void gen_subroutine_type_die (tree, dw_die_ref);
3602 static void gen_typedef_die (tree, dw_die_ref);
3603 static void gen_type_die (tree, dw_die_ref);
3604 static void gen_block_die (tree, dw_die_ref);
3605 static void decls_for_scope (tree, dw_die_ref);
3606 static bool is_naming_typedef_decl (const_tree);
3607 static inline dw_die_ref get_context_die (tree);
3608 static void gen_namespace_die (tree, dw_die_ref);
3609 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3610 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3611 static dw_die_ref force_decl_die (tree);
3612 static dw_die_ref force_type_die (tree);
3613 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3614 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3615 static struct dwarf_file_data * lookup_filename (const char *);
3616 static void retry_incomplete_types (void);
3617 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3618 static void gen_generic_params_dies (tree);
3619 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3620 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3621 static void splice_child_die (dw_die_ref, dw_die_ref);
3622 static int file_info_cmp (const void *, const void *);
3623 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3624 const char *, const char *);
3625 static void output_loc_list (dw_loc_list_ref);
3626 static char *gen_internal_sym (const char *);
3627 static bool want_pubnames (void);
3629 static void prune_unmark_dies (dw_die_ref);
3630 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3631 static void prune_unused_types_mark (dw_die_ref, int);
3632 static void prune_unused_types_walk (dw_die_ref);
3633 static void prune_unused_types_walk_attribs (dw_die_ref);
3634 static void prune_unused_types_prune (dw_die_ref);
3635 static void prune_unused_types (void);
3636 static int maybe_emit_file (struct dwarf_file_data *fd);
3637 static inline const char *AT_vms_delta1 (dw_attr_node *);
3638 static inline const char *AT_vms_delta2 (dw_attr_node *);
3639 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3640 const char *, const char *);
3641 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3642 static void gen_remaining_tmpl_value_param_die_attribute (void);
3643 static bool generic_type_p (tree);
3644 static void schedule_generic_params_dies_gen (tree t);
3645 static void gen_scheduled_generic_parms_dies (void);
3646 static void resolve_variable_values (void);
3648 static const char *comp_dir_string (void);
3650 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3652 /* enum for tracking thread-local variables whose address is really an offset
3653 relative to the TLS pointer, which will need link-time relocation, but will
3654 not need relocation by the DWARF consumer. */
3656 enum dtprel_bool
3658 dtprel_false = 0,
3659 dtprel_true = 1
3662 /* Return the operator to use for an address of a variable. For dtprel_true, we
3663 use DW_OP_const*. For regular variables, which need both link-time
3664 relocation and consumer-level relocation (e.g., to account for shared objects
3665 loaded at a random address), we use DW_OP_addr*. */
3667 static inline enum dwarf_location_atom
3668 dw_addr_op (enum dtprel_bool dtprel)
3670 if (dtprel == dtprel_true)
3671 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3672 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3673 else
3674 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3677 /* Return a pointer to a newly allocated address location description. If
3678 dwarf_split_debug_info is true, then record the address with the appropriate
3679 relocation. */
3680 static inline dw_loc_descr_ref
3681 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3683 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3685 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3686 ref->dw_loc_oprnd1.v.val_addr = addr;
3687 ref->dtprel = dtprel;
3688 if (dwarf_split_debug_info)
3689 ref->dw_loc_oprnd1.val_entry
3690 = add_addr_table_entry (addr,
3691 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3692 else
3693 ref->dw_loc_oprnd1.val_entry = NULL;
3695 return ref;
3698 /* Section names used to hold DWARF debugging information. */
3700 #ifndef DEBUG_INFO_SECTION
3701 #define DEBUG_INFO_SECTION ".debug_info"
3702 #endif
3703 #ifndef DEBUG_DWO_INFO_SECTION
3704 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3705 #endif
3706 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3707 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3708 #endif
3709 #ifndef DEBUG_LTO_INFO_SECTION
3710 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3711 #endif
3712 #ifndef DEBUG_ABBREV_SECTION
3713 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3714 #endif
3715 #ifndef DEBUG_DWO_ABBREV_SECTION
3716 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3717 #endif
3718 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3719 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3720 #endif
3721 #ifndef DEBUG_LTO_ABBREV_SECTION
3722 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3723 #endif
3724 #ifndef DEBUG_ARANGES_SECTION
3725 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3726 #endif
3727 #ifndef DEBUG_ADDR_SECTION
3728 #define DEBUG_ADDR_SECTION ".debug_addr"
3729 #endif
3730 #ifndef DEBUG_MACINFO_SECTION
3731 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3732 #endif
3733 #ifndef DEBUG_DWO_MACINFO_SECTION
3734 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3735 #endif
3736 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3737 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3738 #endif
3739 #ifndef DEBUG_LTO_MACINFO_SECTION
3740 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3741 #endif
3742 #ifndef DEBUG_DWO_MACRO_SECTION
3743 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3744 #endif
3745 #ifndef DEBUG_MACRO_SECTION
3746 #define DEBUG_MACRO_SECTION ".debug_macro"
3747 #endif
3748 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3749 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3750 #endif
3751 #ifndef DEBUG_LTO_MACRO_SECTION
3752 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3753 #endif
3754 #ifndef DEBUG_LINE_SECTION
3755 #define DEBUG_LINE_SECTION ".debug_line"
3756 #endif
3757 #ifndef DEBUG_DWO_LINE_SECTION
3758 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3759 #endif
3760 #ifndef DEBUG_LTO_LINE_SECTION
3761 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3762 #endif
3763 #ifndef DEBUG_LOC_SECTION
3764 #define DEBUG_LOC_SECTION ".debug_loc"
3765 #endif
3766 #ifndef DEBUG_DWO_LOC_SECTION
3767 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3768 #endif
3769 #ifndef DEBUG_LOCLISTS_SECTION
3770 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3771 #endif
3772 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3773 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3774 #endif
3775 #ifndef DEBUG_PUBNAMES_SECTION
3776 #define DEBUG_PUBNAMES_SECTION \
3777 ((debug_generate_pub_sections == 2) \
3778 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3779 #endif
3780 #ifndef DEBUG_PUBTYPES_SECTION
3781 #define DEBUG_PUBTYPES_SECTION \
3782 ((debug_generate_pub_sections == 2) \
3783 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3784 #endif
3785 #ifndef DEBUG_STR_OFFSETS_SECTION
3786 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3787 #endif
3788 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3789 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3790 #endif
3791 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3792 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3793 #endif
3794 #ifndef DEBUG_STR_DWO_SECTION
3795 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3796 #endif
3797 #ifndef DEBUG_LTO_STR_DWO_SECTION
3798 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3799 #endif
3800 #ifndef DEBUG_STR_SECTION
3801 #define DEBUG_STR_SECTION ".debug_str"
3802 #endif
3803 #ifndef DEBUG_LTO_STR_SECTION
3804 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3805 #endif
3806 #ifndef DEBUG_RANGES_SECTION
3807 #define DEBUG_RANGES_SECTION ".debug_ranges"
3808 #endif
3809 #ifndef DEBUG_RNGLISTS_SECTION
3810 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3811 #endif
3812 #ifndef DEBUG_LINE_STR_SECTION
3813 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3814 #endif
3816 /* Standard ELF section names for compiled code and data. */
3817 #ifndef TEXT_SECTION_NAME
3818 #define TEXT_SECTION_NAME ".text"
3819 #endif
3821 /* Section flags for .debug_str section. */
3822 #define DEBUG_STR_SECTION_FLAGS \
3823 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3824 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3825 : SECTION_DEBUG)
3827 /* Section flags for .debug_str.dwo section. */
3828 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3830 /* Attribute used to refer to the macro section. */
3831 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3832 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3834 /* Labels we insert at beginning sections we can reference instead of
3835 the section names themselves. */
3837 #ifndef TEXT_SECTION_LABEL
3838 #define TEXT_SECTION_LABEL "Ltext"
3839 #endif
3840 #ifndef COLD_TEXT_SECTION_LABEL
3841 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3842 #endif
3843 #ifndef DEBUG_LINE_SECTION_LABEL
3844 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3845 #endif
3846 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3847 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3848 #endif
3849 #ifndef DEBUG_INFO_SECTION_LABEL
3850 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3851 #endif
3852 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3853 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3854 #endif
3855 #ifndef DEBUG_ABBREV_SECTION_LABEL
3856 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3857 #endif
3858 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3859 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3860 #endif
3861 #ifndef DEBUG_ADDR_SECTION_LABEL
3862 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3863 #endif
3864 #ifndef DEBUG_LOC_SECTION_LABEL
3865 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3866 #endif
3867 #ifndef DEBUG_RANGES_SECTION_LABEL
3868 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3869 #endif
3870 #ifndef DEBUG_MACINFO_SECTION_LABEL
3871 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3872 #endif
3873 #ifndef DEBUG_MACRO_SECTION_LABEL
3874 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3875 #endif
3876 #define SKELETON_COMP_DIE_ABBREV 1
3877 #define SKELETON_TYPE_DIE_ABBREV 2
3879 /* Definitions of defaults for formats and names of various special
3880 (artificial) labels which may be generated within this file (when the -g
3881 options is used and DWARF2_DEBUGGING_INFO is in effect.
3882 If necessary, these may be overridden from within the tm.h file, but
3883 typically, overriding these defaults is unnecessary. */
3885 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3886 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3887 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3888 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3889 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3890 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3891 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3892 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3901 #ifndef TEXT_END_LABEL
3902 #define TEXT_END_LABEL "Letext"
3903 #endif
3904 #ifndef COLD_END_LABEL
3905 #define COLD_END_LABEL "Letext_cold"
3906 #endif
3907 #ifndef BLOCK_BEGIN_LABEL
3908 #define BLOCK_BEGIN_LABEL "LBB"
3909 #endif
3910 #ifndef BLOCK_END_LABEL
3911 #define BLOCK_END_LABEL "LBE"
3912 #endif
3913 #ifndef LINE_CODE_LABEL
3914 #define LINE_CODE_LABEL "LM"
3915 #endif
3918 /* Return the root of the DIE's built for the current compilation unit. */
3919 static dw_die_ref
3920 comp_unit_die (void)
3922 if (!single_comp_unit_die)
3923 single_comp_unit_die = gen_compile_unit_die (NULL);
3924 return single_comp_unit_die;
3927 /* We allow a language front-end to designate a function that is to be
3928 called to "demangle" any name before it is put into a DIE. */
3930 static const char *(*demangle_name_func) (const char *);
3932 void
3933 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3935 demangle_name_func = func;
3938 /* Test if rtl node points to a pseudo register. */
3940 static inline int
3941 is_pseudo_reg (const_rtx rtl)
3943 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3944 || (GET_CODE (rtl) == SUBREG
3945 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3948 /* Return a reference to a type, with its const and volatile qualifiers
3949 removed. */
3951 static inline tree
3952 type_main_variant (tree type)
3954 type = TYPE_MAIN_VARIANT (type);
3956 /* ??? There really should be only one main variant among any group of
3957 variants of a given type (and all of the MAIN_VARIANT values for all
3958 members of the group should point to that one type) but sometimes the C
3959 front-end messes this up for array types, so we work around that bug
3960 here. */
3961 if (TREE_CODE (type) == ARRAY_TYPE)
3962 while (type != TYPE_MAIN_VARIANT (type))
3963 type = TYPE_MAIN_VARIANT (type);
3965 return type;
3968 /* Return nonzero if the given type node represents a tagged type. */
3970 static inline int
3971 is_tagged_type (const_tree type)
3973 enum tree_code code = TREE_CODE (type);
3975 return (code == RECORD_TYPE || code == UNION_TYPE
3976 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3979 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3981 static void
3982 get_ref_die_offset_label (char *label, dw_die_ref ref)
3984 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3987 /* Return die_offset of a DIE reference to a base type. */
3989 static unsigned long int
3990 get_base_type_offset (dw_die_ref ref)
3992 if (ref->die_offset)
3993 return ref->die_offset;
3994 if (comp_unit_die ()->die_abbrev)
3996 calc_base_type_die_sizes ();
3997 gcc_assert (ref->die_offset);
3999 return ref->die_offset;
4002 /* Return die_offset of a DIE reference other than base type. */
4004 static unsigned long int
4005 get_ref_die_offset (dw_die_ref ref)
4007 gcc_assert (ref->die_offset);
4008 return ref->die_offset;
4011 /* Convert a DIE tag into its string name. */
4013 static const char *
4014 dwarf_tag_name (unsigned int tag)
4016 const char *name = get_DW_TAG_name (tag);
4018 if (name != NULL)
4019 return name;
4021 return "DW_TAG_<unknown>";
4024 /* Convert a DWARF attribute code into its string name. */
4026 static const char *
4027 dwarf_attr_name (unsigned int attr)
4029 const char *name;
4031 switch (attr)
4033 #if VMS_DEBUGGING_INFO
4034 case DW_AT_HP_prologue:
4035 return "DW_AT_HP_prologue";
4036 #else
4037 case DW_AT_MIPS_loop_unroll_factor:
4038 return "DW_AT_MIPS_loop_unroll_factor";
4039 #endif
4041 #if VMS_DEBUGGING_INFO
4042 case DW_AT_HP_epilogue:
4043 return "DW_AT_HP_epilogue";
4044 #else
4045 case DW_AT_MIPS_stride:
4046 return "DW_AT_MIPS_stride";
4047 #endif
4050 name = get_DW_AT_name (attr);
4052 if (name != NULL)
4053 return name;
4055 return "DW_AT_<unknown>";
4058 /* Convert a DWARF value form code into its string name. */
4060 static const char *
4061 dwarf_form_name (unsigned int form)
4063 const char *name = get_DW_FORM_name (form);
4065 if (name != NULL)
4066 return name;
4068 return "DW_FORM_<unknown>";
4071 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4072 instance of an inlined instance of a decl which is local to an inline
4073 function, so we have to trace all of the way back through the origin chain
4074 to find out what sort of node actually served as the original seed for the
4075 given block. */
4077 static tree
4078 decl_ultimate_origin (const_tree decl)
4080 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4081 return NULL_TREE;
4083 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4084 we're trying to output the abstract instance of this function. */
4085 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4086 return NULL_TREE;
4088 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4089 most distant ancestor, this should never happen. */
4090 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4092 return DECL_ABSTRACT_ORIGIN (decl);
4095 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4096 of a virtual function may refer to a base class, so we check the 'this'
4097 parameter. */
4099 static tree
4100 decl_class_context (tree decl)
4102 tree context = NULL_TREE;
4104 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4105 context = DECL_CONTEXT (decl);
4106 else
4107 context = TYPE_MAIN_VARIANT
4108 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4110 if (context && !TYPE_P (context))
4111 context = NULL_TREE;
4113 return context;
4116 /* Add an attribute/value pair to a DIE. */
4118 static inline void
4119 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4121 /* Maybe this should be an assert? */
4122 if (die == NULL)
4123 return;
4125 vec_safe_reserve (die->die_attr, 1);
4126 vec_safe_push (die->die_attr, *attr);
4129 static inline enum dw_val_class
4130 AT_class (dw_attr_node *a)
4132 return a->dw_attr_val.val_class;
4135 /* Return the index for any attribute that will be referenced with a
4136 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4137 are stored in dw_attr_val.v.val_str for reference counting
4138 pruning. */
4140 static inline unsigned int
4141 AT_index (dw_attr_node *a)
4143 if (AT_class (a) == dw_val_class_str)
4144 return a->dw_attr_val.v.val_str->index;
4145 else if (a->dw_attr_val.val_entry != NULL)
4146 return a->dw_attr_val.val_entry->index;
4147 return NOT_INDEXED;
4150 /* Add a flag value attribute to a DIE. */
4152 static inline void
4153 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4155 dw_attr_node attr;
4157 attr.dw_attr = attr_kind;
4158 attr.dw_attr_val.val_class = dw_val_class_flag;
4159 attr.dw_attr_val.val_entry = NULL;
4160 attr.dw_attr_val.v.val_flag = flag;
4161 add_dwarf_attr (die, &attr);
4164 static inline unsigned
4165 AT_flag (dw_attr_node *a)
4167 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4168 return a->dw_attr_val.v.val_flag;
4171 /* Add a signed integer attribute value to a DIE. */
4173 static inline void
4174 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4176 dw_attr_node attr;
4178 attr.dw_attr = attr_kind;
4179 attr.dw_attr_val.val_class = dw_val_class_const;
4180 attr.dw_attr_val.val_entry = NULL;
4181 attr.dw_attr_val.v.val_int = int_val;
4182 add_dwarf_attr (die, &attr);
4185 static inline HOST_WIDE_INT
4186 AT_int (dw_attr_node *a)
4188 gcc_assert (a && (AT_class (a) == dw_val_class_const
4189 || AT_class (a) == dw_val_class_const_implicit));
4190 return a->dw_attr_val.v.val_int;
4193 /* Add an unsigned integer attribute value to a DIE. */
4195 static inline void
4196 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4197 unsigned HOST_WIDE_INT unsigned_val)
4199 dw_attr_node attr;
4201 attr.dw_attr = attr_kind;
4202 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4203 attr.dw_attr_val.val_entry = NULL;
4204 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4205 add_dwarf_attr (die, &attr);
4208 static inline unsigned HOST_WIDE_INT
4209 AT_unsigned (dw_attr_node *a)
4211 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4212 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4213 return a->dw_attr_val.v.val_unsigned;
4216 /* Add an unsigned wide integer attribute value to a DIE. */
4218 static inline void
4219 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4220 const wide_int& w)
4222 dw_attr_node attr;
4224 attr.dw_attr = attr_kind;
4225 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4226 attr.dw_attr_val.val_entry = NULL;
4227 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4228 *attr.dw_attr_val.v.val_wide = w;
4229 add_dwarf_attr (die, &attr);
4232 /* Add an unsigned double integer attribute value to a DIE. */
4234 static inline void
4235 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4236 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4238 dw_attr_node attr;
4240 attr.dw_attr = attr_kind;
4241 attr.dw_attr_val.val_class = dw_val_class_const_double;
4242 attr.dw_attr_val.val_entry = NULL;
4243 attr.dw_attr_val.v.val_double.high = high;
4244 attr.dw_attr_val.v.val_double.low = low;
4245 add_dwarf_attr (die, &attr);
4248 /* Add a floating point attribute value to a DIE and return it. */
4250 static inline void
4251 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4252 unsigned int length, unsigned int elt_size, unsigned char *array)
4254 dw_attr_node attr;
4256 attr.dw_attr = attr_kind;
4257 attr.dw_attr_val.val_class = dw_val_class_vec;
4258 attr.dw_attr_val.val_entry = NULL;
4259 attr.dw_attr_val.v.val_vec.length = length;
4260 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4261 attr.dw_attr_val.v.val_vec.array = array;
4262 add_dwarf_attr (die, &attr);
4265 /* Add an 8-byte data attribute value to a DIE. */
4267 static inline void
4268 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4269 unsigned char data8[8])
4271 dw_attr_node attr;
4273 attr.dw_attr = attr_kind;
4274 attr.dw_attr_val.val_class = dw_val_class_data8;
4275 attr.dw_attr_val.val_entry = NULL;
4276 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4277 add_dwarf_attr (die, &attr);
4280 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4281 dwarf_split_debug_info, address attributes in dies destined for the
4282 final executable have force_direct set to avoid using indexed
4283 references. */
4285 static inline void
4286 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4287 bool force_direct)
4289 dw_attr_node attr;
4290 char * lbl_id;
4292 lbl_id = xstrdup (lbl_low);
4293 attr.dw_attr = DW_AT_low_pc;
4294 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4295 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4296 if (dwarf_split_debug_info && !force_direct)
4297 attr.dw_attr_val.val_entry
4298 = add_addr_table_entry (lbl_id, ate_kind_label);
4299 else
4300 attr.dw_attr_val.val_entry = NULL;
4301 add_dwarf_attr (die, &attr);
4303 attr.dw_attr = DW_AT_high_pc;
4304 if (dwarf_version < 4)
4305 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4306 else
4307 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4308 lbl_id = xstrdup (lbl_high);
4309 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4310 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4311 && dwarf_split_debug_info && !force_direct)
4312 attr.dw_attr_val.val_entry
4313 = add_addr_table_entry (lbl_id, ate_kind_label);
4314 else
4315 attr.dw_attr_val.val_entry = NULL;
4316 add_dwarf_attr (die, &attr);
4319 /* Hash and equality functions for debug_str_hash. */
4321 hashval_t
4322 indirect_string_hasher::hash (indirect_string_node *x)
4324 return htab_hash_string (x->str);
4327 bool
4328 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4330 return strcmp (x1->str, x2) == 0;
4333 /* Add STR to the given string hash table. */
4335 static struct indirect_string_node *
4336 find_AT_string_in_table (const char *str,
4337 hash_table<indirect_string_hasher> *table)
4339 struct indirect_string_node *node;
4341 indirect_string_node **slot
4342 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4343 if (*slot == NULL)
4345 node = ggc_cleared_alloc<indirect_string_node> ();
4346 node->str = ggc_strdup (str);
4347 *slot = node;
4349 else
4350 node = *slot;
4352 node->refcount++;
4353 return node;
4356 /* Add STR to the indirect string hash table. */
4358 static struct indirect_string_node *
4359 find_AT_string (const char *str)
4361 if (! debug_str_hash)
4362 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4364 return find_AT_string_in_table (str, debug_str_hash);
4367 /* Add a string attribute value to a DIE. */
4369 static inline void
4370 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4372 dw_attr_node attr;
4373 struct indirect_string_node *node;
4375 node = find_AT_string (str);
4377 attr.dw_attr = attr_kind;
4378 attr.dw_attr_val.val_class = dw_val_class_str;
4379 attr.dw_attr_val.val_entry = NULL;
4380 attr.dw_attr_val.v.val_str = node;
4381 add_dwarf_attr (die, &attr);
4384 static inline const char *
4385 AT_string (dw_attr_node *a)
4387 gcc_assert (a && AT_class (a) == dw_val_class_str);
4388 return a->dw_attr_val.v.val_str->str;
4391 /* Call this function directly to bypass AT_string_form's logic to put
4392 the string inline in the die. */
4394 static void
4395 set_indirect_string (struct indirect_string_node *node)
4397 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4398 /* Already indirect is a no op. */
4399 if (node->form == DW_FORM_strp
4400 || node->form == DW_FORM_line_strp
4401 || node->form == DW_FORM_GNU_str_index)
4403 gcc_assert (node->label);
4404 return;
4406 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4407 ++dw2_string_counter;
4408 node->label = xstrdup (label);
4410 if (!dwarf_split_debug_info)
4412 node->form = DW_FORM_strp;
4413 node->index = NOT_INDEXED;
4415 else
4417 node->form = DW_FORM_GNU_str_index;
4418 node->index = NO_INDEX_ASSIGNED;
4422 /* A helper function for dwarf2out_finish, called to reset indirect
4423 string decisions done for early LTO dwarf output before fat object
4424 dwarf output. */
4427 reset_indirect_string (indirect_string_node **h, void *)
4429 struct indirect_string_node *node = *h;
4430 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4432 free (node->label);
4433 node->label = NULL;
4434 node->form = (dwarf_form) 0;
4435 node->index = 0;
4437 return 1;
4440 /* Find out whether a string should be output inline in DIE
4441 or out-of-line in .debug_str section. */
4443 static enum dwarf_form
4444 find_string_form (struct indirect_string_node *node)
4446 unsigned int len;
4448 if (node->form)
4449 return node->form;
4451 len = strlen (node->str) + 1;
4453 /* If the string is shorter or equal to the size of the reference, it is
4454 always better to put it inline. */
4455 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4456 return node->form = DW_FORM_string;
4458 /* If we cannot expect the linker to merge strings in .debug_str
4459 section, only put it into .debug_str if it is worth even in this
4460 single module. */
4461 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4462 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4463 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4464 return node->form = DW_FORM_string;
4466 set_indirect_string (node);
4468 return node->form;
4471 /* Find out whether the string referenced from the attribute should be
4472 output inline in DIE or out-of-line in .debug_str section. */
4474 static enum dwarf_form
4475 AT_string_form (dw_attr_node *a)
4477 gcc_assert (a && AT_class (a) == dw_val_class_str);
4478 return find_string_form (a->dw_attr_val.v.val_str);
4481 /* Add a DIE reference attribute value to a DIE. */
4483 static inline void
4484 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4486 dw_attr_node attr;
4487 gcc_checking_assert (targ_die != NULL);
4489 /* With LTO we can end up trying to reference something we didn't create
4490 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4491 if (targ_die == NULL)
4492 return;
4494 attr.dw_attr = attr_kind;
4495 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4496 attr.dw_attr_val.val_entry = NULL;
4497 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4498 attr.dw_attr_val.v.val_die_ref.external = 0;
4499 add_dwarf_attr (die, &attr);
4502 /* Change DIE reference REF to point to NEW_DIE instead. */
4504 static inline void
4505 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4507 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4508 ref->dw_attr_val.v.val_die_ref.die = new_die;
4509 ref->dw_attr_val.v.val_die_ref.external = 0;
4512 /* Add an AT_specification attribute to a DIE, and also make the back
4513 pointer from the specification to the definition. */
4515 static inline void
4516 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4518 add_AT_die_ref (die, DW_AT_specification, targ_die);
4519 gcc_assert (!targ_die->die_definition);
4520 targ_die->die_definition = die;
4523 static inline dw_die_ref
4524 AT_ref (dw_attr_node *a)
4526 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4527 return a->dw_attr_val.v.val_die_ref.die;
4530 static inline int
4531 AT_ref_external (dw_attr_node *a)
4533 if (a && AT_class (a) == dw_val_class_die_ref)
4534 return a->dw_attr_val.v.val_die_ref.external;
4536 return 0;
4539 static inline void
4540 set_AT_ref_external (dw_attr_node *a, int i)
4542 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4543 a->dw_attr_val.v.val_die_ref.external = i;
4546 /* Add an FDE reference attribute value to a DIE. */
4548 static inline void
4549 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4551 dw_attr_node attr;
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4555 attr.dw_attr_val.val_entry = NULL;
4556 attr.dw_attr_val.v.val_fde_index = targ_fde;
4557 add_dwarf_attr (die, &attr);
4560 /* Add a location description attribute value to a DIE. */
4562 static inline void
4563 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4565 dw_attr_node attr;
4567 attr.dw_attr = attr_kind;
4568 attr.dw_attr_val.val_class = dw_val_class_loc;
4569 attr.dw_attr_val.val_entry = NULL;
4570 attr.dw_attr_val.v.val_loc = loc;
4571 add_dwarf_attr (die, &attr);
4574 static inline dw_loc_descr_ref
4575 AT_loc (dw_attr_node *a)
4577 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4578 return a->dw_attr_val.v.val_loc;
4581 static inline void
4582 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4584 dw_attr_node attr;
4586 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4587 return;
4589 attr.dw_attr = attr_kind;
4590 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4591 attr.dw_attr_val.val_entry = NULL;
4592 attr.dw_attr_val.v.val_loc_list = loc_list;
4593 add_dwarf_attr (die, &attr);
4594 have_location_lists = true;
4597 static inline dw_loc_list_ref
4598 AT_loc_list (dw_attr_node *a)
4600 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4601 return a->dw_attr_val.v.val_loc_list;
4604 static inline dw_loc_list_ref *
4605 AT_loc_list_ptr (dw_attr_node *a)
4607 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4608 return &a->dw_attr_val.v.val_loc_list;
4611 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4613 static hashval_t hash (addr_table_entry *);
4614 static bool equal (addr_table_entry *, addr_table_entry *);
4617 /* Table of entries into the .debug_addr section. */
4619 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4621 /* Hash an address_table_entry. */
4623 hashval_t
4624 addr_hasher::hash (addr_table_entry *a)
4626 inchash::hash hstate;
4627 switch (a->kind)
4629 case ate_kind_rtx:
4630 hstate.add_int (0);
4631 break;
4632 case ate_kind_rtx_dtprel:
4633 hstate.add_int (1);
4634 break;
4635 case ate_kind_label:
4636 return htab_hash_string (a->addr.label);
4637 default:
4638 gcc_unreachable ();
4640 inchash::add_rtx (a->addr.rtl, hstate);
4641 return hstate.end ();
4644 /* Determine equality for two address_table_entries. */
4646 bool
4647 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4649 if (a1->kind != a2->kind)
4650 return 0;
4651 switch (a1->kind)
4653 case ate_kind_rtx:
4654 case ate_kind_rtx_dtprel:
4655 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4656 case ate_kind_label:
4657 return strcmp (a1->addr.label, a2->addr.label) == 0;
4658 default:
4659 gcc_unreachable ();
4663 /* Initialize an addr_table_entry. */
4665 void
4666 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4668 e->kind = kind;
4669 switch (kind)
4671 case ate_kind_rtx:
4672 case ate_kind_rtx_dtprel:
4673 e->addr.rtl = (rtx) addr;
4674 break;
4675 case ate_kind_label:
4676 e->addr.label = (char *) addr;
4677 break;
4679 e->refcount = 0;
4680 e->index = NO_INDEX_ASSIGNED;
4683 /* Add attr to the address table entry to the table. Defer setting an
4684 index until output time. */
4686 static addr_table_entry *
4687 add_addr_table_entry (void *addr, enum ate_kind kind)
4689 addr_table_entry *node;
4690 addr_table_entry finder;
4692 gcc_assert (dwarf_split_debug_info);
4693 if (! addr_index_table)
4694 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4695 init_addr_table_entry (&finder, kind, addr);
4696 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4698 if (*slot == HTAB_EMPTY_ENTRY)
4700 node = ggc_cleared_alloc<addr_table_entry> ();
4701 init_addr_table_entry (node, kind, addr);
4702 *slot = node;
4704 else
4705 node = *slot;
4707 node->refcount++;
4708 return node;
4711 /* Remove an entry from the addr table by decrementing its refcount.
4712 Strictly, decrementing the refcount would be enough, but the
4713 assertion that the entry is actually in the table has found
4714 bugs. */
4716 static void
4717 remove_addr_table_entry (addr_table_entry *entry)
4719 gcc_assert (dwarf_split_debug_info && addr_index_table);
4720 /* After an index is assigned, the table is frozen. */
4721 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4722 entry->refcount--;
4725 /* Given a location list, remove all addresses it refers to from the
4726 address_table. */
4728 static void
4729 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4731 for (; descr; descr = descr->dw_loc_next)
4732 if (descr->dw_loc_oprnd1.val_entry != NULL)
4734 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4735 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4739 /* A helper function for dwarf2out_finish called through
4740 htab_traverse. Assign an addr_table_entry its index. All entries
4741 must be collected into the table when this function is called,
4742 because the indexing code relies on htab_traverse to traverse nodes
4743 in the same order for each run. */
4746 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4748 addr_table_entry *node = *h;
4750 /* Don't index unreferenced nodes. */
4751 if (node->refcount == 0)
4752 return 1;
4754 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4755 node->index = *index;
4756 *index += 1;
4758 return 1;
4761 /* Add an address constant attribute value to a DIE. When using
4762 dwarf_split_debug_info, address attributes in dies destined for the
4763 final executable should be direct references--setting the parameter
4764 force_direct ensures this behavior. */
4766 static inline void
4767 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4768 bool force_direct)
4770 dw_attr_node attr;
4772 attr.dw_attr = attr_kind;
4773 attr.dw_attr_val.val_class = dw_val_class_addr;
4774 attr.dw_attr_val.v.val_addr = addr;
4775 if (dwarf_split_debug_info && !force_direct)
4776 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4777 else
4778 attr.dw_attr_val.val_entry = NULL;
4779 add_dwarf_attr (die, &attr);
4782 /* Get the RTX from to an address DIE attribute. */
4784 static inline rtx
4785 AT_addr (dw_attr_node *a)
4787 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4788 return a->dw_attr_val.v.val_addr;
4791 /* Add a file attribute value to a DIE. */
4793 static inline void
4794 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4795 struct dwarf_file_data *fd)
4797 dw_attr_node attr;
4799 attr.dw_attr = attr_kind;
4800 attr.dw_attr_val.val_class = dw_val_class_file;
4801 attr.dw_attr_val.val_entry = NULL;
4802 attr.dw_attr_val.v.val_file = fd;
4803 add_dwarf_attr (die, &attr);
4806 /* Get the dwarf_file_data from a file DIE attribute. */
4808 static inline struct dwarf_file_data *
4809 AT_file (dw_attr_node *a)
4811 gcc_assert (a && (AT_class (a) == dw_val_class_file
4812 || AT_class (a) == dw_val_class_file_implicit));
4813 return a->dw_attr_val.v.val_file;
4816 /* Add a vms delta attribute value to a DIE. */
4818 static inline void
4819 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4820 const char *lbl1, const char *lbl2)
4822 dw_attr_node attr;
4824 attr.dw_attr = attr_kind;
4825 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4826 attr.dw_attr_val.val_entry = NULL;
4827 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4828 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4829 add_dwarf_attr (die, &attr);
4832 /* Add a label identifier attribute value to a DIE. */
4834 static inline void
4835 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4836 const char *lbl_id)
4838 dw_attr_node attr;
4840 attr.dw_attr = attr_kind;
4841 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4842 attr.dw_attr_val.val_entry = NULL;
4843 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4844 if (dwarf_split_debug_info)
4845 attr.dw_attr_val.val_entry
4846 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4847 ate_kind_label);
4848 add_dwarf_attr (die, &attr);
4851 /* Add a section offset attribute value to a DIE, an offset into the
4852 debug_line section. */
4854 static inline void
4855 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4856 const char *label)
4858 dw_attr_node attr;
4860 attr.dw_attr = attr_kind;
4861 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4862 attr.dw_attr_val.val_entry = NULL;
4863 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4864 add_dwarf_attr (die, &attr);
4867 /* Add a section offset attribute value to a DIE, an offset into the
4868 debug_loclists section. */
4870 static inline void
4871 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4872 const char *label)
4874 dw_attr_node attr;
4876 attr.dw_attr = attr_kind;
4877 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4878 attr.dw_attr_val.val_entry = NULL;
4879 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4880 add_dwarf_attr (die, &attr);
4883 /* Add a section offset attribute value to a DIE, an offset into the
4884 debug_macinfo section. */
4886 static inline void
4887 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4888 const char *label)
4890 dw_attr_node attr;
4892 attr.dw_attr = attr_kind;
4893 attr.dw_attr_val.val_class = dw_val_class_macptr;
4894 attr.dw_attr_val.val_entry = NULL;
4895 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4896 add_dwarf_attr (die, &attr);
4899 /* Add an offset attribute value to a DIE. */
4901 static inline void
4902 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4903 unsigned HOST_WIDE_INT offset)
4905 dw_attr_node attr;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_offset;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_offset = offset;
4911 add_dwarf_attr (die, &attr);
4914 /* Add a range_list attribute value to a DIE. When using
4915 dwarf_split_debug_info, address attributes in dies destined for the
4916 final executable should be direct references--setting the parameter
4917 force_direct ensures this behavior. */
4919 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4920 #define RELOCATED_OFFSET (NULL)
4922 static void
4923 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4924 long unsigned int offset, bool force_direct)
4926 dw_attr_node attr;
4928 attr.dw_attr = attr_kind;
4929 attr.dw_attr_val.val_class = dw_val_class_range_list;
4930 /* For the range_list attribute, use val_entry to store whether the
4931 offset should follow split-debug-info or normal semantics. This
4932 value is read in output_range_list_offset. */
4933 if (dwarf_split_debug_info && !force_direct)
4934 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4935 else
4936 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4937 attr.dw_attr_val.v.val_offset = offset;
4938 add_dwarf_attr (die, &attr);
4941 /* Return the start label of a delta attribute. */
4943 static inline const char *
4944 AT_vms_delta1 (dw_attr_node *a)
4946 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4947 return a->dw_attr_val.v.val_vms_delta.lbl1;
4950 /* Return the end label of a delta attribute. */
4952 static inline const char *
4953 AT_vms_delta2 (dw_attr_node *a)
4955 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4956 return a->dw_attr_val.v.val_vms_delta.lbl2;
4959 static inline const char *
4960 AT_lbl (dw_attr_node *a)
4962 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4963 || AT_class (a) == dw_val_class_lineptr
4964 || AT_class (a) == dw_val_class_macptr
4965 || AT_class (a) == dw_val_class_loclistsptr
4966 || AT_class (a) == dw_val_class_high_pc));
4967 return a->dw_attr_val.v.val_lbl_id;
4970 /* Get the attribute of type attr_kind. */
4972 static dw_attr_node *
4973 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4975 dw_attr_node *a;
4976 unsigned ix;
4977 dw_die_ref spec = NULL;
4979 if (! die)
4980 return NULL;
4982 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4983 if (a->dw_attr == attr_kind)
4984 return a;
4985 else if (a->dw_attr == DW_AT_specification
4986 || a->dw_attr == DW_AT_abstract_origin)
4987 spec = AT_ref (a);
4989 if (spec)
4990 return get_AT (spec, attr_kind);
4992 return NULL;
4995 /* Returns the parent of the declaration of DIE. */
4997 static dw_die_ref
4998 get_die_parent (dw_die_ref die)
5000 dw_die_ref t;
5002 if (!die)
5003 return NULL;
5005 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5006 || (t = get_AT_ref (die, DW_AT_specification)))
5007 die = t;
5009 return die->die_parent;
5012 /* Return the "low pc" attribute value, typically associated with a subprogram
5013 DIE. Return null if the "low pc" attribute is either not present, or if it
5014 cannot be represented as an assembler label identifier. */
5016 static inline const char *
5017 get_AT_low_pc (dw_die_ref die)
5019 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5021 return a ? AT_lbl (a) : NULL;
5024 /* Return the "high pc" attribute value, typically associated with a subprogram
5025 DIE. Return null if the "high pc" attribute is either not present, or if it
5026 cannot be represented as an assembler label identifier. */
5028 static inline const char *
5029 get_AT_hi_pc (dw_die_ref die)
5031 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5033 return a ? AT_lbl (a) : NULL;
5036 /* Return the value of the string attribute designated by ATTR_KIND, or
5037 NULL if it is not present. */
5039 static inline const char *
5040 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5042 dw_attr_node *a = get_AT (die, attr_kind);
5044 return a ? AT_string (a) : NULL;
5047 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5048 if it is not present. */
5050 static inline int
5051 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5053 dw_attr_node *a = get_AT (die, attr_kind);
5055 return a ? AT_flag (a) : 0;
5058 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5059 if it is not present. */
5061 static inline unsigned
5062 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5064 dw_attr_node *a = get_AT (die, attr_kind);
5066 return a ? AT_unsigned (a) : 0;
5069 static inline dw_die_ref
5070 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5072 dw_attr_node *a = get_AT (die, attr_kind);
5074 return a ? AT_ref (a) : NULL;
5077 static inline struct dwarf_file_data *
5078 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5080 dw_attr_node *a = get_AT (die, attr_kind);
5082 return a ? AT_file (a) : NULL;
5085 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5087 static const_tree
5088 get_ultimate_context (const_tree decl)
5090 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5092 if (TREE_CODE (decl) == BLOCK)
5093 decl = BLOCK_SUPERCONTEXT (decl);
5094 else
5095 decl = get_containing_scope (decl);
5097 return decl;
5100 /* Return TRUE if the language is C++. */
5102 static inline bool
5103 is_cxx (void)
5105 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5107 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5108 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5111 /* Return TRUE if DECL was created by the C++ frontend. */
5113 static bool
5114 is_cxx (const_tree decl)
5116 if (in_lto_p)
5118 const_tree context = get_ultimate_context (decl);
5119 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5120 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5122 return is_cxx ();
5125 /* Return TRUE if the language is Fortran. */
5127 static inline bool
5128 is_fortran (void)
5130 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5132 return (lang == DW_LANG_Fortran77
5133 || lang == DW_LANG_Fortran90
5134 || lang == DW_LANG_Fortran95
5135 || lang == DW_LANG_Fortran03
5136 || lang == DW_LANG_Fortran08);
5139 static inline bool
5140 is_fortran (const_tree decl)
5142 if (in_lto_p)
5144 const_tree context = get_ultimate_context (decl);
5145 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5146 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5147 "GNU Fortran", 11) == 0
5148 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5149 "GNU F77") == 0);
5151 return is_fortran ();
5154 /* Return TRUE if the language is Ada. */
5156 static inline bool
5157 is_ada (void)
5159 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5161 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5164 /* Remove the specified attribute if present. Return TRUE if removal
5165 was successful. */
5167 static bool
5168 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5170 dw_attr_node *a;
5171 unsigned ix;
5173 if (! die)
5174 return false;
5176 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5177 if (a->dw_attr == attr_kind)
5179 if (AT_class (a) == dw_val_class_str)
5180 if (a->dw_attr_val.v.val_str->refcount)
5181 a->dw_attr_val.v.val_str->refcount--;
5183 /* vec::ordered_remove should help reduce the number of abbrevs
5184 that are needed. */
5185 die->die_attr->ordered_remove (ix);
5186 return true;
5188 return false;
5191 /* Remove CHILD from its parent. PREV must have the property that
5192 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5194 static void
5195 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5197 gcc_assert (child->die_parent == prev->die_parent);
5198 gcc_assert (prev->die_sib == child);
5199 if (prev == child)
5201 gcc_assert (child->die_parent->die_child == child);
5202 prev = NULL;
5204 else
5205 prev->die_sib = child->die_sib;
5206 if (child->die_parent->die_child == child)
5207 child->die_parent->die_child = prev;
5208 child->die_sib = NULL;
5211 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5212 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5214 static void
5215 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5217 dw_die_ref parent = old_child->die_parent;
5219 gcc_assert (parent == prev->die_parent);
5220 gcc_assert (prev->die_sib == old_child);
5222 new_child->die_parent = parent;
5223 if (prev == old_child)
5225 gcc_assert (parent->die_child == old_child);
5226 new_child->die_sib = new_child;
5228 else
5230 prev->die_sib = new_child;
5231 new_child->die_sib = old_child->die_sib;
5233 if (old_child->die_parent->die_child == old_child)
5234 old_child->die_parent->die_child = new_child;
5235 old_child->die_sib = NULL;
5238 /* Move all children from OLD_PARENT to NEW_PARENT. */
5240 static void
5241 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5243 dw_die_ref c;
5244 new_parent->die_child = old_parent->die_child;
5245 old_parent->die_child = NULL;
5246 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5249 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5250 matches TAG. */
5252 static void
5253 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5255 dw_die_ref c;
5257 c = die->die_child;
5258 if (c) do {
5259 dw_die_ref prev = c;
5260 c = c->die_sib;
5261 while (c->die_tag == tag)
5263 remove_child_with_prev (c, prev);
5264 c->die_parent = NULL;
5265 /* Might have removed every child. */
5266 if (die->die_child == NULL)
5267 return;
5268 c = prev->die_sib;
5270 } while (c != die->die_child);
5273 /* Add a CHILD_DIE as the last child of DIE. */
5275 static void
5276 add_child_die (dw_die_ref die, dw_die_ref child_die)
5278 /* FIXME this should probably be an assert. */
5279 if (! die || ! child_die)
5280 return;
5281 gcc_assert (die != child_die);
5283 child_die->die_parent = die;
5284 if (die->die_child)
5286 child_die->die_sib = die->die_child->die_sib;
5287 die->die_child->die_sib = child_die;
5289 else
5290 child_die->die_sib = child_die;
5291 die->die_child = child_die;
5294 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5296 static void
5297 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5298 dw_die_ref after_die)
5300 gcc_assert (die
5301 && child_die
5302 && after_die
5303 && die->die_child
5304 && die != child_die);
5306 child_die->die_parent = die;
5307 child_die->die_sib = after_die->die_sib;
5308 after_die->die_sib = child_die;
5309 if (die->die_child == after_die)
5310 die->die_child = child_die;
5313 /* Unassociate CHILD from its parent, and make its parent be
5314 NEW_PARENT. */
5316 static void
5317 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5319 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5320 if (p->die_sib == child)
5322 remove_child_with_prev (child, p);
5323 break;
5325 add_child_die (new_parent, child);
5328 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5329 is the specification, to the end of PARENT's list of children.
5330 This is done by removing and re-adding it. */
5332 static void
5333 splice_child_die (dw_die_ref parent, dw_die_ref child)
5335 /* We want the declaration DIE from inside the class, not the
5336 specification DIE at toplevel. */
5337 if (child->die_parent != parent)
5339 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5341 if (tmp)
5342 child = tmp;
5345 gcc_assert (child->die_parent == parent
5346 || (child->die_parent
5347 == get_AT_ref (parent, DW_AT_specification)));
5349 reparent_child (child, parent);
5352 /* Create and return a new die with a parent of PARENT_DIE. If
5353 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5354 associated tree T must be supplied to determine parenthood
5355 later. */
5357 static inline dw_die_ref
5358 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5360 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5362 die->die_tag = tag_value;
5364 if (parent_die != NULL)
5365 add_child_die (parent_die, die);
5366 else
5368 limbo_die_node *limbo_node;
5370 /* No DIEs created after early dwarf should end up in limbo,
5371 because the limbo list should not persist past LTO
5372 streaming. */
5373 if (tag_value != DW_TAG_compile_unit
5374 /* These are allowed because they're generated while
5375 breaking out COMDAT units late. */
5376 && tag_value != DW_TAG_type_unit
5377 && tag_value != DW_TAG_skeleton_unit
5378 && !early_dwarf
5379 /* Allow nested functions to live in limbo because they will
5380 only temporarily live there, as decls_for_scope will fix
5381 them up. */
5382 && (TREE_CODE (t) != FUNCTION_DECL
5383 || !decl_function_context (t))
5384 /* Same as nested functions above but for types. Types that
5385 are local to a function will be fixed in
5386 decls_for_scope. */
5387 && (!RECORD_OR_UNION_TYPE_P (t)
5388 || !TYPE_CONTEXT (t)
5389 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5390 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5391 especially in the ltrans stage, but once we implement LTO
5392 dwarf streaming, we should remove this exception. */
5393 && !in_lto_p)
5395 fprintf (stderr, "symbol ended up in limbo too late:");
5396 debug_generic_stmt (t);
5397 gcc_unreachable ();
5400 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5401 limbo_node->die = die;
5402 limbo_node->created_for = t;
5403 limbo_node->next = limbo_die_list;
5404 limbo_die_list = limbo_node;
5407 return die;
5410 /* Return the DIE associated with the given type specifier. */
5412 static inline dw_die_ref
5413 lookup_type_die (tree type)
5415 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5416 if (die && die->removed)
5418 TYPE_SYMTAB_DIE (type) = NULL;
5419 return NULL;
5421 return die;
5424 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5425 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5426 anonymous type instead the one of the naming typedef. */
5428 static inline dw_die_ref
5429 strip_naming_typedef (tree type, dw_die_ref type_die)
5431 if (type
5432 && TREE_CODE (type) == RECORD_TYPE
5433 && type_die
5434 && type_die->die_tag == DW_TAG_typedef
5435 && is_naming_typedef_decl (TYPE_NAME (type)))
5436 type_die = get_AT_ref (type_die, DW_AT_type);
5437 return type_die;
5440 /* Like lookup_type_die, but if type is an anonymous type named by a
5441 typedef[1], return the DIE of the anonymous type instead the one of
5442 the naming typedef. This is because in gen_typedef_die, we did
5443 equate the anonymous struct named by the typedef with the DIE of
5444 the naming typedef. So by default, lookup_type_die on an anonymous
5445 struct yields the DIE of the naming typedef.
5447 [1]: Read the comment of is_naming_typedef_decl to learn about what
5448 a naming typedef is. */
5450 static inline dw_die_ref
5451 lookup_type_die_strip_naming_typedef (tree type)
5453 dw_die_ref die = lookup_type_die (type);
5454 return strip_naming_typedef (type, die);
5457 /* Equate a DIE to a given type specifier. */
5459 static inline void
5460 equate_type_number_to_die (tree type, dw_die_ref type_die)
5462 TYPE_SYMTAB_DIE (type) = type_die;
5465 /* Returns a hash value for X (which really is a die_struct). */
5467 inline hashval_t
5468 decl_die_hasher::hash (die_node *x)
5470 return (hashval_t) x->decl_id;
5473 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5475 inline bool
5476 decl_die_hasher::equal (die_node *x, tree y)
5478 return (x->decl_id == DECL_UID (y));
5481 /* Return the DIE associated with a given declaration. */
5483 static inline dw_die_ref
5484 lookup_decl_die (tree decl)
5486 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5487 NO_INSERT);
5488 if (!die)
5489 return NULL;
5490 if ((*die)->removed)
5492 decl_die_table->clear_slot (die);
5493 return NULL;
5495 return *die;
5499 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5500 style reference. Return true if we found one refering to a DIE for
5501 DECL, otherwise return false. */
5503 static bool
5504 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5505 unsigned HOST_WIDE_INT *off)
5507 dw_die_ref die;
5509 if (flag_wpa && !decl_die_table)
5510 return false;
5512 if (TREE_CODE (decl) == BLOCK)
5513 die = BLOCK_DIE (decl);
5514 else
5515 die = lookup_decl_die (decl);
5516 if (!die)
5517 return false;
5519 /* During WPA stage we currently use DIEs to store the
5520 decl <-> label + offset map. That's quite inefficient but it
5521 works for now. */
5522 if (flag_wpa)
5524 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5525 if (!ref)
5527 gcc_assert (die == comp_unit_die ());
5528 return false;
5530 *off = ref->die_offset;
5531 *sym = ref->die_id.die_symbol;
5532 return true;
5535 /* Similar to get_ref_die_offset_label, but using the "correct"
5536 label. */
5537 *off = die->die_offset;
5538 while (die->die_parent)
5539 die = die->die_parent;
5540 /* For the containing CU DIE we compute a die_symbol in
5541 compute_comp_unit_symbol. */
5542 gcc_assert (die->die_tag == DW_TAG_compile_unit
5543 && die->die_id.die_symbol != NULL);
5544 *sym = die->die_id.die_symbol;
5545 return true;
5548 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5550 static void
5551 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5552 const char *symbol, HOST_WIDE_INT offset)
5554 /* Create a fake DIE that contains the reference. Don't use
5555 new_die because we don't want to end up in the limbo list. */
5556 dw_die_ref ref = ggc_cleared_alloc<die_node> ();
5557 ref->die_tag = die->die_tag;
5558 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5559 ref->die_offset = offset;
5560 ref->with_offset = 1;
5561 add_AT_die_ref (die, attr_kind, ref);
5564 /* Create a DIE for DECL if required and add a reference to a DIE
5565 at SYMBOL + OFFSET which contains attributes dumped early. */
5567 static void
5568 dwarf2out_register_external_die (tree decl, const char *sym,
5569 unsigned HOST_WIDE_INT off)
5571 if (debug_info_level == DINFO_LEVEL_NONE)
5572 return;
5574 if (flag_wpa && !decl_die_table)
5575 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5577 dw_die_ref die
5578 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5579 gcc_assert (!die);
5581 tree ctx;
5582 dw_die_ref parent = NULL;
5583 /* Need to lookup a DIE for the decls context - the containing
5584 function or translation unit. */
5585 if (TREE_CODE (decl) == BLOCK)
5587 ctx = BLOCK_SUPERCONTEXT (decl);
5588 /* ??? We do not output DIEs for all scopes thus skip as
5589 many DIEs as needed. */
5590 while (TREE_CODE (ctx) == BLOCK
5591 && !BLOCK_DIE (ctx))
5592 ctx = BLOCK_SUPERCONTEXT (ctx);
5594 else
5595 ctx = DECL_CONTEXT (decl);
5596 while (ctx && TYPE_P (ctx))
5597 ctx = TYPE_CONTEXT (ctx);
5598 if (ctx)
5600 if (TREE_CODE (ctx) == BLOCK)
5601 parent = BLOCK_DIE (ctx);
5602 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5603 /* Keep the 1:1 association during WPA. */
5604 && !flag_wpa)
5605 /* Otherwise all late annotations go to the main CU which
5606 imports the original CUs. */
5607 parent = comp_unit_die ();
5608 else if (TREE_CODE (ctx) == FUNCTION_DECL
5609 && TREE_CODE (decl) != PARM_DECL
5610 && TREE_CODE (decl) != BLOCK)
5611 /* Leave function local entities parent determination to when
5612 we process scope vars. */
5614 else
5615 parent = lookup_decl_die (ctx);
5617 else
5618 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5619 Handle this case gracefully by globalizing stuff. */
5620 parent = comp_unit_die ();
5621 /* Create a DIE "stub". */
5622 switch (TREE_CODE (decl))
5624 case TRANSLATION_UNIT_DECL:
5625 if (! flag_wpa)
5627 die = comp_unit_die ();
5628 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5629 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5630 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5631 to create a DIE for the original CUs. */
5632 return;
5634 /* Keep the 1:1 association during WPA. */
5635 die = new_die (DW_TAG_compile_unit, NULL, decl);
5636 break;
5637 case NAMESPACE_DECL:
5638 if (is_fortran (decl))
5639 die = new_die (DW_TAG_module, parent, decl);
5640 else
5641 die = new_die (DW_TAG_namespace, parent, decl);
5642 break;
5643 case FUNCTION_DECL:
5644 die = new_die (DW_TAG_subprogram, parent, decl);
5645 break;
5646 case VAR_DECL:
5647 die = new_die (DW_TAG_variable, parent, decl);
5648 break;
5649 case RESULT_DECL:
5650 die = new_die (DW_TAG_variable, parent, decl);
5651 break;
5652 case PARM_DECL:
5653 die = new_die (DW_TAG_formal_parameter, parent, decl);
5654 break;
5655 case CONST_DECL:
5656 die = new_die (DW_TAG_constant, parent, decl);
5657 break;
5658 case LABEL_DECL:
5659 die = new_die (DW_TAG_label, parent, decl);
5660 break;
5661 case BLOCK:
5662 die = new_die (DW_TAG_lexical_block, parent, decl);
5663 break;
5664 default:
5665 gcc_unreachable ();
5667 if (TREE_CODE (decl) == BLOCK)
5668 BLOCK_DIE (decl) = die;
5669 else
5670 equate_decl_number_to_die (decl, die);
5672 /* Add a reference to the DIE providing early debug at $sym + off. */
5673 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5676 /* Returns a hash value for X (which really is a var_loc_list). */
5678 inline hashval_t
5679 decl_loc_hasher::hash (var_loc_list *x)
5681 return (hashval_t) x->decl_id;
5684 /* Return nonzero if decl_id of var_loc_list X is the same as
5685 UID of decl *Y. */
5687 inline bool
5688 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5690 return (x->decl_id == DECL_UID (y));
5693 /* Return the var_loc list associated with a given declaration. */
5695 static inline var_loc_list *
5696 lookup_decl_loc (const_tree decl)
5698 if (!decl_loc_table)
5699 return NULL;
5700 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5703 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5705 inline hashval_t
5706 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5708 return (hashval_t) x->decl_id;
5711 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5712 UID of decl *Y. */
5714 inline bool
5715 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5717 return (x->decl_id == DECL_UID (y));
5720 /* Equate a DIE to a particular declaration. */
5722 static void
5723 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5725 unsigned int decl_id = DECL_UID (decl);
5727 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5728 decl_die->decl_id = decl_id;
5731 /* Return how many bits covers PIECE EXPR_LIST. */
5733 static HOST_WIDE_INT
5734 decl_piece_bitsize (rtx piece)
5736 int ret = (int) GET_MODE (piece);
5737 if (ret)
5738 return ret;
5739 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5740 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5741 return INTVAL (XEXP (XEXP (piece, 0), 0));
5744 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5746 static rtx *
5747 decl_piece_varloc_ptr (rtx piece)
5749 if ((int) GET_MODE (piece))
5750 return &XEXP (piece, 0);
5751 else
5752 return &XEXP (XEXP (piece, 0), 1);
5755 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5756 Next is the chain of following piece nodes. */
5758 static rtx_expr_list *
5759 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5761 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5762 return alloc_EXPR_LIST (bitsize, loc_note, next);
5763 else
5764 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5765 GEN_INT (bitsize),
5766 loc_note), next);
5769 /* Return rtx that should be stored into loc field for
5770 LOC_NOTE and BITPOS/BITSIZE. */
5772 static rtx
5773 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5774 HOST_WIDE_INT bitsize)
5776 if (bitsize != -1)
5778 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5779 if (bitpos != 0)
5780 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5782 return loc_note;
5785 /* This function either modifies location piece list *DEST in
5786 place (if SRC and INNER is NULL), or copies location piece list
5787 *SRC to *DEST while modifying it. Location BITPOS is modified
5788 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5789 not copied and if needed some padding around it is added.
5790 When modifying in place, DEST should point to EXPR_LIST where
5791 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5792 to the start of the whole list and INNER points to the EXPR_LIST
5793 where earlier pieces cover PIECE_BITPOS bits. */
5795 static void
5796 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5797 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5798 HOST_WIDE_INT bitsize, rtx loc_note)
5800 HOST_WIDE_INT diff;
5801 bool copy = inner != NULL;
5803 if (copy)
5805 /* First copy all nodes preceding the current bitpos. */
5806 while (src != inner)
5808 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5809 decl_piece_bitsize (*src), NULL_RTX);
5810 dest = &XEXP (*dest, 1);
5811 src = &XEXP (*src, 1);
5814 /* Add padding if needed. */
5815 if (bitpos != piece_bitpos)
5817 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5818 copy ? NULL_RTX : *dest);
5819 dest = &XEXP (*dest, 1);
5821 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5823 gcc_assert (!copy);
5824 /* A piece with correct bitpos and bitsize already exist,
5825 just update the location for it and return. */
5826 *decl_piece_varloc_ptr (*dest) = loc_note;
5827 return;
5829 /* Add the piece that changed. */
5830 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5831 dest = &XEXP (*dest, 1);
5832 /* Skip over pieces that overlap it. */
5833 diff = bitpos - piece_bitpos + bitsize;
5834 if (!copy)
5835 src = dest;
5836 while (diff > 0 && *src)
5838 rtx piece = *src;
5839 diff -= decl_piece_bitsize (piece);
5840 if (copy)
5841 src = &XEXP (piece, 1);
5842 else
5844 *src = XEXP (piece, 1);
5845 free_EXPR_LIST_node (piece);
5848 /* Add padding if needed. */
5849 if (diff < 0 && *src)
5851 if (!copy)
5852 dest = src;
5853 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5854 dest = &XEXP (*dest, 1);
5856 if (!copy)
5857 return;
5858 /* Finally copy all nodes following it. */
5859 while (*src)
5861 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5862 decl_piece_bitsize (*src), NULL_RTX);
5863 dest = &XEXP (*dest, 1);
5864 src = &XEXP (*src, 1);
5868 /* Add a variable location node to the linked list for DECL. */
5870 static struct var_loc_node *
5871 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5873 unsigned int decl_id;
5874 var_loc_list *temp;
5875 struct var_loc_node *loc = NULL;
5876 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5878 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5880 tree realdecl = DECL_DEBUG_EXPR (decl);
5881 if (handled_component_p (realdecl)
5882 || (TREE_CODE (realdecl) == MEM_REF
5883 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5885 HOST_WIDE_INT maxsize;
5886 bool reverse;
5887 tree innerdecl
5888 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5889 &reverse);
5890 if (!DECL_P (innerdecl)
5891 || DECL_IGNORED_P (innerdecl)
5892 || TREE_STATIC (innerdecl)
5893 || bitsize <= 0
5894 || bitpos + bitsize > 256
5895 || bitsize != maxsize)
5896 return NULL;
5897 decl = innerdecl;
5901 decl_id = DECL_UID (decl);
5902 var_loc_list **slot
5903 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5904 if (*slot == NULL)
5906 temp = ggc_cleared_alloc<var_loc_list> ();
5907 temp->decl_id = decl_id;
5908 *slot = temp;
5910 else
5911 temp = *slot;
5913 /* For PARM_DECLs try to keep around the original incoming value,
5914 even if that means we'll emit a zero-range .debug_loc entry. */
5915 if (temp->last
5916 && temp->first == temp->last
5917 && TREE_CODE (decl) == PARM_DECL
5918 && NOTE_P (temp->first->loc)
5919 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5920 && DECL_INCOMING_RTL (decl)
5921 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5922 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5923 == GET_CODE (DECL_INCOMING_RTL (decl))
5924 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5925 && (bitsize != -1
5926 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5927 NOTE_VAR_LOCATION_LOC (loc_note))
5928 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5929 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5931 loc = ggc_cleared_alloc<var_loc_node> ();
5932 temp->first->next = loc;
5933 temp->last = loc;
5934 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5936 else if (temp->last)
5938 struct var_loc_node *last = temp->last, *unused = NULL;
5939 rtx *piece_loc = NULL, last_loc_note;
5940 HOST_WIDE_INT piece_bitpos = 0;
5941 if (last->next)
5943 last = last->next;
5944 gcc_assert (last->next == NULL);
5946 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5948 piece_loc = &last->loc;
5951 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5952 if (piece_bitpos + cur_bitsize > bitpos)
5953 break;
5954 piece_bitpos += cur_bitsize;
5955 piece_loc = &XEXP (*piece_loc, 1);
5957 while (*piece_loc);
5959 /* TEMP->LAST here is either pointer to the last but one or
5960 last element in the chained list, LAST is pointer to the
5961 last element. */
5962 if (label && strcmp (last->label, label) == 0)
5964 /* For SRA optimized variables if there weren't any real
5965 insns since last note, just modify the last node. */
5966 if (piece_loc != NULL)
5968 adjust_piece_list (piece_loc, NULL, NULL,
5969 bitpos, piece_bitpos, bitsize, loc_note);
5970 return NULL;
5972 /* If the last note doesn't cover any instructions, remove it. */
5973 if (temp->last != last)
5975 temp->last->next = NULL;
5976 unused = last;
5977 last = temp->last;
5978 gcc_assert (strcmp (last->label, label) != 0);
5980 else
5982 gcc_assert (temp->first == temp->last
5983 || (temp->first->next == temp->last
5984 && TREE_CODE (decl) == PARM_DECL));
5985 memset (temp->last, '\0', sizeof (*temp->last));
5986 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5987 return temp->last;
5990 if (bitsize == -1 && NOTE_P (last->loc))
5991 last_loc_note = last->loc;
5992 else if (piece_loc != NULL
5993 && *piece_loc != NULL_RTX
5994 && piece_bitpos == bitpos
5995 && decl_piece_bitsize (*piece_loc) == bitsize)
5996 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5997 else
5998 last_loc_note = NULL_RTX;
5999 /* If the current location is the same as the end of the list,
6000 and either both or neither of the locations is uninitialized,
6001 we have nothing to do. */
6002 if (last_loc_note == NULL_RTX
6003 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6004 NOTE_VAR_LOCATION_LOC (loc_note)))
6005 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6006 != NOTE_VAR_LOCATION_STATUS (loc_note))
6007 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6008 == VAR_INIT_STATUS_UNINITIALIZED)
6009 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6010 == VAR_INIT_STATUS_UNINITIALIZED))))
6012 /* Add LOC to the end of list and update LAST. If the last
6013 element of the list has been removed above, reuse its
6014 memory for the new node, otherwise allocate a new one. */
6015 if (unused)
6017 loc = unused;
6018 memset (loc, '\0', sizeof (*loc));
6020 else
6021 loc = ggc_cleared_alloc<var_loc_node> ();
6022 if (bitsize == -1 || piece_loc == NULL)
6023 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6024 else
6025 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6026 bitpos, piece_bitpos, bitsize, loc_note);
6027 last->next = loc;
6028 /* Ensure TEMP->LAST will point either to the new last but one
6029 element of the chain, or to the last element in it. */
6030 if (last != temp->last)
6031 temp->last = last;
6033 else if (unused)
6034 ggc_free (unused);
6036 else
6038 loc = ggc_cleared_alloc<var_loc_node> ();
6039 temp->first = loc;
6040 temp->last = loc;
6041 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6043 return loc;
6046 /* Keep track of the number of spaces used to indent the
6047 output of the debugging routines that print the structure of
6048 the DIE internal representation. */
6049 static int print_indent;
6051 /* Indent the line the number of spaces given by print_indent. */
6053 static inline void
6054 print_spaces (FILE *outfile)
6056 fprintf (outfile, "%*s", print_indent, "");
6059 /* Print a type signature in hex. */
6061 static inline void
6062 print_signature (FILE *outfile, char *sig)
6064 int i;
6066 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6067 fprintf (outfile, "%02x", sig[i] & 0xff);
6070 static inline void
6071 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6073 if (discr_value->pos)
6074 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6075 else
6076 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6079 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6081 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6082 RECURSE, output location descriptor operations. */
6084 static void
6085 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6087 switch (val->val_class)
6089 case dw_val_class_addr:
6090 fprintf (outfile, "address");
6091 break;
6092 case dw_val_class_offset:
6093 fprintf (outfile, "offset");
6094 break;
6095 case dw_val_class_loc:
6096 fprintf (outfile, "location descriptor");
6097 if (val->v.val_loc == NULL)
6098 fprintf (outfile, " -> <null>\n");
6099 else if (recurse)
6101 fprintf (outfile, ":\n");
6102 print_indent += 4;
6103 print_loc_descr (val->v.val_loc, outfile);
6104 print_indent -= 4;
6106 else
6107 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6108 break;
6109 case dw_val_class_loc_list:
6110 fprintf (outfile, "location list -> label:%s",
6111 val->v.val_loc_list->ll_symbol);
6112 break;
6113 case dw_val_class_range_list:
6114 fprintf (outfile, "range list");
6115 break;
6116 case dw_val_class_const:
6117 case dw_val_class_const_implicit:
6118 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6119 break;
6120 case dw_val_class_unsigned_const:
6121 case dw_val_class_unsigned_const_implicit:
6122 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6123 break;
6124 case dw_val_class_const_double:
6125 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6126 HOST_WIDE_INT_PRINT_UNSIGNED")",
6127 val->v.val_double.high,
6128 val->v.val_double.low);
6129 break;
6130 case dw_val_class_wide_int:
6132 int i = val->v.val_wide->get_len ();
6133 fprintf (outfile, "constant (");
6134 gcc_assert (i > 0);
6135 if (val->v.val_wide->elt (i - 1) == 0)
6136 fprintf (outfile, "0x");
6137 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6138 val->v.val_wide->elt (--i));
6139 while (--i >= 0)
6140 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6141 val->v.val_wide->elt (i));
6142 fprintf (outfile, ")");
6143 break;
6145 case dw_val_class_vec:
6146 fprintf (outfile, "floating-point or vector constant");
6147 break;
6148 case dw_val_class_flag:
6149 fprintf (outfile, "%u", val->v.val_flag);
6150 break;
6151 case dw_val_class_die_ref:
6152 if (val->v.val_die_ref.die != NULL)
6154 dw_die_ref die = val->v.val_die_ref.die;
6156 if (die->comdat_type_p)
6158 fprintf (outfile, "die -> signature: ");
6159 print_signature (outfile,
6160 die->die_id.die_type_node->signature);
6162 else if (die->die_id.die_symbol)
6164 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6165 if (die->with_offset)
6166 fprintf (outfile, " + %ld", die->die_offset);
6168 else
6169 fprintf (outfile, "die -> %ld", die->die_offset);
6170 fprintf (outfile, " (%p)", (void *) die);
6172 else
6173 fprintf (outfile, "die -> <null>");
6174 break;
6175 case dw_val_class_vms_delta:
6176 fprintf (outfile, "delta: @slotcount(%s-%s)",
6177 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6178 break;
6179 case dw_val_class_lbl_id:
6180 case dw_val_class_lineptr:
6181 case dw_val_class_macptr:
6182 case dw_val_class_loclistsptr:
6183 case dw_val_class_high_pc:
6184 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6185 break;
6186 case dw_val_class_str:
6187 if (val->v.val_str->str != NULL)
6188 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6189 else
6190 fprintf (outfile, "<null>");
6191 break;
6192 case dw_val_class_file:
6193 case dw_val_class_file_implicit:
6194 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6195 val->v.val_file->emitted_number);
6196 break;
6197 case dw_val_class_data8:
6199 int i;
6201 for (i = 0; i < 8; i++)
6202 fprintf (outfile, "%02x", val->v.val_data8[i]);
6203 break;
6205 case dw_val_class_discr_value:
6206 print_discr_value (outfile, &val->v.val_discr_value);
6207 break;
6208 case dw_val_class_discr_list:
6209 for (dw_discr_list_ref node = val->v.val_discr_list;
6210 node != NULL;
6211 node = node->dw_discr_next)
6213 if (node->dw_discr_range)
6215 fprintf (outfile, " .. ");
6216 print_discr_value (outfile, &node->dw_discr_lower_bound);
6217 print_discr_value (outfile, &node->dw_discr_upper_bound);
6219 else
6220 print_discr_value (outfile, &node->dw_discr_lower_bound);
6222 if (node->dw_discr_next != NULL)
6223 fprintf (outfile, " | ");
6225 default:
6226 break;
6230 /* Likewise, for a DIE attribute. */
6232 static void
6233 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6235 print_dw_val (&a->dw_attr_val, recurse, outfile);
6239 /* Print the list of operands in the LOC location description to OUTFILE. This
6240 routine is a debugging aid only. */
6242 static void
6243 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6245 dw_loc_descr_ref l = loc;
6247 if (loc == NULL)
6249 print_spaces (outfile);
6250 fprintf (outfile, "<null>\n");
6251 return;
6254 for (l = loc; l != NULL; l = l->dw_loc_next)
6256 print_spaces (outfile);
6257 fprintf (outfile, "(%p) %s",
6258 (void *) l,
6259 dwarf_stack_op_name (l->dw_loc_opc));
6260 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6262 fprintf (outfile, " ");
6263 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6265 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6267 fprintf (outfile, ", ");
6268 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6270 fprintf (outfile, "\n");
6274 /* Print the information associated with a given DIE, and its children.
6275 This routine is a debugging aid only. */
6277 static void
6278 print_die (dw_die_ref die, FILE *outfile)
6280 dw_attr_node *a;
6281 dw_die_ref c;
6282 unsigned ix;
6284 print_spaces (outfile);
6285 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6286 die->die_offset, dwarf_tag_name (die->die_tag),
6287 (void*) die);
6288 print_spaces (outfile);
6289 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6290 fprintf (outfile, " offset: %ld", die->die_offset);
6291 fprintf (outfile, " mark: %d\n", die->die_mark);
6293 if (die->comdat_type_p)
6295 print_spaces (outfile);
6296 fprintf (outfile, " signature: ");
6297 print_signature (outfile, die->die_id.die_type_node->signature);
6298 fprintf (outfile, "\n");
6301 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6303 print_spaces (outfile);
6304 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6306 print_attribute (a, true, outfile);
6307 fprintf (outfile, "\n");
6310 if (die->die_child != NULL)
6312 print_indent += 4;
6313 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6314 print_indent -= 4;
6316 if (print_indent == 0)
6317 fprintf (outfile, "\n");
6320 /* Print the list of operations in the LOC location description. */
6322 DEBUG_FUNCTION void
6323 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6325 print_loc_descr (loc, stderr);
6328 /* Print the information collected for a given DIE. */
6330 DEBUG_FUNCTION void
6331 debug_dwarf_die (dw_die_ref die)
6333 print_die (die, stderr);
6336 DEBUG_FUNCTION void
6337 debug (die_struct &ref)
6339 print_die (&ref, stderr);
6342 DEBUG_FUNCTION void
6343 debug (die_struct *ptr)
6345 if (ptr)
6346 debug (*ptr);
6347 else
6348 fprintf (stderr, "<nil>\n");
6352 /* Print all DWARF information collected for the compilation unit.
6353 This routine is a debugging aid only. */
6355 DEBUG_FUNCTION void
6356 debug_dwarf (void)
6358 print_indent = 0;
6359 print_die (comp_unit_die (), stderr);
6362 /* Verify the DIE tree structure. */
6364 DEBUG_FUNCTION void
6365 verify_die (dw_die_ref die)
6367 gcc_assert (!die->die_mark);
6368 if (die->die_parent == NULL
6369 && die->die_sib == NULL)
6370 return;
6371 /* Verify the die_sib list is cyclic. */
6372 dw_die_ref x = die;
6375 x->die_mark = 1;
6376 x = x->die_sib;
6378 while (x && !x->die_mark);
6379 gcc_assert (x == die);
6380 x = die;
6383 /* Verify all dies have the same parent. */
6384 gcc_assert (x->die_parent == die->die_parent);
6385 if (x->die_child)
6387 /* Verify the child has the proper parent and recurse. */
6388 gcc_assert (x->die_child->die_parent == x);
6389 verify_die (x->die_child);
6391 x->die_mark = 0;
6392 x = x->die_sib;
6394 while (x && x->die_mark);
6397 /* Sanity checks on DIEs. */
6399 static void
6400 check_die (dw_die_ref die)
6402 unsigned ix;
6403 dw_attr_node *a;
6404 bool inline_found = false;
6405 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6406 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6407 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6409 switch (a->dw_attr)
6411 case DW_AT_inline:
6412 if (a->dw_attr_val.v.val_unsigned)
6413 inline_found = true;
6414 break;
6415 case DW_AT_location:
6416 ++n_location;
6417 break;
6418 case DW_AT_low_pc:
6419 ++n_low_pc;
6420 break;
6421 case DW_AT_high_pc:
6422 ++n_high_pc;
6423 break;
6424 case DW_AT_artificial:
6425 ++n_artificial;
6426 break;
6427 case DW_AT_decl_column:
6428 ++n_decl_column;
6429 break;
6430 case DW_AT_decl_line:
6431 ++n_decl_line;
6432 break;
6433 case DW_AT_decl_file:
6434 ++n_decl_file;
6435 break;
6436 default:
6437 break;
6440 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6441 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6443 fprintf (stderr, "Duplicate attributes in DIE:\n");
6444 debug_dwarf_die (die);
6445 gcc_unreachable ();
6447 if (inline_found)
6449 /* A debugging information entry that is a member of an abstract
6450 instance tree [that has DW_AT_inline] should not contain any
6451 attributes which describe aspects of the subroutine which vary
6452 between distinct inlined expansions or distinct out-of-line
6453 expansions. */
6454 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6455 gcc_assert (a->dw_attr != DW_AT_low_pc
6456 && a->dw_attr != DW_AT_high_pc
6457 && a->dw_attr != DW_AT_location
6458 && a->dw_attr != DW_AT_frame_base
6459 && a->dw_attr != DW_AT_call_all_calls
6460 && a->dw_attr != DW_AT_GNU_all_call_sites);
6464 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6465 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6466 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6468 /* Calculate the checksum of a location expression. */
6470 static inline void
6471 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6473 int tem;
6474 inchash::hash hstate;
6475 hashval_t hash;
6477 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6478 CHECKSUM (tem);
6479 hash_loc_operands (loc, hstate);
6480 hash = hstate.end();
6481 CHECKSUM (hash);
6484 /* Calculate the checksum of an attribute. */
6486 static void
6487 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6489 dw_loc_descr_ref loc;
6490 rtx r;
6492 CHECKSUM (at->dw_attr);
6494 /* We don't care that this was compiled with a different compiler
6495 snapshot; if the output is the same, that's what matters. */
6496 if (at->dw_attr == DW_AT_producer)
6497 return;
6499 switch (AT_class (at))
6501 case dw_val_class_const:
6502 case dw_val_class_const_implicit:
6503 CHECKSUM (at->dw_attr_val.v.val_int);
6504 break;
6505 case dw_val_class_unsigned_const:
6506 case dw_val_class_unsigned_const_implicit:
6507 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6508 break;
6509 case dw_val_class_const_double:
6510 CHECKSUM (at->dw_attr_val.v.val_double);
6511 break;
6512 case dw_val_class_wide_int:
6513 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6514 get_full_len (*at->dw_attr_val.v.val_wide)
6515 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6516 break;
6517 case dw_val_class_vec:
6518 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6519 (at->dw_attr_val.v.val_vec.length
6520 * at->dw_attr_val.v.val_vec.elt_size));
6521 break;
6522 case dw_val_class_flag:
6523 CHECKSUM (at->dw_attr_val.v.val_flag);
6524 break;
6525 case dw_val_class_str:
6526 CHECKSUM_STRING (AT_string (at));
6527 break;
6529 case dw_val_class_addr:
6530 r = AT_addr (at);
6531 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6532 CHECKSUM_STRING (XSTR (r, 0));
6533 break;
6535 case dw_val_class_offset:
6536 CHECKSUM (at->dw_attr_val.v.val_offset);
6537 break;
6539 case dw_val_class_loc:
6540 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6541 loc_checksum (loc, ctx);
6542 break;
6544 case dw_val_class_die_ref:
6545 die_checksum (AT_ref (at), ctx, mark);
6546 break;
6548 case dw_val_class_fde_ref:
6549 case dw_val_class_vms_delta:
6550 case dw_val_class_lbl_id:
6551 case dw_val_class_lineptr:
6552 case dw_val_class_macptr:
6553 case dw_val_class_loclistsptr:
6554 case dw_val_class_high_pc:
6555 break;
6557 case dw_val_class_file:
6558 case dw_val_class_file_implicit:
6559 CHECKSUM_STRING (AT_file (at)->filename);
6560 break;
6562 case dw_val_class_data8:
6563 CHECKSUM (at->dw_attr_val.v.val_data8);
6564 break;
6566 default:
6567 break;
6571 /* Calculate the checksum of a DIE. */
6573 static void
6574 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6576 dw_die_ref c;
6577 dw_attr_node *a;
6578 unsigned ix;
6580 /* To avoid infinite recursion. */
6581 if (die->die_mark)
6583 CHECKSUM (die->die_mark);
6584 return;
6586 die->die_mark = ++(*mark);
6588 CHECKSUM (die->die_tag);
6590 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6591 attr_checksum (a, ctx, mark);
6593 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6596 #undef CHECKSUM
6597 #undef CHECKSUM_BLOCK
6598 #undef CHECKSUM_STRING
6600 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6601 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6602 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6603 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6604 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6605 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6606 #define CHECKSUM_ATTR(FOO) \
6607 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6609 /* Calculate the checksum of a number in signed LEB128 format. */
6611 static void
6612 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6614 unsigned char byte;
6615 bool more;
6617 while (1)
6619 byte = (value & 0x7f);
6620 value >>= 7;
6621 more = !((value == 0 && (byte & 0x40) == 0)
6622 || (value == -1 && (byte & 0x40) != 0));
6623 if (more)
6624 byte |= 0x80;
6625 CHECKSUM (byte);
6626 if (!more)
6627 break;
6631 /* Calculate the checksum of a number in unsigned LEB128 format. */
6633 static void
6634 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6636 while (1)
6638 unsigned char byte = (value & 0x7f);
6639 value >>= 7;
6640 if (value != 0)
6641 /* More bytes to follow. */
6642 byte |= 0x80;
6643 CHECKSUM (byte);
6644 if (value == 0)
6645 break;
6649 /* Checksum the context of the DIE. This adds the names of any
6650 surrounding namespaces or structures to the checksum. */
6652 static void
6653 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6655 const char *name;
6656 dw_die_ref spec;
6657 int tag = die->die_tag;
6659 if (tag != DW_TAG_namespace
6660 && tag != DW_TAG_structure_type
6661 && tag != DW_TAG_class_type)
6662 return;
6664 name = get_AT_string (die, DW_AT_name);
6666 spec = get_AT_ref (die, DW_AT_specification);
6667 if (spec != NULL)
6668 die = spec;
6670 if (die->die_parent != NULL)
6671 checksum_die_context (die->die_parent, ctx);
6673 CHECKSUM_ULEB128 ('C');
6674 CHECKSUM_ULEB128 (tag);
6675 if (name != NULL)
6676 CHECKSUM_STRING (name);
6679 /* Calculate the checksum of a location expression. */
6681 static inline void
6682 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6684 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6685 were emitted as a DW_FORM_sdata instead of a location expression. */
6686 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6688 CHECKSUM_ULEB128 (DW_FORM_sdata);
6689 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6690 return;
6693 /* Otherwise, just checksum the raw location expression. */
6694 while (loc != NULL)
6696 inchash::hash hstate;
6697 hashval_t hash;
6699 CHECKSUM_ULEB128 (loc->dtprel);
6700 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6701 hash_loc_operands (loc, hstate);
6702 hash = hstate.end ();
6703 CHECKSUM (hash);
6704 loc = loc->dw_loc_next;
6708 /* Calculate the checksum of an attribute. */
6710 static void
6711 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6712 struct md5_ctx *ctx, int *mark)
6714 dw_loc_descr_ref loc;
6715 rtx r;
6717 if (AT_class (at) == dw_val_class_die_ref)
6719 dw_die_ref target_die = AT_ref (at);
6721 /* For pointer and reference types, we checksum only the (qualified)
6722 name of the target type (if there is a name). For friend entries,
6723 we checksum only the (qualified) name of the target type or function.
6724 This allows the checksum to remain the same whether the target type
6725 is complete or not. */
6726 if ((at->dw_attr == DW_AT_type
6727 && (tag == DW_TAG_pointer_type
6728 || tag == DW_TAG_reference_type
6729 || tag == DW_TAG_rvalue_reference_type
6730 || tag == DW_TAG_ptr_to_member_type))
6731 || (at->dw_attr == DW_AT_friend
6732 && tag == DW_TAG_friend))
6734 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6736 if (name_attr != NULL)
6738 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6740 if (decl == NULL)
6741 decl = target_die;
6742 CHECKSUM_ULEB128 ('N');
6743 CHECKSUM_ULEB128 (at->dw_attr);
6744 if (decl->die_parent != NULL)
6745 checksum_die_context (decl->die_parent, ctx);
6746 CHECKSUM_ULEB128 ('E');
6747 CHECKSUM_STRING (AT_string (name_attr));
6748 return;
6752 /* For all other references to another DIE, we check to see if the
6753 target DIE has already been visited. If it has, we emit a
6754 backward reference; if not, we descend recursively. */
6755 if (target_die->die_mark > 0)
6757 CHECKSUM_ULEB128 ('R');
6758 CHECKSUM_ULEB128 (at->dw_attr);
6759 CHECKSUM_ULEB128 (target_die->die_mark);
6761 else
6763 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6765 if (decl == NULL)
6766 decl = target_die;
6767 target_die->die_mark = ++(*mark);
6768 CHECKSUM_ULEB128 ('T');
6769 CHECKSUM_ULEB128 (at->dw_attr);
6770 if (decl->die_parent != NULL)
6771 checksum_die_context (decl->die_parent, ctx);
6772 die_checksum_ordered (target_die, ctx, mark);
6774 return;
6777 CHECKSUM_ULEB128 ('A');
6778 CHECKSUM_ULEB128 (at->dw_attr);
6780 switch (AT_class (at))
6782 case dw_val_class_const:
6783 case dw_val_class_const_implicit:
6784 CHECKSUM_ULEB128 (DW_FORM_sdata);
6785 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6786 break;
6788 case dw_val_class_unsigned_const:
6789 case dw_val_class_unsigned_const_implicit:
6790 CHECKSUM_ULEB128 (DW_FORM_sdata);
6791 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6792 break;
6794 case dw_val_class_const_double:
6795 CHECKSUM_ULEB128 (DW_FORM_block);
6796 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6797 CHECKSUM (at->dw_attr_val.v.val_double);
6798 break;
6800 case dw_val_class_wide_int:
6801 CHECKSUM_ULEB128 (DW_FORM_block);
6802 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6803 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6804 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6805 get_full_len (*at->dw_attr_val.v.val_wide)
6806 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6807 break;
6809 case dw_val_class_vec:
6810 CHECKSUM_ULEB128 (DW_FORM_block);
6811 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6812 * at->dw_attr_val.v.val_vec.elt_size);
6813 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6814 (at->dw_attr_val.v.val_vec.length
6815 * at->dw_attr_val.v.val_vec.elt_size));
6816 break;
6818 case dw_val_class_flag:
6819 CHECKSUM_ULEB128 (DW_FORM_flag);
6820 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6821 break;
6823 case dw_val_class_str:
6824 CHECKSUM_ULEB128 (DW_FORM_string);
6825 CHECKSUM_STRING (AT_string (at));
6826 break;
6828 case dw_val_class_addr:
6829 r = AT_addr (at);
6830 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6831 CHECKSUM_ULEB128 (DW_FORM_string);
6832 CHECKSUM_STRING (XSTR (r, 0));
6833 break;
6835 case dw_val_class_offset:
6836 CHECKSUM_ULEB128 (DW_FORM_sdata);
6837 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6838 break;
6840 case dw_val_class_loc:
6841 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6842 loc_checksum_ordered (loc, ctx);
6843 break;
6845 case dw_val_class_fde_ref:
6846 case dw_val_class_lbl_id:
6847 case dw_val_class_lineptr:
6848 case dw_val_class_macptr:
6849 case dw_val_class_loclistsptr:
6850 case dw_val_class_high_pc:
6851 break;
6853 case dw_val_class_file:
6854 case dw_val_class_file_implicit:
6855 CHECKSUM_ULEB128 (DW_FORM_string);
6856 CHECKSUM_STRING (AT_file (at)->filename);
6857 break;
6859 case dw_val_class_data8:
6860 CHECKSUM (at->dw_attr_val.v.val_data8);
6861 break;
6863 default:
6864 break;
6868 struct checksum_attributes
6870 dw_attr_node *at_name;
6871 dw_attr_node *at_type;
6872 dw_attr_node *at_friend;
6873 dw_attr_node *at_accessibility;
6874 dw_attr_node *at_address_class;
6875 dw_attr_node *at_alignment;
6876 dw_attr_node *at_allocated;
6877 dw_attr_node *at_artificial;
6878 dw_attr_node *at_associated;
6879 dw_attr_node *at_binary_scale;
6880 dw_attr_node *at_bit_offset;
6881 dw_attr_node *at_bit_size;
6882 dw_attr_node *at_bit_stride;
6883 dw_attr_node *at_byte_size;
6884 dw_attr_node *at_byte_stride;
6885 dw_attr_node *at_const_value;
6886 dw_attr_node *at_containing_type;
6887 dw_attr_node *at_count;
6888 dw_attr_node *at_data_location;
6889 dw_attr_node *at_data_member_location;
6890 dw_attr_node *at_decimal_scale;
6891 dw_attr_node *at_decimal_sign;
6892 dw_attr_node *at_default_value;
6893 dw_attr_node *at_digit_count;
6894 dw_attr_node *at_discr;
6895 dw_attr_node *at_discr_list;
6896 dw_attr_node *at_discr_value;
6897 dw_attr_node *at_encoding;
6898 dw_attr_node *at_endianity;
6899 dw_attr_node *at_explicit;
6900 dw_attr_node *at_is_optional;
6901 dw_attr_node *at_location;
6902 dw_attr_node *at_lower_bound;
6903 dw_attr_node *at_mutable;
6904 dw_attr_node *at_ordering;
6905 dw_attr_node *at_picture_string;
6906 dw_attr_node *at_prototyped;
6907 dw_attr_node *at_small;
6908 dw_attr_node *at_segment;
6909 dw_attr_node *at_string_length;
6910 dw_attr_node *at_string_length_bit_size;
6911 dw_attr_node *at_string_length_byte_size;
6912 dw_attr_node *at_threads_scaled;
6913 dw_attr_node *at_upper_bound;
6914 dw_attr_node *at_use_location;
6915 dw_attr_node *at_use_UTF8;
6916 dw_attr_node *at_variable_parameter;
6917 dw_attr_node *at_virtuality;
6918 dw_attr_node *at_visibility;
6919 dw_attr_node *at_vtable_elem_location;
6922 /* Collect the attributes that we will want to use for the checksum. */
6924 static void
6925 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6927 dw_attr_node *a;
6928 unsigned ix;
6930 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6932 switch (a->dw_attr)
6934 case DW_AT_name:
6935 attrs->at_name = a;
6936 break;
6937 case DW_AT_type:
6938 attrs->at_type = a;
6939 break;
6940 case DW_AT_friend:
6941 attrs->at_friend = a;
6942 break;
6943 case DW_AT_accessibility:
6944 attrs->at_accessibility = a;
6945 break;
6946 case DW_AT_address_class:
6947 attrs->at_address_class = a;
6948 break;
6949 case DW_AT_alignment:
6950 attrs->at_alignment = a;
6951 break;
6952 case DW_AT_allocated:
6953 attrs->at_allocated = a;
6954 break;
6955 case DW_AT_artificial:
6956 attrs->at_artificial = a;
6957 break;
6958 case DW_AT_associated:
6959 attrs->at_associated = a;
6960 break;
6961 case DW_AT_binary_scale:
6962 attrs->at_binary_scale = a;
6963 break;
6964 case DW_AT_bit_offset:
6965 attrs->at_bit_offset = a;
6966 break;
6967 case DW_AT_bit_size:
6968 attrs->at_bit_size = a;
6969 break;
6970 case DW_AT_bit_stride:
6971 attrs->at_bit_stride = a;
6972 break;
6973 case DW_AT_byte_size:
6974 attrs->at_byte_size = a;
6975 break;
6976 case DW_AT_byte_stride:
6977 attrs->at_byte_stride = a;
6978 break;
6979 case DW_AT_const_value:
6980 attrs->at_const_value = a;
6981 break;
6982 case DW_AT_containing_type:
6983 attrs->at_containing_type = a;
6984 break;
6985 case DW_AT_count:
6986 attrs->at_count = a;
6987 break;
6988 case DW_AT_data_location:
6989 attrs->at_data_location = a;
6990 break;
6991 case DW_AT_data_member_location:
6992 attrs->at_data_member_location = a;
6993 break;
6994 case DW_AT_decimal_scale:
6995 attrs->at_decimal_scale = a;
6996 break;
6997 case DW_AT_decimal_sign:
6998 attrs->at_decimal_sign = a;
6999 break;
7000 case DW_AT_default_value:
7001 attrs->at_default_value = a;
7002 break;
7003 case DW_AT_digit_count:
7004 attrs->at_digit_count = a;
7005 break;
7006 case DW_AT_discr:
7007 attrs->at_discr = a;
7008 break;
7009 case DW_AT_discr_list:
7010 attrs->at_discr_list = a;
7011 break;
7012 case DW_AT_discr_value:
7013 attrs->at_discr_value = a;
7014 break;
7015 case DW_AT_encoding:
7016 attrs->at_encoding = a;
7017 break;
7018 case DW_AT_endianity:
7019 attrs->at_endianity = a;
7020 break;
7021 case DW_AT_explicit:
7022 attrs->at_explicit = a;
7023 break;
7024 case DW_AT_is_optional:
7025 attrs->at_is_optional = a;
7026 break;
7027 case DW_AT_location:
7028 attrs->at_location = a;
7029 break;
7030 case DW_AT_lower_bound:
7031 attrs->at_lower_bound = a;
7032 break;
7033 case DW_AT_mutable:
7034 attrs->at_mutable = a;
7035 break;
7036 case DW_AT_ordering:
7037 attrs->at_ordering = a;
7038 break;
7039 case DW_AT_picture_string:
7040 attrs->at_picture_string = a;
7041 break;
7042 case DW_AT_prototyped:
7043 attrs->at_prototyped = a;
7044 break;
7045 case DW_AT_small:
7046 attrs->at_small = a;
7047 break;
7048 case DW_AT_segment:
7049 attrs->at_segment = a;
7050 break;
7051 case DW_AT_string_length:
7052 attrs->at_string_length = a;
7053 break;
7054 case DW_AT_string_length_bit_size:
7055 attrs->at_string_length_bit_size = a;
7056 break;
7057 case DW_AT_string_length_byte_size:
7058 attrs->at_string_length_byte_size = a;
7059 break;
7060 case DW_AT_threads_scaled:
7061 attrs->at_threads_scaled = a;
7062 break;
7063 case DW_AT_upper_bound:
7064 attrs->at_upper_bound = a;
7065 break;
7066 case DW_AT_use_location:
7067 attrs->at_use_location = a;
7068 break;
7069 case DW_AT_use_UTF8:
7070 attrs->at_use_UTF8 = a;
7071 break;
7072 case DW_AT_variable_parameter:
7073 attrs->at_variable_parameter = a;
7074 break;
7075 case DW_AT_virtuality:
7076 attrs->at_virtuality = a;
7077 break;
7078 case DW_AT_visibility:
7079 attrs->at_visibility = a;
7080 break;
7081 case DW_AT_vtable_elem_location:
7082 attrs->at_vtable_elem_location = a;
7083 break;
7084 default:
7085 break;
7090 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7092 static void
7093 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7095 dw_die_ref c;
7096 dw_die_ref decl;
7097 struct checksum_attributes attrs;
7099 CHECKSUM_ULEB128 ('D');
7100 CHECKSUM_ULEB128 (die->die_tag);
7102 memset (&attrs, 0, sizeof (attrs));
7104 decl = get_AT_ref (die, DW_AT_specification);
7105 if (decl != NULL)
7106 collect_checksum_attributes (&attrs, decl);
7107 collect_checksum_attributes (&attrs, die);
7109 CHECKSUM_ATTR (attrs.at_name);
7110 CHECKSUM_ATTR (attrs.at_accessibility);
7111 CHECKSUM_ATTR (attrs.at_address_class);
7112 CHECKSUM_ATTR (attrs.at_allocated);
7113 CHECKSUM_ATTR (attrs.at_artificial);
7114 CHECKSUM_ATTR (attrs.at_associated);
7115 CHECKSUM_ATTR (attrs.at_binary_scale);
7116 CHECKSUM_ATTR (attrs.at_bit_offset);
7117 CHECKSUM_ATTR (attrs.at_bit_size);
7118 CHECKSUM_ATTR (attrs.at_bit_stride);
7119 CHECKSUM_ATTR (attrs.at_byte_size);
7120 CHECKSUM_ATTR (attrs.at_byte_stride);
7121 CHECKSUM_ATTR (attrs.at_const_value);
7122 CHECKSUM_ATTR (attrs.at_containing_type);
7123 CHECKSUM_ATTR (attrs.at_count);
7124 CHECKSUM_ATTR (attrs.at_data_location);
7125 CHECKSUM_ATTR (attrs.at_data_member_location);
7126 CHECKSUM_ATTR (attrs.at_decimal_scale);
7127 CHECKSUM_ATTR (attrs.at_decimal_sign);
7128 CHECKSUM_ATTR (attrs.at_default_value);
7129 CHECKSUM_ATTR (attrs.at_digit_count);
7130 CHECKSUM_ATTR (attrs.at_discr);
7131 CHECKSUM_ATTR (attrs.at_discr_list);
7132 CHECKSUM_ATTR (attrs.at_discr_value);
7133 CHECKSUM_ATTR (attrs.at_encoding);
7134 CHECKSUM_ATTR (attrs.at_endianity);
7135 CHECKSUM_ATTR (attrs.at_explicit);
7136 CHECKSUM_ATTR (attrs.at_is_optional);
7137 CHECKSUM_ATTR (attrs.at_location);
7138 CHECKSUM_ATTR (attrs.at_lower_bound);
7139 CHECKSUM_ATTR (attrs.at_mutable);
7140 CHECKSUM_ATTR (attrs.at_ordering);
7141 CHECKSUM_ATTR (attrs.at_picture_string);
7142 CHECKSUM_ATTR (attrs.at_prototyped);
7143 CHECKSUM_ATTR (attrs.at_small);
7144 CHECKSUM_ATTR (attrs.at_segment);
7145 CHECKSUM_ATTR (attrs.at_string_length);
7146 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7147 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7148 CHECKSUM_ATTR (attrs.at_threads_scaled);
7149 CHECKSUM_ATTR (attrs.at_upper_bound);
7150 CHECKSUM_ATTR (attrs.at_use_location);
7151 CHECKSUM_ATTR (attrs.at_use_UTF8);
7152 CHECKSUM_ATTR (attrs.at_variable_parameter);
7153 CHECKSUM_ATTR (attrs.at_virtuality);
7154 CHECKSUM_ATTR (attrs.at_visibility);
7155 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7156 CHECKSUM_ATTR (attrs.at_type);
7157 CHECKSUM_ATTR (attrs.at_friend);
7158 CHECKSUM_ATTR (attrs.at_alignment);
7160 /* Checksum the child DIEs. */
7161 c = die->die_child;
7162 if (c) do {
7163 dw_attr_node *name_attr;
7165 c = c->die_sib;
7166 name_attr = get_AT (c, DW_AT_name);
7167 if (is_template_instantiation (c))
7169 /* Ignore instantiations of member type and function templates. */
7171 else if (name_attr != NULL
7172 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7174 /* Use a shallow checksum for named nested types and member
7175 functions. */
7176 CHECKSUM_ULEB128 ('S');
7177 CHECKSUM_ULEB128 (c->die_tag);
7178 CHECKSUM_STRING (AT_string (name_attr));
7180 else
7182 /* Use a deep checksum for other children. */
7183 /* Mark this DIE so it gets processed when unmarking. */
7184 if (c->die_mark == 0)
7185 c->die_mark = -1;
7186 die_checksum_ordered (c, ctx, mark);
7188 } while (c != die->die_child);
7190 CHECKSUM_ULEB128 (0);
7193 /* Add a type name and tag to a hash. */
7194 static void
7195 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7197 CHECKSUM_ULEB128 (tag);
7198 CHECKSUM_STRING (name);
7201 #undef CHECKSUM
7202 #undef CHECKSUM_STRING
7203 #undef CHECKSUM_ATTR
7204 #undef CHECKSUM_LEB128
7205 #undef CHECKSUM_ULEB128
7207 /* Generate the type signature for DIE. This is computed by generating an
7208 MD5 checksum over the DIE's tag, its relevant attributes, and its
7209 children. Attributes that are references to other DIEs are processed
7210 by recursion, using the MARK field to prevent infinite recursion.
7211 If the DIE is nested inside a namespace or another type, we also
7212 need to include that context in the signature. The lower 64 bits
7213 of the resulting MD5 checksum comprise the signature. */
7215 static void
7216 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7218 int mark;
7219 const char *name;
7220 unsigned char checksum[16];
7221 struct md5_ctx ctx;
7222 dw_die_ref decl;
7223 dw_die_ref parent;
7225 name = get_AT_string (die, DW_AT_name);
7226 decl = get_AT_ref (die, DW_AT_specification);
7227 parent = get_die_parent (die);
7229 /* First, compute a signature for just the type name (and its surrounding
7230 context, if any. This is stored in the type unit DIE for link-time
7231 ODR (one-definition rule) checking. */
7233 if (is_cxx () && name != NULL)
7235 md5_init_ctx (&ctx);
7237 /* Checksum the names of surrounding namespaces and structures. */
7238 if (parent != NULL)
7239 checksum_die_context (parent, &ctx);
7241 /* Checksum the current DIE. */
7242 die_odr_checksum (die->die_tag, name, &ctx);
7243 md5_finish_ctx (&ctx, checksum);
7245 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7248 /* Next, compute the complete type signature. */
7250 md5_init_ctx (&ctx);
7251 mark = 1;
7252 die->die_mark = mark;
7254 /* Checksum the names of surrounding namespaces and structures. */
7255 if (parent != NULL)
7256 checksum_die_context (parent, &ctx);
7258 /* Checksum the DIE and its children. */
7259 die_checksum_ordered (die, &ctx, &mark);
7260 unmark_all_dies (die);
7261 md5_finish_ctx (&ctx, checksum);
7263 /* Store the signature in the type node and link the type DIE and the
7264 type node together. */
7265 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7266 DWARF_TYPE_SIGNATURE_SIZE);
7267 die->comdat_type_p = true;
7268 die->die_id.die_type_node = type_node;
7269 type_node->type_die = die;
7271 /* If the DIE is a specification, link its declaration to the type node
7272 as well. */
7273 if (decl != NULL)
7275 decl->comdat_type_p = true;
7276 decl->die_id.die_type_node = type_node;
7280 /* Do the location expressions look same? */
7281 static inline int
7282 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7284 return loc1->dw_loc_opc == loc2->dw_loc_opc
7285 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7286 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7289 /* Do the values look the same? */
7290 static int
7291 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7293 dw_loc_descr_ref loc1, loc2;
7294 rtx r1, r2;
7296 if (v1->val_class != v2->val_class)
7297 return 0;
7299 switch (v1->val_class)
7301 case dw_val_class_const:
7302 case dw_val_class_const_implicit:
7303 return v1->v.val_int == v2->v.val_int;
7304 case dw_val_class_unsigned_const:
7305 case dw_val_class_unsigned_const_implicit:
7306 return v1->v.val_unsigned == v2->v.val_unsigned;
7307 case dw_val_class_const_double:
7308 return v1->v.val_double.high == v2->v.val_double.high
7309 && v1->v.val_double.low == v2->v.val_double.low;
7310 case dw_val_class_wide_int:
7311 return *v1->v.val_wide == *v2->v.val_wide;
7312 case dw_val_class_vec:
7313 if (v1->v.val_vec.length != v2->v.val_vec.length
7314 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7315 return 0;
7316 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7317 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7318 return 0;
7319 return 1;
7320 case dw_val_class_flag:
7321 return v1->v.val_flag == v2->v.val_flag;
7322 case dw_val_class_str:
7323 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7325 case dw_val_class_addr:
7326 r1 = v1->v.val_addr;
7327 r2 = v2->v.val_addr;
7328 if (GET_CODE (r1) != GET_CODE (r2))
7329 return 0;
7330 return !rtx_equal_p (r1, r2);
7332 case dw_val_class_offset:
7333 return v1->v.val_offset == v2->v.val_offset;
7335 case dw_val_class_loc:
7336 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7337 loc1 && loc2;
7338 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7339 if (!same_loc_p (loc1, loc2, mark))
7340 return 0;
7341 return !loc1 && !loc2;
7343 case dw_val_class_die_ref:
7344 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7346 case dw_val_class_fde_ref:
7347 case dw_val_class_vms_delta:
7348 case dw_val_class_lbl_id:
7349 case dw_val_class_lineptr:
7350 case dw_val_class_macptr:
7351 case dw_val_class_loclistsptr:
7352 case dw_val_class_high_pc:
7353 return 1;
7355 case dw_val_class_file:
7356 case dw_val_class_file_implicit:
7357 return v1->v.val_file == v2->v.val_file;
7359 case dw_val_class_data8:
7360 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7362 default:
7363 return 1;
7367 /* Do the attributes look the same? */
7369 static int
7370 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7372 if (at1->dw_attr != at2->dw_attr)
7373 return 0;
7375 /* We don't care that this was compiled with a different compiler
7376 snapshot; if the output is the same, that's what matters. */
7377 if (at1->dw_attr == DW_AT_producer)
7378 return 1;
7380 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7383 /* Do the dies look the same? */
7385 static int
7386 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7388 dw_die_ref c1, c2;
7389 dw_attr_node *a1;
7390 unsigned ix;
7392 /* To avoid infinite recursion. */
7393 if (die1->die_mark)
7394 return die1->die_mark == die2->die_mark;
7395 die1->die_mark = die2->die_mark = ++(*mark);
7397 if (die1->die_tag != die2->die_tag)
7398 return 0;
7400 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7401 return 0;
7403 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7404 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7405 return 0;
7407 c1 = die1->die_child;
7408 c2 = die2->die_child;
7409 if (! c1)
7411 if (c2)
7412 return 0;
7414 else
7415 for (;;)
7417 if (!same_die_p (c1, c2, mark))
7418 return 0;
7419 c1 = c1->die_sib;
7420 c2 = c2->die_sib;
7421 if (c1 == die1->die_child)
7423 if (c2 == die2->die_child)
7424 break;
7425 else
7426 return 0;
7430 return 1;
7433 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7434 children, and set die_symbol. */
7436 static void
7437 compute_comp_unit_symbol (dw_die_ref unit_die)
7439 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7440 const char *base = die_name ? lbasename (die_name) : "anonymous";
7441 char *name = XALLOCAVEC (char, strlen (base) + 64);
7442 char *p;
7443 int i, mark;
7444 unsigned char checksum[16];
7445 struct md5_ctx ctx;
7447 /* Compute the checksum of the DIE, then append part of it as hex digits to
7448 the name filename of the unit. */
7450 md5_init_ctx (&ctx);
7451 mark = 0;
7452 die_checksum (unit_die, &ctx, &mark);
7453 unmark_all_dies (unit_die);
7454 md5_finish_ctx (&ctx, checksum);
7456 /* When we this for comp_unit_die () we have a DW_AT_name that might
7457 not start with a letter but with anything valid for filenames and
7458 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7459 character is not a letter. */
7460 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7461 clean_symbol_name (name);
7463 p = name + strlen (name);
7464 for (i = 0; i < 4; i++)
7466 sprintf (p, "%.2x", checksum[i]);
7467 p += 2;
7470 unit_die->die_id.die_symbol = xstrdup (name);
7473 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7475 static int
7476 is_type_die (dw_die_ref die)
7478 switch (die->die_tag)
7480 case DW_TAG_array_type:
7481 case DW_TAG_class_type:
7482 case DW_TAG_interface_type:
7483 case DW_TAG_enumeration_type:
7484 case DW_TAG_pointer_type:
7485 case DW_TAG_reference_type:
7486 case DW_TAG_rvalue_reference_type:
7487 case DW_TAG_string_type:
7488 case DW_TAG_structure_type:
7489 case DW_TAG_subroutine_type:
7490 case DW_TAG_union_type:
7491 case DW_TAG_ptr_to_member_type:
7492 case DW_TAG_set_type:
7493 case DW_TAG_subrange_type:
7494 case DW_TAG_base_type:
7495 case DW_TAG_const_type:
7496 case DW_TAG_file_type:
7497 case DW_TAG_packed_type:
7498 case DW_TAG_volatile_type:
7499 case DW_TAG_typedef:
7500 return 1;
7501 default:
7502 return 0;
7506 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7507 Basically, we want to choose the bits that are likely to be shared between
7508 compilations (types) and leave out the bits that are specific to individual
7509 compilations (functions). */
7511 static int
7512 is_comdat_die (dw_die_ref c)
7514 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7515 we do for stabs. The advantage is a greater likelihood of sharing between
7516 objects that don't include headers in the same order (and therefore would
7517 put the base types in a different comdat). jason 8/28/00 */
7519 if (c->die_tag == DW_TAG_base_type)
7520 return 0;
7522 if (c->die_tag == DW_TAG_pointer_type
7523 || c->die_tag == DW_TAG_reference_type
7524 || c->die_tag == DW_TAG_rvalue_reference_type
7525 || c->die_tag == DW_TAG_const_type
7526 || c->die_tag == DW_TAG_volatile_type)
7528 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7530 return t ? is_comdat_die (t) : 0;
7533 return is_type_die (c);
7536 /* Returns true iff C is a compile-unit DIE. */
7538 static inline bool
7539 is_cu_die (dw_die_ref c)
7541 return c && (c->die_tag == DW_TAG_compile_unit
7542 || c->die_tag == DW_TAG_skeleton_unit);
7545 /* Returns true iff C is a unit DIE of some sort. */
7547 static inline bool
7548 is_unit_die (dw_die_ref c)
7550 return c && (c->die_tag == DW_TAG_compile_unit
7551 || c->die_tag == DW_TAG_partial_unit
7552 || c->die_tag == DW_TAG_type_unit
7553 || c->die_tag == DW_TAG_skeleton_unit);
7556 /* Returns true iff C is a namespace DIE. */
7558 static inline bool
7559 is_namespace_die (dw_die_ref c)
7561 return c && c->die_tag == DW_TAG_namespace;
7564 /* Returns true iff C is a class or structure DIE. */
7566 static inline bool
7567 is_class_die (dw_die_ref c)
7569 return c && (c->die_tag == DW_TAG_class_type
7570 || c->die_tag == DW_TAG_structure_type);
7573 /* Return non-zero if this DIE is a template parameter. */
7575 static inline bool
7576 is_template_parameter (dw_die_ref die)
7578 switch (die->die_tag)
7580 case DW_TAG_template_type_param:
7581 case DW_TAG_template_value_param:
7582 case DW_TAG_GNU_template_template_param:
7583 case DW_TAG_GNU_template_parameter_pack:
7584 return true;
7585 default:
7586 return false;
7590 /* Return non-zero if this DIE represents a template instantiation. */
7592 static inline bool
7593 is_template_instantiation (dw_die_ref die)
7595 dw_die_ref c;
7597 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7598 return false;
7599 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7600 return false;
7603 static char *
7604 gen_internal_sym (const char *prefix)
7606 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7608 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7609 return xstrdup (buf);
7612 /* Return non-zero if this DIE is a declaration. */
7614 static int
7615 is_declaration_die (dw_die_ref die)
7617 dw_attr_node *a;
7618 unsigned ix;
7620 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7621 if (a->dw_attr == DW_AT_declaration)
7622 return 1;
7624 return 0;
7627 /* Return non-zero if this DIE is nested inside a subprogram. */
7629 static int
7630 is_nested_in_subprogram (dw_die_ref die)
7632 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7634 if (decl == NULL)
7635 decl = die;
7636 return local_scope_p (decl);
7639 /* Return non-zero if this DIE contains a defining declaration of a
7640 subprogram. */
7642 static int
7643 contains_subprogram_definition (dw_die_ref die)
7645 dw_die_ref c;
7647 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7648 return 1;
7649 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7650 return 0;
7653 /* Return non-zero if this is a type DIE that should be moved to a
7654 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7655 unit type. */
7657 static int
7658 should_move_die_to_comdat (dw_die_ref die)
7660 switch (die->die_tag)
7662 case DW_TAG_class_type:
7663 case DW_TAG_structure_type:
7664 case DW_TAG_enumeration_type:
7665 case DW_TAG_union_type:
7666 /* Don't move declarations, inlined instances, types nested in a
7667 subprogram, or types that contain subprogram definitions. */
7668 if (is_declaration_die (die)
7669 || get_AT (die, DW_AT_abstract_origin)
7670 || is_nested_in_subprogram (die)
7671 || contains_subprogram_definition (die))
7672 return 0;
7673 return 1;
7674 case DW_TAG_array_type:
7675 case DW_TAG_interface_type:
7676 case DW_TAG_pointer_type:
7677 case DW_TAG_reference_type:
7678 case DW_TAG_rvalue_reference_type:
7679 case DW_TAG_string_type:
7680 case DW_TAG_subroutine_type:
7681 case DW_TAG_ptr_to_member_type:
7682 case DW_TAG_set_type:
7683 case DW_TAG_subrange_type:
7684 case DW_TAG_base_type:
7685 case DW_TAG_const_type:
7686 case DW_TAG_file_type:
7687 case DW_TAG_packed_type:
7688 case DW_TAG_volatile_type:
7689 case DW_TAG_typedef:
7690 default:
7691 return 0;
7695 /* Make a clone of DIE. */
7697 static dw_die_ref
7698 clone_die (dw_die_ref die)
7700 dw_die_ref clone;
7701 dw_attr_node *a;
7702 unsigned ix;
7704 clone = ggc_cleared_alloc<die_node> ();
7705 clone->die_tag = die->die_tag;
7707 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7708 add_dwarf_attr (clone, a);
7710 return clone;
7713 /* Make a clone of the tree rooted at DIE. */
7715 static dw_die_ref
7716 clone_tree (dw_die_ref die)
7718 dw_die_ref c;
7719 dw_die_ref clone = clone_die (die);
7721 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7723 return clone;
7726 /* Make a clone of DIE as a declaration. */
7728 static dw_die_ref
7729 clone_as_declaration (dw_die_ref die)
7731 dw_die_ref clone;
7732 dw_die_ref decl;
7733 dw_attr_node *a;
7734 unsigned ix;
7736 /* If the DIE is already a declaration, just clone it. */
7737 if (is_declaration_die (die))
7738 return clone_die (die);
7740 /* If the DIE is a specification, just clone its declaration DIE. */
7741 decl = get_AT_ref (die, DW_AT_specification);
7742 if (decl != NULL)
7744 clone = clone_die (decl);
7745 if (die->comdat_type_p)
7746 add_AT_die_ref (clone, DW_AT_signature, die);
7747 return clone;
7750 clone = ggc_cleared_alloc<die_node> ();
7751 clone->die_tag = die->die_tag;
7753 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7755 /* We don't want to copy over all attributes.
7756 For example we don't want DW_AT_byte_size because otherwise we will no
7757 longer have a declaration and GDB will treat it as a definition. */
7759 switch (a->dw_attr)
7761 case DW_AT_abstract_origin:
7762 case DW_AT_artificial:
7763 case DW_AT_containing_type:
7764 case DW_AT_external:
7765 case DW_AT_name:
7766 case DW_AT_type:
7767 case DW_AT_virtuality:
7768 case DW_AT_linkage_name:
7769 case DW_AT_MIPS_linkage_name:
7770 add_dwarf_attr (clone, a);
7771 break;
7772 case DW_AT_byte_size:
7773 case DW_AT_alignment:
7774 default:
7775 break;
7779 if (die->comdat_type_p)
7780 add_AT_die_ref (clone, DW_AT_signature, die);
7782 add_AT_flag (clone, DW_AT_declaration, 1);
7783 return clone;
7787 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7789 struct decl_table_entry
7791 dw_die_ref orig;
7792 dw_die_ref copy;
7795 /* Helpers to manipulate hash table of copied declarations. */
7797 /* Hashtable helpers. */
7799 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7801 typedef die_struct *compare_type;
7802 static inline hashval_t hash (const decl_table_entry *);
7803 static inline bool equal (const decl_table_entry *, const die_struct *);
7806 inline hashval_t
7807 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7809 return htab_hash_pointer (entry->orig);
7812 inline bool
7813 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7814 const die_struct *entry2)
7816 return entry1->orig == entry2;
7819 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7821 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7822 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7823 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7824 to check if the ancestor has already been copied into UNIT. */
7826 static dw_die_ref
7827 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7828 decl_hash_type *decl_table)
7830 dw_die_ref parent = die->die_parent;
7831 dw_die_ref new_parent = unit;
7832 dw_die_ref copy;
7833 decl_table_entry **slot = NULL;
7834 struct decl_table_entry *entry = NULL;
7836 if (decl_table)
7838 /* Check if the entry has already been copied to UNIT. */
7839 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7840 INSERT);
7841 if (*slot != HTAB_EMPTY_ENTRY)
7843 entry = *slot;
7844 return entry->copy;
7847 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7848 entry = XCNEW (struct decl_table_entry);
7849 entry->orig = die;
7850 entry->copy = NULL;
7851 *slot = entry;
7854 if (parent != NULL)
7856 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7857 if (spec != NULL)
7858 parent = spec;
7859 if (!is_unit_die (parent))
7860 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7863 copy = clone_as_declaration (die);
7864 add_child_die (new_parent, copy);
7866 if (decl_table)
7868 /* Record the pointer to the copy. */
7869 entry->copy = copy;
7872 return copy;
7874 /* Copy the declaration context to the new type unit DIE. This includes
7875 any surrounding namespace or type declarations. If the DIE has an
7876 AT_specification attribute, it also includes attributes and children
7877 attached to the specification, and returns a pointer to the original
7878 parent of the declaration DIE. Returns NULL otherwise. */
7880 static dw_die_ref
7881 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7883 dw_die_ref decl;
7884 dw_die_ref new_decl;
7885 dw_die_ref orig_parent = NULL;
7887 decl = get_AT_ref (die, DW_AT_specification);
7888 if (decl == NULL)
7889 decl = die;
7890 else
7892 unsigned ix;
7893 dw_die_ref c;
7894 dw_attr_node *a;
7896 /* The original DIE will be changed to a declaration, and must
7897 be moved to be a child of the original declaration DIE. */
7898 orig_parent = decl->die_parent;
7900 /* Copy the type node pointer from the new DIE to the original
7901 declaration DIE so we can forward references later. */
7902 decl->comdat_type_p = true;
7903 decl->die_id.die_type_node = die->die_id.die_type_node;
7905 remove_AT (die, DW_AT_specification);
7907 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7909 if (a->dw_attr != DW_AT_name
7910 && a->dw_attr != DW_AT_declaration
7911 && a->dw_attr != DW_AT_external)
7912 add_dwarf_attr (die, a);
7915 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7918 if (decl->die_parent != NULL
7919 && !is_unit_die (decl->die_parent))
7921 new_decl = copy_ancestor_tree (unit, decl, NULL);
7922 if (new_decl != NULL)
7924 remove_AT (new_decl, DW_AT_signature);
7925 add_AT_specification (die, new_decl);
7929 return orig_parent;
7932 /* Generate the skeleton ancestor tree for the given NODE, then clone
7933 the DIE and add the clone into the tree. */
7935 static void
7936 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7938 if (node->new_die != NULL)
7939 return;
7941 node->new_die = clone_as_declaration (node->old_die);
7943 if (node->parent != NULL)
7945 generate_skeleton_ancestor_tree (node->parent);
7946 add_child_die (node->parent->new_die, node->new_die);
7950 /* Generate a skeleton tree of DIEs containing any declarations that are
7951 found in the original tree. We traverse the tree looking for declaration
7952 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7954 static void
7955 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7957 skeleton_chain_node node;
7958 dw_die_ref c;
7959 dw_die_ref first;
7960 dw_die_ref prev = NULL;
7961 dw_die_ref next = NULL;
7963 node.parent = parent;
7965 first = c = parent->old_die->die_child;
7966 if (c)
7967 next = c->die_sib;
7968 if (c) do {
7969 if (prev == NULL || prev->die_sib == c)
7970 prev = c;
7971 c = next;
7972 next = (c == first ? NULL : c->die_sib);
7973 node.old_die = c;
7974 node.new_die = NULL;
7975 if (is_declaration_die (c))
7977 if (is_template_instantiation (c))
7979 /* Instantiated templates do not need to be cloned into the
7980 type unit. Just move the DIE and its children back to
7981 the skeleton tree (in the main CU). */
7982 remove_child_with_prev (c, prev);
7983 add_child_die (parent->new_die, c);
7984 c = prev;
7986 else if (c->comdat_type_p)
7988 /* This is the skeleton of earlier break_out_comdat_types
7989 type. Clone the existing DIE, but keep the children
7990 under the original (which is in the main CU). */
7991 dw_die_ref clone = clone_die (c);
7993 replace_child (c, clone, prev);
7994 generate_skeleton_ancestor_tree (parent);
7995 add_child_die (parent->new_die, c);
7996 c = clone;
7997 continue;
7999 else
8001 /* Clone the existing DIE, move the original to the skeleton
8002 tree (which is in the main CU), and put the clone, with
8003 all the original's children, where the original came from
8004 (which is about to be moved to the type unit). */
8005 dw_die_ref clone = clone_die (c);
8006 move_all_children (c, clone);
8008 /* If the original has a DW_AT_object_pointer attribute,
8009 it would now point to a child DIE just moved to the
8010 cloned tree, so we need to remove that attribute from
8011 the original. */
8012 remove_AT (c, DW_AT_object_pointer);
8014 replace_child (c, clone, prev);
8015 generate_skeleton_ancestor_tree (parent);
8016 add_child_die (parent->new_die, c);
8017 node.old_die = clone;
8018 node.new_die = c;
8019 c = clone;
8022 generate_skeleton_bottom_up (&node);
8023 } while (next != NULL);
8026 /* Wrapper function for generate_skeleton_bottom_up. */
8028 static dw_die_ref
8029 generate_skeleton (dw_die_ref die)
8031 skeleton_chain_node node;
8033 node.old_die = die;
8034 node.new_die = NULL;
8035 node.parent = NULL;
8037 /* If this type definition is nested inside another type,
8038 and is not an instantiation of a template, always leave
8039 at least a declaration in its place. */
8040 if (die->die_parent != NULL
8041 && is_type_die (die->die_parent)
8042 && !is_template_instantiation (die))
8043 node.new_die = clone_as_declaration (die);
8045 generate_skeleton_bottom_up (&node);
8046 return node.new_die;
8049 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8050 declaration. The original DIE is moved to a new compile unit so that
8051 existing references to it follow it to the new location. If any of the
8052 original DIE's descendants is a declaration, we need to replace the
8053 original DIE with a skeleton tree and move the declarations back into the
8054 skeleton tree. */
8056 static dw_die_ref
8057 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8058 dw_die_ref prev)
8060 dw_die_ref skeleton, orig_parent;
8062 /* Copy the declaration context to the type unit DIE. If the returned
8063 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8064 that DIE. */
8065 orig_parent = copy_declaration_context (unit, child);
8067 skeleton = generate_skeleton (child);
8068 if (skeleton == NULL)
8069 remove_child_with_prev (child, prev);
8070 else
8072 skeleton->comdat_type_p = true;
8073 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8075 /* If the original DIE was a specification, we need to put
8076 the skeleton under the parent DIE of the declaration.
8077 This leaves the original declaration in the tree, but
8078 it will be pruned later since there are no longer any
8079 references to it. */
8080 if (orig_parent != NULL)
8082 remove_child_with_prev (child, prev);
8083 add_child_die (orig_parent, skeleton);
8085 else
8086 replace_child (child, skeleton, prev);
8089 return skeleton;
8092 static void
8093 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8094 comdat_type_node *type_node,
8095 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8097 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8098 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8099 DWARF procedure references in the DW_AT_location attribute. */
8101 static dw_die_ref
8102 copy_dwarf_procedure (dw_die_ref die,
8103 comdat_type_node *type_node,
8104 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8106 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8108 /* DWARF procedures are not supposed to have children... */
8109 gcc_assert (die->die_child == NULL);
8111 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8112 gcc_assert (vec_safe_length (die->die_attr) == 1
8113 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8115 /* Do not copy more than once DWARF procedures. */
8116 bool existed;
8117 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8118 if (existed)
8119 return die_copy;
8121 die_copy = clone_die (die);
8122 add_child_die (type_node->root_die, die_copy);
8123 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8124 return die_copy;
8127 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8128 procedures in DIE's attributes. */
8130 static void
8131 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8132 comdat_type_node *type_node,
8133 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8135 dw_attr_node *a;
8136 unsigned i;
8138 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8140 dw_loc_descr_ref loc;
8142 if (a->dw_attr_val.val_class != dw_val_class_loc)
8143 continue;
8145 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8147 switch (loc->dw_loc_opc)
8149 case DW_OP_call2:
8150 case DW_OP_call4:
8151 case DW_OP_call_ref:
8152 gcc_assert (loc->dw_loc_oprnd1.val_class
8153 == dw_val_class_die_ref);
8154 loc->dw_loc_oprnd1.v.val_die_ref.die
8155 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8156 type_node,
8157 copied_dwarf_procs);
8159 default:
8160 break;
8166 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8167 rewrite references to point to the copies.
8169 References are looked for in DIE's attributes and recursively in all its
8170 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8171 mapping from old DWARF procedures to their copy. It is used not to copy
8172 twice the same DWARF procedure under TYPE_NODE. */
8174 static void
8175 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8176 comdat_type_node *type_node,
8177 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8179 dw_die_ref c;
8181 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8182 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8183 type_node,
8184 copied_dwarf_procs));
8187 /* Traverse the DIE and set up additional .debug_types or .debug_info
8188 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8189 section. */
8191 static void
8192 break_out_comdat_types (dw_die_ref die)
8194 dw_die_ref c;
8195 dw_die_ref first;
8196 dw_die_ref prev = NULL;
8197 dw_die_ref next = NULL;
8198 dw_die_ref unit = NULL;
8200 first = c = die->die_child;
8201 if (c)
8202 next = c->die_sib;
8203 if (c) do {
8204 if (prev == NULL || prev->die_sib == c)
8205 prev = c;
8206 c = next;
8207 next = (c == first ? NULL : c->die_sib);
8208 if (should_move_die_to_comdat (c))
8210 dw_die_ref replacement;
8211 comdat_type_node *type_node;
8213 /* Break out nested types into their own type units. */
8214 break_out_comdat_types (c);
8216 /* Create a new type unit DIE as the root for the new tree, and
8217 add it to the list of comdat types. */
8218 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8219 add_AT_unsigned (unit, DW_AT_language,
8220 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8221 type_node = ggc_cleared_alloc<comdat_type_node> ();
8222 type_node->root_die = unit;
8223 type_node->next = comdat_type_list;
8224 comdat_type_list = type_node;
8226 /* Generate the type signature. */
8227 generate_type_signature (c, type_node);
8229 /* Copy the declaration context, attributes, and children of the
8230 declaration into the new type unit DIE, then remove this DIE
8231 from the main CU (or replace it with a skeleton if necessary). */
8232 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8233 type_node->skeleton_die = replacement;
8235 /* Add the DIE to the new compunit. */
8236 add_child_die (unit, c);
8238 /* Types can reference DWARF procedures for type size or data location
8239 expressions. Calls in DWARF expressions cannot target procedures
8240 that are not in the same section. So we must copy DWARF procedures
8241 along with this type and then rewrite references to them. */
8242 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8243 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8245 if (replacement != NULL)
8246 c = replacement;
8248 else if (c->die_tag == DW_TAG_namespace
8249 || c->die_tag == DW_TAG_class_type
8250 || c->die_tag == DW_TAG_structure_type
8251 || c->die_tag == DW_TAG_union_type)
8253 /* Look for nested types that can be broken out. */
8254 break_out_comdat_types (c);
8256 } while (next != NULL);
8259 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8260 Enter all the cloned children into the hash table decl_table. */
8262 static dw_die_ref
8263 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8265 dw_die_ref c;
8266 dw_die_ref clone;
8267 struct decl_table_entry *entry;
8268 decl_table_entry **slot;
8270 if (die->die_tag == DW_TAG_subprogram)
8271 clone = clone_as_declaration (die);
8272 else
8273 clone = clone_die (die);
8275 slot = decl_table->find_slot_with_hash (die,
8276 htab_hash_pointer (die), INSERT);
8278 /* Assert that DIE isn't in the hash table yet. If it would be there
8279 before, the ancestors would be necessarily there as well, therefore
8280 clone_tree_partial wouldn't be called. */
8281 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8283 entry = XCNEW (struct decl_table_entry);
8284 entry->orig = die;
8285 entry->copy = clone;
8286 *slot = entry;
8288 if (die->die_tag != DW_TAG_subprogram)
8289 FOR_EACH_CHILD (die, c,
8290 add_child_die (clone, clone_tree_partial (c, decl_table)));
8292 return clone;
8295 /* Walk the DIE and its children, looking for references to incomplete
8296 or trivial types that are unmarked (i.e., that are not in the current
8297 type_unit). */
8299 static void
8300 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8302 dw_die_ref c;
8303 dw_attr_node *a;
8304 unsigned ix;
8306 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8308 if (AT_class (a) == dw_val_class_die_ref)
8310 dw_die_ref targ = AT_ref (a);
8311 decl_table_entry **slot;
8312 struct decl_table_entry *entry;
8314 if (targ->die_mark != 0 || targ->comdat_type_p)
8315 continue;
8317 slot = decl_table->find_slot_with_hash (targ,
8318 htab_hash_pointer (targ),
8319 INSERT);
8321 if (*slot != HTAB_EMPTY_ENTRY)
8323 /* TARG has already been copied, so we just need to
8324 modify the reference to point to the copy. */
8325 entry = *slot;
8326 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8328 else
8330 dw_die_ref parent = unit;
8331 dw_die_ref copy = clone_die (targ);
8333 /* Record in DECL_TABLE that TARG has been copied.
8334 Need to do this now, before the recursive call,
8335 because DECL_TABLE may be expanded and SLOT
8336 would no longer be a valid pointer. */
8337 entry = XCNEW (struct decl_table_entry);
8338 entry->orig = targ;
8339 entry->copy = copy;
8340 *slot = entry;
8342 /* If TARG is not a declaration DIE, we need to copy its
8343 children. */
8344 if (!is_declaration_die (targ))
8346 FOR_EACH_CHILD (
8347 targ, c,
8348 add_child_die (copy,
8349 clone_tree_partial (c, decl_table)));
8352 /* Make sure the cloned tree is marked as part of the
8353 type unit. */
8354 mark_dies (copy);
8356 /* If TARG has surrounding context, copy its ancestor tree
8357 into the new type unit. */
8358 if (targ->die_parent != NULL
8359 && !is_unit_die (targ->die_parent))
8360 parent = copy_ancestor_tree (unit, targ->die_parent,
8361 decl_table);
8363 add_child_die (parent, copy);
8364 a->dw_attr_val.v.val_die_ref.die = copy;
8366 /* Make sure the newly-copied DIE is walked. If it was
8367 installed in a previously-added context, it won't
8368 get visited otherwise. */
8369 if (parent != unit)
8371 /* Find the highest point of the newly-added tree,
8372 mark each node along the way, and walk from there. */
8373 parent->die_mark = 1;
8374 while (parent->die_parent
8375 && parent->die_parent->die_mark == 0)
8377 parent = parent->die_parent;
8378 parent->die_mark = 1;
8380 copy_decls_walk (unit, parent, decl_table);
8386 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8389 /* Copy declarations for "unworthy" types into the new comdat section.
8390 Incomplete types, modified types, and certain other types aren't broken
8391 out into comdat sections of their own, so they don't have a signature,
8392 and we need to copy the declaration into the same section so that we
8393 don't have an external reference. */
8395 static void
8396 copy_decls_for_unworthy_types (dw_die_ref unit)
8398 mark_dies (unit);
8399 decl_hash_type decl_table (10);
8400 copy_decls_walk (unit, unit, &decl_table);
8401 unmark_dies (unit);
8404 /* Traverse the DIE and add a sibling attribute if it may have the
8405 effect of speeding up access to siblings. To save some space,
8406 avoid generating sibling attributes for DIE's without children. */
8408 static void
8409 add_sibling_attributes (dw_die_ref die)
8411 dw_die_ref c;
8413 if (! die->die_child)
8414 return;
8416 if (die->die_parent && die != die->die_parent->die_child)
8417 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8419 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8422 /* Output all location lists for the DIE and its children. */
8424 static void
8425 output_location_lists (dw_die_ref die)
8427 dw_die_ref c;
8428 dw_attr_node *a;
8429 unsigned ix;
8431 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8432 if (AT_class (a) == dw_val_class_loc_list)
8433 output_loc_list (AT_loc_list (a));
8435 FOR_EACH_CHILD (die, c, output_location_lists (c));
8438 /* During assign_location_list_indexes and output_loclists_offset the
8439 current index, after it the number of assigned indexes (i.e. how
8440 large the .debug_loclists* offset table should be). */
8441 static unsigned int loc_list_idx;
8443 /* Output all location list offsets for the DIE and its children. */
8445 static void
8446 output_loclists_offsets (dw_die_ref die)
8448 dw_die_ref c;
8449 dw_attr_node *a;
8450 unsigned ix;
8452 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8453 if (AT_class (a) == dw_val_class_loc_list)
8455 dw_loc_list_ref l = AT_loc_list (a);
8456 if (l->offset_emitted)
8457 continue;
8458 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8459 loc_section_label, NULL);
8460 gcc_assert (l->hash == loc_list_idx);
8461 loc_list_idx++;
8462 l->offset_emitted = true;
8465 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8468 /* Recursively set indexes of location lists. */
8470 static void
8471 assign_location_list_indexes (dw_die_ref die)
8473 dw_die_ref c;
8474 dw_attr_node *a;
8475 unsigned ix;
8477 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8478 if (AT_class (a) == dw_val_class_loc_list)
8480 dw_loc_list_ref list = AT_loc_list (a);
8481 if (!list->num_assigned)
8483 list->num_assigned = true;
8484 list->hash = loc_list_idx++;
8488 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8491 /* We want to limit the number of external references, because they are
8492 larger than local references: a relocation takes multiple words, and
8493 even a sig8 reference is always eight bytes, whereas a local reference
8494 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8495 So if we encounter multiple external references to the same type DIE, we
8496 make a local typedef stub for it and redirect all references there.
8498 This is the element of the hash table for keeping track of these
8499 references. */
8501 struct external_ref
8503 dw_die_ref type;
8504 dw_die_ref stub;
8505 unsigned n_refs;
8508 /* Hashtable helpers. */
8510 struct external_ref_hasher : free_ptr_hash <external_ref>
8512 static inline hashval_t hash (const external_ref *);
8513 static inline bool equal (const external_ref *, const external_ref *);
8516 inline hashval_t
8517 external_ref_hasher::hash (const external_ref *r)
8519 dw_die_ref die = r->type;
8520 hashval_t h = 0;
8522 /* We can't use the address of the DIE for hashing, because
8523 that will make the order of the stub DIEs non-deterministic. */
8524 if (! die->comdat_type_p)
8525 /* We have a symbol; use it to compute a hash. */
8526 h = htab_hash_string (die->die_id.die_symbol);
8527 else
8529 /* We have a type signature; use a subset of the bits as the hash.
8530 The 8-byte signature is at least as large as hashval_t. */
8531 comdat_type_node *type_node = die->die_id.die_type_node;
8532 memcpy (&h, type_node->signature, sizeof (h));
8534 return h;
8537 inline bool
8538 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8540 return r1->type == r2->type;
8543 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8545 /* Return a pointer to the external_ref for references to DIE. */
8547 static struct external_ref *
8548 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8550 struct external_ref ref, *ref_p;
8551 external_ref **slot;
8553 ref.type = die;
8554 slot = map->find_slot (&ref, INSERT);
8555 if (*slot != HTAB_EMPTY_ENTRY)
8556 return *slot;
8558 ref_p = XCNEW (struct external_ref);
8559 ref_p->type = die;
8560 *slot = ref_p;
8561 return ref_p;
8564 /* Subroutine of optimize_external_refs, below.
8566 If we see a type skeleton, record it as our stub. If we see external
8567 references, remember how many we've seen. */
8569 static void
8570 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8572 dw_die_ref c;
8573 dw_attr_node *a;
8574 unsigned ix;
8575 struct external_ref *ref_p;
8577 if (is_type_die (die)
8578 && (c = get_AT_ref (die, DW_AT_signature)))
8580 /* This is a local skeleton; use it for local references. */
8581 ref_p = lookup_external_ref (map, c);
8582 ref_p->stub = die;
8585 /* Scan the DIE references, and remember any that refer to DIEs from
8586 other CUs (i.e. those which are not marked). */
8587 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8588 if (AT_class (a) == dw_val_class_die_ref
8589 && (c = AT_ref (a))->die_mark == 0
8590 && is_type_die (c))
8592 ref_p = lookup_external_ref (map, c);
8593 ref_p->n_refs++;
8596 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8599 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8600 points to an external_ref, DATA is the CU we're processing. If we don't
8601 already have a local stub, and we have multiple refs, build a stub. */
8604 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8606 struct external_ref *ref_p = *slot;
8608 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8610 /* We have multiple references to this type, so build a small stub.
8611 Both of these forms are a bit dodgy from the perspective of the
8612 DWARF standard, since technically they should have names. */
8613 dw_die_ref cu = data;
8614 dw_die_ref type = ref_p->type;
8615 dw_die_ref stub = NULL;
8617 if (type->comdat_type_p)
8619 /* If we refer to this type via sig8, use AT_signature. */
8620 stub = new_die (type->die_tag, cu, NULL_TREE);
8621 add_AT_die_ref (stub, DW_AT_signature, type);
8623 else
8625 /* Otherwise, use a typedef with no name. */
8626 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8627 add_AT_die_ref (stub, DW_AT_type, type);
8630 stub->die_mark++;
8631 ref_p->stub = stub;
8633 return 1;
8636 /* DIE is a unit; look through all the DIE references to see if there are
8637 any external references to types, and if so, create local stubs for
8638 them which will be applied in build_abbrev_table. This is useful because
8639 references to local DIEs are smaller. */
8641 static external_ref_hash_type *
8642 optimize_external_refs (dw_die_ref die)
8644 external_ref_hash_type *map = new external_ref_hash_type (10);
8645 optimize_external_refs_1 (die, map);
8646 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8647 return map;
8650 /* The following 3 variables are temporaries that are computed only during the
8651 build_abbrev_table call and used and released during the following
8652 optimize_abbrev_table call. */
8654 /* First abbrev_id that can be optimized based on usage. */
8655 static unsigned int abbrev_opt_start;
8657 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8658 abbrev_id smaller than this, because they must be already sized
8659 during build_abbrev_table). */
8660 static unsigned int abbrev_opt_base_type_end;
8662 /* Vector of usage counts during build_abbrev_table. Indexed by
8663 abbrev_id - abbrev_opt_start. */
8664 static vec<unsigned int> abbrev_usage_count;
8666 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8667 static vec<dw_die_ref> sorted_abbrev_dies;
8669 /* The format of each DIE (and its attribute value pairs) is encoded in an
8670 abbreviation table. This routine builds the abbreviation table and assigns
8671 a unique abbreviation id for each abbreviation entry. The children of each
8672 die are visited recursively. */
8674 static void
8675 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8677 unsigned int abbrev_id = 0;
8678 dw_die_ref c;
8679 dw_attr_node *a;
8680 unsigned ix;
8681 dw_die_ref abbrev;
8683 /* Scan the DIE references, and replace any that refer to
8684 DIEs from other CUs (i.e. those which are not marked) with
8685 the local stubs we built in optimize_external_refs. */
8686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8687 if (AT_class (a) == dw_val_class_die_ref
8688 && (c = AT_ref (a))->die_mark == 0)
8690 struct external_ref *ref_p;
8691 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8693 ref_p = lookup_external_ref (extern_map, c);
8694 if (ref_p->stub && ref_p->stub != die)
8695 change_AT_die_ref (a, ref_p->stub);
8696 else
8697 /* We aren't changing this reference, so mark it external. */
8698 set_AT_ref_external (a, 1);
8701 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8703 dw_attr_node *die_a, *abbrev_a;
8704 unsigned ix;
8705 bool ok = true;
8707 if (abbrev_id == 0)
8708 continue;
8709 if (abbrev->die_tag != die->die_tag)
8710 continue;
8711 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8712 continue;
8714 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8715 continue;
8717 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8719 abbrev_a = &(*abbrev->die_attr)[ix];
8720 if ((abbrev_a->dw_attr != die_a->dw_attr)
8721 || (value_format (abbrev_a) != value_format (die_a)))
8723 ok = false;
8724 break;
8727 if (ok)
8728 break;
8731 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8733 vec_safe_push (abbrev_die_table, die);
8734 if (abbrev_opt_start)
8735 abbrev_usage_count.safe_push (0);
8737 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8739 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8740 sorted_abbrev_dies.safe_push (die);
8743 die->die_abbrev = abbrev_id;
8744 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8747 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8748 by die_abbrev's usage count, from the most commonly used
8749 abbreviation to the least. */
8751 static int
8752 die_abbrev_cmp (const void *p1, const void *p2)
8754 dw_die_ref die1 = *(const dw_die_ref *) p1;
8755 dw_die_ref die2 = *(const dw_die_ref *) p2;
8757 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8758 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8760 if (die1->die_abbrev >= abbrev_opt_base_type_end
8761 && die2->die_abbrev >= abbrev_opt_base_type_end)
8763 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8764 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8765 return -1;
8766 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8767 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8768 return 1;
8771 /* Stabilize the sort. */
8772 if (die1->die_abbrev < die2->die_abbrev)
8773 return -1;
8774 if (die1->die_abbrev > die2->die_abbrev)
8775 return 1;
8777 return 0;
8780 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8781 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8782 into dw_val_class_const_implicit or
8783 dw_val_class_unsigned_const_implicit. */
8785 static void
8786 optimize_implicit_const (unsigned int first_id, unsigned int end,
8787 vec<bool> &implicit_consts)
8789 /* It never makes sense if there is just one DIE using the abbreviation. */
8790 if (end < first_id + 2)
8791 return;
8793 dw_attr_node *a;
8794 unsigned ix, i;
8795 dw_die_ref die = sorted_abbrev_dies[first_id];
8796 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8797 if (implicit_consts[ix])
8799 enum dw_val_class new_class = dw_val_class_none;
8800 switch (AT_class (a))
8802 case dw_val_class_unsigned_const:
8803 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8804 continue;
8806 /* The .debug_abbrev section will grow by
8807 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8808 in all the DIEs using that abbreviation. */
8809 if (constant_size (AT_unsigned (a)) * (end - first_id)
8810 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8811 continue;
8813 new_class = dw_val_class_unsigned_const_implicit;
8814 break;
8816 case dw_val_class_const:
8817 new_class = dw_val_class_const_implicit;
8818 break;
8820 case dw_val_class_file:
8821 new_class = dw_val_class_file_implicit;
8822 break;
8824 default:
8825 continue;
8827 for (i = first_id; i < end; i++)
8828 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8829 = new_class;
8833 /* Attempt to optimize abbreviation table from abbrev_opt_start
8834 abbreviation above. */
8836 static void
8837 optimize_abbrev_table (void)
8839 if (abbrev_opt_start
8840 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8841 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8843 auto_vec<bool, 32> implicit_consts;
8844 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8846 unsigned int abbrev_id = abbrev_opt_start - 1;
8847 unsigned int first_id = ~0U;
8848 unsigned int last_abbrev_id = 0;
8849 unsigned int i;
8850 dw_die_ref die;
8851 if (abbrev_opt_base_type_end > abbrev_opt_start)
8852 abbrev_id = abbrev_opt_base_type_end - 1;
8853 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8854 most commonly used abbreviations come first. */
8855 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8857 dw_attr_node *a;
8858 unsigned ix;
8860 /* If calc_base_type_die_sizes has been called, the CU and
8861 base types after it can't be optimized, because we've already
8862 calculated their DIE offsets. We've sorted them first. */
8863 if (die->die_abbrev < abbrev_opt_base_type_end)
8864 continue;
8865 if (die->die_abbrev != last_abbrev_id)
8867 last_abbrev_id = die->die_abbrev;
8868 if (dwarf_version >= 5 && first_id != ~0U)
8869 optimize_implicit_const (first_id, i, implicit_consts);
8870 abbrev_id++;
8871 (*abbrev_die_table)[abbrev_id] = die;
8872 if (dwarf_version >= 5)
8874 first_id = i;
8875 implicit_consts.truncate (0);
8877 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8878 switch (AT_class (a))
8880 case dw_val_class_const:
8881 case dw_val_class_unsigned_const:
8882 case dw_val_class_file:
8883 implicit_consts.safe_push (true);
8884 break;
8885 default:
8886 implicit_consts.safe_push (false);
8887 break;
8891 else if (dwarf_version >= 5)
8893 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8894 if (!implicit_consts[ix])
8895 continue;
8896 else
8898 dw_attr_node *other_a
8899 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8900 if (!dw_val_equal_p (&a->dw_attr_val,
8901 &other_a->dw_attr_val))
8902 implicit_consts[ix] = false;
8905 die->die_abbrev = abbrev_id;
8907 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8908 if (dwarf_version >= 5 && first_id != ~0U)
8909 optimize_implicit_const (first_id, i, implicit_consts);
8912 abbrev_opt_start = 0;
8913 abbrev_opt_base_type_end = 0;
8914 abbrev_usage_count.release ();
8915 sorted_abbrev_dies.release ();
8918 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8920 static int
8921 constant_size (unsigned HOST_WIDE_INT value)
8923 int log;
8925 if (value == 0)
8926 log = 0;
8927 else
8928 log = floor_log2 (value);
8930 log = log / 8;
8931 log = 1 << (floor_log2 (log) + 1);
8933 return log;
8936 /* Return the size of a DIE as it is represented in the
8937 .debug_info section. */
8939 static unsigned long
8940 size_of_die (dw_die_ref die)
8942 unsigned long size = 0;
8943 dw_attr_node *a;
8944 unsigned ix;
8945 enum dwarf_form form;
8947 size += size_of_uleb128 (die->die_abbrev);
8948 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8950 switch (AT_class (a))
8952 case dw_val_class_addr:
8953 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8955 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8956 size += size_of_uleb128 (AT_index (a));
8958 else
8959 size += DWARF2_ADDR_SIZE;
8960 break;
8961 case dw_val_class_offset:
8962 size += DWARF_OFFSET_SIZE;
8963 break;
8964 case dw_val_class_loc:
8966 unsigned long lsize = size_of_locs (AT_loc (a));
8968 /* Block length. */
8969 if (dwarf_version >= 4)
8970 size += size_of_uleb128 (lsize);
8971 else
8972 size += constant_size (lsize);
8973 size += lsize;
8975 break;
8976 case dw_val_class_loc_list:
8977 if (dwarf_split_debug_info && dwarf_version >= 5)
8979 gcc_assert (AT_loc_list (a)->num_assigned);
8980 size += size_of_uleb128 (AT_loc_list (a)->hash);
8982 else
8983 size += DWARF_OFFSET_SIZE;
8984 break;
8985 case dw_val_class_range_list:
8986 if (value_format (a) == DW_FORM_rnglistx)
8988 gcc_assert (rnglist_idx);
8989 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
8990 size += size_of_uleb128 (r->idx);
8992 else
8993 size += DWARF_OFFSET_SIZE;
8994 break;
8995 case dw_val_class_const:
8996 size += size_of_sleb128 (AT_int (a));
8997 break;
8998 case dw_val_class_unsigned_const:
9000 int csize = constant_size (AT_unsigned (a));
9001 if (dwarf_version == 3
9002 && a->dw_attr == DW_AT_data_member_location
9003 && csize >= 4)
9004 size += size_of_uleb128 (AT_unsigned (a));
9005 else
9006 size += csize;
9008 break;
9009 case dw_val_class_const_implicit:
9010 case dw_val_class_unsigned_const_implicit:
9011 case dw_val_class_file_implicit:
9012 /* These occupy no size in the DIE, just an extra sleb128 in
9013 .debug_abbrev. */
9014 break;
9015 case dw_val_class_const_double:
9016 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9017 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9018 size++; /* block */
9019 break;
9020 case dw_val_class_wide_int:
9021 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9022 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9023 if (get_full_len (*a->dw_attr_val.v.val_wide)
9024 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9025 size++; /* block */
9026 break;
9027 case dw_val_class_vec:
9028 size += constant_size (a->dw_attr_val.v.val_vec.length
9029 * a->dw_attr_val.v.val_vec.elt_size)
9030 + a->dw_attr_val.v.val_vec.length
9031 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9032 break;
9033 case dw_val_class_flag:
9034 if (dwarf_version >= 4)
9035 /* Currently all add_AT_flag calls pass in 1 as last argument,
9036 so DW_FORM_flag_present can be used. If that ever changes,
9037 we'll need to use DW_FORM_flag and have some optimization
9038 in build_abbrev_table that will change those to
9039 DW_FORM_flag_present if it is set to 1 in all DIEs using
9040 the same abbrev entry. */
9041 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9042 else
9043 size += 1;
9044 break;
9045 case dw_val_class_die_ref:
9046 if (AT_ref_external (a))
9048 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9049 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9050 is sized by target address length, whereas in DWARF3
9051 it's always sized as an offset. */
9052 if (use_debug_types)
9053 size += DWARF_TYPE_SIGNATURE_SIZE;
9054 else if (dwarf_version == 2)
9055 size += DWARF2_ADDR_SIZE;
9056 else
9057 size += DWARF_OFFSET_SIZE;
9059 else
9060 size += DWARF_OFFSET_SIZE;
9061 break;
9062 case dw_val_class_fde_ref:
9063 size += DWARF_OFFSET_SIZE;
9064 break;
9065 case dw_val_class_lbl_id:
9066 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9068 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9069 size += size_of_uleb128 (AT_index (a));
9071 else
9072 size += DWARF2_ADDR_SIZE;
9073 break;
9074 case dw_val_class_lineptr:
9075 case dw_val_class_macptr:
9076 case dw_val_class_loclistsptr:
9077 size += DWARF_OFFSET_SIZE;
9078 break;
9079 case dw_val_class_str:
9080 form = AT_string_form (a);
9081 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9082 size += DWARF_OFFSET_SIZE;
9083 else if (form == DW_FORM_GNU_str_index)
9084 size += size_of_uleb128 (AT_index (a));
9085 else
9086 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9087 break;
9088 case dw_val_class_file:
9089 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9090 break;
9091 case dw_val_class_data8:
9092 size += 8;
9093 break;
9094 case dw_val_class_vms_delta:
9095 size += DWARF_OFFSET_SIZE;
9096 break;
9097 case dw_val_class_high_pc:
9098 size += DWARF2_ADDR_SIZE;
9099 break;
9100 case dw_val_class_discr_value:
9101 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9102 break;
9103 case dw_val_class_discr_list:
9105 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9107 /* This is a block, so we have the block length and then its
9108 data. */
9109 size += constant_size (block_size) + block_size;
9111 break;
9112 default:
9113 gcc_unreachable ();
9117 return size;
9120 /* Size the debugging information associated with a given DIE. Visits the
9121 DIE's children recursively. Updates the global variable next_die_offset, on
9122 each time through. Uses the current value of next_die_offset to update the
9123 die_offset field in each DIE. */
9125 static void
9126 calc_die_sizes (dw_die_ref die)
9128 dw_die_ref c;
9130 gcc_assert (die->die_offset == 0
9131 || (unsigned long int) die->die_offset == next_die_offset);
9132 die->die_offset = next_die_offset;
9133 next_die_offset += size_of_die (die);
9135 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9137 if (die->die_child != NULL)
9138 /* Count the null byte used to terminate sibling lists. */
9139 next_die_offset += 1;
9142 /* Size just the base type children at the start of the CU.
9143 This is needed because build_abbrev needs to size locs
9144 and sizing of type based stack ops needs to know die_offset
9145 values for the base types. */
9147 static void
9148 calc_base_type_die_sizes (void)
9150 unsigned long die_offset = (dwarf_split_debug_info
9151 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9152 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9153 unsigned int i;
9154 dw_die_ref base_type;
9155 #if ENABLE_ASSERT_CHECKING
9156 dw_die_ref prev = comp_unit_die ()->die_child;
9157 #endif
9159 die_offset += size_of_die (comp_unit_die ());
9160 for (i = 0; base_types.iterate (i, &base_type); i++)
9162 #if ENABLE_ASSERT_CHECKING
9163 gcc_assert (base_type->die_offset == 0
9164 && prev->die_sib == base_type
9165 && base_type->die_child == NULL
9166 && base_type->die_abbrev);
9167 prev = base_type;
9168 #endif
9169 if (abbrev_opt_start
9170 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9171 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9172 base_type->die_offset = die_offset;
9173 die_offset += size_of_die (base_type);
9177 /* Set the marks for a die and its children. We do this so
9178 that we know whether or not a reference needs to use FORM_ref_addr; only
9179 DIEs in the same CU will be marked. We used to clear out the offset
9180 and use that as the flag, but ran into ordering problems. */
9182 static void
9183 mark_dies (dw_die_ref die)
9185 dw_die_ref c;
9187 gcc_assert (!die->die_mark);
9189 die->die_mark = 1;
9190 FOR_EACH_CHILD (die, c, mark_dies (c));
9193 /* Clear the marks for a die and its children. */
9195 static void
9196 unmark_dies (dw_die_ref die)
9198 dw_die_ref c;
9200 if (! use_debug_types)
9201 gcc_assert (die->die_mark);
9203 die->die_mark = 0;
9204 FOR_EACH_CHILD (die, c, unmark_dies (c));
9207 /* Clear the marks for a die, its children and referred dies. */
9209 static void
9210 unmark_all_dies (dw_die_ref die)
9212 dw_die_ref c;
9213 dw_attr_node *a;
9214 unsigned ix;
9216 if (!die->die_mark)
9217 return;
9218 die->die_mark = 0;
9220 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9222 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9223 if (AT_class (a) == dw_val_class_die_ref)
9224 unmark_all_dies (AT_ref (a));
9227 /* Calculate if the entry should appear in the final output file. It may be
9228 from a pruned a type. */
9230 static bool
9231 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9233 /* By limiting gnu pubnames to definitions only, gold can generate a
9234 gdb index without entries for declarations, which don't include
9235 enough information to be useful. */
9236 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9237 return false;
9239 if (table == pubname_table)
9241 /* Enumerator names are part of the pubname table, but the
9242 parent DW_TAG_enumeration_type die may have been pruned.
9243 Don't output them if that is the case. */
9244 if (p->die->die_tag == DW_TAG_enumerator &&
9245 (p->die->die_parent == NULL
9246 || !p->die->die_parent->die_perennial_p))
9247 return false;
9249 /* Everything else in the pubname table is included. */
9250 return true;
9253 /* The pubtypes table shouldn't include types that have been
9254 pruned. */
9255 return (p->die->die_offset != 0
9256 || !flag_eliminate_unused_debug_types);
9259 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9260 generated for the compilation unit. */
9262 static unsigned long
9263 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9265 unsigned long size;
9266 unsigned i;
9267 pubname_entry *p;
9268 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9270 size = DWARF_PUBNAMES_HEADER_SIZE;
9271 FOR_EACH_VEC_ELT (*names, i, p)
9272 if (include_pubname_in_output (names, p))
9273 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9275 size += DWARF_OFFSET_SIZE;
9276 return size;
9279 /* Return the size of the information in the .debug_aranges section. */
9281 static unsigned long
9282 size_of_aranges (void)
9284 unsigned long size;
9286 size = DWARF_ARANGES_HEADER_SIZE;
9288 /* Count the address/length pair for this compilation unit. */
9289 if (text_section_used)
9290 size += 2 * DWARF2_ADDR_SIZE;
9291 if (cold_text_section_used)
9292 size += 2 * DWARF2_ADDR_SIZE;
9293 if (have_multiple_function_sections)
9295 unsigned fde_idx;
9296 dw_fde_ref fde;
9298 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9300 if (DECL_IGNORED_P (fde->decl))
9301 continue;
9302 if (!fde->in_std_section)
9303 size += 2 * DWARF2_ADDR_SIZE;
9304 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9305 size += 2 * DWARF2_ADDR_SIZE;
9309 /* Count the two zero words used to terminated the address range table. */
9310 size += 2 * DWARF2_ADDR_SIZE;
9311 return size;
9314 /* Select the encoding of an attribute value. */
9316 static enum dwarf_form
9317 value_format (dw_attr_node *a)
9319 switch (AT_class (a))
9321 case dw_val_class_addr:
9322 /* Only very few attributes allow DW_FORM_addr. */
9323 switch (a->dw_attr)
9325 case DW_AT_low_pc:
9326 case DW_AT_high_pc:
9327 case DW_AT_entry_pc:
9328 case DW_AT_trampoline:
9329 return (AT_index (a) == NOT_INDEXED
9330 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9331 default:
9332 break;
9334 switch (DWARF2_ADDR_SIZE)
9336 case 1:
9337 return DW_FORM_data1;
9338 case 2:
9339 return DW_FORM_data2;
9340 case 4:
9341 return DW_FORM_data4;
9342 case 8:
9343 return DW_FORM_data8;
9344 default:
9345 gcc_unreachable ();
9347 case dw_val_class_loc_list:
9348 if (dwarf_split_debug_info
9349 && dwarf_version >= 5
9350 && AT_loc_list (a)->num_assigned)
9351 return DW_FORM_loclistx;
9352 /* FALLTHRU */
9353 case dw_val_class_range_list:
9354 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9355 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9356 care about sizes of .debug* sections in shared libraries and
9357 executables and don't take into account relocations that affect just
9358 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9359 table in the .debug_rnglists section. */
9360 if (dwarf_split_debug_info
9361 && dwarf_version >= 5
9362 && AT_class (a) == dw_val_class_range_list
9363 && rnglist_idx
9364 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9365 return DW_FORM_rnglistx;
9366 if (dwarf_version >= 4)
9367 return DW_FORM_sec_offset;
9368 /* FALLTHRU */
9369 case dw_val_class_vms_delta:
9370 case dw_val_class_offset:
9371 switch (DWARF_OFFSET_SIZE)
9373 case 4:
9374 return DW_FORM_data4;
9375 case 8:
9376 return DW_FORM_data8;
9377 default:
9378 gcc_unreachable ();
9380 case dw_val_class_loc:
9381 if (dwarf_version >= 4)
9382 return DW_FORM_exprloc;
9383 switch (constant_size (size_of_locs (AT_loc (a))))
9385 case 1:
9386 return DW_FORM_block1;
9387 case 2:
9388 return DW_FORM_block2;
9389 case 4:
9390 return DW_FORM_block4;
9391 default:
9392 gcc_unreachable ();
9394 case dw_val_class_const:
9395 return DW_FORM_sdata;
9396 case dw_val_class_unsigned_const:
9397 switch (constant_size (AT_unsigned (a)))
9399 case 1:
9400 return DW_FORM_data1;
9401 case 2:
9402 return DW_FORM_data2;
9403 case 4:
9404 /* In DWARF3 DW_AT_data_member_location with
9405 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9406 constant, so we need to use DW_FORM_udata if we need
9407 a large constant. */
9408 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9409 return DW_FORM_udata;
9410 return DW_FORM_data4;
9411 case 8:
9412 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9413 return DW_FORM_udata;
9414 return DW_FORM_data8;
9415 default:
9416 gcc_unreachable ();
9418 case dw_val_class_const_implicit:
9419 case dw_val_class_unsigned_const_implicit:
9420 case dw_val_class_file_implicit:
9421 return DW_FORM_implicit_const;
9422 case dw_val_class_const_double:
9423 switch (HOST_BITS_PER_WIDE_INT)
9425 case 8:
9426 return DW_FORM_data2;
9427 case 16:
9428 return DW_FORM_data4;
9429 case 32:
9430 return DW_FORM_data8;
9431 case 64:
9432 if (dwarf_version >= 5)
9433 return DW_FORM_data16;
9434 /* FALLTHRU */
9435 default:
9436 return DW_FORM_block1;
9438 case dw_val_class_wide_int:
9439 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9441 case 8:
9442 return DW_FORM_data1;
9443 case 16:
9444 return DW_FORM_data2;
9445 case 32:
9446 return DW_FORM_data4;
9447 case 64:
9448 return DW_FORM_data8;
9449 case 128:
9450 if (dwarf_version >= 5)
9451 return DW_FORM_data16;
9452 /* FALLTHRU */
9453 default:
9454 return DW_FORM_block1;
9456 case dw_val_class_vec:
9457 switch (constant_size (a->dw_attr_val.v.val_vec.length
9458 * a->dw_attr_val.v.val_vec.elt_size))
9460 case 1:
9461 return DW_FORM_block1;
9462 case 2:
9463 return DW_FORM_block2;
9464 case 4:
9465 return DW_FORM_block4;
9466 default:
9467 gcc_unreachable ();
9469 case dw_val_class_flag:
9470 if (dwarf_version >= 4)
9472 /* Currently all add_AT_flag calls pass in 1 as last argument,
9473 so DW_FORM_flag_present can be used. If that ever changes,
9474 we'll need to use DW_FORM_flag and have some optimization
9475 in build_abbrev_table that will change those to
9476 DW_FORM_flag_present if it is set to 1 in all DIEs using
9477 the same abbrev entry. */
9478 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9479 return DW_FORM_flag_present;
9481 return DW_FORM_flag;
9482 case dw_val_class_die_ref:
9483 if (AT_ref_external (a))
9484 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9485 else
9486 return DW_FORM_ref;
9487 case dw_val_class_fde_ref:
9488 return DW_FORM_data;
9489 case dw_val_class_lbl_id:
9490 return (AT_index (a) == NOT_INDEXED
9491 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9492 case dw_val_class_lineptr:
9493 case dw_val_class_macptr:
9494 case dw_val_class_loclistsptr:
9495 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9496 case dw_val_class_str:
9497 return AT_string_form (a);
9498 case dw_val_class_file:
9499 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9501 case 1:
9502 return DW_FORM_data1;
9503 case 2:
9504 return DW_FORM_data2;
9505 case 4:
9506 return DW_FORM_data4;
9507 default:
9508 gcc_unreachable ();
9511 case dw_val_class_data8:
9512 return DW_FORM_data8;
9514 case dw_val_class_high_pc:
9515 switch (DWARF2_ADDR_SIZE)
9517 case 1:
9518 return DW_FORM_data1;
9519 case 2:
9520 return DW_FORM_data2;
9521 case 4:
9522 return DW_FORM_data4;
9523 case 8:
9524 return DW_FORM_data8;
9525 default:
9526 gcc_unreachable ();
9529 case dw_val_class_discr_value:
9530 return (a->dw_attr_val.v.val_discr_value.pos
9531 ? DW_FORM_udata
9532 : DW_FORM_sdata);
9533 case dw_val_class_discr_list:
9534 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9536 case 1:
9537 return DW_FORM_block1;
9538 case 2:
9539 return DW_FORM_block2;
9540 case 4:
9541 return DW_FORM_block4;
9542 default:
9543 gcc_unreachable ();
9546 default:
9547 gcc_unreachable ();
9551 /* Output the encoding of an attribute value. */
9553 static void
9554 output_value_format (dw_attr_node *a)
9556 enum dwarf_form form = value_format (a);
9558 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9561 /* Given a die and id, produce the appropriate abbreviations. */
9563 static void
9564 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9566 unsigned ix;
9567 dw_attr_node *a_attr;
9569 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9570 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9571 dwarf_tag_name (abbrev->die_tag));
9573 if (abbrev->die_child != NULL)
9574 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9575 else
9576 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9578 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9580 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9581 dwarf_attr_name (a_attr->dw_attr));
9582 output_value_format (a_attr);
9583 if (value_format (a_attr) == DW_FORM_implicit_const)
9585 if (AT_class (a_attr) == dw_val_class_file_implicit)
9587 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9588 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9589 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9591 else
9592 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9596 dw2_asm_output_data (1, 0, NULL);
9597 dw2_asm_output_data (1, 0, NULL);
9601 /* Output the .debug_abbrev section which defines the DIE abbreviation
9602 table. */
9604 static void
9605 output_abbrev_section (void)
9607 unsigned int abbrev_id;
9608 dw_die_ref abbrev;
9610 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9611 if (abbrev_id != 0)
9612 output_die_abbrevs (abbrev_id, abbrev);
9614 /* Terminate the table. */
9615 dw2_asm_output_data (1, 0, NULL);
9618 /* Output a symbol we can use to refer to this DIE from another CU. */
9620 static inline void
9621 output_die_symbol (dw_die_ref die)
9623 const char *sym = die->die_id.die_symbol;
9625 gcc_assert (!die->comdat_type_p);
9627 if (sym == 0)
9628 return;
9630 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9631 /* We make these global, not weak; if the target doesn't support
9632 .linkonce, it doesn't support combining the sections, so debugging
9633 will break. */
9634 targetm.asm_out.globalize_label (asm_out_file, sym);
9636 ASM_OUTPUT_LABEL (asm_out_file, sym);
9639 /* Return a new location list, given the begin and end range, and the
9640 expression. */
9642 static inline dw_loc_list_ref
9643 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9644 const char *section)
9646 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9648 retlist->begin = begin;
9649 retlist->begin_entry = NULL;
9650 retlist->end = end;
9651 retlist->expr = expr;
9652 retlist->section = section;
9654 return retlist;
9657 /* Generate a new internal symbol for this location list node, if it
9658 hasn't got one yet. */
9660 static inline void
9661 gen_llsym (dw_loc_list_ref list)
9663 gcc_assert (!list->ll_symbol);
9664 list->ll_symbol = gen_internal_sym ("LLST");
9667 /* Output the location list given to us. */
9669 static void
9670 output_loc_list (dw_loc_list_ref list_head)
9672 if (list_head->emitted)
9673 return;
9674 list_head->emitted = true;
9676 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9678 dw_loc_list_ref curr = list_head;
9679 const char *last_section = NULL;
9680 const char *base_label = NULL;
9682 /* Walk the location list, and output each range + expression. */
9683 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9685 unsigned long size;
9686 /* Don't output an entry that starts and ends at the same address. */
9687 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9688 continue;
9689 size = size_of_locs (curr->expr);
9690 /* If the expression is too large, drop it on the floor. We could
9691 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9692 in the expression, but >= 64KB expressions for a single value
9693 in a single range are unlikely very useful. */
9694 if (dwarf_version < 5 && size > 0xffff)
9695 continue;
9696 if (dwarf_version >= 5)
9698 if (dwarf_split_debug_info)
9700 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9701 uleb128 index into .debug_addr and uleb128 length. */
9702 dw2_asm_output_data (1, DW_LLE_startx_length,
9703 "DW_LLE_startx_length (%s)",
9704 list_head->ll_symbol);
9705 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9706 "Location list range start index "
9707 "(%s)", curr->begin);
9708 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9709 For that case we probably need to emit DW_LLE_startx_endx,
9710 but we'd need 2 .debug_addr entries rather than just one. */
9711 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9712 "Location list length (%s)",
9713 list_head->ll_symbol);
9715 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9717 /* If all code is in .text section, the base address is
9718 already provided by the CU attributes. Use
9719 DW_LLE_offset_pair where both addresses are uleb128 encoded
9720 offsets against that base. */
9721 dw2_asm_output_data (1, DW_LLE_offset_pair,
9722 "DW_LLE_offset_pair (%s)",
9723 list_head->ll_symbol);
9724 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9725 "Location list begin address (%s)",
9726 list_head->ll_symbol);
9727 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9728 "Location list end address (%s)",
9729 list_head->ll_symbol);
9731 else if (HAVE_AS_LEB128)
9733 /* Otherwise, find out how many consecutive entries could share
9734 the same base entry. If just one, emit DW_LLE_start_length,
9735 otherwise emit DW_LLE_base_address for the base address
9736 followed by a series of DW_LLE_offset_pair. */
9737 if (last_section == NULL || curr->section != last_section)
9739 dw_loc_list_ref curr2;
9740 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9741 curr2 = curr2->dw_loc_next)
9743 if (strcmp (curr2->begin, curr2->end) == 0
9744 && !curr2->force)
9745 continue;
9746 break;
9748 if (curr2 == NULL || curr->section != curr2->section)
9749 last_section = NULL;
9750 else
9752 last_section = curr->section;
9753 base_label = curr->begin;
9754 dw2_asm_output_data (1, DW_LLE_base_address,
9755 "DW_LLE_base_address (%s)",
9756 list_head->ll_symbol);
9757 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9758 "Base address (%s)",
9759 list_head->ll_symbol);
9762 /* Only one entry with the same base address. Use
9763 DW_LLE_start_length with absolute address and uleb128
9764 length. */
9765 if (last_section == NULL)
9767 dw2_asm_output_data (1, DW_LLE_start_length,
9768 "DW_LLE_start_length (%s)",
9769 list_head->ll_symbol);
9770 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9771 "Location list begin address (%s)",
9772 list_head->ll_symbol);
9773 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9774 "Location list length "
9775 "(%s)", list_head->ll_symbol);
9777 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9778 DW_LLE_base_address. */
9779 else
9781 dw2_asm_output_data (1, DW_LLE_offset_pair,
9782 "DW_LLE_offset_pair (%s)",
9783 list_head->ll_symbol);
9784 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9785 "Location list begin address "
9786 "(%s)", list_head->ll_symbol);
9787 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9788 "Location list end address "
9789 "(%s)", list_head->ll_symbol);
9792 /* The assembler does not support .uleb128 directive. Emit
9793 DW_LLE_start_end with a pair of absolute addresses. */
9794 else
9796 dw2_asm_output_data (1, DW_LLE_start_end,
9797 "DW_LLE_start_end (%s)",
9798 list_head->ll_symbol);
9799 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9800 "Location list begin address (%s)",
9801 list_head->ll_symbol);
9802 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9803 "Location list end address (%s)",
9804 list_head->ll_symbol);
9807 else if (dwarf_split_debug_info)
9809 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9810 and 4 byte length. */
9811 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9812 "Location list start/length entry (%s)",
9813 list_head->ll_symbol);
9814 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9815 "Location list range start index (%s)",
9816 curr->begin);
9817 /* The length field is 4 bytes. If we ever need to support
9818 an 8-byte length, we can add a new DW_LLE code or fall back
9819 to DW_LLE_GNU_start_end_entry. */
9820 dw2_asm_output_delta (4, curr->end, curr->begin,
9821 "Location list range length (%s)",
9822 list_head->ll_symbol);
9824 else if (!have_multiple_function_sections)
9826 /* Pair of relative addresses against start of text section. */
9827 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9828 "Location list begin address (%s)",
9829 list_head->ll_symbol);
9830 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9831 "Location list end address (%s)",
9832 list_head->ll_symbol);
9834 else
9836 /* Pair of absolute addresses. */
9837 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9838 "Location list begin address (%s)",
9839 list_head->ll_symbol);
9840 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9841 "Location list end address (%s)",
9842 list_head->ll_symbol);
9845 /* Output the block length for this list of location operations. */
9846 if (dwarf_version >= 5)
9847 dw2_asm_output_data_uleb128 (size, "Location expression size");
9848 else
9850 gcc_assert (size <= 0xffff);
9851 dw2_asm_output_data (2, size, "Location expression size");
9854 output_loc_sequence (curr->expr, -1);
9857 /* And finally list termination. */
9858 if (dwarf_version >= 5)
9859 dw2_asm_output_data (1, DW_LLE_end_of_list,
9860 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9861 else if (dwarf_split_debug_info)
9862 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9863 "Location list terminator (%s)",
9864 list_head->ll_symbol);
9865 else
9867 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9868 "Location list terminator begin (%s)",
9869 list_head->ll_symbol);
9870 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9871 "Location list terminator end (%s)",
9872 list_head->ll_symbol);
9876 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9877 section. Emit a relocated reference if val_entry is NULL, otherwise,
9878 emit an indirect reference. */
9880 static void
9881 output_range_list_offset (dw_attr_node *a)
9883 const char *name = dwarf_attr_name (a->dw_attr);
9885 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9887 if (dwarf_version >= 5)
9889 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9890 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9891 debug_ranges_section, "%s", name);
9893 else
9895 char *p = strchr (ranges_section_label, '\0');
9896 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9897 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9898 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9899 debug_ranges_section, "%s", name);
9900 *p = '\0';
9903 else if (dwarf_version >= 5)
9905 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9906 gcc_assert (rnglist_idx);
9907 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9909 else
9910 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9911 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9912 "%s (offset from %s)", name, ranges_section_label);
9915 /* Output the offset into the debug_loc section. */
9917 static void
9918 output_loc_list_offset (dw_attr_node *a)
9920 char *sym = AT_loc_list (a)->ll_symbol;
9922 gcc_assert (sym);
9923 if (!dwarf_split_debug_info)
9924 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9925 "%s", dwarf_attr_name (a->dw_attr));
9926 else if (dwarf_version >= 5)
9928 gcc_assert (AT_loc_list (a)->num_assigned);
9929 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9930 dwarf_attr_name (a->dw_attr),
9931 sym);
9933 else
9934 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9935 "%s", dwarf_attr_name (a->dw_attr));
9938 /* Output an attribute's index or value appropriately. */
9940 static void
9941 output_attr_index_or_value (dw_attr_node *a)
9943 const char *name = dwarf_attr_name (a->dw_attr);
9945 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9947 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9948 return;
9950 switch (AT_class (a))
9952 case dw_val_class_addr:
9953 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9954 break;
9955 case dw_val_class_high_pc:
9956 case dw_val_class_lbl_id:
9957 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9958 break;
9959 default:
9960 gcc_unreachable ();
9964 /* Output a type signature. */
9966 static inline void
9967 output_signature (const char *sig, const char *name)
9969 int i;
9971 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9972 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9975 /* Output a discriminant value. */
9977 static inline void
9978 output_discr_value (dw_discr_value *discr_value, const char *name)
9980 if (discr_value->pos)
9981 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9982 else
9983 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9986 /* Output the DIE and its attributes. Called recursively to generate
9987 the definitions of each child DIE. */
9989 static void
9990 output_die (dw_die_ref die)
9992 dw_attr_node *a;
9993 dw_die_ref c;
9994 unsigned long size;
9995 unsigned ix;
9997 /* If someone in another CU might refer to us, set up a symbol for
9998 them to point to. */
9999 if (! die->comdat_type_p && die->die_id.die_symbol
10000 /* Don't output the symbol twice. For LTO we want the label
10001 on the section beginning, not on the actual DIE. */
10002 && ((!flag_generate_lto && !flag_generate_offload)
10003 || die->die_tag != DW_TAG_compile_unit))
10004 output_die_symbol (die);
10006 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10007 (unsigned long)die->die_offset,
10008 dwarf_tag_name (die->die_tag));
10010 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10012 const char *name = dwarf_attr_name (a->dw_attr);
10014 switch (AT_class (a))
10016 case dw_val_class_addr:
10017 output_attr_index_or_value (a);
10018 break;
10020 case dw_val_class_offset:
10021 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10022 "%s", name);
10023 break;
10025 case dw_val_class_range_list:
10026 output_range_list_offset (a);
10027 break;
10029 case dw_val_class_loc:
10030 size = size_of_locs (AT_loc (a));
10032 /* Output the block length for this list of location operations. */
10033 if (dwarf_version >= 4)
10034 dw2_asm_output_data_uleb128 (size, "%s", name);
10035 else
10036 dw2_asm_output_data (constant_size (size), size, "%s", name);
10038 output_loc_sequence (AT_loc (a), -1);
10039 break;
10041 case dw_val_class_const:
10042 /* ??? It would be slightly more efficient to use a scheme like is
10043 used for unsigned constants below, but gdb 4.x does not sign
10044 extend. Gdb 5.x does sign extend. */
10045 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10046 break;
10048 case dw_val_class_unsigned_const:
10050 int csize = constant_size (AT_unsigned (a));
10051 if (dwarf_version == 3
10052 && a->dw_attr == DW_AT_data_member_location
10053 && csize >= 4)
10054 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10055 else
10056 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10058 break;
10060 case dw_val_class_const_implicit:
10061 if (flag_debug_asm)
10062 fprintf (asm_out_file, "\t\t\t%s %s ("
10063 HOST_WIDE_INT_PRINT_DEC ")\n",
10064 ASM_COMMENT_START, name, AT_int (a));
10065 break;
10067 case dw_val_class_unsigned_const_implicit:
10068 if (flag_debug_asm)
10069 fprintf (asm_out_file, "\t\t\t%s %s ("
10070 HOST_WIDE_INT_PRINT_HEX ")\n",
10071 ASM_COMMENT_START, name, AT_unsigned (a));
10072 break;
10074 case dw_val_class_const_double:
10076 unsigned HOST_WIDE_INT first, second;
10078 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10079 dw2_asm_output_data (1,
10080 HOST_BITS_PER_DOUBLE_INT
10081 / HOST_BITS_PER_CHAR,
10082 NULL);
10084 if (WORDS_BIG_ENDIAN)
10086 first = a->dw_attr_val.v.val_double.high;
10087 second = a->dw_attr_val.v.val_double.low;
10089 else
10091 first = a->dw_attr_val.v.val_double.low;
10092 second = a->dw_attr_val.v.val_double.high;
10095 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10096 first, "%s", name);
10097 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10098 second, NULL);
10100 break;
10102 case dw_val_class_wide_int:
10104 int i;
10105 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10106 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10107 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10108 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10109 * l, NULL);
10111 if (WORDS_BIG_ENDIAN)
10112 for (i = len - 1; i >= 0; --i)
10114 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10115 "%s", name);
10116 name = "";
10118 else
10119 for (i = 0; i < len; ++i)
10121 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10122 "%s", name);
10123 name = "";
10126 break;
10128 case dw_val_class_vec:
10130 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10131 unsigned int len = a->dw_attr_val.v.val_vec.length;
10132 unsigned int i;
10133 unsigned char *p;
10135 dw2_asm_output_data (constant_size (len * elt_size),
10136 len * elt_size, "%s", name);
10137 if (elt_size > sizeof (HOST_WIDE_INT))
10139 elt_size /= 2;
10140 len *= 2;
10142 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10143 i < len;
10144 i++, p += elt_size)
10145 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10146 "fp or vector constant word %u", i);
10147 break;
10150 case dw_val_class_flag:
10151 if (dwarf_version >= 4)
10153 /* Currently all add_AT_flag calls pass in 1 as last argument,
10154 so DW_FORM_flag_present can be used. If that ever changes,
10155 we'll need to use DW_FORM_flag and have some optimization
10156 in build_abbrev_table that will change those to
10157 DW_FORM_flag_present if it is set to 1 in all DIEs using
10158 the same abbrev entry. */
10159 gcc_assert (AT_flag (a) == 1);
10160 if (flag_debug_asm)
10161 fprintf (asm_out_file, "\t\t\t%s %s\n",
10162 ASM_COMMENT_START, name);
10163 break;
10165 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10166 break;
10168 case dw_val_class_loc_list:
10169 output_loc_list_offset (a);
10170 break;
10172 case dw_val_class_die_ref:
10173 if (AT_ref_external (a))
10175 if (AT_ref (a)->comdat_type_p)
10177 comdat_type_node *type_node
10178 = AT_ref (a)->die_id.die_type_node;
10180 gcc_assert (type_node);
10181 output_signature (type_node->signature, name);
10183 else
10185 const char *sym = AT_ref (a)->die_id.die_symbol;
10186 int size;
10188 gcc_assert (sym);
10189 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10190 length, whereas in DWARF3 it's always sized as an
10191 offset. */
10192 if (dwarf_version == 2)
10193 size = DWARF2_ADDR_SIZE;
10194 else
10195 size = DWARF_OFFSET_SIZE;
10196 /* ??? We cannot unconditionally output die_offset if
10197 non-zero - others might create references to those
10198 DIEs via symbols.
10199 And we do not clear its DIE offset after outputting it
10200 (and the label refers to the actual DIEs, not the
10201 DWARF CU unit header which is when using label + offset
10202 would be the correct thing to do).
10203 ??? This is the reason for the with_offset flag. */
10204 if (AT_ref (a)->with_offset)
10205 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10206 debug_info_section, "%s", name);
10207 else
10208 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10209 name);
10212 else
10214 gcc_assert (AT_ref (a)->die_offset);
10215 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10216 "%s", name);
10218 break;
10220 case dw_val_class_fde_ref:
10222 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10224 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10225 a->dw_attr_val.v.val_fde_index * 2);
10226 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10227 "%s", name);
10229 break;
10231 case dw_val_class_vms_delta:
10232 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10233 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10234 AT_vms_delta2 (a), AT_vms_delta1 (a),
10235 "%s", name);
10236 #else
10237 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10238 AT_vms_delta2 (a), AT_vms_delta1 (a),
10239 "%s", name);
10240 #endif
10241 break;
10243 case dw_val_class_lbl_id:
10244 output_attr_index_or_value (a);
10245 break;
10247 case dw_val_class_lineptr:
10248 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10249 debug_line_section, "%s", name);
10250 break;
10252 case dw_val_class_macptr:
10253 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10254 debug_macinfo_section, "%s", name);
10255 break;
10257 case dw_val_class_loclistsptr:
10258 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10259 debug_loc_section, "%s", name);
10260 break;
10262 case dw_val_class_str:
10263 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10264 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10265 a->dw_attr_val.v.val_str->label,
10266 debug_str_section,
10267 "%s: \"%s\"", name, AT_string (a));
10268 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10269 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10270 a->dw_attr_val.v.val_str->label,
10271 debug_line_str_section,
10272 "%s: \"%s\"", name, AT_string (a));
10273 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10274 dw2_asm_output_data_uleb128 (AT_index (a),
10275 "%s: \"%s\"", name, AT_string (a));
10276 else
10277 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10278 break;
10280 case dw_val_class_file:
10282 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10284 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10285 a->dw_attr_val.v.val_file->filename);
10286 break;
10289 case dw_val_class_file_implicit:
10290 if (flag_debug_asm)
10291 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10292 ASM_COMMENT_START, name,
10293 maybe_emit_file (a->dw_attr_val.v.val_file),
10294 a->dw_attr_val.v.val_file->filename);
10295 break;
10297 case dw_val_class_data8:
10299 int i;
10301 for (i = 0; i < 8; i++)
10302 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10303 i == 0 ? "%s" : NULL, name);
10304 break;
10307 case dw_val_class_high_pc:
10308 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10309 get_AT_low_pc (die), "DW_AT_high_pc");
10310 break;
10312 case dw_val_class_discr_value:
10313 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10314 break;
10316 case dw_val_class_discr_list:
10318 dw_discr_list_ref list = AT_discr_list (a);
10319 const int size = size_of_discr_list (list);
10321 /* This is a block, so output its length first. */
10322 dw2_asm_output_data (constant_size (size), size,
10323 "%s: block size", name);
10325 for (; list != NULL; list = list->dw_discr_next)
10327 /* One byte for the discriminant value descriptor, and then as
10328 many LEB128 numbers as required. */
10329 if (list->dw_discr_range)
10330 dw2_asm_output_data (1, DW_DSC_range,
10331 "%s: DW_DSC_range", name);
10332 else
10333 dw2_asm_output_data (1, DW_DSC_label,
10334 "%s: DW_DSC_label", name);
10336 output_discr_value (&list->dw_discr_lower_bound, name);
10337 if (list->dw_discr_range)
10338 output_discr_value (&list->dw_discr_upper_bound, name);
10340 break;
10343 default:
10344 gcc_unreachable ();
10348 FOR_EACH_CHILD (die, c, output_die (c));
10350 /* Add null byte to terminate sibling list. */
10351 if (die->die_child != NULL)
10352 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10353 (unsigned long) die->die_offset);
10356 /* Output the compilation unit that appears at the beginning of the
10357 .debug_info section, and precedes the DIE descriptions. */
10359 static void
10360 output_compilation_unit_header (enum dwarf_unit_type ut)
10362 if (!XCOFF_DEBUGGING_INFO)
10364 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10365 dw2_asm_output_data (4, 0xffffffff,
10366 "Initial length escape value indicating 64-bit DWARF extension");
10367 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10368 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10369 "Length of Compilation Unit Info");
10372 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10373 if (dwarf_version >= 5)
10375 const char *name;
10376 switch (ut)
10378 case DW_UT_compile: name = "DW_UT_compile"; break;
10379 case DW_UT_type: name = "DW_UT_type"; break;
10380 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10381 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10382 default: gcc_unreachable ();
10384 dw2_asm_output_data (1, ut, "%s", name);
10385 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10387 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10388 debug_abbrev_section,
10389 "Offset Into Abbrev. Section");
10390 if (dwarf_version < 5)
10391 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10394 /* Output the compilation unit DIE and its children. */
10396 static void
10397 output_comp_unit (dw_die_ref die, int output_if_empty,
10398 const unsigned char *dwo_id)
10400 const char *secname, *oldsym;
10401 char *tmp;
10403 /* Unless we are outputting main CU, we may throw away empty ones. */
10404 if (!output_if_empty && die->die_child == NULL)
10405 return;
10407 /* Even if there are no children of this DIE, we must output the information
10408 about the compilation unit. Otherwise, on an empty translation unit, we
10409 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10410 will then complain when examining the file. First mark all the DIEs in
10411 this CU so we know which get local refs. */
10412 mark_dies (die);
10414 external_ref_hash_type *extern_map = optimize_external_refs (die);
10416 /* For now, optimize only the main CU, in order to optimize the rest
10417 we'd need to see all of them earlier. Leave the rest for post-linking
10418 tools like DWZ. */
10419 if (die == comp_unit_die ())
10420 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10422 build_abbrev_table (die, extern_map);
10424 optimize_abbrev_table ();
10426 delete extern_map;
10428 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10429 next_die_offset = (dwo_id
10430 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10431 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10432 calc_die_sizes (die);
10434 oldsym = die->die_id.die_symbol;
10435 if (oldsym && die->comdat_type_p)
10437 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10439 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10440 secname = tmp;
10441 die->die_id.die_symbol = NULL;
10442 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10444 else
10446 switch_to_section (debug_info_section);
10447 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10448 info_section_emitted = true;
10451 /* For LTO cross unit DIE refs we want a symbol on the start of the
10452 debuginfo section, not on the CU DIE. */
10453 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10455 /* ??? No way to get visibility assembled without a decl. */
10456 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10457 get_identifier (oldsym), char_type_node);
10458 TREE_PUBLIC (decl) = true;
10459 TREE_STATIC (decl) = true;
10460 DECL_ARTIFICIAL (decl) = true;
10461 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10462 DECL_VISIBILITY_SPECIFIED (decl) = true;
10463 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10464 #ifdef ASM_WEAKEN_LABEL
10465 /* We prefer a .weak because that handles duplicates from duplicate
10466 archive members in a graceful way. */
10467 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10468 #else
10469 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10470 #endif
10471 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10474 /* Output debugging information. */
10475 output_compilation_unit_header (dwo_id
10476 ? DW_UT_split_compile : DW_UT_compile);
10477 if (dwarf_version >= 5)
10479 if (dwo_id != NULL)
10480 for (int i = 0; i < 8; i++)
10481 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10483 output_die (die);
10485 /* Leave the marks on the main CU, so we can check them in
10486 output_pubnames. */
10487 if (oldsym)
10489 unmark_dies (die);
10490 die->die_id.die_symbol = oldsym;
10494 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10495 and .debug_pubtypes. This is configured per-target, but can be
10496 overridden by the -gpubnames or -gno-pubnames options. */
10498 static inline bool
10499 want_pubnames (void)
10501 if (debug_info_level <= DINFO_LEVEL_TERSE)
10502 return false;
10503 if (debug_generate_pub_sections != -1)
10504 return debug_generate_pub_sections;
10505 return targetm.want_debug_pub_sections;
10508 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10510 static void
10511 add_AT_pubnames (dw_die_ref die)
10513 if (want_pubnames ())
10514 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10517 /* Add a string attribute value to a skeleton DIE. */
10519 static inline void
10520 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10521 const char *str)
10523 dw_attr_node attr;
10524 struct indirect_string_node *node;
10526 if (! skeleton_debug_str_hash)
10527 skeleton_debug_str_hash
10528 = hash_table<indirect_string_hasher>::create_ggc (10);
10530 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10531 find_string_form (node);
10532 if (node->form == DW_FORM_GNU_str_index)
10533 node->form = DW_FORM_strp;
10535 attr.dw_attr = attr_kind;
10536 attr.dw_attr_val.val_class = dw_val_class_str;
10537 attr.dw_attr_val.val_entry = NULL;
10538 attr.dw_attr_val.v.val_str = node;
10539 add_dwarf_attr (die, &attr);
10542 /* Helper function to generate top-level dies for skeleton debug_info and
10543 debug_types. */
10545 static void
10546 add_top_level_skeleton_die_attrs (dw_die_ref die)
10548 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10549 const char *comp_dir = comp_dir_string ();
10551 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10552 if (comp_dir != NULL)
10553 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10554 add_AT_pubnames (die);
10555 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10558 /* Output skeleton debug sections that point to the dwo file. */
10560 static void
10561 output_skeleton_debug_sections (dw_die_ref comp_unit,
10562 const unsigned char *dwo_id)
10564 /* These attributes will be found in the full debug_info section. */
10565 remove_AT (comp_unit, DW_AT_producer);
10566 remove_AT (comp_unit, DW_AT_language);
10568 switch_to_section (debug_skeleton_info_section);
10569 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10571 /* Produce the skeleton compilation-unit header. This one differs enough from
10572 a normal CU header that it's better not to call output_compilation_unit
10573 header. */
10574 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10575 dw2_asm_output_data (4, 0xffffffff,
10576 "Initial length escape value indicating 64-bit "
10577 "DWARF extension");
10579 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10580 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10581 - DWARF_INITIAL_LENGTH_SIZE
10582 + size_of_die (comp_unit),
10583 "Length of Compilation Unit Info");
10584 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10585 if (dwarf_version >= 5)
10587 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10588 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10590 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10591 debug_skeleton_abbrev_section,
10592 "Offset Into Abbrev. Section");
10593 if (dwarf_version < 5)
10594 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10595 else
10596 for (int i = 0; i < 8; i++)
10597 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10599 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10600 output_die (comp_unit);
10602 /* Build the skeleton debug_abbrev section. */
10603 switch_to_section (debug_skeleton_abbrev_section);
10604 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10606 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10608 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10611 /* Output a comdat type unit DIE and its children. */
10613 static void
10614 output_comdat_type_unit (comdat_type_node *node)
10616 const char *secname;
10617 char *tmp;
10618 int i;
10619 #if defined (OBJECT_FORMAT_ELF)
10620 tree comdat_key;
10621 #endif
10623 /* First mark all the DIEs in this CU so we know which get local refs. */
10624 mark_dies (node->root_die);
10626 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10628 build_abbrev_table (node->root_die, extern_map);
10630 delete extern_map;
10631 extern_map = NULL;
10633 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10634 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10635 calc_die_sizes (node->root_die);
10637 #if defined (OBJECT_FORMAT_ELF)
10638 if (dwarf_version >= 5)
10640 if (!dwarf_split_debug_info)
10641 secname = ".debug_info";
10642 else
10643 secname = ".debug_info.dwo";
10645 else if (!dwarf_split_debug_info)
10646 secname = ".debug_types";
10647 else
10648 secname = ".debug_types.dwo";
10650 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10651 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10652 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10653 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10654 comdat_key = get_identifier (tmp);
10655 targetm.asm_out.named_section (secname,
10656 SECTION_DEBUG | SECTION_LINKONCE,
10657 comdat_key);
10658 #else
10659 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10660 sprintf (tmp, (dwarf_version >= 5
10661 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10662 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10663 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10664 secname = tmp;
10665 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10666 #endif
10668 /* Output debugging information. */
10669 output_compilation_unit_header (dwarf_split_debug_info
10670 ? DW_UT_split_type : DW_UT_type);
10671 output_signature (node->signature, "Type Signature");
10672 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10673 "Offset to Type DIE");
10674 output_die (node->root_die);
10676 unmark_dies (node->root_die);
10679 /* Return the DWARF2/3 pubname associated with a decl. */
10681 static const char *
10682 dwarf2_name (tree decl, int scope)
10684 if (DECL_NAMELESS (decl))
10685 return NULL;
10686 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10689 /* Add a new entry to .debug_pubnames if appropriate. */
10691 static void
10692 add_pubname_string (const char *str, dw_die_ref die)
10694 pubname_entry e;
10696 e.die = die;
10697 e.name = xstrdup (str);
10698 vec_safe_push (pubname_table, e);
10701 static void
10702 add_pubname (tree decl, dw_die_ref die)
10704 if (!want_pubnames ())
10705 return;
10707 /* Don't add items to the table when we expect that the consumer will have
10708 just read the enclosing die. For example, if the consumer is looking at a
10709 class_member, it will either be inside the class already, or will have just
10710 looked up the class to find the member. Either way, searching the class is
10711 faster than searching the index. */
10712 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10713 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10715 const char *name = dwarf2_name (decl, 1);
10717 if (name)
10718 add_pubname_string (name, die);
10722 /* Add an enumerator to the pubnames section. */
10724 static void
10725 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10727 pubname_entry e;
10729 gcc_assert (scope_name);
10730 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10731 e.die = die;
10732 vec_safe_push (pubname_table, e);
10735 /* Add a new entry to .debug_pubtypes if appropriate. */
10737 static void
10738 add_pubtype (tree decl, dw_die_ref die)
10740 pubname_entry e;
10742 if (!want_pubnames ())
10743 return;
10745 if ((TREE_PUBLIC (decl)
10746 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10747 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10749 tree scope = NULL;
10750 const char *scope_name = "";
10751 const char *sep = is_cxx () ? "::" : ".";
10752 const char *name;
10754 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10755 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10757 scope_name = lang_hooks.dwarf_name (scope, 1);
10758 if (scope_name != NULL && scope_name[0] != '\0')
10759 scope_name = concat (scope_name, sep, NULL);
10760 else
10761 scope_name = "";
10764 if (TYPE_P (decl))
10765 name = type_tag (decl);
10766 else
10767 name = lang_hooks.dwarf_name (decl, 1);
10769 /* If we don't have a name for the type, there's no point in adding
10770 it to the table. */
10771 if (name != NULL && name[0] != '\0')
10773 e.die = die;
10774 e.name = concat (scope_name, name, NULL);
10775 vec_safe_push (pubtype_table, e);
10778 /* Although it might be more consistent to add the pubinfo for the
10779 enumerators as their dies are created, they should only be added if the
10780 enum type meets the criteria above. So rather than re-check the parent
10781 enum type whenever an enumerator die is created, just output them all
10782 here. This isn't protected by the name conditional because anonymous
10783 enums don't have names. */
10784 if (die->die_tag == DW_TAG_enumeration_type)
10786 dw_die_ref c;
10788 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10793 /* Output a single entry in the pubnames table. */
10795 static void
10796 output_pubname (dw_offset die_offset, pubname_entry *entry)
10798 dw_die_ref die = entry->die;
10799 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10801 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10803 if (debug_generate_pub_sections == 2)
10805 /* This logic follows gdb's method for determining the value of the flag
10806 byte. */
10807 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10808 switch (die->die_tag)
10810 case DW_TAG_typedef:
10811 case DW_TAG_base_type:
10812 case DW_TAG_subrange_type:
10813 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10814 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10815 break;
10816 case DW_TAG_enumerator:
10817 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10818 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10819 if (!is_cxx ())
10820 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10821 break;
10822 case DW_TAG_subprogram:
10823 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10824 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10825 if (!is_ada ())
10826 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10827 break;
10828 case DW_TAG_constant:
10829 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10830 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10831 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10832 break;
10833 case DW_TAG_variable:
10834 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10835 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10836 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10837 break;
10838 case DW_TAG_namespace:
10839 case DW_TAG_imported_declaration:
10840 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10841 break;
10842 case DW_TAG_class_type:
10843 case DW_TAG_interface_type:
10844 case DW_TAG_structure_type:
10845 case DW_TAG_union_type:
10846 case DW_TAG_enumeration_type:
10847 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10848 if (!is_cxx ())
10849 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10850 break;
10851 default:
10852 /* An unusual tag. Leave the flag-byte empty. */
10853 break;
10855 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10856 "GDB-index flags");
10859 dw2_asm_output_nstring (entry->name, -1, "external name");
10863 /* Output the public names table used to speed up access to externally
10864 visible names; or the public types table used to find type definitions. */
10866 static void
10867 output_pubnames (vec<pubname_entry, va_gc> *names)
10869 unsigned i;
10870 unsigned long pubnames_length = size_of_pubnames (names);
10871 pubname_entry *pub;
10873 if (!XCOFF_DEBUGGING_INFO)
10875 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10876 dw2_asm_output_data (4, 0xffffffff,
10877 "Initial length escape value indicating 64-bit DWARF extension");
10878 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10879 "Pub Info Length");
10882 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10883 dw2_asm_output_data (2, 2, "DWARF Version");
10885 if (dwarf_split_debug_info)
10886 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10887 debug_skeleton_info_section,
10888 "Offset of Compilation Unit Info");
10889 else
10890 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10891 debug_info_section,
10892 "Offset of Compilation Unit Info");
10893 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10894 "Compilation Unit Length");
10896 FOR_EACH_VEC_ELT (*names, i, pub)
10898 if (include_pubname_in_output (names, pub))
10900 dw_offset die_offset = pub->die->die_offset;
10902 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10903 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10904 gcc_assert (pub->die->die_mark);
10906 /* If we're putting types in their own .debug_types sections,
10907 the .debug_pubtypes table will still point to the compile
10908 unit (not the type unit), so we want to use the offset of
10909 the skeleton DIE (if there is one). */
10910 if (pub->die->comdat_type_p && names == pubtype_table)
10912 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10914 if (type_node != NULL)
10915 die_offset = (type_node->skeleton_die != NULL
10916 ? type_node->skeleton_die->die_offset
10917 : comp_unit_die ()->die_offset);
10920 output_pubname (die_offset, pub);
10924 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10927 /* Output public names and types tables if necessary. */
10929 static void
10930 output_pubtables (void)
10932 if (!want_pubnames () || !info_section_emitted)
10933 return;
10935 switch_to_section (debug_pubnames_section);
10936 output_pubnames (pubname_table);
10937 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10938 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10939 simply won't look for the section. */
10940 switch_to_section (debug_pubtypes_section);
10941 output_pubnames (pubtype_table);
10945 /* Output the information that goes into the .debug_aranges table.
10946 Namely, define the beginning and ending address range of the
10947 text section generated for this compilation unit. */
10949 static void
10950 output_aranges (void)
10952 unsigned i;
10953 unsigned long aranges_length = size_of_aranges ();
10955 if (!XCOFF_DEBUGGING_INFO)
10957 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10958 dw2_asm_output_data (4, 0xffffffff,
10959 "Initial length escape value indicating 64-bit DWARF extension");
10960 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10961 "Length of Address Ranges Info");
10964 /* Version number for aranges is still 2, even up to DWARF5. */
10965 dw2_asm_output_data (2, 2, "DWARF Version");
10966 if (dwarf_split_debug_info)
10967 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10968 debug_skeleton_info_section,
10969 "Offset of Compilation Unit Info");
10970 else
10971 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10972 debug_info_section,
10973 "Offset of Compilation Unit Info");
10974 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10975 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10977 /* We need to align to twice the pointer size here. */
10978 if (DWARF_ARANGES_PAD_SIZE)
10980 /* Pad using a 2 byte words so that padding is correct for any
10981 pointer size. */
10982 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10983 2 * DWARF2_ADDR_SIZE);
10984 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10985 dw2_asm_output_data (2, 0, NULL);
10988 /* It is necessary not to output these entries if the sections were
10989 not used; if the sections were not used, the length will be 0 and
10990 the address may end up as 0 if the section is discarded by ld
10991 --gc-sections, leaving an invalid (0, 0) entry that can be
10992 confused with the terminator. */
10993 if (text_section_used)
10995 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10996 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10997 text_section_label, "Length");
10999 if (cold_text_section_used)
11001 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11002 "Address");
11003 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11004 cold_text_section_label, "Length");
11007 if (have_multiple_function_sections)
11009 unsigned fde_idx;
11010 dw_fde_ref fde;
11012 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11014 if (DECL_IGNORED_P (fde->decl))
11015 continue;
11016 if (!fde->in_std_section)
11018 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11019 "Address");
11020 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11021 fde->dw_fde_begin, "Length");
11023 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11025 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11026 "Address");
11027 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11028 fde->dw_fde_second_begin, "Length");
11033 /* Output the terminator words. */
11034 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11035 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11038 /* Add a new entry to .debug_ranges. Return its index into
11039 ranges_table vector. */
11041 static unsigned int
11042 add_ranges_num (int num, bool maybe_new_sec)
11044 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11045 vec_safe_push (ranges_table, r);
11046 return vec_safe_length (ranges_table) - 1;
11049 /* Add a new entry to .debug_ranges corresponding to a block, or a
11050 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11051 this entry might be in a different section from previous range. */
11053 static unsigned int
11054 add_ranges (const_tree block, bool maybe_new_sec)
11056 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11059 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11060 chain, or middle entry of a chain that will be directly referred to. */
11062 static void
11063 note_rnglist_head (unsigned int offset)
11065 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11066 return;
11067 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11070 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11071 When using dwarf_split_debug_info, address attributes in dies destined
11072 for the final executable should be direct references--setting the
11073 parameter force_direct ensures this behavior. */
11075 static void
11076 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11077 bool *added, bool force_direct)
11079 unsigned int in_use = vec_safe_length (ranges_by_label);
11080 unsigned int offset;
11081 dw_ranges_by_label rbl = { begin, end };
11082 vec_safe_push (ranges_by_label, rbl);
11083 offset = add_ranges_num (-(int)in_use - 1, true);
11084 if (!*added)
11086 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11087 *added = true;
11088 note_rnglist_head (offset);
11092 /* Emit .debug_ranges section. */
11094 static void
11095 output_ranges (void)
11097 unsigned i;
11098 static const char *const start_fmt = "Offset %#x";
11099 const char *fmt = start_fmt;
11100 dw_ranges *r;
11102 switch_to_section (debug_ranges_section);
11103 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11104 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11106 int block_num = r->num;
11108 if (block_num > 0)
11110 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11111 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11113 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11114 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11116 /* If all code is in the text section, then the compilation
11117 unit base address defaults to DW_AT_low_pc, which is the
11118 base of the text section. */
11119 if (!have_multiple_function_sections)
11121 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11122 text_section_label,
11123 fmt, i * 2 * DWARF2_ADDR_SIZE);
11124 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11125 text_section_label, NULL);
11128 /* Otherwise, the compilation unit base address is zero,
11129 which allows us to use absolute addresses, and not worry
11130 about whether the target supports cross-section
11131 arithmetic. */
11132 else
11134 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11135 fmt, i * 2 * DWARF2_ADDR_SIZE);
11136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11139 fmt = NULL;
11142 /* Negative block_num stands for an index into ranges_by_label. */
11143 else if (block_num < 0)
11145 int lab_idx = - block_num - 1;
11147 if (!have_multiple_function_sections)
11149 gcc_unreachable ();
11150 #if 0
11151 /* If we ever use add_ranges_by_labels () for a single
11152 function section, all we have to do is to take out
11153 the #if 0 above. */
11154 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11155 (*ranges_by_label)[lab_idx].begin,
11156 text_section_label,
11157 fmt, i * 2 * DWARF2_ADDR_SIZE);
11158 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11159 (*ranges_by_label)[lab_idx].end,
11160 text_section_label, NULL);
11161 #endif
11163 else
11165 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11166 (*ranges_by_label)[lab_idx].begin,
11167 fmt, i * 2 * DWARF2_ADDR_SIZE);
11168 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11169 (*ranges_by_label)[lab_idx].end,
11170 NULL);
11173 else
11175 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11176 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11177 fmt = start_fmt;
11182 /* Non-zero if .debug_line_str should be used for .debug_line section
11183 strings or strings that are likely shareable with those. */
11184 #define DWARF5_USE_DEBUG_LINE_STR \
11185 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11186 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11187 /* FIXME: there is no .debug_line_str.dwo section, \
11188 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11189 && !dwarf_split_debug_info)
11191 /* Assign .debug_rnglists indexes. */
11193 static void
11194 index_rnglists (void)
11196 unsigned i;
11197 dw_ranges *r;
11199 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11200 if (r->label)
11201 r->idx = rnglist_idx++;
11204 /* Emit .debug_rnglists section. */
11206 static void
11207 output_rnglists (void)
11209 unsigned i;
11210 dw_ranges *r;
11211 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11212 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11213 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11215 switch_to_section (debug_ranges_section);
11216 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11217 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11218 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11219 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11220 dw2_asm_output_data (4, 0xffffffff,
11221 "Initial length escape value indicating "
11222 "64-bit DWARF extension");
11223 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11224 "Length of Range Lists");
11225 ASM_OUTPUT_LABEL (asm_out_file, l1);
11226 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11227 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11228 dw2_asm_output_data (1, 0, "Segment Size");
11229 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11230 about relocation sizes and primarily care about the size of .debug*
11231 sections in linked shared libraries and executables, then
11232 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11233 into it are usually larger than just DW_FORM_sec_offset offsets
11234 into the .debug_rnglists section. */
11235 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11236 "Offset Entry Count");
11237 if (dwarf_split_debug_info)
11239 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11240 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11241 if (r->label)
11242 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11243 ranges_base_label, NULL);
11246 const char *lab = "";
11247 unsigned int len = vec_safe_length (ranges_table);
11248 const char *base = NULL;
11249 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11251 int block_num = r->num;
11253 if (r->label)
11255 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11256 lab = r->label;
11258 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11259 base = NULL;
11260 if (block_num > 0)
11262 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11263 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11265 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11266 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11268 if (HAVE_AS_LEB128)
11270 /* If all code is in the text section, then the compilation
11271 unit base address defaults to DW_AT_low_pc, which is the
11272 base of the text section. */
11273 if (!have_multiple_function_sections)
11275 dw2_asm_output_data (1, DW_RLE_offset_pair,
11276 "DW_RLE_offset_pair (%s)", lab);
11277 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11278 "Range begin address (%s)", lab);
11279 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11280 "Range end address (%s)", lab);
11281 continue;
11283 if (base == NULL)
11285 dw_ranges *r2 = NULL;
11286 if (i < len - 1)
11287 r2 = &(*ranges_table)[i + 1];
11288 if (r2
11289 && r2->num != 0
11290 && r2->label == NULL
11291 && !r2->maybe_new_sec)
11293 dw2_asm_output_data (1, DW_RLE_base_address,
11294 "DW_RLE_base_address (%s)", lab);
11295 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11296 "Base address (%s)", lab);
11297 strcpy (basebuf, blabel);
11298 base = basebuf;
11301 if (base)
11303 dw2_asm_output_data (1, DW_RLE_offset_pair,
11304 "DW_RLE_offset_pair (%s)", lab);
11305 dw2_asm_output_delta_uleb128 (blabel, base,
11306 "Range begin address (%s)", lab);
11307 dw2_asm_output_delta_uleb128 (elabel, base,
11308 "Range end address (%s)", lab);
11309 continue;
11311 dw2_asm_output_data (1, DW_RLE_start_length,
11312 "DW_RLE_start_length (%s)", lab);
11313 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11314 "Range begin address (%s)", lab);
11315 dw2_asm_output_delta_uleb128 (elabel, blabel,
11316 "Range length (%s)", lab);
11318 else
11320 dw2_asm_output_data (1, DW_RLE_start_end,
11321 "DW_RLE_start_end (%s)", lab);
11322 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11323 "Range begin address (%s)", lab);
11324 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11325 "Range end address (%s)", lab);
11329 /* Negative block_num stands for an index into ranges_by_label. */
11330 else if (block_num < 0)
11332 int lab_idx = - block_num - 1;
11333 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11334 const char *elabel = (*ranges_by_label)[lab_idx].end;
11336 if (!have_multiple_function_sections)
11337 gcc_unreachable ();
11338 if (HAVE_AS_LEB128)
11340 dw2_asm_output_data (1, DW_RLE_start_length,
11341 "DW_RLE_start_length (%s)", lab);
11342 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11343 "Range begin address (%s)", lab);
11344 dw2_asm_output_delta_uleb128 (elabel, blabel,
11345 "Range length (%s)", lab);
11347 else
11349 dw2_asm_output_data (1, DW_RLE_start_end,
11350 "DW_RLE_start_end (%s)", lab);
11351 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11352 "Range begin address (%s)", lab);
11353 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11354 "Range end address (%s)", lab);
11357 else
11358 dw2_asm_output_data (1, DW_RLE_end_of_list,
11359 "DW_RLE_end_of_list (%s)", lab);
11361 ASM_OUTPUT_LABEL (asm_out_file, l2);
11364 /* Data structure containing information about input files. */
11365 struct file_info
11367 const char *path; /* Complete file name. */
11368 const char *fname; /* File name part. */
11369 int length; /* Length of entire string. */
11370 struct dwarf_file_data * file_idx; /* Index in input file table. */
11371 int dir_idx; /* Index in directory table. */
11374 /* Data structure containing information about directories with source
11375 files. */
11376 struct dir_info
11378 const char *path; /* Path including directory name. */
11379 int length; /* Path length. */
11380 int prefix; /* Index of directory entry which is a prefix. */
11381 int count; /* Number of files in this directory. */
11382 int dir_idx; /* Index of directory used as base. */
11385 /* Callback function for file_info comparison. We sort by looking at
11386 the directories in the path. */
11388 static int
11389 file_info_cmp (const void *p1, const void *p2)
11391 const struct file_info *const s1 = (const struct file_info *) p1;
11392 const struct file_info *const s2 = (const struct file_info *) p2;
11393 const unsigned char *cp1;
11394 const unsigned char *cp2;
11396 /* Take care of file names without directories. We need to make sure that
11397 we return consistent values to qsort since some will get confused if
11398 we return the same value when identical operands are passed in opposite
11399 orders. So if neither has a directory, return 0 and otherwise return
11400 1 or -1 depending on which one has the directory. */
11401 if ((s1->path == s1->fname || s2->path == s2->fname))
11402 return (s2->path == s2->fname) - (s1->path == s1->fname);
11404 cp1 = (const unsigned char *) s1->path;
11405 cp2 = (const unsigned char *) s2->path;
11407 while (1)
11409 ++cp1;
11410 ++cp2;
11411 /* Reached the end of the first path? If so, handle like above. */
11412 if ((cp1 == (const unsigned char *) s1->fname)
11413 || (cp2 == (const unsigned char *) s2->fname))
11414 return ((cp2 == (const unsigned char *) s2->fname)
11415 - (cp1 == (const unsigned char *) s1->fname));
11417 /* Character of current path component the same? */
11418 else if (*cp1 != *cp2)
11419 return *cp1 - *cp2;
11423 struct file_name_acquire_data
11425 struct file_info *files;
11426 int used_files;
11427 int max_files;
11430 /* Traversal function for the hash table. */
11433 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11435 struct dwarf_file_data *d = *slot;
11436 struct file_info *fi;
11437 const char *f;
11439 gcc_assert (fnad->max_files >= d->emitted_number);
11441 if (! d->emitted_number)
11442 return 1;
11444 gcc_assert (fnad->max_files != fnad->used_files);
11446 fi = fnad->files + fnad->used_files++;
11448 /* Skip all leading "./". */
11449 f = d->filename;
11450 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11451 f += 2;
11453 /* Create a new array entry. */
11454 fi->path = f;
11455 fi->length = strlen (f);
11456 fi->file_idx = d;
11458 /* Search for the file name part. */
11459 f = strrchr (f, DIR_SEPARATOR);
11460 #if defined (DIR_SEPARATOR_2)
11462 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11464 if (g != NULL)
11466 if (f == NULL || f < g)
11467 f = g;
11470 #endif
11472 fi->fname = f == NULL ? fi->path : f + 1;
11473 return 1;
11476 /* Helper function for output_file_names. Emit a FORM encoded
11477 string STR, with assembly comment start ENTRY_KIND and
11478 index IDX */
11480 static void
11481 output_line_string (enum dwarf_form form, const char *str,
11482 const char *entry_kind, unsigned int idx)
11484 switch (form)
11486 case DW_FORM_string:
11487 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11488 break;
11489 case DW_FORM_line_strp:
11490 if (!debug_line_str_hash)
11491 debug_line_str_hash
11492 = hash_table<indirect_string_hasher>::create_ggc (10);
11494 struct indirect_string_node *node;
11495 node = find_AT_string_in_table (str, debug_line_str_hash);
11496 set_indirect_string (node);
11497 node->form = form;
11498 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11499 debug_line_str_section, "%s: %#x: \"%s\"",
11500 entry_kind, 0, node->str);
11501 break;
11502 default:
11503 gcc_unreachable ();
11507 /* Output the directory table and the file name table. We try to minimize
11508 the total amount of memory needed. A heuristic is used to avoid large
11509 slowdowns with many input files. */
11511 static void
11512 output_file_names (void)
11514 struct file_name_acquire_data fnad;
11515 int numfiles;
11516 struct file_info *files;
11517 struct dir_info *dirs;
11518 int *saved;
11519 int *savehere;
11520 int *backmap;
11521 int ndirs;
11522 int idx_offset;
11523 int i;
11525 if (!last_emitted_file)
11527 if (dwarf_version >= 5)
11529 dw2_asm_output_data (1, 0, "Directory entry format count");
11530 dw2_asm_output_data_uleb128 (0, "Directories count");
11531 dw2_asm_output_data (1, 0, "File name entry format count");
11532 dw2_asm_output_data_uleb128 (0, "File names count");
11534 else
11536 dw2_asm_output_data (1, 0, "End directory table");
11537 dw2_asm_output_data (1, 0, "End file name table");
11539 return;
11542 numfiles = last_emitted_file->emitted_number;
11544 /* Allocate the various arrays we need. */
11545 files = XALLOCAVEC (struct file_info, numfiles);
11546 dirs = XALLOCAVEC (struct dir_info, numfiles);
11548 fnad.files = files;
11549 fnad.used_files = 0;
11550 fnad.max_files = numfiles;
11551 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11552 gcc_assert (fnad.used_files == fnad.max_files);
11554 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11556 /* Find all the different directories used. */
11557 dirs[0].path = files[0].path;
11558 dirs[0].length = files[0].fname - files[0].path;
11559 dirs[0].prefix = -1;
11560 dirs[0].count = 1;
11561 dirs[0].dir_idx = 0;
11562 files[0].dir_idx = 0;
11563 ndirs = 1;
11565 for (i = 1; i < numfiles; i++)
11566 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11567 && memcmp (dirs[ndirs - 1].path, files[i].path,
11568 dirs[ndirs - 1].length) == 0)
11570 /* Same directory as last entry. */
11571 files[i].dir_idx = ndirs - 1;
11572 ++dirs[ndirs - 1].count;
11574 else
11576 int j;
11578 /* This is a new directory. */
11579 dirs[ndirs].path = files[i].path;
11580 dirs[ndirs].length = files[i].fname - files[i].path;
11581 dirs[ndirs].count = 1;
11582 dirs[ndirs].dir_idx = ndirs;
11583 files[i].dir_idx = ndirs;
11585 /* Search for a prefix. */
11586 dirs[ndirs].prefix = -1;
11587 for (j = 0; j < ndirs; j++)
11588 if (dirs[j].length < dirs[ndirs].length
11589 && dirs[j].length > 1
11590 && (dirs[ndirs].prefix == -1
11591 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11592 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11593 dirs[ndirs].prefix = j;
11595 ++ndirs;
11598 /* Now to the actual work. We have to find a subset of the directories which
11599 allow expressing the file name using references to the directory table
11600 with the least amount of characters. We do not do an exhaustive search
11601 where we would have to check out every combination of every single
11602 possible prefix. Instead we use a heuristic which provides nearly optimal
11603 results in most cases and never is much off. */
11604 saved = XALLOCAVEC (int, ndirs);
11605 savehere = XALLOCAVEC (int, ndirs);
11607 memset (saved, '\0', ndirs * sizeof (saved[0]));
11608 for (i = 0; i < ndirs; i++)
11610 int j;
11611 int total;
11613 /* We can always save some space for the current directory. But this
11614 does not mean it will be enough to justify adding the directory. */
11615 savehere[i] = dirs[i].length;
11616 total = (savehere[i] - saved[i]) * dirs[i].count;
11618 for (j = i + 1; j < ndirs; j++)
11620 savehere[j] = 0;
11621 if (saved[j] < dirs[i].length)
11623 /* Determine whether the dirs[i] path is a prefix of the
11624 dirs[j] path. */
11625 int k;
11627 k = dirs[j].prefix;
11628 while (k != -1 && k != (int) i)
11629 k = dirs[k].prefix;
11631 if (k == (int) i)
11633 /* Yes it is. We can possibly save some memory by
11634 writing the filenames in dirs[j] relative to
11635 dirs[i]. */
11636 savehere[j] = dirs[i].length;
11637 total += (savehere[j] - saved[j]) * dirs[j].count;
11642 /* Check whether we can save enough to justify adding the dirs[i]
11643 directory. */
11644 if (total > dirs[i].length + 1)
11646 /* It's worthwhile adding. */
11647 for (j = i; j < ndirs; j++)
11648 if (savehere[j] > 0)
11650 /* Remember how much we saved for this directory so far. */
11651 saved[j] = savehere[j];
11653 /* Remember the prefix directory. */
11654 dirs[j].dir_idx = i;
11659 /* Emit the directory name table. */
11660 idx_offset = dirs[0].length > 0 ? 1 : 0;
11661 enum dwarf_form str_form = DW_FORM_string;
11662 enum dwarf_form idx_form = DW_FORM_udata;
11663 if (dwarf_version >= 5)
11665 const char *comp_dir = comp_dir_string ();
11666 if (comp_dir == NULL)
11667 comp_dir = "";
11668 dw2_asm_output_data (1, 1, "Directory entry format count");
11669 if (DWARF5_USE_DEBUG_LINE_STR)
11670 str_form = DW_FORM_line_strp;
11671 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11672 dw2_asm_output_data_uleb128 (str_form, "%s",
11673 get_DW_FORM_name (str_form));
11674 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11675 if (str_form == DW_FORM_string)
11677 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11678 for (i = 1 - idx_offset; i < ndirs; i++)
11679 dw2_asm_output_nstring (dirs[i].path,
11680 dirs[i].length
11681 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11682 "Directory Entry: %#x", i + idx_offset);
11684 else
11686 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11687 for (i = 1 - idx_offset; i < ndirs; i++)
11689 const char *str
11690 = ggc_alloc_string (dirs[i].path,
11691 dirs[i].length
11692 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11693 output_line_string (str_form, str, "Directory Entry",
11694 (unsigned) i + idx_offset);
11698 else
11700 for (i = 1 - idx_offset; i < ndirs; i++)
11701 dw2_asm_output_nstring (dirs[i].path,
11702 dirs[i].length
11703 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11704 "Directory Entry: %#x", i + idx_offset);
11706 dw2_asm_output_data (1, 0, "End directory table");
11709 /* We have to emit them in the order of emitted_number since that's
11710 used in the debug info generation. To do this efficiently we
11711 generate a back-mapping of the indices first. */
11712 backmap = XALLOCAVEC (int, numfiles);
11713 for (i = 0; i < numfiles; i++)
11714 backmap[files[i].file_idx->emitted_number - 1] = i;
11716 if (dwarf_version >= 5)
11718 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11719 if (filename0 == NULL)
11720 filename0 = "";
11721 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11722 DW_FORM_data2. Choose one based on the number of directories
11723 and how much space would they occupy in each encoding.
11724 If we have at most 256 directories, all indexes fit into
11725 a single byte, so DW_FORM_data1 is most compact (if there
11726 are at most 128 directories, DW_FORM_udata would be as
11727 compact as that, but not shorter and slower to decode). */
11728 if (ndirs + idx_offset <= 256)
11729 idx_form = DW_FORM_data1;
11730 /* If there are more than 65536 directories, we have to use
11731 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11732 Otherwise, compute what space would occupy if all the indexes
11733 used DW_FORM_udata - sum - and compare that to how large would
11734 be DW_FORM_data2 encoding, and pick the more efficient one. */
11735 else if (ndirs + idx_offset <= 65536)
11737 unsigned HOST_WIDE_INT sum = 1;
11738 for (i = 0; i < numfiles; i++)
11740 int file_idx = backmap[i];
11741 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11742 sum += size_of_uleb128 (dir_idx);
11744 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11745 idx_form = DW_FORM_data2;
11747 #ifdef VMS_DEBUGGING_INFO
11748 dw2_asm_output_data (1, 4, "File name entry format count");
11749 #else
11750 dw2_asm_output_data (1, 2, "File name entry format count");
11751 #endif
11752 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11753 dw2_asm_output_data_uleb128 (str_form, "%s",
11754 get_DW_FORM_name (str_form));
11755 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11756 "DW_LNCT_directory_index");
11757 dw2_asm_output_data_uleb128 (idx_form, "%s",
11758 get_DW_FORM_name (idx_form));
11759 #ifdef VMS_DEBUGGING_INFO
11760 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11761 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11762 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11763 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11764 #endif
11765 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11767 output_line_string (str_form, filename0, "File Entry", 0);
11769 /* Include directory index. */
11770 if (idx_form != DW_FORM_udata)
11771 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11772 0, NULL);
11773 else
11774 dw2_asm_output_data_uleb128 (0, NULL);
11776 #ifdef VMS_DEBUGGING_INFO
11777 dw2_asm_output_data_uleb128 (0, NULL);
11778 dw2_asm_output_data_uleb128 (0, NULL);
11779 #endif
11782 /* Now write all the file names. */
11783 for (i = 0; i < numfiles; i++)
11785 int file_idx = backmap[i];
11786 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11788 #ifdef VMS_DEBUGGING_INFO
11789 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11791 /* Setting these fields can lead to debugger miscomparisons,
11792 but VMS Debug requires them to be set correctly. */
11794 int ver;
11795 long long cdt;
11796 long siz;
11797 int maxfilelen = (strlen (files[file_idx].path)
11798 + dirs[dir_idx].length
11799 + MAX_VMS_VERSION_LEN + 1);
11800 char *filebuf = XALLOCAVEC (char, maxfilelen);
11802 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11803 snprintf (filebuf, maxfilelen, "%s;%d",
11804 files[file_idx].path + dirs[dir_idx].length, ver);
11806 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11808 /* Include directory index. */
11809 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11810 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11811 dir_idx + idx_offset, NULL);
11812 else
11813 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11815 /* Modification time. */
11816 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11817 &cdt, 0, 0, 0) == 0)
11818 ? cdt : 0, NULL);
11820 /* File length in bytes. */
11821 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11822 0, &siz, 0, 0) == 0)
11823 ? siz : 0, NULL);
11824 #else
11825 output_line_string (str_form,
11826 files[file_idx].path + dirs[dir_idx].length,
11827 "File Entry", (unsigned) i + 1);
11829 /* Include directory index. */
11830 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11831 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11832 dir_idx + idx_offset, NULL);
11833 else
11834 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11836 if (dwarf_version >= 5)
11837 continue;
11839 /* Modification time. */
11840 dw2_asm_output_data_uleb128 (0, NULL);
11842 /* File length in bytes. */
11843 dw2_asm_output_data_uleb128 (0, NULL);
11844 #endif /* VMS_DEBUGGING_INFO */
11847 if (dwarf_version < 5)
11848 dw2_asm_output_data (1, 0, "End file name table");
11852 /* Output one line number table into the .debug_line section. */
11854 static void
11855 output_one_line_info_table (dw_line_info_table *table)
11857 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11858 unsigned int current_line = 1;
11859 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11860 dw_line_info_entry *ent;
11861 size_t i;
11863 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11865 switch (ent->opcode)
11867 case LI_set_address:
11868 /* ??? Unfortunately, we have little choice here currently, and
11869 must always use the most general form. GCC does not know the
11870 address delta itself, so we can't use DW_LNS_advance_pc. Many
11871 ports do have length attributes which will give an upper bound
11872 on the address range. We could perhaps use length attributes
11873 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11874 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11876 /* This can handle any delta. This takes
11877 4+DWARF2_ADDR_SIZE bytes. */
11878 dw2_asm_output_data (1, 0, "set address %s", line_label);
11879 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11880 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11881 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11882 break;
11884 case LI_set_line:
11885 if (ent->val == current_line)
11887 /* We still need to start a new row, so output a copy insn. */
11888 dw2_asm_output_data (1, DW_LNS_copy,
11889 "copy line %u", current_line);
11891 else
11893 int line_offset = ent->val - current_line;
11894 int line_delta = line_offset - DWARF_LINE_BASE;
11896 current_line = ent->val;
11897 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11899 /* This can handle deltas from -10 to 234, using the current
11900 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11901 This takes 1 byte. */
11902 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11903 "line %u", current_line);
11905 else
11907 /* This can handle any delta. This takes at least 4 bytes,
11908 depending on the value being encoded. */
11909 dw2_asm_output_data (1, DW_LNS_advance_line,
11910 "advance to line %u", current_line);
11911 dw2_asm_output_data_sleb128 (line_offset, NULL);
11912 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11915 break;
11917 case LI_set_file:
11918 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11919 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11920 break;
11922 case LI_set_column:
11923 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11924 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11925 break;
11927 case LI_negate_stmt:
11928 current_is_stmt = !current_is_stmt;
11929 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11930 "is_stmt %d", current_is_stmt);
11931 break;
11933 case LI_set_prologue_end:
11934 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11935 "set prologue end");
11936 break;
11938 case LI_set_epilogue_begin:
11939 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11940 "set epilogue begin");
11941 break;
11943 case LI_set_discriminator:
11944 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11945 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11946 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11947 dw2_asm_output_data_uleb128 (ent->val, NULL);
11948 break;
11952 /* Emit debug info for the address of the end of the table. */
11953 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11954 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11955 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11956 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11958 dw2_asm_output_data (1, 0, "end sequence");
11959 dw2_asm_output_data_uleb128 (1, NULL);
11960 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11963 /* Output the source line number correspondence information. This
11964 information goes into the .debug_line section. */
11966 static void
11967 output_line_info (bool prologue_only)
11969 static unsigned int generation;
11970 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11971 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11972 bool saw_one = false;
11973 int opc;
11975 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11976 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11977 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11978 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11980 if (!XCOFF_DEBUGGING_INFO)
11982 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11983 dw2_asm_output_data (4, 0xffffffff,
11984 "Initial length escape value indicating 64-bit DWARF extension");
11985 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11986 "Length of Source Line Info");
11989 ASM_OUTPUT_LABEL (asm_out_file, l1);
11991 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11992 if (dwarf_version >= 5)
11994 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11995 dw2_asm_output_data (1, 0, "Segment Size");
11997 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11998 ASM_OUTPUT_LABEL (asm_out_file, p1);
12000 /* Define the architecture-dependent minimum instruction length (in bytes).
12001 In this implementation of DWARF, this field is used for information
12002 purposes only. Since GCC generates assembly language, we have no
12003 a priori knowledge of how many instruction bytes are generated for each
12004 source line, and therefore can use only the DW_LNE_set_address and
12005 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12006 this as '1', which is "correct enough" for all architectures,
12007 and don't let the target override. */
12008 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12010 if (dwarf_version >= 4)
12011 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12012 "Maximum Operations Per Instruction");
12013 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12014 "Default is_stmt_start flag");
12015 dw2_asm_output_data (1, DWARF_LINE_BASE,
12016 "Line Base Value (Special Opcodes)");
12017 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12018 "Line Range Value (Special Opcodes)");
12019 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12020 "Special Opcode Base");
12022 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12024 int n_op_args;
12025 switch (opc)
12027 case DW_LNS_advance_pc:
12028 case DW_LNS_advance_line:
12029 case DW_LNS_set_file:
12030 case DW_LNS_set_column:
12031 case DW_LNS_fixed_advance_pc:
12032 case DW_LNS_set_isa:
12033 n_op_args = 1;
12034 break;
12035 default:
12036 n_op_args = 0;
12037 break;
12040 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12041 opc, n_op_args);
12044 /* Write out the information about the files we use. */
12045 output_file_names ();
12046 ASM_OUTPUT_LABEL (asm_out_file, p2);
12047 if (prologue_only)
12049 /* Output the marker for the end of the line number info. */
12050 ASM_OUTPUT_LABEL (asm_out_file, l2);
12051 return;
12054 if (separate_line_info)
12056 dw_line_info_table *table;
12057 size_t i;
12059 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12060 if (table->in_use)
12062 output_one_line_info_table (table);
12063 saw_one = true;
12066 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12068 output_one_line_info_table (cold_text_section_line_info);
12069 saw_one = true;
12072 /* ??? Some Darwin linkers crash on a .debug_line section with no
12073 sequences. Further, merely a DW_LNE_end_sequence entry is not
12074 sufficient -- the address column must also be initialized.
12075 Make sure to output at least one set_address/end_sequence pair,
12076 choosing .text since that section is always present. */
12077 if (text_section_line_info->in_use || !saw_one)
12078 output_one_line_info_table (text_section_line_info);
12080 /* Output the marker for the end of the line number info. */
12081 ASM_OUTPUT_LABEL (asm_out_file, l2);
12084 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12086 static inline bool
12087 need_endianity_attribute_p (bool reverse)
12089 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12092 /* Given a pointer to a tree node for some base type, return a pointer to
12093 a DIE that describes the given type. REVERSE is true if the type is
12094 to be interpreted in the reverse storage order wrt the target order.
12096 This routine must only be called for GCC type nodes that correspond to
12097 Dwarf base (fundamental) types. */
12099 static dw_die_ref
12100 base_type_die (tree type, bool reverse)
12102 dw_die_ref base_type_result;
12103 enum dwarf_type encoding;
12104 bool fpt_used = false;
12105 struct fixed_point_type_info fpt_info;
12106 tree type_bias = NULL_TREE;
12108 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12109 return 0;
12111 /* If this is a subtype that should not be emitted as a subrange type,
12112 use the base type. See subrange_type_for_debug_p. */
12113 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12114 type = TREE_TYPE (type);
12116 switch (TREE_CODE (type))
12118 case INTEGER_TYPE:
12119 if ((dwarf_version >= 4 || !dwarf_strict)
12120 && TYPE_NAME (type)
12121 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12122 && DECL_IS_BUILTIN (TYPE_NAME (type))
12123 && DECL_NAME (TYPE_NAME (type)))
12125 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12126 if (strcmp (name, "char16_t") == 0
12127 || strcmp (name, "char32_t") == 0)
12129 encoding = DW_ATE_UTF;
12130 break;
12133 if ((dwarf_version >= 3 || !dwarf_strict)
12134 && lang_hooks.types.get_fixed_point_type_info)
12136 memset (&fpt_info, 0, sizeof (fpt_info));
12137 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12139 fpt_used = true;
12140 encoding = ((TYPE_UNSIGNED (type))
12141 ? DW_ATE_unsigned_fixed
12142 : DW_ATE_signed_fixed);
12143 break;
12146 if (TYPE_STRING_FLAG (type))
12148 if (TYPE_UNSIGNED (type))
12149 encoding = DW_ATE_unsigned_char;
12150 else
12151 encoding = DW_ATE_signed_char;
12153 else if (TYPE_UNSIGNED (type))
12154 encoding = DW_ATE_unsigned;
12155 else
12156 encoding = DW_ATE_signed;
12158 if (!dwarf_strict
12159 && lang_hooks.types.get_type_bias)
12160 type_bias = lang_hooks.types.get_type_bias (type);
12161 break;
12163 case REAL_TYPE:
12164 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12166 if (dwarf_version >= 3 || !dwarf_strict)
12167 encoding = DW_ATE_decimal_float;
12168 else
12169 encoding = DW_ATE_lo_user;
12171 else
12172 encoding = DW_ATE_float;
12173 break;
12175 case FIXED_POINT_TYPE:
12176 if (!(dwarf_version >= 3 || !dwarf_strict))
12177 encoding = DW_ATE_lo_user;
12178 else if (TYPE_UNSIGNED (type))
12179 encoding = DW_ATE_unsigned_fixed;
12180 else
12181 encoding = DW_ATE_signed_fixed;
12182 break;
12184 /* Dwarf2 doesn't know anything about complex ints, so use
12185 a user defined type for it. */
12186 case COMPLEX_TYPE:
12187 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12188 encoding = DW_ATE_complex_float;
12189 else
12190 encoding = DW_ATE_lo_user;
12191 break;
12193 case BOOLEAN_TYPE:
12194 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12195 encoding = DW_ATE_boolean;
12196 break;
12198 default:
12199 /* No other TREE_CODEs are Dwarf fundamental types. */
12200 gcc_unreachable ();
12203 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12205 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12206 int_size_in_bytes (type));
12207 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12209 if (need_endianity_attribute_p (reverse))
12210 add_AT_unsigned (base_type_result, DW_AT_endianity,
12211 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12213 add_alignment_attribute (base_type_result, type);
12215 if (fpt_used)
12217 switch (fpt_info.scale_factor_kind)
12219 case fixed_point_scale_factor_binary:
12220 add_AT_int (base_type_result, DW_AT_binary_scale,
12221 fpt_info.scale_factor.binary);
12222 break;
12224 case fixed_point_scale_factor_decimal:
12225 add_AT_int (base_type_result, DW_AT_decimal_scale,
12226 fpt_info.scale_factor.decimal);
12227 break;
12229 case fixed_point_scale_factor_arbitrary:
12230 /* Arbitrary scale factors cannot be described in standard DWARF,
12231 yet. */
12232 if (!dwarf_strict)
12234 /* Describe the scale factor as a rational constant. */
12235 const dw_die_ref scale_factor
12236 = new_die (DW_TAG_constant, comp_unit_die (), type);
12238 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12239 fpt_info.scale_factor.arbitrary.numerator);
12240 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12241 fpt_info.scale_factor.arbitrary.denominator);
12243 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12245 break;
12247 default:
12248 gcc_unreachable ();
12252 if (type_bias)
12253 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12254 dw_scalar_form_constant
12255 | dw_scalar_form_exprloc
12256 | dw_scalar_form_reference,
12257 NULL);
12259 add_pubtype (type, base_type_result);
12261 return base_type_result;
12264 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12265 named 'auto' in its type: return true for it, false otherwise. */
12267 static inline bool
12268 is_cxx_auto (tree type)
12270 if (is_cxx ())
12272 tree name = TYPE_IDENTIFIER (type);
12273 if (name == get_identifier ("auto")
12274 || name == get_identifier ("decltype(auto)"))
12275 return true;
12277 return false;
12280 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12281 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12283 static inline int
12284 is_base_type (tree type)
12286 switch (TREE_CODE (type))
12288 case ERROR_MARK:
12289 case VOID_TYPE:
12290 case INTEGER_TYPE:
12291 case REAL_TYPE:
12292 case FIXED_POINT_TYPE:
12293 case COMPLEX_TYPE:
12294 case BOOLEAN_TYPE:
12295 case POINTER_BOUNDS_TYPE:
12296 return 1;
12298 case ARRAY_TYPE:
12299 case RECORD_TYPE:
12300 case UNION_TYPE:
12301 case QUAL_UNION_TYPE:
12302 case ENUMERAL_TYPE:
12303 case FUNCTION_TYPE:
12304 case METHOD_TYPE:
12305 case POINTER_TYPE:
12306 case REFERENCE_TYPE:
12307 case NULLPTR_TYPE:
12308 case OFFSET_TYPE:
12309 case LANG_TYPE:
12310 case VECTOR_TYPE:
12311 return 0;
12313 default:
12314 if (is_cxx_auto (type))
12315 return 0;
12316 gcc_unreachable ();
12319 return 0;
12322 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12323 node, return the size in bits for the type if it is a constant, or else
12324 return the alignment for the type if the type's size is not constant, or
12325 else return BITS_PER_WORD if the type actually turns out to be an
12326 ERROR_MARK node. */
12328 static inline unsigned HOST_WIDE_INT
12329 simple_type_size_in_bits (const_tree type)
12331 if (TREE_CODE (type) == ERROR_MARK)
12332 return BITS_PER_WORD;
12333 else if (TYPE_SIZE (type) == NULL_TREE)
12334 return 0;
12335 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12336 return tree_to_uhwi (TYPE_SIZE (type));
12337 else
12338 return TYPE_ALIGN (type);
12341 /* Similarly, but return an offset_int instead of UHWI. */
12343 static inline offset_int
12344 offset_int_type_size_in_bits (const_tree type)
12346 if (TREE_CODE (type) == ERROR_MARK)
12347 return BITS_PER_WORD;
12348 else if (TYPE_SIZE (type) == NULL_TREE)
12349 return 0;
12350 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12351 return wi::to_offset (TYPE_SIZE (type));
12352 else
12353 return TYPE_ALIGN (type);
12356 /* Given a pointer to a tree node for a subrange type, return a pointer
12357 to a DIE that describes the given type. */
12359 static dw_die_ref
12360 subrange_type_die (tree type, tree low, tree high, tree bias,
12361 dw_die_ref context_die)
12363 dw_die_ref subrange_die;
12364 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12366 if (context_die == NULL)
12367 context_die = comp_unit_die ();
12369 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12371 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12373 /* The size of the subrange type and its base type do not match,
12374 so we need to generate a size attribute for the subrange type. */
12375 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12378 add_alignment_attribute (subrange_die, type);
12380 if (low)
12381 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12382 if (high)
12383 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12384 if (bias && !dwarf_strict)
12385 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12386 dw_scalar_form_constant
12387 | dw_scalar_form_exprloc
12388 | dw_scalar_form_reference,
12389 NULL);
12391 return subrange_die;
12394 /* Returns the (const and/or volatile) cv_qualifiers associated with
12395 the decl node. This will normally be augmented with the
12396 cv_qualifiers of the underlying type in add_type_attribute. */
12398 static int
12399 decl_quals (const_tree decl)
12401 return ((TREE_READONLY (decl)
12402 /* The C++ front-end correctly marks reference-typed
12403 variables as readonly, but from a language (and debug
12404 info) standpoint they are not const-qualified. */
12405 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12406 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12407 | (TREE_THIS_VOLATILE (decl)
12408 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12411 /* Determine the TYPE whose qualifiers match the largest strict subset
12412 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12413 qualifiers outside QUAL_MASK. */
12415 static int
12416 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12418 tree t;
12419 int best_rank = 0, best_qual = 0, max_rank;
12421 type_quals &= qual_mask;
12422 max_rank = popcount_hwi (type_quals) - 1;
12424 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12425 t = TYPE_NEXT_VARIANT (t))
12427 int q = TYPE_QUALS (t) & qual_mask;
12429 if ((q & type_quals) == q && q != type_quals
12430 && check_base_type (t, type))
12432 int rank = popcount_hwi (q);
12434 if (rank > best_rank)
12436 best_rank = rank;
12437 best_qual = q;
12442 return best_qual;
12445 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12446 static const dwarf_qual_info_t dwarf_qual_info[] =
12448 { TYPE_QUAL_CONST, DW_TAG_const_type },
12449 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12450 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12451 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12453 static const unsigned int dwarf_qual_info_size
12454 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12456 /* If DIE is a qualified DIE of some base DIE with the same parent,
12457 return the base DIE, otherwise return NULL. Set MASK to the
12458 qualifiers added compared to the returned DIE. */
12460 static dw_die_ref
12461 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12463 unsigned int i;
12464 for (i = 0; i < dwarf_qual_info_size; i++)
12465 if (die->die_tag == dwarf_qual_info[i].t)
12466 break;
12467 if (i == dwarf_qual_info_size)
12468 return NULL;
12469 if (vec_safe_length (die->die_attr) != 1)
12470 return NULL;
12471 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12472 if (type == NULL || type->die_parent != die->die_parent)
12473 return NULL;
12474 *mask |= dwarf_qual_info[i].q;
12475 if (depth)
12477 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12478 if (ret)
12479 return ret;
12481 return type;
12484 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12485 entry that chains the modifiers specified by CV_QUALS in front of the
12486 given type. REVERSE is true if the type is to be interpreted in the
12487 reverse storage order wrt the target order. */
12489 static dw_die_ref
12490 modified_type_die (tree type, int cv_quals, bool reverse,
12491 dw_die_ref context_die)
12493 enum tree_code code = TREE_CODE (type);
12494 dw_die_ref mod_type_die;
12495 dw_die_ref sub_die = NULL;
12496 tree item_type = NULL;
12497 tree qualified_type;
12498 tree name, low, high;
12499 dw_die_ref mod_scope;
12500 /* Only these cv-qualifiers are currently handled. */
12501 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12502 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12504 if (code == ERROR_MARK)
12505 return NULL;
12507 if (lang_hooks.types.get_debug_type)
12509 tree debug_type = lang_hooks.types.get_debug_type (type);
12511 if (debug_type != NULL_TREE && debug_type != type)
12512 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12515 cv_quals &= cv_qual_mask;
12517 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12518 tag modifier (and not an attribute) old consumers won't be able
12519 to handle it. */
12520 if (dwarf_version < 3)
12521 cv_quals &= ~TYPE_QUAL_RESTRICT;
12523 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12524 if (dwarf_version < 5)
12525 cv_quals &= ~TYPE_QUAL_ATOMIC;
12527 /* See if we already have the appropriately qualified variant of
12528 this type. */
12529 qualified_type = get_qualified_type (type, cv_quals);
12531 if (qualified_type == sizetype)
12533 /* Try not to expose the internal sizetype type's name. */
12534 if (TYPE_NAME (qualified_type)
12535 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12537 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12539 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12540 && (TYPE_PRECISION (t)
12541 == TYPE_PRECISION (qualified_type))
12542 && (TYPE_UNSIGNED (t)
12543 == TYPE_UNSIGNED (qualified_type)));
12544 qualified_type = t;
12546 else if (qualified_type == sizetype
12547 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12548 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12549 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12550 qualified_type = size_type_node;
12554 /* If we do, then we can just use its DIE, if it exists. */
12555 if (qualified_type)
12557 mod_type_die = lookup_type_die (qualified_type);
12559 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12560 if (mod_type_die
12561 && (!need_endianity_attribute_p (reverse)
12562 || !is_base_type (type)
12563 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12564 return mod_type_die;
12567 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12569 /* Handle C typedef types. */
12570 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12571 && !DECL_ARTIFICIAL (name))
12573 tree dtype = TREE_TYPE (name);
12575 if (qualified_type == dtype)
12577 tree origin = decl_ultimate_origin (name);
12579 /* Typedef variants that have an abstract origin don't get their own
12580 type DIE (see gen_typedef_die), so fall back on the ultimate
12581 abstract origin instead. */
12582 if (origin != NULL && origin != name)
12583 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12584 context_die);
12586 /* For a named type, use the typedef. */
12587 gen_type_die (qualified_type, context_die);
12588 return lookup_type_die (qualified_type);
12590 else
12592 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12593 dquals &= cv_qual_mask;
12594 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12595 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12596 /* cv-unqualified version of named type. Just use
12597 the unnamed type to which it refers. */
12598 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12599 reverse, context_die);
12600 /* Else cv-qualified version of named type; fall through. */
12604 mod_scope = scope_die_for (type, context_die);
12606 if (cv_quals)
12608 int sub_quals = 0, first_quals = 0;
12609 unsigned i;
12610 dw_die_ref first = NULL, last = NULL;
12612 /* Determine a lesser qualified type that most closely matches
12613 this one. Then generate DW_TAG_* entries for the remaining
12614 qualifiers. */
12615 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12616 cv_qual_mask);
12617 if (sub_quals && use_debug_types)
12619 bool needed = false;
12620 /* If emitting type units, make sure the order of qualifiers
12621 is canonical. Thus, start from unqualified type if
12622 an earlier qualifier is missing in sub_quals, but some later
12623 one is present there. */
12624 for (i = 0; i < dwarf_qual_info_size; i++)
12625 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12626 needed = true;
12627 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12629 sub_quals = 0;
12630 break;
12633 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12634 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12636 /* As not all intermediate qualified DIEs have corresponding
12637 tree types, ensure that qualified DIEs in the same scope
12638 as their DW_AT_type are emitted after their DW_AT_type,
12639 only with other qualified DIEs for the same type possibly
12640 in between them. Determine the range of such qualified
12641 DIEs now (first being the base type, last being corresponding
12642 last qualified DIE for it). */
12643 unsigned int count = 0;
12644 first = qualified_die_p (mod_type_die, &first_quals,
12645 dwarf_qual_info_size);
12646 if (first == NULL)
12647 first = mod_type_die;
12648 gcc_assert ((first_quals & ~sub_quals) == 0);
12649 for (count = 0, last = first;
12650 count < (1U << dwarf_qual_info_size);
12651 count++, last = last->die_sib)
12653 int quals = 0;
12654 if (last == mod_scope->die_child)
12655 break;
12656 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12657 != first)
12658 break;
12662 for (i = 0; i < dwarf_qual_info_size; i++)
12663 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12665 dw_die_ref d;
12666 if (first && first != last)
12668 for (d = first->die_sib; ; d = d->die_sib)
12670 int quals = 0;
12671 qualified_die_p (d, &quals, dwarf_qual_info_size);
12672 if (quals == (first_quals | dwarf_qual_info[i].q))
12673 break;
12674 if (d == last)
12676 d = NULL;
12677 break;
12680 if (d)
12682 mod_type_die = d;
12683 continue;
12686 if (first)
12688 d = ggc_cleared_alloc<die_node> ();
12689 d->die_tag = dwarf_qual_info[i].t;
12690 add_child_die_after (mod_scope, d, last);
12691 last = d;
12693 else
12694 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12695 if (mod_type_die)
12696 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12697 mod_type_die = d;
12698 first_quals |= dwarf_qual_info[i].q;
12701 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12703 dwarf_tag tag = DW_TAG_pointer_type;
12704 if (code == REFERENCE_TYPE)
12706 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12707 tag = DW_TAG_rvalue_reference_type;
12708 else
12709 tag = DW_TAG_reference_type;
12711 mod_type_die = new_die (tag, mod_scope, type);
12713 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12714 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12715 add_alignment_attribute (mod_type_die, type);
12716 item_type = TREE_TYPE (type);
12718 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12719 if (!ADDR_SPACE_GENERIC_P (as))
12721 int action = targetm.addr_space.debug (as);
12722 if (action >= 0)
12724 /* Positive values indicate an address_class. */
12725 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12727 else
12729 /* Negative values indicate an (inverted) segment base reg. */
12730 dw_loc_descr_ref d
12731 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12732 add_AT_loc (mod_type_die, DW_AT_segment, d);
12736 else if (code == INTEGER_TYPE
12737 && TREE_TYPE (type) != NULL_TREE
12738 && subrange_type_for_debug_p (type, &low, &high))
12740 tree bias = NULL_TREE;
12741 if (lang_hooks.types.get_type_bias)
12742 bias = lang_hooks.types.get_type_bias (type);
12743 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12744 item_type = TREE_TYPE (type);
12746 else if (is_base_type (type))
12747 mod_type_die = base_type_die (type, reverse);
12748 else
12750 gen_type_die (type, context_die);
12752 /* We have to get the type_main_variant here (and pass that to the
12753 `lookup_type_die' routine) because the ..._TYPE node we have
12754 might simply be a *copy* of some original type node (where the
12755 copy was created to help us keep track of typedef names) and
12756 that copy might have a different TYPE_UID from the original
12757 ..._TYPE node. */
12758 if (TREE_CODE (type) == FUNCTION_TYPE
12759 || TREE_CODE (type) == METHOD_TYPE)
12761 /* For function/method types, can't just use type_main_variant here,
12762 because that can have different ref-qualifiers for C++,
12763 but try to canonicalize. */
12764 tree main = TYPE_MAIN_VARIANT (type);
12765 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12766 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12767 && check_base_type (t, main)
12768 && check_lang_type (t, type))
12769 return lookup_type_die (t);
12770 return lookup_type_die (type);
12772 else if (TREE_CODE (type) != VECTOR_TYPE
12773 && TREE_CODE (type) != ARRAY_TYPE)
12774 return lookup_type_die (type_main_variant (type));
12775 else
12776 /* Vectors have the debugging information in the type,
12777 not the main variant. */
12778 return lookup_type_die (type);
12781 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12782 don't output a DW_TAG_typedef, since there isn't one in the
12783 user's program; just attach a DW_AT_name to the type.
12784 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12785 if the base type already has the same name. */
12786 if (name
12787 && ((TREE_CODE (name) != TYPE_DECL
12788 && (qualified_type == TYPE_MAIN_VARIANT (type)
12789 || (cv_quals == TYPE_UNQUALIFIED)))
12790 || (TREE_CODE (name) == TYPE_DECL
12791 && TREE_TYPE (name) == qualified_type
12792 && DECL_NAME (name))))
12794 if (TREE_CODE (name) == TYPE_DECL)
12795 /* Could just call add_name_and_src_coords_attributes here,
12796 but since this is a builtin type it doesn't have any
12797 useful source coordinates anyway. */
12798 name = DECL_NAME (name);
12799 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12801 /* This probably indicates a bug. */
12802 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12804 name = TYPE_IDENTIFIER (type);
12805 add_name_attribute (mod_type_die,
12806 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12809 if (qualified_type)
12810 equate_type_number_to_die (qualified_type, mod_type_die);
12812 if (item_type)
12813 /* We must do this after the equate_type_number_to_die call, in case
12814 this is a recursive type. This ensures that the modified_type_die
12815 recursion will terminate even if the type is recursive. Recursive
12816 types are possible in Ada. */
12817 sub_die = modified_type_die (item_type,
12818 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12819 reverse,
12820 context_die);
12822 if (sub_die != NULL)
12823 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12825 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12826 if (TYPE_ARTIFICIAL (type))
12827 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12829 return mod_type_die;
12832 /* Generate DIEs for the generic parameters of T.
12833 T must be either a generic type or a generic function.
12834 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12836 static void
12837 gen_generic_params_dies (tree t)
12839 tree parms, args;
12840 int parms_num, i;
12841 dw_die_ref die = NULL;
12842 int non_default;
12844 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12845 return;
12847 if (TYPE_P (t))
12848 die = lookup_type_die (t);
12849 else if (DECL_P (t))
12850 die = lookup_decl_die (t);
12852 gcc_assert (die);
12854 parms = lang_hooks.get_innermost_generic_parms (t);
12855 if (!parms)
12856 /* T has no generic parameter. It means T is neither a generic type
12857 or function. End of story. */
12858 return;
12860 parms_num = TREE_VEC_LENGTH (parms);
12861 args = lang_hooks.get_innermost_generic_args (t);
12862 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12863 non_default = int_cst_value (TREE_CHAIN (args));
12864 else
12865 non_default = TREE_VEC_LENGTH (args);
12866 for (i = 0; i < parms_num; i++)
12868 tree parm, arg, arg_pack_elems;
12869 dw_die_ref parm_die;
12871 parm = TREE_VEC_ELT (parms, i);
12872 arg = TREE_VEC_ELT (args, i);
12873 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12874 gcc_assert (parm && TREE_VALUE (parm) && arg);
12876 if (parm && TREE_VALUE (parm) && arg)
12878 /* If PARM represents a template parameter pack,
12879 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12880 by DW_TAG_template_*_parameter DIEs for the argument
12881 pack elements of ARG. Note that ARG would then be
12882 an argument pack. */
12883 if (arg_pack_elems)
12884 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12885 arg_pack_elems,
12886 die);
12887 else
12888 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12889 true /* emit name */, die);
12890 if (i >= non_default)
12891 add_AT_flag (parm_die, DW_AT_default_value, 1);
12896 /* Create and return a DIE for PARM which should be
12897 the representation of a generic type parameter.
12898 For instance, in the C++ front end, PARM would be a template parameter.
12899 ARG is the argument to PARM.
12900 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12901 name of the PARM.
12902 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12903 as a child node. */
12905 static dw_die_ref
12906 generic_parameter_die (tree parm, tree arg,
12907 bool emit_name_p,
12908 dw_die_ref parent_die)
12910 dw_die_ref tmpl_die = NULL;
12911 const char *name = NULL;
12913 if (!parm || !DECL_NAME (parm) || !arg)
12914 return NULL;
12916 /* We support non-type generic parameters and arguments,
12917 type generic parameters and arguments, as well as
12918 generic generic parameters (a.k.a. template template parameters in C++)
12919 and arguments. */
12920 if (TREE_CODE (parm) == PARM_DECL)
12921 /* PARM is a nontype generic parameter */
12922 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12923 else if (TREE_CODE (parm) == TYPE_DECL)
12924 /* PARM is a type generic parameter. */
12925 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12926 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12927 /* PARM is a generic generic parameter.
12928 Its DIE is a GNU extension. It shall have a
12929 DW_AT_name attribute to represent the name of the template template
12930 parameter, and a DW_AT_GNU_template_name attribute to represent the
12931 name of the template template argument. */
12932 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12933 parent_die, parm);
12934 else
12935 gcc_unreachable ();
12937 if (tmpl_die)
12939 tree tmpl_type;
12941 /* If PARM is a generic parameter pack, it means we are
12942 emitting debug info for a template argument pack element.
12943 In other terms, ARG is a template argument pack element.
12944 In that case, we don't emit any DW_AT_name attribute for
12945 the die. */
12946 if (emit_name_p)
12948 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12949 gcc_assert (name);
12950 add_AT_string (tmpl_die, DW_AT_name, name);
12953 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12955 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12956 TMPL_DIE should have a child DW_AT_type attribute that is set
12957 to the type of the argument to PARM, which is ARG.
12958 If PARM is a type generic parameter, TMPL_DIE should have a
12959 child DW_AT_type that is set to ARG. */
12960 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12961 add_type_attribute (tmpl_die, tmpl_type,
12962 (TREE_THIS_VOLATILE (tmpl_type)
12963 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12964 false, parent_die);
12966 else
12968 /* So TMPL_DIE is a DIE representing a
12969 a generic generic template parameter, a.k.a template template
12970 parameter in C++ and arg is a template. */
12972 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12973 to the name of the argument. */
12974 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12975 if (name)
12976 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12979 if (TREE_CODE (parm) == PARM_DECL)
12980 /* So PARM is a non-type generic parameter.
12981 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12982 attribute of TMPL_DIE which value represents the value
12983 of ARG.
12984 We must be careful here:
12985 The value of ARG might reference some function decls.
12986 We might currently be emitting debug info for a generic
12987 type and types are emitted before function decls, we don't
12988 know if the function decls referenced by ARG will actually be
12989 emitted after cgraph computations.
12990 So must defer the generation of the DW_AT_const_value to
12991 after cgraph is ready. */
12992 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12995 return tmpl_die;
12998 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
12999 PARM_PACK must be a template parameter pack. The returned DIE
13000 will be child DIE of PARENT_DIE. */
13002 static dw_die_ref
13003 template_parameter_pack_die (tree parm_pack,
13004 tree parm_pack_args,
13005 dw_die_ref parent_die)
13007 dw_die_ref die;
13008 int j;
13010 gcc_assert (parent_die && parm_pack);
13012 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13013 add_name_and_src_coords_attributes (die, parm_pack);
13014 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13015 generic_parameter_die (parm_pack,
13016 TREE_VEC_ELT (parm_pack_args, j),
13017 false /* Don't emit DW_AT_name */,
13018 die);
13019 return die;
13022 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13023 an enumerated type. */
13025 static inline int
13026 type_is_enum (const_tree type)
13028 return TREE_CODE (type) == ENUMERAL_TYPE;
13031 /* Return the DBX register number described by a given RTL node. */
13033 static unsigned int
13034 dbx_reg_number (const_rtx rtl)
13036 unsigned regno = REGNO (rtl);
13038 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13040 #ifdef LEAF_REG_REMAP
13041 if (crtl->uses_only_leaf_regs)
13043 int leaf_reg = LEAF_REG_REMAP (regno);
13044 if (leaf_reg != -1)
13045 regno = (unsigned) leaf_reg;
13047 #endif
13049 regno = DBX_REGISTER_NUMBER (regno);
13050 gcc_assert (regno != INVALID_REGNUM);
13051 return regno;
13054 /* Optionally add a DW_OP_piece term to a location description expression.
13055 DW_OP_piece is only added if the location description expression already
13056 doesn't end with DW_OP_piece. */
13058 static void
13059 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13061 dw_loc_descr_ref loc;
13063 if (*list_head != NULL)
13065 /* Find the end of the chain. */
13066 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13069 if (loc->dw_loc_opc != DW_OP_piece)
13070 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13074 /* Return a location descriptor that designates a machine register or
13075 zero if there is none. */
13077 static dw_loc_descr_ref
13078 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13080 rtx regs;
13082 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13083 return 0;
13085 /* We only use "frame base" when we're sure we're talking about the
13086 post-prologue local stack frame. We do this by *not* running
13087 register elimination until this point, and recognizing the special
13088 argument pointer and soft frame pointer rtx's.
13089 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13090 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13091 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13093 dw_loc_descr_ref result = NULL;
13095 if (dwarf_version >= 4 || !dwarf_strict)
13097 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13098 initialized);
13099 if (result)
13100 add_loc_descr (&result,
13101 new_loc_descr (DW_OP_stack_value, 0, 0));
13103 return result;
13106 regs = targetm.dwarf_register_span (rtl);
13108 if (REG_NREGS (rtl) > 1 || regs)
13109 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13110 else
13112 unsigned int dbx_regnum = dbx_reg_number (rtl);
13113 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13114 return 0;
13115 return one_reg_loc_descriptor (dbx_regnum, initialized);
13119 /* Return a location descriptor that designates a machine register for
13120 a given hard register number. */
13122 static dw_loc_descr_ref
13123 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13125 dw_loc_descr_ref reg_loc_descr;
13127 if (regno <= 31)
13128 reg_loc_descr
13129 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13130 else
13131 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13133 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13134 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13136 return reg_loc_descr;
13139 /* Given an RTL of a register, return a location descriptor that
13140 designates a value that spans more than one register. */
13142 static dw_loc_descr_ref
13143 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13144 enum var_init_status initialized)
13146 int size, i;
13147 dw_loc_descr_ref loc_result = NULL;
13149 /* Simple, contiguous registers. */
13150 if (regs == NULL_RTX)
13152 unsigned reg = REGNO (rtl);
13153 int nregs;
13155 #ifdef LEAF_REG_REMAP
13156 if (crtl->uses_only_leaf_regs)
13158 int leaf_reg = LEAF_REG_REMAP (reg);
13159 if (leaf_reg != -1)
13160 reg = (unsigned) leaf_reg;
13162 #endif
13164 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13165 nregs = REG_NREGS (rtl);
13167 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13169 loc_result = NULL;
13170 while (nregs--)
13172 dw_loc_descr_ref t;
13174 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13175 VAR_INIT_STATUS_INITIALIZED);
13176 add_loc_descr (&loc_result, t);
13177 add_loc_descr_op_piece (&loc_result, size);
13178 ++reg;
13180 return loc_result;
13183 /* Now onto stupid register sets in non contiguous locations. */
13185 gcc_assert (GET_CODE (regs) == PARALLEL);
13187 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13188 loc_result = NULL;
13190 for (i = 0; i < XVECLEN (regs, 0); ++i)
13192 dw_loc_descr_ref t;
13194 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13195 VAR_INIT_STATUS_INITIALIZED);
13196 add_loc_descr (&loc_result, t);
13197 add_loc_descr_op_piece (&loc_result, size);
13200 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13201 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13202 return loc_result;
13205 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13207 /* Return a location descriptor that designates a constant i,
13208 as a compound operation from constant (i >> shift), constant shift
13209 and DW_OP_shl. */
13211 static dw_loc_descr_ref
13212 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13214 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13215 add_loc_descr (&ret, int_loc_descriptor (shift));
13216 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13217 return ret;
13220 /* Return a location descriptor that designates a constant. */
13222 static dw_loc_descr_ref
13223 int_loc_descriptor (HOST_WIDE_INT i)
13225 enum dwarf_location_atom op;
13227 /* Pick the smallest representation of a constant, rather than just
13228 defaulting to the LEB encoding. */
13229 if (i >= 0)
13231 int clz = clz_hwi (i);
13232 int ctz = ctz_hwi (i);
13233 if (i <= 31)
13234 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13235 else if (i <= 0xff)
13236 op = DW_OP_const1u;
13237 else if (i <= 0xffff)
13238 op = DW_OP_const2u;
13239 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13240 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13241 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13242 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13243 while DW_OP_const4u is 5 bytes. */
13244 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13245 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13246 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13247 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13248 while DW_OP_const4u is 5 bytes. */
13249 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13251 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13252 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13253 <= 4)
13255 /* As i >= 2**31, the double cast above will yield a negative number.
13256 Since wrapping is defined in DWARF expressions we can output big
13257 positive integers as small negative ones, regardless of the size
13258 of host wide ints.
13260 Here, since the evaluator will handle 32-bit values and since i >=
13261 2**31, we know it's going to be interpreted as a negative literal:
13262 store it this way if we can do better than 5 bytes this way. */
13263 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13265 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13266 op = DW_OP_const4u;
13268 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13269 least 6 bytes: see if we can do better before falling back to it. */
13270 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13271 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13272 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13273 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13274 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13275 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13276 >= HOST_BITS_PER_WIDE_INT)
13277 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13278 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13279 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13280 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13281 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13282 && size_of_uleb128 (i) > 6)
13283 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13284 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13285 else
13286 op = DW_OP_constu;
13288 else
13290 if (i >= -0x80)
13291 op = DW_OP_const1s;
13292 else if (i >= -0x8000)
13293 op = DW_OP_const2s;
13294 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13296 if (size_of_int_loc_descriptor (i) < 5)
13298 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13299 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13300 return ret;
13302 op = DW_OP_const4s;
13304 else
13306 if (size_of_int_loc_descriptor (i)
13307 < (unsigned long) 1 + size_of_sleb128 (i))
13309 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13310 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13311 return ret;
13313 op = DW_OP_consts;
13317 return new_loc_descr (op, i, 0);
13320 /* Likewise, for unsigned constants. */
13322 static dw_loc_descr_ref
13323 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13325 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13326 const unsigned HOST_WIDE_INT max_uint
13327 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13329 /* If possible, use the clever signed constants handling. */
13330 if (i <= max_int)
13331 return int_loc_descriptor ((HOST_WIDE_INT) i);
13333 /* Here, we are left with positive numbers that cannot be represented as
13334 HOST_WIDE_INT, i.e.:
13335 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13337 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13338 whereas may be better to output a negative integer: thanks to integer
13339 wrapping, we know that:
13340 x = x - 2 ** DWARF2_ADDR_SIZE
13341 = x - 2 * (max (HOST_WIDE_INT) + 1)
13342 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13343 small negative integers. Let's try that in cases it will clearly improve
13344 the encoding: there is no gain turning DW_OP_const4u into
13345 DW_OP_const4s. */
13346 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13347 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13348 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13350 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13352 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13353 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13354 const HOST_WIDE_INT second_shift
13355 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13357 /* So we finally have:
13358 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13359 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13360 return int_loc_descriptor (second_shift);
13363 /* Last chance: fallback to a simple constant operation. */
13364 return new_loc_descr
13365 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13366 ? DW_OP_const4u
13367 : DW_OP_const8u,
13368 i, 0);
13371 /* Generate and return a location description that computes the unsigned
13372 comparison of the two stack top entries (a OP b where b is the top-most
13373 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13374 LE_EXPR, GT_EXPR or GE_EXPR. */
13376 static dw_loc_descr_ref
13377 uint_comparison_loc_list (enum tree_code kind)
13379 enum dwarf_location_atom op, flip_op;
13380 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13382 switch (kind)
13384 case LT_EXPR:
13385 op = DW_OP_lt;
13386 break;
13387 case LE_EXPR:
13388 op = DW_OP_le;
13389 break;
13390 case GT_EXPR:
13391 op = DW_OP_gt;
13392 break;
13393 case GE_EXPR:
13394 op = DW_OP_ge;
13395 break;
13396 default:
13397 gcc_unreachable ();
13400 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13401 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13403 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13404 possible to perform unsigned comparisons: we just have to distinguish
13405 three cases:
13407 1. when a and b have the same sign (as signed integers); then we should
13408 return: a OP(signed) b;
13410 2. when a is a negative signed integer while b is a positive one, then a
13411 is a greater unsigned integer than b; likewise when a and b's roles
13412 are flipped.
13414 So first, compare the sign of the two operands. */
13415 ret = new_loc_descr (DW_OP_over, 0, 0);
13416 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13417 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13418 /* If they have different signs (i.e. they have different sign bits), then
13419 the stack top value has now the sign bit set and thus it's smaller than
13420 zero. */
13421 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13422 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13423 add_loc_descr (&ret, bra_node);
13425 /* We are in case 1. At this point, we know both operands have the same
13426 sign, to it's safe to use the built-in signed comparison. */
13427 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13428 add_loc_descr (&ret, jmp_node);
13430 /* We are in case 2. Here, we know both operands do not have the same sign,
13431 so we have to flip the signed comparison. */
13432 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13433 tmp = new_loc_descr (flip_op, 0, 0);
13434 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13435 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13436 add_loc_descr (&ret, tmp);
13438 /* This dummy operation is necessary to make the two branches join. */
13439 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13440 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13441 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13442 add_loc_descr (&ret, tmp);
13444 return ret;
13447 /* Likewise, but takes the location description lists (might be destructive on
13448 them). Return NULL if either is NULL or if concatenation fails. */
13450 static dw_loc_list_ref
13451 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13452 enum tree_code kind)
13454 if (left == NULL || right == NULL)
13455 return NULL;
13457 add_loc_list (&left, right);
13458 if (left == NULL)
13459 return NULL;
13461 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13462 return left;
13465 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13466 without actually allocating it. */
13468 static unsigned long
13469 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13471 return size_of_int_loc_descriptor (i >> shift)
13472 + size_of_int_loc_descriptor (shift)
13473 + 1;
13476 /* Return size_of_locs (int_loc_descriptor (i)) without
13477 actually allocating it. */
13479 static unsigned long
13480 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13482 unsigned long s;
13484 if (i >= 0)
13486 int clz, ctz;
13487 if (i <= 31)
13488 return 1;
13489 else if (i <= 0xff)
13490 return 2;
13491 else if (i <= 0xffff)
13492 return 3;
13493 clz = clz_hwi (i);
13494 ctz = ctz_hwi (i);
13495 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13496 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13497 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13498 - clz - 5);
13499 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13500 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13501 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13502 - clz - 8);
13503 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13504 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13505 <= 4)
13506 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13507 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13508 return 5;
13509 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13510 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13511 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13512 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13513 - clz - 8);
13514 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13515 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13516 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13517 - clz - 16);
13518 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13519 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13520 && s > 6)
13521 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13522 - clz - 32);
13523 else
13524 return 1 + s;
13526 else
13528 if (i >= -0x80)
13529 return 2;
13530 else if (i >= -0x8000)
13531 return 3;
13532 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13534 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13536 s = size_of_int_loc_descriptor (-i) + 1;
13537 if (s < 5)
13538 return s;
13540 return 5;
13542 else
13544 unsigned long r = 1 + size_of_sleb128 (i);
13545 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13547 s = size_of_int_loc_descriptor (-i) + 1;
13548 if (s < r)
13549 return s;
13551 return r;
13556 /* Return loc description representing "address" of integer value.
13557 This can appear only as toplevel expression. */
13559 static dw_loc_descr_ref
13560 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13562 int litsize;
13563 dw_loc_descr_ref loc_result = NULL;
13565 if (!(dwarf_version >= 4 || !dwarf_strict))
13566 return NULL;
13568 litsize = size_of_int_loc_descriptor (i);
13569 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13570 is more compact. For DW_OP_stack_value we need:
13571 litsize + 1 (DW_OP_stack_value)
13572 and for DW_OP_implicit_value:
13573 1 (DW_OP_implicit_value) + 1 (length) + size. */
13574 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13576 loc_result = int_loc_descriptor (i);
13577 add_loc_descr (&loc_result,
13578 new_loc_descr (DW_OP_stack_value, 0, 0));
13579 return loc_result;
13582 loc_result = new_loc_descr (DW_OP_implicit_value,
13583 size, 0);
13584 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13585 loc_result->dw_loc_oprnd2.v.val_int = i;
13586 return loc_result;
13589 /* Return a location descriptor that designates a base+offset location. */
13591 static dw_loc_descr_ref
13592 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13593 enum var_init_status initialized)
13595 unsigned int regno;
13596 dw_loc_descr_ref result;
13597 dw_fde_ref fde = cfun->fde;
13599 /* We only use "frame base" when we're sure we're talking about the
13600 post-prologue local stack frame. We do this by *not* running
13601 register elimination until this point, and recognizing the special
13602 argument pointer and soft frame pointer rtx's. */
13603 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13605 rtx elim = (ira_use_lra_p
13606 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13607 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13609 if (elim != reg)
13611 if (GET_CODE (elim) == PLUS)
13613 offset += INTVAL (XEXP (elim, 1));
13614 elim = XEXP (elim, 0);
13616 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13617 && (elim == hard_frame_pointer_rtx
13618 || elim == stack_pointer_rtx))
13619 || elim == (frame_pointer_needed
13620 ? hard_frame_pointer_rtx
13621 : stack_pointer_rtx));
13623 /* If drap register is used to align stack, use frame
13624 pointer + offset to access stack variables. If stack
13625 is aligned without drap, use stack pointer + offset to
13626 access stack variables. */
13627 if (crtl->stack_realign_tried
13628 && reg == frame_pointer_rtx)
13630 int base_reg
13631 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13632 ? HARD_FRAME_POINTER_REGNUM
13633 : REGNO (elim));
13634 return new_reg_loc_descr (base_reg, offset);
13637 gcc_assert (frame_pointer_fb_offset_valid);
13638 offset += frame_pointer_fb_offset;
13639 return new_loc_descr (DW_OP_fbreg, offset, 0);
13643 regno = REGNO (reg);
13644 #ifdef LEAF_REG_REMAP
13645 if (crtl->uses_only_leaf_regs)
13647 int leaf_reg = LEAF_REG_REMAP (regno);
13648 if (leaf_reg != -1)
13649 regno = (unsigned) leaf_reg;
13651 #endif
13652 regno = DWARF_FRAME_REGNUM (regno);
13654 if (!optimize && fde
13655 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13657 /* Use cfa+offset to represent the location of arguments passed
13658 on the stack when drap is used to align stack.
13659 Only do this when not optimizing, for optimized code var-tracking
13660 is supposed to track where the arguments live and the register
13661 used as vdrap or drap in some spot might be used for something
13662 else in other part of the routine. */
13663 return new_loc_descr (DW_OP_fbreg, offset, 0);
13666 if (regno <= 31)
13667 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13668 offset, 0);
13669 else
13670 result = new_loc_descr (DW_OP_bregx, regno, offset);
13672 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13673 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13675 return result;
13678 /* Return true if this RTL expression describes a base+offset calculation. */
13680 static inline int
13681 is_based_loc (const_rtx rtl)
13683 return (GET_CODE (rtl) == PLUS
13684 && ((REG_P (XEXP (rtl, 0))
13685 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13686 && CONST_INT_P (XEXP (rtl, 1)))));
13689 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13690 failed. */
13692 static dw_loc_descr_ref
13693 tls_mem_loc_descriptor (rtx mem)
13695 tree base;
13696 dw_loc_descr_ref loc_result;
13698 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13699 return NULL;
13701 base = get_base_address (MEM_EXPR (mem));
13702 if (base == NULL
13703 || !VAR_P (base)
13704 || !DECL_THREAD_LOCAL_P (base))
13705 return NULL;
13707 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13708 if (loc_result == NULL)
13709 return NULL;
13711 if (MEM_OFFSET (mem))
13712 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13714 return loc_result;
13717 /* Output debug info about reason why we failed to expand expression as dwarf
13718 expression. */
13720 static void
13721 expansion_failed (tree expr, rtx rtl, char const *reason)
13723 if (dump_file && (dump_flags & TDF_DETAILS))
13725 fprintf (dump_file, "Failed to expand as dwarf: ");
13726 if (expr)
13727 print_generic_expr (dump_file, expr, dump_flags);
13728 if (rtl)
13730 fprintf (dump_file, "\n");
13731 print_rtl (dump_file, rtl);
13733 fprintf (dump_file, "\nReason: %s\n", reason);
13737 /* Helper function for const_ok_for_output. */
13739 static bool
13740 const_ok_for_output_1 (rtx rtl)
13742 if (GET_CODE (rtl) == UNSPEC)
13744 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13745 we can't express it in the debug info. */
13746 /* Don't complain about TLS UNSPECs, those are just too hard to
13747 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13748 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13749 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13750 if (flag_checking
13751 && (XVECLEN (rtl, 0) == 0
13752 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13753 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13754 inform (current_function_decl
13755 ? DECL_SOURCE_LOCATION (current_function_decl)
13756 : UNKNOWN_LOCATION,
13757 #if NUM_UNSPEC_VALUES > 0
13758 "non-delegitimized UNSPEC %s (%d) found in variable location",
13759 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13760 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13761 XINT (rtl, 1));
13762 #else
13763 "non-delegitimized UNSPEC %d found in variable location",
13764 XINT (rtl, 1));
13765 #endif
13766 expansion_failed (NULL_TREE, rtl,
13767 "UNSPEC hasn't been delegitimized.\n");
13768 return false;
13771 if (targetm.const_not_ok_for_debug_p (rtl))
13773 expansion_failed (NULL_TREE, rtl,
13774 "Expression rejected for debug by the backend.\n");
13775 return false;
13778 /* FIXME: Refer to PR60655. It is possible for simplification
13779 of rtl expressions in var tracking to produce such expressions.
13780 We should really identify / validate expressions
13781 enclosed in CONST that can be handled by assemblers on various
13782 targets and only handle legitimate cases here. */
13783 if (GET_CODE (rtl) != SYMBOL_REF)
13785 if (GET_CODE (rtl) == NOT)
13786 return false;
13787 return true;
13790 if (CONSTANT_POOL_ADDRESS_P (rtl))
13792 bool marked;
13793 get_pool_constant_mark (rtl, &marked);
13794 /* If all references to this pool constant were optimized away,
13795 it was not output and thus we can't represent it. */
13796 if (!marked)
13798 expansion_failed (NULL_TREE, rtl,
13799 "Constant was removed from constant pool.\n");
13800 return false;
13804 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13805 return false;
13807 /* Avoid references to external symbols in debug info, on several targets
13808 the linker might even refuse to link when linking a shared library,
13809 and in many other cases the relocations for .debug_info/.debug_loc are
13810 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13811 to be defined within the same shared library or executable are fine. */
13812 if (SYMBOL_REF_EXTERNAL_P (rtl))
13814 tree decl = SYMBOL_REF_DECL (rtl);
13816 if (decl == NULL || !targetm.binds_local_p (decl))
13818 expansion_failed (NULL_TREE, rtl,
13819 "Symbol not defined in current TU.\n");
13820 return false;
13824 return true;
13827 /* Return true if constant RTL can be emitted in DW_OP_addr or
13828 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13829 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13831 static bool
13832 const_ok_for_output (rtx rtl)
13834 if (GET_CODE (rtl) == SYMBOL_REF)
13835 return const_ok_for_output_1 (rtl);
13837 if (GET_CODE (rtl) == CONST)
13839 subrtx_var_iterator::array_type array;
13840 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13841 if (!const_ok_for_output_1 (*iter))
13842 return false;
13843 return true;
13846 return true;
13849 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13850 if possible, NULL otherwise. */
13852 static dw_die_ref
13853 base_type_for_mode (machine_mode mode, bool unsignedp)
13855 dw_die_ref type_die;
13856 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13858 if (type == NULL)
13859 return NULL;
13860 switch (TREE_CODE (type))
13862 case INTEGER_TYPE:
13863 case REAL_TYPE:
13864 break;
13865 default:
13866 return NULL;
13868 type_die = lookup_type_die (type);
13869 if (!type_die)
13870 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13871 comp_unit_die ());
13872 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13873 return NULL;
13874 return type_die;
13877 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13878 type matching MODE, or, if MODE is narrower than or as wide as
13879 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13880 possible. */
13882 static dw_loc_descr_ref
13883 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
13885 machine_mode outer_mode = mode;
13886 dw_die_ref type_die;
13887 dw_loc_descr_ref cvt;
13889 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13891 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13892 return op;
13894 type_die = base_type_for_mode (outer_mode, 1);
13895 if (type_die == NULL)
13896 return NULL;
13897 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13898 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13899 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13900 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13901 add_loc_descr (&op, cvt);
13902 return op;
13905 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13907 static dw_loc_descr_ref
13908 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13909 dw_loc_descr_ref op1)
13911 dw_loc_descr_ref ret = op0;
13912 add_loc_descr (&ret, op1);
13913 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13914 if (STORE_FLAG_VALUE != 1)
13916 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13917 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13919 return ret;
13922 /* Return location descriptor for signed comparison OP RTL. */
13924 static dw_loc_descr_ref
13925 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13926 machine_mode mem_mode)
13928 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13929 dw_loc_descr_ref op0, op1;
13930 int shift;
13932 if (op_mode == VOIDmode)
13933 op_mode = GET_MODE (XEXP (rtl, 1));
13934 if (op_mode == VOIDmode)
13935 return NULL;
13937 if (dwarf_strict
13938 && dwarf_version < 5
13939 && (!SCALAR_INT_MODE_P (op_mode)
13940 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13941 return NULL;
13943 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13944 VAR_INIT_STATUS_INITIALIZED);
13945 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13946 VAR_INIT_STATUS_INITIALIZED);
13948 if (op0 == NULL || op1 == NULL)
13949 return NULL;
13951 if (!SCALAR_INT_MODE_P (op_mode)
13952 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13953 return compare_loc_descriptor (op, op0, op1);
13955 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13957 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13958 dw_loc_descr_ref cvt;
13960 if (type_die == NULL)
13961 return NULL;
13962 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13963 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13964 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13965 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13966 add_loc_descr (&op0, cvt);
13967 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13968 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13969 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13970 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13971 add_loc_descr (&op1, cvt);
13972 return compare_loc_descriptor (op, op0, op1);
13975 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13976 /* For eq/ne, if the operands are known to be zero-extended,
13977 there is no need to do the fancy shifting up. */
13978 if (op == DW_OP_eq || op == DW_OP_ne)
13980 dw_loc_descr_ref last0, last1;
13981 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13983 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13985 /* deref_size zero extends, and for constants we can check
13986 whether they are zero extended or not. */
13987 if (((last0->dw_loc_opc == DW_OP_deref_size
13988 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13989 || (CONST_INT_P (XEXP (rtl, 0))
13990 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13991 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13992 && ((last1->dw_loc_opc == DW_OP_deref_size
13993 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13994 || (CONST_INT_P (XEXP (rtl, 1))
13995 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13996 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13997 return compare_loc_descriptor (op, op0, op1);
13999 /* EQ/NE comparison against constant in narrower type than
14000 DWARF2_ADDR_SIZE can be performed either as
14001 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14002 DW_OP_{eq,ne}
14004 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14005 DW_OP_{eq,ne}. Pick whatever is shorter. */
14006 if (CONST_INT_P (XEXP (rtl, 1))
14007 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14008 && (size_of_int_loc_descriptor (shift) + 1
14009 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14010 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14011 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14012 & GET_MODE_MASK (op_mode))))
14014 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14015 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14016 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14017 & GET_MODE_MASK (op_mode));
14018 return compare_loc_descriptor (op, op0, op1);
14021 add_loc_descr (&op0, int_loc_descriptor (shift));
14022 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14023 if (CONST_INT_P (XEXP (rtl, 1)))
14024 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14025 else
14027 add_loc_descr (&op1, int_loc_descriptor (shift));
14028 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14030 return compare_loc_descriptor (op, op0, op1);
14033 /* Return location descriptor for unsigned comparison OP RTL. */
14035 static dw_loc_descr_ref
14036 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14037 machine_mode mem_mode)
14039 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14040 dw_loc_descr_ref op0, op1;
14042 if (op_mode == VOIDmode)
14043 op_mode = GET_MODE (XEXP (rtl, 1));
14044 if (op_mode == VOIDmode)
14045 return NULL;
14046 if (!SCALAR_INT_MODE_P (op_mode))
14047 return NULL;
14049 if (dwarf_strict
14050 && dwarf_version < 5
14051 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14052 return NULL;
14054 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14055 VAR_INIT_STATUS_INITIALIZED);
14056 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14057 VAR_INIT_STATUS_INITIALIZED);
14059 if (op0 == NULL || op1 == NULL)
14060 return NULL;
14062 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14064 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14065 dw_loc_descr_ref last0, last1;
14066 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14068 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14070 if (CONST_INT_P (XEXP (rtl, 0)))
14071 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14072 /* deref_size zero extends, so no need to mask it again. */
14073 else if (last0->dw_loc_opc != DW_OP_deref_size
14074 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14076 add_loc_descr (&op0, int_loc_descriptor (mask));
14077 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14079 if (CONST_INT_P (XEXP (rtl, 1)))
14080 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14081 /* deref_size zero extends, so no need to mask it again. */
14082 else if (last1->dw_loc_opc != DW_OP_deref_size
14083 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14085 add_loc_descr (&op1, int_loc_descriptor (mask));
14086 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14089 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14091 HOST_WIDE_INT bias = 1;
14092 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14093 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14094 if (CONST_INT_P (XEXP (rtl, 1)))
14095 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14096 + INTVAL (XEXP (rtl, 1)));
14097 else
14098 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14099 bias, 0));
14101 return compare_loc_descriptor (op, op0, op1);
14104 /* Return location descriptor for {U,S}{MIN,MAX}. */
14106 static dw_loc_descr_ref
14107 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14108 machine_mode mem_mode)
14110 enum dwarf_location_atom op;
14111 dw_loc_descr_ref op0, op1, ret;
14112 dw_loc_descr_ref bra_node, drop_node;
14114 if (dwarf_strict
14115 && dwarf_version < 5
14116 && (!SCALAR_INT_MODE_P (mode)
14117 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14118 return NULL;
14120 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14121 VAR_INIT_STATUS_INITIALIZED);
14122 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14123 VAR_INIT_STATUS_INITIALIZED);
14125 if (op0 == NULL || op1 == NULL)
14126 return NULL;
14128 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14129 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14130 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14131 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14133 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14135 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14136 add_loc_descr (&op0, int_loc_descriptor (mask));
14137 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14138 add_loc_descr (&op1, int_loc_descriptor (mask));
14139 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14141 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14143 HOST_WIDE_INT bias = 1;
14144 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14145 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14146 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14149 else if (!SCALAR_INT_MODE_P (mode)
14150 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14152 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14153 add_loc_descr (&op0, int_loc_descriptor (shift));
14154 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14155 add_loc_descr (&op1, int_loc_descriptor (shift));
14156 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14158 else if (SCALAR_INT_MODE_P (mode)
14159 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14161 dw_die_ref type_die = base_type_for_mode (mode, 0);
14162 dw_loc_descr_ref cvt;
14163 if (type_die == NULL)
14164 return NULL;
14165 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14166 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14167 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14168 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14169 add_loc_descr (&op0, cvt);
14170 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14171 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14172 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14173 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14174 add_loc_descr (&op1, cvt);
14177 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14178 op = DW_OP_lt;
14179 else
14180 op = DW_OP_gt;
14181 ret = op0;
14182 add_loc_descr (&ret, op1);
14183 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14184 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14185 add_loc_descr (&ret, bra_node);
14186 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14187 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14188 add_loc_descr (&ret, drop_node);
14189 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14190 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14191 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14192 && SCALAR_INT_MODE_P (mode)
14193 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14194 ret = convert_descriptor_to_mode (mode, ret);
14195 return ret;
14198 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14199 but after converting arguments to type_die, afterwards
14200 convert back to unsigned. */
14202 static dw_loc_descr_ref
14203 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14204 machine_mode mode, machine_mode mem_mode)
14206 dw_loc_descr_ref cvt, op0, op1;
14208 if (type_die == NULL)
14209 return NULL;
14210 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14211 VAR_INIT_STATUS_INITIALIZED);
14212 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14213 VAR_INIT_STATUS_INITIALIZED);
14214 if (op0 == NULL || op1 == NULL)
14215 return NULL;
14216 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14217 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14218 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14219 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14220 add_loc_descr (&op0, cvt);
14221 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14222 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14223 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14224 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14225 add_loc_descr (&op1, cvt);
14226 add_loc_descr (&op0, op1);
14227 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14228 return convert_descriptor_to_mode (mode, op0);
14231 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14232 const0 is DW_OP_lit0 or corresponding typed constant,
14233 const1 is DW_OP_lit1 or corresponding typed constant
14234 and constMSB is constant with just the MSB bit set
14235 for the mode):
14236 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14237 L1: const0 DW_OP_swap
14238 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14239 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14240 L3: DW_OP_drop
14241 L4: DW_OP_nop
14243 CTZ is similar:
14244 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14245 L1: const0 DW_OP_swap
14246 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14247 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14248 L3: DW_OP_drop
14249 L4: DW_OP_nop
14251 FFS is similar:
14252 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14253 L1: const1 DW_OP_swap
14254 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14255 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14256 L3: DW_OP_drop
14257 L4: DW_OP_nop */
14259 static dw_loc_descr_ref
14260 clz_loc_descriptor (rtx rtl, machine_mode mode,
14261 machine_mode mem_mode)
14263 dw_loc_descr_ref op0, ret, tmp;
14264 HOST_WIDE_INT valv;
14265 dw_loc_descr_ref l1jump, l1label;
14266 dw_loc_descr_ref l2jump, l2label;
14267 dw_loc_descr_ref l3jump, l3label;
14268 dw_loc_descr_ref l4jump, l4label;
14269 rtx msb;
14271 if (!SCALAR_INT_MODE_P (mode)
14272 || GET_MODE (XEXP (rtl, 0)) != mode)
14273 return NULL;
14275 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14276 VAR_INIT_STATUS_INITIALIZED);
14277 if (op0 == NULL)
14278 return NULL;
14279 ret = op0;
14280 if (GET_CODE (rtl) == CLZ)
14282 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14283 valv = GET_MODE_BITSIZE (mode);
14285 else if (GET_CODE (rtl) == FFS)
14286 valv = 0;
14287 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14288 valv = GET_MODE_BITSIZE (mode);
14289 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14290 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14291 add_loc_descr (&ret, l1jump);
14292 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14293 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14294 VAR_INIT_STATUS_INITIALIZED);
14295 if (tmp == NULL)
14296 return NULL;
14297 add_loc_descr (&ret, tmp);
14298 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14299 add_loc_descr (&ret, l4jump);
14300 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14301 ? const1_rtx : const0_rtx,
14302 mode, mem_mode,
14303 VAR_INIT_STATUS_INITIALIZED);
14304 if (l1label == NULL)
14305 return NULL;
14306 add_loc_descr (&ret, l1label);
14307 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14308 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14309 add_loc_descr (&ret, l2label);
14310 if (GET_CODE (rtl) != CLZ)
14311 msb = const1_rtx;
14312 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14313 msb = GEN_INT (HOST_WIDE_INT_1U
14314 << (GET_MODE_BITSIZE (mode) - 1));
14315 else
14316 msb = immed_wide_int_const
14317 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14318 GET_MODE_PRECISION (mode)), mode);
14319 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14320 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14321 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14322 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14323 else
14324 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14325 VAR_INIT_STATUS_INITIALIZED);
14326 if (tmp == NULL)
14327 return NULL;
14328 add_loc_descr (&ret, tmp);
14329 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14330 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14331 add_loc_descr (&ret, l3jump);
14332 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14333 VAR_INIT_STATUS_INITIALIZED);
14334 if (tmp == NULL)
14335 return NULL;
14336 add_loc_descr (&ret, tmp);
14337 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14338 ? DW_OP_shl : DW_OP_shr, 0, 0));
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14340 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14341 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14342 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14343 add_loc_descr (&ret, l2jump);
14344 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14345 add_loc_descr (&ret, l3label);
14346 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14347 add_loc_descr (&ret, l4label);
14348 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14349 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14350 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14351 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14352 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14353 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14354 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14355 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14356 return ret;
14359 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14360 const1 is DW_OP_lit1 or corresponding typed constant):
14361 const0 DW_OP_swap
14362 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14363 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14364 L2: DW_OP_drop
14366 PARITY is similar:
14367 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14368 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14369 L2: DW_OP_drop */
14371 static dw_loc_descr_ref
14372 popcount_loc_descriptor (rtx rtl, machine_mode mode,
14373 machine_mode mem_mode)
14375 dw_loc_descr_ref op0, ret, tmp;
14376 dw_loc_descr_ref l1jump, l1label;
14377 dw_loc_descr_ref l2jump, l2label;
14379 if (!SCALAR_INT_MODE_P (mode)
14380 || GET_MODE (XEXP (rtl, 0)) != mode)
14381 return NULL;
14383 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14384 VAR_INIT_STATUS_INITIALIZED);
14385 if (op0 == NULL)
14386 return NULL;
14387 ret = op0;
14388 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14389 VAR_INIT_STATUS_INITIALIZED);
14390 if (tmp == NULL)
14391 return NULL;
14392 add_loc_descr (&ret, tmp);
14393 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14394 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14395 add_loc_descr (&ret, l1label);
14396 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14397 add_loc_descr (&ret, l2jump);
14398 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14399 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14400 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14401 VAR_INIT_STATUS_INITIALIZED);
14402 if (tmp == NULL)
14403 return NULL;
14404 add_loc_descr (&ret, tmp);
14405 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14406 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14407 ? DW_OP_plus : DW_OP_xor, 0, 0));
14408 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14409 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14410 VAR_INIT_STATUS_INITIALIZED);
14411 add_loc_descr (&ret, tmp);
14412 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14413 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14414 add_loc_descr (&ret, l1jump);
14415 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14416 add_loc_descr (&ret, l2label);
14417 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14418 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14419 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14420 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14421 return ret;
14424 /* BSWAP (constS is initial shift count, either 56 or 24):
14425 constS const0
14426 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14427 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14428 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14429 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14430 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14432 static dw_loc_descr_ref
14433 bswap_loc_descriptor (rtx rtl, machine_mode mode,
14434 machine_mode mem_mode)
14436 dw_loc_descr_ref op0, ret, tmp;
14437 dw_loc_descr_ref l1jump, l1label;
14438 dw_loc_descr_ref l2jump, l2label;
14440 if (!SCALAR_INT_MODE_P (mode)
14441 || BITS_PER_UNIT != 8
14442 || (GET_MODE_BITSIZE (mode) != 32
14443 && GET_MODE_BITSIZE (mode) != 64))
14444 return NULL;
14446 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14447 VAR_INIT_STATUS_INITIALIZED);
14448 if (op0 == NULL)
14449 return NULL;
14451 ret = op0;
14452 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14453 mode, mem_mode,
14454 VAR_INIT_STATUS_INITIALIZED);
14455 if (tmp == NULL)
14456 return NULL;
14457 add_loc_descr (&ret, tmp);
14458 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14459 VAR_INIT_STATUS_INITIALIZED);
14460 if (tmp == NULL)
14461 return NULL;
14462 add_loc_descr (&ret, tmp);
14463 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14464 add_loc_descr (&ret, l1label);
14465 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14466 mode, mem_mode,
14467 VAR_INIT_STATUS_INITIALIZED);
14468 add_loc_descr (&ret, tmp);
14469 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14470 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14471 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14472 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14473 VAR_INIT_STATUS_INITIALIZED);
14474 if (tmp == NULL)
14475 return NULL;
14476 add_loc_descr (&ret, tmp);
14477 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14478 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14479 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14480 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14481 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14482 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14483 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14484 VAR_INIT_STATUS_INITIALIZED);
14485 add_loc_descr (&ret, tmp);
14486 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14487 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14488 add_loc_descr (&ret, l2jump);
14489 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14490 VAR_INIT_STATUS_INITIALIZED);
14491 add_loc_descr (&ret, tmp);
14492 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14493 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14494 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14495 add_loc_descr (&ret, l1jump);
14496 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14497 add_loc_descr (&ret, l2label);
14498 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14500 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14501 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14502 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14503 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14504 return ret;
14507 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14508 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14509 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14510 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14512 ROTATERT is similar:
14513 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14514 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14515 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14517 static dw_loc_descr_ref
14518 rotate_loc_descriptor (rtx rtl, machine_mode mode,
14519 machine_mode mem_mode)
14521 rtx rtlop1 = XEXP (rtl, 1);
14522 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14523 int i;
14525 if (!SCALAR_INT_MODE_P (mode))
14526 return NULL;
14528 if (GET_MODE (rtlop1) != VOIDmode
14529 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14530 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14531 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14532 VAR_INIT_STATUS_INITIALIZED);
14533 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14534 VAR_INIT_STATUS_INITIALIZED);
14535 if (op0 == NULL || op1 == NULL)
14536 return NULL;
14537 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14538 for (i = 0; i < 2; i++)
14540 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14541 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14542 mode, mem_mode,
14543 VAR_INIT_STATUS_INITIALIZED);
14544 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14545 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14546 ? DW_OP_const4u
14547 : HOST_BITS_PER_WIDE_INT == 64
14548 ? DW_OP_const8u : DW_OP_constu,
14549 GET_MODE_MASK (mode), 0);
14550 else
14551 mask[i] = NULL;
14552 if (mask[i] == NULL)
14553 return NULL;
14554 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14556 ret = op0;
14557 add_loc_descr (&ret, op1);
14558 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14559 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14560 if (GET_CODE (rtl) == ROTATERT)
14562 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14563 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14564 GET_MODE_BITSIZE (mode), 0));
14566 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14567 if (mask[0] != NULL)
14568 add_loc_descr (&ret, mask[0]);
14569 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14570 if (mask[1] != NULL)
14572 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14573 add_loc_descr (&ret, mask[1]);
14574 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14576 if (GET_CODE (rtl) == ROTATE)
14578 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14580 GET_MODE_BITSIZE (mode), 0));
14582 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14583 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14584 return ret;
14587 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14588 for DEBUG_PARAMETER_REF RTL. */
14590 static dw_loc_descr_ref
14591 parameter_ref_descriptor (rtx rtl)
14593 dw_loc_descr_ref ret;
14594 dw_die_ref ref;
14596 if (dwarf_strict)
14597 return NULL;
14598 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14599 /* With LTO during LTRANS we get the late DIE that refers to the early
14600 DIE, thus we add another indirection here. This seems to confuse
14601 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14602 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14603 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14604 if (ref)
14606 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14607 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14608 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14610 else
14612 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14613 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14615 return ret;
14618 /* The following routine converts the RTL for a variable or parameter
14619 (resident in memory) into an equivalent Dwarf representation of a
14620 mechanism for getting the address of that same variable onto the top of a
14621 hypothetical "address evaluation" stack.
14623 When creating memory location descriptors, we are effectively transforming
14624 the RTL for a memory-resident object into its Dwarf postfix expression
14625 equivalent. This routine recursively descends an RTL tree, turning
14626 it into Dwarf postfix code as it goes.
14628 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14630 MEM_MODE is the mode of the memory reference, needed to handle some
14631 autoincrement addressing modes.
14633 Return 0 if we can't represent the location. */
14635 dw_loc_descr_ref
14636 mem_loc_descriptor (rtx rtl, machine_mode mode,
14637 machine_mode mem_mode,
14638 enum var_init_status initialized)
14640 dw_loc_descr_ref mem_loc_result = NULL;
14641 enum dwarf_location_atom op;
14642 dw_loc_descr_ref op0, op1;
14643 rtx inner = NULL_RTX;
14645 if (mode == VOIDmode)
14646 mode = GET_MODE (rtl);
14648 /* Note that for a dynamically sized array, the location we will generate a
14649 description of here will be the lowest numbered location which is
14650 actually within the array. That's *not* necessarily the same as the
14651 zeroth element of the array. */
14653 rtl = targetm.delegitimize_address (rtl);
14655 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14656 return NULL;
14658 switch (GET_CODE (rtl))
14660 case POST_INC:
14661 case POST_DEC:
14662 case POST_MODIFY:
14663 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14665 case SUBREG:
14666 /* The case of a subreg may arise when we have a local (register)
14667 variable or a formal (register) parameter which doesn't quite fill
14668 up an entire register. For now, just assume that it is
14669 legitimate to make the Dwarf info refer to the whole register which
14670 contains the given subreg. */
14671 if (!subreg_lowpart_p (rtl))
14672 break;
14673 inner = SUBREG_REG (rtl);
14674 /* FALLTHRU */
14675 case TRUNCATE:
14676 if (inner == NULL_RTX)
14677 inner = XEXP (rtl, 0);
14678 if (SCALAR_INT_MODE_P (mode)
14679 && SCALAR_INT_MODE_P (GET_MODE (inner))
14680 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14681 #ifdef POINTERS_EXTEND_UNSIGNED
14682 || (mode == Pmode && mem_mode != VOIDmode)
14683 #endif
14685 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
14687 mem_loc_result = mem_loc_descriptor (inner,
14688 GET_MODE (inner),
14689 mem_mode, initialized);
14690 break;
14692 if (dwarf_strict && dwarf_version < 5)
14693 break;
14694 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
14695 break;
14696 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
14697 && (!SCALAR_INT_MODE_P (mode)
14698 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
14699 break;
14700 else
14702 dw_die_ref type_die;
14703 dw_loc_descr_ref cvt;
14705 mem_loc_result = mem_loc_descriptor (inner,
14706 GET_MODE (inner),
14707 mem_mode, initialized);
14708 if (mem_loc_result == NULL)
14709 break;
14710 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14711 if (type_die == NULL)
14713 mem_loc_result = NULL;
14714 break;
14716 if (GET_MODE_SIZE (mode)
14717 != GET_MODE_SIZE (GET_MODE (inner)))
14718 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14719 else
14720 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14721 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14722 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14723 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14724 add_loc_descr (&mem_loc_result, cvt);
14725 if (SCALAR_INT_MODE_P (mode)
14726 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14728 /* Convert it to untyped afterwards. */
14729 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14730 add_loc_descr (&mem_loc_result, cvt);
14733 break;
14735 case REG:
14736 if (! SCALAR_INT_MODE_P (mode)
14737 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14738 && rtl != arg_pointer_rtx
14739 && rtl != frame_pointer_rtx
14740 #ifdef POINTERS_EXTEND_UNSIGNED
14741 && (mode != Pmode || mem_mode == VOIDmode)
14742 #endif
14745 dw_die_ref type_die;
14746 unsigned int dbx_regnum;
14748 if (dwarf_strict && dwarf_version < 5)
14749 break;
14750 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14751 break;
14752 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14753 if (type_die == NULL)
14754 break;
14756 dbx_regnum = dbx_reg_number (rtl);
14757 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14758 break;
14759 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14760 dbx_regnum, 0);
14761 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14762 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14763 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14764 break;
14766 /* Whenever a register number forms a part of the description of the
14767 method for calculating the (dynamic) address of a memory resident
14768 object, DWARF rules require the register number be referred to as
14769 a "base register". This distinction is not based in any way upon
14770 what category of register the hardware believes the given register
14771 belongs to. This is strictly DWARF terminology we're dealing with
14772 here. Note that in cases where the location of a memory-resident
14773 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14774 OP_CONST (0)) the actual DWARF location descriptor that we generate
14775 may just be OP_BASEREG (basereg). This may look deceptively like
14776 the object in question was allocated to a register (rather than in
14777 memory) so DWARF consumers need to be aware of the subtle
14778 distinction between OP_REG and OP_BASEREG. */
14779 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14780 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14781 else if (stack_realign_drap
14782 && crtl->drap_reg
14783 && crtl->args.internal_arg_pointer == rtl
14784 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14786 /* If RTL is internal_arg_pointer, which has been optimized
14787 out, use DRAP instead. */
14788 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14789 VAR_INIT_STATUS_INITIALIZED);
14791 break;
14793 case SIGN_EXTEND:
14794 case ZERO_EXTEND:
14795 if (!SCALAR_INT_MODE_P (mode))
14796 break;
14797 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14798 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14799 if (op0 == 0)
14800 break;
14801 else if (GET_CODE (rtl) == ZERO_EXTEND
14802 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14803 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14804 < HOST_BITS_PER_WIDE_INT
14805 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14806 to expand zero extend as two shifts instead of
14807 masking. */
14808 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14810 machine_mode imode = GET_MODE (XEXP (rtl, 0));
14811 mem_loc_result = op0;
14812 add_loc_descr (&mem_loc_result,
14813 int_loc_descriptor (GET_MODE_MASK (imode)));
14814 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14816 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14818 int shift = DWARF2_ADDR_SIZE
14819 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14820 shift *= BITS_PER_UNIT;
14821 if (GET_CODE (rtl) == SIGN_EXTEND)
14822 op = DW_OP_shra;
14823 else
14824 op = DW_OP_shr;
14825 mem_loc_result = op0;
14826 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14827 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14828 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14829 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14831 else if (!dwarf_strict || dwarf_version >= 5)
14833 dw_die_ref type_die1, type_die2;
14834 dw_loc_descr_ref cvt;
14836 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14837 GET_CODE (rtl) == ZERO_EXTEND);
14838 if (type_die1 == NULL)
14839 break;
14840 type_die2 = base_type_for_mode (mode, 1);
14841 if (type_die2 == NULL)
14842 break;
14843 mem_loc_result = op0;
14844 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14845 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14846 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14847 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14848 add_loc_descr (&mem_loc_result, cvt);
14849 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14850 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14851 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14852 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14853 add_loc_descr (&mem_loc_result, cvt);
14855 break;
14857 case MEM:
14859 rtx new_rtl = avoid_constant_pool_reference (rtl);
14860 if (new_rtl != rtl)
14862 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14863 initialized);
14864 if (mem_loc_result != NULL)
14865 return mem_loc_result;
14868 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14869 get_address_mode (rtl), mode,
14870 VAR_INIT_STATUS_INITIALIZED);
14871 if (mem_loc_result == NULL)
14872 mem_loc_result = tls_mem_loc_descriptor (rtl);
14873 if (mem_loc_result != NULL)
14875 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14876 || !SCALAR_INT_MODE_P(mode))
14878 dw_die_ref type_die;
14879 dw_loc_descr_ref deref;
14881 if (dwarf_strict && dwarf_version < 5)
14882 return NULL;
14883 type_die
14884 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14885 if (type_die == NULL)
14886 return NULL;
14887 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14888 GET_MODE_SIZE (mode), 0);
14889 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14890 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14891 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14892 add_loc_descr (&mem_loc_result, deref);
14894 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14895 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14896 else
14897 add_loc_descr (&mem_loc_result,
14898 new_loc_descr (DW_OP_deref_size,
14899 GET_MODE_SIZE (mode), 0));
14901 break;
14903 case LO_SUM:
14904 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14906 case LABEL_REF:
14907 /* Some ports can transform a symbol ref into a label ref, because
14908 the symbol ref is too far away and has to be dumped into a constant
14909 pool. */
14910 case CONST:
14911 case SYMBOL_REF:
14912 if (!SCALAR_INT_MODE_P (mode)
14913 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14914 #ifdef POINTERS_EXTEND_UNSIGNED
14915 && (mode != Pmode || mem_mode == VOIDmode)
14916 #endif
14918 break;
14919 if (GET_CODE (rtl) == SYMBOL_REF
14920 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14922 dw_loc_descr_ref temp;
14924 /* If this is not defined, we have no way to emit the data. */
14925 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14926 break;
14928 temp = new_addr_loc_descr (rtl, dtprel_true);
14930 /* We check for DWARF 5 here because gdb did not implement
14931 DW_OP_form_tls_address until after 7.12. */
14932 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14933 ? DW_OP_form_tls_address
14934 : DW_OP_GNU_push_tls_address),
14935 0, 0);
14936 add_loc_descr (&mem_loc_result, temp);
14938 break;
14941 if (!const_ok_for_output (rtl))
14943 if (GET_CODE (rtl) == CONST)
14944 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14945 initialized);
14946 break;
14949 symref:
14950 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14951 vec_safe_push (used_rtx_array, rtl);
14952 break;
14954 case CONCAT:
14955 case CONCATN:
14956 case VAR_LOCATION:
14957 case DEBUG_IMPLICIT_PTR:
14958 expansion_failed (NULL_TREE, rtl,
14959 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14960 return 0;
14962 case ENTRY_VALUE:
14963 if (dwarf_strict && dwarf_version < 5)
14964 return NULL;
14965 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14967 if (!SCALAR_INT_MODE_P (mode)
14968 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14969 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14970 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14971 else
14973 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
14974 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14975 return NULL;
14976 op0 = one_reg_loc_descriptor (dbx_regnum,
14977 VAR_INIT_STATUS_INITIALIZED);
14980 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14981 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14983 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14984 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14985 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14986 return NULL;
14988 else
14989 gcc_unreachable ();
14990 if (op0 == NULL)
14991 return NULL;
14992 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
14993 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14994 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14995 break;
14997 case DEBUG_PARAMETER_REF:
14998 mem_loc_result = parameter_ref_descriptor (rtl);
14999 break;
15001 case PRE_MODIFY:
15002 /* Extract the PLUS expression nested inside and fall into
15003 PLUS code below. */
15004 rtl = XEXP (rtl, 1);
15005 goto plus;
15007 case PRE_INC:
15008 case PRE_DEC:
15009 /* Turn these into a PLUS expression and fall into the PLUS code
15010 below. */
15011 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15012 gen_int_mode (GET_CODE (rtl) == PRE_INC
15013 ? GET_MODE_UNIT_SIZE (mem_mode)
15014 : -GET_MODE_UNIT_SIZE (mem_mode),
15015 mode));
15017 /* fall through */
15019 case PLUS:
15020 plus:
15021 if (is_based_loc (rtl)
15022 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15023 || XEXP (rtl, 0) == arg_pointer_rtx
15024 || XEXP (rtl, 0) == frame_pointer_rtx)
15025 && SCALAR_INT_MODE_P (mode))
15026 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15027 INTVAL (XEXP (rtl, 1)),
15028 VAR_INIT_STATUS_INITIALIZED);
15029 else
15031 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15032 VAR_INIT_STATUS_INITIALIZED);
15033 if (mem_loc_result == 0)
15034 break;
15036 if (CONST_INT_P (XEXP (rtl, 1))
15037 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15038 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15039 else
15041 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15042 VAR_INIT_STATUS_INITIALIZED);
15043 if (op1 == 0)
15044 return NULL;
15045 add_loc_descr (&mem_loc_result, op1);
15046 add_loc_descr (&mem_loc_result,
15047 new_loc_descr (DW_OP_plus, 0, 0));
15050 break;
15052 /* If a pseudo-reg is optimized away, it is possible for it to
15053 be replaced with a MEM containing a multiply or shift. */
15054 case MINUS:
15055 op = DW_OP_minus;
15056 goto do_binop;
15058 case MULT:
15059 op = DW_OP_mul;
15060 goto do_binop;
15062 case DIV:
15063 if ((!dwarf_strict || dwarf_version >= 5)
15064 && SCALAR_INT_MODE_P (mode)
15065 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15067 mem_loc_result = typed_binop (DW_OP_div, rtl,
15068 base_type_for_mode (mode, 0),
15069 mode, mem_mode);
15070 break;
15072 op = DW_OP_div;
15073 goto do_binop;
15075 case UMOD:
15076 op = DW_OP_mod;
15077 goto do_binop;
15079 case ASHIFT:
15080 op = DW_OP_shl;
15081 goto do_shift;
15083 case ASHIFTRT:
15084 op = DW_OP_shra;
15085 goto do_shift;
15087 case LSHIFTRT:
15088 op = DW_OP_shr;
15089 goto do_shift;
15091 do_shift:
15092 if (!SCALAR_INT_MODE_P (mode))
15093 break;
15094 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15095 VAR_INIT_STATUS_INITIALIZED);
15097 rtx rtlop1 = XEXP (rtl, 1);
15098 if (GET_MODE (rtlop1) != VOIDmode
15099 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15100 < GET_MODE_BITSIZE (mode))
15101 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15102 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15103 VAR_INIT_STATUS_INITIALIZED);
15106 if (op0 == 0 || op1 == 0)
15107 break;
15109 mem_loc_result = op0;
15110 add_loc_descr (&mem_loc_result, op1);
15111 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15112 break;
15114 case AND:
15115 op = DW_OP_and;
15116 goto do_binop;
15118 case IOR:
15119 op = DW_OP_or;
15120 goto do_binop;
15122 case XOR:
15123 op = DW_OP_xor;
15124 goto do_binop;
15126 do_binop:
15127 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15128 VAR_INIT_STATUS_INITIALIZED);
15129 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15130 VAR_INIT_STATUS_INITIALIZED);
15132 if (op0 == 0 || op1 == 0)
15133 break;
15135 mem_loc_result = op0;
15136 add_loc_descr (&mem_loc_result, op1);
15137 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15138 break;
15140 case MOD:
15141 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
15142 && (!dwarf_strict || dwarf_version >= 5))
15144 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15145 base_type_for_mode (mode, 0),
15146 mode, mem_mode);
15147 break;
15150 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15151 VAR_INIT_STATUS_INITIALIZED);
15152 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15153 VAR_INIT_STATUS_INITIALIZED);
15155 if (op0 == 0 || op1 == 0)
15156 break;
15158 mem_loc_result = op0;
15159 add_loc_descr (&mem_loc_result, op1);
15160 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15161 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15162 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15163 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15164 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15165 break;
15167 case UDIV:
15168 if ((!dwarf_strict || dwarf_version >= 5)
15169 && SCALAR_INT_MODE_P (mode))
15171 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15173 op = DW_OP_div;
15174 goto do_binop;
15176 mem_loc_result = typed_binop (DW_OP_div, rtl,
15177 base_type_for_mode (mode, 1),
15178 mode, mem_mode);
15180 break;
15182 case NOT:
15183 op = DW_OP_not;
15184 goto do_unop;
15186 case ABS:
15187 op = DW_OP_abs;
15188 goto do_unop;
15190 case NEG:
15191 op = DW_OP_neg;
15192 goto do_unop;
15194 do_unop:
15195 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15196 VAR_INIT_STATUS_INITIALIZED);
15198 if (op0 == 0)
15199 break;
15201 mem_loc_result = op0;
15202 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15203 break;
15205 case CONST_INT:
15206 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15207 #ifdef POINTERS_EXTEND_UNSIGNED
15208 || (mode == Pmode
15209 && mem_mode != VOIDmode
15210 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15211 #endif
15214 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15215 break;
15217 if ((!dwarf_strict || dwarf_version >= 5)
15218 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15219 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
15221 dw_die_ref type_die = base_type_for_mode (mode, 1);
15222 machine_mode amode;
15223 if (type_die == NULL)
15224 return NULL;
15225 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
15226 MODE_INT, 0);
15227 if (INTVAL (rtl) >= 0
15228 && amode != BLKmode
15229 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15230 /* const DW_OP_convert <XXX> vs.
15231 DW_OP_const_type <XXX, 1, const>. */
15232 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15233 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
15235 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15236 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15237 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15238 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15239 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15240 add_loc_descr (&mem_loc_result, op0);
15241 return mem_loc_result;
15243 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15244 INTVAL (rtl));
15245 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15246 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15247 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15248 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15249 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15250 else
15252 mem_loc_result->dw_loc_oprnd2.val_class
15253 = dw_val_class_const_double;
15254 mem_loc_result->dw_loc_oprnd2.v.val_double
15255 = double_int::from_shwi (INTVAL (rtl));
15258 break;
15260 case CONST_DOUBLE:
15261 if (!dwarf_strict || dwarf_version >= 5)
15263 dw_die_ref type_die;
15265 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15266 CONST_DOUBLE rtx could represent either a large integer
15267 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15268 the value is always a floating point constant.
15270 When it is an integer, a CONST_DOUBLE is used whenever
15271 the constant requires 2 HWIs to be adequately represented.
15272 We output CONST_DOUBLEs as blocks. */
15273 if (mode == VOIDmode
15274 || (GET_MODE (rtl) == VOIDmode
15275 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15276 break;
15277 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15278 if (type_die == NULL)
15279 return NULL;
15280 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15281 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15282 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15283 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15284 #if TARGET_SUPPORTS_WIDE_INT == 0
15285 if (!SCALAR_FLOAT_MODE_P (mode))
15287 mem_loc_result->dw_loc_oprnd2.val_class
15288 = dw_val_class_const_double;
15289 mem_loc_result->dw_loc_oprnd2.v.val_double
15290 = rtx_to_double_int (rtl);
15292 else
15293 #endif
15295 unsigned int length = GET_MODE_SIZE (mode);
15296 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15298 insert_float (rtl, array);
15299 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15300 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15301 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15302 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15305 break;
15307 case CONST_WIDE_INT:
15308 if (!dwarf_strict || dwarf_version >= 5)
15310 dw_die_ref type_die;
15312 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15313 if (type_die == NULL)
15314 return NULL;
15315 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15316 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15317 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15318 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15319 mem_loc_result->dw_loc_oprnd2.val_class
15320 = dw_val_class_wide_int;
15321 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15322 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15324 break;
15326 case EQ:
15327 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15328 break;
15330 case GE:
15331 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15332 break;
15334 case GT:
15335 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15336 break;
15338 case LE:
15339 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15340 break;
15342 case LT:
15343 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15344 break;
15346 case NE:
15347 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15348 break;
15350 case GEU:
15351 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15352 break;
15354 case GTU:
15355 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15356 break;
15358 case LEU:
15359 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15360 break;
15362 case LTU:
15363 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15364 break;
15366 case UMIN:
15367 case UMAX:
15368 if (!SCALAR_INT_MODE_P (mode))
15369 break;
15370 /* FALLTHRU */
15371 case SMIN:
15372 case SMAX:
15373 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15374 break;
15376 case ZERO_EXTRACT:
15377 case SIGN_EXTRACT:
15378 if (CONST_INT_P (XEXP (rtl, 1))
15379 && CONST_INT_P (XEXP (rtl, 2))
15380 && ((unsigned) INTVAL (XEXP (rtl, 1))
15381 + (unsigned) INTVAL (XEXP (rtl, 2))
15382 <= GET_MODE_BITSIZE (mode))
15383 && SCALAR_INT_MODE_P (mode)
15384 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15385 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15387 int shift, size;
15388 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15389 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15390 if (op0 == 0)
15391 break;
15392 if (GET_CODE (rtl) == SIGN_EXTRACT)
15393 op = DW_OP_shra;
15394 else
15395 op = DW_OP_shr;
15396 mem_loc_result = op0;
15397 size = INTVAL (XEXP (rtl, 1));
15398 shift = INTVAL (XEXP (rtl, 2));
15399 if (BITS_BIG_ENDIAN)
15400 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15401 - shift - size;
15402 if (shift + size != (int) DWARF2_ADDR_SIZE)
15404 add_loc_descr (&mem_loc_result,
15405 int_loc_descriptor (DWARF2_ADDR_SIZE
15406 - shift - size));
15407 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15409 if (size != (int) DWARF2_ADDR_SIZE)
15411 add_loc_descr (&mem_loc_result,
15412 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15413 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15416 break;
15418 case IF_THEN_ELSE:
15420 dw_loc_descr_ref op2, bra_node, drop_node;
15421 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15422 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15423 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15424 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15425 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15426 VAR_INIT_STATUS_INITIALIZED);
15427 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15428 VAR_INIT_STATUS_INITIALIZED);
15429 if (op0 == NULL || op1 == NULL || op2 == NULL)
15430 break;
15432 mem_loc_result = op1;
15433 add_loc_descr (&mem_loc_result, op2);
15434 add_loc_descr (&mem_loc_result, op0);
15435 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15436 add_loc_descr (&mem_loc_result, bra_node);
15437 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15438 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15439 add_loc_descr (&mem_loc_result, drop_node);
15440 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15441 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15443 break;
15445 case FLOAT_EXTEND:
15446 case FLOAT_TRUNCATE:
15447 case FLOAT:
15448 case UNSIGNED_FLOAT:
15449 case FIX:
15450 case UNSIGNED_FIX:
15451 if (!dwarf_strict || dwarf_version >= 5)
15453 dw_die_ref type_die;
15454 dw_loc_descr_ref cvt;
15456 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15457 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15458 if (op0 == NULL)
15459 break;
15460 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
15461 && (GET_CODE (rtl) == FLOAT
15462 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15463 <= DWARF2_ADDR_SIZE))
15465 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15466 GET_CODE (rtl) == UNSIGNED_FLOAT);
15467 if (type_die == NULL)
15468 break;
15469 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15470 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15471 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15472 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15473 add_loc_descr (&op0, cvt);
15475 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15476 if (type_die == NULL)
15477 break;
15478 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15479 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15480 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15481 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15482 add_loc_descr (&op0, cvt);
15483 if (SCALAR_INT_MODE_P (mode)
15484 && (GET_CODE (rtl) == FIX
15485 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15487 op0 = convert_descriptor_to_mode (mode, op0);
15488 if (op0 == NULL)
15489 break;
15491 mem_loc_result = op0;
15493 break;
15495 case CLZ:
15496 case CTZ:
15497 case FFS:
15498 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15499 break;
15501 case POPCOUNT:
15502 case PARITY:
15503 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15504 break;
15506 case BSWAP:
15507 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15508 break;
15510 case ROTATE:
15511 case ROTATERT:
15512 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15513 break;
15515 case COMPARE:
15516 /* In theory, we could implement the above. */
15517 /* DWARF cannot represent the unsigned compare operations
15518 natively. */
15519 case SS_MULT:
15520 case US_MULT:
15521 case SS_DIV:
15522 case US_DIV:
15523 case SS_PLUS:
15524 case US_PLUS:
15525 case SS_MINUS:
15526 case US_MINUS:
15527 case SS_NEG:
15528 case US_NEG:
15529 case SS_ABS:
15530 case SS_ASHIFT:
15531 case US_ASHIFT:
15532 case SS_TRUNCATE:
15533 case US_TRUNCATE:
15534 case UNORDERED:
15535 case ORDERED:
15536 case UNEQ:
15537 case UNGE:
15538 case UNGT:
15539 case UNLE:
15540 case UNLT:
15541 case LTGT:
15542 case FRACT_CONVERT:
15543 case UNSIGNED_FRACT_CONVERT:
15544 case SAT_FRACT:
15545 case UNSIGNED_SAT_FRACT:
15546 case SQRT:
15547 case ASM_OPERANDS:
15548 case VEC_MERGE:
15549 case VEC_SELECT:
15550 case VEC_CONCAT:
15551 case VEC_DUPLICATE:
15552 case UNSPEC:
15553 case HIGH:
15554 case FMA:
15555 case STRICT_LOW_PART:
15556 case CONST_VECTOR:
15557 case CONST_FIXED:
15558 case CLRSB:
15559 case CLOBBER:
15560 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15561 can't express it in the debug info. This can happen e.g. with some
15562 TLS UNSPECs. */
15563 break;
15565 case CONST_STRING:
15566 resolve_one_addr (&rtl);
15567 goto symref;
15569 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15570 the expression. An UNSPEC rtx represents a raw DWARF operation,
15571 new_loc_descr is called for it to build the operation directly.
15572 Otherwise mem_loc_descriptor is called recursively. */
15573 case PARALLEL:
15575 int index = 0;
15576 dw_loc_descr_ref exp_result = NULL;
15578 for (; index < XVECLEN (rtl, 0); index++)
15580 rtx elem = XVECEXP (rtl, 0, index);
15581 if (GET_CODE (elem) == UNSPEC)
15583 /* Each DWARF operation UNSPEC contain two operands, if
15584 one operand is not used for the operation, const0_rtx is
15585 passed. */
15586 gcc_assert (XVECLEN (elem, 0) == 2);
15588 HOST_WIDE_INT dw_op = XINT (elem, 1);
15589 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15590 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15591 exp_result
15592 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15593 oprnd2);
15595 else
15596 exp_result
15597 = mem_loc_descriptor (elem, mode, mem_mode,
15598 VAR_INIT_STATUS_INITIALIZED);
15600 if (!mem_loc_result)
15601 mem_loc_result = exp_result;
15602 else
15603 add_loc_descr (&mem_loc_result, exp_result);
15606 break;
15609 default:
15610 if (flag_checking)
15612 print_rtl (stderr, rtl);
15613 gcc_unreachable ();
15615 break;
15618 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15619 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15621 return mem_loc_result;
15624 /* Return a descriptor that describes the concatenation of two locations.
15625 This is typically a complex variable. */
15627 static dw_loc_descr_ref
15628 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15630 dw_loc_descr_ref cc_loc_result = NULL;
15631 dw_loc_descr_ref x0_ref
15632 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15633 dw_loc_descr_ref x1_ref
15634 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15636 if (x0_ref == 0 || x1_ref == 0)
15637 return 0;
15639 cc_loc_result = x0_ref;
15640 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15642 add_loc_descr (&cc_loc_result, x1_ref);
15643 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15645 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15646 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15648 return cc_loc_result;
15651 /* Return a descriptor that describes the concatenation of N
15652 locations. */
15654 static dw_loc_descr_ref
15655 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15657 unsigned int i;
15658 dw_loc_descr_ref cc_loc_result = NULL;
15659 unsigned int n = XVECLEN (concatn, 0);
15661 for (i = 0; i < n; ++i)
15663 dw_loc_descr_ref ref;
15664 rtx x = XVECEXP (concatn, 0, i);
15666 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15667 if (ref == NULL)
15668 return NULL;
15670 add_loc_descr (&cc_loc_result, ref);
15671 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15674 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15675 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15677 return cc_loc_result;
15680 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15681 for DEBUG_IMPLICIT_PTR RTL. */
15683 static dw_loc_descr_ref
15684 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15686 dw_loc_descr_ref ret;
15687 dw_die_ref ref;
15689 if (dwarf_strict && dwarf_version < 5)
15690 return NULL;
15691 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15692 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15693 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15694 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15695 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15696 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15697 if (ref)
15699 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15700 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15701 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15703 else
15705 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15706 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15708 return ret;
15711 /* Output a proper Dwarf location descriptor for a variable or parameter
15712 which is either allocated in a register or in a memory location. For a
15713 register, we just generate an OP_REG and the register number. For a
15714 memory location we provide a Dwarf postfix expression describing how to
15715 generate the (dynamic) address of the object onto the address stack.
15717 MODE is mode of the decl if this loc_descriptor is going to be used in
15718 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15719 allowed, VOIDmode otherwise.
15721 If we don't know how to describe it, return 0. */
15723 static dw_loc_descr_ref
15724 loc_descriptor (rtx rtl, machine_mode mode,
15725 enum var_init_status initialized)
15727 dw_loc_descr_ref loc_result = NULL;
15729 switch (GET_CODE (rtl))
15731 case SUBREG:
15732 /* The case of a subreg may arise when we have a local (register)
15733 variable or a formal (register) parameter which doesn't quite fill
15734 up an entire register. For now, just assume that it is
15735 legitimate to make the Dwarf info refer to the whole register which
15736 contains the given subreg. */
15737 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15738 loc_result = loc_descriptor (SUBREG_REG (rtl),
15739 GET_MODE (SUBREG_REG (rtl)), initialized);
15740 else
15741 goto do_default;
15742 break;
15744 case REG:
15745 loc_result = reg_loc_descriptor (rtl, initialized);
15746 break;
15748 case MEM:
15749 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15750 GET_MODE (rtl), initialized);
15751 if (loc_result == NULL)
15752 loc_result = tls_mem_loc_descriptor (rtl);
15753 if (loc_result == NULL)
15755 rtx new_rtl = avoid_constant_pool_reference (rtl);
15756 if (new_rtl != rtl)
15757 loc_result = loc_descriptor (new_rtl, mode, initialized);
15759 break;
15761 case CONCAT:
15762 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15763 initialized);
15764 break;
15766 case CONCATN:
15767 loc_result = concatn_loc_descriptor (rtl, initialized);
15768 break;
15770 case VAR_LOCATION:
15771 /* Single part. */
15772 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15774 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15775 if (GET_CODE (loc) == EXPR_LIST)
15776 loc = XEXP (loc, 0);
15777 loc_result = loc_descriptor (loc, mode, initialized);
15778 break;
15781 rtl = XEXP (rtl, 1);
15782 /* FALLTHRU */
15784 case PARALLEL:
15786 rtvec par_elems = XVEC (rtl, 0);
15787 int num_elem = GET_NUM_ELEM (par_elems);
15788 machine_mode mode;
15789 int i;
15791 /* Create the first one, so we have something to add to. */
15792 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15793 VOIDmode, initialized);
15794 if (loc_result == NULL)
15795 return NULL;
15796 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15797 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15798 for (i = 1; i < num_elem; i++)
15800 dw_loc_descr_ref temp;
15802 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15803 VOIDmode, initialized);
15804 if (temp == NULL)
15805 return NULL;
15806 add_loc_descr (&loc_result, temp);
15807 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15808 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15811 break;
15813 case CONST_INT:
15814 if (mode != VOIDmode && mode != BLKmode)
15815 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15816 INTVAL (rtl));
15817 break;
15819 case CONST_DOUBLE:
15820 if (mode == VOIDmode)
15821 mode = GET_MODE (rtl);
15823 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15825 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15827 /* Note that a CONST_DOUBLE rtx could represent either an integer
15828 or a floating-point constant. A CONST_DOUBLE is used whenever
15829 the constant requires more than one word in order to be
15830 adequately represented. We output CONST_DOUBLEs as blocks. */
15831 loc_result = new_loc_descr (DW_OP_implicit_value,
15832 GET_MODE_SIZE (mode), 0);
15833 #if TARGET_SUPPORTS_WIDE_INT == 0
15834 if (!SCALAR_FLOAT_MODE_P (mode))
15836 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15837 loc_result->dw_loc_oprnd2.v.val_double
15838 = rtx_to_double_int (rtl);
15840 else
15841 #endif
15843 unsigned int length = GET_MODE_SIZE (mode);
15844 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15846 insert_float (rtl, array);
15847 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15848 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15849 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15850 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15853 break;
15855 case CONST_WIDE_INT:
15856 if (mode == VOIDmode)
15857 mode = GET_MODE (rtl);
15859 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15861 loc_result = new_loc_descr (DW_OP_implicit_value,
15862 GET_MODE_SIZE (mode), 0);
15863 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15864 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15865 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15867 break;
15869 case CONST_VECTOR:
15870 if (mode == VOIDmode)
15871 mode = GET_MODE (rtl);
15873 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15875 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15876 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15877 unsigned char *array
15878 = ggc_vec_alloc<unsigned char> (length * elt_size);
15879 unsigned int i;
15880 unsigned char *p;
15881 machine_mode imode = GET_MODE_INNER (mode);
15883 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15884 switch (GET_MODE_CLASS (mode))
15886 case MODE_VECTOR_INT:
15887 for (i = 0, p = array; i < length; i++, p += elt_size)
15889 rtx elt = CONST_VECTOR_ELT (rtl, i);
15890 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15892 break;
15894 case MODE_VECTOR_FLOAT:
15895 for (i = 0, p = array; i < length; i++, p += elt_size)
15897 rtx elt = CONST_VECTOR_ELT (rtl, i);
15898 insert_float (elt, p);
15900 break;
15902 default:
15903 gcc_unreachable ();
15906 loc_result = new_loc_descr (DW_OP_implicit_value,
15907 length * elt_size, 0);
15908 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15909 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15910 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15911 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15913 break;
15915 case CONST:
15916 if (mode == VOIDmode
15917 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15918 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15919 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15921 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15922 break;
15924 /* FALLTHROUGH */
15925 case SYMBOL_REF:
15926 if (!const_ok_for_output (rtl))
15927 break;
15928 /* FALLTHROUGH */
15929 case LABEL_REF:
15930 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15931 && (dwarf_version >= 4 || !dwarf_strict))
15933 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15934 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15935 vec_safe_push (used_rtx_array, rtl);
15937 break;
15939 case DEBUG_IMPLICIT_PTR:
15940 loc_result = implicit_ptr_descriptor (rtl, 0);
15941 break;
15943 case PLUS:
15944 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15945 && CONST_INT_P (XEXP (rtl, 1)))
15947 loc_result
15948 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15949 break;
15951 /* FALLTHRU */
15952 do_default:
15953 default:
15954 if ((SCALAR_INT_MODE_P (mode)
15955 && GET_MODE (rtl) == mode
15956 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15957 && dwarf_version >= 4)
15958 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15960 /* Value expression. */
15961 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15962 if (loc_result)
15963 add_loc_descr (&loc_result,
15964 new_loc_descr (DW_OP_stack_value, 0, 0));
15966 break;
15969 return loc_result;
15972 /* We need to figure out what section we should use as the base for the
15973 address ranges where a given location is valid.
15974 1. If this particular DECL has a section associated with it, use that.
15975 2. If this function has a section associated with it, use that.
15976 3. Otherwise, use the text section.
15977 XXX: If you split a variable across multiple sections, we won't notice. */
15979 static const char *
15980 secname_for_decl (const_tree decl)
15982 const char *secname;
15984 if (VAR_OR_FUNCTION_DECL_P (decl)
15985 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
15986 && DECL_SECTION_NAME (decl))
15987 secname = DECL_SECTION_NAME (decl);
15988 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15989 secname = DECL_SECTION_NAME (current_function_decl);
15990 else if (cfun && in_cold_section_p)
15991 secname = crtl->subsections.cold_section_label;
15992 else
15993 secname = text_section_label;
15995 return secname;
15998 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16000 static bool
16001 decl_by_reference_p (tree decl)
16003 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16004 || VAR_P (decl))
16005 && DECL_BY_REFERENCE (decl));
16008 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16009 for VARLOC. */
16011 static dw_loc_descr_ref
16012 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16013 enum var_init_status initialized)
16015 int have_address = 0;
16016 dw_loc_descr_ref descr;
16017 machine_mode mode;
16019 if (want_address != 2)
16021 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16022 /* Single part. */
16023 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16025 varloc = PAT_VAR_LOCATION_LOC (varloc);
16026 if (GET_CODE (varloc) == EXPR_LIST)
16027 varloc = XEXP (varloc, 0);
16028 mode = GET_MODE (varloc);
16029 if (MEM_P (varloc))
16031 rtx addr = XEXP (varloc, 0);
16032 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16033 mode, initialized);
16034 if (descr)
16035 have_address = 1;
16036 else
16038 rtx x = avoid_constant_pool_reference (varloc);
16039 if (x != varloc)
16040 descr = mem_loc_descriptor (x, mode, VOIDmode,
16041 initialized);
16044 else
16045 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16047 else
16048 return 0;
16050 else
16052 if (GET_CODE (varloc) == VAR_LOCATION)
16053 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16054 else
16055 mode = DECL_MODE (loc);
16056 descr = loc_descriptor (varloc, mode, initialized);
16057 have_address = 1;
16060 if (!descr)
16061 return 0;
16063 if (want_address == 2 && !have_address
16064 && (dwarf_version >= 4 || !dwarf_strict))
16066 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16068 expansion_failed (loc, NULL_RTX,
16069 "DWARF address size mismatch");
16070 return 0;
16072 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16073 have_address = 1;
16075 /* Show if we can't fill the request for an address. */
16076 if (want_address && !have_address)
16078 expansion_failed (loc, NULL_RTX,
16079 "Want address and only have value");
16080 return 0;
16083 /* If we've got an address and don't want one, dereference. */
16084 if (!want_address && have_address)
16086 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16087 enum dwarf_location_atom op;
16089 if (size > DWARF2_ADDR_SIZE || size == -1)
16091 expansion_failed (loc, NULL_RTX,
16092 "DWARF address size mismatch");
16093 return 0;
16095 else if (size == DWARF2_ADDR_SIZE)
16096 op = DW_OP_deref;
16097 else
16098 op = DW_OP_deref_size;
16100 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16103 return descr;
16106 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16107 if it is not possible. */
16109 static dw_loc_descr_ref
16110 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16112 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16113 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16114 else if (dwarf_version >= 3 || !dwarf_strict)
16115 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16116 else
16117 return NULL;
16120 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16121 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16123 static dw_loc_descr_ref
16124 dw_sra_loc_expr (tree decl, rtx loc)
16126 rtx p;
16127 unsigned HOST_WIDE_INT padsize = 0;
16128 dw_loc_descr_ref descr, *descr_tail;
16129 unsigned HOST_WIDE_INT decl_size;
16130 rtx varloc;
16131 enum var_init_status initialized;
16133 if (DECL_SIZE (decl) == NULL
16134 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16135 return NULL;
16137 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16138 descr = NULL;
16139 descr_tail = &descr;
16141 for (p = loc; p; p = XEXP (p, 1))
16143 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16144 rtx loc_note = *decl_piece_varloc_ptr (p);
16145 dw_loc_descr_ref cur_descr;
16146 dw_loc_descr_ref *tail, last = NULL;
16147 unsigned HOST_WIDE_INT opsize = 0;
16149 if (loc_note == NULL_RTX
16150 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16152 padsize += bitsize;
16153 continue;
16155 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16156 varloc = NOTE_VAR_LOCATION (loc_note);
16157 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16158 if (cur_descr == NULL)
16160 padsize += bitsize;
16161 continue;
16164 /* Check that cur_descr either doesn't use
16165 DW_OP_*piece operations, or their sum is equal
16166 to bitsize. Otherwise we can't embed it. */
16167 for (tail = &cur_descr; *tail != NULL;
16168 tail = &(*tail)->dw_loc_next)
16169 if ((*tail)->dw_loc_opc == DW_OP_piece)
16171 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16172 * BITS_PER_UNIT;
16173 last = *tail;
16175 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16177 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16178 last = *tail;
16181 if (last != NULL && opsize != bitsize)
16183 padsize += bitsize;
16184 /* Discard the current piece of the descriptor and release any
16185 addr_table entries it uses. */
16186 remove_loc_list_addr_table_entries (cur_descr);
16187 continue;
16190 /* If there is a hole, add DW_OP_*piece after empty DWARF
16191 expression, which means that those bits are optimized out. */
16192 if (padsize)
16194 if (padsize > decl_size)
16196 remove_loc_list_addr_table_entries (cur_descr);
16197 goto discard_descr;
16199 decl_size -= padsize;
16200 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16201 if (*descr_tail == NULL)
16203 remove_loc_list_addr_table_entries (cur_descr);
16204 goto discard_descr;
16206 descr_tail = &(*descr_tail)->dw_loc_next;
16207 padsize = 0;
16209 *descr_tail = cur_descr;
16210 descr_tail = tail;
16211 if (bitsize > decl_size)
16212 goto discard_descr;
16213 decl_size -= bitsize;
16214 if (last == NULL)
16216 HOST_WIDE_INT offset = 0;
16217 if (GET_CODE (varloc) == VAR_LOCATION
16218 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16220 varloc = PAT_VAR_LOCATION_LOC (varloc);
16221 if (GET_CODE (varloc) == EXPR_LIST)
16222 varloc = XEXP (varloc, 0);
16226 if (GET_CODE (varloc) == CONST
16227 || GET_CODE (varloc) == SIGN_EXTEND
16228 || GET_CODE (varloc) == ZERO_EXTEND)
16229 varloc = XEXP (varloc, 0);
16230 else if (GET_CODE (varloc) == SUBREG)
16231 varloc = SUBREG_REG (varloc);
16232 else
16233 break;
16235 while (1);
16236 /* DW_OP_bit_size offset should be zero for register
16237 or implicit location descriptions and empty location
16238 descriptions, but for memory addresses needs big endian
16239 adjustment. */
16240 if (MEM_P (varloc))
16242 unsigned HOST_WIDE_INT memsize
16243 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16244 if (memsize != bitsize)
16246 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16247 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16248 goto discard_descr;
16249 if (memsize < bitsize)
16250 goto discard_descr;
16251 if (BITS_BIG_ENDIAN)
16252 offset = memsize - bitsize;
16256 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16257 if (*descr_tail == NULL)
16258 goto discard_descr;
16259 descr_tail = &(*descr_tail)->dw_loc_next;
16263 /* If there were any non-empty expressions, add padding till the end of
16264 the decl. */
16265 if (descr != NULL && decl_size != 0)
16267 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16268 if (*descr_tail == NULL)
16269 goto discard_descr;
16271 return descr;
16273 discard_descr:
16274 /* Discard the descriptor and release any addr_table entries it uses. */
16275 remove_loc_list_addr_table_entries (descr);
16276 return NULL;
16279 /* Return the dwarf representation of the location list LOC_LIST of
16280 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16281 function. */
16283 static dw_loc_list_ref
16284 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16286 const char *endname, *secname;
16287 rtx varloc;
16288 enum var_init_status initialized;
16289 struct var_loc_node *node;
16290 dw_loc_descr_ref descr;
16291 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16292 dw_loc_list_ref list = NULL;
16293 dw_loc_list_ref *listp = &list;
16295 /* Now that we know what section we are using for a base,
16296 actually construct the list of locations.
16297 The first location information is what is passed to the
16298 function that creates the location list, and the remaining
16299 locations just get added on to that list.
16300 Note that we only know the start address for a location
16301 (IE location changes), so to build the range, we use
16302 the range [current location start, next location start].
16303 This means we have to special case the last node, and generate
16304 a range of [last location start, end of function label]. */
16306 secname = secname_for_decl (decl);
16308 for (node = loc_list->first; node; node = node->next)
16309 if (GET_CODE (node->loc) == EXPR_LIST
16310 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16312 if (GET_CODE (node->loc) == EXPR_LIST)
16314 /* This requires DW_OP_{,bit_}piece, which is not usable
16315 inside DWARF expressions. */
16316 if (want_address != 2)
16317 continue;
16318 descr = dw_sra_loc_expr (decl, node->loc);
16319 if (descr == NULL)
16320 continue;
16322 else
16324 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16325 varloc = NOTE_VAR_LOCATION (node->loc);
16326 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16328 if (descr)
16330 bool range_across_switch = false;
16331 /* If section switch happens in between node->label
16332 and node->next->label (or end of function) and
16333 we can't emit it as a single entry list,
16334 emit two ranges, first one ending at the end
16335 of first partition and second one starting at the
16336 beginning of second partition. */
16337 if (node == loc_list->last_before_switch
16338 && (node != loc_list->first || loc_list->first->next)
16339 && current_function_decl)
16341 endname = cfun->fde->dw_fde_end;
16342 range_across_switch = true;
16344 /* The variable has a location between NODE->LABEL and
16345 NODE->NEXT->LABEL. */
16346 else if (node->next)
16347 endname = node->next->label;
16348 /* If the variable has a location at the last label
16349 it keeps its location until the end of function. */
16350 else if (!current_function_decl)
16351 endname = text_end_label;
16352 else
16354 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16355 current_function_funcdef_no);
16356 endname = ggc_strdup (label_id);
16359 *listp = new_loc_list (descr, node->label, endname, secname);
16360 if (TREE_CODE (decl) == PARM_DECL
16361 && node == loc_list->first
16362 && NOTE_P (node->loc)
16363 && strcmp (node->label, endname) == 0)
16364 (*listp)->force = true;
16365 listp = &(*listp)->dw_loc_next;
16367 if (range_across_switch)
16369 if (GET_CODE (node->loc) == EXPR_LIST)
16370 descr = dw_sra_loc_expr (decl, node->loc);
16371 else
16373 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16374 varloc = NOTE_VAR_LOCATION (node->loc);
16375 descr = dw_loc_list_1 (decl, varloc, want_address,
16376 initialized);
16378 gcc_assert (descr);
16379 /* The variable has a location between NODE->LABEL and
16380 NODE->NEXT->LABEL. */
16381 if (node->next)
16382 endname = node->next->label;
16383 else
16384 endname = cfun->fde->dw_fde_second_end;
16385 *listp = new_loc_list (descr,
16386 cfun->fde->dw_fde_second_begin,
16387 endname, secname);
16388 listp = &(*listp)->dw_loc_next;
16393 /* Try to avoid the overhead of a location list emitting a location
16394 expression instead, but only if we didn't have more than one
16395 location entry in the first place. If some entries were not
16396 representable, we don't want to pretend a single entry that was
16397 applies to the entire scope in which the variable is
16398 available. */
16399 if (list && loc_list->first->next)
16400 gen_llsym (list);
16402 return list;
16405 /* Return if the loc_list has only single element and thus can be represented
16406 as location description. */
16408 static bool
16409 single_element_loc_list_p (dw_loc_list_ref list)
16411 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16412 return !list->ll_symbol;
16415 /* Duplicate a single element of location list. */
16417 static inline dw_loc_descr_ref
16418 copy_loc_descr (dw_loc_descr_ref ref)
16420 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16421 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16422 return copy;
16425 /* To each location in list LIST append loc descr REF. */
16427 static void
16428 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16430 dw_loc_descr_ref copy;
16431 add_loc_descr (&list->expr, ref);
16432 list = list->dw_loc_next;
16433 while (list)
16435 copy = copy_loc_descr (ref);
16436 add_loc_descr (&list->expr, copy);
16437 while (copy->dw_loc_next)
16438 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16439 list = list->dw_loc_next;
16443 /* To each location in list LIST prepend loc descr REF. */
16445 static void
16446 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16448 dw_loc_descr_ref copy;
16449 dw_loc_descr_ref ref_end = list->expr;
16450 add_loc_descr (&ref, list->expr);
16451 list->expr = ref;
16452 list = list->dw_loc_next;
16453 while (list)
16455 dw_loc_descr_ref end = list->expr;
16456 list->expr = copy = copy_loc_descr (ref);
16457 while (copy->dw_loc_next != ref_end)
16458 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16459 copy->dw_loc_next = end;
16460 list = list->dw_loc_next;
16464 /* Given two lists RET and LIST
16465 produce location list that is result of adding expression in LIST
16466 to expression in RET on each position in program.
16467 Might be destructive on both RET and LIST.
16469 TODO: We handle only simple cases of RET or LIST having at most one
16470 element. General case would involve sorting the lists in program order
16471 and merging them that will need some additional work.
16472 Adding that will improve quality of debug info especially for SRA-ed
16473 structures. */
16475 static void
16476 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16478 if (!list)
16479 return;
16480 if (!*ret)
16482 *ret = list;
16483 return;
16485 if (!list->dw_loc_next)
16487 add_loc_descr_to_each (*ret, list->expr);
16488 return;
16490 if (!(*ret)->dw_loc_next)
16492 prepend_loc_descr_to_each (list, (*ret)->expr);
16493 *ret = list;
16494 return;
16496 expansion_failed (NULL_TREE, NULL_RTX,
16497 "Don't know how to merge two non-trivial"
16498 " location lists.\n");
16499 *ret = NULL;
16500 return;
16503 /* LOC is constant expression. Try a luck, look it up in constant
16504 pool and return its loc_descr of its address. */
16506 static dw_loc_descr_ref
16507 cst_pool_loc_descr (tree loc)
16509 /* Get an RTL for this, if something has been emitted. */
16510 rtx rtl = lookup_constant_def (loc);
16512 if (!rtl || !MEM_P (rtl))
16514 gcc_assert (!rtl);
16515 return 0;
16517 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16519 /* TODO: We might get more coverage if we was actually delaying expansion
16520 of all expressions till end of compilation when constant pools are fully
16521 populated. */
16522 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16524 expansion_failed (loc, NULL_RTX,
16525 "CST value in contant pool but not marked.");
16526 return 0;
16528 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16529 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16532 /* Return dw_loc_list representing address of addr_expr LOC
16533 by looking for inner INDIRECT_REF expression and turning
16534 it into simple arithmetics.
16536 See loc_list_from_tree for the meaning of CONTEXT. */
16538 static dw_loc_list_ref
16539 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16540 loc_descr_context *context)
16542 tree obj, offset;
16543 HOST_WIDE_INT bitsize, bitpos, bytepos;
16544 machine_mode mode;
16545 int unsignedp, reversep, volatilep = 0;
16546 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16548 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16549 &bitsize, &bitpos, &offset, &mode,
16550 &unsignedp, &reversep, &volatilep);
16551 STRIP_NOPS (obj);
16552 if (bitpos % BITS_PER_UNIT)
16554 expansion_failed (loc, NULL_RTX, "bitfield access");
16555 return 0;
16557 if (!INDIRECT_REF_P (obj))
16559 expansion_failed (obj,
16560 NULL_RTX, "no indirect ref in inner refrence");
16561 return 0;
16563 if (!offset && !bitpos)
16564 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16565 context);
16566 else if (toplev
16567 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16568 && (dwarf_version >= 4 || !dwarf_strict))
16570 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16571 if (!list_ret)
16572 return 0;
16573 if (offset)
16575 /* Variable offset. */
16576 list_ret1 = loc_list_from_tree (offset, 0, context);
16577 if (list_ret1 == 0)
16578 return 0;
16579 add_loc_list (&list_ret, list_ret1);
16580 if (!list_ret)
16581 return 0;
16582 add_loc_descr_to_each (list_ret,
16583 new_loc_descr (DW_OP_plus, 0, 0));
16585 bytepos = bitpos / BITS_PER_UNIT;
16586 if (bytepos > 0)
16587 add_loc_descr_to_each (list_ret,
16588 new_loc_descr (DW_OP_plus_uconst,
16589 bytepos, 0));
16590 else if (bytepos < 0)
16591 loc_list_plus_const (list_ret, bytepos);
16592 add_loc_descr_to_each (list_ret,
16593 new_loc_descr (DW_OP_stack_value, 0, 0));
16595 return list_ret;
16598 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16599 all operations from LOC are nops, move to the last one. Insert in NOPS all
16600 operations that are skipped. */
16602 static void
16603 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16604 hash_set<dw_loc_descr_ref> &nops)
16606 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16608 nops.add (loc);
16609 loc = loc->dw_loc_next;
16613 /* Helper for loc_descr_without_nops: free the location description operation
16614 P. */
16616 bool
16617 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16619 ggc_free (loc);
16620 return true;
16623 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16624 finishes LOC. */
16626 static void
16627 loc_descr_without_nops (dw_loc_descr_ref &loc)
16629 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16630 return;
16632 /* Set of all DW_OP_nop operations we remove. */
16633 hash_set<dw_loc_descr_ref> nops;
16635 /* First, strip all prefix NOP operations in order to keep the head of the
16636 operations list. */
16637 loc_descr_to_next_no_nop (loc, nops);
16639 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16641 /* For control flow operations: strip "prefix" nops in destination
16642 labels. */
16643 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16644 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16645 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16646 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16648 /* Do the same for the operations that follow, then move to the next
16649 iteration. */
16650 if (cur->dw_loc_next != NULL)
16651 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16652 cur = cur->dw_loc_next;
16655 nops.traverse<void *, free_loc_descr> (NULL);
16659 struct dwarf_procedure_info;
16661 /* Helper structure for location descriptions generation. */
16662 struct loc_descr_context
16664 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16665 NULL_TREE if DW_OP_push_object_address in invalid for this location
16666 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16667 tree context_type;
16668 /* The ..._DECL node that should be translated as a
16669 DW_OP_push_object_address operation. */
16670 tree base_decl;
16671 /* Information about the DWARF procedure we are currently generating. NULL if
16672 we are not generating a DWARF procedure. */
16673 struct dwarf_procedure_info *dpi;
16674 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16675 by consumer. Used for DW_TAG_generic_subrange attributes. */
16676 bool placeholder_arg;
16677 /* True if PLACEHOLDER_EXPR has been seen. */
16678 bool placeholder_seen;
16681 /* DWARF procedures generation
16683 DWARF expressions (aka. location descriptions) are used to encode variable
16684 things such as sizes or offsets. Such computations can have redundant parts
16685 that can be factorized in order to reduce the size of the output debug
16686 information. This is the whole point of DWARF procedures.
16688 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16689 already factorized into functions ("size functions") in order to handle very
16690 big and complex types. Such functions are quite simple: they have integral
16691 arguments, they return an integral result and their body contains only a
16692 return statement with arithmetic expressions. This is the only kind of
16693 function we are interested in translating into DWARF procedures, here.
16695 DWARF expressions and DWARF procedure are executed using a stack, so we have
16696 to define some calling convention for them to interact. Let's say that:
16698 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16699 all arguments in reverse order (right-to-left) so that when the DWARF
16700 procedure execution starts, the first argument is the top of the stack.
16702 - Then, when returning, the DWARF procedure must have consumed all arguments
16703 on the stack, must have pushed the result and touched nothing else.
16705 - Each integral argument and the result are integral types can be hold in a
16706 single stack slot.
16708 - We call "frame offset" the number of stack slots that are "under DWARF
16709 procedure control": it includes the arguments slots, the temporaries and
16710 the result slot. Thus, it is equal to the number of arguments when the
16711 procedure execution starts and must be equal to one (the result) when it
16712 returns. */
16714 /* Helper structure used when generating operations for a DWARF procedure. */
16715 struct dwarf_procedure_info
16717 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16718 currently translated. */
16719 tree fndecl;
16720 /* The number of arguments FNDECL takes. */
16721 unsigned args_count;
16724 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16725 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16726 equate it to this DIE. */
16728 static dw_die_ref
16729 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16730 dw_die_ref parent_die)
16732 dw_die_ref dwarf_proc_die;
16734 if ((dwarf_version < 3 && dwarf_strict)
16735 || location == NULL)
16736 return NULL;
16738 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16739 if (fndecl)
16740 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16741 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16742 return dwarf_proc_die;
16745 /* Return whether TYPE is a supported type as a DWARF procedure argument
16746 type or return type (we handle only scalar types and pointer types that
16747 aren't wider than the DWARF expression evaluation stack. */
16749 static bool
16750 is_handled_procedure_type (tree type)
16752 return ((INTEGRAL_TYPE_P (type)
16753 || TREE_CODE (type) == OFFSET_TYPE
16754 || TREE_CODE (type) == POINTER_TYPE)
16755 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16758 /* Helper for resolve_args_picking: do the same but stop when coming across
16759 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16760 offset *before* evaluating the corresponding operation. */
16762 static bool
16763 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16764 struct dwarf_procedure_info *dpi,
16765 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16767 /* The "frame_offset" identifier is already used to name a macro... */
16768 unsigned frame_offset_ = initial_frame_offset;
16769 dw_loc_descr_ref l;
16771 for (l = loc; l != NULL;)
16773 bool existed;
16774 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16776 /* If we already met this node, there is nothing to compute anymore. */
16777 if (existed)
16779 /* Make sure that the stack size is consistent wherever the execution
16780 flow comes from. */
16781 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16782 break;
16784 l_frame_offset = frame_offset_;
16786 /* If needed, relocate the picking offset with respect to the frame
16787 offset. */
16788 if (l->frame_offset_rel)
16790 unsigned HOST_WIDE_INT off;
16791 switch (l->dw_loc_opc)
16793 case DW_OP_pick:
16794 off = l->dw_loc_oprnd1.v.val_unsigned;
16795 break;
16796 case DW_OP_dup:
16797 off = 0;
16798 break;
16799 case DW_OP_over:
16800 off = 1;
16801 break;
16802 default:
16803 gcc_unreachable ();
16805 /* frame_offset_ is the size of the current stack frame, including
16806 incoming arguments. Besides, the arguments are pushed
16807 right-to-left. Thus, in order to access the Nth argument from
16808 this operation node, the picking has to skip temporaries *plus*
16809 one stack slot per argument (0 for the first one, 1 for the second
16810 one, etc.).
16812 The targetted argument number (N) is already set as the operand,
16813 and the number of temporaries can be computed with:
16814 frame_offsets_ - dpi->args_count */
16815 off += frame_offset_ - dpi->args_count;
16817 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16818 if (off > 255)
16819 return false;
16821 if (off == 0)
16823 l->dw_loc_opc = DW_OP_dup;
16824 l->dw_loc_oprnd1.v.val_unsigned = 0;
16826 else if (off == 1)
16828 l->dw_loc_opc = DW_OP_over;
16829 l->dw_loc_oprnd1.v.val_unsigned = 0;
16831 else
16833 l->dw_loc_opc = DW_OP_pick;
16834 l->dw_loc_oprnd1.v.val_unsigned = off;
16838 /* Update frame_offset according to the effect the current operation has
16839 on the stack. */
16840 switch (l->dw_loc_opc)
16842 case DW_OP_deref:
16843 case DW_OP_swap:
16844 case DW_OP_rot:
16845 case DW_OP_abs:
16846 case DW_OP_neg:
16847 case DW_OP_not:
16848 case DW_OP_plus_uconst:
16849 case DW_OP_skip:
16850 case DW_OP_reg0:
16851 case DW_OP_reg1:
16852 case DW_OP_reg2:
16853 case DW_OP_reg3:
16854 case DW_OP_reg4:
16855 case DW_OP_reg5:
16856 case DW_OP_reg6:
16857 case DW_OP_reg7:
16858 case DW_OP_reg8:
16859 case DW_OP_reg9:
16860 case DW_OP_reg10:
16861 case DW_OP_reg11:
16862 case DW_OP_reg12:
16863 case DW_OP_reg13:
16864 case DW_OP_reg14:
16865 case DW_OP_reg15:
16866 case DW_OP_reg16:
16867 case DW_OP_reg17:
16868 case DW_OP_reg18:
16869 case DW_OP_reg19:
16870 case DW_OP_reg20:
16871 case DW_OP_reg21:
16872 case DW_OP_reg22:
16873 case DW_OP_reg23:
16874 case DW_OP_reg24:
16875 case DW_OP_reg25:
16876 case DW_OP_reg26:
16877 case DW_OP_reg27:
16878 case DW_OP_reg28:
16879 case DW_OP_reg29:
16880 case DW_OP_reg30:
16881 case DW_OP_reg31:
16882 case DW_OP_bregx:
16883 case DW_OP_piece:
16884 case DW_OP_deref_size:
16885 case DW_OP_nop:
16886 case DW_OP_bit_piece:
16887 case DW_OP_implicit_value:
16888 case DW_OP_stack_value:
16889 break;
16891 case DW_OP_addr:
16892 case DW_OP_const1u:
16893 case DW_OP_const1s:
16894 case DW_OP_const2u:
16895 case DW_OP_const2s:
16896 case DW_OP_const4u:
16897 case DW_OP_const4s:
16898 case DW_OP_const8u:
16899 case DW_OP_const8s:
16900 case DW_OP_constu:
16901 case DW_OP_consts:
16902 case DW_OP_dup:
16903 case DW_OP_over:
16904 case DW_OP_pick:
16905 case DW_OP_lit0:
16906 case DW_OP_lit1:
16907 case DW_OP_lit2:
16908 case DW_OP_lit3:
16909 case DW_OP_lit4:
16910 case DW_OP_lit5:
16911 case DW_OP_lit6:
16912 case DW_OP_lit7:
16913 case DW_OP_lit8:
16914 case DW_OP_lit9:
16915 case DW_OP_lit10:
16916 case DW_OP_lit11:
16917 case DW_OP_lit12:
16918 case DW_OP_lit13:
16919 case DW_OP_lit14:
16920 case DW_OP_lit15:
16921 case DW_OP_lit16:
16922 case DW_OP_lit17:
16923 case DW_OP_lit18:
16924 case DW_OP_lit19:
16925 case DW_OP_lit20:
16926 case DW_OP_lit21:
16927 case DW_OP_lit22:
16928 case DW_OP_lit23:
16929 case DW_OP_lit24:
16930 case DW_OP_lit25:
16931 case DW_OP_lit26:
16932 case DW_OP_lit27:
16933 case DW_OP_lit28:
16934 case DW_OP_lit29:
16935 case DW_OP_lit30:
16936 case DW_OP_lit31:
16937 case DW_OP_breg0:
16938 case DW_OP_breg1:
16939 case DW_OP_breg2:
16940 case DW_OP_breg3:
16941 case DW_OP_breg4:
16942 case DW_OP_breg5:
16943 case DW_OP_breg6:
16944 case DW_OP_breg7:
16945 case DW_OP_breg8:
16946 case DW_OP_breg9:
16947 case DW_OP_breg10:
16948 case DW_OP_breg11:
16949 case DW_OP_breg12:
16950 case DW_OP_breg13:
16951 case DW_OP_breg14:
16952 case DW_OP_breg15:
16953 case DW_OP_breg16:
16954 case DW_OP_breg17:
16955 case DW_OP_breg18:
16956 case DW_OP_breg19:
16957 case DW_OP_breg20:
16958 case DW_OP_breg21:
16959 case DW_OP_breg22:
16960 case DW_OP_breg23:
16961 case DW_OP_breg24:
16962 case DW_OP_breg25:
16963 case DW_OP_breg26:
16964 case DW_OP_breg27:
16965 case DW_OP_breg28:
16966 case DW_OP_breg29:
16967 case DW_OP_breg30:
16968 case DW_OP_breg31:
16969 case DW_OP_fbreg:
16970 case DW_OP_push_object_address:
16971 case DW_OP_call_frame_cfa:
16972 case DW_OP_GNU_variable_value:
16973 ++frame_offset_;
16974 break;
16976 case DW_OP_drop:
16977 case DW_OP_xderef:
16978 case DW_OP_and:
16979 case DW_OP_div:
16980 case DW_OP_minus:
16981 case DW_OP_mod:
16982 case DW_OP_mul:
16983 case DW_OP_or:
16984 case DW_OP_plus:
16985 case DW_OP_shl:
16986 case DW_OP_shr:
16987 case DW_OP_shra:
16988 case DW_OP_xor:
16989 case DW_OP_bra:
16990 case DW_OP_eq:
16991 case DW_OP_ge:
16992 case DW_OP_gt:
16993 case DW_OP_le:
16994 case DW_OP_lt:
16995 case DW_OP_ne:
16996 case DW_OP_regx:
16997 case DW_OP_xderef_size:
16998 --frame_offset_;
16999 break;
17001 case DW_OP_call2:
17002 case DW_OP_call4:
17003 case DW_OP_call_ref:
17005 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17006 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17008 if (stack_usage == NULL)
17009 return false;
17010 frame_offset_ += *stack_usage;
17011 break;
17014 case DW_OP_implicit_pointer:
17015 case DW_OP_entry_value:
17016 case DW_OP_const_type:
17017 case DW_OP_regval_type:
17018 case DW_OP_deref_type:
17019 case DW_OP_convert:
17020 case DW_OP_reinterpret:
17021 case DW_OP_form_tls_address:
17022 case DW_OP_GNU_push_tls_address:
17023 case DW_OP_GNU_uninit:
17024 case DW_OP_GNU_encoded_addr:
17025 case DW_OP_GNU_implicit_pointer:
17026 case DW_OP_GNU_entry_value:
17027 case DW_OP_GNU_const_type:
17028 case DW_OP_GNU_regval_type:
17029 case DW_OP_GNU_deref_type:
17030 case DW_OP_GNU_convert:
17031 case DW_OP_GNU_reinterpret:
17032 case DW_OP_GNU_parameter_ref:
17033 /* loc_list_from_tree will probably not output these operations for
17034 size functions, so assume they will not appear here. */
17035 /* Fall through... */
17037 default:
17038 gcc_unreachable ();
17041 /* Now, follow the control flow (except subroutine calls). */
17042 switch (l->dw_loc_opc)
17044 case DW_OP_bra:
17045 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17046 frame_offsets))
17047 return false;
17048 /* Fall through. */
17050 case DW_OP_skip:
17051 l = l->dw_loc_oprnd1.v.val_loc;
17052 break;
17054 case DW_OP_stack_value:
17055 return true;
17057 default:
17058 l = l->dw_loc_next;
17059 break;
17063 return true;
17066 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17067 operations) in order to resolve the operand of DW_OP_pick operations that
17068 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17069 offset *before* LOC is executed. Return if all relocations were
17070 successful. */
17072 static bool
17073 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17074 struct dwarf_procedure_info *dpi)
17076 /* Associate to all visited operations the frame offset *before* evaluating
17077 this operation. */
17078 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17080 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17081 frame_offsets);
17084 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17085 Return NULL if it is not possible. */
17087 static dw_die_ref
17088 function_to_dwarf_procedure (tree fndecl)
17090 struct loc_descr_context ctx;
17091 struct dwarf_procedure_info dpi;
17092 dw_die_ref dwarf_proc_die;
17093 tree tree_body = DECL_SAVED_TREE (fndecl);
17094 dw_loc_descr_ref loc_body, epilogue;
17096 tree cursor;
17097 unsigned i;
17099 /* Do not generate multiple DWARF procedures for the same function
17100 declaration. */
17101 dwarf_proc_die = lookup_decl_die (fndecl);
17102 if (dwarf_proc_die != NULL)
17103 return dwarf_proc_die;
17105 /* DWARF procedures are available starting with the DWARFv3 standard. */
17106 if (dwarf_version < 3 && dwarf_strict)
17107 return NULL;
17109 /* We handle only functions for which we still have a body, that return a
17110 supported type and that takes arguments with supported types. Note that
17111 there is no point translating functions that return nothing. */
17112 if (tree_body == NULL_TREE
17113 || DECL_RESULT (fndecl) == NULL_TREE
17114 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17115 return NULL;
17117 for (cursor = DECL_ARGUMENTS (fndecl);
17118 cursor != NULL_TREE;
17119 cursor = TREE_CHAIN (cursor))
17120 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17121 return NULL;
17123 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17124 if (TREE_CODE (tree_body) != RETURN_EXPR)
17125 return NULL;
17126 tree_body = TREE_OPERAND (tree_body, 0);
17127 if (TREE_CODE (tree_body) != MODIFY_EXPR
17128 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17129 return NULL;
17130 tree_body = TREE_OPERAND (tree_body, 1);
17132 /* Try to translate the body expression itself. Note that this will probably
17133 cause an infinite recursion if its call graph has a cycle. This is very
17134 unlikely for size functions, however, so don't bother with such things at
17135 the moment. */
17136 ctx.context_type = NULL_TREE;
17137 ctx.base_decl = NULL_TREE;
17138 ctx.dpi = &dpi;
17139 ctx.placeholder_arg = false;
17140 ctx.placeholder_seen = false;
17141 dpi.fndecl = fndecl;
17142 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17143 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17144 if (!loc_body)
17145 return NULL;
17147 /* After evaluating all operands in "loc_body", we should still have on the
17148 stack all arguments plus the desired function result (top of the stack).
17149 Generate code in order to keep only the result in our stack frame. */
17150 epilogue = NULL;
17151 for (i = 0; i < dpi.args_count; ++i)
17153 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17154 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17155 op_couple->dw_loc_next->dw_loc_next = epilogue;
17156 epilogue = op_couple;
17158 add_loc_descr (&loc_body, epilogue);
17159 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17160 return NULL;
17162 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17163 because they are considered useful. Now there is an epilogue, they are
17164 not anymore, so give it another try. */
17165 loc_descr_without_nops (loc_body);
17167 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17168 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17169 though, given that size functions do not come from source, so they should
17170 not have a dedicated DW_TAG_subprogram DIE. */
17171 dwarf_proc_die
17172 = new_dwarf_proc_die (loc_body, fndecl,
17173 get_context_die (DECL_CONTEXT (fndecl)));
17175 /* The called DWARF procedure consumes one stack slot per argument and
17176 returns one stack slot. */
17177 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17179 return dwarf_proc_die;
17183 /* Generate Dwarf location list representing LOC.
17184 If WANT_ADDRESS is false, expression computing LOC will be computed
17185 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17186 if WANT_ADDRESS is 2, expression computing address useable in location
17187 will be returned (i.e. DW_OP_reg can be used
17188 to refer to register values).
17190 CONTEXT provides information to customize the location descriptions
17191 generation. Its context_type field specifies what type is implicitly
17192 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17193 will not be generated.
17195 Its DPI field determines whether we are generating a DWARF expression for a
17196 DWARF procedure, so PARM_DECL references are processed specifically.
17198 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17199 and dpi fields were null. */
17201 static dw_loc_list_ref
17202 loc_list_from_tree_1 (tree loc, int want_address,
17203 struct loc_descr_context *context)
17205 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17206 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17207 int have_address = 0;
17208 enum dwarf_location_atom op;
17210 /* ??? Most of the time we do not take proper care for sign/zero
17211 extending the values properly. Hopefully this won't be a real
17212 problem... */
17214 if (context != NULL
17215 && context->base_decl == loc
17216 && want_address == 0)
17218 if (dwarf_version >= 3 || !dwarf_strict)
17219 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17220 NULL, NULL, NULL);
17221 else
17222 return NULL;
17225 switch (TREE_CODE (loc))
17227 case ERROR_MARK:
17228 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17229 return 0;
17231 case PLACEHOLDER_EXPR:
17232 /* This case involves extracting fields from an object to determine the
17233 position of other fields. It is supposed to appear only as the first
17234 operand of COMPONENT_REF nodes and to reference precisely the type
17235 that the context allows. */
17236 if (context != NULL
17237 && TREE_TYPE (loc) == context->context_type
17238 && want_address >= 1)
17240 if (dwarf_version >= 3 || !dwarf_strict)
17242 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17243 have_address = 1;
17244 break;
17246 else
17247 return NULL;
17249 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17250 the single argument passed by consumer. */
17251 else if (context != NULL
17252 && context->placeholder_arg
17253 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17254 && want_address == 0)
17256 ret = new_loc_descr (DW_OP_pick, 0, 0);
17257 ret->frame_offset_rel = 1;
17258 context->placeholder_seen = true;
17259 break;
17261 else
17262 expansion_failed (loc, NULL_RTX,
17263 "PLACEHOLDER_EXPR for an unexpected type");
17264 break;
17266 case CALL_EXPR:
17268 const int nargs = call_expr_nargs (loc);
17269 tree callee = get_callee_fndecl (loc);
17270 int i;
17271 dw_die_ref dwarf_proc;
17273 if (callee == NULL_TREE)
17274 goto call_expansion_failed;
17276 /* We handle only functions that return an integer. */
17277 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17278 goto call_expansion_failed;
17280 dwarf_proc = function_to_dwarf_procedure (callee);
17281 if (dwarf_proc == NULL)
17282 goto call_expansion_failed;
17284 /* Evaluate arguments right-to-left so that the first argument will
17285 be the top-most one on the stack. */
17286 for (i = nargs - 1; i >= 0; --i)
17288 dw_loc_descr_ref loc_descr
17289 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17290 context);
17292 if (loc_descr == NULL)
17293 goto call_expansion_failed;
17295 add_loc_descr (&ret, loc_descr);
17298 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17299 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17300 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17301 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17302 add_loc_descr (&ret, ret1);
17303 break;
17305 call_expansion_failed:
17306 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17307 /* There are no opcodes for these operations. */
17308 return 0;
17311 case PREINCREMENT_EXPR:
17312 case PREDECREMENT_EXPR:
17313 case POSTINCREMENT_EXPR:
17314 case POSTDECREMENT_EXPR:
17315 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17316 /* There are no opcodes for these operations. */
17317 return 0;
17319 case ADDR_EXPR:
17320 /* If we already want an address, see if there is INDIRECT_REF inside
17321 e.g. for &this->field. */
17322 if (want_address)
17324 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17325 (loc, want_address == 2, context);
17326 if (list_ret)
17327 have_address = 1;
17328 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17329 && (ret = cst_pool_loc_descr (loc)))
17330 have_address = 1;
17332 /* Otherwise, process the argument and look for the address. */
17333 if (!list_ret && !ret)
17334 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17335 else
17337 if (want_address)
17338 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17339 return NULL;
17341 break;
17343 case VAR_DECL:
17344 if (DECL_THREAD_LOCAL_P (loc))
17346 rtx rtl;
17347 enum dwarf_location_atom tls_op;
17348 enum dtprel_bool dtprel = dtprel_false;
17350 if (targetm.have_tls)
17352 /* If this is not defined, we have no way to emit the
17353 data. */
17354 if (!targetm.asm_out.output_dwarf_dtprel)
17355 return 0;
17357 /* The way DW_OP_GNU_push_tls_address is specified, we
17358 can only look up addresses of objects in the current
17359 module. We used DW_OP_addr as first op, but that's
17360 wrong, because DW_OP_addr is relocated by the debug
17361 info consumer, while DW_OP_GNU_push_tls_address
17362 operand shouldn't be. */
17363 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17364 return 0;
17365 dtprel = dtprel_true;
17366 /* We check for DWARF 5 here because gdb did not implement
17367 DW_OP_form_tls_address until after 7.12. */
17368 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17369 : DW_OP_GNU_push_tls_address);
17371 else
17373 if (!targetm.emutls.debug_form_tls_address
17374 || !(dwarf_version >= 3 || !dwarf_strict))
17375 return 0;
17376 /* We stuffed the control variable into the DECL_VALUE_EXPR
17377 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17378 no longer appear in gimple code. We used the control
17379 variable in specific so that we could pick it up here. */
17380 loc = DECL_VALUE_EXPR (loc);
17381 tls_op = DW_OP_form_tls_address;
17384 rtl = rtl_for_decl_location (loc);
17385 if (rtl == NULL_RTX)
17386 return 0;
17388 if (!MEM_P (rtl))
17389 return 0;
17390 rtl = XEXP (rtl, 0);
17391 if (! CONSTANT_P (rtl))
17392 return 0;
17394 ret = new_addr_loc_descr (rtl, dtprel);
17395 ret1 = new_loc_descr (tls_op, 0, 0);
17396 add_loc_descr (&ret, ret1);
17398 have_address = 1;
17399 break;
17401 /* FALLTHRU */
17403 case PARM_DECL:
17404 if (context != NULL && context->dpi != NULL
17405 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17407 /* We are generating code for a DWARF procedure and we want to access
17408 one of its arguments: find the appropriate argument offset and let
17409 the resolve_args_picking pass compute the offset that complies
17410 with the stack frame size. */
17411 unsigned i = 0;
17412 tree cursor;
17414 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17415 cursor != NULL_TREE && cursor != loc;
17416 cursor = TREE_CHAIN (cursor), ++i)
17418 /* If we are translating a DWARF procedure, all referenced parameters
17419 must belong to the current function. */
17420 gcc_assert (cursor != NULL_TREE);
17422 ret = new_loc_descr (DW_OP_pick, i, 0);
17423 ret->frame_offset_rel = 1;
17424 break;
17426 /* FALLTHRU */
17428 case RESULT_DECL:
17429 if (DECL_HAS_VALUE_EXPR_P (loc))
17430 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17431 want_address, context);
17432 /* FALLTHRU */
17434 case FUNCTION_DECL:
17436 rtx rtl;
17437 var_loc_list *loc_list = lookup_decl_loc (loc);
17439 if (loc_list && loc_list->first)
17441 list_ret = dw_loc_list (loc_list, loc, want_address);
17442 have_address = want_address != 0;
17443 break;
17445 rtl = rtl_for_decl_location (loc);
17446 if (rtl == NULL_RTX)
17448 if (TREE_CODE (loc) != FUNCTION_DECL
17449 && early_dwarf
17450 && current_function_decl
17451 && want_address != 1
17452 && ! DECL_IGNORED_P (loc)
17453 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17454 || POINTER_TYPE_P (TREE_TYPE (loc)))
17455 && DECL_CONTEXT (loc) == current_function_decl
17456 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17457 <= DWARF2_ADDR_SIZE))
17459 dw_die_ref ref = lookup_decl_die (loc);
17460 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17461 if (ref)
17463 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17464 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17465 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17467 else
17469 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17470 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17472 break;
17474 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17475 return 0;
17477 else if (CONST_INT_P (rtl))
17479 HOST_WIDE_INT val = INTVAL (rtl);
17480 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17481 val &= GET_MODE_MASK (DECL_MODE (loc));
17482 ret = int_loc_descriptor (val);
17484 else if (GET_CODE (rtl) == CONST_STRING)
17486 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17487 return 0;
17489 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17490 ret = new_addr_loc_descr (rtl, dtprel_false);
17491 else
17493 machine_mode mode, mem_mode;
17495 /* Certain constructs can only be represented at top-level. */
17496 if (want_address == 2)
17498 ret = loc_descriptor (rtl, VOIDmode,
17499 VAR_INIT_STATUS_INITIALIZED);
17500 have_address = 1;
17502 else
17504 mode = GET_MODE (rtl);
17505 mem_mode = VOIDmode;
17506 if (MEM_P (rtl))
17508 mem_mode = mode;
17509 mode = get_address_mode (rtl);
17510 rtl = XEXP (rtl, 0);
17511 have_address = 1;
17513 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17514 VAR_INIT_STATUS_INITIALIZED);
17516 if (!ret)
17517 expansion_failed (loc, rtl,
17518 "failed to produce loc descriptor for rtl");
17521 break;
17523 case MEM_REF:
17524 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17526 have_address = 1;
17527 goto do_plus;
17529 /* Fallthru. */
17530 case INDIRECT_REF:
17531 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17532 have_address = 1;
17533 break;
17535 case TARGET_MEM_REF:
17536 case SSA_NAME:
17537 case DEBUG_EXPR_DECL:
17538 return NULL;
17540 case COMPOUND_EXPR:
17541 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17542 context);
17544 CASE_CONVERT:
17545 case VIEW_CONVERT_EXPR:
17546 case SAVE_EXPR:
17547 case MODIFY_EXPR:
17548 case NON_LVALUE_EXPR:
17549 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17550 context);
17552 case COMPONENT_REF:
17553 case BIT_FIELD_REF:
17554 case ARRAY_REF:
17555 case ARRAY_RANGE_REF:
17556 case REALPART_EXPR:
17557 case IMAGPART_EXPR:
17559 tree obj, offset;
17560 HOST_WIDE_INT bitsize, bitpos, bytepos;
17561 machine_mode mode;
17562 int unsignedp, reversep, volatilep = 0;
17564 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17565 &unsignedp, &reversep, &volatilep);
17567 gcc_assert (obj != loc);
17569 list_ret = loc_list_from_tree_1 (obj,
17570 want_address == 2
17571 && !bitpos && !offset ? 2 : 1,
17572 context);
17573 /* TODO: We can extract value of the small expression via shifting even
17574 for nonzero bitpos. */
17575 if (list_ret == 0)
17576 return 0;
17577 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17579 expansion_failed (loc, NULL_RTX,
17580 "bitfield access");
17581 return 0;
17584 if (offset != NULL_TREE)
17586 /* Variable offset. */
17587 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17588 if (list_ret1 == 0)
17589 return 0;
17590 add_loc_list (&list_ret, list_ret1);
17591 if (!list_ret)
17592 return 0;
17593 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17596 bytepos = bitpos / BITS_PER_UNIT;
17597 if (bytepos > 0)
17598 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17599 else if (bytepos < 0)
17600 loc_list_plus_const (list_ret, bytepos);
17602 have_address = 1;
17603 break;
17606 case INTEGER_CST:
17607 if ((want_address || !tree_fits_shwi_p (loc))
17608 && (ret = cst_pool_loc_descr (loc)))
17609 have_address = 1;
17610 else if (want_address == 2
17611 && tree_fits_shwi_p (loc)
17612 && (ret = address_of_int_loc_descriptor
17613 (int_size_in_bytes (TREE_TYPE (loc)),
17614 tree_to_shwi (loc))))
17615 have_address = 1;
17616 else if (tree_fits_shwi_p (loc))
17617 ret = int_loc_descriptor (tree_to_shwi (loc));
17618 else if (tree_fits_uhwi_p (loc))
17619 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17620 else
17622 expansion_failed (loc, NULL_RTX,
17623 "Integer operand is not host integer");
17624 return 0;
17626 break;
17628 case CONSTRUCTOR:
17629 case REAL_CST:
17630 case STRING_CST:
17631 case COMPLEX_CST:
17632 if ((ret = cst_pool_loc_descr (loc)))
17633 have_address = 1;
17634 else if (TREE_CODE (loc) == CONSTRUCTOR)
17636 tree type = TREE_TYPE (loc);
17637 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17638 unsigned HOST_WIDE_INT offset = 0;
17639 unsigned HOST_WIDE_INT cnt;
17640 constructor_elt *ce;
17642 if (TREE_CODE (type) == RECORD_TYPE)
17644 /* This is very limited, but it's enough to output
17645 pointers to member functions, as long as the
17646 referenced function is defined in the current
17647 translation unit. */
17648 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17650 tree val = ce->value;
17652 tree field = ce->index;
17654 if (val)
17655 STRIP_NOPS (val);
17657 if (!field || DECL_BIT_FIELD (field))
17659 expansion_failed (loc, NULL_RTX,
17660 "bitfield in record type constructor");
17661 size = offset = (unsigned HOST_WIDE_INT)-1;
17662 ret = NULL;
17663 break;
17666 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17667 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17668 gcc_assert (pos + fieldsize <= size);
17669 if (pos < offset)
17671 expansion_failed (loc, NULL_RTX,
17672 "out-of-order fields in record constructor");
17673 size = offset = (unsigned HOST_WIDE_INT)-1;
17674 ret = NULL;
17675 break;
17677 if (pos > offset)
17679 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17680 add_loc_descr (&ret, ret1);
17681 offset = pos;
17683 if (val && fieldsize != 0)
17685 ret1 = loc_descriptor_from_tree (val, want_address, context);
17686 if (!ret1)
17688 expansion_failed (loc, NULL_RTX,
17689 "unsupported expression in field");
17690 size = offset = (unsigned HOST_WIDE_INT)-1;
17691 ret = NULL;
17692 break;
17694 add_loc_descr (&ret, ret1);
17696 if (fieldsize)
17698 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17699 add_loc_descr (&ret, ret1);
17700 offset = pos + fieldsize;
17704 if (offset != size)
17706 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17707 add_loc_descr (&ret, ret1);
17708 offset = size;
17711 have_address = !!want_address;
17713 else
17714 expansion_failed (loc, NULL_RTX,
17715 "constructor of non-record type");
17717 else
17718 /* We can construct small constants here using int_loc_descriptor. */
17719 expansion_failed (loc, NULL_RTX,
17720 "constructor or constant not in constant pool");
17721 break;
17723 case TRUTH_AND_EXPR:
17724 case TRUTH_ANDIF_EXPR:
17725 case BIT_AND_EXPR:
17726 op = DW_OP_and;
17727 goto do_binop;
17729 case TRUTH_XOR_EXPR:
17730 case BIT_XOR_EXPR:
17731 op = DW_OP_xor;
17732 goto do_binop;
17734 case TRUTH_OR_EXPR:
17735 case TRUTH_ORIF_EXPR:
17736 case BIT_IOR_EXPR:
17737 op = DW_OP_or;
17738 goto do_binop;
17740 case FLOOR_DIV_EXPR:
17741 case CEIL_DIV_EXPR:
17742 case ROUND_DIV_EXPR:
17743 case TRUNC_DIV_EXPR:
17744 case EXACT_DIV_EXPR:
17745 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17746 return 0;
17747 op = DW_OP_div;
17748 goto do_binop;
17750 case MINUS_EXPR:
17751 op = DW_OP_minus;
17752 goto do_binop;
17754 case FLOOR_MOD_EXPR:
17755 case CEIL_MOD_EXPR:
17756 case ROUND_MOD_EXPR:
17757 case TRUNC_MOD_EXPR:
17758 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17760 op = DW_OP_mod;
17761 goto do_binop;
17763 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17764 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17765 if (list_ret == 0 || list_ret1 == 0)
17766 return 0;
17768 add_loc_list (&list_ret, list_ret1);
17769 if (list_ret == 0)
17770 return 0;
17771 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17772 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17773 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17774 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17775 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17776 break;
17778 case MULT_EXPR:
17779 op = DW_OP_mul;
17780 goto do_binop;
17782 case LSHIFT_EXPR:
17783 op = DW_OP_shl;
17784 goto do_binop;
17786 case RSHIFT_EXPR:
17787 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17788 goto do_binop;
17790 case POINTER_PLUS_EXPR:
17791 case PLUS_EXPR:
17792 do_plus:
17793 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17795 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17796 smarter to encode their opposite. The DW_OP_plus_uconst operation
17797 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17798 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17799 bytes, Y being the size of the operation that pushes the opposite
17800 of the addend. So let's choose the smallest representation. */
17801 const tree tree_addend = TREE_OPERAND (loc, 1);
17802 offset_int wi_addend;
17803 HOST_WIDE_INT shwi_addend;
17804 dw_loc_descr_ref loc_naddend;
17806 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17807 if (list_ret == 0)
17808 return 0;
17810 /* Try to get the literal to push. It is the opposite of the addend,
17811 so as we rely on wrapping during DWARF evaluation, first decode
17812 the literal as a "DWARF-sized" signed number. */
17813 wi_addend = wi::to_offset (tree_addend);
17814 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17815 shwi_addend = wi_addend.to_shwi ();
17816 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17817 ? int_loc_descriptor (-shwi_addend)
17818 : NULL;
17820 if (loc_naddend != NULL
17821 && ((unsigned) size_of_uleb128 (shwi_addend)
17822 > size_of_loc_descr (loc_naddend)))
17824 add_loc_descr_to_each (list_ret, loc_naddend);
17825 add_loc_descr_to_each (list_ret,
17826 new_loc_descr (DW_OP_minus, 0, 0));
17828 else
17830 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17832 loc_naddend = loc_cur;
17833 loc_cur = loc_cur->dw_loc_next;
17834 ggc_free (loc_naddend);
17836 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17838 break;
17841 op = DW_OP_plus;
17842 goto do_binop;
17844 case LE_EXPR:
17845 op = DW_OP_le;
17846 goto do_comp_binop;
17848 case GE_EXPR:
17849 op = DW_OP_ge;
17850 goto do_comp_binop;
17852 case LT_EXPR:
17853 op = DW_OP_lt;
17854 goto do_comp_binop;
17856 case GT_EXPR:
17857 op = DW_OP_gt;
17858 goto do_comp_binop;
17860 do_comp_binop:
17861 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17863 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17864 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17865 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17866 TREE_CODE (loc));
17867 break;
17869 else
17870 goto do_binop;
17872 case EQ_EXPR:
17873 op = DW_OP_eq;
17874 goto do_binop;
17876 case NE_EXPR:
17877 op = DW_OP_ne;
17878 goto do_binop;
17880 do_binop:
17881 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17882 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17883 if (list_ret == 0 || list_ret1 == 0)
17884 return 0;
17886 add_loc_list (&list_ret, list_ret1);
17887 if (list_ret == 0)
17888 return 0;
17889 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17890 break;
17892 case TRUTH_NOT_EXPR:
17893 case BIT_NOT_EXPR:
17894 op = DW_OP_not;
17895 goto do_unop;
17897 case ABS_EXPR:
17898 op = DW_OP_abs;
17899 goto do_unop;
17901 case NEGATE_EXPR:
17902 op = DW_OP_neg;
17903 goto do_unop;
17905 do_unop:
17906 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17907 if (list_ret == 0)
17908 return 0;
17910 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17911 break;
17913 case MIN_EXPR:
17914 case MAX_EXPR:
17916 const enum tree_code code =
17917 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17919 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17920 build2 (code, integer_type_node,
17921 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17922 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17925 /* fall through */
17927 case COND_EXPR:
17929 dw_loc_descr_ref lhs
17930 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17931 dw_loc_list_ref rhs
17932 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17933 dw_loc_descr_ref bra_node, jump_node, tmp;
17935 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17936 if (list_ret == 0 || lhs == 0 || rhs == 0)
17937 return 0;
17939 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17940 add_loc_descr_to_each (list_ret, bra_node);
17942 add_loc_list (&list_ret, rhs);
17943 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17944 add_loc_descr_to_each (list_ret, jump_node);
17946 add_loc_descr_to_each (list_ret, lhs);
17947 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17948 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17950 /* ??? Need a node to point the skip at. Use a nop. */
17951 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17952 add_loc_descr_to_each (list_ret, tmp);
17953 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17954 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17956 break;
17958 case FIX_TRUNC_EXPR:
17959 return 0;
17961 default:
17962 /* Leave front-end specific codes as simply unknown. This comes
17963 up, for instance, with the C STMT_EXPR. */
17964 if ((unsigned int) TREE_CODE (loc)
17965 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
17967 expansion_failed (loc, NULL_RTX,
17968 "language specific tree node");
17969 return 0;
17972 /* Otherwise this is a generic code; we should just lists all of
17973 these explicitly. We forgot one. */
17974 if (flag_checking)
17975 gcc_unreachable ();
17977 /* In a release build, we want to degrade gracefully: better to
17978 generate incomplete debugging information than to crash. */
17979 return NULL;
17982 if (!ret && !list_ret)
17983 return 0;
17985 if (want_address == 2 && !have_address
17986 && (dwarf_version >= 4 || !dwarf_strict))
17988 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17990 expansion_failed (loc, NULL_RTX,
17991 "DWARF address size mismatch");
17992 return 0;
17994 if (ret)
17995 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17996 else
17997 add_loc_descr_to_each (list_ret,
17998 new_loc_descr (DW_OP_stack_value, 0, 0));
17999 have_address = 1;
18001 /* Show if we can't fill the request for an address. */
18002 if (want_address && !have_address)
18004 expansion_failed (loc, NULL_RTX,
18005 "Want address and only have value");
18006 return 0;
18009 gcc_assert (!ret || !list_ret);
18011 /* If we've got an address and don't want one, dereference. */
18012 if (!want_address && have_address)
18014 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18016 if (size > DWARF2_ADDR_SIZE || size == -1)
18018 expansion_failed (loc, NULL_RTX,
18019 "DWARF address size mismatch");
18020 return 0;
18022 else if (size == DWARF2_ADDR_SIZE)
18023 op = DW_OP_deref;
18024 else
18025 op = DW_OP_deref_size;
18027 if (ret)
18028 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18029 else
18030 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18032 if (ret)
18033 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18035 return list_ret;
18038 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18039 expressions. */
18041 static dw_loc_list_ref
18042 loc_list_from_tree (tree loc, int want_address,
18043 struct loc_descr_context *context)
18045 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18047 for (dw_loc_list_ref loc_cur = result;
18048 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18049 loc_descr_without_nops (loc_cur->expr);
18050 return result;
18053 /* Same as above but return only single location expression. */
18054 static dw_loc_descr_ref
18055 loc_descriptor_from_tree (tree loc, int want_address,
18056 struct loc_descr_context *context)
18058 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18059 if (!ret)
18060 return NULL;
18061 if (ret->dw_loc_next)
18063 expansion_failed (loc, NULL_RTX,
18064 "Location list where only loc descriptor needed");
18065 return NULL;
18067 return ret->expr;
18070 /* Given a value, round it up to the lowest multiple of `boundary'
18071 which is not less than the value itself. */
18073 static inline HOST_WIDE_INT
18074 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18076 return (((value + boundary - 1) / boundary) * boundary);
18079 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18080 pointer to the declared type for the relevant field variable, or return
18081 `integer_type_node' if the given node turns out to be an
18082 ERROR_MARK node. */
18084 static inline tree
18085 field_type (const_tree decl)
18087 tree type;
18089 if (TREE_CODE (decl) == ERROR_MARK)
18090 return integer_type_node;
18092 type = DECL_BIT_FIELD_TYPE (decl);
18093 if (type == NULL_TREE)
18094 type = TREE_TYPE (decl);
18096 return type;
18099 /* Given a pointer to a tree node, return the alignment in bits for
18100 it, or else return BITS_PER_WORD if the node actually turns out to
18101 be an ERROR_MARK node. */
18103 static inline unsigned
18104 simple_type_align_in_bits (const_tree type)
18106 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18109 static inline unsigned
18110 simple_decl_align_in_bits (const_tree decl)
18112 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18115 /* Return the result of rounding T up to ALIGN. */
18117 static inline offset_int
18118 round_up_to_align (const offset_int &t, unsigned int align)
18120 return wi::udiv_trunc (t + align - 1, align) * align;
18123 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18124 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18125 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18126 if we fail to return the size in one of these two forms. */
18128 static dw_loc_descr_ref
18129 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18131 tree tree_size;
18132 struct loc_descr_context ctx;
18134 /* Return a constant integer in priority, if possible. */
18135 *cst_size = int_size_in_bytes (type);
18136 if (*cst_size != -1)
18137 return NULL;
18139 ctx.context_type = const_cast<tree> (type);
18140 ctx.base_decl = NULL_TREE;
18141 ctx.dpi = NULL;
18142 ctx.placeholder_arg = false;
18143 ctx.placeholder_seen = false;
18145 type = TYPE_MAIN_VARIANT (type);
18146 tree_size = TYPE_SIZE_UNIT (type);
18147 return ((tree_size != NULL_TREE)
18148 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18149 : NULL);
18152 /* Helper structure for RECORD_TYPE processing. */
18153 struct vlr_context
18155 /* Root RECORD_TYPE. It is needed to generate data member location
18156 descriptions in variable-length records (VLR), but also to cope with
18157 variants, which are composed of nested structures multiplexed with
18158 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18159 function processing a FIELD_DECL, it is required to be non null. */
18160 tree struct_type;
18161 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18162 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18163 this variant part as part of the root record (in storage units). For
18164 regular records, it must be NULL_TREE. */
18165 tree variant_part_offset;
18168 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18169 addressed byte of the "containing object" for the given FIELD_DECL. If
18170 possible, return a native constant through CST_OFFSET (in which case NULL is
18171 returned); otherwise return a DWARF expression that computes the offset.
18173 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18174 that offset is, either because the argument turns out to be a pointer to an
18175 ERROR_MARK node, or because the offset expression is too complex for us.
18177 CTX is required: see the comment for VLR_CONTEXT. */
18179 static dw_loc_descr_ref
18180 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18181 HOST_WIDE_INT *cst_offset)
18183 tree tree_result;
18184 dw_loc_list_ref loc_result;
18186 *cst_offset = 0;
18188 if (TREE_CODE (decl) == ERROR_MARK)
18189 return NULL;
18190 else
18191 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18193 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18194 case. */
18195 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18196 return NULL;
18198 #ifdef PCC_BITFIELD_TYPE_MATTERS
18199 /* We used to handle only constant offsets in all cases. Now, we handle
18200 properly dynamic byte offsets only when PCC bitfield type doesn't
18201 matter. */
18202 if (PCC_BITFIELD_TYPE_MATTERS
18203 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18205 offset_int object_offset_in_bits;
18206 offset_int object_offset_in_bytes;
18207 offset_int bitpos_int;
18208 tree type;
18209 tree field_size_tree;
18210 offset_int deepest_bitpos;
18211 offset_int field_size_in_bits;
18212 unsigned int type_align_in_bits;
18213 unsigned int decl_align_in_bits;
18214 offset_int type_size_in_bits;
18216 bitpos_int = wi::to_offset (bit_position (decl));
18217 type = field_type (decl);
18218 type_size_in_bits = offset_int_type_size_in_bits (type);
18219 type_align_in_bits = simple_type_align_in_bits (type);
18221 field_size_tree = DECL_SIZE (decl);
18223 /* The size could be unspecified if there was an error, or for
18224 a flexible array member. */
18225 if (!field_size_tree)
18226 field_size_tree = bitsize_zero_node;
18228 /* If the size of the field is not constant, use the type size. */
18229 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18230 field_size_in_bits = wi::to_offset (field_size_tree);
18231 else
18232 field_size_in_bits = type_size_in_bits;
18234 decl_align_in_bits = simple_decl_align_in_bits (decl);
18236 /* The GCC front-end doesn't make any attempt to keep track of the
18237 starting bit offset (relative to the start of the containing
18238 structure type) of the hypothetical "containing object" for a
18239 bit-field. Thus, when computing the byte offset value for the
18240 start of the "containing object" of a bit-field, we must deduce
18241 this information on our own. This can be rather tricky to do in
18242 some cases. For example, handling the following structure type
18243 definition when compiling for an i386/i486 target (which only
18244 aligns long long's to 32-bit boundaries) can be very tricky:
18246 struct S { int field1; long long field2:31; };
18248 Fortunately, there is a simple rule-of-thumb which can be used
18249 in such cases. When compiling for an i386/i486, GCC will
18250 allocate 8 bytes for the structure shown above. It decides to
18251 do this based upon one simple rule for bit-field allocation.
18252 GCC allocates each "containing object" for each bit-field at
18253 the first (i.e. lowest addressed) legitimate alignment boundary
18254 (based upon the required minimum alignment for the declared
18255 type of the field) which it can possibly use, subject to the
18256 condition that there is still enough available space remaining
18257 in the containing object (when allocated at the selected point)
18258 to fully accommodate all of the bits of the bit-field itself.
18260 This simple rule makes it obvious why GCC allocates 8 bytes for
18261 each object of the structure type shown above. When looking
18262 for a place to allocate the "containing object" for `field2',
18263 the compiler simply tries to allocate a 64-bit "containing
18264 object" at each successive 32-bit boundary (starting at zero)
18265 until it finds a place to allocate that 64- bit field such that
18266 at least 31 contiguous (and previously unallocated) bits remain
18267 within that selected 64 bit field. (As it turns out, for the
18268 example above, the compiler finds it is OK to allocate the
18269 "containing object" 64-bit field at bit-offset zero within the
18270 structure type.)
18272 Here we attempt to work backwards from the limited set of facts
18273 we're given, and we try to deduce from those facts, where GCC
18274 must have believed that the containing object started (within
18275 the structure type). The value we deduce is then used (by the
18276 callers of this routine) to generate DW_AT_location and
18277 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18278 the case of DW_AT_location, regular fields as well). */
18280 /* Figure out the bit-distance from the start of the structure to
18281 the "deepest" bit of the bit-field. */
18282 deepest_bitpos = bitpos_int + field_size_in_bits;
18284 /* This is the tricky part. Use some fancy footwork to deduce
18285 where the lowest addressed bit of the containing object must
18286 be. */
18287 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18289 /* Round up to type_align by default. This works best for
18290 bitfields. */
18291 object_offset_in_bits
18292 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18294 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18296 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18298 /* Round up to decl_align instead. */
18299 object_offset_in_bits
18300 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18303 object_offset_in_bytes
18304 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18305 if (ctx->variant_part_offset == NULL_TREE)
18307 *cst_offset = object_offset_in_bytes.to_shwi ();
18308 return NULL;
18310 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18312 else
18313 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18314 tree_result = byte_position (decl);
18316 if (ctx->variant_part_offset != NULL_TREE)
18317 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18318 ctx->variant_part_offset, tree_result);
18320 /* If the byte offset is a constant, it's simplier to handle a native
18321 constant rather than a DWARF expression. */
18322 if (TREE_CODE (tree_result) == INTEGER_CST)
18324 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18325 return NULL;
18327 struct loc_descr_context loc_ctx = {
18328 ctx->struct_type, /* context_type */
18329 NULL_TREE, /* base_decl */
18330 NULL, /* dpi */
18331 false, /* placeholder_arg */
18332 false /* placeholder_seen */
18334 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18336 /* We want a DWARF expression: abort if we only have a location list with
18337 multiple elements. */
18338 if (!loc_result || !single_element_loc_list_p (loc_result))
18339 return NULL;
18340 else
18341 return loc_result->expr;
18344 /* The following routines define various Dwarf attributes and any data
18345 associated with them. */
18347 /* Add a location description attribute value to a DIE.
18349 This emits location attributes suitable for whole variables and
18350 whole parameters. Note that the location attributes for struct fields are
18351 generated by the routine `data_member_location_attribute' below. */
18353 static inline void
18354 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18355 dw_loc_list_ref descr)
18357 if (descr == 0)
18358 return;
18359 if (single_element_loc_list_p (descr))
18360 add_AT_loc (die, attr_kind, descr->expr);
18361 else
18362 add_AT_loc_list (die, attr_kind, descr);
18365 /* Add DW_AT_accessibility attribute to DIE if needed. */
18367 static void
18368 add_accessibility_attribute (dw_die_ref die, tree decl)
18370 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18371 children, otherwise the default is DW_ACCESS_public. In DWARF2
18372 the default has always been DW_ACCESS_public. */
18373 if (TREE_PROTECTED (decl))
18374 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18375 else if (TREE_PRIVATE (decl))
18377 if (dwarf_version == 2
18378 || die->die_parent == NULL
18379 || die->die_parent->die_tag != DW_TAG_class_type)
18380 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18382 else if (dwarf_version > 2
18383 && die->die_parent
18384 && die->die_parent->die_tag == DW_TAG_class_type)
18385 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18388 /* Attach the specialized form of location attribute used for data members of
18389 struct and union types. In the special case of a FIELD_DECL node which
18390 represents a bit-field, the "offset" part of this special location
18391 descriptor must indicate the distance in bytes from the lowest-addressed
18392 byte of the containing struct or union type to the lowest-addressed byte of
18393 the "containing object" for the bit-field. (See the `field_byte_offset'
18394 function above).
18396 For any given bit-field, the "containing object" is a hypothetical object
18397 (of some integral or enum type) within which the given bit-field lives. The
18398 type of this hypothetical "containing object" is always the same as the
18399 declared type of the individual bit-field itself (for GCC anyway... the
18400 DWARF spec doesn't actually mandate this). Note that it is the size (in
18401 bytes) of the hypothetical "containing object" which will be given in the
18402 DW_AT_byte_size attribute for this bit-field. (See the
18403 `byte_size_attribute' function below.) It is also used when calculating the
18404 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18405 function below.)
18407 CTX is required: see the comment for VLR_CONTEXT. */
18409 static void
18410 add_data_member_location_attribute (dw_die_ref die,
18411 tree decl,
18412 struct vlr_context *ctx)
18414 HOST_WIDE_INT offset;
18415 dw_loc_descr_ref loc_descr = 0;
18417 if (TREE_CODE (decl) == TREE_BINFO)
18419 /* We're working on the TAG_inheritance for a base class. */
18420 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18422 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18423 aren't at a fixed offset from all (sub)objects of the same
18424 type. We need to extract the appropriate offset from our
18425 vtable. The following dwarf expression means
18427 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18429 This is specific to the V3 ABI, of course. */
18431 dw_loc_descr_ref tmp;
18433 /* Make a copy of the object address. */
18434 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18435 add_loc_descr (&loc_descr, tmp);
18437 /* Extract the vtable address. */
18438 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18439 add_loc_descr (&loc_descr, tmp);
18441 /* Calculate the address of the offset. */
18442 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18443 gcc_assert (offset < 0);
18445 tmp = int_loc_descriptor (-offset);
18446 add_loc_descr (&loc_descr, tmp);
18447 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18448 add_loc_descr (&loc_descr, tmp);
18450 /* Extract the offset. */
18451 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18452 add_loc_descr (&loc_descr, tmp);
18454 /* Add it to the object address. */
18455 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18456 add_loc_descr (&loc_descr, tmp);
18458 else
18459 offset = tree_to_shwi (BINFO_OFFSET (decl));
18461 else
18463 loc_descr = field_byte_offset (decl, ctx, &offset);
18465 /* If loc_descr is available then we know the field offset is dynamic.
18466 However, GDB does not handle dynamic field offsets very well at the
18467 moment. */
18468 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18470 loc_descr = NULL;
18471 offset = 0;
18474 /* Data member location evalutation starts with the base address on the
18475 stack. Compute the field offset and add it to this base address. */
18476 else if (loc_descr != NULL)
18477 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18480 if (! loc_descr)
18482 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18483 e.g. GDB only added support to it in November 2016. For DWARF5
18484 we need newer debug info consumers anyway. We might change this
18485 to dwarf_version >= 4 once most consumers catched up. */
18486 if (dwarf_version >= 5
18487 && TREE_CODE (decl) == FIELD_DECL
18488 && DECL_BIT_FIELD_TYPE (decl))
18490 tree off = bit_position (decl);
18491 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18493 remove_AT (die, DW_AT_byte_size);
18494 remove_AT (die, DW_AT_bit_offset);
18495 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18496 return;
18499 if (dwarf_version > 2)
18501 /* Don't need to output a location expression, just the constant. */
18502 if (offset < 0)
18503 add_AT_int (die, DW_AT_data_member_location, offset);
18504 else
18505 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18506 return;
18508 else
18510 enum dwarf_location_atom op;
18512 /* The DWARF2 standard says that we should assume that the structure
18513 address is already on the stack, so we can specify a structure
18514 field address by using DW_OP_plus_uconst. */
18515 op = DW_OP_plus_uconst;
18516 loc_descr = new_loc_descr (op, offset, 0);
18520 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18523 /* Writes integer values to dw_vec_const array. */
18525 static void
18526 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18528 while (size != 0)
18530 *dest++ = val & 0xff;
18531 val >>= 8;
18532 --size;
18536 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18538 static HOST_WIDE_INT
18539 extract_int (const unsigned char *src, unsigned int size)
18541 HOST_WIDE_INT val = 0;
18543 src += size;
18544 while (size != 0)
18546 val <<= 8;
18547 val |= *--src & 0xff;
18548 --size;
18550 return val;
18553 /* Writes wide_int values to dw_vec_const array. */
18555 static void
18556 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18558 int i;
18560 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18562 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18563 return;
18566 /* We'd have to extend this code to support odd sizes. */
18567 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18569 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18571 if (WORDS_BIG_ENDIAN)
18572 for (i = n - 1; i >= 0; i--)
18574 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18575 dest += sizeof (HOST_WIDE_INT);
18577 else
18578 for (i = 0; i < n; i++)
18580 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18581 dest += sizeof (HOST_WIDE_INT);
18585 /* Writes floating point values to dw_vec_const array. */
18587 static void
18588 insert_float (const_rtx rtl, unsigned char *array)
18590 long val[4];
18591 int i;
18593 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
18595 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18596 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
18598 insert_int (val[i], 4, array);
18599 array += 4;
18603 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18604 does not have a "location" either in memory or in a register. These
18605 things can arise in GNU C when a constant is passed as an actual parameter
18606 to an inlined function. They can also arise in C++ where declared
18607 constants do not necessarily get memory "homes". */
18609 static bool
18610 add_const_value_attribute (dw_die_ref die, rtx rtl)
18612 switch (GET_CODE (rtl))
18614 case CONST_INT:
18616 HOST_WIDE_INT val = INTVAL (rtl);
18618 if (val < 0)
18619 add_AT_int (die, DW_AT_const_value, val);
18620 else
18621 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18623 return true;
18625 case CONST_WIDE_INT:
18627 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18628 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18629 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18630 wide_int w = wi::zext (w1, prec);
18631 add_AT_wide (die, DW_AT_const_value, w);
18633 return true;
18635 case CONST_DOUBLE:
18636 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18637 floating-point constant. A CONST_DOUBLE is used whenever the
18638 constant requires more than one word in order to be adequately
18639 represented. */
18641 machine_mode mode = GET_MODE (rtl);
18643 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
18644 add_AT_double (die, DW_AT_const_value,
18645 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18646 else
18648 unsigned int length = GET_MODE_SIZE (mode);
18649 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18651 insert_float (rtl, array);
18652 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18655 return true;
18657 case CONST_VECTOR:
18659 machine_mode mode = GET_MODE (rtl);
18660 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18661 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18662 unsigned char *array
18663 = ggc_vec_alloc<unsigned char> (length * elt_size);
18664 unsigned int i;
18665 unsigned char *p;
18666 machine_mode imode = GET_MODE_INNER (mode);
18668 switch (GET_MODE_CLASS (mode))
18670 case MODE_VECTOR_INT:
18671 for (i = 0, p = array; i < length; i++, p += elt_size)
18673 rtx elt = CONST_VECTOR_ELT (rtl, i);
18674 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18676 break;
18678 case MODE_VECTOR_FLOAT:
18679 for (i = 0, p = array; i < length; i++, p += elt_size)
18681 rtx elt = CONST_VECTOR_ELT (rtl, i);
18682 insert_float (elt, p);
18684 break;
18686 default:
18687 gcc_unreachable ();
18690 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18692 return true;
18694 case CONST_STRING:
18695 if (dwarf_version >= 4 || !dwarf_strict)
18697 dw_loc_descr_ref loc_result;
18698 resolve_one_addr (&rtl);
18699 rtl_addr:
18700 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18701 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18702 add_AT_loc (die, DW_AT_location, loc_result);
18703 vec_safe_push (used_rtx_array, rtl);
18704 return true;
18706 return false;
18708 case CONST:
18709 if (CONSTANT_P (XEXP (rtl, 0)))
18710 return add_const_value_attribute (die, XEXP (rtl, 0));
18711 /* FALLTHROUGH */
18712 case SYMBOL_REF:
18713 if (!const_ok_for_output (rtl))
18714 return false;
18715 /* FALLTHROUGH */
18716 case LABEL_REF:
18717 if (dwarf_version >= 4 || !dwarf_strict)
18718 goto rtl_addr;
18719 return false;
18721 case PLUS:
18722 /* In cases where an inlined instance of an inline function is passed
18723 the address of an `auto' variable (which is local to the caller) we
18724 can get a situation where the DECL_RTL of the artificial local
18725 variable (for the inlining) which acts as a stand-in for the
18726 corresponding formal parameter (of the inline function) will look
18727 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18728 exactly a compile-time constant expression, but it isn't the address
18729 of the (artificial) local variable either. Rather, it represents the
18730 *value* which the artificial local variable always has during its
18731 lifetime. We currently have no way to represent such quasi-constant
18732 values in Dwarf, so for now we just punt and generate nothing. */
18733 return false;
18735 case HIGH:
18736 case CONST_FIXED:
18737 return false;
18739 case MEM:
18740 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18741 && MEM_READONLY_P (rtl)
18742 && GET_MODE (rtl) == BLKmode)
18744 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18745 return true;
18747 return false;
18749 default:
18750 /* No other kinds of rtx should be possible here. */
18751 gcc_unreachable ();
18753 return false;
18756 /* Determine whether the evaluation of EXPR references any variables
18757 or functions which aren't otherwise used (and therefore may not be
18758 output). */
18759 static tree
18760 reference_to_unused (tree * tp, int * walk_subtrees,
18761 void * data ATTRIBUTE_UNUSED)
18763 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18764 *walk_subtrees = 0;
18766 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18767 && ! TREE_ASM_WRITTEN (*tp))
18768 return *tp;
18769 /* ??? The C++ FE emits debug information for using decls, so
18770 putting gcc_unreachable here falls over. See PR31899. For now
18771 be conservative. */
18772 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18773 return *tp;
18774 else if (VAR_P (*tp))
18776 varpool_node *node = varpool_node::get (*tp);
18777 if (!node || !node->definition)
18778 return *tp;
18780 else if (TREE_CODE (*tp) == FUNCTION_DECL
18781 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18783 /* The call graph machinery must have finished analyzing,
18784 optimizing and gimplifying the CU by now.
18785 So if *TP has no call graph node associated
18786 to it, it means *TP will not be emitted. */
18787 if (!cgraph_node::get (*tp))
18788 return *tp;
18790 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18791 return *tp;
18793 return NULL_TREE;
18796 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18797 for use in a later add_const_value_attribute call. */
18799 static rtx
18800 rtl_for_decl_init (tree init, tree type)
18802 rtx rtl = NULL_RTX;
18804 STRIP_NOPS (init);
18806 /* If a variable is initialized with a string constant without embedded
18807 zeros, build CONST_STRING. */
18808 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18810 tree enttype = TREE_TYPE (type);
18811 tree domain = TYPE_DOMAIN (type);
18812 machine_mode mode = TYPE_MODE (enttype);
18814 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
18815 && domain
18816 && integer_zerop (TYPE_MIN_VALUE (domain))
18817 && compare_tree_int (TYPE_MAX_VALUE (domain),
18818 TREE_STRING_LENGTH (init) - 1) == 0
18819 && ((size_t) TREE_STRING_LENGTH (init)
18820 == strlen (TREE_STRING_POINTER (init)) + 1))
18822 rtl = gen_rtx_CONST_STRING (VOIDmode,
18823 ggc_strdup (TREE_STRING_POINTER (init)));
18824 rtl = gen_rtx_MEM (BLKmode, rtl);
18825 MEM_READONLY_P (rtl) = 1;
18828 /* Other aggregates, and complex values, could be represented using
18829 CONCAT: FIXME! */
18830 else if (AGGREGATE_TYPE_P (type)
18831 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18832 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18833 || TREE_CODE (type) == COMPLEX_TYPE)
18835 /* Vectors only work if their mode is supported by the target.
18836 FIXME: generic vectors ought to work too. */
18837 else if (TREE_CODE (type) == VECTOR_TYPE
18838 && !VECTOR_MODE_P (TYPE_MODE (type)))
18840 /* If the initializer is something that we know will expand into an
18841 immediate RTL constant, expand it now. We must be careful not to
18842 reference variables which won't be output. */
18843 else if (initializer_constant_valid_p (init, type)
18844 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18846 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18847 possible. */
18848 if (TREE_CODE (type) == VECTOR_TYPE)
18849 switch (TREE_CODE (init))
18851 case VECTOR_CST:
18852 break;
18853 case CONSTRUCTOR:
18854 if (TREE_CONSTANT (init))
18856 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18857 bool constant_p = true;
18858 tree value;
18859 unsigned HOST_WIDE_INT ix;
18861 /* Even when ctor is constant, it might contain non-*_CST
18862 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18863 belong into VECTOR_CST nodes. */
18864 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18865 if (!CONSTANT_CLASS_P (value))
18867 constant_p = false;
18868 break;
18871 if (constant_p)
18873 init = build_vector_from_ctor (type, elts);
18874 break;
18877 /* FALLTHRU */
18879 default:
18880 return NULL;
18883 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18885 /* If expand_expr returns a MEM, it wasn't immediate. */
18886 gcc_assert (!rtl || !MEM_P (rtl));
18889 return rtl;
18892 /* Generate RTL for the variable DECL to represent its location. */
18894 static rtx
18895 rtl_for_decl_location (tree decl)
18897 rtx rtl;
18899 /* Here we have to decide where we are going to say the parameter "lives"
18900 (as far as the debugger is concerned). We only have a couple of
18901 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18903 DECL_RTL normally indicates where the parameter lives during most of the
18904 activation of the function. If optimization is enabled however, this
18905 could be either NULL or else a pseudo-reg. Both of those cases indicate
18906 that the parameter doesn't really live anywhere (as far as the code
18907 generation parts of GCC are concerned) during most of the function's
18908 activation. That will happen (for example) if the parameter is never
18909 referenced within the function.
18911 We could just generate a location descriptor here for all non-NULL
18912 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18913 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18914 where DECL_RTL is NULL or is a pseudo-reg.
18916 Note however that we can only get away with using DECL_INCOMING_RTL as
18917 a backup substitute for DECL_RTL in certain limited cases. In cases
18918 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18919 we can be sure that the parameter was passed using the same type as it is
18920 declared to have within the function, and that its DECL_INCOMING_RTL
18921 points us to a place where a value of that type is passed.
18923 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18924 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18925 because in these cases DECL_INCOMING_RTL points us to a value of some
18926 type which is *different* from the type of the parameter itself. Thus,
18927 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18928 such cases, the debugger would end up (for example) trying to fetch a
18929 `float' from a place which actually contains the first part of a
18930 `double'. That would lead to really incorrect and confusing
18931 output at debug-time.
18933 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18934 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18935 are a couple of exceptions however. On little-endian machines we can
18936 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18937 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18938 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18939 when (on a little-endian machine) a non-prototyped function has a
18940 parameter declared to be of type `short' or `char'. In such cases,
18941 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18942 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18943 passed `int' value. If the debugger then uses that address to fetch
18944 a `short' or a `char' (on a little-endian machine) the result will be
18945 the correct data, so we allow for such exceptional cases below.
18947 Note that our goal here is to describe the place where the given formal
18948 parameter lives during most of the function's activation (i.e. between the
18949 end of the prologue and the start of the epilogue). We'll do that as best
18950 as we can. Note however that if the given formal parameter is modified
18951 sometime during the execution of the function, then a stack backtrace (at
18952 debug-time) will show the function as having been called with the *new*
18953 value rather than the value which was originally passed in. This happens
18954 rarely enough that it is not a major problem, but it *is* a problem, and
18955 I'd like to fix it.
18957 A future version of dwarf2out.c may generate two additional attributes for
18958 any given DW_TAG_formal_parameter DIE which will describe the "passed
18959 type" and the "passed location" for the given formal parameter in addition
18960 to the attributes we now generate to indicate the "declared type" and the
18961 "active location" for each parameter. This additional set of attributes
18962 could be used by debuggers for stack backtraces. Separately, note that
18963 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
18964 This happens (for example) for inlined-instances of inline function formal
18965 parameters which are never referenced. This really shouldn't be
18966 happening. All PARM_DECL nodes should get valid non-NULL
18967 DECL_INCOMING_RTL values. FIXME. */
18969 /* Use DECL_RTL as the "location" unless we find something better. */
18970 rtl = DECL_RTL_IF_SET (decl);
18972 /* When generating abstract instances, ignore everything except
18973 constants, symbols living in memory, and symbols living in
18974 fixed registers. */
18975 if (! reload_completed)
18977 if (rtl
18978 && (CONSTANT_P (rtl)
18979 || (MEM_P (rtl)
18980 && CONSTANT_P (XEXP (rtl, 0)))
18981 || (REG_P (rtl)
18982 && VAR_P (decl)
18983 && TREE_STATIC (decl))))
18985 rtl = targetm.delegitimize_address (rtl);
18986 return rtl;
18988 rtl = NULL_RTX;
18990 else if (TREE_CODE (decl) == PARM_DECL)
18992 if (rtl == NULL_RTX
18993 || is_pseudo_reg (rtl)
18994 || (MEM_P (rtl)
18995 && is_pseudo_reg (XEXP (rtl, 0))
18996 && DECL_INCOMING_RTL (decl)
18997 && MEM_P (DECL_INCOMING_RTL (decl))
18998 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19000 tree declared_type = TREE_TYPE (decl);
19001 tree passed_type = DECL_ARG_TYPE (decl);
19002 machine_mode dmode = TYPE_MODE (declared_type);
19003 machine_mode pmode = TYPE_MODE (passed_type);
19005 /* This decl represents a formal parameter which was optimized out.
19006 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19007 all cases where (rtl == NULL_RTX) just below. */
19008 if (dmode == pmode)
19009 rtl = DECL_INCOMING_RTL (decl);
19010 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19011 && SCALAR_INT_MODE_P (dmode)
19012 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19013 && DECL_INCOMING_RTL (decl))
19015 rtx inc = DECL_INCOMING_RTL (decl);
19016 if (REG_P (inc))
19017 rtl = inc;
19018 else if (MEM_P (inc))
19020 if (BYTES_BIG_ENDIAN)
19021 rtl = adjust_address_nv (inc, dmode,
19022 GET_MODE_SIZE (pmode)
19023 - GET_MODE_SIZE (dmode));
19024 else
19025 rtl = inc;
19030 /* If the parm was passed in registers, but lives on the stack, then
19031 make a big endian correction if the mode of the type of the
19032 parameter is not the same as the mode of the rtl. */
19033 /* ??? This is the same series of checks that are made in dbxout.c before
19034 we reach the big endian correction code there. It isn't clear if all
19035 of these checks are necessary here, but keeping them all is the safe
19036 thing to do. */
19037 else if (MEM_P (rtl)
19038 && XEXP (rtl, 0) != const0_rtx
19039 && ! CONSTANT_P (XEXP (rtl, 0))
19040 /* Not passed in memory. */
19041 && !MEM_P (DECL_INCOMING_RTL (decl))
19042 /* Not passed by invisible reference. */
19043 && (!REG_P (XEXP (rtl, 0))
19044 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19045 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19046 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19047 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19048 #endif
19050 /* Big endian correction check. */
19051 && BYTES_BIG_ENDIAN
19052 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19053 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19054 < UNITS_PER_WORD))
19056 machine_mode addr_mode = get_address_mode (rtl);
19057 int offset = (UNITS_PER_WORD
19058 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19060 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19061 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19064 else if (VAR_P (decl)
19065 && rtl
19066 && MEM_P (rtl)
19067 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
19068 && BYTES_BIG_ENDIAN)
19070 machine_mode addr_mode = get_address_mode (rtl);
19071 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19072 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19074 /* If a variable is declared "register" yet is smaller than
19075 a register, then if we store the variable to memory, it
19076 looks like we're storing a register-sized value, when in
19077 fact we are not. We need to adjust the offset of the
19078 storage location to reflect the actual value's bytes,
19079 else gdb will not be able to display it. */
19080 if (rsize > dsize)
19081 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19082 plus_constant (addr_mode, XEXP (rtl, 0),
19083 rsize - dsize));
19086 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19087 and will have been substituted directly into all expressions that use it.
19088 C does not have such a concept, but C++ and other languages do. */
19089 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19090 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19092 if (rtl)
19093 rtl = targetm.delegitimize_address (rtl);
19095 /* If we don't look past the constant pool, we risk emitting a
19096 reference to a constant pool entry that isn't referenced from
19097 code, and thus is not emitted. */
19098 if (rtl)
19099 rtl = avoid_constant_pool_reference (rtl);
19101 /* Try harder to get a rtl. If this symbol ends up not being emitted
19102 in the current CU, resolve_addr will remove the expression referencing
19103 it. */
19104 if (rtl == NULL_RTX
19105 && VAR_P (decl)
19106 && !DECL_EXTERNAL (decl)
19107 && TREE_STATIC (decl)
19108 && DECL_NAME (decl)
19109 && !DECL_HARD_REGISTER (decl)
19110 && DECL_MODE (decl) != VOIDmode)
19112 rtl = make_decl_rtl_for_debug (decl);
19113 if (!MEM_P (rtl)
19114 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19115 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19116 rtl = NULL_RTX;
19119 return rtl;
19122 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19123 returned. If so, the decl for the COMMON block is returned, and the
19124 value is the offset into the common block for the symbol. */
19126 static tree
19127 fortran_common (tree decl, HOST_WIDE_INT *value)
19129 tree val_expr, cvar;
19130 machine_mode mode;
19131 HOST_WIDE_INT bitsize, bitpos;
19132 tree offset;
19133 int unsignedp, reversep, volatilep = 0;
19135 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19136 it does not have a value (the offset into the common area), or if it
19137 is thread local (as opposed to global) then it isn't common, and shouldn't
19138 be handled as such. */
19139 if (!VAR_P (decl)
19140 || !TREE_STATIC (decl)
19141 || !DECL_HAS_VALUE_EXPR_P (decl)
19142 || !is_fortran ())
19143 return NULL_TREE;
19145 val_expr = DECL_VALUE_EXPR (decl);
19146 if (TREE_CODE (val_expr) != COMPONENT_REF)
19147 return NULL_TREE;
19149 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19150 &unsignedp, &reversep, &volatilep);
19152 if (cvar == NULL_TREE
19153 || !VAR_P (cvar)
19154 || DECL_ARTIFICIAL (cvar)
19155 || !TREE_PUBLIC (cvar))
19156 return NULL_TREE;
19158 *value = 0;
19159 if (offset != NULL)
19161 if (!tree_fits_shwi_p (offset))
19162 return NULL_TREE;
19163 *value = tree_to_shwi (offset);
19165 if (bitpos != 0)
19166 *value += bitpos / BITS_PER_UNIT;
19168 return cvar;
19171 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19172 data attribute for a variable or a parameter. We generate the
19173 DW_AT_const_value attribute only in those cases where the given variable
19174 or parameter does not have a true "location" either in memory or in a
19175 register. This can happen (for example) when a constant is passed as an
19176 actual argument in a call to an inline function. (It's possible that
19177 these things can crop up in other ways also.) Note that one type of
19178 constant value which can be passed into an inlined function is a constant
19179 pointer. This can happen for example if an actual argument in an inlined
19180 function call evaluates to a compile-time constant address.
19182 CACHE_P is true if it is worth caching the location list for DECL,
19183 so that future calls can reuse it rather than regenerate it from scratch.
19184 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19185 since we will need to refer to them each time the function is inlined. */
19187 static bool
19188 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19190 rtx rtl;
19191 dw_loc_list_ref list;
19192 var_loc_list *loc_list;
19193 cached_dw_loc_list *cache;
19195 if (early_dwarf)
19196 return false;
19198 if (TREE_CODE (decl) == ERROR_MARK)
19199 return false;
19201 if (get_AT (die, DW_AT_location)
19202 || get_AT (die, DW_AT_const_value))
19203 return true;
19205 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19206 || TREE_CODE (decl) == RESULT_DECL);
19208 /* Try to get some constant RTL for this decl, and use that as the value of
19209 the location. */
19211 rtl = rtl_for_decl_location (decl);
19212 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19213 && add_const_value_attribute (die, rtl))
19214 return true;
19216 /* See if we have single element location list that is equivalent to
19217 a constant value. That way we are better to use add_const_value_attribute
19218 rather than expanding constant value equivalent. */
19219 loc_list = lookup_decl_loc (decl);
19220 if (loc_list
19221 && loc_list->first
19222 && loc_list->first->next == NULL
19223 && NOTE_P (loc_list->first->loc)
19224 && NOTE_VAR_LOCATION (loc_list->first->loc)
19225 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19227 struct var_loc_node *node;
19229 node = loc_list->first;
19230 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19231 if (GET_CODE (rtl) == EXPR_LIST)
19232 rtl = XEXP (rtl, 0);
19233 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19234 && add_const_value_attribute (die, rtl))
19235 return true;
19237 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19238 list several times. See if we've already cached the contents. */
19239 list = NULL;
19240 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19241 cache_p = false;
19242 if (cache_p)
19244 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19245 if (cache)
19246 list = cache->loc_list;
19248 if (list == NULL)
19250 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19251 NULL);
19252 /* It is usually worth caching this result if the decl is from
19253 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19254 if (cache_p && list && list->dw_loc_next)
19256 cached_dw_loc_list **slot
19257 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19258 DECL_UID (decl),
19259 INSERT);
19260 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19261 cache->decl_id = DECL_UID (decl);
19262 cache->loc_list = list;
19263 *slot = cache;
19266 if (list)
19268 add_AT_location_description (die, DW_AT_location, list);
19269 return true;
19271 /* None of that worked, so it must not really have a location;
19272 try adding a constant value attribute from the DECL_INITIAL. */
19273 return tree_add_const_value_attribute_for_decl (die, decl);
19276 /* Helper function for tree_add_const_value_attribute. Natively encode
19277 initializer INIT into an array. Return true if successful. */
19279 static bool
19280 native_encode_initializer (tree init, unsigned char *array, int size)
19282 tree type;
19284 if (init == NULL_TREE)
19285 return false;
19287 STRIP_NOPS (init);
19288 switch (TREE_CODE (init))
19290 case STRING_CST:
19291 type = TREE_TYPE (init);
19292 if (TREE_CODE (type) == ARRAY_TYPE)
19294 tree enttype = TREE_TYPE (type);
19295 machine_mode mode = TYPE_MODE (enttype);
19297 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
19298 return false;
19299 if (int_size_in_bytes (type) != size)
19300 return false;
19301 if (size > TREE_STRING_LENGTH (init))
19303 memcpy (array, TREE_STRING_POINTER (init),
19304 TREE_STRING_LENGTH (init));
19305 memset (array + TREE_STRING_LENGTH (init),
19306 '\0', size - TREE_STRING_LENGTH (init));
19308 else
19309 memcpy (array, TREE_STRING_POINTER (init), size);
19310 return true;
19312 return false;
19313 case CONSTRUCTOR:
19314 type = TREE_TYPE (init);
19315 if (int_size_in_bytes (type) != size)
19316 return false;
19317 if (TREE_CODE (type) == ARRAY_TYPE)
19319 HOST_WIDE_INT min_index;
19320 unsigned HOST_WIDE_INT cnt;
19321 int curpos = 0, fieldsize;
19322 constructor_elt *ce;
19324 if (TYPE_DOMAIN (type) == NULL_TREE
19325 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19326 return false;
19328 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19329 if (fieldsize <= 0)
19330 return false;
19332 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19333 memset (array, '\0', size);
19334 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19336 tree val = ce->value;
19337 tree index = ce->index;
19338 int pos = curpos;
19339 if (index && TREE_CODE (index) == RANGE_EXPR)
19340 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19341 * fieldsize;
19342 else if (index)
19343 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19345 if (val)
19347 STRIP_NOPS (val);
19348 if (!native_encode_initializer (val, array + pos, fieldsize))
19349 return false;
19351 curpos = pos + fieldsize;
19352 if (index && TREE_CODE (index) == RANGE_EXPR)
19354 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19355 - tree_to_shwi (TREE_OPERAND (index, 0));
19356 while (count-- > 0)
19358 if (val)
19359 memcpy (array + curpos, array + pos, fieldsize);
19360 curpos += fieldsize;
19363 gcc_assert (curpos <= size);
19365 return true;
19367 else if (TREE_CODE (type) == RECORD_TYPE
19368 || TREE_CODE (type) == UNION_TYPE)
19370 tree field = NULL_TREE;
19371 unsigned HOST_WIDE_INT cnt;
19372 constructor_elt *ce;
19374 if (int_size_in_bytes (type) != size)
19375 return false;
19377 if (TREE_CODE (type) == RECORD_TYPE)
19378 field = TYPE_FIELDS (type);
19380 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19382 tree val = ce->value;
19383 int pos, fieldsize;
19385 if (ce->index != 0)
19386 field = ce->index;
19388 if (val)
19389 STRIP_NOPS (val);
19391 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19392 return false;
19394 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19395 && TYPE_DOMAIN (TREE_TYPE (field))
19396 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19397 return false;
19398 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19399 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19400 return false;
19401 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19402 pos = int_byte_position (field);
19403 gcc_assert (pos + fieldsize <= size);
19404 if (val && fieldsize != 0
19405 && !native_encode_initializer (val, array + pos, fieldsize))
19406 return false;
19408 return true;
19410 return false;
19411 case VIEW_CONVERT_EXPR:
19412 case NON_LVALUE_EXPR:
19413 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19414 default:
19415 return native_encode_expr (init, array, size) == size;
19419 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19420 attribute is the const value T. */
19422 static bool
19423 tree_add_const_value_attribute (dw_die_ref die, tree t)
19425 tree init;
19426 tree type = TREE_TYPE (t);
19427 rtx rtl;
19429 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19430 return false;
19432 init = t;
19433 gcc_assert (!DECL_P (init));
19435 if (! early_dwarf)
19437 rtl = rtl_for_decl_init (init, type);
19438 if (rtl)
19439 return add_const_value_attribute (die, rtl);
19441 /* If the host and target are sane, try harder. */
19442 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19443 && initializer_constant_valid_p (init, type))
19445 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19446 if (size > 0 && (int) size == size)
19448 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19450 if (native_encode_initializer (init, array, size))
19452 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19453 return true;
19455 ggc_free (array);
19458 return false;
19461 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19462 attribute is the const value of T, where T is an integral constant
19463 variable with static storage duration
19464 (so it can't be a PARM_DECL or a RESULT_DECL). */
19466 static bool
19467 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19470 if (!decl
19471 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19472 || (VAR_P (decl) && !TREE_STATIC (decl)))
19473 return false;
19475 if (TREE_READONLY (decl)
19476 && ! TREE_THIS_VOLATILE (decl)
19477 && DECL_INITIAL (decl))
19478 /* OK */;
19479 else
19480 return false;
19482 /* Don't add DW_AT_const_value if abstract origin already has one. */
19483 if (get_AT (var_die, DW_AT_const_value))
19484 return false;
19486 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19489 /* Convert the CFI instructions for the current function into a
19490 location list. This is used for DW_AT_frame_base when we targeting
19491 a dwarf2 consumer that does not support the dwarf3
19492 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19493 expressions. */
19495 static dw_loc_list_ref
19496 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19498 int ix;
19499 dw_fde_ref fde;
19500 dw_loc_list_ref list, *list_tail;
19501 dw_cfi_ref cfi;
19502 dw_cfa_location last_cfa, next_cfa;
19503 const char *start_label, *last_label, *section;
19504 dw_cfa_location remember;
19506 fde = cfun->fde;
19507 gcc_assert (fde != NULL);
19509 section = secname_for_decl (current_function_decl);
19510 list_tail = &list;
19511 list = NULL;
19513 memset (&next_cfa, 0, sizeof (next_cfa));
19514 next_cfa.reg = INVALID_REGNUM;
19515 remember = next_cfa;
19517 start_label = fde->dw_fde_begin;
19519 /* ??? Bald assumption that the CIE opcode list does not contain
19520 advance opcodes. */
19521 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19522 lookup_cfa_1 (cfi, &next_cfa, &remember);
19524 last_cfa = next_cfa;
19525 last_label = start_label;
19527 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19529 /* If the first partition contained no CFI adjustments, the
19530 CIE opcodes apply to the whole first partition. */
19531 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19532 fde->dw_fde_begin, fde->dw_fde_end, section);
19533 list_tail =&(*list_tail)->dw_loc_next;
19534 start_label = last_label = fde->dw_fde_second_begin;
19537 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19539 switch (cfi->dw_cfi_opc)
19541 case DW_CFA_set_loc:
19542 case DW_CFA_advance_loc1:
19543 case DW_CFA_advance_loc2:
19544 case DW_CFA_advance_loc4:
19545 if (!cfa_equal_p (&last_cfa, &next_cfa))
19547 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19548 start_label, last_label, section);
19550 list_tail = &(*list_tail)->dw_loc_next;
19551 last_cfa = next_cfa;
19552 start_label = last_label;
19554 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19555 break;
19557 case DW_CFA_advance_loc:
19558 /* The encoding is complex enough that we should never emit this. */
19559 gcc_unreachable ();
19561 default:
19562 lookup_cfa_1 (cfi, &next_cfa, &remember);
19563 break;
19565 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19567 if (!cfa_equal_p (&last_cfa, &next_cfa))
19569 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19570 start_label, last_label, section);
19572 list_tail = &(*list_tail)->dw_loc_next;
19573 last_cfa = next_cfa;
19574 start_label = last_label;
19576 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19577 start_label, fde->dw_fde_end, section);
19578 list_tail = &(*list_tail)->dw_loc_next;
19579 start_label = last_label = fde->dw_fde_second_begin;
19583 if (!cfa_equal_p (&last_cfa, &next_cfa))
19585 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19586 start_label, last_label, section);
19587 list_tail = &(*list_tail)->dw_loc_next;
19588 start_label = last_label;
19591 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19592 start_label,
19593 fde->dw_fde_second_begin
19594 ? fde->dw_fde_second_end : fde->dw_fde_end,
19595 section);
19597 if (list && list->dw_loc_next)
19598 gen_llsym (list);
19600 return list;
19603 /* Compute a displacement from the "steady-state frame pointer" to the
19604 frame base (often the same as the CFA), and store it in
19605 frame_pointer_fb_offset. OFFSET is added to the displacement
19606 before the latter is negated. */
19608 static void
19609 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19611 rtx reg, elim;
19613 #ifdef FRAME_POINTER_CFA_OFFSET
19614 reg = frame_pointer_rtx;
19615 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19616 #else
19617 reg = arg_pointer_rtx;
19618 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19619 #endif
19621 elim = (ira_use_lra_p
19622 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19623 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19624 if (GET_CODE (elim) == PLUS)
19626 offset += INTVAL (XEXP (elim, 1));
19627 elim = XEXP (elim, 0);
19630 frame_pointer_fb_offset = -offset;
19632 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19633 in which to eliminate. This is because it's stack pointer isn't
19634 directly accessible as a register within the ISA. To work around
19635 this, assume that while we cannot provide a proper value for
19636 frame_pointer_fb_offset, we won't need one either. */
19637 frame_pointer_fb_offset_valid
19638 = ((SUPPORTS_STACK_ALIGNMENT
19639 && (elim == hard_frame_pointer_rtx
19640 || elim == stack_pointer_rtx))
19641 || elim == (frame_pointer_needed
19642 ? hard_frame_pointer_rtx
19643 : stack_pointer_rtx));
19646 /* Generate a DW_AT_name attribute given some string value to be included as
19647 the value of the attribute. */
19649 static void
19650 add_name_attribute (dw_die_ref die, const char *name_string)
19652 if (name_string != NULL && *name_string != 0)
19654 if (demangle_name_func)
19655 name_string = (*demangle_name_func) (name_string);
19657 add_AT_string (die, DW_AT_name, name_string);
19661 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19662 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19663 of TYPE accordingly.
19665 ??? This is a temporary measure until after we're able to generate
19666 regular DWARF for the complex Ada type system. */
19668 static void
19669 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19670 dw_die_ref context_die)
19672 tree dtype;
19673 dw_die_ref dtype_die;
19675 if (!lang_hooks.types.descriptive_type)
19676 return;
19678 dtype = lang_hooks.types.descriptive_type (type);
19679 if (!dtype)
19680 return;
19682 dtype_die = lookup_type_die (dtype);
19683 if (!dtype_die)
19685 gen_type_die (dtype, context_die);
19686 dtype_die = lookup_type_die (dtype);
19687 gcc_assert (dtype_die);
19690 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19693 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19695 static const char *
19696 comp_dir_string (void)
19698 const char *wd;
19699 char *wd1;
19700 static const char *cached_wd = NULL;
19702 if (cached_wd != NULL)
19703 return cached_wd;
19705 wd = get_src_pwd ();
19706 if (wd == NULL)
19707 return NULL;
19709 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19711 int wdlen;
19713 wdlen = strlen (wd);
19714 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19715 strcpy (wd1, wd);
19716 wd1 [wdlen] = DIR_SEPARATOR;
19717 wd1 [wdlen + 1] = 0;
19718 wd = wd1;
19721 cached_wd = remap_debug_filename (wd);
19722 return cached_wd;
19725 /* Generate a DW_AT_comp_dir attribute for DIE. */
19727 static void
19728 add_comp_dir_attribute (dw_die_ref die)
19730 const char * wd = comp_dir_string ();
19731 if (wd != NULL)
19732 add_AT_string (die, DW_AT_comp_dir, wd);
19735 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19736 pointer computation, ...), output a representation for that bound according
19737 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19738 loc_list_from_tree for the meaning of CONTEXT. */
19740 static void
19741 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19742 int forms, struct loc_descr_context *context)
19744 dw_die_ref context_die, decl_die;
19745 dw_loc_list_ref list;
19746 bool strip_conversions = true;
19747 bool placeholder_seen = false;
19749 while (strip_conversions)
19750 switch (TREE_CODE (value))
19752 case ERROR_MARK:
19753 case SAVE_EXPR:
19754 return;
19756 CASE_CONVERT:
19757 case VIEW_CONVERT_EXPR:
19758 value = TREE_OPERAND (value, 0);
19759 break;
19761 default:
19762 strip_conversions = false;
19763 break;
19766 /* If possible and permitted, output the attribute as a constant. */
19767 if ((forms & dw_scalar_form_constant) != 0
19768 && TREE_CODE (value) == INTEGER_CST)
19770 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19772 /* If HOST_WIDE_INT is big enough then represent the bound as
19773 a constant value. We need to choose a form based on
19774 whether the type is signed or unsigned. We cannot just
19775 call add_AT_unsigned if the value itself is positive
19776 (add_AT_unsigned might add the unsigned value encoded as
19777 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19778 bounds type and then sign extend any unsigned values found
19779 for signed types. This is needed only for
19780 DW_AT_{lower,upper}_bound, since for most other attributes,
19781 consumers will treat DW_FORM_data[1248] as unsigned values,
19782 regardless of the underlying type. */
19783 if (prec <= HOST_BITS_PER_WIDE_INT
19784 || tree_fits_uhwi_p (value))
19786 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19787 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19788 else
19789 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19791 else
19792 /* Otherwise represent the bound as an unsigned value with
19793 the precision of its type. The precision and signedness
19794 of the type will be necessary to re-interpret it
19795 unambiguously. */
19796 add_AT_wide (die, attr, value);
19797 return;
19800 /* Otherwise, if it's possible and permitted too, output a reference to
19801 another DIE. */
19802 if ((forms & dw_scalar_form_reference) != 0)
19804 tree decl = NULL_TREE;
19806 /* Some type attributes reference an outer type. For instance, the upper
19807 bound of an array may reference an embedding record (this happens in
19808 Ada). */
19809 if (TREE_CODE (value) == COMPONENT_REF
19810 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19811 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19812 decl = TREE_OPERAND (value, 1);
19814 else if (VAR_P (value)
19815 || TREE_CODE (value) == PARM_DECL
19816 || TREE_CODE (value) == RESULT_DECL)
19817 decl = value;
19819 if (decl != NULL_TREE)
19821 dw_die_ref decl_die = lookup_decl_die (decl);
19823 /* ??? Can this happen, or should the variable have been bound
19824 first? Probably it can, since I imagine that we try to create
19825 the types of parameters in the order in which they exist in
19826 the list, and won't have created a forward reference to a
19827 later parameter. */
19828 if (decl_die != NULL)
19830 add_AT_die_ref (die, attr, decl_die);
19831 return;
19836 /* Last chance: try to create a stack operation procedure to evaluate the
19837 value. Do nothing if even that is not possible or permitted. */
19838 if ((forms & dw_scalar_form_exprloc) == 0)
19839 return;
19841 list = loc_list_from_tree (value, 2, context);
19842 if (context && context->placeholder_arg)
19844 placeholder_seen = context->placeholder_seen;
19845 context->placeholder_seen = false;
19847 if (list == NULL || single_element_loc_list_p (list))
19849 /* If this attribute is not a reference nor constant, it is
19850 a DWARF expression rather than location description. For that
19851 loc_list_from_tree (value, 0, &context) is needed. */
19852 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19853 if (list2 && single_element_loc_list_p (list2))
19855 if (placeholder_seen)
19857 struct dwarf_procedure_info dpi;
19858 dpi.fndecl = NULL_TREE;
19859 dpi.args_count = 1;
19860 if (!resolve_args_picking (list2->expr, 1, &dpi))
19861 return;
19863 add_AT_loc (die, attr, list2->expr);
19864 return;
19868 /* If that failed to give a single element location list, fall back to
19869 outputting this as a reference... still if permitted. */
19870 if (list == NULL
19871 || (forms & dw_scalar_form_reference) == 0
19872 || placeholder_seen)
19873 return;
19875 if (current_function_decl == 0)
19876 context_die = comp_unit_die ();
19877 else
19878 context_die = lookup_decl_die (current_function_decl);
19880 decl_die = new_die (DW_TAG_variable, context_die, value);
19881 add_AT_flag (decl_die, DW_AT_artificial, 1);
19882 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19883 context_die);
19884 add_AT_location_description (decl_die, DW_AT_location, list);
19885 add_AT_die_ref (die, attr, decl_die);
19888 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19889 default. */
19891 static int
19892 lower_bound_default (void)
19894 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19896 case DW_LANG_C:
19897 case DW_LANG_C89:
19898 case DW_LANG_C99:
19899 case DW_LANG_C11:
19900 case DW_LANG_C_plus_plus:
19901 case DW_LANG_C_plus_plus_11:
19902 case DW_LANG_C_plus_plus_14:
19903 case DW_LANG_ObjC:
19904 case DW_LANG_ObjC_plus_plus:
19905 return 0;
19906 case DW_LANG_Fortran77:
19907 case DW_LANG_Fortran90:
19908 case DW_LANG_Fortran95:
19909 case DW_LANG_Fortran03:
19910 case DW_LANG_Fortran08:
19911 return 1;
19912 case DW_LANG_UPC:
19913 case DW_LANG_D:
19914 case DW_LANG_Python:
19915 return dwarf_version >= 4 ? 0 : -1;
19916 case DW_LANG_Ada95:
19917 case DW_LANG_Ada83:
19918 case DW_LANG_Cobol74:
19919 case DW_LANG_Cobol85:
19920 case DW_LANG_Modula2:
19921 case DW_LANG_PLI:
19922 return dwarf_version >= 4 ? 1 : -1;
19923 default:
19924 return -1;
19928 /* Given a tree node describing an array bound (either lower or upper) output
19929 a representation for that bound. */
19931 static void
19932 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19933 tree bound, struct loc_descr_context *context)
19935 int dflt;
19937 while (1)
19938 switch (TREE_CODE (bound))
19940 /* Strip all conversions. */
19941 CASE_CONVERT:
19942 case VIEW_CONVERT_EXPR:
19943 bound = TREE_OPERAND (bound, 0);
19944 break;
19946 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19947 are even omitted when they are the default. */
19948 case INTEGER_CST:
19949 /* If the value for this bound is the default one, we can even omit the
19950 attribute. */
19951 if (bound_attr == DW_AT_lower_bound
19952 && tree_fits_shwi_p (bound)
19953 && (dflt = lower_bound_default ()) != -1
19954 && tree_to_shwi (bound) == dflt)
19955 return;
19957 /* FALLTHRU */
19959 default:
19960 /* Because of the complex interaction there can be with other GNAT
19961 encodings, GDB isn't ready yet to handle proper DWARF description
19962 for self-referencial subrange bounds: let GNAT encodings do the
19963 magic in such a case. */
19964 if (is_ada ()
19965 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
19966 && contains_placeholder_p (bound))
19967 return;
19969 add_scalar_info (subrange_die, bound_attr, bound,
19970 dw_scalar_form_constant
19971 | dw_scalar_form_exprloc
19972 | dw_scalar_form_reference,
19973 context);
19974 return;
19978 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
19979 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
19980 Note that the block of subscript information for an array type also
19981 includes information about the element type of the given array type.
19983 This function reuses previously set type and bound information if
19984 available. */
19986 static void
19987 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
19989 unsigned dimension_number;
19990 tree lower, upper;
19991 dw_die_ref child = type_die->die_child;
19993 for (dimension_number = 0;
19994 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
19995 type = TREE_TYPE (type), dimension_number++)
19997 tree domain = TYPE_DOMAIN (type);
19999 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20000 break;
20002 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20003 and (in GNU C only) variable bounds. Handle all three forms
20004 here. */
20006 /* Find and reuse a previously generated DW_TAG_subrange_type if
20007 available.
20009 For multi-dimensional arrays, as we iterate through the
20010 various dimensions in the enclosing for loop above, we also
20011 iterate through the DIE children and pick at each
20012 DW_TAG_subrange_type previously generated (if available).
20013 Each child DW_TAG_subrange_type DIE describes the range of
20014 the current dimension. At this point we should have as many
20015 DW_TAG_subrange_type's as we have dimensions in the
20016 array. */
20017 dw_die_ref subrange_die = NULL;
20018 if (child)
20019 while (1)
20021 child = child->die_sib;
20022 if (child->die_tag == DW_TAG_subrange_type)
20023 subrange_die = child;
20024 if (child == type_die->die_child)
20026 /* If we wrapped around, stop looking next time. */
20027 child = NULL;
20028 break;
20030 if (child->die_tag == DW_TAG_subrange_type)
20031 break;
20033 if (!subrange_die)
20034 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20036 if (domain)
20038 /* We have an array type with specified bounds. */
20039 lower = TYPE_MIN_VALUE (domain);
20040 upper = TYPE_MAX_VALUE (domain);
20042 /* Define the index type. */
20043 if (TREE_TYPE (domain)
20044 && !get_AT (subrange_die, DW_AT_type))
20046 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20047 TREE_TYPE field. We can't emit debug info for this
20048 because it is an unnamed integral type. */
20049 if (TREE_CODE (domain) == INTEGER_TYPE
20050 && TYPE_NAME (domain) == NULL_TREE
20051 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20052 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20054 else
20055 add_type_attribute (subrange_die, TREE_TYPE (domain),
20056 TYPE_UNQUALIFIED, false, type_die);
20059 /* ??? If upper is NULL, the array has unspecified length,
20060 but it does have a lower bound. This happens with Fortran
20061 dimension arr(N:*)
20062 Since the debugger is definitely going to need to know N
20063 to produce useful results, go ahead and output the lower
20064 bound solo, and hope the debugger can cope. */
20066 if (!get_AT (subrange_die, DW_AT_lower_bound))
20067 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20068 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20069 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20072 /* Otherwise we have an array type with an unspecified length. The
20073 DWARF-2 spec does not say how to handle this; let's just leave out the
20074 bounds. */
20078 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20080 static void
20081 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20083 dw_die_ref decl_die;
20084 HOST_WIDE_INT size;
20085 dw_loc_descr_ref size_expr = NULL;
20087 switch (TREE_CODE (tree_node))
20089 case ERROR_MARK:
20090 size = 0;
20091 break;
20092 case ENUMERAL_TYPE:
20093 case RECORD_TYPE:
20094 case UNION_TYPE:
20095 case QUAL_UNION_TYPE:
20096 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20097 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20099 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20100 return;
20102 size_expr = type_byte_size (tree_node, &size);
20103 break;
20104 case FIELD_DECL:
20105 /* For a data member of a struct or union, the DW_AT_byte_size is
20106 generally given as the number of bytes normally allocated for an
20107 object of the *declared* type of the member itself. This is true
20108 even for bit-fields. */
20109 size = int_size_in_bytes (field_type (tree_node));
20110 break;
20111 default:
20112 gcc_unreachable ();
20115 /* Support for dynamically-sized objects was introduced by DWARFv3.
20116 At the moment, GDB does not handle variable byte sizes very well,
20117 though. */
20118 if ((dwarf_version >= 3 || !dwarf_strict)
20119 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20120 && size_expr != NULL)
20121 add_AT_loc (die, DW_AT_byte_size, size_expr);
20123 /* Note that `size' might be -1 when we get to this point. If it is, that
20124 indicates that the byte size of the entity in question is variable and
20125 that we could not generate a DWARF expression that computes it. */
20126 if (size >= 0)
20127 add_AT_unsigned (die, DW_AT_byte_size, size);
20130 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20131 alignment. */
20133 static void
20134 add_alignment_attribute (dw_die_ref die, tree tree_node)
20136 if (dwarf_version < 5 && dwarf_strict)
20137 return;
20139 unsigned align;
20141 if (DECL_P (tree_node))
20143 if (!DECL_USER_ALIGN (tree_node))
20144 return;
20146 align = DECL_ALIGN_UNIT (tree_node);
20148 else if (TYPE_P (tree_node))
20150 if (!TYPE_USER_ALIGN (tree_node))
20151 return;
20153 align = TYPE_ALIGN_UNIT (tree_node);
20155 else
20156 gcc_unreachable ();
20158 add_AT_unsigned (die, DW_AT_alignment, align);
20161 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20162 which specifies the distance in bits from the highest order bit of the
20163 "containing object" for the bit-field to the highest order bit of the
20164 bit-field itself.
20166 For any given bit-field, the "containing object" is a hypothetical object
20167 (of some integral or enum type) within which the given bit-field lives. The
20168 type of this hypothetical "containing object" is always the same as the
20169 declared type of the individual bit-field itself. The determination of the
20170 exact location of the "containing object" for a bit-field is rather
20171 complicated. It's handled by the `field_byte_offset' function (above).
20173 CTX is required: see the comment for VLR_CONTEXT.
20175 Note that it is the size (in bytes) of the hypothetical "containing object"
20176 which will be given in the DW_AT_byte_size attribute for this bit-field.
20177 (See `byte_size_attribute' above). */
20179 static inline void
20180 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20182 HOST_WIDE_INT object_offset_in_bytes;
20183 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20184 HOST_WIDE_INT bitpos_int;
20185 HOST_WIDE_INT highest_order_object_bit_offset;
20186 HOST_WIDE_INT highest_order_field_bit_offset;
20187 HOST_WIDE_INT bit_offset;
20189 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20191 /* Must be a field and a bit field. */
20192 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20194 /* We can't yet handle bit-fields whose offsets are variable, so if we
20195 encounter such things, just return without generating any attribute
20196 whatsoever. Likewise for variable or too large size. */
20197 if (! tree_fits_shwi_p (bit_position (decl))
20198 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20199 return;
20201 bitpos_int = int_bit_position (decl);
20203 /* Note that the bit offset is always the distance (in bits) from the
20204 highest-order bit of the "containing object" to the highest-order bit of
20205 the bit-field itself. Since the "high-order end" of any object or field
20206 is different on big-endian and little-endian machines, the computation
20207 below must take account of these differences. */
20208 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20209 highest_order_field_bit_offset = bitpos_int;
20211 if (! BYTES_BIG_ENDIAN)
20213 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20214 highest_order_object_bit_offset +=
20215 simple_type_size_in_bits (original_type);
20218 bit_offset
20219 = (! BYTES_BIG_ENDIAN
20220 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20221 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20223 if (bit_offset < 0)
20224 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20225 else
20226 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20229 /* For a FIELD_DECL node which represents a bit field, output an attribute
20230 which specifies the length in bits of the given field. */
20232 static inline void
20233 add_bit_size_attribute (dw_die_ref die, tree decl)
20235 /* Must be a field and a bit field. */
20236 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20237 && DECL_BIT_FIELD_TYPE (decl));
20239 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20240 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20243 /* If the compiled language is ANSI C, then add a 'prototyped'
20244 attribute, if arg types are given for the parameters of a function. */
20246 static inline void
20247 add_prototyped_attribute (dw_die_ref die, tree func_type)
20249 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20251 case DW_LANG_C:
20252 case DW_LANG_C89:
20253 case DW_LANG_C99:
20254 case DW_LANG_C11:
20255 case DW_LANG_ObjC:
20256 if (prototype_p (func_type))
20257 add_AT_flag (die, DW_AT_prototyped, 1);
20258 break;
20259 default:
20260 break;
20264 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20265 by looking in the type declaration, the object declaration equate table or
20266 the block mapping. */
20268 static inline dw_die_ref
20269 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20271 dw_die_ref origin_die = NULL;
20273 if (DECL_P (origin))
20275 dw_die_ref c;
20276 origin_die = lookup_decl_die (origin);
20277 /* "Unwrap" the decls DIE which we put in the imported unit context.
20278 We are looking for the abstract copy here. */
20279 if (in_lto_p
20280 && origin_die
20281 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20282 /* ??? Identify this better. */
20283 && c->with_offset)
20284 origin_die = c;
20286 else if (TYPE_P (origin))
20287 origin_die = lookup_type_die (origin);
20288 else if (TREE_CODE (origin) == BLOCK)
20289 origin_die = BLOCK_DIE (origin);
20291 /* XXX: Functions that are never lowered don't always have correct block
20292 trees (in the case of java, they simply have no block tree, in some other
20293 languages). For these functions, there is nothing we can really do to
20294 output correct debug info for inlined functions in all cases. Rather
20295 than die, we'll just produce deficient debug info now, in that we will
20296 have variables without a proper abstract origin. In the future, when all
20297 functions are lowered, we should re-add a gcc_assert (origin_die)
20298 here. */
20300 if (origin_die)
20301 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20302 return origin_die;
20305 /* We do not currently support the pure_virtual attribute. */
20307 static inline void
20308 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20310 if (DECL_VINDEX (func_decl))
20312 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20314 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20315 add_AT_loc (die, DW_AT_vtable_elem_location,
20316 new_loc_descr (DW_OP_constu,
20317 tree_to_shwi (DECL_VINDEX (func_decl)),
20318 0));
20320 /* GNU extension: Record what type this method came from originally. */
20321 if (debug_info_level > DINFO_LEVEL_TERSE
20322 && DECL_CONTEXT (func_decl))
20323 add_AT_die_ref (die, DW_AT_containing_type,
20324 lookup_type_die (DECL_CONTEXT (func_decl)));
20328 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20329 given decl. This used to be a vendor extension until after DWARF 4
20330 standardized it. */
20332 static void
20333 add_linkage_attr (dw_die_ref die, tree decl)
20335 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20337 /* Mimic what assemble_name_raw does with a leading '*'. */
20338 if (name[0] == '*')
20339 name = &name[1];
20341 if (dwarf_version >= 4)
20342 add_AT_string (die, DW_AT_linkage_name, name);
20343 else
20344 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20347 /* Add source coordinate attributes for the given decl. */
20349 static void
20350 add_src_coords_attributes (dw_die_ref die, tree decl)
20352 expanded_location s;
20354 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20355 return;
20356 s = expand_location (DECL_SOURCE_LOCATION (decl));
20357 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20358 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20359 if (debug_column_info && s.column)
20360 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20363 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20365 static void
20366 add_linkage_name_raw (dw_die_ref die, tree decl)
20368 /* Defer until we have an assembler name set. */
20369 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20371 limbo_die_node *asm_name;
20373 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20374 asm_name->die = die;
20375 asm_name->created_for = decl;
20376 asm_name->next = deferred_asm_name;
20377 deferred_asm_name = asm_name;
20379 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20380 add_linkage_attr (die, decl);
20383 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20385 static void
20386 add_linkage_name (dw_die_ref die, tree decl)
20388 if (debug_info_level > DINFO_LEVEL_NONE
20389 && VAR_OR_FUNCTION_DECL_P (decl)
20390 && TREE_PUBLIC (decl)
20391 && !(VAR_P (decl) && DECL_REGISTER (decl))
20392 && die->die_tag != DW_TAG_member)
20393 add_linkage_name_raw (die, decl);
20396 /* Add a DW_AT_name attribute and source coordinate attribute for the
20397 given decl, but only if it actually has a name. */
20399 static void
20400 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20401 bool no_linkage_name)
20403 tree decl_name;
20405 decl_name = DECL_NAME (decl);
20406 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20408 const char *name = dwarf2_name (decl, 0);
20409 if (name)
20410 add_name_attribute (die, name);
20411 if (! DECL_ARTIFICIAL (decl))
20412 add_src_coords_attributes (die, decl);
20414 if (!no_linkage_name)
20415 add_linkage_name (die, decl);
20418 #ifdef VMS_DEBUGGING_INFO
20419 /* Get the function's name, as described by its RTL. This may be different
20420 from the DECL_NAME name used in the source file. */
20421 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20423 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20424 XEXP (DECL_RTL (decl), 0), false);
20425 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20427 #endif /* VMS_DEBUGGING_INFO */
20430 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20432 static void
20433 add_discr_value (dw_die_ref die, dw_discr_value *value)
20435 dw_attr_node attr;
20437 attr.dw_attr = DW_AT_discr_value;
20438 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20439 attr.dw_attr_val.val_entry = NULL;
20440 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20441 if (value->pos)
20442 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20443 else
20444 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20445 add_dwarf_attr (die, &attr);
20448 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20450 static void
20451 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20453 dw_attr_node attr;
20455 attr.dw_attr = DW_AT_discr_list;
20456 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20457 attr.dw_attr_val.val_entry = NULL;
20458 attr.dw_attr_val.v.val_discr_list = discr_list;
20459 add_dwarf_attr (die, &attr);
20462 static inline dw_discr_list_ref
20463 AT_discr_list (dw_attr_node *attr)
20465 return attr->dw_attr_val.v.val_discr_list;
20468 #ifdef VMS_DEBUGGING_INFO
20469 /* Output the debug main pointer die for VMS */
20471 void
20472 dwarf2out_vms_debug_main_pointer (void)
20474 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20475 dw_die_ref die;
20477 /* Allocate the VMS debug main subprogram die. */
20478 die = ggc_cleared_alloc<die_node> ();
20479 die->die_tag = DW_TAG_subprogram;
20480 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20481 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20482 current_function_funcdef_no);
20483 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20485 /* Make it the first child of comp_unit_die (). */
20486 die->die_parent = comp_unit_die ();
20487 if (comp_unit_die ()->die_child)
20489 die->die_sib = comp_unit_die ()->die_child->die_sib;
20490 comp_unit_die ()->die_child->die_sib = die;
20492 else
20494 die->die_sib = die;
20495 comp_unit_die ()->die_child = die;
20498 #endif /* VMS_DEBUGGING_INFO */
20500 /* Push a new declaration scope. */
20502 static void
20503 push_decl_scope (tree scope)
20505 vec_safe_push (decl_scope_table, scope);
20508 /* Pop a declaration scope. */
20510 static inline void
20511 pop_decl_scope (void)
20513 decl_scope_table->pop ();
20516 /* walk_tree helper function for uses_local_type, below. */
20518 static tree
20519 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20521 if (!TYPE_P (*tp))
20522 *walk_subtrees = 0;
20523 else
20525 tree name = TYPE_NAME (*tp);
20526 if (name && DECL_P (name) && decl_function_context (name))
20527 return *tp;
20529 return NULL_TREE;
20532 /* If TYPE involves a function-local type (including a local typedef to a
20533 non-local type), returns that type; otherwise returns NULL_TREE. */
20535 static tree
20536 uses_local_type (tree type)
20538 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20539 return used;
20542 /* Return the DIE for the scope that immediately contains this type.
20543 Non-named types that do not involve a function-local type get global
20544 scope. Named types nested in namespaces or other types get their
20545 containing scope. All other types (i.e. function-local named types) get
20546 the current active scope. */
20548 static dw_die_ref
20549 scope_die_for (tree t, dw_die_ref context_die)
20551 dw_die_ref scope_die = NULL;
20552 tree containing_scope;
20554 /* Non-types always go in the current scope. */
20555 gcc_assert (TYPE_P (t));
20557 /* Use the scope of the typedef, rather than the scope of the type
20558 it refers to. */
20559 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20560 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20561 else
20562 containing_scope = TYPE_CONTEXT (t);
20564 /* Use the containing namespace if there is one. */
20565 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20567 if (context_die == lookup_decl_die (containing_scope))
20568 /* OK */;
20569 else if (debug_info_level > DINFO_LEVEL_TERSE)
20570 context_die = get_context_die (containing_scope);
20571 else
20572 containing_scope = NULL_TREE;
20575 /* Ignore function type "scopes" from the C frontend. They mean that
20576 a tagged type is local to a parmlist of a function declarator, but
20577 that isn't useful to DWARF. */
20578 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20579 containing_scope = NULL_TREE;
20581 if (SCOPE_FILE_SCOPE_P (containing_scope))
20583 /* If T uses a local type keep it local as well, to avoid references
20584 to function-local DIEs from outside the function. */
20585 if (current_function_decl && uses_local_type (t))
20586 scope_die = context_die;
20587 else
20588 scope_die = comp_unit_die ();
20590 else if (TYPE_P (containing_scope))
20592 /* For types, we can just look up the appropriate DIE. */
20593 if (debug_info_level > DINFO_LEVEL_TERSE)
20594 scope_die = get_context_die (containing_scope);
20595 else
20597 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20598 if (scope_die == NULL)
20599 scope_die = comp_unit_die ();
20602 else
20603 scope_die = context_die;
20605 return scope_die;
20608 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20610 static inline int
20611 local_scope_p (dw_die_ref context_die)
20613 for (; context_die; context_die = context_die->die_parent)
20614 if (context_die->die_tag == DW_TAG_inlined_subroutine
20615 || context_die->die_tag == DW_TAG_subprogram)
20616 return 1;
20618 return 0;
20621 /* Returns nonzero if CONTEXT_DIE is a class. */
20623 static inline int
20624 class_scope_p (dw_die_ref context_die)
20626 return (context_die
20627 && (context_die->die_tag == DW_TAG_structure_type
20628 || context_die->die_tag == DW_TAG_class_type
20629 || context_die->die_tag == DW_TAG_interface_type
20630 || context_die->die_tag == DW_TAG_union_type));
20633 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20634 whether or not to treat a DIE in this context as a declaration. */
20636 static inline int
20637 class_or_namespace_scope_p (dw_die_ref context_die)
20639 return (class_scope_p (context_die)
20640 || (context_die && context_die->die_tag == DW_TAG_namespace));
20643 /* Many forms of DIEs require a "type description" attribute. This
20644 routine locates the proper "type descriptor" die for the type given
20645 by 'type' plus any additional qualifiers given by 'cv_quals', and
20646 adds a DW_AT_type attribute below the given die. */
20648 static void
20649 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20650 bool reverse, dw_die_ref context_die)
20652 enum tree_code code = TREE_CODE (type);
20653 dw_die_ref type_die = NULL;
20655 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20656 or fixed-point type, use the inner type. This is because we have no
20657 support for unnamed types in base_type_die. This can happen if this is
20658 an Ada subrange type. Correct solution is emit a subrange type die. */
20659 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20660 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20661 type = TREE_TYPE (type), code = TREE_CODE (type);
20663 if (code == ERROR_MARK
20664 /* Handle a special case. For functions whose return type is void, we
20665 generate *no* type attribute. (Note that no object may have type
20666 `void', so this only applies to function return types). */
20667 || code == VOID_TYPE)
20668 return;
20670 type_die = modified_type_die (type,
20671 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20672 reverse,
20673 context_die);
20675 if (type_die != NULL)
20676 add_AT_die_ref (object_die, DW_AT_type, type_die);
20679 /* Given an object die, add the calling convention attribute for the
20680 function call type. */
20681 static void
20682 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20684 enum dwarf_calling_convention value = DW_CC_normal;
20686 value = ((enum dwarf_calling_convention)
20687 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20689 if (is_fortran ()
20690 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20692 /* DWARF 2 doesn't provide a way to identify a program's source-level
20693 entry point. DW_AT_calling_convention attributes are only meant
20694 to describe functions' calling conventions. However, lacking a
20695 better way to signal the Fortran main program, we used this for
20696 a long time, following existing custom. Now, DWARF 4 has
20697 DW_AT_main_subprogram, which we add below, but some tools still
20698 rely on the old way, which we thus keep. */
20699 value = DW_CC_program;
20701 if (dwarf_version >= 4 || !dwarf_strict)
20702 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20705 /* Only add the attribute if the backend requests it, and
20706 is not DW_CC_normal. */
20707 if (value && (value != DW_CC_normal))
20708 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20711 /* Given a tree pointer to a struct, class, union, or enum type node, return
20712 a pointer to the (string) tag name for the given type, or zero if the type
20713 was declared without a tag. */
20715 static const char *
20716 type_tag (const_tree type)
20718 const char *name = 0;
20720 if (TYPE_NAME (type) != 0)
20722 tree t = 0;
20724 /* Find the IDENTIFIER_NODE for the type name. */
20725 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20726 && !TYPE_NAMELESS (type))
20727 t = TYPE_NAME (type);
20729 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20730 a TYPE_DECL node, regardless of whether or not a `typedef' was
20731 involved. */
20732 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20733 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20735 /* We want to be extra verbose. Don't call dwarf_name if
20736 DECL_NAME isn't set. The default hook for decl_printable_name
20737 doesn't like that, and in this context it's correct to return
20738 0, instead of "<anonymous>" or the like. */
20739 if (DECL_NAME (TYPE_NAME (type))
20740 && !DECL_NAMELESS (TYPE_NAME (type)))
20741 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20744 /* Now get the name as a string, or invent one. */
20745 if (!name && t != 0)
20746 name = IDENTIFIER_POINTER (t);
20749 return (name == 0 || *name == '\0') ? 0 : name;
20752 /* Return the type associated with a data member, make a special check
20753 for bit field types. */
20755 static inline tree
20756 member_declared_type (const_tree member)
20758 return (DECL_BIT_FIELD_TYPE (member)
20759 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20762 /* Get the decl's label, as described by its RTL. This may be different
20763 from the DECL_NAME name used in the source file. */
20765 #if 0
20766 static const char *
20767 decl_start_label (tree decl)
20769 rtx x;
20770 const char *fnname;
20772 x = DECL_RTL (decl);
20773 gcc_assert (MEM_P (x));
20775 x = XEXP (x, 0);
20776 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20778 fnname = XSTR (x, 0);
20779 return fnname;
20781 #endif
20783 /* For variable-length arrays that have been previously generated, but
20784 may be incomplete due to missing subscript info, fill the subscript
20785 info. Return TRUE if this is one of those cases. */
20786 static bool
20787 fill_variable_array_bounds (tree type)
20789 if (TREE_ASM_WRITTEN (type)
20790 && TREE_CODE (type) == ARRAY_TYPE
20791 && variably_modified_type_p (type, NULL))
20793 dw_die_ref array_die = lookup_type_die (type);
20794 if (!array_die)
20795 return false;
20796 add_subscript_info (array_die, type, !is_ada ());
20797 return true;
20799 return false;
20802 /* These routines generate the internal representation of the DIE's for
20803 the compilation unit. Debugging information is collected by walking
20804 the declaration trees passed in from dwarf2out_decl(). */
20806 static void
20807 gen_array_type_die (tree type, dw_die_ref context_die)
20809 dw_die_ref array_die;
20811 /* GNU compilers represent multidimensional array types as sequences of one
20812 dimensional array types whose element types are themselves array types.
20813 We sometimes squish that down to a single array_type DIE with multiple
20814 subscripts in the Dwarf debugging info. The draft Dwarf specification
20815 say that we are allowed to do this kind of compression in C, because
20816 there is no difference between an array of arrays and a multidimensional
20817 array. We don't do this for Ada to remain as close as possible to the
20818 actual representation, which is especially important against the language
20819 flexibilty wrt arrays of variable size. */
20821 bool collapse_nested_arrays = !is_ada ();
20823 if (fill_variable_array_bounds (type))
20824 return;
20826 dw_die_ref scope_die = scope_die_for (type, context_die);
20827 tree element_type;
20829 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20830 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20831 if (TYPE_STRING_FLAG (type)
20832 && TREE_CODE (type) == ARRAY_TYPE
20833 && is_fortran ()
20834 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20836 HOST_WIDE_INT size;
20838 array_die = new_die (DW_TAG_string_type, scope_die, type);
20839 add_name_attribute (array_die, type_tag (type));
20840 equate_type_number_to_die (type, array_die);
20841 size = int_size_in_bytes (type);
20842 if (size >= 0)
20843 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20844 /* ??? We can't annotate types late, but for LTO we may not
20845 generate a location early either (gfortran.dg/save_6.f90). */
20846 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20847 && TYPE_DOMAIN (type) != NULL_TREE
20848 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20850 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20851 tree rszdecl = szdecl;
20853 size = int_size_in_bytes (TREE_TYPE (szdecl));
20854 if (!DECL_P (szdecl))
20856 if (TREE_CODE (szdecl) == INDIRECT_REF
20857 && DECL_P (TREE_OPERAND (szdecl, 0)))
20859 rszdecl = TREE_OPERAND (szdecl, 0);
20860 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20861 != DWARF2_ADDR_SIZE)
20862 size = 0;
20864 else
20865 size = 0;
20867 if (size > 0)
20869 dw_loc_list_ref loc
20870 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20871 NULL);
20872 if (loc)
20874 add_AT_location_description (array_die, DW_AT_string_length,
20875 loc);
20876 if (size != DWARF2_ADDR_SIZE)
20877 add_AT_unsigned (array_die, dwarf_version >= 5
20878 ? DW_AT_string_length_byte_size
20879 : DW_AT_byte_size, size);
20883 return;
20886 array_die = new_die (DW_TAG_array_type, scope_die, type);
20887 add_name_attribute (array_die, type_tag (type));
20888 equate_type_number_to_die (type, array_die);
20890 if (TREE_CODE (type) == VECTOR_TYPE)
20891 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20893 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20894 if (is_fortran ()
20895 && TREE_CODE (type) == ARRAY_TYPE
20896 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20897 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20898 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20900 #if 0
20901 /* We default the array ordering. SDB will probably do
20902 the right things even if DW_AT_ordering is not present. It's not even
20903 an issue until we start to get into multidimensional arrays anyway. If
20904 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20905 then we'll have to put the DW_AT_ordering attribute back in. (But if
20906 and when we find out that we need to put these in, we will only do so
20907 for multidimensional arrays. */
20908 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20909 #endif
20911 if (TREE_CODE (type) == VECTOR_TYPE)
20913 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20914 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20915 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20916 add_bound_info (subrange_die, DW_AT_upper_bound,
20917 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20919 else
20920 add_subscript_info (array_die, type, collapse_nested_arrays);
20922 /* Add representation of the type of the elements of this array type and
20923 emit the corresponding DIE if we haven't done it already. */
20924 element_type = TREE_TYPE (type);
20925 if (collapse_nested_arrays)
20926 while (TREE_CODE (element_type) == ARRAY_TYPE)
20928 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20929 break;
20930 element_type = TREE_TYPE (element_type);
20933 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20934 TREE_CODE (type) == ARRAY_TYPE
20935 && TYPE_REVERSE_STORAGE_ORDER (type),
20936 context_die);
20938 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20939 if (TYPE_ARTIFICIAL (type))
20940 add_AT_flag (array_die, DW_AT_artificial, 1);
20942 if (get_AT (array_die, DW_AT_name))
20943 add_pubtype (type, array_die);
20945 add_alignment_attribute (array_die, type);
20948 /* This routine generates DIE for array with hidden descriptor, details
20949 are filled into *info by a langhook. */
20951 static void
20952 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20953 dw_die_ref context_die)
20955 const dw_die_ref scope_die = scope_die_for (type, context_die);
20956 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20957 struct loc_descr_context context = { type, info->base_decl, NULL,
20958 false, false };
20959 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20960 int dim;
20962 add_name_attribute (array_die, type_tag (type));
20963 equate_type_number_to_die (type, array_die);
20965 if (info->ndimensions > 1)
20966 switch (info->ordering)
20968 case array_descr_ordering_row_major:
20969 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20970 break;
20971 case array_descr_ordering_column_major:
20972 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20973 break;
20974 default:
20975 break;
20978 if (dwarf_version >= 3 || !dwarf_strict)
20980 if (info->data_location)
20981 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
20982 dw_scalar_form_exprloc, &context);
20983 if (info->associated)
20984 add_scalar_info (array_die, DW_AT_associated, info->associated,
20985 dw_scalar_form_constant
20986 | dw_scalar_form_exprloc
20987 | dw_scalar_form_reference, &context);
20988 if (info->allocated)
20989 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
20990 dw_scalar_form_constant
20991 | dw_scalar_form_exprloc
20992 | dw_scalar_form_reference, &context);
20993 if (info->stride)
20995 const enum dwarf_attribute attr
20996 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
20997 const int forms
20998 = (info->stride_in_bits)
20999 ? dw_scalar_form_constant
21000 : (dw_scalar_form_constant
21001 | dw_scalar_form_exprloc
21002 | dw_scalar_form_reference);
21004 add_scalar_info (array_die, attr, info->stride, forms, &context);
21007 if (dwarf_version >= 5)
21009 if (info->rank)
21011 add_scalar_info (array_die, DW_AT_rank, info->rank,
21012 dw_scalar_form_constant
21013 | dw_scalar_form_exprloc, &context);
21014 subrange_tag = DW_TAG_generic_subrange;
21015 context.placeholder_arg = true;
21019 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21021 for (dim = 0; dim < info->ndimensions; dim++)
21023 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21025 if (info->dimen[dim].bounds_type)
21026 add_type_attribute (subrange_die,
21027 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21028 false, context_die);
21029 if (info->dimen[dim].lower_bound)
21030 add_bound_info (subrange_die, DW_AT_lower_bound,
21031 info->dimen[dim].lower_bound, &context);
21032 if (info->dimen[dim].upper_bound)
21033 add_bound_info (subrange_die, DW_AT_upper_bound,
21034 info->dimen[dim].upper_bound, &context);
21035 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21036 add_scalar_info (subrange_die, DW_AT_byte_stride,
21037 info->dimen[dim].stride,
21038 dw_scalar_form_constant
21039 | dw_scalar_form_exprloc
21040 | dw_scalar_form_reference,
21041 &context);
21044 gen_type_die (info->element_type, context_die);
21045 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21046 TREE_CODE (type) == ARRAY_TYPE
21047 && TYPE_REVERSE_STORAGE_ORDER (type),
21048 context_die);
21050 if (get_AT (array_die, DW_AT_name))
21051 add_pubtype (type, array_die);
21053 add_alignment_attribute (array_die, type);
21056 #if 0
21057 static void
21058 gen_entry_point_die (tree decl, dw_die_ref context_die)
21060 tree origin = decl_ultimate_origin (decl);
21061 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21063 if (origin != NULL)
21064 add_abstract_origin_attribute (decl_die, origin);
21065 else
21067 add_name_and_src_coords_attributes (decl_die, decl);
21068 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21069 TYPE_UNQUALIFIED, false, context_die);
21072 if (DECL_ABSTRACT_P (decl))
21073 equate_decl_number_to_die (decl, decl_die);
21074 else
21075 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21077 #endif
21079 /* Walk through the list of incomplete types again, trying once more to
21080 emit full debugging info for them. */
21082 static void
21083 retry_incomplete_types (void)
21085 set_early_dwarf s;
21086 int i;
21088 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21089 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21090 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21091 vec_safe_truncate (incomplete_types, 0);
21094 /* Determine what tag to use for a record type. */
21096 static enum dwarf_tag
21097 record_type_tag (tree type)
21099 if (! lang_hooks.types.classify_record)
21100 return DW_TAG_structure_type;
21102 switch (lang_hooks.types.classify_record (type))
21104 case RECORD_IS_STRUCT:
21105 return DW_TAG_structure_type;
21107 case RECORD_IS_CLASS:
21108 return DW_TAG_class_type;
21110 case RECORD_IS_INTERFACE:
21111 if (dwarf_version >= 3 || !dwarf_strict)
21112 return DW_TAG_interface_type;
21113 return DW_TAG_structure_type;
21115 default:
21116 gcc_unreachable ();
21120 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21121 include all of the information about the enumeration values also. Each
21122 enumerated type name/value is listed as a child of the enumerated type
21123 DIE. */
21125 static dw_die_ref
21126 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21128 dw_die_ref type_die = lookup_type_die (type);
21130 if (type_die == NULL)
21132 type_die = new_die (DW_TAG_enumeration_type,
21133 scope_die_for (type, context_die), type);
21134 equate_type_number_to_die (type, type_die);
21135 add_name_attribute (type_die, type_tag (type));
21136 if (dwarf_version >= 4 || !dwarf_strict)
21138 if (ENUM_IS_SCOPED (type))
21139 add_AT_flag (type_die, DW_AT_enum_class, 1);
21140 if (ENUM_IS_OPAQUE (type))
21141 add_AT_flag (type_die, DW_AT_declaration, 1);
21143 if (!dwarf_strict)
21144 add_AT_unsigned (type_die, DW_AT_encoding,
21145 TYPE_UNSIGNED (type)
21146 ? DW_ATE_unsigned
21147 : DW_ATE_signed);
21149 else if (! TYPE_SIZE (type))
21150 return type_die;
21151 else
21152 remove_AT (type_die, DW_AT_declaration);
21154 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21155 given enum type is incomplete, do not generate the DW_AT_byte_size
21156 attribute or the DW_AT_element_list attribute. */
21157 if (TYPE_SIZE (type))
21159 tree link;
21161 TREE_ASM_WRITTEN (type) = 1;
21162 add_byte_size_attribute (type_die, type);
21163 add_alignment_attribute (type_die, type);
21164 if (dwarf_version >= 3 || !dwarf_strict)
21166 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21167 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21168 context_die);
21170 if (TYPE_STUB_DECL (type) != NULL_TREE)
21172 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21173 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21176 /* If the first reference to this type was as the return type of an
21177 inline function, then it may not have a parent. Fix this now. */
21178 if (type_die->die_parent == NULL)
21179 add_child_die (scope_die_for (type, context_die), type_die);
21181 for (link = TYPE_VALUES (type);
21182 link != NULL; link = TREE_CHAIN (link))
21184 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21185 tree value = TREE_VALUE (link);
21187 add_name_attribute (enum_die,
21188 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21190 if (TREE_CODE (value) == CONST_DECL)
21191 value = DECL_INITIAL (value);
21193 if (simple_type_size_in_bits (TREE_TYPE (value))
21194 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21196 /* For constant forms created by add_AT_unsigned DWARF
21197 consumers (GDB, elfutils, etc.) always zero extend
21198 the value. Only when the actual value is negative
21199 do we need to use add_AT_int to generate a constant
21200 form that can represent negative values. */
21201 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21202 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21203 add_AT_unsigned (enum_die, DW_AT_const_value,
21204 (unsigned HOST_WIDE_INT) val);
21205 else
21206 add_AT_int (enum_die, DW_AT_const_value, val);
21208 else
21209 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21210 that here. TODO: This should be re-worked to use correct
21211 signed/unsigned double tags for all cases. */
21212 add_AT_wide (enum_die, DW_AT_const_value, value);
21215 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21216 if (TYPE_ARTIFICIAL (type))
21217 add_AT_flag (type_die, DW_AT_artificial, 1);
21219 else
21220 add_AT_flag (type_die, DW_AT_declaration, 1);
21222 add_alignment_attribute (type_die, type);
21224 add_pubtype (type, type_die);
21226 return type_die;
21229 /* Generate a DIE to represent either a real live formal parameter decl or to
21230 represent just the type of some formal parameter position in some function
21231 type.
21233 Note that this routine is a bit unusual because its argument may be a
21234 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21235 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21236 node. If it's the former then this function is being called to output a
21237 DIE to represent a formal parameter object (or some inlining thereof). If
21238 it's the latter, then this function is only being called to output a
21239 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21240 argument type of some subprogram type.
21241 If EMIT_NAME_P is true, name and source coordinate attributes
21242 are emitted. */
21244 static dw_die_ref
21245 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21246 dw_die_ref context_die)
21248 tree node_or_origin = node ? node : origin;
21249 tree ultimate_origin;
21250 dw_die_ref parm_die = NULL;
21252 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21254 parm_die = lookup_decl_die (node);
21256 /* If the contexts differ, we may not be talking about the same
21257 thing.
21258 ??? When in LTO the DIE parent is the "abstract" copy and the
21259 context_die is the specification "copy". But this whole block
21260 should eventually be no longer needed. */
21261 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21263 if (!DECL_ABSTRACT_P (node))
21265 /* This can happen when creating an inlined instance, in
21266 which case we need to create a new DIE that will get
21267 annotated with DW_AT_abstract_origin. */
21268 parm_die = NULL;
21270 else
21271 gcc_unreachable ();
21274 if (parm_die && parm_die->die_parent == NULL)
21276 /* Check that parm_die already has the right attributes that
21277 we would have added below. If any attributes are
21278 missing, fall through to add them. */
21279 if (! DECL_ABSTRACT_P (node_or_origin)
21280 && !get_AT (parm_die, DW_AT_location)
21281 && !get_AT (parm_die, DW_AT_const_value))
21282 /* We are missing location info, and are about to add it. */
21284 else
21286 add_child_die (context_die, parm_die);
21287 return parm_die;
21292 /* If we have a previously generated DIE, use it, unless this is an
21293 concrete instance (origin != NULL), in which case we need a new
21294 DIE with a corresponding DW_AT_abstract_origin. */
21295 bool reusing_die;
21296 if (parm_die && origin == NULL)
21297 reusing_die = true;
21298 else
21300 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21301 reusing_die = false;
21304 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21306 case tcc_declaration:
21307 ultimate_origin = decl_ultimate_origin (node_or_origin);
21308 if (node || ultimate_origin)
21309 origin = ultimate_origin;
21311 if (reusing_die)
21312 goto add_location;
21314 if (origin != NULL)
21315 add_abstract_origin_attribute (parm_die, origin);
21316 else if (emit_name_p)
21317 add_name_and_src_coords_attributes (parm_die, node);
21318 if (origin == NULL
21319 || (! DECL_ABSTRACT_P (node_or_origin)
21320 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21321 decl_function_context
21322 (node_or_origin))))
21324 tree type = TREE_TYPE (node_or_origin);
21325 if (decl_by_reference_p (node_or_origin))
21326 add_type_attribute (parm_die, TREE_TYPE (type),
21327 TYPE_UNQUALIFIED,
21328 false, context_die);
21329 else
21330 add_type_attribute (parm_die, type,
21331 decl_quals (node_or_origin),
21332 false, context_die);
21334 if (origin == NULL && DECL_ARTIFICIAL (node))
21335 add_AT_flag (parm_die, DW_AT_artificial, 1);
21336 add_location:
21337 if (node && node != origin)
21338 equate_decl_number_to_die (node, parm_die);
21339 if (! DECL_ABSTRACT_P (node_or_origin))
21340 add_location_or_const_value_attribute (parm_die, node_or_origin,
21341 node == NULL);
21343 break;
21345 case tcc_type:
21346 /* We were called with some kind of a ..._TYPE node. */
21347 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21348 context_die);
21349 break;
21351 default:
21352 gcc_unreachable ();
21355 return parm_die;
21358 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21359 children DW_TAG_formal_parameter DIEs representing the arguments of the
21360 parameter pack.
21362 PARM_PACK must be a function parameter pack.
21363 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21364 must point to the subsequent arguments of the function PACK_ARG belongs to.
21365 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21366 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21367 following the last one for which a DIE was generated. */
21369 static dw_die_ref
21370 gen_formal_parameter_pack_die (tree parm_pack,
21371 tree pack_arg,
21372 dw_die_ref subr_die,
21373 tree *next_arg)
21375 tree arg;
21376 dw_die_ref parm_pack_die;
21378 gcc_assert (parm_pack
21379 && lang_hooks.function_parameter_pack_p (parm_pack)
21380 && subr_die);
21382 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21383 add_src_coords_attributes (parm_pack_die, parm_pack);
21385 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21387 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21388 parm_pack))
21389 break;
21390 gen_formal_parameter_die (arg, NULL,
21391 false /* Don't emit name attribute. */,
21392 parm_pack_die);
21394 if (next_arg)
21395 *next_arg = arg;
21396 return parm_pack_die;
21399 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21400 at the end of an (ANSI prototyped) formal parameters list. */
21402 static void
21403 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21405 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21408 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21409 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21410 parameters as specified in some function type specification (except for
21411 those which appear as part of a function *definition*). */
21413 static void
21414 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21416 tree link;
21417 tree formal_type = NULL;
21418 tree first_parm_type;
21419 tree arg;
21421 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21423 arg = DECL_ARGUMENTS (function_or_method_type);
21424 function_or_method_type = TREE_TYPE (function_or_method_type);
21426 else
21427 arg = NULL_TREE;
21429 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21431 /* Make our first pass over the list of formal parameter types and output a
21432 DW_TAG_formal_parameter DIE for each one. */
21433 for (link = first_parm_type; link; )
21435 dw_die_ref parm_die;
21437 formal_type = TREE_VALUE (link);
21438 if (formal_type == void_type_node)
21439 break;
21441 /* Output a (nameless) DIE to represent the formal parameter itself. */
21442 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21444 parm_die = gen_formal_parameter_die (formal_type, NULL,
21445 true /* Emit name attribute. */,
21446 context_die);
21447 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21448 && link == first_parm_type)
21450 add_AT_flag (parm_die, DW_AT_artificial, 1);
21451 if (dwarf_version >= 3 || !dwarf_strict)
21452 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21454 else if (arg && DECL_ARTIFICIAL (arg))
21455 add_AT_flag (parm_die, DW_AT_artificial, 1);
21458 link = TREE_CHAIN (link);
21459 if (arg)
21460 arg = DECL_CHAIN (arg);
21463 /* If this function type has an ellipsis, add a
21464 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21465 if (formal_type != void_type_node)
21466 gen_unspecified_parameters_die (function_or_method_type, context_die);
21468 /* Make our second (and final) pass over the list of formal parameter types
21469 and output DIEs to represent those types (as necessary). */
21470 for (link = TYPE_ARG_TYPES (function_or_method_type);
21471 link && TREE_VALUE (link);
21472 link = TREE_CHAIN (link))
21473 gen_type_die (TREE_VALUE (link), context_die);
21476 /* We want to generate the DIE for TYPE so that we can generate the
21477 die for MEMBER, which has been defined; we will need to refer back
21478 to the member declaration nested within TYPE. If we're trying to
21479 generate minimal debug info for TYPE, processing TYPE won't do the
21480 trick; we need to attach the member declaration by hand. */
21482 static void
21483 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21485 gen_type_die (type, context_die);
21487 /* If we're trying to avoid duplicate debug info, we may not have
21488 emitted the member decl for this function. Emit it now. */
21489 if (TYPE_STUB_DECL (type)
21490 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21491 && ! lookup_decl_die (member))
21493 dw_die_ref type_die;
21494 gcc_assert (!decl_ultimate_origin (member));
21496 push_decl_scope (type);
21497 type_die = lookup_type_die_strip_naming_typedef (type);
21498 if (TREE_CODE (member) == FUNCTION_DECL)
21499 gen_subprogram_die (member, type_die);
21500 else if (TREE_CODE (member) == FIELD_DECL)
21502 /* Ignore the nameless fields that are used to skip bits but handle
21503 C++ anonymous unions and structs. */
21504 if (DECL_NAME (member) != NULL_TREE
21505 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21506 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21508 struct vlr_context vlr_ctx = {
21509 DECL_CONTEXT (member), /* struct_type */
21510 NULL_TREE /* variant_part_offset */
21512 gen_type_die (member_declared_type (member), type_die);
21513 gen_field_die (member, &vlr_ctx, type_die);
21516 else
21517 gen_variable_die (member, NULL_TREE, type_die);
21519 pop_decl_scope ();
21523 /* Forward declare these functions, because they are mutually recursive
21524 with their set_block_* pairing functions. */
21525 static void set_decl_origin_self (tree);
21527 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21528 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21529 that it points to the node itself, thus indicating that the node is its
21530 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21531 the given node is NULL, recursively descend the decl/block tree which
21532 it is the root of, and for each other ..._DECL or BLOCK node contained
21533 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21534 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21535 values to point to themselves. */
21537 static void
21538 set_block_origin_self (tree stmt)
21540 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21542 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21545 tree local_decl;
21547 for (local_decl = BLOCK_VARS (stmt);
21548 local_decl != NULL_TREE;
21549 local_decl = DECL_CHAIN (local_decl))
21550 /* Do not recurse on nested functions since the inlining status
21551 of parent and child can be different as per the DWARF spec. */
21552 if (TREE_CODE (local_decl) != FUNCTION_DECL
21553 && !DECL_EXTERNAL (local_decl))
21554 set_decl_origin_self (local_decl);
21558 tree subblock;
21560 for (subblock = BLOCK_SUBBLOCKS (stmt);
21561 subblock != NULL_TREE;
21562 subblock = BLOCK_CHAIN (subblock))
21563 set_block_origin_self (subblock); /* Recurse. */
21568 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21569 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21570 node to so that it points to the node itself, thus indicating that the
21571 node represents its own (abstract) origin. Additionally, if the
21572 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21573 the decl/block tree of which the given node is the root of, and for
21574 each other ..._DECL or BLOCK node contained therein whose
21575 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21576 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21577 point to themselves. */
21579 static void
21580 set_decl_origin_self (tree decl)
21582 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21584 DECL_ABSTRACT_ORIGIN (decl) = decl;
21585 if (TREE_CODE (decl) == FUNCTION_DECL)
21587 tree arg;
21589 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21590 DECL_ABSTRACT_ORIGIN (arg) = arg;
21591 if (DECL_INITIAL (decl) != NULL_TREE
21592 && DECL_INITIAL (decl) != error_mark_node)
21593 set_block_origin_self (DECL_INITIAL (decl));
21598 /* Mark the early DIE for DECL as the abstract instance. */
21600 static void
21601 dwarf2out_abstract_function (tree decl)
21603 dw_die_ref old_die;
21605 /* Make sure we have the actual abstract inline, not a clone. */
21606 decl = DECL_ORIGIN (decl);
21608 if (DECL_IGNORED_P (decl))
21609 return;
21611 old_die = lookup_decl_die (decl);
21612 /* With early debug we always have an old DIE unless we are in LTO
21613 and the user did not compile but only link with debug. */
21614 if (in_lto_p && ! old_die)
21615 return;
21616 gcc_assert (old_die != NULL);
21617 if (get_AT (old_die, DW_AT_inline)
21618 || get_AT (old_die, DW_AT_abstract_origin))
21619 /* We've already generated the abstract instance. */
21620 return;
21622 /* Go ahead and put DW_AT_inline on the DIE. */
21623 if (DECL_DECLARED_INLINE_P (decl))
21625 if (cgraph_function_possibly_inlined_p (decl))
21626 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21627 else
21628 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21630 else
21632 if (cgraph_function_possibly_inlined_p (decl))
21633 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21634 else
21635 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21638 if (DECL_DECLARED_INLINE_P (decl)
21639 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21640 add_AT_flag (old_die, DW_AT_artificial, 1);
21642 set_decl_origin_self (decl);
21645 /* Helper function of premark_used_types() which gets called through
21646 htab_traverse.
21648 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21649 marked as unused by prune_unused_types. */
21651 bool
21652 premark_used_types_helper (tree const &type, void *)
21654 dw_die_ref die;
21656 die = lookup_type_die (type);
21657 if (die != NULL)
21658 die->die_perennial_p = 1;
21659 return true;
21662 /* Helper function of premark_types_used_by_global_vars which gets called
21663 through htab_traverse.
21665 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21666 marked as unused by prune_unused_types. The DIE of the type is marked
21667 only if the global variable using the type will actually be emitted. */
21670 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21671 void *)
21673 struct types_used_by_vars_entry *entry;
21674 dw_die_ref die;
21676 entry = (struct types_used_by_vars_entry *) *slot;
21677 gcc_assert (entry->type != NULL
21678 && entry->var_decl != NULL);
21679 die = lookup_type_die (entry->type);
21680 if (die)
21682 /* Ask cgraph if the global variable really is to be emitted.
21683 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21684 varpool_node *node = varpool_node::get (entry->var_decl);
21685 if (node && node->definition)
21687 die->die_perennial_p = 1;
21688 /* Keep the parent DIEs as well. */
21689 while ((die = die->die_parent) && die->die_perennial_p == 0)
21690 die->die_perennial_p = 1;
21693 return 1;
21696 /* Mark all members of used_types_hash as perennial. */
21698 static void
21699 premark_used_types (struct function *fun)
21701 if (fun && fun->used_types_hash)
21702 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21705 /* Mark all members of types_used_by_vars_entry as perennial. */
21707 static void
21708 premark_types_used_by_global_vars (void)
21710 if (types_used_by_vars_hash)
21711 types_used_by_vars_hash
21712 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21715 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21716 for CA_LOC call arg loc node. */
21718 static dw_die_ref
21719 gen_call_site_die (tree decl, dw_die_ref subr_die,
21720 struct call_arg_loc_node *ca_loc)
21722 dw_die_ref stmt_die = NULL, die;
21723 tree block = ca_loc->block;
21725 while (block
21726 && block != DECL_INITIAL (decl)
21727 && TREE_CODE (block) == BLOCK)
21729 stmt_die = BLOCK_DIE (block);
21730 if (stmt_die)
21731 break;
21732 block = BLOCK_SUPERCONTEXT (block);
21734 if (stmt_die == NULL)
21735 stmt_die = subr_die;
21736 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21737 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21738 if (ca_loc->tail_call_p)
21739 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21740 if (ca_loc->symbol_ref)
21742 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21743 if (tdie)
21744 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21745 else
21746 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21747 false);
21749 return die;
21752 /* Generate a DIE to represent a declared function (either file-scope or
21753 block-local). */
21755 static void
21756 gen_subprogram_die (tree decl, dw_die_ref context_die)
21758 tree origin = decl_ultimate_origin (decl);
21759 dw_die_ref subr_die;
21760 dw_die_ref old_die = lookup_decl_die (decl);
21762 /* This function gets called multiple times for different stages of
21763 the debug process. For example, for func() in this code:
21765 namespace S
21767 void func() { ... }
21770 ...we get called 4 times. Twice in early debug and twice in
21771 late debug:
21773 Early debug
21774 -----------
21776 1. Once while generating func() within the namespace. This is
21777 the declaration. The declaration bit below is set, as the
21778 context is the namespace.
21780 A new DIE will be generated with DW_AT_declaration set.
21782 2. Once for func() itself. This is the specification. The
21783 declaration bit below is clear as the context is the CU.
21785 We will use the cached DIE from (1) to create a new DIE with
21786 DW_AT_specification pointing to the declaration in (1).
21788 Late debug via rest_of_handle_final()
21789 -------------------------------------
21791 3. Once generating func() within the namespace. This is also the
21792 declaration, as in (1), but this time we will early exit below
21793 as we have a cached DIE and a declaration needs no additional
21794 annotations (no locations), as the source declaration line
21795 info is enough.
21797 4. Once for func() itself. As in (2), this is the specification,
21798 but this time we will re-use the cached DIE, and just annotate
21799 it with the location information that should now be available.
21801 For something without namespaces, but with abstract instances, we
21802 are also called a multiple times:
21804 class Base
21806 public:
21807 Base (); // constructor declaration (1)
21810 Base::Base () { } // constructor specification (2)
21812 Early debug
21813 -----------
21815 1. Once for the Base() constructor by virtue of it being a
21816 member of the Base class. This is done via
21817 rest_of_type_compilation.
21819 This is a declaration, so a new DIE will be created with
21820 DW_AT_declaration.
21822 2. Once for the Base() constructor definition, but this time
21823 while generating the abstract instance of the base
21824 constructor (__base_ctor) which is being generated via early
21825 debug of reachable functions.
21827 Even though we have a cached version of the declaration (1),
21828 we will create a DW_AT_specification of the declaration DIE
21829 in (1).
21831 3. Once for the __base_ctor itself, but this time, we generate
21832 an DW_AT_abstract_origin version of the DW_AT_specification in
21833 (2).
21835 Late debug via rest_of_handle_final
21836 -----------------------------------
21838 4. One final time for the __base_ctor (which will have a cached
21839 DIE with DW_AT_abstract_origin created in (3). This time,
21840 we will just annotate the location information now
21841 available.
21843 int declaration = (current_function_decl != decl
21844 || class_or_namespace_scope_p (context_die));
21846 /* Now that the C++ front end lazily declares artificial member fns, we
21847 might need to retrofit the declaration into its class. */
21848 if (!declaration && !origin && !old_die
21849 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21850 && !class_or_namespace_scope_p (context_die)
21851 && debug_info_level > DINFO_LEVEL_TERSE)
21852 old_die = force_decl_die (decl);
21854 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21855 if (origin != NULL)
21857 gcc_assert (!declaration || local_scope_p (context_die));
21859 /* Fixup die_parent for the abstract instance of a nested
21860 inline function. */
21861 if (old_die && old_die->die_parent == NULL)
21862 add_child_die (context_die, old_die);
21864 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21866 /* If we have a DW_AT_abstract_origin we have a working
21867 cached version. */
21868 subr_die = old_die;
21870 else
21872 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21873 add_abstract_origin_attribute (subr_die, origin);
21874 /* This is where the actual code for a cloned function is.
21875 Let's emit linkage name attribute for it. This helps
21876 debuggers to e.g, set breakpoints into
21877 constructors/destructors when the user asks "break
21878 K::K". */
21879 add_linkage_name (subr_die, decl);
21882 /* A cached copy, possibly from early dwarf generation. Reuse as
21883 much as possible. */
21884 else if (old_die)
21886 /* A declaration that has been previously dumped needs no
21887 additional information. */
21888 if (declaration)
21889 return;
21891 if (!get_AT_flag (old_die, DW_AT_declaration)
21892 /* We can have a normal definition following an inline one in the
21893 case of redefinition of GNU C extern inlines.
21894 It seems reasonable to use AT_specification in this case. */
21895 && !get_AT (old_die, DW_AT_inline))
21897 /* Detect and ignore this case, where we are trying to output
21898 something we have already output. */
21899 if (get_AT (old_die, DW_AT_low_pc)
21900 || get_AT (old_die, DW_AT_ranges))
21901 return;
21903 /* If we have no location information, this must be a
21904 partially generated DIE from early dwarf generation.
21905 Fall through and generate it. */
21908 /* If the definition comes from the same place as the declaration,
21909 maybe use the old DIE. We always want the DIE for this function
21910 that has the *_pc attributes to be under comp_unit_die so the
21911 debugger can find it. We also need to do this for abstract
21912 instances of inlines, since the spec requires the out-of-line copy
21913 to have the same parent. For local class methods, this doesn't
21914 apply; we just use the old DIE. */
21915 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21916 struct dwarf_file_data * file_index = lookup_filename (s.file);
21917 if ((is_cu_die (old_die->die_parent)
21918 /* This condition fixes the inconsistency/ICE with the
21919 following Fortran test (or some derivative thereof) while
21920 building libgfortran:
21922 module some_m
21923 contains
21924 logical function funky (FLAG)
21925 funky = .true.
21926 end function
21927 end module
21929 || (old_die->die_parent
21930 && old_die->die_parent->die_tag == DW_TAG_module)
21931 || context_die == NULL)
21932 && (DECL_ARTIFICIAL (decl)
21933 /* The location attributes may be in the abstract origin
21934 which in the case of LTO might be not available to
21935 look at. */
21936 || get_AT (old_die, DW_AT_abstract_origin)
21937 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21938 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21939 == (unsigned) s.line)
21940 && (!debug_column_info
21941 || s.column == 0
21942 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21943 == (unsigned) s.column)))))
21945 subr_die = old_die;
21947 /* Clear out the declaration attribute, but leave the
21948 parameters so they can be augmented with location
21949 information later. Unless this was a declaration, in
21950 which case, wipe out the nameless parameters and recreate
21951 them further down. */
21952 if (remove_AT (subr_die, DW_AT_declaration))
21955 remove_AT (subr_die, DW_AT_object_pointer);
21956 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21959 /* Make a specification pointing to the previously built
21960 declaration. */
21961 else
21963 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21964 add_AT_specification (subr_die, old_die);
21965 add_pubname (decl, subr_die);
21966 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21967 add_AT_file (subr_die, DW_AT_decl_file, file_index);
21968 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21969 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
21970 if (debug_column_info
21971 && s.column
21972 && (get_AT_unsigned (old_die, DW_AT_decl_column)
21973 != (unsigned) s.column))
21974 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
21976 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
21977 emit the real type on the definition die. */
21978 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
21980 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
21981 if (die == auto_die || die == decltype_auto_die)
21982 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
21983 TYPE_UNQUALIFIED, false, context_die);
21986 /* When we process the method declaration, we haven't seen
21987 the out-of-class defaulted definition yet, so we have to
21988 recheck now. */
21989 if ((dwarf_version >= 5 || ! dwarf_strict)
21990 && !get_AT (subr_die, DW_AT_defaulted))
21992 int defaulted
21993 = lang_hooks.decls.decl_dwarf_attribute (decl,
21994 DW_AT_defaulted);
21995 if (defaulted != -1)
21997 /* Other values must have been handled before. */
21998 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
21999 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22004 /* Create a fresh DIE for anything else. */
22005 else
22007 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22009 if (TREE_PUBLIC (decl))
22010 add_AT_flag (subr_die, DW_AT_external, 1);
22012 add_name_and_src_coords_attributes (subr_die, decl);
22013 add_pubname (decl, subr_die);
22014 if (debug_info_level > DINFO_LEVEL_TERSE)
22016 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22017 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22018 TYPE_UNQUALIFIED, false, context_die);
22021 add_pure_or_virtual_attribute (subr_die, decl);
22022 if (DECL_ARTIFICIAL (decl))
22023 add_AT_flag (subr_die, DW_AT_artificial, 1);
22025 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22026 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22028 add_alignment_attribute (subr_die, decl);
22030 add_accessibility_attribute (subr_die, decl);
22033 /* Unless we have an existing non-declaration DIE, equate the new
22034 DIE. */
22035 if (!old_die || is_declaration_die (old_die))
22036 equate_decl_number_to_die (decl, subr_die);
22038 if (declaration)
22040 if (!old_die || !get_AT (old_die, DW_AT_inline))
22042 add_AT_flag (subr_die, DW_AT_declaration, 1);
22044 /* If this is an explicit function declaration then generate
22045 a DW_AT_explicit attribute. */
22046 if ((dwarf_version >= 3 || !dwarf_strict)
22047 && lang_hooks.decls.decl_dwarf_attribute (decl,
22048 DW_AT_explicit) == 1)
22049 add_AT_flag (subr_die, DW_AT_explicit, 1);
22051 /* If this is a C++11 deleted special function member then generate
22052 a DW_AT_deleted attribute. */
22053 if ((dwarf_version >= 5 || !dwarf_strict)
22054 && lang_hooks.decls.decl_dwarf_attribute (decl,
22055 DW_AT_deleted) == 1)
22056 add_AT_flag (subr_die, DW_AT_deleted, 1);
22058 /* If this is a C++11 defaulted special function member then
22059 generate a DW_AT_defaulted attribute. */
22060 if (dwarf_version >= 5 || !dwarf_strict)
22062 int defaulted
22063 = lang_hooks.decls.decl_dwarf_attribute (decl,
22064 DW_AT_defaulted);
22065 if (defaulted != -1)
22066 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22069 /* If this is a C++11 non-static member function with & ref-qualifier
22070 then generate a DW_AT_reference attribute. */
22071 if ((dwarf_version >= 5 || !dwarf_strict)
22072 && lang_hooks.decls.decl_dwarf_attribute (decl,
22073 DW_AT_reference) == 1)
22074 add_AT_flag (subr_die, DW_AT_reference, 1);
22076 /* If this is a C++11 non-static member function with &&
22077 ref-qualifier then generate a DW_AT_reference attribute. */
22078 if ((dwarf_version >= 5 || !dwarf_strict)
22079 && lang_hooks.decls.decl_dwarf_attribute (decl,
22080 DW_AT_rvalue_reference)
22081 == 1)
22082 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22085 /* Tag abstract instances with DW_AT_inline. */
22086 else if (DECL_ABSTRACT_P (decl))
22088 if (DECL_DECLARED_INLINE_P (decl))
22090 if (cgraph_function_possibly_inlined_p (decl))
22091 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
22092 else
22093 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
22095 else
22097 if (cgraph_function_possibly_inlined_p (decl))
22098 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
22099 else
22100 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
22103 if (DECL_DECLARED_INLINE_P (decl)
22104 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22105 add_AT_flag (subr_die, DW_AT_artificial, 1);
22107 /* For non DECL_EXTERNALs, if range information is available, fill
22108 the DIE with it. */
22109 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22111 HOST_WIDE_INT cfa_fb_offset;
22113 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22115 if (!crtl->has_bb_partition)
22117 dw_fde_ref fde = fun->fde;
22118 if (fde->dw_fde_begin)
22120 /* We have already generated the labels. */
22121 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22122 fde->dw_fde_end, false);
22124 else
22126 /* Create start/end labels and add the range. */
22127 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22128 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22129 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22130 current_function_funcdef_no);
22131 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22132 current_function_funcdef_no);
22133 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22134 false);
22137 #if VMS_DEBUGGING_INFO
22138 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22139 Section 2.3 Prologue and Epilogue Attributes:
22140 When a breakpoint is set on entry to a function, it is generally
22141 desirable for execution to be suspended, not on the very first
22142 instruction of the function, but rather at a point after the
22143 function's frame has been set up, after any language defined local
22144 declaration processing has been completed, and before execution of
22145 the first statement of the function begins. Debuggers generally
22146 cannot properly determine where this point is. Similarly for a
22147 breakpoint set on exit from a function. The prologue and epilogue
22148 attributes allow a compiler to communicate the location(s) to use. */
22151 if (fde->dw_fde_vms_end_prologue)
22152 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22153 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22155 if (fde->dw_fde_vms_begin_epilogue)
22156 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22157 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22159 #endif
22162 else
22164 /* Generate pubnames entries for the split function code ranges. */
22165 dw_fde_ref fde = fun->fde;
22167 if (fde->dw_fde_second_begin)
22169 if (dwarf_version >= 3 || !dwarf_strict)
22171 /* We should use ranges for non-contiguous code section
22172 addresses. Use the actual code range for the initial
22173 section, since the HOT/COLD labels might precede an
22174 alignment offset. */
22175 bool range_list_added = false;
22176 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22177 fde->dw_fde_end, &range_list_added,
22178 false);
22179 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22180 fde->dw_fde_second_end,
22181 &range_list_added, false);
22182 if (range_list_added)
22183 add_ranges (NULL);
22185 else
22187 /* There is no real support in DW2 for this .. so we make
22188 a work-around. First, emit the pub name for the segment
22189 containing the function label. Then make and emit a
22190 simplified subprogram DIE for the second segment with the
22191 name pre-fixed by __hot/cold_sect_of_. We use the same
22192 linkage name for the second die so that gdb will find both
22193 sections when given "b foo". */
22194 const char *name = NULL;
22195 tree decl_name = DECL_NAME (decl);
22196 dw_die_ref seg_die;
22198 /* Do the 'primary' section. */
22199 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22200 fde->dw_fde_end, false);
22202 /* Build a minimal DIE for the secondary section. */
22203 seg_die = new_die (DW_TAG_subprogram,
22204 subr_die->die_parent, decl);
22206 if (TREE_PUBLIC (decl))
22207 add_AT_flag (seg_die, DW_AT_external, 1);
22209 if (decl_name != NULL
22210 && IDENTIFIER_POINTER (decl_name) != NULL)
22212 name = dwarf2_name (decl, 1);
22213 if (! DECL_ARTIFICIAL (decl))
22214 add_src_coords_attributes (seg_die, decl);
22216 add_linkage_name (seg_die, decl);
22218 gcc_assert (name != NULL);
22219 add_pure_or_virtual_attribute (seg_die, decl);
22220 if (DECL_ARTIFICIAL (decl))
22221 add_AT_flag (seg_die, DW_AT_artificial, 1);
22223 name = concat ("__second_sect_of_", name, NULL);
22224 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22225 fde->dw_fde_second_end, false);
22226 add_name_attribute (seg_die, name);
22227 if (want_pubnames ())
22228 add_pubname_string (name, seg_die);
22231 else
22232 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22233 false);
22236 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22238 /* We define the "frame base" as the function's CFA. This is more
22239 convenient for several reasons: (1) It's stable across the prologue
22240 and epilogue, which makes it better than just a frame pointer,
22241 (2) With dwarf3, there exists a one-byte encoding that allows us
22242 to reference the .debug_frame data by proxy, but failing that,
22243 (3) We can at least reuse the code inspection and interpretation
22244 code that determines the CFA position at various points in the
22245 function. */
22246 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22248 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22249 add_AT_loc (subr_die, DW_AT_frame_base, op);
22251 else
22253 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22254 if (list->dw_loc_next)
22255 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22256 else
22257 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22260 /* Compute a displacement from the "steady-state frame pointer" to
22261 the CFA. The former is what all stack slots and argument slots
22262 will reference in the rtl; the latter is what we've told the
22263 debugger about. We'll need to adjust all frame_base references
22264 by this displacement. */
22265 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22267 if (fun->static_chain_decl)
22269 /* DWARF requires here a location expression that computes the
22270 address of the enclosing subprogram's frame base. The machinery
22271 in tree-nested.c is supposed to store this specific address in the
22272 last field of the FRAME record. */
22273 const tree frame_type
22274 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22275 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22277 tree fb_expr
22278 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22279 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22280 fb_expr, fb_decl, NULL_TREE);
22282 add_AT_location_description (subr_die, DW_AT_static_link,
22283 loc_list_from_tree (fb_expr, 0, NULL));
22286 resolve_variable_values ();
22289 /* Generate child dies for template paramaters. */
22290 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22291 gen_generic_params_dies (decl);
22293 /* Now output descriptions of the arguments for this function. This gets
22294 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22295 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22296 `...' at the end of the formal parameter list. In order to find out if
22297 there was a trailing ellipsis or not, we must instead look at the type
22298 associated with the FUNCTION_DECL. This will be a node of type
22299 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22300 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22301 an ellipsis at the end. */
22303 /* In the case where we are describing a mere function declaration, all we
22304 need to do here (and all we *can* do here) is to describe the *types* of
22305 its formal parameters. */
22306 if (debug_info_level <= DINFO_LEVEL_TERSE)
22308 else if (declaration)
22309 gen_formal_types_die (decl, subr_die);
22310 else
22312 /* Generate DIEs to represent all known formal parameters. */
22313 tree parm = DECL_ARGUMENTS (decl);
22314 tree generic_decl = early_dwarf
22315 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22316 tree generic_decl_parm = generic_decl
22317 ? DECL_ARGUMENTS (generic_decl)
22318 : NULL;
22320 /* Now we want to walk the list of parameters of the function and
22321 emit their relevant DIEs.
22323 We consider the case of DECL being an instance of a generic function
22324 as well as it being a normal function.
22326 If DECL is an instance of a generic function we walk the
22327 parameters of the generic function declaration _and_ the parameters of
22328 DECL itself. This is useful because we want to emit specific DIEs for
22329 function parameter packs and those are declared as part of the
22330 generic function declaration. In that particular case,
22331 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22332 That DIE has children DIEs representing the set of arguments
22333 of the pack. Note that the set of pack arguments can be empty.
22334 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22335 children DIE.
22337 Otherwise, we just consider the parameters of DECL. */
22338 while (generic_decl_parm || parm)
22340 if (generic_decl_parm
22341 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22342 gen_formal_parameter_pack_die (generic_decl_parm,
22343 parm, subr_die,
22344 &parm);
22345 else if (parm && !POINTER_BOUNDS_P (parm))
22347 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22349 if (parm == DECL_ARGUMENTS (decl)
22350 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22351 && parm_die
22352 && (dwarf_version >= 3 || !dwarf_strict))
22353 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22355 parm = DECL_CHAIN (parm);
22357 else if (parm)
22358 parm = DECL_CHAIN (parm);
22360 if (generic_decl_parm)
22361 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22364 /* Decide whether we need an unspecified_parameters DIE at the end.
22365 There are 2 more cases to do this for: 1) the ansi ... declaration -
22366 this is detectable when the end of the arg list is not a
22367 void_type_node 2) an unprototyped function declaration (not a
22368 definition). This just means that we have no info about the
22369 parameters at all. */
22370 if (early_dwarf)
22372 if (prototype_p (TREE_TYPE (decl)))
22374 /* This is the prototyped case, check for.... */
22375 if (stdarg_p (TREE_TYPE (decl)))
22376 gen_unspecified_parameters_die (decl, subr_die);
22378 else if (DECL_INITIAL (decl) == NULL_TREE)
22379 gen_unspecified_parameters_die (decl, subr_die);
22383 if (subr_die != old_die)
22384 /* Add the calling convention attribute if requested. */
22385 add_calling_convention_attribute (subr_die, decl);
22387 /* Output Dwarf info for all of the stuff within the body of the function
22388 (if it has one - it may be just a declaration).
22390 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22391 a function. This BLOCK actually represents the outermost binding contour
22392 for the function, i.e. the contour in which the function's formal
22393 parameters and labels get declared. Curiously, it appears that the front
22394 end doesn't actually put the PARM_DECL nodes for the current function onto
22395 the BLOCK_VARS list for this outer scope, but are strung off of the
22396 DECL_ARGUMENTS list for the function instead.
22398 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22399 the LABEL_DECL nodes for the function however, and we output DWARF info
22400 for those in decls_for_scope. Just within the `outer_scope' there will be
22401 a BLOCK node representing the function's outermost pair of curly braces,
22402 and any blocks used for the base and member initializers of a C++
22403 constructor function. */
22404 tree outer_scope = DECL_INITIAL (decl);
22405 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22407 int call_site_note_count = 0;
22408 int tail_call_site_note_count = 0;
22410 /* Emit a DW_TAG_variable DIE for a named return value. */
22411 if (DECL_NAME (DECL_RESULT (decl)))
22412 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22414 /* The first time through decls_for_scope we will generate the
22415 DIEs for the locals. The second time, we fill in the
22416 location info. */
22417 decls_for_scope (outer_scope, subr_die);
22419 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22421 struct call_arg_loc_node *ca_loc;
22422 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22424 dw_die_ref die = NULL;
22425 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22426 rtx arg, next_arg;
22428 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22429 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22430 : NULL_RTX);
22431 arg; arg = next_arg)
22433 dw_loc_descr_ref reg, val;
22434 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22435 dw_die_ref cdie, tdie = NULL;
22437 next_arg = XEXP (arg, 1);
22438 if (REG_P (XEXP (XEXP (arg, 0), 0))
22439 && next_arg
22440 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22441 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22442 && REGNO (XEXP (XEXP (arg, 0), 0))
22443 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22444 next_arg = XEXP (next_arg, 1);
22445 if (mode == VOIDmode)
22447 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22448 if (mode == VOIDmode)
22449 mode = GET_MODE (XEXP (arg, 0));
22451 if (mode == VOIDmode || mode == BLKmode)
22452 continue;
22453 /* Get dynamic information about call target only if we
22454 have no static information: we cannot generate both
22455 DW_AT_call_origin and DW_AT_call_target
22456 attributes. */
22457 if (ca_loc->symbol_ref == NULL_RTX)
22459 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22461 tloc = XEXP (XEXP (arg, 0), 1);
22462 continue;
22464 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22465 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22467 tlocc = XEXP (XEXP (arg, 0), 1);
22468 continue;
22471 reg = NULL;
22472 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22473 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22474 VAR_INIT_STATUS_INITIALIZED);
22475 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22477 rtx mem = XEXP (XEXP (arg, 0), 0);
22478 reg = mem_loc_descriptor (XEXP (mem, 0),
22479 get_address_mode (mem),
22480 GET_MODE (mem),
22481 VAR_INIT_STATUS_INITIALIZED);
22483 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22484 == DEBUG_PARAMETER_REF)
22486 tree tdecl
22487 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22488 tdie = lookup_decl_die (tdecl);
22489 if (tdie == NULL)
22490 continue;
22492 else
22493 continue;
22494 if (reg == NULL
22495 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22496 != DEBUG_PARAMETER_REF)
22497 continue;
22498 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22499 VOIDmode,
22500 VAR_INIT_STATUS_INITIALIZED);
22501 if (val == NULL)
22502 continue;
22503 if (die == NULL)
22504 die = gen_call_site_die (decl, subr_die, ca_loc);
22505 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22506 NULL_TREE);
22507 if (reg != NULL)
22508 add_AT_loc (cdie, DW_AT_location, reg);
22509 else if (tdie != NULL)
22510 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22511 tdie);
22512 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22513 if (next_arg != XEXP (arg, 1))
22515 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22516 if (mode == VOIDmode)
22517 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22518 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22519 0), 1),
22520 mode, VOIDmode,
22521 VAR_INIT_STATUS_INITIALIZED);
22522 if (val != NULL)
22523 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22524 val);
22527 if (die == NULL
22528 && (ca_loc->symbol_ref || tloc))
22529 die = gen_call_site_die (decl, subr_die, ca_loc);
22530 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22532 dw_loc_descr_ref tval = NULL;
22534 if (tloc != NULL_RTX)
22535 tval = mem_loc_descriptor (tloc,
22536 GET_MODE (tloc) == VOIDmode
22537 ? Pmode : GET_MODE (tloc),
22538 VOIDmode,
22539 VAR_INIT_STATUS_INITIALIZED);
22540 if (tval)
22541 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22542 else if (tlocc != NULL_RTX)
22544 tval = mem_loc_descriptor (tlocc,
22545 GET_MODE (tlocc) == VOIDmode
22546 ? Pmode : GET_MODE (tlocc),
22547 VOIDmode,
22548 VAR_INIT_STATUS_INITIALIZED);
22549 if (tval)
22550 add_AT_loc (die,
22551 dwarf_AT (DW_AT_call_target_clobbered),
22552 tval);
22555 if (die != NULL)
22557 call_site_note_count++;
22558 if (ca_loc->tail_call_p)
22559 tail_call_site_note_count++;
22563 call_arg_locations = NULL;
22564 call_arg_loc_last = NULL;
22565 if (tail_call_site_count >= 0
22566 && tail_call_site_count == tail_call_site_note_count
22567 && (!dwarf_strict || dwarf_version >= 5))
22569 if (call_site_count >= 0
22570 && call_site_count == call_site_note_count)
22571 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22572 else
22573 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22575 call_site_count = -1;
22576 tail_call_site_count = -1;
22579 /* Mark used types after we have created DIEs for the functions scopes. */
22580 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22583 /* Returns a hash value for X (which really is a die_struct). */
22585 hashval_t
22586 block_die_hasher::hash (die_struct *d)
22588 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22591 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22592 as decl_id and die_parent of die_struct Y. */
22594 bool
22595 block_die_hasher::equal (die_struct *x, die_struct *y)
22597 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22600 /* Return TRUE if DECL, which may have been previously generated as
22601 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22602 true if decl (or its origin) is either an extern declaration or a
22603 class/namespace scoped declaration.
22605 The declare_in_namespace support causes us to get two DIEs for one
22606 variable, both of which are declarations. We want to avoid
22607 considering one to be a specification, so we must test for
22608 DECLARATION and DW_AT_declaration. */
22609 static inline bool
22610 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22612 return (old_die && TREE_STATIC (decl) && !declaration
22613 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22616 /* Return true if DECL is a local static. */
22618 static inline bool
22619 local_function_static (tree decl)
22621 gcc_assert (VAR_P (decl));
22622 return TREE_STATIC (decl)
22623 && DECL_CONTEXT (decl)
22624 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22627 /* Generate a DIE to represent a declared data object.
22628 Either DECL or ORIGIN must be non-null. */
22630 static void
22631 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22633 HOST_WIDE_INT off = 0;
22634 tree com_decl;
22635 tree decl_or_origin = decl ? decl : origin;
22636 tree ultimate_origin;
22637 dw_die_ref var_die;
22638 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22639 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22640 || class_or_namespace_scope_p (context_die));
22641 bool specialization_p = false;
22642 bool no_linkage_name = false;
22644 /* While C++ inline static data members have definitions inside of the
22645 class, force the first DIE to be a declaration, then let gen_member_die
22646 reparent it to the class context and call gen_variable_die again
22647 to create the outside of the class DIE for the definition. */
22648 if (!declaration
22649 && old_die == NULL
22650 && decl
22651 && DECL_CONTEXT (decl)
22652 && TYPE_P (DECL_CONTEXT (decl))
22653 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22655 declaration = true;
22656 if (dwarf_version < 5)
22657 no_linkage_name = true;
22660 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22661 if (decl || ultimate_origin)
22662 origin = ultimate_origin;
22663 com_decl = fortran_common (decl_or_origin, &off);
22665 /* Symbol in common gets emitted as a child of the common block, in the form
22666 of a data member. */
22667 if (com_decl)
22669 dw_die_ref com_die;
22670 dw_loc_list_ref loc = NULL;
22671 die_node com_die_arg;
22673 var_die = lookup_decl_die (decl_or_origin);
22674 if (var_die)
22676 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22678 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22679 if (loc)
22681 if (off)
22683 /* Optimize the common case. */
22684 if (single_element_loc_list_p (loc)
22685 && loc->expr->dw_loc_opc == DW_OP_addr
22686 && loc->expr->dw_loc_next == NULL
22687 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22688 == SYMBOL_REF)
22690 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22691 loc->expr->dw_loc_oprnd1.v.val_addr
22692 = plus_constant (GET_MODE (x), x , off);
22694 else
22695 loc_list_plus_const (loc, off);
22697 add_AT_location_description (var_die, DW_AT_location, loc);
22698 remove_AT (var_die, DW_AT_declaration);
22701 return;
22704 if (common_block_die_table == NULL)
22705 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22707 com_die_arg.decl_id = DECL_UID (com_decl);
22708 com_die_arg.die_parent = context_die;
22709 com_die = common_block_die_table->find (&com_die_arg);
22710 if (! early_dwarf)
22711 loc = loc_list_from_tree (com_decl, 2, NULL);
22712 if (com_die == NULL)
22714 const char *cnam
22715 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22716 die_node **slot;
22718 com_die = new_die (DW_TAG_common_block, context_die, decl);
22719 add_name_and_src_coords_attributes (com_die, com_decl);
22720 if (loc)
22722 add_AT_location_description (com_die, DW_AT_location, loc);
22723 /* Avoid sharing the same loc descriptor between
22724 DW_TAG_common_block and DW_TAG_variable. */
22725 loc = loc_list_from_tree (com_decl, 2, NULL);
22727 else if (DECL_EXTERNAL (decl_or_origin))
22728 add_AT_flag (com_die, DW_AT_declaration, 1);
22729 if (want_pubnames ())
22730 add_pubname_string (cnam, com_die); /* ??? needed? */
22731 com_die->decl_id = DECL_UID (com_decl);
22732 slot = common_block_die_table->find_slot (com_die, INSERT);
22733 *slot = com_die;
22735 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22737 add_AT_location_description (com_die, DW_AT_location, loc);
22738 loc = loc_list_from_tree (com_decl, 2, NULL);
22739 remove_AT (com_die, DW_AT_declaration);
22741 var_die = new_die (DW_TAG_variable, com_die, decl);
22742 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22743 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22744 decl_quals (decl_or_origin), false,
22745 context_die);
22746 add_alignment_attribute (var_die, decl);
22747 add_AT_flag (var_die, DW_AT_external, 1);
22748 if (loc)
22750 if (off)
22752 /* Optimize the common case. */
22753 if (single_element_loc_list_p (loc)
22754 && loc->expr->dw_loc_opc == DW_OP_addr
22755 && loc->expr->dw_loc_next == NULL
22756 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22758 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22759 loc->expr->dw_loc_oprnd1.v.val_addr
22760 = plus_constant (GET_MODE (x), x, off);
22762 else
22763 loc_list_plus_const (loc, off);
22765 add_AT_location_description (var_die, DW_AT_location, loc);
22767 else if (DECL_EXTERNAL (decl_or_origin))
22768 add_AT_flag (var_die, DW_AT_declaration, 1);
22769 if (decl)
22770 equate_decl_number_to_die (decl, var_die);
22771 return;
22774 if (old_die)
22776 if (declaration)
22778 /* A declaration that has been previously dumped, needs no
22779 further annotations, since it doesn't need location on
22780 the second pass. */
22781 return;
22783 else if (decl_will_get_specification_p (old_die, decl, declaration)
22784 && !get_AT (old_die, DW_AT_specification))
22786 /* Fall-thru so we can make a new variable die along with a
22787 DW_AT_specification. */
22789 else if (origin && old_die->die_parent != context_die)
22791 /* If we will be creating an inlined instance, we need a
22792 new DIE that will get annotated with
22793 DW_AT_abstract_origin. Clear things so we can get a
22794 new DIE. */
22795 gcc_assert (!DECL_ABSTRACT_P (decl));
22796 old_die = NULL;
22798 else
22800 /* If a DIE was dumped early, it still needs location info.
22801 Skip to where we fill the location bits. */
22802 var_die = old_die;
22804 /* ??? In LTRANS we cannot annotate early created variably
22805 modified type DIEs without copying them and adjusting all
22806 references to them. Thus we dumped them again, also add a
22807 reference to them. */
22808 tree type = TREE_TYPE (decl_or_origin);
22809 if (in_lto_p
22810 && variably_modified_type_p
22811 (type, decl_function_context (decl_or_origin)))
22813 if (decl_by_reference_p (decl_or_origin))
22814 add_type_attribute (var_die, TREE_TYPE (type),
22815 TYPE_UNQUALIFIED, false, context_die);
22816 else
22817 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22818 false, context_die);
22821 goto gen_variable_die_location;
22825 /* For static data members, the declaration in the class is supposed
22826 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22827 also in DWARF2; the specification should still be DW_TAG_variable
22828 referencing the DW_TAG_member DIE. */
22829 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22830 var_die = new_die (DW_TAG_member, context_die, decl);
22831 else
22832 var_die = new_die (DW_TAG_variable, context_die, decl);
22834 if (origin != NULL)
22835 add_abstract_origin_attribute (var_die, origin);
22837 /* Loop unrolling can create multiple blocks that refer to the same
22838 static variable, so we must test for the DW_AT_declaration flag.
22840 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22841 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22842 sharing them.
22844 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22845 else if (decl_will_get_specification_p (old_die, decl, declaration))
22847 /* This is a definition of a C++ class level static. */
22848 add_AT_specification (var_die, old_die);
22849 specialization_p = true;
22850 if (DECL_NAME (decl))
22852 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22853 struct dwarf_file_data * file_index = lookup_filename (s.file);
22855 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22856 add_AT_file (var_die, DW_AT_decl_file, file_index);
22858 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22859 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22861 if (debug_column_info
22862 && s.column
22863 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22864 != (unsigned) s.column))
22865 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22867 if (old_die->die_tag == DW_TAG_member)
22868 add_linkage_name (var_die, decl);
22871 else
22872 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22874 if ((origin == NULL && !specialization_p)
22875 || (origin != NULL
22876 && !DECL_ABSTRACT_P (decl_or_origin)
22877 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22878 decl_function_context
22879 (decl_or_origin))))
22881 tree type = TREE_TYPE (decl_or_origin);
22883 if (decl_by_reference_p (decl_or_origin))
22884 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22885 context_die);
22886 else
22887 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22888 context_die);
22891 if (origin == NULL && !specialization_p)
22893 if (TREE_PUBLIC (decl))
22894 add_AT_flag (var_die, DW_AT_external, 1);
22896 if (DECL_ARTIFICIAL (decl))
22897 add_AT_flag (var_die, DW_AT_artificial, 1);
22899 add_alignment_attribute (var_die, decl);
22901 add_accessibility_attribute (var_die, decl);
22904 if (declaration)
22905 add_AT_flag (var_die, DW_AT_declaration, 1);
22907 if (decl && (DECL_ABSTRACT_P (decl)
22908 || !old_die || is_declaration_die (old_die)))
22909 equate_decl_number_to_die (decl, var_die);
22911 gen_variable_die_location:
22912 if (! declaration
22913 && (! DECL_ABSTRACT_P (decl_or_origin)
22914 /* Local static vars are shared between all clones/inlines,
22915 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22916 already set. */
22917 || (VAR_P (decl_or_origin)
22918 && TREE_STATIC (decl_or_origin)
22919 && DECL_RTL_SET_P (decl_or_origin))))
22921 if (early_dwarf)
22922 add_pubname (decl_or_origin, var_die);
22923 else
22924 add_location_or_const_value_attribute (var_die, decl_or_origin,
22925 decl == NULL);
22927 else
22928 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22930 if ((dwarf_version >= 4 || !dwarf_strict)
22931 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22932 DW_AT_const_expr) == 1
22933 && !get_AT (var_die, DW_AT_const_expr)
22934 && !specialization_p)
22935 add_AT_flag (var_die, DW_AT_const_expr, 1);
22937 if (!dwarf_strict)
22939 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22940 DW_AT_inline);
22941 if (inl != -1
22942 && !get_AT (var_die, DW_AT_inline)
22943 && !specialization_p)
22944 add_AT_unsigned (var_die, DW_AT_inline, inl);
22948 /* Generate a DIE to represent a named constant. */
22950 static void
22951 gen_const_die (tree decl, dw_die_ref context_die)
22953 dw_die_ref const_die;
22954 tree type = TREE_TYPE (decl);
22956 const_die = lookup_decl_die (decl);
22957 if (const_die)
22958 return;
22960 const_die = new_die (DW_TAG_constant, context_die, decl);
22961 equate_decl_number_to_die (decl, const_die);
22962 add_name_and_src_coords_attributes (const_die, decl);
22963 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22964 if (TREE_PUBLIC (decl))
22965 add_AT_flag (const_die, DW_AT_external, 1);
22966 if (DECL_ARTIFICIAL (decl))
22967 add_AT_flag (const_die, DW_AT_artificial, 1);
22968 tree_add_const_value_attribute_for_decl (const_die, decl);
22971 /* Generate a DIE to represent a label identifier. */
22973 static void
22974 gen_label_die (tree decl, dw_die_ref context_die)
22976 tree origin = decl_ultimate_origin (decl);
22977 dw_die_ref lbl_die = lookup_decl_die (decl);
22978 rtx insn;
22979 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22981 if (!lbl_die)
22983 lbl_die = new_die (DW_TAG_label, context_die, decl);
22984 equate_decl_number_to_die (decl, lbl_die);
22986 if (origin != NULL)
22987 add_abstract_origin_attribute (lbl_die, origin);
22988 else
22989 add_name_and_src_coords_attributes (lbl_die, decl);
22992 if (DECL_ABSTRACT_P (decl))
22993 equate_decl_number_to_die (decl, lbl_die);
22994 else if (! early_dwarf)
22996 insn = DECL_RTL_IF_SET (decl);
22998 /* Deleted labels are programmer specified labels which have been
22999 eliminated because of various optimizations. We still emit them
23000 here so that it is possible to put breakpoints on them. */
23001 if (insn
23002 && (LABEL_P (insn)
23003 || ((NOTE_P (insn)
23004 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23006 /* When optimization is enabled (via -O) some parts of the compiler
23007 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23008 represent source-level labels which were explicitly declared by
23009 the user. This really shouldn't be happening though, so catch
23010 it if it ever does happen. */
23011 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23013 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23014 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23016 else if (insn
23017 && NOTE_P (insn)
23018 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23019 && CODE_LABEL_NUMBER (insn) != -1)
23021 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23022 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23027 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23028 attributes to the DIE for a block STMT, to describe where the inlined
23029 function was called from. This is similar to add_src_coords_attributes. */
23031 static inline void
23032 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23034 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23036 if (dwarf_version >= 3 || !dwarf_strict)
23038 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23039 add_AT_unsigned (die, DW_AT_call_line, s.line);
23040 if (debug_column_info && s.column)
23041 add_AT_unsigned (die, DW_AT_call_column, s.column);
23046 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23047 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23049 static inline void
23050 add_high_low_attributes (tree stmt, dw_die_ref die)
23052 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23054 if (BLOCK_FRAGMENT_CHAIN (stmt)
23055 && (dwarf_version >= 3 || !dwarf_strict))
23057 tree chain, superblock = NULL_TREE;
23058 dw_die_ref pdie;
23059 dw_attr_node *attr = NULL;
23061 if (inlined_function_outer_scope_p (stmt))
23063 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23064 BLOCK_NUMBER (stmt));
23065 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23068 /* Optimize duplicate .debug_ranges lists or even tails of
23069 lists. If this BLOCK has same ranges as its supercontext,
23070 lookup DW_AT_ranges attribute in the supercontext (and
23071 recursively so), verify that the ranges_table contains the
23072 right values and use it instead of adding a new .debug_range. */
23073 for (chain = stmt, pdie = die;
23074 BLOCK_SAME_RANGE (chain);
23075 chain = BLOCK_SUPERCONTEXT (chain))
23077 dw_attr_node *new_attr;
23079 pdie = pdie->die_parent;
23080 if (pdie == NULL)
23081 break;
23082 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23083 break;
23084 new_attr = get_AT (pdie, DW_AT_ranges);
23085 if (new_attr == NULL
23086 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23087 break;
23088 attr = new_attr;
23089 superblock = BLOCK_SUPERCONTEXT (chain);
23091 if (attr != NULL
23092 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23093 == BLOCK_NUMBER (superblock))
23094 && BLOCK_FRAGMENT_CHAIN (superblock))
23096 unsigned long off = attr->dw_attr_val.v.val_offset;
23097 unsigned long supercnt = 0, thiscnt = 0;
23098 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23099 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23101 ++supercnt;
23102 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23103 == BLOCK_NUMBER (chain));
23105 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23106 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23107 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23108 ++thiscnt;
23109 gcc_assert (supercnt >= thiscnt);
23110 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23111 false);
23112 note_rnglist_head (off + supercnt - thiscnt);
23113 return;
23116 unsigned int offset = add_ranges (stmt, true);
23117 add_AT_range_list (die, DW_AT_ranges, offset, false);
23118 note_rnglist_head (offset);
23120 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23121 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23124 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23125 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23126 chain = BLOCK_FRAGMENT_CHAIN (chain);
23128 while (chain);
23129 add_ranges (NULL);
23131 else
23133 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23134 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23135 BLOCK_NUMBER (stmt));
23136 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23137 BLOCK_NUMBER (stmt));
23138 add_AT_low_high_pc (die, label, label_high, false);
23142 /* Generate a DIE for a lexical block. */
23144 static void
23145 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23147 dw_die_ref old_die = BLOCK_DIE (stmt);
23148 dw_die_ref stmt_die = NULL;
23149 if (!old_die)
23151 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23152 BLOCK_DIE (stmt) = stmt_die;
23155 if (BLOCK_ABSTRACT (stmt))
23157 if (old_die)
23159 /* This must have been generated early and it won't even
23160 need location information since it's a DW_AT_inline
23161 function. */
23162 if (flag_checking)
23163 for (dw_die_ref c = context_die; c; c = c->die_parent)
23164 if (c->die_tag == DW_TAG_inlined_subroutine
23165 || c->die_tag == DW_TAG_subprogram)
23167 gcc_assert (get_AT (c, DW_AT_inline));
23168 break;
23170 return;
23173 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23175 /* If this is an inlined instance, create a new lexical die for
23176 anything below to attach DW_AT_abstract_origin to. */
23177 if (old_die)
23179 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23180 BLOCK_DIE (stmt) = stmt_die;
23181 old_die = NULL;
23184 tree origin = block_ultimate_origin (stmt);
23185 if (origin != NULL_TREE && origin != stmt)
23186 add_abstract_origin_attribute (stmt_die, origin);
23189 if (old_die)
23190 stmt_die = old_die;
23192 /* A non abstract block whose blocks have already been reordered
23193 should have the instruction range for this block. If so, set the
23194 high/low attributes. */
23195 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23197 gcc_assert (stmt_die);
23198 add_high_low_attributes (stmt, stmt_die);
23201 decls_for_scope (stmt, stmt_die);
23204 /* Generate a DIE for an inlined subprogram. */
23206 static void
23207 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23209 tree decl;
23211 /* The instance of function that is effectively being inlined shall not
23212 be abstract. */
23213 gcc_assert (! BLOCK_ABSTRACT (stmt));
23215 decl = block_ultimate_origin (stmt);
23217 /* Make sure any inlined functions are known to be inlineable. */
23218 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23219 || cgraph_function_possibly_inlined_p (decl));
23221 if (! BLOCK_ABSTRACT (stmt))
23223 dw_die_ref subr_die
23224 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23226 if (call_arg_locations)
23227 BLOCK_DIE (stmt) = subr_die;
23228 add_abstract_origin_attribute (subr_die, decl);
23229 if (TREE_ASM_WRITTEN (stmt))
23230 add_high_low_attributes (stmt, subr_die);
23231 add_call_src_coords_attributes (stmt, subr_die);
23233 decls_for_scope (stmt, subr_die);
23237 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23238 the comment for VLR_CONTEXT. */
23240 static void
23241 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23243 dw_die_ref decl_die;
23245 if (TREE_TYPE (decl) == error_mark_node)
23246 return;
23248 decl_die = new_die (DW_TAG_member, context_die, decl);
23249 add_name_and_src_coords_attributes (decl_die, decl);
23250 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23251 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23252 context_die);
23254 if (DECL_BIT_FIELD_TYPE (decl))
23256 add_byte_size_attribute (decl_die, decl);
23257 add_bit_size_attribute (decl_die, decl);
23258 add_bit_offset_attribute (decl_die, decl, ctx);
23261 add_alignment_attribute (decl_die, decl);
23263 /* If we have a variant part offset, then we are supposed to process a member
23264 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23265 trees. */
23266 gcc_assert (ctx->variant_part_offset == NULL_TREE
23267 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23268 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23269 add_data_member_location_attribute (decl_die, decl, ctx);
23271 if (DECL_ARTIFICIAL (decl))
23272 add_AT_flag (decl_die, DW_AT_artificial, 1);
23274 add_accessibility_attribute (decl_die, decl);
23276 /* Equate decl number to die, so that we can look up this decl later on. */
23277 equate_decl_number_to_die (decl, decl_die);
23280 /* Generate a DIE for a pointer to a member type. TYPE can be an
23281 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23282 pointer to member function. */
23284 static void
23285 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23287 if (lookup_type_die (type))
23288 return;
23290 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23291 scope_die_for (type, context_die), type);
23293 equate_type_number_to_die (type, ptr_die);
23294 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23295 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23296 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23297 context_die);
23298 add_alignment_attribute (ptr_die, type);
23300 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23301 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23303 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23304 add_AT_loc (ptr_die, DW_AT_use_location, op);
23308 static char *producer_string;
23310 /* Return a heap allocated producer string including command line options
23311 if -grecord-gcc-switches. */
23313 static char *
23314 gen_producer_string (void)
23316 size_t j;
23317 auto_vec<const char *> switches;
23318 const char *language_string = lang_hooks.name;
23319 char *producer, *tail;
23320 const char *p;
23321 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23322 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23324 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23325 switch (save_decoded_options[j].opt_index)
23327 case OPT_o:
23328 case OPT_d:
23329 case OPT_dumpbase:
23330 case OPT_dumpdir:
23331 case OPT_auxbase:
23332 case OPT_auxbase_strip:
23333 case OPT_quiet:
23334 case OPT_version:
23335 case OPT_v:
23336 case OPT_w:
23337 case OPT_L:
23338 case OPT_D:
23339 case OPT_I:
23340 case OPT_U:
23341 case OPT_SPECIAL_unknown:
23342 case OPT_SPECIAL_ignore:
23343 case OPT_SPECIAL_program_name:
23344 case OPT_SPECIAL_input_file:
23345 case OPT_grecord_gcc_switches:
23346 case OPT_gno_record_gcc_switches:
23347 case OPT__output_pch_:
23348 case OPT_fdiagnostics_show_location_:
23349 case OPT_fdiagnostics_show_option:
23350 case OPT_fdiagnostics_show_caret:
23351 case OPT_fdiagnostics_color_:
23352 case OPT_fverbose_asm:
23353 case OPT____:
23354 case OPT__sysroot_:
23355 case OPT_nostdinc:
23356 case OPT_nostdinc__:
23357 case OPT_fpreprocessed:
23358 case OPT_fltrans_output_list_:
23359 case OPT_fresolution_:
23360 case OPT_fdebug_prefix_map_:
23361 /* Ignore these. */
23362 continue;
23363 default:
23364 if (cl_options[save_decoded_options[j].opt_index].flags
23365 & CL_NO_DWARF_RECORD)
23366 continue;
23367 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23368 == '-');
23369 switch (save_decoded_options[j].canonical_option[0][1])
23371 case 'M':
23372 case 'i':
23373 case 'W':
23374 continue;
23375 case 'f':
23376 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23377 "dump", 4) == 0)
23378 continue;
23379 break;
23380 default:
23381 break;
23383 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23384 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23385 break;
23388 producer = XNEWVEC (char, plen + 1 + len + 1);
23389 tail = producer;
23390 sprintf (tail, "%s %s", language_string, version_string);
23391 tail += plen;
23393 FOR_EACH_VEC_ELT (switches, j, p)
23395 len = strlen (p);
23396 *tail = ' ';
23397 memcpy (tail + 1, p, len);
23398 tail += len + 1;
23401 *tail = '\0';
23402 return producer;
23405 /* Given a C and/or C++ language/version string return the "highest".
23406 C++ is assumed to be "higher" than C in this case. Used for merging
23407 LTO translation unit languages. */
23408 static const char *
23409 highest_c_language (const char *lang1, const char *lang2)
23411 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23412 return "GNU C++14";
23413 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23414 return "GNU C++11";
23415 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23416 return "GNU C++98";
23418 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23419 return "GNU C11";
23420 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23421 return "GNU C99";
23422 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23423 return "GNU C89";
23425 gcc_unreachable ();
23429 /* Generate the DIE for the compilation unit. */
23431 static dw_die_ref
23432 gen_compile_unit_die (const char *filename)
23434 dw_die_ref die;
23435 const char *language_string = lang_hooks.name;
23436 int language;
23438 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23440 if (filename)
23442 add_name_attribute (die, filename);
23443 /* Don't add cwd for <built-in>. */
23444 if (filename[0] != '<')
23445 add_comp_dir_attribute (die);
23448 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23450 /* If our producer is LTO try to figure out a common language to use
23451 from the global list of translation units. */
23452 if (strcmp (language_string, "GNU GIMPLE") == 0)
23454 unsigned i;
23455 tree t;
23456 const char *common_lang = NULL;
23458 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23460 if (!TRANSLATION_UNIT_LANGUAGE (t))
23461 continue;
23462 if (!common_lang)
23463 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23464 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23466 else if (strncmp (common_lang, "GNU C", 5) == 0
23467 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23468 /* Mixing C and C++ is ok, use C++ in that case. */
23469 common_lang = highest_c_language (common_lang,
23470 TRANSLATION_UNIT_LANGUAGE (t));
23471 else
23473 /* Fall back to C. */
23474 common_lang = NULL;
23475 break;
23479 if (common_lang)
23480 language_string = common_lang;
23483 language = DW_LANG_C;
23484 if (strncmp (language_string, "GNU C", 5) == 0
23485 && ISDIGIT (language_string[5]))
23487 language = DW_LANG_C89;
23488 if (dwarf_version >= 3 || !dwarf_strict)
23490 if (strcmp (language_string, "GNU C89") != 0)
23491 language = DW_LANG_C99;
23493 if (dwarf_version >= 5 /* || !dwarf_strict */)
23494 if (strcmp (language_string, "GNU C11") == 0)
23495 language = DW_LANG_C11;
23498 else if (strncmp (language_string, "GNU C++", 7) == 0)
23500 language = DW_LANG_C_plus_plus;
23501 if (dwarf_version >= 5 /* || !dwarf_strict */)
23503 if (strcmp (language_string, "GNU C++11") == 0)
23504 language = DW_LANG_C_plus_plus_11;
23505 else if (strcmp (language_string, "GNU C++14") == 0)
23506 language = DW_LANG_C_plus_plus_14;
23509 else if (strcmp (language_string, "GNU F77") == 0)
23510 language = DW_LANG_Fortran77;
23511 else if (dwarf_version >= 3 || !dwarf_strict)
23513 if (strcmp (language_string, "GNU Ada") == 0)
23514 language = DW_LANG_Ada95;
23515 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23517 language = DW_LANG_Fortran95;
23518 if (dwarf_version >= 5 /* || !dwarf_strict */)
23520 if (strcmp (language_string, "GNU Fortran2003") == 0)
23521 language = DW_LANG_Fortran03;
23522 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23523 language = DW_LANG_Fortran08;
23526 else if (strcmp (language_string, "GNU Objective-C") == 0)
23527 language = DW_LANG_ObjC;
23528 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23529 language = DW_LANG_ObjC_plus_plus;
23530 else if (dwarf_version >= 5 || !dwarf_strict)
23532 if (strcmp (language_string, "GNU Go") == 0)
23533 language = DW_LANG_Go;
23536 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23537 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23538 language = DW_LANG_Fortran90;
23540 add_AT_unsigned (die, DW_AT_language, language);
23542 switch (language)
23544 case DW_LANG_Fortran77:
23545 case DW_LANG_Fortran90:
23546 case DW_LANG_Fortran95:
23547 case DW_LANG_Fortran03:
23548 case DW_LANG_Fortran08:
23549 /* Fortran has case insensitive identifiers and the front-end
23550 lowercases everything. */
23551 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23552 break;
23553 default:
23554 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23555 break;
23557 return die;
23560 /* Generate the DIE for a base class. */
23562 static void
23563 gen_inheritance_die (tree binfo, tree access, tree type,
23564 dw_die_ref context_die)
23566 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23567 struct vlr_context ctx = { type, NULL };
23569 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23570 context_die);
23571 add_data_member_location_attribute (die, binfo, &ctx);
23573 if (BINFO_VIRTUAL_P (binfo))
23574 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23576 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23577 children, otherwise the default is DW_ACCESS_public. In DWARF2
23578 the default has always been DW_ACCESS_private. */
23579 if (access == access_public_node)
23581 if (dwarf_version == 2
23582 || context_die->die_tag == DW_TAG_class_type)
23583 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23585 else if (access == access_protected_node)
23586 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23587 else if (dwarf_version > 2
23588 && context_die->die_tag != DW_TAG_class_type)
23589 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23592 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23593 structure. */
23594 static bool
23595 is_variant_part (tree decl)
23597 return (TREE_CODE (decl) == FIELD_DECL
23598 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23601 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23602 return the FIELD_DECL. Return NULL_TREE otherwise. */
23604 static tree
23605 analyze_discr_in_predicate (tree operand, tree struct_type)
23607 bool continue_stripping = true;
23608 while (continue_stripping)
23609 switch (TREE_CODE (operand))
23611 CASE_CONVERT:
23612 operand = TREE_OPERAND (operand, 0);
23613 break;
23614 default:
23615 continue_stripping = false;
23616 break;
23619 /* Match field access to members of struct_type only. */
23620 if (TREE_CODE (operand) == COMPONENT_REF
23621 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23622 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23623 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23624 return TREE_OPERAND (operand, 1);
23625 else
23626 return NULL_TREE;
23629 /* Check that SRC is a constant integer that can be represented as a native
23630 integer constant (either signed or unsigned). If so, store it into DEST and
23631 return true. Return false otherwise. */
23633 static bool
23634 get_discr_value (tree src, dw_discr_value *dest)
23636 tree discr_type = TREE_TYPE (src);
23638 if (lang_hooks.types.get_debug_type)
23640 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23641 if (debug_type != NULL)
23642 discr_type = debug_type;
23645 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23646 return false;
23648 /* Signedness can vary between the original type and the debug type. This
23649 can happen for character types in Ada for instance: the character type
23650 used for code generation can be signed, to be compatible with the C one,
23651 but from a debugger point of view, it must be unsigned. */
23652 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23653 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23655 if (is_orig_unsigned != is_debug_unsigned)
23656 src = fold_convert (discr_type, src);
23658 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23659 return false;
23661 dest->pos = is_debug_unsigned;
23662 if (is_debug_unsigned)
23663 dest->v.uval = tree_to_uhwi (src);
23664 else
23665 dest->v.sval = tree_to_shwi (src);
23667 return true;
23670 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23671 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23672 store NULL_TREE in DISCR_DECL. Otherwise:
23674 - store the discriminant field in STRUCT_TYPE that controls the variant
23675 part to *DISCR_DECL
23677 - put in *DISCR_LISTS_P an array where for each variant, the item
23678 represents the corresponding matching list of discriminant values.
23680 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23681 the above array.
23683 Note that when the array is allocated (i.e. when the analysis is
23684 successful), it is up to the caller to free the array. */
23686 static void
23687 analyze_variants_discr (tree variant_part_decl,
23688 tree struct_type,
23689 tree *discr_decl,
23690 dw_discr_list_ref **discr_lists_p,
23691 unsigned *discr_lists_length)
23693 tree variant_part_type = TREE_TYPE (variant_part_decl);
23694 tree variant;
23695 dw_discr_list_ref *discr_lists;
23696 unsigned i;
23698 /* Compute how many variants there are in this variant part. */
23699 *discr_lists_length = 0;
23700 for (variant = TYPE_FIELDS (variant_part_type);
23701 variant != NULL_TREE;
23702 variant = DECL_CHAIN (variant))
23703 ++*discr_lists_length;
23705 *discr_decl = NULL_TREE;
23706 *discr_lists_p
23707 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23708 sizeof (**discr_lists_p));
23709 discr_lists = *discr_lists_p;
23711 /* And then analyze all variants to extract discriminant information for all
23712 of them. This analysis is conservative: as soon as we detect something we
23713 do not support, abort everything and pretend we found nothing. */
23714 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23715 variant != NULL_TREE;
23716 variant = DECL_CHAIN (variant), ++i)
23718 tree match_expr = DECL_QUALIFIER (variant);
23720 /* Now, try to analyze the predicate and deduce a discriminant for
23721 it. */
23722 if (match_expr == boolean_true_node)
23723 /* Typically happens for the default variant: it matches all cases that
23724 previous variants rejected. Don't output any matching value for
23725 this one. */
23726 continue;
23728 /* The following loop tries to iterate over each discriminant
23729 possibility: single values or ranges. */
23730 while (match_expr != NULL_TREE)
23732 tree next_round_match_expr;
23733 tree candidate_discr = NULL_TREE;
23734 dw_discr_list_ref new_node = NULL;
23736 /* Possibilities are matched one after the other by nested
23737 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23738 continue with the rest at next iteration. */
23739 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23741 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23742 match_expr = TREE_OPERAND (match_expr, 1);
23744 else
23745 next_round_match_expr = NULL_TREE;
23747 if (match_expr == boolean_false_node)
23748 /* This sub-expression matches nothing: just wait for the next
23749 one. */
23752 else if (TREE_CODE (match_expr) == EQ_EXPR)
23754 /* We are matching: <discr_field> == <integer_cst>
23755 This sub-expression matches a single value. */
23756 tree integer_cst = TREE_OPERAND (match_expr, 1);
23758 candidate_discr
23759 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23760 struct_type);
23762 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23763 if (!get_discr_value (integer_cst,
23764 &new_node->dw_discr_lower_bound))
23765 goto abort;
23766 new_node->dw_discr_range = false;
23769 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23771 /* We are matching:
23772 <discr_field> > <integer_cst>
23773 && <discr_field> < <integer_cst>.
23774 This sub-expression matches the range of values between the
23775 two matched integer constants. Note that comparisons can be
23776 inclusive or exclusive. */
23777 tree candidate_discr_1, candidate_discr_2;
23778 tree lower_cst, upper_cst;
23779 bool lower_cst_included, upper_cst_included;
23780 tree lower_op = TREE_OPERAND (match_expr, 0);
23781 tree upper_op = TREE_OPERAND (match_expr, 1);
23783 /* When the comparison is exclusive, the integer constant is not
23784 the discriminant range bound we are looking for: we will have
23785 to increment or decrement it. */
23786 if (TREE_CODE (lower_op) == GE_EXPR)
23787 lower_cst_included = true;
23788 else if (TREE_CODE (lower_op) == GT_EXPR)
23789 lower_cst_included = false;
23790 else
23791 goto abort;
23793 if (TREE_CODE (upper_op) == LE_EXPR)
23794 upper_cst_included = true;
23795 else if (TREE_CODE (upper_op) == LT_EXPR)
23796 upper_cst_included = false;
23797 else
23798 goto abort;
23800 /* Extract the discriminant from the first operand and check it
23801 is consistant with the same analysis in the second
23802 operand. */
23803 candidate_discr_1
23804 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23805 struct_type);
23806 candidate_discr_2
23807 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23808 struct_type);
23809 if (candidate_discr_1 == candidate_discr_2)
23810 candidate_discr = candidate_discr_1;
23811 else
23812 goto abort;
23814 /* Extract bounds from both. */
23815 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23816 lower_cst = TREE_OPERAND (lower_op, 1);
23817 upper_cst = TREE_OPERAND (upper_op, 1);
23819 if (!lower_cst_included)
23820 lower_cst
23821 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23822 build_int_cst (TREE_TYPE (lower_cst), 1));
23823 if (!upper_cst_included)
23824 upper_cst
23825 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23826 build_int_cst (TREE_TYPE (upper_cst), 1));
23828 if (!get_discr_value (lower_cst,
23829 &new_node->dw_discr_lower_bound)
23830 || !get_discr_value (upper_cst,
23831 &new_node->dw_discr_upper_bound))
23832 goto abort;
23834 new_node->dw_discr_range = true;
23837 else
23838 /* Unsupported sub-expression: we cannot determine the set of
23839 matching discriminant values. Abort everything. */
23840 goto abort;
23842 /* If the discriminant info is not consistant with what we saw so
23843 far, consider the analysis failed and abort everything. */
23844 if (candidate_discr == NULL_TREE
23845 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23846 goto abort;
23847 else
23848 *discr_decl = candidate_discr;
23850 if (new_node != NULL)
23852 new_node->dw_discr_next = discr_lists[i];
23853 discr_lists[i] = new_node;
23855 match_expr = next_round_match_expr;
23859 /* If we reach this point, we could match everything we were interested
23860 in. */
23861 return;
23863 abort:
23864 /* Clean all data structure and return no result. */
23865 free (*discr_lists_p);
23866 *discr_lists_p = NULL;
23867 *discr_decl = NULL_TREE;
23870 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23871 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23872 under CONTEXT_DIE.
23874 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23875 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23876 this type, which are record types, represent the available variants and each
23877 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23878 values are inferred from these attributes.
23880 In trees, the offsets for the fields inside these sub-records are relative
23881 to the variant part itself, whereas the corresponding DIEs should have
23882 offset attributes that are relative to the embedding record base address.
23883 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23884 must be an expression that computes the offset of the variant part to
23885 describe in DWARF. */
23887 static void
23888 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23889 dw_die_ref context_die)
23891 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23892 tree variant_part_offset = vlr_ctx->variant_part_offset;
23893 struct loc_descr_context ctx = {
23894 vlr_ctx->struct_type, /* context_type */
23895 NULL_TREE, /* base_decl */
23896 NULL, /* dpi */
23897 false, /* placeholder_arg */
23898 false /* placeholder_seen */
23901 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23902 NULL_TREE if there is no such field. */
23903 tree discr_decl = NULL_TREE;
23904 dw_discr_list_ref *discr_lists;
23905 unsigned discr_lists_length = 0;
23906 unsigned i;
23908 dw_die_ref dwarf_proc_die = NULL;
23909 dw_die_ref variant_part_die
23910 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23912 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23914 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23915 &discr_decl, &discr_lists, &discr_lists_length);
23917 if (discr_decl != NULL_TREE)
23919 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23921 if (discr_die)
23922 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23923 else
23924 /* We have no DIE for the discriminant, so just discard all
23925 discrimimant information in the output. */
23926 discr_decl = NULL_TREE;
23929 /* If the offset for this variant part is more complex than a constant,
23930 create a DWARF procedure for it so that we will not have to generate DWARF
23931 expressions for it for each member. */
23932 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23933 && (dwarf_version >= 3 || !dwarf_strict))
23935 const tree dwarf_proc_fndecl
23936 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23937 build_function_type (TREE_TYPE (variant_part_offset),
23938 NULL_TREE));
23939 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23940 const dw_loc_descr_ref dwarf_proc_body
23941 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23943 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23944 dwarf_proc_fndecl, context_die);
23945 if (dwarf_proc_die != NULL)
23946 variant_part_offset = dwarf_proc_call;
23949 /* Output DIEs for all variants. */
23950 i = 0;
23951 for (tree variant = TYPE_FIELDS (variant_part_type);
23952 variant != NULL_TREE;
23953 variant = DECL_CHAIN (variant), ++i)
23955 tree variant_type = TREE_TYPE (variant);
23956 dw_die_ref variant_die;
23958 /* All variants (i.e. members of a variant part) are supposed to be
23959 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23960 under these records. */
23961 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23963 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23964 equate_decl_number_to_die (variant, variant_die);
23966 /* Output discriminant values this variant matches, if any. */
23967 if (discr_decl == NULL || discr_lists[i] == NULL)
23968 /* In the case we have discriminant information at all, this is
23969 probably the default variant: as the standard says, don't
23970 output any discriminant value/list attribute. */
23972 else if (discr_lists[i]->dw_discr_next == NULL
23973 && !discr_lists[i]->dw_discr_range)
23974 /* If there is only one accepted value, don't bother outputting a
23975 list. */
23976 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23977 else
23978 add_discr_list (variant_die, discr_lists[i]);
23980 for (tree member = TYPE_FIELDS (variant_type);
23981 member != NULL_TREE;
23982 member = DECL_CHAIN (member))
23984 struct vlr_context vlr_sub_ctx = {
23985 vlr_ctx->struct_type, /* struct_type */
23986 NULL /* variant_part_offset */
23988 if (is_variant_part (member))
23990 /* All offsets for fields inside variant parts are relative to
23991 the top-level embedding RECORD_TYPE's base address. On the
23992 other hand, offsets in GCC's types are relative to the
23993 nested-most variant part. So we have to sum offsets each time
23994 we recurse. */
23996 vlr_sub_ctx.variant_part_offset
23997 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
23998 variant_part_offset, byte_position (member));
23999 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24001 else
24003 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24004 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24009 free (discr_lists);
24012 /* Generate a DIE for a class member. */
24014 static void
24015 gen_member_die (tree type, dw_die_ref context_die)
24017 tree member;
24018 tree binfo = TYPE_BINFO (type);
24020 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24022 /* If this is not an incomplete type, output descriptions of each of its
24023 members. Note that as we output the DIEs necessary to represent the
24024 members of this record or union type, we will also be trying to output
24025 DIEs to represent the *types* of those members. However the `type'
24026 function (above) will specifically avoid generating type DIEs for member
24027 types *within* the list of member DIEs for this (containing) type except
24028 for those types (of members) which are explicitly marked as also being
24029 members of this (containing) type themselves. The g++ front- end can
24030 force any given type to be treated as a member of some other (containing)
24031 type by setting the TYPE_CONTEXT of the given (member) type to point to
24032 the TREE node representing the appropriate (containing) type. */
24034 /* First output info about the base classes. */
24035 if (binfo)
24037 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24038 int i;
24039 tree base;
24041 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24042 gen_inheritance_die (base,
24043 (accesses ? (*accesses)[i] : access_public_node),
24044 type,
24045 context_die);
24048 /* Now output info about the data members and type members. */
24049 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24051 struct vlr_context vlr_ctx = { type, NULL_TREE };
24052 bool static_inline_p
24053 = (TREE_STATIC (member)
24054 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24055 != -1));
24057 /* Ignore clones. */
24058 if (DECL_ABSTRACT_ORIGIN (member))
24059 continue;
24061 /* If we thought we were generating minimal debug info for TYPE
24062 and then changed our minds, some of the member declarations
24063 may have already been defined. Don't define them again, but
24064 do put them in the right order. */
24066 if (dw_die_ref child = lookup_decl_die (member))
24068 /* Handle inline static data members, which only have in-class
24069 declarations. */
24070 dw_die_ref ref = NULL;
24071 if (child->die_tag == DW_TAG_variable
24072 && child->die_parent == comp_unit_die ())
24074 ref = get_AT_ref (child, DW_AT_specification);
24075 /* For C++17 inline static data members followed by redundant
24076 out of class redeclaration, we might get here with
24077 child being the DIE created for the out of class
24078 redeclaration and with its DW_AT_specification being
24079 the DIE created for in-class definition. We want to
24080 reparent the latter, and don't want to create another
24081 DIE with DW_AT_specification in that case, because
24082 we already have one. */
24083 if (ref
24084 && static_inline_p
24085 && ref->die_tag == DW_TAG_variable
24086 && ref->die_parent == comp_unit_die ()
24087 && get_AT (ref, DW_AT_specification) == NULL)
24089 child = ref;
24090 ref = NULL;
24091 static_inline_p = false;
24095 if (child->die_tag == DW_TAG_variable
24096 && child->die_parent == comp_unit_die ()
24097 && ref == NULL)
24099 reparent_child (child, context_die);
24100 if (dwarf_version < 5)
24101 child->die_tag = DW_TAG_member;
24103 else
24104 splice_child_die (context_die, child);
24107 /* Do not generate standard DWARF for variant parts if we are generating
24108 the corresponding GNAT encodings: DIEs generated for both would
24109 conflict in our mappings. */
24110 else if (is_variant_part (member)
24111 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24113 vlr_ctx.variant_part_offset = byte_position (member);
24114 gen_variant_part (member, &vlr_ctx, context_die);
24116 else
24118 vlr_ctx.variant_part_offset = NULL_TREE;
24119 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24122 /* For C++ inline static data members emit immediately a DW_TAG_variable
24123 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24124 DW_AT_specification. */
24125 if (static_inline_p)
24127 int old_extern = DECL_EXTERNAL (member);
24128 DECL_EXTERNAL (member) = 0;
24129 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24130 DECL_EXTERNAL (member) = old_extern;
24135 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24136 is set, we pretend that the type was never defined, so we only get the
24137 member DIEs needed by later specification DIEs. */
24139 static void
24140 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24141 enum debug_info_usage usage)
24143 if (TREE_ASM_WRITTEN (type))
24145 /* Fill in the bound of variable-length fields in late dwarf if
24146 still incomplete. */
24147 if (!early_dwarf && variably_modified_type_p (type, NULL))
24148 for (tree member = TYPE_FIELDS (type);
24149 member;
24150 member = DECL_CHAIN (member))
24151 fill_variable_array_bounds (TREE_TYPE (member));
24152 return;
24155 dw_die_ref type_die = lookup_type_die (type);
24156 dw_die_ref scope_die = 0;
24157 int nested = 0;
24158 int complete = (TYPE_SIZE (type)
24159 && (! TYPE_STUB_DECL (type)
24160 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24161 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24162 complete = complete && should_emit_struct_debug (type, usage);
24164 if (type_die && ! complete)
24165 return;
24167 if (TYPE_CONTEXT (type) != NULL_TREE
24168 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24169 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24170 nested = 1;
24172 scope_die = scope_die_for (type, context_die);
24174 /* Generate child dies for template paramaters. */
24175 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24176 schedule_generic_params_dies_gen (type);
24178 if (! type_die || (nested && is_cu_die (scope_die)))
24179 /* First occurrence of type or toplevel definition of nested class. */
24181 dw_die_ref old_die = type_die;
24183 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24184 ? record_type_tag (type) : DW_TAG_union_type,
24185 scope_die, type);
24186 equate_type_number_to_die (type, type_die);
24187 if (old_die)
24188 add_AT_specification (type_die, old_die);
24189 else
24190 add_name_attribute (type_die, type_tag (type));
24192 else
24193 remove_AT (type_die, DW_AT_declaration);
24195 /* If this type has been completed, then give it a byte_size attribute and
24196 then give a list of members. */
24197 if (complete && !ns_decl)
24199 /* Prevent infinite recursion in cases where the type of some member of
24200 this type is expressed in terms of this type itself. */
24201 TREE_ASM_WRITTEN (type) = 1;
24202 add_byte_size_attribute (type_die, type);
24203 add_alignment_attribute (type_die, type);
24204 if (TYPE_STUB_DECL (type) != NULL_TREE)
24206 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24207 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24210 /* If the first reference to this type was as the return type of an
24211 inline function, then it may not have a parent. Fix this now. */
24212 if (type_die->die_parent == NULL)
24213 add_child_die (scope_die, type_die);
24215 push_decl_scope (type);
24216 gen_member_die (type, type_die);
24217 pop_decl_scope ();
24219 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24220 if (TYPE_ARTIFICIAL (type))
24221 add_AT_flag (type_die, DW_AT_artificial, 1);
24223 /* GNU extension: Record what type our vtable lives in. */
24224 if (TYPE_VFIELD (type))
24226 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24228 gen_type_die (vtype, context_die);
24229 add_AT_die_ref (type_die, DW_AT_containing_type,
24230 lookup_type_die (vtype));
24233 else
24235 add_AT_flag (type_die, DW_AT_declaration, 1);
24237 /* We don't need to do this for function-local types. */
24238 if (TYPE_STUB_DECL (type)
24239 && ! decl_function_context (TYPE_STUB_DECL (type)))
24240 vec_safe_push (incomplete_types, type);
24243 if (get_AT (type_die, DW_AT_name))
24244 add_pubtype (type, type_die);
24247 /* Generate a DIE for a subroutine _type_. */
24249 static void
24250 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24252 tree return_type = TREE_TYPE (type);
24253 dw_die_ref subr_die
24254 = new_die (DW_TAG_subroutine_type,
24255 scope_die_for (type, context_die), type);
24257 equate_type_number_to_die (type, subr_die);
24258 add_prototyped_attribute (subr_die, type);
24259 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24260 context_die);
24261 add_alignment_attribute (subr_die, type);
24262 gen_formal_types_die (type, subr_die);
24264 if (get_AT (subr_die, DW_AT_name))
24265 add_pubtype (type, subr_die);
24266 if ((dwarf_version >= 5 || !dwarf_strict)
24267 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24268 add_AT_flag (subr_die, DW_AT_reference, 1);
24269 if ((dwarf_version >= 5 || !dwarf_strict)
24270 && lang_hooks.types.type_dwarf_attribute (type,
24271 DW_AT_rvalue_reference) != -1)
24272 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24275 /* Generate a DIE for a type definition. */
24277 static void
24278 gen_typedef_die (tree decl, dw_die_ref context_die)
24280 dw_die_ref type_die;
24281 tree type;
24283 if (TREE_ASM_WRITTEN (decl))
24285 if (DECL_ORIGINAL_TYPE (decl))
24286 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24287 return;
24290 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24291 checks in process_scope_var and modified_type_die), this should be called
24292 only for original types. */
24293 gcc_assert (decl_ultimate_origin (decl) == NULL
24294 || decl_ultimate_origin (decl) == decl);
24296 TREE_ASM_WRITTEN (decl) = 1;
24297 type_die = new_die (DW_TAG_typedef, context_die, decl);
24299 add_name_and_src_coords_attributes (type_die, decl);
24300 if (DECL_ORIGINAL_TYPE (decl))
24302 type = DECL_ORIGINAL_TYPE (decl);
24303 if (type == error_mark_node)
24304 return;
24306 gcc_assert (type != TREE_TYPE (decl));
24307 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24309 else
24311 type = TREE_TYPE (decl);
24312 if (type == error_mark_node)
24313 return;
24315 if (is_naming_typedef_decl (TYPE_NAME (type)))
24317 /* Here, we are in the case of decl being a typedef naming
24318 an anonymous type, e.g:
24319 typedef struct {...} foo;
24320 In that case TREE_TYPE (decl) is not a typedef variant
24321 type and TYPE_NAME of the anonymous type is set to the
24322 TYPE_DECL of the typedef. This construct is emitted by
24323 the C++ FE.
24325 TYPE is the anonymous struct named by the typedef
24326 DECL. As we need the DW_AT_type attribute of the
24327 DW_TAG_typedef to point to the DIE of TYPE, let's
24328 generate that DIE right away. add_type_attribute
24329 called below will then pick (via lookup_type_die) that
24330 anonymous struct DIE. */
24331 if (!TREE_ASM_WRITTEN (type))
24332 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24334 /* This is a GNU Extension. We are adding a
24335 DW_AT_linkage_name attribute to the DIE of the
24336 anonymous struct TYPE. The value of that attribute
24337 is the name of the typedef decl naming the anonymous
24338 struct. This greatly eases the work of consumers of
24339 this debug info. */
24340 add_linkage_name_raw (lookup_type_die (type), decl);
24344 add_type_attribute (type_die, type, decl_quals (decl), false,
24345 context_die);
24347 if (is_naming_typedef_decl (decl))
24348 /* We want that all subsequent calls to lookup_type_die with
24349 TYPE in argument yield the DW_TAG_typedef we have just
24350 created. */
24351 equate_type_number_to_die (type, type_die);
24353 add_alignment_attribute (type_die, TREE_TYPE (decl));
24355 add_accessibility_attribute (type_die, decl);
24357 if (DECL_ABSTRACT_P (decl))
24358 equate_decl_number_to_die (decl, type_die);
24360 if (get_AT (type_die, DW_AT_name))
24361 add_pubtype (decl, type_die);
24364 /* Generate a DIE for a struct, class, enum or union type. */
24366 static void
24367 gen_tagged_type_die (tree type,
24368 dw_die_ref context_die,
24369 enum debug_info_usage usage)
24371 int need_pop;
24373 if (type == NULL_TREE
24374 || !is_tagged_type (type))
24375 return;
24377 if (TREE_ASM_WRITTEN (type))
24378 need_pop = 0;
24379 /* If this is a nested type whose containing class hasn't been written
24380 out yet, writing it out will cover this one, too. This does not apply
24381 to instantiations of member class templates; they need to be added to
24382 the containing class as they are generated. FIXME: This hurts the
24383 idea of combining type decls from multiple TUs, since we can't predict
24384 what set of template instantiations we'll get. */
24385 else if (TYPE_CONTEXT (type)
24386 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24387 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24389 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24391 if (TREE_ASM_WRITTEN (type))
24392 return;
24394 /* If that failed, attach ourselves to the stub. */
24395 push_decl_scope (TYPE_CONTEXT (type));
24396 context_die = lookup_type_die (TYPE_CONTEXT (type));
24397 need_pop = 1;
24399 else if (TYPE_CONTEXT (type) != NULL_TREE
24400 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24402 /* If this type is local to a function that hasn't been written
24403 out yet, use a NULL context for now; it will be fixed up in
24404 decls_for_scope. */
24405 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24406 /* A declaration DIE doesn't count; nested types need to go in the
24407 specification. */
24408 if (context_die && is_declaration_die (context_die))
24409 context_die = NULL;
24410 need_pop = 0;
24412 else
24414 context_die = declare_in_namespace (type, context_die);
24415 need_pop = 0;
24418 if (TREE_CODE (type) == ENUMERAL_TYPE)
24420 /* This might have been written out by the call to
24421 declare_in_namespace. */
24422 if (!TREE_ASM_WRITTEN (type))
24423 gen_enumeration_type_die (type, context_die);
24425 else
24426 gen_struct_or_union_type_die (type, context_die, usage);
24428 if (need_pop)
24429 pop_decl_scope ();
24431 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24432 it up if it is ever completed. gen_*_type_die will set it for us
24433 when appropriate. */
24436 /* Generate a type description DIE. */
24438 static void
24439 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24440 enum debug_info_usage usage)
24442 struct array_descr_info info;
24444 if (type == NULL_TREE || type == error_mark_node)
24445 return;
24447 if (flag_checking && type)
24448 verify_type (type);
24450 if (TYPE_NAME (type) != NULL_TREE
24451 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24452 && is_redundant_typedef (TYPE_NAME (type))
24453 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24454 /* The DECL of this type is a typedef we don't want to emit debug
24455 info for but we want debug info for its underlying typedef.
24456 This can happen for e.g, the injected-class-name of a C++
24457 type. */
24458 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24460 /* If TYPE is a typedef type variant, let's generate debug info
24461 for the parent typedef which TYPE is a type of. */
24462 if (typedef_variant_p (type))
24464 if (TREE_ASM_WRITTEN (type))
24465 return;
24467 tree name = TYPE_NAME (type);
24468 tree origin = decl_ultimate_origin (name);
24469 if (origin != NULL && origin != name)
24471 gen_decl_die (origin, NULL, NULL, context_die);
24472 return;
24475 /* Prevent broken recursion; we can't hand off to the same type. */
24476 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24478 /* Give typedefs the right scope. */
24479 context_die = scope_die_for (type, context_die);
24481 TREE_ASM_WRITTEN (type) = 1;
24483 gen_decl_die (name, NULL, NULL, context_die);
24484 return;
24487 /* If type is an anonymous tagged type named by a typedef, let's
24488 generate debug info for the typedef. */
24489 if (is_naming_typedef_decl (TYPE_NAME (type)))
24491 /* Use the DIE of the containing namespace as the parent DIE of
24492 the type description DIE we want to generate. */
24493 if (DECL_CONTEXT (TYPE_NAME (type))
24494 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24495 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24497 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24498 return;
24501 if (lang_hooks.types.get_debug_type)
24503 tree debug_type = lang_hooks.types.get_debug_type (type);
24505 if (debug_type != NULL_TREE && debug_type != type)
24507 gen_type_die_with_usage (debug_type, context_die, usage);
24508 return;
24512 /* We are going to output a DIE to represent the unqualified version
24513 of this type (i.e. without any const or volatile qualifiers) so
24514 get the main variant (i.e. the unqualified version) of this type
24515 now. (Vectors and arrays are special because the debugging info is in the
24516 cloned type itself. Similarly function/method types can contain extra
24517 ref-qualification). */
24518 if (TREE_CODE (type) == FUNCTION_TYPE
24519 || TREE_CODE (type) == METHOD_TYPE)
24521 /* For function/method types, can't use type_main_variant here,
24522 because that can have different ref-qualifiers for C++,
24523 but try to canonicalize. */
24524 tree main = TYPE_MAIN_VARIANT (type);
24525 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24526 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24527 && check_base_type (t, main)
24528 && check_lang_type (t, type))
24530 type = t;
24531 break;
24534 else if (TREE_CODE (type) != VECTOR_TYPE
24535 && TREE_CODE (type) != ARRAY_TYPE)
24536 type = type_main_variant (type);
24538 /* If this is an array type with hidden descriptor, handle it first. */
24539 if (!TREE_ASM_WRITTEN (type)
24540 && lang_hooks.types.get_array_descr_info)
24542 memset (&info, 0, sizeof (info));
24543 if (lang_hooks.types.get_array_descr_info (type, &info))
24545 /* Fortran sometimes emits array types with no dimension. */
24546 gcc_assert (info.ndimensions >= 0
24547 && (info.ndimensions
24548 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24549 gen_descr_array_type_die (type, &info, context_die);
24550 TREE_ASM_WRITTEN (type) = 1;
24551 return;
24555 if (TREE_ASM_WRITTEN (type))
24557 /* Variable-length types may be incomplete even if
24558 TREE_ASM_WRITTEN. For such types, fall through to
24559 gen_array_type_die() and possibly fill in
24560 DW_AT_{upper,lower}_bound attributes. */
24561 if ((TREE_CODE (type) != ARRAY_TYPE
24562 && TREE_CODE (type) != RECORD_TYPE
24563 && TREE_CODE (type) != UNION_TYPE
24564 && TREE_CODE (type) != QUAL_UNION_TYPE)
24565 || !variably_modified_type_p (type, NULL))
24566 return;
24569 switch (TREE_CODE (type))
24571 case ERROR_MARK:
24572 break;
24574 case POINTER_TYPE:
24575 case REFERENCE_TYPE:
24576 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24577 ensures that the gen_type_die recursion will terminate even if the
24578 type is recursive. Recursive types are possible in Ada. */
24579 /* ??? We could perhaps do this for all types before the switch
24580 statement. */
24581 TREE_ASM_WRITTEN (type) = 1;
24583 /* For these types, all that is required is that we output a DIE (or a
24584 set of DIEs) to represent the "basis" type. */
24585 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24586 DINFO_USAGE_IND_USE);
24587 break;
24589 case OFFSET_TYPE:
24590 /* This code is used for C++ pointer-to-data-member types.
24591 Output a description of the relevant class type. */
24592 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24593 DINFO_USAGE_IND_USE);
24595 /* Output a description of the type of the object pointed to. */
24596 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24597 DINFO_USAGE_IND_USE);
24599 /* Now output a DIE to represent this pointer-to-data-member type
24600 itself. */
24601 gen_ptr_to_mbr_type_die (type, context_die);
24602 break;
24604 case FUNCTION_TYPE:
24605 /* Force out return type (in case it wasn't forced out already). */
24606 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24607 DINFO_USAGE_DIR_USE);
24608 gen_subroutine_type_die (type, context_die);
24609 break;
24611 case METHOD_TYPE:
24612 /* Force out return type (in case it wasn't forced out already). */
24613 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24614 DINFO_USAGE_DIR_USE);
24615 gen_subroutine_type_die (type, context_die);
24616 break;
24618 case ARRAY_TYPE:
24619 case VECTOR_TYPE:
24620 gen_array_type_die (type, context_die);
24621 break;
24623 case ENUMERAL_TYPE:
24624 case RECORD_TYPE:
24625 case UNION_TYPE:
24626 case QUAL_UNION_TYPE:
24627 gen_tagged_type_die (type, context_die, usage);
24628 return;
24630 case VOID_TYPE:
24631 case INTEGER_TYPE:
24632 case REAL_TYPE:
24633 case FIXED_POINT_TYPE:
24634 case COMPLEX_TYPE:
24635 case BOOLEAN_TYPE:
24636 case POINTER_BOUNDS_TYPE:
24637 /* No DIEs needed for fundamental types. */
24638 break;
24640 case NULLPTR_TYPE:
24641 case LANG_TYPE:
24642 /* Just use DW_TAG_unspecified_type. */
24644 dw_die_ref type_die = lookup_type_die (type);
24645 if (type_die == NULL)
24647 tree name = TYPE_IDENTIFIER (type);
24648 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24649 type);
24650 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24651 equate_type_number_to_die (type, type_die);
24654 break;
24656 default:
24657 if (is_cxx_auto (type))
24659 tree name = TYPE_IDENTIFIER (type);
24660 dw_die_ref *die = (name == get_identifier ("auto")
24661 ? &auto_die : &decltype_auto_die);
24662 if (!*die)
24664 *die = new_die (DW_TAG_unspecified_type,
24665 comp_unit_die (), NULL_TREE);
24666 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24668 equate_type_number_to_die (type, *die);
24669 break;
24671 gcc_unreachable ();
24674 TREE_ASM_WRITTEN (type) = 1;
24677 static void
24678 gen_type_die (tree type, dw_die_ref context_die)
24680 if (type != error_mark_node)
24682 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24683 if (flag_checking)
24685 dw_die_ref die = lookup_type_die (type);
24686 if (die)
24687 check_die (die);
24692 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24693 things which are local to the given block. */
24695 static void
24696 gen_block_die (tree stmt, dw_die_ref context_die)
24698 int must_output_die = 0;
24699 bool inlined_func;
24701 /* Ignore blocks that are NULL. */
24702 if (stmt == NULL_TREE)
24703 return;
24705 inlined_func = inlined_function_outer_scope_p (stmt);
24707 /* If the block is one fragment of a non-contiguous block, do not
24708 process the variables, since they will have been done by the
24709 origin block. Do process subblocks. */
24710 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24712 tree sub;
24714 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24715 gen_block_die (sub, context_die);
24717 return;
24720 /* Determine if we need to output any Dwarf DIEs at all to represent this
24721 block. */
24722 if (inlined_func)
24723 /* The outer scopes for inlinings *must* always be represented. We
24724 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24725 must_output_die = 1;
24726 else
24728 /* Determine if this block directly contains any "significant"
24729 local declarations which we will need to output DIEs for. */
24730 if (debug_info_level > DINFO_LEVEL_TERSE)
24731 /* We are not in terse mode so *any* local declaration counts
24732 as being a "significant" one. */
24733 must_output_die = ((BLOCK_VARS (stmt) != NULL
24734 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24735 && (TREE_USED (stmt)
24736 || TREE_ASM_WRITTEN (stmt)
24737 || BLOCK_ABSTRACT (stmt)));
24738 else if ((TREE_USED (stmt)
24739 || TREE_ASM_WRITTEN (stmt)
24740 || BLOCK_ABSTRACT (stmt))
24741 && !dwarf2out_ignore_block (stmt))
24742 must_output_die = 1;
24745 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24746 DIE for any block which contains no significant local declarations at
24747 all. Rather, in such cases we just call `decls_for_scope' so that any
24748 needed Dwarf info for any sub-blocks will get properly generated. Note
24749 that in terse mode, our definition of what constitutes a "significant"
24750 local declaration gets restricted to include only inlined function
24751 instances and local (nested) function definitions. */
24752 if (must_output_die)
24754 if (inlined_func)
24756 /* If STMT block is abstract, that means we have been called
24757 indirectly from dwarf2out_abstract_function.
24758 That function rightfully marks the descendent blocks (of
24759 the abstract function it is dealing with) as being abstract,
24760 precisely to prevent us from emitting any
24761 DW_TAG_inlined_subroutine DIE as a descendent
24762 of an abstract function instance. So in that case, we should
24763 not call gen_inlined_subroutine_die.
24765 Later though, when cgraph asks dwarf2out to emit info
24766 for the concrete instance of the function decl into which
24767 the concrete instance of STMT got inlined, the later will lead
24768 to the generation of a DW_TAG_inlined_subroutine DIE. */
24769 if (! BLOCK_ABSTRACT (stmt))
24770 gen_inlined_subroutine_die (stmt, context_die);
24772 else
24773 gen_lexical_block_die (stmt, context_die);
24775 else
24776 decls_for_scope (stmt, context_die);
24779 /* Process variable DECL (or variable with origin ORIGIN) within
24780 block STMT and add it to CONTEXT_DIE. */
24781 static void
24782 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24784 dw_die_ref die;
24785 tree decl_or_origin = decl ? decl : origin;
24787 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24788 die = lookup_decl_die (decl_or_origin);
24789 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24791 if (TYPE_DECL_IS_STUB (decl_or_origin))
24792 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24793 else
24794 die = lookup_decl_die (decl_or_origin);
24795 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24796 if (! die && ! early_dwarf)
24797 return;
24799 else
24800 die = NULL;
24802 /* Avoid creating DIEs for local typedefs and concrete static variables that
24803 will only be pruned later. */
24804 if ((origin || decl_ultimate_origin (decl))
24805 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24806 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24808 origin = decl_ultimate_origin (decl_or_origin);
24809 if (decl && VAR_P (decl) && die != NULL)
24811 die = lookup_decl_die (origin);
24812 if (die != NULL)
24813 equate_decl_number_to_die (decl, die);
24815 return;
24818 if (die != NULL && die->die_parent == NULL)
24819 add_child_die (context_die, die);
24820 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24822 if (early_dwarf)
24823 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24824 stmt, context_die);
24826 else
24828 if (decl && DECL_P (decl))
24830 die = lookup_decl_die (decl);
24832 /* Early created DIEs do not have a parent as the decls refer
24833 to the function as DECL_CONTEXT rather than the BLOCK. */
24834 if (die && die->die_parent == NULL)
24836 gcc_assert (in_lto_p);
24837 add_child_die (context_die, die);
24841 gen_decl_die (decl, origin, NULL, context_die);
24845 /* Generate all of the decls declared within a given scope and (recursively)
24846 all of its sub-blocks. */
24848 static void
24849 decls_for_scope (tree stmt, dw_die_ref context_die)
24851 tree decl;
24852 unsigned int i;
24853 tree subblocks;
24855 /* Ignore NULL blocks. */
24856 if (stmt == NULL_TREE)
24857 return;
24859 /* Output the DIEs to represent all of the data objects and typedefs
24860 declared directly within this block but not within any nested
24861 sub-blocks. Also, nested function and tag DIEs have been
24862 generated with a parent of NULL; fix that up now. We don't
24863 have to do this if we're at -g1. */
24864 if (debug_info_level > DINFO_LEVEL_TERSE)
24866 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24867 process_scope_var (stmt, decl, NULL_TREE, context_die);
24868 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24869 origin - avoid doing this twice as we have no good way to see
24870 if we've done it once already. */
24871 if (! early_dwarf)
24872 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24874 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24875 if (decl == current_function_decl)
24876 /* Ignore declarations of the current function, while they
24877 are declarations, gen_subprogram_die would treat them
24878 as definitions again, because they are equal to
24879 current_function_decl and endlessly recurse. */;
24880 else if (TREE_CODE (decl) == FUNCTION_DECL)
24881 process_scope_var (stmt, decl, NULL_TREE, context_die);
24882 else
24883 process_scope_var (stmt, NULL_TREE, decl, context_die);
24887 /* Even if we're at -g1, we need to process the subblocks in order to get
24888 inlined call information. */
24890 /* Output the DIEs to represent all sub-blocks (and the items declared
24891 therein) of this block. */
24892 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24893 subblocks != NULL;
24894 subblocks = BLOCK_CHAIN (subblocks))
24895 gen_block_die (subblocks, context_die);
24898 /* Is this a typedef we can avoid emitting? */
24900 bool
24901 is_redundant_typedef (const_tree decl)
24903 if (TYPE_DECL_IS_STUB (decl))
24904 return true;
24906 if (DECL_ARTIFICIAL (decl)
24907 && DECL_CONTEXT (decl)
24908 && is_tagged_type (DECL_CONTEXT (decl))
24909 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24910 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24911 /* Also ignore the artificial member typedef for the class name. */
24912 return true;
24914 return false;
24917 /* Return TRUE if TYPE is a typedef that names a type for linkage
24918 purposes. This kind of typedefs is produced by the C++ FE for
24919 constructs like:
24921 typedef struct {...} foo;
24923 In that case, there is no typedef variant type produced for foo.
24924 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24925 struct type. */
24927 static bool
24928 is_naming_typedef_decl (const_tree decl)
24930 if (decl == NULL_TREE
24931 || TREE_CODE (decl) != TYPE_DECL
24932 || DECL_NAMELESS (decl)
24933 || !is_tagged_type (TREE_TYPE (decl))
24934 || DECL_IS_BUILTIN (decl)
24935 || is_redundant_typedef (decl)
24936 /* It looks like Ada produces TYPE_DECLs that are very similar
24937 to C++ naming typedefs but that have different
24938 semantics. Let's be specific to c++ for now. */
24939 || !is_cxx (decl))
24940 return FALSE;
24942 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24943 && TYPE_NAME (TREE_TYPE (decl)) == decl
24944 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24945 != TYPE_NAME (TREE_TYPE (decl))));
24948 /* Looks up the DIE for a context. */
24950 static inline dw_die_ref
24951 lookup_context_die (tree context)
24953 if (context)
24955 /* Find die that represents this context. */
24956 if (TYPE_P (context))
24958 context = TYPE_MAIN_VARIANT (context);
24959 dw_die_ref ctx = lookup_type_die (context);
24960 if (!ctx)
24961 return NULL;
24962 return strip_naming_typedef (context, ctx);
24964 else
24965 return lookup_decl_die (context);
24967 return comp_unit_die ();
24970 /* Returns the DIE for a context. */
24972 static inline dw_die_ref
24973 get_context_die (tree context)
24975 if (context)
24977 /* Find die that represents this context. */
24978 if (TYPE_P (context))
24980 context = TYPE_MAIN_VARIANT (context);
24981 return strip_naming_typedef (context, force_type_die (context));
24983 else
24984 return force_decl_die (context);
24986 return comp_unit_die ();
24989 /* Returns the DIE for decl. A DIE will always be returned. */
24991 static dw_die_ref
24992 force_decl_die (tree decl)
24994 dw_die_ref decl_die;
24995 unsigned saved_external_flag;
24996 tree save_fn = NULL_TREE;
24997 decl_die = lookup_decl_die (decl);
24998 if (!decl_die)
25000 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25002 decl_die = lookup_decl_die (decl);
25003 if (decl_die)
25004 return decl_die;
25006 switch (TREE_CODE (decl))
25008 case FUNCTION_DECL:
25009 /* Clear current_function_decl, so that gen_subprogram_die thinks
25010 that this is a declaration. At this point, we just want to force
25011 declaration die. */
25012 save_fn = current_function_decl;
25013 current_function_decl = NULL_TREE;
25014 gen_subprogram_die (decl, context_die);
25015 current_function_decl = save_fn;
25016 break;
25018 case VAR_DECL:
25019 /* Set external flag to force declaration die. Restore it after
25020 gen_decl_die() call. */
25021 saved_external_flag = DECL_EXTERNAL (decl);
25022 DECL_EXTERNAL (decl) = 1;
25023 gen_decl_die (decl, NULL, NULL, context_die);
25024 DECL_EXTERNAL (decl) = saved_external_flag;
25025 break;
25027 case NAMESPACE_DECL:
25028 if (dwarf_version >= 3 || !dwarf_strict)
25029 dwarf2out_decl (decl);
25030 else
25031 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25032 decl_die = comp_unit_die ();
25033 break;
25035 case TRANSLATION_UNIT_DECL:
25036 decl_die = comp_unit_die ();
25037 break;
25039 default:
25040 gcc_unreachable ();
25043 /* We should be able to find the DIE now. */
25044 if (!decl_die)
25045 decl_die = lookup_decl_die (decl);
25046 gcc_assert (decl_die);
25049 return decl_die;
25052 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25053 always returned. */
25055 static dw_die_ref
25056 force_type_die (tree type)
25058 dw_die_ref type_die;
25060 type_die = lookup_type_die (type);
25061 if (!type_die)
25063 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25065 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25066 false, context_die);
25067 gcc_assert (type_die);
25069 return type_die;
25072 /* Force out any required namespaces to be able to output DECL,
25073 and return the new context_die for it, if it's changed. */
25075 static dw_die_ref
25076 setup_namespace_context (tree thing, dw_die_ref context_die)
25078 tree context = (DECL_P (thing)
25079 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25080 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25081 /* Force out the namespace. */
25082 context_die = force_decl_die (context);
25084 return context_die;
25087 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25088 type) within its namespace, if appropriate.
25090 For compatibility with older debuggers, namespace DIEs only contain
25091 declarations; all definitions are emitted at CU scope, with
25092 DW_AT_specification pointing to the declaration (like with class
25093 members). */
25095 static dw_die_ref
25096 declare_in_namespace (tree thing, dw_die_ref context_die)
25098 dw_die_ref ns_context;
25100 if (debug_info_level <= DINFO_LEVEL_TERSE)
25101 return context_die;
25103 /* External declarations in the local scope only need to be emitted
25104 once, not once in the namespace and once in the scope.
25106 This avoids declaring the `extern' below in the
25107 namespace DIE as well as in the innermost scope:
25109 namespace S
25111 int i=5;
25112 int foo()
25114 int i=8;
25115 extern int i;
25116 return i;
25120 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25121 return context_die;
25123 /* If this decl is from an inlined function, then don't try to emit it in its
25124 namespace, as we will get confused. It would have already been emitted
25125 when the abstract instance of the inline function was emitted anyways. */
25126 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25127 return context_die;
25129 ns_context = setup_namespace_context (thing, context_die);
25131 if (ns_context != context_die)
25133 if (is_fortran ())
25134 return ns_context;
25135 if (DECL_P (thing))
25136 gen_decl_die (thing, NULL, NULL, ns_context);
25137 else
25138 gen_type_die (thing, ns_context);
25140 return context_die;
25143 /* Generate a DIE for a namespace or namespace alias. */
25145 static void
25146 gen_namespace_die (tree decl, dw_die_ref context_die)
25148 dw_die_ref namespace_die;
25150 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25151 they are an alias of. */
25152 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25154 /* Output a real namespace or module. */
25155 context_die = setup_namespace_context (decl, comp_unit_die ());
25156 namespace_die = new_die (is_fortran ()
25157 ? DW_TAG_module : DW_TAG_namespace,
25158 context_die, decl);
25159 /* For Fortran modules defined in different CU don't add src coords. */
25160 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25162 const char *name = dwarf2_name (decl, 0);
25163 if (name)
25164 add_name_attribute (namespace_die, name);
25166 else
25167 add_name_and_src_coords_attributes (namespace_die, decl);
25168 if (DECL_EXTERNAL (decl))
25169 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25170 equate_decl_number_to_die (decl, namespace_die);
25172 else
25174 /* Output a namespace alias. */
25176 /* Force out the namespace we are an alias of, if necessary. */
25177 dw_die_ref origin_die
25178 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25180 if (DECL_FILE_SCOPE_P (decl)
25181 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25182 context_die = setup_namespace_context (decl, comp_unit_die ());
25183 /* Now create the namespace alias DIE. */
25184 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25185 add_name_and_src_coords_attributes (namespace_die, decl);
25186 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25187 equate_decl_number_to_die (decl, namespace_die);
25189 if ((dwarf_version >= 5 || !dwarf_strict)
25190 && lang_hooks.decls.decl_dwarf_attribute (decl,
25191 DW_AT_export_symbols) == 1)
25192 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25194 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25195 if (want_pubnames ())
25196 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25199 /* Generate Dwarf debug information for a decl described by DECL.
25200 The return value is currently only meaningful for PARM_DECLs,
25201 for all other decls it returns NULL.
25203 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25204 It can be NULL otherwise. */
25206 static dw_die_ref
25207 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25208 dw_die_ref context_die)
25210 tree decl_or_origin = decl ? decl : origin;
25211 tree class_origin = NULL, ultimate_origin;
25213 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25214 return NULL;
25216 /* Ignore pointer bounds decls. */
25217 if (DECL_P (decl_or_origin)
25218 && TREE_TYPE (decl_or_origin)
25219 && POINTER_BOUNDS_P (decl_or_origin))
25220 return NULL;
25222 switch (TREE_CODE (decl_or_origin))
25224 case ERROR_MARK:
25225 break;
25227 case CONST_DECL:
25228 if (!is_fortran () && !is_ada ())
25230 /* The individual enumerators of an enum type get output when we output
25231 the Dwarf representation of the relevant enum type itself. */
25232 break;
25235 /* Emit its type. */
25236 gen_type_die (TREE_TYPE (decl), context_die);
25238 /* And its containing namespace. */
25239 context_die = declare_in_namespace (decl, context_die);
25241 gen_const_die (decl, context_die);
25242 break;
25244 case FUNCTION_DECL:
25245 #if 0
25246 /* FIXME */
25247 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25248 on local redeclarations of global functions. That seems broken. */
25249 if (current_function_decl != decl)
25250 /* This is only a declaration. */;
25251 #endif
25253 /* We should have abstract copies already and should not generate
25254 stray type DIEs in late LTO dumping. */
25255 if (! early_dwarf)
25258 /* If we're emitting a clone, emit info for the abstract instance. */
25259 else if (origin || DECL_ORIGIN (decl) != decl)
25260 dwarf2out_abstract_function (origin
25261 ? DECL_ORIGIN (origin)
25262 : DECL_ABSTRACT_ORIGIN (decl));
25264 /* If we're emitting a possibly inlined function emit it as
25265 abstract instance. */
25266 else if (cgraph_function_possibly_inlined_p (decl)
25267 && ! DECL_ABSTRACT_P (decl)
25268 && ! class_or_namespace_scope_p (context_die)
25269 /* dwarf2out_abstract_function won't emit a die if this is just
25270 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25271 that case, because that works only if we have a die. */
25272 && DECL_INITIAL (decl) != NULL_TREE)
25273 dwarf2out_abstract_function (decl);
25275 /* Otherwise we're emitting the primary DIE for this decl. */
25276 else if (debug_info_level > DINFO_LEVEL_TERSE)
25278 /* Before we describe the FUNCTION_DECL itself, make sure that we
25279 have its containing type. */
25280 if (!origin)
25281 origin = decl_class_context (decl);
25282 if (origin != NULL_TREE)
25283 gen_type_die (origin, context_die);
25285 /* And its return type. */
25286 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25288 /* And its virtual context. */
25289 if (DECL_VINDEX (decl) != NULL_TREE)
25290 gen_type_die (DECL_CONTEXT (decl), context_die);
25292 /* Make sure we have a member DIE for decl. */
25293 if (origin != NULL_TREE)
25294 gen_type_die_for_member (origin, decl, context_die);
25296 /* And its containing namespace. */
25297 context_die = declare_in_namespace (decl, context_die);
25300 /* Now output a DIE to represent the function itself. */
25301 if (decl)
25302 gen_subprogram_die (decl, context_die);
25303 break;
25305 case TYPE_DECL:
25306 /* If we are in terse mode, don't generate any DIEs to represent any
25307 actual typedefs. */
25308 if (debug_info_level <= DINFO_LEVEL_TERSE)
25309 break;
25311 /* In the special case of a TYPE_DECL node representing the declaration
25312 of some type tag, if the given TYPE_DECL is marked as having been
25313 instantiated from some other (original) TYPE_DECL node (e.g. one which
25314 was generated within the original definition of an inline function) we
25315 used to generate a special (abbreviated) DW_TAG_structure_type,
25316 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25317 should be actually referencing those DIEs, as variable DIEs with that
25318 type would be emitted already in the abstract origin, so it was always
25319 removed during unused type prunning. Don't add anything in this
25320 case. */
25321 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25322 break;
25324 if (is_redundant_typedef (decl))
25325 gen_type_die (TREE_TYPE (decl), context_die);
25326 else
25327 /* Output a DIE to represent the typedef itself. */
25328 gen_typedef_die (decl, context_die);
25329 break;
25331 case LABEL_DECL:
25332 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25333 gen_label_die (decl, context_die);
25334 break;
25336 case VAR_DECL:
25337 case RESULT_DECL:
25338 /* If we are in terse mode, don't generate any DIEs to represent any
25339 variable declarations or definitions. */
25340 if (debug_info_level <= DINFO_LEVEL_TERSE)
25341 break;
25343 /* Avoid generating stray type DIEs during late dwarf dumping.
25344 All types have been dumped early. */
25345 if (early_dwarf
25346 /* ??? But in LTRANS we cannot annotate early created variably
25347 modified type DIEs without copying them and adjusting all
25348 references to them. Dump them again as happens for inlining
25349 which copies both the decl and the types. */
25350 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25351 in VLA bound information for example. */
25352 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25353 current_function_decl)))
25355 /* Output any DIEs that are needed to specify the type of this data
25356 object. */
25357 if (decl_by_reference_p (decl_or_origin))
25358 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25359 else
25360 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25363 if (early_dwarf)
25365 /* And its containing type. */
25366 class_origin = decl_class_context (decl_or_origin);
25367 if (class_origin != NULL_TREE)
25368 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25370 /* And its containing namespace. */
25371 context_die = declare_in_namespace (decl_or_origin, context_die);
25374 /* Now output the DIE to represent the data object itself. This gets
25375 complicated because of the possibility that the VAR_DECL really
25376 represents an inlined instance of a formal parameter for an inline
25377 function. */
25378 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25379 if (ultimate_origin != NULL_TREE
25380 && TREE_CODE (ultimate_origin) == PARM_DECL)
25381 gen_formal_parameter_die (decl, origin,
25382 true /* Emit name attribute. */,
25383 context_die);
25384 else
25385 gen_variable_die (decl, origin, context_die);
25386 break;
25388 case FIELD_DECL:
25389 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25390 /* Ignore the nameless fields that are used to skip bits but handle C++
25391 anonymous unions and structs. */
25392 if (DECL_NAME (decl) != NULL_TREE
25393 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25394 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25396 gen_type_die (member_declared_type (decl), context_die);
25397 gen_field_die (decl, ctx, context_die);
25399 break;
25401 case PARM_DECL:
25402 /* Avoid generating stray type DIEs during late dwarf dumping.
25403 All types have been dumped early. */
25404 if (early_dwarf
25405 /* ??? But in LTRANS we cannot annotate early created variably
25406 modified type DIEs without copying them and adjusting all
25407 references to them. Dump them again as happens for inlining
25408 which copies both the decl and the types. */
25409 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25410 in VLA bound information for example. */
25411 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25412 current_function_decl)))
25414 if (DECL_BY_REFERENCE (decl_or_origin))
25415 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25416 else
25417 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25419 return gen_formal_parameter_die (decl, origin,
25420 true /* Emit name attribute. */,
25421 context_die);
25423 case NAMESPACE_DECL:
25424 if (dwarf_version >= 3 || !dwarf_strict)
25425 gen_namespace_die (decl, context_die);
25426 break;
25428 case IMPORTED_DECL:
25429 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25430 DECL_CONTEXT (decl), context_die);
25431 break;
25433 case NAMELIST_DECL:
25434 gen_namelist_decl (DECL_NAME (decl), context_die,
25435 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25436 break;
25438 default:
25439 /* Probably some frontend-internal decl. Assume we don't care. */
25440 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25441 break;
25444 return NULL;
25447 /* Output initial debug information for global DECL. Called at the
25448 end of the parsing process.
25450 This is the initial debug generation process. As such, the DIEs
25451 generated may be incomplete. A later debug generation pass
25452 (dwarf2out_late_global_decl) will augment the information generated
25453 in this pass (e.g., with complete location info). */
25455 static void
25456 dwarf2out_early_global_decl (tree decl)
25458 set_early_dwarf s;
25460 /* gen_decl_die() will set DECL_ABSTRACT because
25461 cgraph_function_possibly_inlined_p() returns true. This is in
25462 turn will cause DW_AT_inline attributes to be set.
25464 This happens because at early dwarf generation, there is no
25465 cgraph information, causing cgraph_function_possibly_inlined_p()
25466 to return true. Trick cgraph_function_possibly_inlined_p()
25467 while we generate dwarf early. */
25468 bool save = symtab->global_info_ready;
25469 symtab->global_info_ready = true;
25471 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25472 other DECLs and they can point to template types or other things
25473 that dwarf2out can't handle when done via dwarf2out_decl. */
25474 if (TREE_CODE (decl) != TYPE_DECL
25475 && TREE_CODE (decl) != PARM_DECL)
25477 tree save_fndecl = current_function_decl;
25478 if (TREE_CODE (decl) == FUNCTION_DECL)
25480 /* For nested functions, make sure we have DIEs for the parents first
25481 so that all nested DIEs are generated at the proper scope in the
25482 first shot. */
25483 tree context = decl_function_context (decl);
25484 if (context != NULL && lookup_decl_die (context) == NULL)
25486 current_function_decl = context;
25487 dwarf2out_decl (context);
25490 /* Emit an abstract origin of a function first. This happens
25491 with C++ constructor clones for example and makes
25492 dwarf2out_abstract_function happy which requires the early
25493 DIE of the abstract instance to be present. */
25494 if (DECL_ABSTRACT_ORIGIN (decl))
25496 current_function_decl = DECL_ABSTRACT_ORIGIN (decl);
25497 dwarf2out_decl (DECL_ABSTRACT_ORIGIN (decl));
25500 current_function_decl = decl;
25502 dwarf2out_decl (decl);
25503 if (TREE_CODE (decl) == FUNCTION_DECL)
25504 current_function_decl = save_fndecl;
25506 symtab->global_info_ready = save;
25509 /* Output debug information for global decl DECL. Called from
25510 toplev.c after compilation proper has finished. */
25512 static void
25513 dwarf2out_late_global_decl (tree decl)
25515 /* Fill-in any location information we were unable to determine
25516 on the first pass. */
25517 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25519 dw_die_ref die = lookup_decl_die (decl);
25521 /* We may have to generate early debug late for LTO in case debug
25522 was not enabled at compile-time or the target doesn't support
25523 the LTO early debug scheme. */
25524 if (! die && in_lto_p)
25526 dwarf2out_decl (decl);
25527 die = lookup_decl_die (decl);
25530 if (die)
25532 /* We get called via the symtab code invoking late_global_decl
25533 for symbols that are optimized out. Do not add locations
25534 for those, except if they have a DECL_VALUE_EXPR, in which case
25535 they are relevant for debuggers. */
25536 varpool_node *node = varpool_node::get (decl);
25537 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25538 tree_add_const_value_attribute_for_decl (die, decl);
25539 else
25540 add_location_or_const_value_attribute (die, decl, false);
25545 /* Output debug information for type decl DECL. Called from toplev.c
25546 and from language front ends (to record built-in types). */
25547 static void
25548 dwarf2out_type_decl (tree decl, int local)
25550 if (!local)
25552 set_early_dwarf s;
25553 dwarf2out_decl (decl);
25557 /* Output debug information for imported module or decl DECL.
25558 NAME is non-NULL name in the lexical block if the decl has been renamed.
25559 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25560 that DECL belongs to.
25561 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25562 static void
25563 dwarf2out_imported_module_or_decl_1 (tree decl,
25564 tree name,
25565 tree lexical_block,
25566 dw_die_ref lexical_block_die)
25568 expanded_location xloc;
25569 dw_die_ref imported_die = NULL;
25570 dw_die_ref at_import_die;
25572 if (TREE_CODE (decl) == IMPORTED_DECL)
25574 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25575 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25576 gcc_assert (decl);
25578 else
25579 xloc = expand_location (input_location);
25581 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25583 at_import_die = force_type_die (TREE_TYPE (decl));
25584 /* For namespace N { typedef void T; } using N::T; base_type_die
25585 returns NULL, but DW_TAG_imported_declaration requires
25586 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25587 if (!at_import_die)
25589 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25590 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25591 at_import_die = lookup_type_die (TREE_TYPE (decl));
25592 gcc_assert (at_import_die);
25595 else
25597 at_import_die = lookup_decl_die (decl);
25598 if (!at_import_die)
25600 /* If we're trying to avoid duplicate debug info, we may not have
25601 emitted the member decl for this field. Emit it now. */
25602 if (TREE_CODE (decl) == FIELD_DECL)
25604 tree type = DECL_CONTEXT (decl);
25606 if (TYPE_CONTEXT (type)
25607 && TYPE_P (TYPE_CONTEXT (type))
25608 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25609 DINFO_USAGE_DIR_USE))
25610 return;
25611 gen_type_die_for_member (type, decl,
25612 get_context_die (TYPE_CONTEXT (type)));
25614 if (TREE_CODE (decl) == NAMELIST_DECL)
25615 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25616 get_context_die (DECL_CONTEXT (decl)),
25617 NULL_TREE);
25618 else
25619 at_import_die = force_decl_die (decl);
25623 if (TREE_CODE (decl) == NAMESPACE_DECL)
25625 if (dwarf_version >= 3 || !dwarf_strict)
25626 imported_die = new_die (DW_TAG_imported_module,
25627 lexical_block_die,
25628 lexical_block);
25629 else
25630 return;
25632 else
25633 imported_die = new_die (DW_TAG_imported_declaration,
25634 lexical_block_die,
25635 lexical_block);
25637 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25638 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25639 if (debug_column_info && xloc.column)
25640 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25641 if (name)
25642 add_AT_string (imported_die, DW_AT_name,
25643 IDENTIFIER_POINTER (name));
25644 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25647 /* Output debug information for imported module or decl DECL.
25648 NAME is non-NULL name in context if the decl has been renamed.
25649 CHILD is true if decl is one of the renamed decls as part of
25650 importing whole module.
25651 IMPLICIT is set if this hook is called for an implicit import
25652 such as inline namespace. */
25654 static void
25655 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25656 bool child, bool implicit)
25658 /* dw_die_ref at_import_die; */
25659 dw_die_ref scope_die;
25661 if (debug_info_level <= DINFO_LEVEL_TERSE)
25662 return;
25664 gcc_assert (decl);
25666 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25667 should be enough, for DWARF4 and older even if we emit as extension
25668 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25669 for the benefit of consumers unaware of DW_AT_export_symbols. */
25670 if (implicit
25671 && dwarf_version >= 5
25672 && lang_hooks.decls.decl_dwarf_attribute (decl,
25673 DW_AT_export_symbols) == 1)
25674 return;
25676 set_early_dwarf s;
25678 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25679 We need decl DIE for reference and scope die. First, get DIE for the decl
25680 itself. */
25682 /* Get the scope die for decl context. Use comp_unit_die for global module
25683 or decl. If die is not found for non globals, force new die. */
25684 if (context
25685 && TYPE_P (context)
25686 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25687 return;
25689 scope_die = get_context_die (context);
25691 if (child)
25693 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25694 there is nothing we can do, here. */
25695 if (dwarf_version < 3 && dwarf_strict)
25696 return;
25698 gcc_assert (scope_die->die_child);
25699 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25700 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25701 scope_die = scope_die->die_child;
25704 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25705 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25708 /* Output debug information for namelists. */
25710 static dw_die_ref
25711 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25713 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25714 tree value;
25715 unsigned i;
25717 if (debug_info_level <= DINFO_LEVEL_TERSE)
25718 return NULL;
25720 gcc_assert (scope_die != NULL);
25721 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25722 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25724 /* If there are no item_decls, we have a nondefining namelist, e.g.
25725 with USE association; hence, set DW_AT_declaration. */
25726 if (item_decls == NULL_TREE)
25728 add_AT_flag (nml_die, DW_AT_declaration, 1);
25729 return nml_die;
25732 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25734 nml_item_ref_die = lookup_decl_die (value);
25735 if (!nml_item_ref_die)
25736 nml_item_ref_die = force_decl_die (value);
25738 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25739 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25741 return nml_die;
25745 /* Write the debugging output for DECL and return the DIE. */
25747 static void
25748 dwarf2out_decl (tree decl)
25750 dw_die_ref context_die = comp_unit_die ();
25752 switch (TREE_CODE (decl))
25754 case ERROR_MARK:
25755 return;
25757 case FUNCTION_DECL:
25758 /* If we're a nested function, initially use a parent of NULL; if we're
25759 a plain function, this will be fixed up in decls_for_scope. If
25760 we're a method, it will be ignored, since we already have a DIE. */
25761 if (decl_function_context (decl)
25762 /* But if we're in terse mode, we don't care about scope. */
25763 && debug_info_level > DINFO_LEVEL_TERSE)
25764 context_die = NULL;
25765 break;
25767 case VAR_DECL:
25768 /* For local statics lookup proper context die. */
25769 if (local_function_static (decl))
25770 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25772 /* If we are in terse mode, don't generate any DIEs to represent any
25773 variable declarations or definitions. */
25774 if (debug_info_level <= DINFO_LEVEL_TERSE)
25775 return;
25776 break;
25778 case CONST_DECL:
25779 if (debug_info_level <= DINFO_LEVEL_TERSE)
25780 return;
25781 if (!is_fortran () && !is_ada ())
25782 return;
25783 if (TREE_STATIC (decl) && decl_function_context (decl))
25784 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25785 break;
25787 case NAMESPACE_DECL:
25788 case IMPORTED_DECL:
25789 if (debug_info_level <= DINFO_LEVEL_TERSE)
25790 return;
25791 if (lookup_decl_die (decl) != NULL)
25792 return;
25793 break;
25795 case TYPE_DECL:
25796 /* Don't emit stubs for types unless they are needed by other DIEs. */
25797 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25798 return;
25800 /* Don't bother trying to generate any DIEs to represent any of the
25801 normal built-in types for the language we are compiling. */
25802 if (DECL_IS_BUILTIN (decl))
25803 return;
25805 /* If we are in terse mode, don't generate any DIEs for types. */
25806 if (debug_info_level <= DINFO_LEVEL_TERSE)
25807 return;
25809 /* If we're a function-scope tag, initially use a parent of NULL;
25810 this will be fixed up in decls_for_scope. */
25811 if (decl_function_context (decl))
25812 context_die = NULL;
25814 break;
25816 case NAMELIST_DECL:
25817 break;
25819 default:
25820 return;
25823 gen_decl_die (decl, NULL, NULL, context_die);
25825 if (flag_checking)
25827 dw_die_ref die = lookup_decl_die (decl);
25828 if (die)
25829 check_die (die);
25833 /* Write the debugging output for DECL. */
25835 static void
25836 dwarf2out_function_decl (tree decl)
25838 dwarf2out_decl (decl);
25839 call_arg_locations = NULL;
25840 call_arg_loc_last = NULL;
25841 call_site_count = -1;
25842 tail_call_site_count = -1;
25843 decl_loc_table->empty ();
25844 cached_dw_loc_list_table->empty ();
25847 /* Output a marker (i.e. a label) for the beginning of the generated code for
25848 a lexical block. */
25850 static void
25851 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25852 unsigned int blocknum)
25854 switch_to_section (current_function_section ());
25855 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25858 /* Output a marker (i.e. a label) for the end of the generated code for a
25859 lexical block. */
25861 static void
25862 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25864 switch_to_section (current_function_section ());
25865 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25868 /* Returns nonzero if it is appropriate not to emit any debugging
25869 information for BLOCK, because it doesn't contain any instructions.
25871 Don't allow this for blocks with nested functions or local classes
25872 as we would end up with orphans, and in the presence of scheduling
25873 we may end up calling them anyway. */
25875 static bool
25876 dwarf2out_ignore_block (const_tree block)
25878 tree decl;
25879 unsigned int i;
25881 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25882 if (TREE_CODE (decl) == FUNCTION_DECL
25883 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25884 return 0;
25885 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25887 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25888 if (TREE_CODE (decl) == FUNCTION_DECL
25889 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25890 return 0;
25893 return 1;
25896 /* Hash table routines for file_hash. */
25898 bool
25899 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25901 return filename_cmp (p1->filename, p2) == 0;
25904 hashval_t
25905 dwarf_file_hasher::hash (dwarf_file_data *p)
25907 return htab_hash_string (p->filename);
25910 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25911 dwarf2out.c) and return its "index". The index of each (known) filename is
25912 just a unique number which is associated with only that one filename. We
25913 need such numbers for the sake of generating labels (in the .debug_sfnames
25914 section) and references to those files numbers (in the .debug_srcinfo
25915 and .debug_macinfo sections). If the filename given as an argument is not
25916 found in our current list, add it to the list and assign it the next
25917 available unique index number. */
25919 static struct dwarf_file_data *
25920 lookup_filename (const char *file_name)
25922 struct dwarf_file_data * created;
25924 if (!file_name)
25925 return NULL;
25927 dwarf_file_data **slot
25928 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25929 INSERT);
25930 if (*slot)
25931 return *slot;
25933 created = ggc_alloc<dwarf_file_data> ();
25934 created->filename = file_name;
25935 created->emitted_number = 0;
25936 *slot = created;
25937 return created;
25940 /* If the assembler will construct the file table, then translate the compiler
25941 internal file table number into the assembler file table number, and emit
25942 a .file directive if we haven't already emitted one yet. The file table
25943 numbers are different because we prune debug info for unused variables and
25944 types, which may include filenames. */
25946 static int
25947 maybe_emit_file (struct dwarf_file_data * fd)
25949 if (! fd->emitted_number)
25951 if (last_emitted_file)
25952 fd->emitted_number = last_emitted_file->emitted_number + 1;
25953 else
25954 fd->emitted_number = 1;
25955 last_emitted_file = fd;
25957 if (DWARF2_ASM_LINE_DEBUG_INFO)
25959 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25960 output_quoted_string (asm_out_file,
25961 remap_debug_filename (fd->filename));
25962 fputc ('\n', asm_out_file);
25966 return fd->emitted_number;
25969 /* Schedule generation of a DW_AT_const_value attribute to DIE.
25970 That generation should happen after function debug info has been
25971 generated. The value of the attribute is the constant value of ARG. */
25973 static void
25974 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
25976 die_arg_entry entry;
25978 if (!die || !arg)
25979 return;
25981 gcc_assert (early_dwarf);
25983 if (!tmpl_value_parm_die_table)
25984 vec_alloc (tmpl_value_parm_die_table, 32);
25986 entry.die = die;
25987 entry.arg = arg;
25988 vec_safe_push (tmpl_value_parm_die_table, entry);
25991 /* Return TRUE if T is an instance of generic type, FALSE
25992 otherwise. */
25994 static bool
25995 generic_type_p (tree t)
25997 if (t == NULL_TREE || !TYPE_P (t))
25998 return false;
25999 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26002 /* Schedule the generation of the generic parameter dies for the
26003 instance of generic type T. The proper generation itself is later
26004 done by gen_scheduled_generic_parms_dies. */
26006 static void
26007 schedule_generic_params_dies_gen (tree t)
26009 if (!generic_type_p (t))
26010 return;
26012 gcc_assert (early_dwarf);
26014 if (!generic_type_instances)
26015 vec_alloc (generic_type_instances, 256);
26017 vec_safe_push (generic_type_instances, t);
26020 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26021 by append_entry_to_tmpl_value_parm_die_table. This function must
26022 be called after function DIEs have been generated. */
26024 static void
26025 gen_remaining_tmpl_value_param_die_attribute (void)
26027 if (tmpl_value_parm_die_table)
26029 unsigned i, j;
26030 die_arg_entry *e;
26032 /* We do this in two phases - first get the cases we can
26033 handle during early-finish, preserving those we cannot
26034 (containing symbolic constants where we don't yet know
26035 whether we are going to output the referenced symbols).
26036 For those we try again at late-finish. */
26037 j = 0;
26038 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26040 if (!tree_add_const_value_attribute (e->die, e->arg))
26042 dw_loc_descr_ref loc = NULL;
26043 if (! early_dwarf
26044 && (dwarf_version >= 5 || !dwarf_strict))
26045 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26046 if (loc)
26047 add_AT_loc (e->die, DW_AT_location, loc);
26048 else
26049 (*tmpl_value_parm_die_table)[j++] = *e;
26052 tmpl_value_parm_die_table->truncate (j);
26056 /* Generate generic parameters DIEs for instances of generic types
26057 that have been previously scheduled by
26058 schedule_generic_params_dies_gen. This function must be called
26059 after all the types of the CU have been laid out. */
26061 static void
26062 gen_scheduled_generic_parms_dies (void)
26064 unsigned i;
26065 tree t;
26067 if (!generic_type_instances)
26068 return;
26070 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26071 if (COMPLETE_TYPE_P (t))
26072 gen_generic_params_dies (t);
26074 generic_type_instances = NULL;
26078 /* Replace DW_AT_name for the decl with name. */
26080 static void
26081 dwarf2out_set_name (tree decl, tree name)
26083 dw_die_ref die;
26084 dw_attr_node *attr;
26085 const char *dname;
26087 die = TYPE_SYMTAB_DIE (decl);
26088 if (!die)
26089 return;
26091 dname = dwarf2_name (name, 0);
26092 if (!dname)
26093 return;
26095 attr = get_AT (die, DW_AT_name);
26096 if (attr)
26098 struct indirect_string_node *node;
26100 node = find_AT_string (dname);
26101 /* replace the string. */
26102 attr->dw_attr_val.v.val_str = node;
26105 else
26106 add_name_attribute (die, dname);
26109 /* True if before or during processing of the first function being emitted. */
26110 static bool in_first_function_p = true;
26111 /* True if loc_note during dwarf2out_var_location call might still be
26112 before first real instruction at address equal to .Ltext0. */
26113 static bool maybe_at_text_label_p = true;
26114 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26115 static unsigned int first_loclabel_num_not_at_text_label;
26117 /* Called by the final INSN scan whenever we see a var location. We
26118 use it to drop labels in the right places, and throw the location in
26119 our lookup table. */
26121 static void
26122 dwarf2out_var_location (rtx_insn *loc_note)
26124 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26125 struct var_loc_node *newloc;
26126 rtx_insn *next_real, *next_note;
26127 rtx_insn *call_insn = NULL;
26128 static const char *last_label;
26129 static const char *last_postcall_label;
26130 static bool last_in_cold_section_p;
26131 static rtx_insn *expected_next_loc_note;
26132 tree decl;
26133 bool var_loc_p;
26135 if (!NOTE_P (loc_note))
26137 if (CALL_P (loc_note))
26139 call_site_count++;
26140 if (SIBLING_CALL_P (loc_note))
26141 tail_call_site_count++;
26142 if (optimize == 0 && !flag_var_tracking)
26144 /* When the var-tracking pass is not running, there is no note
26145 for indirect calls whose target is compile-time known. In this
26146 case, process such calls specifically so that we generate call
26147 sites for them anyway. */
26148 rtx x = PATTERN (loc_note);
26149 if (GET_CODE (x) == PARALLEL)
26150 x = XVECEXP (x, 0, 0);
26151 if (GET_CODE (x) == SET)
26152 x = SET_SRC (x);
26153 if (GET_CODE (x) == CALL)
26154 x = XEXP (x, 0);
26155 if (!MEM_P (x)
26156 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26157 || !SYMBOL_REF_DECL (XEXP (x, 0))
26158 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26159 != FUNCTION_DECL))
26161 call_insn = loc_note;
26162 loc_note = NULL;
26163 var_loc_p = false;
26165 next_real = next_real_insn (call_insn);
26166 next_note = NULL;
26167 cached_next_real_insn = NULL;
26168 goto create_label;
26172 return;
26175 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26176 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26177 return;
26179 /* Optimize processing a large consecutive sequence of location
26180 notes so we don't spend too much time in next_real_insn. If the
26181 next insn is another location note, remember the next_real_insn
26182 calculation for next time. */
26183 next_real = cached_next_real_insn;
26184 if (next_real)
26186 if (expected_next_loc_note != loc_note)
26187 next_real = NULL;
26190 next_note = NEXT_INSN (loc_note);
26191 if (! next_note
26192 || next_note->deleted ()
26193 || ! NOTE_P (next_note)
26194 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26195 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26196 next_note = NULL;
26198 if (! next_real)
26199 next_real = next_real_insn (loc_note);
26201 if (next_note)
26203 expected_next_loc_note = next_note;
26204 cached_next_real_insn = next_real;
26206 else
26207 cached_next_real_insn = NULL;
26209 /* If there are no instructions which would be affected by this note,
26210 don't do anything. */
26211 if (var_loc_p
26212 && next_real == NULL_RTX
26213 && !NOTE_DURING_CALL_P (loc_note))
26214 return;
26216 create_label:
26218 if (next_real == NULL_RTX)
26219 next_real = get_last_insn ();
26221 /* If there were any real insns between note we processed last time
26222 and this note (or if it is the first note), clear
26223 last_{,postcall_}label so that they are not reused this time. */
26224 if (last_var_location_insn == NULL_RTX
26225 || last_var_location_insn != next_real
26226 || last_in_cold_section_p != in_cold_section_p)
26228 last_label = NULL;
26229 last_postcall_label = NULL;
26232 if (var_loc_p)
26234 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26235 newloc = add_var_loc_to_decl (decl, loc_note,
26236 NOTE_DURING_CALL_P (loc_note)
26237 ? last_postcall_label : last_label);
26238 if (newloc == NULL)
26239 return;
26241 else
26243 decl = NULL_TREE;
26244 newloc = NULL;
26247 /* If there were no real insns between note we processed last time
26248 and this note, use the label we emitted last time. Otherwise
26249 create a new label and emit it. */
26250 if (last_label == NULL)
26252 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26253 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26254 loclabel_num++;
26255 last_label = ggc_strdup (loclabel);
26256 /* See if loclabel might be equal to .Ltext0. If yes,
26257 bump first_loclabel_num_not_at_text_label. */
26258 if (!have_multiple_function_sections
26259 && in_first_function_p
26260 && maybe_at_text_label_p)
26262 static rtx_insn *last_start;
26263 rtx_insn *insn;
26264 for (insn = loc_note; insn; insn = previous_insn (insn))
26265 if (insn == last_start)
26266 break;
26267 else if (!NONDEBUG_INSN_P (insn))
26268 continue;
26269 else
26271 rtx body = PATTERN (insn);
26272 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26273 continue;
26274 /* Inline asm could occupy zero bytes. */
26275 else if (GET_CODE (body) == ASM_INPUT
26276 || asm_noperands (body) >= 0)
26277 continue;
26278 #ifdef HAVE_attr_length
26279 else if (get_attr_min_length (insn) == 0)
26280 continue;
26281 #endif
26282 else
26284 /* Assume insn has non-zero length. */
26285 maybe_at_text_label_p = false;
26286 break;
26289 if (maybe_at_text_label_p)
26291 last_start = loc_note;
26292 first_loclabel_num_not_at_text_label = loclabel_num;
26297 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26298 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26300 if (!var_loc_p)
26302 struct call_arg_loc_node *ca_loc
26303 = ggc_cleared_alloc<call_arg_loc_node> ();
26304 rtx_insn *prev
26305 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26307 ca_loc->call_arg_loc_note = loc_note;
26308 ca_loc->next = NULL;
26309 ca_loc->label = last_label;
26310 gcc_assert (prev
26311 && (CALL_P (prev)
26312 || (NONJUMP_INSN_P (prev)
26313 && GET_CODE (PATTERN (prev)) == SEQUENCE
26314 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26315 if (!CALL_P (prev))
26316 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26317 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26319 /* Look for a SYMBOL_REF in the "prev" instruction. */
26320 rtx x = get_call_rtx_from (PATTERN (prev));
26321 if (x)
26323 /* Try to get the call symbol, if any. */
26324 if (MEM_P (XEXP (x, 0)))
26325 x = XEXP (x, 0);
26326 /* First, look for a memory access to a symbol_ref. */
26327 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26328 && SYMBOL_REF_DECL (XEXP (x, 0))
26329 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26330 ca_loc->symbol_ref = XEXP (x, 0);
26331 /* Otherwise, look at a compile-time known user-level function
26332 declaration. */
26333 else if (MEM_P (x)
26334 && MEM_EXPR (x)
26335 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26336 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26339 ca_loc->block = insn_scope (prev);
26340 if (call_arg_locations)
26341 call_arg_loc_last->next = ca_loc;
26342 else
26343 call_arg_locations = ca_loc;
26344 call_arg_loc_last = ca_loc;
26346 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26347 newloc->label = last_label;
26348 else
26350 if (!last_postcall_label)
26352 sprintf (loclabel, "%s-1", last_label);
26353 last_postcall_label = ggc_strdup (loclabel);
26355 newloc->label = last_postcall_label;
26358 last_var_location_insn = next_real;
26359 last_in_cold_section_p = in_cold_section_p;
26362 /* Called from finalize_size_functions for size functions so that their body
26363 can be encoded in the debug info to describe the layout of variable-length
26364 structures. */
26366 static void
26367 dwarf2out_size_function (tree decl)
26369 function_to_dwarf_procedure (decl);
26372 /* Note in one location list that text section has changed. */
26375 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26377 var_loc_list *list = *slot;
26378 if (list->first)
26379 list->last_before_switch
26380 = list->last->next ? list->last->next : list->last;
26381 return 1;
26384 /* Note in all location lists that text section has changed. */
26386 static void
26387 var_location_switch_text_section (void)
26389 if (decl_loc_table == NULL)
26390 return;
26392 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26395 /* Create a new line number table. */
26397 static dw_line_info_table *
26398 new_line_info_table (void)
26400 dw_line_info_table *table;
26402 table = ggc_cleared_alloc<dw_line_info_table> ();
26403 table->file_num = 1;
26404 table->line_num = 1;
26405 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26407 return table;
26410 /* Lookup the "current" table into which we emit line info, so
26411 that we don't have to do it for every source line. */
26413 static void
26414 set_cur_line_info_table (section *sec)
26416 dw_line_info_table *table;
26418 if (sec == text_section)
26419 table = text_section_line_info;
26420 else if (sec == cold_text_section)
26422 table = cold_text_section_line_info;
26423 if (!table)
26425 cold_text_section_line_info = table = new_line_info_table ();
26426 table->end_label = cold_end_label;
26429 else
26431 const char *end_label;
26433 if (crtl->has_bb_partition)
26435 if (in_cold_section_p)
26436 end_label = crtl->subsections.cold_section_end_label;
26437 else
26438 end_label = crtl->subsections.hot_section_end_label;
26440 else
26442 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26443 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26444 current_function_funcdef_no);
26445 end_label = ggc_strdup (label);
26448 table = new_line_info_table ();
26449 table->end_label = end_label;
26451 vec_safe_push (separate_line_info, table);
26454 if (DWARF2_ASM_LINE_DEBUG_INFO)
26455 table->is_stmt = (cur_line_info_table
26456 ? cur_line_info_table->is_stmt
26457 : DWARF_LINE_DEFAULT_IS_STMT_START);
26458 cur_line_info_table = table;
26462 /* We need to reset the locations at the beginning of each
26463 function. We can't do this in the end_function hook, because the
26464 declarations that use the locations won't have been output when
26465 that hook is called. Also compute have_multiple_function_sections here. */
26467 static void
26468 dwarf2out_begin_function (tree fun)
26470 section *sec = function_section (fun);
26472 if (sec != text_section)
26473 have_multiple_function_sections = true;
26475 if (crtl->has_bb_partition && !cold_text_section)
26477 gcc_assert (current_function_decl == fun);
26478 cold_text_section = unlikely_text_section ();
26479 switch_to_section (cold_text_section);
26480 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26481 switch_to_section (sec);
26484 dwarf2out_note_section_used ();
26485 call_site_count = 0;
26486 tail_call_site_count = 0;
26488 set_cur_line_info_table (sec);
26491 /* Helper function of dwarf2out_end_function, called only after emitting
26492 the very first function into assembly. Check if some .debug_loc range
26493 might end with a .LVL* label that could be equal to .Ltext0.
26494 In that case we must force using absolute addresses in .debug_loc ranges,
26495 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26496 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26497 list terminator.
26498 Set have_multiple_function_sections to true in that case and
26499 terminate htab traversal. */
26502 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26504 var_loc_list *entry = *slot;
26505 struct var_loc_node *node;
26507 node = entry->first;
26508 if (node && node->next && node->next->label)
26510 unsigned int i;
26511 const char *label = node->next->label;
26512 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26514 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26516 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26517 if (strcmp (label, loclabel) == 0)
26519 have_multiple_function_sections = true;
26520 return 0;
26524 return 1;
26527 /* Hook called after emitting a function into assembly.
26528 This does something only for the very first function emitted. */
26530 static void
26531 dwarf2out_end_function (unsigned int)
26533 if (in_first_function_p
26534 && !have_multiple_function_sections
26535 && first_loclabel_num_not_at_text_label
26536 && decl_loc_table)
26537 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26538 in_first_function_p = false;
26539 maybe_at_text_label_p = false;
26542 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26543 front-ends register a translation unit even before dwarf2out_init is
26544 called. */
26545 static tree main_translation_unit = NULL_TREE;
26547 /* Hook called by front-ends after they built their main translation unit.
26548 Associate comp_unit_die to UNIT. */
26550 static void
26551 dwarf2out_register_main_translation_unit (tree unit)
26553 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26554 && main_translation_unit == NULL_TREE);
26555 main_translation_unit = unit;
26556 /* If dwarf2out_init has not been called yet, it will perform the association
26557 itself looking at main_translation_unit. */
26558 if (decl_die_table != NULL)
26559 equate_decl_number_to_die (unit, comp_unit_die ());
26562 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26564 static void
26565 push_dw_line_info_entry (dw_line_info_table *table,
26566 enum dw_line_info_opcode opcode, unsigned int val)
26568 dw_line_info_entry e;
26569 e.opcode = opcode;
26570 e.val = val;
26571 vec_safe_push (table->entries, e);
26574 /* Output a label to mark the beginning of a source code line entry
26575 and record information relating to this source line, in
26576 'line_info_table' for later output of the .debug_line section. */
26577 /* ??? The discriminator parameter ought to be unsigned. */
26579 static void
26580 dwarf2out_source_line (unsigned int line, unsigned int column,
26581 const char *filename,
26582 int discriminator, bool is_stmt)
26584 unsigned int file_num;
26585 dw_line_info_table *table;
26587 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26588 return;
26590 /* The discriminator column was added in dwarf4. Simplify the below
26591 by simply removing it if we're not supposed to output it. */
26592 if (dwarf_version < 4 && dwarf_strict)
26593 discriminator = 0;
26595 if (!debug_column_info)
26596 column = 0;
26598 table = cur_line_info_table;
26599 file_num = maybe_emit_file (lookup_filename (filename));
26601 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26602 the debugger has used the second (possibly duplicate) line number
26603 at the beginning of the function to mark the end of the prologue.
26604 We could eliminate any other duplicates within the function. For
26605 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26606 that second line number entry. */
26607 /* Recall that this end-of-prologue indication is *not* the same thing
26608 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26609 to which the hook corresponds, follows the last insn that was
26610 emitted by gen_prologue. What we need is to precede the first insn
26611 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26612 insn that corresponds to something the user wrote. These may be
26613 very different locations once scheduling is enabled. */
26615 if (0 && file_num == table->file_num
26616 && line == table->line_num
26617 && column == table->column_num
26618 && discriminator == table->discrim_num
26619 && is_stmt == table->is_stmt)
26620 return;
26622 switch_to_section (current_function_section ());
26624 /* If requested, emit something human-readable. */
26625 if (flag_debug_asm)
26627 if (debug_column_info)
26628 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26629 filename, line, column);
26630 else
26631 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26632 filename, line);
26635 if (DWARF2_ASM_LINE_DEBUG_INFO)
26637 /* Emit the .loc directive understood by GNU as. */
26638 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26639 file_num, line, is_stmt, discriminator */
26640 fputs ("\t.loc ", asm_out_file);
26641 fprint_ul (asm_out_file, file_num);
26642 putc (' ', asm_out_file);
26643 fprint_ul (asm_out_file, line);
26644 putc (' ', asm_out_file);
26645 if (debug_column_info)
26646 fprint_ul (asm_out_file, column);
26647 else
26648 putc ('0', asm_out_file);
26650 if (is_stmt != table->is_stmt)
26652 fputs (" is_stmt ", asm_out_file);
26653 putc (is_stmt ? '1' : '0', asm_out_file);
26655 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26657 gcc_assert (discriminator > 0);
26658 fputs (" discriminator ", asm_out_file);
26659 fprint_ul (asm_out_file, (unsigned long) discriminator);
26661 putc ('\n', asm_out_file);
26663 else
26665 unsigned int label_num = ++line_info_label_num;
26667 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26669 push_dw_line_info_entry (table, LI_set_address, label_num);
26670 if (file_num != table->file_num)
26671 push_dw_line_info_entry (table, LI_set_file, file_num);
26672 if (discriminator != table->discrim_num)
26673 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26674 if (is_stmt != table->is_stmt)
26675 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26676 push_dw_line_info_entry (table, LI_set_line, line);
26677 if (debug_column_info)
26678 push_dw_line_info_entry (table, LI_set_column, column);
26681 table->file_num = file_num;
26682 table->line_num = line;
26683 table->column_num = column;
26684 table->discrim_num = discriminator;
26685 table->is_stmt = is_stmt;
26686 table->in_use = true;
26689 /* Record the beginning of a new source file. */
26691 static void
26692 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26694 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26696 macinfo_entry e;
26697 e.code = DW_MACINFO_start_file;
26698 e.lineno = lineno;
26699 e.info = ggc_strdup (filename);
26700 vec_safe_push (macinfo_table, e);
26704 /* Record the end of a source file. */
26706 static void
26707 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26709 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26711 macinfo_entry e;
26712 e.code = DW_MACINFO_end_file;
26713 e.lineno = lineno;
26714 e.info = NULL;
26715 vec_safe_push (macinfo_table, e);
26719 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26720 the tail part of the directive line, i.e. the part which is past the
26721 initial whitespace, #, whitespace, directive-name, whitespace part. */
26723 static void
26724 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26725 const char *buffer ATTRIBUTE_UNUSED)
26727 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26729 macinfo_entry e;
26730 /* Insert a dummy first entry to be able to optimize the whole
26731 predefined macro block using DW_MACRO_import. */
26732 if (macinfo_table->is_empty () && lineno <= 1)
26734 e.code = 0;
26735 e.lineno = 0;
26736 e.info = NULL;
26737 vec_safe_push (macinfo_table, e);
26739 e.code = DW_MACINFO_define;
26740 e.lineno = lineno;
26741 e.info = ggc_strdup (buffer);
26742 vec_safe_push (macinfo_table, e);
26746 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26747 the tail part of the directive line, i.e. the part which is past the
26748 initial whitespace, #, whitespace, directive-name, whitespace part. */
26750 static void
26751 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26752 const char *buffer ATTRIBUTE_UNUSED)
26754 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26756 macinfo_entry e;
26757 /* Insert a dummy first entry to be able to optimize the whole
26758 predefined macro block using DW_MACRO_import. */
26759 if (macinfo_table->is_empty () && lineno <= 1)
26761 e.code = 0;
26762 e.lineno = 0;
26763 e.info = NULL;
26764 vec_safe_push (macinfo_table, e);
26766 e.code = DW_MACINFO_undef;
26767 e.lineno = lineno;
26768 e.info = ggc_strdup (buffer);
26769 vec_safe_push (macinfo_table, e);
26773 /* Helpers to manipulate hash table of CUs. */
26775 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26777 static inline hashval_t hash (const macinfo_entry *);
26778 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26781 inline hashval_t
26782 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26784 return htab_hash_string (entry->info);
26787 inline bool
26788 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26789 const macinfo_entry *entry2)
26791 return !strcmp (entry1->info, entry2->info);
26794 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26796 /* Output a single .debug_macinfo entry. */
26798 static void
26799 output_macinfo_op (macinfo_entry *ref)
26801 int file_num;
26802 size_t len;
26803 struct indirect_string_node *node;
26804 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26805 struct dwarf_file_data *fd;
26807 switch (ref->code)
26809 case DW_MACINFO_start_file:
26810 fd = lookup_filename (ref->info);
26811 file_num = maybe_emit_file (fd);
26812 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26813 dw2_asm_output_data_uleb128 (ref->lineno,
26814 "Included from line number %lu",
26815 (unsigned long) ref->lineno);
26816 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26817 break;
26818 case DW_MACINFO_end_file:
26819 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26820 break;
26821 case DW_MACINFO_define:
26822 case DW_MACINFO_undef:
26823 len = strlen (ref->info) + 1;
26824 if (!dwarf_strict
26825 && len > DWARF_OFFSET_SIZE
26826 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26827 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26829 ref->code = ref->code == DW_MACINFO_define
26830 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26831 output_macinfo_op (ref);
26832 return;
26834 dw2_asm_output_data (1, ref->code,
26835 ref->code == DW_MACINFO_define
26836 ? "Define macro" : "Undefine macro");
26837 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26838 (unsigned long) ref->lineno);
26839 dw2_asm_output_nstring (ref->info, -1, "The macro");
26840 break;
26841 case DW_MACRO_define_strp:
26842 case DW_MACRO_undef_strp:
26843 node = find_AT_string (ref->info);
26844 gcc_assert (node
26845 && (node->form == DW_FORM_strp
26846 || node->form == DW_FORM_GNU_str_index));
26847 dw2_asm_output_data (1, ref->code,
26848 ref->code == DW_MACRO_define_strp
26849 ? "Define macro strp"
26850 : "Undefine macro strp");
26851 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26852 (unsigned long) ref->lineno);
26853 if (node->form == DW_FORM_strp)
26854 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26855 debug_str_section, "The macro: \"%s\"",
26856 ref->info);
26857 else
26858 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26859 ref->info);
26860 break;
26861 case DW_MACRO_import:
26862 dw2_asm_output_data (1, ref->code, "Import");
26863 ASM_GENERATE_INTERNAL_LABEL (label,
26864 DEBUG_MACRO_SECTION_LABEL,
26865 ref->lineno + macinfo_label_base);
26866 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26867 break;
26868 default:
26869 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26870 ASM_COMMENT_START, (unsigned long) ref->code);
26871 break;
26875 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26876 other compilation unit .debug_macinfo sections. IDX is the first
26877 index of a define/undef, return the number of ops that should be
26878 emitted in a comdat .debug_macinfo section and emit
26879 a DW_MACRO_import entry referencing it.
26880 If the define/undef entry should be emitted normally, return 0. */
26882 static unsigned
26883 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26884 macinfo_hash_type **macinfo_htab)
26886 macinfo_entry *first, *second, *cur, *inc;
26887 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26888 unsigned char checksum[16];
26889 struct md5_ctx ctx;
26890 char *grp_name, *tail;
26891 const char *base;
26892 unsigned int i, count, encoded_filename_len, linebuf_len;
26893 macinfo_entry **slot;
26895 first = &(*macinfo_table)[idx];
26896 second = &(*macinfo_table)[idx + 1];
26898 /* Optimize only if there are at least two consecutive define/undef ops,
26899 and either all of them are before first DW_MACINFO_start_file
26900 with lineno {0,1} (i.e. predefined macro block), or all of them are
26901 in some included header file. */
26902 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26903 return 0;
26904 if (vec_safe_is_empty (files))
26906 if (first->lineno > 1 || second->lineno > 1)
26907 return 0;
26909 else if (first->lineno == 0)
26910 return 0;
26912 /* Find the last define/undef entry that can be grouped together
26913 with first and at the same time compute md5 checksum of their
26914 codes, linenumbers and strings. */
26915 md5_init_ctx (&ctx);
26916 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26917 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26918 break;
26919 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26920 break;
26921 else
26923 unsigned char code = cur->code;
26924 md5_process_bytes (&code, 1, &ctx);
26925 checksum_uleb128 (cur->lineno, &ctx);
26926 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26928 md5_finish_ctx (&ctx, checksum);
26929 count = i - idx;
26931 /* From the containing include filename (if any) pick up just
26932 usable characters from its basename. */
26933 if (vec_safe_is_empty (files))
26934 base = "";
26935 else
26936 base = lbasename (files->last ().info);
26937 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26938 if (ISIDNUM (base[i]) || base[i] == '.')
26939 encoded_filename_len++;
26940 /* Count . at the end. */
26941 if (encoded_filename_len)
26942 encoded_filename_len++;
26944 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26945 linebuf_len = strlen (linebuf);
26947 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26948 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26949 + 16 * 2 + 1);
26950 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26951 tail = grp_name + 4;
26952 if (encoded_filename_len)
26954 for (i = 0; base[i]; i++)
26955 if (ISIDNUM (base[i]) || base[i] == '.')
26956 *tail++ = base[i];
26957 *tail++ = '.';
26959 memcpy (tail, linebuf, linebuf_len);
26960 tail += linebuf_len;
26961 *tail++ = '.';
26962 for (i = 0; i < 16; i++)
26963 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26965 /* Construct a macinfo_entry for DW_MACRO_import
26966 in the empty vector entry before the first define/undef. */
26967 inc = &(*macinfo_table)[idx - 1];
26968 inc->code = DW_MACRO_import;
26969 inc->lineno = 0;
26970 inc->info = ggc_strdup (grp_name);
26971 if (!*macinfo_htab)
26972 *macinfo_htab = new macinfo_hash_type (10);
26973 /* Avoid emitting duplicates. */
26974 slot = (*macinfo_htab)->find_slot (inc, INSERT);
26975 if (*slot != NULL)
26977 inc->code = 0;
26978 inc->info = NULL;
26979 /* If such an entry has been used before, just emit
26980 a DW_MACRO_import op. */
26981 inc = *slot;
26982 output_macinfo_op (inc);
26983 /* And clear all macinfo_entry in the range to avoid emitting them
26984 in the second pass. */
26985 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
26987 cur->code = 0;
26988 cur->info = NULL;
26991 else
26993 *slot = inc;
26994 inc->lineno = (*macinfo_htab)->elements ();
26995 output_macinfo_op (inc);
26997 return count;
27000 /* Save any strings needed by the macinfo table in the debug str
27001 table. All strings must be collected into the table by the time
27002 index_string is called. */
27004 static void
27005 save_macinfo_strings (void)
27007 unsigned len;
27008 unsigned i;
27009 macinfo_entry *ref;
27011 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27013 switch (ref->code)
27015 /* Match the logic in output_macinfo_op to decide on
27016 indirect strings. */
27017 case DW_MACINFO_define:
27018 case DW_MACINFO_undef:
27019 len = strlen (ref->info) + 1;
27020 if (!dwarf_strict
27021 && len > DWARF_OFFSET_SIZE
27022 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27023 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27024 set_indirect_string (find_AT_string (ref->info));
27025 break;
27026 case DW_MACRO_define_strp:
27027 case DW_MACRO_undef_strp:
27028 set_indirect_string (find_AT_string (ref->info));
27029 break;
27030 default:
27031 break;
27036 /* Output macinfo section(s). */
27038 static void
27039 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27041 unsigned i;
27042 unsigned long length = vec_safe_length (macinfo_table);
27043 macinfo_entry *ref;
27044 vec<macinfo_entry, va_gc> *files = NULL;
27045 macinfo_hash_type *macinfo_htab = NULL;
27046 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27048 if (! length)
27049 return;
27051 /* output_macinfo* uses these interchangeably. */
27052 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27053 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27054 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27055 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27057 /* AIX Assembler inserts the length, so adjust the reference to match the
27058 offset expected by debuggers. */
27059 strcpy (dl_section_ref, debug_line_label);
27060 if (XCOFF_DEBUGGING_INFO)
27061 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27063 /* For .debug_macro emit the section header. */
27064 if (!dwarf_strict || dwarf_version >= 5)
27066 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27067 "DWARF macro version number");
27068 if (DWARF_OFFSET_SIZE == 8)
27069 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27070 else
27071 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27072 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27073 debug_line_section, NULL);
27076 /* In the first loop, it emits the primary .debug_macinfo section
27077 and after each emitted op the macinfo_entry is cleared.
27078 If a longer range of define/undef ops can be optimized using
27079 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27080 the vector before the first define/undef in the range and the
27081 whole range of define/undef ops is not emitted and kept. */
27082 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27084 switch (ref->code)
27086 case DW_MACINFO_start_file:
27087 vec_safe_push (files, *ref);
27088 break;
27089 case DW_MACINFO_end_file:
27090 if (!vec_safe_is_empty (files))
27091 files->pop ();
27092 break;
27093 case DW_MACINFO_define:
27094 case DW_MACINFO_undef:
27095 if ((!dwarf_strict || dwarf_version >= 5)
27096 && HAVE_COMDAT_GROUP
27097 && vec_safe_length (files) != 1
27098 && i > 0
27099 && i + 1 < length
27100 && (*macinfo_table)[i - 1].code == 0)
27102 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27103 if (count)
27105 i += count - 1;
27106 continue;
27109 break;
27110 case 0:
27111 /* A dummy entry may be inserted at the beginning to be able
27112 to optimize the whole block of predefined macros. */
27113 if (i == 0)
27114 continue;
27115 default:
27116 break;
27118 output_macinfo_op (ref);
27119 ref->info = NULL;
27120 ref->code = 0;
27123 if (!macinfo_htab)
27124 return;
27126 /* Save the number of transparent includes so we can adjust the
27127 label number for the fat LTO object DWARF. */
27128 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27130 delete macinfo_htab;
27131 macinfo_htab = NULL;
27133 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27134 terminate the current chain and switch to a new comdat .debug_macinfo
27135 section and emit the define/undef entries within it. */
27136 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27137 switch (ref->code)
27139 case 0:
27140 continue;
27141 case DW_MACRO_import:
27143 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27144 tree comdat_key = get_identifier (ref->info);
27145 /* Terminate the previous .debug_macinfo section. */
27146 dw2_asm_output_data (1, 0, "End compilation unit");
27147 targetm.asm_out.named_section (debug_macinfo_section_name,
27148 SECTION_DEBUG
27149 | SECTION_LINKONCE
27150 | (early_lto_debug
27151 ? SECTION_EXCLUDE : 0),
27152 comdat_key);
27153 ASM_GENERATE_INTERNAL_LABEL (label,
27154 DEBUG_MACRO_SECTION_LABEL,
27155 ref->lineno + macinfo_label_base);
27156 ASM_OUTPUT_LABEL (asm_out_file, label);
27157 ref->code = 0;
27158 ref->info = NULL;
27159 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27160 "DWARF macro version number");
27161 if (DWARF_OFFSET_SIZE == 8)
27162 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27163 else
27164 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27166 break;
27167 case DW_MACINFO_define:
27168 case DW_MACINFO_undef:
27169 output_macinfo_op (ref);
27170 ref->code = 0;
27171 ref->info = NULL;
27172 break;
27173 default:
27174 gcc_unreachable ();
27177 macinfo_label_base += macinfo_label_base_adj;
27180 /* Initialize the various sections and labels for dwarf output and prefix
27181 them with PREFIX if non-NULL. */
27183 static void
27184 init_sections_and_labels (bool early_lto_debug)
27186 /* As we may get called multiple times have a generation count for labels. */
27187 static unsigned generation = 0;
27189 if (early_lto_debug)
27191 if (!dwarf_split_debug_info)
27193 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27194 SECTION_DEBUG | SECTION_EXCLUDE,
27195 NULL);
27196 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27197 SECTION_DEBUG | SECTION_EXCLUDE,
27198 NULL);
27199 debug_macinfo_section_name = ((dwarf_strict && dwarf_version < 5)
27200 ? DEBUG_LTO_MACINFO_SECTION
27201 : DEBUG_LTO_MACRO_SECTION);
27202 debug_macinfo_section = get_section (debug_macinfo_section_name,
27203 SECTION_DEBUG
27204 | SECTION_EXCLUDE, NULL);
27205 /* For macro info we have to refer to a debug_line section, so similar
27206 to split-dwarf emit a skeleton one for early debug. */
27207 debug_skeleton_line_section
27208 = get_section (DEBUG_LTO_LINE_SECTION,
27209 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27210 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27211 DEBUG_SKELETON_LINE_SECTION_LABEL,
27212 generation);
27214 else
27216 /* ??? Which of the following do we need early? */
27217 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27218 SECTION_DEBUG | SECTION_EXCLUDE,
27219 NULL);
27220 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27221 SECTION_DEBUG | SECTION_EXCLUDE,
27222 NULL);
27223 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27224 SECTION_DEBUG
27225 | SECTION_EXCLUDE, NULL);
27226 debug_skeleton_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27227 SECTION_DEBUG
27228 | SECTION_EXCLUDE, NULL);
27229 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27230 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27231 generation);
27233 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27234 the main .o, but the skeleton_line goes into the split off dwo. */
27235 debug_skeleton_line_section
27236 = get_section (DEBUG_LTO_LINE_SECTION,
27237 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27238 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27239 DEBUG_SKELETON_LINE_SECTION_LABEL,
27240 generation);
27241 debug_str_offsets_section
27242 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27243 SECTION_DEBUG | SECTION_EXCLUDE,
27244 NULL);
27245 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27246 DEBUG_SKELETON_INFO_SECTION_LABEL,
27247 generation);
27248 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27249 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27250 debug_macinfo_section_name
27251 = (dwarf_strict
27252 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27253 debug_macinfo_section = get_section (debug_macinfo_section_name,
27254 SECTION_DEBUG | SECTION_EXCLUDE,
27255 NULL);
27257 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27258 DEBUG_STR_SECTION_FLAGS
27259 | SECTION_EXCLUDE, NULL);
27261 else
27263 if (!dwarf_split_debug_info)
27265 debug_info_section = get_section (DEBUG_INFO_SECTION,
27266 SECTION_DEBUG, NULL);
27267 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27268 SECTION_DEBUG, NULL);
27269 debug_loc_section = get_section (DEBUG_LOC_SECTION,
27270 SECTION_DEBUG, NULL);
27271 debug_macinfo_section_name
27272 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27273 debug_macinfo_section = get_section (debug_macinfo_section_name,
27274 SECTION_DEBUG, NULL);
27276 else
27278 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27279 SECTION_DEBUG | SECTION_EXCLUDE,
27280 NULL);
27281 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27282 SECTION_DEBUG | SECTION_EXCLUDE,
27283 NULL);
27284 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27285 SECTION_DEBUG, NULL);
27286 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27287 SECTION_DEBUG, NULL);
27288 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27289 SECTION_DEBUG, NULL);
27290 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27291 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27292 generation);
27294 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27295 stay in the main .o, but the skeleton_line goes into the
27296 split off dwo. */
27297 debug_skeleton_line_section
27298 = get_section (DEBUG_DWO_LINE_SECTION,
27299 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27300 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27301 DEBUG_SKELETON_LINE_SECTION_LABEL,
27302 generation);
27303 debug_str_offsets_section
27304 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27305 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27306 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27307 DEBUG_SKELETON_INFO_SECTION_LABEL,
27308 generation);
27309 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
27310 SECTION_DEBUG | SECTION_EXCLUDE,
27311 NULL);
27312 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27313 DEBUG_STR_DWO_SECTION_FLAGS,
27314 NULL);
27315 debug_macinfo_section_name
27316 = (dwarf_strict && dwarf_version < 5)
27317 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27318 debug_macinfo_section = get_section (debug_macinfo_section_name,
27319 SECTION_DEBUG | SECTION_EXCLUDE,
27320 NULL);
27322 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27323 SECTION_DEBUG, NULL);
27324 debug_line_section = get_section (DEBUG_LINE_SECTION,
27325 SECTION_DEBUG, NULL);
27326 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27327 SECTION_DEBUG, NULL);
27328 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27329 SECTION_DEBUG, NULL);
27330 debug_str_section = get_section (DEBUG_STR_SECTION,
27331 DEBUG_STR_SECTION_FLAGS, NULL);
27332 debug_ranges_section = get_section (dwarf_version >= 5
27333 ? DEBUG_RNGLISTS_SECTION
27334 : DEBUG_RANGES_SECTION,
27335 SECTION_DEBUG, NULL);
27336 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27337 SECTION_DEBUG, NULL);
27340 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27341 DEBUG_ABBREV_SECTION_LABEL, generation);
27342 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27343 DEBUG_INFO_SECTION_LABEL, generation);
27344 info_section_emitted = false;
27345 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27346 DEBUG_LINE_SECTION_LABEL, generation);
27347 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27348 DEBUG_RANGES_SECTION_LABEL, generation);
27349 if (dwarf_version >= 5 && dwarf_split_debug_info)
27350 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27351 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27352 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27353 DEBUG_ADDR_SECTION_LABEL, generation);
27354 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27355 (dwarf_strict && dwarf_version < 5)
27356 ? DEBUG_MACINFO_SECTION_LABEL
27357 : DEBUG_MACRO_SECTION_LABEL, generation);
27358 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27359 generation);
27361 ++generation;
27364 /* Set up for Dwarf output at the start of compilation. */
27366 static void
27367 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27369 /* Allocate the file_table. */
27370 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27372 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27373 /* Allocate the decl_die_table. */
27374 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27376 /* Allocate the decl_loc_table. */
27377 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27379 /* Allocate the cached_dw_loc_list_table. */
27380 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27382 /* Allocate the initial hunk of the decl_scope_table. */
27383 vec_alloc (decl_scope_table, 256);
27385 /* Allocate the initial hunk of the abbrev_die_table. */
27386 vec_alloc (abbrev_die_table, 256);
27387 /* Zero-th entry is allocated, but unused. */
27388 abbrev_die_table->quick_push (NULL);
27390 /* Allocate the dwarf_proc_stack_usage_map. */
27391 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27393 /* Allocate the pubtypes and pubnames vectors. */
27394 vec_alloc (pubname_table, 32);
27395 vec_alloc (pubtype_table, 32);
27397 vec_alloc (incomplete_types, 64);
27399 vec_alloc (used_rtx_array, 32);
27401 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27402 vec_alloc (macinfo_table, 64);
27403 #endif
27405 /* If front-ends already registered a main translation unit but we were not
27406 ready to perform the association, do this now. */
27407 if (main_translation_unit != NULL_TREE)
27408 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27411 /* Called before compile () starts outputtting functions, variables
27412 and toplevel asms into assembly. */
27414 static void
27415 dwarf2out_assembly_start (void)
27417 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27418 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27419 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27420 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27421 COLD_TEXT_SECTION_LABEL, 0);
27422 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27424 switch_to_section (text_section);
27425 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27426 #endif
27428 /* Make sure the line number table for .text always exists. */
27429 text_section_line_info = new_line_info_table ();
27430 text_section_line_info->end_label = text_end_label;
27432 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27433 cur_line_info_table = text_section_line_info;
27434 #endif
27436 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27437 && dwarf2out_do_cfi_asm ()
27438 && (!(flag_unwind_tables || flag_exceptions)
27439 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27440 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27443 /* A helper function for dwarf2out_finish called through
27444 htab_traverse. Assign a string its index. All strings must be
27445 collected into the table by the time index_string is called,
27446 because the indexing code relies on htab_traverse to traverse nodes
27447 in the same order for each run. */
27450 index_string (indirect_string_node **h, unsigned int *index)
27452 indirect_string_node *node = *h;
27454 find_string_form (node);
27455 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27457 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27458 node->index = *index;
27459 *index += 1;
27461 return 1;
27464 /* A helper function for output_indirect_strings called through
27465 htab_traverse. Output the offset to a string and update the
27466 current offset. */
27469 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27471 indirect_string_node *node = *h;
27473 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27475 /* Assert that this node has been assigned an index. */
27476 gcc_assert (node->index != NO_INDEX_ASSIGNED
27477 && node->index != NOT_INDEXED);
27478 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27479 "indexed string 0x%x: %s", node->index, node->str);
27480 *offset += strlen (node->str) + 1;
27482 return 1;
27485 /* A helper function for dwarf2out_finish called through
27486 htab_traverse. Output the indexed string. */
27489 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27491 struct indirect_string_node *node = *h;
27493 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27495 /* Assert that the strings are output in the same order as their
27496 indexes were assigned. */
27497 gcc_assert (*cur_idx == node->index);
27498 assemble_string (node->str, strlen (node->str) + 1);
27499 *cur_idx += 1;
27501 return 1;
27504 /* A helper function for dwarf2out_finish called through
27505 htab_traverse. Emit one queued .debug_str string. */
27508 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27510 struct indirect_string_node *node = *h;
27512 node->form = find_string_form (node);
27513 if (node->form == form && node->refcount > 0)
27515 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27516 assemble_string (node->str, strlen (node->str) + 1);
27519 return 1;
27522 /* Output the indexed string table. */
27524 static void
27525 output_indirect_strings (void)
27527 switch_to_section (debug_str_section);
27528 if (!dwarf_split_debug_info)
27529 debug_str_hash->traverse<enum dwarf_form,
27530 output_indirect_string> (DW_FORM_strp);
27531 else
27533 unsigned int offset = 0;
27534 unsigned int cur_idx = 0;
27536 skeleton_debug_str_hash->traverse<enum dwarf_form,
27537 output_indirect_string> (DW_FORM_strp);
27539 switch_to_section (debug_str_offsets_section);
27540 debug_str_hash->traverse_noresize
27541 <unsigned int *, output_index_string_offset> (&offset);
27542 switch_to_section (debug_str_dwo_section);
27543 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27544 (&cur_idx);
27548 /* Callback for htab_traverse to assign an index to an entry in the
27549 table, and to write that entry to the .debug_addr section. */
27552 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27554 addr_table_entry *entry = *slot;
27556 if (entry->refcount == 0)
27558 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27559 || entry->index == NOT_INDEXED);
27560 return 1;
27563 gcc_assert (entry->index == *cur_index);
27564 (*cur_index)++;
27566 switch (entry->kind)
27568 case ate_kind_rtx:
27569 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27570 "0x%x", entry->index);
27571 break;
27572 case ate_kind_rtx_dtprel:
27573 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27574 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27575 DWARF2_ADDR_SIZE,
27576 entry->addr.rtl);
27577 fputc ('\n', asm_out_file);
27578 break;
27579 case ate_kind_label:
27580 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27581 "0x%x", entry->index);
27582 break;
27583 default:
27584 gcc_unreachable ();
27586 return 1;
27589 /* Produce the .debug_addr section. */
27591 static void
27592 output_addr_table (void)
27594 unsigned int index = 0;
27595 if (addr_index_table == NULL || addr_index_table->size () == 0)
27596 return;
27598 switch_to_section (debug_addr_section);
27599 addr_index_table
27600 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27603 #if ENABLE_ASSERT_CHECKING
27604 /* Verify that all marks are clear. */
27606 static void
27607 verify_marks_clear (dw_die_ref die)
27609 dw_die_ref c;
27611 gcc_assert (! die->die_mark);
27612 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27614 #endif /* ENABLE_ASSERT_CHECKING */
27616 /* Clear the marks for a die and its children.
27617 Be cool if the mark isn't set. */
27619 static void
27620 prune_unmark_dies (dw_die_ref die)
27622 dw_die_ref c;
27624 if (die->die_mark)
27625 die->die_mark = 0;
27626 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27629 /* Given LOC that is referenced by a DIE we're marking as used, find all
27630 referenced DWARF procedures it references and mark them as used. */
27632 static void
27633 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27635 for (; loc != NULL; loc = loc->dw_loc_next)
27636 switch (loc->dw_loc_opc)
27638 case DW_OP_implicit_pointer:
27639 case DW_OP_convert:
27640 case DW_OP_reinterpret:
27641 case DW_OP_GNU_implicit_pointer:
27642 case DW_OP_GNU_convert:
27643 case DW_OP_GNU_reinterpret:
27644 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27645 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27646 break;
27647 case DW_OP_GNU_variable_value:
27648 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27650 dw_die_ref ref
27651 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27652 if (ref == NULL)
27653 break;
27654 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27655 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27656 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27658 /* FALLTHRU */
27659 case DW_OP_call2:
27660 case DW_OP_call4:
27661 case DW_OP_call_ref:
27662 case DW_OP_const_type:
27663 case DW_OP_GNU_const_type:
27664 case DW_OP_GNU_parameter_ref:
27665 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27666 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27667 break;
27668 case DW_OP_regval_type:
27669 case DW_OP_deref_type:
27670 case DW_OP_GNU_regval_type:
27671 case DW_OP_GNU_deref_type:
27672 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27673 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27674 break;
27675 case DW_OP_entry_value:
27676 case DW_OP_GNU_entry_value:
27677 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27678 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27679 break;
27680 default:
27681 break;
27685 /* Given DIE that we're marking as used, find any other dies
27686 it references as attributes and mark them as used. */
27688 static void
27689 prune_unused_types_walk_attribs (dw_die_ref die)
27691 dw_attr_node *a;
27692 unsigned ix;
27694 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27696 switch (AT_class (a))
27698 /* Make sure DWARF procedures referenced by location descriptions will
27699 get emitted. */
27700 case dw_val_class_loc:
27701 prune_unused_types_walk_loc_descr (AT_loc (a));
27702 break;
27703 case dw_val_class_loc_list:
27704 for (dw_loc_list_ref list = AT_loc_list (a);
27705 list != NULL;
27706 list = list->dw_loc_next)
27707 prune_unused_types_walk_loc_descr (list->expr);
27708 break;
27710 case dw_val_class_die_ref:
27711 /* A reference to another DIE.
27712 Make sure that it will get emitted.
27713 If it was broken out into a comdat group, don't follow it. */
27714 if (! AT_ref (a)->comdat_type_p
27715 || a->dw_attr == DW_AT_specification)
27716 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27717 break;
27719 case dw_val_class_str:
27720 /* Set the string's refcount to 0 so that prune_unused_types_mark
27721 accounts properly for it. */
27722 a->dw_attr_val.v.val_str->refcount = 0;
27723 break;
27725 default:
27726 break;
27731 /* Mark the generic parameters and arguments children DIEs of DIE. */
27733 static void
27734 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27736 dw_die_ref c;
27738 if (die == NULL || die->die_child == NULL)
27739 return;
27740 c = die->die_child;
27743 if (is_template_parameter (c))
27744 prune_unused_types_mark (c, 1);
27745 c = c->die_sib;
27746 } while (c && c != die->die_child);
27749 /* Mark DIE as being used. If DOKIDS is true, then walk down
27750 to DIE's children. */
27752 static void
27753 prune_unused_types_mark (dw_die_ref die, int dokids)
27755 dw_die_ref c;
27757 if (die->die_mark == 0)
27759 /* We haven't done this node yet. Mark it as used. */
27760 die->die_mark = 1;
27761 /* If this is the DIE of a generic type instantiation,
27762 mark the children DIEs that describe its generic parms and
27763 args. */
27764 prune_unused_types_mark_generic_parms_dies (die);
27766 /* We also have to mark its parents as used.
27767 (But we don't want to mark our parent's kids due to this,
27768 unless it is a class.) */
27769 if (die->die_parent)
27770 prune_unused_types_mark (die->die_parent,
27771 class_scope_p (die->die_parent));
27773 /* Mark any referenced nodes. */
27774 prune_unused_types_walk_attribs (die);
27776 /* If this node is a specification,
27777 also mark the definition, if it exists. */
27778 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27779 prune_unused_types_mark (die->die_definition, 1);
27782 if (dokids && die->die_mark != 2)
27784 /* We need to walk the children, but haven't done so yet.
27785 Remember that we've walked the kids. */
27786 die->die_mark = 2;
27788 /* If this is an array type, we need to make sure our
27789 kids get marked, even if they're types. If we're
27790 breaking out types into comdat sections, do this
27791 for all type definitions. */
27792 if (die->die_tag == DW_TAG_array_type
27793 || (use_debug_types
27794 && is_type_die (die) && ! is_declaration_die (die)))
27795 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27796 else
27797 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27801 /* For local classes, look if any static member functions were emitted
27802 and if so, mark them. */
27804 static void
27805 prune_unused_types_walk_local_classes (dw_die_ref die)
27807 dw_die_ref c;
27809 if (die->die_mark == 2)
27810 return;
27812 switch (die->die_tag)
27814 case DW_TAG_structure_type:
27815 case DW_TAG_union_type:
27816 case DW_TAG_class_type:
27817 break;
27819 case DW_TAG_subprogram:
27820 if (!get_AT_flag (die, DW_AT_declaration)
27821 || die->die_definition != NULL)
27822 prune_unused_types_mark (die, 1);
27823 return;
27825 default:
27826 return;
27829 /* Mark children. */
27830 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27833 /* Walk the tree DIE and mark types that we actually use. */
27835 static void
27836 prune_unused_types_walk (dw_die_ref die)
27838 dw_die_ref c;
27840 /* Don't do anything if this node is already marked and
27841 children have been marked as well. */
27842 if (die->die_mark == 2)
27843 return;
27845 switch (die->die_tag)
27847 case DW_TAG_structure_type:
27848 case DW_TAG_union_type:
27849 case DW_TAG_class_type:
27850 if (die->die_perennial_p)
27851 break;
27853 for (c = die->die_parent; c; c = c->die_parent)
27854 if (c->die_tag == DW_TAG_subprogram)
27855 break;
27857 /* Finding used static member functions inside of classes
27858 is needed just for local classes, because for other classes
27859 static member function DIEs with DW_AT_specification
27860 are emitted outside of the DW_TAG_*_type. If we ever change
27861 it, we'd need to call this even for non-local classes. */
27862 if (c)
27863 prune_unused_types_walk_local_classes (die);
27865 /* It's a type node --- don't mark it. */
27866 return;
27868 case DW_TAG_const_type:
27869 case DW_TAG_packed_type:
27870 case DW_TAG_pointer_type:
27871 case DW_TAG_reference_type:
27872 case DW_TAG_rvalue_reference_type:
27873 case DW_TAG_volatile_type:
27874 case DW_TAG_typedef:
27875 case DW_TAG_array_type:
27876 case DW_TAG_interface_type:
27877 case DW_TAG_friend:
27878 case DW_TAG_enumeration_type:
27879 case DW_TAG_subroutine_type:
27880 case DW_TAG_string_type:
27881 case DW_TAG_set_type:
27882 case DW_TAG_subrange_type:
27883 case DW_TAG_ptr_to_member_type:
27884 case DW_TAG_file_type:
27885 /* Type nodes are useful only when other DIEs reference them --- don't
27886 mark them. */
27887 /* FALLTHROUGH */
27889 case DW_TAG_dwarf_procedure:
27890 /* Likewise for DWARF procedures. */
27892 if (die->die_perennial_p)
27893 break;
27895 return;
27897 default:
27898 /* Mark everything else. */
27899 break;
27902 if (die->die_mark == 0)
27904 die->die_mark = 1;
27906 /* Now, mark any dies referenced from here. */
27907 prune_unused_types_walk_attribs (die);
27910 die->die_mark = 2;
27912 /* Mark children. */
27913 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27916 /* Increment the string counts on strings referred to from DIE's
27917 attributes. */
27919 static void
27920 prune_unused_types_update_strings (dw_die_ref die)
27922 dw_attr_node *a;
27923 unsigned ix;
27925 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27926 if (AT_class (a) == dw_val_class_str)
27928 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27929 s->refcount++;
27930 /* Avoid unnecessarily putting strings that are used less than
27931 twice in the hash table. */
27932 if (s->refcount
27933 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27935 indirect_string_node **slot
27936 = debug_str_hash->find_slot_with_hash (s->str,
27937 htab_hash_string (s->str),
27938 INSERT);
27939 gcc_assert (*slot == NULL);
27940 *slot = s;
27945 /* Mark DIE and its children as removed. */
27947 static void
27948 mark_removed (dw_die_ref die)
27950 dw_die_ref c;
27951 die->removed = true;
27952 FOR_EACH_CHILD (die, c, mark_removed (c));
27955 /* Remove from the tree DIE any dies that aren't marked. */
27957 static void
27958 prune_unused_types_prune (dw_die_ref die)
27960 dw_die_ref c;
27962 gcc_assert (die->die_mark);
27963 prune_unused_types_update_strings (die);
27965 if (! die->die_child)
27966 return;
27968 c = die->die_child;
27969 do {
27970 dw_die_ref prev = c, next;
27971 for (c = c->die_sib; ! c->die_mark; c = next)
27972 if (c == die->die_child)
27974 /* No marked children between 'prev' and the end of the list. */
27975 if (prev == c)
27976 /* No marked children at all. */
27977 die->die_child = NULL;
27978 else
27980 prev->die_sib = c->die_sib;
27981 die->die_child = prev;
27983 c->die_sib = NULL;
27984 mark_removed (c);
27985 return;
27987 else
27989 next = c->die_sib;
27990 c->die_sib = NULL;
27991 mark_removed (c);
27994 if (c != prev->die_sib)
27995 prev->die_sib = c;
27996 prune_unused_types_prune (c);
27997 } while (c != die->die_child);
28000 /* Remove dies representing declarations that we never use. */
28002 static void
28003 prune_unused_types (void)
28005 unsigned int i;
28006 limbo_die_node *node;
28007 comdat_type_node *ctnode;
28008 pubname_entry *pub;
28009 dw_die_ref base_type;
28011 #if ENABLE_ASSERT_CHECKING
28012 /* All the marks should already be clear. */
28013 verify_marks_clear (comp_unit_die ());
28014 for (node = limbo_die_list; node; node = node->next)
28015 verify_marks_clear (node->die);
28016 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28017 verify_marks_clear (ctnode->root_die);
28018 #endif /* ENABLE_ASSERT_CHECKING */
28020 /* Mark types that are used in global variables. */
28021 premark_types_used_by_global_vars ();
28023 /* Set the mark on nodes that are actually used. */
28024 prune_unused_types_walk (comp_unit_die ());
28025 for (node = limbo_die_list; node; node = node->next)
28026 prune_unused_types_walk (node->die);
28027 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28029 prune_unused_types_walk (ctnode->root_die);
28030 prune_unused_types_mark (ctnode->type_die, 1);
28033 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28034 are unusual in that they are pubnames that are the children of pubtypes.
28035 They should only be marked via their parent DW_TAG_enumeration_type die,
28036 not as roots in themselves. */
28037 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28038 if (pub->die->die_tag != DW_TAG_enumerator)
28039 prune_unused_types_mark (pub->die, 1);
28040 for (i = 0; base_types.iterate (i, &base_type); i++)
28041 prune_unused_types_mark (base_type, 1);
28043 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28044 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28045 callees). */
28046 cgraph_node *cnode;
28047 FOR_EACH_FUNCTION (cnode)
28048 if (cnode->referred_to_p (false))
28050 dw_die_ref die = lookup_decl_die (cnode->decl);
28051 if (die == NULL || die->die_mark)
28052 continue;
28053 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28054 if (e->caller != cnode
28055 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28057 prune_unused_types_mark (die, 1);
28058 break;
28062 if (debug_str_hash)
28063 debug_str_hash->empty ();
28064 if (skeleton_debug_str_hash)
28065 skeleton_debug_str_hash->empty ();
28066 prune_unused_types_prune (comp_unit_die ());
28067 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28069 node = *pnode;
28070 if (!node->die->die_mark)
28071 *pnode = node->next;
28072 else
28074 prune_unused_types_prune (node->die);
28075 pnode = &node->next;
28078 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28079 prune_unused_types_prune (ctnode->root_die);
28081 /* Leave the marks clear. */
28082 prune_unmark_dies (comp_unit_die ());
28083 for (node = limbo_die_list; node; node = node->next)
28084 prune_unmark_dies (node->die);
28085 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28086 prune_unmark_dies (ctnode->root_die);
28089 /* Helpers to manipulate hash table of comdat type units. */
28091 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28093 static inline hashval_t hash (const comdat_type_node *);
28094 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28097 inline hashval_t
28098 comdat_type_hasher::hash (const comdat_type_node *type_node)
28100 hashval_t h;
28101 memcpy (&h, type_node->signature, sizeof (h));
28102 return h;
28105 inline bool
28106 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28107 const comdat_type_node *type_node_2)
28109 return (! memcmp (type_node_1->signature, type_node_2->signature,
28110 DWARF_TYPE_SIGNATURE_SIZE));
28113 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28114 to the location it would have been added, should we know its
28115 DECL_ASSEMBLER_NAME when we added other attributes. This will
28116 probably improve compactness of debug info, removing equivalent
28117 abbrevs, and hide any differences caused by deferring the
28118 computation of the assembler name, triggered by e.g. PCH. */
28120 static inline void
28121 move_linkage_attr (dw_die_ref die)
28123 unsigned ix = vec_safe_length (die->die_attr);
28124 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28126 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28127 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28129 while (--ix > 0)
28131 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28133 if (prev->dw_attr == DW_AT_decl_line
28134 || prev->dw_attr == DW_AT_decl_column
28135 || prev->dw_attr == DW_AT_name)
28136 break;
28139 if (ix != vec_safe_length (die->die_attr) - 1)
28141 die->die_attr->pop ();
28142 die->die_attr->quick_insert (ix, linkage);
28146 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28147 referenced from typed stack ops and count how often they are used. */
28149 static void
28150 mark_base_types (dw_loc_descr_ref loc)
28152 dw_die_ref base_type = NULL;
28154 for (; loc; loc = loc->dw_loc_next)
28156 switch (loc->dw_loc_opc)
28158 case DW_OP_regval_type:
28159 case DW_OP_deref_type:
28160 case DW_OP_GNU_regval_type:
28161 case DW_OP_GNU_deref_type:
28162 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28163 break;
28164 case DW_OP_convert:
28165 case DW_OP_reinterpret:
28166 case DW_OP_GNU_convert:
28167 case DW_OP_GNU_reinterpret:
28168 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28169 continue;
28170 /* FALLTHRU */
28171 case DW_OP_const_type:
28172 case DW_OP_GNU_const_type:
28173 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28174 break;
28175 case DW_OP_entry_value:
28176 case DW_OP_GNU_entry_value:
28177 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28178 continue;
28179 default:
28180 continue;
28182 gcc_assert (base_type->die_parent == comp_unit_die ());
28183 if (base_type->die_mark)
28184 base_type->die_mark++;
28185 else
28187 base_types.safe_push (base_type);
28188 base_type->die_mark = 1;
28193 /* Comparison function for sorting marked base types. */
28195 static int
28196 base_type_cmp (const void *x, const void *y)
28198 dw_die_ref dx = *(const dw_die_ref *) x;
28199 dw_die_ref dy = *(const dw_die_ref *) y;
28200 unsigned int byte_size1, byte_size2;
28201 unsigned int encoding1, encoding2;
28202 unsigned int align1, align2;
28203 if (dx->die_mark > dy->die_mark)
28204 return -1;
28205 if (dx->die_mark < dy->die_mark)
28206 return 1;
28207 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28208 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28209 if (byte_size1 < byte_size2)
28210 return 1;
28211 if (byte_size1 > byte_size2)
28212 return -1;
28213 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28214 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28215 if (encoding1 < encoding2)
28216 return 1;
28217 if (encoding1 > encoding2)
28218 return -1;
28219 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28220 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28221 if (align1 < align2)
28222 return 1;
28223 if (align1 > align2)
28224 return -1;
28225 return 0;
28228 /* Move base types marked by mark_base_types as early as possible
28229 in the CU, sorted by decreasing usage count both to make the
28230 uleb128 references as small as possible and to make sure they
28231 will have die_offset already computed by calc_die_sizes when
28232 sizes of typed stack loc ops is computed. */
28234 static void
28235 move_marked_base_types (void)
28237 unsigned int i;
28238 dw_die_ref base_type, die, c;
28240 if (base_types.is_empty ())
28241 return;
28243 /* Sort by decreasing usage count, they will be added again in that
28244 order later on. */
28245 base_types.qsort (base_type_cmp);
28246 die = comp_unit_die ();
28247 c = die->die_child;
28250 dw_die_ref prev = c;
28251 c = c->die_sib;
28252 while (c->die_mark)
28254 remove_child_with_prev (c, prev);
28255 /* As base types got marked, there must be at least
28256 one node other than DW_TAG_base_type. */
28257 gcc_assert (die->die_child != NULL);
28258 c = prev->die_sib;
28261 while (c != die->die_child);
28262 gcc_assert (die->die_child);
28263 c = die->die_child;
28264 for (i = 0; base_types.iterate (i, &base_type); i++)
28266 base_type->die_mark = 0;
28267 base_type->die_sib = c->die_sib;
28268 c->die_sib = base_type;
28269 c = base_type;
28273 /* Helper function for resolve_addr, attempt to resolve
28274 one CONST_STRING, return true if successful. Similarly verify that
28275 SYMBOL_REFs refer to variables emitted in the current CU. */
28277 static bool
28278 resolve_one_addr (rtx *addr)
28280 rtx rtl = *addr;
28282 if (GET_CODE (rtl) == CONST_STRING)
28284 size_t len = strlen (XSTR (rtl, 0)) + 1;
28285 tree t = build_string (len, XSTR (rtl, 0));
28286 tree tlen = size_int (len - 1);
28287 TREE_TYPE (t)
28288 = build_array_type (char_type_node, build_index_type (tlen));
28289 rtl = lookup_constant_def (t);
28290 if (!rtl || !MEM_P (rtl))
28291 return false;
28292 rtl = XEXP (rtl, 0);
28293 if (GET_CODE (rtl) == SYMBOL_REF
28294 && SYMBOL_REF_DECL (rtl)
28295 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28296 return false;
28297 vec_safe_push (used_rtx_array, rtl);
28298 *addr = rtl;
28299 return true;
28302 if (GET_CODE (rtl) == SYMBOL_REF
28303 && SYMBOL_REF_DECL (rtl))
28305 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28307 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28308 return false;
28310 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28311 return false;
28314 if (GET_CODE (rtl) == CONST)
28316 subrtx_ptr_iterator::array_type array;
28317 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28318 if (!resolve_one_addr (*iter))
28319 return false;
28322 return true;
28325 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28326 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28327 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28329 static rtx
28330 string_cst_pool_decl (tree t)
28332 rtx rtl = output_constant_def (t, 1);
28333 unsigned char *array;
28334 dw_loc_descr_ref l;
28335 tree decl;
28336 size_t len;
28337 dw_die_ref ref;
28339 if (!rtl || !MEM_P (rtl))
28340 return NULL_RTX;
28341 rtl = XEXP (rtl, 0);
28342 if (GET_CODE (rtl) != SYMBOL_REF
28343 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28344 return NULL_RTX;
28346 decl = SYMBOL_REF_DECL (rtl);
28347 if (!lookup_decl_die (decl))
28349 len = TREE_STRING_LENGTH (t);
28350 vec_safe_push (used_rtx_array, rtl);
28351 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28352 array = ggc_vec_alloc<unsigned char> (len);
28353 memcpy (array, TREE_STRING_POINTER (t), len);
28354 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28355 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28356 l->dw_loc_oprnd2.v.val_vec.length = len;
28357 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28358 l->dw_loc_oprnd2.v.val_vec.array = array;
28359 add_AT_loc (ref, DW_AT_location, l);
28360 equate_decl_number_to_die (decl, ref);
28362 return rtl;
28365 /* Helper function of resolve_addr_in_expr. LOC is
28366 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28367 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28368 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28369 with DW_OP_implicit_pointer if possible
28370 and return true, if unsuccessful, return false. */
28372 static bool
28373 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28375 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28376 HOST_WIDE_INT offset = 0;
28377 dw_die_ref ref = NULL;
28378 tree decl;
28380 if (GET_CODE (rtl) == CONST
28381 && GET_CODE (XEXP (rtl, 0)) == PLUS
28382 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28384 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28385 rtl = XEXP (XEXP (rtl, 0), 0);
28387 if (GET_CODE (rtl) == CONST_STRING)
28389 size_t len = strlen (XSTR (rtl, 0)) + 1;
28390 tree t = build_string (len, XSTR (rtl, 0));
28391 tree tlen = size_int (len - 1);
28393 TREE_TYPE (t)
28394 = build_array_type (char_type_node, build_index_type (tlen));
28395 rtl = string_cst_pool_decl (t);
28396 if (!rtl)
28397 return false;
28399 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28401 decl = SYMBOL_REF_DECL (rtl);
28402 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28404 ref = lookup_decl_die (decl);
28405 if (ref && (get_AT (ref, DW_AT_location)
28406 || get_AT (ref, DW_AT_const_value)))
28408 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28409 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28410 loc->dw_loc_oprnd1.val_entry = NULL;
28411 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28412 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28413 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28414 loc->dw_loc_oprnd2.v.val_int = offset;
28415 return true;
28419 return false;
28422 /* Helper function for resolve_addr, handle one location
28423 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28424 the location list couldn't be resolved. */
28426 static bool
28427 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28429 dw_loc_descr_ref keep = NULL;
28430 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28431 switch (loc->dw_loc_opc)
28433 case DW_OP_addr:
28434 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28436 if ((prev == NULL
28437 || prev->dw_loc_opc == DW_OP_piece
28438 || prev->dw_loc_opc == DW_OP_bit_piece)
28439 && loc->dw_loc_next
28440 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28441 && (!dwarf_strict || dwarf_version >= 5)
28442 && optimize_one_addr_into_implicit_ptr (loc))
28443 break;
28444 return false;
28446 break;
28447 case DW_OP_GNU_addr_index:
28448 case DW_OP_GNU_const_index:
28449 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28450 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28452 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28453 if (!resolve_one_addr (&rtl))
28454 return false;
28455 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28456 loc->dw_loc_oprnd1.val_entry
28457 = add_addr_table_entry (rtl, ate_kind_rtx);
28459 break;
28460 case DW_OP_const4u:
28461 case DW_OP_const8u:
28462 if (loc->dtprel
28463 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28464 return false;
28465 break;
28466 case DW_OP_plus_uconst:
28467 if (size_of_loc_descr (loc)
28468 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28470 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28472 dw_loc_descr_ref repl
28473 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28474 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28475 add_loc_descr (&repl, loc->dw_loc_next);
28476 *loc = *repl;
28478 break;
28479 case DW_OP_implicit_value:
28480 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28481 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28482 return false;
28483 break;
28484 case DW_OP_implicit_pointer:
28485 case DW_OP_GNU_implicit_pointer:
28486 case DW_OP_GNU_parameter_ref:
28487 case DW_OP_GNU_variable_value:
28488 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28490 dw_die_ref ref
28491 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28492 if (ref == NULL)
28493 return false;
28494 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28495 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28496 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28498 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28500 if (prev == NULL
28501 && loc->dw_loc_next == NULL
28502 && AT_class (a) == dw_val_class_loc)
28503 switch (a->dw_attr)
28505 /* Following attributes allow both exprloc and reference,
28506 so if the whole expression is DW_OP_GNU_variable_value
28507 alone we could transform it into reference. */
28508 case DW_AT_byte_size:
28509 case DW_AT_bit_size:
28510 case DW_AT_lower_bound:
28511 case DW_AT_upper_bound:
28512 case DW_AT_bit_stride:
28513 case DW_AT_count:
28514 case DW_AT_allocated:
28515 case DW_AT_associated:
28516 case DW_AT_byte_stride:
28517 a->dw_attr_val.val_class = dw_val_class_die_ref;
28518 a->dw_attr_val.val_entry = NULL;
28519 a->dw_attr_val.v.val_die_ref.die
28520 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28521 a->dw_attr_val.v.val_die_ref.external = 0;
28522 return true;
28523 default:
28524 break;
28526 if (dwarf_strict)
28527 return false;
28529 break;
28530 case DW_OP_const_type:
28531 case DW_OP_regval_type:
28532 case DW_OP_deref_type:
28533 case DW_OP_convert:
28534 case DW_OP_reinterpret:
28535 case DW_OP_GNU_const_type:
28536 case DW_OP_GNU_regval_type:
28537 case DW_OP_GNU_deref_type:
28538 case DW_OP_GNU_convert:
28539 case DW_OP_GNU_reinterpret:
28540 while (loc->dw_loc_next
28541 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28542 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28544 dw_die_ref base1, base2;
28545 unsigned enc1, enc2, size1, size2;
28546 if (loc->dw_loc_opc == DW_OP_regval_type
28547 || loc->dw_loc_opc == DW_OP_deref_type
28548 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28549 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28550 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28551 else if (loc->dw_loc_oprnd1.val_class
28552 == dw_val_class_unsigned_const)
28553 break;
28554 else
28555 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28556 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28557 == dw_val_class_unsigned_const)
28558 break;
28559 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28560 gcc_assert (base1->die_tag == DW_TAG_base_type
28561 && base2->die_tag == DW_TAG_base_type);
28562 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28563 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28564 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28565 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28566 if (size1 == size2
28567 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28568 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28569 && loc != keep)
28570 || enc1 == enc2))
28572 /* Optimize away next DW_OP_convert after
28573 adjusting LOC's base type die reference. */
28574 if (loc->dw_loc_opc == DW_OP_regval_type
28575 || loc->dw_loc_opc == DW_OP_deref_type
28576 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28577 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28578 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28579 else
28580 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28581 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28582 continue;
28584 /* Don't change integer DW_OP_convert after e.g. floating
28585 point typed stack entry. */
28586 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28587 keep = loc->dw_loc_next;
28588 break;
28590 break;
28591 default:
28592 break;
28594 return true;
28597 /* Helper function of resolve_addr. DIE had DW_AT_location of
28598 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28599 and DW_OP_addr couldn't be resolved. resolve_addr has already
28600 removed the DW_AT_location attribute. This function attempts to
28601 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28602 to it or DW_AT_const_value attribute, if possible. */
28604 static void
28605 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28607 if (!VAR_P (decl)
28608 || lookup_decl_die (decl) != die
28609 || DECL_EXTERNAL (decl)
28610 || !TREE_STATIC (decl)
28611 || DECL_INITIAL (decl) == NULL_TREE
28612 || DECL_P (DECL_INITIAL (decl))
28613 || get_AT (die, DW_AT_const_value))
28614 return;
28616 tree init = DECL_INITIAL (decl);
28617 HOST_WIDE_INT offset = 0;
28618 /* For variables that have been optimized away and thus
28619 don't have a memory location, see if we can emit
28620 DW_AT_const_value instead. */
28621 if (tree_add_const_value_attribute (die, init))
28622 return;
28623 if (dwarf_strict && dwarf_version < 5)
28624 return;
28625 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28626 and ADDR_EXPR refers to a decl that has DW_AT_location or
28627 DW_AT_const_value (but isn't addressable, otherwise
28628 resolving the original DW_OP_addr wouldn't fail), see if
28629 we can add DW_OP_implicit_pointer. */
28630 STRIP_NOPS (init);
28631 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28632 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28634 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28635 init = TREE_OPERAND (init, 0);
28636 STRIP_NOPS (init);
28638 if (TREE_CODE (init) != ADDR_EXPR)
28639 return;
28640 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28641 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28642 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28643 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28644 && TREE_OPERAND (init, 0) != decl))
28646 dw_die_ref ref;
28647 dw_loc_descr_ref l;
28649 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28651 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28652 if (!rtl)
28653 return;
28654 decl = SYMBOL_REF_DECL (rtl);
28656 else
28657 decl = TREE_OPERAND (init, 0);
28658 ref = lookup_decl_die (decl);
28659 if (ref == NULL
28660 || (!get_AT (ref, DW_AT_location)
28661 && !get_AT (ref, DW_AT_const_value)))
28662 return;
28663 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28664 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28665 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28666 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28667 add_AT_loc (die, DW_AT_location, l);
28671 /* Return NULL if l is a DWARF expression, or first op that is not
28672 valid DWARF expression. */
28674 static dw_loc_descr_ref
28675 non_dwarf_expression (dw_loc_descr_ref l)
28677 while (l)
28679 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28680 return l;
28681 switch (l->dw_loc_opc)
28683 case DW_OP_regx:
28684 case DW_OP_implicit_value:
28685 case DW_OP_stack_value:
28686 case DW_OP_implicit_pointer:
28687 case DW_OP_GNU_implicit_pointer:
28688 case DW_OP_GNU_parameter_ref:
28689 case DW_OP_piece:
28690 case DW_OP_bit_piece:
28691 return l;
28692 default:
28693 break;
28695 l = l->dw_loc_next;
28697 return NULL;
28700 /* Return adjusted copy of EXPR:
28701 If it is empty DWARF expression, return it.
28702 If it is valid non-empty DWARF expression,
28703 return copy of EXPR with DW_OP_deref appended to it.
28704 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28705 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28706 If it is DWARF expression followed by DW_OP_stack_value, return
28707 copy of the DWARF expression without anything appended.
28708 Otherwise, return NULL. */
28710 static dw_loc_descr_ref
28711 copy_deref_exprloc (dw_loc_descr_ref expr)
28713 dw_loc_descr_ref tail = NULL;
28715 if (expr == NULL)
28716 return NULL;
28718 dw_loc_descr_ref l = non_dwarf_expression (expr);
28719 if (l && l->dw_loc_next)
28720 return NULL;
28722 if (l)
28724 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28725 tail = new_loc_descr ((enum dwarf_location_atom)
28726 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28727 0, 0);
28728 else
28729 switch (l->dw_loc_opc)
28731 case DW_OP_regx:
28732 tail = new_loc_descr (DW_OP_bregx,
28733 l->dw_loc_oprnd1.v.val_unsigned, 0);
28734 break;
28735 case DW_OP_stack_value:
28736 break;
28737 default:
28738 return NULL;
28741 else
28742 tail = new_loc_descr (DW_OP_deref, 0, 0);
28744 dw_loc_descr_ref ret = NULL, *p = &ret;
28745 while (expr != l)
28747 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28748 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28749 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28750 p = &(*p)->dw_loc_next;
28751 expr = expr->dw_loc_next;
28753 *p = tail;
28754 return ret;
28757 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28758 reference to a variable or argument, adjust it if needed and return:
28759 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28760 attribute if present should be removed
28761 0 keep the attribute perhaps with minor modifications, no need to rescan
28762 1 if the attribute has been successfully adjusted. */
28764 static int
28765 optimize_string_length (dw_attr_node *a)
28767 dw_loc_descr_ref l = AT_loc (a), lv;
28768 dw_die_ref die;
28769 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28771 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28772 die = lookup_decl_die (decl);
28773 if (die)
28775 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28776 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28777 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28779 else
28780 return -1;
28782 else
28783 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28785 /* DWARF5 allows reference class, so we can then reference the DIE.
28786 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28787 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28789 a->dw_attr_val.val_class = dw_val_class_die_ref;
28790 a->dw_attr_val.val_entry = NULL;
28791 a->dw_attr_val.v.val_die_ref.die = die;
28792 a->dw_attr_val.v.val_die_ref.external = 0;
28793 return 0;
28796 dw_attr_node *av = get_AT (die, DW_AT_location);
28797 dw_loc_list_ref d;
28798 bool non_dwarf_expr = false;
28800 if (av == NULL)
28801 return dwarf_strict ? -1 : 0;
28802 switch (AT_class (av))
28804 case dw_val_class_loc_list:
28805 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28806 if (d->expr && non_dwarf_expression (d->expr))
28807 non_dwarf_expr = true;
28808 break;
28809 case dw_val_class_loc:
28810 lv = AT_loc (av);
28811 if (lv == NULL)
28812 return dwarf_strict ? -1 : 0;
28813 if (non_dwarf_expression (lv))
28814 non_dwarf_expr = true;
28815 break;
28816 default:
28817 return dwarf_strict ? -1 : 0;
28820 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28821 into DW_OP_call4 or DW_OP_GNU_variable_value into
28822 DW_OP_call4 DW_OP_deref, do so. */
28823 if (!non_dwarf_expr
28824 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28826 l->dw_loc_opc = DW_OP_call4;
28827 if (l->dw_loc_next)
28828 l->dw_loc_next = NULL;
28829 else
28830 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28831 return 0;
28834 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28835 copy over the DW_AT_location attribute from die to a. */
28836 if (l->dw_loc_next != NULL)
28838 a->dw_attr_val = av->dw_attr_val;
28839 return 1;
28842 dw_loc_list_ref list, *p;
28843 switch (AT_class (av))
28845 case dw_val_class_loc_list:
28846 p = &list;
28847 list = NULL;
28848 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28850 lv = copy_deref_exprloc (d->expr);
28851 if (lv)
28853 *p = new_loc_list (lv, d->begin, d->end, d->section);
28854 p = &(*p)->dw_loc_next;
28856 else if (!dwarf_strict && d->expr)
28857 return 0;
28859 if (list == NULL)
28860 return dwarf_strict ? -1 : 0;
28861 a->dw_attr_val.val_class = dw_val_class_loc_list;
28862 gen_llsym (list);
28863 *AT_loc_list_ptr (a) = list;
28864 return 1;
28865 case dw_val_class_loc:
28866 lv = copy_deref_exprloc (AT_loc (av));
28867 if (lv == NULL)
28868 return dwarf_strict ? -1 : 0;
28869 a->dw_attr_val.v.val_loc = lv;
28870 return 1;
28871 default:
28872 gcc_unreachable ();
28876 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28877 an address in .rodata section if the string literal is emitted there,
28878 or remove the containing location list or replace DW_AT_const_value
28879 with DW_AT_location and empty location expression, if it isn't found
28880 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28881 to something that has been emitted in the current CU. */
28883 static void
28884 resolve_addr (dw_die_ref die)
28886 dw_die_ref c;
28887 dw_attr_node *a;
28888 dw_loc_list_ref *curr, *start, loc;
28889 unsigned ix;
28890 bool remove_AT_byte_size = false;
28892 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28893 switch (AT_class (a))
28895 case dw_val_class_loc_list:
28896 start = curr = AT_loc_list_ptr (a);
28897 loc = *curr;
28898 gcc_assert (loc);
28899 /* The same list can be referenced more than once. See if we have
28900 already recorded the result from a previous pass. */
28901 if (loc->replaced)
28902 *curr = loc->dw_loc_next;
28903 else if (!loc->resolved_addr)
28905 /* As things stand, we do not expect or allow one die to
28906 reference a suffix of another die's location list chain.
28907 References must be identical or completely separate.
28908 There is therefore no need to cache the result of this
28909 pass on any list other than the first; doing so
28910 would lead to unnecessary writes. */
28911 while (*curr)
28913 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28914 if (!resolve_addr_in_expr (a, (*curr)->expr))
28916 dw_loc_list_ref next = (*curr)->dw_loc_next;
28917 dw_loc_descr_ref l = (*curr)->expr;
28919 if (next && (*curr)->ll_symbol)
28921 gcc_assert (!next->ll_symbol);
28922 next->ll_symbol = (*curr)->ll_symbol;
28924 if (dwarf_split_debug_info)
28925 remove_loc_list_addr_table_entries (l);
28926 *curr = next;
28928 else
28930 mark_base_types ((*curr)->expr);
28931 curr = &(*curr)->dw_loc_next;
28934 if (loc == *start)
28935 loc->resolved_addr = 1;
28936 else
28938 loc->replaced = 1;
28939 loc->dw_loc_next = *start;
28942 if (!*start)
28944 remove_AT (die, a->dw_attr);
28945 ix--;
28947 break;
28948 case dw_val_class_loc:
28950 dw_loc_descr_ref l = AT_loc (a);
28951 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28952 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28953 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28954 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28955 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28956 with DW_FORM_ref referencing the same DIE as
28957 DW_OP_GNU_variable_value used to reference. */
28958 if (a->dw_attr == DW_AT_string_length
28959 && l
28960 && l->dw_loc_opc == DW_OP_GNU_variable_value
28961 && (l->dw_loc_next == NULL
28962 || (l->dw_loc_next->dw_loc_next == NULL
28963 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28965 switch (optimize_string_length (a))
28967 case -1:
28968 remove_AT (die, a->dw_attr);
28969 ix--;
28970 /* If we drop DW_AT_string_length, we need to drop also
28971 DW_AT_{string_length_,}byte_size. */
28972 remove_AT_byte_size = true;
28973 continue;
28974 default:
28975 break;
28976 case 1:
28977 /* Even if we keep the optimized DW_AT_string_length,
28978 it might have changed AT_class, so process it again. */
28979 ix--;
28980 continue;
28983 /* For -gdwarf-2 don't attempt to optimize
28984 DW_AT_data_member_location containing
28985 DW_OP_plus_uconst - older consumers might
28986 rely on it being that op instead of a more complex,
28987 but shorter, location description. */
28988 if ((dwarf_version > 2
28989 || a->dw_attr != DW_AT_data_member_location
28990 || l == NULL
28991 || l->dw_loc_opc != DW_OP_plus_uconst
28992 || l->dw_loc_next != NULL)
28993 && !resolve_addr_in_expr (a, l))
28995 if (dwarf_split_debug_info)
28996 remove_loc_list_addr_table_entries (l);
28997 if (l != NULL
28998 && l->dw_loc_next == NULL
28999 && l->dw_loc_opc == DW_OP_addr
29000 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29001 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29002 && a->dw_attr == DW_AT_location)
29004 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29005 remove_AT (die, a->dw_attr);
29006 ix--;
29007 optimize_location_into_implicit_ptr (die, decl);
29008 break;
29010 if (a->dw_attr == DW_AT_string_length)
29011 /* If we drop DW_AT_string_length, we need to drop also
29012 DW_AT_{string_length_,}byte_size. */
29013 remove_AT_byte_size = true;
29014 remove_AT (die, a->dw_attr);
29015 ix--;
29017 else
29018 mark_base_types (l);
29020 break;
29021 case dw_val_class_addr:
29022 if (a->dw_attr == DW_AT_const_value
29023 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29025 if (AT_index (a) != NOT_INDEXED)
29026 remove_addr_table_entry (a->dw_attr_val.val_entry);
29027 remove_AT (die, a->dw_attr);
29028 ix--;
29030 if ((die->die_tag == DW_TAG_call_site
29031 && a->dw_attr == DW_AT_call_origin)
29032 || (die->die_tag == DW_TAG_GNU_call_site
29033 && a->dw_attr == DW_AT_abstract_origin))
29035 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29036 dw_die_ref tdie = lookup_decl_die (tdecl);
29037 dw_die_ref cdie;
29038 if (tdie == NULL
29039 && DECL_EXTERNAL (tdecl)
29040 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29041 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29043 dw_die_ref pdie = cdie;
29044 /* Make sure we don't add these DIEs into type units.
29045 We could emit skeleton DIEs for context (namespaces,
29046 outer structs/classes) and a skeleton DIE for the
29047 innermost context with DW_AT_signature pointing to the
29048 type unit. See PR78835. */
29049 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29050 pdie = pdie->die_parent;
29051 if (pdie == NULL)
29053 /* Creating a full DIE for tdecl is overly expensive and
29054 at this point even wrong when in the LTO phase
29055 as it can end up generating new type DIEs we didn't
29056 output and thus optimize_external_refs will crash. */
29057 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29058 add_AT_flag (tdie, DW_AT_external, 1);
29059 add_AT_flag (tdie, DW_AT_declaration, 1);
29060 add_linkage_attr (tdie, tdecl);
29061 add_name_and_src_coords_attributes (tdie, tdecl);
29062 equate_decl_number_to_die (tdecl, tdie);
29065 if (tdie)
29067 a->dw_attr_val.val_class = dw_val_class_die_ref;
29068 a->dw_attr_val.v.val_die_ref.die = tdie;
29069 a->dw_attr_val.v.val_die_ref.external = 0;
29071 else
29073 if (AT_index (a) != NOT_INDEXED)
29074 remove_addr_table_entry (a->dw_attr_val.val_entry);
29075 remove_AT (die, a->dw_attr);
29076 ix--;
29079 break;
29080 default:
29081 break;
29084 if (remove_AT_byte_size)
29085 remove_AT (die, dwarf_version >= 5
29086 ? DW_AT_string_length_byte_size
29087 : DW_AT_byte_size);
29089 FOR_EACH_CHILD (die, c, resolve_addr (c));
29092 /* Helper routines for optimize_location_lists.
29093 This pass tries to share identical local lists in .debug_loc
29094 section. */
29096 /* Iteratively hash operands of LOC opcode into HSTATE. */
29098 static void
29099 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29101 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29102 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29104 switch (loc->dw_loc_opc)
29106 case DW_OP_const4u:
29107 case DW_OP_const8u:
29108 if (loc->dtprel)
29109 goto hash_addr;
29110 /* FALLTHRU */
29111 case DW_OP_const1u:
29112 case DW_OP_const1s:
29113 case DW_OP_const2u:
29114 case DW_OP_const2s:
29115 case DW_OP_const4s:
29116 case DW_OP_const8s:
29117 case DW_OP_constu:
29118 case DW_OP_consts:
29119 case DW_OP_pick:
29120 case DW_OP_plus_uconst:
29121 case DW_OP_breg0:
29122 case DW_OP_breg1:
29123 case DW_OP_breg2:
29124 case DW_OP_breg3:
29125 case DW_OP_breg4:
29126 case DW_OP_breg5:
29127 case DW_OP_breg6:
29128 case DW_OP_breg7:
29129 case DW_OP_breg8:
29130 case DW_OP_breg9:
29131 case DW_OP_breg10:
29132 case DW_OP_breg11:
29133 case DW_OP_breg12:
29134 case DW_OP_breg13:
29135 case DW_OP_breg14:
29136 case DW_OP_breg15:
29137 case DW_OP_breg16:
29138 case DW_OP_breg17:
29139 case DW_OP_breg18:
29140 case DW_OP_breg19:
29141 case DW_OP_breg20:
29142 case DW_OP_breg21:
29143 case DW_OP_breg22:
29144 case DW_OP_breg23:
29145 case DW_OP_breg24:
29146 case DW_OP_breg25:
29147 case DW_OP_breg26:
29148 case DW_OP_breg27:
29149 case DW_OP_breg28:
29150 case DW_OP_breg29:
29151 case DW_OP_breg30:
29152 case DW_OP_breg31:
29153 case DW_OP_regx:
29154 case DW_OP_fbreg:
29155 case DW_OP_piece:
29156 case DW_OP_deref_size:
29157 case DW_OP_xderef_size:
29158 hstate.add_object (val1->v.val_int);
29159 break;
29160 case DW_OP_skip:
29161 case DW_OP_bra:
29163 int offset;
29165 gcc_assert (val1->val_class == dw_val_class_loc);
29166 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29167 hstate.add_object (offset);
29169 break;
29170 case DW_OP_implicit_value:
29171 hstate.add_object (val1->v.val_unsigned);
29172 switch (val2->val_class)
29174 case dw_val_class_const:
29175 hstate.add_object (val2->v.val_int);
29176 break;
29177 case dw_val_class_vec:
29179 unsigned int elt_size = val2->v.val_vec.elt_size;
29180 unsigned int len = val2->v.val_vec.length;
29182 hstate.add_int (elt_size);
29183 hstate.add_int (len);
29184 hstate.add (val2->v.val_vec.array, len * elt_size);
29186 break;
29187 case dw_val_class_const_double:
29188 hstate.add_object (val2->v.val_double.low);
29189 hstate.add_object (val2->v.val_double.high);
29190 break;
29191 case dw_val_class_wide_int:
29192 hstate.add (val2->v.val_wide->get_val (),
29193 get_full_len (*val2->v.val_wide)
29194 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29195 break;
29196 case dw_val_class_addr:
29197 inchash::add_rtx (val2->v.val_addr, hstate);
29198 break;
29199 default:
29200 gcc_unreachable ();
29202 break;
29203 case DW_OP_bregx:
29204 case DW_OP_bit_piece:
29205 hstate.add_object (val1->v.val_int);
29206 hstate.add_object (val2->v.val_int);
29207 break;
29208 case DW_OP_addr:
29209 hash_addr:
29210 if (loc->dtprel)
29212 unsigned char dtprel = 0xd1;
29213 hstate.add_object (dtprel);
29215 inchash::add_rtx (val1->v.val_addr, hstate);
29216 break;
29217 case DW_OP_GNU_addr_index:
29218 case DW_OP_GNU_const_index:
29220 if (loc->dtprel)
29222 unsigned char dtprel = 0xd1;
29223 hstate.add_object (dtprel);
29225 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29227 break;
29228 case DW_OP_implicit_pointer:
29229 case DW_OP_GNU_implicit_pointer:
29230 hstate.add_int (val2->v.val_int);
29231 break;
29232 case DW_OP_entry_value:
29233 case DW_OP_GNU_entry_value:
29234 hstate.add_object (val1->v.val_loc);
29235 break;
29236 case DW_OP_regval_type:
29237 case DW_OP_deref_type:
29238 case DW_OP_GNU_regval_type:
29239 case DW_OP_GNU_deref_type:
29241 unsigned int byte_size
29242 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29243 unsigned int encoding
29244 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29245 hstate.add_object (val1->v.val_int);
29246 hstate.add_object (byte_size);
29247 hstate.add_object (encoding);
29249 break;
29250 case DW_OP_convert:
29251 case DW_OP_reinterpret:
29252 case DW_OP_GNU_convert:
29253 case DW_OP_GNU_reinterpret:
29254 if (val1->val_class == dw_val_class_unsigned_const)
29256 hstate.add_object (val1->v.val_unsigned);
29257 break;
29259 /* FALLTHRU */
29260 case DW_OP_const_type:
29261 case DW_OP_GNU_const_type:
29263 unsigned int byte_size
29264 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29265 unsigned int encoding
29266 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29267 hstate.add_object (byte_size);
29268 hstate.add_object (encoding);
29269 if (loc->dw_loc_opc != DW_OP_const_type
29270 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29271 break;
29272 hstate.add_object (val2->val_class);
29273 switch (val2->val_class)
29275 case dw_val_class_const:
29276 hstate.add_object (val2->v.val_int);
29277 break;
29278 case dw_val_class_vec:
29280 unsigned int elt_size = val2->v.val_vec.elt_size;
29281 unsigned int len = val2->v.val_vec.length;
29283 hstate.add_object (elt_size);
29284 hstate.add_object (len);
29285 hstate.add (val2->v.val_vec.array, len * elt_size);
29287 break;
29288 case dw_val_class_const_double:
29289 hstate.add_object (val2->v.val_double.low);
29290 hstate.add_object (val2->v.val_double.high);
29291 break;
29292 case dw_val_class_wide_int:
29293 hstate.add (val2->v.val_wide->get_val (),
29294 get_full_len (*val2->v.val_wide)
29295 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29296 break;
29297 default:
29298 gcc_unreachable ();
29301 break;
29303 default:
29304 /* Other codes have no operands. */
29305 break;
29309 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29311 static inline void
29312 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29314 dw_loc_descr_ref l;
29315 bool sizes_computed = false;
29316 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29317 size_of_locs (loc);
29319 for (l = loc; l != NULL; l = l->dw_loc_next)
29321 enum dwarf_location_atom opc = l->dw_loc_opc;
29322 hstate.add_object (opc);
29323 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29325 size_of_locs (loc);
29326 sizes_computed = true;
29328 hash_loc_operands (l, hstate);
29332 /* Compute hash of the whole location list LIST_HEAD. */
29334 static inline void
29335 hash_loc_list (dw_loc_list_ref list_head)
29337 dw_loc_list_ref curr = list_head;
29338 inchash::hash hstate;
29340 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29342 hstate.add (curr->begin, strlen (curr->begin) + 1);
29343 hstate.add (curr->end, strlen (curr->end) + 1);
29344 if (curr->section)
29345 hstate.add (curr->section, strlen (curr->section) + 1);
29346 hash_locs (curr->expr, hstate);
29348 list_head->hash = hstate.end ();
29351 /* Return true if X and Y opcodes have the same operands. */
29353 static inline bool
29354 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29356 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29357 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29358 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29359 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29361 switch (x->dw_loc_opc)
29363 case DW_OP_const4u:
29364 case DW_OP_const8u:
29365 if (x->dtprel)
29366 goto hash_addr;
29367 /* FALLTHRU */
29368 case DW_OP_const1u:
29369 case DW_OP_const1s:
29370 case DW_OP_const2u:
29371 case DW_OP_const2s:
29372 case DW_OP_const4s:
29373 case DW_OP_const8s:
29374 case DW_OP_constu:
29375 case DW_OP_consts:
29376 case DW_OP_pick:
29377 case DW_OP_plus_uconst:
29378 case DW_OP_breg0:
29379 case DW_OP_breg1:
29380 case DW_OP_breg2:
29381 case DW_OP_breg3:
29382 case DW_OP_breg4:
29383 case DW_OP_breg5:
29384 case DW_OP_breg6:
29385 case DW_OP_breg7:
29386 case DW_OP_breg8:
29387 case DW_OP_breg9:
29388 case DW_OP_breg10:
29389 case DW_OP_breg11:
29390 case DW_OP_breg12:
29391 case DW_OP_breg13:
29392 case DW_OP_breg14:
29393 case DW_OP_breg15:
29394 case DW_OP_breg16:
29395 case DW_OP_breg17:
29396 case DW_OP_breg18:
29397 case DW_OP_breg19:
29398 case DW_OP_breg20:
29399 case DW_OP_breg21:
29400 case DW_OP_breg22:
29401 case DW_OP_breg23:
29402 case DW_OP_breg24:
29403 case DW_OP_breg25:
29404 case DW_OP_breg26:
29405 case DW_OP_breg27:
29406 case DW_OP_breg28:
29407 case DW_OP_breg29:
29408 case DW_OP_breg30:
29409 case DW_OP_breg31:
29410 case DW_OP_regx:
29411 case DW_OP_fbreg:
29412 case DW_OP_piece:
29413 case DW_OP_deref_size:
29414 case DW_OP_xderef_size:
29415 return valx1->v.val_int == valy1->v.val_int;
29416 case DW_OP_skip:
29417 case DW_OP_bra:
29418 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29419 can cause irrelevant differences in dw_loc_addr. */
29420 gcc_assert (valx1->val_class == dw_val_class_loc
29421 && valy1->val_class == dw_val_class_loc
29422 && (dwarf_split_debug_info
29423 || x->dw_loc_addr == y->dw_loc_addr));
29424 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29425 case DW_OP_implicit_value:
29426 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29427 || valx2->val_class != valy2->val_class)
29428 return false;
29429 switch (valx2->val_class)
29431 case dw_val_class_const:
29432 return valx2->v.val_int == valy2->v.val_int;
29433 case dw_val_class_vec:
29434 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29435 && valx2->v.val_vec.length == valy2->v.val_vec.length
29436 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29437 valx2->v.val_vec.elt_size
29438 * valx2->v.val_vec.length) == 0;
29439 case dw_val_class_const_double:
29440 return valx2->v.val_double.low == valy2->v.val_double.low
29441 && valx2->v.val_double.high == valy2->v.val_double.high;
29442 case dw_val_class_wide_int:
29443 return *valx2->v.val_wide == *valy2->v.val_wide;
29444 case dw_val_class_addr:
29445 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29446 default:
29447 gcc_unreachable ();
29449 case DW_OP_bregx:
29450 case DW_OP_bit_piece:
29451 return valx1->v.val_int == valy1->v.val_int
29452 && valx2->v.val_int == valy2->v.val_int;
29453 case DW_OP_addr:
29454 hash_addr:
29455 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29456 case DW_OP_GNU_addr_index:
29457 case DW_OP_GNU_const_index:
29459 rtx ax1 = valx1->val_entry->addr.rtl;
29460 rtx ay1 = valy1->val_entry->addr.rtl;
29461 return rtx_equal_p (ax1, ay1);
29463 case DW_OP_implicit_pointer:
29464 case DW_OP_GNU_implicit_pointer:
29465 return valx1->val_class == dw_val_class_die_ref
29466 && valx1->val_class == valy1->val_class
29467 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29468 && valx2->v.val_int == valy2->v.val_int;
29469 case DW_OP_entry_value:
29470 case DW_OP_GNU_entry_value:
29471 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29472 case DW_OP_const_type:
29473 case DW_OP_GNU_const_type:
29474 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29475 || valx2->val_class != valy2->val_class)
29476 return false;
29477 switch (valx2->val_class)
29479 case dw_val_class_const:
29480 return valx2->v.val_int == valy2->v.val_int;
29481 case dw_val_class_vec:
29482 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29483 && valx2->v.val_vec.length == valy2->v.val_vec.length
29484 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29485 valx2->v.val_vec.elt_size
29486 * valx2->v.val_vec.length) == 0;
29487 case dw_val_class_const_double:
29488 return valx2->v.val_double.low == valy2->v.val_double.low
29489 && valx2->v.val_double.high == valy2->v.val_double.high;
29490 case dw_val_class_wide_int:
29491 return *valx2->v.val_wide == *valy2->v.val_wide;
29492 default:
29493 gcc_unreachable ();
29495 case DW_OP_regval_type:
29496 case DW_OP_deref_type:
29497 case DW_OP_GNU_regval_type:
29498 case DW_OP_GNU_deref_type:
29499 return valx1->v.val_int == valy1->v.val_int
29500 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29501 case DW_OP_convert:
29502 case DW_OP_reinterpret:
29503 case DW_OP_GNU_convert:
29504 case DW_OP_GNU_reinterpret:
29505 if (valx1->val_class != valy1->val_class)
29506 return false;
29507 if (valx1->val_class == dw_val_class_unsigned_const)
29508 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29509 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29510 case DW_OP_GNU_parameter_ref:
29511 return valx1->val_class == dw_val_class_die_ref
29512 && valx1->val_class == valy1->val_class
29513 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29514 default:
29515 /* Other codes have no operands. */
29516 return true;
29520 /* Return true if DWARF location expressions X and Y are the same. */
29522 static inline bool
29523 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29525 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29526 if (x->dw_loc_opc != y->dw_loc_opc
29527 || x->dtprel != y->dtprel
29528 || !compare_loc_operands (x, y))
29529 break;
29530 return x == NULL && y == NULL;
29533 /* Hashtable helpers. */
29535 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29537 static inline hashval_t hash (const dw_loc_list_struct *);
29538 static inline bool equal (const dw_loc_list_struct *,
29539 const dw_loc_list_struct *);
29542 /* Return precomputed hash of location list X. */
29544 inline hashval_t
29545 loc_list_hasher::hash (const dw_loc_list_struct *x)
29547 return x->hash;
29550 /* Return true if location lists A and B are the same. */
29552 inline bool
29553 loc_list_hasher::equal (const dw_loc_list_struct *a,
29554 const dw_loc_list_struct *b)
29556 if (a == b)
29557 return 1;
29558 if (a->hash != b->hash)
29559 return 0;
29560 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29561 if (strcmp (a->begin, b->begin) != 0
29562 || strcmp (a->end, b->end) != 0
29563 || (a->section == NULL) != (b->section == NULL)
29564 || (a->section && strcmp (a->section, b->section) != 0)
29565 || !compare_locs (a->expr, b->expr))
29566 break;
29567 return a == NULL && b == NULL;
29570 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29573 /* Recursively optimize location lists referenced from DIE
29574 children and share them whenever possible. */
29576 static void
29577 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29579 dw_die_ref c;
29580 dw_attr_node *a;
29581 unsigned ix;
29582 dw_loc_list_struct **slot;
29584 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29585 if (AT_class (a) == dw_val_class_loc_list)
29587 dw_loc_list_ref list = AT_loc_list (a);
29588 /* TODO: perform some optimizations here, before hashing
29589 it and storing into the hash table. */
29590 hash_loc_list (list);
29591 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29592 if (*slot == NULL)
29593 *slot = list;
29594 else
29595 a->dw_attr_val.v.val_loc_list = *slot;
29598 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29602 /* Recursively assign each location list a unique index into the debug_addr
29603 section. */
29605 static void
29606 index_location_lists (dw_die_ref die)
29608 dw_die_ref c;
29609 dw_attr_node *a;
29610 unsigned ix;
29612 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29613 if (AT_class (a) == dw_val_class_loc_list)
29615 dw_loc_list_ref list = AT_loc_list (a);
29616 dw_loc_list_ref curr;
29617 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29619 /* Don't index an entry that has already been indexed
29620 or won't be output. */
29621 if (curr->begin_entry != NULL
29622 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29623 continue;
29625 curr->begin_entry
29626 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29630 FOR_EACH_CHILD (die, c, index_location_lists (c));
29633 /* Optimize location lists referenced from DIE
29634 children and share them whenever possible. */
29636 static void
29637 optimize_location_lists (dw_die_ref die)
29639 loc_list_hash_type htab (500);
29640 optimize_location_lists_1 (die, &htab);
29643 /* Traverse the limbo die list, and add parent/child links. The only
29644 dies without parents that should be here are concrete instances of
29645 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29646 For concrete instances, we can get the parent die from the abstract
29647 instance. */
29649 static void
29650 flush_limbo_die_list (void)
29652 limbo_die_node *node;
29654 /* get_context_die calls force_decl_die, which can put new DIEs on the
29655 limbo list in LTO mode when nested functions are put in a different
29656 partition than that of their parent function. */
29657 while ((node = limbo_die_list))
29659 dw_die_ref die = node->die;
29660 limbo_die_list = node->next;
29662 if (die->die_parent == NULL)
29664 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29666 if (origin && origin->die_parent)
29667 add_child_die (origin->die_parent, die);
29668 else if (is_cu_die (die))
29670 else if (seen_error ())
29671 /* It's OK to be confused by errors in the input. */
29672 add_child_die (comp_unit_die (), die);
29673 else
29675 /* In certain situations, the lexical block containing a
29676 nested function can be optimized away, which results
29677 in the nested function die being orphaned. Likewise
29678 with the return type of that nested function. Force
29679 this to be a child of the containing function.
29681 It may happen that even the containing function got fully
29682 inlined and optimized out. In that case we are lost and
29683 assign the empty child. This should not be big issue as
29684 the function is likely unreachable too. */
29685 gcc_assert (node->created_for);
29687 if (DECL_P (node->created_for))
29688 origin = get_context_die (DECL_CONTEXT (node->created_for));
29689 else if (TYPE_P (node->created_for))
29690 origin = scope_die_for (node->created_for, comp_unit_die ());
29691 else
29692 origin = comp_unit_die ();
29694 add_child_die (origin, die);
29700 /* Reset DIEs so we can output them again. */
29702 static void
29703 reset_dies (dw_die_ref die)
29705 dw_die_ref c;
29707 /* Remove stuff we re-generate. */
29708 die->die_mark = 0;
29709 die->die_offset = 0;
29710 die->die_abbrev = 0;
29711 remove_AT (die, DW_AT_sibling);
29713 FOR_EACH_CHILD (die, c, reset_dies (c));
29716 /* Output stuff that dwarf requires at the end of every file,
29717 and generate the DWARF-2 debugging info. */
29719 static void
29720 dwarf2out_finish (const char *)
29722 comdat_type_node *ctnode;
29723 dw_die_ref main_comp_unit_die;
29724 unsigned char checksum[16];
29725 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29727 /* Flush out any latecomers to the limbo party. */
29728 flush_limbo_die_list ();
29730 if (flag_checking)
29732 verify_die (comp_unit_die ());
29733 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29734 verify_die (node->die);
29737 /* We shouldn't have any symbols with delayed asm names for
29738 DIEs generated after early finish. */
29739 gcc_assert (deferred_asm_name == NULL);
29741 gen_remaining_tmpl_value_param_die_attribute ();
29743 if (flag_generate_lto || flag_generate_offload)
29745 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29747 /* Prune stuff so that dwarf2out_finish runs successfully
29748 for the fat part of the object. */
29749 reset_dies (comp_unit_die ());
29750 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29751 reset_dies (node->die);
29753 hash_table<comdat_type_hasher> comdat_type_table (100);
29754 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29756 comdat_type_node **slot
29757 = comdat_type_table.find_slot (ctnode, INSERT);
29759 /* Don't reset types twice. */
29760 if (*slot != HTAB_EMPTY_ENTRY)
29761 continue;
29763 /* Add a pointer to the line table for the main compilation unit
29764 so that the debugger can make sense of DW_AT_decl_file
29765 attributes. */
29766 if (debug_info_level >= DINFO_LEVEL_TERSE)
29767 reset_dies (ctnode->root_die);
29769 *slot = ctnode;
29772 /* Reset die CU symbol so we don't output it twice. */
29773 comp_unit_die ()->die_id.die_symbol = NULL;
29775 /* Remove DW_AT_macro from the early output. */
29776 if (have_macinfo)
29777 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29779 /* Remove indirect string decisions. */
29780 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29783 #if ENABLE_ASSERT_CHECKING
29785 dw_die_ref die = comp_unit_die (), c;
29786 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29788 #endif
29789 resolve_addr (comp_unit_die ());
29790 move_marked_base_types ();
29792 /* Initialize sections and labels used for actual assembler output. */
29793 init_sections_and_labels (false);
29795 /* Traverse the DIE's and add sibling attributes to those DIE's that
29796 have children. */
29797 add_sibling_attributes (comp_unit_die ());
29798 limbo_die_node *node;
29799 for (node = cu_die_list; node; node = node->next)
29800 add_sibling_attributes (node->die);
29801 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29802 add_sibling_attributes (ctnode->root_die);
29804 /* When splitting DWARF info, we put some attributes in the
29805 skeleton compile_unit DIE that remains in the .o, while
29806 most attributes go in the DWO compile_unit_die. */
29807 if (dwarf_split_debug_info)
29809 limbo_die_node *cu;
29810 main_comp_unit_die = gen_compile_unit_die (NULL);
29811 if (dwarf_version >= 5)
29812 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29813 cu = limbo_die_list;
29814 gcc_assert (cu->die == main_comp_unit_die);
29815 limbo_die_list = limbo_die_list->next;
29816 cu->next = cu_die_list;
29817 cu_die_list = cu;
29819 else
29820 main_comp_unit_die = comp_unit_die ();
29822 /* Output a terminator label for the .text section. */
29823 switch_to_section (text_section);
29824 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29825 if (cold_text_section)
29827 switch_to_section (cold_text_section);
29828 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29831 /* We can only use the low/high_pc attributes if all of the code was
29832 in .text. */
29833 if (!have_multiple_function_sections
29834 || (dwarf_version < 3 && dwarf_strict))
29836 /* Don't add if the CU has no associated code. */
29837 if (text_section_used)
29838 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29839 text_end_label, true);
29841 else
29843 unsigned fde_idx;
29844 dw_fde_ref fde;
29845 bool range_list_added = false;
29847 if (text_section_used)
29848 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29849 text_end_label, &range_list_added, true);
29850 if (cold_text_section_used)
29851 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29852 cold_end_label, &range_list_added, true);
29854 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29856 if (DECL_IGNORED_P (fde->decl))
29857 continue;
29858 if (!fde->in_std_section)
29859 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29860 fde->dw_fde_end, &range_list_added,
29861 true);
29862 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29863 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29864 fde->dw_fde_second_end, &range_list_added,
29865 true);
29868 if (range_list_added)
29870 /* We need to give .debug_loc and .debug_ranges an appropriate
29871 "base address". Use zero so that these addresses become
29872 absolute. Historically, we've emitted the unexpected
29873 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29874 Emit both to give time for other tools to adapt. */
29875 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29876 if (! dwarf_strict && dwarf_version < 4)
29877 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29879 add_ranges (NULL);
29883 /* AIX Assembler inserts the length, so adjust the reference to match the
29884 offset expected by debuggers. */
29885 strcpy (dl_section_ref, debug_line_section_label);
29886 if (XCOFF_DEBUGGING_INFO)
29887 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29889 if (debug_info_level >= DINFO_LEVEL_TERSE)
29890 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29891 dl_section_ref);
29893 if (have_macinfo)
29894 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29895 macinfo_section_label);
29897 if (dwarf_split_debug_info)
29899 if (have_location_lists)
29901 if (dwarf_version >= 5)
29902 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29903 loc_section_label);
29904 /* optimize_location_lists calculates the size of the lists,
29905 so index them first, and assign indices to the entries.
29906 Although optimize_location_lists will remove entries from
29907 the table, it only does so for duplicates, and therefore
29908 only reduces ref_counts to 1. */
29909 index_location_lists (comp_unit_die ());
29912 if (addr_index_table != NULL)
29914 unsigned int index = 0;
29915 addr_index_table
29916 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29917 (&index);
29921 loc_list_idx = 0;
29922 if (have_location_lists)
29924 optimize_location_lists (comp_unit_die ());
29925 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29926 if (dwarf_version >= 5 && dwarf_split_debug_info)
29927 assign_location_list_indexes (comp_unit_die ());
29930 save_macinfo_strings ();
29932 if (dwarf_split_debug_info)
29934 unsigned int index = 0;
29936 /* Add attributes common to skeleton compile_units and
29937 type_units. Because these attributes include strings, it
29938 must be done before freezing the string table. Top-level
29939 skeleton die attrs are added when the skeleton type unit is
29940 created, so ensure it is created by this point. */
29941 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29942 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29945 /* Output all of the compilation units. We put the main one last so that
29946 the offsets are available to output_pubnames. */
29947 for (node = cu_die_list; node; node = node->next)
29948 output_comp_unit (node->die, 0, NULL);
29950 hash_table<comdat_type_hasher> comdat_type_table (100);
29951 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29953 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29955 /* Don't output duplicate types. */
29956 if (*slot != HTAB_EMPTY_ENTRY)
29957 continue;
29959 /* Add a pointer to the line table for the main compilation unit
29960 so that the debugger can make sense of DW_AT_decl_file
29961 attributes. */
29962 if (debug_info_level >= DINFO_LEVEL_TERSE)
29963 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29964 (!dwarf_split_debug_info
29965 ? dl_section_ref
29966 : debug_skeleton_line_section_label));
29968 output_comdat_type_unit (ctnode);
29969 *slot = ctnode;
29972 /* The AT_pubnames attribute needs to go in all skeleton dies, including
29973 both the main_cu and all skeleton TUs. Making this call unconditional
29974 would end up either adding a second copy of the AT_pubnames attribute, or
29975 requiring a special case in add_top_level_skeleton_die_attrs. */
29976 if (!dwarf_split_debug_info)
29977 add_AT_pubnames (comp_unit_die ());
29979 if (dwarf_split_debug_info)
29981 int mark;
29982 struct md5_ctx ctx;
29984 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
29985 index_rnglists ();
29987 /* Compute a checksum of the comp_unit to use as the dwo_id. */
29988 md5_init_ctx (&ctx);
29989 mark = 0;
29990 die_checksum (comp_unit_die (), &ctx, &mark);
29991 unmark_all_dies (comp_unit_die ());
29992 md5_finish_ctx (&ctx, checksum);
29994 if (dwarf_version < 5)
29996 /* Use the first 8 bytes of the checksum as the dwo_id,
29997 and add it to both comp-unit DIEs. */
29998 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
29999 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30002 /* Add the base offset of the ranges table to the skeleton
30003 comp-unit DIE. */
30004 if (!vec_safe_is_empty (ranges_table))
30006 if (dwarf_version >= 5)
30007 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30008 ranges_base_label);
30009 else
30010 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30011 ranges_section_label);
30014 switch_to_section (debug_addr_section);
30015 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30016 output_addr_table ();
30019 /* Output the main compilation unit if non-empty or if .debug_macinfo
30020 or .debug_macro will be emitted. */
30021 output_comp_unit (comp_unit_die (), have_macinfo,
30022 dwarf_split_debug_info ? checksum : NULL);
30024 if (dwarf_split_debug_info && info_section_emitted)
30025 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30027 /* Output the abbreviation table. */
30028 if (vec_safe_length (abbrev_die_table) != 1)
30030 switch_to_section (debug_abbrev_section);
30031 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30032 output_abbrev_section ();
30035 /* Output location list section if necessary. */
30036 if (have_location_lists)
30038 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30039 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30040 /* Output the location lists info. */
30041 switch_to_section (debug_loc_section);
30042 if (dwarf_version >= 5)
30044 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30045 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30046 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30047 dw2_asm_output_data (4, 0xffffffff,
30048 "Initial length escape value indicating "
30049 "64-bit DWARF extension");
30050 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30051 "Length of Location Lists");
30052 ASM_OUTPUT_LABEL (asm_out_file, l1);
30053 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30054 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30055 dw2_asm_output_data (1, 0, "Segment Size");
30056 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30057 "Offset Entry Count");
30059 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30060 if (dwarf_version >= 5 && dwarf_split_debug_info)
30062 unsigned int save_loc_list_idx = loc_list_idx;
30063 loc_list_idx = 0;
30064 output_loclists_offsets (comp_unit_die ());
30065 gcc_assert (save_loc_list_idx == loc_list_idx);
30067 output_location_lists (comp_unit_die ());
30068 if (dwarf_version >= 5)
30069 ASM_OUTPUT_LABEL (asm_out_file, l2);
30072 output_pubtables ();
30074 /* Output the address range information if a CU (.debug_info section)
30075 was emitted. We output an empty table even if we had no functions
30076 to put in it. This because the consumer has no way to tell the
30077 difference between an empty table that we omitted and failure to
30078 generate a table that would have contained data. */
30079 if (info_section_emitted)
30081 switch_to_section (debug_aranges_section);
30082 output_aranges ();
30085 /* Output ranges section if necessary. */
30086 if (!vec_safe_is_empty (ranges_table))
30088 if (dwarf_version >= 5)
30089 output_rnglists ();
30090 else
30091 output_ranges ();
30094 /* Have to end the macro section. */
30095 if (have_macinfo)
30097 switch_to_section (debug_macinfo_section);
30098 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30099 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30100 : debug_skeleton_line_section_label, false);
30101 dw2_asm_output_data (1, 0, "End compilation unit");
30104 /* Output the source line correspondence table. We must do this
30105 even if there is no line information. Otherwise, on an empty
30106 translation unit, we will generate a present, but empty,
30107 .debug_info section. IRIX 6.5 `nm' will then complain when
30108 examining the file. This is done late so that any filenames
30109 used by the debug_info section are marked as 'used'. */
30110 switch_to_section (debug_line_section);
30111 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30112 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30113 output_line_info (false);
30115 if (dwarf_split_debug_info && info_section_emitted)
30117 switch_to_section (debug_skeleton_line_section);
30118 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30119 output_line_info (true);
30122 /* If we emitted any indirect strings, output the string table too. */
30123 if (debug_str_hash || skeleton_debug_str_hash)
30124 output_indirect_strings ();
30125 if (debug_line_str_hash)
30127 switch_to_section (debug_line_str_section);
30128 const enum dwarf_form form = DW_FORM_line_strp;
30129 debug_line_str_hash->traverse<enum dwarf_form,
30130 output_indirect_string> (form);
30134 /* Returns a hash value for X (which really is a variable_value_struct). */
30136 inline hashval_t
30137 variable_value_hasher::hash (variable_value_struct *x)
30139 return (hashval_t) x->decl_id;
30142 /* Return nonzero if decl_id of variable_value_struct X is the same as
30143 UID of decl Y. */
30145 inline bool
30146 variable_value_hasher::equal (variable_value_struct *x, tree y)
30148 return x->decl_id == DECL_UID (y);
30151 /* Helper function for resolve_variable_value, handle
30152 DW_OP_GNU_variable_value in one location expression.
30153 Return true if exprloc has been changed into loclist. */
30155 static bool
30156 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30158 dw_loc_descr_ref next;
30159 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30161 next = loc->dw_loc_next;
30162 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30163 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30164 continue;
30166 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30167 if (DECL_CONTEXT (decl) != current_function_decl)
30168 continue;
30170 dw_die_ref ref = lookup_decl_die (decl);
30171 if (ref)
30173 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30174 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30175 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30176 continue;
30178 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30179 if (l == NULL)
30180 continue;
30181 if (l->dw_loc_next)
30183 if (AT_class (a) != dw_val_class_loc)
30184 continue;
30185 switch (a->dw_attr)
30187 /* Following attributes allow both exprloc and loclist
30188 classes, so we can change them into a loclist. */
30189 case DW_AT_location:
30190 case DW_AT_string_length:
30191 case DW_AT_return_addr:
30192 case DW_AT_data_member_location:
30193 case DW_AT_frame_base:
30194 case DW_AT_segment:
30195 case DW_AT_static_link:
30196 case DW_AT_use_location:
30197 case DW_AT_vtable_elem_location:
30198 if (prev)
30200 prev->dw_loc_next = NULL;
30201 prepend_loc_descr_to_each (l, AT_loc (a));
30203 if (next)
30204 add_loc_descr_to_each (l, next);
30205 a->dw_attr_val.val_class = dw_val_class_loc_list;
30206 a->dw_attr_val.val_entry = NULL;
30207 a->dw_attr_val.v.val_loc_list = l;
30208 have_location_lists = true;
30209 return true;
30210 /* Following attributes allow both exprloc and reference,
30211 so if the whole expression is DW_OP_GNU_variable_value alone
30212 we could transform it into reference. */
30213 case DW_AT_byte_size:
30214 case DW_AT_bit_size:
30215 case DW_AT_lower_bound:
30216 case DW_AT_upper_bound:
30217 case DW_AT_bit_stride:
30218 case DW_AT_count:
30219 case DW_AT_allocated:
30220 case DW_AT_associated:
30221 case DW_AT_byte_stride:
30222 if (prev == NULL && next == NULL)
30223 break;
30224 /* FALLTHRU */
30225 default:
30226 if (dwarf_strict)
30227 continue;
30228 break;
30230 /* Create DW_TAG_variable that we can refer to. */
30231 gen_decl_die (decl, NULL_TREE, NULL,
30232 lookup_decl_die (current_function_decl));
30233 ref = lookup_decl_die (decl);
30234 if (ref)
30236 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30237 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30238 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30240 continue;
30242 if (prev)
30244 prev->dw_loc_next = l->expr;
30245 add_loc_descr (&prev->dw_loc_next, next);
30246 free_loc_descr (loc, NULL);
30247 next = prev->dw_loc_next;
30249 else
30251 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30252 add_loc_descr (&loc, next);
30253 next = loc;
30255 loc = prev;
30257 return false;
30260 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30262 static void
30263 resolve_variable_value (dw_die_ref die)
30265 dw_attr_node *a;
30266 dw_loc_list_ref loc;
30267 unsigned ix;
30269 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30270 switch (AT_class (a))
30272 case dw_val_class_loc:
30273 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30274 break;
30275 /* FALLTHRU */
30276 case dw_val_class_loc_list:
30277 loc = AT_loc_list (a);
30278 gcc_assert (loc);
30279 for (; loc; loc = loc->dw_loc_next)
30280 resolve_variable_value_in_expr (a, loc->expr);
30281 break;
30282 default:
30283 break;
30287 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30288 temporaries in the current function. */
30290 static void
30291 resolve_variable_values (void)
30293 if (!variable_value_hash || !current_function_decl)
30294 return;
30296 struct variable_value_struct *node
30297 = variable_value_hash->find_with_hash (current_function_decl,
30298 DECL_UID (current_function_decl));
30300 if (node == NULL)
30301 return;
30303 unsigned int i;
30304 dw_die_ref die;
30305 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30306 resolve_variable_value (die);
30309 /* Helper function for note_variable_value, handle one location
30310 expression. */
30312 static void
30313 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30315 for (; loc; loc = loc->dw_loc_next)
30316 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30317 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30319 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30320 dw_die_ref ref = lookup_decl_die (decl);
30321 if (! ref && (flag_generate_lto || flag_generate_offload))
30323 /* ??? This is somewhat a hack because we do not create DIEs
30324 for variables not in BLOCK trees early but when generating
30325 early LTO output we need the dw_val_class_decl_ref to be
30326 fully resolved. For fat LTO objects we'd also like to
30327 undo this after LTO dwarf output. */
30328 gcc_assert (DECL_CONTEXT (decl));
30329 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30330 gcc_assert (ctx != NULL);
30331 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30332 ref = lookup_decl_die (decl);
30333 gcc_assert (ref != NULL);
30335 if (ref)
30337 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30338 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30339 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30340 continue;
30342 if (VAR_P (decl)
30343 && DECL_CONTEXT (decl)
30344 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30345 && lookup_decl_die (DECL_CONTEXT (decl)))
30347 if (!variable_value_hash)
30348 variable_value_hash
30349 = hash_table<variable_value_hasher>::create_ggc (10);
30351 tree fndecl = DECL_CONTEXT (decl);
30352 struct variable_value_struct *node;
30353 struct variable_value_struct **slot
30354 = variable_value_hash->find_slot_with_hash (fndecl,
30355 DECL_UID (fndecl),
30356 INSERT);
30357 if (*slot == NULL)
30359 node = ggc_cleared_alloc<variable_value_struct> ();
30360 node->decl_id = DECL_UID (fndecl);
30361 *slot = node;
30363 else
30364 node = *slot;
30366 vec_safe_push (node->dies, die);
30371 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30372 with dw_val_class_decl_ref operand. */
30374 static void
30375 note_variable_value (dw_die_ref die)
30377 dw_die_ref c;
30378 dw_attr_node *a;
30379 dw_loc_list_ref loc;
30380 unsigned ix;
30382 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30383 switch (AT_class (a))
30385 case dw_val_class_loc_list:
30386 loc = AT_loc_list (a);
30387 gcc_assert (loc);
30388 if (!loc->noted_variable_value)
30390 loc->noted_variable_value = 1;
30391 for (; loc; loc = loc->dw_loc_next)
30392 note_variable_value_in_expr (die, loc->expr);
30394 break;
30395 case dw_val_class_loc:
30396 note_variable_value_in_expr (die, AT_loc (a));
30397 break;
30398 default:
30399 break;
30402 /* Mark children. */
30403 FOR_EACH_CHILD (die, c, note_variable_value (c));
30406 /* Perform any cleanups needed after the early debug generation pass
30407 has run. */
30409 static void
30410 dwarf2out_early_finish (const char *filename)
30412 set_early_dwarf s;
30414 /* PCH might result in DW_AT_producer string being restored from the
30415 header compilation, so always fill it with empty string initially
30416 and overwrite only here. */
30417 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30418 producer_string = gen_producer_string ();
30419 producer->dw_attr_val.v.val_str->refcount--;
30420 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30422 /* Add the name for the main input file now. We delayed this from
30423 dwarf2out_init to avoid complications with PCH. */
30424 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30425 add_comp_dir_attribute (comp_unit_die ());
30427 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30428 DW_AT_comp_dir into .debug_line_str section. */
30429 if (!DWARF2_ASM_LINE_DEBUG_INFO
30430 && dwarf_version >= 5
30431 && DWARF5_USE_DEBUG_LINE_STR)
30433 for (int i = 0; i < 2; i++)
30435 dw_attr_node *a = get_AT (comp_unit_die (),
30436 i ? DW_AT_comp_dir : DW_AT_name);
30437 if (a == NULL
30438 || AT_class (a) != dw_val_class_str
30439 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30440 continue;
30442 if (! debug_line_str_hash)
30443 debug_line_str_hash
30444 = hash_table<indirect_string_hasher>::create_ggc (10);
30446 struct indirect_string_node *node
30447 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30448 set_indirect_string (node);
30449 node->form = DW_FORM_line_strp;
30450 a->dw_attr_val.v.val_str->refcount--;
30451 a->dw_attr_val.v.val_str = node;
30455 /* With LTO early dwarf was really finished at compile-time, so make
30456 sure to adjust the phase after annotating the LTRANS CU DIE. */
30457 if (in_lto_p)
30459 early_dwarf_finished = true;
30460 return;
30463 /* Walk through the list of incomplete types again, trying once more to
30464 emit full debugging info for them. */
30465 retry_incomplete_types ();
30467 /* The point here is to flush out the limbo list so that it is empty
30468 and we don't need to stream it for LTO. */
30469 flush_limbo_die_list ();
30471 gen_scheduled_generic_parms_dies ();
30472 gen_remaining_tmpl_value_param_die_attribute ();
30474 /* Add DW_AT_linkage_name for all deferred DIEs. */
30475 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30477 tree decl = node->created_for;
30478 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30479 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30480 ended up in deferred_asm_name before we knew it was
30481 constant and never written to disk. */
30482 && DECL_ASSEMBLER_NAME (decl))
30484 add_linkage_attr (node->die, decl);
30485 move_linkage_attr (node->die);
30488 deferred_asm_name = NULL;
30490 if (flag_eliminate_unused_debug_types)
30491 prune_unused_types ();
30493 /* Generate separate COMDAT sections for type DIEs. */
30494 if (use_debug_types)
30496 break_out_comdat_types (comp_unit_die ());
30498 /* Each new type_unit DIE was added to the limbo die list when created.
30499 Since these have all been added to comdat_type_list, clear the
30500 limbo die list. */
30501 limbo_die_list = NULL;
30503 /* For each new comdat type unit, copy declarations for incomplete
30504 types to make the new unit self-contained (i.e., no direct
30505 references to the main compile unit). */
30506 for (comdat_type_node *ctnode = comdat_type_list;
30507 ctnode != NULL; ctnode = ctnode->next)
30508 copy_decls_for_unworthy_types (ctnode->root_die);
30509 copy_decls_for_unworthy_types (comp_unit_die ());
30511 /* In the process of copying declarations from one unit to another,
30512 we may have left some declarations behind that are no longer
30513 referenced. Prune them. */
30514 prune_unused_types ();
30517 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30518 with dw_val_class_decl_ref operand. */
30519 note_variable_value (comp_unit_die ());
30520 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30521 note_variable_value (node->die);
30522 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30523 ctnode = ctnode->next)
30524 note_variable_value (ctnode->root_die);
30525 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30526 note_variable_value (node->die);
30528 /* The early debug phase is now finished. */
30529 early_dwarf_finished = true;
30531 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30532 if (!flag_generate_lto && !flag_generate_offload)
30533 return;
30535 /* Now as we are going to output for LTO initialize sections and labels
30536 to the LTO variants. We don't need a random-seed postfix as other
30537 LTO sections as linking the LTO debug sections into one in a partial
30538 link is fine. */
30539 init_sections_and_labels (true);
30541 /* The output below is modeled after dwarf2out_finish with all
30542 location related output removed and some LTO specific changes.
30543 Some refactoring might make both smaller and easier to match up. */
30545 /* Traverse the DIE's and add add sibling attributes to those DIE's
30546 that have children. */
30547 add_sibling_attributes (comp_unit_die ());
30548 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30549 add_sibling_attributes (node->die);
30550 for (comdat_type_node *ctnode = comdat_type_list;
30551 ctnode != NULL; ctnode = ctnode->next)
30552 add_sibling_attributes (ctnode->root_die);
30554 if (have_macinfo)
30555 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30556 macinfo_section_label);
30558 save_macinfo_strings ();
30560 /* Output all of the compilation units. We put the main one last so that
30561 the offsets are available to output_pubnames. */
30562 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30563 output_comp_unit (node->die, 0, NULL);
30565 hash_table<comdat_type_hasher> comdat_type_table (100);
30566 for (comdat_type_node *ctnode = comdat_type_list;
30567 ctnode != NULL; ctnode = ctnode->next)
30569 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30571 /* Don't output duplicate types. */
30572 if (*slot != HTAB_EMPTY_ENTRY)
30573 continue;
30575 /* Add a pointer to the line table for the main compilation unit
30576 so that the debugger can make sense of DW_AT_decl_file
30577 attributes. */
30578 if (debug_info_level >= DINFO_LEVEL_TERSE)
30579 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30580 (!dwarf_split_debug_info
30581 ? debug_line_section_label
30582 : debug_skeleton_line_section_label));
30584 output_comdat_type_unit (ctnode);
30585 *slot = ctnode;
30588 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30589 both the main_cu and all skeleton TUs. Making this call unconditional
30590 would end up either adding a second copy of the AT_pubnames attribute, or
30591 requiring a special case in add_top_level_skeleton_die_attrs. */
30592 if (!dwarf_split_debug_info)
30593 add_AT_pubnames (comp_unit_die ());
30595 /* Stick a unique symbol to the main debuginfo section. */
30596 compute_comp_unit_symbol (comp_unit_die ());
30598 /* Output the main compilation unit. We always need it if only for
30599 the CU symbol. */
30600 output_comp_unit (comp_unit_die (), true, NULL);
30602 /* Output the abbreviation table. */
30603 if (vec_safe_length (abbrev_die_table) != 1)
30605 switch_to_section (debug_abbrev_section);
30606 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30607 output_abbrev_section ();
30610 /* Have to end the macro section. */
30611 if (have_macinfo)
30613 /* We have to save macinfo state if we need to output it again
30614 for the FAT part of the object. */
30615 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30616 if (flag_fat_lto_objects)
30617 macinfo_table = macinfo_table->copy ();
30619 switch_to_section (debug_macinfo_section);
30620 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30621 output_macinfo (debug_skeleton_line_section_label, true);
30622 dw2_asm_output_data (1, 0, "End compilation unit");
30624 /* Emit a skeleton debug_line section. */
30625 switch_to_section (debug_skeleton_line_section);
30626 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30627 output_line_info (true);
30629 if (flag_fat_lto_objects)
30631 vec_free (macinfo_table);
30632 macinfo_table = saved_macinfo_table;
30637 /* If we emitted any indirect strings, output the string table too. */
30638 if (debug_str_hash || skeleton_debug_str_hash)
30639 output_indirect_strings ();
30641 /* Switch back to the text section. */
30642 switch_to_section (text_section);
30645 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30646 within the same process. For use by toplev::finalize. */
30648 void
30649 dwarf2out_c_finalize (void)
30651 last_var_location_insn = NULL;
30652 cached_next_real_insn = NULL;
30653 used_rtx_array = NULL;
30654 incomplete_types = NULL;
30655 decl_scope_table = NULL;
30656 debug_info_section = NULL;
30657 debug_skeleton_info_section = NULL;
30658 debug_abbrev_section = NULL;
30659 debug_skeleton_abbrev_section = NULL;
30660 debug_aranges_section = NULL;
30661 debug_addr_section = NULL;
30662 debug_macinfo_section = NULL;
30663 debug_line_section = NULL;
30664 debug_skeleton_line_section = NULL;
30665 debug_loc_section = NULL;
30666 debug_pubnames_section = NULL;
30667 debug_pubtypes_section = NULL;
30668 debug_str_section = NULL;
30669 debug_line_str_section = NULL;
30670 debug_str_dwo_section = NULL;
30671 debug_str_offsets_section = NULL;
30672 debug_ranges_section = NULL;
30673 debug_frame_section = NULL;
30674 fde_vec = NULL;
30675 debug_str_hash = NULL;
30676 debug_line_str_hash = NULL;
30677 skeleton_debug_str_hash = NULL;
30678 dw2_string_counter = 0;
30679 have_multiple_function_sections = false;
30680 text_section_used = false;
30681 cold_text_section_used = false;
30682 cold_text_section = NULL;
30683 current_unit_personality = NULL;
30685 early_dwarf = false;
30686 early_dwarf_finished = false;
30688 next_die_offset = 0;
30689 single_comp_unit_die = NULL;
30690 comdat_type_list = NULL;
30691 limbo_die_list = NULL;
30692 file_table = NULL;
30693 decl_die_table = NULL;
30694 common_block_die_table = NULL;
30695 decl_loc_table = NULL;
30696 call_arg_locations = NULL;
30697 call_arg_loc_last = NULL;
30698 call_site_count = -1;
30699 tail_call_site_count = -1;
30700 cached_dw_loc_list_table = NULL;
30701 abbrev_die_table = NULL;
30702 delete dwarf_proc_stack_usage_map;
30703 dwarf_proc_stack_usage_map = NULL;
30704 line_info_label_num = 0;
30705 cur_line_info_table = NULL;
30706 text_section_line_info = NULL;
30707 cold_text_section_line_info = NULL;
30708 separate_line_info = NULL;
30709 info_section_emitted = false;
30710 pubname_table = NULL;
30711 pubtype_table = NULL;
30712 macinfo_table = NULL;
30713 ranges_table = NULL;
30714 ranges_by_label = NULL;
30715 rnglist_idx = 0;
30716 have_location_lists = false;
30717 loclabel_num = 0;
30718 poc_label_num = 0;
30719 last_emitted_file = NULL;
30720 label_num = 0;
30721 tmpl_value_parm_die_table = NULL;
30722 generic_type_instances = NULL;
30723 frame_pointer_fb_offset = 0;
30724 frame_pointer_fb_offset_valid = false;
30725 base_types.release ();
30726 XDELETEVEC (producer_string);
30727 producer_string = NULL;
30730 #include "gt-dwarf2out.h"