PR rtl-optimization/82913
[official-gcc.git] / gcc / dwarf2out.c
blobb8f4e4888f1bc0884564bf9b972f34295def1aa8
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 (dw_die_ref);
3476 static void output_compilation_unit_header (enum dwarf_unit_type);
3477 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3478 static void output_comdat_type_unit (comdat_type_node *);
3479 static const char *dwarf2_name (tree, int);
3480 static void add_pubname (tree, dw_die_ref);
3481 static void add_enumerator_pubname (const char *, dw_die_ref);
3482 static void add_pubname_string (const char *, dw_die_ref);
3483 static void add_pubtype (tree, dw_die_ref);
3484 static void output_pubnames (vec<pubname_entry, va_gc> *);
3485 static void output_aranges (void);
3486 static unsigned int add_ranges (const_tree, bool = false);
3487 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3488 bool *, bool);
3489 static void output_ranges (void);
3490 static dw_line_info_table *new_line_info_table (void);
3491 static void output_line_info (bool);
3492 static void output_file_names (void);
3493 static dw_die_ref base_type_die (tree, bool);
3494 static int is_base_type (tree);
3495 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3496 static int decl_quals (const_tree);
3497 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3498 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3499 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3500 static int type_is_enum (const_tree);
3501 static unsigned int dbx_reg_number (const_rtx);
3502 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3503 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3504 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3505 enum var_init_status);
3506 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3507 enum var_init_status);
3508 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3509 enum var_init_status);
3510 static int is_based_loc (const_rtx);
3511 static bool resolve_one_addr (rtx *);
3512 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3513 enum var_init_status);
3514 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3515 enum var_init_status);
3516 struct loc_descr_context;
3517 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3518 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3519 static dw_loc_list_ref loc_list_from_tree (tree, int,
3520 struct loc_descr_context *);
3521 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3522 struct loc_descr_context *);
3523 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3524 static tree field_type (const_tree);
3525 static unsigned int simple_type_align_in_bits (const_tree);
3526 static unsigned int simple_decl_align_in_bits (const_tree);
3527 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3528 struct vlr_context;
3529 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3530 HOST_WIDE_INT *);
3531 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3532 dw_loc_list_ref);
3533 static void add_data_member_location_attribute (dw_die_ref, tree,
3534 struct vlr_context *);
3535 static bool add_const_value_attribute (dw_die_ref, rtx);
3536 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3537 static void insert_wide_int (const wide_int &, unsigned char *, int);
3538 static void insert_float (const_rtx, unsigned char *);
3539 static rtx rtl_for_decl_location (tree);
3540 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3541 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3542 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3543 static void add_name_attribute (dw_die_ref, const char *);
3544 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3545 static void add_comp_dir_attribute (dw_die_ref);
3546 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3547 struct loc_descr_context *);
3548 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3549 struct loc_descr_context *);
3550 static void add_subscript_info (dw_die_ref, tree, bool);
3551 static void add_byte_size_attribute (dw_die_ref, tree);
3552 static void add_alignment_attribute (dw_die_ref, tree);
3553 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3554 struct vlr_context *);
3555 static void add_bit_size_attribute (dw_die_ref, tree);
3556 static void add_prototyped_attribute (dw_die_ref, tree);
3557 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3558 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3559 static void add_src_coords_attributes (dw_die_ref, tree);
3560 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3561 static void add_discr_value (dw_die_ref, dw_discr_value *);
3562 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3563 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3564 static void push_decl_scope (tree);
3565 static void pop_decl_scope (void);
3566 static dw_die_ref scope_die_for (tree, dw_die_ref);
3567 static inline int local_scope_p (dw_die_ref);
3568 static inline int class_scope_p (dw_die_ref);
3569 static inline int class_or_namespace_scope_p (dw_die_ref);
3570 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3571 static void add_calling_convention_attribute (dw_die_ref, tree);
3572 static const char *type_tag (const_tree);
3573 static tree member_declared_type (const_tree);
3574 #if 0
3575 static const char *decl_start_label (tree);
3576 #endif
3577 static void gen_array_type_die (tree, dw_die_ref);
3578 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3579 #if 0
3580 static void gen_entry_point_die (tree, dw_die_ref);
3581 #endif
3582 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3583 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3584 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3585 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3586 static void gen_formal_types_die (tree, dw_die_ref);
3587 static void gen_subprogram_die (tree, dw_die_ref);
3588 static void gen_variable_die (tree, tree, dw_die_ref);
3589 static void gen_const_die (tree, dw_die_ref);
3590 static void gen_label_die (tree, dw_die_ref);
3591 static void gen_lexical_block_die (tree, dw_die_ref);
3592 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3593 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3594 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3595 static dw_die_ref gen_compile_unit_die (const char *);
3596 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3597 static void gen_member_die (tree, dw_die_ref);
3598 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3599 enum debug_info_usage);
3600 static void gen_subroutine_type_die (tree, dw_die_ref);
3601 static void gen_typedef_die (tree, dw_die_ref);
3602 static void gen_type_die (tree, dw_die_ref);
3603 static void gen_block_die (tree, dw_die_ref);
3604 static void decls_for_scope (tree, dw_die_ref);
3605 static bool is_naming_typedef_decl (const_tree);
3606 static inline dw_die_ref get_context_die (tree);
3607 static void gen_namespace_die (tree, dw_die_ref);
3608 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3609 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3610 static dw_die_ref force_decl_die (tree);
3611 static dw_die_ref force_type_die (tree);
3612 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3613 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3614 static struct dwarf_file_data * lookup_filename (const char *);
3615 static void retry_incomplete_types (void);
3616 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3617 static void gen_generic_params_dies (tree);
3618 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3619 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3620 static void splice_child_die (dw_die_ref, dw_die_ref);
3621 static int file_info_cmp (const void *, const void *);
3622 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3623 const char *, const char *);
3624 static void output_loc_list (dw_loc_list_ref);
3625 static char *gen_internal_sym (const char *);
3626 static bool want_pubnames (void);
3628 static void prune_unmark_dies (dw_die_ref);
3629 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3630 static void prune_unused_types_mark (dw_die_ref, int);
3631 static void prune_unused_types_walk (dw_die_ref);
3632 static void prune_unused_types_walk_attribs (dw_die_ref);
3633 static void prune_unused_types_prune (dw_die_ref);
3634 static void prune_unused_types (void);
3635 static int maybe_emit_file (struct dwarf_file_data *fd);
3636 static inline const char *AT_vms_delta1 (dw_attr_node *);
3637 static inline const char *AT_vms_delta2 (dw_attr_node *);
3638 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3639 const char *, const char *);
3640 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3641 static void gen_remaining_tmpl_value_param_die_attribute (void);
3642 static bool generic_type_p (tree);
3643 static void schedule_generic_params_dies_gen (tree t);
3644 static void gen_scheduled_generic_parms_dies (void);
3645 static void resolve_variable_values (void);
3647 static const char *comp_dir_string (void);
3649 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3651 /* enum for tracking thread-local variables whose address is really an offset
3652 relative to the TLS pointer, which will need link-time relocation, but will
3653 not need relocation by the DWARF consumer. */
3655 enum dtprel_bool
3657 dtprel_false = 0,
3658 dtprel_true = 1
3661 /* Return the operator to use for an address of a variable. For dtprel_true, we
3662 use DW_OP_const*. For regular variables, which need both link-time
3663 relocation and consumer-level relocation (e.g., to account for shared objects
3664 loaded at a random address), we use DW_OP_addr*. */
3666 static inline enum dwarf_location_atom
3667 dw_addr_op (enum dtprel_bool dtprel)
3669 if (dtprel == dtprel_true)
3670 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3671 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3672 else
3673 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3676 /* Return a pointer to a newly allocated address location description. If
3677 dwarf_split_debug_info is true, then record the address with the appropriate
3678 relocation. */
3679 static inline dw_loc_descr_ref
3680 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3682 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3684 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3685 ref->dw_loc_oprnd1.v.val_addr = addr;
3686 ref->dtprel = dtprel;
3687 if (dwarf_split_debug_info)
3688 ref->dw_loc_oprnd1.val_entry
3689 = add_addr_table_entry (addr,
3690 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3691 else
3692 ref->dw_loc_oprnd1.val_entry = NULL;
3694 return ref;
3697 /* Section names used to hold DWARF debugging information. */
3699 #ifndef DEBUG_INFO_SECTION
3700 #define DEBUG_INFO_SECTION ".debug_info"
3701 #endif
3702 #ifndef DEBUG_DWO_INFO_SECTION
3703 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3704 #endif
3705 #ifndef DEBUG_LTO_INFO_SECTION
3706 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3707 #endif
3708 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3709 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3710 #endif
3711 #ifndef DEBUG_ABBREV_SECTION
3712 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3713 #endif
3714 #ifndef DEBUG_LTO_ABBREV_SECTION
3715 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3716 #endif
3717 #ifndef DEBUG_DWO_ABBREV_SECTION
3718 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3719 #endif
3720 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3721 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3722 #endif
3723 #ifndef DEBUG_ARANGES_SECTION
3724 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3725 #endif
3726 #ifndef DEBUG_ADDR_SECTION
3727 #define DEBUG_ADDR_SECTION ".debug_addr"
3728 #endif
3729 #ifndef DEBUG_MACINFO_SECTION
3730 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3731 #endif
3732 #ifndef DEBUG_LTO_MACINFO_SECTION
3733 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3734 #endif
3735 #ifndef DEBUG_DWO_MACINFO_SECTION
3736 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3737 #endif
3738 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3739 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3740 #endif
3741 #ifndef DEBUG_MACRO_SECTION
3742 #define DEBUG_MACRO_SECTION ".debug_macro"
3743 #endif
3744 #ifndef DEBUG_LTO_MACRO_SECTION
3745 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3746 #endif
3747 #ifndef DEBUG_DWO_MACRO_SECTION
3748 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3749 #endif
3750 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3751 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3752 #endif
3753 #ifndef DEBUG_LINE_SECTION
3754 #define DEBUG_LINE_SECTION ".debug_line"
3755 #endif
3756 #ifndef DEBUG_LTO_LINE_SECTION
3757 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3758 #endif
3759 #ifndef DEBUG_DWO_LINE_SECTION
3760 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3761 #endif
3762 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3763 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3764 #endif
3765 #ifndef DEBUG_LOC_SECTION
3766 #define DEBUG_LOC_SECTION ".debug_loc"
3767 #endif
3768 #ifndef DEBUG_DWO_LOC_SECTION
3769 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3770 #endif
3771 #ifndef DEBUG_LOCLISTS_SECTION
3772 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3773 #endif
3774 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3775 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3776 #endif
3777 #ifndef DEBUG_PUBNAMES_SECTION
3778 #define DEBUG_PUBNAMES_SECTION \
3779 ((debug_generate_pub_sections == 2) \
3780 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3781 #endif
3782 #ifndef DEBUG_PUBTYPES_SECTION
3783 #define DEBUG_PUBTYPES_SECTION \
3784 ((debug_generate_pub_sections == 2) \
3785 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3786 #endif
3787 #ifndef DEBUG_STR_OFFSETS_SECTION
3788 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3789 #endif
3790 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3791 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3792 #endif
3793 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3794 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3795 #endif
3796 #ifndef DEBUG_STR_SECTION
3797 #define DEBUG_STR_SECTION ".debug_str"
3798 #endif
3799 #ifndef DEBUG_LTO_STR_SECTION
3800 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3801 #endif
3802 #ifndef DEBUG_STR_DWO_SECTION
3803 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3804 #endif
3805 #ifndef DEBUG_LTO_STR_DWO_SECTION
3806 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3807 #endif
3808 #ifndef DEBUG_RANGES_SECTION
3809 #define DEBUG_RANGES_SECTION ".debug_ranges"
3810 #endif
3811 #ifndef DEBUG_RNGLISTS_SECTION
3812 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3813 #endif
3814 #ifndef DEBUG_LINE_STR_SECTION
3815 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3816 #endif
3817 #ifndef DEBUG_LTO_LINE_STR_SECTION
3818 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3819 #endif
3821 /* Standard ELF section names for compiled code and data. */
3822 #ifndef TEXT_SECTION_NAME
3823 #define TEXT_SECTION_NAME ".text"
3824 #endif
3826 /* Section flags for .debug_str section. */
3827 #define DEBUG_STR_SECTION_FLAGS \
3828 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3829 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3830 : SECTION_DEBUG)
3832 /* Section flags for .debug_str.dwo section. */
3833 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3835 /* Attribute used to refer to the macro section. */
3836 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3837 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3839 /* Labels we insert at beginning sections we can reference instead of
3840 the section names themselves. */
3842 #ifndef TEXT_SECTION_LABEL
3843 #define TEXT_SECTION_LABEL "Ltext"
3844 #endif
3845 #ifndef COLD_TEXT_SECTION_LABEL
3846 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3847 #endif
3848 #ifndef DEBUG_LINE_SECTION_LABEL
3849 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3850 #endif
3851 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3852 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3853 #endif
3854 #ifndef DEBUG_INFO_SECTION_LABEL
3855 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3856 #endif
3857 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3858 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3859 #endif
3860 #ifndef DEBUG_ABBREV_SECTION_LABEL
3861 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3862 #endif
3863 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3864 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3865 #endif
3866 #ifndef DEBUG_ADDR_SECTION_LABEL
3867 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3868 #endif
3869 #ifndef DEBUG_LOC_SECTION_LABEL
3870 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3871 #endif
3872 #ifndef DEBUG_RANGES_SECTION_LABEL
3873 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3874 #endif
3875 #ifndef DEBUG_MACINFO_SECTION_LABEL
3876 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3877 #endif
3878 #ifndef DEBUG_MACRO_SECTION_LABEL
3879 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3880 #endif
3881 #define SKELETON_COMP_DIE_ABBREV 1
3882 #define SKELETON_TYPE_DIE_ABBREV 2
3884 /* Definitions of defaults for formats and names of various special
3885 (artificial) labels which may be generated within this file (when the -g
3886 options is used and DWARF2_DEBUGGING_INFO is in effect.
3887 If necessary, these may be overridden from within the tm.h file, but
3888 typically, overriding these defaults is unnecessary. */
3890 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3891 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3892 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3900 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3901 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3904 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3906 #ifndef TEXT_END_LABEL
3907 #define TEXT_END_LABEL "Letext"
3908 #endif
3909 #ifndef COLD_END_LABEL
3910 #define COLD_END_LABEL "Letext_cold"
3911 #endif
3912 #ifndef BLOCK_BEGIN_LABEL
3913 #define BLOCK_BEGIN_LABEL "LBB"
3914 #endif
3915 #ifndef BLOCK_END_LABEL
3916 #define BLOCK_END_LABEL "LBE"
3917 #endif
3918 #ifndef LINE_CODE_LABEL
3919 #define LINE_CODE_LABEL "LM"
3920 #endif
3923 /* Return the root of the DIE's built for the current compilation unit. */
3924 static dw_die_ref
3925 comp_unit_die (void)
3927 if (!single_comp_unit_die)
3928 single_comp_unit_die = gen_compile_unit_die (NULL);
3929 return single_comp_unit_die;
3932 /* We allow a language front-end to designate a function that is to be
3933 called to "demangle" any name before it is put into a DIE. */
3935 static const char *(*demangle_name_func) (const char *);
3937 void
3938 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3940 demangle_name_func = func;
3943 /* Test if rtl node points to a pseudo register. */
3945 static inline int
3946 is_pseudo_reg (const_rtx rtl)
3948 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3949 || (GET_CODE (rtl) == SUBREG
3950 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3953 /* Return a reference to a type, with its const and volatile qualifiers
3954 removed. */
3956 static inline tree
3957 type_main_variant (tree type)
3959 type = TYPE_MAIN_VARIANT (type);
3961 /* ??? There really should be only one main variant among any group of
3962 variants of a given type (and all of the MAIN_VARIANT values for all
3963 members of the group should point to that one type) but sometimes the C
3964 front-end messes this up for array types, so we work around that bug
3965 here. */
3966 if (TREE_CODE (type) == ARRAY_TYPE)
3967 while (type != TYPE_MAIN_VARIANT (type))
3968 type = TYPE_MAIN_VARIANT (type);
3970 return type;
3973 /* Return nonzero if the given type node represents a tagged type. */
3975 static inline int
3976 is_tagged_type (const_tree type)
3978 enum tree_code code = TREE_CODE (type);
3980 return (code == RECORD_TYPE || code == UNION_TYPE
3981 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3984 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3986 static void
3987 get_ref_die_offset_label (char *label, dw_die_ref ref)
3989 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3992 /* Return die_offset of a DIE reference to a base type. */
3994 static unsigned long int
3995 get_base_type_offset (dw_die_ref ref)
3997 if (ref->die_offset)
3998 return ref->die_offset;
3999 if (comp_unit_die ()->die_abbrev)
4001 calc_base_type_die_sizes ();
4002 gcc_assert (ref->die_offset);
4004 return ref->die_offset;
4007 /* Return die_offset of a DIE reference other than base type. */
4009 static unsigned long int
4010 get_ref_die_offset (dw_die_ref ref)
4012 gcc_assert (ref->die_offset);
4013 return ref->die_offset;
4016 /* Convert a DIE tag into its string name. */
4018 static const char *
4019 dwarf_tag_name (unsigned int tag)
4021 const char *name = get_DW_TAG_name (tag);
4023 if (name != NULL)
4024 return name;
4026 return "DW_TAG_<unknown>";
4029 /* Convert a DWARF attribute code into its string name. */
4031 static const char *
4032 dwarf_attr_name (unsigned int attr)
4034 const char *name;
4036 switch (attr)
4038 #if VMS_DEBUGGING_INFO
4039 case DW_AT_HP_prologue:
4040 return "DW_AT_HP_prologue";
4041 #else
4042 case DW_AT_MIPS_loop_unroll_factor:
4043 return "DW_AT_MIPS_loop_unroll_factor";
4044 #endif
4046 #if VMS_DEBUGGING_INFO
4047 case DW_AT_HP_epilogue:
4048 return "DW_AT_HP_epilogue";
4049 #else
4050 case DW_AT_MIPS_stride:
4051 return "DW_AT_MIPS_stride";
4052 #endif
4055 name = get_DW_AT_name (attr);
4057 if (name != NULL)
4058 return name;
4060 return "DW_AT_<unknown>";
4063 /* Convert a DWARF value form code into its string name. */
4065 static const char *
4066 dwarf_form_name (unsigned int form)
4068 const char *name = get_DW_FORM_name (form);
4070 if (name != NULL)
4071 return name;
4073 return "DW_FORM_<unknown>";
4076 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4077 instance of an inlined instance of a decl which is local to an inline
4078 function, so we have to trace all of the way back through the origin chain
4079 to find out what sort of node actually served as the original seed for the
4080 given block. */
4082 static tree
4083 decl_ultimate_origin (const_tree decl)
4085 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4086 return NULL_TREE;
4088 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4089 we're trying to output the abstract instance of this function. */
4090 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4091 return NULL_TREE;
4093 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4094 most distant ancestor, this should never happen. */
4095 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4097 return DECL_ABSTRACT_ORIGIN (decl);
4100 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4101 of a virtual function may refer to a base class, so we check the 'this'
4102 parameter. */
4104 static tree
4105 decl_class_context (tree decl)
4107 tree context = NULL_TREE;
4109 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4110 context = DECL_CONTEXT (decl);
4111 else
4112 context = TYPE_MAIN_VARIANT
4113 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4115 if (context && !TYPE_P (context))
4116 context = NULL_TREE;
4118 return context;
4121 /* Add an attribute/value pair to a DIE. */
4123 static inline void
4124 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4126 /* Maybe this should be an assert? */
4127 if (die == NULL)
4128 return;
4130 if (flag_checking)
4132 /* Check we do not add duplicate attrs. Can't use get_AT here
4133 because that recurses to the specification/abstract origin DIE. */
4134 dw_attr_node *a;
4135 unsigned ix;
4136 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4137 gcc_assert (a->dw_attr != attr->dw_attr);
4140 vec_safe_reserve (die->die_attr, 1);
4141 vec_safe_push (die->die_attr, *attr);
4144 static inline enum dw_val_class
4145 AT_class (dw_attr_node *a)
4147 return a->dw_attr_val.val_class;
4150 /* Return the index for any attribute that will be referenced with a
4151 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4152 are stored in dw_attr_val.v.val_str for reference counting
4153 pruning. */
4155 static inline unsigned int
4156 AT_index (dw_attr_node *a)
4158 if (AT_class (a) == dw_val_class_str)
4159 return a->dw_attr_val.v.val_str->index;
4160 else if (a->dw_attr_val.val_entry != NULL)
4161 return a->dw_attr_val.val_entry->index;
4162 return NOT_INDEXED;
4165 /* Add a flag value attribute to a DIE. */
4167 static inline void
4168 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4170 dw_attr_node attr;
4172 attr.dw_attr = attr_kind;
4173 attr.dw_attr_val.val_class = dw_val_class_flag;
4174 attr.dw_attr_val.val_entry = NULL;
4175 attr.dw_attr_val.v.val_flag = flag;
4176 add_dwarf_attr (die, &attr);
4179 static inline unsigned
4180 AT_flag (dw_attr_node *a)
4182 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4183 return a->dw_attr_val.v.val_flag;
4186 /* Add a signed integer attribute value to a DIE. */
4188 static inline void
4189 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4191 dw_attr_node attr;
4193 attr.dw_attr = attr_kind;
4194 attr.dw_attr_val.val_class = dw_val_class_const;
4195 attr.dw_attr_val.val_entry = NULL;
4196 attr.dw_attr_val.v.val_int = int_val;
4197 add_dwarf_attr (die, &attr);
4200 static inline HOST_WIDE_INT
4201 AT_int (dw_attr_node *a)
4203 gcc_assert (a && (AT_class (a) == dw_val_class_const
4204 || AT_class (a) == dw_val_class_const_implicit));
4205 return a->dw_attr_val.v.val_int;
4208 /* Add an unsigned integer attribute value to a DIE. */
4210 static inline void
4211 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4212 unsigned HOST_WIDE_INT unsigned_val)
4214 dw_attr_node attr;
4216 attr.dw_attr = attr_kind;
4217 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4218 attr.dw_attr_val.val_entry = NULL;
4219 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4220 add_dwarf_attr (die, &attr);
4223 static inline unsigned HOST_WIDE_INT
4224 AT_unsigned (dw_attr_node *a)
4226 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4227 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4228 return a->dw_attr_val.v.val_unsigned;
4231 /* Add an unsigned wide integer attribute value to a DIE. */
4233 static inline void
4234 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4235 const wide_int& w)
4237 dw_attr_node attr;
4239 attr.dw_attr = attr_kind;
4240 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4241 attr.dw_attr_val.val_entry = NULL;
4242 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4243 *attr.dw_attr_val.v.val_wide = w;
4244 add_dwarf_attr (die, &attr);
4247 /* Add an unsigned double integer attribute value to a DIE. */
4249 static inline void
4250 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4251 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4253 dw_attr_node attr;
4255 attr.dw_attr = attr_kind;
4256 attr.dw_attr_val.val_class = dw_val_class_const_double;
4257 attr.dw_attr_val.val_entry = NULL;
4258 attr.dw_attr_val.v.val_double.high = high;
4259 attr.dw_attr_val.v.val_double.low = low;
4260 add_dwarf_attr (die, &attr);
4263 /* Add a floating point attribute value to a DIE and return it. */
4265 static inline void
4266 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4267 unsigned int length, unsigned int elt_size, unsigned char *array)
4269 dw_attr_node attr;
4271 attr.dw_attr = attr_kind;
4272 attr.dw_attr_val.val_class = dw_val_class_vec;
4273 attr.dw_attr_val.val_entry = NULL;
4274 attr.dw_attr_val.v.val_vec.length = length;
4275 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4276 attr.dw_attr_val.v.val_vec.array = array;
4277 add_dwarf_attr (die, &attr);
4280 /* Add an 8-byte data attribute value to a DIE. */
4282 static inline void
4283 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4284 unsigned char data8[8])
4286 dw_attr_node attr;
4288 attr.dw_attr = attr_kind;
4289 attr.dw_attr_val.val_class = dw_val_class_data8;
4290 attr.dw_attr_val.val_entry = NULL;
4291 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4292 add_dwarf_attr (die, &attr);
4295 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4296 dwarf_split_debug_info, address attributes in dies destined for the
4297 final executable have force_direct set to avoid using indexed
4298 references. */
4300 static inline void
4301 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4302 bool force_direct)
4304 dw_attr_node attr;
4305 char * lbl_id;
4307 lbl_id = xstrdup (lbl_low);
4308 attr.dw_attr = DW_AT_low_pc;
4309 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4310 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4311 if (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);
4318 attr.dw_attr = DW_AT_high_pc;
4319 if (dwarf_version < 4)
4320 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4321 else
4322 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4323 lbl_id = xstrdup (lbl_high);
4324 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4325 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4326 && dwarf_split_debug_info && !force_direct)
4327 attr.dw_attr_val.val_entry
4328 = add_addr_table_entry (lbl_id, ate_kind_label);
4329 else
4330 attr.dw_attr_val.val_entry = NULL;
4331 add_dwarf_attr (die, &attr);
4334 /* Hash and equality functions for debug_str_hash. */
4336 hashval_t
4337 indirect_string_hasher::hash (indirect_string_node *x)
4339 return htab_hash_string (x->str);
4342 bool
4343 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4345 return strcmp (x1->str, x2) == 0;
4348 /* Add STR to the given string hash table. */
4350 static struct indirect_string_node *
4351 find_AT_string_in_table (const char *str,
4352 hash_table<indirect_string_hasher> *table)
4354 struct indirect_string_node *node;
4356 indirect_string_node **slot
4357 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4358 if (*slot == NULL)
4360 node = ggc_cleared_alloc<indirect_string_node> ();
4361 node->str = ggc_strdup (str);
4362 *slot = node;
4364 else
4365 node = *slot;
4367 node->refcount++;
4368 return node;
4371 /* Add STR to the indirect string hash table. */
4373 static struct indirect_string_node *
4374 find_AT_string (const char *str)
4376 if (! debug_str_hash)
4377 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4379 return find_AT_string_in_table (str, debug_str_hash);
4382 /* Add a string attribute value to a DIE. */
4384 static inline void
4385 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4387 dw_attr_node attr;
4388 struct indirect_string_node *node;
4390 node = find_AT_string (str);
4392 attr.dw_attr = attr_kind;
4393 attr.dw_attr_val.val_class = dw_val_class_str;
4394 attr.dw_attr_val.val_entry = NULL;
4395 attr.dw_attr_val.v.val_str = node;
4396 add_dwarf_attr (die, &attr);
4399 static inline const char *
4400 AT_string (dw_attr_node *a)
4402 gcc_assert (a && AT_class (a) == dw_val_class_str);
4403 return a->dw_attr_val.v.val_str->str;
4406 /* Call this function directly to bypass AT_string_form's logic to put
4407 the string inline in the die. */
4409 static void
4410 set_indirect_string (struct indirect_string_node *node)
4412 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4413 /* Already indirect is a no op. */
4414 if (node->form == DW_FORM_strp
4415 || node->form == DW_FORM_line_strp
4416 || node->form == DW_FORM_GNU_str_index)
4418 gcc_assert (node->label);
4419 return;
4421 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4422 ++dw2_string_counter;
4423 node->label = xstrdup (label);
4425 if (!dwarf_split_debug_info)
4427 node->form = DW_FORM_strp;
4428 node->index = NOT_INDEXED;
4430 else
4432 node->form = DW_FORM_GNU_str_index;
4433 node->index = NO_INDEX_ASSIGNED;
4437 /* A helper function for dwarf2out_finish, called to reset indirect
4438 string decisions done for early LTO dwarf output before fat object
4439 dwarf output. */
4442 reset_indirect_string (indirect_string_node **h, void *)
4444 struct indirect_string_node *node = *h;
4445 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4447 free (node->label);
4448 node->label = NULL;
4449 node->form = (dwarf_form) 0;
4450 node->index = 0;
4452 return 1;
4455 /* Find out whether a string should be output inline in DIE
4456 or out-of-line in .debug_str section. */
4458 static enum dwarf_form
4459 find_string_form (struct indirect_string_node *node)
4461 unsigned int len;
4463 if (node->form)
4464 return node->form;
4466 len = strlen (node->str) + 1;
4468 /* If the string is shorter or equal to the size of the reference, it is
4469 always better to put it inline. */
4470 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4471 return node->form = DW_FORM_string;
4473 /* If we cannot expect the linker to merge strings in .debug_str
4474 section, only put it into .debug_str if it is worth even in this
4475 single module. */
4476 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4477 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4478 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4479 return node->form = DW_FORM_string;
4481 set_indirect_string (node);
4483 return node->form;
4486 /* Find out whether the string referenced from the attribute should be
4487 output inline in DIE or out-of-line in .debug_str section. */
4489 static enum dwarf_form
4490 AT_string_form (dw_attr_node *a)
4492 gcc_assert (a && AT_class (a) == dw_val_class_str);
4493 return find_string_form (a->dw_attr_val.v.val_str);
4496 /* Add a DIE reference attribute value to a DIE. */
4498 static inline void
4499 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4501 dw_attr_node attr;
4502 gcc_checking_assert (targ_die != NULL);
4504 /* With LTO we can end up trying to reference something we didn't create
4505 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4506 if (targ_die == NULL)
4507 return;
4509 attr.dw_attr = attr_kind;
4510 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4511 attr.dw_attr_val.val_entry = NULL;
4512 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4513 attr.dw_attr_val.v.val_die_ref.external = 0;
4514 add_dwarf_attr (die, &attr);
4517 /* Change DIE reference REF to point to NEW_DIE instead. */
4519 static inline void
4520 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4522 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4523 ref->dw_attr_val.v.val_die_ref.die = new_die;
4524 ref->dw_attr_val.v.val_die_ref.external = 0;
4527 /* Add an AT_specification attribute to a DIE, and also make the back
4528 pointer from the specification to the definition. */
4530 static inline void
4531 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4533 add_AT_die_ref (die, DW_AT_specification, targ_die);
4534 gcc_assert (!targ_die->die_definition);
4535 targ_die->die_definition = die;
4538 static inline dw_die_ref
4539 AT_ref (dw_attr_node *a)
4541 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4542 return a->dw_attr_val.v.val_die_ref.die;
4545 static inline int
4546 AT_ref_external (dw_attr_node *a)
4548 if (a && AT_class (a) == dw_val_class_die_ref)
4549 return a->dw_attr_val.v.val_die_ref.external;
4551 return 0;
4554 static inline void
4555 set_AT_ref_external (dw_attr_node *a, int i)
4557 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4558 a->dw_attr_val.v.val_die_ref.external = i;
4561 /* Add an FDE reference attribute value to a DIE. */
4563 static inline void
4564 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4566 dw_attr_node attr;
4568 attr.dw_attr = attr_kind;
4569 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4570 attr.dw_attr_val.val_entry = NULL;
4571 attr.dw_attr_val.v.val_fde_index = targ_fde;
4572 add_dwarf_attr (die, &attr);
4575 /* Add a location description attribute value to a DIE. */
4577 static inline void
4578 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4580 dw_attr_node attr;
4582 attr.dw_attr = attr_kind;
4583 attr.dw_attr_val.val_class = dw_val_class_loc;
4584 attr.dw_attr_val.val_entry = NULL;
4585 attr.dw_attr_val.v.val_loc = loc;
4586 add_dwarf_attr (die, &attr);
4589 static inline dw_loc_descr_ref
4590 AT_loc (dw_attr_node *a)
4592 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4593 return a->dw_attr_val.v.val_loc;
4596 static inline void
4597 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4599 dw_attr_node attr;
4601 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4602 return;
4604 attr.dw_attr = attr_kind;
4605 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4606 attr.dw_attr_val.val_entry = NULL;
4607 attr.dw_attr_val.v.val_loc_list = loc_list;
4608 add_dwarf_attr (die, &attr);
4609 have_location_lists = true;
4612 static inline dw_loc_list_ref
4613 AT_loc_list (dw_attr_node *a)
4615 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4616 return a->dw_attr_val.v.val_loc_list;
4619 static inline dw_loc_list_ref *
4620 AT_loc_list_ptr (dw_attr_node *a)
4622 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4623 return &a->dw_attr_val.v.val_loc_list;
4626 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4628 static hashval_t hash (addr_table_entry *);
4629 static bool equal (addr_table_entry *, addr_table_entry *);
4632 /* Table of entries into the .debug_addr section. */
4634 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4636 /* Hash an address_table_entry. */
4638 hashval_t
4639 addr_hasher::hash (addr_table_entry *a)
4641 inchash::hash hstate;
4642 switch (a->kind)
4644 case ate_kind_rtx:
4645 hstate.add_int (0);
4646 break;
4647 case ate_kind_rtx_dtprel:
4648 hstate.add_int (1);
4649 break;
4650 case ate_kind_label:
4651 return htab_hash_string (a->addr.label);
4652 default:
4653 gcc_unreachable ();
4655 inchash::add_rtx (a->addr.rtl, hstate);
4656 return hstate.end ();
4659 /* Determine equality for two address_table_entries. */
4661 bool
4662 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4664 if (a1->kind != a2->kind)
4665 return 0;
4666 switch (a1->kind)
4668 case ate_kind_rtx:
4669 case ate_kind_rtx_dtprel:
4670 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4671 case ate_kind_label:
4672 return strcmp (a1->addr.label, a2->addr.label) == 0;
4673 default:
4674 gcc_unreachable ();
4678 /* Initialize an addr_table_entry. */
4680 void
4681 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4683 e->kind = kind;
4684 switch (kind)
4686 case ate_kind_rtx:
4687 case ate_kind_rtx_dtprel:
4688 e->addr.rtl = (rtx) addr;
4689 break;
4690 case ate_kind_label:
4691 e->addr.label = (char *) addr;
4692 break;
4694 e->refcount = 0;
4695 e->index = NO_INDEX_ASSIGNED;
4698 /* Add attr to the address table entry to the table. Defer setting an
4699 index until output time. */
4701 static addr_table_entry *
4702 add_addr_table_entry (void *addr, enum ate_kind kind)
4704 addr_table_entry *node;
4705 addr_table_entry finder;
4707 gcc_assert (dwarf_split_debug_info);
4708 if (! addr_index_table)
4709 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4710 init_addr_table_entry (&finder, kind, addr);
4711 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4713 if (*slot == HTAB_EMPTY_ENTRY)
4715 node = ggc_cleared_alloc<addr_table_entry> ();
4716 init_addr_table_entry (node, kind, addr);
4717 *slot = node;
4719 else
4720 node = *slot;
4722 node->refcount++;
4723 return node;
4726 /* Remove an entry from the addr table by decrementing its refcount.
4727 Strictly, decrementing the refcount would be enough, but the
4728 assertion that the entry is actually in the table has found
4729 bugs. */
4731 static void
4732 remove_addr_table_entry (addr_table_entry *entry)
4734 gcc_assert (dwarf_split_debug_info && addr_index_table);
4735 /* After an index is assigned, the table is frozen. */
4736 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4737 entry->refcount--;
4740 /* Given a location list, remove all addresses it refers to from the
4741 address_table. */
4743 static void
4744 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4746 for (; descr; descr = descr->dw_loc_next)
4747 if (descr->dw_loc_oprnd1.val_entry != NULL)
4749 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4750 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4754 /* A helper function for dwarf2out_finish called through
4755 htab_traverse. Assign an addr_table_entry its index. All entries
4756 must be collected into the table when this function is called,
4757 because the indexing code relies on htab_traverse to traverse nodes
4758 in the same order for each run. */
4761 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4763 addr_table_entry *node = *h;
4765 /* Don't index unreferenced nodes. */
4766 if (node->refcount == 0)
4767 return 1;
4769 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4770 node->index = *index;
4771 *index += 1;
4773 return 1;
4776 /* Add an address constant attribute value to a DIE. When using
4777 dwarf_split_debug_info, address attributes in dies destined for the
4778 final executable should be direct references--setting the parameter
4779 force_direct ensures this behavior. */
4781 static inline void
4782 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4783 bool force_direct)
4785 dw_attr_node attr;
4787 attr.dw_attr = attr_kind;
4788 attr.dw_attr_val.val_class = dw_val_class_addr;
4789 attr.dw_attr_val.v.val_addr = addr;
4790 if (dwarf_split_debug_info && !force_direct)
4791 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4792 else
4793 attr.dw_attr_val.val_entry = NULL;
4794 add_dwarf_attr (die, &attr);
4797 /* Get the RTX from to an address DIE attribute. */
4799 static inline rtx
4800 AT_addr (dw_attr_node *a)
4802 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4803 return a->dw_attr_val.v.val_addr;
4806 /* Add a file attribute value to a DIE. */
4808 static inline void
4809 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4810 struct dwarf_file_data *fd)
4812 dw_attr_node attr;
4814 attr.dw_attr = attr_kind;
4815 attr.dw_attr_val.val_class = dw_val_class_file;
4816 attr.dw_attr_val.val_entry = NULL;
4817 attr.dw_attr_val.v.val_file = fd;
4818 add_dwarf_attr (die, &attr);
4821 /* Get the dwarf_file_data from a file DIE attribute. */
4823 static inline struct dwarf_file_data *
4824 AT_file (dw_attr_node *a)
4826 gcc_assert (a && (AT_class (a) == dw_val_class_file
4827 || AT_class (a) == dw_val_class_file_implicit));
4828 return a->dw_attr_val.v.val_file;
4831 /* Add a vms delta attribute value to a DIE. */
4833 static inline void
4834 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4835 const char *lbl1, const char *lbl2)
4837 dw_attr_node attr;
4839 attr.dw_attr = attr_kind;
4840 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4841 attr.dw_attr_val.val_entry = NULL;
4842 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4843 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4844 add_dwarf_attr (die, &attr);
4847 /* Add a label identifier attribute value to a DIE. */
4849 static inline void
4850 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4851 const char *lbl_id)
4853 dw_attr_node attr;
4855 attr.dw_attr = attr_kind;
4856 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4857 attr.dw_attr_val.val_entry = NULL;
4858 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4859 if (dwarf_split_debug_info)
4860 attr.dw_attr_val.val_entry
4861 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4862 ate_kind_label);
4863 add_dwarf_attr (die, &attr);
4866 /* Add a section offset attribute value to a DIE, an offset into the
4867 debug_line section. */
4869 static inline void
4870 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4871 const char *label)
4873 dw_attr_node attr;
4875 attr.dw_attr = attr_kind;
4876 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4877 attr.dw_attr_val.val_entry = NULL;
4878 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4879 add_dwarf_attr (die, &attr);
4882 /* Add a section offset attribute value to a DIE, an offset into the
4883 debug_loclists section. */
4885 static inline void
4886 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4887 const char *label)
4889 dw_attr_node attr;
4891 attr.dw_attr = attr_kind;
4892 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4893 attr.dw_attr_val.val_entry = NULL;
4894 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4895 add_dwarf_attr (die, &attr);
4898 /* Add a section offset attribute value to a DIE, an offset into the
4899 debug_macinfo section. */
4901 static inline void
4902 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4903 const char *label)
4905 dw_attr_node attr;
4907 attr.dw_attr = attr_kind;
4908 attr.dw_attr_val.val_class = dw_val_class_macptr;
4909 attr.dw_attr_val.val_entry = NULL;
4910 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4911 add_dwarf_attr (die, &attr);
4914 /* Add an offset attribute value to a DIE. */
4916 static inline void
4917 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4918 unsigned HOST_WIDE_INT offset)
4920 dw_attr_node attr;
4922 attr.dw_attr = attr_kind;
4923 attr.dw_attr_val.val_class = dw_val_class_offset;
4924 attr.dw_attr_val.val_entry = NULL;
4925 attr.dw_attr_val.v.val_offset = offset;
4926 add_dwarf_attr (die, &attr);
4929 /* Add a range_list attribute value to a DIE. When using
4930 dwarf_split_debug_info, address attributes in dies destined for the
4931 final executable should be direct references--setting the parameter
4932 force_direct ensures this behavior. */
4934 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4935 #define RELOCATED_OFFSET (NULL)
4937 static void
4938 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4939 long unsigned int offset, bool force_direct)
4941 dw_attr_node attr;
4943 attr.dw_attr = attr_kind;
4944 attr.dw_attr_val.val_class = dw_val_class_range_list;
4945 /* For the range_list attribute, use val_entry to store whether the
4946 offset should follow split-debug-info or normal semantics. This
4947 value is read in output_range_list_offset. */
4948 if (dwarf_split_debug_info && !force_direct)
4949 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4950 else
4951 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4952 attr.dw_attr_val.v.val_offset = offset;
4953 add_dwarf_attr (die, &attr);
4956 /* Return the start label of a delta attribute. */
4958 static inline const char *
4959 AT_vms_delta1 (dw_attr_node *a)
4961 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4962 return a->dw_attr_val.v.val_vms_delta.lbl1;
4965 /* Return the end label of a delta attribute. */
4967 static inline const char *
4968 AT_vms_delta2 (dw_attr_node *a)
4970 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4971 return a->dw_attr_val.v.val_vms_delta.lbl2;
4974 static inline const char *
4975 AT_lbl (dw_attr_node *a)
4977 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4978 || AT_class (a) == dw_val_class_lineptr
4979 || AT_class (a) == dw_val_class_macptr
4980 || AT_class (a) == dw_val_class_loclistsptr
4981 || AT_class (a) == dw_val_class_high_pc));
4982 return a->dw_attr_val.v.val_lbl_id;
4985 /* Get the attribute of type attr_kind. */
4987 static dw_attr_node *
4988 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4990 dw_attr_node *a;
4991 unsigned ix;
4992 dw_die_ref spec = NULL;
4994 if (! die)
4995 return NULL;
4997 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4998 if (a->dw_attr == attr_kind)
4999 return a;
5000 else if (a->dw_attr == DW_AT_specification
5001 || a->dw_attr == DW_AT_abstract_origin)
5002 spec = AT_ref (a);
5004 if (spec)
5005 return get_AT (spec, attr_kind);
5007 return NULL;
5010 /* Returns the parent of the declaration of DIE. */
5012 static dw_die_ref
5013 get_die_parent (dw_die_ref die)
5015 dw_die_ref t;
5017 if (!die)
5018 return NULL;
5020 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5021 || (t = get_AT_ref (die, DW_AT_specification)))
5022 die = t;
5024 return die->die_parent;
5027 /* Return the "low pc" attribute value, typically associated with a subprogram
5028 DIE. Return null if the "low pc" attribute is either not present, or if it
5029 cannot be represented as an assembler label identifier. */
5031 static inline const char *
5032 get_AT_low_pc (dw_die_ref die)
5034 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5036 return a ? AT_lbl (a) : NULL;
5039 /* Return the "high pc" attribute value, typically associated with a subprogram
5040 DIE. Return null if the "high pc" attribute is either not present, or if it
5041 cannot be represented as an assembler label identifier. */
5043 static inline const char *
5044 get_AT_hi_pc (dw_die_ref die)
5046 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5048 return a ? AT_lbl (a) : NULL;
5051 /* Return the value of the string attribute designated by ATTR_KIND, or
5052 NULL if it is not present. */
5054 static inline const char *
5055 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5057 dw_attr_node *a = get_AT (die, attr_kind);
5059 return a ? AT_string (a) : NULL;
5062 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5063 if it is not present. */
5065 static inline int
5066 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5068 dw_attr_node *a = get_AT (die, attr_kind);
5070 return a ? AT_flag (a) : 0;
5073 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5074 if it is not present. */
5076 static inline unsigned
5077 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5079 dw_attr_node *a = get_AT (die, attr_kind);
5081 return a ? AT_unsigned (a) : 0;
5084 static inline dw_die_ref
5085 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5087 dw_attr_node *a = get_AT (die, attr_kind);
5089 return a ? AT_ref (a) : NULL;
5092 static inline struct dwarf_file_data *
5093 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5095 dw_attr_node *a = get_AT (die, attr_kind);
5097 return a ? AT_file (a) : NULL;
5100 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5102 static const_tree
5103 get_ultimate_context (const_tree decl)
5105 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5107 if (TREE_CODE (decl) == BLOCK)
5108 decl = BLOCK_SUPERCONTEXT (decl);
5109 else
5110 decl = get_containing_scope (decl);
5112 return decl;
5115 /* Return TRUE if the language is C++. */
5117 static inline bool
5118 is_cxx (void)
5120 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5122 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5123 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5126 /* Return TRUE if DECL was created by the C++ frontend. */
5128 static bool
5129 is_cxx (const_tree decl)
5131 if (in_lto_p)
5133 const_tree context = get_ultimate_context (decl);
5134 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5135 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5137 return is_cxx ();
5140 /* Return TRUE if the language is Fortran. */
5142 static inline bool
5143 is_fortran (void)
5145 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5147 return (lang == DW_LANG_Fortran77
5148 || lang == DW_LANG_Fortran90
5149 || lang == DW_LANG_Fortran95
5150 || lang == DW_LANG_Fortran03
5151 || lang == DW_LANG_Fortran08);
5154 static inline bool
5155 is_fortran (const_tree decl)
5157 if (in_lto_p)
5159 const_tree context = get_ultimate_context (decl);
5160 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5161 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5162 "GNU Fortran", 11) == 0
5163 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5164 "GNU F77") == 0);
5166 return is_fortran ();
5169 /* Return TRUE if the language is Ada. */
5171 static inline bool
5172 is_ada (void)
5174 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5176 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5179 /* Remove the specified attribute if present. Return TRUE if removal
5180 was successful. */
5182 static bool
5183 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5185 dw_attr_node *a;
5186 unsigned ix;
5188 if (! die)
5189 return false;
5191 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5192 if (a->dw_attr == attr_kind)
5194 if (AT_class (a) == dw_val_class_str)
5195 if (a->dw_attr_val.v.val_str->refcount)
5196 a->dw_attr_val.v.val_str->refcount--;
5198 /* vec::ordered_remove should help reduce the number of abbrevs
5199 that are needed. */
5200 die->die_attr->ordered_remove (ix);
5201 return true;
5203 return false;
5206 /* Remove CHILD from its parent. PREV must have the property that
5207 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5209 static void
5210 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5212 gcc_assert (child->die_parent == prev->die_parent);
5213 gcc_assert (prev->die_sib == child);
5214 if (prev == child)
5216 gcc_assert (child->die_parent->die_child == child);
5217 prev = NULL;
5219 else
5220 prev->die_sib = child->die_sib;
5221 if (child->die_parent->die_child == child)
5222 child->die_parent->die_child = prev;
5223 child->die_sib = NULL;
5226 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5227 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5229 static void
5230 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5232 dw_die_ref parent = old_child->die_parent;
5234 gcc_assert (parent == prev->die_parent);
5235 gcc_assert (prev->die_sib == old_child);
5237 new_child->die_parent = parent;
5238 if (prev == old_child)
5240 gcc_assert (parent->die_child == old_child);
5241 new_child->die_sib = new_child;
5243 else
5245 prev->die_sib = new_child;
5246 new_child->die_sib = old_child->die_sib;
5248 if (old_child->die_parent->die_child == old_child)
5249 old_child->die_parent->die_child = new_child;
5250 old_child->die_sib = NULL;
5253 /* Move all children from OLD_PARENT to NEW_PARENT. */
5255 static void
5256 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5258 dw_die_ref c;
5259 new_parent->die_child = old_parent->die_child;
5260 old_parent->die_child = NULL;
5261 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5264 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5265 matches TAG. */
5267 static void
5268 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5270 dw_die_ref c;
5272 c = die->die_child;
5273 if (c) do {
5274 dw_die_ref prev = c;
5275 c = c->die_sib;
5276 while (c->die_tag == tag)
5278 remove_child_with_prev (c, prev);
5279 c->die_parent = NULL;
5280 /* Might have removed every child. */
5281 if (die->die_child == NULL)
5282 return;
5283 c = prev->die_sib;
5285 } while (c != die->die_child);
5288 /* Add a CHILD_DIE as the last child of DIE. */
5290 static void
5291 add_child_die (dw_die_ref die, dw_die_ref child_die)
5293 /* FIXME this should probably be an assert. */
5294 if (! die || ! child_die)
5295 return;
5296 gcc_assert (die != child_die);
5298 child_die->die_parent = die;
5299 if (die->die_child)
5301 child_die->die_sib = die->die_child->die_sib;
5302 die->die_child->die_sib = child_die;
5304 else
5305 child_die->die_sib = child_die;
5306 die->die_child = child_die;
5309 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5311 static void
5312 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5313 dw_die_ref after_die)
5315 gcc_assert (die
5316 && child_die
5317 && after_die
5318 && die->die_child
5319 && die != child_die);
5321 child_die->die_parent = die;
5322 child_die->die_sib = after_die->die_sib;
5323 after_die->die_sib = child_die;
5324 if (die->die_child == after_die)
5325 die->die_child = child_die;
5328 /* Unassociate CHILD from its parent, and make its parent be
5329 NEW_PARENT. */
5331 static void
5332 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5334 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5335 if (p->die_sib == child)
5337 remove_child_with_prev (child, p);
5338 break;
5340 add_child_die (new_parent, child);
5343 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5344 is the specification, to the end of PARENT's list of children.
5345 This is done by removing and re-adding it. */
5347 static void
5348 splice_child_die (dw_die_ref parent, dw_die_ref child)
5350 /* We want the declaration DIE from inside the class, not the
5351 specification DIE at toplevel. */
5352 if (child->die_parent != parent)
5354 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5356 if (tmp)
5357 child = tmp;
5360 gcc_assert (child->die_parent == parent
5361 || (child->die_parent
5362 == get_AT_ref (parent, DW_AT_specification)));
5364 reparent_child (child, parent);
5367 /* Create and return a new die with TAG_VALUE as tag. */
5369 static inline dw_die_ref
5370 new_die_raw (enum dwarf_tag tag_value)
5372 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5373 die->die_tag = tag_value;
5374 return die;
5377 /* Create and return a new die with a parent of PARENT_DIE. If
5378 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5379 associated tree T must be supplied to determine parenthood
5380 later. */
5382 static inline dw_die_ref
5383 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5385 dw_die_ref die = new_die_raw (tag_value);
5387 if (parent_die != NULL)
5388 add_child_die (parent_die, die);
5389 else
5391 limbo_die_node *limbo_node;
5393 /* No DIEs created after early dwarf should end up in limbo,
5394 because the limbo list should not persist past LTO
5395 streaming. */
5396 if (tag_value != DW_TAG_compile_unit
5397 /* These are allowed because they're generated while
5398 breaking out COMDAT units late. */
5399 && tag_value != DW_TAG_type_unit
5400 && tag_value != DW_TAG_skeleton_unit
5401 && !early_dwarf
5402 /* Allow nested functions to live in limbo because they will
5403 only temporarily live there, as decls_for_scope will fix
5404 them up. */
5405 && (TREE_CODE (t) != FUNCTION_DECL
5406 || !decl_function_context (t))
5407 /* Same as nested functions above but for types. Types that
5408 are local to a function will be fixed in
5409 decls_for_scope. */
5410 && (!RECORD_OR_UNION_TYPE_P (t)
5411 || !TYPE_CONTEXT (t)
5412 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5413 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5414 especially in the ltrans stage, but once we implement LTO
5415 dwarf streaming, we should remove this exception. */
5416 && !in_lto_p)
5418 fprintf (stderr, "symbol ended up in limbo too late:");
5419 debug_generic_stmt (t);
5420 gcc_unreachable ();
5423 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5424 limbo_node->die = die;
5425 limbo_node->created_for = t;
5426 limbo_node->next = limbo_die_list;
5427 limbo_die_list = limbo_node;
5430 return die;
5433 /* Return the DIE associated with the given type specifier. */
5435 static inline dw_die_ref
5436 lookup_type_die (tree type)
5438 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5439 if (die && die->removed)
5441 TYPE_SYMTAB_DIE (type) = NULL;
5442 return NULL;
5444 return die;
5447 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5448 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5449 anonymous type instead the one of the naming typedef. */
5451 static inline dw_die_ref
5452 strip_naming_typedef (tree type, dw_die_ref type_die)
5454 if (type
5455 && TREE_CODE (type) == RECORD_TYPE
5456 && type_die
5457 && type_die->die_tag == DW_TAG_typedef
5458 && is_naming_typedef_decl (TYPE_NAME (type)))
5459 type_die = get_AT_ref (type_die, DW_AT_type);
5460 return type_die;
5463 /* Like lookup_type_die, but if type is an anonymous type named by a
5464 typedef[1], return the DIE of the anonymous type instead the one of
5465 the naming typedef. This is because in gen_typedef_die, we did
5466 equate the anonymous struct named by the typedef with the DIE of
5467 the naming typedef. So by default, lookup_type_die on an anonymous
5468 struct yields the DIE of the naming typedef.
5470 [1]: Read the comment of is_naming_typedef_decl to learn about what
5471 a naming typedef is. */
5473 static inline dw_die_ref
5474 lookup_type_die_strip_naming_typedef (tree type)
5476 dw_die_ref die = lookup_type_die (type);
5477 return strip_naming_typedef (type, die);
5480 /* Equate a DIE to a given type specifier. */
5482 static inline void
5483 equate_type_number_to_die (tree type, dw_die_ref type_die)
5485 TYPE_SYMTAB_DIE (type) = type_die;
5488 /* Returns a hash value for X (which really is a die_struct). */
5490 inline hashval_t
5491 decl_die_hasher::hash (die_node *x)
5493 return (hashval_t) x->decl_id;
5496 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5498 inline bool
5499 decl_die_hasher::equal (die_node *x, tree y)
5501 return (x->decl_id == DECL_UID (y));
5504 /* Return the DIE associated with a given declaration. */
5506 static inline dw_die_ref
5507 lookup_decl_die (tree decl)
5509 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5510 NO_INSERT);
5511 if (!die)
5512 return NULL;
5513 if ((*die)->removed)
5515 decl_die_table->clear_slot (die);
5516 return NULL;
5518 return *die;
5522 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5523 style reference. Return true if we found one refering to a DIE for
5524 DECL, otherwise return false. */
5526 static bool
5527 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5528 unsigned HOST_WIDE_INT *off)
5530 dw_die_ref die;
5532 if (flag_wpa && !decl_die_table)
5533 return false;
5535 if (TREE_CODE (decl) == BLOCK)
5536 die = BLOCK_DIE (decl);
5537 else
5538 die = lookup_decl_die (decl);
5539 if (!die)
5540 return false;
5542 /* During WPA stage we currently use DIEs to store the
5543 decl <-> label + offset map. That's quite inefficient but it
5544 works for now. */
5545 if (flag_wpa)
5547 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5548 if (!ref)
5550 gcc_assert (die == comp_unit_die ());
5551 return false;
5553 *off = ref->die_offset;
5554 *sym = ref->die_id.die_symbol;
5555 return true;
5558 /* Similar to get_ref_die_offset_label, but using the "correct"
5559 label. */
5560 *off = die->die_offset;
5561 while (die->die_parent)
5562 die = die->die_parent;
5563 /* For the containing CU DIE we compute a die_symbol in
5564 compute_comp_unit_symbol. */
5565 gcc_assert (die->die_tag == DW_TAG_compile_unit
5566 && die->die_id.die_symbol != NULL);
5567 *sym = die->die_id.die_symbol;
5568 return true;
5571 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5573 static void
5574 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5575 const char *symbol, HOST_WIDE_INT offset)
5577 /* Create a fake DIE that contains the reference. Don't use
5578 new_die because we don't want to end up in the limbo list. */
5579 dw_die_ref ref = new_die_raw (die->die_tag);
5580 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5581 ref->die_offset = offset;
5582 ref->with_offset = 1;
5583 add_AT_die_ref (die, attr_kind, ref);
5586 /* Create a DIE for DECL if required and add a reference to a DIE
5587 at SYMBOL + OFFSET which contains attributes dumped early. */
5589 static void
5590 dwarf2out_register_external_die (tree decl, const char *sym,
5591 unsigned HOST_WIDE_INT off)
5593 if (debug_info_level == DINFO_LEVEL_NONE)
5594 return;
5596 if (flag_wpa && !decl_die_table)
5597 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5599 dw_die_ref die
5600 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5601 gcc_assert (!die);
5603 tree ctx;
5604 dw_die_ref parent = NULL;
5605 /* Need to lookup a DIE for the decls context - the containing
5606 function or translation unit. */
5607 if (TREE_CODE (decl) == BLOCK)
5609 ctx = BLOCK_SUPERCONTEXT (decl);
5610 /* ??? We do not output DIEs for all scopes thus skip as
5611 many DIEs as needed. */
5612 while (TREE_CODE (ctx) == BLOCK
5613 && !BLOCK_DIE (ctx))
5614 ctx = BLOCK_SUPERCONTEXT (ctx);
5616 else
5617 ctx = DECL_CONTEXT (decl);
5618 while (ctx && TYPE_P (ctx))
5619 ctx = TYPE_CONTEXT (ctx);
5620 if (ctx)
5622 if (TREE_CODE (ctx) == BLOCK)
5623 parent = BLOCK_DIE (ctx);
5624 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5625 /* Keep the 1:1 association during WPA. */
5626 && !flag_wpa)
5627 /* Otherwise all late annotations go to the main CU which
5628 imports the original CUs. */
5629 parent = comp_unit_die ();
5630 else if (TREE_CODE (ctx) == FUNCTION_DECL
5631 && TREE_CODE (decl) != PARM_DECL
5632 && TREE_CODE (decl) != BLOCK)
5633 /* Leave function local entities parent determination to when
5634 we process scope vars. */
5636 else
5637 parent = lookup_decl_die (ctx);
5639 else
5640 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5641 Handle this case gracefully by globalizing stuff. */
5642 parent = comp_unit_die ();
5643 /* Create a DIE "stub". */
5644 switch (TREE_CODE (decl))
5646 case TRANSLATION_UNIT_DECL:
5647 if (! flag_wpa)
5649 die = comp_unit_die ();
5650 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5651 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5652 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5653 to create a DIE for the original CUs. */
5654 return;
5656 /* Keep the 1:1 association during WPA. */
5657 die = new_die (DW_TAG_compile_unit, NULL, decl);
5658 break;
5659 case NAMESPACE_DECL:
5660 if (is_fortran (decl))
5661 die = new_die (DW_TAG_module, parent, decl);
5662 else
5663 die = new_die (DW_TAG_namespace, parent, decl);
5664 break;
5665 case FUNCTION_DECL:
5666 die = new_die (DW_TAG_subprogram, parent, decl);
5667 break;
5668 case VAR_DECL:
5669 die = new_die (DW_TAG_variable, parent, decl);
5670 break;
5671 case RESULT_DECL:
5672 die = new_die (DW_TAG_variable, parent, decl);
5673 break;
5674 case PARM_DECL:
5675 die = new_die (DW_TAG_formal_parameter, parent, decl);
5676 break;
5677 case CONST_DECL:
5678 die = new_die (DW_TAG_constant, parent, decl);
5679 break;
5680 case LABEL_DECL:
5681 die = new_die (DW_TAG_label, parent, decl);
5682 break;
5683 case BLOCK:
5684 die = new_die (DW_TAG_lexical_block, parent, decl);
5685 break;
5686 default:
5687 gcc_unreachable ();
5689 if (TREE_CODE (decl) == BLOCK)
5690 BLOCK_DIE (decl) = die;
5691 else
5692 equate_decl_number_to_die (decl, die);
5694 /* Add a reference to the DIE providing early debug at $sym + off. */
5695 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5698 /* Returns a hash value for X (which really is a var_loc_list). */
5700 inline hashval_t
5701 decl_loc_hasher::hash (var_loc_list *x)
5703 return (hashval_t) x->decl_id;
5706 /* Return nonzero if decl_id of var_loc_list X is the same as
5707 UID of decl *Y. */
5709 inline bool
5710 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5712 return (x->decl_id == DECL_UID (y));
5715 /* Return the var_loc list associated with a given declaration. */
5717 static inline var_loc_list *
5718 lookup_decl_loc (const_tree decl)
5720 if (!decl_loc_table)
5721 return NULL;
5722 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5725 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5727 inline hashval_t
5728 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5730 return (hashval_t) x->decl_id;
5733 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5734 UID of decl *Y. */
5736 inline bool
5737 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5739 return (x->decl_id == DECL_UID (y));
5742 /* Equate a DIE to a particular declaration. */
5744 static void
5745 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5747 unsigned int decl_id = DECL_UID (decl);
5749 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5750 decl_die->decl_id = decl_id;
5753 /* Return how many bits covers PIECE EXPR_LIST. */
5755 static HOST_WIDE_INT
5756 decl_piece_bitsize (rtx piece)
5758 int ret = (int) GET_MODE (piece);
5759 if (ret)
5760 return ret;
5761 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5762 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5763 return INTVAL (XEXP (XEXP (piece, 0), 0));
5766 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5768 static rtx *
5769 decl_piece_varloc_ptr (rtx piece)
5771 if ((int) GET_MODE (piece))
5772 return &XEXP (piece, 0);
5773 else
5774 return &XEXP (XEXP (piece, 0), 1);
5777 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5778 Next is the chain of following piece nodes. */
5780 static rtx_expr_list *
5781 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5783 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5784 return alloc_EXPR_LIST (bitsize, loc_note, next);
5785 else
5786 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5787 GEN_INT (bitsize),
5788 loc_note), next);
5791 /* Return rtx that should be stored into loc field for
5792 LOC_NOTE and BITPOS/BITSIZE. */
5794 static rtx
5795 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5796 HOST_WIDE_INT bitsize)
5798 if (bitsize != -1)
5800 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5801 if (bitpos != 0)
5802 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5804 return loc_note;
5807 /* This function either modifies location piece list *DEST in
5808 place (if SRC and INNER is NULL), or copies location piece list
5809 *SRC to *DEST while modifying it. Location BITPOS is modified
5810 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5811 not copied and if needed some padding around it is added.
5812 When modifying in place, DEST should point to EXPR_LIST where
5813 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5814 to the start of the whole list and INNER points to the EXPR_LIST
5815 where earlier pieces cover PIECE_BITPOS bits. */
5817 static void
5818 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5819 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5820 HOST_WIDE_INT bitsize, rtx loc_note)
5822 HOST_WIDE_INT diff;
5823 bool copy = inner != NULL;
5825 if (copy)
5827 /* First copy all nodes preceding the current bitpos. */
5828 while (src != inner)
5830 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5831 decl_piece_bitsize (*src), NULL_RTX);
5832 dest = &XEXP (*dest, 1);
5833 src = &XEXP (*src, 1);
5836 /* Add padding if needed. */
5837 if (bitpos != piece_bitpos)
5839 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5840 copy ? NULL_RTX : *dest);
5841 dest = &XEXP (*dest, 1);
5843 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5845 gcc_assert (!copy);
5846 /* A piece with correct bitpos and bitsize already exist,
5847 just update the location for it and return. */
5848 *decl_piece_varloc_ptr (*dest) = loc_note;
5849 return;
5851 /* Add the piece that changed. */
5852 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5853 dest = &XEXP (*dest, 1);
5854 /* Skip over pieces that overlap it. */
5855 diff = bitpos - piece_bitpos + bitsize;
5856 if (!copy)
5857 src = dest;
5858 while (diff > 0 && *src)
5860 rtx piece = *src;
5861 diff -= decl_piece_bitsize (piece);
5862 if (copy)
5863 src = &XEXP (piece, 1);
5864 else
5866 *src = XEXP (piece, 1);
5867 free_EXPR_LIST_node (piece);
5870 /* Add padding if needed. */
5871 if (diff < 0 && *src)
5873 if (!copy)
5874 dest = src;
5875 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5876 dest = &XEXP (*dest, 1);
5878 if (!copy)
5879 return;
5880 /* Finally copy all nodes following it. */
5881 while (*src)
5883 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5884 decl_piece_bitsize (*src), NULL_RTX);
5885 dest = &XEXP (*dest, 1);
5886 src = &XEXP (*src, 1);
5890 /* Add a variable location node to the linked list for DECL. */
5892 static struct var_loc_node *
5893 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5895 unsigned int decl_id;
5896 var_loc_list *temp;
5897 struct var_loc_node *loc = NULL;
5898 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5900 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5902 tree realdecl = DECL_DEBUG_EXPR (decl);
5903 if (handled_component_p (realdecl)
5904 || (TREE_CODE (realdecl) == MEM_REF
5905 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5907 HOST_WIDE_INT maxsize;
5908 bool reverse;
5909 tree innerdecl
5910 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5911 &reverse);
5912 if (!DECL_P (innerdecl)
5913 || DECL_IGNORED_P (innerdecl)
5914 || TREE_STATIC (innerdecl)
5915 || bitsize <= 0
5916 || bitpos + bitsize > 256
5917 || bitsize != maxsize)
5918 return NULL;
5919 decl = innerdecl;
5923 decl_id = DECL_UID (decl);
5924 var_loc_list **slot
5925 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5926 if (*slot == NULL)
5928 temp = ggc_cleared_alloc<var_loc_list> ();
5929 temp->decl_id = decl_id;
5930 *slot = temp;
5932 else
5933 temp = *slot;
5935 /* For PARM_DECLs try to keep around the original incoming value,
5936 even if that means we'll emit a zero-range .debug_loc entry. */
5937 if (temp->last
5938 && temp->first == temp->last
5939 && TREE_CODE (decl) == PARM_DECL
5940 && NOTE_P (temp->first->loc)
5941 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5942 && DECL_INCOMING_RTL (decl)
5943 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5944 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5945 == GET_CODE (DECL_INCOMING_RTL (decl))
5946 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5947 && (bitsize != -1
5948 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5949 NOTE_VAR_LOCATION_LOC (loc_note))
5950 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5951 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5953 loc = ggc_cleared_alloc<var_loc_node> ();
5954 temp->first->next = loc;
5955 temp->last = loc;
5956 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5958 else if (temp->last)
5960 struct var_loc_node *last = temp->last, *unused = NULL;
5961 rtx *piece_loc = NULL, last_loc_note;
5962 HOST_WIDE_INT piece_bitpos = 0;
5963 if (last->next)
5965 last = last->next;
5966 gcc_assert (last->next == NULL);
5968 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5970 piece_loc = &last->loc;
5973 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5974 if (piece_bitpos + cur_bitsize > bitpos)
5975 break;
5976 piece_bitpos += cur_bitsize;
5977 piece_loc = &XEXP (*piece_loc, 1);
5979 while (*piece_loc);
5981 /* TEMP->LAST here is either pointer to the last but one or
5982 last element in the chained list, LAST is pointer to the
5983 last element. */
5984 if (label && strcmp (last->label, label) == 0)
5986 /* For SRA optimized variables if there weren't any real
5987 insns since last note, just modify the last node. */
5988 if (piece_loc != NULL)
5990 adjust_piece_list (piece_loc, NULL, NULL,
5991 bitpos, piece_bitpos, bitsize, loc_note);
5992 return NULL;
5994 /* If the last note doesn't cover any instructions, remove it. */
5995 if (temp->last != last)
5997 temp->last->next = NULL;
5998 unused = last;
5999 last = temp->last;
6000 gcc_assert (strcmp (last->label, label) != 0);
6002 else
6004 gcc_assert (temp->first == temp->last
6005 || (temp->first->next == temp->last
6006 && TREE_CODE (decl) == PARM_DECL));
6007 memset (temp->last, '\0', sizeof (*temp->last));
6008 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6009 return temp->last;
6012 if (bitsize == -1 && NOTE_P (last->loc))
6013 last_loc_note = last->loc;
6014 else if (piece_loc != NULL
6015 && *piece_loc != NULL_RTX
6016 && piece_bitpos == bitpos
6017 && decl_piece_bitsize (*piece_loc) == bitsize)
6018 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6019 else
6020 last_loc_note = NULL_RTX;
6021 /* If the current location is the same as the end of the list,
6022 and either both or neither of the locations is uninitialized,
6023 we have nothing to do. */
6024 if (last_loc_note == NULL_RTX
6025 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6026 NOTE_VAR_LOCATION_LOC (loc_note)))
6027 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6028 != NOTE_VAR_LOCATION_STATUS (loc_note))
6029 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6030 == VAR_INIT_STATUS_UNINITIALIZED)
6031 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6032 == VAR_INIT_STATUS_UNINITIALIZED))))
6034 /* Add LOC to the end of list and update LAST. If the last
6035 element of the list has been removed above, reuse its
6036 memory for the new node, otherwise allocate a new one. */
6037 if (unused)
6039 loc = unused;
6040 memset (loc, '\0', sizeof (*loc));
6042 else
6043 loc = ggc_cleared_alloc<var_loc_node> ();
6044 if (bitsize == -1 || piece_loc == NULL)
6045 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6046 else
6047 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6048 bitpos, piece_bitpos, bitsize, loc_note);
6049 last->next = loc;
6050 /* Ensure TEMP->LAST will point either to the new last but one
6051 element of the chain, or to the last element in it. */
6052 if (last != temp->last)
6053 temp->last = last;
6055 else if (unused)
6056 ggc_free (unused);
6058 else
6060 loc = ggc_cleared_alloc<var_loc_node> ();
6061 temp->first = loc;
6062 temp->last = loc;
6063 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6065 return loc;
6068 /* Keep track of the number of spaces used to indent the
6069 output of the debugging routines that print the structure of
6070 the DIE internal representation. */
6071 static int print_indent;
6073 /* Indent the line the number of spaces given by print_indent. */
6075 static inline void
6076 print_spaces (FILE *outfile)
6078 fprintf (outfile, "%*s", print_indent, "");
6081 /* Print a type signature in hex. */
6083 static inline void
6084 print_signature (FILE *outfile, char *sig)
6086 int i;
6088 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6089 fprintf (outfile, "%02x", sig[i] & 0xff);
6092 static inline void
6093 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6095 if (discr_value->pos)
6096 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6097 else
6098 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6101 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6103 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6104 RECURSE, output location descriptor operations. */
6106 static void
6107 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6109 switch (val->val_class)
6111 case dw_val_class_addr:
6112 fprintf (outfile, "address");
6113 break;
6114 case dw_val_class_offset:
6115 fprintf (outfile, "offset");
6116 break;
6117 case dw_val_class_loc:
6118 fprintf (outfile, "location descriptor");
6119 if (val->v.val_loc == NULL)
6120 fprintf (outfile, " -> <null>\n");
6121 else if (recurse)
6123 fprintf (outfile, ":\n");
6124 print_indent += 4;
6125 print_loc_descr (val->v.val_loc, outfile);
6126 print_indent -= 4;
6128 else
6129 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6130 break;
6131 case dw_val_class_loc_list:
6132 fprintf (outfile, "location list -> label:%s",
6133 val->v.val_loc_list->ll_symbol);
6134 break;
6135 case dw_val_class_range_list:
6136 fprintf (outfile, "range list");
6137 break;
6138 case dw_val_class_const:
6139 case dw_val_class_const_implicit:
6140 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6141 break;
6142 case dw_val_class_unsigned_const:
6143 case dw_val_class_unsigned_const_implicit:
6144 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6145 break;
6146 case dw_val_class_const_double:
6147 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6148 HOST_WIDE_INT_PRINT_UNSIGNED")",
6149 val->v.val_double.high,
6150 val->v.val_double.low);
6151 break;
6152 case dw_val_class_wide_int:
6154 int i = val->v.val_wide->get_len ();
6155 fprintf (outfile, "constant (");
6156 gcc_assert (i > 0);
6157 if (val->v.val_wide->elt (i - 1) == 0)
6158 fprintf (outfile, "0x");
6159 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6160 val->v.val_wide->elt (--i));
6161 while (--i >= 0)
6162 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6163 val->v.val_wide->elt (i));
6164 fprintf (outfile, ")");
6165 break;
6167 case dw_val_class_vec:
6168 fprintf (outfile, "floating-point or vector constant");
6169 break;
6170 case dw_val_class_flag:
6171 fprintf (outfile, "%u", val->v.val_flag);
6172 break;
6173 case dw_val_class_die_ref:
6174 if (val->v.val_die_ref.die != NULL)
6176 dw_die_ref die = val->v.val_die_ref.die;
6178 if (die->comdat_type_p)
6180 fprintf (outfile, "die -> signature: ");
6181 print_signature (outfile,
6182 die->die_id.die_type_node->signature);
6184 else if (die->die_id.die_symbol)
6186 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6187 if (die->with_offset)
6188 fprintf (outfile, " + %ld", die->die_offset);
6190 else
6191 fprintf (outfile, "die -> %ld", die->die_offset);
6192 fprintf (outfile, " (%p)", (void *) die);
6194 else
6195 fprintf (outfile, "die -> <null>");
6196 break;
6197 case dw_val_class_vms_delta:
6198 fprintf (outfile, "delta: @slotcount(%s-%s)",
6199 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6200 break;
6201 case dw_val_class_lbl_id:
6202 case dw_val_class_lineptr:
6203 case dw_val_class_macptr:
6204 case dw_val_class_loclistsptr:
6205 case dw_val_class_high_pc:
6206 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6207 break;
6208 case dw_val_class_str:
6209 if (val->v.val_str->str != NULL)
6210 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6211 else
6212 fprintf (outfile, "<null>");
6213 break;
6214 case dw_val_class_file:
6215 case dw_val_class_file_implicit:
6216 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6217 val->v.val_file->emitted_number);
6218 break;
6219 case dw_val_class_data8:
6221 int i;
6223 for (i = 0; i < 8; i++)
6224 fprintf (outfile, "%02x", val->v.val_data8[i]);
6225 break;
6227 case dw_val_class_discr_value:
6228 print_discr_value (outfile, &val->v.val_discr_value);
6229 break;
6230 case dw_val_class_discr_list:
6231 for (dw_discr_list_ref node = val->v.val_discr_list;
6232 node != NULL;
6233 node = node->dw_discr_next)
6235 if (node->dw_discr_range)
6237 fprintf (outfile, " .. ");
6238 print_discr_value (outfile, &node->dw_discr_lower_bound);
6239 print_discr_value (outfile, &node->dw_discr_upper_bound);
6241 else
6242 print_discr_value (outfile, &node->dw_discr_lower_bound);
6244 if (node->dw_discr_next != NULL)
6245 fprintf (outfile, " | ");
6247 default:
6248 break;
6252 /* Likewise, for a DIE attribute. */
6254 static void
6255 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6257 print_dw_val (&a->dw_attr_val, recurse, outfile);
6261 /* Print the list of operands in the LOC location description to OUTFILE. This
6262 routine is a debugging aid only. */
6264 static void
6265 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6267 dw_loc_descr_ref l = loc;
6269 if (loc == NULL)
6271 print_spaces (outfile);
6272 fprintf (outfile, "<null>\n");
6273 return;
6276 for (l = loc; l != NULL; l = l->dw_loc_next)
6278 print_spaces (outfile);
6279 fprintf (outfile, "(%p) %s",
6280 (void *) l,
6281 dwarf_stack_op_name (l->dw_loc_opc));
6282 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6284 fprintf (outfile, " ");
6285 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6287 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6289 fprintf (outfile, ", ");
6290 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6292 fprintf (outfile, "\n");
6296 /* Print the information associated with a given DIE, and its children.
6297 This routine is a debugging aid only. */
6299 static void
6300 print_die (dw_die_ref die, FILE *outfile)
6302 dw_attr_node *a;
6303 dw_die_ref c;
6304 unsigned ix;
6306 print_spaces (outfile);
6307 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6308 die->die_offset, dwarf_tag_name (die->die_tag),
6309 (void*) die);
6310 print_spaces (outfile);
6311 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6312 fprintf (outfile, " offset: %ld", die->die_offset);
6313 fprintf (outfile, " mark: %d\n", die->die_mark);
6315 if (die->comdat_type_p)
6317 print_spaces (outfile);
6318 fprintf (outfile, " signature: ");
6319 print_signature (outfile, die->die_id.die_type_node->signature);
6320 fprintf (outfile, "\n");
6323 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6325 print_spaces (outfile);
6326 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6328 print_attribute (a, true, outfile);
6329 fprintf (outfile, "\n");
6332 if (die->die_child != NULL)
6334 print_indent += 4;
6335 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6336 print_indent -= 4;
6338 if (print_indent == 0)
6339 fprintf (outfile, "\n");
6342 /* Print the list of operations in the LOC location description. */
6344 DEBUG_FUNCTION void
6345 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6347 print_loc_descr (loc, stderr);
6350 /* Print the information collected for a given DIE. */
6352 DEBUG_FUNCTION void
6353 debug_dwarf_die (dw_die_ref die)
6355 print_die (die, stderr);
6358 DEBUG_FUNCTION void
6359 debug (die_struct &ref)
6361 print_die (&ref, stderr);
6364 DEBUG_FUNCTION void
6365 debug (die_struct *ptr)
6367 if (ptr)
6368 debug (*ptr);
6369 else
6370 fprintf (stderr, "<nil>\n");
6374 /* Print all DWARF information collected for the compilation unit.
6375 This routine is a debugging aid only. */
6377 DEBUG_FUNCTION void
6378 debug_dwarf (void)
6380 print_indent = 0;
6381 print_die (comp_unit_die (), stderr);
6384 /* Verify the DIE tree structure. */
6386 DEBUG_FUNCTION void
6387 verify_die (dw_die_ref die)
6389 gcc_assert (!die->die_mark);
6390 if (die->die_parent == NULL
6391 && die->die_sib == NULL)
6392 return;
6393 /* Verify the die_sib list is cyclic. */
6394 dw_die_ref x = die;
6397 x->die_mark = 1;
6398 x = x->die_sib;
6400 while (x && !x->die_mark);
6401 gcc_assert (x == die);
6402 x = die;
6405 /* Verify all dies have the same parent. */
6406 gcc_assert (x->die_parent == die->die_parent);
6407 if (x->die_child)
6409 /* Verify the child has the proper parent and recurse. */
6410 gcc_assert (x->die_child->die_parent == x);
6411 verify_die (x->die_child);
6413 x->die_mark = 0;
6414 x = x->die_sib;
6416 while (x && x->die_mark);
6419 /* Sanity checks on DIEs. */
6421 static void
6422 check_die (dw_die_ref die)
6424 unsigned ix;
6425 dw_attr_node *a;
6426 bool inline_found = false;
6427 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6428 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6429 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6431 switch (a->dw_attr)
6433 case DW_AT_inline:
6434 if (a->dw_attr_val.v.val_unsigned)
6435 inline_found = true;
6436 break;
6437 case DW_AT_location:
6438 ++n_location;
6439 break;
6440 case DW_AT_low_pc:
6441 ++n_low_pc;
6442 break;
6443 case DW_AT_high_pc:
6444 ++n_high_pc;
6445 break;
6446 case DW_AT_artificial:
6447 ++n_artificial;
6448 break;
6449 case DW_AT_decl_column:
6450 ++n_decl_column;
6451 break;
6452 case DW_AT_decl_line:
6453 ++n_decl_line;
6454 break;
6455 case DW_AT_decl_file:
6456 ++n_decl_file;
6457 break;
6458 default:
6459 break;
6462 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6463 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6465 fprintf (stderr, "Duplicate attributes in DIE:\n");
6466 debug_dwarf_die (die);
6467 gcc_unreachable ();
6469 if (inline_found)
6471 /* A debugging information entry that is a member of an abstract
6472 instance tree [that has DW_AT_inline] should not contain any
6473 attributes which describe aspects of the subroutine which vary
6474 between distinct inlined expansions or distinct out-of-line
6475 expansions. */
6476 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6477 gcc_assert (a->dw_attr != DW_AT_low_pc
6478 && a->dw_attr != DW_AT_high_pc
6479 && a->dw_attr != DW_AT_location
6480 && a->dw_attr != DW_AT_frame_base
6481 && a->dw_attr != DW_AT_call_all_calls
6482 && a->dw_attr != DW_AT_GNU_all_call_sites);
6486 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6487 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6488 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6490 /* Calculate the checksum of a location expression. */
6492 static inline void
6493 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6495 int tem;
6496 inchash::hash hstate;
6497 hashval_t hash;
6499 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6500 CHECKSUM (tem);
6501 hash_loc_operands (loc, hstate);
6502 hash = hstate.end();
6503 CHECKSUM (hash);
6506 /* Calculate the checksum of an attribute. */
6508 static void
6509 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6511 dw_loc_descr_ref loc;
6512 rtx r;
6514 CHECKSUM (at->dw_attr);
6516 /* We don't care that this was compiled with a different compiler
6517 snapshot; if the output is the same, that's what matters. */
6518 if (at->dw_attr == DW_AT_producer)
6519 return;
6521 switch (AT_class (at))
6523 case dw_val_class_const:
6524 case dw_val_class_const_implicit:
6525 CHECKSUM (at->dw_attr_val.v.val_int);
6526 break;
6527 case dw_val_class_unsigned_const:
6528 case dw_val_class_unsigned_const_implicit:
6529 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6530 break;
6531 case dw_val_class_const_double:
6532 CHECKSUM (at->dw_attr_val.v.val_double);
6533 break;
6534 case dw_val_class_wide_int:
6535 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6536 get_full_len (*at->dw_attr_val.v.val_wide)
6537 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6538 break;
6539 case dw_val_class_vec:
6540 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6541 (at->dw_attr_val.v.val_vec.length
6542 * at->dw_attr_val.v.val_vec.elt_size));
6543 break;
6544 case dw_val_class_flag:
6545 CHECKSUM (at->dw_attr_val.v.val_flag);
6546 break;
6547 case dw_val_class_str:
6548 CHECKSUM_STRING (AT_string (at));
6549 break;
6551 case dw_val_class_addr:
6552 r = AT_addr (at);
6553 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6554 CHECKSUM_STRING (XSTR (r, 0));
6555 break;
6557 case dw_val_class_offset:
6558 CHECKSUM (at->dw_attr_val.v.val_offset);
6559 break;
6561 case dw_val_class_loc:
6562 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6563 loc_checksum (loc, ctx);
6564 break;
6566 case dw_val_class_die_ref:
6567 die_checksum (AT_ref (at), ctx, mark);
6568 break;
6570 case dw_val_class_fde_ref:
6571 case dw_val_class_vms_delta:
6572 case dw_val_class_lbl_id:
6573 case dw_val_class_lineptr:
6574 case dw_val_class_macptr:
6575 case dw_val_class_loclistsptr:
6576 case dw_val_class_high_pc:
6577 break;
6579 case dw_val_class_file:
6580 case dw_val_class_file_implicit:
6581 CHECKSUM_STRING (AT_file (at)->filename);
6582 break;
6584 case dw_val_class_data8:
6585 CHECKSUM (at->dw_attr_val.v.val_data8);
6586 break;
6588 default:
6589 break;
6593 /* Calculate the checksum of a DIE. */
6595 static void
6596 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6598 dw_die_ref c;
6599 dw_attr_node *a;
6600 unsigned ix;
6602 /* To avoid infinite recursion. */
6603 if (die->die_mark)
6605 CHECKSUM (die->die_mark);
6606 return;
6608 die->die_mark = ++(*mark);
6610 CHECKSUM (die->die_tag);
6612 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6613 attr_checksum (a, ctx, mark);
6615 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6618 #undef CHECKSUM
6619 #undef CHECKSUM_BLOCK
6620 #undef CHECKSUM_STRING
6622 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6623 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6624 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6625 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6626 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6627 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6628 #define CHECKSUM_ATTR(FOO) \
6629 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6631 /* Calculate the checksum of a number in signed LEB128 format. */
6633 static void
6634 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6636 unsigned char byte;
6637 bool more;
6639 while (1)
6641 byte = (value & 0x7f);
6642 value >>= 7;
6643 more = !((value == 0 && (byte & 0x40) == 0)
6644 || (value == -1 && (byte & 0x40) != 0));
6645 if (more)
6646 byte |= 0x80;
6647 CHECKSUM (byte);
6648 if (!more)
6649 break;
6653 /* Calculate the checksum of a number in unsigned LEB128 format. */
6655 static void
6656 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6658 while (1)
6660 unsigned char byte = (value & 0x7f);
6661 value >>= 7;
6662 if (value != 0)
6663 /* More bytes to follow. */
6664 byte |= 0x80;
6665 CHECKSUM (byte);
6666 if (value == 0)
6667 break;
6671 /* Checksum the context of the DIE. This adds the names of any
6672 surrounding namespaces or structures to the checksum. */
6674 static void
6675 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6677 const char *name;
6678 dw_die_ref spec;
6679 int tag = die->die_tag;
6681 if (tag != DW_TAG_namespace
6682 && tag != DW_TAG_structure_type
6683 && tag != DW_TAG_class_type)
6684 return;
6686 name = get_AT_string (die, DW_AT_name);
6688 spec = get_AT_ref (die, DW_AT_specification);
6689 if (spec != NULL)
6690 die = spec;
6692 if (die->die_parent != NULL)
6693 checksum_die_context (die->die_parent, ctx);
6695 CHECKSUM_ULEB128 ('C');
6696 CHECKSUM_ULEB128 (tag);
6697 if (name != NULL)
6698 CHECKSUM_STRING (name);
6701 /* Calculate the checksum of a location expression. */
6703 static inline void
6704 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6706 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6707 were emitted as a DW_FORM_sdata instead of a location expression. */
6708 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6710 CHECKSUM_ULEB128 (DW_FORM_sdata);
6711 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6712 return;
6715 /* Otherwise, just checksum the raw location expression. */
6716 while (loc != NULL)
6718 inchash::hash hstate;
6719 hashval_t hash;
6721 CHECKSUM_ULEB128 (loc->dtprel);
6722 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6723 hash_loc_operands (loc, hstate);
6724 hash = hstate.end ();
6725 CHECKSUM (hash);
6726 loc = loc->dw_loc_next;
6730 /* Calculate the checksum of an attribute. */
6732 static void
6733 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6734 struct md5_ctx *ctx, int *mark)
6736 dw_loc_descr_ref loc;
6737 rtx r;
6739 if (AT_class (at) == dw_val_class_die_ref)
6741 dw_die_ref target_die = AT_ref (at);
6743 /* For pointer and reference types, we checksum only the (qualified)
6744 name of the target type (if there is a name). For friend entries,
6745 we checksum only the (qualified) name of the target type or function.
6746 This allows the checksum to remain the same whether the target type
6747 is complete or not. */
6748 if ((at->dw_attr == DW_AT_type
6749 && (tag == DW_TAG_pointer_type
6750 || tag == DW_TAG_reference_type
6751 || tag == DW_TAG_rvalue_reference_type
6752 || tag == DW_TAG_ptr_to_member_type))
6753 || (at->dw_attr == DW_AT_friend
6754 && tag == DW_TAG_friend))
6756 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6758 if (name_attr != NULL)
6760 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6762 if (decl == NULL)
6763 decl = target_die;
6764 CHECKSUM_ULEB128 ('N');
6765 CHECKSUM_ULEB128 (at->dw_attr);
6766 if (decl->die_parent != NULL)
6767 checksum_die_context (decl->die_parent, ctx);
6768 CHECKSUM_ULEB128 ('E');
6769 CHECKSUM_STRING (AT_string (name_attr));
6770 return;
6774 /* For all other references to another DIE, we check to see if the
6775 target DIE has already been visited. If it has, we emit a
6776 backward reference; if not, we descend recursively. */
6777 if (target_die->die_mark > 0)
6779 CHECKSUM_ULEB128 ('R');
6780 CHECKSUM_ULEB128 (at->dw_attr);
6781 CHECKSUM_ULEB128 (target_die->die_mark);
6783 else
6785 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6787 if (decl == NULL)
6788 decl = target_die;
6789 target_die->die_mark = ++(*mark);
6790 CHECKSUM_ULEB128 ('T');
6791 CHECKSUM_ULEB128 (at->dw_attr);
6792 if (decl->die_parent != NULL)
6793 checksum_die_context (decl->die_parent, ctx);
6794 die_checksum_ordered (target_die, ctx, mark);
6796 return;
6799 CHECKSUM_ULEB128 ('A');
6800 CHECKSUM_ULEB128 (at->dw_attr);
6802 switch (AT_class (at))
6804 case dw_val_class_const:
6805 case dw_val_class_const_implicit:
6806 CHECKSUM_ULEB128 (DW_FORM_sdata);
6807 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6808 break;
6810 case dw_val_class_unsigned_const:
6811 case dw_val_class_unsigned_const_implicit:
6812 CHECKSUM_ULEB128 (DW_FORM_sdata);
6813 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6814 break;
6816 case dw_val_class_const_double:
6817 CHECKSUM_ULEB128 (DW_FORM_block);
6818 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6819 CHECKSUM (at->dw_attr_val.v.val_double);
6820 break;
6822 case dw_val_class_wide_int:
6823 CHECKSUM_ULEB128 (DW_FORM_block);
6824 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6825 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6826 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6827 get_full_len (*at->dw_attr_val.v.val_wide)
6828 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6829 break;
6831 case dw_val_class_vec:
6832 CHECKSUM_ULEB128 (DW_FORM_block);
6833 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6834 * at->dw_attr_val.v.val_vec.elt_size);
6835 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6836 (at->dw_attr_val.v.val_vec.length
6837 * at->dw_attr_val.v.val_vec.elt_size));
6838 break;
6840 case dw_val_class_flag:
6841 CHECKSUM_ULEB128 (DW_FORM_flag);
6842 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6843 break;
6845 case dw_val_class_str:
6846 CHECKSUM_ULEB128 (DW_FORM_string);
6847 CHECKSUM_STRING (AT_string (at));
6848 break;
6850 case dw_val_class_addr:
6851 r = AT_addr (at);
6852 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6853 CHECKSUM_ULEB128 (DW_FORM_string);
6854 CHECKSUM_STRING (XSTR (r, 0));
6855 break;
6857 case dw_val_class_offset:
6858 CHECKSUM_ULEB128 (DW_FORM_sdata);
6859 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6860 break;
6862 case dw_val_class_loc:
6863 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6864 loc_checksum_ordered (loc, ctx);
6865 break;
6867 case dw_val_class_fde_ref:
6868 case dw_val_class_lbl_id:
6869 case dw_val_class_lineptr:
6870 case dw_val_class_macptr:
6871 case dw_val_class_loclistsptr:
6872 case dw_val_class_high_pc:
6873 break;
6875 case dw_val_class_file:
6876 case dw_val_class_file_implicit:
6877 CHECKSUM_ULEB128 (DW_FORM_string);
6878 CHECKSUM_STRING (AT_file (at)->filename);
6879 break;
6881 case dw_val_class_data8:
6882 CHECKSUM (at->dw_attr_val.v.val_data8);
6883 break;
6885 default:
6886 break;
6890 struct checksum_attributes
6892 dw_attr_node *at_name;
6893 dw_attr_node *at_type;
6894 dw_attr_node *at_friend;
6895 dw_attr_node *at_accessibility;
6896 dw_attr_node *at_address_class;
6897 dw_attr_node *at_alignment;
6898 dw_attr_node *at_allocated;
6899 dw_attr_node *at_artificial;
6900 dw_attr_node *at_associated;
6901 dw_attr_node *at_binary_scale;
6902 dw_attr_node *at_bit_offset;
6903 dw_attr_node *at_bit_size;
6904 dw_attr_node *at_bit_stride;
6905 dw_attr_node *at_byte_size;
6906 dw_attr_node *at_byte_stride;
6907 dw_attr_node *at_const_value;
6908 dw_attr_node *at_containing_type;
6909 dw_attr_node *at_count;
6910 dw_attr_node *at_data_location;
6911 dw_attr_node *at_data_member_location;
6912 dw_attr_node *at_decimal_scale;
6913 dw_attr_node *at_decimal_sign;
6914 dw_attr_node *at_default_value;
6915 dw_attr_node *at_digit_count;
6916 dw_attr_node *at_discr;
6917 dw_attr_node *at_discr_list;
6918 dw_attr_node *at_discr_value;
6919 dw_attr_node *at_encoding;
6920 dw_attr_node *at_endianity;
6921 dw_attr_node *at_explicit;
6922 dw_attr_node *at_is_optional;
6923 dw_attr_node *at_location;
6924 dw_attr_node *at_lower_bound;
6925 dw_attr_node *at_mutable;
6926 dw_attr_node *at_ordering;
6927 dw_attr_node *at_picture_string;
6928 dw_attr_node *at_prototyped;
6929 dw_attr_node *at_small;
6930 dw_attr_node *at_segment;
6931 dw_attr_node *at_string_length;
6932 dw_attr_node *at_string_length_bit_size;
6933 dw_attr_node *at_string_length_byte_size;
6934 dw_attr_node *at_threads_scaled;
6935 dw_attr_node *at_upper_bound;
6936 dw_attr_node *at_use_location;
6937 dw_attr_node *at_use_UTF8;
6938 dw_attr_node *at_variable_parameter;
6939 dw_attr_node *at_virtuality;
6940 dw_attr_node *at_visibility;
6941 dw_attr_node *at_vtable_elem_location;
6944 /* Collect the attributes that we will want to use for the checksum. */
6946 static void
6947 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6949 dw_attr_node *a;
6950 unsigned ix;
6952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6954 switch (a->dw_attr)
6956 case DW_AT_name:
6957 attrs->at_name = a;
6958 break;
6959 case DW_AT_type:
6960 attrs->at_type = a;
6961 break;
6962 case DW_AT_friend:
6963 attrs->at_friend = a;
6964 break;
6965 case DW_AT_accessibility:
6966 attrs->at_accessibility = a;
6967 break;
6968 case DW_AT_address_class:
6969 attrs->at_address_class = a;
6970 break;
6971 case DW_AT_alignment:
6972 attrs->at_alignment = a;
6973 break;
6974 case DW_AT_allocated:
6975 attrs->at_allocated = a;
6976 break;
6977 case DW_AT_artificial:
6978 attrs->at_artificial = a;
6979 break;
6980 case DW_AT_associated:
6981 attrs->at_associated = a;
6982 break;
6983 case DW_AT_binary_scale:
6984 attrs->at_binary_scale = a;
6985 break;
6986 case DW_AT_bit_offset:
6987 attrs->at_bit_offset = a;
6988 break;
6989 case DW_AT_bit_size:
6990 attrs->at_bit_size = a;
6991 break;
6992 case DW_AT_bit_stride:
6993 attrs->at_bit_stride = a;
6994 break;
6995 case DW_AT_byte_size:
6996 attrs->at_byte_size = a;
6997 break;
6998 case DW_AT_byte_stride:
6999 attrs->at_byte_stride = a;
7000 break;
7001 case DW_AT_const_value:
7002 attrs->at_const_value = a;
7003 break;
7004 case DW_AT_containing_type:
7005 attrs->at_containing_type = a;
7006 break;
7007 case DW_AT_count:
7008 attrs->at_count = a;
7009 break;
7010 case DW_AT_data_location:
7011 attrs->at_data_location = a;
7012 break;
7013 case DW_AT_data_member_location:
7014 attrs->at_data_member_location = a;
7015 break;
7016 case DW_AT_decimal_scale:
7017 attrs->at_decimal_scale = a;
7018 break;
7019 case DW_AT_decimal_sign:
7020 attrs->at_decimal_sign = a;
7021 break;
7022 case DW_AT_default_value:
7023 attrs->at_default_value = a;
7024 break;
7025 case DW_AT_digit_count:
7026 attrs->at_digit_count = a;
7027 break;
7028 case DW_AT_discr:
7029 attrs->at_discr = a;
7030 break;
7031 case DW_AT_discr_list:
7032 attrs->at_discr_list = a;
7033 break;
7034 case DW_AT_discr_value:
7035 attrs->at_discr_value = a;
7036 break;
7037 case DW_AT_encoding:
7038 attrs->at_encoding = a;
7039 break;
7040 case DW_AT_endianity:
7041 attrs->at_endianity = a;
7042 break;
7043 case DW_AT_explicit:
7044 attrs->at_explicit = a;
7045 break;
7046 case DW_AT_is_optional:
7047 attrs->at_is_optional = a;
7048 break;
7049 case DW_AT_location:
7050 attrs->at_location = a;
7051 break;
7052 case DW_AT_lower_bound:
7053 attrs->at_lower_bound = a;
7054 break;
7055 case DW_AT_mutable:
7056 attrs->at_mutable = a;
7057 break;
7058 case DW_AT_ordering:
7059 attrs->at_ordering = a;
7060 break;
7061 case DW_AT_picture_string:
7062 attrs->at_picture_string = a;
7063 break;
7064 case DW_AT_prototyped:
7065 attrs->at_prototyped = a;
7066 break;
7067 case DW_AT_small:
7068 attrs->at_small = a;
7069 break;
7070 case DW_AT_segment:
7071 attrs->at_segment = a;
7072 break;
7073 case DW_AT_string_length:
7074 attrs->at_string_length = a;
7075 break;
7076 case DW_AT_string_length_bit_size:
7077 attrs->at_string_length_bit_size = a;
7078 break;
7079 case DW_AT_string_length_byte_size:
7080 attrs->at_string_length_byte_size = a;
7081 break;
7082 case DW_AT_threads_scaled:
7083 attrs->at_threads_scaled = a;
7084 break;
7085 case DW_AT_upper_bound:
7086 attrs->at_upper_bound = a;
7087 break;
7088 case DW_AT_use_location:
7089 attrs->at_use_location = a;
7090 break;
7091 case DW_AT_use_UTF8:
7092 attrs->at_use_UTF8 = a;
7093 break;
7094 case DW_AT_variable_parameter:
7095 attrs->at_variable_parameter = a;
7096 break;
7097 case DW_AT_virtuality:
7098 attrs->at_virtuality = a;
7099 break;
7100 case DW_AT_visibility:
7101 attrs->at_visibility = a;
7102 break;
7103 case DW_AT_vtable_elem_location:
7104 attrs->at_vtable_elem_location = a;
7105 break;
7106 default:
7107 break;
7112 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7114 static void
7115 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7117 dw_die_ref c;
7118 dw_die_ref decl;
7119 struct checksum_attributes attrs;
7121 CHECKSUM_ULEB128 ('D');
7122 CHECKSUM_ULEB128 (die->die_tag);
7124 memset (&attrs, 0, sizeof (attrs));
7126 decl = get_AT_ref (die, DW_AT_specification);
7127 if (decl != NULL)
7128 collect_checksum_attributes (&attrs, decl);
7129 collect_checksum_attributes (&attrs, die);
7131 CHECKSUM_ATTR (attrs.at_name);
7132 CHECKSUM_ATTR (attrs.at_accessibility);
7133 CHECKSUM_ATTR (attrs.at_address_class);
7134 CHECKSUM_ATTR (attrs.at_allocated);
7135 CHECKSUM_ATTR (attrs.at_artificial);
7136 CHECKSUM_ATTR (attrs.at_associated);
7137 CHECKSUM_ATTR (attrs.at_binary_scale);
7138 CHECKSUM_ATTR (attrs.at_bit_offset);
7139 CHECKSUM_ATTR (attrs.at_bit_size);
7140 CHECKSUM_ATTR (attrs.at_bit_stride);
7141 CHECKSUM_ATTR (attrs.at_byte_size);
7142 CHECKSUM_ATTR (attrs.at_byte_stride);
7143 CHECKSUM_ATTR (attrs.at_const_value);
7144 CHECKSUM_ATTR (attrs.at_containing_type);
7145 CHECKSUM_ATTR (attrs.at_count);
7146 CHECKSUM_ATTR (attrs.at_data_location);
7147 CHECKSUM_ATTR (attrs.at_data_member_location);
7148 CHECKSUM_ATTR (attrs.at_decimal_scale);
7149 CHECKSUM_ATTR (attrs.at_decimal_sign);
7150 CHECKSUM_ATTR (attrs.at_default_value);
7151 CHECKSUM_ATTR (attrs.at_digit_count);
7152 CHECKSUM_ATTR (attrs.at_discr);
7153 CHECKSUM_ATTR (attrs.at_discr_list);
7154 CHECKSUM_ATTR (attrs.at_discr_value);
7155 CHECKSUM_ATTR (attrs.at_encoding);
7156 CHECKSUM_ATTR (attrs.at_endianity);
7157 CHECKSUM_ATTR (attrs.at_explicit);
7158 CHECKSUM_ATTR (attrs.at_is_optional);
7159 CHECKSUM_ATTR (attrs.at_location);
7160 CHECKSUM_ATTR (attrs.at_lower_bound);
7161 CHECKSUM_ATTR (attrs.at_mutable);
7162 CHECKSUM_ATTR (attrs.at_ordering);
7163 CHECKSUM_ATTR (attrs.at_picture_string);
7164 CHECKSUM_ATTR (attrs.at_prototyped);
7165 CHECKSUM_ATTR (attrs.at_small);
7166 CHECKSUM_ATTR (attrs.at_segment);
7167 CHECKSUM_ATTR (attrs.at_string_length);
7168 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7169 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7170 CHECKSUM_ATTR (attrs.at_threads_scaled);
7171 CHECKSUM_ATTR (attrs.at_upper_bound);
7172 CHECKSUM_ATTR (attrs.at_use_location);
7173 CHECKSUM_ATTR (attrs.at_use_UTF8);
7174 CHECKSUM_ATTR (attrs.at_variable_parameter);
7175 CHECKSUM_ATTR (attrs.at_virtuality);
7176 CHECKSUM_ATTR (attrs.at_visibility);
7177 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7178 CHECKSUM_ATTR (attrs.at_type);
7179 CHECKSUM_ATTR (attrs.at_friend);
7180 CHECKSUM_ATTR (attrs.at_alignment);
7182 /* Checksum the child DIEs. */
7183 c = die->die_child;
7184 if (c) do {
7185 dw_attr_node *name_attr;
7187 c = c->die_sib;
7188 name_attr = get_AT (c, DW_AT_name);
7189 if (is_template_instantiation (c))
7191 /* Ignore instantiations of member type and function templates. */
7193 else if (name_attr != NULL
7194 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7196 /* Use a shallow checksum for named nested types and member
7197 functions. */
7198 CHECKSUM_ULEB128 ('S');
7199 CHECKSUM_ULEB128 (c->die_tag);
7200 CHECKSUM_STRING (AT_string (name_attr));
7202 else
7204 /* Use a deep checksum for other children. */
7205 /* Mark this DIE so it gets processed when unmarking. */
7206 if (c->die_mark == 0)
7207 c->die_mark = -1;
7208 die_checksum_ordered (c, ctx, mark);
7210 } while (c != die->die_child);
7212 CHECKSUM_ULEB128 (0);
7215 /* Add a type name and tag to a hash. */
7216 static void
7217 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7219 CHECKSUM_ULEB128 (tag);
7220 CHECKSUM_STRING (name);
7223 #undef CHECKSUM
7224 #undef CHECKSUM_STRING
7225 #undef CHECKSUM_ATTR
7226 #undef CHECKSUM_LEB128
7227 #undef CHECKSUM_ULEB128
7229 /* Generate the type signature for DIE. This is computed by generating an
7230 MD5 checksum over the DIE's tag, its relevant attributes, and its
7231 children. Attributes that are references to other DIEs are processed
7232 by recursion, using the MARK field to prevent infinite recursion.
7233 If the DIE is nested inside a namespace or another type, we also
7234 need to include that context in the signature. The lower 64 bits
7235 of the resulting MD5 checksum comprise the signature. */
7237 static void
7238 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7240 int mark;
7241 const char *name;
7242 unsigned char checksum[16];
7243 struct md5_ctx ctx;
7244 dw_die_ref decl;
7245 dw_die_ref parent;
7247 name = get_AT_string (die, DW_AT_name);
7248 decl = get_AT_ref (die, DW_AT_specification);
7249 parent = get_die_parent (die);
7251 /* First, compute a signature for just the type name (and its surrounding
7252 context, if any. This is stored in the type unit DIE for link-time
7253 ODR (one-definition rule) checking. */
7255 if (is_cxx () && name != NULL)
7257 md5_init_ctx (&ctx);
7259 /* Checksum the names of surrounding namespaces and structures. */
7260 if (parent != NULL)
7261 checksum_die_context (parent, &ctx);
7263 /* Checksum the current DIE. */
7264 die_odr_checksum (die->die_tag, name, &ctx);
7265 md5_finish_ctx (&ctx, checksum);
7267 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7270 /* Next, compute the complete type signature. */
7272 md5_init_ctx (&ctx);
7273 mark = 1;
7274 die->die_mark = mark;
7276 /* Checksum the names of surrounding namespaces and structures. */
7277 if (parent != NULL)
7278 checksum_die_context (parent, &ctx);
7280 /* Checksum the DIE and its children. */
7281 die_checksum_ordered (die, &ctx, &mark);
7282 unmark_all_dies (die);
7283 md5_finish_ctx (&ctx, checksum);
7285 /* Store the signature in the type node and link the type DIE and the
7286 type node together. */
7287 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7288 DWARF_TYPE_SIGNATURE_SIZE);
7289 die->comdat_type_p = true;
7290 die->die_id.die_type_node = type_node;
7291 type_node->type_die = die;
7293 /* If the DIE is a specification, link its declaration to the type node
7294 as well. */
7295 if (decl != NULL)
7297 decl->comdat_type_p = true;
7298 decl->die_id.die_type_node = type_node;
7302 /* Do the location expressions look same? */
7303 static inline int
7304 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7306 return loc1->dw_loc_opc == loc2->dw_loc_opc
7307 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7308 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7311 /* Do the values look the same? */
7312 static int
7313 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7315 dw_loc_descr_ref loc1, loc2;
7316 rtx r1, r2;
7318 if (v1->val_class != v2->val_class)
7319 return 0;
7321 switch (v1->val_class)
7323 case dw_val_class_const:
7324 case dw_val_class_const_implicit:
7325 return v1->v.val_int == v2->v.val_int;
7326 case dw_val_class_unsigned_const:
7327 case dw_val_class_unsigned_const_implicit:
7328 return v1->v.val_unsigned == v2->v.val_unsigned;
7329 case dw_val_class_const_double:
7330 return v1->v.val_double.high == v2->v.val_double.high
7331 && v1->v.val_double.low == v2->v.val_double.low;
7332 case dw_val_class_wide_int:
7333 return *v1->v.val_wide == *v2->v.val_wide;
7334 case dw_val_class_vec:
7335 if (v1->v.val_vec.length != v2->v.val_vec.length
7336 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7337 return 0;
7338 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7339 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7340 return 0;
7341 return 1;
7342 case dw_val_class_flag:
7343 return v1->v.val_flag == v2->v.val_flag;
7344 case dw_val_class_str:
7345 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7347 case dw_val_class_addr:
7348 r1 = v1->v.val_addr;
7349 r2 = v2->v.val_addr;
7350 if (GET_CODE (r1) != GET_CODE (r2))
7351 return 0;
7352 return !rtx_equal_p (r1, r2);
7354 case dw_val_class_offset:
7355 return v1->v.val_offset == v2->v.val_offset;
7357 case dw_val_class_loc:
7358 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7359 loc1 && loc2;
7360 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7361 if (!same_loc_p (loc1, loc2, mark))
7362 return 0;
7363 return !loc1 && !loc2;
7365 case dw_val_class_die_ref:
7366 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7368 case dw_val_class_fde_ref:
7369 case dw_val_class_vms_delta:
7370 case dw_val_class_lbl_id:
7371 case dw_val_class_lineptr:
7372 case dw_val_class_macptr:
7373 case dw_val_class_loclistsptr:
7374 case dw_val_class_high_pc:
7375 return 1;
7377 case dw_val_class_file:
7378 case dw_val_class_file_implicit:
7379 return v1->v.val_file == v2->v.val_file;
7381 case dw_val_class_data8:
7382 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7384 default:
7385 return 1;
7389 /* Do the attributes look the same? */
7391 static int
7392 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7394 if (at1->dw_attr != at2->dw_attr)
7395 return 0;
7397 /* We don't care that this was compiled with a different compiler
7398 snapshot; if the output is the same, that's what matters. */
7399 if (at1->dw_attr == DW_AT_producer)
7400 return 1;
7402 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7405 /* Do the dies look the same? */
7407 static int
7408 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7410 dw_die_ref c1, c2;
7411 dw_attr_node *a1;
7412 unsigned ix;
7414 /* To avoid infinite recursion. */
7415 if (die1->die_mark)
7416 return die1->die_mark == die2->die_mark;
7417 die1->die_mark = die2->die_mark = ++(*mark);
7419 if (die1->die_tag != die2->die_tag)
7420 return 0;
7422 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7423 return 0;
7425 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7426 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7427 return 0;
7429 c1 = die1->die_child;
7430 c2 = die2->die_child;
7431 if (! c1)
7433 if (c2)
7434 return 0;
7436 else
7437 for (;;)
7439 if (!same_die_p (c1, c2, mark))
7440 return 0;
7441 c1 = c1->die_sib;
7442 c2 = c2->die_sib;
7443 if (c1 == die1->die_child)
7445 if (c2 == die2->die_child)
7446 break;
7447 else
7448 return 0;
7452 return 1;
7455 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7456 children, and set die_symbol. */
7458 static void
7459 compute_comp_unit_symbol (dw_die_ref unit_die)
7461 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7462 const char *base = die_name ? lbasename (die_name) : "anonymous";
7463 char *name = XALLOCAVEC (char, strlen (base) + 64);
7464 char *p;
7465 int i, mark;
7466 unsigned char checksum[16];
7467 struct md5_ctx ctx;
7469 /* Compute the checksum of the DIE, then append part of it as hex digits to
7470 the name filename of the unit. */
7472 md5_init_ctx (&ctx);
7473 mark = 0;
7474 die_checksum (unit_die, &ctx, &mark);
7475 unmark_all_dies (unit_die);
7476 md5_finish_ctx (&ctx, checksum);
7478 /* When we this for comp_unit_die () we have a DW_AT_name that might
7479 not start with a letter but with anything valid for filenames and
7480 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7481 character is not a letter. */
7482 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7483 clean_symbol_name (name);
7485 p = name + strlen (name);
7486 for (i = 0; i < 4; i++)
7488 sprintf (p, "%.2x", checksum[i]);
7489 p += 2;
7492 unit_die->die_id.die_symbol = xstrdup (name);
7495 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7497 static int
7498 is_type_die (dw_die_ref die)
7500 switch (die->die_tag)
7502 case DW_TAG_array_type:
7503 case DW_TAG_class_type:
7504 case DW_TAG_interface_type:
7505 case DW_TAG_enumeration_type:
7506 case DW_TAG_pointer_type:
7507 case DW_TAG_reference_type:
7508 case DW_TAG_rvalue_reference_type:
7509 case DW_TAG_string_type:
7510 case DW_TAG_structure_type:
7511 case DW_TAG_subroutine_type:
7512 case DW_TAG_union_type:
7513 case DW_TAG_ptr_to_member_type:
7514 case DW_TAG_set_type:
7515 case DW_TAG_subrange_type:
7516 case DW_TAG_base_type:
7517 case DW_TAG_const_type:
7518 case DW_TAG_file_type:
7519 case DW_TAG_packed_type:
7520 case DW_TAG_volatile_type:
7521 case DW_TAG_typedef:
7522 return 1;
7523 default:
7524 return 0;
7528 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7529 Basically, we want to choose the bits that are likely to be shared between
7530 compilations (types) and leave out the bits that are specific to individual
7531 compilations (functions). */
7533 static int
7534 is_comdat_die (dw_die_ref c)
7536 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7537 we do for stabs. The advantage is a greater likelihood of sharing between
7538 objects that don't include headers in the same order (and therefore would
7539 put the base types in a different comdat). jason 8/28/00 */
7541 if (c->die_tag == DW_TAG_base_type)
7542 return 0;
7544 if (c->die_tag == DW_TAG_pointer_type
7545 || c->die_tag == DW_TAG_reference_type
7546 || c->die_tag == DW_TAG_rvalue_reference_type
7547 || c->die_tag == DW_TAG_const_type
7548 || c->die_tag == DW_TAG_volatile_type)
7550 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7552 return t ? is_comdat_die (t) : 0;
7555 return is_type_die (c);
7558 /* Returns true iff C is a compile-unit DIE. */
7560 static inline bool
7561 is_cu_die (dw_die_ref c)
7563 return c && (c->die_tag == DW_TAG_compile_unit
7564 || c->die_tag == DW_TAG_skeleton_unit);
7567 /* Returns true iff C is a unit DIE of some sort. */
7569 static inline bool
7570 is_unit_die (dw_die_ref c)
7572 return c && (c->die_tag == DW_TAG_compile_unit
7573 || c->die_tag == DW_TAG_partial_unit
7574 || c->die_tag == DW_TAG_type_unit
7575 || c->die_tag == DW_TAG_skeleton_unit);
7578 /* Returns true iff C is a namespace DIE. */
7580 static inline bool
7581 is_namespace_die (dw_die_ref c)
7583 return c && c->die_tag == DW_TAG_namespace;
7586 /* Returns true iff C is a class or structure DIE. */
7588 static inline bool
7589 is_class_die (dw_die_ref c)
7591 return c && (c->die_tag == DW_TAG_class_type
7592 || c->die_tag == DW_TAG_structure_type);
7595 /* Return non-zero if this DIE is a template parameter. */
7597 static inline bool
7598 is_template_parameter (dw_die_ref die)
7600 switch (die->die_tag)
7602 case DW_TAG_template_type_param:
7603 case DW_TAG_template_value_param:
7604 case DW_TAG_GNU_template_template_param:
7605 case DW_TAG_GNU_template_parameter_pack:
7606 return true;
7607 default:
7608 return false;
7612 /* Return non-zero if this DIE represents a template instantiation. */
7614 static inline bool
7615 is_template_instantiation (dw_die_ref die)
7617 dw_die_ref c;
7619 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7620 return false;
7621 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7622 return false;
7625 static char *
7626 gen_internal_sym (const char *prefix)
7628 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7630 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7631 return xstrdup (buf);
7634 /* Return non-zero if this DIE is a declaration. */
7636 static int
7637 is_declaration_die (dw_die_ref die)
7639 dw_attr_node *a;
7640 unsigned ix;
7642 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7643 if (a->dw_attr == DW_AT_declaration)
7644 return 1;
7646 return 0;
7649 /* Return non-zero if this DIE is nested inside a subprogram. */
7651 static int
7652 is_nested_in_subprogram (dw_die_ref die)
7654 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7656 if (decl == NULL)
7657 decl = die;
7658 return local_scope_p (decl);
7661 /* Return non-zero if this DIE contains a defining declaration of a
7662 subprogram. */
7664 static int
7665 contains_subprogram_definition (dw_die_ref die)
7667 dw_die_ref c;
7669 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7670 return 1;
7671 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7672 return 0;
7675 /* Return non-zero if this is a type DIE that should be moved to a
7676 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7677 unit type. */
7679 static int
7680 should_move_die_to_comdat (dw_die_ref die)
7682 switch (die->die_tag)
7684 case DW_TAG_class_type:
7685 case DW_TAG_structure_type:
7686 case DW_TAG_enumeration_type:
7687 case DW_TAG_union_type:
7688 /* Don't move declarations, inlined instances, types nested in a
7689 subprogram, or types that contain subprogram definitions. */
7690 if (is_declaration_die (die)
7691 || get_AT (die, DW_AT_abstract_origin)
7692 || is_nested_in_subprogram (die)
7693 || contains_subprogram_definition (die))
7694 return 0;
7695 return 1;
7696 case DW_TAG_array_type:
7697 case DW_TAG_interface_type:
7698 case DW_TAG_pointer_type:
7699 case DW_TAG_reference_type:
7700 case DW_TAG_rvalue_reference_type:
7701 case DW_TAG_string_type:
7702 case DW_TAG_subroutine_type:
7703 case DW_TAG_ptr_to_member_type:
7704 case DW_TAG_set_type:
7705 case DW_TAG_subrange_type:
7706 case DW_TAG_base_type:
7707 case DW_TAG_const_type:
7708 case DW_TAG_file_type:
7709 case DW_TAG_packed_type:
7710 case DW_TAG_volatile_type:
7711 case DW_TAG_typedef:
7712 default:
7713 return 0;
7717 /* Make a clone of DIE. */
7719 static dw_die_ref
7720 clone_die (dw_die_ref die)
7722 dw_die_ref clone = new_die_raw (die->die_tag);
7723 dw_attr_node *a;
7724 unsigned ix;
7726 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7727 add_dwarf_attr (clone, a);
7729 return clone;
7732 /* Make a clone of the tree rooted at DIE. */
7734 static dw_die_ref
7735 clone_tree (dw_die_ref die)
7737 dw_die_ref c;
7738 dw_die_ref clone = clone_die (die);
7740 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7742 return clone;
7745 /* Make a clone of DIE as a declaration. */
7747 static dw_die_ref
7748 clone_as_declaration (dw_die_ref die)
7750 dw_die_ref clone;
7751 dw_die_ref decl;
7752 dw_attr_node *a;
7753 unsigned ix;
7755 /* If the DIE is already a declaration, just clone it. */
7756 if (is_declaration_die (die))
7757 return clone_die (die);
7759 /* If the DIE is a specification, just clone its declaration DIE. */
7760 decl = get_AT_ref (die, DW_AT_specification);
7761 if (decl != NULL)
7763 clone = clone_die (decl);
7764 if (die->comdat_type_p)
7765 add_AT_die_ref (clone, DW_AT_signature, die);
7766 return clone;
7769 clone = new_die_raw (die->die_tag);
7771 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7773 /* We don't want to copy over all attributes.
7774 For example we don't want DW_AT_byte_size because otherwise we will no
7775 longer have a declaration and GDB will treat it as a definition. */
7777 switch (a->dw_attr)
7779 case DW_AT_abstract_origin:
7780 case DW_AT_artificial:
7781 case DW_AT_containing_type:
7782 case DW_AT_external:
7783 case DW_AT_name:
7784 case DW_AT_type:
7785 case DW_AT_virtuality:
7786 case DW_AT_linkage_name:
7787 case DW_AT_MIPS_linkage_name:
7788 add_dwarf_attr (clone, a);
7789 break;
7790 case DW_AT_byte_size:
7791 case DW_AT_alignment:
7792 default:
7793 break;
7797 if (die->comdat_type_p)
7798 add_AT_die_ref (clone, DW_AT_signature, die);
7800 add_AT_flag (clone, DW_AT_declaration, 1);
7801 return clone;
7805 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7807 struct decl_table_entry
7809 dw_die_ref orig;
7810 dw_die_ref copy;
7813 /* Helpers to manipulate hash table of copied declarations. */
7815 /* Hashtable helpers. */
7817 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7819 typedef die_struct *compare_type;
7820 static inline hashval_t hash (const decl_table_entry *);
7821 static inline bool equal (const decl_table_entry *, const die_struct *);
7824 inline hashval_t
7825 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7827 return htab_hash_pointer (entry->orig);
7830 inline bool
7831 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7832 const die_struct *entry2)
7834 return entry1->orig == entry2;
7837 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7839 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7840 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7841 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7842 to check if the ancestor has already been copied into UNIT. */
7844 static dw_die_ref
7845 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7846 decl_hash_type *decl_table)
7848 dw_die_ref parent = die->die_parent;
7849 dw_die_ref new_parent = unit;
7850 dw_die_ref copy;
7851 decl_table_entry **slot = NULL;
7852 struct decl_table_entry *entry = NULL;
7854 if (decl_table)
7856 /* Check if the entry has already been copied to UNIT. */
7857 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7858 INSERT);
7859 if (*slot != HTAB_EMPTY_ENTRY)
7861 entry = *slot;
7862 return entry->copy;
7865 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7866 entry = XCNEW (struct decl_table_entry);
7867 entry->orig = die;
7868 entry->copy = NULL;
7869 *slot = entry;
7872 if (parent != NULL)
7874 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7875 if (spec != NULL)
7876 parent = spec;
7877 if (!is_unit_die (parent))
7878 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7881 copy = clone_as_declaration (die);
7882 add_child_die (new_parent, copy);
7884 if (decl_table)
7886 /* Record the pointer to the copy. */
7887 entry->copy = copy;
7890 return copy;
7892 /* Copy the declaration context to the new type unit DIE. This includes
7893 any surrounding namespace or type declarations. If the DIE has an
7894 AT_specification attribute, it also includes attributes and children
7895 attached to the specification, and returns a pointer to the original
7896 parent of the declaration DIE. Returns NULL otherwise. */
7898 static dw_die_ref
7899 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7901 dw_die_ref decl;
7902 dw_die_ref new_decl;
7903 dw_die_ref orig_parent = NULL;
7905 decl = get_AT_ref (die, DW_AT_specification);
7906 if (decl == NULL)
7907 decl = die;
7908 else
7910 unsigned ix;
7911 dw_die_ref c;
7912 dw_attr_node *a;
7914 /* The original DIE will be changed to a declaration, and must
7915 be moved to be a child of the original declaration DIE. */
7916 orig_parent = decl->die_parent;
7918 /* Copy the type node pointer from the new DIE to the original
7919 declaration DIE so we can forward references later. */
7920 decl->comdat_type_p = true;
7921 decl->die_id.die_type_node = die->die_id.die_type_node;
7923 remove_AT (die, DW_AT_specification);
7925 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7927 if (a->dw_attr != DW_AT_name
7928 && a->dw_attr != DW_AT_declaration
7929 && a->dw_attr != DW_AT_external)
7930 add_dwarf_attr (die, a);
7933 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7936 if (decl->die_parent != NULL
7937 && !is_unit_die (decl->die_parent))
7939 new_decl = copy_ancestor_tree (unit, decl, NULL);
7940 if (new_decl != NULL)
7942 remove_AT (new_decl, DW_AT_signature);
7943 add_AT_specification (die, new_decl);
7947 return orig_parent;
7950 /* Generate the skeleton ancestor tree for the given NODE, then clone
7951 the DIE and add the clone into the tree. */
7953 static void
7954 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7956 if (node->new_die != NULL)
7957 return;
7959 node->new_die = clone_as_declaration (node->old_die);
7961 if (node->parent != NULL)
7963 generate_skeleton_ancestor_tree (node->parent);
7964 add_child_die (node->parent->new_die, node->new_die);
7968 /* Generate a skeleton tree of DIEs containing any declarations that are
7969 found in the original tree. We traverse the tree looking for declaration
7970 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7972 static void
7973 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7975 skeleton_chain_node node;
7976 dw_die_ref c;
7977 dw_die_ref first;
7978 dw_die_ref prev = NULL;
7979 dw_die_ref next = NULL;
7981 node.parent = parent;
7983 first = c = parent->old_die->die_child;
7984 if (c)
7985 next = c->die_sib;
7986 if (c) do {
7987 if (prev == NULL || prev->die_sib == c)
7988 prev = c;
7989 c = next;
7990 next = (c == first ? NULL : c->die_sib);
7991 node.old_die = c;
7992 node.new_die = NULL;
7993 if (is_declaration_die (c))
7995 if (is_template_instantiation (c))
7997 /* Instantiated templates do not need to be cloned into the
7998 type unit. Just move the DIE and its children back to
7999 the skeleton tree (in the main CU). */
8000 remove_child_with_prev (c, prev);
8001 add_child_die (parent->new_die, c);
8002 c = prev;
8004 else if (c->comdat_type_p)
8006 /* This is the skeleton of earlier break_out_comdat_types
8007 type. Clone the existing DIE, but keep the children
8008 under the original (which is in the main CU). */
8009 dw_die_ref clone = clone_die (c);
8011 replace_child (c, clone, prev);
8012 generate_skeleton_ancestor_tree (parent);
8013 add_child_die (parent->new_die, c);
8014 c = clone;
8015 continue;
8017 else
8019 /* Clone the existing DIE, move the original to the skeleton
8020 tree (which is in the main CU), and put the clone, with
8021 all the original's children, where the original came from
8022 (which is about to be moved to the type unit). */
8023 dw_die_ref clone = clone_die (c);
8024 move_all_children (c, clone);
8026 /* If the original has a DW_AT_object_pointer attribute,
8027 it would now point to a child DIE just moved to the
8028 cloned tree, so we need to remove that attribute from
8029 the original. */
8030 remove_AT (c, DW_AT_object_pointer);
8032 replace_child (c, clone, prev);
8033 generate_skeleton_ancestor_tree (parent);
8034 add_child_die (parent->new_die, c);
8035 node.old_die = clone;
8036 node.new_die = c;
8037 c = clone;
8040 generate_skeleton_bottom_up (&node);
8041 } while (next != NULL);
8044 /* Wrapper function for generate_skeleton_bottom_up. */
8046 static dw_die_ref
8047 generate_skeleton (dw_die_ref die)
8049 skeleton_chain_node node;
8051 node.old_die = die;
8052 node.new_die = NULL;
8053 node.parent = NULL;
8055 /* If this type definition is nested inside another type,
8056 and is not an instantiation of a template, always leave
8057 at least a declaration in its place. */
8058 if (die->die_parent != NULL
8059 && is_type_die (die->die_parent)
8060 && !is_template_instantiation (die))
8061 node.new_die = clone_as_declaration (die);
8063 generate_skeleton_bottom_up (&node);
8064 return node.new_die;
8067 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8068 declaration. The original DIE is moved to a new compile unit so that
8069 existing references to it follow it to the new location. If any of the
8070 original DIE's descendants is a declaration, we need to replace the
8071 original DIE with a skeleton tree and move the declarations back into the
8072 skeleton tree. */
8074 static dw_die_ref
8075 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8076 dw_die_ref prev)
8078 dw_die_ref skeleton, orig_parent;
8080 /* Copy the declaration context to the type unit DIE. If the returned
8081 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8082 that DIE. */
8083 orig_parent = copy_declaration_context (unit, child);
8085 skeleton = generate_skeleton (child);
8086 if (skeleton == NULL)
8087 remove_child_with_prev (child, prev);
8088 else
8090 skeleton->comdat_type_p = true;
8091 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8093 /* If the original DIE was a specification, we need to put
8094 the skeleton under the parent DIE of the declaration.
8095 This leaves the original declaration in the tree, but
8096 it will be pruned later since there are no longer any
8097 references to it. */
8098 if (orig_parent != NULL)
8100 remove_child_with_prev (child, prev);
8101 add_child_die (orig_parent, skeleton);
8103 else
8104 replace_child (child, skeleton, prev);
8107 return skeleton;
8110 static void
8111 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8112 comdat_type_node *type_node,
8113 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8115 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8116 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8117 DWARF procedure references in the DW_AT_location attribute. */
8119 static dw_die_ref
8120 copy_dwarf_procedure (dw_die_ref die,
8121 comdat_type_node *type_node,
8122 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8124 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8126 /* DWARF procedures are not supposed to have children... */
8127 gcc_assert (die->die_child == NULL);
8129 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8130 gcc_assert (vec_safe_length (die->die_attr) == 1
8131 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8133 /* Do not copy more than once DWARF procedures. */
8134 bool existed;
8135 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8136 if (existed)
8137 return die_copy;
8139 die_copy = clone_die (die);
8140 add_child_die (type_node->root_die, die_copy);
8141 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8142 return die_copy;
8145 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8146 procedures in DIE's attributes. */
8148 static void
8149 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8150 comdat_type_node *type_node,
8151 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8153 dw_attr_node *a;
8154 unsigned i;
8156 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8158 dw_loc_descr_ref loc;
8160 if (a->dw_attr_val.val_class != dw_val_class_loc)
8161 continue;
8163 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8165 switch (loc->dw_loc_opc)
8167 case DW_OP_call2:
8168 case DW_OP_call4:
8169 case DW_OP_call_ref:
8170 gcc_assert (loc->dw_loc_oprnd1.val_class
8171 == dw_val_class_die_ref);
8172 loc->dw_loc_oprnd1.v.val_die_ref.die
8173 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8174 type_node,
8175 copied_dwarf_procs);
8177 default:
8178 break;
8184 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8185 rewrite references to point to the copies.
8187 References are looked for in DIE's attributes and recursively in all its
8188 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8189 mapping from old DWARF procedures to their copy. It is used not to copy
8190 twice the same DWARF procedure under TYPE_NODE. */
8192 static void
8193 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8194 comdat_type_node *type_node,
8195 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8197 dw_die_ref c;
8199 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8200 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8201 type_node,
8202 copied_dwarf_procs));
8205 /* Traverse the DIE and set up additional .debug_types or .debug_info
8206 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8207 section. */
8209 static void
8210 break_out_comdat_types (dw_die_ref die)
8212 dw_die_ref c;
8213 dw_die_ref first;
8214 dw_die_ref prev = NULL;
8215 dw_die_ref next = NULL;
8216 dw_die_ref unit = NULL;
8218 first = c = die->die_child;
8219 if (c)
8220 next = c->die_sib;
8221 if (c) do {
8222 if (prev == NULL || prev->die_sib == c)
8223 prev = c;
8224 c = next;
8225 next = (c == first ? NULL : c->die_sib);
8226 if (should_move_die_to_comdat (c))
8228 dw_die_ref replacement;
8229 comdat_type_node *type_node;
8231 /* Break out nested types into their own type units. */
8232 break_out_comdat_types (c);
8234 /* Create a new type unit DIE as the root for the new tree, and
8235 add it to the list of comdat types. */
8236 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8237 add_AT_unsigned (unit, DW_AT_language,
8238 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8239 type_node = ggc_cleared_alloc<comdat_type_node> ();
8240 type_node->root_die = unit;
8241 type_node->next = comdat_type_list;
8242 comdat_type_list = type_node;
8244 /* Generate the type signature. */
8245 generate_type_signature (c, type_node);
8247 /* Copy the declaration context, attributes, and children of the
8248 declaration into the new type unit DIE, then remove this DIE
8249 from the main CU (or replace it with a skeleton if necessary). */
8250 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8251 type_node->skeleton_die = replacement;
8253 /* Add the DIE to the new compunit. */
8254 add_child_die (unit, c);
8256 /* Types can reference DWARF procedures for type size or data location
8257 expressions. Calls in DWARF expressions cannot target procedures
8258 that are not in the same section. So we must copy DWARF procedures
8259 along with this type and then rewrite references to them. */
8260 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8261 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8263 if (replacement != NULL)
8264 c = replacement;
8266 else if (c->die_tag == DW_TAG_namespace
8267 || c->die_tag == DW_TAG_class_type
8268 || c->die_tag == DW_TAG_structure_type
8269 || c->die_tag == DW_TAG_union_type)
8271 /* Look for nested types that can be broken out. */
8272 break_out_comdat_types (c);
8274 } while (next != NULL);
8277 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8278 Enter all the cloned children into the hash table decl_table. */
8280 static dw_die_ref
8281 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8283 dw_die_ref c;
8284 dw_die_ref clone;
8285 struct decl_table_entry *entry;
8286 decl_table_entry **slot;
8288 if (die->die_tag == DW_TAG_subprogram)
8289 clone = clone_as_declaration (die);
8290 else
8291 clone = clone_die (die);
8293 slot = decl_table->find_slot_with_hash (die,
8294 htab_hash_pointer (die), INSERT);
8296 /* Assert that DIE isn't in the hash table yet. If it would be there
8297 before, the ancestors would be necessarily there as well, therefore
8298 clone_tree_partial wouldn't be called. */
8299 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8301 entry = XCNEW (struct decl_table_entry);
8302 entry->orig = die;
8303 entry->copy = clone;
8304 *slot = entry;
8306 if (die->die_tag != DW_TAG_subprogram)
8307 FOR_EACH_CHILD (die, c,
8308 add_child_die (clone, clone_tree_partial (c, decl_table)));
8310 return clone;
8313 /* Walk the DIE and its children, looking for references to incomplete
8314 or trivial types that are unmarked (i.e., that are not in the current
8315 type_unit). */
8317 static void
8318 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8320 dw_die_ref c;
8321 dw_attr_node *a;
8322 unsigned ix;
8324 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8326 if (AT_class (a) == dw_val_class_die_ref)
8328 dw_die_ref targ = AT_ref (a);
8329 decl_table_entry **slot;
8330 struct decl_table_entry *entry;
8332 if (targ->die_mark != 0 || targ->comdat_type_p)
8333 continue;
8335 slot = decl_table->find_slot_with_hash (targ,
8336 htab_hash_pointer (targ),
8337 INSERT);
8339 if (*slot != HTAB_EMPTY_ENTRY)
8341 /* TARG has already been copied, so we just need to
8342 modify the reference to point to the copy. */
8343 entry = *slot;
8344 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8346 else
8348 dw_die_ref parent = unit;
8349 dw_die_ref copy = clone_die (targ);
8351 /* Record in DECL_TABLE that TARG has been copied.
8352 Need to do this now, before the recursive call,
8353 because DECL_TABLE may be expanded and SLOT
8354 would no longer be a valid pointer. */
8355 entry = XCNEW (struct decl_table_entry);
8356 entry->orig = targ;
8357 entry->copy = copy;
8358 *slot = entry;
8360 /* If TARG is not a declaration DIE, we need to copy its
8361 children. */
8362 if (!is_declaration_die (targ))
8364 FOR_EACH_CHILD (
8365 targ, c,
8366 add_child_die (copy,
8367 clone_tree_partial (c, decl_table)));
8370 /* Make sure the cloned tree is marked as part of the
8371 type unit. */
8372 mark_dies (copy);
8374 /* If TARG has surrounding context, copy its ancestor tree
8375 into the new type unit. */
8376 if (targ->die_parent != NULL
8377 && !is_unit_die (targ->die_parent))
8378 parent = copy_ancestor_tree (unit, targ->die_parent,
8379 decl_table);
8381 add_child_die (parent, copy);
8382 a->dw_attr_val.v.val_die_ref.die = copy;
8384 /* Make sure the newly-copied DIE is walked. If it was
8385 installed in a previously-added context, it won't
8386 get visited otherwise. */
8387 if (parent != unit)
8389 /* Find the highest point of the newly-added tree,
8390 mark each node along the way, and walk from there. */
8391 parent->die_mark = 1;
8392 while (parent->die_parent
8393 && parent->die_parent->die_mark == 0)
8395 parent = parent->die_parent;
8396 parent->die_mark = 1;
8398 copy_decls_walk (unit, parent, decl_table);
8404 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8407 /* Copy declarations for "unworthy" types into the new comdat section.
8408 Incomplete types, modified types, and certain other types aren't broken
8409 out into comdat sections of their own, so they don't have a signature,
8410 and we need to copy the declaration into the same section so that we
8411 don't have an external reference. */
8413 static void
8414 copy_decls_for_unworthy_types (dw_die_ref unit)
8416 mark_dies (unit);
8417 decl_hash_type decl_table (10);
8418 copy_decls_walk (unit, unit, &decl_table);
8419 unmark_dies (unit);
8422 /* Traverse the DIE and add a sibling attribute if it may have the
8423 effect of speeding up access to siblings. To save some space,
8424 avoid generating sibling attributes for DIE's without children. */
8426 static void
8427 add_sibling_attributes (dw_die_ref die)
8429 dw_die_ref c;
8431 if (! die->die_child)
8432 return;
8434 if (die->die_parent && die != die->die_parent->die_child)
8435 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8437 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8440 /* Output all location lists for the DIE and its children. */
8442 static void
8443 output_location_lists (dw_die_ref die)
8445 dw_die_ref c;
8446 dw_attr_node *a;
8447 unsigned ix;
8449 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8450 if (AT_class (a) == dw_val_class_loc_list)
8451 output_loc_list (AT_loc_list (a));
8453 FOR_EACH_CHILD (die, c, output_location_lists (c));
8456 /* During assign_location_list_indexes and output_loclists_offset the
8457 current index, after it the number of assigned indexes (i.e. how
8458 large the .debug_loclists* offset table should be). */
8459 static unsigned int loc_list_idx;
8461 /* Output all location list offsets for the DIE and its children. */
8463 static void
8464 output_loclists_offsets (dw_die_ref die)
8466 dw_die_ref c;
8467 dw_attr_node *a;
8468 unsigned ix;
8470 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8471 if (AT_class (a) == dw_val_class_loc_list)
8473 dw_loc_list_ref l = AT_loc_list (a);
8474 if (l->offset_emitted)
8475 continue;
8476 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8477 loc_section_label, NULL);
8478 gcc_assert (l->hash == loc_list_idx);
8479 loc_list_idx++;
8480 l->offset_emitted = true;
8483 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8486 /* Recursively set indexes of location lists. */
8488 static void
8489 assign_location_list_indexes (dw_die_ref die)
8491 dw_die_ref c;
8492 dw_attr_node *a;
8493 unsigned ix;
8495 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8496 if (AT_class (a) == dw_val_class_loc_list)
8498 dw_loc_list_ref list = AT_loc_list (a);
8499 if (!list->num_assigned)
8501 list->num_assigned = true;
8502 list->hash = loc_list_idx++;
8506 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8509 /* We want to limit the number of external references, because they are
8510 larger than local references: a relocation takes multiple words, and
8511 even a sig8 reference is always eight bytes, whereas a local reference
8512 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8513 So if we encounter multiple external references to the same type DIE, we
8514 make a local typedef stub for it and redirect all references there.
8516 This is the element of the hash table for keeping track of these
8517 references. */
8519 struct external_ref
8521 dw_die_ref type;
8522 dw_die_ref stub;
8523 unsigned n_refs;
8526 /* Hashtable helpers. */
8528 struct external_ref_hasher : free_ptr_hash <external_ref>
8530 static inline hashval_t hash (const external_ref *);
8531 static inline bool equal (const external_ref *, const external_ref *);
8534 inline hashval_t
8535 external_ref_hasher::hash (const external_ref *r)
8537 dw_die_ref die = r->type;
8538 hashval_t h = 0;
8540 /* We can't use the address of the DIE for hashing, because
8541 that will make the order of the stub DIEs non-deterministic. */
8542 if (! die->comdat_type_p)
8543 /* We have a symbol; use it to compute a hash. */
8544 h = htab_hash_string (die->die_id.die_symbol);
8545 else
8547 /* We have a type signature; use a subset of the bits as the hash.
8548 The 8-byte signature is at least as large as hashval_t. */
8549 comdat_type_node *type_node = die->die_id.die_type_node;
8550 memcpy (&h, type_node->signature, sizeof (h));
8552 return h;
8555 inline bool
8556 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8558 return r1->type == r2->type;
8561 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8563 /* Return a pointer to the external_ref for references to DIE. */
8565 static struct external_ref *
8566 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8568 struct external_ref ref, *ref_p;
8569 external_ref **slot;
8571 ref.type = die;
8572 slot = map->find_slot (&ref, INSERT);
8573 if (*slot != HTAB_EMPTY_ENTRY)
8574 return *slot;
8576 ref_p = XCNEW (struct external_ref);
8577 ref_p->type = die;
8578 *slot = ref_p;
8579 return ref_p;
8582 /* Subroutine of optimize_external_refs, below.
8584 If we see a type skeleton, record it as our stub. If we see external
8585 references, remember how many we've seen. */
8587 static void
8588 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8590 dw_die_ref c;
8591 dw_attr_node *a;
8592 unsigned ix;
8593 struct external_ref *ref_p;
8595 if (is_type_die (die)
8596 && (c = get_AT_ref (die, DW_AT_signature)))
8598 /* This is a local skeleton; use it for local references. */
8599 ref_p = lookup_external_ref (map, c);
8600 ref_p->stub = die;
8603 /* Scan the DIE references, and remember any that refer to DIEs from
8604 other CUs (i.e. those which are not marked). */
8605 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8606 if (AT_class (a) == dw_val_class_die_ref
8607 && (c = AT_ref (a))->die_mark == 0
8608 && is_type_die (c))
8610 ref_p = lookup_external_ref (map, c);
8611 ref_p->n_refs++;
8614 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8617 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8618 points to an external_ref, DATA is the CU we're processing. If we don't
8619 already have a local stub, and we have multiple refs, build a stub. */
8622 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8624 struct external_ref *ref_p = *slot;
8626 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8628 /* We have multiple references to this type, so build a small stub.
8629 Both of these forms are a bit dodgy from the perspective of the
8630 DWARF standard, since technically they should have names. */
8631 dw_die_ref cu = data;
8632 dw_die_ref type = ref_p->type;
8633 dw_die_ref stub = NULL;
8635 if (type->comdat_type_p)
8637 /* If we refer to this type via sig8, use AT_signature. */
8638 stub = new_die (type->die_tag, cu, NULL_TREE);
8639 add_AT_die_ref (stub, DW_AT_signature, type);
8641 else
8643 /* Otherwise, use a typedef with no name. */
8644 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8645 add_AT_die_ref (stub, DW_AT_type, type);
8648 stub->die_mark++;
8649 ref_p->stub = stub;
8651 return 1;
8654 /* DIE is a unit; look through all the DIE references to see if there are
8655 any external references to types, and if so, create local stubs for
8656 them which will be applied in build_abbrev_table. This is useful because
8657 references to local DIEs are smaller. */
8659 static external_ref_hash_type *
8660 optimize_external_refs (dw_die_ref die)
8662 external_ref_hash_type *map = new external_ref_hash_type (10);
8663 optimize_external_refs_1 (die, map);
8664 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8665 return map;
8668 /* The following 3 variables are temporaries that are computed only during the
8669 build_abbrev_table call and used and released during the following
8670 optimize_abbrev_table call. */
8672 /* First abbrev_id that can be optimized based on usage. */
8673 static unsigned int abbrev_opt_start;
8675 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8676 abbrev_id smaller than this, because they must be already sized
8677 during build_abbrev_table). */
8678 static unsigned int abbrev_opt_base_type_end;
8680 /* Vector of usage counts during build_abbrev_table. Indexed by
8681 abbrev_id - abbrev_opt_start. */
8682 static vec<unsigned int> abbrev_usage_count;
8684 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8685 static vec<dw_die_ref> sorted_abbrev_dies;
8687 /* The format of each DIE (and its attribute value pairs) is encoded in an
8688 abbreviation table. This routine builds the abbreviation table and assigns
8689 a unique abbreviation id for each abbreviation entry. The children of each
8690 die are visited recursively. */
8692 static void
8693 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8695 unsigned int abbrev_id = 0;
8696 dw_die_ref c;
8697 dw_attr_node *a;
8698 unsigned ix;
8699 dw_die_ref abbrev;
8701 /* Scan the DIE references, and replace any that refer to
8702 DIEs from other CUs (i.e. those which are not marked) with
8703 the local stubs we built in optimize_external_refs. */
8704 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8705 if (AT_class (a) == dw_val_class_die_ref
8706 && (c = AT_ref (a))->die_mark == 0)
8708 struct external_ref *ref_p;
8709 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8711 ref_p = lookup_external_ref (extern_map, c);
8712 if (ref_p->stub && ref_p->stub != die)
8713 change_AT_die_ref (a, ref_p->stub);
8714 else
8715 /* We aren't changing this reference, so mark it external. */
8716 set_AT_ref_external (a, 1);
8719 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8721 dw_attr_node *die_a, *abbrev_a;
8722 unsigned ix;
8723 bool ok = true;
8725 if (abbrev_id == 0)
8726 continue;
8727 if (abbrev->die_tag != die->die_tag)
8728 continue;
8729 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8730 continue;
8732 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8733 continue;
8735 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8737 abbrev_a = &(*abbrev->die_attr)[ix];
8738 if ((abbrev_a->dw_attr != die_a->dw_attr)
8739 || (value_format (abbrev_a) != value_format (die_a)))
8741 ok = false;
8742 break;
8745 if (ok)
8746 break;
8749 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8751 vec_safe_push (abbrev_die_table, die);
8752 if (abbrev_opt_start)
8753 abbrev_usage_count.safe_push (0);
8755 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8757 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8758 sorted_abbrev_dies.safe_push (die);
8761 die->die_abbrev = abbrev_id;
8762 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8765 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8766 by die_abbrev's usage count, from the most commonly used
8767 abbreviation to the least. */
8769 static int
8770 die_abbrev_cmp (const void *p1, const void *p2)
8772 dw_die_ref die1 = *(const dw_die_ref *) p1;
8773 dw_die_ref die2 = *(const dw_die_ref *) p2;
8775 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8776 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8778 if (die1->die_abbrev >= abbrev_opt_base_type_end
8779 && die2->die_abbrev >= abbrev_opt_base_type_end)
8781 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8782 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8783 return -1;
8784 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8785 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8786 return 1;
8789 /* Stabilize the sort. */
8790 if (die1->die_abbrev < die2->die_abbrev)
8791 return -1;
8792 if (die1->die_abbrev > die2->die_abbrev)
8793 return 1;
8795 return 0;
8798 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8799 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8800 into dw_val_class_const_implicit or
8801 dw_val_class_unsigned_const_implicit. */
8803 static void
8804 optimize_implicit_const (unsigned int first_id, unsigned int end,
8805 vec<bool> &implicit_consts)
8807 /* It never makes sense if there is just one DIE using the abbreviation. */
8808 if (end < first_id + 2)
8809 return;
8811 dw_attr_node *a;
8812 unsigned ix, i;
8813 dw_die_ref die = sorted_abbrev_dies[first_id];
8814 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8815 if (implicit_consts[ix])
8817 enum dw_val_class new_class = dw_val_class_none;
8818 switch (AT_class (a))
8820 case dw_val_class_unsigned_const:
8821 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8822 continue;
8824 /* The .debug_abbrev section will grow by
8825 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8826 in all the DIEs using that abbreviation. */
8827 if (constant_size (AT_unsigned (a)) * (end - first_id)
8828 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8829 continue;
8831 new_class = dw_val_class_unsigned_const_implicit;
8832 break;
8834 case dw_val_class_const:
8835 new_class = dw_val_class_const_implicit;
8836 break;
8838 case dw_val_class_file:
8839 new_class = dw_val_class_file_implicit;
8840 break;
8842 default:
8843 continue;
8845 for (i = first_id; i < end; i++)
8846 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8847 = new_class;
8851 /* Attempt to optimize abbreviation table from abbrev_opt_start
8852 abbreviation above. */
8854 static void
8855 optimize_abbrev_table (void)
8857 if (abbrev_opt_start
8858 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8859 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8861 auto_vec<bool, 32> implicit_consts;
8862 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8864 unsigned int abbrev_id = abbrev_opt_start - 1;
8865 unsigned int first_id = ~0U;
8866 unsigned int last_abbrev_id = 0;
8867 unsigned int i;
8868 dw_die_ref die;
8869 if (abbrev_opt_base_type_end > abbrev_opt_start)
8870 abbrev_id = abbrev_opt_base_type_end - 1;
8871 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8872 most commonly used abbreviations come first. */
8873 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8875 dw_attr_node *a;
8876 unsigned ix;
8878 /* If calc_base_type_die_sizes has been called, the CU and
8879 base types after it can't be optimized, because we've already
8880 calculated their DIE offsets. We've sorted them first. */
8881 if (die->die_abbrev < abbrev_opt_base_type_end)
8882 continue;
8883 if (die->die_abbrev != last_abbrev_id)
8885 last_abbrev_id = die->die_abbrev;
8886 if (dwarf_version >= 5 && first_id != ~0U)
8887 optimize_implicit_const (first_id, i, implicit_consts);
8888 abbrev_id++;
8889 (*abbrev_die_table)[abbrev_id] = die;
8890 if (dwarf_version >= 5)
8892 first_id = i;
8893 implicit_consts.truncate (0);
8895 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8896 switch (AT_class (a))
8898 case dw_val_class_const:
8899 case dw_val_class_unsigned_const:
8900 case dw_val_class_file:
8901 implicit_consts.safe_push (true);
8902 break;
8903 default:
8904 implicit_consts.safe_push (false);
8905 break;
8909 else if (dwarf_version >= 5)
8911 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8912 if (!implicit_consts[ix])
8913 continue;
8914 else
8916 dw_attr_node *other_a
8917 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8918 if (!dw_val_equal_p (&a->dw_attr_val,
8919 &other_a->dw_attr_val))
8920 implicit_consts[ix] = false;
8923 die->die_abbrev = abbrev_id;
8925 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8926 if (dwarf_version >= 5 && first_id != ~0U)
8927 optimize_implicit_const (first_id, i, implicit_consts);
8930 abbrev_opt_start = 0;
8931 abbrev_opt_base_type_end = 0;
8932 abbrev_usage_count.release ();
8933 sorted_abbrev_dies.release ();
8936 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8938 static int
8939 constant_size (unsigned HOST_WIDE_INT value)
8941 int log;
8943 if (value == 0)
8944 log = 0;
8945 else
8946 log = floor_log2 (value);
8948 log = log / 8;
8949 log = 1 << (floor_log2 (log) + 1);
8951 return log;
8954 /* Return the size of a DIE as it is represented in the
8955 .debug_info section. */
8957 static unsigned long
8958 size_of_die (dw_die_ref die)
8960 unsigned long size = 0;
8961 dw_attr_node *a;
8962 unsigned ix;
8963 enum dwarf_form form;
8965 size += size_of_uleb128 (die->die_abbrev);
8966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8968 switch (AT_class (a))
8970 case dw_val_class_addr:
8971 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8973 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8974 size += size_of_uleb128 (AT_index (a));
8976 else
8977 size += DWARF2_ADDR_SIZE;
8978 break;
8979 case dw_val_class_offset:
8980 size += DWARF_OFFSET_SIZE;
8981 break;
8982 case dw_val_class_loc:
8984 unsigned long lsize = size_of_locs (AT_loc (a));
8986 /* Block length. */
8987 if (dwarf_version >= 4)
8988 size += size_of_uleb128 (lsize);
8989 else
8990 size += constant_size (lsize);
8991 size += lsize;
8993 break;
8994 case dw_val_class_loc_list:
8995 if (dwarf_split_debug_info && dwarf_version >= 5)
8997 gcc_assert (AT_loc_list (a)->num_assigned);
8998 size += size_of_uleb128 (AT_loc_list (a)->hash);
9000 else
9001 size += DWARF_OFFSET_SIZE;
9002 break;
9003 case dw_val_class_range_list:
9004 if (value_format (a) == DW_FORM_rnglistx)
9006 gcc_assert (rnglist_idx);
9007 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9008 size += size_of_uleb128 (r->idx);
9010 else
9011 size += DWARF_OFFSET_SIZE;
9012 break;
9013 case dw_val_class_const:
9014 size += size_of_sleb128 (AT_int (a));
9015 break;
9016 case dw_val_class_unsigned_const:
9018 int csize = constant_size (AT_unsigned (a));
9019 if (dwarf_version == 3
9020 && a->dw_attr == DW_AT_data_member_location
9021 && csize >= 4)
9022 size += size_of_uleb128 (AT_unsigned (a));
9023 else
9024 size += csize;
9026 break;
9027 case dw_val_class_const_implicit:
9028 case dw_val_class_unsigned_const_implicit:
9029 case dw_val_class_file_implicit:
9030 /* These occupy no size in the DIE, just an extra sleb128 in
9031 .debug_abbrev. */
9032 break;
9033 case dw_val_class_const_double:
9034 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9035 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9036 size++; /* block */
9037 break;
9038 case dw_val_class_wide_int:
9039 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9040 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9041 if (get_full_len (*a->dw_attr_val.v.val_wide)
9042 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9043 size++; /* block */
9044 break;
9045 case dw_val_class_vec:
9046 size += constant_size (a->dw_attr_val.v.val_vec.length
9047 * a->dw_attr_val.v.val_vec.elt_size)
9048 + a->dw_attr_val.v.val_vec.length
9049 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9050 break;
9051 case dw_val_class_flag:
9052 if (dwarf_version >= 4)
9053 /* Currently all add_AT_flag calls pass in 1 as last argument,
9054 so DW_FORM_flag_present can be used. If that ever changes,
9055 we'll need to use DW_FORM_flag and have some optimization
9056 in build_abbrev_table that will change those to
9057 DW_FORM_flag_present if it is set to 1 in all DIEs using
9058 the same abbrev entry. */
9059 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9060 else
9061 size += 1;
9062 break;
9063 case dw_val_class_die_ref:
9064 if (AT_ref_external (a))
9066 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9067 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9068 is sized by target address length, whereas in DWARF3
9069 it's always sized as an offset. */
9070 if (use_debug_types)
9071 size += DWARF_TYPE_SIGNATURE_SIZE;
9072 else if (dwarf_version == 2)
9073 size += DWARF2_ADDR_SIZE;
9074 else
9075 size += DWARF_OFFSET_SIZE;
9077 else
9078 size += DWARF_OFFSET_SIZE;
9079 break;
9080 case dw_val_class_fde_ref:
9081 size += DWARF_OFFSET_SIZE;
9082 break;
9083 case dw_val_class_lbl_id:
9084 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9086 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9087 size += size_of_uleb128 (AT_index (a));
9089 else
9090 size += DWARF2_ADDR_SIZE;
9091 break;
9092 case dw_val_class_lineptr:
9093 case dw_val_class_macptr:
9094 case dw_val_class_loclistsptr:
9095 size += DWARF_OFFSET_SIZE;
9096 break;
9097 case dw_val_class_str:
9098 form = AT_string_form (a);
9099 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9100 size += DWARF_OFFSET_SIZE;
9101 else if (form == DW_FORM_GNU_str_index)
9102 size += size_of_uleb128 (AT_index (a));
9103 else
9104 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9105 break;
9106 case dw_val_class_file:
9107 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9108 break;
9109 case dw_val_class_data8:
9110 size += 8;
9111 break;
9112 case dw_val_class_vms_delta:
9113 size += DWARF_OFFSET_SIZE;
9114 break;
9115 case dw_val_class_high_pc:
9116 size += DWARF2_ADDR_SIZE;
9117 break;
9118 case dw_val_class_discr_value:
9119 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9120 break;
9121 case dw_val_class_discr_list:
9123 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9125 /* This is a block, so we have the block length and then its
9126 data. */
9127 size += constant_size (block_size) + block_size;
9129 break;
9130 default:
9131 gcc_unreachable ();
9135 return size;
9138 /* Size the debugging information associated with a given DIE. Visits the
9139 DIE's children recursively. Updates the global variable next_die_offset, on
9140 each time through. Uses the current value of next_die_offset to update the
9141 die_offset field in each DIE. */
9143 static void
9144 calc_die_sizes (dw_die_ref die)
9146 dw_die_ref c;
9148 gcc_assert (die->die_offset == 0
9149 || (unsigned long int) die->die_offset == next_die_offset);
9150 die->die_offset = next_die_offset;
9151 next_die_offset += size_of_die (die);
9153 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9155 if (die->die_child != NULL)
9156 /* Count the null byte used to terminate sibling lists. */
9157 next_die_offset += 1;
9160 /* Size just the base type children at the start of the CU.
9161 This is needed because build_abbrev needs to size locs
9162 and sizing of type based stack ops needs to know die_offset
9163 values for the base types. */
9165 static void
9166 calc_base_type_die_sizes (void)
9168 unsigned long die_offset = (dwarf_split_debug_info
9169 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9170 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9171 unsigned int i;
9172 dw_die_ref base_type;
9173 #if ENABLE_ASSERT_CHECKING
9174 dw_die_ref prev = comp_unit_die ()->die_child;
9175 #endif
9177 die_offset += size_of_die (comp_unit_die ());
9178 for (i = 0; base_types.iterate (i, &base_type); i++)
9180 #if ENABLE_ASSERT_CHECKING
9181 gcc_assert (base_type->die_offset == 0
9182 && prev->die_sib == base_type
9183 && base_type->die_child == NULL
9184 && base_type->die_abbrev);
9185 prev = base_type;
9186 #endif
9187 if (abbrev_opt_start
9188 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9189 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9190 base_type->die_offset = die_offset;
9191 die_offset += size_of_die (base_type);
9195 /* Set the marks for a die and its children. We do this so
9196 that we know whether or not a reference needs to use FORM_ref_addr; only
9197 DIEs in the same CU will be marked. We used to clear out the offset
9198 and use that as the flag, but ran into ordering problems. */
9200 static void
9201 mark_dies (dw_die_ref die)
9203 dw_die_ref c;
9205 gcc_assert (!die->die_mark);
9207 die->die_mark = 1;
9208 FOR_EACH_CHILD (die, c, mark_dies (c));
9211 /* Clear the marks for a die and its children. */
9213 static void
9214 unmark_dies (dw_die_ref die)
9216 dw_die_ref c;
9218 if (! use_debug_types)
9219 gcc_assert (die->die_mark);
9221 die->die_mark = 0;
9222 FOR_EACH_CHILD (die, c, unmark_dies (c));
9225 /* Clear the marks for a die, its children and referred dies. */
9227 static void
9228 unmark_all_dies (dw_die_ref die)
9230 dw_die_ref c;
9231 dw_attr_node *a;
9232 unsigned ix;
9234 if (!die->die_mark)
9235 return;
9236 die->die_mark = 0;
9238 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9240 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9241 if (AT_class (a) == dw_val_class_die_ref)
9242 unmark_all_dies (AT_ref (a));
9245 /* Calculate if the entry should appear in the final output file. It may be
9246 from a pruned a type. */
9248 static bool
9249 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9251 /* By limiting gnu pubnames to definitions only, gold can generate a
9252 gdb index without entries for declarations, which don't include
9253 enough information to be useful. */
9254 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9255 return false;
9257 if (table == pubname_table)
9259 /* Enumerator names are part of the pubname table, but the
9260 parent DW_TAG_enumeration_type die may have been pruned.
9261 Don't output them if that is the case. */
9262 if (p->die->die_tag == DW_TAG_enumerator &&
9263 (p->die->die_parent == NULL
9264 || !p->die->die_parent->die_perennial_p))
9265 return false;
9267 /* Everything else in the pubname table is included. */
9268 return true;
9271 /* The pubtypes table shouldn't include types that have been
9272 pruned. */
9273 return (p->die->die_offset != 0
9274 || !flag_eliminate_unused_debug_types);
9277 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9278 generated for the compilation unit. */
9280 static unsigned long
9281 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9283 unsigned long size;
9284 unsigned i;
9285 pubname_entry *p;
9286 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9288 size = DWARF_PUBNAMES_HEADER_SIZE;
9289 FOR_EACH_VEC_ELT (*names, i, p)
9290 if (include_pubname_in_output (names, p))
9291 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9293 size += DWARF_OFFSET_SIZE;
9294 return size;
9297 /* Return the size of the information in the .debug_aranges section. */
9299 static unsigned long
9300 size_of_aranges (void)
9302 unsigned long size;
9304 size = DWARF_ARANGES_HEADER_SIZE;
9306 /* Count the address/length pair for this compilation unit. */
9307 if (text_section_used)
9308 size += 2 * DWARF2_ADDR_SIZE;
9309 if (cold_text_section_used)
9310 size += 2 * DWARF2_ADDR_SIZE;
9311 if (have_multiple_function_sections)
9313 unsigned fde_idx;
9314 dw_fde_ref fde;
9316 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9318 if (DECL_IGNORED_P (fde->decl))
9319 continue;
9320 if (!fde->in_std_section)
9321 size += 2 * DWARF2_ADDR_SIZE;
9322 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9323 size += 2 * DWARF2_ADDR_SIZE;
9327 /* Count the two zero words used to terminated the address range table. */
9328 size += 2 * DWARF2_ADDR_SIZE;
9329 return size;
9332 /* Select the encoding of an attribute value. */
9334 static enum dwarf_form
9335 value_format (dw_attr_node *a)
9337 switch (AT_class (a))
9339 case dw_val_class_addr:
9340 /* Only very few attributes allow DW_FORM_addr. */
9341 switch (a->dw_attr)
9343 case DW_AT_low_pc:
9344 case DW_AT_high_pc:
9345 case DW_AT_entry_pc:
9346 case DW_AT_trampoline:
9347 return (AT_index (a) == NOT_INDEXED
9348 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9349 default:
9350 break;
9352 switch (DWARF2_ADDR_SIZE)
9354 case 1:
9355 return DW_FORM_data1;
9356 case 2:
9357 return DW_FORM_data2;
9358 case 4:
9359 return DW_FORM_data4;
9360 case 8:
9361 return DW_FORM_data8;
9362 default:
9363 gcc_unreachable ();
9365 case dw_val_class_loc_list:
9366 if (dwarf_split_debug_info
9367 && dwarf_version >= 5
9368 && AT_loc_list (a)->num_assigned)
9369 return DW_FORM_loclistx;
9370 /* FALLTHRU */
9371 case dw_val_class_range_list:
9372 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9373 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9374 care about sizes of .debug* sections in shared libraries and
9375 executables and don't take into account relocations that affect just
9376 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9377 table in the .debug_rnglists section. */
9378 if (dwarf_split_debug_info
9379 && dwarf_version >= 5
9380 && AT_class (a) == dw_val_class_range_list
9381 && rnglist_idx
9382 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9383 return DW_FORM_rnglistx;
9384 if (dwarf_version >= 4)
9385 return DW_FORM_sec_offset;
9386 /* FALLTHRU */
9387 case dw_val_class_vms_delta:
9388 case dw_val_class_offset:
9389 switch (DWARF_OFFSET_SIZE)
9391 case 4:
9392 return DW_FORM_data4;
9393 case 8:
9394 return DW_FORM_data8;
9395 default:
9396 gcc_unreachable ();
9398 case dw_val_class_loc:
9399 if (dwarf_version >= 4)
9400 return DW_FORM_exprloc;
9401 switch (constant_size (size_of_locs (AT_loc (a))))
9403 case 1:
9404 return DW_FORM_block1;
9405 case 2:
9406 return DW_FORM_block2;
9407 case 4:
9408 return DW_FORM_block4;
9409 default:
9410 gcc_unreachable ();
9412 case dw_val_class_const:
9413 return DW_FORM_sdata;
9414 case dw_val_class_unsigned_const:
9415 switch (constant_size (AT_unsigned (a)))
9417 case 1:
9418 return DW_FORM_data1;
9419 case 2:
9420 return DW_FORM_data2;
9421 case 4:
9422 /* In DWARF3 DW_AT_data_member_location with
9423 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9424 constant, so we need to use DW_FORM_udata if we need
9425 a large constant. */
9426 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9427 return DW_FORM_udata;
9428 return DW_FORM_data4;
9429 case 8:
9430 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9431 return DW_FORM_udata;
9432 return DW_FORM_data8;
9433 default:
9434 gcc_unreachable ();
9436 case dw_val_class_const_implicit:
9437 case dw_val_class_unsigned_const_implicit:
9438 case dw_val_class_file_implicit:
9439 return DW_FORM_implicit_const;
9440 case dw_val_class_const_double:
9441 switch (HOST_BITS_PER_WIDE_INT)
9443 case 8:
9444 return DW_FORM_data2;
9445 case 16:
9446 return DW_FORM_data4;
9447 case 32:
9448 return DW_FORM_data8;
9449 case 64:
9450 if (dwarf_version >= 5)
9451 return DW_FORM_data16;
9452 /* FALLTHRU */
9453 default:
9454 return DW_FORM_block1;
9456 case dw_val_class_wide_int:
9457 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9459 case 8:
9460 return DW_FORM_data1;
9461 case 16:
9462 return DW_FORM_data2;
9463 case 32:
9464 return DW_FORM_data4;
9465 case 64:
9466 return DW_FORM_data8;
9467 case 128:
9468 if (dwarf_version >= 5)
9469 return DW_FORM_data16;
9470 /* FALLTHRU */
9471 default:
9472 return DW_FORM_block1;
9474 case dw_val_class_vec:
9475 switch (constant_size (a->dw_attr_val.v.val_vec.length
9476 * a->dw_attr_val.v.val_vec.elt_size))
9478 case 1:
9479 return DW_FORM_block1;
9480 case 2:
9481 return DW_FORM_block2;
9482 case 4:
9483 return DW_FORM_block4;
9484 default:
9485 gcc_unreachable ();
9487 case dw_val_class_flag:
9488 if (dwarf_version >= 4)
9490 /* Currently all add_AT_flag calls pass in 1 as last argument,
9491 so DW_FORM_flag_present can be used. If that ever changes,
9492 we'll need to use DW_FORM_flag and have some optimization
9493 in build_abbrev_table that will change those to
9494 DW_FORM_flag_present if it is set to 1 in all DIEs using
9495 the same abbrev entry. */
9496 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9497 return DW_FORM_flag_present;
9499 return DW_FORM_flag;
9500 case dw_val_class_die_ref:
9501 if (AT_ref_external (a))
9502 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9503 else
9504 return DW_FORM_ref;
9505 case dw_val_class_fde_ref:
9506 return DW_FORM_data;
9507 case dw_val_class_lbl_id:
9508 return (AT_index (a) == NOT_INDEXED
9509 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9510 case dw_val_class_lineptr:
9511 case dw_val_class_macptr:
9512 case dw_val_class_loclistsptr:
9513 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9514 case dw_val_class_str:
9515 return AT_string_form (a);
9516 case dw_val_class_file:
9517 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9519 case 1:
9520 return DW_FORM_data1;
9521 case 2:
9522 return DW_FORM_data2;
9523 case 4:
9524 return DW_FORM_data4;
9525 default:
9526 gcc_unreachable ();
9529 case dw_val_class_data8:
9530 return DW_FORM_data8;
9532 case dw_val_class_high_pc:
9533 switch (DWARF2_ADDR_SIZE)
9535 case 1:
9536 return DW_FORM_data1;
9537 case 2:
9538 return DW_FORM_data2;
9539 case 4:
9540 return DW_FORM_data4;
9541 case 8:
9542 return DW_FORM_data8;
9543 default:
9544 gcc_unreachable ();
9547 case dw_val_class_discr_value:
9548 return (a->dw_attr_val.v.val_discr_value.pos
9549 ? DW_FORM_udata
9550 : DW_FORM_sdata);
9551 case dw_val_class_discr_list:
9552 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9554 case 1:
9555 return DW_FORM_block1;
9556 case 2:
9557 return DW_FORM_block2;
9558 case 4:
9559 return DW_FORM_block4;
9560 default:
9561 gcc_unreachable ();
9564 default:
9565 gcc_unreachable ();
9569 /* Output the encoding of an attribute value. */
9571 static void
9572 output_value_format (dw_attr_node *a)
9574 enum dwarf_form form = value_format (a);
9576 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9579 /* Given a die and id, produce the appropriate abbreviations. */
9581 static void
9582 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9584 unsigned ix;
9585 dw_attr_node *a_attr;
9587 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9588 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9589 dwarf_tag_name (abbrev->die_tag));
9591 if (abbrev->die_child != NULL)
9592 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9593 else
9594 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9596 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9598 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9599 dwarf_attr_name (a_attr->dw_attr));
9600 output_value_format (a_attr);
9601 if (value_format (a_attr) == DW_FORM_implicit_const)
9603 if (AT_class (a_attr) == dw_val_class_file_implicit)
9605 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9606 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9607 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9609 else
9610 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9614 dw2_asm_output_data (1, 0, NULL);
9615 dw2_asm_output_data (1, 0, NULL);
9619 /* Output the .debug_abbrev section which defines the DIE abbreviation
9620 table. */
9622 static void
9623 output_abbrev_section (void)
9625 unsigned int abbrev_id;
9626 dw_die_ref abbrev;
9628 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9629 if (abbrev_id != 0)
9630 output_die_abbrevs (abbrev_id, abbrev);
9632 /* Terminate the table. */
9633 dw2_asm_output_data (1, 0, NULL);
9636 /* Return a new location list, given the begin and end range, and the
9637 expression. */
9639 static inline dw_loc_list_ref
9640 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9641 const char *section)
9643 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9645 retlist->begin = begin;
9646 retlist->begin_entry = NULL;
9647 retlist->end = end;
9648 retlist->expr = expr;
9649 retlist->section = section;
9651 return retlist;
9654 /* Generate a new internal symbol for this location list node, if it
9655 hasn't got one yet. */
9657 static inline void
9658 gen_llsym (dw_loc_list_ref list)
9660 gcc_assert (!list->ll_symbol);
9661 list->ll_symbol = gen_internal_sym ("LLST");
9664 /* Output the location list given to us. */
9666 static void
9667 output_loc_list (dw_loc_list_ref list_head)
9669 if (list_head->emitted)
9670 return;
9671 list_head->emitted = true;
9673 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9675 dw_loc_list_ref curr = list_head;
9676 const char *last_section = NULL;
9677 const char *base_label = NULL;
9679 /* Walk the location list, and output each range + expression. */
9680 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9682 unsigned long size;
9683 /* Don't output an entry that starts and ends at the same address. */
9684 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9685 continue;
9686 size = size_of_locs (curr->expr);
9687 /* If the expression is too large, drop it on the floor. We could
9688 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9689 in the expression, but >= 64KB expressions for a single value
9690 in a single range are unlikely very useful. */
9691 if (dwarf_version < 5 && size > 0xffff)
9692 continue;
9693 if (dwarf_version >= 5)
9695 if (dwarf_split_debug_info)
9697 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9698 uleb128 index into .debug_addr and uleb128 length. */
9699 dw2_asm_output_data (1, DW_LLE_startx_length,
9700 "DW_LLE_startx_length (%s)",
9701 list_head->ll_symbol);
9702 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9703 "Location list range start index "
9704 "(%s)", curr->begin);
9705 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9706 For that case we probably need to emit DW_LLE_startx_endx,
9707 but we'd need 2 .debug_addr entries rather than just one. */
9708 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9709 "Location list length (%s)",
9710 list_head->ll_symbol);
9712 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9714 /* If all code is in .text section, the base address is
9715 already provided by the CU attributes. Use
9716 DW_LLE_offset_pair where both addresses are uleb128 encoded
9717 offsets against that base. */
9718 dw2_asm_output_data (1, DW_LLE_offset_pair,
9719 "DW_LLE_offset_pair (%s)",
9720 list_head->ll_symbol);
9721 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9722 "Location list begin address (%s)",
9723 list_head->ll_symbol);
9724 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9725 "Location list end address (%s)",
9726 list_head->ll_symbol);
9728 else if (HAVE_AS_LEB128)
9730 /* Otherwise, find out how many consecutive entries could share
9731 the same base entry. If just one, emit DW_LLE_start_length,
9732 otherwise emit DW_LLE_base_address for the base address
9733 followed by a series of DW_LLE_offset_pair. */
9734 if (last_section == NULL || curr->section != last_section)
9736 dw_loc_list_ref curr2;
9737 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9738 curr2 = curr2->dw_loc_next)
9740 if (strcmp (curr2->begin, curr2->end) == 0
9741 && !curr2->force)
9742 continue;
9743 break;
9745 if (curr2 == NULL || curr->section != curr2->section)
9746 last_section = NULL;
9747 else
9749 last_section = curr->section;
9750 base_label = curr->begin;
9751 dw2_asm_output_data (1, DW_LLE_base_address,
9752 "DW_LLE_base_address (%s)",
9753 list_head->ll_symbol);
9754 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9755 "Base address (%s)",
9756 list_head->ll_symbol);
9759 /* Only one entry with the same base address. Use
9760 DW_LLE_start_length with absolute address and uleb128
9761 length. */
9762 if (last_section == NULL)
9764 dw2_asm_output_data (1, DW_LLE_start_length,
9765 "DW_LLE_start_length (%s)",
9766 list_head->ll_symbol);
9767 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9768 "Location list begin address (%s)",
9769 list_head->ll_symbol);
9770 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9771 "Location list length "
9772 "(%s)", list_head->ll_symbol);
9774 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9775 DW_LLE_base_address. */
9776 else
9778 dw2_asm_output_data (1, DW_LLE_offset_pair,
9779 "DW_LLE_offset_pair (%s)",
9780 list_head->ll_symbol);
9781 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9782 "Location list begin address "
9783 "(%s)", list_head->ll_symbol);
9784 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9785 "Location list end address "
9786 "(%s)", list_head->ll_symbol);
9789 /* The assembler does not support .uleb128 directive. Emit
9790 DW_LLE_start_end with a pair of absolute addresses. */
9791 else
9793 dw2_asm_output_data (1, DW_LLE_start_end,
9794 "DW_LLE_start_end (%s)",
9795 list_head->ll_symbol);
9796 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9797 "Location list begin address (%s)",
9798 list_head->ll_symbol);
9799 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9800 "Location list end address (%s)",
9801 list_head->ll_symbol);
9804 else if (dwarf_split_debug_info)
9806 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9807 and 4 byte length. */
9808 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9809 "Location list start/length entry (%s)",
9810 list_head->ll_symbol);
9811 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9812 "Location list range start index (%s)",
9813 curr->begin);
9814 /* The length field is 4 bytes. If we ever need to support
9815 an 8-byte length, we can add a new DW_LLE code or fall back
9816 to DW_LLE_GNU_start_end_entry. */
9817 dw2_asm_output_delta (4, curr->end, curr->begin,
9818 "Location list range length (%s)",
9819 list_head->ll_symbol);
9821 else if (!have_multiple_function_sections)
9823 /* Pair of relative addresses against start of text section. */
9824 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9825 "Location list begin address (%s)",
9826 list_head->ll_symbol);
9827 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9828 "Location list end address (%s)",
9829 list_head->ll_symbol);
9831 else
9833 /* Pair of absolute addresses. */
9834 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9835 "Location list begin address (%s)",
9836 list_head->ll_symbol);
9837 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9838 "Location list end address (%s)",
9839 list_head->ll_symbol);
9842 /* Output the block length for this list of location operations. */
9843 if (dwarf_version >= 5)
9844 dw2_asm_output_data_uleb128 (size, "Location expression size");
9845 else
9847 gcc_assert (size <= 0xffff);
9848 dw2_asm_output_data (2, size, "Location expression size");
9851 output_loc_sequence (curr->expr, -1);
9854 /* And finally list termination. */
9855 if (dwarf_version >= 5)
9856 dw2_asm_output_data (1, DW_LLE_end_of_list,
9857 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9858 else if (dwarf_split_debug_info)
9859 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9860 "Location list terminator (%s)",
9861 list_head->ll_symbol);
9862 else
9864 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9865 "Location list terminator begin (%s)",
9866 list_head->ll_symbol);
9867 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9868 "Location list terminator end (%s)",
9869 list_head->ll_symbol);
9873 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9874 section. Emit a relocated reference if val_entry is NULL, otherwise,
9875 emit an indirect reference. */
9877 static void
9878 output_range_list_offset (dw_attr_node *a)
9880 const char *name = dwarf_attr_name (a->dw_attr);
9882 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9884 if (dwarf_version >= 5)
9886 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9887 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9888 debug_ranges_section, "%s", name);
9890 else
9892 char *p = strchr (ranges_section_label, '\0');
9893 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9894 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9895 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9896 debug_ranges_section, "%s", name);
9897 *p = '\0';
9900 else if (dwarf_version >= 5)
9902 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9903 gcc_assert (rnglist_idx);
9904 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9906 else
9907 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9908 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9909 "%s (offset from %s)", name, ranges_section_label);
9912 /* Output the offset into the debug_loc section. */
9914 static void
9915 output_loc_list_offset (dw_attr_node *a)
9917 char *sym = AT_loc_list (a)->ll_symbol;
9919 gcc_assert (sym);
9920 if (!dwarf_split_debug_info)
9921 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9922 "%s", dwarf_attr_name (a->dw_attr));
9923 else if (dwarf_version >= 5)
9925 gcc_assert (AT_loc_list (a)->num_assigned);
9926 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9927 dwarf_attr_name (a->dw_attr),
9928 sym);
9930 else
9931 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9932 "%s", dwarf_attr_name (a->dw_attr));
9935 /* Output an attribute's index or value appropriately. */
9937 static void
9938 output_attr_index_or_value (dw_attr_node *a)
9940 const char *name = dwarf_attr_name (a->dw_attr);
9942 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9944 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9945 return;
9947 switch (AT_class (a))
9949 case dw_val_class_addr:
9950 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9951 break;
9952 case dw_val_class_high_pc:
9953 case dw_val_class_lbl_id:
9954 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9955 break;
9956 default:
9957 gcc_unreachable ();
9961 /* Output a type signature. */
9963 static inline void
9964 output_signature (const char *sig, const char *name)
9966 int i;
9968 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9969 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9972 /* Output a discriminant value. */
9974 static inline void
9975 output_discr_value (dw_discr_value *discr_value, const char *name)
9977 if (discr_value->pos)
9978 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9979 else
9980 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9983 /* Output the DIE and its attributes. Called recursively to generate
9984 the definitions of each child DIE. */
9986 static void
9987 output_die (dw_die_ref die)
9989 dw_attr_node *a;
9990 dw_die_ref c;
9991 unsigned long size;
9992 unsigned ix;
9994 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9995 (unsigned long)die->die_offset,
9996 dwarf_tag_name (die->die_tag));
9998 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10000 const char *name = dwarf_attr_name (a->dw_attr);
10002 switch (AT_class (a))
10004 case dw_val_class_addr:
10005 output_attr_index_or_value (a);
10006 break;
10008 case dw_val_class_offset:
10009 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10010 "%s", name);
10011 break;
10013 case dw_val_class_range_list:
10014 output_range_list_offset (a);
10015 break;
10017 case dw_val_class_loc:
10018 size = size_of_locs (AT_loc (a));
10020 /* Output the block length for this list of location operations. */
10021 if (dwarf_version >= 4)
10022 dw2_asm_output_data_uleb128 (size, "%s", name);
10023 else
10024 dw2_asm_output_data (constant_size (size), size, "%s", name);
10026 output_loc_sequence (AT_loc (a), -1);
10027 break;
10029 case dw_val_class_const:
10030 /* ??? It would be slightly more efficient to use a scheme like is
10031 used for unsigned constants below, but gdb 4.x does not sign
10032 extend. Gdb 5.x does sign extend. */
10033 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10034 break;
10036 case dw_val_class_unsigned_const:
10038 int csize = constant_size (AT_unsigned (a));
10039 if (dwarf_version == 3
10040 && a->dw_attr == DW_AT_data_member_location
10041 && csize >= 4)
10042 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10043 else
10044 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10046 break;
10048 case dw_val_class_const_implicit:
10049 if (flag_debug_asm)
10050 fprintf (asm_out_file, "\t\t\t%s %s ("
10051 HOST_WIDE_INT_PRINT_DEC ")\n",
10052 ASM_COMMENT_START, name, AT_int (a));
10053 break;
10055 case dw_val_class_unsigned_const_implicit:
10056 if (flag_debug_asm)
10057 fprintf (asm_out_file, "\t\t\t%s %s ("
10058 HOST_WIDE_INT_PRINT_HEX ")\n",
10059 ASM_COMMENT_START, name, AT_unsigned (a));
10060 break;
10062 case dw_val_class_const_double:
10064 unsigned HOST_WIDE_INT first, second;
10066 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10067 dw2_asm_output_data (1,
10068 HOST_BITS_PER_DOUBLE_INT
10069 / HOST_BITS_PER_CHAR,
10070 NULL);
10072 if (WORDS_BIG_ENDIAN)
10074 first = a->dw_attr_val.v.val_double.high;
10075 second = a->dw_attr_val.v.val_double.low;
10077 else
10079 first = a->dw_attr_val.v.val_double.low;
10080 second = a->dw_attr_val.v.val_double.high;
10083 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10084 first, "%s", name);
10085 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10086 second, NULL);
10088 break;
10090 case dw_val_class_wide_int:
10092 int i;
10093 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10094 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10095 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10096 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10097 * l, NULL);
10099 if (WORDS_BIG_ENDIAN)
10100 for (i = len - 1; i >= 0; --i)
10102 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10103 "%s", name);
10104 name = "";
10106 else
10107 for (i = 0; i < len; ++i)
10109 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10110 "%s", name);
10111 name = "";
10114 break;
10116 case dw_val_class_vec:
10118 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10119 unsigned int len = a->dw_attr_val.v.val_vec.length;
10120 unsigned int i;
10121 unsigned char *p;
10123 dw2_asm_output_data (constant_size (len * elt_size),
10124 len * elt_size, "%s", name);
10125 if (elt_size > sizeof (HOST_WIDE_INT))
10127 elt_size /= 2;
10128 len *= 2;
10130 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10131 i < len;
10132 i++, p += elt_size)
10133 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10134 "fp or vector constant word %u", i);
10135 break;
10138 case dw_val_class_flag:
10139 if (dwarf_version >= 4)
10141 /* Currently all add_AT_flag calls pass in 1 as last argument,
10142 so DW_FORM_flag_present can be used. If that ever changes,
10143 we'll need to use DW_FORM_flag and have some optimization
10144 in build_abbrev_table that will change those to
10145 DW_FORM_flag_present if it is set to 1 in all DIEs using
10146 the same abbrev entry. */
10147 gcc_assert (AT_flag (a) == 1);
10148 if (flag_debug_asm)
10149 fprintf (asm_out_file, "\t\t\t%s %s\n",
10150 ASM_COMMENT_START, name);
10151 break;
10153 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10154 break;
10156 case dw_val_class_loc_list:
10157 output_loc_list_offset (a);
10158 break;
10160 case dw_val_class_die_ref:
10161 if (AT_ref_external (a))
10163 if (AT_ref (a)->comdat_type_p)
10165 comdat_type_node *type_node
10166 = AT_ref (a)->die_id.die_type_node;
10168 gcc_assert (type_node);
10169 output_signature (type_node->signature, name);
10171 else
10173 const char *sym = AT_ref (a)->die_id.die_symbol;
10174 int size;
10176 gcc_assert (sym);
10177 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10178 length, whereas in DWARF3 it's always sized as an
10179 offset. */
10180 if (dwarf_version == 2)
10181 size = DWARF2_ADDR_SIZE;
10182 else
10183 size = DWARF_OFFSET_SIZE;
10184 /* ??? We cannot unconditionally output die_offset if
10185 non-zero - others might create references to those
10186 DIEs via symbols.
10187 And we do not clear its DIE offset after outputting it
10188 (and the label refers to the actual DIEs, not the
10189 DWARF CU unit header which is when using label + offset
10190 would be the correct thing to do).
10191 ??? This is the reason for the with_offset flag. */
10192 if (AT_ref (a)->with_offset)
10193 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10194 debug_info_section, "%s", name);
10195 else
10196 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10197 name);
10200 else
10202 gcc_assert (AT_ref (a)->die_offset);
10203 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10204 "%s", name);
10206 break;
10208 case dw_val_class_fde_ref:
10210 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10212 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10213 a->dw_attr_val.v.val_fde_index * 2);
10214 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10215 "%s", name);
10217 break;
10219 case dw_val_class_vms_delta:
10220 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10221 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10222 AT_vms_delta2 (a), AT_vms_delta1 (a),
10223 "%s", name);
10224 #else
10225 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10226 AT_vms_delta2 (a), AT_vms_delta1 (a),
10227 "%s", name);
10228 #endif
10229 break;
10231 case dw_val_class_lbl_id:
10232 output_attr_index_or_value (a);
10233 break;
10235 case dw_val_class_lineptr:
10236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10237 debug_line_section, "%s", name);
10238 break;
10240 case dw_val_class_macptr:
10241 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10242 debug_macinfo_section, "%s", name);
10243 break;
10245 case dw_val_class_loclistsptr:
10246 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10247 debug_loc_section, "%s", name);
10248 break;
10250 case dw_val_class_str:
10251 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10252 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10253 a->dw_attr_val.v.val_str->label,
10254 debug_str_section,
10255 "%s: \"%s\"", name, AT_string (a));
10256 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10257 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10258 a->dw_attr_val.v.val_str->label,
10259 debug_line_str_section,
10260 "%s: \"%s\"", name, AT_string (a));
10261 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10262 dw2_asm_output_data_uleb128 (AT_index (a),
10263 "%s: \"%s\"", name, AT_string (a));
10264 else
10265 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10266 break;
10268 case dw_val_class_file:
10270 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10272 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10273 a->dw_attr_val.v.val_file->filename);
10274 break;
10277 case dw_val_class_file_implicit:
10278 if (flag_debug_asm)
10279 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10280 ASM_COMMENT_START, name,
10281 maybe_emit_file (a->dw_attr_val.v.val_file),
10282 a->dw_attr_val.v.val_file->filename);
10283 break;
10285 case dw_val_class_data8:
10287 int i;
10289 for (i = 0; i < 8; i++)
10290 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10291 i == 0 ? "%s" : NULL, name);
10292 break;
10295 case dw_val_class_high_pc:
10296 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10297 get_AT_low_pc (die), "DW_AT_high_pc");
10298 break;
10300 case dw_val_class_discr_value:
10301 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10302 break;
10304 case dw_val_class_discr_list:
10306 dw_discr_list_ref list = AT_discr_list (a);
10307 const int size = size_of_discr_list (list);
10309 /* This is a block, so output its length first. */
10310 dw2_asm_output_data (constant_size (size), size,
10311 "%s: block size", name);
10313 for (; list != NULL; list = list->dw_discr_next)
10315 /* One byte for the discriminant value descriptor, and then as
10316 many LEB128 numbers as required. */
10317 if (list->dw_discr_range)
10318 dw2_asm_output_data (1, DW_DSC_range,
10319 "%s: DW_DSC_range", name);
10320 else
10321 dw2_asm_output_data (1, DW_DSC_label,
10322 "%s: DW_DSC_label", name);
10324 output_discr_value (&list->dw_discr_lower_bound, name);
10325 if (list->dw_discr_range)
10326 output_discr_value (&list->dw_discr_upper_bound, name);
10328 break;
10331 default:
10332 gcc_unreachable ();
10336 FOR_EACH_CHILD (die, c, output_die (c));
10338 /* Add null byte to terminate sibling list. */
10339 if (die->die_child != NULL)
10340 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10341 (unsigned long) die->die_offset);
10344 /* Output the compilation unit that appears at the beginning of the
10345 .debug_info section, and precedes the DIE descriptions. */
10347 static void
10348 output_compilation_unit_header (enum dwarf_unit_type ut)
10350 if (!XCOFF_DEBUGGING_INFO)
10352 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10353 dw2_asm_output_data (4, 0xffffffff,
10354 "Initial length escape value indicating 64-bit DWARF extension");
10355 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10356 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10357 "Length of Compilation Unit Info");
10360 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10361 if (dwarf_version >= 5)
10363 const char *name;
10364 switch (ut)
10366 case DW_UT_compile: name = "DW_UT_compile"; break;
10367 case DW_UT_type: name = "DW_UT_type"; break;
10368 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10369 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10370 default: gcc_unreachable ();
10372 dw2_asm_output_data (1, ut, "%s", name);
10373 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10375 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10376 debug_abbrev_section,
10377 "Offset Into Abbrev. Section");
10378 if (dwarf_version < 5)
10379 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10382 /* Output the compilation unit DIE and its children. */
10384 static void
10385 output_comp_unit (dw_die_ref die, int output_if_empty,
10386 const unsigned char *dwo_id)
10388 const char *secname, *oldsym;
10389 char *tmp;
10391 /* Unless we are outputting main CU, we may throw away empty ones. */
10392 if (!output_if_empty && die->die_child == NULL)
10393 return;
10395 /* Even if there are no children of this DIE, we must output the information
10396 about the compilation unit. Otherwise, on an empty translation unit, we
10397 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10398 will then complain when examining the file. First mark all the DIEs in
10399 this CU so we know which get local refs. */
10400 mark_dies (die);
10402 external_ref_hash_type *extern_map = optimize_external_refs (die);
10404 /* For now, optimize only the main CU, in order to optimize the rest
10405 we'd need to see all of them earlier. Leave the rest for post-linking
10406 tools like DWZ. */
10407 if (die == comp_unit_die ())
10408 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10410 build_abbrev_table (die, extern_map);
10412 optimize_abbrev_table ();
10414 delete extern_map;
10416 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10417 next_die_offset = (dwo_id
10418 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10419 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10420 calc_die_sizes (die);
10422 oldsym = die->die_id.die_symbol;
10423 if (oldsym && die->comdat_type_p)
10425 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10427 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10428 secname = tmp;
10429 die->die_id.die_symbol = NULL;
10430 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10432 else
10434 switch_to_section (debug_info_section);
10435 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10436 info_section_emitted = true;
10439 /* For LTO cross unit DIE refs we want a symbol on the start of the
10440 debuginfo section, not on the CU DIE. */
10441 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10443 /* ??? No way to get visibility assembled without a decl. */
10444 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10445 get_identifier (oldsym), char_type_node);
10446 TREE_PUBLIC (decl) = true;
10447 TREE_STATIC (decl) = true;
10448 DECL_ARTIFICIAL (decl) = true;
10449 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10450 DECL_VISIBILITY_SPECIFIED (decl) = true;
10451 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10452 #ifdef ASM_WEAKEN_LABEL
10453 /* We prefer a .weak because that handles duplicates from duplicate
10454 archive members in a graceful way. */
10455 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10456 #else
10457 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10458 #endif
10459 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10462 /* Output debugging information. */
10463 output_compilation_unit_header (dwo_id
10464 ? DW_UT_split_compile : DW_UT_compile);
10465 if (dwarf_version >= 5)
10467 if (dwo_id != NULL)
10468 for (int i = 0; i < 8; i++)
10469 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10471 output_die (die);
10473 /* Leave the marks on the main CU, so we can check them in
10474 output_pubnames. */
10475 if (oldsym)
10477 unmark_dies (die);
10478 die->die_id.die_symbol = oldsym;
10482 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10483 and .debug_pubtypes. This is configured per-target, but can be
10484 overridden by the -gpubnames or -gno-pubnames options. */
10486 static inline bool
10487 want_pubnames (void)
10489 if (debug_info_level <= DINFO_LEVEL_TERSE)
10490 return false;
10491 if (debug_generate_pub_sections != -1)
10492 return debug_generate_pub_sections;
10493 return targetm.want_debug_pub_sections;
10496 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10498 static void
10499 add_AT_pubnames (dw_die_ref die)
10501 if (want_pubnames ())
10502 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10505 /* Add a string attribute value to a skeleton DIE. */
10507 static inline void
10508 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10509 const char *str)
10511 dw_attr_node attr;
10512 struct indirect_string_node *node;
10514 if (! skeleton_debug_str_hash)
10515 skeleton_debug_str_hash
10516 = hash_table<indirect_string_hasher>::create_ggc (10);
10518 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10519 find_string_form (node);
10520 if (node->form == DW_FORM_GNU_str_index)
10521 node->form = DW_FORM_strp;
10523 attr.dw_attr = attr_kind;
10524 attr.dw_attr_val.val_class = dw_val_class_str;
10525 attr.dw_attr_val.val_entry = NULL;
10526 attr.dw_attr_val.v.val_str = node;
10527 add_dwarf_attr (die, &attr);
10530 /* Helper function to generate top-level dies for skeleton debug_info and
10531 debug_types. */
10533 static void
10534 add_top_level_skeleton_die_attrs (dw_die_ref die)
10536 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10537 const char *comp_dir = comp_dir_string ();
10539 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10540 if (comp_dir != NULL)
10541 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10542 add_AT_pubnames (die);
10543 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10546 /* Output skeleton debug sections that point to the dwo file. */
10548 static void
10549 output_skeleton_debug_sections (dw_die_ref comp_unit,
10550 const unsigned char *dwo_id)
10552 /* These attributes will be found in the full debug_info section. */
10553 remove_AT (comp_unit, DW_AT_producer);
10554 remove_AT (comp_unit, DW_AT_language);
10556 switch_to_section (debug_skeleton_info_section);
10557 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10559 /* Produce the skeleton compilation-unit header. This one differs enough from
10560 a normal CU header that it's better not to call output_compilation_unit
10561 header. */
10562 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10563 dw2_asm_output_data (4, 0xffffffff,
10564 "Initial length escape value indicating 64-bit "
10565 "DWARF extension");
10567 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10568 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10569 - DWARF_INITIAL_LENGTH_SIZE
10570 + size_of_die (comp_unit),
10571 "Length of Compilation Unit Info");
10572 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10573 if (dwarf_version >= 5)
10575 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10576 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10578 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10579 debug_skeleton_abbrev_section,
10580 "Offset Into Abbrev. Section");
10581 if (dwarf_version < 5)
10582 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10583 else
10584 for (int i = 0; i < 8; i++)
10585 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10587 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10588 output_die (comp_unit);
10590 /* Build the skeleton debug_abbrev section. */
10591 switch_to_section (debug_skeleton_abbrev_section);
10592 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10594 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10596 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10599 /* Output a comdat type unit DIE and its children. */
10601 static void
10602 output_comdat_type_unit (comdat_type_node *node)
10604 const char *secname;
10605 char *tmp;
10606 int i;
10607 #if defined (OBJECT_FORMAT_ELF)
10608 tree comdat_key;
10609 #endif
10611 /* First mark all the DIEs in this CU so we know which get local refs. */
10612 mark_dies (node->root_die);
10614 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10616 build_abbrev_table (node->root_die, extern_map);
10618 delete extern_map;
10619 extern_map = NULL;
10621 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10622 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10623 calc_die_sizes (node->root_die);
10625 #if defined (OBJECT_FORMAT_ELF)
10626 if (dwarf_version >= 5)
10628 if (!dwarf_split_debug_info)
10629 secname = ".debug_info";
10630 else
10631 secname = ".debug_info.dwo";
10633 else if (!dwarf_split_debug_info)
10634 secname = ".debug_types";
10635 else
10636 secname = ".debug_types.dwo";
10638 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10639 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10640 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10641 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10642 comdat_key = get_identifier (tmp);
10643 targetm.asm_out.named_section (secname,
10644 SECTION_DEBUG | SECTION_LINKONCE,
10645 comdat_key);
10646 #else
10647 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10648 sprintf (tmp, (dwarf_version >= 5
10649 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10650 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10651 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10652 secname = tmp;
10653 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10654 #endif
10656 /* Output debugging information. */
10657 output_compilation_unit_header (dwarf_split_debug_info
10658 ? DW_UT_split_type : DW_UT_type);
10659 output_signature (node->signature, "Type Signature");
10660 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10661 "Offset to Type DIE");
10662 output_die (node->root_die);
10664 unmark_dies (node->root_die);
10667 /* Return the DWARF2/3 pubname associated with a decl. */
10669 static const char *
10670 dwarf2_name (tree decl, int scope)
10672 if (DECL_NAMELESS (decl))
10673 return NULL;
10674 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10677 /* Add a new entry to .debug_pubnames if appropriate. */
10679 static void
10680 add_pubname_string (const char *str, dw_die_ref die)
10682 pubname_entry e;
10684 e.die = die;
10685 e.name = xstrdup (str);
10686 vec_safe_push (pubname_table, e);
10689 static void
10690 add_pubname (tree decl, dw_die_ref die)
10692 if (!want_pubnames ())
10693 return;
10695 /* Don't add items to the table when we expect that the consumer will have
10696 just read the enclosing die. For example, if the consumer is looking at a
10697 class_member, it will either be inside the class already, or will have just
10698 looked up the class to find the member. Either way, searching the class is
10699 faster than searching the index. */
10700 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10701 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10703 const char *name = dwarf2_name (decl, 1);
10705 if (name)
10706 add_pubname_string (name, die);
10710 /* Add an enumerator to the pubnames section. */
10712 static void
10713 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10715 pubname_entry e;
10717 gcc_assert (scope_name);
10718 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10719 e.die = die;
10720 vec_safe_push (pubname_table, e);
10723 /* Add a new entry to .debug_pubtypes if appropriate. */
10725 static void
10726 add_pubtype (tree decl, dw_die_ref die)
10728 pubname_entry e;
10730 if (!want_pubnames ())
10731 return;
10733 if ((TREE_PUBLIC (decl)
10734 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10735 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10737 tree scope = NULL;
10738 const char *scope_name = "";
10739 const char *sep = is_cxx () ? "::" : ".";
10740 const char *name;
10742 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10743 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10745 scope_name = lang_hooks.dwarf_name (scope, 1);
10746 if (scope_name != NULL && scope_name[0] != '\0')
10747 scope_name = concat (scope_name, sep, NULL);
10748 else
10749 scope_name = "";
10752 if (TYPE_P (decl))
10753 name = type_tag (decl);
10754 else
10755 name = lang_hooks.dwarf_name (decl, 1);
10757 /* If we don't have a name for the type, there's no point in adding
10758 it to the table. */
10759 if (name != NULL && name[0] != '\0')
10761 e.die = die;
10762 e.name = concat (scope_name, name, NULL);
10763 vec_safe_push (pubtype_table, e);
10766 /* Although it might be more consistent to add the pubinfo for the
10767 enumerators as their dies are created, they should only be added if the
10768 enum type meets the criteria above. So rather than re-check the parent
10769 enum type whenever an enumerator die is created, just output them all
10770 here. This isn't protected by the name conditional because anonymous
10771 enums don't have names. */
10772 if (die->die_tag == DW_TAG_enumeration_type)
10774 dw_die_ref c;
10776 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10781 /* Output a single entry in the pubnames table. */
10783 static void
10784 output_pubname (dw_offset die_offset, pubname_entry *entry)
10786 dw_die_ref die = entry->die;
10787 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10789 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10791 if (debug_generate_pub_sections == 2)
10793 /* This logic follows gdb's method for determining the value of the flag
10794 byte. */
10795 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10796 switch (die->die_tag)
10798 case DW_TAG_typedef:
10799 case DW_TAG_base_type:
10800 case DW_TAG_subrange_type:
10801 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10802 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10803 break;
10804 case DW_TAG_enumerator:
10805 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10806 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10807 if (!is_cxx ())
10808 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10809 break;
10810 case DW_TAG_subprogram:
10811 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10812 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10813 if (!is_ada ())
10814 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10815 break;
10816 case DW_TAG_constant:
10817 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10818 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10819 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10820 break;
10821 case DW_TAG_variable:
10822 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10823 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10824 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10825 break;
10826 case DW_TAG_namespace:
10827 case DW_TAG_imported_declaration:
10828 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10829 break;
10830 case DW_TAG_class_type:
10831 case DW_TAG_interface_type:
10832 case DW_TAG_structure_type:
10833 case DW_TAG_union_type:
10834 case DW_TAG_enumeration_type:
10835 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10836 if (!is_cxx ())
10837 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10838 break;
10839 default:
10840 /* An unusual tag. Leave the flag-byte empty. */
10841 break;
10843 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10844 "GDB-index flags");
10847 dw2_asm_output_nstring (entry->name, -1, "external name");
10851 /* Output the public names table used to speed up access to externally
10852 visible names; or the public types table used to find type definitions. */
10854 static void
10855 output_pubnames (vec<pubname_entry, va_gc> *names)
10857 unsigned i;
10858 unsigned long pubnames_length = size_of_pubnames (names);
10859 pubname_entry *pub;
10861 if (!XCOFF_DEBUGGING_INFO)
10863 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10864 dw2_asm_output_data (4, 0xffffffff,
10865 "Initial length escape value indicating 64-bit DWARF extension");
10866 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10867 "Pub Info Length");
10870 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10871 dw2_asm_output_data (2, 2, "DWARF Version");
10873 if (dwarf_split_debug_info)
10874 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10875 debug_skeleton_info_section,
10876 "Offset of Compilation Unit Info");
10877 else
10878 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10879 debug_info_section,
10880 "Offset of Compilation Unit Info");
10881 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10882 "Compilation Unit Length");
10884 FOR_EACH_VEC_ELT (*names, i, pub)
10886 if (include_pubname_in_output (names, pub))
10888 dw_offset die_offset = pub->die->die_offset;
10890 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10891 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10892 gcc_assert (pub->die->die_mark);
10894 /* If we're putting types in their own .debug_types sections,
10895 the .debug_pubtypes table will still point to the compile
10896 unit (not the type unit), so we want to use the offset of
10897 the skeleton DIE (if there is one). */
10898 if (pub->die->comdat_type_p && names == pubtype_table)
10900 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10902 if (type_node != NULL)
10903 die_offset = (type_node->skeleton_die != NULL
10904 ? type_node->skeleton_die->die_offset
10905 : comp_unit_die ()->die_offset);
10908 output_pubname (die_offset, pub);
10912 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10915 /* Output public names and types tables if necessary. */
10917 static void
10918 output_pubtables (void)
10920 if (!want_pubnames () || !info_section_emitted)
10921 return;
10923 switch_to_section (debug_pubnames_section);
10924 output_pubnames (pubname_table);
10925 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10926 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10927 simply won't look for the section. */
10928 switch_to_section (debug_pubtypes_section);
10929 output_pubnames (pubtype_table);
10933 /* Output the information that goes into the .debug_aranges table.
10934 Namely, define the beginning and ending address range of the
10935 text section generated for this compilation unit. */
10937 static void
10938 output_aranges (void)
10940 unsigned i;
10941 unsigned long aranges_length = size_of_aranges ();
10943 if (!XCOFF_DEBUGGING_INFO)
10945 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10946 dw2_asm_output_data (4, 0xffffffff,
10947 "Initial length escape value indicating 64-bit DWARF extension");
10948 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10949 "Length of Address Ranges Info");
10952 /* Version number for aranges is still 2, even up to DWARF5. */
10953 dw2_asm_output_data (2, 2, "DWARF Version");
10954 if (dwarf_split_debug_info)
10955 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10956 debug_skeleton_info_section,
10957 "Offset of Compilation Unit Info");
10958 else
10959 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10960 debug_info_section,
10961 "Offset of Compilation Unit Info");
10962 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10963 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10965 /* We need to align to twice the pointer size here. */
10966 if (DWARF_ARANGES_PAD_SIZE)
10968 /* Pad using a 2 byte words so that padding is correct for any
10969 pointer size. */
10970 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10971 2 * DWARF2_ADDR_SIZE);
10972 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10973 dw2_asm_output_data (2, 0, NULL);
10976 /* It is necessary not to output these entries if the sections were
10977 not used; if the sections were not used, the length will be 0 and
10978 the address may end up as 0 if the section is discarded by ld
10979 --gc-sections, leaving an invalid (0, 0) entry that can be
10980 confused with the terminator. */
10981 if (text_section_used)
10983 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10984 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10985 text_section_label, "Length");
10987 if (cold_text_section_used)
10989 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10990 "Address");
10991 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10992 cold_text_section_label, "Length");
10995 if (have_multiple_function_sections)
10997 unsigned fde_idx;
10998 dw_fde_ref fde;
11000 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11002 if (DECL_IGNORED_P (fde->decl))
11003 continue;
11004 if (!fde->in_std_section)
11006 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11007 "Address");
11008 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11009 fde->dw_fde_begin, "Length");
11011 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11013 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11014 "Address");
11015 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11016 fde->dw_fde_second_begin, "Length");
11021 /* Output the terminator words. */
11022 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11023 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11026 /* Add a new entry to .debug_ranges. Return its index into
11027 ranges_table vector. */
11029 static unsigned int
11030 add_ranges_num (int num, bool maybe_new_sec)
11032 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11033 vec_safe_push (ranges_table, r);
11034 return vec_safe_length (ranges_table) - 1;
11037 /* Add a new entry to .debug_ranges corresponding to a block, or a
11038 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11039 this entry might be in a different section from previous range. */
11041 static unsigned int
11042 add_ranges (const_tree block, bool maybe_new_sec)
11044 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11047 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11048 chain, or middle entry of a chain that will be directly referred to. */
11050 static void
11051 note_rnglist_head (unsigned int offset)
11053 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11054 return;
11055 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11058 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11059 When using dwarf_split_debug_info, address attributes in dies destined
11060 for the final executable should be direct references--setting the
11061 parameter force_direct ensures this behavior. */
11063 static void
11064 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11065 bool *added, bool force_direct)
11067 unsigned int in_use = vec_safe_length (ranges_by_label);
11068 unsigned int offset;
11069 dw_ranges_by_label rbl = { begin, end };
11070 vec_safe_push (ranges_by_label, rbl);
11071 offset = add_ranges_num (-(int)in_use - 1, true);
11072 if (!*added)
11074 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11075 *added = true;
11076 note_rnglist_head (offset);
11080 /* Emit .debug_ranges section. */
11082 static void
11083 output_ranges (void)
11085 unsigned i;
11086 static const char *const start_fmt = "Offset %#x";
11087 const char *fmt = start_fmt;
11088 dw_ranges *r;
11090 switch_to_section (debug_ranges_section);
11091 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11092 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11094 int block_num = r->num;
11096 if (block_num > 0)
11098 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11099 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11101 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11102 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11104 /* If all code is in the text section, then the compilation
11105 unit base address defaults to DW_AT_low_pc, which is the
11106 base of the text section. */
11107 if (!have_multiple_function_sections)
11109 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11110 text_section_label,
11111 fmt, i * 2 * DWARF2_ADDR_SIZE);
11112 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11113 text_section_label, NULL);
11116 /* Otherwise, the compilation unit base address is zero,
11117 which allows us to use absolute addresses, and not worry
11118 about whether the target supports cross-section
11119 arithmetic. */
11120 else
11122 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11123 fmt, i * 2 * DWARF2_ADDR_SIZE);
11124 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11127 fmt = NULL;
11130 /* Negative block_num stands for an index into ranges_by_label. */
11131 else if (block_num < 0)
11133 int lab_idx = - block_num - 1;
11135 if (!have_multiple_function_sections)
11137 gcc_unreachable ();
11138 #if 0
11139 /* If we ever use add_ranges_by_labels () for a single
11140 function section, all we have to do is to take out
11141 the #if 0 above. */
11142 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11143 (*ranges_by_label)[lab_idx].begin,
11144 text_section_label,
11145 fmt, i * 2 * DWARF2_ADDR_SIZE);
11146 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11147 (*ranges_by_label)[lab_idx].end,
11148 text_section_label, NULL);
11149 #endif
11151 else
11153 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11154 (*ranges_by_label)[lab_idx].begin,
11155 fmt, i * 2 * DWARF2_ADDR_SIZE);
11156 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11157 (*ranges_by_label)[lab_idx].end,
11158 NULL);
11161 else
11163 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11164 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11165 fmt = start_fmt;
11170 /* Non-zero if .debug_line_str should be used for .debug_line section
11171 strings or strings that are likely shareable with those. */
11172 #define DWARF5_USE_DEBUG_LINE_STR \
11173 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11174 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11175 /* FIXME: there is no .debug_line_str.dwo section, \
11176 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11177 && !dwarf_split_debug_info)
11179 /* Assign .debug_rnglists indexes. */
11181 static void
11182 index_rnglists (void)
11184 unsigned i;
11185 dw_ranges *r;
11187 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11188 if (r->label)
11189 r->idx = rnglist_idx++;
11192 /* Emit .debug_rnglists section. */
11194 static void
11195 output_rnglists (void)
11197 unsigned i;
11198 dw_ranges *r;
11199 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11200 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11201 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11203 switch_to_section (debug_ranges_section);
11204 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11205 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11206 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11207 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11208 dw2_asm_output_data (4, 0xffffffff,
11209 "Initial length escape value indicating "
11210 "64-bit DWARF extension");
11211 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11212 "Length of Range Lists");
11213 ASM_OUTPUT_LABEL (asm_out_file, l1);
11214 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11215 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11216 dw2_asm_output_data (1, 0, "Segment Size");
11217 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11218 about relocation sizes and primarily care about the size of .debug*
11219 sections in linked shared libraries and executables, then
11220 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11221 into it are usually larger than just DW_FORM_sec_offset offsets
11222 into the .debug_rnglists section. */
11223 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11224 "Offset Entry Count");
11225 if (dwarf_split_debug_info)
11227 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11228 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11229 if (r->label)
11230 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11231 ranges_base_label, NULL);
11234 const char *lab = "";
11235 unsigned int len = vec_safe_length (ranges_table);
11236 const char *base = NULL;
11237 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11239 int block_num = r->num;
11241 if (r->label)
11243 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11244 lab = r->label;
11246 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11247 base = NULL;
11248 if (block_num > 0)
11250 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11251 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11253 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11254 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11256 if (HAVE_AS_LEB128)
11258 /* If all code is in the text section, then the compilation
11259 unit base address defaults to DW_AT_low_pc, which is the
11260 base of the text section. */
11261 if (!have_multiple_function_sections)
11263 dw2_asm_output_data (1, DW_RLE_offset_pair,
11264 "DW_RLE_offset_pair (%s)", lab);
11265 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11266 "Range begin address (%s)", lab);
11267 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11268 "Range end address (%s)", lab);
11269 continue;
11271 if (base == NULL)
11273 dw_ranges *r2 = NULL;
11274 if (i < len - 1)
11275 r2 = &(*ranges_table)[i + 1];
11276 if (r2
11277 && r2->num != 0
11278 && r2->label == NULL
11279 && !r2->maybe_new_sec)
11281 dw2_asm_output_data (1, DW_RLE_base_address,
11282 "DW_RLE_base_address (%s)", lab);
11283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11284 "Base address (%s)", lab);
11285 strcpy (basebuf, blabel);
11286 base = basebuf;
11289 if (base)
11291 dw2_asm_output_data (1, DW_RLE_offset_pair,
11292 "DW_RLE_offset_pair (%s)", lab);
11293 dw2_asm_output_delta_uleb128 (blabel, base,
11294 "Range begin address (%s)", lab);
11295 dw2_asm_output_delta_uleb128 (elabel, base,
11296 "Range end address (%s)", lab);
11297 continue;
11299 dw2_asm_output_data (1, DW_RLE_start_length,
11300 "DW_RLE_start_length (%s)", lab);
11301 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11302 "Range begin address (%s)", lab);
11303 dw2_asm_output_delta_uleb128 (elabel, blabel,
11304 "Range length (%s)", lab);
11306 else
11308 dw2_asm_output_data (1, DW_RLE_start_end,
11309 "DW_RLE_start_end (%s)", lab);
11310 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11311 "Range begin address (%s)", lab);
11312 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11313 "Range end address (%s)", lab);
11317 /* Negative block_num stands for an index into ranges_by_label. */
11318 else if (block_num < 0)
11320 int lab_idx = - block_num - 1;
11321 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11322 const char *elabel = (*ranges_by_label)[lab_idx].end;
11324 if (!have_multiple_function_sections)
11325 gcc_unreachable ();
11326 if (HAVE_AS_LEB128)
11328 dw2_asm_output_data (1, DW_RLE_start_length,
11329 "DW_RLE_start_length (%s)", lab);
11330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11331 "Range begin address (%s)", lab);
11332 dw2_asm_output_delta_uleb128 (elabel, blabel,
11333 "Range length (%s)", lab);
11335 else
11337 dw2_asm_output_data (1, DW_RLE_start_end,
11338 "DW_RLE_start_end (%s)", lab);
11339 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11340 "Range begin address (%s)", lab);
11341 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11342 "Range end address (%s)", lab);
11345 else
11346 dw2_asm_output_data (1, DW_RLE_end_of_list,
11347 "DW_RLE_end_of_list (%s)", lab);
11349 ASM_OUTPUT_LABEL (asm_out_file, l2);
11352 /* Data structure containing information about input files. */
11353 struct file_info
11355 const char *path; /* Complete file name. */
11356 const char *fname; /* File name part. */
11357 int length; /* Length of entire string. */
11358 struct dwarf_file_data * file_idx; /* Index in input file table. */
11359 int dir_idx; /* Index in directory table. */
11362 /* Data structure containing information about directories with source
11363 files. */
11364 struct dir_info
11366 const char *path; /* Path including directory name. */
11367 int length; /* Path length. */
11368 int prefix; /* Index of directory entry which is a prefix. */
11369 int count; /* Number of files in this directory. */
11370 int dir_idx; /* Index of directory used as base. */
11373 /* Callback function for file_info comparison. We sort by looking at
11374 the directories in the path. */
11376 static int
11377 file_info_cmp (const void *p1, const void *p2)
11379 const struct file_info *const s1 = (const struct file_info *) p1;
11380 const struct file_info *const s2 = (const struct file_info *) p2;
11381 const unsigned char *cp1;
11382 const unsigned char *cp2;
11384 /* Take care of file names without directories. We need to make sure that
11385 we return consistent values to qsort since some will get confused if
11386 we return the same value when identical operands are passed in opposite
11387 orders. So if neither has a directory, return 0 and otherwise return
11388 1 or -1 depending on which one has the directory. */
11389 if ((s1->path == s1->fname || s2->path == s2->fname))
11390 return (s2->path == s2->fname) - (s1->path == s1->fname);
11392 cp1 = (const unsigned char *) s1->path;
11393 cp2 = (const unsigned char *) s2->path;
11395 while (1)
11397 ++cp1;
11398 ++cp2;
11399 /* Reached the end of the first path? If so, handle like above. */
11400 if ((cp1 == (const unsigned char *) s1->fname)
11401 || (cp2 == (const unsigned char *) s2->fname))
11402 return ((cp2 == (const unsigned char *) s2->fname)
11403 - (cp1 == (const unsigned char *) s1->fname));
11405 /* Character of current path component the same? */
11406 else if (*cp1 != *cp2)
11407 return *cp1 - *cp2;
11411 struct file_name_acquire_data
11413 struct file_info *files;
11414 int used_files;
11415 int max_files;
11418 /* Traversal function for the hash table. */
11421 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11423 struct dwarf_file_data *d = *slot;
11424 struct file_info *fi;
11425 const char *f;
11427 gcc_assert (fnad->max_files >= d->emitted_number);
11429 if (! d->emitted_number)
11430 return 1;
11432 gcc_assert (fnad->max_files != fnad->used_files);
11434 fi = fnad->files + fnad->used_files++;
11436 /* Skip all leading "./". */
11437 f = d->filename;
11438 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11439 f += 2;
11441 /* Create a new array entry. */
11442 fi->path = f;
11443 fi->length = strlen (f);
11444 fi->file_idx = d;
11446 /* Search for the file name part. */
11447 f = strrchr (f, DIR_SEPARATOR);
11448 #if defined (DIR_SEPARATOR_2)
11450 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11452 if (g != NULL)
11454 if (f == NULL || f < g)
11455 f = g;
11458 #endif
11460 fi->fname = f == NULL ? fi->path : f + 1;
11461 return 1;
11464 /* Helper function for output_file_names. Emit a FORM encoded
11465 string STR, with assembly comment start ENTRY_KIND and
11466 index IDX */
11468 static void
11469 output_line_string (enum dwarf_form form, const char *str,
11470 const char *entry_kind, unsigned int idx)
11472 switch (form)
11474 case DW_FORM_string:
11475 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11476 break;
11477 case DW_FORM_line_strp:
11478 if (!debug_line_str_hash)
11479 debug_line_str_hash
11480 = hash_table<indirect_string_hasher>::create_ggc (10);
11482 struct indirect_string_node *node;
11483 node = find_AT_string_in_table (str, debug_line_str_hash);
11484 set_indirect_string (node);
11485 node->form = form;
11486 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11487 debug_line_str_section, "%s: %#x: \"%s\"",
11488 entry_kind, 0, node->str);
11489 break;
11490 default:
11491 gcc_unreachable ();
11495 /* Output the directory table and the file name table. We try to minimize
11496 the total amount of memory needed. A heuristic is used to avoid large
11497 slowdowns with many input files. */
11499 static void
11500 output_file_names (void)
11502 struct file_name_acquire_data fnad;
11503 int numfiles;
11504 struct file_info *files;
11505 struct dir_info *dirs;
11506 int *saved;
11507 int *savehere;
11508 int *backmap;
11509 int ndirs;
11510 int idx_offset;
11511 int i;
11513 if (!last_emitted_file)
11515 if (dwarf_version >= 5)
11517 dw2_asm_output_data (1, 0, "Directory entry format count");
11518 dw2_asm_output_data_uleb128 (0, "Directories count");
11519 dw2_asm_output_data (1, 0, "File name entry format count");
11520 dw2_asm_output_data_uleb128 (0, "File names count");
11522 else
11524 dw2_asm_output_data (1, 0, "End directory table");
11525 dw2_asm_output_data (1, 0, "End file name table");
11527 return;
11530 numfiles = last_emitted_file->emitted_number;
11532 /* Allocate the various arrays we need. */
11533 files = XALLOCAVEC (struct file_info, numfiles);
11534 dirs = XALLOCAVEC (struct dir_info, numfiles);
11536 fnad.files = files;
11537 fnad.used_files = 0;
11538 fnad.max_files = numfiles;
11539 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11540 gcc_assert (fnad.used_files == fnad.max_files);
11542 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11544 /* Find all the different directories used. */
11545 dirs[0].path = files[0].path;
11546 dirs[0].length = files[0].fname - files[0].path;
11547 dirs[0].prefix = -1;
11548 dirs[0].count = 1;
11549 dirs[0].dir_idx = 0;
11550 files[0].dir_idx = 0;
11551 ndirs = 1;
11553 for (i = 1; i < numfiles; i++)
11554 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11555 && memcmp (dirs[ndirs - 1].path, files[i].path,
11556 dirs[ndirs - 1].length) == 0)
11558 /* Same directory as last entry. */
11559 files[i].dir_idx = ndirs - 1;
11560 ++dirs[ndirs - 1].count;
11562 else
11564 int j;
11566 /* This is a new directory. */
11567 dirs[ndirs].path = files[i].path;
11568 dirs[ndirs].length = files[i].fname - files[i].path;
11569 dirs[ndirs].count = 1;
11570 dirs[ndirs].dir_idx = ndirs;
11571 files[i].dir_idx = ndirs;
11573 /* Search for a prefix. */
11574 dirs[ndirs].prefix = -1;
11575 for (j = 0; j < ndirs; j++)
11576 if (dirs[j].length < dirs[ndirs].length
11577 && dirs[j].length > 1
11578 && (dirs[ndirs].prefix == -1
11579 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11580 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11581 dirs[ndirs].prefix = j;
11583 ++ndirs;
11586 /* Now to the actual work. We have to find a subset of the directories which
11587 allow expressing the file name using references to the directory table
11588 with the least amount of characters. We do not do an exhaustive search
11589 where we would have to check out every combination of every single
11590 possible prefix. Instead we use a heuristic which provides nearly optimal
11591 results in most cases and never is much off. */
11592 saved = XALLOCAVEC (int, ndirs);
11593 savehere = XALLOCAVEC (int, ndirs);
11595 memset (saved, '\0', ndirs * sizeof (saved[0]));
11596 for (i = 0; i < ndirs; i++)
11598 int j;
11599 int total;
11601 /* We can always save some space for the current directory. But this
11602 does not mean it will be enough to justify adding the directory. */
11603 savehere[i] = dirs[i].length;
11604 total = (savehere[i] - saved[i]) * dirs[i].count;
11606 for (j = i + 1; j < ndirs; j++)
11608 savehere[j] = 0;
11609 if (saved[j] < dirs[i].length)
11611 /* Determine whether the dirs[i] path is a prefix of the
11612 dirs[j] path. */
11613 int k;
11615 k = dirs[j].prefix;
11616 while (k != -1 && k != (int) i)
11617 k = dirs[k].prefix;
11619 if (k == (int) i)
11621 /* Yes it is. We can possibly save some memory by
11622 writing the filenames in dirs[j] relative to
11623 dirs[i]. */
11624 savehere[j] = dirs[i].length;
11625 total += (savehere[j] - saved[j]) * dirs[j].count;
11630 /* Check whether we can save enough to justify adding the dirs[i]
11631 directory. */
11632 if (total > dirs[i].length + 1)
11634 /* It's worthwhile adding. */
11635 for (j = i; j < ndirs; j++)
11636 if (savehere[j] > 0)
11638 /* Remember how much we saved for this directory so far. */
11639 saved[j] = savehere[j];
11641 /* Remember the prefix directory. */
11642 dirs[j].dir_idx = i;
11647 /* Emit the directory name table. */
11648 idx_offset = dirs[0].length > 0 ? 1 : 0;
11649 enum dwarf_form str_form = DW_FORM_string;
11650 enum dwarf_form idx_form = DW_FORM_udata;
11651 if (dwarf_version >= 5)
11653 const char *comp_dir = comp_dir_string ();
11654 if (comp_dir == NULL)
11655 comp_dir = "";
11656 dw2_asm_output_data (1, 1, "Directory entry format count");
11657 if (DWARF5_USE_DEBUG_LINE_STR)
11658 str_form = DW_FORM_line_strp;
11659 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11660 dw2_asm_output_data_uleb128 (str_form, "%s",
11661 get_DW_FORM_name (str_form));
11662 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11663 if (str_form == DW_FORM_string)
11665 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11666 for (i = 1 - idx_offset; i < ndirs; i++)
11667 dw2_asm_output_nstring (dirs[i].path,
11668 dirs[i].length
11669 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11670 "Directory Entry: %#x", i + idx_offset);
11672 else
11674 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11675 for (i = 1 - idx_offset; i < ndirs; i++)
11677 const char *str
11678 = ggc_alloc_string (dirs[i].path,
11679 dirs[i].length
11680 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11681 output_line_string (str_form, str, "Directory Entry",
11682 (unsigned) i + idx_offset);
11686 else
11688 for (i = 1 - idx_offset; i < ndirs; i++)
11689 dw2_asm_output_nstring (dirs[i].path,
11690 dirs[i].length
11691 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11692 "Directory Entry: %#x", i + idx_offset);
11694 dw2_asm_output_data (1, 0, "End directory table");
11697 /* We have to emit them in the order of emitted_number since that's
11698 used in the debug info generation. To do this efficiently we
11699 generate a back-mapping of the indices first. */
11700 backmap = XALLOCAVEC (int, numfiles);
11701 for (i = 0; i < numfiles; i++)
11702 backmap[files[i].file_idx->emitted_number - 1] = i;
11704 if (dwarf_version >= 5)
11706 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11707 if (filename0 == NULL)
11708 filename0 = "";
11709 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11710 DW_FORM_data2. Choose one based on the number of directories
11711 and how much space would they occupy in each encoding.
11712 If we have at most 256 directories, all indexes fit into
11713 a single byte, so DW_FORM_data1 is most compact (if there
11714 are at most 128 directories, DW_FORM_udata would be as
11715 compact as that, but not shorter and slower to decode). */
11716 if (ndirs + idx_offset <= 256)
11717 idx_form = DW_FORM_data1;
11718 /* If there are more than 65536 directories, we have to use
11719 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11720 Otherwise, compute what space would occupy if all the indexes
11721 used DW_FORM_udata - sum - and compare that to how large would
11722 be DW_FORM_data2 encoding, and pick the more efficient one. */
11723 else if (ndirs + idx_offset <= 65536)
11725 unsigned HOST_WIDE_INT sum = 1;
11726 for (i = 0; i < numfiles; i++)
11728 int file_idx = backmap[i];
11729 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11730 sum += size_of_uleb128 (dir_idx);
11732 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11733 idx_form = DW_FORM_data2;
11735 #ifdef VMS_DEBUGGING_INFO
11736 dw2_asm_output_data (1, 4, "File name entry format count");
11737 #else
11738 dw2_asm_output_data (1, 2, "File name entry format count");
11739 #endif
11740 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11741 dw2_asm_output_data_uleb128 (str_form, "%s",
11742 get_DW_FORM_name (str_form));
11743 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11744 "DW_LNCT_directory_index");
11745 dw2_asm_output_data_uleb128 (idx_form, "%s",
11746 get_DW_FORM_name (idx_form));
11747 #ifdef VMS_DEBUGGING_INFO
11748 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11749 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11750 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11751 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11752 #endif
11753 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11755 output_line_string (str_form, filename0, "File Entry", 0);
11757 /* Include directory index. */
11758 if (idx_form != DW_FORM_udata)
11759 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11760 0, NULL);
11761 else
11762 dw2_asm_output_data_uleb128 (0, NULL);
11764 #ifdef VMS_DEBUGGING_INFO
11765 dw2_asm_output_data_uleb128 (0, NULL);
11766 dw2_asm_output_data_uleb128 (0, NULL);
11767 #endif
11770 /* Now write all the file names. */
11771 for (i = 0; i < numfiles; i++)
11773 int file_idx = backmap[i];
11774 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11776 #ifdef VMS_DEBUGGING_INFO
11777 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11779 /* Setting these fields can lead to debugger miscomparisons,
11780 but VMS Debug requires them to be set correctly. */
11782 int ver;
11783 long long cdt;
11784 long siz;
11785 int maxfilelen = (strlen (files[file_idx].path)
11786 + dirs[dir_idx].length
11787 + MAX_VMS_VERSION_LEN + 1);
11788 char *filebuf = XALLOCAVEC (char, maxfilelen);
11790 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11791 snprintf (filebuf, maxfilelen, "%s;%d",
11792 files[file_idx].path + dirs[dir_idx].length, ver);
11794 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11796 /* Include directory index. */
11797 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11798 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11799 dir_idx + idx_offset, NULL);
11800 else
11801 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11803 /* Modification time. */
11804 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11805 &cdt, 0, 0, 0) == 0)
11806 ? cdt : 0, NULL);
11808 /* File length in bytes. */
11809 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11810 0, &siz, 0, 0) == 0)
11811 ? siz : 0, NULL);
11812 #else
11813 output_line_string (str_form,
11814 files[file_idx].path + dirs[dir_idx].length,
11815 "File Entry", (unsigned) i + 1);
11817 /* Include directory index. */
11818 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11819 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11820 dir_idx + idx_offset, NULL);
11821 else
11822 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11824 if (dwarf_version >= 5)
11825 continue;
11827 /* Modification time. */
11828 dw2_asm_output_data_uleb128 (0, NULL);
11830 /* File length in bytes. */
11831 dw2_asm_output_data_uleb128 (0, NULL);
11832 #endif /* VMS_DEBUGGING_INFO */
11835 if (dwarf_version < 5)
11836 dw2_asm_output_data (1, 0, "End file name table");
11840 /* Output one line number table into the .debug_line section. */
11842 static void
11843 output_one_line_info_table (dw_line_info_table *table)
11845 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11846 unsigned int current_line = 1;
11847 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11848 dw_line_info_entry *ent;
11849 size_t i;
11851 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11853 switch (ent->opcode)
11855 case LI_set_address:
11856 /* ??? Unfortunately, we have little choice here currently, and
11857 must always use the most general form. GCC does not know the
11858 address delta itself, so we can't use DW_LNS_advance_pc. Many
11859 ports do have length attributes which will give an upper bound
11860 on the address range. We could perhaps use length attributes
11861 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11862 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11864 /* This can handle any delta. This takes
11865 4+DWARF2_ADDR_SIZE bytes. */
11866 dw2_asm_output_data (1, 0, "set address %s", line_label);
11867 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11868 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11869 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11870 break;
11872 case LI_set_line:
11873 if (ent->val == current_line)
11875 /* We still need to start a new row, so output a copy insn. */
11876 dw2_asm_output_data (1, DW_LNS_copy,
11877 "copy line %u", current_line);
11879 else
11881 int line_offset = ent->val - current_line;
11882 int line_delta = line_offset - DWARF_LINE_BASE;
11884 current_line = ent->val;
11885 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11887 /* This can handle deltas from -10 to 234, using the current
11888 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11889 This takes 1 byte. */
11890 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11891 "line %u", current_line);
11893 else
11895 /* This can handle any delta. This takes at least 4 bytes,
11896 depending on the value being encoded. */
11897 dw2_asm_output_data (1, DW_LNS_advance_line,
11898 "advance to line %u", current_line);
11899 dw2_asm_output_data_sleb128 (line_offset, NULL);
11900 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11903 break;
11905 case LI_set_file:
11906 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11907 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11908 break;
11910 case LI_set_column:
11911 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11912 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11913 break;
11915 case LI_negate_stmt:
11916 current_is_stmt = !current_is_stmt;
11917 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11918 "is_stmt %d", current_is_stmt);
11919 break;
11921 case LI_set_prologue_end:
11922 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11923 "set prologue end");
11924 break;
11926 case LI_set_epilogue_begin:
11927 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11928 "set epilogue begin");
11929 break;
11931 case LI_set_discriminator:
11932 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11933 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11934 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11935 dw2_asm_output_data_uleb128 (ent->val, NULL);
11936 break;
11940 /* Emit debug info for the address of the end of the table. */
11941 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11942 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11943 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11944 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11946 dw2_asm_output_data (1, 0, "end sequence");
11947 dw2_asm_output_data_uleb128 (1, NULL);
11948 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11951 /* Output the source line number correspondence information. This
11952 information goes into the .debug_line section. */
11954 static void
11955 output_line_info (bool prologue_only)
11957 static unsigned int generation;
11958 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11959 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11960 bool saw_one = false;
11961 int opc;
11963 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11964 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11965 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11966 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11968 if (!XCOFF_DEBUGGING_INFO)
11970 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11971 dw2_asm_output_data (4, 0xffffffff,
11972 "Initial length escape value indicating 64-bit DWARF extension");
11973 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11974 "Length of Source Line Info");
11977 ASM_OUTPUT_LABEL (asm_out_file, l1);
11979 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11980 if (dwarf_version >= 5)
11982 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11983 dw2_asm_output_data (1, 0, "Segment Size");
11985 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11986 ASM_OUTPUT_LABEL (asm_out_file, p1);
11988 /* Define the architecture-dependent minimum instruction length (in bytes).
11989 In this implementation of DWARF, this field is used for information
11990 purposes only. Since GCC generates assembly language, we have no
11991 a priori knowledge of how many instruction bytes are generated for each
11992 source line, and therefore can use only the DW_LNE_set_address and
11993 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
11994 this as '1', which is "correct enough" for all architectures,
11995 and don't let the target override. */
11996 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
11998 if (dwarf_version >= 4)
11999 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12000 "Maximum Operations Per Instruction");
12001 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12002 "Default is_stmt_start flag");
12003 dw2_asm_output_data (1, DWARF_LINE_BASE,
12004 "Line Base Value (Special Opcodes)");
12005 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12006 "Line Range Value (Special Opcodes)");
12007 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12008 "Special Opcode Base");
12010 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12012 int n_op_args;
12013 switch (opc)
12015 case DW_LNS_advance_pc:
12016 case DW_LNS_advance_line:
12017 case DW_LNS_set_file:
12018 case DW_LNS_set_column:
12019 case DW_LNS_fixed_advance_pc:
12020 case DW_LNS_set_isa:
12021 n_op_args = 1;
12022 break;
12023 default:
12024 n_op_args = 0;
12025 break;
12028 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12029 opc, n_op_args);
12032 /* Write out the information about the files we use. */
12033 output_file_names ();
12034 ASM_OUTPUT_LABEL (asm_out_file, p2);
12035 if (prologue_only)
12037 /* Output the marker for the end of the line number info. */
12038 ASM_OUTPUT_LABEL (asm_out_file, l2);
12039 return;
12042 if (separate_line_info)
12044 dw_line_info_table *table;
12045 size_t i;
12047 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12048 if (table->in_use)
12050 output_one_line_info_table (table);
12051 saw_one = true;
12054 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12056 output_one_line_info_table (cold_text_section_line_info);
12057 saw_one = true;
12060 /* ??? Some Darwin linkers crash on a .debug_line section with no
12061 sequences. Further, merely a DW_LNE_end_sequence entry is not
12062 sufficient -- the address column must also be initialized.
12063 Make sure to output at least one set_address/end_sequence pair,
12064 choosing .text since that section is always present. */
12065 if (text_section_line_info->in_use || !saw_one)
12066 output_one_line_info_table (text_section_line_info);
12068 /* Output the marker for the end of the line number info. */
12069 ASM_OUTPUT_LABEL (asm_out_file, l2);
12072 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12074 static inline bool
12075 need_endianity_attribute_p (bool reverse)
12077 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12080 /* Given a pointer to a tree node for some base type, return a pointer to
12081 a DIE that describes the given type. REVERSE is true if the type is
12082 to be interpreted in the reverse storage order wrt the target order.
12084 This routine must only be called for GCC type nodes that correspond to
12085 Dwarf base (fundamental) types. */
12087 static dw_die_ref
12088 base_type_die (tree type, bool reverse)
12090 dw_die_ref base_type_result;
12091 enum dwarf_type encoding;
12092 bool fpt_used = false;
12093 struct fixed_point_type_info fpt_info;
12094 tree type_bias = NULL_TREE;
12096 /* If this is a subtype that should not be emitted as a subrange type,
12097 use the base type. See subrange_type_for_debug_p. */
12098 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12099 type = TREE_TYPE (type);
12101 switch (TREE_CODE (type))
12103 case INTEGER_TYPE:
12104 if ((dwarf_version >= 4 || !dwarf_strict)
12105 && TYPE_NAME (type)
12106 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12107 && DECL_IS_BUILTIN (TYPE_NAME (type))
12108 && DECL_NAME (TYPE_NAME (type)))
12110 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12111 if (strcmp (name, "char16_t") == 0
12112 || strcmp (name, "char32_t") == 0)
12114 encoding = DW_ATE_UTF;
12115 break;
12118 if ((dwarf_version >= 3 || !dwarf_strict)
12119 && lang_hooks.types.get_fixed_point_type_info)
12121 memset (&fpt_info, 0, sizeof (fpt_info));
12122 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12124 fpt_used = true;
12125 encoding = ((TYPE_UNSIGNED (type))
12126 ? DW_ATE_unsigned_fixed
12127 : DW_ATE_signed_fixed);
12128 break;
12131 if (TYPE_STRING_FLAG (type))
12133 if (TYPE_UNSIGNED (type))
12134 encoding = DW_ATE_unsigned_char;
12135 else
12136 encoding = DW_ATE_signed_char;
12138 else if (TYPE_UNSIGNED (type))
12139 encoding = DW_ATE_unsigned;
12140 else
12141 encoding = DW_ATE_signed;
12143 if (!dwarf_strict
12144 && lang_hooks.types.get_type_bias)
12145 type_bias = lang_hooks.types.get_type_bias (type);
12146 break;
12148 case REAL_TYPE:
12149 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12151 if (dwarf_version >= 3 || !dwarf_strict)
12152 encoding = DW_ATE_decimal_float;
12153 else
12154 encoding = DW_ATE_lo_user;
12156 else
12157 encoding = DW_ATE_float;
12158 break;
12160 case FIXED_POINT_TYPE:
12161 if (!(dwarf_version >= 3 || !dwarf_strict))
12162 encoding = DW_ATE_lo_user;
12163 else if (TYPE_UNSIGNED (type))
12164 encoding = DW_ATE_unsigned_fixed;
12165 else
12166 encoding = DW_ATE_signed_fixed;
12167 break;
12169 /* Dwarf2 doesn't know anything about complex ints, so use
12170 a user defined type for it. */
12171 case COMPLEX_TYPE:
12172 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12173 encoding = DW_ATE_complex_float;
12174 else
12175 encoding = DW_ATE_lo_user;
12176 break;
12178 case BOOLEAN_TYPE:
12179 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12180 encoding = DW_ATE_boolean;
12181 break;
12183 default:
12184 /* No other TREE_CODEs are Dwarf fundamental types. */
12185 gcc_unreachable ();
12188 base_type_result = new_die_raw (DW_TAG_base_type);
12190 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12191 int_size_in_bytes (type));
12192 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12194 if (need_endianity_attribute_p (reverse))
12195 add_AT_unsigned (base_type_result, DW_AT_endianity,
12196 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12198 add_alignment_attribute (base_type_result, type);
12200 if (fpt_used)
12202 switch (fpt_info.scale_factor_kind)
12204 case fixed_point_scale_factor_binary:
12205 add_AT_int (base_type_result, DW_AT_binary_scale,
12206 fpt_info.scale_factor.binary);
12207 break;
12209 case fixed_point_scale_factor_decimal:
12210 add_AT_int (base_type_result, DW_AT_decimal_scale,
12211 fpt_info.scale_factor.decimal);
12212 break;
12214 case fixed_point_scale_factor_arbitrary:
12215 /* Arbitrary scale factors cannot be described in standard DWARF,
12216 yet. */
12217 if (!dwarf_strict)
12219 /* Describe the scale factor as a rational constant. */
12220 const dw_die_ref scale_factor
12221 = new_die (DW_TAG_constant, comp_unit_die (), type);
12223 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12224 fpt_info.scale_factor.arbitrary.numerator);
12225 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12226 fpt_info.scale_factor.arbitrary.denominator);
12228 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12230 break;
12232 default:
12233 gcc_unreachable ();
12237 if (type_bias)
12238 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12239 dw_scalar_form_constant
12240 | dw_scalar_form_exprloc
12241 | dw_scalar_form_reference,
12242 NULL);
12244 return base_type_result;
12247 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12248 named 'auto' in its type: return true for it, false otherwise. */
12250 static inline bool
12251 is_cxx_auto (tree type)
12253 if (is_cxx ())
12255 tree name = TYPE_IDENTIFIER (type);
12256 if (name == get_identifier ("auto")
12257 || name == get_identifier ("decltype(auto)"))
12258 return true;
12260 return false;
12263 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12264 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12266 static inline int
12267 is_base_type (tree type)
12269 switch (TREE_CODE (type))
12271 case INTEGER_TYPE:
12272 case REAL_TYPE:
12273 case FIXED_POINT_TYPE:
12274 case COMPLEX_TYPE:
12275 case BOOLEAN_TYPE:
12276 case POINTER_BOUNDS_TYPE:
12277 return 1;
12279 case VOID_TYPE:
12280 case ARRAY_TYPE:
12281 case RECORD_TYPE:
12282 case UNION_TYPE:
12283 case QUAL_UNION_TYPE:
12284 case ENUMERAL_TYPE:
12285 case FUNCTION_TYPE:
12286 case METHOD_TYPE:
12287 case POINTER_TYPE:
12288 case REFERENCE_TYPE:
12289 case NULLPTR_TYPE:
12290 case OFFSET_TYPE:
12291 case LANG_TYPE:
12292 case VECTOR_TYPE:
12293 return 0;
12295 default:
12296 if (is_cxx_auto (type))
12297 return 0;
12298 gcc_unreachable ();
12301 return 0;
12304 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12305 node, return the size in bits for the type if it is a constant, or else
12306 return the alignment for the type if the type's size is not constant, or
12307 else return BITS_PER_WORD if the type actually turns out to be an
12308 ERROR_MARK node. */
12310 static inline unsigned HOST_WIDE_INT
12311 simple_type_size_in_bits (const_tree type)
12313 if (TREE_CODE (type) == ERROR_MARK)
12314 return BITS_PER_WORD;
12315 else if (TYPE_SIZE (type) == NULL_TREE)
12316 return 0;
12317 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12318 return tree_to_uhwi (TYPE_SIZE (type));
12319 else
12320 return TYPE_ALIGN (type);
12323 /* Similarly, but return an offset_int instead of UHWI. */
12325 static inline offset_int
12326 offset_int_type_size_in_bits (const_tree type)
12328 if (TREE_CODE (type) == ERROR_MARK)
12329 return BITS_PER_WORD;
12330 else if (TYPE_SIZE (type) == NULL_TREE)
12331 return 0;
12332 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12333 return wi::to_offset (TYPE_SIZE (type));
12334 else
12335 return TYPE_ALIGN (type);
12338 /* Given a pointer to a tree node for a subrange type, return a pointer
12339 to a DIE that describes the given type. */
12341 static dw_die_ref
12342 subrange_type_die (tree type, tree low, tree high, tree bias,
12343 dw_die_ref context_die)
12345 dw_die_ref subrange_die;
12346 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12348 if (context_die == NULL)
12349 context_die = comp_unit_die ();
12351 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12353 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12355 /* The size of the subrange type and its base type do not match,
12356 so we need to generate a size attribute for the subrange type. */
12357 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12360 add_alignment_attribute (subrange_die, type);
12362 if (low)
12363 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12364 if (high)
12365 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12366 if (bias && !dwarf_strict)
12367 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12368 dw_scalar_form_constant
12369 | dw_scalar_form_exprloc
12370 | dw_scalar_form_reference,
12371 NULL);
12373 return subrange_die;
12376 /* Returns the (const and/or volatile) cv_qualifiers associated with
12377 the decl node. This will normally be augmented with the
12378 cv_qualifiers of the underlying type in add_type_attribute. */
12380 static int
12381 decl_quals (const_tree decl)
12383 return ((TREE_READONLY (decl)
12384 /* The C++ front-end correctly marks reference-typed
12385 variables as readonly, but from a language (and debug
12386 info) standpoint they are not const-qualified. */
12387 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12388 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12389 | (TREE_THIS_VOLATILE (decl)
12390 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12393 /* Determine the TYPE whose qualifiers match the largest strict subset
12394 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12395 qualifiers outside QUAL_MASK. */
12397 static int
12398 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12400 tree t;
12401 int best_rank = 0, best_qual = 0, max_rank;
12403 type_quals &= qual_mask;
12404 max_rank = popcount_hwi (type_quals) - 1;
12406 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12407 t = TYPE_NEXT_VARIANT (t))
12409 int q = TYPE_QUALS (t) & qual_mask;
12411 if ((q & type_quals) == q && q != type_quals
12412 && check_base_type (t, type))
12414 int rank = popcount_hwi (q);
12416 if (rank > best_rank)
12418 best_rank = rank;
12419 best_qual = q;
12424 return best_qual;
12427 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12428 static const dwarf_qual_info_t dwarf_qual_info[] =
12430 { TYPE_QUAL_CONST, DW_TAG_const_type },
12431 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12432 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12433 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12435 static const unsigned int dwarf_qual_info_size
12436 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12438 /* If DIE is a qualified DIE of some base DIE with the same parent,
12439 return the base DIE, otherwise return NULL. Set MASK to the
12440 qualifiers added compared to the returned DIE. */
12442 static dw_die_ref
12443 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12445 unsigned int i;
12446 for (i = 0; i < dwarf_qual_info_size; i++)
12447 if (die->die_tag == dwarf_qual_info[i].t)
12448 break;
12449 if (i == dwarf_qual_info_size)
12450 return NULL;
12451 if (vec_safe_length (die->die_attr) != 1)
12452 return NULL;
12453 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12454 if (type == NULL || type->die_parent != die->die_parent)
12455 return NULL;
12456 *mask |= dwarf_qual_info[i].q;
12457 if (depth)
12459 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12460 if (ret)
12461 return ret;
12463 return type;
12466 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12467 entry that chains the modifiers specified by CV_QUALS in front of the
12468 given type. REVERSE is true if the type is to be interpreted in the
12469 reverse storage order wrt the target order. */
12471 static dw_die_ref
12472 modified_type_die (tree type, int cv_quals, bool reverse,
12473 dw_die_ref context_die)
12475 enum tree_code code = TREE_CODE (type);
12476 dw_die_ref mod_type_die;
12477 dw_die_ref sub_die = NULL;
12478 tree item_type = NULL;
12479 tree qualified_type;
12480 tree name, low, high;
12481 dw_die_ref mod_scope;
12482 /* Only these cv-qualifiers are currently handled. */
12483 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12484 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12485 ENCODE_QUAL_ADDR_SPACE(~0U));
12486 const bool reverse_base_type
12487 = need_endianity_attribute_p (reverse) && is_base_type (type);
12489 if (code == ERROR_MARK)
12490 return NULL;
12492 if (lang_hooks.types.get_debug_type)
12494 tree debug_type = lang_hooks.types.get_debug_type (type);
12496 if (debug_type != NULL_TREE && debug_type != type)
12497 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12500 cv_quals &= cv_qual_mask;
12502 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12503 tag modifier (and not an attribute) old consumers won't be able
12504 to handle it. */
12505 if (dwarf_version < 3)
12506 cv_quals &= ~TYPE_QUAL_RESTRICT;
12508 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12509 if (dwarf_version < 5)
12510 cv_quals &= ~TYPE_QUAL_ATOMIC;
12512 /* See if we already have the appropriately qualified variant of
12513 this type. */
12514 qualified_type = get_qualified_type (type, cv_quals);
12516 if (qualified_type == sizetype)
12518 /* Try not to expose the internal sizetype type's name. */
12519 if (TYPE_NAME (qualified_type)
12520 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12522 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12524 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12525 && (TYPE_PRECISION (t)
12526 == TYPE_PRECISION (qualified_type))
12527 && (TYPE_UNSIGNED (t)
12528 == TYPE_UNSIGNED (qualified_type)));
12529 qualified_type = t;
12531 else if (qualified_type == sizetype
12532 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12533 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12534 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12535 qualified_type = size_type_node;
12538 /* If we do, then we can just use its DIE, if it exists. */
12539 if (qualified_type)
12541 mod_type_die = lookup_type_die (qualified_type);
12543 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12544 dealt with specially: the DIE with the attribute, if it exists, is
12545 placed immediately after the regular DIE for the same base type. */
12546 if (mod_type_die
12547 && (!reverse_base_type
12548 || ((mod_type_die = mod_type_die->die_sib) != NULL
12549 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12550 return mod_type_die;
12553 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12555 /* Handle C typedef types. */
12556 if (name
12557 && TREE_CODE (name) == TYPE_DECL
12558 && DECL_ORIGINAL_TYPE (name)
12559 && !DECL_ARTIFICIAL (name))
12561 tree dtype = TREE_TYPE (name);
12563 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12564 if (qualified_type == dtype && !reverse_base_type)
12566 tree origin = decl_ultimate_origin (name);
12568 /* Typedef variants that have an abstract origin don't get their own
12569 type DIE (see gen_typedef_die), so fall back on the ultimate
12570 abstract origin instead. */
12571 if (origin != NULL && origin != name)
12572 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12573 context_die);
12575 /* For a named type, use the typedef. */
12576 gen_type_die (qualified_type, context_die);
12577 return lookup_type_die (qualified_type);
12579 else
12581 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12582 dquals &= cv_qual_mask;
12583 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12584 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12585 /* cv-unqualified version of named type. Just use
12586 the unnamed type to which it refers. */
12587 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12588 reverse, context_die);
12589 /* Else cv-qualified version of named type; fall through. */
12593 mod_scope = scope_die_for (type, context_die);
12595 if (cv_quals)
12597 int sub_quals = 0, first_quals = 0;
12598 unsigned i;
12599 dw_die_ref first = NULL, last = NULL;
12601 /* Determine a lesser qualified type that most closely matches
12602 this one. Then generate DW_TAG_* entries for the remaining
12603 qualifiers. */
12604 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12605 cv_qual_mask);
12606 if (sub_quals && use_debug_types)
12608 bool needed = false;
12609 /* If emitting type units, make sure the order of qualifiers
12610 is canonical. Thus, start from unqualified type if
12611 an earlier qualifier is missing in sub_quals, but some later
12612 one is present there. */
12613 for (i = 0; i < dwarf_qual_info_size; i++)
12614 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12615 needed = true;
12616 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12618 sub_quals = 0;
12619 break;
12622 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12623 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12625 /* As not all intermediate qualified DIEs have corresponding
12626 tree types, ensure that qualified DIEs in the same scope
12627 as their DW_AT_type are emitted after their DW_AT_type,
12628 only with other qualified DIEs for the same type possibly
12629 in between them. Determine the range of such qualified
12630 DIEs now (first being the base type, last being corresponding
12631 last qualified DIE for it). */
12632 unsigned int count = 0;
12633 first = qualified_die_p (mod_type_die, &first_quals,
12634 dwarf_qual_info_size);
12635 if (first == NULL)
12636 first = mod_type_die;
12637 gcc_assert ((first_quals & ~sub_quals) == 0);
12638 for (count = 0, last = first;
12639 count < (1U << dwarf_qual_info_size);
12640 count++, last = last->die_sib)
12642 int quals = 0;
12643 if (last == mod_scope->die_child)
12644 break;
12645 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12646 != first)
12647 break;
12651 for (i = 0; i < dwarf_qual_info_size; i++)
12652 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12654 dw_die_ref d;
12655 if (first && first != last)
12657 for (d = first->die_sib; ; d = d->die_sib)
12659 int quals = 0;
12660 qualified_die_p (d, &quals, dwarf_qual_info_size);
12661 if (quals == (first_quals | dwarf_qual_info[i].q))
12662 break;
12663 if (d == last)
12665 d = NULL;
12666 break;
12669 if (d)
12671 mod_type_die = d;
12672 continue;
12675 if (first)
12677 d = new_die_raw (dwarf_qual_info[i].t);
12678 add_child_die_after (mod_scope, d, last);
12679 last = d;
12681 else
12682 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12683 if (mod_type_die)
12684 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12685 mod_type_die = d;
12686 first_quals |= dwarf_qual_info[i].q;
12689 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12691 dwarf_tag tag = DW_TAG_pointer_type;
12692 if (code == REFERENCE_TYPE)
12694 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12695 tag = DW_TAG_rvalue_reference_type;
12696 else
12697 tag = DW_TAG_reference_type;
12699 mod_type_die = new_die (tag, mod_scope, type);
12701 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12702 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12703 add_alignment_attribute (mod_type_die, type);
12704 item_type = TREE_TYPE (type);
12706 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12707 if (!ADDR_SPACE_GENERIC_P (as))
12709 int action = targetm.addr_space.debug (as);
12710 if (action >= 0)
12712 /* Positive values indicate an address_class. */
12713 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12715 else
12717 /* Negative values indicate an (inverted) segment base reg. */
12718 dw_loc_descr_ref d
12719 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12720 add_AT_loc (mod_type_die, DW_AT_segment, d);
12724 else if (code == INTEGER_TYPE
12725 && TREE_TYPE (type) != NULL_TREE
12726 && subrange_type_for_debug_p (type, &low, &high))
12728 tree bias = NULL_TREE;
12729 if (lang_hooks.types.get_type_bias)
12730 bias = lang_hooks.types.get_type_bias (type);
12731 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12732 item_type = TREE_TYPE (type);
12734 else if (is_base_type (type))
12736 mod_type_die = base_type_die (type, reverse);
12738 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12739 if (reverse_base_type)
12741 dw_die_ref after_die
12742 = modified_type_die (type, cv_quals, false, context_die);
12743 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12745 else
12746 add_child_die (comp_unit_die (), mod_type_die);
12748 add_pubtype (type, mod_type_die);
12750 else
12752 gen_type_die (type, context_die);
12754 /* We have to get the type_main_variant here (and pass that to the
12755 `lookup_type_die' routine) because the ..._TYPE node we have
12756 might simply be a *copy* of some original type node (where the
12757 copy was created to help us keep track of typedef names) and
12758 that copy might have a different TYPE_UID from the original
12759 ..._TYPE node. */
12760 if (TREE_CODE (type) == FUNCTION_TYPE
12761 || TREE_CODE (type) == METHOD_TYPE)
12763 /* For function/method types, can't just use type_main_variant here,
12764 because that can have different ref-qualifiers for C++,
12765 but try to canonicalize. */
12766 tree main = TYPE_MAIN_VARIANT (type);
12767 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12768 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12769 && check_base_type (t, main)
12770 && check_lang_type (t, type))
12771 return lookup_type_die (t);
12772 return lookup_type_die (type);
12774 else if (TREE_CODE (type) != VECTOR_TYPE
12775 && TREE_CODE (type) != ARRAY_TYPE)
12776 return lookup_type_die (type_main_variant (type));
12777 else
12778 /* Vectors have the debugging information in the type,
12779 not the main variant. */
12780 return lookup_type_die (type);
12783 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12784 don't output a DW_TAG_typedef, since there isn't one in the
12785 user's program; just attach a DW_AT_name to the type.
12786 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12787 if the base type already has the same name. */
12788 if (name
12789 && ((TREE_CODE (name) != TYPE_DECL
12790 && (qualified_type == TYPE_MAIN_VARIANT (type)
12791 || (cv_quals == TYPE_UNQUALIFIED)))
12792 || (TREE_CODE (name) == TYPE_DECL
12793 && TREE_TYPE (name) == qualified_type
12794 && DECL_NAME (name))))
12796 if (TREE_CODE (name) == TYPE_DECL)
12797 /* Could just call add_name_and_src_coords_attributes here,
12798 but since this is a builtin type it doesn't have any
12799 useful source coordinates anyway. */
12800 name = DECL_NAME (name);
12801 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12803 /* This probably indicates a bug. */
12804 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12806 name = TYPE_IDENTIFIER (type);
12807 add_name_attribute (mod_type_die,
12808 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12811 if (qualified_type && !reverse_base_type)
12812 equate_type_number_to_die (qualified_type, mod_type_die);
12814 if (item_type)
12815 /* We must do this after the equate_type_number_to_die call, in case
12816 this is a recursive type. This ensures that the modified_type_die
12817 recursion will terminate even if the type is recursive. Recursive
12818 types are possible in Ada. */
12819 sub_die = modified_type_die (item_type,
12820 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12821 reverse,
12822 context_die);
12824 if (sub_die != NULL)
12825 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12827 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12828 if (TYPE_ARTIFICIAL (type))
12829 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12831 return mod_type_die;
12834 /* Generate DIEs for the generic parameters of T.
12835 T must be either a generic type or a generic function.
12836 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12838 static void
12839 gen_generic_params_dies (tree t)
12841 tree parms, args;
12842 int parms_num, i;
12843 dw_die_ref die = NULL;
12844 int non_default;
12846 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12847 return;
12849 if (TYPE_P (t))
12850 die = lookup_type_die (t);
12851 else if (DECL_P (t))
12852 die = lookup_decl_die (t);
12854 gcc_assert (die);
12856 parms = lang_hooks.get_innermost_generic_parms (t);
12857 if (!parms)
12858 /* T has no generic parameter. It means T is neither a generic type
12859 or function. End of story. */
12860 return;
12862 parms_num = TREE_VEC_LENGTH (parms);
12863 args = lang_hooks.get_innermost_generic_args (t);
12864 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12865 non_default = int_cst_value (TREE_CHAIN (args));
12866 else
12867 non_default = TREE_VEC_LENGTH (args);
12868 for (i = 0; i < parms_num; i++)
12870 tree parm, arg, arg_pack_elems;
12871 dw_die_ref parm_die;
12873 parm = TREE_VEC_ELT (parms, i);
12874 arg = TREE_VEC_ELT (args, i);
12875 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12876 gcc_assert (parm && TREE_VALUE (parm) && arg);
12878 if (parm && TREE_VALUE (parm) && arg)
12880 /* If PARM represents a template parameter pack,
12881 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12882 by DW_TAG_template_*_parameter DIEs for the argument
12883 pack elements of ARG. Note that ARG would then be
12884 an argument pack. */
12885 if (arg_pack_elems)
12886 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12887 arg_pack_elems,
12888 die);
12889 else
12890 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12891 true /* emit name */, die);
12892 if (i >= non_default)
12893 add_AT_flag (parm_die, DW_AT_default_value, 1);
12898 /* Create and return a DIE for PARM which should be
12899 the representation of a generic type parameter.
12900 For instance, in the C++ front end, PARM would be a template parameter.
12901 ARG is the argument to PARM.
12902 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12903 name of the PARM.
12904 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12905 as a child node. */
12907 static dw_die_ref
12908 generic_parameter_die (tree parm, tree arg,
12909 bool emit_name_p,
12910 dw_die_ref parent_die)
12912 dw_die_ref tmpl_die = NULL;
12913 const char *name = NULL;
12915 if (!parm || !DECL_NAME (parm) || !arg)
12916 return NULL;
12918 /* We support non-type generic parameters and arguments,
12919 type generic parameters and arguments, as well as
12920 generic generic parameters (a.k.a. template template parameters in C++)
12921 and arguments. */
12922 if (TREE_CODE (parm) == PARM_DECL)
12923 /* PARM is a nontype generic parameter */
12924 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12925 else if (TREE_CODE (parm) == TYPE_DECL)
12926 /* PARM is a type generic parameter. */
12927 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12928 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12929 /* PARM is a generic generic parameter.
12930 Its DIE is a GNU extension. It shall have a
12931 DW_AT_name attribute to represent the name of the template template
12932 parameter, and a DW_AT_GNU_template_name attribute to represent the
12933 name of the template template argument. */
12934 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12935 parent_die, parm);
12936 else
12937 gcc_unreachable ();
12939 if (tmpl_die)
12941 tree tmpl_type;
12943 /* If PARM is a generic parameter pack, it means we are
12944 emitting debug info for a template argument pack element.
12945 In other terms, ARG is a template argument pack element.
12946 In that case, we don't emit any DW_AT_name attribute for
12947 the die. */
12948 if (emit_name_p)
12950 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12951 gcc_assert (name);
12952 add_AT_string (tmpl_die, DW_AT_name, name);
12955 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12957 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12958 TMPL_DIE should have a child DW_AT_type attribute that is set
12959 to the type of the argument to PARM, which is ARG.
12960 If PARM is a type generic parameter, TMPL_DIE should have a
12961 child DW_AT_type that is set to ARG. */
12962 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12963 add_type_attribute (tmpl_die, tmpl_type,
12964 (TREE_THIS_VOLATILE (tmpl_type)
12965 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12966 false, parent_die);
12968 else
12970 /* So TMPL_DIE is a DIE representing a
12971 a generic generic template parameter, a.k.a template template
12972 parameter in C++ and arg is a template. */
12974 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12975 to the name of the argument. */
12976 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12977 if (name)
12978 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12981 if (TREE_CODE (parm) == PARM_DECL)
12982 /* So PARM is a non-type generic parameter.
12983 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12984 attribute of TMPL_DIE which value represents the value
12985 of ARG.
12986 We must be careful here:
12987 The value of ARG might reference some function decls.
12988 We might currently be emitting debug info for a generic
12989 type and types are emitted before function decls, we don't
12990 know if the function decls referenced by ARG will actually be
12991 emitted after cgraph computations.
12992 So must defer the generation of the DW_AT_const_value to
12993 after cgraph is ready. */
12994 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12997 return tmpl_die;
13000 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13001 PARM_PACK must be a template parameter pack. The returned DIE
13002 will be child DIE of PARENT_DIE. */
13004 static dw_die_ref
13005 template_parameter_pack_die (tree parm_pack,
13006 tree parm_pack_args,
13007 dw_die_ref parent_die)
13009 dw_die_ref die;
13010 int j;
13012 gcc_assert (parent_die && parm_pack);
13014 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13015 add_name_and_src_coords_attributes (die, parm_pack);
13016 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13017 generic_parameter_die (parm_pack,
13018 TREE_VEC_ELT (parm_pack_args, j),
13019 false /* Don't emit DW_AT_name */,
13020 die);
13021 return die;
13024 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13025 an enumerated type. */
13027 static inline int
13028 type_is_enum (const_tree type)
13030 return TREE_CODE (type) == ENUMERAL_TYPE;
13033 /* Return the DBX register number described by a given RTL node. */
13035 static unsigned int
13036 dbx_reg_number (const_rtx rtl)
13038 unsigned regno = REGNO (rtl);
13040 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13042 #ifdef LEAF_REG_REMAP
13043 if (crtl->uses_only_leaf_regs)
13045 int leaf_reg = LEAF_REG_REMAP (regno);
13046 if (leaf_reg != -1)
13047 regno = (unsigned) leaf_reg;
13049 #endif
13051 regno = DBX_REGISTER_NUMBER (regno);
13052 gcc_assert (regno != INVALID_REGNUM);
13053 return regno;
13056 /* Optionally add a DW_OP_piece term to a location description expression.
13057 DW_OP_piece is only added if the location description expression already
13058 doesn't end with DW_OP_piece. */
13060 static void
13061 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13063 dw_loc_descr_ref loc;
13065 if (*list_head != NULL)
13067 /* Find the end of the chain. */
13068 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13071 if (loc->dw_loc_opc != DW_OP_piece)
13072 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13076 /* Return a location descriptor that designates a machine register or
13077 zero if there is none. */
13079 static dw_loc_descr_ref
13080 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13082 rtx regs;
13084 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13085 return 0;
13087 /* We only use "frame base" when we're sure we're talking about the
13088 post-prologue local stack frame. We do this by *not* running
13089 register elimination until this point, and recognizing the special
13090 argument pointer and soft frame pointer rtx's.
13091 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13092 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13093 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13095 dw_loc_descr_ref result = NULL;
13097 if (dwarf_version >= 4 || !dwarf_strict)
13099 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13100 initialized);
13101 if (result)
13102 add_loc_descr (&result,
13103 new_loc_descr (DW_OP_stack_value, 0, 0));
13105 return result;
13108 regs = targetm.dwarf_register_span (rtl);
13110 if (REG_NREGS (rtl) > 1 || regs)
13111 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13112 else
13114 unsigned int dbx_regnum = dbx_reg_number (rtl);
13115 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13116 return 0;
13117 return one_reg_loc_descriptor (dbx_regnum, initialized);
13121 /* Return a location descriptor that designates a machine register for
13122 a given hard register number. */
13124 static dw_loc_descr_ref
13125 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13127 dw_loc_descr_ref reg_loc_descr;
13129 if (regno <= 31)
13130 reg_loc_descr
13131 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13132 else
13133 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13135 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13136 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13138 return reg_loc_descr;
13141 /* Given an RTL of a register, return a location descriptor that
13142 designates a value that spans more than one register. */
13144 static dw_loc_descr_ref
13145 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13146 enum var_init_status initialized)
13148 int size, i;
13149 dw_loc_descr_ref loc_result = NULL;
13151 /* Simple, contiguous registers. */
13152 if (regs == NULL_RTX)
13154 unsigned reg = REGNO (rtl);
13155 int nregs;
13157 #ifdef LEAF_REG_REMAP
13158 if (crtl->uses_only_leaf_regs)
13160 int leaf_reg = LEAF_REG_REMAP (reg);
13161 if (leaf_reg != -1)
13162 reg = (unsigned) leaf_reg;
13164 #endif
13166 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13167 nregs = REG_NREGS (rtl);
13169 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13171 loc_result = NULL;
13172 while (nregs--)
13174 dw_loc_descr_ref t;
13176 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13177 VAR_INIT_STATUS_INITIALIZED);
13178 add_loc_descr (&loc_result, t);
13179 add_loc_descr_op_piece (&loc_result, size);
13180 ++reg;
13182 return loc_result;
13185 /* Now onto stupid register sets in non contiguous locations. */
13187 gcc_assert (GET_CODE (regs) == PARALLEL);
13189 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13190 loc_result = NULL;
13192 for (i = 0; i < XVECLEN (regs, 0); ++i)
13194 dw_loc_descr_ref t;
13196 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13197 VAR_INIT_STATUS_INITIALIZED);
13198 add_loc_descr (&loc_result, t);
13199 add_loc_descr_op_piece (&loc_result, size);
13202 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13203 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13204 return loc_result;
13207 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13209 /* Return a location descriptor that designates a constant i,
13210 as a compound operation from constant (i >> shift), constant shift
13211 and DW_OP_shl. */
13213 static dw_loc_descr_ref
13214 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13216 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13217 add_loc_descr (&ret, int_loc_descriptor (shift));
13218 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13219 return ret;
13222 /* Return a location descriptor that designates a constant. */
13224 static dw_loc_descr_ref
13225 int_loc_descriptor (HOST_WIDE_INT i)
13227 enum dwarf_location_atom op;
13229 /* Pick the smallest representation of a constant, rather than just
13230 defaulting to the LEB encoding. */
13231 if (i >= 0)
13233 int clz = clz_hwi (i);
13234 int ctz = ctz_hwi (i);
13235 if (i <= 31)
13236 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13237 else if (i <= 0xff)
13238 op = DW_OP_const1u;
13239 else if (i <= 0xffff)
13240 op = DW_OP_const2u;
13241 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13242 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13243 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13244 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13245 while DW_OP_const4u is 5 bytes. */
13246 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13247 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13248 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13249 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13250 while DW_OP_const4u is 5 bytes. */
13251 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13253 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13254 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13255 <= 4)
13257 /* As i >= 2**31, the double cast above will yield a negative number.
13258 Since wrapping is defined in DWARF expressions we can output big
13259 positive integers as small negative ones, regardless of the size
13260 of host wide ints.
13262 Here, since the evaluator will handle 32-bit values and since i >=
13263 2**31, we know it's going to be interpreted as a negative literal:
13264 store it this way if we can do better than 5 bytes this way. */
13265 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13267 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13268 op = DW_OP_const4u;
13270 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13271 least 6 bytes: see if we can do better before falling back to it. */
13272 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13273 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13274 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13275 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13276 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13277 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13278 >= HOST_BITS_PER_WIDE_INT)
13279 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13280 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13281 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13282 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13283 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13284 && size_of_uleb128 (i) > 6)
13285 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13286 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13287 else
13288 op = DW_OP_constu;
13290 else
13292 if (i >= -0x80)
13293 op = DW_OP_const1s;
13294 else if (i >= -0x8000)
13295 op = DW_OP_const2s;
13296 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13298 if (size_of_int_loc_descriptor (i) < 5)
13300 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13301 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13302 return ret;
13304 op = DW_OP_const4s;
13306 else
13308 if (size_of_int_loc_descriptor (i)
13309 < (unsigned long) 1 + size_of_sleb128 (i))
13311 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13312 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13313 return ret;
13315 op = DW_OP_consts;
13319 return new_loc_descr (op, i, 0);
13322 /* Likewise, for unsigned constants. */
13324 static dw_loc_descr_ref
13325 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13327 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13328 const unsigned HOST_WIDE_INT max_uint
13329 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13331 /* If possible, use the clever signed constants handling. */
13332 if (i <= max_int)
13333 return int_loc_descriptor ((HOST_WIDE_INT) i);
13335 /* Here, we are left with positive numbers that cannot be represented as
13336 HOST_WIDE_INT, i.e.:
13337 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13339 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13340 whereas may be better to output a negative integer: thanks to integer
13341 wrapping, we know that:
13342 x = x - 2 ** DWARF2_ADDR_SIZE
13343 = x - 2 * (max (HOST_WIDE_INT) + 1)
13344 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13345 small negative integers. Let's try that in cases it will clearly improve
13346 the encoding: there is no gain turning DW_OP_const4u into
13347 DW_OP_const4s. */
13348 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13349 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13350 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13352 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13354 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13355 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13356 const HOST_WIDE_INT second_shift
13357 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13359 /* So we finally have:
13360 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13361 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13362 return int_loc_descriptor (second_shift);
13365 /* Last chance: fallback to a simple constant operation. */
13366 return new_loc_descr
13367 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13368 ? DW_OP_const4u
13369 : DW_OP_const8u,
13370 i, 0);
13373 /* Generate and return a location description that computes the unsigned
13374 comparison of the two stack top entries (a OP b where b is the top-most
13375 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13376 LE_EXPR, GT_EXPR or GE_EXPR. */
13378 static dw_loc_descr_ref
13379 uint_comparison_loc_list (enum tree_code kind)
13381 enum dwarf_location_atom op, flip_op;
13382 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13384 switch (kind)
13386 case LT_EXPR:
13387 op = DW_OP_lt;
13388 break;
13389 case LE_EXPR:
13390 op = DW_OP_le;
13391 break;
13392 case GT_EXPR:
13393 op = DW_OP_gt;
13394 break;
13395 case GE_EXPR:
13396 op = DW_OP_ge;
13397 break;
13398 default:
13399 gcc_unreachable ();
13402 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13403 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13405 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13406 possible to perform unsigned comparisons: we just have to distinguish
13407 three cases:
13409 1. when a and b have the same sign (as signed integers); then we should
13410 return: a OP(signed) b;
13412 2. when a is a negative signed integer while b is a positive one, then a
13413 is a greater unsigned integer than b; likewise when a and b's roles
13414 are flipped.
13416 So first, compare the sign of the two operands. */
13417 ret = new_loc_descr (DW_OP_over, 0, 0);
13418 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13419 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13420 /* If they have different signs (i.e. they have different sign bits), then
13421 the stack top value has now the sign bit set and thus it's smaller than
13422 zero. */
13423 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13424 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13425 add_loc_descr (&ret, bra_node);
13427 /* We are in case 1. At this point, we know both operands have the same
13428 sign, to it's safe to use the built-in signed comparison. */
13429 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13430 add_loc_descr (&ret, jmp_node);
13432 /* We are in case 2. Here, we know both operands do not have the same sign,
13433 so we have to flip the signed comparison. */
13434 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13435 tmp = new_loc_descr (flip_op, 0, 0);
13436 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13437 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13438 add_loc_descr (&ret, tmp);
13440 /* This dummy operation is necessary to make the two branches join. */
13441 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13442 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13443 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13444 add_loc_descr (&ret, tmp);
13446 return ret;
13449 /* Likewise, but takes the location description lists (might be destructive on
13450 them). Return NULL if either is NULL or if concatenation fails. */
13452 static dw_loc_list_ref
13453 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13454 enum tree_code kind)
13456 if (left == NULL || right == NULL)
13457 return NULL;
13459 add_loc_list (&left, right);
13460 if (left == NULL)
13461 return NULL;
13463 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13464 return left;
13467 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13468 without actually allocating it. */
13470 static unsigned long
13471 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13473 return size_of_int_loc_descriptor (i >> shift)
13474 + size_of_int_loc_descriptor (shift)
13475 + 1;
13478 /* Return size_of_locs (int_loc_descriptor (i)) without
13479 actually allocating it. */
13481 static unsigned long
13482 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13484 unsigned long s;
13486 if (i >= 0)
13488 int clz, ctz;
13489 if (i <= 31)
13490 return 1;
13491 else if (i <= 0xff)
13492 return 2;
13493 else if (i <= 0xffff)
13494 return 3;
13495 clz = clz_hwi (i);
13496 ctz = ctz_hwi (i);
13497 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13498 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13499 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13500 - clz - 5);
13501 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13502 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13503 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13504 - clz - 8);
13505 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13506 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13507 <= 4)
13508 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13509 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13510 return 5;
13511 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13512 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13513 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13514 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13515 - clz - 8);
13516 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13517 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13518 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13519 - clz - 16);
13520 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13521 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13522 && s > 6)
13523 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13524 - clz - 32);
13525 else
13526 return 1 + s;
13528 else
13530 if (i >= -0x80)
13531 return 2;
13532 else if (i >= -0x8000)
13533 return 3;
13534 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13536 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13538 s = size_of_int_loc_descriptor (-i) + 1;
13539 if (s < 5)
13540 return s;
13542 return 5;
13544 else
13546 unsigned long r = 1 + size_of_sleb128 (i);
13547 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13549 s = size_of_int_loc_descriptor (-i) + 1;
13550 if (s < r)
13551 return s;
13553 return r;
13558 /* Return loc description representing "address" of integer value.
13559 This can appear only as toplevel expression. */
13561 static dw_loc_descr_ref
13562 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13564 int litsize;
13565 dw_loc_descr_ref loc_result = NULL;
13567 if (!(dwarf_version >= 4 || !dwarf_strict))
13568 return NULL;
13570 litsize = size_of_int_loc_descriptor (i);
13571 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13572 is more compact. For DW_OP_stack_value we need:
13573 litsize + 1 (DW_OP_stack_value)
13574 and for DW_OP_implicit_value:
13575 1 (DW_OP_implicit_value) + 1 (length) + size. */
13576 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13578 loc_result = int_loc_descriptor (i);
13579 add_loc_descr (&loc_result,
13580 new_loc_descr (DW_OP_stack_value, 0, 0));
13581 return loc_result;
13584 loc_result = new_loc_descr (DW_OP_implicit_value,
13585 size, 0);
13586 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13587 loc_result->dw_loc_oprnd2.v.val_int = i;
13588 return loc_result;
13591 /* Return a location descriptor that designates a base+offset location. */
13593 static dw_loc_descr_ref
13594 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13595 enum var_init_status initialized)
13597 unsigned int regno;
13598 dw_loc_descr_ref result;
13599 dw_fde_ref fde = cfun->fde;
13601 /* We only use "frame base" when we're sure we're talking about the
13602 post-prologue local stack frame. We do this by *not* running
13603 register elimination until this point, and recognizing the special
13604 argument pointer and soft frame pointer rtx's. */
13605 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13607 rtx elim = (ira_use_lra_p
13608 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13609 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13611 if (elim != reg)
13613 if (GET_CODE (elim) == PLUS)
13615 offset += INTVAL (XEXP (elim, 1));
13616 elim = XEXP (elim, 0);
13618 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13619 && (elim == hard_frame_pointer_rtx
13620 || elim == stack_pointer_rtx))
13621 || elim == (frame_pointer_needed
13622 ? hard_frame_pointer_rtx
13623 : stack_pointer_rtx));
13625 /* If drap register is used to align stack, use frame
13626 pointer + offset to access stack variables. If stack
13627 is aligned without drap, use stack pointer + offset to
13628 access stack variables. */
13629 if (crtl->stack_realign_tried
13630 && reg == frame_pointer_rtx)
13632 int base_reg
13633 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13634 ? HARD_FRAME_POINTER_REGNUM
13635 : REGNO (elim));
13636 return new_reg_loc_descr (base_reg, offset);
13639 gcc_assert (frame_pointer_fb_offset_valid);
13640 offset += frame_pointer_fb_offset;
13641 return new_loc_descr (DW_OP_fbreg, offset, 0);
13645 regno = REGNO (reg);
13646 #ifdef LEAF_REG_REMAP
13647 if (crtl->uses_only_leaf_regs)
13649 int leaf_reg = LEAF_REG_REMAP (regno);
13650 if (leaf_reg != -1)
13651 regno = (unsigned) leaf_reg;
13653 #endif
13654 regno = DWARF_FRAME_REGNUM (regno);
13656 if (!optimize && fde
13657 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13659 /* Use cfa+offset to represent the location of arguments passed
13660 on the stack when drap is used to align stack.
13661 Only do this when not optimizing, for optimized code var-tracking
13662 is supposed to track where the arguments live and the register
13663 used as vdrap or drap in some spot might be used for something
13664 else in other part of the routine. */
13665 return new_loc_descr (DW_OP_fbreg, offset, 0);
13668 if (regno <= 31)
13669 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13670 offset, 0);
13671 else
13672 result = new_loc_descr (DW_OP_bregx, regno, offset);
13674 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13675 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13677 return result;
13680 /* Return true if this RTL expression describes a base+offset calculation. */
13682 static inline int
13683 is_based_loc (const_rtx rtl)
13685 return (GET_CODE (rtl) == PLUS
13686 && ((REG_P (XEXP (rtl, 0))
13687 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13688 && CONST_INT_P (XEXP (rtl, 1)))));
13691 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13692 failed. */
13694 static dw_loc_descr_ref
13695 tls_mem_loc_descriptor (rtx mem)
13697 tree base;
13698 dw_loc_descr_ref loc_result;
13700 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13701 return NULL;
13703 base = get_base_address (MEM_EXPR (mem));
13704 if (base == NULL
13705 || !VAR_P (base)
13706 || !DECL_THREAD_LOCAL_P (base))
13707 return NULL;
13709 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13710 if (loc_result == NULL)
13711 return NULL;
13713 if (MEM_OFFSET (mem))
13714 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13716 return loc_result;
13719 /* Output debug info about reason why we failed to expand expression as dwarf
13720 expression. */
13722 static void
13723 expansion_failed (tree expr, rtx rtl, char const *reason)
13725 if (dump_file && (dump_flags & TDF_DETAILS))
13727 fprintf (dump_file, "Failed to expand as dwarf: ");
13728 if (expr)
13729 print_generic_expr (dump_file, expr, dump_flags);
13730 if (rtl)
13732 fprintf (dump_file, "\n");
13733 print_rtl (dump_file, rtl);
13735 fprintf (dump_file, "\nReason: %s\n", reason);
13739 /* Helper function for const_ok_for_output. */
13741 static bool
13742 const_ok_for_output_1 (rtx rtl)
13744 if (targetm.const_not_ok_for_debug_p (rtl))
13746 if (GET_CODE (rtl) != UNSPEC)
13748 expansion_failed (NULL_TREE, rtl,
13749 "Expression rejected for debug by the backend.\n");
13750 return false;
13753 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13754 the target hook doesn't explicitly allow it in debug info, assume
13755 we can't express it in the debug info. */
13756 /* Don't complain about TLS UNSPECs, those are just too hard to
13757 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13758 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13759 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13760 if (flag_checking
13761 && (XVECLEN (rtl, 0) == 0
13762 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13763 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13764 inform (current_function_decl
13765 ? DECL_SOURCE_LOCATION (current_function_decl)
13766 : UNKNOWN_LOCATION,
13767 #if NUM_UNSPEC_VALUES > 0
13768 "non-delegitimized UNSPEC %s (%d) found in variable location",
13769 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13770 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13771 XINT (rtl, 1));
13772 #else
13773 "non-delegitimized UNSPEC %d found in variable location",
13774 XINT (rtl, 1));
13775 #endif
13776 expansion_failed (NULL_TREE, rtl,
13777 "UNSPEC hasn't been delegitimized.\n");
13778 return false;
13781 /* FIXME: Refer to PR60655. It is possible for simplification
13782 of rtl expressions in var tracking to produce such expressions.
13783 We should really identify / validate expressions
13784 enclosed in CONST that can be handled by assemblers on various
13785 targets and only handle legitimate cases here. */
13786 switch (GET_CODE (rtl))
13788 case SYMBOL_REF:
13789 break;
13790 case NOT:
13791 case NEG:
13792 return false;
13793 default:
13794 return true;
13797 if (CONSTANT_POOL_ADDRESS_P (rtl))
13799 bool marked;
13800 get_pool_constant_mark (rtl, &marked);
13801 /* If all references to this pool constant were optimized away,
13802 it was not output and thus we can't represent it. */
13803 if (!marked)
13805 expansion_failed (NULL_TREE, rtl,
13806 "Constant was removed from constant pool.\n");
13807 return false;
13811 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13812 return false;
13814 /* Avoid references to external symbols in debug info, on several targets
13815 the linker might even refuse to link when linking a shared library,
13816 and in many other cases the relocations for .debug_info/.debug_loc are
13817 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13818 to be defined within the same shared library or executable are fine. */
13819 if (SYMBOL_REF_EXTERNAL_P (rtl))
13821 tree decl = SYMBOL_REF_DECL (rtl);
13823 if (decl == NULL || !targetm.binds_local_p (decl))
13825 expansion_failed (NULL_TREE, rtl,
13826 "Symbol not defined in current TU.\n");
13827 return false;
13831 return true;
13834 /* Return true if constant RTL can be emitted in DW_OP_addr or
13835 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13836 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13838 static bool
13839 const_ok_for_output (rtx rtl)
13841 if (GET_CODE (rtl) == SYMBOL_REF)
13842 return const_ok_for_output_1 (rtl);
13844 if (GET_CODE (rtl) == CONST)
13846 subrtx_var_iterator::array_type array;
13847 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13848 if (!const_ok_for_output_1 (*iter))
13849 return false;
13850 return true;
13853 return true;
13856 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13857 if possible, NULL otherwise. */
13859 static dw_die_ref
13860 base_type_for_mode (machine_mode mode, bool unsignedp)
13862 dw_die_ref type_die;
13863 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13865 if (type == NULL)
13866 return NULL;
13867 switch (TREE_CODE (type))
13869 case INTEGER_TYPE:
13870 case REAL_TYPE:
13871 break;
13872 default:
13873 return NULL;
13875 type_die = lookup_type_die (type);
13876 if (!type_die)
13877 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13878 comp_unit_die ());
13879 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13880 return NULL;
13881 return type_die;
13884 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13885 type matching MODE, or, if MODE is narrower than or as wide as
13886 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13887 possible. */
13889 static dw_loc_descr_ref
13890 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13892 machine_mode outer_mode = mode;
13893 dw_die_ref type_die;
13894 dw_loc_descr_ref cvt;
13896 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13898 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13899 return op;
13901 type_die = base_type_for_mode (outer_mode, 1);
13902 if (type_die == NULL)
13903 return NULL;
13904 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13905 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13906 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13907 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13908 add_loc_descr (&op, cvt);
13909 return op;
13912 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13914 static dw_loc_descr_ref
13915 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13916 dw_loc_descr_ref op1)
13918 dw_loc_descr_ref ret = op0;
13919 add_loc_descr (&ret, op1);
13920 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13921 if (STORE_FLAG_VALUE != 1)
13923 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13924 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13926 return ret;
13929 /* Subroutine of scompare_loc_descriptor for the case in which we're
13930 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13931 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13933 static dw_loc_descr_ref
13934 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13935 scalar_int_mode op_mode,
13936 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13938 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13939 dw_loc_descr_ref cvt;
13941 if (type_die == NULL)
13942 return NULL;
13943 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13944 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13945 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13946 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13947 add_loc_descr (&op0, cvt);
13948 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13949 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13950 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13951 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13952 add_loc_descr (&op1, cvt);
13953 return compare_loc_descriptor (op, op0, op1);
13956 /* Subroutine of scompare_loc_descriptor for the case in which we're
13957 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13958 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13960 static dw_loc_descr_ref
13961 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13962 scalar_int_mode op_mode,
13963 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13965 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13966 /* For eq/ne, if the operands are known to be zero-extended,
13967 there is no need to do the fancy shifting up. */
13968 if (op == DW_OP_eq || op == DW_OP_ne)
13970 dw_loc_descr_ref last0, last1;
13971 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13973 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13975 /* deref_size zero extends, and for constants we can check
13976 whether they are zero extended or not. */
13977 if (((last0->dw_loc_opc == DW_OP_deref_size
13978 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13979 || (CONST_INT_P (XEXP (rtl, 0))
13980 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13981 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13982 && ((last1->dw_loc_opc == DW_OP_deref_size
13983 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13984 || (CONST_INT_P (XEXP (rtl, 1))
13985 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13986 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13987 return compare_loc_descriptor (op, op0, op1);
13989 /* EQ/NE comparison against constant in narrower type than
13990 DWARF2_ADDR_SIZE can be performed either as
13991 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13992 DW_OP_{eq,ne}
13994 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13995 DW_OP_{eq,ne}. Pick whatever is shorter. */
13996 if (CONST_INT_P (XEXP (rtl, 1))
13997 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
13998 && (size_of_int_loc_descriptor (shift) + 1
13999 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14000 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14001 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14002 & GET_MODE_MASK (op_mode))))
14004 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14005 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14006 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14007 & GET_MODE_MASK (op_mode));
14008 return compare_loc_descriptor (op, op0, op1);
14011 add_loc_descr (&op0, int_loc_descriptor (shift));
14012 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14013 if (CONST_INT_P (XEXP (rtl, 1)))
14014 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14015 else
14017 add_loc_descr (&op1, int_loc_descriptor (shift));
14018 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14020 return compare_loc_descriptor (op, op0, op1);
14023 /* Return location descriptor for unsigned comparison OP RTL. */
14025 static dw_loc_descr_ref
14026 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14027 machine_mode mem_mode)
14029 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14030 dw_loc_descr_ref op0, op1;
14032 if (op_mode == VOIDmode)
14033 op_mode = GET_MODE (XEXP (rtl, 1));
14034 if (op_mode == VOIDmode)
14035 return NULL;
14037 scalar_int_mode int_op_mode;
14038 if (dwarf_strict
14039 && dwarf_version < 5
14040 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14041 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14042 return NULL;
14044 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14045 VAR_INIT_STATUS_INITIALIZED);
14046 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14047 VAR_INIT_STATUS_INITIALIZED);
14049 if (op0 == NULL || op1 == NULL)
14050 return NULL;
14052 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14054 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14055 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14057 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14058 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14060 return compare_loc_descriptor (op, op0, op1);
14063 /* Return location descriptor for unsigned comparison OP RTL. */
14065 static dw_loc_descr_ref
14066 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14067 machine_mode mem_mode)
14069 dw_loc_descr_ref op0, op1;
14071 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14072 if (test_op_mode == VOIDmode)
14073 test_op_mode = GET_MODE (XEXP (rtl, 1));
14075 scalar_int_mode op_mode;
14076 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14077 return NULL;
14079 if (dwarf_strict
14080 && dwarf_version < 5
14081 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14082 return NULL;
14084 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14085 VAR_INIT_STATUS_INITIALIZED);
14086 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14087 VAR_INIT_STATUS_INITIALIZED);
14089 if (op0 == NULL || op1 == NULL)
14090 return NULL;
14092 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14094 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14095 dw_loc_descr_ref last0, last1;
14096 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14098 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14100 if (CONST_INT_P (XEXP (rtl, 0)))
14101 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14102 /* deref_size zero extends, so no need to mask it again. */
14103 else if (last0->dw_loc_opc != DW_OP_deref_size
14104 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14106 add_loc_descr (&op0, int_loc_descriptor (mask));
14107 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14109 if (CONST_INT_P (XEXP (rtl, 1)))
14110 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14111 /* deref_size zero extends, so no need to mask it again. */
14112 else if (last1->dw_loc_opc != DW_OP_deref_size
14113 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14115 add_loc_descr (&op1, int_loc_descriptor (mask));
14116 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14119 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14121 HOST_WIDE_INT bias = 1;
14122 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14123 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14124 if (CONST_INT_P (XEXP (rtl, 1)))
14125 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14126 + INTVAL (XEXP (rtl, 1)));
14127 else
14128 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14129 bias, 0));
14131 return compare_loc_descriptor (op, op0, op1);
14134 /* Return location descriptor for {U,S}{MIN,MAX}. */
14136 static dw_loc_descr_ref
14137 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14138 machine_mode mem_mode)
14140 enum dwarf_location_atom op;
14141 dw_loc_descr_ref op0, op1, ret;
14142 dw_loc_descr_ref bra_node, drop_node;
14144 scalar_int_mode int_mode;
14145 if (dwarf_strict
14146 && dwarf_version < 5
14147 && (!is_a <scalar_int_mode> (mode, &int_mode)
14148 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14149 return NULL;
14151 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14152 VAR_INIT_STATUS_INITIALIZED);
14153 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14154 VAR_INIT_STATUS_INITIALIZED);
14156 if (op0 == NULL || op1 == NULL)
14157 return NULL;
14159 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14160 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14161 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14162 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14164 /* Checked by the caller. */
14165 int_mode = as_a <scalar_int_mode> (mode);
14166 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14168 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14169 add_loc_descr (&op0, int_loc_descriptor (mask));
14170 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14171 add_loc_descr (&op1, int_loc_descriptor (mask));
14172 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14174 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14176 HOST_WIDE_INT bias = 1;
14177 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14178 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14179 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14182 else if (is_a <scalar_int_mode> (mode, &int_mode)
14183 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14185 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14186 add_loc_descr (&op0, int_loc_descriptor (shift));
14187 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14188 add_loc_descr (&op1, int_loc_descriptor (shift));
14189 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14191 else if (is_a <scalar_int_mode> (mode, &int_mode)
14192 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14194 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14195 dw_loc_descr_ref cvt;
14196 if (type_die == NULL)
14197 return NULL;
14198 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14199 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14200 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14201 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14202 add_loc_descr (&op0, cvt);
14203 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14204 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14205 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14206 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14207 add_loc_descr (&op1, cvt);
14210 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14211 op = DW_OP_lt;
14212 else
14213 op = DW_OP_gt;
14214 ret = op0;
14215 add_loc_descr (&ret, op1);
14216 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14217 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14218 add_loc_descr (&ret, bra_node);
14219 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14220 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14221 add_loc_descr (&ret, drop_node);
14222 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14223 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14224 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14225 && is_a <scalar_int_mode> (mode, &int_mode)
14226 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14227 ret = convert_descriptor_to_mode (int_mode, ret);
14228 return ret;
14231 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14232 but after converting arguments to type_die, afterwards
14233 convert back to unsigned. */
14235 static dw_loc_descr_ref
14236 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14237 scalar_int_mode mode, machine_mode mem_mode)
14239 dw_loc_descr_ref cvt, op0, op1;
14241 if (type_die == NULL)
14242 return NULL;
14243 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14244 VAR_INIT_STATUS_INITIALIZED);
14245 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14246 VAR_INIT_STATUS_INITIALIZED);
14247 if (op0 == NULL || op1 == NULL)
14248 return NULL;
14249 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14250 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14251 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14252 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14253 add_loc_descr (&op0, cvt);
14254 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14255 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14256 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14257 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14258 add_loc_descr (&op1, cvt);
14259 add_loc_descr (&op0, op1);
14260 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14261 return convert_descriptor_to_mode (mode, op0);
14264 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14265 const0 is DW_OP_lit0 or corresponding typed constant,
14266 const1 is DW_OP_lit1 or corresponding typed constant
14267 and constMSB is constant with just the MSB bit set
14268 for the mode):
14269 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14270 L1: const0 DW_OP_swap
14271 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14272 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14273 L3: DW_OP_drop
14274 L4: DW_OP_nop
14276 CTZ is similar:
14277 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14278 L1: const0 DW_OP_swap
14279 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14280 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14281 L3: DW_OP_drop
14282 L4: DW_OP_nop
14284 FFS is similar:
14285 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14286 L1: const1 DW_OP_swap
14287 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14288 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14289 L3: DW_OP_drop
14290 L4: DW_OP_nop */
14292 static dw_loc_descr_ref
14293 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14294 machine_mode mem_mode)
14296 dw_loc_descr_ref op0, ret, tmp;
14297 HOST_WIDE_INT valv;
14298 dw_loc_descr_ref l1jump, l1label;
14299 dw_loc_descr_ref l2jump, l2label;
14300 dw_loc_descr_ref l3jump, l3label;
14301 dw_loc_descr_ref l4jump, l4label;
14302 rtx msb;
14304 if (GET_MODE (XEXP (rtl, 0)) != mode)
14305 return NULL;
14307 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14308 VAR_INIT_STATUS_INITIALIZED);
14309 if (op0 == NULL)
14310 return NULL;
14311 ret = op0;
14312 if (GET_CODE (rtl) == CLZ)
14314 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14315 valv = GET_MODE_BITSIZE (mode);
14317 else if (GET_CODE (rtl) == FFS)
14318 valv = 0;
14319 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14320 valv = GET_MODE_BITSIZE (mode);
14321 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14322 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14323 add_loc_descr (&ret, l1jump);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14325 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14326 VAR_INIT_STATUS_INITIALIZED);
14327 if (tmp == NULL)
14328 return NULL;
14329 add_loc_descr (&ret, tmp);
14330 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14331 add_loc_descr (&ret, l4jump);
14332 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14333 ? const1_rtx : const0_rtx,
14334 mode, mem_mode,
14335 VAR_INIT_STATUS_INITIALIZED);
14336 if (l1label == NULL)
14337 return NULL;
14338 add_loc_descr (&ret, l1label);
14339 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14340 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14341 add_loc_descr (&ret, l2label);
14342 if (GET_CODE (rtl) != CLZ)
14343 msb = const1_rtx;
14344 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14345 msb = GEN_INT (HOST_WIDE_INT_1U
14346 << (GET_MODE_BITSIZE (mode) - 1));
14347 else
14348 msb = immed_wide_int_const
14349 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14350 GET_MODE_PRECISION (mode)), mode);
14351 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14352 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14353 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14354 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14355 else
14356 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14357 VAR_INIT_STATUS_INITIALIZED);
14358 if (tmp == NULL)
14359 return NULL;
14360 add_loc_descr (&ret, tmp);
14361 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14362 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14363 add_loc_descr (&ret, l3jump);
14364 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14365 VAR_INIT_STATUS_INITIALIZED);
14366 if (tmp == NULL)
14367 return NULL;
14368 add_loc_descr (&ret, tmp);
14369 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14370 ? DW_OP_shl : DW_OP_shr, 0, 0));
14371 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14372 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14373 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14374 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14375 add_loc_descr (&ret, l2jump);
14376 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14377 add_loc_descr (&ret, l3label);
14378 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14379 add_loc_descr (&ret, l4label);
14380 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14381 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14382 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14383 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14384 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14385 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14386 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14387 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14388 return ret;
14391 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14392 const1 is DW_OP_lit1 or corresponding typed constant):
14393 const0 DW_OP_swap
14394 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14395 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14396 L2: DW_OP_drop
14398 PARITY is similar:
14399 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14400 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14401 L2: DW_OP_drop */
14403 static dw_loc_descr_ref
14404 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14405 machine_mode mem_mode)
14407 dw_loc_descr_ref op0, ret, tmp;
14408 dw_loc_descr_ref l1jump, l1label;
14409 dw_loc_descr_ref l2jump, l2label;
14411 if (GET_MODE (XEXP (rtl, 0)) != mode)
14412 return NULL;
14414 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 if (op0 == NULL)
14417 return NULL;
14418 ret = op0;
14419 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14420 VAR_INIT_STATUS_INITIALIZED);
14421 if (tmp == NULL)
14422 return NULL;
14423 add_loc_descr (&ret, tmp);
14424 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14425 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14426 add_loc_descr (&ret, l1label);
14427 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14428 add_loc_descr (&ret, l2jump);
14429 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14430 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14431 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14432 VAR_INIT_STATUS_INITIALIZED);
14433 if (tmp == NULL)
14434 return NULL;
14435 add_loc_descr (&ret, tmp);
14436 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14437 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14438 ? DW_OP_plus : DW_OP_xor, 0, 0));
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14440 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14441 VAR_INIT_STATUS_INITIALIZED);
14442 add_loc_descr (&ret, tmp);
14443 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14444 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14445 add_loc_descr (&ret, l1jump);
14446 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14447 add_loc_descr (&ret, l2label);
14448 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14449 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14450 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14451 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14452 return ret;
14455 /* BSWAP (constS is initial shift count, either 56 or 24):
14456 constS const0
14457 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14458 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14459 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14460 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14461 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14463 static dw_loc_descr_ref
14464 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14465 machine_mode mem_mode)
14467 dw_loc_descr_ref op0, ret, tmp;
14468 dw_loc_descr_ref l1jump, l1label;
14469 dw_loc_descr_ref l2jump, l2label;
14471 if (BITS_PER_UNIT != 8
14472 || (GET_MODE_BITSIZE (mode) != 32
14473 && GET_MODE_BITSIZE (mode) != 64))
14474 return NULL;
14476 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14477 VAR_INIT_STATUS_INITIALIZED);
14478 if (op0 == NULL)
14479 return NULL;
14481 ret = op0;
14482 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14483 mode, mem_mode,
14484 VAR_INIT_STATUS_INITIALIZED);
14485 if (tmp == NULL)
14486 return NULL;
14487 add_loc_descr (&ret, tmp);
14488 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14489 VAR_INIT_STATUS_INITIALIZED);
14490 if (tmp == NULL)
14491 return NULL;
14492 add_loc_descr (&ret, tmp);
14493 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14494 add_loc_descr (&ret, l1label);
14495 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14496 mode, mem_mode,
14497 VAR_INIT_STATUS_INITIALIZED);
14498 add_loc_descr (&ret, tmp);
14499 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14500 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14501 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14502 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14503 VAR_INIT_STATUS_INITIALIZED);
14504 if (tmp == NULL)
14505 return NULL;
14506 add_loc_descr (&ret, tmp);
14507 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14508 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14513 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14514 VAR_INIT_STATUS_INITIALIZED);
14515 add_loc_descr (&ret, tmp);
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14517 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14518 add_loc_descr (&ret, l2jump);
14519 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14520 VAR_INIT_STATUS_INITIALIZED);
14521 add_loc_descr (&ret, tmp);
14522 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14524 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14525 add_loc_descr (&ret, l1jump);
14526 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14527 add_loc_descr (&ret, l2label);
14528 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14529 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14530 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14531 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14532 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14533 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14534 return ret;
14537 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14538 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14539 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14540 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14542 ROTATERT is similar:
14543 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14544 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14545 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14547 static dw_loc_descr_ref
14548 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14549 machine_mode mem_mode)
14551 rtx rtlop1 = XEXP (rtl, 1);
14552 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14553 int i;
14555 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14556 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14557 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14558 VAR_INIT_STATUS_INITIALIZED);
14559 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14560 VAR_INIT_STATUS_INITIALIZED);
14561 if (op0 == NULL || op1 == NULL)
14562 return NULL;
14563 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14564 for (i = 0; i < 2; i++)
14566 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14567 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14568 mode, mem_mode,
14569 VAR_INIT_STATUS_INITIALIZED);
14570 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14571 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14572 ? DW_OP_const4u
14573 : HOST_BITS_PER_WIDE_INT == 64
14574 ? DW_OP_const8u : DW_OP_constu,
14575 GET_MODE_MASK (mode), 0);
14576 else
14577 mask[i] = NULL;
14578 if (mask[i] == NULL)
14579 return NULL;
14580 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14582 ret = op0;
14583 add_loc_descr (&ret, op1);
14584 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14585 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14586 if (GET_CODE (rtl) == ROTATERT)
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14590 GET_MODE_BITSIZE (mode), 0));
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14593 if (mask[0] != NULL)
14594 add_loc_descr (&ret, mask[0]);
14595 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14596 if (mask[1] != NULL)
14598 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14599 add_loc_descr (&ret, mask[1]);
14600 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14602 if (GET_CODE (rtl) == ROTATE)
14604 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14605 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14606 GET_MODE_BITSIZE (mode), 0));
14608 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14610 return ret;
14613 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14614 for DEBUG_PARAMETER_REF RTL. */
14616 static dw_loc_descr_ref
14617 parameter_ref_descriptor (rtx rtl)
14619 dw_loc_descr_ref ret;
14620 dw_die_ref ref;
14622 if (dwarf_strict)
14623 return NULL;
14624 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14625 /* With LTO during LTRANS we get the late DIE that refers to the early
14626 DIE, thus we add another indirection here. This seems to confuse
14627 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14628 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14629 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14630 if (ref)
14632 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14633 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14634 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14636 else
14638 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14639 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14641 return ret;
14644 /* The following routine converts the RTL for a variable or parameter
14645 (resident in memory) into an equivalent Dwarf representation of a
14646 mechanism for getting the address of that same variable onto the top of a
14647 hypothetical "address evaluation" stack.
14649 When creating memory location descriptors, we are effectively transforming
14650 the RTL for a memory-resident object into its Dwarf postfix expression
14651 equivalent. This routine recursively descends an RTL tree, turning
14652 it into Dwarf postfix code as it goes.
14654 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14656 MEM_MODE is the mode of the memory reference, needed to handle some
14657 autoincrement addressing modes.
14659 Return 0 if we can't represent the location. */
14661 dw_loc_descr_ref
14662 mem_loc_descriptor (rtx rtl, machine_mode mode,
14663 machine_mode mem_mode,
14664 enum var_init_status initialized)
14666 dw_loc_descr_ref mem_loc_result = NULL;
14667 enum dwarf_location_atom op;
14668 dw_loc_descr_ref op0, op1;
14669 rtx inner = NULL_RTX;
14671 if (mode == VOIDmode)
14672 mode = GET_MODE (rtl);
14674 /* Note that for a dynamically sized array, the location we will generate a
14675 description of here will be the lowest numbered location which is
14676 actually within the array. That's *not* necessarily the same as the
14677 zeroth element of the array. */
14679 rtl = targetm.delegitimize_address (rtl);
14681 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14682 return NULL;
14684 scalar_int_mode int_mode, inner_mode, op1_mode;
14685 switch (GET_CODE (rtl))
14687 case POST_INC:
14688 case POST_DEC:
14689 case POST_MODIFY:
14690 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14692 case SUBREG:
14693 /* The case of a subreg may arise when we have a local (register)
14694 variable or a formal (register) parameter which doesn't quite fill
14695 up an entire register. For now, just assume that it is
14696 legitimate to make the Dwarf info refer to the whole register which
14697 contains the given subreg. */
14698 if (!subreg_lowpart_p (rtl))
14699 break;
14700 inner = SUBREG_REG (rtl);
14701 /* FALLTHRU */
14702 case TRUNCATE:
14703 if (inner == NULL_RTX)
14704 inner = XEXP (rtl, 0);
14705 if (is_a <scalar_int_mode> (mode, &int_mode)
14706 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14707 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14708 #ifdef POINTERS_EXTEND_UNSIGNED
14709 || (int_mode == Pmode && mem_mode != VOIDmode)
14710 #endif
14712 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14714 mem_loc_result = mem_loc_descriptor (inner,
14715 inner_mode,
14716 mem_mode, initialized);
14717 break;
14719 if (dwarf_strict && dwarf_version < 5)
14720 break;
14721 if (is_a <scalar_int_mode> (mode, &int_mode)
14722 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14723 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14724 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14726 dw_die_ref type_die;
14727 dw_loc_descr_ref cvt;
14729 mem_loc_result = mem_loc_descriptor (inner,
14730 GET_MODE (inner),
14731 mem_mode, initialized);
14732 if (mem_loc_result == NULL)
14733 break;
14734 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14735 if (type_die == NULL)
14737 mem_loc_result = NULL;
14738 break;
14740 if (GET_MODE_SIZE (mode)
14741 != GET_MODE_SIZE (GET_MODE (inner)))
14742 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14743 else
14744 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14745 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14746 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14747 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14748 add_loc_descr (&mem_loc_result, cvt);
14749 if (is_a <scalar_int_mode> (mode, &int_mode)
14750 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14752 /* Convert it to untyped afterwards. */
14753 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14754 add_loc_descr (&mem_loc_result, cvt);
14757 break;
14759 case REG:
14760 if (!is_a <scalar_int_mode> (mode, &int_mode)
14761 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14762 && rtl != arg_pointer_rtx
14763 && rtl != frame_pointer_rtx
14764 #ifdef POINTERS_EXTEND_UNSIGNED
14765 && (int_mode != Pmode || mem_mode == VOIDmode)
14766 #endif
14769 dw_die_ref type_die;
14770 unsigned int dbx_regnum;
14772 if (dwarf_strict && dwarf_version < 5)
14773 break;
14774 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14775 break;
14776 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14777 if (type_die == NULL)
14778 break;
14780 dbx_regnum = dbx_reg_number (rtl);
14781 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14782 break;
14783 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14784 dbx_regnum, 0);
14785 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14786 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14787 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14788 break;
14790 /* Whenever a register number forms a part of the description of the
14791 method for calculating the (dynamic) address of a memory resident
14792 object, DWARF rules require the register number be referred to as
14793 a "base register". This distinction is not based in any way upon
14794 what category of register the hardware believes the given register
14795 belongs to. This is strictly DWARF terminology we're dealing with
14796 here. Note that in cases where the location of a memory-resident
14797 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14798 OP_CONST (0)) the actual DWARF location descriptor that we generate
14799 may just be OP_BASEREG (basereg). This may look deceptively like
14800 the object in question was allocated to a register (rather than in
14801 memory) so DWARF consumers need to be aware of the subtle
14802 distinction between OP_REG and OP_BASEREG. */
14803 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14804 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14805 else if (stack_realign_drap
14806 && crtl->drap_reg
14807 && crtl->args.internal_arg_pointer == rtl
14808 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14810 /* If RTL is internal_arg_pointer, which has been optimized
14811 out, use DRAP instead. */
14812 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14813 VAR_INIT_STATUS_INITIALIZED);
14815 break;
14817 case SIGN_EXTEND:
14818 case ZERO_EXTEND:
14819 if (!is_a <scalar_int_mode> (mode, &int_mode)
14820 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14821 break;
14822 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14823 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14824 if (op0 == 0)
14825 break;
14826 else if (GET_CODE (rtl) == ZERO_EXTEND
14827 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14828 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14829 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14830 to expand zero extend as two shifts instead of
14831 masking. */
14832 && GET_MODE_SIZE (inner_mode) <= 4)
14834 mem_loc_result = op0;
14835 add_loc_descr (&mem_loc_result,
14836 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14837 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14839 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14841 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14842 shift *= BITS_PER_UNIT;
14843 if (GET_CODE (rtl) == SIGN_EXTEND)
14844 op = DW_OP_shra;
14845 else
14846 op = DW_OP_shr;
14847 mem_loc_result = op0;
14848 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14849 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14850 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14851 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14853 else if (!dwarf_strict || dwarf_version >= 5)
14855 dw_die_ref type_die1, type_die2;
14856 dw_loc_descr_ref cvt;
14858 type_die1 = base_type_for_mode (inner_mode,
14859 GET_CODE (rtl) == ZERO_EXTEND);
14860 if (type_die1 == NULL)
14861 break;
14862 type_die2 = base_type_for_mode (int_mode, 1);
14863 if (type_die2 == NULL)
14864 break;
14865 mem_loc_result = op0;
14866 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14867 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14868 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14869 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14870 add_loc_descr (&mem_loc_result, cvt);
14871 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14872 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14873 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14874 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14875 add_loc_descr (&mem_loc_result, cvt);
14877 break;
14879 case MEM:
14881 rtx new_rtl = avoid_constant_pool_reference (rtl);
14882 if (new_rtl != rtl)
14884 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14885 initialized);
14886 if (mem_loc_result != NULL)
14887 return mem_loc_result;
14890 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14891 get_address_mode (rtl), mode,
14892 VAR_INIT_STATUS_INITIALIZED);
14893 if (mem_loc_result == NULL)
14894 mem_loc_result = tls_mem_loc_descriptor (rtl);
14895 if (mem_loc_result != NULL)
14897 if (!is_a <scalar_int_mode> (mode, &int_mode)
14898 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14900 dw_die_ref type_die;
14901 dw_loc_descr_ref deref;
14903 if (dwarf_strict && dwarf_version < 5)
14904 return NULL;
14905 type_die
14906 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14907 if (type_die == NULL)
14908 return NULL;
14909 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14910 GET_MODE_SIZE (mode), 0);
14911 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14912 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14913 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14914 add_loc_descr (&mem_loc_result, deref);
14916 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14917 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14918 else
14919 add_loc_descr (&mem_loc_result,
14920 new_loc_descr (DW_OP_deref_size,
14921 GET_MODE_SIZE (int_mode), 0));
14923 break;
14925 case LO_SUM:
14926 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14928 case LABEL_REF:
14929 /* Some ports can transform a symbol ref into a label ref, because
14930 the symbol ref is too far away and has to be dumped into a constant
14931 pool. */
14932 case CONST:
14933 case SYMBOL_REF:
14934 if (!is_a <scalar_int_mode> (mode, &int_mode)
14935 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14936 #ifdef POINTERS_EXTEND_UNSIGNED
14937 && (int_mode != Pmode || mem_mode == VOIDmode)
14938 #endif
14940 break;
14941 if (GET_CODE (rtl) == SYMBOL_REF
14942 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14944 dw_loc_descr_ref temp;
14946 /* If this is not defined, we have no way to emit the data. */
14947 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14948 break;
14950 temp = new_addr_loc_descr (rtl, dtprel_true);
14952 /* We check for DWARF 5 here because gdb did not implement
14953 DW_OP_form_tls_address until after 7.12. */
14954 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14955 ? DW_OP_form_tls_address
14956 : DW_OP_GNU_push_tls_address),
14957 0, 0);
14958 add_loc_descr (&mem_loc_result, temp);
14960 break;
14963 if (!const_ok_for_output (rtl))
14965 if (GET_CODE (rtl) == CONST)
14966 switch (GET_CODE (XEXP (rtl, 0)))
14968 case NOT:
14969 op = DW_OP_not;
14970 goto try_const_unop;
14971 case NEG:
14972 op = DW_OP_neg;
14973 goto try_const_unop;
14974 try_const_unop:
14975 rtx arg;
14976 arg = XEXP (XEXP (rtl, 0), 0);
14977 if (!CONSTANT_P (arg))
14978 arg = gen_rtx_CONST (int_mode, arg);
14979 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
14980 initialized);
14981 if (op0)
14983 mem_loc_result = op0;
14984 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14986 break;
14987 default:
14988 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14989 mem_mode, initialized);
14990 break;
14992 break;
14995 symref:
14996 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14997 vec_safe_push (used_rtx_array, rtl);
14998 break;
15000 case CONCAT:
15001 case CONCATN:
15002 case VAR_LOCATION:
15003 case DEBUG_IMPLICIT_PTR:
15004 expansion_failed (NULL_TREE, rtl,
15005 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15006 return 0;
15008 case ENTRY_VALUE:
15009 if (dwarf_strict && dwarf_version < 5)
15010 return NULL;
15011 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15013 if (!is_a <scalar_int_mode> (mode, &int_mode)
15014 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15015 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15016 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15017 else
15019 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15020 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15021 return NULL;
15022 op0 = one_reg_loc_descriptor (dbx_regnum,
15023 VAR_INIT_STATUS_INITIALIZED);
15026 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15027 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15029 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15030 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15031 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15032 return NULL;
15034 else
15035 gcc_unreachable ();
15036 if (op0 == NULL)
15037 return NULL;
15038 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15039 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15040 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15041 break;
15043 case DEBUG_PARAMETER_REF:
15044 mem_loc_result = parameter_ref_descriptor (rtl);
15045 break;
15047 case PRE_MODIFY:
15048 /* Extract the PLUS expression nested inside and fall into
15049 PLUS code below. */
15050 rtl = XEXP (rtl, 1);
15051 goto plus;
15053 case PRE_INC:
15054 case PRE_DEC:
15055 /* Turn these into a PLUS expression and fall into the PLUS code
15056 below. */
15057 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15058 gen_int_mode (GET_CODE (rtl) == PRE_INC
15059 ? GET_MODE_UNIT_SIZE (mem_mode)
15060 : -GET_MODE_UNIT_SIZE (mem_mode),
15061 mode));
15063 /* fall through */
15065 case PLUS:
15066 plus:
15067 if (is_based_loc (rtl)
15068 && is_a <scalar_int_mode> (mode, &int_mode)
15069 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15070 || XEXP (rtl, 0) == arg_pointer_rtx
15071 || XEXP (rtl, 0) == frame_pointer_rtx))
15072 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15073 INTVAL (XEXP (rtl, 1)),
15074 VAR_INIT_STATUS_INITIALIZED);
15075 else
15077 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15078 VAR_INIT_STATUS_INITIALIZED);
15079 if (mem_loc_result == 0)
15080 break;
15082 if (CONST_INT_P (XEXP (rtl, 1))
15083 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15084 <= DWARF2_ADDR_SIZE))
15085 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15086 else
15088 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15089 VAR_INIT_STATUS_INITIALIZED);
15090 if (op1 == 0)
15091 return NULL;
15092 add_loc_descr (&mem_loc_result, op1);
15093 add_loc_descr (&mem_loc_result,
15094 new_loc_descr (DW_OP_plus, 0, 0));
15097 break;
15099 /* If a pseudo-reg is optimized away, it is possible for it to
15100 be replaced with a MEM containing a multiply or shift. */
15101 case MINUS:
15102 op = DW_OP_minus;
15103 goto do_binop;
15105 case MULT:
15106 op = DW_OP_mul;
15107 goto do_binop;
15109 case DIV:
15110 if ((!dwarf_strict || dwarf_version >= 5)
15111 && is_a <scalar_int_mode> (mode, &int_mode)
15112 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15114 mem_loc_result = typed_binop (DW_OP_div, rtl,
15115 base_type_for_mode (mode, 0),
15116 int_mode, mem_mode);
15117 break;
15119 op = DW_OP_div;
15120 goto do_binop;
15122 case UMOD:
15123 op = DW_OP_mod;
15124 goto do_binop;
15126 case ASHIFT:
15127 op = DW_OP_shl;
15128 goto do_shift;
15130 case ASHIFTRT:
15131 op = DW_OP_shra;
15132 goto do_shift;
15134 case LSHIFTRT:
15135 op = DW_OP_shr;
15136 goto do_shift;
15138 do_shift:
15139 if (!is_a <scalar_int_mode> (mode, &int_mode))
15140 break;
15141 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15142 VAR_INIT_STATUS_INITIALIZED);
15144 rtx rtlop1 = XEXP (rtl, 1);
15145 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15146 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15147 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15148 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15149 VAR_INIT_STATUS_INITIALIZED);
15152 if (op0 == 0 || op1 == 0)
15153 break;
15155 mem_loc_result = op0;
15156 add_loc_descr (&mem_loc_result, op1);
15157 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15158 break;
15160 case AND:
15161 op = DW_OP_and;
15162 goto do_binop;
15164 case IOR:
15165 op = DW_OP_or;
15166 goto do_binop;
15168 case XOR:
15169 op = DW_OP_xor;
15170 goto do_binop;
15172 do_binop:
15173 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15174 VAR_INIT_STATUS_INITIALIZED);
15175 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15176 VAR_INIT_STATUS_INITIALIZED);
15178 if (op0 == 0 || op1 == 0)
15179 break;
15181 mem_loc_result = op0;
15182 add_loc_descr (&mem_loc_result, op1);
15183 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15184 break;
15186 case MOD:
15187 if ((!dwarf_strict || dwarf_version >= 5)
15188 && is_a <scalar_int_mode> (mode, &int_mode)
15189 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15191 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15192 base_type_for_mode (mode, 0),
15193 int_mode, mem_mode);
15194 break;
15197 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15198 VAR_INIT_STATUS_INITIALIZED);
15199 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15200 VAR_INIT_STATUS_INITIALIZED);
15202 if (op0 == 0 || op1 == 0)
15203 break;
15205 mem_loc_result = op0;
15206 add_loc_descr (&mem_loc_result, op1);
15207 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15208 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15209 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15210 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15211 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15212 break;
15214 case UDIV:
15215 if ((!dwarf_strict || dwarf_version >= 5)
15216 && is_a <scalar_int_mode> (mode, &int_mode))
15218 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15220 op = DW_OP_div;
15221 goto do_binop;
15223 mem_loc_result = typed_binop (DW_OP_div, rtl,
15224 base_type_for_mode (int_mode, 1),
15225 int_mode, mem_mode);
15227 break;
15229 case NOT:
15230 op = DW_OP_not;
15231 goto do_unop;
15233 case ABS:
15234 op = DW_OP_abs;
15235 goto do_unop;
15237 case NEG:
15238 op = DW_OP_neg;
15239 goto do_unop;
15241 do_unop:
15242 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15243 VAR_INIT_STATUS_INITIALIZED);
15245 if (op0 == 0)
15246 break;
15248 mem_loc_result = op0;
15249 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15250 break;
15252 case CONST_INT:
15253 if (!is_a <scalar_int_mode> (mode, &int_mode)
15254 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15255 #ifdef POINTERS_EXTEND_UNSIGNED
15256 || (int_mode == Pmode
15257 && mem_mode != VOIDmode
15258 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15259 #endif
15262 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15263 break;
15265 if ((!dwarf_strict || dwarf_version >= 5)
15266 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15267 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15269 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15270 scalar_int_mode amode;
15271 if (type_die == NULL)
15272 return NULL;
15273 if (INTVAL (rtl) >= 0
15274 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15275 .exists (&amode))
15276 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15277 /* const DW_OP_convert <XXX> vs.
15278 DW_OP_const_type <XXX, 1, const>. */
15279 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15280 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15282 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15283 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15284 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15285 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15286 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15287 add_loc_descr (&mem_loc_result, op0);
15288 return mem_loc_result;
15290 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15291 INTVAL (rtl));
15292 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15293 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15294 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15295 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15296 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15297 else
15299 mem_loc_result->dw_loc_oprnd2.val_class
15300 = dw_val_class_const_double;
15301 mem_loc_result->dw_loc_oprnd2.v.val_double
15302 = double_int::from_shwi (INTVAL (rtl));
15305 break;
15307 case CONST_DOUBLE:
15308 if (!dwarf_strict || dwarf_version >= 5)
15310 dw_die_ref type_die;
15312 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15313 CONST_DOUBLE rtx could represent either a large integer
15314 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15315 the value is always a floating point constant.
15317 When it is an integer, a CONST_DOUBLE is used whenever
15318 the constant requires 2 HWIs to be adequately represented.
15319 We output CONST_DOUBLEs as blocks. */
15320 if (mode == VOIDmode
15321 || (GET_MODE (rtl) == VOIDmode
15322 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15323 break;
15324 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15325 if (type_die == NULL)
15326 return NULL;
15327 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15328 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15329 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15330 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15331 #if TARGET_SUPPORTS_WIDE_INT == 0
15332 if (!SCALAR_FLOAT_MODE_P (mode))
15334 mem_loc_result->dw_loc_oprnd2.val_class
15335 = dw_val_class_const_double;
15336 mem_loc_result->dw_loc_oprnd2.v.val_double
15337 = rtx_to_double_int (rtl);
15339 else
15340 #endif
15342 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15343 unsigned int length = GET_MODE_SIZE (float_mode);
15344 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15346 insert_float (rtl, array);
15347 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15348 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15349 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15350 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15353 break;
15355 case CONST_WIDE_INT:
15356 if (!dwarf_strict || dwarf_version >= 5)
15358 dw_die_ref type_die;
15360 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15361 if (type_die == NULL)
15362 return NULL;
15363 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15364 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15365 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15366 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15367 mem_loc_result->dw_loc_oprnd2.val_class
15368 = dw_val_class_wide_int;
15369 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15370 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15372 break;
15374 case EQ:
15375 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15376 break;
15378 case GE:
15379 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15380 break;
15382 case GT:
15383 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15384 break;
15386 case LE:
15387 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15388 break;
15390 case LT:
15391 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15392 break;
15394 case NE:
15395 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15396 break;
15398 case GEU:
15399 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15400 break;
15402 case GTU:
15403 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15404 break;
15406 case LEU:
15407 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15408 break;
15410 case LTU:
15411 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15412 break;
15414 case UMIN:
15415 case UMAX:
15416 if (!SCALAR_INT_MODE_P (mode))
15417 break;
15418 /* FALLTHRU */
15419 case SMIN:
15420 case SMAX:
15421 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15422 break;
15424 case ZERO_EXTRACT:
15425 case SIGN_EXTRACT:
15426 if (CONST_INT_P (XEXP (rtl, 1))
15427 && CONST_INT_P (XEXP (rtl, 2))
15428 && is_a <scalar_int_mode> (mode, &int_mode)
15429 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15430 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15431 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15432 && ((unsigned) INTVAL (XEXP (rtl, 1))
15433 + (unsigned) INTVAL (XEXP (rtl, 2))
15434 <= GET_MODE_BITSIZE (int_mode)))
15436 int shift, size;
15437 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15438 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15439 if (op0 == 0)
15440 break;
15441 if (GET_CODE (rtl) == SIGN_EXTRACT)
15442 op = DW_OP_shra;
15443 else
15444 op = DW_OP_shr;
15445 mem_loc_result = op0;
15446 size = INTVAL (XEXP (rtl, 1));
15447 shift = INTVAL (XEXP (rtl, 2));
15448 if (BITS_BIG_ENDIAN)
15449 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15450 if (shift + size != (int) DWARF2_ADDR_SIZE)
15452 add_loc_descr (&mem_loc_result,
15453 int_loc_descriptor (DWARF2_ADDR_SIZE
15454 - shift - size));
15455 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15457 if (size != (int) DWARF2_ADDR_SIZE)
15459 add_loc_descr (&mem_loc_result,
15460 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15461 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15464 break;
15466 case IF_THEN_ELSE:
15468 dw_loc_descr_ref op2, bra_node, drop_node;
15469 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15470 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15471 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15472 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15473 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15474 VAR_INIT_STATUS_INITIALIZED);
15475 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15476 VAR_INIT_STATUS_INITIALIZED);
15477 if (op0 == NULL || op1 == NULL || op2 == NULL)
15478 break;
15480 mem_loc_result = op1;
15481 add_loc_descr (&mem_loc_result, op2);
15482 add_loc_descr (&mem_loc_result, op0);
15483 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15484 add_loc_descr (&mem_loc_result, bra_node);
15485 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15486 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15487 add_loc_descr (&mem_loc_result, drop_node);
15488 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15489 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15491 break;
15493 case FLOAT_EXTEND:
15494 case FLOAT_TRUNCATE:
15495 case FLOAT:
15496 case UNSIGNED_FLOAT:
15497 case FIX:
15498 case UNSIGNED_FIX:
15499 if (!dwarf_strict || dwarf_version >= 5)
15501 dw_die_ref type_die;
15502 dw_loc_descr_ref cvt;
15504 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15505 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15506 if (op0 == NULL)
15507 break;
15508 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15509 && (GET_CODE (rtl) == FLOAT
15510 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15512 type_die = base_type_for_mode (int_mode,
15513 GET_CODE (rtl) == UNSIGNED_FLOAT);
15514 if (type_die == NULL)
15515 break;
15516 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15517 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15518 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15519 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15520 add_loc_descr (&op0, cvt);
15522 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15523 if (type_die == NULL)
15524 break;
15525 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15526 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15527 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15528 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15529 add_loc_descr (&op0, cvt);
15530 if (is_a <scalar_int_mode> (mode, &int_mode)
15531 && (GET_CODE (rtl) == FIX
15532 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15534 op0 = convert_descriptor_to_mode (int_mode, op0);
15535 if (op0 == NULL)
15536 break;
15538 mem_loc_result = op0;
15540 break;
15542 case CLZ:
15543 case CTZ:
15544 case FFS:
15545 if (is_a <scalar_int_mode> (mode, &int_mode))
15546 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15547 break;
15549 case POPCOUNT:
15550 case PARITY:
15551 if (is_a <scalar_int_mode> (mode, &int_mode))
15552 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15553 break;
15555 case BSWAP:
15556 if (is_a <scalar_int_mode> (mode, &int_mode))
15557 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15558 break;
15560 case ROTATE:
15561 case ROTATERT:
15562 if (is_a <scalar_int_mode> (mode, &int_mode))
15563 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15564 break;
15566 case COMPARE:
15567 /* In theory, we could implement the above. */
15568 /* DWARF cannot represent the unsigned compare operations
15569 natively. */
15570 case SS_MULT:
15571 case US_MULT:
15572 case SS_DIV:
15573 case US_DIV:
15574 case SS_PLUS:
15575 case US_PLUS:
15576 case SS_MINUS:
15577 case US_MINUS:
15578 case SS_NEG:
15579 case US_NEG:
15580 case SS_ABS:
15581 case SS_ASHIFT:
15582 case US_ASHIFT:
15583 case SS_TRUNCATE:
15584 case US_TRUNCATE:
15585 case UNORDERED:
15586 case ORDERED:
15587 case UNEQ:
15588 case UNGE:
15589 case UNGT:
15590 case UNLE:
15591 case UNLT:
15592 case LTGT:
15593 case FRACT_CONVERT:
15594 case UNSIGNED_FRACT_CONVERT:
15595 case SAT_FRACT:
15596 case UNSIGNED_SAT_FRACT:
15597 case SQRT:
15598 case ASM_OPERANDS:
15599 case VEC_MERGE:
15600 case VEC_SELECT:
15601 case VEC_CONCAT:
15602 case VEC_DUPLICATE:
15603 case UNSPEC:
15604 case HIGH:
15605 case FMA:
15606 case STRICT_LOW_PART:
15607 case CONST_VECTOR:
15608 case CONST_FIXED:
15609 case CLRSB:
15610 case CLOBBER:
15611 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15612 can't express it in the debug info. This can happen e.g. with some
15613 TLS UNSPECs. */
15614 break;
15616 case CONST_STRING:
15617 resolve_one_addr (&rtl);
15618 goto symref;
15620 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15621 the expression. An UNSPEC rtx represents a raw DWARF operation,
15622 new_loc_descr is called for it to build the operation directly.
15623 Otherwise mem_loc_descriptor is called recursively. */
15624 case PARALLEL:
15626 int index = 0;
15627 dw_loc_descr_ref exp_result = NULL;
15629 for (; index < XVECLEN (rtl, 0); index++)
15631 rtx elem = XVECEXP (rtl, 0, index);
15632 if (GET_CODE (elem) == UNSPEC)
15634 /* Each DWARF operation UNSPEC contain two operands, if
15635 one operand is not used for the operation, const0_rtx is
15636 passed. */
15637 gcc_assert (XVECLEN (elem, 0) == 2);
15639 HOST_WIDE_INT dw_op = XINT (elem, 1);
15640 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15641 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15642 exp_result
15643 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15644 oprnd2);
15646 else
15647 exp_result
15648 = mem_loc_descriptor (elem, mode, mem_mode,
15649 VAR_INIT_STATUS_INITIALIZED);
15651 if (!mem_loc_result)
15652 mem_loc_result = exp_result;
15653 else
15654 add_loc_descr (&mem_loc_result, exp_result);
15657 break;
15660 default:
15661 if (flag_checking)
15663 print_rtl (stderr, rtl);
15664 gcc_unreachable ();
15666 break;
15669 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15670 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15672 return mem_loc_result;
15675 /* Return a descriptor that describes the concatenation of two locations.
15676 This is typically a complex variable. */
15678 static dw_loc_descr_ref
15679 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15681 dw_loc_descr_ref cc_loc_result = NULL;
15682 dw_loc_descr_ref x0_ref
15683 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15684 dw_loc_descr_ref x1_ref
15685 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15687 if (x0_ref == 0 || x1_ref == 0)
15688 return 0;
15690 cc_loc_result = x0_ref;
15691 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15693 add_loc_descr (&cc_loc_result, x1_ref);
15694 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15696 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15697 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15699 return cc_loc_result;
15702 /* Return a descriptor that describes the concatenation of N
15703 locations. */
15705 static dw_loc_descr_ref
15706 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15708 unsigned int i;
15709 dw_loc_descr_ref cc_loc_result = NULL;
15710 unsigned int n = XVECLEN (concatn, 0);
15712 for (i = 0; i < n; ++i)
15714 dw_loc_descr_ref ref;
15715 rtx x = XVECEXP (concatn, 0, i);
15717 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15718 if (ref == NULL)
15719 return NULL;
15721 add_loc_descr (&cc_loc_result, ref);
15722 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15725 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15726 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15728 return cc_loc_result;
15731 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15732 for DEBUG_IMPLICIT_PTR RTL. */
15734 static dw_loc_descr_ref
15735 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15737 dw_loc_descr_ref ret;
15738 dw_die_ref ref;
15740 if (dwarf_strict && dwarf_version < 5)
15741 return NULL;
15742 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15743 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15744 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15745 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15746 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15747 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15748 if (ref)
15750 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15751 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15752 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15754 else
15756 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15757 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15759 return ret;
15762 /* Output a proper Dwarf location descriptor for a variable or parameter
15763 which is either allocated in a register or in a memory location. For a
15764 register, we just generate an OP_REG and the register number. For a
15765 memory location we provide a Dwarf postfix expression describing how to
15766 generate the (dynamic) address of the object onto the address stack.
15768 MODE is mode of the decl if this loc_descriptor is going to be used in
15769 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15770 allowed, VOIDmode otherwise.
15772 If we don't know how to describe it, return 0. */
15774 static dw_loc_descr_ref
15775 loc_descriptor (rtx rtl, machine_mode mode,
15776 enum var_init_status initialized)
15778 dw_loc_descr_ref loc_result = NULL;
15779 scalar_int_mode int_mode;
15781 switch (GET_CODE (rtl))
15783 case SUBREG:
15784 /* The case of a subreg may arise when we have a local (register)
15785 variable or a formal (register) parameter which doesn't quite fill
15786 up an entire register. For now, just assume that it is
15787 legitimate to make the Dwarf info refer to the whole register which
15788 contains the given subreg. */
15789 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15790 loc_result = loc_descriptor (SUBREG_REG (rtl),
15791 GET_MODE (SUBREG_REG (rtl)), initialized);
15792 else
15793 goto do_default;
15794 break;
15796 case REG:
15797 loc_result = reg_loc_descriptor (rtl, initialized);
15798 break;
15800 case MEM:
15801 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15802 GET_MODE (rtl), initialized);
15803 if (loc_result == NULL)
15804 loc_result = tls_mem_loc_descriptor (rtl);
15805 if (loc_result == NULL)
15807 rtx new_rtl = avoid_constant_pool_reference (rtl);
15808 if (new_rtl != rtl)
15809 loc_result = loc_descriptor (new_rtl, mode, initialized);
15811 break;
15813 case CONCAT:
15814 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15815 initialized);
15816 break;
15818 case CONCATN:
15819 loc_result = concatn_loc_descriptor (rtl, initialized);
15820 break;
15822 case VAR_LOCATION:
15823 /* Single part. */
15824 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15826 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15827 if (GET_CODE (loc) == EXPR_LIST)
15828 loc = XEXP (loc, 0);
15829 loc_result = loc_descriptor (loc, mode, initialized);
15830 break;
15833 rtl = XEXP (rtl, 1);
15834 /* FALLTHRU */
15836 case PARALLEL:
15838 rtvec par_elems = XVEC (rtl, 0);
15839 int num_elem = GET_NUM_ELEM (par_elems);
15840 machine_mode mode;
15841 int i;
15843 /* Create the first one, so we have something to add to. */
15844 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15845 VOIDmode, initialized);
15846 if (loc_result == NULL)
15847 return NULL;
15848 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15849 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15850 for (i = 1; i < num_elem; i++)
15852 dw_loc_descr_ref temp;
15854 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15855 VOIDmode, initialized);
15856 if (temp == NULL)
15857 return NULL;
15858 add_loc_descr (&loc_result, temp);
15859 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15860 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15863 break;
15865 case CONST_INT:
15866 if (mode != VOIDmode && mode != BLKmode)
15868 int_mode = as_a <scalar_int_mode> (mode);
15869 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15870 INTVAL (rtl));
15872 break;
15874 case CONST_DOUBLE:
15875 if (mode == VOIDmode)
15876 mode = GET_MODE (rtl);
15878 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15880 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15882 /* Note that a CONST_DOUBLE rtx could represent either an integer
15883 or a floating-point constant. A CONST_DOUBLE is used whenever
15884 the constant requires more than one word in order to be
15885 adequately represented. We output CONST_DOUBLEs as blocks. */
15886 scalar_mode smode = as_a <scalar_mode> (mode);
15887 loc_result = new_loc_descr (DW_OP_implicit_value,
15888 GET_MODE_SIZE (smode), 0);
15889 #if TARGET_SUPPORTS_WIDE_INT == 0
15890 if (!SCALAR_FLOAT_MODE_P (smode))
15892 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15893 loc_result->dw_loc_oprnd2.v.val_double
15894 = rtx_to_double_int (rtl);
15896 else
15897 #endif
15899 unsigned int length = GET_MODE_SIZE (smode);
15900 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15902 insert_float (rtl, array);
15903 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15904 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15905 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15906 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15909 break;
15911 case CONST_WIDE_INT:
15912 if (mode == VOIDmode)
15913 mode = GET_MODE (rtl);
15915 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15917 int_mode = as_a <scalar_int_mode> (mode);
15918 loc_result = new_loc_descr (DW_OP_implicit_value,
15919 GET_MODE_SIZE (int_mode), 0);
15920 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15921 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15922 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15924 break;
15926 case CONST_VECTOR:
15927 if (mode == VOIDmode)
15928 mode = GET_MODE (rtl);
15930 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15932 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15933 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15934 unsigned char *array
15935 = ggc_vec_alloc<unsigned char> (length * elt_size);
15936 unsigned int i;
15937 unsigned char *p;
15938 machine_mode imode = GET_MODE_INNER (mode);
15940 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15941 switch (GET_MODE_CLASS (mode))
15943 case MODE_VECTOR_INT:
15944 for (i = 0, p = array; i < length; i++, p += elt_size)
15946 rtx elt = CONST_VECTOR_ELT (rtl, i);
15947 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15949 break;
15951 case MODE_VECTOR_FLOAT:
15952 for (i = 0, p = array; i < length; i++, p += elt_size)
15954 rtx elt = CONST_VECTOR_ELT (rtl, i);
15955 insert_float (elt, p);
15957 break;
15959 default:
15960 gcc_unreachable ();
15963 loc_result = new_loc_descr (DW_OP_implicit_value,
15964 length * elt_size, 0);
15965 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15966 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15967 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15968 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15970 break;
15972 case CONST:
15973 if (mode == VOIDmode
15974 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15975 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15976 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15978 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15979 break;
15981 /* FALLTHROUGH */
15982 case SYMBOL_REF:
15983 if (!const_ok_for_output (rtl))
15984 break;
15985 /* FALLTHROUGH */
15986 case LABEL_REF:
15987 if (is_a <scalar_int_mode> (mode, &int_mode)
15988 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15989 && (dwarf_version >= 4 || !dwarf_strict))
15991 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15992 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15993 vec_safe_push (used_rtx_array, rtl);
15995 break;
15997 case DEBUG_IMPLICIT_PTR:
15998 loc_result = implicit_ptr_descriptor (rtl, 0);
15999 break;
16001 case PLUS:
16002 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16003 && CONST_INT_P (XEXP (rtl, 1)))
16005 loc_result
16006 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16007 break;
16009 /* FALLTHRU */
16010 do_default:
16011 default:
16012 if ((is_a <scalar_int_mode> (mode, &int_mode)
16013 && GET_MODE (rtl) == int_mode
16014 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16015 && dwarf_version >= 4)
16016 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16018 /* Value expression. */
16019 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16020 if (loc_result)
16021 add_loc_descr (&loc_result,
16022 new_loc_descr (DW_OP_stack_value, 0, 0));
16024 break;
16027 return loc_result;
16030 /* We need to figure out what section we should use as the base for the
16031 address ranges where a given location is valid.
16032 1. If this particular DECL has a section associated with it, use that.
16033 2. If this function has a section associated with it, use that.
16034 3. Otherwise, use the text section.
16035 XXX: If you split a variable across multiple sections, we won't notice. */
16037 static const char *
16038 secname_for_decl (const_tree decl)
16040 const char *secname;
16042 if (VAR_OR_FUNCTION_DECL_P (decl)
16043 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16044 && DECL_SECTION_NAME (decl))
16045 secname = DECL_SECTION_NAME (decl);
16046 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16047 secname = DECL_SECTION_NAME (current_function_decl);
16048 else if (cfun && in_cold_section_p)
16049 secname = crtl->subsections.cold_section_label;
16050 else
16051 secname = text_section_label;
16053 return secname;
16056 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16058 static bool
16059 decl_by_reference_p (tree decl)
16061 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16062 || VAR_P (decl))
16063 && DECL_BY_REFERENCE (decl));
16066 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16067 for VARLOC. */
16069 static dw_loc_descr_ref
16070 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16071 enum var_init_status initialized)
16073 int have_address = 0;
16074 dw_loc_descr_ref descr;
16075 machine_mode mode;
16077 if (want_address != 2)
16079 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16080 /* Single part. */
16081 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16083 varloc = PAT_VAR_LOCATION_LOC (varloc);
16084 if (GET_CODE (varloc) == EXPR_LIST)
16085 varloc = XEXP (varloc, 0);
16086 mode = GET_MODE (varloc);
16087 if (MEM_P (varloc))
16089 rtx addr = XEXP (varloc, 0);
16090 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16091 mode, initialized);
16092 if (descr)
16093 have_address = 1;
16094 else
16096 rtx x = avoid_constant_pool_reference (varloc);
16097 if (x != varloc)
16098 descr = mem_loc_descriptor (x, mode, VOIDmode,
16099 initialized);
16102 else
16103 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16105 else
16106 return 0;
16108 else
16110 if (GET_CODE (varloc) == VAR_LOCATION)
16111 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16112 else
16113 mode = DECL_MODE (loc);
16114 descr = loc_descriptor (varloc, mode, initialized);
16115 have_address = 1;
16118 if (!descr)
16119 return 0;
16121 if (want_address == 2 && !have_address
16122 && (dwarf_version >= 4 || !dwarf_strict))
16124 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16126 expansion_failed (loc, NULL_RTX,
16127 "DWARF address size mismatch");
16128 return 0;
16130 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16131 have_address = 1;
16133 /* Show if we can't fill the request for an address. */
16134 if (want_address && !have_address)
16136 expansion_failed (loc, NULL_RTX,
16137 "Want address and only have value");
16138 return 0;
16141 /* If we've got an address and don't want one, dereference. */
16142 if (!want_address && have_address)
16144 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16145 enum dwarf_location_atom op;
16147 if (size > DWARF2_ADDR_SIZE || size == -1)
16149 expansion_failed (loc, NULL_RTX,
16150 "DWARF address size mismatch");
16151 return 0;
16153 else if (size == DWARF2_ADDR_SIZE)
16154 op = DW_OP_deref;
16155 else
16156 op = DW_OP_deref_size;
16158 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16161 return descr;
16164 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16165 if it is not possible. */
16167 static dw_loc_descr_ref
16168 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16170 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16171 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16172 else if (dwarf_version >= 3 || !dwarf_strict)
16173 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16174 else
16175 return NULL;
16178 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16179 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16181 static dw_loc_descr_ref
16182 dw_sra_loc_expr (tree decl, rtx loc)
16184 rtx p;
16185 unsigned HOST_WIDE_INT padsize = 0;
16186 dw_loc_descr_ref descr, *descr_tail;
16187 unsigned HOST_WIDE_INT decl_size;
16188 rtx varloc;
16189 enum var_init_status initialized;
16191 if (DECL_SIZE (decl) == NULL
16192 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16193 return NULL;
16195 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16196 descr = NULL;
16197 descr_tail = &descr;
16199 for (p = loc; p; p = XEXP (p, 1))
16201 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16202 rtx loc_note = *decl_piece_varloc_ptr (p);
16203 dw_loc_descr_ref cur_descr;
16204 dw_loc_descr_ref *tail, last = NULL;
16205 unsigned HOST_WIDE_INT opsize = 0;
16207 if (loc_note == NULL_RTX
16208 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16210 padsize += bitsize;
16211 continue;
16213 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16214 varloc = NOTE_VAR_LOCATION (loc_note);
16215 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16216 if (cur_descr == NULL)
16218 padsize += bitsize;
16219 continue;
16222 /* Check that cur_descr either doesn't use
16223 DW_OP_*piece operations, or their sum is equal
16224 to bitsize. Otherwise we can't embed it. */
16225 for (tail = &cur_descr; *tail != NULL;
16226 tail = &(*tail)->dw_loc_next)
16227 if ((*tail)->dw_loc_opc == DW_OP_piece)
16229 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16230 * BITS_PER_UNIT;
16231 last = *tail;
16233 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16235 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16236 last = *tail;
16239 if (last != NULL && opsize != bitsize)
16241 padsize += bitsize;
16242 /* Discard the current piece of the descriptor and release any
16243 addr_table entries it uses. */
16244 remove_loc_list_addr_table_entries (cur_descr);
16245 continue;
16248 /* If there is a hole, add DW_OP_*piece after empty DWARF
16249 expression, which means that those bits are optimized out. */
16250 if (padsize)
16252 if (padsize > decl_size)
16254 remove_loc_list_addr_table_entries (cur_descr);
16255 goto discard_descr;
16257 decl_size -= padsize;
16258 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16259 if (*descr_tail == NULL)
16261 remove_loc_list_addr_table_entries (cur_descr);
16262 goto discard_descr;
16264 descr_tail = &(*descr_tail)->dw_loc_next;
16265 padsize = 0;
16267 *descr_tail = cur_descr;
16268 descr_tail = tail;
16269 if (bitsize > decl_size)
16270 goto discard_descr;
16271 decl_size -= bitsize;
16272 if (last == NULL)
16274 HOST_WIDE_INT offset = 0;
16275 if (GET_CODE (varloc) == VAR_LOCATION
16276 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16278 varloc = PAT_VAR_LOCATION_LOC (varloc);
16279 if (GET_CODE (varloc) == EXPR_LIST)
16280 varloc = XEXP (varloc, 0);
16284 if (GET_CODE (varloc) == CONST
16285 || GET_CODE (varloc) == SIGN_EXTEND
16286 || GET_CODE (varloc) == ZERO_EXTEND)
16287 varloc = XEXP (varloc, 0);
16288 else if (GET_CODE (varloc) == SUBREG)
16289 varloc = SUBREG_REG (varloc);
16290 else
16291 break;
16293 while (1);
16294 /* DW_OP_bit_size offset should be zero for register
16295 or implicit location descriptions and empty location
16296 descriptions, but for memory addresses needs big endian
16297 adjustment. */
16298 if (MEM_P (varloc))
16300 unsigned HOST_WIDE_INT memsize
16301 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16302 if (memsize != bitsize)
16304 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16305 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16306 goto discard_descr;
16307 if (memsize < bitsize)
16308 goto discard_descr;
16309 if (BITS_BIG_ENDIAN)
16310 offset = memsize - bitsize;
16314 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16315 if (*descr_tail == NULL)
16316 goto discard_descr;
16317 descr_tail = &(*descr_tail)->dw_loc_next;
16321 /* If there were any non-empty expressions, add padding till the end of
16322 the decl. */
16323 if (descr != NULL && decl_size != 0)
16325 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16326 if (*descr_tail == NULL)
16327 goto discard_descr;
16329 return descr;
16331 discard_descr:
16332 /* Discard the descriptor and release any addr_table entries it uses. */
16333 remove_loc_list_addr_table_entries (descr);
16334 return NULL;
16337 /* Return the dwarf representation of the location list LOC_LIST of
16338 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16339 function. */
16341 static dw_loc_list_ref
16342 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16344 const char *endname, *secname;
16345 rtx varloc;
16346 enum var_init_status initialized;
16347 struct var_loc_node *node;
16348 dw_loc_descr_ref descr;
16349 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16350 dw_loc_list_ref list = NULL;
16351 dw_loc_list_ref *listp = &list;
16353 /* Now that we know what section we are using for a base,
16354 actually construct the list of locations.
16355 The first location information is what is passed to the
16356 function that creates the location list, and the remaining
16357 locations just get added on to that list.
16358 Note that we only know the start address for a location
16359 (IE location changes), so to build the range, we use
16360 the range [current location start, next location start].
16361 This means we have to special case the last node, and generate
16362 a range of [last location start, end of function label]. */
16364 secname = secname_for_decl (decl);
16366 for (node = loc_list->first; node; node = node->next)
16367 if (GET_CODE (node->loc) == EXPR_LIST
16368 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16370 if (GET_CODE (node->loc) == EXPR_LIST)
16372 /* This requires DW_OP_{,bit_}piece, which is not usable
16373 inside DWARF expressions. */
16374 if (want_address != 2)
16375 continue;
16376 descr = dw_sra_loc_expr (decl, node->loc);
16377 if (descr == NULL)
16378 continue;
16380 else
16382 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16383 varloc = NOTE_VAR_LOCATION (node->loc);
16384 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16386 if (descr)
16388 bool range_across_switch = false;
16389 /* If section switch happens in between node->label
16390 and node->next->label (or end of function) and
16391 we can't emit it as a single entry list,
16392 emit two ranges, first one ending at the end
16393 of first partition and second one starting at the
16394 beginning of second partition. */
16395 if (node == loc_list->last_before_switch
16396 && (node != loc_list->first || loc_list->first->next)
16397 && current_function_decl)
16399 endname = cfun->fde->dw_fde_end;
16400 range_across_switch = true;
16402 /* The variable has a location between NODE->LABEL and
16403 NODE->NEXT->LABEL. */
16404 else if (node->next)
16405 endname = node->next->label;
16406 /* If the variable has a location at the last label
16407 it keeps its location until the end of function. */
16408 else if (!current_function_decl)
16409 endname = text_end_label;
16410 else
16412 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16413 current_function_funcdef_no);
16414 endname = ggc_strdup (label_id);
16417 *listp = new_loc_list (descr, node->label, endname, secname);
16418 if (TREE_CODE (decl) == PARM_DECL
16419 && node == loc_list->first
16420 && NOTE_P (node->loc)
16421 && strcmp (node->label, endname) == 0)
16422 (*listp)->force = true;
16423 listp = &(*listp)->dw_loc_next;
16425 if (range_across_switch)
16427 if (GET_CODE (node->loc) == EXPR_LIST)
16428 descr = dw_sra_loc_expr (decl, node->loc);
16429 else
16431 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16432 varloc = NOTE_VAR_LOCATION (node->loc);
16433 descr = dw_loc_list_1 (decl, varloc, want_address,
16434 initialized);
16436 gcc_assert (descr);
16437 /* The variable has a location between NODE->LABEL and
16438 NODE->NEXT->LABEL. */
16439 if (node->next)
16440 endname = node->next->label;
16441 else
16442 endname = cfun->fde->dw_fde_second_end;
16443 *listp = new_loc_list (descr,
16444 cfun->fde->dw_fde_second_begin,
16445 endname, secname);
16446 listp = &(*listp)->dw_loc_next;
16451 /* Try to avoid the overhead of a location list emitting a location
16452 expression instead, but only if we didn't have more than one
16453 location entry in the first place. If some entries were not
16454 representable, we don't want to pretend a single entry that was
16455 applies to the entire scope in which the variable is
16456 available. */
16457 if (list && loc_list->first->next)
16458 gen_llsym (list);
16460 return list;
16463 /* Return if the loc_list has only single element and thus can be represented
16464 as location description. */
16466 static bool
16467 single_element_loc_list_p (dw_loc_list_ref list)
16469 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16470 return !list->ll_symbol;
16473 /* Duplicate a single element of location list. */
16475 static inline dw_loc_descr_ref
16476 copy_loc_descr (dw_loc_descr_ref ref)
16478 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16479 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16480 return copy;
16483 /* To each location in list LIST append loc descr REF. */
16485 static void
16486 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16488 dw_loc_descr_ref copy;
16489 add_loc_descr (&list->expr, ref);
16490 list = list->dw_loc_next;
16491 while (list)
16493 copy = copy_loc_descr (ref);
16494 add_loc_descr (&list->expr, copy);
16495 while (copy->dw_loc_next)
16496 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16497 list = list->dw_loc_next;
16501 /* To each location in list LIST prepend loc descr REF. */
16503 static void
16504 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16506 dw_loc_descr_ref copy;
16507 dw_loc_descr_ref ref_end = list->expr;
16508 add_loc_descr (&ref, list->expr);
16509 list->expr = ref;
16510 list = list->dw_loc_next;
16511 while (list)
16513 dw_loc_descr_ref end = list->expr;
16514 list->expr = copy = copy_loc_descr (ref);
16515 while (copy->dw_loc_next != ref_end)
16516 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16517 copy->dw_loc_next = end;
16518 list = list->dw_loc_next;
16522 /* Given two lists RET and LIST
16523 produce location list that is result of adding expression in LIST
16524 to expression in RET on each position in program.
16525 Might be destructive on both RET and LIST.
16527 TODO: We handle only simple cases of RET or LIST having at most one
16528 element. General case would involve sorting the lists in program order
16529 and merging them that will need some additional work.
16530 Adding that will improve quality of debug info especially for SRA-ed
16531 structures. */
16533 static void
16534 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16536 if (!list)
16537 return;
16538 if (!*ret)
16540 *ret = list;
16541 return;
16543 if (!list->dw_loc_next)
16545 add_loc_descr_to_each (*ret, list->expr);
16546 return;
16548 if (!(*ret)->dw_loc_next)
16550 prepend_loc_descr_to_each (list, (*ret)->expr);
16551 *ret = list;
16552 return;
16554 expansion_failed (NULL_TREE, NULL_RTX,
16555 "Don't know how to merge two non-trivial"
16556 " location lists.\n");
16557 *ret = NULL;
16558 return;
16561 /* LOC is constant expression. Try a luck, look it up in constant
16562 pool and return its loc_descr of its address. */
16564 static dw_loc_descr_ref
16565 cst_pool_loc_descr (tree loc)
16567 /* Get an RTL for this, if something has been emitted. */
16568 rtx rtl = lookup_constant_def (loc);
16570 if (!rtl || !MEM_P (rtl))
16572 gcc_assert (!rtl);
16573 return 0;
16575 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16577 /* TODO: We might get more coverage if we was actually delaying expansion
16578 of all expressions till end of compilation when constant pools are fully
16579 populated. */
16580 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16582 expansion_failed (loc, NULL_RTX,
16583 "CST value in contant pool but not marked.");
16584 return 0;
16586 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16587 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16590 /* Return dw_loc_list representing address of addr_expr LOC
16591 by looking for inner INDIRECT_REF expression and turning
16592 it into simple arithmetics.
16594 See loc_list_from_tree for the meaning of CONTEXT. */
16596 static dw_loc_list_ref
16597 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16598 loc_descr_context *context)
16600 tree obj, offset;
16601 HOST_WIDE_INT bitsize, bitpos, bytepos;
16602 machine_mode mode;
16603 int unsignedp, reversep, volatilep = 0;
16604 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16606 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16607 &bitsize, &bitpos, &offset, &mode,
16608 &unsignedp, &reversep, &volatilep);
16609 STRIP_NOPS (obj);
16610 if (bitpos % BITS_PER_UNIT)
16612 expansion_failed (loc, NULL_RTX, "bitfield access");
16613 return 0;
16615 if (!INDIRECT_REF_P (obj))
16617 expansion_failed (obj,
16618 NULL_RTX, "no indirect ref in inner refrence");
16619 return 0;
16621 if (!offset && !bitpos)
16622 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16623 context);
16624 else if (toplev
16625 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16626 && (dwarf_version >= 4 || !dwarf_strict))
16628 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16629 if (!list_ret)
16630 return 0;
16631 if (offset)
16633 /* Variable offset. */
16634 list_ret1 = loc_list_from_tree (offset, 0, context);
16635 if (list_ret1 == 0)
16636 return 0;
16637 add_loc_list (&list_ret, list_ret1);
16638 if (!list_ret)
16639 return 0;
16640 add_loc_descr_to_each (list_ret,
16641 new_loc_descr (DW_OP_plus, 0, 0));
16643 bytepos = bitpos / BITS_PER_UNIT;
16644 if (bytepos > 0)
16645 add_loc_descr_to_each (list_ret,
16646 new_loc_descr (DW_OP_plus_uconst,
16647 bytepos, 0));
16648 else if (bytepos < 0)
16649 loc_list_plus_const (list_ret, bytepos);
16650 add_loc_descr_to_each (list_ret,
16651 new_loc_descr (DW_OP_stack_value, 0, 0));
16653 return list_ret;
16656 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16657 all operations from LOC are nops, move to the last one. Insert in NOPS all
16658 operations that are skipped. */
16660 static void
16661 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16662 hash_set<dw_loc_descr_ref> &nops)
16664 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16666 nops.add (loc);
16667 loc = loc->dw_loc_next;
16671 /* Helper for loc_descr_without_nops: free the location description operation
16672 P. */
16674 bool
16675 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16677 ggc_free (loc);
16678 return true;
16681 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16682 finishes LOC. */
16684 static void
16685 loc_descr_without_nops (dw_loc_descr_ref &loc)
16687 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16688 return;
16690 /* Set of all DW_OP_nop operations we remove. */
16691 hash_set<dw_loc_descr_ref> nops;
16693 /* First, strip all prefix NOP operations in order to keep the head of the
16694 operations list. */
16695 loc_descr_to_next_no_nop (loc, nops);
16697 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16699 /* For control flow operations: strip "prefix" nops in destination
16700 labels. */
16701 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16702 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16703 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16704 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16706 /* Do the same for the operations that follow, then move to the next
16707 iteration. */
16708 if (cur->dw_loc_next != NULL)
16709 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16710 cur = cur->dw_loc_next;
16713 nops.traverse<void *, free_loc_descr> (NULL);
16717 struct dwarf_procedure_info;
16719 /* Helper structure for location descriptions generation. */
16720 struct loc_descr_context
16722 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16723 NULL_TREE if DW_OP_push_object_address in invalid for this location
16724 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16725 tree context_type;
16726 /* The ..._DECL node that should be translated as a
16727 DW_OP_push_object_address operation. */
16728 tree base_decl;
16729 /* Information about the DWARF procedure we are currently generating. NULL if
16730 we are not generating a DWARF procedure. */
16731 struct dwarf_procedure_info *dpi;
16732 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16733 by consumer. Used for DW_TAG_generic_subrange attributes. */
16734 bool placeholder_arg;
16735 /* True if PLACEHOLDER_EXPR has been seen. */
16736 bool placeholder_seen;
16739 /* DWARF procedures generation
16741 DWARF expressions (aka. location descriptions) are used to encode variable
16742 things such as sizes or offsets. Such computations can have redundant parts
16743 that can be factorized in order to reduce the size of the output debug
16744 information. This is the whole point of DWARF procedures.
16746 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16747 already factorized into functions ("size functions") in order to handle very
16748 big and complex types. Such functions are quite simple: they have integral
16749 arguments, they return an integral result and their body contains only a
16750 return statement with arithmetic expressions. This is the only kind of
16751 function we are interested in translating into DWARF procedures, here.
16753 DWARF expressions and DWARF procedure are executed using a stack, so we have
16754 to define some calling convention for them to interact. Let's say that:
16756 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16757 all arguments in reverse order (right-to-left) so that when the DWARF
16758 procedure execution starts, the first argument is the top of the stack.
16760 - Then, when returning, the DWARF procedure must have consumed all arguments
16761 on the stack, must have pushed the result and touched nothing else.
16763 - Each integral argument and the result are integral types can be hold in a
16764 single stack slot.
16766 - We call "frame offset" the number of stack slots that are "under DWARF
16767 procedure control": it includes the arguments slots, the temporaries and
16768 the result slot. Thus, it is equal to the number of arguments when the
16769 procedure execution starts and must be equal to one (the result) when it
16770 returns. */
16772 /* Helper structure used when generating operations for a DWARF procedure. */
16773 struct dwarf_procedure_info
16775 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16776 currently translated. */
16777 tree fndecl;
16778 /* The number of arguments FNDECL takes. */
16779 unsigned args_count;
16782 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16783 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16784 equate it to this DIE. */
16786 static dw_die_ref
16787 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16788 dw_die_ref parent_die)
16790 dw_die_ref dwarf_proc_die;
16792 if ((dwarf_version < 3 && dwarf_strict)
16793 || location == NULL)
16794 return NULL;
16796 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16797 if (fndecl)
16798 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16799 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16800 return dwarf_proc_die;
16803 /* Return whether TYPE is a supported type as a DWARF procedure argument
16804 type or return type (we handle only scalar types and pointer types that
16805 aren't wider than the DWARF expression evaluation stack. */
16807 static bool
16808 is_handled_procedure_type (tree type)
16810 return ((INTEGRAL_TYPE_P (type)
16811 || TREE_CODE (type) == OFFSET_TYPE
16812 || TREE_CODE (type) == POINTER_TYPE)
16813 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16816 /* Helper for resolve_args_picking: do the same but stop when coming across
16817 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16818 offset *before* evaluating the corresponding operation. */
16820 static bool
16821 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16822 struct dwarf_procedure_info *dpi,
16823 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16825 /* The "frame_offset" identifier is already used to name a macro... */
16826 unsigned frame_offset_ = initial_frame_offset;
16827 dw_loc_descr_ref l;
16829 for (l = loc; l != NULL;)
16831 bool existed;
16832 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16834 /* If we already met this node, there is nothing to compute anymore. */
16835 if (existed)
16837 /* Make sure that the stack size is consistent wherever the execution
16838 flow comes from. */
16839 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16840 break;
16842 l_frame_offset = frame_offset_;
16844 /* If needed, relocate the picking offset with respect to the frame
16845 offset. */
16846 if (l->frame_offset_rel)
16848 unsigned HOST_WIDE_INT off;
16849 switch (l->dw_loc_opc)
16851 case DW_OP_pick:
16852 off = l->dw_loc_oprnd1.v.val_unsigned;
16853 break;
16854 case DW_OP_dup:
16855 off = 0;
16856 break;
16857 case DW_OP_over:
16858 off = 1;
16859 break;
16860 default:
16861 gcc_unreachable ();
16863 /* frame_offset_ is the size of the current stack frame, including
16864 incoming arguments. Besides, the arguments are pushed
16865 right-to-left. Thus, in order to access the Nth argument from
16866 this operation node, the picking has to skip temporaries *plus*
16867 one stack slot per argument (0 for the first one, 1 for the second
16868 one, etc.).
16870 The targetted argument number (N) is already set as the operand,
16871 and the number of temporaries can be computed with:
16872 frame_offsets_ - dpi->args_count */
16873 off += frame_offset_ - dpi->args_count;
16875 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16876 if (off > 255)
16877 return false;
16879 if (off == 0)
16881 l->dw_loc_opc = DW_OP_dup;
16882 l->dw_loc_oprnd1.v.val_unsigned = 0;
16884 else if (off == 1)
16886 l->dw_loc_opc = DW_OP_over;
16887 l->dw_loc_oprnd1.v.val_unsigned = 0;
16889 else
16891 l->dw_loc_opc = DW_OP_pick;
16892 l->dw_loc_oprnd1.v.val_unsigned = off;
16896 /* Update frame_offset according to the effect the current operation has
16897 on the stack. */
16898 switch (l->dw_loc_opc)
16900 case DW_OP_deref:
16901 case DW_OP_swap:
16902 case DW_OP_rot:
16903 case DW_OP_abs:
16904 case DW_OP_neg:
16905 case DW_OP_not:
16906 case DW_OP_plus_uconst:
16907 case DW_OP_skip:
16908 case DW_OP_reg0:
16909 case DW_OP_reg1:
16910 case DW_OP_reg2:
16911 case DW_OP_reg3:
16912 case DW_OP_reg4:
16913 case DW_OP_reg5:
16914 case DW_OP_reg6:
16915 case DW_OP_reg7:
16916 case DW_OP_reg8:
16917 case DW_OP_reg9:
16918 case DW_OP_reg10:
16919 case DW_OP_reg11:
16920 case DW_OP_reg12:
16921 case DW_OP_reg13:
16922 case DW_OP_reg14:
16923 case DW_OP_reg15:
16924 case DW_OP_reg16:
16925 case DW_OP_reg17:
16926 case DW_OP_reg18:
16927 case DW_OP_reg19:
16928 case DW_OP_reg20:
16929 case DW_OP_reg21:
16930 case DW_OP_reg22:
16931 case DW_OP_reg23:
16932 case DW_OP_reg24:
16933 case DW_OP_reg25:
16934 case DW_OP_reg26:
16935 case DW_OP_reg27:
16936 case DW_OP_reg28:
16937 case DW_OP_reg29:
16938 case DW_OP_reg30:
16939 case DW_OP_reg31:
16940 case DW_OP_bregx:
16941 case DW_OP_piece:
16942 case DW_OP_deref_size:
16943 case DW_OP_nop:
16944 case DW_OP_bit_piece:
16945 case DW_OP_implicit_value:
16946 case DW_OP_stack_value:
16947 break;
16949 case DW_OP_addr:
16950 case DW_OP_const1u:
16951 case DW_OP_const1s:
16952 case DW_OP_const2u:
16953 case DW_OP_const2s:
16954 case DW_OP_const4u:
16955 case DW_OP_const4s:
16956 case DW_OP_const8u:
16957 case DW_OP_const8s:
16958 case DW_OP_constu:
16959 case DW_OP_consts:
16960 case DW_OP_dup:
16961 case DW_OP_over:
16962 case DW_OP_pick:
16963 case DW_OP_lit0:
16964 case DW_OP_lit1:
16965 case DW_OP_lit2:
16966 case DW_OP_lit3:
16967 case DW_OP_lit4:
16968 case DW_OP_lit5:
16969 case DW_OP_lit6:
16970 case DW_OP_lit7:
16971 case DW_OP_lit8:
16972 case DW_OP_lit9:
16973 case DW_OP_lit10:
16974 case DW_OP_lit11:
16975 case DW_OP_lit12:
16976 case DW_OP_lit13:
16977 case DW_OP_lit14:
16978 case DW_OP_lit15:
16979 case DW_OP_lit16:
16980 case DW_OP_lit17:
16981 case DW_OP_lit18:
16982 case DW_OP_lit19:
16983 case DW_OP_lit20:
16984 case DW_OP_lit21:
16985 case DW_OP_lit22:
16986 case DW_OP_lit23:
16987 case DW_OP_lit24:
16988 case DW_OP_lit25:
16989 case DW_OP_lit26:
16990 case DW_OP_lit27:
16991 case DW_OP_lit28:
16992 case DW_OP_lit29:
16993 case DW_OP_lit30:
16994 case DW_OP_lit31:
16995 case DW_OP_breg0:
16996 case DW_OP_breg1:
16997 case DW_OP_breg2:
16998 case DW_OP_breg3:
16999 case DW_OP_breg4:
17000 case DW_OP_breg5:
17001 case DW_OP_breg6:
17002 case DW_OP_breg7:
17003 case DW_OP_breg8:
17004 case DW_OP_breg9:
17005 case DW_OP_breg10:
17006 case DW_OP_breg11:
17007 case DW_OP_breg12:
17008 case DW_OP_breg13:
17009 case DW_OP_breg14:
17010 case DW_OP_breg15:
17011 case DW_OP_breg16:
17012 case DW_OP_breg17:
17013 case DW_OP_breg18:
17014 case DW_OP_breg19:
17015 case DW_OP_breg20:
17016 case DW_OP_breg21:
17017 case DW_OP_breg22:
17018 case DW_OP_breg23:
17019 case DW_OP_breg24:
17020 case DW_OP_breg25:
17021 case DW_OP_breg26:
17022 case DW_OP_breg27:
17023 case DW_OP_breg28:
17024 case DW_OP_breg29:
17025 case DW_OP_breg30:
17026 case DW_OP_breg31:
17027 case DW_OP_fbreg:
17028 case DW_OP_push_object_address:
17029 case DW_OP_call_frame_cfa:
17030 case DW_OP_GNU_variable_value:
17031 ++frame_offset_;
17032 break;
17034 case DW_OP_drop:
17035 case DW_OP_xderef:
17036 case DW_OP_and:
17037 case DW_OP_div:
17038 case DW_OP_minus:
17039 case DW_OP_mod:
17040 case DW_OP_mul:
17041 case DW_OP_or:
17042 case DW_OP_plus:
17043 case DW_OP_shl:
17044 case DW_OP_shr:
17045 case DW_OP_shra:
17046 case DW_OP_xor:
17047 case DW_OP_bra:
17048 case DW_OP_eq:
17049 case DW_OP_ge:
17050 case DW_OP_gt:
17051 case DW_OP_le:
17052 case DW_OP_lt:
17053 case DW_OP_ne:
17054 case DW_OP_regx:
17055 case DW_OP_xderef_size:
17056 --frame_offset_;
17057 break;
17059 case DW_OP_call2:
17060 case DW_OP_call4:
17061 case DW_OP_call_ref:
17063 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17064 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17066 if (stack_usage == NULL)
17067 return false;
17068 frame_offset_ += *stack_usage;
17069 break;
17072 case DW_OP_implicit_pointer:
17073 case DW_OP_entry_value:
17074 case DW_OP_const_type:
17075 case DW_OP_regval_type:
17076 case DW_OP_deref_type:
17077 case DW_OP_convert:
17078 case DW_OP_reinterpret:
17079 case DW_OP_form_tls_address:
17080 case DW_OP_GNU_push_tls_address:
17081 case DW_OP_GNU_uninit:
17082 case DW_OP_GNU_encoded_addr:
17083 case DW_OP_GNU_implicit_pointer:
17084 case DW_OP_GNU_entry_value:
17085 case DW_OP_GNU_const_type:
17086 case DW_OP_GNU_regval_type:
17087 case DW_OP_GNU_deref_type:
17088 case DW_OP_GNU_convert:
17089 case DW_OP_GNU_reinterpret:
17090 case DW_OP_GNU_parameter_ref:
17091 /* loc_list_from_tree will probably not output these operations for
17092 size functions, so assume they will not appear here. */
17093 /* Fall through... */
17095 default:
17096 gcc_unreachable ();
17099 /* Now, follow the control flow (except subroutine calls). */
17100 switch (l->dw_loc_opc)
17102 case DW_OP_bra:
17103 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17104 frame_offsets))
17105 return false;
17106 /* Fall through. */
17108 case DW_OP_skip:
17109 l = l->dw_loc_oprnd1.v.val_loc;
17110 break;
17112 case DW_OP_stack_value:
17113 return true;
17115 default:
17116 l = l->dw_loc_next;
17117 break;
17121 return true;
17124 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17125 operations) in order to resolve the operand of DW_OP_pick operations that
17126 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17127 offset *before* LOC is executed. Return if all relocations were
17128 successful. */
17130 static bool
17131 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17132 struct dwarf_procedure_info *dpi)
17134 /* Associate to all visited operations the frame offset *before* evaluating
17135 this operation. */
17136 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17138 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17139 frame_offsets);
17142 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17143 Return NULL if it is not possible. */
17145 static dw_die_ref
17146 function_to_dwarf_procedure (tree fndecl)
17148 struct loc_descr_context ctx;
17149 struct dwarf_procedure_info dpi;
17150 dw_die_ref dwarf_proc_die;
17151 tree tree_body = DECL_SAVED_TREE (fndecl);
17152 dw_loc_descr_ref loc_body, epilogue;
17154 tree cursor;
17155 unsigned i;
17157 /* Do not generate multiple DWARF procedures for the same function
17158 declaration. */
17159 dwarf_proc_die = lookup_decl_die (fndecl);
17160 if (dwarf_proc_die != NULL)
17161 return dwarf_proc_die;
17163 /* DWARF procedures are available starting with the DWARFv3 standard. */
17164 if (dwarf_version < 3 && dwarf_strict)
17165 return NULL;
17167 /* We handle only functions for which we still have a body, that return a
17168 supported type and that takes arguments with supported types. Note that
17169 there is no point translating functions that return nothing. */
17170 if (tree_body == NULL_TREE
17171 || DECL_RESULT (fndecl) == NULL_TREE
17172 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17173 return NULL;
17175 for (cursor = DECL_ARGUMENTS (fndecl);
17176 cursor != NULL_TREE;
17177 cursor = TREE_CHAIN (cursor))
17178 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17179 return NULL;
17181 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17182 if (TREE_CODE (tree_body) != RETURN_EXPR)
17183 return NULL;
17184 tree_body = TREE_OPERAND (tree_body, 0);
17185 if (TREE_CODE (tree_body) != MODIFY_EXPR
17186 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17187 return NULL;
17188 tree_body = TREE_OPERAND (tree_body, 1);
17190 /* Try to translate the body expression itself. Note that this will probably
17191 cause an infinite recursion if its call graph has a cycle. This is very
17192 unlikely for size functions, however, so don't bother with such things at
17193 the moment. */
17194 ctx.context_type = NULL_TREE;
17195 ctx.base_decl = NULL_TREE;
17196 ctx.dpi = &dpi;
17197 ctx.placeholder_arg = false;
17198 ctx.placeholder_seen = false;
17199 dpi.fndecl = fndecl;
17200 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17201 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17202 if (!loc_body)
17203 return NULL;
17205 /* After evaluating all operands in "loc_body", we should still have on the
17206 stack all arguments plus the desired function result (top of the stack).
17207 Generate code in order to keep only the result in our stack frame. */
17208 epilogue = NULL;
17209 for (i = 0; i < dpi.args_count; ++i)
17211 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17212 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17213 op_couple->dw_loc_next->dw_loc_next = epilogue;
17214 epilogue = op_couple;
17216 add_loc_descr (&loc_body, epilogue);
17217 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17218 return NULL;
17220 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17221 because they are considered useful. Now there is an epilogue, they are
17222 not anymore, so give it another try. */
17223 loc_descr_without_nops (loc_body);
17225 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17226 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17227 though, given that size functions do not come from source, so they should
17228 not have a dedicated DW_TAG_subprogram DIE. */
17229 dwarf_proc_die
17230 = new_dwarf_proc_die (loc_body, fndecl,
17231 get_context_die (DECL_CONTEXT (fndecl)));
17233 /* The called DWARF procedure consumes one stack slot per argument and
17234 returns one stack slot. */
17235 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17237 return dwarf_proc_die;
17241 /* Generate Dwarf location list representing LOC.
17242 If WANT_ADDRESS is false, expression computing LOC will be computed
17243 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17244 if WANT_ADDRESS is 2, expression computing address useable in location
17245 will be returned (i.e. DW_OP_reg can be used
17246 to refer to register values).
17248 CONTEXT provides information to customize the location descriptions
17249 generation. Its context_type field specifies what type is implicitly
17250 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17251 will not be generated.
17253 Its DPI field determines whether we are generating a DWARF expression for a
17254 DWARF procedure, so PARM_DECL references are processed specifically.
17256 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17257 and dpi fields were null. */
17259 static dw_loc_list_ref
17260 loc_list_from_tree_1 (tree loc, int want_address,
17261 struct loc_descr_context *context)
17263 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17264 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17265 int have_address = 0;
17266 enum dwarf_location_atom op;
17268 /* ??? Most of the time we do not take proper care for sign/zero
17269 extending the values properly. Hopefully this won't be a real
17270 problem... */
17272 if (context != NULL
17273 && context->base_decl == loc
17274 && want_address == 0)
17276 if (dwarf_version >= 3 || !dwarf_strict)
17277 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17278 NULL, NULL, NULL);
17279 else
17280 return NULL;
17283 switch (TREE_CODE (loc))
17285 case ERROR_MARK:
17286 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17287 return 0;
17289 case PLACEHOLDER_EXPR:
17290 /* This case involves extracting fields from an object to determine the
17291 position of other fields. It is supposed to appear only as the first
17292 operand of COMPONENT_REF nodes and to reference precisely the type
17293 that the context allows. */
17294 if (context != NULL
17295 && TREE_TYPE (loc) == context->context_type
17296 && want_address >= 1)
17298 if (dwarf_version >= 3 || !dwarf_strict)
17300 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17301 have_address = 1;
17302 break;
17304 else
17305 return NULL;
17307 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17308 the single argument passed by consumer. */
17309 else if (context != NULL
17310 && context->placeholder_arg
17311 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17312 && want_address == 0)
17314 ret = new_loc_descr (DW_OP_pick, 0, 0);
17315 ret->frame_offset_rel = 1;
17316 context->placeholder_seen = true;
17317 break;
17319 else
17320 expansion_failed (loc, NULL_RTX,
17321 "PLACEHOLDER_EXPR for an unexpected type");
17322 break;
17324 case CALL_EXPR:
17326 const int nargs = call_expr_nargs (loc);
17327 tree callee = get_callee_fndecl (loc);
17328 int i;
17329 dw_die_ref dwarf_proc;
17331 if (callee == NULL_TREE)
17332 goto call_expansion_failed;
17334 /* We handle only functions that return an integer. */
17335 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17336 goto call_expansion_failed;
17338 dwarf_proc = function_to_dwarf_procedure (callee);
17339 if (dwarf_proc == NULL)
17340 goto call_expansion_failed;
17342 /* Evaluate arguments right-to-left so that the first argument will
17343 be the top-most one on the stack. */
17344 for (i = nargs - 1; i >= 0; --i)
17346 dw_loc_descr_ref loc_descr
17347 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17348 context);
17350 if (loc_descr == NULL)
17351 goto call_expansion_failed;
17353 add_loc_descr (&ret, loc_descr);
17356 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17357 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17358 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17359 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17360 add_loc_descr (&ret, ret1);
17361 break;
17363 call_expansion_failed:
17364 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17365 /* There are no opcodes for these operations. */
17366 return 0;
17369 case PREINCREMENT_EXPR:
17370 case PREDECREMENT_EXPR:
17371 case POSTINCREMENT_EXPR:
17372 case POSTDECREMENT_EXPR:
17373 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17374 /* There are no opcodes for these operations. */
17375 return 0;
17377 case ADDR_EXPR:
17378 /* If we already want an address, see if there is INDIRECT_REF inside
17379 e.g. for &this->field. */
17380 if (want_address)
17382 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17383 (loc, want_address == 2, context);
17384 if (list_ret)
17385 have_address = 1;
17386 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17387 && (ret = cst_pool_loc_descr (loc)))
17388 have_address = 1;
17390 /* Otherwise, process the argument and look for the address. */
17391 if (!list_ret && !ret)
17392 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17393 else
17395 if (want_address)
17396 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17397 return NULL;
17399 break;
17401 case VAR_DECL:
17402 if (DECL_THREAD_LOCAL_P (loc))
17404 rtx rtl;
17405 enum dwarf_location_atom tls_op;
17406 enum dtprel_bool dtprel = dtprel_false;
17408 if (targetm.have_tls)
17410 /* If this is not defined, we have no way to emit the
17411 data. */
17412 if (!targetm.asm_out.output_dwarf_dtprel)
17413 return 0;
17415 /* The way DW_OP_GNU_push_tls_address is specified, we
17416 can only look up addresses of objects in the current
17417 module. We used DW_OP_addr as first op, but that's
17418 wrong, because DW_OP_addr is relocated by the debug
17419 info consumer, while DW_OP_GNU_push_tls_address
17420 operand shouldn't be. */
17421 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17422 return 0;
17423 dtprel = dtprel_true;
17424 /* We check for DWARF 5 here because gdb did not implement
17425 DW_OP_form_tls_address until after 7.12. */
17426 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17427 : DW_OP_GNU_push_tls_address);
17429 else
17431 if (!targetm.emutls.debug_form_tls_address
17432 || !(dwarf_version >= 3 || !dwarf_strict))
17433 return 0;
17434 /* We stuffed the control variable into the DECL_VALUE_EXPR
17435 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17436 no longer appear in gimple code. We used the control
17437 variable in specific so that we could pick it up here. */
17438 loc = DECL_VALUE_EXPR (loc);
17439 tls_op = DW_OP_form_tls_address;
17442 rtl = rtl_for_decl_location (loc);
17443 if (rtl == NULL_RTX)
17444 return 0;
17446 if (!MEM_P (rtl))
17447 return 0;
17448 rtl = XEXP (rtl, 0);
17449 if (! CONSTANT_P (rtl))
17450 return 0;
17452 ret = new_addr_loc_descr (rtl, dtprel);
17453 ret1 = new_loc_descr (tls_op, 0, 0);
17454 add_loc_descr (&ret, ret1);
17456 have_address = 1;
17457 break;
17459 /* FALLTHRU */
17461 case PARM_DECL:
17462 if (context != NULL && context->dpi != NULL
17463 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17465 /* We are generating code for a DWARF procedure and we want to access
17466 one of its arguments: find the appropriate argument offset and let
17467 the resolve_args_picking pass compute the offset that complies
17468 with the stack frame size. */
17469 unsigned i = 0;
17470 tree cursor;
17472 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17473 cursor != NULL_TREE && cursor != loc;
17474 cursor = TREE_CHAIN (cursor), ++i)
17476 /* If we are translating a DWARF procedure, all referenced parameters
17477 must belong to the current function. */
17478 gcc_assert (cursor != NULL_TREE);
17480 ret = new_loc_descr (DW_OP_pick, i, 0);
17481 ret->frame_offset_rel = 1;
17482 break;
17484 /* FALLTHRU */
17486 case RESULT_DECL:
17487 if (DECL_HAS_VALUE_EXPR_P (loc))
17488 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17489 want_address, context);
17490 /* FALLTHRU */
17492 case FUNCTION_DECL:
17494 rtx rtl;
17495 var_loc_list *loc_list = lookup_decl_loc (loc);
17497 if (loc_list && loc_list->first)
17499 list_ret = dw_loc_list (loc_list, loc, want_address);
17500 have_address = want_address != 0;
17501 break;
17503 rtl = rtl_for_decl_location (loc);
17504 if (rtl == NULL_RTX)
17506 if (TREE_CODE (loc) != FUNCTION_DECL
17507 && early_dwarf
17508 && current_function_decl
17509 && want_address != 1
17510 && ! DECL_IGNORED_P (loc)
17511 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17512 || POINTER_TYPE_P (TREE_TYPE (loc)))
17513 && DECL_CONTEXT (loc) == current_function_decl
17514 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17515 <= DWARF2_ADDR_SIZE))
17517 dw_die_ref ref = lookup_decl_die (loc);
17518 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17519 if (ref)
17521 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17522 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17523 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17525 else
17527 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17528 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17530 break;
17532 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17533 return 0;
17535 else if (CONST_INT_P (rtl))
17537 HOST_WIDE_INT val = INTVAL (rtl);
17538 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17539 val &= GET_MODE_MASK (DECL_MODE (loc));
17540 ret = int_loc_descriptor (val);
17542 else if (GET_CODE (rtl) == CONST_STRING)
17544 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17545 return 0;
17547 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17548 ret = new_addr_loc_descr (rtl, dtprel_false);
17549 else
17551 machine_mode mode, mem_mode;
17553 /* Certain constructs can only be represented at top-level. */
17554 if (want_address == 2)
17556 ret = loc_descriptor (rtl, VOIDmode,
17557 VAR_INIT_STATUS_INITIALIZED);
17558 have_address = 1;
17560 else
17562 mode = GET_MODE (rtl);
17563 mem_mode = VOIDmode;
17564 if (MEM_P (rtl))
17566 mem_mode = mode;
17567 mode = get_address_mode (rtl);
17568 rtl = XEXP (rtl, 0);
17569 have_address = 1;
17571 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17572 VAR_INIT_STATUS_INITIALIZED);
17574 if (!ret)
17575 expansion_failed (loc, rtl,
17576 "failed to produce loc descriptor for rtl");
17579 break;
17581 case MEM_REF:
17582 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17584 have_address = 1;
17585 goto do_plus;
17587 /* Fallthru. */
17588 case INDIRECT_REF:
17589 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17590 have_address = 1;
17591 break;
17593 case TARGET_MEM_REF:
17594 case SSA_NAME:
17595 case DEBUG_EXPR_DECL:
17596 return NULL;
17598 case COMPOUND_EXPR:
17599 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17600 context);
17602 CASE_CONVERT:
17603 case VIEW_CONVERT_EXPR:
17604 case SAVE_EXPR:
17605 case MODIFY_EXPR:
17606 case NON_LVALUE_EXPR:
17607 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17608 context);
17610 case COMPONENT_REF:
17611 case BIT_FIELD_REF:
17612 case ARRAY_REF:
17613 case ARRAY_RANGE_REF:
17614 case REALPART_EXPR:
17615 case IMAGPART_EXPR:
17617 tree obj, offset;
17618 HOST_WIDE_INT bitsize, bitpos, bytepos;
17619 machine_mode mode;
17620 int unsignedp, reversep, volatilep = 0;
17622 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17623 &unsignedp, &reversep, &volatilep);
17625 gcc_assert (obj != loc);
17627 list_ret = loc_list_from_tree_1 (obj,
17628 want_address == 2
17629 && !bitpos && !offset ? 2 : 1,
17630 context);
17631 /* TODO: We can extract value of the small expression via shifting even
17632 for nonzero bitpos. */
17633 if (list_ret == 0)
17634 return 0;
17635 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17637 expansion_failed (loc, NULL_RTX,
17638 "bitfield access");
17639 return 0;
17642 if (offset != NULL_TREE)
17644 /* Variable offset. */
17645 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17646 if (list_ret1 == 0)
17647 return 0;
17648 add_loc_list (&list_ret, list_ret1);
17649 if (!list_ret)
17650 return 0;
17651 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17654 bytepos = bitpos / BITS_PER_UNIT;
17655 if (bytepos > 0)
17656 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17657 else if (bytepos < 0)
17658 loc_list_plus_const (list_ret, bytepos);
17660 have_address = 1;
17661 break;
17664 case INTEGER_CST:
17665 if ((want_address || !tree_fits_shwi_p (loc))
17666 && (ret = cst_pool_loc_descr (loc)))
17667 have_address = 1;
17668 else if (want_address == 2
17669 && tree_fits_shwi_p (loc)
17670 && (ret = address_of_int_loc_descriptor
17671 (int_size_in_bytes (TREE_TYPE (loc)),
17672 tree_to_shwi (loc))))
17673 have_address = 1;
17674 else if (tree_fits_shwi_p (loc))
17675 ret = int_loc_descriptor (tree_to_shwi (loc));
17676 else if (tree_fits_uhwi_p (loc))
17677 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17678 else
17680 expansion_failed (loc, NULL_RTX,
17681 "Integer operand is not host integer");
17682 return 0;
17684 break;
17686 case CONSTRUCTOR:
17687 case REAL_CST:
17688 case STRING_CST:
17689 case COMPLEX_CST:
17690 if ((ret = cst_pool_loc_descr (loc)))
17691 have_address = 1;
17692 else if (TREE_CODE (loc) == CONSTRUCTOR)
17694 tree type = TREE_TYPE (loc);
17695 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17696 unsigned HOST_WIDE_INT offset = 0;
17697 unsigned HOST_WIDE_INT cnt;
17698 constructor_elt *ce;
17700 if (TREE_CODE (type) == RECORD_TYPE)
17702 /* This is very limited, but it's enough to output
17703 pointers to member functions, as long as the
17704 referenced function is defined in the current
17705 translation unit. */
17706 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17708 tree val = ce->value;
17710 tree field = ce->index;
17712 if (val)
17713 STRIP_NOPS (val);
17715 if (!field || DECL_BIT_FIELD (field))
17717 expansion_failed (loc, NULL_RTX,
17718 "bitfield in record type constructor");
17719 size = offset = (unsigned HOST_WIDE_INT)-1;
17720 ret = NULL;
17721 break;
17724 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17725 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17726 gcc_assert (pos + fieldsize <= size);
17727 if (pos < offset)
17729 expansion_failed (loc, NULL_RTX,
17730 "out-of-order fields in record constructor");
17731 size = offset = (unsigned HOST_WIDE_INT)-1;
17732 ret = NULL;
17733 break;
17735 if (pos > offset)
17737 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17738 add_loc_descr (&ret, ret1);
17739 offset = pos;
17741 if (val && fieldsize != 0)
17743 ret1 = loc_descriptor_from_tree (val, want_address, context);
17744 if (!ret1)
17746 expansion_failed (loc, NULL_RTX,
17747 "unsupported expression in field");
17748 size = offset = (unsigned HOST_WIDE_INT)-1;
17749 ret = NULL;
17750 break;
17752 add_loc_descr (&ret, ret1);
17754 if (fieldsize)
17756 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17757 add_loc_descr (&ret, ret1);
17758 offset = pos + fieldsize;
17762 if (offset != size)
17764 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17765 add_loc_descr (&ret, ret1);
17766 offset = size;
17769 have_address = !!want_address;
17771 else
17772 expansion_failed (loc, NULL_RTX,
17773 "constructor of non-record type");
17775 else
17776 /* We can construct small constants here using int_loc_descriptor. */
17777 expansion_failed (loc, NULL_RTX,
17778 "constructor or constant not in constant pool");
17779 break;
17781 case TRUTH_AND_EXPR:
17782 case TRUTH_ANDIF_EXPR:
17783 case BIT_AND_EXPR:
17784 op = DW_OP_and;
17785 goto do_binop;
17787 case TRUTH_XOR_EXPR:
17788 case BIT_XOR_EXPR:
17789 op = DW_OP_xor;
17790 goto do_binop;
17792 case TRUTH_OR_EXPR:
17793 case TRUTH_ORIF_EXPR:
17794 case BIT_IOR_EXPR:
17795 op = DW_OP_or;
17796 goto do_binop;
17798 case FLOOR_DIV_EXPR:
17799 case CEIL_DIV_EXPR:
17800 case ROUND_DIV_EXPR:
17801 case TRUNC_DIV_EXPR:
17802 case EXACT_DIV_EXPR:
17803 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17804 return 0;
17805 op = DW_OP_div;
17806 goto do_binop;
17808 case MINUS_EXPR:
17809 op = DW_OP_minus;
17810 goto do_binop;
17812 case FLOOR_MOD_EXPR:
17813 case CEIL_MOD_EXPR:
17814 case ROUND_MOD_EXPR:
17815 case TRUNC_MOD_EXPR:
17816 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17818 op = DW_OP_mod;
17819 goto do_binop;
17821 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17822 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17823 if (list_ret == 0 || list_ret1 == 0)
17824 return 0;
17826 add_loc_list (&list_ret, list_ret1);
17827 if (list_ret == 0)
17828 return 0;
17829 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17830 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17831 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17832 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17833 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17834 break;
17836 case MULT_EXPR:
17837 op = DW_OP_mul;
17838 goto do_binop;
17840 case LSHIFT_EXPR:
17841 op = DW_OP_shl;
17842 goto do_binop;
17844 case RSHIFT_EXPR:
17845 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17846 goto do_binop;
17848 case POINTER_PLUS_EXPR:
17849 case PLUS_EXPR:
17850 do_plus:
17851 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17853 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17854 smarter to encode their opposite. The DW_OP_plus_uconst operation
17855 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17856 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17857 bytes, Y being the size of the operation that pushes the opposite
17858 of the addend. So let's choose the smallest representation. */
17859 const tree tree_addend = TREE_OPERAND (loc, 1);
17860 offset_int wi_addend;
17861 HOST_WIDE_INT shwi_addend;
17862 dw_loc_descr_ref loc_naddend;
17864 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17865 if (list_ret == 0)
17866 return 0;
17868 /* Try to get the literal to push. It is the opposite of the addend,
17869 so as we rely on wrapping during DWARF evaluation, first decode
17870 the literal as a "DWARF-sized" signed number. */
17871 wi_addend = wi::to_offset (tree_addend);
17872 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17873 shwi_addend = wi_addend.to_shwi ();
17874 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17875 ? int_loc_descriptor (-shwi_addend)
17876 : NULL;
17878 if (loc_naddend != NULL
17879 && ((unsigned) size_of_uleb128 (shwi_addend)
17880 > size_of_loc_descr (loc_naddend)))
17882 add_loc_descr_to_each (list_ret, loc_naddend);
17883 add_loc_descr_to_each (list_ret,
17884 new_loc_descr (DW_OP_minus, 0, 0));
17886 else
17888 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17890 loc_naddend = loc_cur;
17891 loc_cur = loc_cur->dw_loc_next;
17892 ggc_free (loc_naddend);
17894 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17896 break;
17899 op = DW_OP_plus;
17900 goto do_binop;
17902 case LE_EXPR:
17903 op = DW_OP_le;
17904 goto do_comp_binop;
17906 case GE_EXPR:
17907 op = DW_OP_ge;
17908 goto do_comp_binop;
17910 case LT_EXPR:
17911 op = DW_OP_lt;
17912 goto do_comp_binop;
17914 case GT_EXPR:
17915 op = DW_OP_gt;
17916 goto do_comp_binop;
17918 do_comp_binop:
17919 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17921 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17922 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17923 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17924 TREE_CODE (loc));
17925 break;
17927 else
17928 goto do_binop;
17930 case EQ_EXPR:
17931 op = DW_OP_eq;
17932 goto do_binop;
17934 case NE_EXPR:
17935 op = DW_OP_ne;
17936 goto do_binop;
17938 do_binop:
17939 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17940 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17941 if (list_ret == 0 || list_ret1 == 0)
17942 return 0;
17944 add_loc_list (&list_ret, list_ret1);
17945 if (list_ret == 0)
17946 return 0;
17947 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17948 break;
17950 case TRUTH_NOT_EXPR:
17951 case BIT_NOT_EXPR:
17952 op = DW_OP_not;
17953 goto do_unop;
17955 case ABS_EXPR:
17956 op = DW_OP_abs;
17957 goto do_unop;
17959 case NEGATE_EXPR:
17960 op = DW_OP_neg;
17961 goto do_unop;
17963 do_unop:
17964 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17965 if (list_ret == 0)
17966 return 0;
17968 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17969 break;
17971 case MIN_EXPR:
17972 case MAX_EXPR:
17974 const enum tree_code code =
17975 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17977 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17978 build2 (code, integer_type_node,
17979 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17980 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17983 /* fall through */
17985 case COND_EXPR:
17987 dw_loc_descr_ref lhs
17988 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17989 dw_loc_list_ref rhs
17990 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17991 dw_loc_descr_ref bra_node, jump_node, tmp;
17993 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17994 if (list_ret == 0 || lhs == 0 || rhs == 0)
17995 return 0;
17997 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17998 add_loc_descr_to_each (list_ret, bra_node);
18000 add_loc_list (&list_ret, rhs);
18001 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18002 add_loc_descr_to_each (list_ret, jump_node);
18004 add_loc_descr_to_each (list_ret, lhs);
18005 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18006 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18008 /* ??? Need a node to point the skip at. Use a nop. */
18009 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18010 add_loc_descr_to_each (list_ret, tmp);
18011 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18012 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18014 break;
18016 case FIX_TRUNC_EXPR:
18017 return 0;
18019 default:
18020 /* Leave front-end specific codes as simply unknown. This comes
18021 up, for instance, with the C STMT_EXPR. */
18022 if ((unsigned int) TREE_CODE (loc)
18023 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18025 expansion_failed (loc, NULL_RTX,
18026 "language specific tree node");
18027 return 0;
18030 /* Otherwise this is a generic code; we should just lists all of
18031 these explicitly. We forgot one. */
18032 if (flag_checking)
18033 gcc_unreachable ();
18035 /* In a release build, we want to degrade gracefully: better to
18036 generate incomplete debugging information than to crash. */
18037 return NULL;
18040 if (!ret && !list_ret)
18041 return 0;
18043 if (want_address == 2 && !have_address
18044 && (dwarf_version >= 4 || !dwarf_strict))
18046 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18048 expansion_failed (loc, NULL_RTX,
18049 "DWARF address size mismatch");
18050 return 0;
18052 if (ret)
18053 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18054 else
18055 add_loc_descr_to_each (list_ret,
18056 new_loc_descr (DW_OP_stack_value, 0, 0));
18057 have_address = 1;
18059 /* Show if we can't fill the request for an address. */
18060 if (want_address && !have_address)
18062 expansion_failed (loc, NULL_RTX,
18063 "Want address and only have value");
18064 return 0;
18067 gcc_assert (!ret || !list_ret);
18069 /* If we've got an address and don't want one, dereference. */
18070 if (!want_address && have_address)
18072 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18074 if (size > DWARF2_ADDR_SIZE || size == -1)
18076 expansion_failed (loc, NULL_RTX,
18077 "DWARF address size mismatch");
18078 return 0;
18080 else if (size == DWARF2_ADDR_SIZE)
18081 op = DW_OP_deref;
18082 else
18083 op = DW_OP_deref_size;
18085 if (ret)
18086 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18087 else
18088 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18090 if (ret)
18091 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18093 return list_ret;
18096 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18097 expressions. */
18099 static dw_loc_list_ref
18100 loc_list_from_tree (tree loc, int want_address,
18101 struct loc_descr_context *context)
18103 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18105 for (dw_loc_list_ref loc_cur = result;
18106 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18107 loc_descr_without_nops (loc_cur->expr);
18108 return result;
18111 /* Same as above but return only single location expression. */
18112 static dw_loc_descr_ref
18113 loc_descriptor_from_tree (tree loc, int want_address,
18114 struct loc_descr_context *context)
18116 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18117 if (!ret)
18118 return NULL;
18119 if (ret->dw_loc_next)
18121 expansion_failed (loc, NULL_RTX,
18122 "Location list where only loc descriptor needed");
18123 return NULL;
18125 return ret->expr;
18128 /* Given a value, round it up to the lowest multiple of `boundary'
18129 which is not less than the value itself. */
18131 static inline HOST_WIDE_INT
18132 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18134 return (((value + boundary - 1) / boundary) * boundary);
18137 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18138 pointer to the declared type for the relevant field variable, or return
18139 `integer_type_node' if the given node turns out to be an
18140 ERROR_MARK node. */
18142 static inline tree
18143 field_type (const_tree decl)
18145 tree type;
18147 if (TREE_CODE (decl) == ERROR_MARK)
18148 return integer_type_node;
18150 type = DECL_BIT_FIELD_TYPE (decl);
18151 if (type == NULL_TREE)
18152 type = TREE_TYPE (decl);
18154 return type;
18157 /* Given a pointer to a tree node, return the alignment in bits for
18158 it, or else return BITS_PER_WORD if the node actually turns out to
18159 be an ERROR_MARK node. */
18161 static inline unsigned
18162 simple_type_align_in_bits (const_tree type)
18164 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18167 static inline unsigned
18168 simple_decl_align_in_bits (const_tree decl)
18170 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18173 /* Return the result of rounding T up to ALIGN. */
18175 static inline offset_int
18176 round_up_to_align (const offset_int &t, unsigned int align)
18178 return wi::udiv_trunc (t + align - 1, align) * align;
18181 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18182 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18183 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18184 if we fail to return the size in one of these two forms. */
18186 static dw_loc_descr_ref
18187 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18189 tree tree_size;
18190 struct loc_descr_context ctx;
18192 /* Return a constant integer in priority, if possible. */
18193 *cst_size = int_size_in_bytes (type);
18194 if (*cst_size != -1)
18195 return NULL;
18197 ctx.context_type = const_cast<tree> (type);
18198 ctx.base_decl = NULL_TREE;
18199 ctx.dpi = NULL;
18200 ctx.placeholder_arg = false;
18201 ctx.placeholder_seen = false;
18203 type = TYPE_MAIN_VARIANT (type);
18204 tree_size = TYPE_SIZE_UNIT (type);
18205 return ((tree_size != NULL_TREE)
18206 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18207 : NULL);
18210 /* Helper structure for RECORD_TYPE processing. */
18211 struct vlr_context
18213 /* Root RECORD_TYPE. It is needed to generate data member location
18214 descriptions in variable-length records (VLR), but also to cope with
18215 variants, which are composed of nested structures multiplexed with
18216 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18217 function processing a FIELD_DECL, it is required to be non null. */
18218 tree struct_type;
18219 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18220 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18221 this variant part as part of the root record (in storage units). For
18222 regular records, it must be NULL_TREE. */
18223 tree variant_part_offset;
18226 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18227 addressed byte of the "containing object" for the given FIELD_DECL. If
18228 possible, return a native constant through CST_OFFSET (in which case NULL is
18229 returned); otherwise return a DWARF expression that computes the offset.
18231 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18232 that offset is, either because the argument turns out to be a pointer to an
18233 ERROR_MARK node, or because the offset expression is too complex for us.
18235 CTX is required: see the comment for VLR_CONTEXT. */
18237 static dw_loc_descr_ref
18238 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18239 HOST_WIDE_INT *cst_offset)
18241 tree tree_result;
18242 dw_loc_list_ref loc_result;
18244 *cst_offset = 0;
18246 if (TREE_CODE (decl) == ERROR_MARK)
18247 return NULL;
18248 else
18249 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18251 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18252 case. */
18253 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18254 return NULL;
18256 #ifdef PCC_BITFIELD_TYPE_MATTERS
18257 /* We used to handle only constant offsets in all cases. Now, we handle
18258 properly dynamic byte offsets only when PCC bitfield type doesn't
18259 matter. */
18260 if (PCC_BITFIELD_TYPE_MATTERS
18261 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18263 offset_int object_offset_in_bits;
18264 offset_int object_offset_in_bytes;
18265 offset_int bitpos_int;
18266 tree type;
18267 tree field_size_tree;
18268 offset_int deepest_bitpos;
18269 offset_int field_size_in_bits;
18270 unsigned int type_align_in_bits;
18271 unsigned int decl_align_in_bits;
18272 offset_int type_size_in_bits;
18274 bitpos_int = wi::to_offset (bit_position (decl));
18275 type = field_type (decl);
18276 type_size_in_bits = offset_int_type_size_in_bits (type);
18277 type_align_in_bits = simple_type_align_in_bits (type);
18279 field_size_tree = DECL_SIZE (decl);
18281 /* The size could be unspecified if there was an error, or for
18282 a flexible array member. */
18283 if (!field_size_tree)
18284 field_size_tree = bitsize_zero_node;
18286 /* If the size of the field is not constant, use the type size. */
18287 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18288 field_size_in_bits = wi::to_offset (field_size_tree);
18289 else
18290 field_size_in_bits = type_size_in_bits;
18292 decl_align_in_bits = simple_decl_align_in_bits (decl);
18294 /* The GCC front-end doesn't make any attempt to keep track of the
18295 starting bit offset (relative to the start of the containing
18296 structure type) of the hypothetical "containing object" for a
18297 bit-field. Thus, when computing the byte offset value for the
18298 start of the "containing object" of a bit-field, we must deduce
18299 this information on our own. This can be rather tricky to do in
18300 some cases. For example, handling the following structure type
18301 definition when compiling for an i386/i486 target (which only
18302 aligns long long's to 32-bit boundaries) can be very tricky:
18304 struct S { int field1; long long field2:31; };
18306 Fortunately, there is a simple rule-of-thumb which can be used
18307 in such cases. When compiling for an i386/i486, GCC will
18308 allocate 8 bytes for the structure shown above. It decides to
18309 do this based upon one simple rule for bit-field allocation.
18310 GCC allocates each "containing object" for each bit-field at
18311 the first (i.e. lowest addressed) legitimate alignment boundary
18312 (based upon the required minimum alignment for the declared
18313 type of the field) which it can possibly use, subject to the
18314 condition that there is still enough available space remaining
18315 in the containing object (when allocated at the selected point)
18316 to fully accommodate all of the bits of the bit-field itself.
18318 This simple rule makes it obvious why GCC allocates 8 bytes for
18319 each object of the structure type shown above. When looking
18320 for a place to allocate the "containing object" for `field2',
18321 the compiler simply tries to allocate a 64-bit "containing
18322 object" at each successive 32-bit boundary (starting at zero)
18323 until it finds a place to allocate that 64- bit field such that
18324 at least 31 contiguous (and previously unallocated) bits remain
18325 within that selected 64 bit field. (As it turns out, for the
18326 example above, the compiler finds it is OK to allocate the
18327 "containing object" 64-bit field at bit-offset zero within the
18328 structure type.)
18330 Here we attempt to work backwards from the limited set of facts
18331 we're given, and we try to deduce from those facts, where GCC
18332 must have believed that the containing object started (within
18333 the structure type). The value we deduce is then used (by the
18334 callers of this routine) to generate DW_AT_location and
18335 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18336 the case of DW_AT_location, regular fields as well). */
18338 /* Figure out the bit-distance from the start of the structure to
18339 the "deepest" bit of the bit-field. */
18340 deepest_bitpos = bitpos_int + field_size_in_bits;
18342 /* This is the tricky part. Use some fancy footwork to deduce
18343 where the lowest addressed bit of the containing object must
18344 be. */
18345 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18347 /* Round up to type_align by default. This works best for
18348 bitfields. */
18349 object_offset_in_bits
18350 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18352 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18354 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18356 /* Round up to decl_align instead. */
18357 object_offset_in_bits
18358 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18361 object_offset_in_bytes
18362 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18363 if (ctx->variant_part_offset == NULL_TREE)
18365 *cst_offset = object_offset_in_bytes.to_shwi ();
18366 return NULL;
18368 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18370 else
18371 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18372 tree_result = byte_position (decl);
18374 if (ctx->variant_part_offset != NULL_TREE)
18375 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18376 ctx->variant_part_offset, tree_result);
18378 /* If the byte offset is a constant, it's simplier to handle a native
18379 constant rather than a DWARF expression. */
18380 if (TREE_CODE (tree_result) == INTEGER_CST)
18382 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18383 return NULL;
18385 struct loc_descr_context loc_ctx = {
18386 ctx->struct_type, /* context_type */
18387 NULL_TREE, /* base_decl */
18388 NULL, /* dpi */
18389 false, /* placeholder_arg */
18390 false /* placeholder_seen */
18392 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18394 /* We want a DWARF expression: abort if we only have a location list with
18395 multiple elements. */
18396 if (!loc_result || !single_element_loc_list_p (loc_result))
18397 return NULL;
18398 else
18399 return loc_result->expr;
18402 /* The following routines define various Dwarf attributes and any data
18403 associated with them. */
18405 /* Add a location description attribute value to a DIE.
18407 This emits location attributes suitable for whole variables and
18408 whole parameters. Note that the location attributes for struct fields are
18409 generated by the routine `data_member_location_attribute' below. */
18411 static inline void
18412 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18413 dw_loc_list_ref descr)
18415 if (descr == 0)
18416 return;
18417 if (single_element_loc_list_p (descr))
18418 add_AT_loc (die, attr_kind, descr->expr);
18419 else
18420 add_AT_loc_list (die, attr_kind, descr);
18423 /* Add DW_AT_accessibility attribute to DIE if needed. */
18425 static void
18426 add_accessibility_attribute (dw_die_ref die, tree decl)
18428 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18429 children, otherwise the default is DW_ACCESS_public. In DWARF2
18430 the default has always been DW_ACCESS_public. */
18431 if (TREE_PROTECTED (decl))
18432 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18433 else if (TREE_PRIVATE (decl))
18435 if (dwarf_version == 2
18436 || die->die_parent == NULL
18437 || die->die_parent->die_tag != DW_TAG_class_type)
18438 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18440 else if (dwarf_version > 2
18441 && die->die_parent
18442 && die->die_parent->die_tag == DW_TAG_class_type)
18443 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18446 /* Attach the specialized form of location attribute used for data members of
18447 struct and union types. In the special case of a FIELD_DECL node which
18448 represents a bit-field, the "offset" part of this special location
18449 descriptor must indicate the distance in bytes from the lowest-addressed
18450 byte of the containing struct or union type to the lowest-addressed byte of
18451 the "containing object" for the bit-field. (See the `field_byte_offset'
18452 function above).
18454 For any given bit-field, the "containing object" is a hypothetical object
18455 (of some integral or enum type) within which the given bit-field lives. The
18456 type of this hypothetical "containing object" is always the same as the
18457 declared type of the individual bit-field itself (for GCC anyway... the
18458 DWARF spec doesn't actually mandate this). Note that it is the size (in
18459 bytes) of the hypothetical "containing object" which will be given in the
18460 DW_AT_byte_size attribute for this bit-field. (See the
18461 `byte_size_attribute' function below.) It is also used when calculating the
18462 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18463 function below.)
18465 CTX is required: see the comment for VLR_CONTEXT. */
18467 static void
18468 add_data_member_location_attribute (dw_die_ref die,
18469 tree decl,
18470 struct vlr_context *ctx)
18472 HOST_WIDE_INT offset;
18473 dw_loc_descr_ref loc_descr = 0;
18475 if (TREE_CODE (decl) == TREE_BINFO)
18477 /* We're working on the TAG_inheritance for a base class. */
18478 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18480 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18481 aren't at a fixed offset from all (sub)objects of the same
18482 type. We need to extract the appropriate offset from our
18483 vtable. The following dwarf expression means
18485 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18487 This is specific to the V3 ABI, of course. */
18489 dw_loc_descr_ref tmp;
18491 /* Make a copy of the object address. */
18492 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18493 add_loc_descr (&loc_descr, tmp);
18495 /* Extract the vtable address. */
18496 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18497 add_loc_descr (&loc_descr, tmp);
18499 /* Calculate the address of the offset. */
18500 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18501 gcc_assert (offset < 0);
18503 tmp = int_loc_descriptor (-offset);
18504 add_loc_descr (&loc_descr, tmp);
18505 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18506 add_loc_descr (&loc_descr, tmp);
18508 /* Extract the offset. */
18509 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18510 add_loc_descr (&loc_descr, tmp);
18512 /* Add it to the object address. */
18513 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18514 add_loc_descr (&loc_descr, tmp);
18516 else
18517 offset = tree_to_shwi (BINFO_OFFSET (decl));
18519 else
18521 loc_descr = field_byte_offset (decl, ctx, &offset);
18523 /* If loc_descr is available then we know the field offset is dynamic.
18524 However, GDB does not handle dynamic field offsets very well at the
18525 moment. */
18526 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18528 loc_descr = NULL;
18529 offset = 0;
18532 /* Data member location evalutation starts with the base address on the
18533 stack. Compute the field offset and add it to this base address. */
18534 else if (loc_descr != NULL)
18535 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18538 if (! loc_descr)
18540 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18541 e.g. GDB only added support to it in November 2016. For DWARF5
18542 we need newer debug info consumers anyway. We might change this
18543 to dwarf_version >= 4 once most consumers catched up. */
18544 if (dwarf_version >= 5
18545 && TREE_CODE (decl) == FIELD_DECL
18546 && DECL_BIT_FIELD_TYPE (decl))
18548 tree off = bit_position (decl);
18549 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18551 remove_AT (die, DW_AT_byte_size);
18552 remove_AT (die, DW_AT_bit_offset);
18553 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18554 return;
18557 if (dwarf_version > 2)
18559 /* Don't need to output a location expression, just the constant. */
18560 if (offset < 0)
18561 add_AT_int (die, DW_AT_data_member_location, offset);
18562 else
18563 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18564 return;
18566 else
18568 enum dwarf_location_atom op;
18570 /* The DWARF2 standard says that we should assume that the structure
18571 address is already on the stack, so we can specify a structure
18572 field address by using DW_OP_plus_uconst. */
18573 op = DW_OP_plus_uconst;
18574 loc_descr = new_loc_descr (op, offset, 0);
18578 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18581 /* Writes integer values to dw_vec_const array. */
18583 static void
18584 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18586 while (size != 0)
18588 *dest++ = val & 0xff;
18589 val >>= 8;
18590 --size;
18594 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18596 static HOST_WIDE_INT
18597 extract_int (const unsigned char *src, unsigned int size)
18599 HOST_WIDE_INT val = 0;
18601 src += size;
18602 while (size != 0)
18604 val <<= 8;
18605 val |= *--src & 0xff;
18606 --size;
18608 return val;
18611 /* Writes wide_int values to dw_vec_const array. */
18613 static void
18614 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18616 int i;
18618 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18620 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18621 return;
18624 /* We'd have to extend this code to support odd sizes. */
18625 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18627 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18629 if (WORDS_BIG_ENDIAN)
18630 for (i = n - 1; i >= 0; i--)
18632 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18633 dest += sizeof (HOST_WIDE_INT);
18635 else
18636 for (i = 0; i < n; i++)
18638 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18639 dest += sizeof (HOST_WIDE_INT);
18643 /* Writes floating point values to dw_vec_const array. */
18645 static void
18646 insert_float (const_rtx rtl, unsigned char *array)
18648 long val[4];
18649 int i;
18650 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18652 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18654 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18655 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18657 insert_int (val[i], 4, array);
18658 array += 4;
18662 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18663 does not have a "location" either in memory or in a register. These
18664 things can arise in GNU C when a constant is passed as an actual parameter
18665 to an inlined function. They can also arise in C++ where declared
18666 constants do not necessarily get memory "homes". */
18668 static bool
18669 add_const_value_attribute (dw_die_ref die, rtx rtl)
18671 switch (GET_CODE (rtl))
18673 case CONST_INT:
18675 HOST_WIDE_INT val = INTVAL (rtl);
18677 if (val < 0)
18678 add_AT_int (die, DW_AT_const_value, val);
18679 else
18680 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18682 return true;
18684 case CONST_WIDE_INT:
18686 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18687 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18688 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18689 wide_int w = wi::zext (w1, prec);
18690 add_AT_wide (die, DW_AT_const_value, w);
18692 return true;
18694 case CONST_DOUBLE:
18695 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18696 floating-point constant. A CONST_DOUBLE is used whenever the
18697 constant requires more than one word in order to be adequately
18698 represented. */
18699 if (TARGET_SUPPORTS_WIDE_INT == 0
18700 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18701 add_AT_double (die, DW_AT_const_value,
18702 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18703 else
18705 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18706 unsigned int length = GET_MODE_SIZE (mode);
18707 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18709 insert_float (rtl, array);
18710 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18712 return true;
18714 case CONST_VECTOR:
18716 machine_mode mode = GET_MODE (rtl);
18717 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18718 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18719 unsigned char *array
18720 = ggc_vec_alloc<unsigned char> (length * elt_size);
18721 unsigned int i;
18722 unsigned char *p;
18723 machine_mode imode = GET_MODE_INNER (mode);
18725 switch (GET_MODE_CLASS (mode))
18727 case MODE_VECTOR_INT:
18728 for (i = 0, p = array; i < length; i++, p += elt_size)
18730 rtx elt = CONST_VECTOR_ELT (rtl, i);
18731 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18733 break;
18735 case MODE_VECTOR_FLOAT:
18736 for (i = 0, p = array; i < length; i++, p += elt_size)
18738 rtx elt = CONST_VECTOR_ELT (rtl, i);
18739 insert_float (elt, p);
18741 break;
18743 default:
18744 gcc_unreachable ();
18747 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18749 return true;
18751 case CONST_STRING:
18752 if (dwarf_version >= 4 || !dwarf_strict)
18754 dw_loc_descr_ref loc_result;
18755 resolve_one_addr (&rtl);
18756 rtl_addr:
18757 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18758 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18759 add_AT_loc (die, DW_AT_location, loc_result);
18760 vec_safe_push (used_rtx_array, rtl);
18761 return true;
18763 return false;
18765 case CONST:
18766 if (CONSTANT_P (XEXP (rtl, 0)))
18767 return add_const_value_attribute (die, XEXP (rtl, 0));
18768 /* FALLTHROUGH */
18769 case SYMBOL_REF:
18770 if (!const_ok_for_output (rtl))
18771 return false;
18772 /* FALLTHROUGH */
18773 case LABEL_REF:
18774 if (dwarf_version >= 4 || !dwarf_strict)
18775 goto rtl_addr;
18776 return false;
18778 case PLUS:
18779 /* In cases where an inlined instance of an inline function is passed
18780 the address of an `auto' variable (which is local to the caller) we
18781 can get a situation where the DECL_RTL of the artificial local
18782 variable (for the inlining) which acts as a stand-in for the
18783 corresponding formal parameter (of the inline function) will look
18784 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18785 exactly a compile-time constant expression, but it isn't the address
18786 of the (artificial) local variable either. Rather, it represents the
18787 *value* which the artificial local variable always has during its
18788 lifetime. We currently have no way to represent such quasi-constant
18789 values in Dwarf, so for now we just punt and generate nothing. */
18790 return false;
18792 case HIGH:
18793 case CONST_FIXED:
18794 return false;
18796 case MEM:
18797 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18798 && MEM_READONLY_P (rtl)
18799 && GET_MODE (rtl) == BLKmode)
18801 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18802 return true;
18804 return false;
18806 default:
18807 /* No other kinds of rtx should be possible here. */
18808 gcc_unreachable ();
18810 return false;
18813 /* Determine whether the evaluation of EXPR references any variables
18814 or functions which aren't otherwise used (and therefore may not be
18815 output). */
18816 static tree
18817 reference_to_unused (tree * tp, int * walk_subtrees,
18818 void * data ATTRIBUTE_UNUSED)
18820 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18821 *walk_subtrees = 0;
18823 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18824 && ! TREE_ASM_WRITTEN (*tp))
18825 return *tp;
18826 /* ??? The C++ FE emits debug information for using decls, so
18827 putting gcc_unreachable here falls over. See PR31899. For now
18828 be conservative. */
18829 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18830 return *tp;
18831 else if (VAR_P (*tp))
18833 varpool_node *node = varpool_node::get (*tp);
18834 if (!node || !node->definition)
18835 return *tp;
18837 else if (TREE_CODE (*tp) == FUNCTION_DECL
18838 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18840 /* The call graph machinery must have finished analyzing,
18841 optimizing and gimplifying the CU by now.
18842 So if *TP has no call graph node associated
18843 to it, it means *TP will not be emitted. */
18844 if (!cgraph_node::get (*tp))
18845 return *tp;
18847 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18848 return *tp;
18850 return NULL_TREE;
18853 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18854 for use in a later add_const_value_attribute call. */
18856 static rtx
18857 rtl_for_decl_init (tree init, tree type)
18859 rtx rtl = NULL_RTX;
18861 STRIP_NOPS (init);
18863 /* If a variable is initialized with a string constant without embedded
18864 zeros, build CONST_STRING. */
18865 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18867 tree enttype = TREE_TYPE (type);
18868 tree domain = TYPE_DOMAIN (type);
18869 scalar_int_mode mode;
18871 if (is_int_mode (TYPE_MODE (enttype), &mode)
18872 && GET_MODE_SIZE (mode) == 1
18873 && domain
18874 && integer_zerop (TYPE_MIN_VALUE (domain))
18875 && compare_tree_int (TYPE_MAX_VALUE (domain),
18876 TREE_STRING_LENGTH (init) - 1) == 0
18877 && ((size_t) TREE_STRING_LENGTH (init)
18878 == strlen (TREE_STRING_POINTER (init)) + 1))
18880 rtl = gen_rtx_CONST_STRING (VOIDmode,
18881 ggc_strdup (TREE_STRING_POINTER (init)));
18882 rtl = gen_rtx_MEM (BLKmode, rtl);
18883 MEM_READONLY_P (rtl) = 1;
18886 /* Other aggregates, and complex values, could be represented using
18887 CONCAT: FIXME! */
18888 else if (AGGREGATE_TYPE_P (type)
18889 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18890 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18891 || TREE_CODE (type) == COMPLEX_TYPE)
18893 /* Vectors only work if their mode is supported by the target.
18894 FIXME: generic vectors ought to work too. */
18895 else if (TREE_CODE (type) == VECTOR_TYPE
18896 && !VECTOR_MODE_P (TYPE_MODE (type)))
18898 /* If the initializer is something that we know will expand into an
18899 immediate RTL constant, expand it now. We must be careful not to
18900 reference variables which won't be output. */
18901 else if (initializer_constant_valid_p (init, type)
18902 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18904 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18905 possible. */
18906 if (TREE_CODE (type) == VECTOR_TYPE)
18907 switch (TREE_CODE (init))
18909 case VECTOR_CST:
18910 break;
18911 case CONSTRUCTOR:
18912 if (TREE_CONSTANT (init))
18914 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18915 bool constant_p = true;
18916 tree value;
18917 unsigned HOST_WIDE_INT ix;
18919 /* Even when ctor is constant, it might contain non-*_CST
18920 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18921 belong into VECTOR_CST nodes. */
18922 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18923 if (!CONSTANT_CLASS_P (value))
18925 constant_p = false;
18926 break;
18929 if (constant_p)
18931 init = build_vector_from_ctor (type, elts);
18932 break;
18935 /* FALLTHRU */
18937 default:
18938 return NULL;
18941 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18943 /* If expand_expr returns a MEM, it wasn't immediate. */
18944 gcc_assert (!rtl || !MEM_P (rtl));
18947 return rtl;
18950 /* Generate RTL for the variable DECL to represent its location. */
18952 static rtx
18953 rtl_for_decl_location (tree decl)
18955 rtx rtl;
18957 /* Here we have to decide where we are going to say the parameter "lives"
18958 (as far as the debugger is concerned). We only have a couple of
18959 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18961 DECL_RTL normally indicates where the parameter lives during most of the
18962 activation of the function. If optimization is enabled however, this
18963 could be either NULL or else a pseudo-reg. Both of those cases indicate
18964 that the parameter doesn't really live anywhere (as far as the code
18965 generation parts of GCC are concerned) during most of the function's
18966 activation. That will happen (for example) if the parameter is never
18967 referenced within the function.
18969 We could just generate a location descriptor here for all non-NULL
18970 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18971 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18972 where DECL_RTL is NULL or is a pseudo-reg.
18974 Note however that we can only get away with using DECL_INCOMING_RTL as
18975 a backup substitute for DECL_RTL in certain limited cases. In cases
18976 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18977 we can be sure that the parameter was passed using the same type as it is
18978 declared to have within the function, and that its DECL_INCOMING_RTL
18979 points us to a place where a value of that type is passed.
18981 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18982 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18983 because in these cases DECL_INCOMING_RTL points us to a value of some
18984 type which is *different* from the type of the parameter itself. Thus,
18985 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18986 such cases, the debugger would end up (for example) trying to fetch a
18987 `float' from a place which actually contains the first part of a
18988 `double'. That would lead to really incorrect and confusing
18989 output at debug-time.
18991 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18992 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18993 are a couple of exceptions however. On little-endian machines we can
18994 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18995 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18996 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18997 when (on a little-endian machine) a non-prototyped function has a
18998 parameter declared to be of type `short' or `char'. In such cases,
18999 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19000 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19001 passed `int' value. If the debugger then uses that address to fetch
19002 a `short' or a `char' (on a little-endian machine) the result will be
19003 the correct data, so we allow for such exceptional cases below.
19005 Note that our goal here is to describe the place where the given formal
19006 parameter lives during most of the function's activation (i.e. between the
19007 end of the prologue and the start of the epilogue). We'll do that as best
19008 as we can. Note however that if the given formal parameter is modified
19009 sometime during the execution of the function, then a stack backtrace (at
19010 debug-time) will show the function as having been called with the *new*
19011 value rather than the value which was originally passed in. This happens
19012 rarely enough that it is not a major problem, but it *is* a problem, and
19013 I'd like to fix it.
19015 A future version of dwarf2out.c may generate two additional attributes for
19016 any given DW_TAG_formal_parameter DIE which will describe the "passed
19017 type" and the "passed location" for the given formal parameter in addition
19018 to the attributes we now generate to indicate the "declared type" and the
19019 "active location" for each parameter. This additional set of attributes
19020 could be used by debuggers for stack backtraces. Separately, note that
19021 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19022 This happens (for example) for inlined-instances of inline function formal
19023 parameters which are never referenced. This really shouldn't be
19024 happening. All PARM_DECL nodes should get valid non-NULL
19025 DECL_INCOMING_RTL values. FIXME. */
19027 /* Use DECL_RTL as the "location" unless we find something better. */
19028 rtl = DECL_RTL_IF_SET (decl);
19030 /* When generating abstract instances, ignore everything except
19031 constants, symbols living in memory, and symbols living in
19032 fixed registers. */
19033 if (! reload_completed)
19035 if (rtl
19036 && (CONSTANT_P (rtl)
19037 || (MEM_P (rtl)
19038 && CONSTANT_P (XEXP (rtl, 0)))
19039 || (REG_P (rtl)
19040 && VAR_P (decl)
19041 && TREE_STATIC (decl))))
19043 rtl = targetm.delegitimize_address (rtl);
19044 return rtl;
19046 rtl = NULL_RTX;
19048 else if (TREE_CODE (decl) == PARM_DECL)
19050 if (rtl == NULL_RTX
19051 || is_pseudo_reg (rtl)
19052 || (MEM_P (rtl)
19053 && is_pseudo_reg (XEXP (rtl, 0))
19054 && DECL_INCOMING_RTL (decl)
19055 && MEM_P (DECL_INCOMING_RTL (decl))
19056 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19058 tree declared_type = TREE_TYPE (decl);
19059 tree passed_type = DECL_ARG_TYPE (decl);
19060 machine_mode dmode = TYPE_MODE (declared_type);
19061 machine_mode pmode = TYPE_MODE (passed_type);
19063 /* This decl represents a formal parameter which was optimized out.
19064 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19065 all cases where (rtl == NULL_RTX) just below. */
19066 if (dmode == pmode)
19067 rtl = DECL_INCOMING_RTL (decl);
19068 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19069 && SCALAR_INT_MODE_P (dmode)
19070 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19071 && DECL_INCOMING_RTL (decl))
19073 rtx inc = DECL_INCOMING_RTL (decl);
19074 if (REG_P (inc))
19075 rtl = inc;
19076 else if (MEM_P (inc))
19078 if (BYTES_BIG_ENDIAN)
19079 rtl = adjust_address_nv (inc, dmode,
19080 GET_MODE_SIZE (pmode)
19081 - GET_MODE_SIZE (dmode));
19082 else
19083 rtl = inc;
19088 /* If the parm was passed in registers, but lives on the stack, then
19089 make a big endian correction if the mode of the type of the
19090 parameter is not the same as the mode of the rtl. */
19091 /* ??? This is the same series of checks that are made in dbxout.c before
19092 we reach the big endian correction code there. It isn't clear if all
19093 of these checks are necessary here, but keeping them all is the safe
19094 thing to do. */
19095 else if (MEM_P (rtl)
19096 && XEXP (rtl, 0) != const0_rtx
19097 && ! CONSTANT_P (XEXP (rtl, 0))
19098 /* Not passed in memory. */
19099 && !MEM_P (DECL_INCOMING_RTL (decl))
19100 /* Not passed by invisible reference. */
19101 && (!REG_P (XEXP (rtl, 0))
19102 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19103 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19104 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19105 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19106 #endif
19108 /* Big endian correction check. */
19109 && BYTES_BIG_ENDIAN
19110 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19111 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19112 < UNITS_PER_WORD))
19114 machine_mode addr_mode = get_address_mode (rtl);
19115 int offset = (UNITS_PER_WORD
19116 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19118 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19119 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19122 else if (VAR_P (decl)
19123 && rtl
19124 && MEM_P (rtl)
19125 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19127 machine_mode addr_mode = get_address_mode (rtl);
19128 HOST_WIDE_INT offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19129 GET_MODE (rtl));
19131 /* If a variable is declared "register" yet is smaller than
19132 a register, then if we store the variable to memory, it
19133 looks like we're storing a register-sized value, when in
19134 fact we are not. We need to adjust the offset of the
19135 storage location to reflect the actual value's bytes,
19136 else gdb will not be able to display it. */
19137 if (offset != 0)
19138 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19139 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19142 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19143 and will have been substituted directly into all expressions that use it.
19144 C does not have such a concept, but C++ and other languages do. */
19145 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19146 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19148 if (rtl)
19149 rtl = targetm.delegitimize_address (rtl);
19151 /* If we don't look past the constant pool, we risk emitting a
19152 reference to a constant pool entry that isn't referenced from
19153 code, and thus is not emitted. */
19154 if (rtl)
19155 rtl = avoid_constant_pool_reference (rtl);
19157 /* Try harder to get a rtl. If this symbol ends up not being emitted
19158 in the current CU, resolve_addr will remove the expression referencing
19159 it. */
19160 if (rtl == NULL_RTX
19161 && VAR_P (decl)
19162 && !DECL_EXTERNAL (decl)
19163 && TREE_STATIC (decl)
19164 && DECL_NAME (decl)
19165 && !DECL_HARD_REGISTER (decl)
19166 && DECL_MODE (decl) != VOIDmode)
19168 rtl = make_decl_rtl_for_debug (decl);
19169 if (!MEM_P (rtl)
19170 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19171 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19172 rtl = NULL_RTX;
19175 return rtl;
19178 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19179 returned. If so, the decl for the COMMON block is returned, and the
19180 value is the offset into the common block for the symbol. */
19182 static tree
19183 fortran_common (tree decl, HOST_WIDE_INT *value)
19185 tree val_expr, cvar;
19186 machine_mode mode;
19187 HOST_WIDE_INT bitsize, bitpos;
19188 tree offset;
19189 int unsignedp, reversep, volatilep = 0;
19191 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19192 it does not have a value (the offset into the common area), or if it
19193 is thread local (as opposed to global) then it isn't common, and shouldn't
19194 be handled as such. */
19195 if (!VAR_P (decl)
19196 || !TREE_STATIC (decl)
19197 || !DECL_HAS_VALUE_EXPR_P (decl)
19198 || !is_fortran ())
19199 return NULL_TREE;
19201 val_expr = DECL_VALUE_EXPR (decl);
19202 if (TREE_CODE (val_expr) != COMPONENT_REF)
19203 return NULL_TREE;
19205 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19206 &unsignedp, &reversep, &volatilep);
19208 if (cvar == NULL_TREE
19209 || !VAR_P (cvar)
19210 || DECL_ARTIFICIAL (cvar)
19211 || !TREE_PUBLIC (cvar))
19212 return NULL_TREE;
19214 *value = 0;
19215 if (offset != NULL)
19217 if (!tree_fits_shwi_p (offset))
19218 return NULL_TREE;
19219 *value = tree_to_shwi (offset);
19221 if (bitpos != 0)
19222 *value += bitpos / BITS_PER_UNIT;
19224 return cvar;
19227 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19228 data attribute for a variable or a parameter. We generate the
19229 DW_AT_const_value attribute only in those cases where the given variable
19230 or parameter does not have a true "location" either in memory or in a
19231 register. This can happen (for example) when a constant is passed as an
19232 actual argument in a call to an inline function. (It's possible that
19233 these things can crop up in other ways also.) Note that one type of
19234 constant value which can be passed into an inlined function is a constant
19235 pointer. This can happen for example if an actual argument in an inlined
19236 function call evaluates to a compile-time constant address.
19238 CACHE_P is true if it is worth caching the location list for DECL,
19239 so that future calls can reuse it rather than regenerate it from scratch.
19240 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19241 since we will need to refer to them each time the function is inlined. */
19243 static bool
19244 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19246 rtx rtl;
19247 dw_loc_list_ref list;
19248 var_loc_list *loc_list;
19249 cached_dw_loc_list *cache;
19251 if (early_dwarf)
19252 return false;
19254 if (TREE_CODE (decl) == ERROR_MARK)
19255 return false;
19257 if (get_AT (die, DW_AT_location)
19258 || get_AT (die, DW_AT_const_value))
19259 return true;
19261 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19262 || TREE_CODE (decl) == RESULT_DECL);
19264 /* Try to get some constant RTL for this decl, and use that as the value of
19265 the location. */
19267 rtl = rtl_for_decl_location (decl);
19268 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19269 && add_const_value_attribute (die, rtl))
19270 return true;
19272 /* See if we have single element location list that is equivalent to
19273 a constant value. That way we are better to use add_const_value_attribute
19274 rather than expanding constant value equivalent. */
19275 loc_list = lookup_decl_loc (decl);
19276 if (loc_list
19277 && loc_list->first
19278 && loc_list->first->next == NULL
19279 && NOTE_P (loc_list->first->loc)
19280 && NOTE_VAR_LOCATION (loc_list->first->loc)
19281 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19283 struct var_loc_node *node;
19285 node = loc_list->first;
19286 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19287 if (GET_CODE (rtl) == EXPR_LIST)
19288 rtl = XEXP (rtl, 0);
19289 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19290 && add_const_value_attribute (die, rtl))
19291 return true;
19293 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19294 list several times. See if we've already cached the contents. */
19295 list = NULL;
19296 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19297 cache_p = false;
19298 if (cache_p)
19300 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19301 if (cache)
19302 list = cache->loc_list;
19304 if (list == NULL)
19306 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19307 NULL);
19308 /* It is usually worth caching this result if the decl is from
19309 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19310 if (cache_p && list && list->dw_loc_next)
19312 cached_dw_loc_list **slot
19313 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19314 DECL_UID (decl),
19315 INSERT);
19316 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19317 cache->decl_id = DECL_UID (decl);
19318 cache->loc_list = list;
19319 *slot = cache;
19322 if (list)
19324 add_AT_location_description (die, DW_AT_location, list);
19325 return true;
19327 /* None of that worked, so it must not really have a location;
19328 try adding a constant value attribute from the DECL_INITIAL. */
19329 return tree_add_const_value_attribute_for_decl (die, decl);
19332 /* Helper function for tree_add_const_value_attribute. Natively encode
19333 initializer INIT into an array. Return true if successful. */
19335 static bool
19336 native_encode_initializer (tree init, unsigned char *array, int size)
19338 tree type;
19340 if (init == NULL_TREE)
19341 return false;
19343 STRIP_NOPS (init);
19344 switch (TREE_CODE (init))
19346 case STRING_CST:
19347 type = TREE_TYPE (init);
19348 if (TREE_CODE (type) == ARRAY_TYPE)
19350 tree enttype = TREE_TYPE (type);
19351 scalar_int_mode mode;
19353 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19354 || GET_MODE_SIZE (mode) != 1)
19355 return false;
19356 if (int_size_in_bytes (type) != size)
19357 return false;
19358 if (size > TREE_STRING_LENGTH (init))
19360 memcpy (array, TREE_STRING_POINTER (init),
19361 TREE_STRING_LENGTH (init));
19362 memset (array + TREE_STRING_LENGTH (init),
19363 '\0', size - TREE_STRING_LENGTH (init));
19365 else
19366 memcpy (array, TREE_STRING_POINTER (init), size);
19367 return true;
19369 return false;
19370 case CONSTRUCTOR:
19371 type = TREE_TYPE (init);
19372 if (int_size_in_bytes (type) != size)
19373 return false;
19374 if (TREE_CODE (type) == ARRAY_TYPE)
19376 HOST_WIDE_INT min_index;
19377 unsigned HOST_WIDE_INT cnt;
19378 int curpos = 0, fieldsize;
19379 constructor_elt *ce;
19381 if (TYPE_DOMAIN (type) == NULL_TREE
19382 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19383 return false;
19385 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19386 if (fieldsize <= 0)
19387 return false;
19389 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19390 memset (array, '\0', size);
19391 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19393 tree val = ce->value;
19394 tree index = ce->index;
19395 int pos = curpos;
19396 if (index && TREE_CODE (index) == RANGE_EXPR)
19397 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19398 * fieldsize;
19399 else if (index)
19400 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19402 if (val)
19404 STRIP_NOPS (val);
19405 if (!native_encode_initializer (val, array + pos, fieldsize))
19406 return false;
19408 curpos = pos + fieldsize;
19409 if (index && TREE_CODE (index) == RANGE_EXPR)
19411 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19412 - tree_to_shwi (TREE_OPERAND (index, 0));
19413 while (count-- > 0)
19415 if (val)
19416 memcpy (array + curpos, array + pos, fieldsize);
19417 curpos += fieldsize;
19420 gcc_assert (curpos <= size);
19422 return true;
19424 else if (TREE_CODE (type) == RECORD_TYPE
19425 || TREE_CODE (type) == UNION_TYPE)
19427 tree field = NULL_TREE;
19428 unsigned HOST_WIDE_INT cnt;
19429 constructor_elt *ce;
19431 if (int_size_in_bytes (type) != size)
19432 return false;
19434 if (TREE_CODE (type) == RECORD_TYPE)
19435 field = TYPE_FIELDS (type);
19437 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19439 tree val = ce->value;
19440 int pos, fieldsize;
19442 if (ce->index != 0)
19443 field = ce->index;
19445 if (val)
19446 STRIP_NOPS (val);
19448 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19449 return false;
19451 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19452 && TYPE_DOMAIN (TREE_TYPE (field))
19453 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19454 return false;
19455 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19456 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19457 return false;
19458 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19459 pos = int_byte_position (field);
19460 gcc_assert (pos + fieldsize <= size);
19461 if (val && fieldsize != 0
19462 && !native_encode_initializer (val, array + pos, fieldsize))
19463 return false;
19465 return true;
19467 return false;
19468 case VIEW_CONVERT_EXPR:
19469 case NON_LVALUE_EXPR:
19470 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19471 default:
19472 return native_encode_expr (init, array, size) == size;
19476 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19477 attribute is the const value T. */
19479 static bool
19480 tree_add_const_value_attribute (dw_die_ref die, tree t)
19482 tree init;
19483 tree type = TREE_TYPE (t);
19484 rtx rtl;
19486 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19487 return false;
19489 init = t;
19490 gcc_assert (!DECL_P (init));
19492 if (TREE_CODE (init) == INTEGER_CST)
19494 if (tree_fits_uhwi_p (init))
19496 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19497 return true;
19499 if (tree_fits_shwi_p (init))
19501 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19502 return true;
19505 if (! early_dwarf)
19507 rtl = rtl_for_decl_init (init, type);
19508 if (rtl)
19509 return add_const_value_attribute (die, rtl);
19511 /* If the host and target are sane, try harder. */
19512 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19513 && initializer_constant_valid_p (init, type))
19515 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19516 if (size > 0 && (int) size == size)
19518 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19520 if (native_encode_initializer (init, array, size))
19522 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19523 return true;
19525 ggc_free (array);
19528 return false;
19531 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19532 attribute is the const value of T, where T is an integral constant
19533 variable with static storage duration
19534 (so it can't be a PARM_DECL or a RESULT_DECL). */
19536 static bool
19537 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19540 if (!decl
19541 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19542 || (VAR_P (decl) && !TREE_STATIC (decl)))
19543 return false;
19545 if (TREE_READONLY (decl)
19546 && ! TREE_THIS_VOLATILE (decl)
19547 && DECL_INITIAL (decl))
19548 /* OK */;
19549 else
19550 return false;
19552 /* Don't add DW_AT_const_value if abstract origin already has one. */
19553 if (get_AT (var_die, DW_AT_const_value))
19554 return false;
19556 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19559 /* Convert the CFI instructions for the current function into a
19560 location list. This is used for DW_AT_frame_base when we targeting
19561 a dwarf2 consumer that does not support the dwarf3
19562 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19563 expressions. */
19565 static dw_loc_list_ref
19566 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19568 int ix;
19569 dw_fde_ref fde;
19570 dw_loc_list_ref list, *list_tail;
19571 dw_cfi_ref cfi;
19572 dw_cfa_location last_cfa, next_cfa;
19573 const char *start_label, *last_label, *section;
19574 dw_cfa_location remember;
19576 fde = cfun->fde;
19577 gcc_assert (fde != NULL);
19579 section = secname_for_decl (current_function_decl);
19580 list_tail = &list;
19581 list = NULL;
19583 memset (&next_cfa, 0, sizeof (next_cfa));
19584 next_cfa.reg = INVALID_REGNUM;
19585 remember = next_cfa;
19587 start_label = fde->dw_fde_begin;
19589 /* ??? Bald assumption that the CIE opcode list does not contain
19590 advance opcodes. */
19591 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19592 lookup_cfa_1 (cfi, &next_cfa, &remember);
19594 last_cfa = next_cfa;
19595 last_label = start_label;
19597 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19599 /* If the first partition contained no CFI adjustments, the
19600 CIE opcodes apply to the whole first partition. */
19601 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19602 fde->dw_fde_begin, fde->dw_fde_end, section);
19603 list_tail =&(*list_tail)->dw_loc_next;
19604 start_label = last_label = fde->dw_fde_second_begin;
19607 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19609 switch (cfi->dw_cfi_opc)
19611 case DW_CFA_set_loc:
19612 case DW_CFA_advance_loc1:
19613 case DW_CFA_advance_loc2:
19614 case DW_CFA_advance_loc4:
19615 if (!cfa_equal_p (&last_cfa, &next_cfa))
19617 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19618 start_label, last_label, section);
19620 list_tail = &(*list_tail)->dw_loc_next;
19621 last_cfa = next_cfa;
19622 start_label = last_label;
19624 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19625 break;
19627 case DW_CFA_advance_loc:
19628 /* The encoding is complex enough that we should never emit this. */
19629 gcc_unreachable ();
19631 default:
19632 lookup_cfa_1 (cfi, &next_cfa, &remember);
19633 break;
19635 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19637 if (!cfa_equal_p (&last_cfa, &next_cfa))
19639 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19640 start_label, last_label, section);
19642 list_tail = &(*list_tail)->dw_loc_next;
19643 last_cfa = next_cfa;
19644 start_label = last_label;
19646 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19647 start_label, fde->dw_fde_end, section);
19648 list_tail = &(*list_tail)->dw_loc_next;
19649 start_label = last_label = fde->dw_fde_second_begin;
19653 if (!cfa_equal_p (&last_cfa, &next_cfa))
19655 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19656 start_label, last_label, section);
19657 list_tail = &(*list_tail)->dw_loc_next;
19658 start_label = last_label;
19661 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19662 start_label,
19663 fde->dw_fde_second_begin
19664 ? fde->dw_fde_second_end : fde->dw_fde_end,
19665 section);
19667 if (list && list->dw_loc_next)
19668 gen_llsym (list);
19670 return list;
19673 /* Compute a displacement from the "steady-state frame pointer" to the
19674 frame base (often the same as the CFA), and store it in
19675 frame_pointer_fb_offset. OFFSET is added to the displacement
19676 before the latter is negated. */
19678 static void
19679 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19681 rtx reg, elim;
19683 #ifdef FRAME_POINTER_CFA_OFFSET
19684 reg = frame_pointer_rtx;
19685 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19686 #else
19687 reg = arg_pointer_rtx;
19688 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19689 #endif
19691 elim = (ira_use_lra_p
19692 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19693 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19694 if (GET_CODE (elim) == PLUS)
19696 offset += INTVAL (XEXP (elim, 1));
19697 elim = XEXP (elim, 0);
19700 frame_pointer_fb_offset = -offset;
19702 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19703 in which to eliminate. This is because it's stack pointer isn't
19704 directly accessible as a register within the ISA. To work around
19705 this, assume that while we cannot provide a proper value for
19706 frame_pointer_fb_offset, we won't need one either. */
19707 frame_pointer_fb_offset_valid
19708 = ((SUPPORTS_STACK_ALIGNMENT
19709 && (elim == hard_frame_pointer_rtx
19710 || elim == stack_pointer_rtx))
19711 || elim == (frame_pointer_needed
19712 ? hard_frame_pointer_rtx
19713 : stack_pointer_rtx));
19716 /* Generate a DW_AT_name attribute given some string value to be included as
19717 the value of the attribute. */
19719 static void
19720 add_name_attribute (dw_die_ref die, const char *name_string)
19722 if (name_string != NULL && *name_string != 0)
19724 if (demangle_name_func)
19725 name_string = (*demangle_name_func) (name_string);
19727 add_AT_string (die, DW_AT_name, name_string);
19731 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19732 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19733 of TYPE accordingly.
19735 ??? This is a temporary measure until after we're able to generate
19736 regular DWARF for the complex Ada type system. */
19738 static void
19739 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19740 dw_die_ref context_die)
19742 tree dtype;
19743 dw_die_ref dtype_die;
19745 if (!lang_hooks.types.descriptive_type)
19746 return;
19748 dtype = lang_hooks.types.descriptive_type (type);
19749 if (!dtype)
19750 return;
19752 dtype_die = lookup_type_die (dtype);
19753 if (!dtype_die)
19755 gen_type_die (dtype, context_die);
19756 dtype_die = lookup_type_die (dtype);
19757 gcc_assert (dtype_die);
19760 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19763 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19765 static const char *
19766 comp_dir_string (void)
19768 const char *wd;
19769 char *wd1;
19770 static const char *cached_wd = NULL;
19772 if (cached_wd != NULL)
19773 return cached_wd;
19775 wd = get_src_pwd ();
19776 if (wd == NULL)
19777 return NULL;
19779 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19781 int wdlen;
19783 wdlen = strlen (wd);
19784 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19785 strcpy (wd1, wd);
19786 wd1 [wdlen] = DIR_SEPARATOR;
19787 wd1 [wdlen + 1] = 0;
19788 wd = wd1;
19791 cached_wd = remap_debug_filename (wd);
19792 return cached_wd;
19795 /* Generate a DW_AT_comp_dir attribute for DIE. */
19797 static void
19798 add_comp_dir_attribute (dw_die_ref die)
19800 const char * wd = comp_dir_string ();
19801 if (wd != NULL)
19802 add_AT_string (die, DW_AT_comp_dir, wd);
19805 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19806 pointer computation, ...), output a representation for that bound according
19807 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19808 loc_list_from_tree for the meaning of CONTEXT. */
19810 static void
19811 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19812 int forms, struct loc_descr_context *context)
19814 dw_die_ref context_die, decl_die;
19815 dw_loc_list_ref list;
19816 bool strip_conversions = true;
19817 bool placeholder_seen = false;
19819 while (strip_conversions)
19820 switch (TREE_CODE (value))
19822 case ERROR_MARK:
19823 case SAVE_EXPR:
19824 return;
19826 CASE_CONVERT:
19827 case VIEW_CONVERT_EXPR:
19828 value = TREE_OPERAND (value, 0);
19829 break;
19831 default:
19832 strip_conversions = false;
19833 break;
19836 /* If possible and permitted, output the attribute as a constant. */
19837 if ((forms & dw_scalar_form_constant) != 0
19838 && TREE_CODE (value) == INTEGER_CST)
19840 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19842 /* If HOST_WIDE_INT is big enough then represent the bound as
19843 a constant value. We need to choose a form based on
19844 whether the type is signed or unsigned. We cannot just
19845 call add_AT_unsigned if the value itself is positive
19846 (add_AT_unsigned might add the unsigned value encoded as
19847 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19848 bounds type and then sign extend any unsigned values found
19849 for signed types. This is needed only for
19850 DW_AT_{lower,upper}_bound, since for most other attributes,
19851 consumers will treat DW_FORM_data[1248] as unsigned values,
19852 regardless of the underlying type. */
19853 if (prec <= HOST_BITS_PER_WIDE_INT
19854 || tree_fits_uhwi_p (value))
19856 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19857 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19858 else
19859 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19861 else
19862 /* Otherwise represent the bound as an unsigned value with
19863 the precision of its type. The precision and signedness
19864 of the type will be necessary to re-interpret it
19865 unambiguously. */
19866 add_AT_wide (die, attr, wi::to_wide (value));
19867 return;
19870 /* Otherwise, if it's possible and permitted too, output a reference to
19871 another DIE. */
19872 if ((forms & dw_scalar_form_reference) != 0)
19874 tree decl = NULL_TREE;
19876 /* Some type attributes reference an outer type. For instance, the upper
19877 bound of an array may reference an embedding record (this happens in
19878 Ada). */
19879 if (TREE_CODE (value) == COMPONENT_REF
19880 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19881 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19882 decl = TREE_OPERAND (value, 1);
19884 else if (VAR_P (value)
19885 || TREE_CODE (value) == PARM_DECL
19886 || TREE_CODE (value) == RESULT_DECL)
19887 decl = value;
19889 if (decl != NULL_TREE)
19891 dw_die_ref decl_die = lookup_decl_die (decl);
19893 /* ??? Can this happen, or should the variable have been bound
19894 first? Probably it can, since I imagine that we try to create
19895 the types of parameters in the order in which they exist in
19896 the list, and won't have created a forward reference to a
19897 later parameter. */
19898 if (decl_die != NULL)
19900 add_AT_die_ref (die, attr, decl_die);
19901 return;
19906 /* Last chance: try to create a stack operation procedure to evaluate the
19907 value. Do nothing if even that is not possible or permitted. */
19908 if ((forms & dw_scalar_form_exprloc) == 0)
19909 return;
19911 list = loc_list_from_tree (value, 2, context);
19912 if (context && context->placeholder_arg)
19914 placeholder_seen = context->placeholder_seen;
19915 context->placeholder_seen = false;
19917 if (list == NULL || single_element_loc_list_p (list))
19919 /* If this attribute is not a reference nor constant, it is
19920 a DWARF expression rather than location description. For that
19921 loc_list_from_tree (value, 0, &context) is needed. */
19922 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19923 if (list2 && single_element_loc_list_p (list2))
19925 if (placeholder_seen)
19927 struct dwarf_procedure_info dpi;
19928 dpi.fndecl = NULL_TREE;
19929 dpi.args_count = 1;
19930 if (!resolve_args_picking (list2->expr, 1, &dpi))
19931 return;
19933 add_AT_loc (die, attr, list2->expr);
19934 return;
19938 /* If that failed to give a single element location list, fall back to
19939 outputting this as a reference... still if permitted. */
19940 if (list == NULL
19941 || (forms & dw_scalar_form_reference) == 0
19942 || placeholder_seen)
19943 return;
19945 if (current_function_decl == 0)
19946 context_die = comp_unit_die ();
19947 else
19948 context_die = lookup_decl_die (current_function_decl);
19950 decl_die = new_die (DW_TAG_variable, context_die, value);
19951 add_AT_flag (decl_die, DW_AT_artificial, 1);
19952 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19953 context_die);
19954 add_AT_location_description (decl_die, DW_AT_location, list);
19955 add_AT_die_ref (die, attr, decl_die);
19958 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19959 default. */
19961 static int
19962 lower_bound_default (void)
19964 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19966 case DW_LANG_C:
19967 case DW_LANG_C89:
19968 case DW_LANG_C99:
19969 case DW_LANG_C11:
19970 case DW_LANG_C_plus_plus:
19971 case DW_LANG_C_plus_plus_11:
19972 case DW_LANG_C_plus_plus_14:
19973 case DW_LANG_ObjC:
19974 case DW_LANG_ObjC_plus_plus:
19975 return 0;
19976 case DW_LANG_Fortran77:
19977 case DW_LANG_Fortran90:
19978 case DW_LANG_Fortran95:
19979 case DW_LANG_Fortran03:
19980 case DW_LANG_Fortran08:
19981 return 1;
19982 case DW_LANG_UPC:
19983 case DW_LANG_D:
19984 case DW_LANG_Python:
19985 return dwarf_version >= 4 ? 0 : -1;
19986 case DW_LANG_Ada95:
19987 case DW_LANG_Ada83:
19988 case DW_LANG_Cobol74:
19989 case DW_LANG_Cobol85:
19990 case DW_LANG_Modula2:
19991 case DW_LANG_PLI:
19992 return dwarf_version >= 4 ? 1 : -1;
19993 default:
19994 return -1;
19998 /* Given a tree node describing an array bound (either lower or upper) output
19999 a representation for that bound. */
20001 static void
20002 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20003 tree bound, struct loc_descr_context *context)
20005 int dflt;
20007 while (1)
20008 switch (TREE_CODE (bound))
20010 /* Strip all conversions. */
20011 CASE_CONVERT:
20012 case VIEW_CONVERT_EXPR:
20013 bound = TREE_OPERAND (bound, 0);
20014 break;
20016 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20017 are even omitted when they are the default. */
20018 case INTEGER_CST:
20019 /* If the value for this bound is the default one, we can even omit the
20020 attribute. */
20021 if (bound_attr == DW_AT_lower_bound
20022 && tree_fits_shwi_p (bound)
20023 && (dflt = lower_bound_default ()) != -1
20024 && tree_to_shwi (bound) == dflt)
20025 return;
20027 /* FALLTHRU */
20029 default:
20030 /* Because of the complex interaction there can be with other GNAT
20031 encodings, GDB isn't ready yet to handle proper DWARF description
20032 for self-referencial subrange bounds: let GNAT encodings do the
20033 magic in such a case. */
20034 if (is_ada ()
20035 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20036 && contains_placeholder_p (bound))
20037 return;
20039 add_scalar_info (subrange_die, bound_attr, bound,
20040 dw_scalar_form_constant
20041 | dw_scalar_form_exprloc
20042 | dw_scalar_form_reference,
20043 context);
20044 return;
20048 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20049 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20050 Note that the block of subscript information for an array type also
20051 includes information about the element type of the given array type.
20053 This function reuses previously set type and bound information if
20054 available. */
20056 static void
20057 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20059 unsigned dimension_number;
20060 tree lower, upper;
20061 dw_die_ref child = type_die->die_child;
20063 for (dimension_number = 0;
20064 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20065 type = TREE_TYPE (type), dimension_number++)
20067 tree domain = TYPE_DOMAIN (type);
20069 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20070 break;
20072 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20073 and (in GNU C only) variable bounds. Handle all three forms
20074 here. */
20076 /* Find and reuse a previously generated DW_TAG_subrange_type if
20077 available.
20079 For multi-dimensional arrays, as we iterate through the
20080 various dimensions in the enclosing for loop above, we also
20081 iterate through the DIE children and pick at each
20082 DW_TAG_subrange_type previously generated (if available).
20083 Each child DW_TAG_subrange_type DIE describes the range of
20084 the current dimension. At this point we should have as many
20085 DW_TAG_subrange_type's as we have dimensions in the
20086 array. */
20087 dw_die_ref subrange_die = NULL;
20088 if (child)
20089 while (1)
20091 child = child->die_sib;
20092 if (child->die_tag == DW_TAG_subrange_type)
20093 subrange_die = child;
20094 if (child == type_die->die_child)
20096 /* If we wrapped around, stop looking next time. */
20097 child = NULL;
20098 break;
20100 if (child->die_tag == DW_TAG_subrange_type)
20101 break;
20103 if (!subrange_die)
20104 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20106 if (domain)
20108 /* We have an array type with specified bounds. */
20109 lower = TYPE_MIN_VALUE (domain);
20110 upper = TYPE_MAX_VALUE (domain);
20112 /* Define the index type. */
20113 if (TREE_TYPE (domain)
20114 && !get_AT (subrange_die, DW_AT_type))
20116 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20117 TREE_TYPE field. We can't emit debug info for this
20118 because it is an unnamed integral type. */
20119 if (TREE_CODE (domain) == INTEGER_TYPE
20120 && TYPE_NAME (domain) == NULL_TREE
20121 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20122 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20124 else
20125 add_type_attribute (subrange_die, TREE_TYPE (domain),
20126 TYPE_UNQUALIFIED, false, type_die);
20129 /* ??? If upper is NULL, the array has unspecified length,
20130 but it does have a lower bound. This happens with Fortran
20131 dimension arr(N:*)
20132 Since the debugger is definitely going to need to know N
20133 to produce useful results, go ahead and output the lower
20134 bound solo, and hope the debugger can cope. */
20136 if (!get_AT (subrange_die, DW_AT_lower_bound))
20137 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20138 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20139 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20142 /* Otherwise we have an array type with an unspecified length. The
20143 DWARF-2 spec does not say how to handle this; let's just leave out the
20144 bounds. */
20148 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20150 static void
20151 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20153 dw_die_ref decl_die;
20154 HOST_WIDE_INT size;
20155 dw_loc_descr_ref size_expr = NULL;
20157 switch (TREE_CODE (tree_node))
20159 case ERROR_MARK:
20160 size = 0;
20161 break;
20162 case ENUMERAL_TYPE:
20163 case RECORD_TYPE:
20164 case UNION_TYPE:
20165 case QUAL_UNION_TYPE:
20166 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20167 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20169 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20170 return;
20172 size_expr = type_byte_size (tree_node, &size);
20173 break;
20174 case FIELD_DECL:
20175 /* For a data member of a struct or union, the DW_AT_byte_size is
20176 generally given as the number of bytes normally allocated for an
20177 object of the *declared* type of the member itself. This is true
20178 even for bit-fields. */
20179 size = int_size_in_bytes (field_type (tree_node));
20180 break;
20181 default:
20182 gcc_unreachable ();
20185 /* Support for dynamically-sized objects was introduced by DWARFv3.
20186 At the moment, GDB does not handle variable byte sizes very well,
20187 though. */
20188 if ((dwarf_version >= 3 || !dwarf_strict)
20189 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20190 && size_expr != NULL)
20191 add_AT_loc (die, DW_AT_byte_size, size_expr);
20193 /* Note that `size' might be -1 when we get to this point. If it is, that
20194 indicates that the byte size of the entity in question is variable and
20195 that we could not generate a DWARF expression that computes it. */
20196 if (size >= 0)
20197 add_AT_unsigned (die, DW_AT_byte_size, size);
20200 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20201 alignment. */
20203 static void
20204 add_alignment_attribute (dw_die_ref die, tree tree_node)
20206 if (dwarf_version < 5 && dwarf_strict)
20207 return;
20209 unsigned align;
20211 if (DECL_P (tree_node))
20213 if (!DECL_USER_ALIGN (tree_node))
20214 return;
20216 align = DECL_ALIGN_UNIT (tree_node);
20218 else if (TYPE_P (tree_node))
20220 if (!TYPE_USER_ALIGN (tree_node))
20221 return;
20223 align = TYPE_ALIGN_UNIT (tree_node);
20225 else
20226 gcc_unreachable ();
20228 add_AT_unsigned (die, DW_AT_alignment, align);
20231 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20232 which specifies the distance in bits from the highest order bit of the
20233 "containing object" for the bit-field to the highest order bit of the
20234 bit-field itself.
20236 For any given bit-field, the "containing object" is a hypothetical object
20237 (of some integral or enum type) within which the given bit-field lives. The
20238 type of this hypothetical "containing object" is always the same as the
20239 declared type of the individual bit-field itself. The determination of the
20240 exact location of the "containing object" for a bit-field is rather
20241 complicated. It's handled by the `field_byte_offset' function (above).
20243 CTX is required: see the comment for VLR_CONTEXT.
20245 Note that it is the size (in bytes) of the hypothetical "containing object"
20246 which will be given in the DW_AT_byte_size attribute for this bit-field.
20247 (See `byte_size_attribute' above). */
20249 static inline void
20250 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20252 HOST_WIDE_INT object_offset_in_bytes;
20253 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20254 HOST_WIDE_INT bitpos_int;
20255 HOST_WIDE_INT highest_order_object_bit_offset;
20256 HOST_WIDE_INT highest_order_field_bit_offset;
20257 HOST_WIDE_INT bit_offset;
20259 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20261 /* Must be a field and a bit field. */
20262 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20264 /* We can't yet handle bit-fields whose offsets are variable, so if we
20265 encounter such things, just return without generating any attribute
20266 whatsoever. Likewise for variable or too large size. */
20267 if (! tree_fits_shwi_p (bit_position (decl))
20268 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20269 return;
20271 bitpos_int = int_bit_position (decl);
20273 /* Note that the bit offset is always the distance (in bits) from the
20274 highest-order bit of the "containing object" to the highest-order bit of
20275 the bit-field itself. Since the "high-order end" of any object or field
20276 is different on big-endian and little-endian machines, the computation
20277 below must take account of these differences. */
20278 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20279 highest_order_field_bit_offset = bitpos_int;
20281 if (! BYTES_BIG_ENDIAN)
20283 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20284 highest_order_object_bit_offset +=
20285 simple_type_size_in_bits (original_type);
20288 bit_offset
20289 = (! BYTES_BIG_ENDIAN
20290 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20291 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20293 if (bit_offset < 0)
20294 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20295 else
20296 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20299 /* For a FIELD_DECL node which represents a bit field, output an attribute
20300 which specifies the length in bits of the given field. */
20302 static inline void
20303 add_bit_size_attribute (dw_die_ref die, tree decl)
20305 /* Must be a field and a bit field. */
20306 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20307 && DECL_BIT_FIELD_TYPE (decl));
20309 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20310 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20313 /* If the compiled language is ANSI C, then add a 'prototyped'
20314 attribute, if arg types are given for the parameters of a function. */
20316 static inline void
20317 add_prototyped_attribute (dw_die_ref die, tree func_type)
20319 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20321 case DW_LANG_C:
20322 case DW_LANG_C89:
20323 case DW_LANG_C99:
20324 case DW_LANG_C11:
20325 case DW_LANG_ObjC:
20326 if (prototype_p (func_type))
20327 add_AT_flag (die, DW_AT_prototyped, 1);
20328 break;
20329 default:
20330 break;
20334 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20335 by looking in the type declaration, the object declaration equate table or
20336 the block mapping. */
20338 static inline dw_die_ref
20339 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20341 dw_die_ref origin_die = NULL;
20343 if (DECL_P (origin))
20345 dw_die_ref c;
20346 origin_die = lookup_decl_die (origin);
20347 /* "Unwrap" the decls DIE which we put in the imported unit context.
20348 We are looking for the abstract copy here. */
20349 if (in_lto_p
20350 && origin_die
20351 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20352 /* ??? Identify this better. */
20353 && c->with_offset)
20354 origin_die = c;
20356 else if (TYPE_P (origin))
20357 origin_die = lookup_type_die (origin);
20358 else if (TREE_CODE (origin) == BLOCK)
20359 origin_die = BLOCK_DIE (origin);
20361 /* XXX: Functions that are never lowered don't always have correct block
20362 trees (in the case of java, they simply have no block tree, in some other
20363 languages). For these functions, there is nothing we can really do to
20364 output correct debug info for inlined functions in all cases. Rather
20365 than die, we'll just produce deficient debug info now, in that we will
20366 have variables without a proper abstract origin. In the future, when all
20367 functions are lowered, we should re-add a gcc_assert (origin_die)
20368 here. */
20370 if (origin_die)
20371 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20372 return origin_die;
20375 /* We do not currently support the pure_virtual attribute. */
20377 static inline void
20378 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20380 if (DECL_VINDEX (func_decl))
20382 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20384 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20385 add_AT_loc (die, DW_AT_vtable_elem_location,
20386 new_loc_descr (DW_OP_constu,
20387 tree_to_shwi (DECL_VINDEX (func_decl)),
20388 0));
20390 /* GNU extension: Record what type this method came from originally. */
20391 if (debug_info_level > DINFO_LEVEL_TERSE
20392 && DECL_CONTEXT (func_decl))
20393 add_AT_die_ref (die, DW_AT_containing_type,
20394 lookup_type_die (DECL_CONTEXT (func_decl)));
20398 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20399 given decl. This used to be a vendor extension until after DWARF 4
20400 standardized it. */
20402 static void
20403 add_linkage_attr (dw_die_ref die, tree decl)
20405 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20407 /* Mimic what assemble_name_raw does with a leading '*'. */
20408 if (name[0] == '*')
20409 name = &name[1];
20411 if (dwarf_version >= 4)
20412 add_AT_string (die, DW_AT_linkage_name, name);
20413 else
20414 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20417 /* Add source coordinate attributes for the given decl. */
20419 static void
20420 add_src_coords_attributes (dw_die_ref die, tree decl)
20422 expanded_location s;
20424 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20425 return;
20426 s = expand_location (DECL_SOURCE_LOCATION (decl));
20427 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20428 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20429 if (debug_column_info && s.column)
20430 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20433 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20435 static void
20436 add_linkage_name_raw (dw_die_ref die, tree decl)
20438 /* Defer until we have an assembler name set. */
20439 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20441 limbo_die_node *asm_name;
20443 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20444 asm_name->die = die;
20445 asm_name->created_for = decl;
20446 asm_name->next = deferred_asm_name;
20447 deferred_asm_name = asm_name;
20449 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20450 add_linkage_attr (die, decl);
20453 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20455 static void
20456 add_linkage_name (dw_die_ref die, tree decl)
20458 if (debug_info_level > DINFO_LEVEL_NONE
20459 && VAR_OR_FUNCTION_DECL_P (decl)
20460 && TREE_PUBLIC (decl)
20461 && !(VAR_P (decl) && DECL_REGISTER (decl))
20462 && die->die_tag != DW_TAG_member)
20463 add_linkage_name_raw (die, decl);
20466 /* Add a DW_AT_name attribute and source coordinate attribute for the
20467 given decl, but only if it actually has a name. */
20469 static void
20470 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20471 bool no_linkage_name)
20473 tree decl_name;
20475 decl_name = DECL_NAME (decl);
20476 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20478 const char *name = dwarf2_name (decl, 0);
20479 if (name)
20480 add_name_attribute (die, name);
20481 if (! DECL_ARTIFICIAL (decl))
20482 add_src_coords_attributes (die, decl);
20484 if (!no_linkage_name)
20485 add_linkage_name (die, decl);
20488 #ifdef VMS_DEBUGGING_INFO
20489 /* Get the function's name, as described by its RTL. This may be different
20490 from the DECL_NAME name used in the source file. */
20491 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20493 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20494 XEXP (DECL_RTL (decl), 0), false);
20495 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20497 #endif /* VMS_DEBUGGING_INFO */
20500 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20502 static void
20503 add_discr_value (dw_die_ref die, dw_discr_value *value)
20505 dw_attr_node attr;
20507 attr.dw_attr = DW_AT_discr_value;
20508 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20509 attr.dw_attr_val.val_entry = NULL;
20510 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20511 if (value->pos)
20512 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20513 else
20514 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20515 add_dwarf_attr (die, &attr);
20518 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20520 static void
20521 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20523 dw_attr_node attr;
20525 attr.dw_attr = DW_AT_discr_list;
20526 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20527 attr.dw_attr_val.val_entry = NULL;
20528 attr.dw_attr_val.v.val_discr_list = discr_list;
20529 add_dwarf_attr (die, &attr);
20532 static inline dw_discr_list_ref
20533 AT_discr_list (dw_attr_node *attr)
20535 return attr->dw_attr_val.v.val_discr_list;
20538 #ifdef VMS_DEBUGGING_INFO
20539 /* Output the debug main pointer die for VMS */
20541 void
20542 dwarf2out_vms_debug_main_pointer (void)
20544 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20545 dw_die_ref die;
20547 /* Allocate the VMS debug main subprogram die. */
20548 die = new_die_raw (DW_TAG_subprogram);
20549 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20550 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20551 current_function_funcdef_no);
20552 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20554 /* Make it the first child of comp_unit_die (). */
20555 die->die_parent = comp_unit_die ();
20556 if (comp_unit_die ()->die_child)
20558 die->die_sib = comp_unit_die ()->die_child->die_sib;
20559 comp_unit_die ()->die_child->die_sib = die;
20561 else
20563 die->die_sib = die;
20564 comp_unit_die ()->die_child = die;
20567 #endif /* VMS_DEBUGGING_INFO */
20569 /* Push a new declaration scope. */
20571 static void
20572 push_decl_scope (tree scope)
20574 vec_safe_push (decl_scope_table, scope);
20577 /* Pop a declaration scope. */
20579 static inline void
20580 pop_decl_scope (void)
20582 decl_scope_table->pop ();
20585 /* walk_tree helper function for uses_local_type, below. */
20587 static tree
20588 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20590 if (!TYPE_P (*tp))
20591 *walk_subtrees = 0;
20592 else
20594 tree name = TYPE_NAME (*tp);
20595 if (name && DECL_P (name) && decl_function_context (name))
20596 return *tp;
20598 return NULL_TREE;
20601 /* If TYPE involves a function-local type (including a local typedef to a
20602 non-local type), returns that type; otherwise returns NULL_TREE. */
20604 static tree
20605 uses_local_type (tree type)
20607 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20608 return used;
20611 /* Return the DIE for the scope that immediately contains this type.
20612 Non-named types that do not involve a function-local type get global
20613 scope. Named types nested in namespaces or other types get their
20614 containing scope. All other types (i.e. function-local named types) get
20615 the current active scope. */
20617 static dw_die_ref
20618 scope_die_for (tree t, dw_die_ref context_die)
20620 dw_die_ref scope_die = NULL;
20621 tree containing_scope;
20623 /* Non-types always go in the current scope. */
20624 gcc_assert (TYPE_P (t));
20626 /* Use the scope of the typedef, rather than the scope of the type
20627 it refers to. */
20628 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20629 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20630 else
20631 containing_scope = TYPE_CONTEXT (t);
20633 /* Use the containing namespace if there is one. */
20634 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20636 if (context_die == lookup_decl_die (containing_scope))
20637 /* OK */;
20638 else if (debug_info_level > DINFO_LEVEL_TERSE)
20639 context_die = get_context_die (containing_scope);
20640 else
20641 containing_scope = NULL_TREE;
20644 /* Ignore function type "scopes" from the C frontend. They mean that
20645 a tagged type is local to a parmlist of a function declarator, but
20646 that isn't useful to DWARF. */
20647 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20648 containing_scope = NULL_TREE;
20650 if (SCOPE_FILE_SCOPE_P (containing_scope))
20652 /* If T uses a local type keep it local as well, to avoid references
20653 to function-local DIEs from outside the function. */
20654 if (current_function_decl && uses_local_type (t))
20655 scope_die = context_die;
20656 else
20657 scope_die = comp_unit_die ();
20659 else if (TYPE_P (containing_scope))
20661 /* For types, we can just look up the appropriate DIE. */
20662 if (debug_info_level > DINFO_LEVEL_TERSE)
20663 scope_die = get_context_die (containing_scope);
20664 else
20666 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20667 if (scope_die == NULL)
20668 scope_die = comp_unit_die ();
20671 else
20672 scope_die = context_die;
20674 return scope_die;
20677 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20679 static inline int
20680 local_scope_p (dw_die_ref context_die)
20682 for (; context_die; context_die = context_die->die_parent)
20683 if (context_die->die_tag == DW_TAG_inlined_subroutine
20684 || context_die->die_tag == DW_TAG_subprogram)
20685 return 1;
20687 return 0;
20690 /* Returns nonzero if CONTEXT_DIE is a class. */
20692 static inline int
20693 class_scope_p (dw_die_ref context_die)
20695 return (context_die
20696 && (context_die->die_tag == DW_TAG_structure_type
20697 || context_die->die_tag == DW_TAG_class_type
20698 || context_die->die_tag == DW_TAG_interface_type
20699 || context_die->die_tag == DW_TAG_union_type));
20702 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20703 whether or not to treat a DIE in this context as a declaration. */
20705 static inline int
20706 class_or_namespace_scope_p (dw_die_ref context_die)
20708 return (class_scope_p (context_die)
20709 || (context_die && context_die->die_tag == DW_TAG_namespace));
20712 /* Many forms of DIEs require a "type description" attribute. This
20713 routine locates the proper "type descriptor" die for the type given
20714 by 'type' plus any additional qualifiers given by 'cv_quals', and
20715 adds a DW_AT_type attribute below the given die. */
20717 static void
20718 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20719 bool reverse, dw_die_ref context_die)
20721 enum tree_code code = TREE_CODE (type);
20722 dw_die_ref type_die = NULL;
20724 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20725 or fixed-point type, use the inner type. This is because we have no
20726 support for unnamed types in base_type_die. This can happen if this is
20727 an Ada subrange type. Correct solution is emit a subrange type die. */
20728 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20729 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20730 type = TREE_TYPE (type), code = TREE_CODE (type);
20732 if (code == ERROR_MARK
20733 /* Handle a special case. For functions whose return type is void, we
20734 generate *no* type attribute. (Note that no object may have type
20735 `void', so this only applies to function return types). */
20736 || code == VOID_TYPE)
20737 return;
20739 type_die = modified_type_die (type,
20740 cv_quals | TYPE_QUALS (type),
20741 reverse,
20742 context_die);
20744 if (type_die != NULL)
20745 add_AT_die_ref (object_die, DW_AT_type, type_die);
20748 /* Given an object die, add the calling convention attribute for the
20749 function call type. */
20750 static void
20751 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20753 enum dwarf_calling_convention value = DW_CC_normal;
20755 value = ((enum dwarf_calling_convention)
20756 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20758 if (is_fortran ()
20759 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20761 /* DWARF 2 doesn't provide a way to identify a program's source-level
20762 entry point. DW_AT_calling_convention attributes are only meant
20763 to describe functions' calling conventions. However, lacking a
20764 better way to signal the Fortran main program, we used this for
20765 a long time, following existing custom. Now, DWARF 4 has
20766 DW_AT_main_subprogram, which we add below, but some tools still
20767 rely on the old way, which we thus keep. */
20768 value = DW_CC_program;
20770 if (dwarf_version >= 4 || !dwarf_strict)
20771 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20774 /* Only add the attribute if the backend requests it, and
20775 is not DW_CC_normal. */
20776 if (value && (value != DW_CC_normal))
20777 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20780 /* Given a tree pointer to a struct, class, union, or enum type node, return
20781 a pointer to the (string) tag name for the given type, or zero if the type
20782 was declared without a tag. */
20784 static const char *
20785 type_tag (const_tree type)
20787 const char *name = 0;
20789 if (TYPE_NAME (type) != 0)
20791 tree t = 0;
20793 /* Find the IDENTIFIER_NODE for the type name. */
20794 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20795 && !TYPE_NAMELESS (type))
20796 t = TYPE_NAME (type);
20798 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20799 a TYPE_DECL node, regardless of whether or not a `typedef' was
20800 involved. */
20801 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20802 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20804 /* We want to be extra verbose. Don't call dwarf_name if
20805 DECL_NAME isn't set. The default hook for decl_printable_name
20806 doesn't like that, and in this context it's correct to return
20807 0, instead of "<anonymous>" or the like. */
20808 if (DECL_NAME (TYPE_NAME (type))
20809 && !DECL_NAMELESS (TYPE_NAME (type)))
20810 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20813 /* Now get the name as a string, or invent one. */
20814 if (!name && t != 0)
20815 name = IDENTIFIER_POINTER (t);
20818 return (name == 0 || *name == '\0') ? 0 : name;
20821 /* Return the type associated with a data member, make a special check
20822 for bit field types. */
20824 static inline tree
20825 member_declared_type (const_tree member)
20827 return (DECL_BIT_FIELD_TYPE (member)
20828 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20831 /* Get the decl's label, as described by its RTL. This may be different
20832 from the DECL_NAME name used in the source file. */
20834 #if 0
20835 static const char *
20836 decl_start_label (tree decl)
20838 rtx x;
20839 const char *fnname;
20841 x = DECL_RTL (decl);
20842 gcc_assert (MEM_P (x));
20844 x = XEXP (x, 0);
20845 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20847 fnname = XSTR (x, 0);
20848 return fnname;
20850 #endif
20852 /* For variable-length arrays that have been previously generated, but
20853 may be incomplete due to missing subscript info, fill the subscript
20854 info. Return TRUE if this is one of those cases. */
20855 static bool
20856 fill_variable_array_bounds (tree type)
20858 if (TREE_ASM_WRITTEN (type)
20859 && TREE_CODE (type) == ARRAY_TYPE
20860 && variably_modified_type_p (type, NULL))
20862 dw_die_ref array_die = lookup_type_die (type);
20863 if (!array_die)
20864 return false;
20865 add_subscript_info (array_die, type, !is_ada ());
20866 return true;
20868 return false;
20871 /* These routines generate the internal representation of the DIE's for
20872 the compilation unit. Debugging information is collected by walking
20873 the declaration trees passed in from dwarf2out_decl(). */
20875 static void
20876 gen_array_type_die (tree type, dw_die_ref context_die)
20878 dw_die_ref array_die;
20880 /* GNU compilers represent multidimensional array types as sequences of one
20881 dimensional array types whose element types are themselves array types.
20882 We sometimes squish that down to a single array_type DIE with multiple
20883 subscripts in the Dwarf debugging info. The draft Dwarf specification
20884 say that we are allowed to do this kind of compression in C, because
20885 there is no difference between an array of arrays and a multidimensional
20886 array. We don't do this for Ada to remain as close as possible to the
20887 actual representation, which is especially important against the language
20888 flexibilty wrt arrays of variable size. */
20890 bool collapse_nested_arrays = !is_ada ();
20892 if (fill_variable_array_bounds (type))
20893 return;
20895 dw_die_ref scope_die = scope_die_for (type, context_die);
20896 tree element_type;
20898 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20899 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20900 if (TYPE_STRING_FLAG (type)
20901 && TREE_CODE (type) == ARRAY_TYPE
20902 && is_fortran ()
20903 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20905 HOST_WIDE_INT size;
20907 array_die = new_die (DW_TAG_string_type, scope_die, type);
20908 add_name_attribute (array_die, type_tag (type));
20909 equate_type_number_to_die (type, array_die);
20910 size = int_size_in_bytes (type);
20911 if (size >= 0)
20912 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20913 /* ??? We can't annotate types late, but for LTO we may not
20914 generate a location early either (gfortran.dg/save_6.f90). */
20915 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20916 && TYPE_DOMAIN (type) != NULL_TREE
20917 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20919 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20920 tree rszdecl = szdecl;
20922 size = int_size_in_bytes (TREE_TYPE (szdecl));
20923 if (!DECL_P (szdecl))
20925 if (TREE_CODE (szdecl) == INDIRECT_REF
20926 && DECL_P (TREE_OPERAND (szdecl, 0)))
20928 rszdecl = TREE_OPERAND (szdecl, 0);
20929 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20930 != DWARF2_ADDR_SIZE)
20931 size = 0;
20933 else
20934 size = 0;
20936 if (size > 0)
20938 dw_loc_list_ref loc
20939 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20940 NULL);
20941 if (loc)
20943 add_AT_location_description (array_die, DW_AT_string_length,
20944 loc);
20945 if (size != DWARF2_ADDR_SIZE)
20946 add_AT_unsigned (array_die, dwarf_version >= 5
20947 ? DW_AT_string_length_byte_size
20948 : DW_AT_byte_size, size);
20952 return;
20955 array_die = new_die (DW_TAG_array_type, scope_die, type);
20956 add_name_attribute (array_die, type_tag (type));
20957 equate_type_number_to_die (type, array_die);
20959 if (TREE_CODE (type) == VECTOR_TYPE)
20960 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20962 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20963 if (is_fortran ()
20964 && TREE_CODE (type) == ARRAY_TYPE
20965 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20966 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20967 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20969 #if 0
20970 /* We default the array ordering. Debuggers will probably do the right
20971 things even if DW_AT_ordering is not present. It's not even an issue
20972 until we start to get into multidimensional arrays anyway. If a debugger
20973 is ever caught doing the Wrong Thing for multi-dimensional arrays,
20974 then we'll have to put the DW_AT_ordering attribute back in. (But if
20975 and when we find out that we need to put these in, we will only do so
20976 for multidimensional arrays. */
20977 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20978 #endif
20980 if (TREE_CODE (type) == VECTOR_TYPE)
20982 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20983 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20984 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20985 add_bound_info (subrange_die, DW_AT_upper_bound,
20986 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20988 else
20989 add_subscript_info (array_die, type, collapse_nested_arrays);
20991 /* Add representation of the type of the elements of this array type and
20992 emit the corresponding DIE if we haven't done it already. */
20993 element_type = TREE_TYPE (type);
20994 if (collapse_nested_arrays)
20995 while (TREE_CODE (element_type) == ARRAY_TYPE)
20997 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20998 break;
20999 element_type = TREE_TYPE (element_type);
21002 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21003 TREE_CODE (type) == ARRAY_TYPE
21004 && TYPE_REVERSE_STORAGE_ORDER (type),
21005 context_die);
21007 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21008 if (TYPE_ARTIFICIAL (type))
21009 add_AT_flag (array_die, DW_AT_artificial, 1);
21011 if (get_AT (array_die, DW_AT_name))
21012 add_pubtype (type, array_die);
21014 add_alignment_attribute (array_die, type);
21017 /* This routine generates DIE for array with hidden descriptor, details
21018 are filled into *info by a langhook. */
21020 static void
21021 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21022 dw_die_ref context_die)
21024 const dw_die_ref scope_die = scope_die_for (type, context_die);
21025 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21026 struct loc_descr_context context = { type, info->base_decl, NULL,
21027 false, false };
21028 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21029 int dim;
21031 add_name_attribute (array_die, type_tag (type));
21032 equate_type_number_to_die (type, array_die);
21034 if (info->ndimensions > 1)
21035 switch (info->ordering)
21037 case array_descr_ordering_row_major:
21038 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21039 break;
21040 case array_descr_ordering_column_major:
21041 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21042 break;
21043 default:
21044 break;
21047 if (dwarf_version >= 3 || !dwarf_strict)
21049 if (info->data_location)
21050 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21051 dw_scalar_form_exprloc, &context);
21052 if (info->associated)
21053 add_scalar_info (array_die, DW_AT_associated, info->associated,
21054 dw_scalar_form_constant
21055 | dw_scalar_form_exprloc
21056 | dw_scalar_form_reference, &context);
21057 if (info->allocated)
21058 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21059 dw_scalar_form_constant
21060 | dw_scalar_form_exprloc
21061 | dw_scalar_form_reference, &context);
21062 if (info->stride)
21064 const enum dwarf_attribute attr
21065 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21066 const int forms
21067 = (info->stride_in_bits)
21068 ? dw_scalar_form_constant
21069 : (dw_scalar_form_constant
21070 | dw_scalar_form_exprloc
21071 | dw_scalar_form_reference);
21073 add_scalar_info (array_die, attr, info->stride, forms, &context);
21076 if (dwarf_version >= 5)
21078 if (info->rank)
21080 add_scalar_info (array_die, DW_AT_rank, info->rank,
21081 dw_scalar_form_constant
21082 | dw_scalar_form_exprloc, &context);
21083 subrange_tag = DW_TAG_generic_subrange;
21084 context.placeholder_arg = true;
21088 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21090 for (dim = 0; dim < info->ndimensions; dim++)
21092 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21094 if (info->dimen[dim].bounds_type)
21095 add_type_attribute (subrange_die,
21096 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21097 false, context_die);
21098 if (info->dimen[dim].lower_bound)
21099 add_bound_info (subrange_die, DW_AT_lower_bound,
21100 info->dimen[dim].lower_bound, &context);
21101 if (info->dimen[dim].upper_bound)
21102 add_bound_info (subrange_die, DW_AT_upper_bound,
21103 info->dimen[dim].upper_bound, &context);
21104 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21105 add_scalar_info (subrange_die, DW_AT_byte_stride,
21106 info->dimen[dim].stride,
21107 dw_scalar_form_constant
21108 | dw_scalar_form_exprloc
21109 | dw_scalar_form_reference,
21110 &context);
21113 gen_type_die (info->element_type, context_die);
21114 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21115 TREE_CODE (type) == ARRAY_TYPE
21116 && TYPE_REVERSE_STORAGE_ORDER (type),
21117 context_die);
21119 if (get_AT (array_die, DW_AT_name))
21120 add_pubtype (type, array_die);
21122 add_alignment_attribute (array_die, type);
21125 #if 0
21126 static void
21127 gen_entry_point_die (tree decl, dw_die_ref context_die)
21129 tree origin = decl_ultimate_origin (decl);
21130 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21132 if (origin != NULL)
21133 add_abstract_origin_attribute (decl_die, origin);
21134 else
21136 add_name_and_src_coords_attributes (decl_die, decl);
21137 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21138 TYPE_UNQUALIFIED, false, context_die);
21141 if (DECL_ABSTRACT_P (decl))
21142 equate_decl_number_to_die (decl, decl_die);
21143 else
21144 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21146 #endif
21148 /* Walk through the list of incomplete types again, trying once more to
21149 emit full debugging info for them. */
21151 static void
21152 retry_incomplete_types (void)
21154 set_early_dwarf s;
21155 int i;
21157 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21158 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21159 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21160 vec_safe_truncate (incomplete_types, 0);
21163 /* Determine what tag to use for a record type. */
21165 static enum dwarf_tag
21166 record_type_tag (tree type)
21168 if (! lang_hooks.types.classify_record)
21169 return DW_TAG_structure_type;
21171 switch (lang_hooks.types.classify_record (type))
21173 case RECORD_IS_STRUCT:
21174 return DW_TAG_structure_type;
21176 case RECORD_IS_CLASS:
21177 return DW_TAG_class_type;
21179 case RECORD_IS_INTERFACE:
21180 if (dwarf_version >= 3 || !dwarf_strict)
21181 return DW_TAG_interface_type;
21182 return DW_TAG_structure_type;
21184 default:
21185 gcc_unreachable ();
21189 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21190 include all of the information about the enumeration values also. Each
21191 enumerated type name/value is listed as a child of the enumerated type
21192 DIE. */
21194 static dw_die_ref
21195 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21197 dw_die_ref type_die = lookup_type_die (type);
21199 if (type_die == NULL)
21201 type_die = new_die (DW_TAG_enumeration_type,
21202 scope_die_for (type, context_die), type);
21203 equate_type_number_to_die (type, type_die);
21204 add_name_attribute (type_die, type_tag (type));
21205 if (dwarf_version >= 4 || !dwarf_strict)
21207 if (ENUM_IS_SCOPED (type))
21208 add_AT_flag (type_die, DW_AT_enum_class, 1);
21209 if (ENUM_IS_OPAQUE (type))
21210 add_AT_flag (type_die, DW_AT_declaration, 1);
21212 if (!dwarf_strict)
21213 add_AT_unsigned (type_die, DW_AT_encoding,
21214 TYPE_UNSIGNED (type)
21215 ? DW_ATE_unsigned
21216 : DW_ATE_signed);
21218 else if (! TYPE_SIZE (type))
21219 return type_die;
21220 else
21221 remove_AT (type_die, DW_AT_declaration);
21223 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21224 given enum type is incomplete, do not generate the DW_AT_byte_size
21225 attribute or the DW_AT_element_list attribute. */
21226 if (TYPE_SIZE (type))
21228 tree link;
21230 TREE_ASM_WRITTEN (type) = 1;
21231 add_byte_size_attribute (type_die, type);
21232 add_alignment_attribute (type_die, type);
21233 if (dwarf_version >= 3 || !dwarf_strict)
21235 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21236 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21237 context_die);
21239 if (TYPE_STUB_DECL (type) != NULL_TREE)
21241 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21242 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21245 /* If the first reference to this type was as the return type of an
21246 inline function, then it may not have a parent. Fix this now. */
21247 if (type_die->die_parent == NULL)
21248 add_child_die (scope_die_for (type, context_die), type_die);
21250 for (link = TYPE_VALUES (type);
21251 link != NULL; link = TREE_CHAIN (link))
21253 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21254 tree value = TREE_VALUE (link);
21256 add_name_attribute (enum_die,
21257 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21259 if (TREE_CODE (value) == CONST_DECL)
21260 value = DECL_INITIAL (value);
21262 if (simple_type_size_in_bits (TREE_TYPE (value))
21263 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21265 /* For constant forms created by add_AT_unsigned DWARF
21266 consumers (GDB, elfutils, etc.) always zero extend
21267 the value. Only when the actual value is negative
21268 do we need to use add_AT_int to generate a constant
21269 form that can represent negative values. */
21270 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21271 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21272 add_AT_unsigned (enum_die, DW_AT_const_value,
21273 (unsigned HOST_WIDE_INT) val);
21274 else
21275 add_AT_int (enum_die, DW_AT_const_value, val);
21277 else
21278 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21279 that here. TODO: This should be re-worked to use correct
21280 signed/unsigned double tags for all cases. */
21281 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21284 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21285 if (TYPE_ARTIFICIAL (type))
21286 add_AT_flag (type_die, DW_AT_artificial, 1);
21288 else
21289 add_AT_flag (type_die, DW_AT_declaration, 1);
21291 add_pubtype (type, type_die);
21293 return type_die;
21296 /* Generate a DIE to represent either a real live formal parameter decl or to
21297 represent just the type of some formal parameter position in some function
21298 type.
21300 Note that this routine is a bit unusual because its argument may be a
21301 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21302 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21303 node. If it's the former then this function is being called to output a
21304 DIE to represent a formal parameter object (or some inlining thereof). If
21305 it's the latter, then this function is only being called to output a
21306 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21307 argument type of some subprogram type.
21308 If EMIT_NAME_P is true, name and source coordinate attributes
21309 are emitted. */
21311 static dw_die_ref
21312 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21313 dw_die_ref context_die)
21315 tree node_or_origin = node ? node : origin;
21316 tree ultimate_origin;
21317 dw_die_ref parm_die = NULL;
21319 if (DECL_P (node_or_origin))
21321 parm_die = lookup_decl_die (node);
21323 /* If the contexts differ, we may not be talking about the same
21324 thing.
21325 ??? When in LTO the DIE parent is the "abstract" copy and the
21326 context_die is the specification "copy". But this whole block
21327 should eventually be no longer needed. */
21328 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21330 if (!DECL_ABSTRACT_P (node))
21332 /* This can happen when creating an inlined instance, in
21333 which case we need to create a new DIE that will get
21334 annotated with DW_AT_abstract_origin. */
21335 parm_die = NULL;
21337 else
21338 gcc_unreachable ();
21341 if (parm_die && parm_die->die_parent == NULL)
21343 /* Check that parm_die already has the right attributes that
21344 we would have added below. If any attributes are
21345 missing, fall through to add them. */
21346 if (! DECL_ABSTRACT_P (node_or_origin)
21347 && !get_AT (parm_die, DW_AT_location)
21348 && !get_AT (parm_die, DW_AT_const_value))
21349 /* We are missing location info, and are about to add it. */
21351 else
21353 add_child_die (context_die, parm_die);
21354 return parm_die;
21359 /* If we have a previously generated DIE, use it, unless this is an
21360 concrete instance (origin != NULL), in which case we need a new
21361 DIE with a corresponding DW_AT_abstract_origin. */
21362 bool reusing_die;
21363 if (parm_die && origin == NULL)
21364 reusing_die = true;
21365 else
21367 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21368 reusing_die = false;
21371 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21373 case tcc_declaration:
21374 ultimate_origin = decl_ultimate_origin (node_or_origin);
21375 if (node || ultimate_origin)
21376 origin = ultimate_origin;
21378 if (reusing_die)
21379 goto add_location;
21381 if (origin != NULL)
21382 add_abstract_origin_attribute (parm_die, origin);
21383 else if (emit_name_p)
21384 add_name_and_src_coords_attributes (parm_die, node);
21385 if (origin == NULL
21386 || (! DECL_ABSTRACT_P (node_or_origin)
21387 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21388 decl_function_context
21389 (node_or_origin))))
21391 tree type = TREE_TYPE (node_or_origin);
21392 if (decl_by_reference_p (node_or_origin))
21393 add_type_attribute (parm_die, TREE_TYPE (type),
21394 TYPE_UNQUALIFIED,
21395 false, context_die);
21396 else
21397 add_type_attribute (parm_die, type,
21398 decl_quals (node_or_origin),
21399 false, context_die);
21401 if (origin == NULL && DECL_ARTIFICIAL (node))
21402 add_AT_flag (parm_die, DW_AT_artificial, 1);
21403 add_location:
21404 if (node && node != origin)
21405 equate_decl_number_to_die (node, parm_die);
21406 if (! DECL_ABSTRACT_P (node_or_origin))
21407 add_location_or_const_value_attribute (parm_die, node_or_origin,
21408 node == NULL);
21410 break;
21412 case tcc_type:
21413 /* We were called with some kind of a ..._TYPE node. */
21414 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21415 context_die);
21416 break;
21418 default:
21419 gcc_unreachable ();
21422 return parm_die;
21425 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21426 children DW_TAG_formal_parameter DIEs representing the arguments of the
21427 parameter pack.
21429 PARM_PACK must be a function parameter pack.
21430 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21431 must point to the subsequent arguments of the function PACK_ARG belongs to.
21432 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21433 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21434 following the last one for which a DIE was generated. */
21436 static dw_die_ref
21437 gen_formal_parameter_pack_die (tree parm_pack,
21438 tree pack_arg,
21439 dw_die_ref subr_die,
21440 tree *next_arg)
21442 tree arg;
21443 dw_die_ref parm_pack_die;
21445 gcc_assert (parm_pack
21446 && lang_hooks.function_parameter_pack_p (parm_pack)
21447 && subr_die);
21449 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21450 add_src_coords_attributes (parm_pack_die, parm_pack);
21452 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21454 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21455 parm_pack))
21456 break;
21457 gen_formal_parameter_die (arg, NULL,
21458 false /* Don't emit name attribute. */,
21459 parm_pack_die);
21461 if (next_arg)
21462 *next_arg = arg;
21463 return parm_pack_die;
21466 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21467 at the end of an (ANSI prototyped) formal parameters list. */
21469 static void
21470 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21472 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21475 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21476 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21477 parameters as specified in some function type specification (except for
21478 those which appear as part of a function *definition*). */
21480 static void
21481 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21483 tree link;
21484 tree formal_type = NULL;
21485 tree first_parm_type;
21486 tree arg;
21488 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21490 arg = DECL_ARGUMENTS (function_or_method_type);
21491 function_or_method_type = TREE_TYPE (function_or_method_type);
21493 else
21494 arg = NULL_TREE;
21496 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21498 /* Make our first pass over the list of formal parameter types and output a
21499 DW_TAG_formal_parameter DIE for each one. */
21500 for (link = first_parm_type; link; )
21502 dw_die_ref parm_die;
21504 formal_type = TREE_VALUE (link);
21505 if (formal_type == void_type_node)
21506 break;
21508 /* Output a (nameless) DIE to represent the formal parameter itself. */
21509 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21511 parm_die = gen_formal_parameter_die (formal_type, NULL,
21512 true /* Emit name attribute. */,
21513 context_die);
21514 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21515 && link == first_parm_type)
21517 add_AT_flag (parm_die, DW_AT_artificial, 1);
21518 if (dwarf_version >= 3 || !dwarf_strict)
21519 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21521 else if (arg && DECL_ARTIFICIAL (arg))
21522 add_AT_flag (parm_die, DW_AT_artificial, 1);
21525 link = TREE_CHAIN (link);
21526 if (arg)
21527 arg = DECL_CHAIN (arg);
21530 /* If this function type has an ellipsis, add a
21531 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21532 if (formal_type != void_type_node)
21533 gen_unspecified_parameters_die (function_or_method_type, context_die);
21535 /* Make our second (and final) pass over the list of formal parameter types
21536 and output DIEs to represent those types (as necessary). */
21537 for (link = TYPE_ARG_TYPES (function_or_method_type);
21538 link && TREE_VALUE (link);
21539 link = TREE_CHAIN (link))
21540 gen_type_die (TREE_VALUE (link), context_die);
21543 /* We want to generate the DIE for TYPE so that we can generate the
21544 die for MEMBER, which has been defined; we will need to refer back
21545 to the member declaration nested within TYPE. If we're trying to
21546 generate minimal debug info for TYPE, processing TYPE won't do the
21547 trick; we need to attach the member declaration by hand. */
21549 static void
21550 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21552 gen_type_die (type, context_die);
21554 /* If we're trying to avoid duplicate debug info, we may not have
21555 emitted the member decl for this function. Emit it now. */
21556 if (TYPE_STUB_DECL (type)
21557 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21558 && ! lookup_decl_die (member))
21560 dw_die_ref type_die;
21561 gcc_assert (!decl_ultimate_origin (member));
21563 push_decl_scope (type);
21564 type_die = lookup_type_die_strip_naming_typedef (type);
21565 if (TREE_CODE (member) == FUNCTION_DECL)
21566 gen_subprogram_die (member, type_die);
21567 else if (TREE_CODE (member) == FIELD_DECL)
21569 /* Ignore the nameless fields that are used to skip bits but handle
21570 C++ anonymous unions and structs. */
21571 if (DECL_NAME (member) != NULL_TREE
21572 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21573 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21575 struct vlr_context vlr_ctx = {
21576 DECL_CONTEXT (member), /* struct_type */
21577 NULL_TREE /* variant_part_offset */
21579 gen_type_die (member_declared_type (member), type_die);
21580 gen_field_die (member, &vlr_ctx, type_die);
21583 else
21584 gen_variable_die (member, NULL_TREE, type_die);
21586 pop_decl_scope ();
21590 /* Forward declare these functions, because they are mutually recursive
21591 with their set_block_* pairing functions. */
21592 static void set_decl_origin_self (tree);
21594 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21595 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21596 that it points to the node itself, thus indicating that the node is its
21597 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21598 the given node is NULL, recursively descend the decl/block tree which
21599 it is the root of, and for each other ..._DECL or BLOCK node contained
21600 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21601 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21602 values to point to themselves. */
21604 static void
21605 set_block_origin_self (tree stmt)
21607 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21609 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21612 tree local_decl;
21614 for (local_decl = BLOCK_VARS (stmt);
21615 local_decl != NULL_TREE;
21616 local_decl = DECL_CHAIN (local_decl))
21617 /* Do not recurse on nested functions since the inlining status
21618 of parent and child can be different as per the DWARF spec. */
21619 if (TREE_CODE (local_decl) != FUNCTION_DECL
21620 && !DECL_EXTERNAL (local_decl))
21621 set_decl_origin_self (local_decl);
21625 tree subblock;
21627 for (subblock = BLOCK_SUBBLOCKS (stmt);
21628 subblock != NULL_TREE;
21629 subblock = BLOCK_CHAIN (subblock))
21630 set_block_origin_self (subblock); /* Recurse. */
21635 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21636 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21637 node to so that it points to the node itself, thus indicating that the
21638 node represents its own (abstract) origin. Additionally, if the
21639 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21640 the decl/block tree of which the given node is the root of, and for
21641 each other ..._DECL or BLOCK node contained therein whose
21642 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21643 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21644 point to themselves. */
21646 static void
21647 set_decl_origin_self (tree decl)
21649 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21651 DECL_ABSTRACT_ORIGIN (decl) = decl;
21652 if (TREE_CODE (decl) == FUNCTION_DECL)
21654 tree arg;
21656 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21657 DECL_ABSTRACT_ORIGIN (arg) = arg;
21658 if (DECL_INITIAL (decl) != NULL_TREE
21659 && DECL_INITIAL (decl) != error_mark_node)
21660 set_block_origin_self (DECL_INITIAL (decl));
21665 /* Mark the early DIE for DECL as the abstract instance. */
21667 static void
21668 dwarf2out_abstract_function (tree decl)
21670 dw_die_ref old_die;
21672 /* Make sure we have the actual abstract inline, not a clone. */
21673 decl = DECL_ORIGIN (decl);
21675 if (DECL_IGNORED_P (decl))
21676 return;
21678 old_die = lookup_decl_die (decl);
21679 /* With early debug we always have an old DIE unless we are in LTO
21680 and the user did not compile but only link with debug. */
21681 if (in_lto_p && ! old_die)
21682 return;
21683 gcc_assert (old_die != NULL);
21684 if (get_AT (old_die, DW_AT_inline)
21685 || get_AT (old_die, DW_AT_abstract_origin))
21686 /* We've already generated the abstract instance. */
21687 return;
21689 /* Go ahead and put DW_AT_inline on the DIE. */
21690 if (DECL_DECLARED_INLINE_P (decl))
21692 if (cgraph_function_possibly_inlined_p (decl))
21693 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21694 else
21695 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21697 else
21699 if (cgraph_function_possibly_inlined_p (decl))
21700 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21701 else
21702 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21705 if (DECL_DECLARED_INLINE_P (decl)
21706 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21707 add_AT_flag (old_die, DW_AT_artificial, 1);
21709 set_decl_origin_self (decl);
21712 /* Helper function of premark_used_types() which gets called through
21713 htab_traverse.
21715 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21716 marked as unused by prune_unused_types. */
21718 bool
21719 premark_used_types_helper (tree const &type, void *)
21721 dw_die_ref die;
21723 die = lookup_type_die (type);
21724 if (die != NULL)
21725 die->die_perennial_p = 1;
21726 return true;
21729 /* Helper function of premark_types_used_by_global_vars which gets called
21730 through htab_traverse.
21732 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21733 marked as unused by prune_unused_types. The DIE of the type is marked
21734 only if the global variable using the type will actually be emitted. */
21737 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21738 void *)
21740 struct types_used_by_vars_entry *entry;
21741 dw_die_ref die;
21743 entry = (struct types_used_by_vars_entry *) *slot;
21744 gcc_assert (entry->type != NULL
21745 && entry->var_decl != NULL);
21746 die = lookup_type_die (entry->type);
21747 if (die)
21749 /* Ask cgraph if the global variable really is to be emitted.
21750 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21751 varpool_node *node = varpool_node::get (entry->var_decl);
21752 if (node && node->definition)
21754 die->die_perennial_p = 1;
21755 /* Keep the parent DIEs as well. */
21756 while ((die = die->die_parent) && die->die_perennial_p == 0)
21757 die->die_perennial_p = 1;
21760 return 1;
21763 /* Mark all members of used_types_hash as perennial. */
21765 static void
21766 premark_used_types (struct function *fun)
21768 if (fun && fun->used_types_hash)
21769 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21772 /* Mark all members of types_used_by_vars_entry as perennial. */
21774 static void
21775 premark_types_used_by_global_vars (void)
21777 if (types_used_by_vars_hash)
21778 types_used_by_vars_hash
21779 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21782 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21783 for CA_LOC call arg loc node. */
21785 static dw_die_ref
21786 gen_call_site_die (tree decl, dw_die_ref subr_die,
21787 struct call_arg_loc_node *ca_loc)
21789 dw_die_ref stmt_die = NULL, die;
21790 tree block = ca_loc->block;
21792 while (block
21793 && block != DECL_INITIAL (decl)
21794 && TREE_CODE (block) == BLOCK)
21796 stmt_die = BLOCK_DIE (block);
21797 if (stmt_die)
21798 break;
21799 block = BLOCK_SUPERCONTEXT (block);
21801 if (stmt_die == NULL)
21802 stmt_die = subr_die;
21803 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21804 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21805 if (ca_loc->tail_call_p)
21806 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21807 if (ca_loc->symbol_ref)
21809 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21810 if (tdie)
21811 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21812 else
21813 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21814 false);
21816 return die;
21819 /* Generate a DIE to represent a declared function (either file-scope or
21820 block-local). */
21822 static void
21823 gen_subprogram_die (tree decl, dw_die_ref context_die)
21825 tree origin = decl_ultimate_origin (decl);
21826 dw_die_ref subr_die;
21827 dw_die_ref old_die = lookup_decl_die (decl);
21829 /* This function gets called multiple times for different stages of
21830 the debug process. For example, for func() in this code:
21832 namespace S
21834 void func() { ... }
21837 ...we get called 4 times. Twice in early debug and twice in
21838 late debug:
21840 Early debug
21841 -----------
21843 1. Once while generating func() within the namespace. This is
21844 the declaration. The declaration bit below is set, as the
21845 context is the namespace.
21847 A new DIE will be generated with DW_AT_declaration set.
21849 2. Once for func() itself. This is the specification. The
21850 declaration bit below is clear as the context is the CU.
21852 We will use the cached DIE from (1) to create a new DIE with
21853 DW_AT_specification pointing to the declaration in (1).
21855 Late debug via rest_of_handle_final()
21856 -------------------------------------
21858 3. Once generating func() within the namespace. This is also the
21859 declaration, as in (1), but this time we will early exit below
21860 as we have a cached DIE and a declaration needs no additional
21861 annotations (no locations), as the source declaration line
21862 info is enough.
21864 4. Once for func() itself. As in (2), this is the specification,
21865 but this time we will re-use the cached DIE, and just annotate
21866 it with the location information that should now be available.
21868 For something without namespaces, but with abstract instances, we
21869 are also called a multiple times:
21871 class Base
21873 public:
21874 Base (); // constructor declaration (1)
21877 Base::Base () { } // constructor specification (2)
21879 Early debug
21880 -----------
21882 1. Once for the Base() constructor by virtue of it being a
21883 member of the Base class. This is done via
21884 rest_of_type_compilation.
21886 This is a declaration, so a new DIE will be created with
21887 DW_AT_declaration.
21889 2. Once for the Base() constructor definition, but this time
21890 while generating the abstract instance of the base
21891 constructor (__base_ctor) which is being generated via early
21892 debug of reachable functions.
21894 Even though we have a cached version of the declaration (1),
21895 we will create a DW_AT_specification of the declaration DIE
21896 in (1).
21898 3. Once for the __base_ctor itself, but this time, we generate
21899 an DW_AT_abstract_origin version of the DW_AT_specification in
21900 (2).
21902 Late debug via rest_of_handle_final
21903 -----------------------------------
21905 4. One final time for the __base_ctor (which will have a cached
21906 DIE with DW_AT_abstract_origin created in (3). This time,
21907 we will just annotate the location information now
21908 available.
21910 int declaration = (current_function_decl != decl
21911 || class_or_namespace_scope_p (context_die));
21913 /* Now that the C++ front end lazily declares artificial member fns, we
21914 might need to retrofit the declaration into its class. */
21915 if (!declaration && !origin && !old_die
21916 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21917 && !class_or_namespace_scope_p (context_die)
21918 && debug_info_level > DINFO_LEVEL_TERSE)
21919 old_die = force_decl_die (decl);
21921 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21922 if (origin != NULL)
21924 gcc_assert (!declaration || local_scope_p (context_die));
21926 /* Fixup die_parent for the abstract instance of a nested
21927 inline function. */
21928 if (old_die && old_die->die_parent == NULL)
21929 add_child_die (context_die, old_die);
21931 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21933 /* If we have a DW_AT_abstract_origin we have a working
21934 cached version. */
21935 subr_die = old_die;
21937 else
21939 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21940 add_abstract_origin_attribute (subr_die, origin);
21941 /* This is where the actual code for a cloned function is.
21942 Let's emit linkage name attribute for it. This helps
21943 debuggers to e.g, set breakpoints into
21944 constructors/destructors when the user asks "break
21945 K::K". */
21946 add_linkage_name (subr_die, decl);
21949 /* A cached copy, possibly from early dwarf generation. Reuse as
21950 much as possible. */
21951 else if (old_die)
21953 /* A declaration that has been previously dumped needs no
21954 additional information. */
21955 if (declaration)
21956 return;
21958 if (!get_AT_flag (old_die, DW_AT_declaration)
21959 /* We can have a normal definition following an inline one in the
21960 case of redefinition of GNU C extern inlines.
21961 It seems reasonable to use AT_specification in this case. */
21962 && !get_AT (old_die, DW_AT_inline))
21964 /* Detect and ignore this case, where we are trying to output
21965 something we have already output. */
21966 if (get_AT (old_die, DW_AT_low_pc)
21967 || get_AT (old_die, DW_AT_ranges))
21968 return;
21970 /* If we have no location information, this must be a
21971 partially generated DIE from early dwarf generation.
21972 Fall through and generate it. */
21975 /* If the definition comes from the same place as the declaration,
21976 maybe use the old DIE. We always want the DIE for this function
21977 that has the *_pc attributes to be under comp_unit_die so the
21978 debugger can find it. We also need to do this for abstract
21979 instances of inlines, since the spec requires the out-of-line copy
21980 to have the same parent. For local class methods, this doesn't
21981 apply; we just use the old DIE. */
21982 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21983 struct dwarf_file_data * file_index = lookup_filename (s.file);
21984 if ((is_cu_die (old_die->die_parent)
21985 /* This condition fixes the inconsistency/ICE with the
21986 following Fortran test (or some derivative thereof) while
21987 building libgfortran:
21989 module some_m
21990 contains
21991 logical function funky (FLAG)
21992 funky = .true.
21993 end function
21994 end module
21996 || (old_die->die_parent
21997 && old_die->die_parent->die_tag == DW_TAG_module)
21998 || context_die == NULL)
21999 && (DECL_ARTIFICIAL (decl)
22000 /* The location attributes may be in the abstract origin
22001 which in the case of LTO might be not available to
22002 look at. */
22003 || get_AT (old_die, DW_AT_abstract_origin)
22004 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22005 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22006 == (unsigned) s.line)
22007 && (!debug_column_info
22008 || s.column == 0
22009 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22010 == (unsigned) s.column)))))
22012 subr_die = old_die;
22014 /* Clear out the declaration attribute, but leave the
22015 parameters so they can be augmented with location
22016 information later. Unless this was a declaration, in
22017 which case, wipe out the nameless parameters and recreate
22018 them further down. */
22019 if (remove_AT (subr_die, DW_AT_declaration))
22022 remove_AT (subr_die, DW_AT_object_pointer);
22023 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22026 /* Make a specification pointing to the previously built
22027 declaration. */
22028 else
22030 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22031 add_AT_specification (subr_die, old_die);
22032 add_pubname (decl, subr_die);
22033 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22034 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22035 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22036 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22037 if (debug_column_info
22038 && s.column
22039 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22040 != (unsigned) s.column))
22041 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22043 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22044 emit the real type on the definition die. */
22045 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22047 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22048 if (die == auto_die || die == decltype_auto_die)
22049 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22050 TYPE_UNQUALIFIED, false, context_die);
22053 /* When we process the method declaration, we haven't seen
22054 the out-of-class defaulted definition yet, so we have to
22055 recheck now. */
22056 if ((dwarf_version >= 5 || ! dwarf_strict)
22057 && !get_AT (subr_die, DW_AT_defaulted))
22059 int defaulted
22060 = lang_hooks.decls.decl_dwarf_attribute (decl,
22061 DW_AT_defaulted);
22062 if (defaulted != -1)
22064 /* Other values must have been handled before. */
22065 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22066 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22071 /* Create a fresh DIE for anything else. */
22072 else
22074 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22076 if (TREE_PUBLIC (decl))
22077 add_AT_flag (subr_die, DW_AT_external, 1);
22079 add_name_and_src_coords_attributes (subr_die, decl);
22080 add_pubname (decl, subr_die);
22081 if (debug_info_level > DINFO_LEVEL_TERSE)
22083 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22084 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22085 TYPE_UNQUALIFIED, false, context_die);
22088 add_pure_or_virtual_attribute (subr_die, decl);
22089 if (DECL_ARTIFICIAL (decl))
22090 add_AT_flag (subr_die, DW_AT_artificial, 1);
22092 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22093 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22095 add_alignment_attribute (subr_die, decl);
22097 add_accessibility_attribute (subr_die, decl);
22100 /* Unless we have an existing non-declaration DIE, equate the new
22101 DIE. */
22102 if (!old_die || is_declaration_die (old_die))
22103 equate_decl_number_to_die (decl, subr_die);
22105 if (declaration)
22107 if (!old_die || !get_AT (old_die, DW_AT_inline))
22109 add_AT_flag (subr_die, DW_AT_declaration, 1);
22111 /* If this is an explicit function declaration then generate
22112 a DW_AT_explicit attribute. */
22113 if ((dwarf_version >= 3 || !dwarf_strict)
22114 && lang_hooks.decls.decl_dwarf_attribute (decl,
22115 DW_AT_explicit) == 1)
22116 add_AT_flag (subr_die, DW_AT_explicit, 1);
22118 /* If this is a C++11 deleted special function member then generate
22119 a DW_AT_deleted attribute. */
22120 if ((dwarf_version >= 5 || !dwarf_strict)
22121 && lang_hooks.decls.decl_dwarf_attribute (decl,
22122 DW_AT_deleted) == 1)
22123 add_AT_flag (subr_die, DW_AT_deleted, 1);
22125 /* If this is a C++11 defaulted special function member then
22126 generate a DW_AT_defaulted attribute. */
22127 if (dwarf_version >= 5 || !dwarf_strict)
22129 int defaulted
22130 = lang_hooks.decls.decl_dwarf_attribute (decl,
22131 DW_AT_defaulted);
22132 if (defaulted != -1)
22133 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22136 /* If this is a C++11 non-static member function with & ref-qualifier
22137 then generate a DW_AT_reference attribute. */
22138 if ((dwarf_version >= 5 || !dwarf_strict)
22139 && lang_hooks.decls.decl_dwarf_attribute (decl,
22140 DW_AT_reference) == 1)
22141 add_AT_flag (subr_die, DW_AT_reference, 1);
22143 /* If this is a C++11 non-static member function with &&
22144 ref-qualifier then generate a DW_AT_reference attribute. */
22145 if ((dwarf_version >= 5 || !dwarf_strict)
22146 && lang_hooks.decls.decl_dwarf_attribute (decl,
22147 DW_AT_rvalue_reference)
22148 == 1)
22149 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22152 /* For non DECL_EXTERNALs, if range information is available, fill
22153 the DIE with it. */
22154 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22156 HOST_WIDE_INT cfa_fb_offset;
22158 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22160 if (!crtl->has_bb_partition)
22162 dw_fde_ref fde = fun->fde;
22163 if (fde->dw_fde_begin)
22165 /* We have already generated the labels. */
22166 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22167 fde->dw_fde_end, false);
22169 else
22171 /* Create start/end labels and add the range. */
22172 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22173 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22174 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22175 current_function_funcdef_no);
22176 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22177 current_function_funcdef_no);
22178 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22179 false);
22182 #if VMS_DEBUGGING_INFO
22183 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22184 Section 2.3 Prologue and Epilogue Attributes:
22185 When a breakpoint is set on entry to a function, it is generally
22186 desirable for execution to be suspended, not on the very first
22187 instruction of the function, but rather at a point after the
22188 function's frame has been set up, after any language defined local
22189 declaration processing has been completed, and before execution of
22190 the first statement of the function begins. Debuggers generally
22191 cannot properly determine where this point is. Similarly for a
22192 breakpoint set on exit from a function. The prologue and epilogue
22193 attributes allow a compiler to communicate the location(s) to use. */
22196 if (fde->dw_fde_vms_end_prologue)
22197 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22198 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22200 if (fde->dw_fde_vms_begin_epilogue)
22201 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22202 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22204 #endif
22207 else
22209 /* Generate pubnames entries for the split function code ranges. */
22210 dw_fde_ref fde = fun->fde;
22212 if (fde->dw_fde_second_begin)
22214 if (dwarf_version >= 3 || !dwarf_strict)
22216 /* We should use ranges for non-contiguous code section
22217 addresses. Use the actual code range for the initial
22218 section, since the HOT/COLD labels might precede an
22219 alignment offset. */
22220 bool range_list_added = false;
22221 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22222 fde->dw_fde_end, &range_list_added,
22223 false);
22224 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22225 fde->dw_fde_second_end,
22226 &range_list_added, false);
22227 if (range_list_added)
22228 add_ranges (NULL);
22230 else
22232 /* There is no real support in DW2 for this .. so we make
22233 a work-around. First, emit the pub name for the segment
22234 containing the function label. Then make and emit a
22235 simplified subprogram DIE for the second segment with the
22236 name pre-fixed by __hot/cold_sect_of_. We use the same
22237 linkage name for the second die so that gdb will find both
22238 sections when given "b foo". */
22239 const char *name = NULL;
22240 tree decl_name = DECL_NAME (decl);
22241 dw_die_ref seg_die;
22243 /* Do the 'primary' section. */
22244 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22245 fde->dw_fde_end, false);
22247 /* Build a minimal DIE for the secondary section. */
22248 seg_die = new_die (DW_TAG_subprogram,
22249 subr_die->die_parent, decl);
22251 if (TREE_PUBLIC (decl))
22252 add_AT_flag (seg_die, DW_AT_external, 1);
22254 if (decl_name != NULL
22255 && IDENTIFIER_POINTER (decl_name) != NULL)
22257 name = dwarf2_name (decl, 1);
22258 if (! DECL_ARTIFICIAL (decl))
22259 add_src_coords_attributes (seg_die, decl);
22261 add_linkage_name (seg_die, decl);
22263 gcc_assert (name != NULL);
22264 add_pure_or_virtual_attribute (seg_die, decl);
22265 if (DECL_ARTIFICIAL (decl))
22266 add_AT_flag (seg_die, DW_AT_artificial, 1);
22268 name = concat ("__second_sect_of_", name, NULL);
22269 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22270 fde->dw_fde_second_end, false);
22271 add_name_attribute (seg_die, name);
22272 if (want_pubnames ())
22273 add_pubname_string (name, seg_die);
22276 else
22277 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22278 false);
22281 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22283 /* We define the "frame base" as the function's CFA. This is more
22284 convenient for several reasons: (1) It's stable across the prologue
22285 and epilogue, which makes it better than just a frame pointer,
22286 (2) With dwarf3, there exists a one-byte encoding that allows us
22287 to reference the .debug_frame data by proxy, but failing that,
22288 (3) We can at least reuse the code inspection and interpretation
22289 code that determines the CFA position at various points in the
22290 function. */
22291 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22293 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22294 add_AT_loc (subr_die, DW_AT_frame_base, op);
22296 else
22298 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22299 if (list->dw_loc_next)
22300 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22301 else
22302 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22305 /* Compute a displacement from the "steady-state frame pointer" to
22306 the CFA. The former is what all stack slots and argument slots
22307 will reference in the rtl; the latter is what we've told the
22308 debugger about. We'll need to adjust all frame_base references
22309 by this displacement. */
22310 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22312 if (fun->static_chain_decl)
22314 /* DWARF requires here a location expression that computes the
22315 address of the enclosing subprogram's frame base. The machinery
22316 in tree-nested.c is supposed to store this specific address in the
22317 last field of the FRAME record. */
22318 const tree frame_type
22319 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22320 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22322 tree fb_expr
22323 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22324 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22325 fb_expr, fb_decl, NULL_TREE);
22327 add_AT_location_description (subr_die, DW_AT_static_link,
22328 loc_list_from_tree (fb_expr, 0, NULL));
22331 resolve_variable_values ();
22334 /* Generate child dies for template paramaters. */
22335 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22336 gen_generic_params_dies (decl);
22338 /* Now output descriptions of the arguments for this function. This gets
22339 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22340 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22341 `...' at the end of the formal parameter list. In order to find out if
22342 there was a trailing ellipsis or not, we must instead look at the type
22343 associated with the FUNCTION_DECL. This will be a node of type
22344 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22345 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22346 an ellipsis at the end. */
22348 /* In the case where we are describing a mere function declaration, all we
22349 need to do here (and all we *can* do here) is to describe the *types* of
22350 its formal parameters. */
22351 if (debug_info_level <= DINFO_LEVEL_TERSE)
22353 else if (declaration)
22354 gen_formal_types_die (decl, subr_die);
22355 else
22357 /* Generate DIEs to represent all known formal parameters. */
22358 tree parm = DECL_ARGUMENTS (decl);
22359 tree generic_decl = early_dwarf
22360 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22361 tree generic_decl_parm = generic_decl
22362 ? DECL_ARGUMENTS (generic_decl)
22363 : NULL;
22365 /* Now we want to walk the list of parameters of the function and
22366 emit their relevant DIEs.
22368 We consider the case of DECL being an instance of a generic function
22369 as well as it being a normal function.
22371 If DECL is an instance of a generic function we walk the
22372 parameters of the generic function declaration _and_ the parameters of
22373 DECL itself. This is useful because we want to emit specific DIEs for
22374 function parameter packs and those are declared as part of the
22375 generic function declaration. In that particular case,
22376 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22377 That DIE has children DIEs representing the set of arguments
22378 of the pack. Note that the set of pack arguments can be empty.
22379 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22380 children DIE.
22382 Otherwise, we just consider the parameters of DECL. */
22383 while (generic_decl_parm || parm)
22385 if (generic_decl_parm
22386 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22387 gen_formal_parameter_pack_die (generic_decl_parm,
22388 parm, subr_die,
22389 &parm);
22390 else if (parm && !POINTER_BOUNDS_P (parm))
22392 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22394 if (early_dwarf
22395 && parm == DECL_ARGUMENTS (decl)
22396 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22397 && parm_die
22398 && (dwarf_version >= 3 || !dwarf_strict))
22399 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22401 parm = DECL_CHAIN (parm);
22403 else if (parm)
22404 parm = DECL_CHAIN (parm);
22406 if (generic_decl_parm)
22407 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22410 /* Decide whether we need an unspecified_parameters DIE at the end.
22411 There are 2 more cases to do this for: 1) the ansi ... declaration -
22412 this is detectable when the end of the arg list is not a
22413 void_type_node 2) an unprototyped function declaration (not a
22414 definition). This just means that we have no info about the
22415 parameters at all. */
22416 if (early_dwarf)
22418 if (prototype_p (TREE_TYPE (decl)))
22420 /* This is the prototyped case, check for.... */
22421 if (stdarg_p (TREE_TYPE (decl)))
22422 gen_unspecified_parameters_die (decl, subr_die);
22424 else if (DECL_INITIAL (decl) == NULL_TREE)
22425 gen_unspecified_parameters_die (decl, subr_die);
22429 if (subr_die != old_die)
22430 /* Add the calling convention attribute if requested. */
22431 add_calling_convention_attribute (subr_die, decl);
22433 /* Output Dwarf info for all of the stuff within the body of the function
22434 (if it has one - it may be just a declaration).
22436 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22437 a function. This BLOCK actually represents the outermost binding contour
22438 for the function, i.e. the contour in which the function's formal
22439 parameters and labels get declared. Curiously, it appears that the front
22440 end doesn't actually put the PARM_DECL nodes for the current function onto
22441 the BLOCK_VARS list for this outer scope, but are strung off of the
22442 DECL_ARGUMENTS list for the function instead.
22444 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22445 the LABEL_DECL nodes for the function however, and we output DWARF info
22446 for those in decls_for_scope. Just within the `outer_scope' there will be
22447 a BLOCK node representing the function's outermost pair of curly braces,
22448 and any blocks used for the base and member initializers of a C++
22449 constructor function. */
22450 tree outer_scope = DECL_INITIAL (decl);
22451 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22453 int call_site_note_count = 0;
22454 int tail_call_site_note_count = 0;
22456 /* Emit a DW_TAG_variable DIE for a named return value. */
22457 if (DECL_NAME (DECL_RESULT (decl)))
22458 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22460 /* The first time through decls_for_scope we will generate the
22461 DIEs for the locals. The second time, we fill in the
22462 location info. */
22463 decls_for_scope (outer_scope, subr_die);
22465 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22467 struct call_arg_loc_node *ca_loc;
22468 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22470 dw_die_ref die = NULL;
22471 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22472 rtx arg, next_arg;
22474 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22475 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22476 : NULL_RTX);
22477 arg; arg = next_arg)
22479 dw_loc_descr_ref reg, val;
22480 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22481 dw_die_ref cdie, tdie = NULL;
22483 next_arg = XEXP (arg, 1);
22484 if (REG_P (XEXP (XEXP (arg, 0), 0))
22485 && next_arg
22486 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22487 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22488 && REGNO (XEXP (XEXP (arg, 0), 0))
22489 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22490 next_arg = XEXP (next_arg, 1);
22491 if (mode == VOIDmode)
22493 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22494 if (mode == VOIDmode)
22495 mode = GET_MODE (XEXP (arg, 0));
22497 if (mode == VOIDmode || mode == BLKmode)
22498 continue;
22499 /* Get dynamic information about call target only if we
22500 have no static information: we cannot generate both
22501 DW_AT_call_origin and DW_AT_call_target
22502 attributes. */
22503 if (ca_loc->symbol_ref == NULL_RTX)
22505 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22507 tloc = XEXP (XEXP (arg, 0), 1);
22508 continue;
22510 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22511 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22513 tlocc = XEXP (XEXP (arg, 0), 1);
22514 continue;
22517 reg = NULL;
22518 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22519 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22520 VAR_INIT_STATUS_INITIALIZED);
22521 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22523 rtx mem = XEXP (XEXP (arg, 0), 0);
22524 reg = mem_loc_descriptor (XEXP (mem, 0),
22525 get_address_mode (mem),
22526 GET_MODE (mem),
22527 VAR_INIT_STATUS_INITIALIZED);
22529 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22530 == DEBUG_PARAMETER_REF)
22532 tree tdecl
22533 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22534 tdie = lookup_decl_die (tdecl);
22535 if (tdie == NULL)
22536 continue;
22538 else
22539 continue;
22540 if (reg == NULL
22541 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22542 != DEBUG_PARAMETER_REF)
22543 continue;
22544 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22545 VOIDmode,
22546 VAR_INIT_STATUS_INITIALIZED);
22547 if (val == NULL)
22548 continue;
22549 if (die == NULL)
22550 die = gen_call_site_die (decl, subr_die, ca_loc);
22551 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22552 NULL_TREE);
22553 if (reg != NULL)
22554 add_AT_loc (cdie, DW_AT_location, reg);
22555 else if (tdie != NULL)
22556 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22557 tdie);
22558 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22559 if (next_arg != XEXP (arg, 1))
22561 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22562 if (mode == VOIDmode)
22563 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22564 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22565 0), 1),
22566 mode, VOIDmode,
22567 VAR_INIT_STATUS_INITIALIZED);
22568 if (val != NULL)
22569 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22570 val);
22573 if (die == NULL
22574 && (ca_loc->symbol_ref || tloc))
22575 die = gen_call_site_die (decl, subr_die, ca_loc);
22576 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22578 dw_loc_descr_ref tval = NULL;
22580 if (tloc != NULL_RTX)
22581 tval = mem_loc_descriptor (tloc,
22582 GET_MODE (tloc) == VOIDmode
22583 ? Pmode : GET_MODE (tloc),
22584 VOIDmode,
22585 VAR_INIT_STATUS_INITIALIZED);
22586 if (tval)
22587 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22588 else if (tlocc != NULL_RTX)
22590 tval = mem_loc_descriptor (tlocc,
22591 GET_MODE (tlocc) == VOIDmode
22592 ? Pmode : GET_MODE (tlocc),
22593 VOIDmode,
22594 VAR_INIT_STATUS_INITIALIZED);
22595 if (tval)
22596 add_AT_loc (die,
22597 dwarf_AT (DW_AT_call_target_clobbered),
22598 tval);
22601 if (die != NULL)
22603 call_site_note_count++;
22604 if (ca_loc->tail_call_p)
22605 tail_call_site_note_count++;
22609 call_arg_locations = NULL;
22610 call_arg_loc_last = NULL;
22611 if (tail_call_site_count >= 0
22612 && tail_call_site_count == tail_call_site_note_count
22613 && (!dwarf_strict || dwarf_version >= 5))
22615 if (call_site_count >= 0
22616 && call_site_count == call_site_note_count)
22617 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22618 else
22619 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22621 call_site_count = -1;
22622 tail_call_site_count = -1;
22625 /* Mark used types after we have created DIEs for the functions scopes. */
22626 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22629 /* Returns a hash value for X (which really is a die_struct). */
22631 hashval_t
22632 block_die_hasher::hash (die_struct *d)
22634 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22637 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22638 as decl_id and die_parent of die_struct Y. */
22640 bool
22641 block_die_hasher::equal (die_struct *x, die_struct *y)
22643 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22646 /* Return TRUE if DECL, which may have been previously generated as
22647 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22648 true if decl (or its origin) is either an extern declaration or a
22649 class/namespace scoped declaration.
22651 The declare_in_namespace support causes us to get two DIEs for one
22652 variable, both of which are declarations. We want to avoid
22653 considering one to be a specification, so we must test for
22654 DECLARATION and DW_AT_declaration. */
22655 static inline bool
22656 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22658 return (old_die && TREE_STATIC (decl) && !declaration
22659 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22662 /* Return true if DECL is a local static. */
22664 static inline bool
22665 local_function_static (tree decl)
22667 gcc_assert (VAR_P (decl));
22668 return TREE_STATIC (decl)
22669 && DECL_CONTEXT (decl)
22670 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22673 /* Generate a DIE to represent a declared data object.
22674 Either DECL or ORIGIN must be non-null. */
22676 static void
22677 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22679 HOST_WIDE_INT off = 0;
22680 tree com_decl;
22681 tree decl_or_origin = decl ? decl : origin;
22682 tree ultimate_origin;
22683 dw_die_ref var_die;
22684 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22685 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22686 || class_or_namespace_scope_p (context_die));
22687 bool specialization_p = false;
22688 bool no_linkage_name = false;
22690 /* While C++ inline static data members have definitions inside of the
22691 class, force the first DIE to be a declaration, then let gen_member_die
22692 reparent it to the class context and call gen_variable_die again
22693 to create the outside of the class DIE for the definition. */
22694 if (!declaration
22695 && old_die == NULL
22696 && decl
22697 && DECL_CONTEXT (decl)
22698 && TYPE_P (DECL_CONTEXT (decl))
22699 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22701 declaration = true;
22702 if (dwarf_version < 5)
22703 no_linkage_name = true;
22706 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22707 if (decl || ultimate_origin)
22708 origin = ultimate_origin;
22709 com_decl = fortran_common (decl_or_origin, &off);
22711 /* Symbol in common gets emitted as a child of the common block, in the form
22712 of a data member. */
22713 if (com_decl)
22715 dw_die_ref com_die;
22716 dw_loc_list_ref loc = NULL;
22717 die_node com_die_arg;
22719 var_die = lookup_decl_die (decl_or_origin);
22720 if (var_die)
22722 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22724 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22725 if (loc)
22727 if (off)
22729 /* Optimize the common case. */
22730 if (single_element_loc_list_p (loc)
22731 && loc->expr->dw_loc_opc == DW_OP_addr
22732 && loc->expr->dw_loc_next == NULL
22733 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22734 == SYMBOL_REF)
22736 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22737 loc->expr->dw_loc_oprnd1.v.val_addr
22738 = plus_constant (GET_MODE (x), x , off);
22740 else
22741 loc_list_plus_const (loc, off);
22743 add_AT_location_description (var_die, DW_AT_location, loc);
22744 remove_AT (var_die, DW_AT_declaration);
22747 return;
22750 if (common_block_die_table == NULL)
22751 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22753 com_die_arg.decl_id = DECL_UID (com_decl);
22754 com_die_arg.die_parent = context_die;
22755 com_die = common_block_die_table->find (&com_die_arg);
22756 if (! early_dwarf)
22757 loc = loc_list_from_tree (com_decl, 2, NULL);
22758 if (com_die == NULL)
22760 const char *cnam
22761 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22762 die_node **slot;
22764 com_die = new_die (DW_TAG_common_block, context_die, decl);
22765 add_name_and_src_coords_attributes (com_die, com_decl);
22766 if (loc)
22768 add_AT_location_description (com_die, DW_AT_location, loc);
22769 /* Avoid sharing the same loc descriptor between
22770 DW_TAG_common_block and DW_TAG_variable. */
22771 loc = loc_list_from_tree (com_decl, 2, NULL);
22773 else if (DECL_EXTERNAL (decl_or_origin))
22774 add_AT_flag (com_die, DW_AT_declaration, 1);
22775 if (want_pubnames ())
22776 add_pubname_string (cnam, com_die); /* ??? needed? */
22777 com_die->decl_id = DECL_UID (com_decl);
22778 slot = common_block_die_table->find_slot (com_die, INSERT);
22779 *slot = com_die;
22781 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22783 add_AT_location_description (com_die, DW_AT_location, loc);
22784 loc = loc_list_from_tree (com_decl, 2, NULL);
22785 remove_AT (com_die, DW_AT_declaration);
22787 var_die = new_die (DW_TAG_variable, com_die, decl);
22788 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22789 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22790 decl_quals (decl_or_origin), false,
22791 context_die);
22792 add_alignment_attribute (var_die, decl);
22793 add_AT_flag (var_die, DW_AT_external, 1);
22794 if (loc)
22796 if (off)
22798 /* Optimize the common case. */
22799 if (single_element_loc_list_p (loc)
22800 && loc->expr->dw_loc_opc == DW_OP_addr
22801 && loc->expr->dw_loc_next == NULL
22802 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22804 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22805 loc->expr->dw_loc_oprnd1.v.val_addr
22806 = plus_constant (GET_MODE (x), x, off);
22808 else
22809 loc_list_plus_const (loc, off);
22811 add_AT_location_description (var_die, DW_AT_location, loc);
22813 else if (DECL_EXTERNAL (decl_or_origin))
22814 add_AT_flag (var_die, DW_AT_declaration, 1);
22815 if (decl)
22816 equate_decl_number_to_die (decl, var_die);
22817 return;
22820 if (old_die)
22822 if (declaration)
22824 /* A declaration that has been previously dumped, needs no
22825 further annotations, since it doesn't need location on
22826 the second pass. */
22827 return;
22829 else if (decl_will_get_specification_p (old_die, decl, declaration)
22830 && !get_AT (old_die, DW_AT_specification))
22832 /* Fall-thru so we can make a new variable die along with a
22833 DW_AT_specification. */
22835 else if (origin && old_die->die_parent != context_die)
22837 /* If we will be creating an inlined instance, we need a
22838 new DIE that will get annotated with
22839 DW_AT_abstract_origin. Clear things so we can get a
22840 new DIE. */
22841 gcc_assert (!DECL_ABSTRACT_P (decl));
22842 old_die = NULL;
22844 else
22846 /* If a DIE was dumped early, it still needs location info.
22847 Skip to where we fill the location bits. */
22848 var_die = old_die;
22850 /* ??? In LTRANS we cannot annotate early created variably
22851 modified type DIEs without copying them and adjusting all
22852 references to them. Thus we dumped them again, also add a
22853 reference to them. */
22854 tree type = TREE_TYPE (decl_or_origin);
22855 if (in_lto_p
22856 && variably_modified_type_p
22857 (type, decl_function_context (decl_or_origin)))
22859 if (decl_by_reference_p (decl_or_origin))
22860 add_type_attribute (var_die, TREE_TYPE (type),
22861 TYPE_UNQUALIFIED, false, context_die);
22862 else
22863 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22864 false, context_die);
22867 goto gen_variable_die_location;
22871 /* For static data members, the declaration in the class is supposed
22872 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22873 also in DWARF2; the specification should still be DW_TAG_variable
22874 referencing the DW_TAG_member DIE. */
22875 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22876 var_die = new_die (DW_TAG_member, context_die, decl);
22877 else
22878 var_die = new_die (DW_TAG_variable, context_die, decl);
22880 if (origin != NULL)
22881 add_abstract_origin_attribute (var_die, origin);
22883 /* Loop unrolling can create multiple blocks that refer to the same
22884 static variable, so we must test for the DW_AT_declaration flag.
22886 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22887 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22888 sharing them.
22890 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22891 else if (decl_will_get_specification_p (old_die, decl, declaration))
22893 /* This is a definition of a C++ class level static. */
22894 add_AT_specification (var_die, old_die);
22895 specialization_p = true;
22896 if (DECL_NAME (decl))
22898 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22899 struct dwarf_file_data * file_index = lookup_filename (s.file);
22901 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22902 add_AT_file (var_die, DW_AT_decl_file, file_index);
22904 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22905 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22907 if (debug_column_info
22908 && s.column
22909 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22910 != (unsigned) s.column))
22911 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22913 if (old_die->die_tag == DW_TAG_member)
22914 add_linkage_name (var_die, decl);
22917 else
22918 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22920 if ((origin == NULL && !specialization_p)
22921 || (origin != NULL
22922 && !DECL_ABSTRACT_P (decl_or_origin)
22923 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22924 decl_function_context
22925 (decl_or_origin))))
22927 tree type = TREE_TYPE (decl_or_origin);
22929 if (decl_by_reference_p (decl_or_origin))
22930 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22931 context_die);
22932 else
22933 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22934 context_die);
22937 if (origin == NULL && !specialization_p)
22939 if (TREE_PUBLIC (decl))
22940 add_AT_flag (var_die, DW_AT_external, 1);
22942 if (DECL_ARTIFICIAL (decl))
22943 add_AT_flag (var_die, DW_AT_artificial, 1);
22945 add_alignment_attribute (var_die, decl);
22947 add_accessibility_attribute (var_die, decl);
22950 if (declaration)
22951 add_AT_flag (var_die, DW_AT_declaration, 1);
22953 if (decl && (DECL_ABSTRACT_P (decl)
22954 || !old_die || is_declaration_die (old_die)))
22955 equate_decl_number_to_die (decl, var_die);
22957 gen_variable_die_location:
22958 if (! declaration
22959 && (! DECL_ABSTRACT_P (decl_or_origin)
22960 /* Local static vars are shared between all clones/inlines,
22961 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22962 already set. */
22963 || (VAR_P (decl_or_origin)
22964 && TREE_STATIC (decl_or_origin)
22965 && DECL_RTL_SET_P (decl_or_origin))))
22967 if (early_dwarf)
22968 add_pubname (decl_or_origin, var_die);
22969 else
22970 add_location_or_const_value_attribute (var_die, decl_or_origin,
22971 decl == NULL);
22973 else
22974 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22976 if ((dwarf_version >= 4 || !dwarf_strict)
22977 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22978 DW_AT_const_expr) == 1
22979 && !get_AT (var_die, DW_AT_const_expr)
22980 && !specialization_p)
22981 add_AT_flag (var_die, DW_AT_const_expr, 1);
22983 if (!dwarf_strict)
22985 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22986 DW_AT_inline);
22987 if (inl != -1
22988 && !get_AT (var_die, DW_AT_inline)
22989 && !specialization_p)
22990 add_AT_unsigned (var_die, DW_AT_inline, inl);
22994 /* Generate a DIE to represent a named constant. */
22996 static void
22997 gen_const_die (tree decl, dw_die_ref context_die)
22999 dw_die_ref const_die;
23000 tree type = TREE_TYPE (decl);
23002 const_die = lookup_decl_die (decl);
23003 if (const_die)
23004 return;
23006 const_die = new_die (DW_TAG_constant, context_die, decl);
23007 equate_decl_number_to_die (decl, const_die);
23008 add_name_and_src_coords_attributes (const_die, decl);
23009 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23010 if (TREE_PUBLIC (decl))
23011 add_AT_flag (const_die, DW_AT_external, 1);
23012 if (DECL_ARTIFICIAL (decl))
23013 add_AT_flag (const_die, DW_AT_artificial, 1);
23014 tree_add_const_value_attribute_for_decl (const_die, decl);
23017 /* Generate a DIE to represent a label identifier. */
23019 static void
23020 gen_label_die (tree decl, dw_die_ref context_die)
23022 tree origin = decl_ultimate_origin (decl);
23023 dw_die_ref lbl_die = lookup_decl_die (decl);
23024 rtx insn;
23025 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23027 if (!lbl_die)
23029 lbl_die = new_die (DW_TAG_label, context_die, decl);
23030 equate_decl_number_to_die (decl, lbl_die);
23032 if (origin != NULL)
23033 add_abstract_origin_attribute (lbl_die, origin);
23034 else
23035 add_name_and_src_coords_attributes (lbl_die, decl);
23038 if (DECL_ABSTRACT_P (decl))
23039 equate_decl_number_to_die (decl, lbl_die);
23040 else if (! early_dwarf)
23042 insn = DECL_RTL_IF_SET (decl);
23044 /* Deleted labels are programmer specified labels which have been
23045 eliminated because of various optimizations. We still emit them
23046 here so that it is possible to put breakpoints on them. */
23047 if (insn
23048 && (LABEL_P (insn)
23049 || ((NOTE_P (insn)
23050 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23052 /* When optimization is enabled (via -O) some parts of the compiler
23053 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23054 represent source-level labels which were explicitly declared by
23055 the user. This really shouldn't be happening though, so catch
23056 it if it ever does happen. */
23057 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23059 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23060 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23062 else if (insn
23063 && NOTE_P (insn)
23064 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23065 && CODE_LABEL_NUMBER (insn) != -1)
23067 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23068 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23073 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23074 attributes to the DIE for a block STMT, to describe where the inlined
23075 function was called from. This is similar to add_src_coords_attributes. */
23077 static inline void
23078 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23080 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23082 if (dwarf_version >= 3 || !dwarf_strict)
23084 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23085 add_AT_unsigned (die, DW_AT_call_line, s.line);
23086 if (debug_column_info && s.column)
23087 add_AT_unsigned (die, DW_AT_call_column, s.column);
23092 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23093 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23095 static inline void
23096 add_high_low_attributes (tree stmt, dw_die_ref die)
23098 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23100 if (BLOCK_FRAGMENT_CHAIN (stmt)
23101 && (dwarf_version >= 3 || !dwarf_strict))
23103 tree chain, superblock = NULL_TREE;
23104 dw_die_ref pdie;
23105 dw_attr_node *attr = NULL;
23107 if (inlined_function_outer_scope_p (stmt))
23109 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23110 BLOCK_NUMBER (stmt));
23111 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23114 /* Optimize duplicate .debug_ranges lists or even tails of
23115 lists. If this BLOCK has same ranges as its supercontext,
23116 lookup DW_AT_ranges attribute in the supercontext (and
23117 recursively so), verify that the ranges_table contains the
23118 right values and use it instead of adding a new .debug_range. */
23119 for (chain = stmt, pdie = die;
23120 BLOCK_SAME_RANGE (chain);
23121 chain = BLOCK_SUPERCONTEXT (chain))
23123 dw_attr_node *new_attr;
23125 pdie = pdie->die_parent;
23126 if (pdie == NULL)
23127 break;
23128 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23129 break;
23130 new_attr = get_AT (pdie, DW_AT_ranges);
23131 if (new_attr == NULL
23132 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23133 break;
23134 attr = new_attr;
23135 superblock = BLOCK_SUPERCONTEXT (chain);
23137 if (attr != NULL
23138 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23139 == BLOCK_NUMBER (superblock))
23140 && BLOCK_FRAGMENT_CHAIN (superblock))
23142 unsigned long off = attr->dw_attr_val.v.val_offset;
23143 unsigned long supercnt = 0, thiscnt = 0;
23144 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23145 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23147 ++supercnt;
23148 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23149 == BLOCK_NUMBER (chain));
23151 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23152 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23153 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23154 ++thiscnt;
23155 gcc_assert (supercnt >= thiscnt);
23156 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23157 false);
23158 note_rnglist_head (off + supercnt - thiscnt);
23159 return;
23162 unsigned int offset = add_ranges (stmt, true);
23163 add_AT_range_list (die, DW_AT_ranges, offset, false);
23164 note_rnglist_head (offset);
23166 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23167 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23170 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23171 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23172 chain = BLOCK_FRAGMENT_CHAIN (chain);
23174 while (chain);
23175 add_ranges (NULL);
23177 else
23179 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23180 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23181 BLOCK_NUMBER (stmt));
23182 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23183 BLOCK_NUMBER (stmt));
23184 add_AT_low_high_pc (die, label, label_high, false);
23188 /* Generate a DIE for a lexical block. */
23190 static void
23191 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23193 dw_die_ref old_die = BLOCK_DIE (stmt);
23194 dw_die_ref stmt_die = NULL;
23195 if (!old_die)
23197 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23198 BLOCK_DIE (stmt) = stmt_die;
23201 if (BLOCK_ABSTRACT (stmt))
23203 if (old_die)
23205 /* This must have been generated early and it won't even
23206 need location information since it's a DW_AT_inline
23207 function. */
23208 if (flag_checking)
23209 for (dw_die_ref c = context_die; c; c = c->die_parent)
23210 if (c->die_tag == DW_TAG_inlined_subroutine
23211 || c->die_tag == DW_TAG_subprogram)
23213 gcc_assert (get_AT (c, DW_AT_inline));
23214 break;
23216 return;
23219 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23221 /* If this is an inlined instance, create a new lexical die for
23222 anything below to attach DW_AT_abstract_origin to. */
23223 if (old_die)
23225 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23226 BLOCK_DIE (stmt) = stmt_die;
23227 old_die = NULL;
23230 tree origin = block_ultimate_origin (stmt);
23231 if (origin != NULL_TREE && origin != stmt)
23232 add_abstract_origin_attribute (stmt_die, origin);
23235 if (old_die)
23236 stmt_die = old_die;
23238 /* A non abstract block whose blocks have already been reordered
23239 should have the instruction range for this block. If so, set the
23240 high/low attributes. */
23241 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23243 gcc_assert (stmt_die);
23244 add_high_low_attributes (stmt, stmt_die);
23247 decls_for_scope (stmt, stmt_die);
23250 /* Generate a DIE for an inlined subprogram. */
23252 static void
23253 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23255 tree decl;
23257 /* The instance of function that is effectively being inlined shall not
23258 be abstract. */
23259 gcc_assert (! BLOCK_ABSTRACT (stmt));
23261 decl = block_ultimate_origin (stmt);
23263 /* Make sure any inlined functions are known to be inlineable. */
23264 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23265 || cgraph_function_possibly_inlined_p (decl));
23267 if (! BLOCK_ABSTRACT (stmt))
23269 dw_die_ref subr_die
23270 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23272 if (call_arg_locations)
23273 BLOCK_DIE (stmt) = subr_die;
23274 add_abstract_origin_attribute (subr_die, decl);
23275 if (TREE_ASM_WRITTEN (stmt))
23276 add_high_low_attributes (stmt, subr_die);
23277 add_call_src_coords_attributes (stmt, subr_die);
23279 decls_for_scope (stmt, subr_die);
23283 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23284 the comment for VLR_CONTEXT. */
23286 static void
23287 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23289 dw_die_ref decl_die;
23291 if (TREE_TYPE (decl) == error_mark_node)
23292 return;
23294 decl_die = new_die (DW_TAG_member, context_die, decl);
23295 add_name_and_src_coords_attributes (decl_die, decl);
23296 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23297 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23298 context_die);
23300 if (DECL_BIT_FIELD_TYPE (decl))
23302 add_byte_size_attribute (decl_die, decl);
23303 add_bit_size_attribute (decl_die, decl);
23304 add_bit_offset_attribute (decl_die, decl, ctx);
23307 add_alignment_attribute (decl_die, decl);
23309 /* If we have a variant part offset, then we are supposed to process a member
23310 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23311 trees. */
23312 gcc_assert (ctx->variant_part_offset == NULL_TREE
23313 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23314 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23315 add_data_member_location_attribute (decl_die, decl, ctx);
23317 if (DECL_ARTIFICIAL (decl))
23318 add_AT_flag (decl_die, DW_AT_artificial, 1);
23320 add_accessibility_attribute (decl_die, decl);
23322 /* Equate decl number to die, so that we can look up this decl later on. */
23323 equate_decl_number_to_die (decl, decl_die);
23326 /* Generate a DIE for a pointer to a member type. TYPE can be an
23327 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23328 pointer to member function. */
23330 static void
23331 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23333 if (lookup_type_die (type))
23334 return;
23336 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23337 scope_die_for (type, context_die), type);
23339 equate_type_number_to_die (type, ptr_die);
23340 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23341 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23342 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23343 context_die);
23344 add_alignment_attribute (ptr_die, type);
23346 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23347 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23349 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23350 add_AT_loc (ptr_die, DW_AT_use_location, op);
23354 static char *producer_string;
23356 /* Return a heap allocated producer string including command line options
23357 if -grecord-gcc-switches. */
23359 static char *
23360 gen_producer_string (void)
23362 size_t j;
23363 auto_vec<const char *> switches;
23364 const char *language_string = lang_hooks.name;
23365 char *producer, *tail;
23366 const char *p;
23367 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23368 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23370 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23371 switch (save_decoded_options[j].opt_index)
23373 case OPT_o:
23374 case OPT_d:
23375 case OPT_dumpbase:
23376 case OPT_dumpdir:
23377 case OPT_auxbase:
23378 case OPT_auxbase_strip:
23379 case OPT_quiet:
23380 case OPT_version:
23381 case OPT_v:
23382 case OPT_w:
23383 case OPT_L:
23384 case OPT_D:
23385 case OPT_I:
23386 case OPT_U:
23387 case OPT_SPECIAL_unknown:
23388 case OPT_SPECIAL_ignore:
23389 case OPT_SPECIAL_program_name:
23390 case OPT_SPECIAL_input_file:
23391 case OPT_grecord_gcc_switches:
23392 case OPT__output_pch_:
23393 case OPT_fdiagnostics_show_location_:
23394 case OPT_fdiagnostics_show_option:
23395 case OPT_fdiagnostics_show_caret:
23396 case OPT_fdiagnostics_color_:
23397 case OPT_fverbose_asm:
23398 case OPT____:
23399 case OPT__sysroot_:
23400 case OPT_nostdinc:
23401 case OPT_nostdinc__:
23402 case OPT_fpreprocessed:
23403 case OPT_fltrans_output_list_:
23404 case OPT_fresolution_:
23405 case OPT_fdebug_prefix_map_:
23406 /* Ignore these. */
23407 continue;
23408 default:
23409 if (cl_options[save_decoded_options[j].opt_index].flags
23410 & CL_NO_DWARF_RECORD)
23411 continue;
23412 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23413 == '-');
23414 switch (save_decoded_options[j].canonical_option[0][1])
23416 case 'M':
23417 case 'i':
23418 case 'W':
23419 continue;
23420 case 'f':
23421 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23422 "dump", 4) == 0)
23423 continue;
23424 break;
23425 default:
23426 break;
23428 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23429 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23430 break;
23433 producer = XNEWVEC (char, plen + 1 + len + 1);
23434 tail = producer;
23435 sprintf (tail, "%s %s", language_string, version_string);
23436 tail += plen;
23438 FOR_EACH_VEC_ELT (switches, j, p)
23440 len = strlen (p);
23441 *tail = ' ';
23442 memcpy (tail + 1, p, len);
23443 tail += len + 1;
23446 *tail = '\0';
23447 return producer;
23450 /* Given a C and/or C++ language/version string return the "highest".
23451 C++ is assumed to be "higher" than C in this case. Used for merging
23452 LTO translation unit languages. */
23453 static const char *
23454 highest_c_language (const char *lang1, const char *lang2)
23456 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23457 return "GNU C++17";
23458 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23459 return "GNU C++14";
23460 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23461 return "GNU C++11";
23462 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23463 return "GNU C++98";
23465 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23466 return "GNU C17";
23467 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23468 return "GNU C11";
23469 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23470 return "GNU C99";
23471 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23472 return "GNU C89";
23474 gcc_unreachable ();
23478 /* Generate the DIE for the compilation unit. */
23480 static dw_die_ref
23481 gen_compile_unit_die (const char *filename)
23483 dw_die_ref die;
23484 const char *language_string = lang_hooks.name;
23485 int language;
23487 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23489 if (filename)
23491 add_name_attribute (die, filename);
23492 /* Don't add cwd for <built-in>. */
23493 if (filename[0] != '<')
23494 add_comp_dir_attribute (die);
23497 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23499 /* If our producer is LTO try to figure out a common language to use
23500 from the global list of translation units. */
23501 if (strcmp (language_string, "GNU GIMPLE") == 0)
23503 unsigned i;
23504 tree t;
23505 const char *common_lang = NULL;
23507 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23509 if (!TRANSLATION_UNIT_LANGUAGE (t))
23510 continue;
23511 if (!common_lang)
23512 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23513 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23515 else if (strncmp (common_lang, "GNU C", 5) == 0
23516 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23517 /* Mixing C and C++ is ok, use C++ in that case. */
23518 common_lang = highest_c_language (common_lang,
23519 TRANSLATION_UNIT_LANGUAGE (t));
23520 else
23522 /* Fall back to C. */
23523 common_lang = NULL;
23524 break;
23528 if (common_lang)
23529 language_string = common_lang;
23532 language = DW_LANG_C;
23533 if (strncmp (language_string, "GNU C", 5) == 0
23534 && ISDIGIT (language_string[5]))
23536 language = DW_LANG_C89;
23537 if (dwarf_version >= 3 || !dwarf_strict)
23539 if (strcmp (language_string, "GNU C89") != 0)
23540 language = DW_LANG_C99;
23542 if (dwarf_version >= 5 /* || !dwarf_strict */)
23543 if (strcmp (language_string, "GNU C11") == 0
23544 || strcmp (language_string, "GNU C17") == 0)
23545 language = DW_LANG_C11;
23548 else if (strncmp (language_string, "GNU C++", 7) == 0)
23550 language = DW_LANG_C_plus_plus;
23551 if (dwarf_version >= 5 /* || !dwarf_strict */)
23553 if (strcmp (language_string, "GNU C++11") == 0)
23554 language = DW_LANG_C_plus_plus_11;
23555 else if (strcmp (language_string, "GNU C++14") == 0)
23556 language = DW_LANG_C_plus_plus_14;
23557 else if (strcmp (language_string, "GNU C++17") == 0)
23558 /* For now. */
23559 language = DW_LANG_C_plus_plus_14;
23562 else if (strcmp (language_string, "GNU F77") == 0)
23563 language = DW_LANG_Fortran77;
23564 else if (dwarf_version >= 3 || !dwarf_strict)
23566 if (strcmp (language_string, "GNU Ada") == 0)
23567 language = DW_LANG_Ada95;
23568 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23570 language = DW_LANG_Fortran95;
23571 if (dwarf_version >= 5 /* || !dwarf_strict */)
23573 if (strcmp (language_string, "GNU Fortran2003") == 0)
23574 language = DW_LANG_Fortran03;
23575 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23576 language = DW_LANG_Fortran08;
23579 else if (strcmp (language_string, "GNU Objective-C") == 0)
23580 language = DW_LANG_ObjC;
23581 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23582 language = DW_LANG_ObjC_plus_plus;
23583 else if (dwarf_version >= 5 || !dwarf_strict)
23585 if (strcmp (language_string, "GNU Go") == 0)
23586 language = DW_LANG_Go;
23589 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23590 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23591 language = DW_LANG_Fortran90;
23593 add_AT_unsigned (die, DW_AT_language, language);
23595 switch (language)
23597 case DW_LANG_Fortran77:
23598 case DW_LANG_Fortran90:
23599 case DW_LANG_Fortran95:
23600 case DW_LANG_Fortran03:
23601 case DW_LANG_Fortran08:
23602 /* Fortran has case insensitive identifiers and the front-end
23603 lowercases everything. */
23604 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23605 break;
23606 default:
23607 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23608 break;
23610 return die;
23613 /* Generate the DIE for a base class. */
23615 static void
23616 gen_inheritance_die (tree binfo, tree access, tree type,
23617 dw_die_ref context_die)
23619 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23620 struct vlr_context ctx = { type, NULL };
23622 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23623 context_die);
23624 add_data_member_location_attribute (die, binfo, &ctx);
23626 if (BINFO_VIRTUAL_P (binfo))
23627 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23629 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23630 children, otherwise the default is DW_ACCESS_public. In DWARF2
23631 the default has always been DW_ACCESS_private. */
23632 if (access == access_public_node)
23634 if (dwarf_version == 2
23635 || context_die->die_tag == DW_TAG_class_type)
23636 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23638 else if (access == access_protected_node)
23639 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23640 else if (dwarf_version > 2
23641 && context_die->die_tag != DW_TAG_class_type)
23642 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23645 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23646 structure. */
23647 static bool
23648 is_variant_part (tree decl)
23650 return (TREE_CODE (decl) == FIELD_DECL
23651 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23654 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23655 return the FIELD_DECL. Return NULL_TREE otherwise. */
23657 static tree
23658 analyze_discr_in_predicate (tree operand, tree struct_type)
23660 bool continue_stripping = true;
23661 while (continue_stripping)
23662 switch (TREE_CODE (operand))
23664 CASE_CONVERT:
23665 operand = TREE_OPERAND (operand, 0);
23666 break;
23667 default:
23668 continue_stripping = false;
23669 break;
23672 /* Match field access to members of struct_type only. */
23673 if (TREE_CODE (operand) == COMPONENT_REF
23674 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23675 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23676 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23677 return TREE_OPERAND (operand, 1);
23678 else
23679 return NULL_TREE;
23682 /* Check that SRC is a constant integer that can be represented as a native
23683 integer constant (either signed or unsigned). If so, store it into DEST and
23684 return true. Return false otherwise. */
23686 static bool
23687 get_discr_value (tree src, dw_discr_value *dest)
23689 tree discr_type = TREE_TYPE (src);
23691 if (lang_hooks.types.get_debug_type)
23693 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23694 if (debug_type != NULL)
23695 discr_type = debug_type;
23698 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23699 return false;
23701 /* Signedness can vary between the original type and the debug type. This
23702 can happen for character types in Ada for instance: the character type
23703 used for code generation can be signed, to be compatible with the C one,
23704 but from a debugger point of view, it must be unsigned. */
23705 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23706 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23708 if (is_orig_unsigned != is_debug_unsigned)
23709 src = fold_convert (discr_type, src);
23711 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23712 return false;
23714 dest->pos = is_debug_unsigned;
23715 if (is_debug_unsigned)
23716 dest->v.uval = tree_to_uhwi (src);
23717 else
23718 dest->v.sval = tree_to_shwi (src);
23720 return true;
23723 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23724 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23725 store NULL_TREE in DISCR_DECL. Otherwise:
23727 - store the discriminant field in STRUCT_TYPE that controls the variant
23728 part to *DISCR_DECL
23730 - put in *DISCR_LISTS_P an array where for each variant, the item
23731 represents the corresponding matching list of discriminant values.
23733 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23734 the above array.
23736 Note that when the array is allocated (i.e. when the analysis is
23737 successful), it is up to the caller to free the array. */
23739 static void
23740 analyze_variants_discr (tree variant_part_decl,
23741 tree struct_type,
23742 tree *discr_decl,
23743 dw_discr_list_ref **discr_lists_p,
23744 unsigned *discr_lists_length)
23746 tree variant_part_type = TREE_TYPE (variant_part_decl);
23747 tree variant;
23748 dw_discr_list_ref *discr_lists;
23749 unsigned i;
23751 /* Compute how many variants there are in this variant part. */
23752 *discr_lists_length = 0;
23753 for (variant = TYPE_FIELDS (variant_part_type);
23754 variant != NULL_TREE;
23755 variant = DECL_CHAIN (variant))
23756 ++*discr_lists_length;
23758 *discr_decl = NULL_TREE;
23759 *discr_lists_p
23760 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23761 sizeof (**discr_lists_p));
23762 discr_lists = *discr_lists_p;
23764 /* And then analyze all variants to extract discriminant information for all
23765 of them. This analysis is conservative: as soon as we detect something we
23766 do not support, abort everything and pretend we found nothing. */
23767 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23768 variant != NULL_TREE;
23769 variant = DECL_CHAIN (variant), ++i)
23771 tree match_expr = DECL_QUALIFIER (variant);
23773 /* Now, try to analyze the predicate and deduce a discriminant for
23774 it. */
23775 if (match_expr == boolean_true_node)
23776 /* Typically happens for the default variant: it matches all cases that
23777 previous variants rejected. Don't output any matching value for
23778 this one. */
23779 continue;
23781 /* The following loop tries to iterate over each discriminant
23782 possibility: single values or ranges. */
23783 while (match_expr != NULL_TREE)
23785 tree next_round_match_expr;
23786 tree candidate_discr = NULL_TREE;
23787 dw_discr_list_ref new_node = NULL;
23789 /* Possibilities are matched one after the other by nested
23790 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23791 continue with the rest at next iteration. */
23792 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23794 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23795 match_expr = TREE_OPERAND (match_expr, 1);
23797 else
23798 next_round_match_expr = NULL_TREE;
23800 if (match_expr == boolean_false_node)
23801 /* This sub-expression matches nothing: just wait for the next
23802 one. */
23805 else if (TREE_CODE (match_expr) == EQ_EXPR)
23807 /* We are matching: <discr_field> == <integer_cst>
23808 This sub-expression matches a single value. */
23809 tree integer_cst = TREE_OPERAND (match_expr, 1);
23811 candidate_discr
23812 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23813 struct_type);
23815 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23816 if (!get_discr_value (integer_cst,
23817 &new_node->dw_discr_lower_bound))
23818 goto abort;
23819 new_node->dw_discr_range = false;
23822 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23824 /* We are matching:
23825 <discr_field> > <integer_cst>
23826 && <discr_field> < <integer_cst>.
23827 This sub-expression matches the range of values between the
23828 two matched integer constants. Note that comparisons can be
23829 inclusive or exclusive. */
23830 tree candidate_discr_1, candidate_discr_2;
23831 tree lower_cst, upper_cst;
23832 bool lower_cst_included, upper_cst_included;
23833 tree lower_op = TREE_OPERAND (match_expr, 0);
23834 tree upper_op = TREE_OPERAND (match_expr, 1);
23836 /* When the comparison is exclusive, the integer constant is not
23837 the discriminant range bound we are looking for: we will have
23838 to increment or decrement it. */
23839 if (TREE_CODE (lower_op) == GE_EXPR)
23840 lower_cst_included = true;
23841 else if (TREE_CODE (lower_op) == GT_EXPR)
23842 lower_cst_included = false;
23843 else
23844 goto abort;
23846 if (TREE_CODE (upper_op) == LE_EXPR)
23847 upper_cst_included = true;
23848 else if (TREE_CODE (upper_op) == LT_EXPR)
23849 upper_cst_included = false;
23850 else
23851 goto abort;
23853 /* Extract the discriminant from the first operand and check it
23854 is consistant with the same analysis in the second
23855 operand. */
23856 candidate_discr_1
23857 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23858 struct_type);
23859 candidate_discr_2
23860 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23861 struct_type);
23862 if (candidate_discr_1 == candidate_discr_2)
23863 candidate_discr = candidate_discr_1;
23864 else
23865 goto abort;
23867 /* Extract bounds from both. */
23868 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23869 lower_cst = TREE_OPERAND (lower_op, 1);
23870 upper_cst = TREE_OPERAND (upper_op, 1);
23872 if (!lower_cst_included)
23873 lower_cst
23874 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23875 build_int_cst (TREE_TYPE (lower_cst), 1));
23876 if (!upper_cst_included)
23877 upper_cst
23878 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23879 build_int_cst (TREE_TYPE (upper_cst), 1));
23881 if (!get_discr_value (lower_cst,
23882 &new_node->dw_discr_lower_bound)
23883 || !get_discr_value (upper_cst,
23884 &new_node->dw_discr_upper_bound))
23885 goto abort;
23887 new_node->dw_discr_range = true;
23890 else
23891 /* Unsupported sub-expression: we cannot determine the set of
23892 matching discriminant values. Abort everything. */
23893 goto abort;
23895 /* If the discriminant info is not consistant with what we saw so
23896 far, consider the analysis failed and abort everything. */
23897 if (candidate_discr == NULL_TREE
23898 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23899 goto abort;
23900 else
23901 *discr_decl = candidate_discr;
23903 if (new_node != NULL)
23905 new_node->dw_discr_next = discr_lists[i];
23906 discr_lists[i] = new_node;
23908 match_expr = next_round_match_expr;
23912 /* If we reach this point, we could match everything we were interested
23913 in. */
23914 return;
23916 abort:
23917 /* Clean all data structure and return no result. */
23918 free (*discr_lists_p);
23919 *discr_lists_p = NULL;
23920 *discr_decl = NULL_TREE;
23923 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23924 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23925 under CONTEXT_DIE.
23927 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23928 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23929 this type, which are record types, represent the available variants and each
23930 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23931 values are inferred from these attributes.
23933 In trees, the offsets for the fields inside these sub-records are relative
23934 to the variant part itself, whereas the corresponding DIEs should have
23935 offset attributes that are relative to the embedding record base address.
23936 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23937 must be an expression that computes the offset of the variant part to
23938 describe in DWARF. */
23940 static void
23941 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23942 dw_die_ref context_die)
23944 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23945 tree variant_part_offset = vlr_ctx->variant_part_offset;
23946 struct loc_descr_context ctx = {
23947 vlr_ctx->struct_type, /* context_type */
23948 NULL_TREE, /* base_decl */
23949 NULL, /* dpi */
23950 false, /* placeholder_arg */
23951 false /* placeholder_seen */
23954 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23955 NULL_TREE if there is no such field. */
23956 tree discr_decl = NULL_TREE;
23957 dw_discr_list_ref *discr_lists;
23958 unsigned discr_lists_length = 0;
23959 unsigned i;
23961 dw_die_ref dwarf_proc_die = NULL;
23962 dw_die_ref variant_part_die
23963 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23965 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23967 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23968 &discr_decl, &discr_lists, &discr_lists_length);
23970 if (discr_decl != NULL_TREE)
23972 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23974 if (discr_die)
23975 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23976 else
23977 /* We have no DIE for the discriminant, so just discard all
23978 discrimimant information in the output. */
23979 discr_decl = NULL_TREE;
23982 /* If the offset for this variant part is more complex than a constant,
23983 create a DWARF procedure for it so that we will not have to generate DWARF
23984 expressions for it for each member. */
23985 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23986 && (dwarf_version >= 3 || !dwarf_strict))
23988 const tree dwarf_proc_fndecl
23989 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23990 build_function_type (TREE_TYPE (variant_part_offset),
23991 NULL_TREE));
23992 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23993 const dw_loc_descr_ref dwarf_proc_body
23994 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23996 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23997 dwarf_proc_fndecl, context_die);
23998 if (dwarf_proc_die != NULL)
23999 variant_part_offset = dwarf_proc_call;
24002 /* Output DIEs for all variants. */
24003 i = 0;
24004 for (tree variant = TYPE_FIELDS (variant_part_type);
24005 variant != NULL_TREE;
24006 variant = DECL_CHAIN (variant), ++i)
24008 tree variant_type = TREE_TYPE (variant);
24009 dw_die_ref variant_die;
24011 /* All variants (i.e. members of a variant part) are supposed to be
24012 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24013 under these records. */
24014 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24016 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24017 equate_decl_number_to_die (variant, variant_die);
24019 /* Output discriminant values this variant matches, if any. */
24020 if (discr_decl == NULL || discr_lists[i] == NULL)
24021 /* In the case we have discriminant information at all, this is
24022 probably the default variant: as the standard says, don't
24023 output any discriminant value/list attribute. */
24025 else if (discr_lists[i]->dw_discr_next == NULL
24026 && !discr_lists[i]->dw_discr_range)
24027 /* If there is only one accepted value, don't bother outputting a
24028 list. */
24029 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24030 else
24031 add_discr_list (variant_die, discr_lists[i]);
24033 for (tree member = TYPE_FIELDS (variant_type);
24034 member != NULL_TREE;
24035 member = DECL_CHAIN (member))
24037 struct vlr_context vlr_sub_ctx = {
24038 vlr_ctx->struct_type, /* struct_type */
24039 NULL /* variant_part_offset */
24041 if (is_variant_part (member))
24043 /* All offsets for fields inside variant parts are relative to
24044 the top-level embedding RECORD_TYPE's base address. On the
24045 other hand, offsets in GCC's types are relative to the
24046 nested-most variant part. So we have to sum offsets each time
24047 we recurse. */
24049 vlr_sub_ctx.variant_part_offset
24050 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24051 variant_part_offset, byte_position (member));
24052 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24054 else
24056 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24057 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24062 free (discr_lists);
24065 /* Generate a DIE for a class member. */
24067 static void
24068 gen_member_die (tree type, dw_die_ref context_die)
24070 tree member;
24071 tree binfo = TYPE_BINFO (type);
24073 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24075 /* If this is not an incomplete type, output descriptions of each of its
24076 members. Note that as we output the DIEs necessary to represent the
24077 members of this record or union type, we will also be trying to output
24078 DIEs to represent the *types* of those members. However the `type'
24079 function (above) will specifically avoid generating type DIEs for member
24080 types *within* the list of member DIEs for this (containing) type except
24081 for those types (of members) which are explicitly marked as also being
24082 members of this (containing) type themselves. The g++ front- end can
24083 force any given type to be treated as a member of some other (containing)
24084 type by setting the TYPE_CONTEXT of the given (member) type to point to
24085 the TREE node representing the appropriate (containing) type. */
24087 /* First output info about the base classes. */
24088 if (binfo)
24090 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24091 int i;
24092 tree base;
24094 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24095 gen_inheritance_die (base,
24096 (accesses ? (*accesses)[i] : access_public_node),
24097 type,
24098 context_die);
24101 /* Now output info about the data members and type members. */
24102 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24104 struct vlr_context vlr_ctx = { type, NULL_TREE };
24105 bool static_inline_p
24106 = (TREE_STATIC (member)
24107 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24108 != -1));
24110 /* Ignore clones. */
24111 if (DECL_ABSTRACT_ORIGIN (member))
24112 continue;
24114 /* If we thought we were generating minimal debug info for TYPE
24115 and then changed our minds, some of the member declarations
24116 may have already been defined. Don't define them again, but
24117 do put them in the right order. */
24119 if (dw_die_ref child = lookup_decl_die (member))
24121 /* Handle inline static data members, which only have in-class
24122 declarations. */
24123 dw_die_ref ref = NULL;
24124 if (child->die_tag == DW_TAG_variable
24125 && child->die_parent == comp_unit_die ())
24127 ref = get_AT_ref (child, DW_AT_specification);
24128 /* For C++17 inline static data members followed by redundant
24129 out of class redeclaration, we might get here with
24130 child being the DIE created for the out of class
24131 redeclaration and with its DW_AT_specification being
24132 the DIE created for in-class definition. We want to
24133 reparent the latter, and don't want to create another
24134 DIE with DW_AT_specification in that case, because
24135 we already have one. */
24136 if (ref
24137 && static_inline_p
24138 && ref->die_tag == DW_TAG_variable
24139 && ref->die_parent == comp_unit_die ()
24140 && get_AT (ref, DW_AT_specification) == NULL)
24142 child = ref;
24143 ref = NULL;
24144 static_inline_p = false;
24148 if (child->die_tag == DW_TAG_variable
24149 && child->die_parent == comp_unit_die ()
24150 && ref == NULL)
24152 reparent_child (child, context_die);
24153 if (dwarf_version < 5)
24154 child->die_tag = DW_TAG_member;
24156 else
24157 splice_child_die (context_die, child);
24160 /* Do not generate standard DWARF for variant parts if we are generating
24161 the corresponding GNAT encodings: DIEs generated for both would
24162 conflict in our mappings. */
24163 else if (is_variant_part (member)
24164 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24166 vlr_ctx.variant_part_offset = byte_position (member);
24167 gen_variant_part (member, &vlr_ctx, context_die);
24169 else
24171 vlr_ctx.variant_part_offset = NULL_TREE;
24172 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24175 /* For C++ inline static data members emit immediately a DW_TAG_variable
24176 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24177 DW_AT_specification. */
24178 if (static_inline_p)
24180 int old_extern = DECL_EXTERNAL (member);
24181 DECL_EXTERNAL (member) = 0;
24182 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24183 DECL_EXTERNAL (member) = old_extern;
24188 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24189 is set, we pretend that the type was never defined, so we only get the
24190 member DIEs needed by later specification DIEs. */
24192 static void
24193 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24194 enum debug_info_usage usage)
24196 if (TREE_ASM_WRITTEN (type))
24198 /* Fill in the bound of variable-length fields in late dwarf if
24199 still incomplete. */
24200 if (!early_dwarf && variably_modified_type_p (type, NULL))
24201 for (tree member = TYPE_FIELDS (type);
24202 member;
24203 member = DECL_CHAIN (member))
24204 fill_variable_array_bounds (TREE_TYPE (member));
24205 return;
24208 dw_die_ref type_die = lookup_type_die (type);
24209 dw_die_ref scope_die = 0;
24210 int nested = 0;
24211 int complete = (TYPE_SIZE (type)
24212 && (! TYPE_STUB_DECL (type)
24213 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24214 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24215 complete = complete && should_emit_struct_debug (type, usage);
24217 if (type_die && ! complete)
24218 return;
24220 if (TYPE_CONTEXT (type) != NULL_TREE
24221 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24222 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24223 nested = 1;
24225 scope_die = scope_die_for (type, context_die);
24227 /* Generate child dies for template paramaters. */
24228 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24229 schedule_generic_params_dies_gen (type);
24231 if (! type_die || (nested && is_cu_die (scope_die)))
24232 /* First occurrence of type or toplevel definition of nested class. */
24234 dw_die_ref old_die = type_die;
24236 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24237 ? record_type_tag (type) : DW_TAG_union_type,
24238 scope_die, type);
24239 equate_type_number_to_die (type, type_die);
24240 if (old_die)
24241 add_AT_specification (type_die, old_die);
24242 else
24243 add_name_attribute (type_die, type_tag (type));
24245 else
24246 remove_AT (type_die, DW_AT_declaration);
24248 /* If this type has been completed, then give it a byte_size attribute and
24249 then give a list of members. */
24250 if (complete && !ns_decl)
24252 /* Prevent infinite recursion in cases where the type of some member of
24253 this type is expressed in terms of this type itself. */
24254 TREE_ASM_WRITTEN (type) = 1;
24255 add_byte_size_attribute (type_die, type);
24256 add_alignment_attribute (type_die, type);
24257 if (TYPE_STUB_DECL (type) != NULL_TREE)
24259 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24260 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24263 /* If the first reference to this type was as the return type of an
24264 inline function, then it may not have a parent. Fix this now. */
24265 if (type_die->die_parent == NULL)
24266 add_child_die (scope_die, type_die);
24268 push_decl_scope (type);
24269 gen_member_die (type, type_die);
24270 pop_decl_scope ();
24272 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24273 if (TYPE_ARTIFICIAL (type))
24274 add_AT_flag (type_die, DW_AT_artificial, 1);
24276 /* GNU extension: Record what type our vtable lives in. */
24277 if (TYPE_VFIELD (type))
24279 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24281 gen_type_die (vtype, context_die);
24282 add_AT_die_ref (type_die, DW_AT_containing_type,
24283 lookup_type_die (vtype));
24286 else
24288 add_AT_flag (type_die, DW_AT_declaration, 1);
24290 /* We don't need to do this for function-local types. */
24291 if (TYPE_STUB_DECL (type)
24292 && ! decl_function_context (TYPE_STUB_DECL (type)))
24293 vec_safe_push (incomplete_types, type);
24296 if (get_AT (type_die, DW_AT_name))
24297 add_pubtype (type, type_die);
24300 /* Generate a DIE for a subroutine _type_. */
24302 static void
24303 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24305 tree return_type = TREE_TYPE (type);
24306 dw_die_ref subr_die
24307 = new_die (DW_TAG_subroutine_type,
24308 scope_die_for (type, context_die), type);
24310 equate_type_number_to_die (type, subr_die);
24311 add_prototyped_attribute (subr_die, type);
24312 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24313 context_die);
24314 add_alignment_attribute (subr_die, type);
24315 gen_formal_types_die (type, subr_die);
24317 if (get_AT (subr_die, DW_AT_name))
24318 add_pubtype (type, subr_die);
24319 if ((dwarf_version >= 5 || !dwarf_strict)
24320 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24321 add_AT_flag (subr_die, DW_AT_reference, 1);
24322 if ((dwarf_version >= 5 || !dwarf_strict)
24323 && lang_hooks.types.type_dwarf_attribute (type,
24324 DW_AT_rvalue_reference) != -1)
24325 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24328 /* Generate a DIE for a type definition. */
24330 static void
24331 gen_typedef_die (tree decl, dw_die_ref context_die)
24333 dw_die_ref type_die;
24334 tree type;
24336 if (TREE_ASM_WRITTEN (decl))
24338 if (DECL_ORIGINAL_TYPE (decl))
24339 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24340 return;
24343 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24344 checks in process_scope_var and modified_type_die), this should be called
24345 only for original types. */
24346 gcc_assert (decl_ultimate_origin (decl) == NULL
24347 || decl_ultimate_origin (decl) == decl);
24349 TREE_ASM_WRITTEN (decl) = 1;
24350 type_die = new_die (DW_TAG_typedef, context_die, decl);
24352 add_name_and_src_coords_attributes (type_die, decl);
24353 if (DECL_ORIGINAL_TYPE (decl))
24355 type = DECL_ORIGINAL_TYPE (decl);
24356 if (type == error_mark_node)
24357 return;
24359 gcc_assert (type != TREE_TYPE (decl));
24360 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24362 else
24364 type = TREE_TYPE (decl);
24365 if (type == error_mark_node)
24366 return;
24368 if (is_naming_typedef_decl (TYPE_NAME (type)))
24370 /* Here, we are in the case of decl being a typedef naming
24371 an anonymous type, e.g:
24372 typedef struct {...} foo;
24373 In that case TREE_TYPE (decl) is not a typedef variant
24374 type and TYPE_NAME of the anonymous type is set to the
24375 TYPE_DECL of the typedef. This construct is emitted by
24376 the C++ FE.
24378 TYPE is the anonymous struct named by the typedef
24379 DECL. As we need the DW_AT_type attribute of the
24380 DW_TAG_typedef to point to the DIE of TYPE, let's
24381 generate that DIE right away. add_type_attribute
24382 called below will then pick (via lookup_type_die) that
24383 anonymous struct DIE. */
24384 if (!TREE_ASM_WRITTEN (type))
24385 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24387 /* This is a GNU Extension. We are adding a
24388 DW_AT_linkage_name attribute to the DIE of the
24389 anonymous struct TYPE. The value of that attribute
24390 is the name of the typedef decl naming the anonymous
24391 struct. This greatly eases the work of consumers of
24392 this debug info. */
24393 add_linkage_name_raw (lookup_type_die (type), decl);
24397 add_type_attribute (type_die, type, decl_quals (decl), false,
24398 context_die);
24400 if (is_naming_typedef_decl (decl))
24401 /* We want that all subsequent calls to lookup_type_die with
24402 TYPE in argument yield the DW_TAG_typedef we have just
24403 created. */
24404 equate_type_number_to_die (type, type_die);
24406 add_alignment_attribute (type_die, TREE_TYPE (decl));
24408 add_accessibility_attribute (type_die, decl);
24410 if (DECL_ABSTRACT_P (decl))
24411 equate_decl_number_to_die (decl, type_die);
24413 if (get_AT (type_die, DW_AT_name))
24414 add_pubtype (decl, type_die);
24417 /* Generate a DIE for a struct, class, enum or union type. */
24419 static void
24420 gen_tagged_type_die (tree type,
24421 dw_die_ref context_die,
24422 enum debug_info_usage usage)
24424 int need_pop;
24426 if (type == NULL_TREE
24427 || !is_tagged_type (type))
24428 return;
24430 if (TREE_ASM_WRITTEN (type))
24431 need_pop = 0;
24432 /* If this is a nested type whose containing class hasn't been written
24433 out yet, writing it out will cover this one, too. This does not apply
24434 to instantiations of member class templates; they need to be added to
24435 the containing class as they are generated. FIXME: This hurts the
24436 idea of combining type decls from multiple TUs, since we can't predict
24437 what set of template instantiations we'll get. */
24438 else if (TYPE_CONTEXT (type)
24439 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24440 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24442 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24444 if (TREE_ASM_WRITTEN (type))
24445 return;
24447 /* If that failed, attach ourselves to the stub. */
24448 push_decl_scope (TYPE_CONTEXT (type));
24449 context_die = lookup_type_die (TYPE_CONTEXT (type));
24450 need_pop = 1;
24452 else if (TYPE_CONTEXT (type) != NULL_TREE
24453 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24455 /* If this type is local to a function that hasn't been written
24456 out yet, use a NULL context for now; it will be fixed up in
24457 decls_for_scope. */
24458 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24459 /* A declaration DIE doesn't count; nested types need to go in the
24460 specification. */
24461 if (context_die && is_declaration_die (context_die))
24462 context_die = NULL;
24463 need_pop = 0;
24465 else
24467 context_die = declare_in_namespace (type, context_die);
24468 need_pop = 0;
24471 if (TREE_CODE (type) == ENUMERAL_TYPE)
24473 /* This might have been written out by the call to
24474 declare_in_namespace. */
24475 if (!TREE_ASM_WRITTEN (type))
24476 gen_enumeration_type_die (type, context_die);
24478 else
24479 gen_struct_or_union_type_die (type, context_die, usage);
24481 if (need_pop)
24482 pop_decl_scope ();
24484 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24485 it up if it is ever completed. gen_*_type_die will set it for us
24486 when appropriate. */
24489 /* Generate a type description DIE. */
24491 static void
24492 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24493 enum debug_info_usage usage)
24495 struct array_descr_info info;
24497 if (type == NULL_TREE || type == error_mark_node)
24498 return;
24500 if (flag_checking && type)
24501 verify_type (type);
24503 if (TYPE_NAME (type) != NULL_TREE
24504 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24505 && is_redundant_typedef (TYPE_NAME (type))
24506 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24507 /* The DECL of this type is a typedef we don't want to emit debug
24508 info for but we want debug info for its underlying typedef.
24509 This can happen for e.g, the injected-class-name of a C++
24510 type. */
24511 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24513 /* If TYPE is a typedef type variant, let's generate debug info
24514 for the parent typedef which TYPE is a type of. */
24515 if (typedef_variant_p (type))
24517 if (TREE_ASM_WRITTEN (type))
24518 return;
24520 tree name = TYPE_NAME (type);
24521 tree origin = decl_ultimate_origin (name);
24522 if (origin != NULL && origin != name)
24524 gen_decl_die (origin, NULL, NULL, context_die);
24525 return;
24528 /* Prevent broken recursion; we can't hand off to the same type. */
24529 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24531 /* Give typedefs the right scope. */
24532 context_die = scope_die_for (type, context_die);
24534 TREE_ASM_WRITTEN (type) = 1;
24536 gen_decl_die (name, NULL, NULL, context_die);
24537 return;
24540 /* If type is an anonymous tagged type named by a typedef, let's
24541 generate debug info for the typedef. */
24542 if (is_naming_typedef_decl (TYPE_NAME (type)))
24544 /* Use the DIE of the containing namespace as the parent DIE of
24545 the type description DIE we want to generate. */
24546 if (DECL_CONTEXT (TYPE_NAME (type))
24547 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24548 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24550 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24551 return;
24554 if (lang_hooks.types.get_debug_type)
24556 tree debug_type = lang_hooks.types.get_debug_type (type);
24558 if (debug_type != NULL_TREE && debug_type != type)
24560 gen_type_die_with_usage (debug_type, context_die, usage);
24561 return;
24565 /* We are going to output a DIE to represent the unqualified version
24566 of this type (i.e. without any const or volatile qualifiers) so
24567 get the main variant (i.e. the unqualified version) of this type
24568 now. (Vectors and arrays are special because the debugging info is in the
24569 cloned type itself. Similarly function/method types can contain extra
24570 ref-qualification). */
24571 if (TREE_CODE (type) == FUNCTION_TYPE
24572 || TREE_CODE (type) == METHOD_TYPE)
24574 /* For function/method types, can't use type_main_variant here,
24575 because that can have different ref-qualifiers for C++,
24576 but try to canonicalize. */
24577 tree main = TYPE_MAIN_VARIANT (type);
24578 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24579 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24580 && check_base_type (t, main)
24581 && check_lang_type (t, type))
24583 type = t;
24584 break;
24587 else if (TREE_CODE (type) != VECTOR_TYPE
24588 && TREE_CODE (type) != ARRAY_TYPE)
24589 type = type_main_variant (type);
24591 /* If this is an array type with hidden descriptor, handle it first. */
24592 if (!TREE_ASM_WRITTEN (type)
24593 && lang_hooks.types.get_array_descr_info)
24595 memset (&info, 0, sizeof (info));
24596 if (lang_hooks.types.get_array_descr_info (type, &info))
24598 /* Fortran sometimes emits array types with no dimension. */
24599 gcc_assert (info.ndimensions >= 0
24600 && (info.ndimensions
24601 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24602 gen_descr_array_type_die (type, &info, context_die);
24603 TREE_ASM_WRITTEN (type) = 1;
24604 return;
24608 if (TREE_ASM_WRITTEN (type))
24610 /* Variable-length types may be incomplete even if
24611 TREE_ASM_WRITTEN. For such types, fall through to
24612 gen_array_type_die() and possibly fill in
24613 DW_AT_{upper,lower}_bound attributes. */
24614 if ((TREE_CODE (type) != ARRAY_TYPE
24615 && TREE_CODE (type) != RECORD_TYPE
24616 && TREE_CODE (type) != UNION_TYPE
24617 && TREE_CODE (type) != QUAL_UNION_TYPE)
24618 || !variably_modified_type_p (type, NULL))
24619 return;
24622 switch (TREE_CODE (type))
24624 case ERROR_MARK:
24625 break;
24627 case POINTER_TYPE:
24628 case REFERENCE_TYPE:
24629 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24630 ensures that the gen_type_die recursion will terminate even if the
24631 type is recursive. Recursive types are possible in Ada. */
24632 /* ??? We could perhaps do this for all types before the switch
24633 statement. */
24634 TREE_ASM_WRITTEN (type) = 1;
24636 /* For these types, all that is required is that we output a DIE (or a
24637 set of DIEs) to represent the "basis" type. */
24638 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24639 DINFO_USAGE_IND_USE);
24640 break;
24642 case OFFSET_TYPE:
24643 /* This code is used for C++ pointer-to-data-member types.
24644 Output a description of the relevant class type. */
24645 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24646 DINFO_USAGE_IND_USE);
24648 /* Output a description of the type of the object pointed to. */
24649 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24650 DINFO_USAGE_IND_USE);
24652 /* Now output a DIE to represent this pointer-to-data-member type
24653 itself. */
24654 gen_ptr_to_mbr_type_die (type, context_die);
24655 break;
24657 case FUNCTION_TYPE:
24658 /* Force out return type (in case it wasn't forced out already). */
24659 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24660 DINFO_USAGE_DIR_USE);
24661 gen_subroutine_type_die (type, context_die);
24662 break;
24664 case METHOD_TYPE:
24665 /* Force out return type (in case it wasn't forced out already). */
24666 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24667 DINFO_USAGE_DIR_USE);
24668 gen_subroutine_type_die (type, context_die);
24669 break;
24671 case ARRAY_TYPE:
24672 case VECTOR_TYPE:
24673 gen_array_type_die (type, context_die);
24674 break;
24676 case ENUMERAL_TYPE:
24677 case RECORD_TYPE:
24678 case UNION_TYPE:
24679 case QUAL_UNION_TYPE:
24680 gen_tagged_type_die (type, context_die, usage);
24681 return;
24683 case VOID_TYPE:
24684 case INTEGER_TYPE:
24685 case REAL_TYPE:
24686 case FIXED_POINT_TYPE:
24687 case COMPLEX_TYPE:
24688 case BOOLEAN_TYPE:
24689 case POINTER_BOUNDS_TYPE:
24690 /* No DIEs needed for fundamental types. */
24691 break;
24693 case NULLPTR_TYPE:
24694 case LANG_TYPE:
24695 /* Just use DW_TAG_unspecified_type. */
24697 dw_die_ref type_die = lookup_type_die (type);
24698 if (type_die == NULL)
24700 tree name = TYPE_IDENTIFIER (type);
24701 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24702 type);
24703 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24704 equate_type_number_to_die (type, type_die);
24707 break;
24709 default:
24710 if (is_cxx_auto (type))
24712 tree name = TYPE_IDENTIFIER (type);
24713 dw_die_ref *die = (name == get_identifier ("auto")
24714 ? &auto_die : &decltype_auto_die);
24715 if (!*die)
24717 *die = new_die (DW_TAG_unspecified_type,
24718 comp_unit_die (), NULL_TREE);
24719 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24721 equate_type_number_to_die (type, *die);
24722 break;
24724 gcc_unreachable ();
24727 TREE_ASM_WRITTEN (type) = 1;
24730 static void
24731 gen_type_die (tree type, dw_die_ref context_die)
24733 if (type != error_mark_node)
24735 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24736 if (flag_checking)
24738 dw_die_ref die = lookup_type_die (type);
24739 if (die)
24740 check_die (die);
24745 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24746 things which are local to the given block. */
24748 static void
24749 gen_block_die (tree stmt, dw_die_ref context_die)
24751 int must_output_die = 0;
24752 bool inlined_func;
24754 /* Ignore blocks that are NULL. */
24755 if (stmt == NULL_TREE)
24756 return;
24758 inlined_func = inlined_function_outer_scope_p (stmt);
24760 /* If the block is one fragment of a non-contiguous block, do not
24761 process the variables, since they will have been done by the
24762 origin block. Do process subblocks. */
24763 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24765 tree sub;
24767 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24768 gen_block_die (sub, context_die);
24770 return;
24773 /* Determine if we need to output any Dwarf DIEs at all to represent this
24774 block. */
24775 if (inlined_func)
24776 /* The outer scopes for inlinings *must* always be represented. We
24777 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24778 must_output_die = 1;
24779 else
24781 /* Determine if this block directly contains any "significant"
24782 local declarations which we will need to output DIEs for. */
24783 if (debug_info_level > DINFO_LEVEL_TERSE)
24784 /* We are not in terse mode so *any* local declaration counts
24785 as being a "significant" one. */
24786 must_output_die = ((BLOCK_VARS (stmt) != NULL
24787 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24788 && (TREE_USED (stmt)
24789 || TREE_ASM_WRITTEN (stmt)
24790 || BLOCK_ABSTRACT (stmt)));
24791 else if ((TREE_USED (stmt)
24792 || TREE_ASM_WRITTEN (stmt)
24793 || BLOCK_ABSTRACT (stmt))
24794 && !dwarf2out_ignore_block (stmt))
24795 must_output_die = 1;
24798 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24799 DIE for any block which contains no significant local declarations at
24800 all. Rather, in such cases we just call `decls_for_scope' so that any
24801 needed Dwarf info for any sub-blocks will get properly generated. Note
24802 that in terse mode, our definition of what constitutes a "significant"
24803 local declaration gets restricted to include only inlined function
24804 instances and local (nested) function definitions. */
24805 if (must_output_die)
24807 if (inlined_func)
24809 /* If STMT block is abstract, that means we have been called
24810 indirectly from dwarf2out_abstract_function.
24811 That function rightfully marks the descendent blocks (of
24812 the abstract function it is dealing with) as being abstract,
24813 precisely to prevent us from emitting any
24814 DW_TAG_inlined_subroutine DIE as a descendent
24815 of an abstract function instance. So in that case, we should
24816 not call gen_inlined_subroutine_die.
24818 Later though, when cgraph asks dwarf2out to emit info
24819 for the concrete instance of the function decl into which
24820 the concrete instance of STMT got inlined, the later will lead
24821 to the generation of a DW_TAG_inlined_subroutine DIE. */
24822 if (! BLOCK_ABSTRACT (stmt))
24823 gen_inlined_subroutine_die (stmt, context_die);
24825 else
24826 gen_lexical_block_die (stmt, context_die);
24828 else
24829 decls_for_scope (stmt, context_die);
24832 /* Process variable DECL (or variable with origin ORIGIN) within
24833 block STMT and add it to CONTEXT_DIE. */
24834 static void
24835 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24837 dw_die_ref die;
24838 tree decl_or_origin = decl ? decl : origin;
24840 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24841 die = lookup_decl_die (decl_or_origin);
24842 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24844 if (TYPE_DECL_IS_STUB (decl_or_origin))
24845 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24846 else
24847 die = lookup_decl_die (decl_or_origin);
24848 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24849 if (! die && ! early_dwarf)
24850 return;
24852 else
24853 die = NULL;
24855 /* Avoid creating DIEs for local typedefs and concrete static variables that
24856 will only be pruned later. */
24857 if ((origin || decl_ultimate_origin (decl))
24858 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24859 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24861 origin = decl_ultimate_origin (decl_or_origin);
24862 if (decl && VAR_P (decl) && die != NULL)
24864 die = lookup_decl_die (origin);
24865 if (die != NULL)
24866 equate_decl_number_to_die (decl, die);
24868 return;
24871 if (die != NULL && die->die_parent == NULL)
24872 add_child_die (context_die, die);
24873 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24875 if (early_dwarf)
24876 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24877 stmt, context_die);
24879 else
24881 if (decl && DECL_P (decl))
24883 die = lookup_decl_die (decl);
24885 /* Early created DIEs do not have a parent as the decls refer
24886 to the function as DECL_CONTEXT rather than the BLOCK. */
24887 if (die && die->die_parent == NULL)
24889 gcc_assert (in_lto_p);
24890 add_child_die (context_die, die);
24894 gen_decl_die (decl, origin, NULL, context_die);
24898 /* Generate all of the decls declared within a given scope and (recursively)
24899 all of its sub-blocks. */
24901 static void
24902 decls_for_scope (tree stmt, dw_die_ref context_die)
24904 tree decl;
24905 unsigned int i;
24906 tree subblocks;
24908 /* Ignore NULL blocks. */
24909 if (stmt == NULL_TREE)
24910 return;
24912 /* Output the DIEs to represent all of the data objects and typedefs
24913 declared directly within this block but not within any nested
24914 sub-blocks. Also, nested function and tag DIEs have been
24915 generated with a parent of NULL; fix that up now. We don't
24916 have to do this if we're at -g1. */
24917 if (debug_info_level > DINFO_LEVEL_TERSE)
24919 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24920 process_scope_var (stmt, decl, NULL_TREE, context_die);
24921 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24922 origin - avoid doing this twice as we have no good way to see
24923 if we've done it once already. */
24924 if (! early_dwarf)
24925 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24927 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24928 if (decl == current_function_decl)
24929 /* Ignore declarations of the current function, while they
24930 are declarations, gen_subprogram_die would treat them
24931 as definitions again, because they are equal to
24932 current_function_decl and endlessly recurse. */;
24933 else if (TREE_CODE (decl) == FUNCTION_DECL)
24934 process_scope_var (stmt, decl, NULL_TREE, context_die);
24935 else
24936 process_scope_var (stmt, NULL_TREE, decl, context_die);
24940 /* Even if we're at -g1, we need to process the subblocks in order to get
24941 inlined call information. */
24943 /* Output the DIEs to represent all sub-blocks (and the items declared
24944 therein) of this block. */
24945 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24946 subblocks != NULL;
24947 subblocks = BLOCK_CHAIN (subblocks))
24948 gen_block_die (subblocks, context_die);
24951 /* Is this a typedef we can avoid emitting? */
24953 bool
24954 is_redundant_typedef (const_tree decl)
24956 if (TYPE_DECL_IS_STUB (decl))
24957 return true;
24959 if (DECL_ARTIFICIAL (decl)
24960 && DECL_CONTEXT (decl)
24961 && is_tagged_type (DECL_CONTEXT (decl))
24962 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24963 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24964 /* Also ignore the artificial member typedef for the class name. */
24965 return true;
24967 return false;
24970 /* Return TRUE if TYPE is a typedef that names a type for linkage
24971 purposes. This kind of typedefs is produced by the C++ FE for
24972 constructs like:
24974 typedef struct {...} foo;
24976 In that case, there is no typedef variant type produced for foo.
24977 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24978 struct type. */
24980 static bool
24981 is_naming_typedef_decl (const_tree decl)
24983 if (decl == NULL_TREE
24984 || TREE_CODE (decl) != TYPE_DECL
24985 || DECL_NAMELESS (decl)
24986 || !is_tagged_type (TREE_TYPE (decl))
24987 || DECL_IS_BUILTIN (decl)
24988 || is_redundant_typedef (decl)
24989 /* It looks like Ada produces TYPE_DECLs that are very similar
24990 to C++ naming typedefs but that have different
24991 semantics. Let's be specific to c++ for now. */
24992 || !is_cxx (decl))
24993 return FALSE;
24995 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24996 && TYPE_NAME (TREE_TYPE (decl)) == decl
24997 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24998 != TYPE_NAME (TREE_TYPE (decl))));
25001 /* Looks up the DIE for a context. */
25003 static inline dw_die_ref
25004 lookup_context_die (tree context)
25006 if (context)
25008 /* Find die that represents this context. */
25009 if (TYPE_P (context))
25011 context = TYPE_MAIN_VARIANT (context);
25012 dw_die_ref ctx = lookup_type_die (context);
25013 if (!ctx)
25014 return NULL;
25015 return strip_naming_typedef (context, ctx);
25017 else
25018 return lookup_decl_die (context);
25020 return comp_unit_die ();
25023 /* Returns the DIE for a context. */
25025 static inline dw_die_ref
25026 get_context_die (tree context)
25028 if (context)
25030 /* Find die that represents this context. */
25031 if (TYPE_P (context))
25033 context = TYPE_MAIN_VARIANT (context);
25034 return strip_naming_typedef (context, force_type_die (context));
25036 else
25037 return force_decl_die (context);
25039 return comp_unit_die ();
25042 /* Returns the DIE for decl. A DIE will always be returned. */
25044 static dw_die_ref
25045 force_decl_die (tree decl)
25047 dw_die_ref decl_die;
25048 unsigned saved_external_flag;
25049 tree save_fn = NULL_TREE;
25050 decl_die = lookup_decl_die (decl);
25051 if (!decl_die)
25053 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25055 decl_die = lookup_decl_die (decl);
25056 if (decl_die)
25057 return decl_die;
25059 switch (TREE_CODE (decl))
25061 case FUNCTION_DECL:
25062 /* Clear current_function_decl, so that gen_subprogram_die thinks
25063 that this is a declaration. At this point, we just want to force
25064 declaration die. */
25065 save_fn = current_function_decl;
25066 current_function_decl = NULL_TREE;
25067 gen_subprogram_die (decl, context_die);
25068 current_function_decl = save_fn;
25069 break;
25071 case VAR_DECL:
25072 /* Set external flag to force declaration die. Restore it after
25073 gen_decl_die() call. */
25074 saved_external_flag = DECL_EXTERNAL (decl);
25075 DECL_EXTERNAL (decl) = 1;
25076 gen_decl_die (decl, NULL, NULL, context_die);
25077 DECL_EXTERNAL (decl) = saved_external_flag;
25078 break;
25080 case NAMESPACE_DECL:
25081 if (dwarf_version >= 3 || !dwarf_strict)
25082 dwarf2out_decl (decl);
25083 else
25084 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25085 decl_die = comp_unit_die ();
25086 break;
25088 case TRANSLATION_UNIT_DECL:
25089 decl_die = comp_unit_die ();
25090 break;
25092 default:
25093 gcc_unreachable ();
25096 /* We should be able to find the DIE now. */
25097 if (!decl_die)
25098 decl_die = lookup_decl_die (decl);
25099 gcc_assert (decl_die);
25102 return decl_die;
25105 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25106 always returned. */
25108 static dw_die_ref
25109 force_type_die (tree type)
25111 dw_die_ref type_die;
25113 type_die = lookup_type_die (type);
25114 if (!type_die)
25116 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25118 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25119 false, context_die);
25120 gcc_assert (type_die);
25122 return type_die;
25125 /* Force out any required namespaces to be able to output DECL,
25126 and return the new context_die for it, if it's changed. */
25128 static dw_die_ref
25129 setup_namespace_context (tree thing, dw_die_ref context_die)
25131 tree context = (DECL_P (thing)
25132 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25133 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25134 /* Force out the namespace. */
25135 context_die = force_decl_die (context);
25137 return context_die;
25140 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25141 type) within its namespace, if appropriate.
25143 For compatibility with older debuggers, namespace DIEs only contain
25144 declarations; all definitions are emitted at CU scope, with
25145 DW_AT_specification pointing to the declaration (like with class
25146 members). */
25148 static dw_die_ref
25149 declare_in_namespace (tree thing, dw_die_ref context_die)
25151 dw_die_ref ns_context;
25153 if (debug_info_level <= DINFO_LEVEL_TERSE)
25154 return context_die;
25156 /* External declarations in the local scope only need to be emitted
25157 once, not once in the namespace and once in the scope.
25159 This avoids declaring the `extern' below in the
25160 namespace DIE as well as in the innermost scope:
25162 namespace S
25164 int i=5;
25165 int foo()
25167 int i=8;
25168 extern int i;
25169 return i;
25173 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25174 return context_die;
25176 /* If this decl is from an inlined function, then don't try to emit it in its
25177 namespace, as we will get confused. It would have already been emitted
25178 when the abstract instance of the inline function was emitted anyways. */
25179 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25180 return context_die;
25182 ns_context = setup_namespace_context (thing, context_die);
25184 if (ns_context != context_die)
25186 if (is_fortran ())
25187 return ns_context;
25188 if (DECL_P (thing))
25189 gen_decl_die (thing, NULL, NULL, ns_context);
25190 else
25191 gen_type_die (thing, ns_context);
25193 return context_die;
25196 /* Generate a DIE for a namespace or namespace alias. */
25198 static void
25199 gen_namespace_die (tree decl, dw_die_ref context_die)
25201 dw_die_ref namespace_die;
25203 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25204 they are an alias of. */
25205 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25207 /* Output a real namespace or module. */
25208 context_die = setup_namespace_context (decl, comp_unit_die ());
25209 namespace_die = new_die (is_fortran ()
25210 ? DW_TAG_module : DW_TAG_namespace,
25211 context_die, decl);
25212 /* For Fortran modules defined in different CU don't add src coords. */
25213 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25215 const char *name = dwarf2_name (decl, 0);
25216 if (name)
25217 add_name_attribute (namespace_die, name);
25219 else
25220 add_name_and_src_coords_attributes (namespace_die, decl);
25221 if (DECL_EXTERNAL (decl))
25222 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25223 equate_decl_number_to_die (decl, namespace_die);
25225 else
25227 /* Output a namespace alias. */
25229 /* Force out the namespace we are an alias of, if necessary. */
25230 dw_die_ref origin_die
25231 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25233 if (DECL_FILE_SCOPE_P (decl)
25234 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25235 context_die = setup_namespace_context (decl, comp_unit_die ());
25236 /* Now create the namespace alias DIE. */
25237 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25238 add_name_and_src_coords_attributes (namespace_die, decl);
25239 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25240 equate_decl_number_to_die (decl, namespace_die);
25242 if ((dwarf_version >= 5 || !dwarf_strict)
25243 && lang_hooks.decls.decl_dwarf_attribute (decl,
25244 DW_AT_export_symbols) == 1)
25245 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25247 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25248 if (want_pubnames ())
25249 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25252 /* Generate Dwarf debug information for a decl described by DECL.
25253 The return value is currently only meaningful for PARM_DECLs,
25254 for all other decls it returns NULL.
25256 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25257 It can be NULL otherwise. */
25259 static dw_die_ref
25260 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25261 dw_die_ref context_die)
25263 tree decl_or_origin = decl ? decl : origin;
25264 tree class_origin = NULL, ultimate_origin;
25266 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25267 return NULL;
25269 /* Ignore pointer bounds decls. */
25270 if (DECL_P (decl_or_origin)
25271 && TREE_TYPE (decl_or_origin)
25272 && POINTER_BOUNDS_P (decl_or_origin))
25273 return NULL;
25275 switch (TREE_CODE (decl_or_origin))
25277 case ERROR_MARK:
25278 break;
25280 case CONST_DECL:
25281 if (!is_fortran () && !is_ada ())
25283 /* The individual enumerators of an enum type get output when we output
25284 the Dwarf representation of the relevant enum type itself. */
25285 break;
25288 /* Emit its type. */
25289 gen_type_die (TREE_TYPE (decl), context_die);
25291 /* And its containing namespace. */
25292 context_die = declare_in_namespace (decl, context_die);
25294 gen_const_die (decl, context_die);
25295 break;
25297 case FUNCTION_DECL:
25298 #if 0
25299 /* FIXME */
25300 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25301 on local redeclarations of global functions. That seems broken. */
25302 if (current_function_decl != decl)
25303 /* This is only a declaration. */;
25304 #endif
25306 /* We should have abstract copies already and should not generate
25307 stray type DIEs in late LTO dumping. */
25308 if (! early_dwarf)
25311 /* If we're emitting a clone, emit info for the abstract instance. */
25312 else if (origin || DECL_ORIGIN (decl) != decl)
25313 dwarf2out_abstract_function (origin
25314 ? DECL_ORIGIN (origin)
25315 : DECL_ABSTRACT_ORIGIN (decl));
25317 /* If we're emitting a possibly inlined function emit it as
25318 abstract instance. */
25319 else if (cgraph_function_possibly_inlined_p (decl)
25320 && ! DECL_ABSTRACT_P (decl)
25321 && ! class_or_namespace_scope_p (context_die)
25322 /* dwarf2out_abstract_function won't emit a die if this is just
25323 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25324 that case, because that works only if we have a die. */
25325 && DECL_INITIAL (decl) != NULL_TREE)
25326 dwarf2out_abstract_function (decl);
25328 /* Otherwise we're emitting the primary DIE for this decl. */
25329 else if (debug_info_level > DINFO_LEVEL_TERSE)
25331 /* Before we describe the FUNCTION_DECL itself, make sure that we
25332 have its containing type. */
25333 if (!origin)
25334 origin = decl_class_context (decl);
25335 if (origin != NULL_TREE)
25336 gen_type_die (origin, context_die);
25338 /* And its return type. */
25339 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25341 /* And its virtual context. */
25342 if (DECL_VINDEX (decl) != NULL_TREE)
25343 gen_type_die (DECL_CONTEXT (decl), context_die);
25345 /* Make sure we have a member DIE for decl. */
25346 if (origin != NULL_TREE)
25347 gen_type_die_for_member (origin, decl, context_die);
25349 /* And its containing namespace. */
25350 context_die = declare_in_namespace (decl, context_die);
25353 /* Now output a DIE to represent the function itself. */
25354 if (decl)
25355 gen_subprogram_die (decl, context_die);
25356 break;
25358 case TYPE_DECL:
25359 /* If we are in terse mode, don't generate any DIEs to represent any
25360 actual typedefs. */
25361 if (debug_info_level <= DINFO_LEVEL_TERSE)
25362 break;
25364 /* In the special case of a TYPE_DECL node representing the declaration
25365 of some type tag, if the given TYPE_DECL is marked as having been
25366 instantiated from some other (original) TYPE_DECL node (e.g. one which
25367 was generated within the original definition of an inline function) we
25368 used to generate a special (abbreviated) DW_TAG_structure_type,
25369 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25370 should be actually referencing those DIEs, as variable DIEs with that
25371 type would be emitted already in the abstract origin, so it was always
25372 removed during unused type prunning. Don't add anything in this
25373 case. */
25374 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25375 break;
25377 if (is_redundant_typedef (decl))
25378 gen_type_die (TREE_TYPE (decl), context_die);
25379 else
25380 /* Output a DIE to represent the typedef itself. */
25381 gen_typedef_die (decl, context_die);
25382 break;
25384 case LABEL_DECL:
25385 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25386 gen_label_die (decl, context_die);
25387 break;
25389 case VAR_DECL:
25390 case RESULT_DECL:
25391 /* If we are in terse mode, don't generate any DIEs to represent any
25392 variable declarations or definitions. */
25393 if (debug_info_level <= DINFO_LEVEL_TERSE)
25394 break;
25396 /* Avoid generating stray type DIEs during late dwarf dumping.
25397 All types have been dumped early. */
25398 if (early_dwarf
25399 /* ??? But in LTRANS we cannot annotate early created variably
25400 modified type DIEs without copying them and adjusting all
25401 references to them. Dump them again as happens for inlining
25402 which copies both the decl and the types. */
25403 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25404 in VLA bound information for example. */
25405 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25406 current_function_decl)))
25408 /* Output any DIEs that are needed to specify the type of this data
25409 object. */
25410 if (decl_by_reference_p (decl_or_origin))
25411 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25412 else
25413 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25416 if (early_dwarf)
25418 /* And its containing type. */
25419 class_origin = decl_class_context (decl_or_origin);
25420 if (class_origin != NULL_TREE)
25421 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25423 /* And its containing namespace. */
25424 context_die = declare_in_namespace (decl_or_origin, context_die);
25427 /* Now output the DIE to represent the data object itself. This gets
25428 complicated because of the possibility that the VAR_DECL really
25429 represents an inlined instance of a formal parameter for an inline
25430 function. */
25431 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25432 if (ultimate_origin != NULL_TREE
25433 && TREE_CODE (ultimate_origin) == PARM_DECL)
25434 gen_formal_parameter_die (decl, origin,
25435 true /* Emit name attribute. */,
25436 context_die);
25437 else
25438 gen_variable_die (decl, origin, context_die);
25439 break;
25441 case FIELD_DECL:
25442 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25443 /* Ignore the nameless fields that are used to skip bits but handle C++
25444 anonymous unions and structs. */
25445 if (DECL_NAME (decl) != NULL_TREE
25446 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25447 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25449 gen_type_die (member_declared_type (decl), context_die);
25450 gen_field_die (decl, ctx, context_die);
25452 break;
25454 case PARM_DECL:
25455 /* Avoid generating stray type DIEs during late dwarf dumping.
25456 All types have been dumped early. */
25457 if (early_dwarf
25458 /* ??? But in LTRANS we cannot annotate early created variably
25459 modified type DIEs without copying them and adjusting all
25460 references to them. Dump them again as happens for inlining
25461 which copies both the decl and the types. */
25462 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25463 in VLA bound information for example. */
25464 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25465 current_function_decl)))
25467 if (DECL_BY_REFERENCE (decl_or_origin))
25468 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25469 else
25470 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25472 return gen_formal_parameter_die (decl, origin,
25473 true /* Emit name attribute. */,
25474 context_die);
25476 case NAMESPACE_DECL:
25477 if (dwarf_version >= 3 || !dwarf_strict)
25478 gen_namespace_die (decl, context_die);
25479 break;
25481 case IMPORTED_DECL:
25482 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25483 DECL_CONTEXT (decl), context_die);
25484 break;
25486 case NAMELIST_DECL:
25487 gen_namelist_decl (DECL_NAME (decl), context_die,
25488 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25489 break;
25491 default:
25492 /* Probably some frontend-internal decl. Assume we don't care. */
25493 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25494 break;
25497 return NULL;
25500 /* Output initial debug information for global DECL. Called at the
25501 end of the parsing process.
25503 This is the initial debug generation process. As such, the DIEs
25504 generated may be incomplete. A later debug generation pass
25505 (dwarf2out_late_global_decl) will augment the information generated
25506 in this pass (e.g., with complete location info). */
25508 static void
25509 dwarf2out_early_global_decl (tree decl)
25511 set_early_dwarf s;
25513 /* gen_decl_die() will set DECL_ABSTRACT because
25514 cgraph_function_possibly_inlined_p() returns true. This is in
25515 turn will cause DW_AT_inline attributes to be set.
25517 This happens because at early dwarf generation, there is no
25518 cgraph information, causing cgraph_function_possibly_inlined_p()
25519 to return true. Trick cgraph_function_possibly_inlined_p()
25520 while we generate dwarf early. */
25521 bool save = symtab->global_info_ready;
25522 symtab->global_info_ready = true;
25524 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25525 other DECLs and they can point to template types or other things
25526 that dwarf2out can't handle when done via dwarf2out_decl. */
25527 if (TREE_CODE (decl) != TYPE_DECL
25528 && TREE_CODE (decl) != PARM_DECL)
25530 if (TREE_CODE (decl) == FUNCTION_DECL)
25532 tree save_fndecl = current_function_decl;
25534 /* For nested functions, make sure we have DIEs for the parents first
25535 so that all nested DIEs are generated at the proper scope in the
25536 first shot. */
25537 tree context = decl_function_context (decl);
25538 if (context != NULL)
25540 dw_die_ref context_die = lookup_decl_die (context);
25541 current_function_decl = context;
25543 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25544 enough so that it lands in its own context. This avoids type
25545 pruning issues later on. */
25546 if (context_die == NULL || is_declaration_die (context_die))
25547 dwarf2out_decl (context);
25550 /* Emit an abstract origin of a function first. This happens
25551 with C++ constructor clones for example and makes
25552 dwarf2out_abstract_function happy which requires the early
25553 DIE of the abstract instance to be present. */
25554 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25555 dw_die_ref origin_die;
25556 if (origin != NULL
25557 /* Do not emit the DIE multiple times but make sure to
25558 process it fully here in case we just saw a declaration. */
25559 && ((origin_die = lookup_decl_die (origin)) == NULL
25560 || is_declaration_die (origin_die)))
25562 current_function_decl = origin;
25563 dwarf2out_decl (origin);
25566 /* Emit the DIE for decl but avoid doing that multiple times. */
25567 dw_die_ref old_die;
25568 if ((old_die = lookup_decl_die (decl)) == NULL
25569 || is_declaration_die (old_die))
25571 current_function_decl = decl;
25572 dwarf2out_decl (decl);
25575 current_function_decl = save_fndecl;
25577 else
25578 dwarf2out_decl (decl);
25580 symtab->global_info_ready = save;
25583 /* Output debug information for global decl DECL. Called from
25584 toplev.c after compilation proper has finished. */
25586 static void
25587 dwarf2out_late_global_decl (tree decl)
25589 /* Fill-in any location information we were unable to determine
25590 on the first pass. */
25591 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25593 dw_die_ref die = lookup_decl_die (decl);
25595 /* We may have to generate early debug late for LTO in case debug
25596 was not enabled at compile-time or the target doesn't support
25597 the LTO early debug scheme. */
25598 if (! die && in_lto_p)
25600 dwarf2out_decl (decl);
25601 die = lookup_decl_die (decl);
25604 if (die)
25606 /* We get called via the symtab code invoking late_global_decl
25607 for symbols that are optimized out. Do not add locations
25608 for those, except if they have a DECL_VALUE_EXPR, in which case
25609 they are relevant for debuggers. */
25610 varpool_node *node = varpool_node::get (decl);
25611 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25612 tree_add_const_value_attribute_for_decl (die, decl);
25613 else
25614 add_location_or_const_value_attribute (die, decl, false);
25619 /* Output debug information for type decl DECL. Called from toplev.c
25620 and from language front ends (to record built-in types). */
25621 static void
25622 dwarf2out_type_decl (tree decl, int local)
25624 if (!local)
25626 set_early_dwarf s;
25627 dwarf2out_decl (decl);
25631 /* Output debug information for imported module or decl DECL.
25632 NAME is non-NULL name in the lexical block if the decl has been renamed.
25633 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25634 that DECL belongs to.
25635 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25636 static void
25637 dwarf2out_imported_module_or_decl_1 (tree decl,
25638 tree name,
25639 tree lexical_block,
25640 dw_die_ref lexical_block_die)
25642 expanded_location xloc;
25643 dw_die_ref imported_die = NULL;
25644 dw_die_ref at_import_die;
25646 if (TREE_CODE (decl) == IMPORTED_DECL)
25648 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25649 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25650 gcc_assert (decl);
25652 else
25653 xloc = expand_location (input_location);
25655 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25657 at_import_die = force_type_die (TREE_TYPE (decl));
25658 /* For namespace N { typedef void T; } using N::T; base_type_die
25659 returns NULL, but DW_TAG_imported_declaration requires
25660 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25661 if (!at_import_die)
25663 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25664 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25665 at_import_die = lookup_type_die (TREE_TYPE (decl));
25666 gcc_assert (at_import_die);
25669 else
25671 at_import_die = lookup_decl_die (decl);
25672 if (!at_import_die)
25674 /* If we're trying to avoid duplicate debug info, we may not have
25675 emitted the member decl for this field. Emit it now. */
25676 if (TREE_CODE (decl) == FIELD_DECL)
25678 tree type = DECL_CONTEXT (decl);
25680 if (TYPE_CONTEXT (type)
25681 && TYPE_P (TYPE_CONTEXT (type))
25682 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25683 DINFO_USAGE_DIR_USE))
25684 return;
25685 gen_type_die_for_member (type, decl,
25686 get_context_die (TYPE_CONTEXT (type)));
25688 if (TREE_CODE (decl) == NAMELIST_DECL)
25689 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25690 get_context_die (DECL_CONTEXT (decl)),
25691 NULL_TREE);
25692 else
25693 at_import_die = force_decl_die (decl);
25697 if (TREE_CODE (decl) == NAMESPACE_DECL)
25699 if (dwarf_version >= 3 || !dwarf_strict)
25700 imported_die = new_die (DW_TAG_imported_module,
25701 lexical_block_die,
25702 lexical_block);
25703 else
25704 return;
25706 else
25707 imported_die = new_die (DW_TAG_imported_declaration,
25708 lexical_block_die,
25709 lexical_block);
25711 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25712 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25713 if (debug_column_info && xloc.column)
25714 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25715 if (name)
25716 add_AT_string (imported_die, DW_AT_name,
25717 IDENTIFIER_POINTER (name));
25718 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25721 /* Output debug information for imported module or decl DECL.
25722 NAME is non-NULL name in context if the decl has been renamed.
25723 CHILD is true if decl is one of the renamed decls as part of
25724 importing whole module.
25725 IMPLICIT is set if this hook is called for an implicit import
25726 such as inline namespace. */
25728 static void
25729 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25730 bool child, bool implicit)
25732 /* dw_die_ref at_import_die; */
25733 dw_die_ref scope_die;
25735 if (debug_info_level <= DINFO_LEVEL_TERSE)
25736 return;
25738 gcc_assert (decl);
25740 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25741 should be enough, for DWARF4 and older even if we emit as extension
25742 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25743 for the benefit of consumers unaware of DW_AT_export_symbols. */
25744 if (implicit
25745 && dwarf_version >= 5
25746 && lang_hooks.decls.decl_dwarf_attribute (decl,
25747 DW_AT_export_symbols) == 1)
25748 return;
25750 set_early_dwarf s;
25752 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25753 We need decl DIE for reference and scope die. First, get DIE for the decl
25754 itself. */
25756 /* Get the scope die for decl context. Use comp_unit_die for global module
25757 or decl. If die is not found for non globals, force new die. */
25758 if (context
25759 && TYPE_P (context)
25760 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25761 return;
25763 scope_die = get_context_die (context);
25765 if (child)
25767 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25768 there is nothing we can do, here. */
25769 if (dwarf_version < 3 && dwarf_strict)
25770 return;
25772 gcc_assert (scope_die->die_child);
25773 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25774 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25775 scope_die = scope_die->die_child;
25778 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25779 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25782 /* Output debug information for namelists. */
25784 static dw_die_ref
25785 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25787 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25788 tree value;
25789 unsigned i;
25791 if (debug_info_level <= DINFO_LEVEL_TERSE)
25792 return NULL;
25794 gcc_assert (scope_die != NULL);
25795 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25796 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25798 /* If there are no item_decls, we have a nondefining namelist, e.g.
25799 with USE association; hence, set DW_AT_declaration. */
25800 if (item_decls == NULL_TREE)
25802 add_AT_flag (nml_die, DW_AT_declaration, 1);
25803 return nml_die;
25806 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25808 nml_item_ref_die = lookup_decl_die (value);
25809 if (!nml_item_ref_die)
25810 nml_item_ref_die = force_decl_die (value);
25812 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25813 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25815 return nml_die;
25819 /* Write the debugging output for DECL and return the DIE. */
25821 static void
25822 dwarf2out_decl (tree decl)
25824 dw_die_ref context_die = comp_unit_die ();
25826 switch (TREE_CODE (decl))
25828 case ERROR_MARK:
25829 return;
25831 case FUNCTION_DECL:
25832 /* If we're a nested function, initially use a parent of NULL; if we're
25833 a plain function, this will be fixed up in decls_for_scope. If
25834 we're a method, it will be ignored, since we already have a DIE. */
25835 if (decl_function_context (decl)
25836 /* But if we're in terse mode, we don't care about scope. */
25837 && debug_info_level > DINFO_LEVEL_TERSE)
25838 context_die = NULL;
25839 break;
25841 case VAR_DECL:
25842 /* For local statics lookup proper context die. */
25843 if (local_function_static (decl))
25844 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25846 /* If we are in terse mode, don't generate any DIEs to represent any
25847 variable declarations or definitions. */
25848 if (debug_info_level <= DINFO_LEVEL_TERSE)
25849 return;
25850 break;
25852 case CONST_DECL:
25853 if (debug_info_level <= DINFO_LEVEL_TERSE)
25854 return;
25855 if (!is_fortran () && !is_ada ())
25856 return;
25857 if (TREE_STATIC (decl) && decl_function_context (decl))
25858 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25859 break;
25861 case NAMESPACE_DECL:
25862 case IMPORTED_DECL:
25863 if (debug_info_level <= DINFO_LEVEL_TERSE)
25864 return;
25865 if (lookup_decl_die (decl) != NULL)
25866 return;
25867 break;
25869 case TYPE_DECL:
25870 /* Don't emit stubs for types unless they are needed by other DIEs. */
25871 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25872 return;
25874 /* Don't bother trying to generate any DIEs to represent any of the
25875 normal built-in types for the language we are compiling. */
25876 if (DECL_IS_BUILTIN (decl))
25877 return;
25879 /* If we are in terse mode, don't generate any DIEs for types. */
25880 if (debug_info_level <= DINFO_LEVEL_TERSE)
25881 return;
25883 /* If we're a function-scope tag, initially use a parent of NULL;
25884 this will be fixed up in decls_for_scope. */
25885 if (decl_function_context (decl))
25886 context_die = NULL;
25888 break;
25890 case NAMELIST_DECL:
25891 break;
25893 default:
25894 return;
25897 gen_decl_die (decl, NULL, NULL, context_die);
25899 if (flag_checking)
25901 dw_die_ref die = lookup_decl_die (decl);
25902 if (die)
25903 check_die (die);
25907 /* Write the debugging output for DECL. */
25909 static void
25910 dwarf2out_function_decl (tree decl)
25912 dwarf2out_decl (decl);
25913 call_arg_locations = NULL;
25914 call_arg_loc_last = NULL;
25915 call_site_count = -1;
25916 tail_call_site_count = -1;
25917 decl_loc_table->empty ();
25918 cached_dw_loc_list_table->empty ();
25921 /* Output a marker (i.e. a label) for the beginning of the generated code for
25922 a lexical block. */
25924 static void
25925 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25926 unsigned int blocknum)
25928 switch_to_section (current_function_section ());
25929 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25932 /* Output a marker (i.e. a label) for the end of the generated code for a
25933 lexical block. */
25935 static void
25936 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25938 switch_to_section (current_function_section ());
25939 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25942 /* Returns nonzero if it is appropriate not to emit any debugging
25943 information for BLOCK, because it doesn't contain any instructions.
25945 Don't allow this for blocks with nested functions or local classes
25946 as we would end up with orphans, and in the presence of scheduling
25947 we may end up calling them anyway. */
25949 static bool
25950 dwarf2out_ignore_block (const_tree block)
25952 tree decl;
25953 unsigned int i;
25955 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25956 if (TREE_CODE (decl) == FUNCTION_DECL
25957 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25958 return 0;
25959 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25961 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25962 if (TREE_CODE (decl) == FUNCTION_DECL
25963 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25964 return 0;
25967 return 1;
25970 /* Hash table routines for file_hash. */
25972 bool
25973 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25975 return filename_cmp (p1->filename, p2) == 0;
25978 hashval_t
25979 dwarf_file_hasher::hash (dwarf_file_data *p)
25981 return htab_hash_string (p->filename);
25984 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25985 dwarf2out.c) and return its "index". The index of each (known) filename is
25986 just a unique number which is associated with only that one filename. We
25987 need such numbers for the sake of generating labels (in the .debug_sfnames
25988 section) and references to those files numbers (in the .debug_srcinfo
25989 and .debug_macinfo sections). If the filename given as an argument is not
25990 found in our current list, add it to the list and assign it the next
25991 available unique index number. */
25993 static struct dwarf_file_data *
25994 lookup_filename (const char *file_name)
25996 struct dwarf_file_data * created;
25998 if (!file_name)
25999 return NULL;
26001 dwarf_file_data **slot
26002 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26003 INSERT);
26004 if (*slot)
26005 return *slot;
26007 created = ggc_alloc<dwarf_file_data> ();
26008 created->filename = file_name;
26009 created->emitted_number = 0;
26010 *slot = created;
26011 return created;
26014 /* If the assembler will construct the file table, then translate the compiler
26015 internal file table number into the assembler file table number, and emit
26016 a .file directive if we haven't already emitted one yet. The file table
26017 numbers are different because we prune debug info for unused variables and
26018 types, which may include filenames. */
26020 static int
26021 maybe_emit_file (struct dwarf_file_data * fd)
26023 if (! fd->emitted_number)
26025 if (last_emitted_file)
26026 fd->emitted_number = last_emitted_file->emitted_number + 1;
26027 else
26028 fd->emitted_number = 1;
26029 last_emitted_file = fd;
26031 if (DWARF2_ASM_LINE_DEBUG_INFO)
26033 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26034 output_quoted_string (asm_out_file,
26035 remap_debug_filename (fd->filename));
26036 fputc ('\n', asm_out_file);
26040 return fd->emitted_number;
26043 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26044 That generation should happen after function debug info has been
26045 generated. The value of the attribute is the constant value of ARG. */
26047 static void
26048 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26050 die_arg_entry entry;
26052 if (!die || !arg)
26053 return;
26055 gcc_assert (early_dwarf);
26057 if (!tmpl_value_parm_die_table)
26058 vec_alloc (tmpl_value_parm_die_table, 32);
26060 entry.die = die;
26061 entry.arg = arg;
26062 vec_safe_push (tmpl_value_parm_die_table, entry);
26065 /* Return TRUE if T is an instance of generic type, FALSE
26066 otherwise. */
26068 static bool
26069 generic_type_p (tree t)
26071 if (t == NULL_TREE || !TYPE_P (t))
26072 return false;
26073 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26076 /* Schedule the generation of the generic parameter dies for the
26077 instance of generic type T. The proper generation itself is later
26078 done by gen_scheduled_generic_parms_dies. */
26080 static void
26081 schedule_generic_params_dies_gen (tree t)
26083 if (!generic_type_p (t))
26084 return;
26086 gcc_assert (early_dwarf);
26088 if (!generic_type_instances)
26089 vec_alloc (generic_type_instances, 256);
26091 vec_safe_push (generic_type_instances, t);
26094 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26095 by append_entry_to_tmpl_value_parm_die_table. This function must
26096 be called after function DIEs have been generated. */
26098 static void
26099 gen_remaining_tmpl_value_param_die_attribute (void)
26101 if (tmpl_value_parm_die_table)
26103 unsigned i, j;
26104 die_arg_entry *e;
26106 /* We do this in two phases - first get the cases we can
26107 handle during early-finish, preserving those we cannot
26108 (containing symbolic constants where we don't yet know
26109 whether we are going to output the referenced symbols).
26110 For those we try again at late-finish. */
26111 j = 0;
26112 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26114 if (!e->die->removed
26115 && !tree_add_const_value_attribute (e->die, e->arg))
26117 dw_loc_descr_ref loc = NULL;
26118 if (! early_dwarf
26119 && (dwarf_version >= 5 || !dwarf_strict))
26120 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26121 if (loc)
26122 add_AT_loc (e->die, DW_AT_location, loc);
26123 else
26124 (*tmpl_value_parm_die_table)[j++] = *e;
26127 tmpl_value_parm_die_table->truncate (j);
26131 /* Generate generic parameters DIEs for instances of generic types
26132 that have been previously scheduled by
26133 schedule_generic_params_dies_gen. This function must be called
26134 after all the types of the CU have been laid out. */
26136 static void
26137 gen_scheduled_generic_parms_dies (void)
26139 unsigned i;
26140 tree t;
26142 if (!generic_type_instances)
26143 return;
26145 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26146 if (COMPLETE_TYPE_P (t))
26147 gen_generic_params_dies (t);
26149 generic_type_instances = NULL;
26153 /* Replace DW_AT_name for the decl with name. */
26155 static void
26156 dwarf2out_set_name (tree decl, tree name)
26158 dw_die_ref die;
26159 dw_attr_node *attr;
26160 const char *dname;
26162 die = TYPE_SYMTAB_DIE (decl);
26163 if (!die)
26164 return;
26166 dname = dwarf2_name (name, 0);
26167 if (!dname)
26168 return;
26170 attr = get_AT (die, DW_AT_name);
26171 if (attr)
26173 struct indirect_string_node *node;
26175 node = find_AT_string (dname);
26176 /* replace the string. */
26177 attr->dw_attr_val.v.val_str = node;
26180 else
26181 add_name_attribute (die, dname);
26184 /* True if before or during processing of the first function being emitted. */
26185 static bool in_first_function_p = true;
26186 /* True if loc_note during dwarf2out_var_location call might still be
26187 before first real instruction at address equal to .Ltext0. */
26188 static bool maybe_at_text_label_p = true;
26189 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26190 static unsigned int first_loclabel_num_not_at_text_label;
26192 /* Called by the final INSN scan whenever we see a var location. We
26193 use it to drop labels in the right places, and throw the location in
26194 our lookup table. */
26196 static void
26197 dwarf2out_var_location (rtx_insn *loc_note)
26199 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26200 struct var_loc_node *newloc;
26201 rtx_insn *next_real, *next_note;
26202 rtx_insn *call_insn = NULL;
26203 static const char *last_label;
26204 static const char *last_postcall_label;
26205 static bool last_in_cold_section_p;
26206 static rtx_insn *expected_next_loc_note;
26207 tree decl;
26208 bool var_loc_p;
26210 if (!NOTE_P (loc_note))
26212 if (CALL_P (loc_note))
26214 call_site_count++;
26215 if (SIBLING_CALL_P (loc_note))
26216 tail_call_site_count++;
26217 if (optimize == 0 && !flag_var_tracking)
26219 /* When the var-tracking pass is not running, there is no note
26220 for indirect calls whose target is compile-time known. In this
26221 case, process such calls specifically so that we generate call
26222 sites for them anyway. */
26223 rtx x = PATTERN (loc_note);
26224 if (GET_CODE (x) == PARALLEL)
26225 x = XVECEXP (x, 0, 0);
26226 if (GET_CODE (x) == SET)
26227 x = SET_SRC (x);
26228 if (GET_CODE (x) == CALL)
26229 x = XEXP (x, 0);
26230 if (!MEM_P (x)
26231 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26232 || !SYMBOL_REF_DECL (XEXP (x, 0))
26233 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26234 != FUNCTION_DECL))
26236 call_insn = loc_note;
26237 loc_note = NULL;
26238 var_loc_p = false;
26240 next_real = next_real_insn (call_insn);
26241 next_note = NULL;
26242 cached_next_real_insn = NULL;
26243 goto create_label;
26247 return;
26250 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26251 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26252 return;
26254 /* Optimize processing a large consecutive sequence of location
26255 notes so we don't spend too much time in next_real_insn. If the
26256 next insn is another location note, remember the next_real_insn
26257 calculation for next time. */
26258 next_real = cached_next_real_insn;
26259 if (next_real)
26261 if (expected_next_loc_note != loc_note)
26262 next_real = NULL;
26265 next_note = NEXT_INSN (loc_note);
26266 if (! next_note
26267 || next_note->deleted ()
26268 || ! NOTE_P (next_note)
26269 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26270 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26271 next_note = NULL;
26273 if (! next_real)
26274 next_real = next_real_insn (loc_note);
26276 if (next_note)
26278 expected_next_loc_note = next_note;
26279 cached_next_real_insn = next_real;
26281 else
26282 cached_next_real_insn = NULL;
26284 /* If there are no instructions which would be affected by this note,
26285 don't do anything. */
26286 if (var_loc_p
26287 && next_real == NULL_RTX
26288 && !NOTE_DURING_CALL_P (loc_note))
26289 return;
26291 create_label:
26293 if (next_real == NULL_RTX)
26294 next_real = get_last_insn ();
26296 /* If there were any real insns between note we processed last time
26297 and this note (or if it is the first note), clear
26298 last_{,postcall_}label so that they are not reused this time. */
26299 if (last_var_location_insn == NULL_RTX
26300 || last_var_location_insn != next_real
26301 || last_in_cold_section_p != in_cold_section_p)
26303 last_label = NULL;
26304 last_postcall_label = NULL;
26307 if (var_loc_p)
26309 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26310 newloc = add_var_loc_to_decl (decl, loc_note,
26311 NOTE_DURING_CALL_P (loc_note)
26312 ? last_postcall_label : last_label);
26313 if (newloc == NULL)
26314 return;
26316 else
26318 decl = NULL_TREE;
26319 newloc = NULL;
26322 /* If there were no real insns between note we processed last time
26323 and this note, use the label we emitted last time. Otherwise
26324 create a new label and emit it. */
26325 if (last_label == NULL)
26327 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26328 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26329 loclabel_num++;
26330 last_label = ggc_strdup (loclabel);
26331 /* See if loclabel might be equal to .Ltext0. If yes,
26332 bump first_loclabel_num_not_at_text_label. */
26333 if (!have_multiple_function_sections
26334 && in_first_function_p
26335 && maybe_at_text_label_p)
26337 static rtx_insn *last_start;
26338 rtx_insn *insn;
26339 for (insn = loc_note; insn; insn = previous_insn (insn))
26340 if (insn == last_start)
26341 break;
26342 else if (!NONDEBUG_INSN_P (insn))
26343 continue;
26344 else
26346 rtx body = PATTERN (insn);
26347 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26348 continue;
26349 /* Inline asm could occupy zero bytes. */
26350 else if (GET_CODE (body) == ASM_INPUT
26351 || asm_noperands (body) >= 0)
26352 continue;
26353 #ifdef HAVE_attr_length
26354 else if (get_attr_min_length (insn) == 0)
26355 continue;
26356 #endif
26357 else
26359 /* Assume insn has non-zero length. */
26360 maybe_at_text_label_p = false;
26361 break;
26364 if (maybe_at_text_label_p)
26366 last_start = loc_note;
26367 first_loclabel_num_not_at_text_label = loclabel_num;
26372 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26373 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26375 if (!var_loc_p)
26377 struct call_arg_loc_node *ca_loc
26378 = ggc_cleared_alloc<call_arg_loc_node> ();
26379 rtx_insn *prev
26380 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26382 ca_loc->call_arg_loc_note = loc_note;
26383 ca_loc->next = NULL;
26384 ca_loc->label = last_label;
26385 gcc_assert (prev
26386 && (CALL_P (prev)
26387 || (NONJUMP_INSN_P (prev)
26388 && GET_CODE (PATTERN (prev)) == SEQUENCE
26389 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26390 if (!CALL_P (prev))
26391 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26392 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26394 /* Look for a SYMBOL_REF in the "prev" instruction. */
26395 rtx x = get_call_rtx_from (PATTERN (prev));
26396 if (x)
26398 /* Try to get the call symbol, if any. */
26399 if (MEM_P (XEXP (x, 0)))
26400 x = XEXP (x, 0);
26401 /* First, look for a memory access to a symbol_ref. */
26402 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26403 && SYMBOL_REF_DECL (XEXP (x, 0))
26404 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26405 ca_loc->symbol_ref = XEXP (x, 0);
26406 /* Otherwise, look at a compile-time known user-level function
26407 declaration. */
26408 else if (MEM_P (x)
26409 && MEM_EXPR (x)
26410 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26411 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26414 ca_loc->block = insn_scope (prev);
26415 if (call_arg_locations)
26416 call_arg_loc_last->next = ca_loc;
26417 else
26418 call_arg_locations = ca_loc;
26419 call_arg_loc_last = ca_loc;
26421 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26422 newloc->label = last_label;
26423 else
26425 if (!last_postcall_label)
26427 sprintf (loclabel, "%s-1", last_label);
26428 last_postcall_label = ggc_strdup (loclabel);
26430 newloc->label = last_postcall_label;
26433 last_var_location_insn = next_real;
26434 last_in_cold_section_p = in_cold_section_p;
26437 /* Called from finalize_size_functions for size functions so that their body
26438 can be encoded in the debug info to describe the layout of variable-length
26439 structures. */
26441 static void
26442 dwarf2out_size_function (tree decl)
26444 function_to_dwarf_procedure (decl);
26447 /* Note in one location list that text section has changed. */
26450 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26452 var_loc_list *list = *slot;
26453 if (list->first)
26454 list->last_before_switch
26455 = list->last->next ? list->last->next : list->last;
26456 return 1;
26459 /* Note in all location lists that text section has changed. */
26461 static void
26462 var_location_switch_text_section (void)
26464 if (decl_loc_table == NULL)
26465 return;
26467 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26470 /* Create a new line number table. */
26472 static dw_line_info_table *
26473 new_line_info_table (void)
26475 dw_line_info_table *table;
26477 table = ggc_cleared_alloc<dw_line_info_table> ();
26478 table->file_num = 1;
26479 table->line_num = 1;
26480 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26482 return table;
26485 /* Lookup the "current" table into which we emit line info, so
26486 that we don't have to do it for every source line. */
26488 static void
26489 set_cur_line_info_table (section *sec)
26491 dw_line_info_table *table;
26493 if (sec == text_section)
26494 table = text_section_line_info;
26495 else if (sec == cold_text_section)
26497 table = cold_text_section_line_info;
26498 if (!table)
26500 cold_text_section_line_info = table = new_line_info_table ();
26501 table->end_label = cold_end_label;
26504 else
26506 const char *end_label;
26508 if (crtl->has_bb_partition)
26510 if (in_cold_section_p)
26511 end_label = crtl->subsections.cold_section_end_label;
26512 else
26513 end_label = crtl->subsections.hot_section_end_label;
26515 else
26517 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26518 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26519 current_function_funcdef_no);
26520 end_label = ggc_strdup (label);
26523 table = new_line_info_table ();
26524 table->end_label = end_label;
26526 vec_safe_push (separate_line_info, table);
26529 if (DWARF2_ASM_LINE_DEBUG_INFO)
26530 table->is_stmt = (cur_line_info_table
26531 ? cur_line_info_table->is_stmt
26532 : DWARF_LINE_DEFAULT_IS_STMT_START);
26533 cur_line_info_table = table;
26537 /* We need to reset the locations at the beginning of each
26538 function. We can't do this in the end_function hook, because the
26539 declarations that use the locations won't have been output when
26540 that hook is called. Also compute have_multiple_function_sections here. */
26542 static void
26543 dwarf2out_begin_function (tree fun)
26545 section *sec = function_section (fun);
26547 if (sec != text_section)
26548 have_multiple_function_sections = true;
26550 if (crtl->has_bb_partition && !cold_text_section)
26552 gcc_assert (current_function_decl == fun);
26553 cold_text_section = unlikely_text_section ();
26554 switch_to_section (cold_text_section);
26555 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26556 switch_to_section (sec);
26559 dwarf2out_note_section_used ();
26560 call_site_count = 0;
26561 tail_call_site_count = 0;
26563 set_cur_line_info_table (sec);
26566 /* Helper function of dwarf2out_end_function, called only after emitting
26567 the very first function into assembly. Check if some .debug_loc range
26568 might end with a .LVL* label that could be equal to .Ltext0.
26569 In that case we must force using absolute addresses in .debug_loc ranges,
26570 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26571 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26572 list terminator.
26573 Set have_multiple_function_sections to true in that case and
26574 terminate htab traversal. */
26577 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26579 var_loc_list *entry = *slot;
26580 struct var_loc_node *node;
26582 node = entry->first;
26583 if (node && node->next && node->next->label)
26585 unsigned int i;
26586 const char *label = node->next->label;
26587 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26589 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26591 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26592 if (strcmp (label, loclabel) == 0)
26594 have_multiple_function_sections = true;
26595 return 0;
26599 return 1;
26602 /* Hook called after emitting a function into assembly.
26603 This does something only for the very first function emitted. */
26605 static void
26606 dwarf2out_end_function (unsigned int)
26608 if (in_first_function_p
26609 && !have_multiple_function_sections
26610 && first_loclabel_num_not_at_text_label
26611 && decl_loc_table)
26612 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26613 in_first_function_p = false;
26614 maybe_at_text_label_p = false;
26617 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26618 front-ends register a translation unit even before dwarf2out_init is
26619 called. */
26620 static tree main_translation_unit = NULL_TREE;
26622 /* Hook called by front-ends after they built their main translation unit.
26623 Associate comp_unit_die to UNIT. */
26625 static void
26626 dwarf2out_register_main_translation_unit (tree unit)
26628 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26629 && main_translation_unit == NULL_TREE);
26630 main_translation_unit = unit;
26631 /* If dwarf2out_init has not been called yet, it will perform the association
26632 itself looking at main_translation_unit. */
26633 if (decl_die_table != NULL)
26634 equate_decl_number_to_die (unit, comp_unit_die ());
26637 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26639 static void
26640 push_dw_line_info_entry (dw_line_info_table *table,
26641 enum dw_line_info_opcode opcode, unsigned int val)
26643 dw_line_info_entry e;
26644 e.opcode = opcode;
26645 e.val = val;
26646 vec_safe_push (table->entries, e);
26649 /* Output a label to mark the beginning of a source code line entry
26650 and record information relating to this source line, in
26651 'line_info_table' for later output of the .debug_line section. */
26652 /* ??? The discriminator parameter ought to be unsigned. */
26654 static void
26655 dwarf2out_source_line (unsigned int line, unsigned int column,
26656 const char *filename,
26657 int discriminator, bool is_stmt)
26659 unsigned int file_num;
26660 dw_line_info_table *table;
26662 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26663 return;
26665 /* The discriminator column was added in dwarf4. Simplify the below
26666 by simply removing it if we're not supposed to output it. */
26667 if (dwarf_version < 4 && dwarf_strict)
26668 discriminator = 0;
26670 if (!debug_column_info)
26671 column = 0;
26673 table = cur_line_info_table;
26674 file_num = maybe_emit_file (lookup_filename (filename));
26676 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26677 the debugger has used the second (possibly duplicate) line number
26678 at the beginning of the function to mark the end of the prologue.
26679 We could eliminate any other duplicates within the function. For
26680 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26681 that second line number entry. */
26682 /* Recall that this end-of-prologue indication is *not* the same thing
26683 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26684 to which the hook corresponds, follows the last insn that was
26685 emitted by gen_prologue. What we need is to precede the first insn
26686 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26687 insn that corresponds to something the user wrote. These may be
26688 very different locations once scheduling is enabled. */
26690 if (0 && file_num == table->file_num
26691 && line == table->line_num
26692 && column == table->column_num
26693 && discriminator == table->discrim_num
26694 && is_stmt == table->is_stmt)
26695 return;
26697 switch_to_section (current_function_section ());
26699 /* If requested, emit something human-readable. */
26700 if (flag_debug_asm)
26702 if (debug_column_info)
26703 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26704 filename, line, column);
26705 else
26706 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26707 filename, line);
26710 if (DWARF2_ASM_LINE_DEBUG_INFO)
26712 /* Emit the .loc directive understood by GNU as. */
26713 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26714 file_num, line, is_stmt, discriminator */
26715 fputs ("\t.loc ", asm_out_file);
26716 fprint_ul (asm_out_file, file_num);
26717 putc (' ', asm_out_file);
26718 fprint_ul (asm_out_file, line);
26719 putc (' ', asm_out_file);
26720 fprint_ul (asm_out_file, column);
26722 if (is_stmt != table->is_stmt)
26724 fputs (" is_stmt ", asm_out_file);
26725 putc (is_stmt ? '1' : '0', asm_out_file);
26727 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26729 gcc_assert (discriminator > 0);
26730 fputs (" discriminator ", asm_out_file);
26731 fprint_ul (asm_out_file, (unsigned long) discriminator);
26733 putc ('\n', asm_out_file);
26735 else
26737 unsigned int label_num = ++line_info_label_num;
26739 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26741 push_dw_line_info_entry (table, LI_set_address, label_num);
26742 if (file_num != table->file_num)
26743 push_dw_line_info_entry (table, LI_set_file, file_num);
26744 if (discriminator != table->discrim_num)
26745 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26746 if (is_stmt != table->is_stmt)
26747 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26748 push_dw_line_info_entry (table, LI_set_line, line);
26749 if (debug_column_info)
26750 push_dw_line_info_entry (table, LI_set_column, column);
26753 table->file_num = file_num;
26754 table->line_num = line;
26755 table->column_num = column;
26756 table->discrim_num = discriminator;
26757 table->is_stmt = is_stmt;
26758 table->in_use = true;
26761 /* Record the beginning of a new source file. */
26763 static void
26764 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26766 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26768 macinfo_entry e;
26769 e.code = DW_MACINFO_start_file;
26770 e.lineno = lineno;
26771 e.info = ggc_strdup (filename);
26772 vec_safe_push (macinfo_table, e);
26776 /* Record the end of a source file. */
26778 static void
26779 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26781 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26783 macinfo_entry e;
26784 e.code = DW_MACINFO_end_file;
26785 e.lineno = lineno;
26786 e.info = NULL;
26787 vec_safe_push (macinfo_table, e);
26791 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26792 the tail part of the directive line, i.e. the part which is past the
26793 initial whitespace, #, whitespace, directive-name, whitespace part. */
26795 static void
26796 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26797 const char *buffer ATTRIBUTE_UNUSED)
26799 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26801 macinfo_entry e;
26802 /* Insert a dummy first entry to be able to optimize the whole
26803 predefined macro block using DW_MACRO_import. */
26804 if (macinfo_table->is_empty () && lineno <= 1)
26806 e.code = 0;
26807 e.lineno = 0;
26808 e.info = NULL;
26809 vec_safe_push (macinfo_table, e);
26811 e.code = DW_MACINFO_define;
26812 e.lineno = lineno;
26813 e.info = ggc_strdup (buffer);
26814 vec_safe_push (macinfo_table, e);
26818 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26819 the tail part of the directive line, i.e. the part which is past the
26820 initial whitespace, #, whitespace, directive-name, whitespace part. */
26822 static void
26823 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26824 const char *buffer ATTRIBUTE_UNUSED)
26826 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26828 macinfo_entry e;
26829 /* Insert a dummy first entry to be able to optimize the whole
26830 predefined macro block using DW_MACRO_import. */
26831 if (macinfo_table->is_empty () && lineno <= 1)
26833 e.code = 0;
26834 e.lineno = 0;
26835 e.info = NULL;
26836 vec_safe_push (macinfo_table, e);
26838 e.code = DW_MACINFO_undef;
26839 e.lineno = lineno;
26840 e.info = ggc_strdup (buffer);
26841 vec_safe_push (macinfo_table, e);
26845 /* Helpers to manipulate hash table of CUs. */
26847 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26849 static inline hashval_t hash (const macinfo_entry *);
26850 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26853 inline hashval_t
26854 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26856 return htab_hash_string (entry->info);
26859 inline bool
26860 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26861 const macinfo_entry *entry2)
26863 return !strcmp (entry1->info, entry2->info);
26866 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26868 /* Output a single .debug_macinfo entry. */
26870 static void
26871 output_macinfo_op (macinfo_entry *ref)
26873 int file_num;
26874 size_t len;
26875 struct indirect_string_node *node;
26876 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26877 struct dwarf_file_data *fd;
26879 switch (ref->code)
26881 case DW_MACINFO_start_file:
26882 fd = lookup_filename (ref->info);
26883 file_num = maybe_emit_file (fd);
26884 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26885 dw2_asm_output_data_uleb128 (ref->lineno,
26886 "Included from line number %lu",
26887 (unsigned long) ref->lineno);
26888 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26889 break;
26890 case DW_MACINFO_end_file:
26891 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26892 break;
26893 case DW_MACINFO_define:
26894 case DW_MACINFO_undef:
26895 len = strlen (ref->info) + 1;
26896 if (!dwarf_strict
26897 && len > DWARF_OFFSET_SIZE
26898 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26899 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26901 ref->code = ref->code == DW_MACINFO_define
26902 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26903 output_macinfo_op (ref);
26904 return;
26906 dw2_asm_output_data (1, ref->code,
26907 ref->code == DW_MACINFO_define
26908 ? "Define macro" : "Undefine macro");
26909 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26910 (unsigned long) ref->lineno);
26911 dw2_asm_output_nstring (ref->info, -1, "The macro");
26912 break;
26913 case DW_MACRO_define_strp:
26914 case DW_MACRO_undef_strp:
26915 node = find_AT_string (ref->info);
26916 gcc_assert (node
26917 && (node->form == DW_FORM_strp
26918 || node->form == DW_FORM_GNU_str_index));
26919 dw2_asm_output_data (1, ref->code,
26920 ref->code == DW_MACRO_define_strp
26921 ? "Define macro strp"
26922 : "Undefine macro strp");
26923 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26924 (unsigned long) ref->lineno);
26925 if (node->form == DW_FORM_strp)
26926 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26927 debug_str_section, "The macro: \"%s\"",
26928 ref->info);
26929 else
26930 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26931 ref->info);
26932 break;
26933 case DW_MACRO_import:
26934 dw2_asm_output_data (1, ref->code, "Import");
26935 ASM_GENERATE_INTERNAL_LABEL (label,
26936 DEBUG_MACRO_SECTION_LABEL,
26937 ref->lineno + macinfo_label_base);
26938 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26939 break;
26940 default:
26941 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26942 ASM_COMMENT_START, (unsigned long) ref->code);
26943 break;
26947 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26948 other compilation unit .debug_macinfo sections. IDX is the first
26949 index of a define/undef, return the number of ops that should be
26950 emitted in a comdat .debug_macinfo section and emit
26951 a DW_MACRO_import entry referencing it.
26952 If the define/undef entry should be emitted normally, return 0. */
26954 static unsigned
26955 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26956 macinfo_hash_type **macinfo_htab)
26958 macinfo_entry *first, *second, *cur, *inc;
26959 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26960 unsigned char checksum[16];
26961 struct md5_ctx ctx;
26962 char *grp_name, *tail;
26963 const char *base;
26964 unsigned int i, count, encoded_filename_len, linebuf_len;
26965 macinfo_entry **slot;
26967 first = &(*macinfo_table)[idx];
26968 second = &(*macinfo_table)[idx + 1];
26970 /* Optimize only if there are at least two consecutive define/undef ops,
26971 and either all of them are before first DW_MACINFO_start_file
26972 with lineno {0,1} (i.e. predefined macro block), or all of them are
26973 in some included header file. */
26974 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26975 return 0;
26976 if (vec_safe_is_empty (files))
26978 if (first->lineno > 1 || second->lineno > 1)
26979 return 0;
26981 else if (first->lineno == 0)
26982 return 0;
26984 /* Find the last define/undef entry that can be grouped together
26985 with first and at the same time compute md5 checksum of their
26986 codes, linenumbers and strings. */
26987 md5_init_ctx (&ctx);
26988 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26989 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26990 break;
26991 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26992 break;
26993 else
26995 unsigned char code = cur->code;
26996 md5_process_bytes (&code, 1, &ctx);
26997 checksum_uleb128 (cur->lineno, &ctx);
26998 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27000 md5_finish_ctx (&ctx, checksum);
27001 count = i - idx;
27003 /* From the containing include filename (if any) pick up just
27004 usable characters from its basename. */
27005 if (vec_safe_is_empty (files))
27006 base = "";
27007 else
27008 base = lbasename (files->last ().info);
27009 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27010 if (ISIDNUM (base[i]) || base[i] == '.')
27011 encoded_filename_len++;
27012 /* Count . at the end. */
27013 if (encoded_filename_len)
27014 encoded_filename_len++;
27016 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27017 linebuf_len = strlen (linebuf);
27019 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27020 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27021 + 16 * 2 + 1);
27022 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27023 tail = grp_name + 4;
27024 if (encoded_filename_len)
27026 for (i = 0; base[i]; i++)
27027 if (ISIDNUM (base[i]) || base[i] == '.')
27028 *tail++ = base[i];
27029 *tail++ = '.';
27031 memcpy (tail, linebuf, linebuf_len);
27032 tail += linebuf_len;
27033 *tail++ = '.';
27034 for (i = 0; i < 16; i++)
27035 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27037 /* Construct a macinfo_entry for DW_MACRO_import
27038 in the empty vector entry before the first define/undef. */
27039 inc = &(*macinfo_table)[idx - 1];
27040 inc->code = DW_MACRO_import;
27041 inc->lineno = 0;
27042 inc->info = ggc_strdup (grp_name);
27043 if (!*macinfo_htab)
27044 *macinfo_htab = new macinfo_hash_type (10);
27045 /* Avoid emitting duplicates. */
27046 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27047 if (*slot != NULL)
27049 inc->code = 0;
27050 inc->info = NULL;
27051 /* If such an entry has been used before, just emit
27052 a DW_MACRO_import op. */
27053 inc = *slot;
27054 output_macinfo_op (inc);
27055 /* And clear all macinfo_entry in the range to avoid emitting them
27056 in the second pass. */
27057 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27059 cur->code = 0;
27060 cur->info = NULL;
27063 else
27065 *slot = inc;
27066 inc->lineno = (*macinfo_htab)->elements ();
27067 output_macinfo_op (inc);
27069 return count;
27072 /* Save any strings needed by the macinfo table in the debug str
27073 table. All strings must be collected into the table by the time
27074 index_string is called. */
27076 static void
27077 save_macinfo_strings (void)
27079 unsigned len;
27080 unsigned i;
27081 macinfo_entry *ref;
27083 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27085 switch (ref->code)
27087 /* Match the logic in output_macinfo_op to decide on
27088 indirect strings. */
27089 case DW_MACINFO_define:
27090 case DW_MACINFO_undef:
27091 len = strlen (ref->info) + 1;
27092 if (!dwarf_strict
27093 && len > DWARF_OFFSET_SIZE
27094 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27095 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27096 set_indirect_string (find_AT_string (ref->info));
27097 break;
27098 case DW_MACRO_define_strp:
27099 case DW_MACRO_undef_strp:
27100 set_indirect_string (find_AT_string (ref->info));
27101 break;
27102 default:
27103 break;
27108 /* Output macinfo section(s). */
27110 static void
27111 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27113 unsigned i;
27114 unsigned long length = vec_safe_length (macinfo_table);
27115 macinfo_entry *ref;
27116 vec<macinfo_entry, va_gc> *files = NULL;
27117 macinfo_hash_type *macinfo_htab = NULL;
27118 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27120 if (! length)
27121 return;
27123 /* output_macinfo* uses these interchangeably. */
27124 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27125 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27126 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27127 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27129 /* AIX Assembler inserts the length, so adjust the reference to match the
27130 offset expected by debuggers. */
27131 strcpy (dl_section_ref, debug_line_label);
27132 if (XCOFF_DEBUGGING_INFO)
27133 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27135 /* For .debug_macro emit the section header. */
27136 if (!dwarf_strict || dwarf_version >= 5)
27138 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27139 "DWARF macro version number");
27140 if (DWARF_OFFSET_SIZE == 8)
27141 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27142 else
27143 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27144 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27145 debug_line_section, NULL);
27148 /* In the first loop, it emits the primary .debug_macinfo section
27149 and after each emitted op the macinfo_entry is cleared.
27150 If a longer range of define/undef ops can be optimized using
27151 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27152 the vector before the first define/undef in the range and the
27153 whole range of define/undef ops is not emitted and kept. */
27154 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27156 switch (ref->code)
27158 case DW_MACINFO_start_file:
27159 vec_safe_push (files, *ref);
27160 break;
27161 case DW_MACINFO_end_file:
27162 if (!vec_safe_is_empty (files))
27163 files->pop ();
27164 break;
27165 case DW_MACINFO_define:
27166 case DW_MACINFO_undef:
27167 if ((!dwarf_strict || dwarf_version >= 5)
27168 && HAVE_COMDAT_GROUP
27169 && vec_safe_length (files) != 1
27170 && i > 0
27171 && i + 1 < length
27172 && (*macinfo_table)[i - 1].code == 0)
27174 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27175 if (count)
27177 i += count - 1;
27178 continue;
27181 break;
27182 case 0:
27183 /* A dummy entry may be inserted at the beginning to be able
27184 to optimize the whole block of predefined macros. */
27185 if (i == 0)
27186 continue;
27187 default:
27188 break;
27190 output_macinfo_op (ref);
27191 ref->info = NULL;
27192 ref->code = 0;
27195 if (!macinfo_htab)
27196 return;
27198 /* Save the number of transparent includes so we can adjust the
27199 label number for the fat LTO object DWARF. */
27200 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27202 delete macinfo_htab;
27203 macinfo_htab = NULL;
27205 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27206 terminate the current chain and switch to a new comdat .debug_macinfo
27207 section and emit the define/undef entries within it. */
27208 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27209 switch (ref->code)
27211 case 0:
27212 continue;
27213 case DW_MACRO_import:
27215 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27216 tree comdat_key = get_identifier (ref->info);
27217 /* Terminate the previous .debug_macinfo section. */
27218 dw2_asm_output_data (1, 0, "End compilation unit");
27219 targetm.asm_out.named_section (debug_macinfo_section_name,
27220 SECTION_DEBUG
27221 | SECTION_LINKONCE
27222 | (early_lto_debug
27223 ? SECTION_EXCLUDE : 0),
27224 comdat_key);
27225 ASM_GENERATE_INTERNAL_LABEL (label,
27226 DEBUG_MACRO_SECTION_LABEL,
27227 ref->lineno + macinfo_label_base);
27228 ASM_OUTPUT_LABEL (asm_out_file, label);
27229 ref->code = 0;
27230 ref->info = NULL;
27231 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27232 "DWARF macro version number");
27233 if (DWARF_OFFSET_SIZE == 8)
27234 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27235 else
27236 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27238 break;
27239 case DW_MACINFO_define:
27240 case DW_MACINFO_undef:
27241 output_macinfo_op (ref);
27242 ref->code = 0;
27243 ref->info = NULL;
27244 break;
27245 default:
27246 gcc_unreachable ();
27249 macinfo_label_base += macinfo_label_base_adj;
27252 /* Initialize the various sections and labels for dwarf output and prefix
27253 them with PREFIX if non-NULL. */
27255 static void
27256 init_sections_and_labels (bool early_lto_debug)
27258 /* As we may get called multiple times have a generation count for
27259 labels. */
27260 static unsigned generation = 0;
27262 if (early_lto_debug)
27264 if (!dwarf_split_debug_info)
27266 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27267 SECTION_DEBUG | SECTION_EXCLUDE,
27268 NULL);
27269 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27270 SECTION_DEBUG | SECTION_EXCLUDE,
27271 NULL);
27272 debug_macinfo_section_name
27273 = ((dwarf_strict && dwarf_version < 5)
27274 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27275 debug_macinfo_section = get_section (debug_macinfo_section_name,
27276 SECTION_DEBUG
27277 | SECTION_EXCLUDE, NULL);
27278 /* For macro info we have to refer to a debug_line section, so
27279 similar to split-dwarf emit a skeleton one for early debug. */
27280 debug_skeleton_line_section
27281 = get_section (DEBUG_LTO_LINE_SECTION,
27282 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27283 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27284 DEBUG_SKELETON_LINE_SECTION_LABEL,
27285 generation);
27287 else
27289 /* ??? Which of the following do we need early? */
27290 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27291 SECTION_DEBUG | SECTION_EXCLUDE,
27292 NULL);
27293 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27294 SECTION_DEBUG | SECTION_EXCLUDE,
27295 NULL);
27296 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27297 SECTION_DEBUG
27298 | SECTION_EXCLUDE, NULL);
27299 debug_skeleton_abbrev_section
27300 = get_section (DEBUG_LTO_ABBREV_SECTION,
27301 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27302 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27303 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27304 generation);
27306 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27307 stay in the main .o, but the skeleton_line goes into the split
27308 off dwo. */
27309 debug_skeleton_line_section
27310 = get_section (DEBUG_LTO_LINE_SECTION,
27311 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27312 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27313 DEBUG_SKELETON_LINE_SECTION_LABEL,
27314 generation);
27315 debug_str_offsets_section
27316 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27317 SECTION_DEBUG | SECTION_EXCLUDE,
27318 NULL);
27319 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27320 DEBUG_SKELETON_INFO_SECTION_LABEL,
27321 generation);
27322 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27323 DEBUG_STR_DWO_SECTION_FLAGS,
27324 NULL);
27325 debug_macinfo_section_name
27326 = ((dwarf_strict && dwarf_version < 5)
27327 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27328 debug_macinfo_section = get_section (debug_macinfo_section_name,
27329 SECTION_DEBUG | SECTION_EXCLUDE,
27330 NULL);
27332 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27333 DEBUG_STR_SECTION_FLAGS
27334 | SECTION_EXCLUDE, NULL);
27335 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27336 debug_line_str_section
27337 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27338 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27340 else
27342 if (!dwarf_split_debug_info)
27344 debug_info_section = get_section (DEBUG_INFO_SECTION,
27345 SECTION_DEBUG, NULL);
27346 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27347 SECTION_DEBUG, NULL);
27348 debug_loc_section = get_section (dwarf_version >= 5
27349 ? DEBUG_LOCLISTS_SECTION
27350 : DEBUG_LOC_SECTION,
27351 SECTION_DEBUG, NULL);
27352 debug_macinfo_section_name
27353 = ((dwarf_strict && dwarf_version < 5)
27354 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27355 debug_macinfo_section = get_section (debug_macinfo_section_name,
27356 SECTION_DEBUG, NULL);
27358 else
27360 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27361 SECTION_DEBUG | SECTION_EXCLUDE,
27362 NULL);
27363 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27364 SECTION_DEBUG | SECTION_EXCLUDE,
27365 NULL);
27366 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27367 SECTION_DEBUG, NULL);
27368 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27369 SECTION_DEBUG, NULL);
27370 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27371 SECTION_DEBUG, NULL);
27372 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27373 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27374 generation);
27376 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27377 stay in the main .o, but the skeleton_line goes into the
27378 split off dwo. */
27379 debug_skeleton_line_section
27380 = get_section (DEBUG_DWO_LINE_SECTION,
27381 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27382 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27383 DEBUG_SKELETON_LINE_SECTION_LABEL,
27384 generation);
27385 debug_str_offsets_section
27386 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27387 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27388 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27389 DEBUG_SKELETON_INFO_SECTION_LABEL,
27390 generation);
27391 debug_loc_section = get_section (dwarf_version >= 5
27392 ? DEBUG_DWO_LOCLISTS_SECTION
27393 : DEBUG_DWO_LOC_SECTION,
27394 SECTION_DEBUG | SECTION_EXCLUDE,
27395 NULL);
27396 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27397 DEBUG_STR_DWO_SECTION_FLAGS,
27398 NULL);
27399 debug_macinfo_section_name
27400 = ((dwarf_strict && dwarf_version < 5)
27401 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27402 debug_macinfo_section = get_section (debug_macinfo_section_name,
27403 SECTION_DEBUG | SECTION_EXCLUDE,
27404 NULL);
27406 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27407 SECTION_DEBUG, NULL);
27408 debug_line_section = get_section (DEBUG_LINE_SECTION,
27409 SECTION_DEBUG, NULL);
27410 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27411 SECTION_DEBUG, NULL);
27412 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27413 SECTION_DEBUG, NULL);
27414 debug_str_section = get_section (DEBUG_STR_SECTION,
27415 DEBUG_STR_SECTION_FLAGS, NULL);
27416 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27417 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27418 DEBUG_STR_SECTION_FLAGS, NULL);
27419 debug_ranges_section = get_section (dwarf_version >= 5
27420 ? DEBUG_RNGLISTS_SECTION
27421 : DEBUG_RANGES_SECTION,
27422 SECTION_DEBUG, NULL);
27423 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27424 SECTION_DEBUG, NULL);
27427 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27428 DEBUG_ABBREV_SECTION_LABEL, generation);
27429 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27430 DEBUG_INFO_SECTION_LABEL, generation);
27431 info_section_emitted = false;
27432 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27433 DEBUG_LINE_SECTION_LABEL, generation);
27434 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27435 DEBUG_RANGES_SECTION_LABEL, generation);
27436 if (dwarf_version >= 5 && dwarf_split_debug_info)
27437 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27438 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27439 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27440 DEBUG_ADDR_SECTION_LABEL, generation);
27441 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27442 (dwarf_strict && dwarf_version < 5)
27443 ? DEBUG_MACINFO_SECTION_LABEL
27444 : DEBUG_MACRO_SECTION_LABEL, generation);
27445 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27446 generation);
27448 ++generation;
27451 /* Set up for Dwarf output at the start of compilation. */
27453 static void
27454 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27456 /* Allocate the file_table. */
27457 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27459 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27460 /* Allocate the decl_die_table. */
27461 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27463 /* Allocate the decl_loc_table. */
27464 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27466 /* Allocate the cached_dw_loc_list_table. */
27467 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27469 /* Allocate the initial hunk of the decl_scope_table. */
27470 vec_alloc (decl_scope_table, 256);
27472 /* Allocate the initial hunk of the abbrev_die_table. */
27473 vec_alloc (abbrev_die_table, 256);
27474 /* Zero-th entry is allocated, but unused. */
27475 abbrev_die_table->quick_push (NULL);
27477 /* Allocate the dwarf_proc_stack_usage_map. */
27478 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27480 /* Allocate the pubtypes and pubnames vectors. */
27481 vec_alloc (pubname_table, 32);
27482 vec_alloc (pubtype_table, 32);
27484 vec_alloc (incomplete_types, 64);
27486 vec_alloc (used_rtx_array, 32);
27488 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27489 vec_alloc (macinfo_table, 64);
27490 #endif
27492 /* If front-ends already registered a main translation unit but we were not
27493 ready to perform the association, do this now. */
27494 if (main_translation_unit != NULL_TREE)
27495 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27498 /* Called before compile () starts outputtting functions, variables
27499 and toplevel asms into assembly. */
27501 static void
27502 dwarf2out_assembly_start (void)
27504 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27505 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27506 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27507 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27508 COLD_TEXT_SECTION_LABEL, 0);
27509 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27511 switch_to_section (text_section);
27512 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27513 #endif
27515 /* Make sure the line number table for .text always exists. */
27516 text_section_line_info = new_line_info_table ();
27517 text_section_line_info->end_label = text_end_label;
27519 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27520 cur_line_info_table = text_section_line_info;
27521 #endif
27523 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27524 && dwarf2out_do_cfi_asm ()
27525 && (!(flag_unwind_tables || flag_exceptions)
27526 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27527 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27530 /* A helper function for dwarf2out_finish called through
27531 htab_traverse. Assign a string its index. All strings must be
27532 collected into the table by the time index_string is called,
27533 because the indexing code relies on htab_traverse to traverse nodes
27534 in the same order for each run. */
27537 index_string (indirect_string_node **h, unsigned int *index)
27539 indirect_string_node *node = *h;
27541 find_string_form (node);
27542 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27544 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27545 node->index = *index;
27546 *index += 1;
27548 return 1;
27551 /* A helper function for output_indirect_strings called through
27552 htab_traverse. Output the offset to a string and update the
27553 current offset. */
27556 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27558 indirect_string_node *node = *h;
27560 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27562 /* Assert that this node has been assigned an index. */
27563 gcc_assert (node->index != NO_INDEX_ASSIGNED
27564 && node->index != NOT_INDEXED);
27565 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27566 "indexed string 0x%x: %s", node->index, node->str);
27567 *offset += strlen (node->str) + 1;
27569 return 1;
27572 /* A helper function for dwarf2out_finish called through
27573 htab_traverse. Output the indexed string. */
27576 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27578 struct indirect_string_node *node = *h;
27580 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27582 /* Assert that the strings are output in the same order as their
27583 indexes were assigned. */
27584 gcc_assert (*cur_idx == node->index);
27585 assemble_string (node->str, strlen (node->str) + 1);
27586 *cur_idx += 1;
27588 return 1;
27591 /* A helper function for dwarf2out_finish called through
27592 htab_traverse. Emit one queued .debug_str string. */
27595 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27597 struct indirect_string_node *node = *h;
27599 node->form = find_string_form (node);
27600 if (node->form == form && node->refcount > 0)
27602 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27603 assemble_string (node->str, strlen (node->str) + 1);
27606 return 1;
27609 /* Output the indexed string table. */
27611 static void
27612 output_indirect_strings (void)
27614 switch_to_section (debug_str_section);
27615 if (!dwarf_split_debug_info)
27616 debug_str_hash->traverse<enum dwarf_form,
27617 output_indirect_string> (DW_FORM_strp);
27618 else
27620 unsigned int offset = 0;
27621 unsigned int cur_idx = 0;
27623 skeleton_debug_str_hash->traverse<enum dwarf_form,
27624 output_indirect_string> (DW_FORM_strp);
27626 switch_to_section (debug_str_offsets_section);
27627 debug_str_hash->traverse_noresize
27628 <unsigned int *, output_index_string_offset> (&offset);
27629 switch_to_section (debug_str_dwo_section);
27630 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27631 (&cur_idx);
27635 /* Callback for htab_traverse to assign an index to an entry in the
27636 table, and to write that entry to the .debug_addr section. */
27639 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27641 addr_table_entry *entry = *slot;
27643 if (entry->refcount == 0)
27645 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27646 || entry->index == NOT_INDEXED);
27647 return 1;
27650 gcc_assert (entry->index == *cur_index);
27651 (*cur_index)++;
27653 switch (entry->kind)
27655 case ate_kind_rtx:
27656 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27657 "0x%x", entry->index);
27658 break;
27659 case ate_kind_rtx_dtprel:
27660 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27661 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27662 DWARF2_ADDR_SIZE,
27663 entry->addr.rtl);
27664 fputc ('\n', asm_out_file);
27665 break;
27666 case ate_kind_label:
27667 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27668 "0x%x", entry->index);
27669 break;
27670 default:
27671 gcc_unreachable ();
27673 return 1;
27676 /* Produce the .debug_addr section. */
27678 static void
27679 output_addr_table (void)
27681 unsigned int index = 0;
27682 if (addr_index_table == NULL || addr_index_table->size () == 0)
27683 return;
27685 switch_to_section (debug_addr_section);
27686 addr_index_table
27687 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27690 #if ENABLE_ASSERT_CHECKING
27691 /* Verify that all marks are clear. */
27693 static void
27694 verify_marks_clear (dw_die_ref die)
27696 dw_die_ref c;
27698 gcc_assert (! die->die_mark);
27699 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27701 #endif /* ENABLE_ASSERT_CHECKING */
27703 /* Clear the marks for a die and its children.
27704 Be cool if the mark isn't set. */
27706 static void
27707 prune_unmark_dies (dw_die_ref die)
27709 dw_die_ref c;
27711 if (die->die_mark)
27712 die->die_mark = 0;
27713 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27716 /* Given LOC that is referenced by a DIE we're marking as used, find all
27717 referenced DWARF procedures it references and mark them as used. */
27719 static void
27720 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27722 for (; loc != NULL; loc = loc->dw_loc_next)
27723 switch (loc->dw_loc_opc)
27725 case DW_OP_implicit_pointer:
27726 case DW_OP_convert:
27727 case DW_OP_reinterpret:
27728 case DW_OP_GNU_implicit_pointer:
27729 case DW_OP_GNU_convert:
27730 case DW_OP_GNU_reinterpret:
27731 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27732 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27733 break;
27734 case DW_OP_GNU_variable_value:
27735 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27737 dw_die_ref ref
27738 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27739 if (ref == NULL)
27740 break;
27741 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27742 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27743 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27745 /* FALLTHRU */
27746 case DW_OP_call2:
27747 case DW_OP_call4:
27748 case DW_OP_call_ref:
27749 case DW_OP_const_type:
27750 case DW_OP_GNU_const_type:
27751 case DW_OP_GNU_parameter_ref:
27752 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27753 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27754 break;
27755 case DW_OP_regval_type:
27756 case DW_OP_deref_type:
27757 case DW_OP_GNU_regval_type:
27758 case DW_OP_GNU_deref_type:
27759 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27760 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27761 break;
27762 case DW_OP_entry_value:
27763 case DW_OP_GNU_entry_value:
27764 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27765 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27766 break;
27767 default:
27768 break;
27772 /* Given DIE that we're marking as used, find any other dies
27773 it references as attributes and mark them as used. */
27775 static void
27776 prune_unused_types_walk_attribs (dw_die_ref die)
27778 dw_attr_node *a;
27779 unsigned ix;
27781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27783 switch (AT_class (a))
27785 /* Make sure DWARF procedures referenced by location descriptions will
27786 get emitted. */
27787 case dw_val_class_loc:
27788 prune_unused_types_walk_loc_descr (AT_loc (a));
27789 break;
27790 case dw_val_class_loc_list:
27791 for (dw_loc_list_ref list = AT_loc_list (a);
27792 list != NULL;
27793 list = list->dw_loc_next)
27794 prune_unused_types_walk_loc_descr (list->expr);
27795 break;
27797 case dw_val_class_die_ref:
27798 /* A reference to another DIE.
27799 Make sure that it will get emitted.
27800 If it was broken out into a comdat group, don't follow it. */
27801 if (! AT_ref (a)->comdat_type_p
27802 || a->dw_attr == DW_AT_specification)
27803 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27804 break;
27806 case dw_val_class_str:
27807 /* Set the string's refcount to 0 so that prune_unused_types_mark
27808 accounts properly for it. */
27809 a->dw_attr_val.v.val_str->refcount = 0;
27810 break;
27812 default:
27813 break;
27818 /* Mark the generic parameters and arguments children DIEs of DIE. */
27820 static void
27821 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27823 dw_die_ref c;
27825 if (die == NULL || die->die_child == NULL)
27826 return;
27827 c = die->die_child;
27830 if (is_template_parameter (c))
27831 prune_unused_types_mark (c, 1);
27832 c = c->die_sib;
27833 } while (c && c != die->die_child);
27836 /* Mark DIE as being used. If DOKIDS is true, then walk down
27837 to DIE's children. */
27839 static void
27840 prune_unused_types_mark (dw_die_ref die, int dokids)
27842 dw_die_ref c;
27844 if (die->die_mark == 0)
27846 /* We haven't done this node yet. Mark it as used. */
27847 die->die_mark = 1;
27848 /* If this is the DIE of a generic type instantiation,
27849 mark the children DIEs that describe its generic parms and
27850 args. */
27851 prune_unused_types_mark_generic_parms_dies (die);
27853 /* We also have to mark its parents as used.
27854 (But we don't want to mark our parent's kids due to this,
27855 unless it is a class.) */
27856 if (die->die_parent)
27857 prune_unused_types_mark (die->die_parent,
27858 class_scope_p (die->die_parent));
27860 /* Mark any referenced nodes. */
27861 prune_unused_types_walk_attribs (die);
27863 /* If this node is a specification,
27864 also mark the definition, if it exists. */
27865 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27866 prune_unused_types_mark (die->die_definition, 1);
27869 if (dokids && die->die_mark != 2)
27871 /* We need to walk the children, but haven't done so yet.
27872 Remember that we've walked the kids. */
27873 die->die_mark = 2;
27875 /* If this is an array type, we need to make sure our
27876 kids get marked, even if they're types. If we're
27877 breaking out types into comdat sections, do this
27878 for all type definitions. */
27879 if (die->die_tag == DW_TAG_array_type
27880 || (use_debug_types
27881 && is_type_die (die) && ! is_declaration_die (die)))
27882 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27883 else
27884 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27888 /* For local classes, look if any static member functions were emitted
27889 and if so, mark them. */
27891 static void
27892 prune_unused_types_walk_local_classes (dw_die_ref die)
27894 dw_die_ref c;
27896 if (die->die_mark == 2)
27897 return;
27899 switch (die->die_tag)
27901 case DW_TAG_structure_type:
27902 case DW_TAG_union_type:
27903 case DW_TAG_class_type:
27904 break;
27906 case DW_TAG_subprogram:
27907 if (!get_AT_flag (die, DW_AT_declaration)
27908 || die->die_definition != NULL)
27909 prune_unused_types_mark (die, 1);
27910 return;
27912 default:
27913 return;
27916 /* Mark children. */
27917 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27920 /* Walk the tree DIE and mark types that we actually use. */
27922 static void
27923 prune_unused_types_walk (dw_die_ref die)
27925 dw_die_ref c;
27927 /* Don't do anything if this node is already marked and
27928 children have been marked as well. */
27929 if (die->die_mark == 2)
27930 return;
27932 switch (die->die_tag)
27934 case DW_TAG_structure_type:
27935 case DW_TAG_union_type:
27936 case DW_TAG_class_type:
27937 if (die->die_perennial_p)
27938 break;
27940 for (c = die->die_parent; c; c = c->die_parent)
27941 if (c->die_tag == DW_TAG_subprogram)
27942 break;
27944 /* Finding used static member functions inside of classes
27945 is needed just for local classes, because for other classes
27946 static member function DIEs with DW_AT_specification
27947 are emitted outside of the DW_TAG_*_type. If we ever change
27948 it, we'd need to call this even for non-local classes. */
27949 if (c)
27950 prune_unused_types_walk_local_classes (die);
27952 /* It's a type node --- don't mark it. */
27953 return;
27955 case DW_TAG_const_type:
27956 case DW_TAG_packed_type:
27957 case DW_TAG_pointer_type:
27958 case DW_TAG_reference_type:
27959 case DW_TAG_rvalue_reference_type:
27960 case DW_TAG_volatile_type:
27961 case DW_TAG_typedef:
27962 case DW_TAG_array_type:
27963 case DW_TAG_interface_type:
27964 case DW_TAG_friend:
27965 case DW_TAG_enumeration_type:
27966 case DW_TAG_subroutine_type:
27967 case DW_TAG_string_type:
27968 case DW_TAG_set_type:
27969 case DW_TAG_subrange_type:
27970 case DW_TAG_ptr_to_member_type:
27971 case DW_TAG_file_type:
27972 /* Type nodes are useful only when other DIEs reference them --- don't
27973 mark them. */
27974 /* FALLTHROUGH */
27976 case DW_TAG_dwarf_procedure:
27977 /* Likewise for DWARF procedures. */
27979 if (die->die_perennial_p)
27980 break;
27982 return;
27984 default:
27985 /* Mark everything else. */
27986 break;
27989 if (die->die_mark == 0)
27991 die->die_mark = 1;
27993 /* Now, mark any dies referenced from here. */
27994 prune_unused_types_walk_attribs (die);
27997 die->die_mark = 2;
27999 /* Mark children. */
28000 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28003 /* Increment the string counts on strings referred to from DIE's
28004 attributes. */
28006 static void
28007 prune_unused_types_update_strings (dw_die_ref die)
28009 dw_attr_node *a;
28010 unsigned ix;
28012 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28013 if (AT_class (a) == dw_val_class_str)
28015 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28016 s->refcount++;
28017 /* Avoid unnecessarily putting strings that are used less than
28018 twice in the hash table. */
28019 if (s->refcount
28020 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28022 indirect_string_node **slot
28023 = debug_str_hash->find_slot_with_hash (s->str,
28024 htab_hash_string (s->str),
28025 INSERT);
28026 gcc_assert (*slot == NULL);
28027 *slot = s;
28032 /* Mark DIE and its children as removed. */
28034 static void
28035 mark_removed (dw_die_ref die)
28037 dw_die_ref c;
28038 die->removed = true;
28039 FOR_EACH_CHILD (die, c, mark_removed (c));
28042 /* Remove from the tree DIE any dies that aren't marked. */
28044 static void
28045 prune_unused_types_prune (dw_die_ref die)
28047 dw_die_ref c;
28049 gcc_assert (die->die_mark);
28050 prune_unused_types_update_strings (die);
28052 if (! die->die_child)
28053 return;
28055 c = die->die_child;
28056 do {
28057 dw_die_ref prev = c, next;
28058 for (c = c->die_sib; ! c->die_mark; c = next)
28059 if (c == die->die_child)
28061 /* No marked children between 'prev' and the end of the list. */
28062 if (prev == c)
28063 /* No marked children at all. */
28064 die->die_child = NULL;
28065 else
28067 prev->die_sib = c->die_sib;
28068 die->die_child = prev;
28070 c->die_sib = NULL;
28071 mark_removed (c);
28072 return;
28074 else
28076 next = c->die_sib;
28077 c->die_sib = NULL;
28078 mark_removed (c);
28081 if (c != prev->die_sib)
28082 prev->die_sib = c;
28083 prune_unused_types_prune (c);
28084 } while (c != die->die_child);
28087 /* Remove dies representing declarations that we never use. */
28089 static void
28090 prune_unused_types (void)
28092 unsigned int i;
28093 limbo_die_node *node;
28094 comdat_type_node *ctnode;
28095 pubname_entry *pub;
28096 dw_die_ref base_type;
28098 #if ENABLE_ASSERT_CHECKING
28099 /* All the marks should already be clear. */
28100 verify_marks_clear (comp_unit_die ());
28101 for (node = limbo_die_list; node; node = node->next)
28102 verify_marks_clear (node->die);
28103 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28104 verify_marks_clear (ctnode->root_die);
28105 #endif /* ENABLE_ASSERT_CHECKING */
28107 /* Mark types that are used in global variables. */
28108 premark_types_used_by_global_vars ();
28110 /* Set the mark on nodes that are actually used. */
28111 prune_unused_types_walk (comp_unit_die ());
28112 for (node = limbo_die_list; node; node = node->next)
28113 prune_unused_types_walk (node->die);
28114 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28116 prune_unused_types_walk (ctnode->root_die);
28117 prune_unused_types_mark (ctnode->type_die, 1);
28120 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28121 are unusual in that they are pubnames that are the children of pubtypes.
28122 They should only be marked via their parent DW_TAG_enumeration_type die,
28123 not as roots in themselves. */
28124 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28125 if (pub->die->die_tag != DW_TAG_enumerator)
28126 prune_unused_types_mark (pub->die, 1);
28127 for (i = 0; base_types.iterate (i, &base_type); i++)
28128 prune_unused_types_mark (base_type, 1);
28130 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28131 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28132 callees). */
28133 cgraph_node *cnode;
28134 FOR_EACH_FUNCTION (cnode)
28135 if (cnode->referred_to_p (false))
28137 dw_die_ref die = lookup_decl_die (cnode->decl);
28138 if (die == NULL || die->die_mark)
28139 continue;
28140 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28141 if (e->caller != cnode
28142 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28144 prune_unused_types_mark (die, 1);
28145 break;
28149 if (debug_str_hash)
28150 debug_str_hash->empty ();
28151 if (skeleton_debug_str_hash)
28152 skeleton_debug_str_hash->empty ();
28153 prune_unused_types_prune (comp_unit_die ());
28154 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28156 node = *pnode;
28157 if (!node->die->die_mark)
28158 *pnode = node->next;
28159 else
28161 prune_unused_types_prune (node->die);
28162 pnode = &node->next;
28165 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28166 prune_unused_types_prune (ctnode->root_die);
28168 /* Leave the marks clear. */
28169 prune_unmark_dies (comp_unit_die ());
28170 for (node = limbo_die_list; node; node = node->next)
28171 prune_unmark_dies (node->die);
28172 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28173 prune_unmark_dies (ctnode->root_die);
28176 /* Helpers to manipulate hash table of comdat type units. */
28178 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28180 static inline hashval_t hash (const comdat_type_node *);
28181 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28184 inline hashval_t
28185 comdat_type_hasher::hash (const comdat_type_node *type_node)
28187 hashval_t h;
28188 memcpy (&h, type_node->signature, sizeof (h));
28189 return h;
28192 inline bool
28193 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28194 const comdat_type_node *type_node_2)
28196 return (! memcmp (type_node_1->signature, type_node_2->signature,
28197 DWARF_TYPE_SIGNATURE_SIZE));
28200 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28201 to the location it would have been added, should we know its
28202 DECL_ASSEMBLER_NAME when we added other attributes. This will
28203 probably improve compactness of debug info, removing equivalent
28204 abbrevs, and hide any differences caused by deferring the
28205 computation of the assembler name, triggered by e.g. PCH. */
28207 static inline void
28208 move_linkage_attr (dw_die_ref die)
28210 unsigned ix = vec_safe_length (die->die_attr);
28211 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28213 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28214 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28216 while (--ix > 0)
28218 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28220 if (prev->dw_attr == DW_AT_decl_line
28221 || prev->dw_attr == DW_AT_decl_column
28222 || prev->dw_attr == DW_AT_name)
28223 break;
28226 if (ix != vec_safe_length (die->die_attr) - 1)
28228 die->die_attr->pop ();
28229 die->die_attr->quick_insert (ix, linkage);
28233 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28234 referenced from typed stack ops and count how often they are used. */
28236 static void
28237 mark_base_types (dw_loc_descr_ref loc)
28239 dw_die_ref base_type = NULL;
28241 for (; loc; loc = loc->dw_loc_next)
28243 switch (loc->dw_loc_opc)
28245 case DW_OP_regval_type:
28246 case DW_OP_deref_type:
28247 case DW_OP_GNU_regval_type:
28248 case DW_OP_GNU_deref_type:
28249 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28250 break;
28251 case DW_OP_convert:
28252 case DW_OP_reinterpret:
28253 case DW_OP_GNU_convert:
28254 case DW_OP_GNU_reinterpret:
28255 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28256 continue;
28257 /* FALLTHRU */
28258 case DW_OP_const_type:
28259 case DW_OP_GNU_const_type:
28260 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28261 break;
28262 case DW_OP_entry_value:
28263 case DW_OP_GNU_entry_value:
28264 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28265 continue;
28266 default:
28267 continue;
28269 gcc_assert (base_type->die_parent == comp_unit_die ());
28270 if (base_type->die_mark)
28271 base_type->die_mark++;
28272 else
28274 base_types.safe_push (base_type);
28275 base_type->die_mark = 1;
28280 /* Comparison function for sorting marked base types. */
28282 static int
28283 base_type_cmp (const void *x, const void *y)
28285 dw_die_ref dx = *(const dw_die_ref *) x;
28286 dw_die_ref dy = *(const dw_die_ref *) y;
28287 unsigned int byte_size1, byte_size2;
28288 unsigned int encoding1, encoding2;
28289 unsigned int align1, align2;
28290 if (dx->die_mark > dy->die_mark)
28291 return -1;
28292 if (dx->die_mark < dy->die_mark)
28293 return 1;
28294 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28295 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28296 if (byte_size1 < byte_size2)
28297 return 1;
28298 if (byte_size1 > byte_size2)
28299 return -1;
28300 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28301 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28302 if (encoding1 < encoding2)
28303 return 1;
28304 if (encoding1 > encoding2)
28305 return -1;
28306 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28307 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28308 if (align1 < align2)
28309 return 1;
28310 if (align1 > align2)
28311 return -1;
28312 return 0;
28315 /* Move base types marked by mark_base_types as early as possible
28316 in the CU, sorted by decreasing usage count both to make the
28317 uleb128 references as small as possible and to make sure they
28318 will have die_offset already computed by calc_die_sizes when
28319 sizes of typed stack loc ops is computed. */
28321 static void
28322 move_marked_base_types (void)
28324 unsigned int i;
28325 dw_die_ref base_type, die, c;
28327 if (base_types.is_empty ())
28328 return;
28330 /* Sort by decreasing usage count, they will be added again in that
28331 order later on. */
28332 base_types.qsort (base_type_cmp);
28333 die = comp_unit_die ();
28334 c = die->die_child;
28337 dw_die_ref prev = c;
28338 c = c->die_sib;
28339 while (c->die_mark)
28341 remove_child_with_prev (c, prev);
28342 /* As base types got marked, there must be at least
28343 one node other than DW_TAG_base_type. */
28344 gcc_assert (die->die_child != NULL);
28345 c = prev->die_sib;
28348 while (c != die->die_child);
28349 gcc_assert (die->die_child);
28350 c = die->die_child;
28351 for (i = 0; base_types.iterate (i, &base_type); i++)
28353 base_type->die_mark = 0;
28354 base_type->die_sib = c->die_sib;
28355 c->die_sib = base_type;
28356 c = base_type;
28360 /* Helper function for resolve_addr, attempt to resolve
28361 one CONST_STRING, return true if successful. Similarly verify that
28362 SYMBOL_REFs refer to variables emitted in the current CU. */
28364 static bool
28365 resolve_one_addr (rtx *addr)
28367 rtx rtl = *addr;
28369 if (GET_CODE (rtl) == CONST_STRING)
28371 size_t len = strlen (XSTR (rtl, 0)) + 1;
28372 tree t = build_string (len, XSTR (rtl, 0));
28373 tree tlen = size_int (len - 1);
28374 TREE_TYPE (t)
28375 = build_array_type (char_type_node, build_index_type (tlen));
28376 rtl = lookup_constant_def (t);
28377 if (!rtl || !MEM_P (rtl))
28378 return false;
28379 rtl = XEXP (rtl, 0);
28380 if (GET_CODE (rtl) == SYMBOL_REF
28381 && SYMBOL_REF_DECL (rtl)
28382 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28383 return false;
28384 vec_safe_push (used_rtx_array, rtl);
28385 *addr = rtl;
28386 return true;
28389 if (GET_CODE (rtl) == SYMBOL_REF
28390 && SYMBOL_REF_DECL (rtl))
28392 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28394 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28395 return false;
28397 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28398 return false;
28401 if (GET_CODE (rtl) == CONST)
28403 subrtx_ptr_iterator::array_type array;
28404 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28405 if (!resolve_one_addr (*iter))
28406 return false;
28409 return true;
28412 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28413 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28414 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28416 static rtx
28417 string_cst_pool_decl (tree t)
28419 rtx rtl = output_constant_def (t, 1);
28420 unsigned char *array;
28421 dw_loc_descr_ref l;
28422 tree decl;
28423 size_t len;
28424 dw_die_ref ref;
28426 if (!rtl || !MEM_P (rtl))
28427 return NULL_RTX;
28428 rtl = XEXP (rtl, 0);
28429 if (GET_CODE (rtl) != SYMBOL_REF
28430 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28431 return NULL_RTX;
28433 decl = SYMBOL_REF_DECL (rtl);
28434 if (!lookup_decl_die (decl))
28436 len = TREE_STRING_LENGTH (t);
28437 vec_safe_push (used_rtx_array, rtl);
28438 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28439 array = ggc_vec_alloc<unsigned char> (len);
28440 memcpy (array, TREE_STRING_POINTER (t), len);
28441 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28442 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28443 l->dw_loc_oprnd2.v.val_vec.length = len;
28444 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28445 l->dw_loc_oprnd2.v.val_vec.array = array;
28446 add_AT_loc (ref, DW_AT_location, l);
28447 equate_decl_number_to_die (decl, ref);
28449 return rtl;
28452 /* Helper function of resolve_addr_in_expr. LOC is
28453 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28454 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28455 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28456 with DW_OP_implicit_pointer if possible
28457 and return true, if unsuccessful, return false. */
28459 static bool
28460 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28462 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28463 HOST_WIDE_INT offset = 0;
28464 dw_die_ref ref = NULL;
28465 tree decl;
28467 if (GET_CODE (rtl) == CONST
28468 && GET_CODE (XEXP (rtl, 0)) == PLUS
28469 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28471 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28472 rtl = XEXP (XEXP (rtl, 0), 0);
28474 if (GET_CODE (rtl) == CONST_STRING)
28476 size_t len = strlen (XSTR (rtl, 0)) + 1;
28477 tree t = build_string (len, XSTR (rtl, 0));
28478 tree tlen = size_int (len - 1);
28480 TREE_TYPE (t)
28481 = build_array_type (char_type_node, build_index_type (tlen));
28482 rtl = string_cst_pool_decl (t);
28483 if (!rtl)
28484 return false;
28486 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28488 decl = SYMBOL_REF_DECL (rtl);
28489 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28491 ref = lookup_decl_die (decl);
28492 if (ref && (get_AT (ref, DW_AT_location)
28493 || get_AT (ref, DW_AT_const_value)))
28495 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28496 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28497 loc->dw_loc_oprnd1.val_entry = NULL;
28498 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28499 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28500 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28501 loc->dw_loc_oprnd2.v.val_int = offset;
28502 return true;
28506 return false;
28509 /* Helper function for resolve_addr, handle one location
28510 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28511 the location list couldn't be resolved. */
28513 static bool
28514 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28516 dw_loc_descr_ref keep = NULL;
28517 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28518 switch (loc->dw_loc_opc)
28520 case DW_OP_addr:
28521 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28523 if ((prev == NULL
28524 || prev->dw_loc_opc == DW_OP_piece
28525 || prev->dw_loc_opc == DW_OP_bit_piece)
28526 && loc->dw_loc_next
28527 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28528 && (!dwarf_strict || dwarf_version >= 5)
28529 && optimize_one_addr_into_implicit_ptr (loc))
28530 break;
28531 return false;
28533 break;
28534 case DW_OP_GNU_addr_index:
28535 case DW_OP_GNU_const_index:
28536 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28537 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28539 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28540 if (!resolve_one_addr (&rtl))
28541 return false;
28542 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28543 loc->dw_loc_oprnd1.val_entry
28544 = add_addr_table_entry (rtl, ate_kind_rtx);
28546 break;
28547 case DW_OP_const4u:
28548 case DW_OP_const8u:
28549 if (loc->dtprel
28550 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28551 return false;
28552 break;
28553 case DW_OP_plus_uconst:
28554 if (size_of_loc_descr (loc)
28555 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28557 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28559 dw_loc_descr_ref repl
28560 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28561 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28562 add_loc_descr (&repl, loc->dw_loc_next);
28563 *loc = *repl;
28565 break;
28566 case DW_OP_implicit_value:
28567 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28568 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28569 return false;
28570 break;
28571 case DW_OP_implicit_pointer:
28572 case DW_OP_GNU_implicit_pointer:
28573 case DW_OP_GNU_parameter_ref:
28574 case DW_OP_GNU_variable_value:
28575 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28577 dw_die_ref ref
28578 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28579 if (ref == NULL)
28580 return false;
28581 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28582 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28583 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28585 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28587 if (prev == NULL
28588 && loc->dw_loc_next == NULL
28589 && AT_class (a) == dw_val_class_loc)
28590 switch (a->dw_attr)
28592 /* Following attributes allow both exprloc and reference,
28593 so if the whole expression is DW_OP_GNU_variable_value
28594 alone we could transform it into reference. */
28595 case DW_AT_byte_size:
28596 case DW_AT_bit_size:
28597 case DW_AT_lower_bound:
28598 case DW_AT_upper_bound:
28599 case DW_AT_bit_stride:
28600 case DW_AT_count:
28601 case DW_AT_allocated:
28602 case DW_AT_associated:
28603 case DW_AT_byte_stride:
28604 a->dw_attr_val.val_class = dw_val_class_die_ref;
28605 a->dw_attr_val.val_entry = NULL;
28606 a->dw_attr_val.v.val_die_ref.die
28607 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28608 a->dw_attr_val.v.val_die_ref.external = 0;
28609 return true;
28610 default:
28611 break;
28613 if (dwarf_strict)
28614 return false;
28616 break;
28617 case DW_OP_const_type:
28618 case DW_OP_regval_type:
28619 case DW_OP_deref_type:
28620 case DW_OP_convert:
28621 case DW_OP_reinterpret:
28622 case DW_OP_GNU_const_type:
28623 case DW_OP_GNU_regval_type:
28624 case DW_OP_GNU_deref_type:
28625 case DW_OP_GNU_convert:
28626 case DW_OP_GNU_reinterpret:
28627 while (loc->dw_loc_next
28628 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28629 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28631 dw_die_ref base1, base2;
28632 unsigned enc1, enc2, size1, size2;
28633 if (loc->dw_loc_opc == DW_OP_regval_type
28634 || loc->dw_loc_opc == DW_OP_deref_type
28635 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28636 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28637 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28638 else if (loc->dw_loc_oprnd1.val_class
28639 == dw_val_class_unsigned_const)
28640 break;
28641 else
28642 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28643 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28644 == dw_val_class_unsigned_const)
28645 break;
28646 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28647 gcc_assert (base1->die_tag == DW_TAG_base_type
28648 && base2->die_tag == DW_TAG_base_type);
28649 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28650 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28651 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28652 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28653 if (size1 == size2
28654 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28655 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28656 && loc != keep)
28657 || enc1 == enc2))
28659 /* Optimize away next DW_OP_convert after
28660 adjusting LOC's base type die reference. */
28661 if (loc->dw_loc_opc == DW_OP_regval_type
28662 || loc->dw_loc_opc == DW_OP_deref_type
28663 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28664 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28665 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28666 else
28667 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28668 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28669 continue;
28671 /* Don't change integer DW_OP_convert after e.g. floating
28672 point typed stack entry. */
28673 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28674 keep = loc->dw_loc_next;
28675 break;
28677 break;
28678 default:
28679 break;
28681 return true;
28684 /* Helper function of resolve_addr. DIE had DW_AT_location of
28685 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28686 and DW_OP_addr couldn't be resolved. resolve_addr has already
28687 removed the DW_AT_location attribute. This function attempts to
28688 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28689 to it or DW_AT_const_value attribute, if possible. */
28691 static void
28692 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28694 if (!VAR_P (decl)
28695 || lookup_decl_die (decl) != die
28696 || DECL_EXTERNAL (decl)
28697 || !TREE_STATIC (decl)
28698 || DECL_INITIAL (decl) == NULL_TREE
28699 || DECL_P (DECL_INITIAL (decl))
28700 || get_AT (die, DW_AT_const_value))
28701 return;
28703 tree init = DECL_INITIAL (decl);
28704 HOST_WIDE_INT offset = 0;
28705 /* For variables that have been optimized away and thus
28706 don't have a memory location, see if we can emit
28707 DW_AT_const_value instead. */
28708 if (tree_add_const_value_attribute (die, init))
28709 return;
28710 if (dwarf_strict && dwarf_version < 5)
28711 return;
28712 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28713 and ADDR_EXPR refers to a decl that has DW_AT_location or
28714 DW_AT_const_value (but isn't addressable, otherwise
28715 resolving the original DW_OP_addr wouldn't fail), see if
28716 we can add DW_OP_implicit_pointer. */
28717 STRIP_NOPS (init);
28718 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28719 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28721 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28722 init = TREE_OPERAND (init, 0);
28723 STRIP_NOPS (init);
28725 if (TREE_CODE (init) != ADDR_EXPR)
28726 return;
28727 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28728 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28729 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28730 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28731 && TREE_OPERAND (init, 0) != decl))
28733 dw_die_ref ref;
28734 dw_loc_descr_ref l;
28736 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28738 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28739 if (!rtl)
28740 return;
28741 decl = SYMBOL_REF_DECL (rtl);
28743 else
28744 decl = TREE_OPERAND (init, 0);
28745 ref = lookup_decl_die (decl);
28746 if (ref == NULL
28747 || (!get_AT (ref, DW_AT_location)
28748 && !get_AT (ref, DW_AT_const_value)))
28749 return;
28750 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28751 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28752 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28753 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28754 add_AT_loc (die, DW_AT_location, l);
28758 /* Return NULL if l is a DWARF expression, or first op that is not
28759 valid DWARF expression. */
28761 static dw_loc_descr_ref
28762 non_dwarf_expression (dw_loc_descr_ref l)
28764 while (l)
28766 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28767 return l;
28768 switch (l->dw_loc_opc)
28770 case DW_OP_regx:
28771 case DW_OP_implicit_value:
28772 case DW_OP_stack_value:
28773 case DW_OP_implicit_pointer:
28774 case DW_OP_GNU_implicit_pointer:
28775 case DW_OP_GNU_parameter_ref:
28776 case DW_OP_piece:
28777 case DW_OP_bit_piece:
28778 return l;
28779 default:
28780 break;
28782 l = l->dw_loc_next;
28784 return NULL;
28787 /* Return adjusted copy of EXPR:
28788 If it is empty DWARF expression, return it.
28789 If it is valid non-empty DWARF expression,
28790 return copy of EXPR with DW_OP_deref appended to it.
28791 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28792 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28793 If it is DWARF expression followed by DW_OP_stack_value, return
28794 copy of the DWARF expression without anything appended.
28795 Otherwise, return NULL. */
28797 static dw_loc_descr_ref
28798 copy_deref_exprloc (dw_loc_descr_ref expr)
28800 dw_loc_descr_ref tail = NULL;
28802 if (expr == NULL)
28803 return NULL;
28805 dw_loc_descr_ref l = non_dwarf_expression (expr);
28806 if (l && l->dw_loc_next)
28807 return NULL;
28809 if (l)
28811 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28812 tail = new_loc_descr ((enum dwarf_location_atom)
28813 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28814 0, 0);
28815 else
28816 switch (l->dw_loc_opc)
28818 case DW_OP_regx:
28819 tail = new_loc_descr (DW_OP_bregx,
28820 l->dw_loc_oprnd1.v.val_unsigned, 0);
28821 break;
28822 case DW_OP_stack_value:
28823 break;
28824 default:
28825 return NULL;
28828 else
28829 tail = new_loc_descr (DW_OP_deref, 0, 0);
28831 dw_loc_descr_ref ret = NULL, *p = &ret;
28832 while (expr != l)
28834 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28835 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28836 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28837 p = &(*p)->dw_loc_next;
28838 expr = expr->dw_loc_next;
28840 *p = tail;
28841 return ret;
28844 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28845 reference to a variable or argument, adjust it if needed and return:
28846 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28847 attribute if present should be removed
28848 0 keep the attribute perhaps with minor modifications, no need to rescan
28849 1 if the attribute has been successfully adjusted. */
28851 static int
28852 optimize_string_length (dw_attr_node *a)
28854 dw_loc_descr_ref l = AT_loc (a), lv;
28855 dw_die_ref die;
28856 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28858 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28859 die = lookup_decl_die (decl);
28860 if (die)
28862 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28863 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28864 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28866 else
28867 return -1;
28869 else
28870 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28872 /* DWARF5 allows reference class, so we can then reference the DIE.
28873 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28874 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28876 a->dw_attr_val.val_class = dw_val_class_die_ref;
28877 a->dw_attr_val.val_entry = NULL;
28878 a->dw_attr_val.v.val_die_ref.die = die;
28879 a->dw_attr_val.v.val_die_ref.external = 0;
28880 return 0;
28883 dw_attr_node *av = get_AT (die, DW_AT_location);
28884 dw_loc_list_ref d;
28885 bool non_dwarf_expr = false;
28887 if (av == NULL)
28888 return dwarf_strict ? -1 : 0;
28889 switch (AT_class (av))
28891 case dw_val_class_loc_list:
28892 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28893 if (d->expr && non_dwarf_expression (d->expr))
28894 non_dwarf_expr = true;
28895 break;
28896 case dw_val_class_loc:
28897 lv = AT_loc (av);
28898 if (lv == NULL)
28899 return dwarf_strict ? -1 : 0;
28900 if (non_dwarf_expression (lv))
28901 non_dwarf_expr = true;
28902 break;
28903 default:
28904 return dwarf_strict ? -1 : 0;
28907 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28908 into DW_OP_call4 or DW_OP_GNU_variable_value into
28909 DW_OP_call4 DW_OP_deref, do so. */
28910 if (!non_dwarf_expr
28911 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28913 l->dw_loc_opc = DW_OP_call4;
28914 if (l->dw_loc_next)
28915 l->dw_loc_next = NULL;
28916 else
28917 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28918 return 0;
28921 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28922 copy over the DW_AT_location attribute from die to a. */
28923 if (l->dw_loc_next != NULL)
28925 a->dw_attr_val = av->dw_attr_val;
28926 return 1;
28929 dw_loc_list_ref list, *p;
28930 switch (AT_class (av))
28932 case dw_val_class_loc_list:
28933 p = &list;
28934 list = NULL;
28935 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28937 lv = copy_deref_exprloc (d->expr);
28938 if (lv)
28940 *p = new_loc_list (lv, d->begin, d->end, d->section);
28941 p = &(*p)->dw_loc_next;
28943 else if (!dwarf_strict && d->expr)
28944 return 0;
28946 if (list == NULL)
28947 return dwarf_strict ? -1 : 0;
28948 a->dw_attr_val.val_class = dw_val_class_loc_list;
28949 gen_llsym (list);
28950 *AT_loc_list_ptr (a) = list;
28951 return 1;
28952 case dw_val_class_loc:
28953 lv = copy_deref_exprloc (AT_loc (av));
28954 if (lv == NULL)
28955 return dwarf_strict ? -1 : 0;
28956 a->dw_attr_val.v.val_loc = lv;
28957 return 1;
28958 default:
28959 gcc_unreachable ();
28963 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28964 an address in .rodata section if the string literal is emitted there,
28965 or remove the containing location list or replace DW_AT_const_value
28966 with DW_AT_location and empty location expression, if it isn't found
28967 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28968 to something that has been emitted in the current CU. */
28970 static void
28971 resolve_addr (dw_die_ref die)
28973 dw_die_ref c;
28974 dw_attr_node *a;
28975 dw_loc_list_ref *curr, *start, loc;
28976 unsigned ix;
28977 bool remove_AT_byte_size = false;
28979 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28980 switch (AT_class (a))
28982 case dw_val_class_loc_list:
28983 start = curr = AT_loc_list_ptr (a);
28984 loc = *curr;
28985 gcc_assert (loc);
28986 /* The same list can be referenced more than once. See if we have
28987 already recorded the result from a previous pass. */
28988 if (loc->replaced)
28989 *curr = loc->dw_loc_next;
28990 else if (!loc->resolved_addr)
28992 /* As things stand, we do not expect or allow one die to
28993 reference a suffix of another die's location list chain.
28994 References must be identical or completely separate.
28995 There is therefore no need to cache the result of this
28996 pass on any list other than the first; doing so
28997 would lead to unnecessary writes. */
28998 while (*curr)
29000 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29001 if (!resolve_addr_in_expr (a, (*curr)->expr))
29003 dw_loc_list_ref next = (*curr)->dw_loc_next;
29004 dw_loc_descr_ref l = (*curr)->expr;
29006 if (next && (*curr)->ll_symbol)
29008 gcc_assert (!next->ll_symbol);
29009 next->ll_symbol = (*curr)->ll_symbol;
29011 if (dwarf_split_debug_info)
29012 remove_loc_list_addr_table_entries (l);
29013 *curr = next;
29015 else
29017 mark_base_types ((*curr)->expr);
29018 curr = &(*curr)->dw_loc_next;
29021 if (loc == *start)
29022 loc->resolved_addr = 1;
29023 else
29025 loc->replaced = 1;
29026 loc->dw_loc_next = *start;
29029 if (!*start)
29031 remove_AT (die, a->dw_attr);
29032 ix--;
29034 break;
29035 case dw_val_class_loc:
29037 dw_loc_descr_ref l = AT_loc (a);
29038 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29039 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29040 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29041 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29042 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29043 with DW_FORM_ref referencing the same DIE as
29044 DW_OP_GNU_variable_value used to reference. */
29045 if (a->dw_attr == DW_AT_string_length
29046 && l
29047 && l->dw_loc_opc == DW_OP_GNU_variable_value
29048 && (l->dw_loc_next == NULL
29049 || (l->dw_loc_next->dw_loc_next == NULL
29050 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29052 switch (optimize_string_length (a))
29054 case -1:
29055 remove_AT (die, a->dw_attr);
29056 ix--;
29057 /* If we drop DW_AT_string_length, we need to drop also
29058 DW_AT_{string_length_,}byte_size. */
29059 remove_AT_byte_size = true;
29060 continue;
29061 default:
29062 break;
29063 case 1:
29064 /* Even if we keep the optimized DW_AT_string_length,
29065 it might have changed AT_class, so process it again. */
29066 ix--;
29067 continue;
29070 /* For -gdwarf-2 don't attempt to optimize
29071 DW_AT_data_member_location containing
29072 DW_OP_plus_uconst - older consumers might
29073 rely on it being that op instead of a more complex,
29074 but shorter, location description. */
29075 if ((dwarf_version > 2
29076 || a->dw_attr != DW_AT_data_member_location
29077 || l == NULL
29078 || l->dw_loc_opc != DW_OP_plus_uconst
29079 || l->dw_loc_next != NULL)
29080 && !resolve_addr_in_expr (a, l))
29082 if (dwarf_split_debug_info)
29083 remove_loc_list_addr_table_entries (l);
29084 if (l != NULL
29085 && l->dw_loc_next == NULL
29086 && l->dw_loc_opc == DW_OP_addr
29087 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29088 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29089 && a->dw_attr == DW_AT_location)
29091 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29092 remove_AT (die, a->dw_attr);
29093 ix--;
29094 optimize_location_into_implicit_ptr (die, decl);
29095 break;
29097 if (a->dw_attr == DW_AT_string_length)
29098 /* If we drop DW_AT_string_length, we need to drop also
29099 DW_AT_{string_length_,}byte_size. */
29100 remove_AT_byte_size = true;
29101 remove_AT (die, a->dw_attr);
29102 ix--;
29104 else
29105 mark_base_types (l);
29107 break;
29108 case dw_val_class_addr:
29109 if (a->dw_attr == DW_AT_const_value
29110 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29112 if (AT_index (a) != NOT_INDEXED)
29113 remove_addr_table_entry (a->dw_attr_val.val_entry);
29114 remove_AT (die, a->dw_attr);
29115 ix--;
29117 if ((die->die_tag == DW_TAG_call_site
29118 && a->dw_attr == DW_AT_call_origin)
29119 || (die->die_tag == DW_TAG_GNU_call_site
29120 && a->dw_attr == DW_AT_abstract_origin))
29122 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29123 dw_die_ref tdie = lookup_decl_die (tdecl);
29124 dw_die_ref cdie;
29125 if (tdie == NULL
29126 && DECL_EXTERNAL (tdecl)
29127 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29128 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29130 dw_die_ref pdie = cdie;
29131 /* Make sure we don't add these DIEs into type units.
29132 We could emit skeleton DIEs for context (namespaces,
29133 outer structs/classes) and a skeleton DIE for the
29134 innermost context with DW_AT_signature pointing to the
29135 type unit. See PR78835. */
29136 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29137 pdie = pdie->die_parent;
29138 if (pdie == NULL)
29140 /* Creating a full DIE for tdecl is overly expensive and
29141 at this point even wrong when in the LTO phase
29142 as it can end up generating new type DIEs we didn't
29143 output and thus optimize_external_refs will crash. */
29144 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29145 add_AT_flag (tdie, DW_AT_external, 1);
29146 add_AT_flag (tdie, DW_AT_declaration, 1);
29147 add_linkage_attr (tdie, tdecl);
29148 add_name_and_src_coords_attributes (tdie, tdecl, true);
29149 equate_decl_number_to_die (tdecl, tdie);
29152 if (tdie)
29154 a->dw_attr_val.val_class = dw_val_class_die_ref;
29155 a->dw_attr_val.v.val_die_ref.die = tdie;
29156 a->dw_attr_val.v.val_die_ref.external = 0;
29158 else
29160 if (AT_index (a) != NOT_INDEXED)
29161 remove_addr_table_entry (a->dw_attr_val.val_entry);
29162 remove_AT (die, a->dw_attr);
29163 ix--;
29166 break;
29167 default:
29168 break;
29171 if (remove_AT_byte_size)
29172 remove_AT (die, dwarf_version >= 5
29173 ? DW_AT_string_length_byte_size
29174 : DW_AT_byte_size);
29176 FOR_EACH_CHILD (die, c, resolve_addr (c));
29179 /* Helper routines for optimize_location_lists.
29180 This pass tries to share identical local lists in .debug_loc
29181 section. */
29183 /* Iteratively hash operands of LOC opcode into HSTATE. */
29185 static void
29186 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29188 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29189 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29191 switch (loc->dw_loc_opc)
29193 case DW_OP_const4u:
29194 case DW_OP_const8u:
29195 if (loc->dtprel)
29196 goto hash_addr;
29197 /* FALLTHRU */
29198 case DW_OP_const1u:
29199 case DW_OP_const1s:
29200 case DW_OP_const2u:
29201 case DW_OP_const2s:
29202 case DW_OP_const4s:
29203 case DW_OP_const8s:
29204 case DW_OP_constu:
29205 case DW_OP_consts:
29206 case DW_OP_pick:
29207 case DW_OP_plus_uconst:
29208 case DW_OP_breg0:
29209 case DW_OP_breg1:
29210 case DW_OP_breg2:
29211 case DW_OP_breg3:
29212 case DW_OP_breg4:
29213 case DW_OP_breg5:
29214 case DW_OP_breg6:
29215 case DW_OP_breg7:
29216 case DW_OP_breg8:
29217 case DW_OP_breg9:
29218 case DW_OP_breg10:
29219 case DW_OP_breg11:
29220 case DW_OP_breg12:
29221 case DW_OP_breg13:
29222 case DW_OP_breg14:
29223 case DW_OP_breg15:
29224 case DW_OP_breg16:
29225 case DW_OP_breg17:
29226 case DW_OP_breg18:
29227 case DW_OP_breg19:
29228 case DW_OP_breg20:
29229 case DW_OP_breg21:
29230 case DW_OP_breg22:
29231 case DW_OP_breg23:
29232 case DW_OP_breg24:
29233 case DW_OP_breg25:
29234 case DW_OP_breg26:
29235 case DW_OP_breg27:
29236 case DW_OP_breg28:
29237 case DW_OP_breg29:
29238 case DW_OP_breg30:
29239 case DW_OP_breg31:
29240 case DW_OP_regx:
29241 case DW_OP_fbreg:
29242 case DW_OP_piece:
29243 case DW_OP_deref_size:
29244 case DW_OP_xderef_size:
29245 hstate.add_object (val1->v.val_int);
29246 break;
29247 case DW_OP_skip:
29248 case DW_OP_bra:
29250 int offset;
29252 gcc_assert (val1->val_class == dw_val_class_loc);
29253 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29254 hstate.add_object (offset);
29256 break;
29257 case DW_OP_implicit_value:
29258 hstate.add_object (val1->v.val_unsigned);
29259 switch (val2->val_class)
29261 case dw_val_class_const:
29262 hstate.add_object (val2->v.val_int);
29263 break;
29264 case dw_val_class_vec:
29266 unsigned int elt_size = val2->v.val_vec.elt_size;
29267 unsigned int len = val2->v.val_vec.length;
29269 hstate.add_int (elt_size);
29270 hstate.add_int (len);
29271 hstate.add (val2->v.val_vec.array, len * elt_size);
29273 break;
29274 case dw_val_class_const_double:
29275 hstate.add_object (val2->v.val_double.low);
29276 hstate.add_object (val2->v.val_double.high);
29277 break;
29278 case dw_val_class_wide_int:
29279 hstate.add (val2->v.val_wide->get_val (),
29280 get_full_len (*val2->v.val_wide)
29281 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29282 break;
29283 case dw_val_class_addr:
29284 inchash::add_rtx (val2->v.val_addr, hstate);
29285 break;
29286 default:
29287 gcc_unreachable ();
29289 break;
29290 case DW_OP_bregx:
29291 case DW_OP_bit_piece:
29292 hstate.add_object (val1->v.val_int);
29293 hstate.add_object (val2->v.val_int);
29294 break;
29295 case DW_OP_addr:
29296 hash_addr:
29297 if (loc->dtprel)
29299 unsigned char dtprel = 0xd1;
29300 hstate.add_object (dtprel);
29302 inchash::add_rtx (val1->v.val_addr, hstate);
29303 break;
29304 case DW_OP_GNU_addr_index:
29305 case DW_OP_GNU_const_index:
29307 if (loc->dtprel)
29309 unsigned char dtprel = 0xd1;
29310 hstate.add_object (dtprel);
29312 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29314 break;
29315 case DW_OP_implicit_pointer:
29316 case DW_OP_GNU_implicit_pointer:
29317 hstate.add_int (val2->v.val_int);
29318 break;
29319 case DW_OP_entry_value:
29320 case DW_OP_GNU_entry_value:
29321 hstate.add_object (val1->v.val_loc);
29322 break;
29323 case DW_OP_regval_type:
29324 case DW_OP_deref_type:
29325 case DW_OP_GNU_regval_type:
29326 case DW_OP_GNU_deref_type:
29328 unsigned int byte_size
29329 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29330 unsigned int encoding
29331 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29332 hstate.add_object (val1->v.val_int);
29333 hstate.add_object (byte_size);
29334 hstate.add_object (encoding);
29336 break;
29337 case DW_OP_convert:
29338 case DW_OP_reinterpret:
29339 case DW_OP_GNU_convert:
29340 case DW_OP_GNU_reinterpret:
29341 if (val1->val_class == dw_val_class_unsigned_const)
29343 hstate.add_object (val1->v.val_unsigned);
29344 break;
29346 /* FALLTHRU */
29347 case DW_OP_const_type:
29348 case DW_OP_GNU_const_type:
29350 unsigned int byte_size
29351 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29352 unsigned int encoding
29353 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29354 hstate.add_object (byte_size);
29355 hstate.add_object (encoding);
29356 if (loc->dw_loc_opc != DW_OP_const_type
29357 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29358 break;
29359 hstate.add_object (val2->val_class);
29360 switch (val2->val_class)
29362 case dw_val_class_const:
29363 hstate.add_object (val2->v.val_int);
29364 break;
29365 case dw_val_class_vec:
29367 unsigned int elt_size = val2->v.val_vec.elt_size;
29368 unsigned int len = val2->v.val_vec.length;
29370 hstate.add_object (elt_size);
29371 hstate.add_object (len);
29372 hstate.add (val2->v.val_vec.array, len * elt_size);
29374 break;
29375 case dw_val_class_const_double:
29376 hstate.add_object (val2->v.val_double.low);
29377 hstate.add_object (val2->v.val_double.high);
29378 break;
29379 case dw_val_class_wide_int:
29380 hstate.add (val2->v.val_wide->get_val (),
29381 get_full_len (*val2->v.val_wide)
29382 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29383 break;
29384 default:
29385 gcc_unreachable ();
29388 break;
29390 default:
29391 /* Other codes have no operands. */
29392 break;
29396 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29398 static inline void
29399 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29401 dw_loc_descr_ref l;
29402 bool sizes_computed = false;
29403 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29404 size_of_locs (loc);
29406 for (l = loc; l != NULL; l = l->dw_loc_next)
29408 enum dwarf_location_atom opc = l->dw_loc_opc;
29409 hstate.add_object (opc);
29410 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29412 size_of_locs (loc);
29413 sizes_computed = true;
29415 hash_loc_operands (l, hstate);
29419 /* Compute hash of the whole location list LIST_HEAD. */
29421 static inline void
29422 hash_loc_list (dw_loc_list_ref list_head)
29424 dw_loc_list_ref curr = list_head;
29425 inchash::hash hstate;
29427 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29429 hstate.add (curr->begin, strlen (curr->begin) + 1);
29430 hstate.add (curr->end, strlen (curr->end) + 1);
29431 if (curr->section)
29432 hstate.add (curr->section, strlen (curr->section) + 1);
29433 hash_locs (curr->expr, hstate);
29435 list_head->hash = hstate.end ();
29438 /* Return true if X and Y opcodes have the same operands. */
29440 static inline bool
29441 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29443 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29444 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29445 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29446 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29448 switch (x->dw_loc_opc)
29450 case DW_OP_const4u:
29451 case DW_OP_const8u:
29452 if (x->dtprel)
29453 goto hash_addr;
29454 /* FALLTHRU */
29455 case DW_OP_const1u:
29456 case DW_OP_const1s:
29457 case DW_OP_const2u:
29458 case DW_OP_const2s:
29459 case DW_OP_const4s:
29460 case DW_OP_const8s:
29461 case DW_OP_constu:
29462 case DW_OP_consts:
29463 case DW_OP_pick:
29464 case DW_OP_plus_uconst:
29465 case DW_OP_breg0:
29466 case DW_OP_breg1:
29467 case DW_OP_breg2:
29468 case DW_OP_breg3:
29469 case DW_OP_breg4:
29470 case DW_OP_breg5:
29471 case DW_OP_breg6:
29472 case DW_OP_breg7:
29473 case DW_OP_breg8:
29474 case DW_OP_breg9:
29475 case DW_OP_breg10:
29476 case DW_OP_breg11:
29477 case DW_OP_breg12:
29478 case DW_OP_breg13:
29479 case DW_OP_breg14:
29480 case DW_OP_breg15:
29481 case DW_OP_breg16:
29482 case DW_OP_breg17:
29483 case DW_OP_breg18:
29484 case DW_OP_breg19:
29485 case DW_OP_breg20:
29486 case DW_OP_breg21:
29487 case DW_OP_breg22:
29488 case DW_OP_breg23:
29489 case DW_OP_breg24:
29490 case DW_OP_breg25:
29491 case DW_OP_breg26:
29492 case DW_OP_breg27:
29493 case DW_OP_breg28:
29494 case DW_OP_breg29:
29495 case DW_OP_breg30:
29496 case DW_OP_breg31:
29497 case DW_OP_regx:
29498 case DW_OP_fbreg:
29499 case DW_OP_piece:
29500 case DW_OP_deref_size:
29501 case DW_OP_xderef_size:
29502 return valx1->v.val_int == valy1->v.val_int;
29503 case DW_OP_skip:
29504 case DW_OP_bra:
29505 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29506 can cause irrelevant differences in dw_loc_addr. */
29507 gcc_assert (valx1->val_class == dw_val_class_loc
29508 && valy1->val_class == dw_val_class_loc
29509 && (dwarf_split_debug_info
29510 || x->dw_loc_addr == y->dw_loc_addr));
29511 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29512 case DW_OP_implicit_value:
29513 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29514 || valx2->val_class != valy2->val_class)
29515 return false;
29516 switch (valx2->val_class)
29518 case dw_val_class_const:
29519 return valx2->v.val_int == valy2->v.val_int;
29520 case dw_val_class_vec:
29521 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29522 && valx2->v.val_vec.length == valy2->v.val_vec.length
29523 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29524 valx2->v.val_vec.elt_size
29525 * valx2->v.val_vec.length) == 0;
29526 case dw_val_class_const_double:
29527 return valx2->v.val_double.low == valy2->v.val_double.low
29528 && valx2->v.val_double.high == valy2->v.val_double.high;
29529 case dw_val_class_wide_int:
29530 return *valx2->v.val_wide == *valy2->v.val_wide;
29531 case dw_val_class_addr:
29532 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29533 default:
29534 gcc_unreachable ();
29536 case DW_OP_bregx:
29537 case DW_OP_bit_piece:
29538 return valx1->v.val_int == valy1->v.val_int
29539 && valx2->v.val_int == valy2->v.val_int;
29540 case DW_OP_addr:
29541 hash_addr:
29542 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29543 case DW_OP_GNU_addr_index:
29544 case DW_OP_GNU_const_index:
29546 rtx ax1 = valx1->val_entry->addr.rtl;
29547 rtx ay1 = valy1->val_entry->addr.rtl;
29548 return rtx_equal_p (ax1, ay1);
29550 case DW_OP_implicit_pointer:
29551 case DW_OP_GNU_implicit_pointer:
29552 return valx1->val_class == dw_val_class_die_ref
29553 && valx1->val_class == valy1->val_class
29554 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29555 && valx2->v.val_int == valy2->v.val_int;
29556 case DW_OP_entry_value:
29557 case DW_OP_GNU_entry_value:
29558 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29559 case DW_OP_const_type:
29560 case DW_OP_GNU_const_type:
29561 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29562 || valx2->val_class != valy2->val_class)
29563 return false;
29564 switch (valx2->val_class)
29566 case dw_val_class_const:
29567 return valx2->v.val_int == valy2->v.val_int;
29568 case dw_val_class_vec:
29569 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29570 && valx2->v.val_vec.length == valy2->v.val_vec.length
29571 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29572 valx2->v.val_vec.elt_size
29573 * valx2->v.val_vec.length) == 0;
29574 case dw_val_class_const_double:
29575 return valx2->v.val_double.low == valy2->v.val_double.low
29576 && valx2->v.val_double.high == valy2->v.val_double.high;
29577 case dw_val_class_wide_int:
29578 return *valx2->v.val_wide == *valy2->v.val_wide;
29579 default:
29580 gcc_unreachable ();
29582 case DW_OP_regval_type:
29583 case DW_OP_deref_type:
29584 case DW_OP_GNU_regval_type:
29585 case DW_OP_GNU_deref_type:
29586 return valx1->v.val_int == valy1->v.val_int
29587 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29588 case DW_OP_convert:
29589 case DW_OP_reinterpret:
29590 case DW_OP_GNU_convert:
29591 case DW_OP_GNU_reinterpret:
29592 if (valx1->val_class != valy1->val_class)
29593 return false;
29594 if (valx1->val_class == dw_val_class_unsigned_const)
29595 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29596 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29597 case DW_OP_GNU_parameter_ref:
29598 return valx1->val_class == dw_val_class_die_ref
29599 && valx1->val_class == valy1->val_class
29600 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29601 default:
29602 /* Other codes have no operands. */
29603 return true;
29607 /* Return true if DWARF location expressions X and Y are the same. */
29609 static inline bool
29610 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29612 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29613 if (x->dw_loc_opc != y->dw_loc_opc
29614 || x->dtprel != y->dtprel
29615 || !compare_loc_operands (x, y))
29616 break;
29617 return x == NULL && y == NULL;
29620 /* Hashtable helpers. */
29622 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29624 static inline hashval_t hash (const dw_loc_list_struct *);
29625 static inline bool equal (const dw_loc_list_struct *,
29626 const dw_loc_list_struct *);
29629 /* Return precomputed hash of location list X. */
29631 inline hashval_t
29632 loc_list_hasher::hash (const dw_loc_list_struct *x)
29634 return x->hash;
29637 /* Return true if location lists A and B are the same. */
29639 inline bool
29640 loc_list_hasher::equal (const dw_loc_list_struct *a,
29641 const dw_loc_list_struct *b)
29643 if (a == b)
29644 return 1;
29645 if (a->hash != b->hash)
29646 return 0;
29647 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29648 if (strcmp (a->begin, b->begin) != 0
29649 || strcmp (a->end, b->end) != 0
29650 || (a->section == NULL) != (b->section == NULL)
29651 || (a->section && strcmp (a->section, b->section) != 0)
29652 || !compare_locs (a->expr, b->expr))
29653 break;
29654 return a == NULL && b == NULL;
29657 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29660 /* Recursively optimize location lists referenced from DIE
29661 children and share them whenever possible. */
29663 static void
29664 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29666 dw_die_ref c;
29667 dw_attr_node *a;
29668 unsigned ix;
29669 dw_loc_list_struct **slot;
29671 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29672 if (AT_class (a) == dw_val_class_loc_list)
29674 dw_loc_list_ref list = AT_loc_list (a);
29675 /* TODO: perform some optimizations here, before hashing
29676 it and storing into the hash table. */
29677 hash_loc_list (list);
29678 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29679 if (*slot == NULL)
29680 *slot = list;
29681 else
29682 a->dw_attr_val.v.val_loc_list = *slot;
29685 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29689 /* Recursively assign each location list a unique index into the debug_addr
29690 section. */
29692 static void
29693 index_location_lists (dw_die_ref die)
29695 dw_die_ref c;
29696 dw_attr_node *a;
29697 unsigned ix;
29699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29700 if (AT_class (a) == dw_val_class_loc_list)
29702 dw_loc_list_ref list = AT_loc_list (a);
29703 dw_loc_list_ref curr;
29704 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29706 /* Don't index an entry that has already been indexed
29707 or won't be output. */
29708 if (curr->begin_entry != NULL
29709 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29710 continue;
29712 curr->begin_entry
29713 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29717 FOR_EACH_CHILD (die, c, index_location_lists (c));
29720 /* Optimize location lists referenced from DIE
29721 children and share them whenever possible. */
29723 static void
29724 optimize_location_lists (dw_die_ref die)
29726 loc_list_hash_type htab (500);
29727 optimize_location_lists_1 (die, &htab);
29730 /* Traverse the limbo die list, and add parent/child links. The only
29731 dies without parents that should be here are concrete instances of
29732 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29733 For concrete instances, we can get the parent die from the abstract
29734 instance. */
29736 static void
29737 flush_limbo_die_list (void)
29739 limbo_die_node *node;
29741 /* get_context_die calls force_decl_die, which can put new DIEs on the
29742 limbo list in LTO mode when nested functions are put in a different
29743 partition than that of their parent function. */
29744 while ((node = limbo_die_list))
29746 dw_die_ref die = node->die;
29747 limbo_die_list = node->next;
29749 if (die->die_parent == NULL)
29751 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29753 if (origin && origin->die_parent)
29754 add_child_die (origin->die_parent, die);
29755 else if (is_cu_die (die))
29757 else if (seen_error ())
29758 /* It's OK to be confused by errors in the input. */
29759 add_child_die (comp_unit_die (), die);
29760 else
29762 /* In certain situations, the lexical block containing a
29763 nested function can be optimized away, which results
29764 in the nested function die being orphaned. Likewise
29765 with the return type of that nested function. Force
29766 this to be a child of the containing function.
29768 It may happen that even the containing function got fully
29769 inlined and optimized out. In that case we are lost and
29770 assign the empty child. This should not be big issue as
29771 the function is likely unreachable too. */
29772 gcc_assert (node->created_for);
29774 if (DECL_P (node->created_for))
29775 origin = get_context_die (DECL_CONTEXT (node->created_for));
29776 else if (TYPE_P (node->created_for))
29777 origin = scope_die_for (node->created_for, comp_unit_die ());
29778 else
29779 origin = comp_unit_die ();
29781 add_child_die (origin, die);
29787 /* Reset DIEs so we can output them again. */
29789 static void
29790 reset_dies (dw_die_ref die)
29792 dw_die_ref c;
29794 /* Remove stuff we re-generate. */
29795 die->die_mark = 0;
29796 die->die_offset = 0;
29797 die->die_abbrev = 0;
29798 remove_AT (die, DW_AT_sibling);
29800 FOR_EACH_CHILD (die, c, reset_dies (c));
29803 /* Output stuff that dwarf requires at the end of every file,
29804 and generate the DWARF-2 debugging info. */
29806 static void
29807 dwarf2out_finish (const char *)
29809 comdat_type_node *ctnode;
29810 dw_die_ref main_comp_unit_die;
29811 unsigned char checksum[16];
29812 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29814 /* Flush out any latecomers to the limbo party. */
29815 flush_limbo_die_list ();
29817 if (flag_checking)
29819 verify_die (comp_unit_die ());
29820 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29821 verify_die (node->die);
29824 /* We shouldn't have any symbols with delayed asm names for
29825 DIEs generated after early finish. */
29826 gcc_assert (deferred_asm_name == NULL);
29828 gen_remaining_tmpl_value_param_die_attribute ();
29830 if (flag_generate_lto || flag_generate_offload)
29832 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29834 /* Prune stuff so that dwarf2out_finish runs successfully
29835 for the fat part of the object. */
29836 reset_dies (comp_unit_die ());
29837 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29838 reset_dies (node->die);
29840 hash_table<comdat_type_hasher> comdat_type_table (100);
29841 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29843 comdat_type_node **slot
29844 = comdat_type_table.find_slot (ctnode, INSERT);
29846 /* Don't reset types twice. */
29847 if (*slot != HTAB_EMPTY_ENTRY)
29848 continue;
29850 /* Add a pointer to the line table for the main compilation unit
29851 so that the debugger can make sense of DW_AT_decl_file
29852 attributes. */
29853 if (debug_info_level >= DINFO_LEVEL_TERSE)
29854 reset_dies (ctnode->root_die);
29856 *slot = ctnode;
29859 /* Reset die CU symbol so we don't output it twice. */
29860 comp_unit_die ()->die_id.die_symbol = NULL;
29862 /* Remove DW_AT_macro from the early output. */
29863 if (have_macinfo)
29864 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29866 /* Remove indirect string decisions. */
29867 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29870 #if ENABLE_ASSERT_CHECKING
29872 dw_die_ref die = comp_unit_die (), c;
29873 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29875 #endif
29876 resolve_addr (comp_unit_die ());
29877 move_marked_base_types ();
29879 /* Initialize sections and labels used for actual assembler output. */
29880 init_sections_and_labels (false);
29882 /* Traverse the DIE's and add sibling attributes to those DIE's that
29883 have children. */
29884 add_sibling_attributes (comp_unit_die ());
29885 limbo_die_node *node;
29886 for (node = cu_die_list; node; node = node->next)
29887 add_sibling_attributes (node->die);
29888 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29889 add_sibling_attributes (ctnode->root_die);
29891 /* When splitting DWARF info, we put some attributes in the
29892 skeleton compile_unit DIE that remains in the .o, while
29893 most attributes go in the DWO compile_unit_die. */
29894 if (dwarf_split_debug_info)
29896 limbo_die_node *cu;
29897 main_comp_unit_die = gen_compile_unit_die (NULL);
29898 if (dwarf_version >= 5)
29899 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29900 cu = limbo_die_list;
29901 gcc_assert (cu->die == main_comp_unit_die);
29902 limbo_die_list = limbo_die_list->next;
29903 cu->next = cu_die_list;
29904 cu_die_list = cu;
29906 else
29907 main_comp_unit_die = comp_unit_die ();
29909 /* Output a terminator label for the .text section. */
29910 switch_to_section (text_section);
29911 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29912 if (cold_text_section)
29914 switch_to_section (cold_text_section);
29915 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29918 /* We can only use the low/high_pc attributes if all of the code was
29919 in .text. */
29920 if (!have_multiple_function_sections
29921 || (dwarf_version < 3 && dwarf_strict))
29923 /* Don't add if the CU has no associated code. */
29924 if (text_section_used)
29925 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29926 text_end_label, true);
29928 else
29930 unsigned fde_idx;
29931 dw_fde_ref fde;
29932 bool range_list_added = false;
29934 if (text_section_used)
29935 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29936 text_end_label, &range_list_added, true);
29937 if (cold_text_section_used)
29938 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29939 cold_end_label, &range_list_added, true);
29941 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29943 if (DECL_IGNORED_P (fde->decl))
29944 continue;
29945 if (!fde->in_std_section)
29946 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29947 fde->dw_fde_end, &range_list_added,
29948 true);
29949 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29950 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29951 fde->dw_fde_second_end, &range_list_added,
29952 true);
29955 if (range_list_added)
29957 /* We need to give .debug_loc and .debug_ranges an appropriate
29958 "base address". Use zero so that these addresses become
29959 absolute. Historically, we've emitted the unexpected
29960 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29961 Emit both to give time for other tools to adapt. */
29962 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29963 if (! dwarf_strict && dwarf_version < 4)
29964 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29966 add_ranges (NULL);
29970 /* AIX Assembler inserts the length, so adjust the reference to match the
29971 offset expected by debuggers. */
29972 strcpy (dl_section_ref, debug_line_section_label);
29973 if (XCOFF_DEBUGGING_INFO)
29974 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29976 if (debug_info_level >= DINFO_LEVEL_TERSE)
29977 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29978 dl_section_ref);
29980 if (have_macinfo)
29981 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29982 macinfo_section_label);
29984 if (dwarf_split_debug_info)
29986 if (have_location_lists)
29988 if (dwarf_version >= 5)
29989 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29990 loc_section_label);
29991 /* optimize_location_lists calculates the size of the lists,
29992 so index them first, and assign indices to the entries.
29993 Although optimize_location_lists will remove entries from
29994 the table, it only does so for duplicates, and therefore
29995 only reduces ref_counts to 1. */
29996 index_location_lists (comp_unit_die ());
29999 if (addr_index_table != NULL)
30001 unsigned int index = 0;
30002 addr_index_table
30003 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30004 (&index);
30008 loc_list_idx = 0;
30009 if (have_location_lists)
30011 optimize_location_lists (comp_unit_die ());
30012 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30013 if (dwarf_version >= 5 && dwarf_split_debug_info)
30014 assign_location_list_indexes (comp_unit_die ());
30017 save_macinfo_strings ();
30019 if (dwarf_split_debug_info)
30021 unsigned int index = 0;
30023 /* Add attributes common to skeleton compile_units and
30024 type_units. Because these attributes include strings, it
30025 must be done before freezing the string table. Top-level
30026 skeleton die attrs are added when the skeleton type unit is
30027 created, so ensure it is created by this point. */
30028 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30029 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30032 /* Output all of the compilation units. We put the main one last so that
30033 the offsets are available to output_pubnames. */
30034 for (node = cu_die_list; node; node = node->next)
30035 output_comp_unit (node->die, 0, NULL);
30037 hash_table<comdat_type_hasher> comdat_type_table (100);
30038 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30040 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30042 /* Don't output duplicate types. */
30043 if (*slot != HTAB_EMPTY_ENTRY)
30044 continue;
30046 /* Add a pointer to the line table for the main compilation unit
30047 so that the debugger can make sense of DW_AT_decl_file
30048 attributes. */
30049 if (debug_info_level >= DINFO_LEVEL_TERSE)
30050 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30051 (!dwarf_split_debug_info
30052 ? dl_section_ref
30053 : debug_skeleton_line_section_label));
30055 output_comdat_type_unit (ctnode);
30056 *slot = ctnode;
30059 if (dwarf_split_debug_info)
30061 int mark;
30062 struct md5_ctx ctx;
30064 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30065 index_rnglists ();
30067 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30068 md5_init_ctx (&ctx);
30069 mark = 0;
30070 die_checksum (comp_unit_die (), &ctx, &mark);
30071 unmark_all_dies (comp_unit_die ());
30072 md5_finish_ctx (&ctx, checksum);
30074 if (dwarf_version < 5)
30076 /* Use the first 8 bytes of the checksum as the dwo_id,
30077 and add it to both comp-unit DIEs. */
30078 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30079 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30082 /* Add the base offset of the ranges table to the skeleton
30083 comp-unit DIE. */
30084 if (!vec_safe_is_empty (ranges_table))
30086 if (dwarf_version >= 5)
30087 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30088 ranges_base_label);
30089 else
30090 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30091 ranges_section_label);
30094 switch_to_section (debug_addr_section);
30095 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30096 output_addr_table ();
30099 /* Output the main compilation unit if non-empty or if .debug_macinfo
30100 or .debug_macro will be emitted. */
30101 output_comp_unit (comp_unit_die (), have_macinfo,
30102 dwarf_split_debug_info ? checksum : NULL);
30104 if (dwarf_split_debug_info && info_section_emitted)
30105 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30107 /* Output the abbreviation table. */
30108 if (vec_safe_length (abbrev_die_table) != 1)
30110 switch_to_section (debug_abbrev_section);
30111 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30112 output_abbrev_section ();
30115 /* Output location list section if necessary. */
30116 if (have_location_lists)
30118 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30119 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30120 /* Output the location lists info. */
30121 switch_to_section (debug_loc_section);
30122 if (dwarf_version >= 5)
30124 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30125 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30126 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30127 dw2_asm_output_data (4, 0xffffffff,
30128 "Initial length escape value indicating "
30129 "64-bit DWARF extension");
30130 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30131 "Length of Location Lists");
30132 ASM_OUTPUT_LABEL (asm_out_file, l1);
30133 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30134 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30135 dw2_asm_output_data (1, 0, "Segment Size");
30136 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30137 "Offset Entry Count");
30139 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30140 if (dwarf_version >= 5 && dwarf_split_debug_info)
30142 unsigned int save_loc_list_idx = loc_list_idx;
30143 loc_list_idx = 0;
30144 output_loclists_offsets (comp_unit_die ());
30145 gcc_assert (save_loc_list_idx == loc_list_idx);
30147 output_location_lists (comp_unit_die ());
30148 if (dwarf_version >= 5)
30149 ASM_OUTPUT_LABEL (asm_out_file, l2);
30152 output_pubtables ();
30154 /* Output the address range information if a CU (.debug_info section)
30155 was emitted. We output an empty table even if we had no functions
30156 to put in it. This because the consumer has no way to tell the
30157 difference between an empty table that we omitted and failure to
30158 generate a table that would have contained data. */
30159 if (info_section_emitted)
30161 switch_to_section (debug_aranges_section);
30162 output_aranges ();
30165 /* Output ranges section if necessary. */
30166 if (!vec_safe_is_empty (ranges_table))
30168 if (dwarf_version >= 5)
30169 output_rnglists ();
30170 else
30171 output_ranges ();
30174 /* Have to end the macro section. */
30175 if (have_macinfo)
30177 switch_to_section (debug_macinfo_section);
30178 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30179 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30180 : debug_skeleton_line_section_label, false);
30181 dw2_asm_output_data (1, 0, "End compilation unit");
30184 /* Output the source line correspondence table. We must do this
30185 even if there is no line information. Otherwise, on an empty
30186 translation unit, we will generate a present, but empty,
30187 .debug_info section. IRIX 6.5 `nm' will then complain when
30188 examining the file. This is done late so that any filenames
30189 used by the debug_info section are marked as 'used'. */
30190 switch_to_section (debug_line_section);
30191 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30192 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30193 output_line_info (false);
30195 if (dwarf_split_debug_info && info_section_emitted)
30197 switch_to_section (debug_skeleton_line_section);
30198 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30199 output_line_info (true);
30202 /* If we emitted any indirect strings, output the string table too. */
30203 if (debug_str_hash || skeleton_debug_str_hash)
30204 output_indirect_strings ();
30205 if (debug_line_str_hash)
30207 switch_to_section (debug_line_str_section);
30208 const enum dwarf_form form = DW_FORM_line_strp;
30209 debug_line_str_hash->traverse<enum dwarf_form,
30210 output_indirect_string> (form);
30214 /* Returns a hash value for X (which really is a variable_value_struct). */
30216 inline hashval_t
30217 variable_value_hasher::hash (variable_value_struct *x)
30219 return (hashval_t) x->decl_id;
30222 /* Return nonzero if decl_id of variable_value_struct X is the same as
30223 UID of decl Y. */
30225 inline bool
30226 variable_value_hasher::equal (variable_value_struct *x, tree y)
30228 return x->decl_id == DECL_UID (y);
30231 /* Helper function for resolve_variable_value, handle
30232 DW_OP_GNU_variable_value in one location expression.
30233 Return true if exprloc has been changed into loclist. */
30235 static bool
30236 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30238 dw_loc_descr_ref next;
30239 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30241 next = loc->dw_loc_next;
30242 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30243 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30244 continue;
30246 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30247 if (DECL_CONTEXT (decl) != current_function_decl)
30248 continue;
30250 dw_die_ref ref = lookup_decl_die (decl);
30251 if (ref)
30253 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30254 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30255 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30256 continue;
30258 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30259 if (l == NULL)
30260 continue;
30261 if (l->dw_loc_next)
30263 if (AT_class (a) != dw_val_class_loc)
30264 continue;
30265 switch (a->dw_attr)
30267 /* Following attributes allow both exprloc and loclist
30268 classes, so we can change them into a loclist. */
30269 case DW_AT_location:
30270 case DW_AT_string_length:
30271 case DW_AT_return_addr:
30272 case DW_AT_data_member_location:
30273 case DW_AT_frame_base:
30274 case DW_AT_segment:
30275 case DW_AT_static_link:
30276 case DW_AT_use_location:
30277 case DW_AT_vtable_elem_location:
30278 if (prev)
30280 prev->dw_loc_next = NULL;
30281 prepend_loc_descr_to_each (l, AT_loc (a));
30283 if (next)
30284 add_loc_descr_to_each (l, next);
30285 a->dw_attr_val.val_class = dw_val_class_loc_list;
30286 a->dw_attr_val.val_entry = NULL;
30287 a->dw_attr_val.v.val_loc_list = l;
30288 have_location_lists = true;
30289 return true;
30290 /* Following attributes allow both exprloc and reference,
30291 so if the whole expression is DW_OP_GNU_variable_value alone
30292 we could transform it into reference. */
30293 case DW_AT_byte_size:
30294 case DW_AT_bit_size:
30295 case DW_AT_lower_bound:
30296 case DW_AT_upper_bound:
30297 case DW_AT_bit_stride:
30298 case DW_AT_count:
30299 case DW_AT_allocated:
30300 case DW_AT_associated:
30301 case DW_AT_byte_stride:
30302 if (prev == NULL && next == NULL)
30303 break;
30304 /* FALLTHRU */
30305 default:
30306 if (dwarf_strict)
30307 continue;
30308 break;
30310 /* Create DW_TAG_variable that we can refer to. */
30311 gen_decl_die (decl, NULL_TREE, NULL,
30312 lookup_decl_die (current_function_decl));
30313 ref = lookup_decl_die (decl);
30314 if (ref)
30316 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30317 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30318 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30320 continue;
30322 if (prev)
30324 prev->dw_loc_next = l->expr;
30325 add_loc_descr (&prev->dw_loc_next, next);
30326 free_loc_descr (loc, NULL);
30327 next = prev->dw_loc_next;
30329 else
30331 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30332 add_loc_descr (&loc, next);
30333 next = loc;
30335 loc = prev;
30337 return false;
30340 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30342 static void
30343 resolve_variable_value (dw_die_ref die)
30345 dw_attr_node *a;
30346 dw_loc_list_ref loc;
30347 unsigned ix;
30349 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30350 switch (AT_class (a))
30352 case dw_val_class_loc:
30353 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30354 break;
30355 /* FALLTHRU */
30356 case dw_val_class_loc_list:
30357 loc = AT_loc_list (a);
30358 gcc_assert (loc);
30359 for (; loc; loc = loc->dw_loc_next)
30360 resolve_variable_value_in_expr (a, loc->expr);
30361 break;
30362 default:
30363 break;
30367 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30368 temporaries in the current function. */
30370 static void
30371 resolve_variable_values (void)
30373 if (!variable_value_hash || !current_function_decl)
30374 return;
30376 struct variable_value_struct *node
30377 = variable_value_hash->find_with_hash (current_function_decl,
30378 DECL_UID (current_function_decl));
30380 if (node == NULL)
30381 return;
30383 unsigned int i;
30384 dw_die_ref die;
30385 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30386 resolve_variable_value (die);
30389 /* Helper function for note_variable_value, handle one location
30390 expression. */
30392 static void
30393 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30395 for (; loc; loc = loc->dw_loc_next)
30396 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30397 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30399 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30400 dw_die_ref ref = lookup_decl_die (decl);
30401 if (! ref && (flag_generate_lto || flag_generate_offload))
30403 /* ??? This is somewhat a hack because we do not create DIEs
30404 for variables not in BLOCK trees early but when generating
30405 early LTO output we need the dw_val_class_decl_ref to be
30406 fully resolved. For fat LTO objects we'd also like to
30407 undo this after LTO dwarf output. */
30408 gcc_assert (DECL_CONTEXT (decl));
30409 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30410 gcc_assert (ctx != NULL);
30411 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30412 ref = lookup_decl_die (decl);
30413 gcc_assert (ref != NULL);
30415 if (ref)
30417 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30418 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30419 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30420 continue;
30422 if (VAR_P (decl)
30423 && DECL_CONTEXT (decl)
30424 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30425 && lookup_decl_die (DECL_CONTEXT (decl)))
30427 if (!variable_value_hash)
30428 variable_value_hash
30429 = hash_table<variable_value_hasher>::create_ggc (10);
30431 tree fndecl = DECL_CONTEXT (decl);
30432 struct variable_value_struct *node;
30433 struct variable_value_struct **slot
30434 = variable_value_hash->find_slot_with_hash (fndecl,
30435 DECL_UID (fndecl),
30436 INSERT);
30437 if (*slot == NULL)
30439 node = ggc_cleared_alloc<variable_value_struct> ();
30440 node->decl_id = DECL_UID (fndecl);
30441 *slot = node;
30443 else
30444 node = *slot;
30446 vec_safe_push (node->dies, die);
30451 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30452 with dw_val_class_decl_ref operand. */
30454 static void
30455 note_variable_value (dw_die_ref die)
30457 dw_die_ref c;
30458 dw_attr_node *a;
30459 dw_loc_list_ref loc;
30460 unsigned ix;
30462 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30463 switch (AT_class (a))
30465 case dw_val_class_loc_list:
30466 loc = AT_loc_list (a);
30467 gcc_assert (loc);
30468 if (!loc->noted_variable_value)
30470 loc->noted_variable_value = 1;
30471 for (; loc; loc = loc->dw_loc_next)
30472 note_variable_value_in_expr (die, loc->expr);
30474 break;
30475 case dw_val_class_loc:
30476 note_variable_value_in_expr (die, AT_loc (a));
30477 break;
30478 default:
30479 break;
30482 /* Mark children. */
30483 FOR_EACH_CHILD (die, c, note_variable_value (c));
30486 /* Perform any cleanups needed after the early debug generation pass
30487 has run. */
30489 static void
30490 dwarf2out_early_finish (const char *filename)
30492 set_early_dwarf s;
30494 /* PCH might result in DW_AT_producer string being restored from the
30495 header compilation, so always fill it with empty string initially
30496 and overwrite only here. */
30497 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30498 producer_string = gen_producer_string ();
30499 producer->dw_attr_val.v.val_str->refcount--;
30500 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30502 /* Add the name for the main input file now. We delayed this from
30503 dwarf2out_init to avoid complications with PCH. */
30504 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30505 add_comp_dir_attribute (comp_unit_die ());
30507 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30508 DW_AT_comp_dir into .debug_line_str section. */
30509 if (!DWARF2_ASM_LINE_DEBUG_INFO
30510 && dwarf_version >= 5
30511 && DWARF5_USE_DEBUG_LINE_STR)
30513 for (int i = 0; i < 2; i++)
30515 dw_attr_node *a = get_AT (comp_unit_die (),
30516 i ? DW_AT_comp_dir : DW_AT_name);
30517 if (a == NULL
30518 || AT_class (a) != dw_val_class_str
30519 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30520 continue;
30522 if (! debug_line_str_hash)
30523 debug_line_str_hash
30524 = hash_table<indirect_string_hasher>::create_ggc (10);
30526 struct indirect_string_node *node
30527 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30528 set_indirect_string (node);
30529 node->form = DW_FORM_line_strp;
30530 a->dw_attr_val.v.val_str->refcount--;
30531 a->dw_attr_val.v.val_str = node;
30535 /* With LTO early dwarf was really finished at compile-time, so make
30536 sure to adjust the phase after annotating the LTRANS CU DIE. */
30537 if (in_lto_p)
30539 early_dwarf_finished = true;
30540 return;
30543 /* Walk through the list of incomplete types again, trying once more to
30544 emit full debugging info for them. */
30545 retry_incomplete_types ();
30547 /* The point here is to flush out the limbo list so that it is empty
30548 and we don't need to stream it for LTO. */
30549 flush_limbo_die_list ();
30551 gen_scheduled_generic_parms_dies ();
30552 gen_remaining_tmpl_value_param_die_attribute ();
30554 /* Add DW_AT_linkage_name for all deferred DIEs. */
30555 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30557 tree decl = node->created_for;
30558 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30559 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30560 ended up in deferred_asm_name before we knew it was
30561 constant and never written to disk. */
30562 && DECL_ASSEMBLER_NAME (decl))
30564 add_linkage_attr (node->die, decl);
30565 move_linkage_attr (node->die);
30568 deferred_asm_name = NULL;
30570 if (flag_eliminate_unused_debug_types)
30571 prune_unused_types ();
30573 /* Generate separate COMDAT sections for type DIEs. */
30574 if (use_debug_types)
30576 break_out_comdat_types (comp_unit_die ());
30578 /* Each new type_unit DIE was added to the limbo die list when created.
30579 Since these have all been added to comdat_type_list, clear the
30580 limbo die list. */
30581 limbo_die_list = NULL;
30583 /* For each new comdat type unit, copy declarations for incomplete
30584 types to make the new unit self-contained (i.e., no direct
30585 references to the main compile unit). */
30586 for (comdat_type_node *ctnode = comdat_type_list;
30587 ctnode != NULL; ctnode = ctnode->next)
30588 copy_decls_for_unworthy_types (ctnode->root_die);
30589 copy_decls_for_unworthy_types (comp_unit_die ());
30591 /* In the process of copying declarations from one unit to another,
30592 we may have left some declarations behind that are no longer
30593 referenced. Prune them. */
30594 prune_unused_types ();
30597 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30598 with dw_val_class_decl_ref operand. */
30599 note_variable_value (comp_unit_die ());
30600 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30601 note_variable_value (node->die);
30602 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30603 ctnode = ctnode->next)
30604 note_variable_value (ctnode->root_die);
30605 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30606 note_variable_value (node->die);
30608 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30609 both the main_cu and all skeleton TUs. Making this call unconditional
30610 would end up either adding a second copy of the AT_pubnames attribute, or
30611 requiring a special case in add_top_level_skeleton_die_attrs. */
30612 if (!dwarf_split_debug_info)
30613 add_AT_pubnames (comp_unit_die ());
30615 /* The early debug phase is now finished. */
30616 early_dwarf_finished = true;
30618 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30619 if (!flag_generate_lto && !flag_generate_offload)
30620 return;
30622 /* Now as we are going to output for LTO initialize sections and labels
30623 to the LTO variants. We don't need a random-seed postfix as other
30624 LTO sections as linking the LTO debug sections into one in a partial
30625 link is fine. */
30626 init_sections_and_labels (true);
30628 /* The output below is modeled after dwarf2out_finish with all
30629 location related output removed and some LTO specific changes.
30630 Some refactoring might make both smaller and easier to match up. */
30632 /* Traverse the DIE's and add add sibling attributes to those DIE's
30633 that have children. */
30634 add_sibling_attributes (comp_unit_die ());
30635 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30636 add_sibling_attributes (node->die);
30637 for (comdat_type_node *ctnode = comdat_type_list;
30638 ctnode != NULL; ctnode = ctnode->next)
30639 add_sibling_attributes (ctnode->root_die);
30641 if (have_macinfo)
30642 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30643 macinfo_section_label);
30645 save_macinfo_strings ();
30647 /* Output all of the compilation units. We put the main one last so that
30648 the offsets are available to output_pubnames. */
30649 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30650 output_comp_unit (node->die, 0, NULL);
30652 hash_table<comdat_type_hasher> comdat_type_table (100);
30653 for (comdat_type_node *ctnode = comdat_type_list;
30654 ctnode != NULL; ctnode = ctnode->next)
30656 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30658 /* Don't output duplicate types. */
30659 if (*slot != HTAB_EMPTY_ENTRY)
30660 continue;
30662 /* Add a pointer to the line table for the main compilation unit
30663 so that the debugger can make sense of DW_AT_decl_file
30664 attributes. */
30665 if (debug_info_level >= DINFO_LEVEL_TERSE)
30666 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30667 (!dwarf_split_debug_info
30668 ? debug_line_section_label
30669 : debug_skeleton_line_section_label));
30671 output_comdat_type_unit (ctnode);
30672 *slot = ctnode;
30675 /* Stick a unique symbol to the main debuginfo section. */
30676 compute_comp_unit_symbol (comp_unit_die ());
30678 /* Output the main compilation unit. We always need it if only for
30679 the CU symbol. */
30680 output_comp_unit (comp_unit_die (), true, NULL);
30682 /* Output the abbreviation table. */
30683 if (vec_safe_length (abbrev_die_table) != 1)
30685 switch_to_section (debug_abbrev_section);
30686 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30687 output_abbrev_section ();
30690 /* Have to end the macro section. */
30691 if (have_macinfo)
30693 /* We have to save macinfo state if we need to output it again
30694 for the FAT part of the object. */
30695 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30696 if (flag_fat_lto_objects)
30697 macinfo_table = macinfo_table->copy ();
30699 switch_to_section (debug_macinfo_section);
30700 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30701 output_macinfo (debug_skeleton_line_section_label, true);
30702 dw2_asm_output_data (1, 0, "End compilation unit");
30704 /* Emit a skeleton debug_line section. */
30705 switch_to_section (debug_skeleton_line_section);
30706 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30707 output_line_info (true);
30709 if (flag_fat_lto_objects)
30711 vec_free (macinfo_table);
30712 macinfo_table = saved_macinfo_table;
30717 /* If we emitted any indirect strings, output the string table too. */
30718 if (debug_str_hash || skeleton_debug_str_hash)
30719 output_indirect_strings ();
30721 /* Switch back to the text section. */
30722 switch_to_section (text_section);
30725 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30726 within the same process. For use by toplev::finalize. */
30728 void
30729 dwarf2out_c_finalize (void)
30731 last_var_location_insn = NULL;
30732 cached_next_real_insn = NULL;
30733 used_rtx_array = NULL;
30734 incomplete_types = NULL;
30735 decl_scope_table = NULL;
30736 debug_info_section = NULL;
30737 debug_skeleton_info_section = NULL;
30738 debug_abbrev_section = NULL;
30739 debug_skeleton_abbrev_section = NULL;
30740 debug_aranges_section = NULL;
30741 debug_addr_section = NULL;
30742 debug_macinfo_section = NULL;
30743 debug_line_section = NULL;
30744 debug_skeleton_line_section = NULL;
30745 debug_loc_section = NULL;
30746 debug_pubnames_section = NULL;
30747 debug_pubtypes_section = NULL;
30748 debug_str_section = NULL;
30749 debug_line_str_section = NULL;
30750 debug_str_dwo_section = NULL;
30751 debug_str_offsets_section = NULL;
30752 debug_ranges_section = NULL;
30753 debug_frame_section = NULL;
30754 fde_vec = NULL;
30755 debug_str_hash = NULL;
30756 debug_line_str_hash = NULL;
30757 skeleton_debug_str_hash = NULL;
30758 dw2_string_counter = 0;
30759 have_multiple_function_sections = false;
30760 text_section_used = false;
30761 cold_text_section_used = false;
30762 cold_text_section = NULL;
30763 current_unit_personality = NULL;
30765 early_dwarf = false;
30766 early_dwarf_finished = false;
30768 next_die_offset = 0;
30769 single_comp_unit_die = NULL;
30770 comdat_type_list = NULL;
30771 limbo_die_list = NULL;
30772 file_table = NULL;
30773 decl_die_table = NULL;
30774 common_block_die_table = NULL;
30775 decl_loc_table = NULL;
30776 call_arg_locations = NULL;
30777 call_arg_loc_last = NULL;
30778 call_site_count = -1;
30779 tail_call_site_count = -1;
30780 cached_dw_loc_list_table = NULL;
30781 abbrev_die_table = NULL;
30782 delete dwarf_proc_stack_usage_map;
30783 dwarf_proc_stack_usage_map = NULL;
30784 line_info_label_num = 0;
30785 cur_line_info_table = NULL;
30786 text_section_line_info = NULL;
30787 cold_text_section_line_info = NULL;
30788 separate_line_info = NULL;
30789 info_section_emitted = false;
30790 pubname_table = NULL;
30791 pubtype_table = NULL;
30792 macinfo_table = NULL;
30793 ranges_table = NULL;
30794 ranges_by_label = NULL;
30795 rnglist_idx = 0;
30796 have_location_lists = false;
30797 loclabel_num = 0;
30798 poc_label_num = 0;
30799 last_emitted_file = NULL;
30800 label_num = 0;
30801 tmpl_value_parm_die_table = NULL;
30802 generic_type_instances = NULL;
30803 frame_pointer_fb_offset = 0;
30804 frame_pointer_fb_offset_valid = false;
30805 base_types.release ();
30806 XDELETEVEC (producer_string);
30807 producer_string = NULL;
30810 #include "gt-dwarf2out.h"