PR82045: Avoid passing machine modes through "..."
[official-gcc.git] / gcc / dwarf2out.c
blob651dd0c78729bee119f85a85484249f8b50745b3
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "lra.h"
90 #include "dumpfile.h"
91 #include "opts.h"
92 #include "tree-dfa.h"
93 #include "gdb/gdb-index.h"
94 #include "rtl-iter.h"
95 #include "stringpool.h"
96 #include "attribs.h"
98 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
99 int, bool);
100 static rtx_insn *last_var_location_insn;
101 static rtx_insn *cached_next_real_insn;
102 static void dwarf2out_decl (tree);
104 #ifndef XCOFF_DEBUGGING_INFO
105 #define XCOFF_DEBUGGING_INFO 0
106 #endif
108 #ifndef HAVE_XCOFF_DWARF_EXTRAS
109 #define HAVE_XCOFF_DWARF_EXTRAS 0
110 #endif
112 #ifdef VMS_DEBUGGING_INFO
113 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
115 /* Define this macro to be a nonzero value if the directory specifications
116 which are output in the debug info should end with a separator. */
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
118 /* Define this macro to evaluate to a nonzero value if GCC should refrain
119 from generating indirect strings in DWARF2 debug information, for instance
120 if your target is stuck with an old version of GDB that is unable to
121 process them properly or uses VMS Debug. */
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
123 #else
124 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
126 #endif
128 /* ??? Poison these here until it can be done generically. They've been
129 totally replaced in this file; make sure it stays that way. */
130 #undef DWARF2_UNWIND_INFO
131 #undef DWARF2_FRAME_INFO
132 #if (GCC_VERSION >= 3000)
133 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
134 #endif
136 /* The size of the target's pointer type. */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
141 /* Array of RTXes referenced by the debugging information, which therefore
142 must be kept around forever. */
143 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
145 /* A pointer to the base of a list of incomplete types which might be
146 completed at some later time. incomplete_types_list needs to be a
147 vec<tree, va_gc> *because we want to tell the garbage collector about
148 it. */
149 static GTY(()) vec<tree, va_gc> *incomplete_types;
151 /* A pointer to the base of a table of references to declaration
152 scopes. This table is a display which tracks the nesting
153 of declaration scopes at the current scope and containing
154 scopes. This table is used to find the proper place to
155 define type declaration DIE's. */
156 static GTY(()) vec<tree, va_gc> *decl_scope_table;
158 /* Pointers to various DWARF2 sections. */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_skeleton_info_section;
161 static GTY(()) section *debug_abbrev_section;
162 static GTY(()) section *debug_skeleton_abbrev_section;
163 static GTY(()) section *debug_aranges_section;
164 static GTY(()) section *debug_addr_section;
165 static GTY(()) section *debug_macinfo_section;
166 static const char *debug_macinfo_section_name;
167 static unsigned macinfo_label_base = 1;
168 static GTY(()) section *debug_line_section;
169 static GTY(()) section *debug_skeleton_line_section;
170 static GTY(()) section *debug_loc_section;
171 static GTY(()) section *debug_pubnames_section;
172 static GTY(()) section *debug_pubtypes_section;
173 static GTY(()) section *debug_str_section;
174 static GTY(()) section *debug_line_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 40
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
196 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
197 #endif
199 /* Round SIZE up to the nearest BOUNDARY. */
200 #define DWARF_ROUND(SIZE,BOUNDARY) \
201 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
203 /* CIE identifier. */
204 #if HOST_BITS_PER_WIDE_INT >= 64
205 #define DWARF_CIE_ID \
206 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
207 #else
208 #define DWARF_CIE_ID DW_CIE_ID
209 #endif
212 /* A vector for a table that contains frame description
213 information for each routine. */
214 #define NOT_INDEXED (-1U)
215 #define NO_INDEX_ASSIGNED (-2U)
217 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
219 struct GTY((for_user)) indirect_string_node {
220 const char *str;
221 unsigned int refcount;
222 enum dwarf_form form;
223 char *label;
224 unsigned int index;
227 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
229 typedef const char *compare_type;
231 static hashval_t hash (indirect_string_node *);
232 static bool equal (indirect_string_node *, const char *);
235 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
237 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
239 /* With split_debug_info, both the comp_dir and dwo_name go in the
240 main object file, rather than the dwo, similar to the force_direct
241 parameter elsewhere but with additional complications:
243 1) The string is needed in both the main object file and the dwo.
244 That is, the comp_dir and dwo_name will appear in both places.
246 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
247 DW_FORM_line_strp or DW_FORM_GNU_str_index.
249 3) GCC chooses the form to use late, depending on the size and
250 reference count.
252 Rather than forcing the all debug string handling functions and
253 callers to deal with these complications, simply use a separate,
254 special-cased string table for any attribute that should go in the
255 main object file. This limits the complexity to just the places
256 that need it. */
258 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
260 static GTY(()) int dw2_string_counter;
262 /* True if the compilation unit places functions in more than one section. */
263 static GTY(()) bool have_multiple_function_sections = false;
265 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
279 /* Forward declarations for functions defined in this file. */
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
288 /* .debug_rnglists next index. */
289 static unsigned int rnglist_idx;
291 /* Data and reference forms for relocatable data. */
292 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
293 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
295 #ifndef DEBUG_FRAME_SECTION
296 #define DEBUG_FRAME_SECTION ".debug_frame"
297 #endif
299 #ifndef FUNC_BEGIN_LABEL
300 #define FUNC_BEGIN_LABEL "LFB"
301 #endif
303 #ifndef FUNC_END_LABEL
304 #define FUNC_END_LABEL "LFE"
305 #endif
307 #ifndef PROLOGUE_END_LABEL
308 #define PROLOGUE_END_LABEL "LPE"
309 #endif
311 #ifndef EPILOGUE_BEGIN_LABEL
312 #define EPILOGUE_BEGIN_LABEL "LEB"
313 #endif
315 #ifndef FRAME_BEGIN_LABEL
316 #define FRAME_BEGIN_LABEL "Lframe"
317 #endif
318 #define CIE_AFTER_SIZE_LABEL "LSCIE"
319 #define CIE_END_LABEL "LECIE"
320 #define FDE_LABEL "LSFDE"
321 #define FDE_AFTER_SIZE_LABEL "LASFDE"
322 #define FDE_END_LABEL "LEFDE"
323 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
324 #define LINE_NUMBER_END_LABEL "LELT"
325 #define LN_PROLOG_AS_LABEL "LASLTP"
326 #define LN_PROLOG_END_LABEL "LELTP"
327 #define DIE_LABEL_PREFIX "DW"
329 /* Match the base name of a file to the base name of a compilation unit. */
331 static int
332 matches_main_base (const char *path)
334 /* Cache the last query. */
335 static const char *last_path = NULL;
336 static int last_match = 0;
337 if (path != last_path)
339 const char *base;
340 int length = base_of_path (path, &base);
341 last_path = path;
342 last_match = (length == main_input_baselength
343 && memcmp (base, main_input_basename, length) == 0);
345 return last_match;
348 #ifdef DEBUG_DEBUG_STRUCT
350 static int
351 dump_struct_debug (tree type, enum debug_info_usage usage,
352 enum debug_struct_file criterion, int generic,
353 int matches, int result)
355 /* Find the type name. */
356 tree type_decl = TYPE_STUB_DECL (type);
357 tree t = type_decl;
358 const char *name = 0;
359 if (TREE_CODE (t) == TYPE_DECL)
360 t = DECL_NAME (t);
361 if (t)
362 name = IDENTIFIER_POINTER (t);
364 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
365 criterion,
366 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
367 matches ? "bas" : "hdr",
368 generic ? "gen" : "ord",
369 usage == DINFO_USAGE_DFN ? ";" :
370 usage == DINFO_USAGE_DIR_USE ? "." : "*",
371 result,
372 (void*) type_decl, name);
373 return result;
375 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
376 dump_struct_debug (type, usage, criterion, generic, matches, result)
378 #else
380 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
381 (result)
383 #endif
385 /* Get the number of HOST_WIDE_INTs needed to represent the precision
386 of the number. Some constants have a large uniform precision, so
387 we get the precision needed for the actual value of the number. */
389 static unsigned int
390 get_full_len (const wide_int &op)
392 int prec = wi::min_precision (op, UNSIGNED);
393 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
394 / HOST_BITS_PER_WIDE_INT);
397 static bool
398 should_emit_struct_debug (tree type, enum debug_info_usage usage)
400 enum debug_struct_file criterion;
401 tree type_decl;
402 bool generic = lang_hooks.types.generic_p (type);
404 if (generic)
405 criterion = debug_struct_generic[usage];
406 else
407 criterion = debug_struct_ordinary[usage];
409 if (criterion == DINFO_STRUCT_FILE_NONE)
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 if (criterion == DINFO_STRUCT_FILE_ANY)
412 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
414 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
416 if (type_decl != NULL)
418 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
419 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
421 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
425 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
428 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
429 switch to the data section instead, and write out a synthetic start label
430 for collect2 the first time around. */
432 static void
433 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
435 if (eh_frame_section == 0)
437 int flags;
439 if (EH_TABLES_CAN_BE_READ_ONLY)
441 int fde_encoding;
442 int per_encoding;
443 int lsda_encoding;
445 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
446 /*global=*/0);
447 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
448 /*global=*/1);
449 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
450 /*global=*/0);
451 flags = ((! flag_pic
452 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
453 && (fde_encoding & 0x70) != DW_EH_PE_aligned
454 && (per_encoding & 0x70) != DW_EH_PE_absptr
455 && (per_encoding & 0x70) != DW_EH_PE_aligned
456 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
457 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
458 ? 0 : SECTION_WRITE);
460 else
461 flags = SECTION_WRITE;
463 #ifdef EH_FRAME_SECTION_NAME
464 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
465 #else
466 eh_frame_section = ((flags == SECTION_WRITE)
467 ? data_section : readonly_data_section);
468 #endif /* EH_FRAME_SECTION_NAME */
471 switch_to_section (eh_frame_section);
473 #ifdef EH_FRAME_THROUGH_COLLECT2
474 /* We have no special eh_frame section. Emit special labels to guide
475 collect2. */
476 if (!back)
478 tree label = get_file_function_name ("F");
479 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
480 targetm.asm_out.globalize_label (asm_out_file,
481 IDENTIFIER_POINTER (label));
482 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
484 #endif
487 /* Switch [BACK] to the eh or debug frame table section, depending on
488 FOR_EH. */
490 static void
491 switch_to_frame_table_section (int for_eh, bool back)
493 if (for_eh)
494 switch_to_eh_frame_section (back);
495 else
497 if (!debug_frame_section)
498 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
499 SECTION_DEBUG, NULL);
500 switch_to_section (debug_frame_section);
504 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
506 enum dw_cfi_oprnd_type
507 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
509 switch (cfi)
511 case DW_CFA_nop:
512 case DW_CFA_GNU_window_save:
513 case DW_CFA_remember_state:
514 case DW_CFA_restore_state:
515 return dw_cfi_oprnd_unused;
517 case DW_CFA_set_loc:
518 case DW_CFA_advance_loc1:
519 case DW_CFA_advance_loc2:
520 case DW_CFA_advance_loc4:
521 case DW_CFA_MIPS_advance_loc8:
522 return dw_cfi_oprnd_addr;
524 case DW_CFA_offset:
525 case DW_CFA_offset_extended:
526 case DW_CFA_def_cfa:
527 case DW_CFA_offset_extended_sf:
528 case DW_CFA_def_cfa_sf:
529 case DW_CFA_restore:
530 case DW_CFA_restore_extended:
531 case DW_CFA_undefined:
532 case DW_CFA_same_value:
533 case DW_CFA_def_cfa_register:
534 case DW_CFA_register:
535 case DW_CFA_expression:
536 case DW_CFA_val_expression:
537 return dw_cfi_oprnd_reg_num;
539 case DW_CFA_def_cfa_offset:
540 case DW_CFA_GNU_args_size:
541 case DW_CFA_def_cfa_offset_sf:
542 return dw_cfi_oprnd_offset;
544 case DW_CFA_def_cfa_expression:
545 return dw_cfi_oprnd_loc;
547 default:
548 gcc_unreachable ();
552 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
554 enum dw_cfi_oprnd_type
555 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
557 switch (cfi)
559 case DW_CFA_def_cfa:
560 case DW_CFA_def_cfa_sf:
561 case DW_CFA_offset:
562 case DW_CFA_offset_extended_sf:
563 case DW_CFA_offset_extended:
564 return dw_cfi_oprnd_offset;
566 case DW_CFA_register:
567 return dw_cfi_oprnd_reg_num;
569 case DW_CFA_expression:
570 case DW_CFA_val_expression:
571 return dw_cfi_oprnd_loc;
573 default:
574 return dw_cfi_oprnd_unused;
578 /* Output one FDE. */
580 static void
581 output_fde (dw_fde_ref fde, bool for_eh, bool second,
582 char *section_start_label, int fde_encoding, char *augmentation,
583 bool any_lsda_needed, int lsda_encoding)
585 const char *begin, *end;
586 static unsigned int j;
587 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
589 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
590 /* empty */ 0);
591 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
592 for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
595 if (!XCOFF_DEBUGGING_INFO || for_eh)
597 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
598 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
599 " indicating 64-bit DWARF extension");
600 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
601 "FDE Length");
603 ASM_OUTPUT_LABEL (asm_out_file, l1);
605 if (for_eh)
606 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
607 else
608 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
609 debug_frame_section, "FDE CIE offset");
611 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
612 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
614 if (for_eh)
616 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
617 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
618 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
619 "FDE initial location");
620 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
621 end, begin, "FDE address range");
623 else
625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
626 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
629 if (augmentation[0])
631 if (any_lsda_needed)
633 int size = size_of_encoded_value (lsda_encoding);
635 if (lsda_encoding == DW_EH_PE_aligned)
637 int offset = ( 4 /* Length */
638 + 4 /* CIE offset */
639 + 2 * size_of_encoded_value (fde_encoding)
640 + 1 /* Augmentation size */ );
641 int pad = -offset & (PTR_SIZE - 1);
643 size += pad;
644 gcc_assert (size_of_uleb128 (size) == 1);
647 dw2_asm_output_data_uleb128 (size, "Augmentation size");
649 if (fde->uses_eh_lsda)
651 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
652 fde->funcdef_number);
653 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
654 gen_rtx_SYMBOL_REF (Pmode, l1),
655 false,
656 "Language Specific Data Area");
658 else
660 if (lsda_encoding == DW_EH_PE_aligned)
661 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
662 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
663 "Language Specific Data Area (none)");
666 else
667 dw2_asm_output_data_uleb128 (0, "Augmentation size");
670 /* Loop through the Call Frame Instructions associated with this FDE. */
671 fde->dw_fde_current_label = begin;
673 size_t from, until, i;
675 from = 0;
676 until = vec_safe_length (fde->dw_fde_cfi);
678 if (fde->dw_fde_second_begin == NULL)
680 else if (!second)
681 until = fde->dw_fde_switch_cfi_index;
682 else
683 from = fde->dw_fde_switch_cfi_index;
685 for (i = from; i < until; i++)
686 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
689 /* If we are to emit a ref/link from function bodies to their frame tables,
690 do it now. This is typically performed to make sure that tables
691 associated with functions are dragged with them and not discarded in
692 garbage collecting links. We need to do this on a per function basis to
693 cope with -ffunction-sections. */
695 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
696 /* Switch to the function section, emit the ref to the tables, and
697 switch *back* into the table section. */
698 switch_to_section (function_section (fde->decl));
699 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
700 switch_to_frame_table_section (for_eh, true);
701 #endif
703 /* Pad the FDE out to an address sized boundary. */
704 ASM_OUTPUT_ALIGN (asm_out_file,
705 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
706 ASM_OUTPUT_LABEL (asm_out_file, l2);
708 j += 2;
711 /* Return true if frame description entry FDE is needed for EH. */
713 static bool
714 fde_needed_for_eh_p (dw_fde_ref fde)
716 if (flag_asynchronous_unwind_tables)
717 return true;
719 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
720 return true;
722 if (fde->uses_eh_lsda)
723 return true;
725 /* If exceptions are enabled, we have collected nothrow info. */
726 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
727 return false;
729 return true;
732 /* Output the call frame information used to record information
733 that relates to calculating the frame pointer, and records the
734 location of saved registers. */
736 static void
737 output_call_frame_info (int for_eh)
739 unsigned int i;
740 dw_fde_ref fde;
741 dw_cfi_ref cfi;
742 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
743 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 unsigned int column ATTRIBUTE_UNUSED,
1035 const char *file ATTRIBUTE_UNUSED)
1037 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1038 char * dup_label;
1039 dw_fde_ref fde;
1040 section *fnsec;
1041 bool do_frame;
1043 current_function_func_begin_label = NULL;
1045 do_frame = dwarf2out_do_frame ();
1047 /* ??? current_function_func_begin_label is also used by except.c for
1048 call-site information. We must emit this label if it might be used. */
1049 if (!do_frame
1050 && (!flag_exceptions
1051 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1052 return;
1054 fnsec = function_section (current_function_decl);
1055 switch_to_section (fnsec);
1056 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1057 current_function_funcdef_no);
1058 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1059 current_function_funcdef_no);
1060 dup_label = xstrdup (label);
1061 current_function_func_begin_label = dup_label;
1063 /* We can elide the fde allocation if we're not emitting debug info. */
1064 if (!do_frame)
1065 return;
1067 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1068 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1069 would include pass_dwarf2_frame. If we've not created the FDE yet,
1070 do so now. */
1071 fde = cfun->fde;
1072 if (fde == NULL)
1073 fde = dwarf2out_alloc_current_fde ();
1075 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1076 fde->dw_fde_begin = dup_label;
1077 fde->dw_fde_current_label = dup_label;
1078 fde->in_std_section = (fnsec == text_section
1079 || (cold_text_section && fnsec == cold_text_section));
1081 /* We only want to output line number information for the genuine dwarf2
1082 prologue case, not the eh frame case. */
1083 #ifdef DWARF2_DEBUGGING_INFO
1084 if (file)
1085 dwarf2out_source_line (line, column, file, 0, true);
1086 #endif
1088 if (dwarf2out_do_cfi_asm ())
1089 dwarf2out_do_cfi_startproc (false);
1090 else
1092 rtx personality = get_personality_function (current_function_decl);
1093 if (!current_unit_personality)
1094 current_unit_personality = personality;
1096 /* We cannot keep a current personality per function as without CFI
1097 asm, at the point where we emit the CFI data, there is no current
1098 function anymore. */
1099 if (personality && current_unit_personality != personality)
1100 sorry ("multiple EH personalities are supported only with assemblers "
1101 "supporting .cfi_personality directive");
1105 /* Output a marker (i.e. a label) for the end of the generated code
1106 for a function prologue. This gets called *after* the prologue code has
1107 been generated. */
1109 void
1110 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1111 const char *file ATTRIBUTE_UNUSED)
1113 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1115 /* Output a label to mark the endpoint of the code generated for this
1116 function. */
1117 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1118 current_function_funcdef_no);
1119 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1120 current_function_funcdef_no);
1121 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1124 /* Output a marker (i.e. a label) for the beginning of the generated code
1125 for a function epilogue. This gets called *before* the prologue code has
1126 been generated. */
1128 void
1129 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1130 const char *file ATTRIBUTE_UNUSED)
1132 dw_fde_ref fde = cfun->fde;
1133 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1135 if (fde->dw_fde_vms_begin_epilogue)
1136 return;
1138 /* Output a label to mark the endpoint of the code generated for this
1139 function. */
1140 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1141 current_function_funcdef_no);
1142 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1143 current_function_funcdef_no);
1144 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1147 /* Output a marker (i.e. a label) for the absolute end of the generated code
1148 for a function definition. This gets called *after* the epilogue code has
1149 been generated. */
1151 void
1152 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1153 const char *file ATTRIBUTE_UNUSED)
1155 dw_fde_ref fde;
1156 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1158 last_var_location_insn = NULL;
1159 cached_next_real_insn = NULL;
1161 if (dwarf2out_do_cfi_asm ())
1162 fprintf (asm_out_file, "\t.cfi_endproc\n");
1164 /* Output a label to mark the endpoint of the code generated for this
1165 function. */
1166 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1167 current_function_funcdef_no);
1168 ASM_OUTPUT_LABEL (asm_out_file, label);
1169 fde = cfun->fde;
1170 gcc_assert (fde != NULL);
1171 if (fde->dw_fde_second_begin == NULL)
1172 fde->dw_fde_end = xstrdup (label);
1175 void
1176 dwarf2out_frame_finish (void)
1178 /* Output call frame information. */
1179 if (targetm.debug_unwind_info () == UI_DWARF2)
1180 output_call_frame_info (0);
1182 /* Output another copy for the unwinder. */
1183 if ((flag_unwind_tables || flag_exceptions)
1184 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1185 output_call_frame_info (1);
1188 /* Note that the current function section is being used for code. */
1190 static void
1191 dwarf2out_note_section_used (void)
1193 section *sec = current_function_section ();
1194 if (sec == text_section)
1195 text_section_used = true;
1196 else if (sec == cold_text_section)
1197 cold_text_section_used = true;
1200 static void var_location_switch_text_section (void);
1201 static void set_cur_line_info_table (section *);
1203 void
1204 dwarf2out_switch_text_section (void)
1206 section *sect;
1207 dw_fde_ref fde = cfun->fde;
1209 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1211 if (!in_cold_section_p)
1213 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1214 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1215 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1217 else
1219 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1220 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1221 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1223 have_multiple_function_sections = true;
1225 /* There is no need to mark used sections when not debugging. */
1226 if (cold_text_section != NULL)
1227 dwarf2out_note_section_used ();
1229 if (dwarf2out_do_cfi_asm ())
1230 fprintf (asm_out_file, "\t.cfi_endproc\n");
1232 /* Now do the real section switch. */
1233 sect = current_function_section ();
1234 switch_to_section (sect);
1236 fde->second_in_std_section
1237 = (sect == text_section
1238 || (cold_text_section && sect == cold_text_section));
1240 if (dwarf2out_do_cfi_asm ())
1241 dwarf2out_do_cfi_startproc (true);
1243 var_location_switch_text_section ();
1245 if (cold_text_section != NULL)
1246 set_cur_line_info_table (sect);
1249 /* And now, the subset of the debugging information support code necessary
1250 for emitting location expressions. */
1252 /* Data about a single source file. */
1253 struct GTY((for_user)) dwarf_file_data {
1254 const char * filename;
1255 int emitted_number;
1258 /* Describe an entry into the .debug_addr section. */
1260 enum ate_kind {
1261 ate_kind_rtx,
1262 ate_kind_rtx_dtprel,
1263 ate_kind_label
1266 struct GTY((for_user)) addr_table_entry {
1267 enum ate_kind kind;
1268 unsigned int refcount;
1269 unsigned int index;
1270 union addr_table_entry_struct_union
1272 rtx GTY ((tag ("0"))) rtl;
1273 char * GTY ((tag ("1"))) label;
1275 GTY ((desc ("%1.kind"))) addr;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1297 section. */
1298 unsigned char emitted : 1;
1299 /* True if hash field is index rather than hash value. */
1300 unsigned char num_assigned : 1;
1301 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1302 unsigned char offset_emitted : 1;
1303 /* True if note_variable_value_in_expr has been called on it. */
1304 unsigned char noted_variable_value : 1;
1305 /* True if the range should be emitted even if begin and end
1306 are the same. */
1307 bool force;
1308 } dw_loc_list_node;
1310 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1311 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1313 /* Convert a DWARF stack opcode into its string name. */
1315 static const char *
1316 dwarf_stack_op_name (unsigned int op)
1318 const char *name = get_DW_OP_name (op);
1320 if (name != NULL)
1321 return name;
1323 return "OP_<unknown>";
1326 /* Return a pointer to a newly allocated location description. Location
1327 descriptions are simple expression terms that can be strung
1328 together to form more complicated location (address) descriptions. */
1330 static inline dw_loc_descr_ref
1331 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1332 unsigned HOST_WIDE_INT oprnd2)
1334 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1336 descr->dw_loc_opc = op;
1337 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1338 descr->dw_loc_oprnd1.val_entry = NULL;
1339 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1340 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1341 descr->dw_loc_oprnd2.val_entry = NULL;
1342 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1344 return descr;
1347 /* Return a pointer to a newly allocated location description for
1348 REG and OFFSET. */
1350 static inline dw_loc_descr_ref
1351 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1353 if (reg <= 31)
1354 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1355 offset, 0);
1356 else
1357 return new_loc_descr (DW_OP_bregx, reg, offset);
1360 /* Add a location description term to a location description expression. */
1362 static inline void
1363 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1365 dw_loc_descr_ref *d;
1367 /* Find the end of the chain. */
1368 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1371 *d = descr;
1374 /* Compare two location operands for exact equality. */
1376 static bool
1377 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1379 if (a->val_class != b->val_class)
1380 return false;
1381 switch (a->val_class)
1383 case dw_val_class_none:
1384 return true;
1385 case dw_val_class_addr:
1386 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1388 case dw_val_class_offset:
1389 case dw_val_class_unsigned_const:
1390 case dw_val_class_const:
1391 case dw_val_class_unsigned_const_implicit:
1392 case dw_val_class_const_implicit:
1393 case dw_val_class_range_list:
1394 /* These are all HOST_WIDE_INT, signed or unsigned. */
1395 return a->v.val_unsigned == b->v.val_unsigned;
1397 case dw_val_class_loc:
1398 return a->v.val_loc == b->v.val_loc;
1399 case dw_val_class_loc_list:
1400 return a->v.val_loc_list == b->v.val_loc_list;
1401 case dw_val_class_die_ref:
1402 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1403 case dw_val_class_fde_ref:
1404 return a->v.val_fde_index == b->v.val_fde_index;
1405 case dw_val_class_lbl_id:
1406 case dw_val_class_lineptr:
1407 case dw_val_class_macptr:
1408 case dw_val_class_loclistsptr:
1409 case dw_val_class_high_pc:
1410 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1411 case dw_val_class_str:
1412 return a->v.val_str == b->v.val_str;
1413 case dw_val_class_flag:
1414 return a->v.val_flag == b->v.val_flag;
1415 case dw_val_class_file:
1416 case dw_val_class_file_implicit:
1417 return a->v.val_file == b->v.val_file;
1418 case dw_val_class_decl_ref:
1419 return a->v.val_decl_ref == b->v.val_decl_ref;
1421 case dw_val_class_const_double:
1422 return (a->v.val_double.high == b->v.val_double.high
1423 && a->v.val_double.low == b->v.val_double.low);
1425 case dw_val_class_wide_int:
1426 return *a->v.val_wide == *b->v.val_wide;
1428 case dw_val_class_vec:
1430 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1431 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1433 return (a_len == b_len
1434 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1437 case dw_val_class_data8:
1438 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1440 case dw_val_class_vms_delta:
1441 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1442 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1444 case dw_val_class_discr_value:
1445 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1446 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1447 case dw_val_class_discr_list:
1448 /* It makes no sense comparing two discriminant value lists. */
1449 return false;
1451 gcc_unreachable ();
1454 /* Compare two location atoms for exact equality. */
1456 static bool
1457 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1459 if (a->dw_loc_opc != b->dw_loc_opc)
1460 return false;
1462 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1463 address size, but since we always allocate cleared storage it
1464 should be zero for other types of locations. */
1465 if (a->dtprel != b->dtprel)
1466 return false;
1468 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1469 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1472 /* Compare two complete location expressions for exact equality. */
1474 bool
1475 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1477 while (1)
1479 if (a == b)
1480 return true;
1481 if (a == NULL || b == NULL)
1482 return false;
1483 if (!loc_descr_equal_p_1 (a, b))
1484 return false;
1486 a = a->dw_loc_next;
1487 b = b->dw_loc_next;
1492 /* Add a constant OFFSET to a location expression. */
1494 static void
1495 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1497 dw_loc_descr_ref loc;
1498 HOST_WIDE_INT *p;
1500 gcc_assert (*list_head != NULL);
1502 if (!offset)
1503 return;
1505 /* Find the end of the chain. */
1506 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1509 p = NULL;
1510 if (loc->dw_loc_opc == DW_OP_fbreg
1511 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1512 p = &loc->dw_loc_oprnd1.v.val_int;
1513 else if (loc->dw_loc_opc == DW_OP_bregx)
1514 p = &loc->dw_loc_oprnd2.v.val_int;
1516 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1517 offset. Don't optimize if an signed integer overflow would happen. */
1518 if (p != NULL
1519 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1520 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1521 *p += offset;
1523 else if (offset > 0)
1524 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1526 else
1528 loc->dw_loc_next
1529 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1530 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1534 /* Add a constant OFFSET to a location list. */
1536 static void
1537 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1539 dw_loc_list_ref d;
1540 for (d = list_head; d != NULL; d = d->dw_loc_next)
1541 loc_descr_plus_const (&d->expr, offset);
1544 #define DWARF_REF_SIZE \
1545 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1547 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1548 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1549 DW_FORM_data16 with 128 bits. */
1550 #define DWARF_LARGEST_DATA_FORM_BITS \
1551 (dwarf_version >= 5 ? 128 : 64)
1553 /* Utility inline function for construction of ops that were GNU extension
1554 before DWARF 5. */
1555 static inline enum dwarf_location_atom
1556 dwarf_OP (enum dwarf_location_atom op)
1558 switch (op)
1560 case DW_OP_implicit_pointer:
1561 if (dwarf_version < 5)
1562 return DW_OP_GNU_implicit_pointer;
1563 break;
1565 case DW_OP_entry_value:
1566 if (dwarf_version < 5)
1567 return DW_OP_GNU_entry_value;
1568 break;
1570 case DW_OP_const_type:
1571 if (dwarf_version < 5)
1572 return DW_OP_GNU_const_type;
1573 break;
1575 case DW_OP_regval_type:
1576 if (dwarf_version < 5)
1577 return DW_OP_GNU_regval_type;
1578 break;
1580 case DW_OP_deref_type:
1581 if (dwarf_version < 5)
1582 return DW_OP_GNU_deref_type;
1583 break;
1585 case DW_OP_convert:
1586 if (dwarf_version < 5)
1587 return DW_OP_GNU_convert;
1588 break;
1590 case DW_OP_reinterpret:
1591 if (dwarf_version < 5)
1592 return DW_OP_GNU_reinterpret;
1593 break;
1595 default:
1596 break;
1598 return op;
1601 /* Similarly for attributes. */
1602 static inline enum dwarf_attribute
1603 dwarf_AT (enum dwarf_attribute at)
1605 switch (at)
1607 case DW_AT_call_return_pc:
1608 if (dwarf_version < 5)
1609 return DW_AT_low_pc;
1610 break;
1612 case DW_AT_call_tail_call:
1613 if (dwarf_version < 5)
1614 return DW_AT_GNU_tail_call;
1615 break;
1617 case DW_AT_call_origin:
1618 if (dwarf_version < 5)
1619 return DW_AT_abstract_origin;
1620 break;
1622 case DW_AT_call_target:
1623 if (dwarf_version < 5)
1624 return DW_AT_GNU_call_site_target;
1625 break;
1627 case DW_AT_call_target_clobbered:
1628 if (dwarf_version < 5)
1629 return DW_AT_GNU_call_site_target_clobbered;
1630 break;
1632 case DW_AT_call_parameter:
1633 if (dwarf_version < 5)
1634 return DW_AT_abstract_origin;
1635 break;
1637 case DW_AT_call_value:
1638 if (dwarf_version < 5)
1639 return DW_AT_GNU_call_site_value;
1640 break;
1642 case DW_AT_call_data_value:
1643 if (dwarf_version < 5)
1644 return DW_AT_GNU_call_site_data_value;
1645 break;
1647 case DW_AT_call_all_calls:
1648 if (dwarf_version < 5)
1649 return DW_AT_GNU_all_call_sites;
1650 break;
1652 case DW_AT_call_all_tail_calls:
1653 if (dwarf_version < 5)
1654 return DW_AT_GNU_all_tail_call_sites;
1655 break;
1657 case DW_AT_dwo_name:
1658 if (dwarf_version < 5)
1659 return DW_AT_GNU_dwo_name;
1660 break;
1662 default:
1663 break;
1665 return at;
1668 /* And similarly for tags. */
1669 static inline enum dwarf_tag
1670 dwarf_TAG (enum dwarf_tag tag)
1672 switch (tag)
1674 case DW_TAG_call_site:
1675 if (dwarf_version < 5)
1676 return DW_TAG_GNU_call_site;
1677 break;
1679 case DW_TAG_call_site_parameter:
1680 if (dwarf_version < 5)
1681 return DW_TAG_GNU_call_site_parameter;
1682 break;
1684 default:
1685 break;
1687 return tag;
1690 static unsigned long int get_base_type_offset (dw_die_ref);
1692 /* Return the size of a location descriptor. */
1694 static unsigned long
1695 size_of_loc_descr (dw_loc_descr_ref loc)
1697 unsigned long size = 1;
1699 switch (loc->dw_loc_opc)
1701 case DW_OP_addr:
1702 size += DWARF2_ADDR_SIZE;
1703 break;
1704 case DW_OP_GNU_addr_index:
1705 case DW_OP_GNU_const_index:
1706 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1707 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1708 break;
1709 case DW_OP_const1u:
1710 case DW_OP_const1s:
1711 size += 1;
1712 break;
1713 case DW_OP_const2u:
1714 case DW_OP_const2s:
1715 size += 2;
1716 break;
1717 case DW_OP_const4u:
1718 case DW_OP_const4s:
1719 size += 4;
1720 break;
1721 case DW_OP_const8u:
1722 case DW_OP_const8s:
1723 size += 8;
1724 break;
1725 case DW_OP_constu:
1726 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1727 break;
1728 case DW_OP_consts:
1729 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1730 break;
1731 case DW_OP_pick:
1732 size += 1;
1733 break;
1734 case DW_OP_plus_uconst:
1735 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1736 break;
1737 case DW_OP_skip:
1738 case DW_OP_bra:
1739 size += 2;
1740 break;
1741 case DW_OP_breg0:
1742 case DW_OP_breg1:
1743 case DW_OP_breg2:
1744 case DW_OP_breg3:
1745 case DW_OP_breg4:
1746 case DW_OP_breg5:
1747 case DW_OP_breg6:
1748 case DW_OP_breg7:
1749 case DW_OP_breg8:
1750 case DW_OP_breg9:
1751 case DW_OP_breg10:
1752 case DW_OP_breg11:
1753 case DW_OP_breg12:
1754 case DW_OP_breg13:
1755 case DW_OP_breg14:
1756 case DW_OP_breg15:
1757 case DW_OP_breg16:
1758 case DW_OP_breg17:
1759 case DW_OP_breg18:
1760 case DW_OP_breg19:
1761 case DW_OP_breg20:
1762 case DW_OP_breg21:
1763 case DW_OP_breg22:
1764 case DW_OP_breg23:
1765 case DW_OP_breg24:
1766 case DW_OP_breg25:
1767 case DW_OP_breg26:
1768 case DW_OP_breg27:
1769 case DW_OP_breg28:
1770 case DW_OP_breg29:
1771 case DW_OP_breg30:
1772 case DW_OP_breg31:
1773 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1774 break;
1775 case DW_OP_regx:
1776 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1777 break;
1778 case DW_OP_fbreg:
1779 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1780 break;
1781 case DW_OP_bregx:
1782 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1783 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1784 break;
1785 case DW_OP_piece:
1786 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1787 break;
1788 case DW_OP_bit_piece:
1789 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1790 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1791 break;
1792 case DW_OP_deref_size:
1793 case DW_OP_xderef_size:
1794 size += 1;
1795 break;
1796 case DW_OP_call2:
1797 size += 2;
1798 break;
1799 case DW_OP_call4:
1800 size += 4;
1801 break;
1802 case DW_OP_call_ref:
1803 case DW_OP_GNU_variable_value:
1804 size += DWARF_REF_SIZE;
1805 break;
1806 case DW_OP_implicit_value:
1807 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1808 + loc->dw_loc_oprnd1.v.val_unsigned;
1809 break;
1810 case DW_OP_implicit_pointer:
1811 case DW_OP_GNU_implicit_pointer:
1812 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1813 break;
1814 case DW_OP_entry_value:
1815 case DW_OP_GNU_entry_value:
1817 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1818 size += size_of_uleb128 (op_size) + op_size;
1819 break;
1821 case DW_OP_const_type:
1822 case DW_OP_GNU_const_type:
1824 unsigned long o
1825 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1826 size += size_of_uleb128 (o) + 1;
1827 switch (loc->dw_loc_oprnd2.val_class)
1829 case dw_val_class_vec:
1830 size += loc->dw_loc_oprnd2.v.val_vec.length
1831 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1832 break;
1833 case dw_val_class_const:
1834 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1835 break;
1836 case dw_val_class_const_double:
1837 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1838 break;
1839 case dw_val_class_wide_int:
1840 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1841 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1842 break;
1843 default:
1844 gcc_unreachable ();
1846 break;
1848 case DW_OP_regval_type:
1849 case DW_OP_GNU_regval_type:
1851 unsigned long o
1852 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1853 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1854 + size_of_uleb128 (o);
1856 break;
1857 case DW_OP_deref_type:
1858 case DW_OP_GNU_deref_type:
1860 unsigned long o
1861 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1862 size += 1 + size_of_uleb128 (o);
1864 break;
1865 case DW_OP_convert:
1866 case DW_OP_reinterpret:
1867 case DW_OP_GNU_convert:
1868 case DW_OP_GNU_reinterpret:
1869 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1870 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1871 else
1873 unsigned long o
1874 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1875 size += size_of_uleb128 (o);
1877 break;
1878 case DW_OP_GNU_parameter_ref:
1879 size += 4;
1880 break;
1881 default:
1882 break;
1885 return size;
1888 /* Return the size of a series of location descriptors. */
1890 unsigned long
1891 size_of_locs (dw_loc_descr_ref loc)
1893 dw_loc_descr_ref l;
1894 unsigned long size;
1896 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1897 field, to avoid writing to a PCH file. */
1898 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1900 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1901 break;
1902 size += size_of_loc_descr (l);
1904 if (! l)
1905 return size;
1907 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1909 l->dw_loc_addr = size;
1910 size += size_of_loc_descr (l);
1913 return size;
1916 /* Return the size of the value in a DW_AT_discr_value attribute. */
1918 static int
1919 size_of_discr_value (dw_discr_value *discr_value)
1921 if (discr_value->pos)
1922 return size_of_uleb128 (discr_value->v.uval);
1923 else
1924 return size_of_sleb128 (discr_value->v.sval);
1927 /* Return the size of the value in a DW_AT_discr_list attribute. */
1929 static int
1930 size_of_discr_list (dw_discr_list_ref discr_list)
1932 int size = 0;
1934 for (dw_discr_list_ref list = discr_list;
1935 list != NULL;
1936 list = list->dw_discr_next)
1938 /* One byte for the discriminant value descriptor, and then one or two
1939 LEB128 numbers, depending on whether it's a single case label or a
1940 range label. */
1941 size += 1;
1942 size += size_of_discr_value (&list->dw_discr_lower_bound);
1943 if (list->dw_discr_range != 0)
1944 size += size_of_discr_value (&list->dw_discr_upper_bound);
1946 return size;
1949 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1950 static void get_ref_die_offset_label (char *, dw_die_ref);
1951 static unsigned long int get_ref_die_offset (dw_die_ref);
1953 /* Output location description stack opcode's operands (if any).
1954 The for_eh_or_skip parameter controls whether register numbers are
1955 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1956 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1957 info). This should be suppressed for the cases that have not been converted
1958 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1960 static void
1961 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1963 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1964 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1966 switch (loc->dw_loc_opc)
1968 #ifdef DWARF2_DEBUGGING_INFO
1969 case DW_OP_const2u:
1970 case DW_OP_const2s:
1971 dw2_asm_output_data (2, val1->v.val_int, NULL);
1972 break;
1973 case DW_OP_const4u:
1974 if (loc->dtprel)
1976 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1977 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1978 val1->v.val_addr);
1979 fputc ('\n', asm_out_file);
1980 break;
1982 /* FALLTHRU */
1983 case DW_OP_const4s:
1984 dw2_asm_output_data (4, val1->v.val_int, NULL);
1985 break;
1986 case DW_OP_const8u:
1987 if (loc->dtprel)
1989 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1990 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1991 val1->v.val_addr);
1992 fputc ('\n', asm_out_file);
1993 break;
1995 /* FALLTHRU */
1996 case DW_OP_const8s:
1997 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1998 dw2_asm_output_data (8, val1->v.val_int, NULL);
1999 break;
2000 case DW_OP_skip:
2001 case DW_OP_bra:
2003 int offset;
2005 gcc_assert (val1->val_class == dw_val_class_loc);
2006 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2008 dw2_asm_output_data (2, offset, NULL);
2010 break;
2011 case DW_OP_implicit_value:
2012 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2013 switch (val2->val_class)
2015 case dw_val_class_const:
2016 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2017 break;
2018 case dw_val_class_vec:
2020 unsigned int elt_size = val2->v.val_vec.elt_size;
2021 unsigned int len = val2->v.val_vec.length;
2022 unsigned int i;
2023 unsigned char *p;
2025 if (elt_size > sizeof (HOST_WIDE_INT))
2027 elt_size /= 2;
2028 len *= 2;
2030 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2031 i < len;
2032 i++, p += elt_size)
2033 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2034 "fp or vector constant word %u", i);
2036 break;
2037 case dw_val_class_const_double:
2039 unsigned HOST_WIDE_INT first, second;
2041 if (WORDS_BIG_ENDIAN)
2043 first = val2->v.val_double.high;
2044 second = val2->v.val_double.low;
2046 else
2048 first = val2->v.val_double.low;
2049 second = val2->v.val_double.high;
2051 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2052 first, NULL);
2053 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2054 second, NULL);
2056 break;
2057 case dw_val_class_wide_int:
2059 int i;
2060 int len = get_full_len (*val2->v.val_wide);
2061 if (WORDS_BIG_ENDIAN)
2062 for (i = len - 1; i >= 0; --i)
2063 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2064 val2->v.val_wide->elt (i), NULL);
2065 else
2066 for (i = 0; i < len; ++i)
2067 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2068 val2->v.val_wide->elt (i), NULL);
2070 break;
2071 case dw_val_class_addr:
2072 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2073 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2074 break;
2075 default:
2076 gcc_unreachable ();
2078 break;
2079 #else
2080 case DW_OP_const2u:
2081 case DW_OP_const2s:
2082 case DW_OP_const4u:
2083 case DW_OP_const4s:
2084 case DW_OP_const8u:
2085 case DW_OP_const8s:
2086 case DW_OP_skip:
2087 case DW_OP_bra:
2088 case DW_OP_implicit_value:
2089 /* We currently don't make any attempt to make sure these are
2090 aligned properly like we do for the main unwind info, so
2091 don't support emitting things larger than a byte if we're
2092 only doing unwinding. */
2093 gcc_unreachable ();
2094 #endif
2095 case DW_OP_const1u:
2096 case DW_OP_const1s:
2097 dw2_asm_output_data (1, val1->v.val_int, NULL);
2098 break;
2099 case DW_OP_constu:
2100 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2101 break;
2102 case DW_OP_consts:
2103 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2104 break;
2105 case DW_OP_pick:
2106 dw2_asm_output_data (1, val1->v.val_int, NULL);
2107 break;
2108 case DW_OP_plus_uconst:
2109 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2110 break;
2111 case DW_OP_breg0:
2112 case DW_OP_breg1:
2113 case DW_OP_breg2:
2114 case DW_OP_breg3:
2115 case DW_OP_breg4:
2116 case DW_OP_breg5:
2117 case DW_OP_breg6:
2118 case DW_OP_breg7:
2119 case DW_OP_breg8:
2120 case DW_OP_breg9:
2121 case DW_OP_breg10:
2122 case DW_OP_breg11:
2123 case DW_OP_breg12:
2124 case DW_OP_breg13:
2125 case DW_OP_breg14:
2126 case DW_OP_breg15:
2127 case DW_OP_breg16:
2128 case DW_OP_breg17:
2129 case DW_OP_breg18:
2130 case DW_OP_breg19:
2131 case DW_OP_breg20:
2132 case DW_OP_breg21:
2133 case DW_OP_breg22:
2134 case DW_OP_breg23:
2135 case DW_OP_breg24:
2136 case DW_OP_breg25:
2137 case DW_OP_breg26:
2138 case DW_OP_breg27:
2139 case DW_OP_breg28:
2140 case DW_OP_breg29:
2141 case DW_OP_breg30:
2142 case DW_OP_breg31:
2143 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2144 break;
2145 case DW_OP_regx:
2147 unsigned r = val1->v.val_unsigned;
2148 if (for_eh_or_skip >= 0)
2149 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2150 gcc_assert (size_of_uleb128 (r)
2151 == size_of_uleb128 (val1->v.val_unsigned));
2152 dw2_asm_output_data_uleb128 (r, NULL);
2154 break;
2155 case DW_OP_fbreg:
2156 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2157 break;
2158 case DW_OP_bregx:
2160 unsigned r = val1->v.val_unsigned;
2161 if (for_eh_or_skip >= 0)
2162 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2163 gcc_assert (size_of_uleb128 (r)
2164 == size_of_uleb128 (val1->v.val_unsigned));
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2168 break;
2169 case DW_OP_piece:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 break;
2172 case DW_OP_bit_piece:
2173 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2174 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2175 break;
2176 case DW_OP_deref_size:
2177 case DW_OP_xderef_size:
2178 dw2_asm_output_data (1, val1->v.val_int, NULL);
2179 break;
2181 case DW_OP_addr:
2182 if (loc->dtprel)
2184 if (targetm.asm_out.output_dwarf_dtprel)
2186 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2187 DWARF2_ADDR_SIZE,
2188 val1->v.val_addr);
2189 fputc ('\n', asm_out_file);
2191 else
2192 gcc_unreachable ();
2194 else
2196 #ifdef DWARF2_DEBUGGING_INFO
2197 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2198 #else
2199 gcc_unreachable ();
2200 #endif
2202 break;
2204 case DW_OP_GNU_addr_index:
2205 case DW_OP_GNU_const_index:
2206 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2207 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2208 "(index into .debug_addr)");
2209 break;
2211 case DW_OP_call2:
2212 case DW_OP_call4:
2214 unsigned long die_offset
2215 = get_ref_die_offset (val1->v.val_die_ref.die);
2216 /* Make sure the offset has been computed and that we can encode it as
2217 an operand. */
2218 gcc_assert (die_offset > 0
2219 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2220 ? 0xffff
2221 : 0xffffffff));
2222 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2223 die_offset, NULL);
2225 break;
2227 case DW_OP_call_ref:
2228 case DW_OP_GNU_variable_value:
2230 char label[MAX_ARTIFICIAL_LABEL_BYTES
2231 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2232 gcc_assert (val1->val_class == dw_val_class_die_ref);
2233 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2234 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2236 break;
2238 case DW_OP_implicit_pointer:
2239 case DW_OP_GNU_implicit_pointer:
2241 char label[MAX_ARTIFICIAL_LABEL_BYTES
2242 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2243 gcc_assert (val1->val_class == dw_val_class_die_ref);
2244 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2245 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2246 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2248 break;
2250 case DW_OP_entry_value:
2251 case DW_OP_GNU_entry_value:
2252 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2253 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2254 break;
2256 case DW_OP_const_type:
2257 case DW_OP_GNU_const_type:
2259 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2260 gcc_assert (o);
2261 dw2_asm_output_data_uleb128 (o, NULL);
2262 switch (val2->val_class)
2264 case dw_val_class_const:
2265 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2266 dw2_asm_output_data (1, l, NULL);
2267 dw2_asm_output_data (l, val2->v.val_int, NULL);
2268 break;
2269 case dw_val_class_vec:
2271 unsigned int elt_size = val2->v.val_vec.elt_size;
2272 unsigned int len = val2->v.val_vec.length;
2273 unsigned int i;
2274 unsigned char *p;
2276 l = len * elt_size;
2277 dw2_asm_output_data (1, l, NULL);
2278 if (elt_size > sizeof (HOST_WIDE_INT))
2280 elt_size /= 2;
2281 len *= 2;
2283 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2284 i < len;
2285 i++, p += elt_size)
2286 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2287 "fp or vector constant word %u", i);
2289 break;
2290 case dw_val_class_const_double:
2292 unsigned HOST_WIDE_INT first, second;
2293 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2295 dw2_asm_output_data (1, 2 * l, NULL);
2296 if (WORDS_BIG_ENDIAN)
2298 first = val2->v.val_double.high;
2299 second = val2->v.val_double.low;
2301 else
2303 first = val2->v.val_double.low;
2304 second = val2->v.val_double.high;
2306 dw2_asm_output_data (l, first, NULL);
2307 dw2_asm_output_data (l, second, NULL);
2309 break;
2310 case dw_val_class_wide_int:
2312 int i;
2313 int len = get_full_len (*val2->v.val_wide);
2314 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2316 dw2_asm_output_data (1, len * l, NULL);
2317 if (WORDS_BIG_ENDIAN)
2318 for (i = len - 1; i >= 0; --i)
2319 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2320 else
2321 for (i = 0; i < len; ++i)
2322 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2324 break;
2325 default:
2326 gcc_unreachable ();
2329 break;
2330 case DW_OP_regval_type:
2331 case DW_OP_GNU_regval_type:
2333 unsigned r = val1->v.val_unsigned;
2334 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2335 gcc_assert (o);
2336 if (for_eh_or_skip >= 0)
2338 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2339 gcc_assert (size_of_uleb128 (r)
2340 == size_of_uleb128 (val1->v.val_unsigned));
2342 dw2_asm_output_data_uleb128 (r, NULL);
2343 dw2_asm_output_data_uleb128 (o, NULL);
2345 break;
2346 case DW_OP_deref_type:
2347 case DW_OP_GNU_deref_type:
2349 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2350 gcc_assert (o);
2351 dw2_asm_output_data (1, val1->v.val_int, NULL);
2352 dw2_asm_output_data_uleb128 (o, NULL);
2354 break;
2355 case DW_OP_convert:
2356 case DW_OP_reinterpret:
2357 case DW_OP_GNU_convert:
2358 case DW_OP_GNU_reinterpret:
2359 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2360 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2361 else
2363 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2364 gcc_assert (o);
2365 dw2_asm_output_data_uleb128 (o, NULL);
2367 break;
2369 case DW_OP_GNU_parameter_ref:
2371 unsigned long o;
2372 gcc_assert (val1->val_class == dw_val_class_die_ref);
2373 o = get_ref_die_offset (val1->v.val_die_ref.die);
2374 dw2_asm_output_data (4, o, NULL);
2376 break;
2378 default:
2379 /* Other codes have no operands. */
2380 break;
2384 /* Output a sequence of location operations.
2385 The for_eh_or_skip parameter controls whether register numbers are
2386 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2387 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2388 info). This should be suppressed for the cases that have not been converted
2389 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2391 void
2392 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2394 for (; loc != NULL; loc = loc->dw_loc_next)
2396 enum dwarf_location_atom opc = loc->dw_loc_opc;
2397 /* Output the opcode. */
2398 if (for_eh_or_skip >= 0
2399 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2401 unsigned r = (opc - DW_OP_breg0);
2402 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2403 gcc_assert (r <= 31);
2404 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2406 else if (for_eh_or_skip >= 0
2407 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2409 unsigned r = (opc - DW_OP_reg0);
2410 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2411 gcc_assert (r <= 31);
2412 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2415 dw2_asm_output_data (1, opc,
2416 "%s", dwarf_stack_op_name (opc));
2418 /* Output the operand(s) (if any). */
2419 output_loc_operands (loc, for_eh_or_skip);
2423 /* Output location description stack opcode's operands (if any).
2424 The output is single bytes on a line, suitable for .cfi_escape. */
2426 static void
2427 output_loc_operands_raw (dw_loc_descr_ref loc)
2429 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2430 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2432 switch (loc->dw_loc_opc)
2434 case DW_OP_addr:
2435 case DW_OP_GNU_addr_index:
2436 case DW_OP_GNU_const_index:
2437 case DW_OP_implicit_value:
2438 /* We cannot output addresses in .cfi_escape, only bytes. */
2439 gcc_unreachable ();
2441 case DW_OP_const1u:
2442 case DW_OP_const1s:
2443 case DW_OP_pick:
2444 case DW_OP_deref_size:
2445 case DW_OP_xderef_size:
2446 fputc (',', asm_out_file);
2447 dw2_asm_output_data_raw (1, val1->v.val_int);
2448 break;
2450 case DW_OP_const2u:
2451 case DW_OP_const2s:
2452 fputc (',', asm_out_file);
2453 dw2_asm_output_data_raw (2, val1->v.val_int);
2454 break;
2456 case DW_OP_const4u:
2457 case DW_OP_const4s:
2458 fputc (',', asm_out_file);
2459 dw2_asm_output_data_raw (4, val1->v.val_int);
2460 break;
2462 case DW_OP_const8u:
2463 case DW_OP_const8s:
2464 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2465 fputc (',', asm_out_file);
2466 dw2_asm_output_data_raw (8, val1->v.val_int);
2467 break;
2469 case DW_OP_skip:
2470 case DW_OP_bra:
2472 int offset;
2474 gcc_assert (val1->val_class == dw_val_class_loc);
2475 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2477 fputc (',', asm_out_file);
2478 dw2_asm_output_data_raw (2, offset);
2480 break;
2482 case DW_OP_regx:
2484 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2485 gcc_assert (size_of_uleb128 (r)
2486 == size_of_uleb128 (val1->v.val_unsigned));
2487 fputc (',', asm_out_file);
2488 dw2_asm_output_data_uleb128_raw (r);
2490 break;
2492 case DW_OP_constu:
2493 case DW_OP_plus_uconst:
2494 case DW_OP_piece:
2495 fputc (',', asm_out_file);
2496 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2497 break;
2499 case DW_OP_bit_piece:
2500 fputc (',', asm_out_file);
2501 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2502 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2503 break;
2505 case DW_OP_consts:
2506 case DW_OP_breg0:
2507 case DW_OP_breg1:
2508 case DW_OP_breg2:
2509 case DW_OP_breg3:
2510 case DW_OP_breg4:
2511 case DW_OP_breg5:
2512 case DW_OP_breg6:
2513 case DW_OP_breg7:
2514 case DW_OP_breg8:
2515 case DW_OP_breg9:
2516 case DW_OP_breg10:
2517 case DW_OP_breg11:
2518 case DW_OP_breg12:
2519 case DW_OP_breg13:
2520 case DW_OP_breg14:
2521 case DW_OP_breg15:
2522 case DW_OP_breg16:
2523 case DW_OP_breg17:
2524 case DW_OP_breg18:
2525 case DW_OP_breg19:
2526 case DW_OP_breg20:
2527 case DW_OP_breg21:
2528 case DW_OP_breg22:
2529 case DW_OP_breg23:
2530 case DW_OP_breg24:
2531 case DW_OP_breg25:
2532 case DW_OP_breg26:
2533 case DW_OP_breg27:
2534 case DW_OP_breg28:
2535 case DW_OP_breg29:
2536 case DW_OP_breg30:
2537 case DW_OP_breg31:
2538 case DW_OP_fbreg:
2539 fputc (',', asm_out_file);
2540 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2541 break;
2543 case DW_OP_bregx:
2545 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2546 gcc_assert (size_of_uleb128 (r)
2547 == size_of_uleb128 (val1->v.val_unsigned));
2548 fputc (',', asm_out_file);
2549 dw2_asm_output_data_uleb128_raw (r);
2550 fputc (',', asm_out_file);
2551 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2553 break;
2555 case DW_OP_implicit_pointer:
2556 case DW_OP_entry_value:
2557 case DW_OP_const_type:
2558 case DW_OP_regval_type:
2559 case DW_OP_deref_type:
2560 case DW_OP_convert:
2561 case DW_OP_reinterpret:
2562 case DW_OP_GNU_implicit_pointer:
2563 case DW_OP_GNU_entry_value:
2564 case DW_OP_GNU_const_type:
2565 case DW_OP_GNU_regval_type:
2566 case DW_OP_GNU_deref_type:
2567 case DW_OP_GNU_convert:
2568 case DW_OP_GNU_reinterpret:
2569 case DW_OP_GNU_parameter_ref:
2570 gcc_unreachable ();
2571 break;
2573 default:
2574 /* Other codes have no operands. */
2575 break;
2579 void
2580 output_loc_sequence_raw (dw_loc_descr_ref loc)
2582 while (1)
2584 enum dwarf_location_atom opc = loc->dw_loc_opc;
2585 /* Output the opcode. */
2586 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2588 unsigned r = (opc - DW_OP_breg0);
2589 r = DWARF2_FRAME_REG_OUT (r, 1);
2590 gcc_assert (r <= 31);
2591 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2593 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2595 unsigned r = (opc - DW_OP_reg0);
2596 r = DWARF2_FRAME_REG_OUT (r, 1);
2597 gcc_assert (r <= 31);
2598 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2600 /* Output the opcode. */
2601 fprintf (asm_out_file, "%#x", opc);
2602 output_loc_operands_raw (loc);
2604 if (!loc->dw_loc_next)
2605 break;
2606 loc = loc->dw_loc_next;
2608 fputc (',', asm_out_file);
2612 /* This function builds a dwarf location descriptor sequence from a
2613 dw_cfa_location, adding the given OFFSET to the result of the
2614 expression. */
2616 struct dw_loc_descr_node *
2617 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2619 struct dw_loc_descr_node *head, *tmp;
2621 offset += cfa->offset;
2623 if (cfa->indirect)
2625 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2626 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2627 head->dw_loc_oprnd1.val_entry = NULL;
2628 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2629 add_loc_descr (&head, tmp);
2630 if (offset != 0)
2632 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2633 add_loc_descr (&head, tmp);
2636 else
2637 head = new_reg_loc_descr (cfa->reg, offset);
2639 return head;
2642 /* This function builds a dwarf location descriptor sequence for
2643 the address at OFFSET from the CFA when stack is aligned to
2644 ALIGNMENT byte. */
2646 struct dw_loc_descr_node *
2647 build_cfa_aligned_loc (dw_cfa_location *cfa,
2648 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2650 struct dw_loc_descr_node *head;
2651 unsigned int dwarf_fp
2652 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2654 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2655 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2657 head = new_reg_loc_descr (dwarf_fp, 0);
2658 add_loc_descr (&head, int_loc_descriptor (alignment));
2659 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2660 loc_descr_plus_const (&head, offset);
2662 else
2663 head = new_reg_loc_descr (dwarf_fp, offset);
2664 return head;
2667 /* And now, the support for symbolic debugging information. */
2669 /* .debug_str support. */
2671 static void dwarf2out_init (const char *);
2672 static void dwarf2out_finish (const char *);
2673 static void dwarf2out_early_finish (const char *);
2674 static void dwarf2out_assembly_start (void);
2675 static void dwarf2out_define (unsigned int, const char *);
2676 static void dwarf2out_undef (unsigned int, const char *);
2677 static void dwarf2out_start_source_file (unsigned, const char *);
2678 static void dwarf2out_end_source_file (unsigned);
2679 static void dwarf2out_function_decl (tree);
2680 static void dwarf2out_begin_block (unsigned, unsigned);
2681 static void dwarf2out_end_block (unsigned, unsigned);
2682 static bool dwarf2out_ignore_block (const_tree);
2683 static void dwarf2out_early_global_decl (tree);
2684 static void dwarf2out_late_global_decl (tree);
2685 static void dwarf2out_type_decl (tree, int);
2686 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2687 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2688 dw_die_ref);
2689 static void dwarf2out_abstract_function (tree);
2690 static void dwarf2out_var_location (rtx_insn *);
2691 static void dwarf2out_size_function (tree);
2692 static void dwarf2out_begin_function (tree);
2693 static void dwarf2out_end_function (unsigned int);
2694 static void dwarf2out_register_main_translation_unit (tree unit);
2695 static void dwarf2out_set_name (tree, tree);
2696 static void dwarf2out_register_external_die (tree decl, const char *sym,
2697 unsigned HOST_WIDE_INT off);
2698 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2699 unsigned HOST_WIDE_INT *off);
2701 /* The debug hooks structure. */
2703 const struct gcc_debug_hooks dwarf2_debug_hooks =
2705 dwarf2out_init,
2706 dwarf2out_finish,
2707 dwarf2out_early_finish,
2708 dwarf2out_assembly_start,
2709 dwarf2out_define,
2710 dwarf2out_undef,
2711 dwarf2out_start_source_file,
2712 dwarf2out_end_source_file,
2713 dwarf2out_begin_block,
2714 dwarf2out_end_block,
2715 dwarf2out_ignore_block,
2716 dwarf2out_source_line,
2717 dwarf2out_begin_prologue,
2718 #if VMS_DEBUGGING_INFO
2719 dwarf2out_vms_end_prologue,
2720 dwarf2out_vms_begin_epilogue,
2721 #else
2722 debug_nothing_int_charstar,
2723 debug_nothing_int_charstar,
2724 #endif
2725 dwarf2out_end_epilogue,
2726 dwarf2out_begin_function,
2727 dwarf2out_end_function, /* end_function */
2728 dwarf2out_register_main_translation_unit,
2729 dwarf2out_function_decl, /* function_decl */
2730 dwarf2out_early_global_decl,
2731 dwarf2out_late_global_decl,
2732 dwarf2out_type_decl, /* type_decl */
2733 dwarf2out_imported_module_or_decl,
2734 dwarf2out_die_ref_for_decl,
2735 dwarf2out_register_external_die,
2736 debug_nothing_tree, /* deferred_inline_function */
2737 /* The DWARF 2 backend tries to reduce debugging bloat by not
2738 emitting the abstract description of inline functions until
2739 something tries to reference them. */
2740 dwarf2out_abstract_function, /* outlining_inline_function */
2741 debug_nothing_rtx_code_label, /* label */
2742 debug_nothing_int, /* handle_pch */
2743 dwarf2out_var_location,
2744 dwarf2out_size_function, /* size_function */
2745 dwarf2out_switch_text_section,
2746 dwarf2out_set_name,
2747 1, /* start_end_main_source_file */
2748 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2751 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2753 dwarf2out_init,
2754 debug_nothing_charstar,
2755 debug_nothing_charstar,
2756 dwarf2out_assembly_start,
2757 debug_nothing_int_charstar,
2758 debug_nothing_int_charstar,
2759 debug_nothing_int_charstar,
2760 debug_nothing_int,
2761 debug_nothing_int_int, /* begin_block */
2762 debug_nothing_int_int, /* end_block */
2763 debug_true_const_tree, /* ignore_block */
2764 dwarf2out_source_line, /* source_line */
2765 debug_nothing_int_int_charstar, /* begin_prologue */
2766 debug_nothing_int_charstar, /* end_prologue */
2767 debug_nothing_int_charstar, /* begin_epilogue */
2768 debug_nothing_int_charstar, /* end_epilogue */
2769 debug_nothing_tree, /* begin_function */
2770 debug_nothing_int, /* end_function */
2771 debug_nothing_tree, /* register_main_translation_unit */
2772 debug_nothing_tree, /* function_decl */
2773 debug_nothing_tree, /* early_global_decl */
2774 debug_nothing_tree, /* late_global_decl */
2775 debug_nothing_tree_int, /* type_decl */
2776 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2777 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2778 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2779 debug_nothing_tree, /* deferred_inline_function */
2780 debug_nothing_tree, /* outlining_inline_function */
2781 debug_nothing_rtx_code_label, /* label */
2782 debug_nothing_int, /* handle_pch */
2783 debug_nothing_rtx_insn, /* var_location */
2784 debug_nothing_tree, /* size_function */
2785 debug_nothing_void, /* switch_text_section */
2786 debug_nothing_tree_tree, /* set_name */
2787 0, /* start_end_main_source_file */
2788 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2791 /* NOTE: In the comments in this file, many references are made to
2792 "Debugging Information Entries". This term is abbreviated as `DIE'
2793 throughout the remainder of this file. */
2795 /* An internal representation of the DWARF output is built, and then
2796 walked to generate the DWARF debugging info. The walk of the internal
2797 representation is done after the entire program has been compiled.
2798 The types below are used to describe the internal representation. */
2800 /* Whether to put type DIEs into their own section .debug_types instead
2801 of making them part of the .debug_info section. Only supported for
2802 Dwarf V4 or higher and the user didn't disable them through
2803 -fno-debug-types-section. It is more efficient to put them in a
2804 separate comdat sections since the linker will then be able to
2805 remove duplicates. But not all tools support .debug_types sections
2806 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2807 it is DW_UT_type unit type in .debug_info section. */
2809 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2811 /* Various DIE's use offsets relative to the beginning of the
2812 .debug_info section to refer to each other. */
2814 typedef long int dw_offset;
2816 struct comdat_type_node;
2818 /* The entries in the line_info table more-or-less mirror the opcodes
2819 that are used in the real dwarf line table. Arrays of these entries
2820 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2821 supported. */
2823 enum dw_line_info_opcode {
2824 /* Emit DW_LNE_set_address; the operand is the label index. */
2825 LI_set_address,
2827 /* Emit a row to the matrix with the given line. This may be done
2828 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2829 special opcodes. */
2830 LI_set_line,
2832 /* Emit a DW_LNS_set_file. */
2833 LI_set_file,
2835 /* Emit a DW_LNS_set_column. */
2836 LI_set_column,
2838 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2839 LI_negate_stmt,
2841 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2842 LI_set_prologue_end,
2843 LI_set_epilogue_begin,
2845 /* Emit a DW_LNE_set_discriminator. */
2846 LI_set_discriminator
2849 typedef struct GTY(()) dw_line_info_struct {
2850 enum dw_line_info_opcode opcode;
2851 unsigned int val;
2852 } dw_line_info_entry;
2855 struct GTY(()) dw_line_info_table {
2856 /* The label that marks the end of this section. */
2857 const char *end_label;
2859 /* The values for the last row of the matrix, as collected in the table.
2860 These are used to minimize the changes to the next row. */
2861 unsigned int file_num;
2862 unsigned int line_num;
2863 unsigned int column_num;
2864 int discrim_num;
2865 bool is_stmt;
2866 bool in_use;
2868 vec<dw_line_info_entry, va_gc> *entries;
2872 /* Each DIE attribute has a field specifying the attribute kind,
2873 a link to the next attribute in the chain, and an attribute value.
2874 Attributes are typically linked below the DIE they modify. */
2876 typedef struct GTY(()) dw_attr_struct {
2877 enum dwarf_attribute dw_attr;
2878 dw_val_node dw_attr_val;
2880 dw_attr_node;
2883 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2884 The children of each node form a circular list linked by
2885 die_sib. die_child points to the node *before* the "first" child node. */
2887 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2888 union die_symbol_or_type_node
2890 const char * GTY ((tag ("0"))) die_symbol;
2891 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2893 GTY ((desc ("%0.comdat_type_p"))) die_id;
2894 vec<dw_attr_node, va_gc> *die_attr;
2895 dw_die_ref die_parent;
2896 dw_die_ref die_child;
2897 dw_die_ref die_sib;
2898 dw_die_ref die_definition; /* ref from a specification to its definition */
2899 dw_offset die_offset;
2900 unsigned long die_abbrev;
2901 int die_mark;
2902 unsigned int decl_id;
2903 enum dwarf_tag die_tag;
2904 /* Die is used and must not be pruned as unused. */
2905 BOOL_BITFIELD die_perennial_p : 1;
2906 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2907 /* For an external ref to die_symbol if die_offset contains an extra
2908 offset to that symbol. */
2909 BOOL_BITFIELD with_offset : 1;
2910 /* Whether this DIE was removed from the DIE tree, for example via
2911 prune_unused_types. We don't consider those present from the
2912 DIE lookup routines. */
2913 BOOL_BITFIELD removed : 1;
2914 /* Lots of spare bits. */
2916 die_node;
2918 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2919 static bool early_dwarf;
2920 static bool early_dwarf_finished;
2921 struct set_early_dwarf {
2922 bool saved;
2923 set_early_dwarf () : saved(early_dwarf)
2925 gcc_assert (! early_dwarf_finished);
2926 early_dwarf = true;
2928 ~set_early_dwarf () { early_dwarf = saved; }
2931 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2932 #define FOR_EACH_CHILD(die, c, expr) do { \
2933 c = die->die_child; \
2934 if (c) do { \
2935 c = c->die_sib; \
2936 expr; \
2937 } while (c != die->die_child); \
2938 } while (0)
2940 /* The pubname structure */
2942 typedef struct GTY(()) pubname_struct {
2943 dw_die_ref die;
2944 const char *name;
2946 pubname_entry;
2949 struct GTY(()) dw_ranges {
2950 const char *label;
2951 /* If this is positive, it's a block number, otherwise it's a
2952 bitwise-negated index into dw_ranges_by_label. */
2953 int num;
2954 /* Index for the range list for DW_FORM_rnglistx. */
2955 unsigned int idx : 31;
2956 /* True if this range might be possibly in a different section
2957 from previous entry. */
2958 unsigned int maybe_new_sec : 1;
2961 /* A structure to hold a macinfo entry. */
2963 typedef struct GTY(()) macinfo_struct {
2964 unsigned char code;
2965 unsigned HOST_WIDE_INT lineno;
2966 const char *info;
2968 macinfo_entry;
2971 struct GTY(()) dw_ranges_by_label {
2972 const char *begin;
2973 const char *end;
2976 /* The comdat type node structure. */
2977 struct GTY(()) comdat_type_node
2979 dw_die_ref root_die;
2980 dw_die_ref type_die;
2981 dw_die_ref skeleton_die;
2982 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2983 comdat_type_node *next;
2986 /* A list of DIEs for which we can't determine ancestry (parent_die
2987 field) just yet. Later in dwarf2out_finish we will fill in the
2988 missing bits. */
2989 typedef struct GTY(()) limbo_die_struct {
2990 dw_die_ref die;
2991 /* The tree for which this DIE was created. We use this to
2992 determine ancestry later. */
2993 tree created_for;
2994 struct limbo_die_struct *next;
2996 limbo_die_node;
2998 typedef struct skeleton_chain_struct
3000 dw_die_ref old_die;
3001 dw_die_ref new_die;
3002 struct skeleton_chain_struct *parent;
3004 skeleton_chain_node;
3006 /* Define a macro which returns nonzero for a TYPE_DECL which was
3007 implicitly generated for a type.
3009 Note that, unlike the C front-end (which generates a NULL named
3010 TYPE_DECL node for each complete tagged type, each array type,
3011 and each function type node created) the C++ front-end generates
3012 a _named_ TYPE_DECL node for each tagged type node created.
3013 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3014 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3015 front-end, but for each type, tagged or not. */
3017 #define TYPE_DECL_IS_STUB(decl) \
3018 (DECL_NAME (decl) == NULL_TREE \
3019 || (DECL_ARTIFICIAL (decl) \
3020 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3021 /* This is necessary for stub decls that \
3022 appear in nested inline functions. */ \
3023 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3024 && (decl_ultimate_origin (decl) \
3025 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3027 /* Information concerning the compilation unit's programming
3028 language, and compiler version. */
3030 /* Fixed size portion of the DWARF compilation unit header. */
3031 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3032 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3033 + (dwarf_version >= 5 ? 4 : 3))
3035 /* Fixed size portion of the DWARF comdat type unit header. */
3036 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3037 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3038 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3040 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3041 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3042 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3044 /* Fixed size portion of public names info. */
3045 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3047 /* Fixed size portion of the address range info. */
3048 #define DWARF_ARANGES_HEADER_SIZE \
3049 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3050 DWARF2_ADDR_SIZE * 2) \
3051 - DWARF_INITIAL_LENGTH_SIZE)
3053 /* Size of padding portion in the address range info. It must be
3054 aligned to twice the pointer size. */
3055 #define DWARF_ARANGES_PAD_SIZE \
3056 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3057 DWARF2_ADDR_SIZE * 2) \
3058 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3060 /* Use assembler line directives if available. */
3061 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3062 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3063 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3064 #else
3065 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3066 #endif
3067 #endif
3069 /* Minimum line offset in a special line info. opcode.
3070 This value was chosen to give a reasonable range of values. */
3071 #define DWARF_LINE_BASE -10
3073 /* First special line opcode - leave room for the standard opcodes. */
3074 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3076 /* Range of line offsets in a special line info. opcode. */
3077 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3079 /* Flag that indicates the initial value of the is_stmt_start flag.
3080 In the present implementation, we do not mark any lines as
3081 the beginning of a source statement, because that information
3082 is not made available by the GCC front-end. */
3083 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3085 /* Maximum number of operations per instruction bundle. */
3086 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3087 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3088 #endif
3090 /* This location is used by calc_die_sizes() to keep track
3091 the offset of each DIE within the .debug_info section. */
3092 static unsigned long next_die_offset;
3094 /* Record the root of the DIE's built for the current compilation unit. */
3095 static GTY(()) dw_die_ref single_comp_unit_die;
3097 /* A list of type DIEs that have been separated into comdat sections. */
3098 static GTY(()) comdat_type_node *comdat_type_list;
3100 /* A list of CU DIEs that have been separated. */
3101 static GTY(()) limbo_die_node *cu_die_list;
3103 /* A list of DIEs with a NULL parent waiting to be relocated. */
3104 static GTY(()) limbo_die_node *limbo_die_list;
3106 /* A list of DIEs for which we may have to generate
3107 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3108 static GTY(()) limbo_die_node *deferred_asm_name;
3110 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3112 typedef const char *compare_type;
3114 static hashval_t hash (dwarf_file_data *);
3115 static bool equal (dwarf_file_data *, const char *);
3118 /* Filenames referenced by this compilation unit. */
3119 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3121 struct decl_die_hasher : ggc_ptr_hash<die_node>
3123 typedef tree compare_type;
3125 static hashval_t hash (die_node *);
3126 static bool equal (die_node *, tree);
3128 /* A hash table of references to DIE's that describe declarations.
3129 The key is a DECL_UID() which is a unique number identifying each decl. */
3130 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3132 struct GTY ((for_user)) variable_value_struct {
3133 unsigned int decl_id;
3134 vec<dw_die_ref, va_gc> *dies;
3137 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3139 typedef tree compare_type;
3141 static hashval_t hash (variable_value_struct *);
3142 static bool equal (variable_value_struct *, tree);
3144 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3145 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3146 DECL_CONTEXT of the referenced VAR_DECLs. */
3147 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3149 struct block_die_hasher : ggc_ptr_hash<die_struct>
3151 static hashval_t hash (die_struct *);
3152 static bool equal (die_struct *, die_struct *);
3155 /* A hash table of references to DIE's that describe COMMON blocks.
3156 The key is DECL_UID() ^ die_parent. */
3157 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3159 typedef struct GTY(()) die_arg_entry_struct {
3160 dw_die_ref die;
3161 tree arg;
3162 } die_arg_entry;
3165 /* Node of the variable location list. */
3166 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3167 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3168 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3169 in mode of the EXPR_LIST node and first EXPR_LIST operand
3170 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3171 location or NULL for padding. For larger bitsizes,
3172 mode is 0 and first operand is a CONCAT with bitsize
3173 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3174 NULL as second operand. */
3175 rtx GTY (()) loc;
3176 const char * GTY (()) label;
3177 struct var_loc_node * GTY (()) next;
3180 /* Variable location list. */
3181 struct GTY ((for_user)) var_loc_list_def {
3182 struct var_loc_node * GTY (()) first;
3184 /* Pointer to the last but one or last element of the
3185 chained list. If the list is empty, both first and
3186 last are NULL, if the list contains just one node
3187 or the last node certainly is not redundant, it points
3188 to the last node, otherwise points to the last but one.
3189 Do not mark it for GC because it is marked through the chain. */
3190 struct var_loc_node * GTY ((skip ("%h"))) last;
3192 /* Pointer to the last element before section switch,
3193 if NULL, either sections weren't switched or first
3194 is after section switch. */
3195 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3197 /* DECL_UID of the variable decl. */
3198 unsigned int decl_id;
3200 typedef struct var_loc_list_def var_loc_list;
3202 /* Call argument location list. */
3203 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3204 rtx GTY (()) call_arg_loc_note;
3205 const char * GTY (()) label;
3206 tree GTY (()) block;
3207 bool tail_call_p;
3208 rtx GTY (()) symbol_ref;
3209 struct call_arg_loc_node * GTY (()) next;
3213 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3215 typedef const_tree compare_type;
3217 static hashval_t hash (var_loc_list *);
3218 static bool equal (var_loc_list *, const_tree);
3221 /* Table of decl location linked lists. */
3222 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3224 /* Head and tail of call_arg_loc chain. */
3225 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3226 static struct call_arg_loc_node *call_arg_loc_last;
3228 /* Number of call sites in the current function. */
3229 static int call_site_count = -1;
3230 /* Number of tail call sites in the current function. */
3231 static int tail_call_site_count = -1;
3233 /* A cached location list. */
3234 struct GTY ((for_user)) cached_dw_loc_list_def {
3235 /* The DECL_UID of the decl that this entry describes. */
3236 unsigned int decl_id;
3238 /* The cached location list. */
3239 dw_loc_list_ref loc_list;
3241 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3243 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3246 typedef const_tree compare_type;
3248 static hashval_t hash (cached_dw_loc_list *);
3249 static bool equal (cached_dw_loc_list *, const_tree);
3252 /* Table of cached location lists. */
3253 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3255 /* A vector of references to DIE's that are uniquely identified by their tag,
3256 presence/absence of children DIE's, and list of attribute/value pairs. */
3257 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3259 /* A hash map to remember the stack usage for DWARF procedures. The value
3260 stored is the stack size difference between before the DWARF procedure
3261 invokation and after it returned. In other words, for a DWARF procedure
3262 that consumes N stack slots and that pushes M ones, this stores M - N. */
3263 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3265 /* A global counter for generating labels for line number data. */
3266 static unsigned int line_info_label_num;
3268 /* The current table to which we should emit line number information
3269 for the current function. This will be set up at the beginning of
3270 assembly for the function. */
3271 static GTY(()) dw_line_info_table *cur_line_info_table;
3273 /* The two default tables of line number info. */
3274 static GTY(()) dw_line_info_table *text_section_line_info;
3275 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3277 /* The set of all non-default tables of line number info. */
3278 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3280 /* A flag to tell pubnames/types export if there is an info section to
3281 refer to. */
3282 static bool info_section_emitted;
3284 /* A pointer to the base of a table that contains a list of publicly
3285 accessible names. */
3286 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3288 /* A pointer to the base of a table that contains a list of publicly
3289 accessible types. */
3290 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3292 /* A pointer to the base of a table that contains a list of macro
3293 defines/undefines (and file start/end markers). */
3294 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3296 /* True if .debug_macinfo or .debug_macros section is going to be
3297 emitted. */
3298 #define have_macinfo \
3299 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3300 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3301 && !macinfo_table->is_empty ())
3303 /* Vector of dies for which we should generate .debug_ranges info. */
3304 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3306 /* Vector of pairs of labels referenced in ranges_table. */
3307 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3309 /* Whether we have location lists that need outputting */
3310 static GTY(()) bool have_location_lists;
3312 /* Unique label counter. */
3313 static GTY(()) unsigned int loclabel_num;
3315 /* Unique label counter for point-of-call tables. */
3316 static GTY(()) unsigned int poc_label_num;
3318 /* The last file entry emitted by maybe_emit_file(). */
3319 static GTY(()) struct dwarf_file_data * last_emitted_file;
3321 /* Number of internal labels generated by gen_internal_sym(). */
3322 static GTY(()) int label_num;
3324 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3326 /* Instances of generic types for which we need to generate debug
3327 info that describe their generic parameters and arguments. That
3328 generation needs to happen once all types are properly laid out so
3329 we do it at the end of compilation. */
3330 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3332 /* Offset from the "steady-state frame pointer" to the frame base,
3333 within the current function. */
3334 static HOST_WIDE_INT frame_pointer_fb_offset;
3335 static bool frame_pointer_fb_offset_valid;
3337 static vec<dw_die_ref> base_types;
3339 /* Flags to represent a set of attribute classes for attributes that represent
3340 a scalar value (bounds, pointers, ...). */
3341 enum dw_scalar_form
3343 dw_scalar_form_constant = 0x01,
3344 dw_scalar_form_exprloc = 0x02,
3345 dw_scalar_form_reference = 0x04
3348 /* Forward declarations for functions defined in this file. */
3350 static int is_pseudo_reg (const_rtx);
3351 static tree type_main_variant (tree);
3352 static int is_tagged_type (const_tree);
3353 static const char *dwarf_tag_name (unsigned);
3354 static const char *dwarf_attr_name (unsigned);
3355 static const char *dwarf_form_name (unsigned);
3356 static tree decl_ultimate_origin (const_tree);
3357 static tree decl_class_context (tree);
3358 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3359 static inline enum dw_val_class AT_class (dw_attr_node *);
3360 static inline unsigned int AT_index (dw_attr_node *);
3361 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3362 static inline unsigned AT_flag (dw_attr_node *);
3363 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3364 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3365 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3366 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3367 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3368 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3369 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3370 unsigned int, unsigned char *);
3371 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3372 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3373 static inline const char *AT_string (dw_attr_node *);
3374 static enum dwarf_form AT_string_form (dw_attr_node *);
3375 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3376 static void add_AT_specification (dw_die_ref, dw_die_ref);
3377 static inline dw_die_ref AT_ref (dw_attr_node *);
3378 static inline int AT_ref_external (dw_attr_node *);
3379 static inline void set_AT_ref_external (dw_attr_node *, int);
3380 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3381 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3382 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3383 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3384 dw_loc_list_ref);
3385 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3386 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3387 static void remove_addr_table_entry (addr_table_entry *);
3388 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3389 static inline rtx AT_addr (dw_attr_node *);
3390 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3391 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3392 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3393 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3394 const char *);
3395 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3396 unsigned HOST_WIDE_INT);
3397 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3398 unsigned long, bool);
3399 static inline const char *AT_lbl (dw_attr_node *);
3400 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3401 static const char *get_AT_low_pc (dw_die_ref);
3402 static const char *get_AT_hi_pc (dw_die_ref);
3403 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3404 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3405 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3406 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3407 static bool is_cxx (void);
3408 static bool is_cxx (const_tree);
3409 static bool is_fortran (void);
3410 static bool is_ada (void);
3411 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3412 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3413 static void add_child_die (dw_die_ref, dw_die_ref);
3414 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3415 static dw_die_ref lookup_type_die (tree);
3416 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3417 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3418 static void equate_type_number_to_die (tree, dw_die_ref);
3419 static dw_die_ref lookup_decl_die (tree);
3420 static var_loc_list *lookup_decl_loc (const_tree);
3421 static void equate_decl_number_to_die (tree, dw_die_ref);
3422 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3423 static void print_spaces (FILE *);
3424 static void print_die (dw_die_ref, FILE *);
3425 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3426 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3427 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3428 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3429 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3430 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3431 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3432 struct md5_ctx *, int *);
3433 struct checksum_attributes;
3434 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3435 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3436 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3437 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3438 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3439 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3440 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3441 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3442 static int is_type_die (dw_die_ref);
3443 static int is_comdat_die (dw_die_ref);
3444 static inline bool is_template_instantiation (dw_die_ref);
3445 static int is_declaration_die (dw_die_ref);
3446 static int should_move_die_to_comdat (dw_die_ref);
3447 static dw_die_ref clone_as_declaration (dw_die_ref);
3448 static dw_die_ref clone_die (dw_die_ref);
3449 static dw_die_ref clone_tree (dw_die_ref);
3450 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3451 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3452 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3453 static dw_die_ref generate_skeleton (dw_die_ref);
3454 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3455 dw_die_ref,
3456 dw_die_ref);
3457 static void break_out_comdat_types (dw_die_ref);
3458 static void copy_decls_for_unworthy_types (dw_die_ref);
3460 static void add_sibling_attributes (dw_die_ref);
3461 static void output_location_lists (dw_die_ref);
3462 static int constant_size (unsigned HOST_WIDE_INT);
3463 static unsigned long size_of_die (dw_die_ref);
3464 static void calc_die_sizes (dw_die_ref);
3465 static void calc_base_type_die_sizes (void);
3466 static void mark_dies (dw_die_ref);
3467 static void unmark_dies (dw_die_ref);
3468 static void unmark_all_dies (dw_die_ref);
3469 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3470 static unsigned long size_of_aranges (void);
3471 static enum dwarf_form value_format (dw_attr_node *);
3472 static void output_value_format (dw_attr_node *);
3473 static void output_abbrev_section (void);
3474 static void output_die_abbrevs (unsigned long, dw_die_ref);
3475 static void output_die_symbol (dw_die_ref);
3476 static void output_die (dw_die_ref);
3477 static void output_compilation_unit_header (enum dwarf_unit_type);
3478 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3479 static void output_comdat_type_unit (comdat_type_node *);
3480 static const char *dwarf2_name (tree, int);
3481 static void add_pubname (tree, dw_die_ref);
3482 static void add_enumerator_pubname (const char *, dw_die_ref);
3483 static void add_pubname_string (const char *, dw_die_ref);
3484 static void add_pubtype (tree, dw_die_ref);
3485 static void output_pubnames (vec<pubname_entry, va_gc> *);
3486 static void output_aranges (void);
3487 static unsigned int add_ranges (const_tree, bool = false);
3488 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3489 bool *, bool);
3490 static void output_ranges (void);
3491 static dw_line_info_table *new_line_info_table (void);
3492 static void output_line_info (bool);
3493 static void output_file_names (void);
3494 static dw_die_ref base_type_die (tree, bool);
3495 static int is_base_type (tree);
3496 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3497 static int decl_quals (const_tree);
3498 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3499 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3500 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3501 static int type_is_enum (const_tree);
3502 static unsigned int dbx_reg_number (const_rtx);
3503 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3504 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3505 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3506 enum var_init_status);
3507 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3508 enum var_init_status);
3509 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3510 enum var_init_status);
3511 static int is_based_loc (const_rtx);
3512 static bool resolve_one_addr (rtx *);
3513 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3514 enum var_init_status);
3515 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3516 enum var_init_status);
3517 struct loc_descr_context;
3518 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3519 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3520 static dw_loc_list_ref loc_list_from_tree (tree, int,
3521 struct loc_descr_context *);
3522 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3523 struct loc_descr_context *);
3524 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3525 static tree field_type (const_tree);
3526 static unsigned int simple_type_align_in_bits (const_tree);
3527 static unsigned int simple_decl_align_in_bits (const_tree);
3528 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3529 struct vlr_context;
3530 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3531 HOST_WIDE_INT *);
3532 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3533 dw_loc_list_ref);
3534 static void add_data_member_location_attribute (dw_die_ref, tree,
3535 struct vlr_context *);
3536 static bool add_const_value_attribute (dw_die_ref, rtx);
3537 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3538 static void insert_wide_int (const wide_int &, unsigned char *, int);
3539 static void insert_float (const_rtx, unsigned char *);
3540 static rtx rtl_for_decl_location (tree);
3541 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3542 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3543 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3544 static void add_name_attribute (dw_die_ref, const char *);
3545 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3546 static void add_comp_dir_attribute (dw_die_ref);
3547 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3548 struct loc_descr_context *);
3549 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3550 struct loc_descr_context *);
3551 static void add_subscript_info (dw_die_ref, tree, bool);
3552 static void add_byte_size_attribute (dw_die_ref, tree);
3553 static void add_alignment_attribute (dw_die_ref, tree);
3554 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3555 struct vlr_context *);
3556 static void add_bit_size_attribute (dw_die_ref, tree);
3557 static void add_prototyped_attribute (dw_die_ref, tree);
3558 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3559 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3560 static void add_src_coords_attributes (dw_die_ref, tree);
3561 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3562 static void add_discr_value (dw_die_ref, dw_discr_value *);
3563 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3564 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3565 static void push_decl_scope (tree);
3566 static void pop_decl_scope (void);
3567 static dw_die_ref scope_die_for (tree, dw_die_ref);
3568 static inline int local_scope_p (dw_die_ref);
3569 static inline int class_scope_p (dw_die_ref);
3570 static inline int class_or_namespace_scope_p (dw_die_ref);
3571 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3572 static void add_calling_convention_attribute (dw_die_ref, tree);
3573 static const char *type_tag (const_tree);
3574 static tree member_declared_type (const_tree);
3575 #if 0
3576 static const char *decl_start_label (tree);
3577 #endif
3578 static void gen_array_type_die (tree, dw_die_ref);
3579 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3580 #if 0
3581 static void gen_entry_point_die (tree, dw_die_ref);
3582 #endif
3583 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3584 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3585 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3586 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3587 static void gen_formal_types_die (tree, dw_die_ref);
3588 static void gen_subprogram_die (tree, dw_die_ref);
3589 static void gen_variable_die (tree, tree, dw_die_ref);
3590 static void gen_const_die (tree, dw_die_ref);
3591 static void gen_label_die (tree, dw_die_ref);
3592 static void gen_lexical_block_die (tree, dw_die_ref);
3593 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3594 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3595 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3596 static dw_die_ref gen_compile_unit_die (const char *);
3597 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3598 static void gen_member_die (tree, dw_die_ref);
3599 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3600 enum debug_info_usage);
3601 static void gen_subroutine_type_die (tree, dw_die_ref);
3602 static void gen_typedef_die (tree, dw_die_ref);
3603 static void gen_type_die (tree, dw_die_ref);
3604 static void gen_block_die (tree, dw_die_ref);
3605 static void decls_for_scope (tree, dw_die_ref);
3606 static bool is_naming_typedef_decl (const_tree);
3607 static inline dw_die_ref get_context_die (tree);
3608 static void gen_namespace_die (tree, dw_die_ref);
3609 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3610 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3611 static dw_die_ref force_decl_die (tree);
3612 static dw_die_ref force_type_die (tree);
3613 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3614 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3615 static struct dwarf_file_data * lookup_filename (const char *);
3616 static void retry_incomplete_types (void);
3617 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3618 static void gen_generic_params_dies (tree);
3619 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3620 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3621 static void splice_child_die (dw_die_ref, dw_die_ref);
3622 static int file_info_cmp (const void *, const void *);
3623 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3624 const char *, const char *);
3625 static void output_loc_list (dw_loc_list_ref);
3626 static char *gen_internal_sym (const char *);
3627 static bool want_pubnames (void);
3629 static void prune_unmark_dies (dw_die_ref);
3630 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3631 static void prune_unused_types_mark (dw_die_ref, int);
3632 static void prune_unused_types_walk (dw_die_ref);
3633 static void prune_unused_types_walk_attribs (dw_die_ref);
3634 static void prune_unused_types_prune (dw_die_ref);
3635 static void prune_unused_types (void);
3636 static int maybe_emit_file (struct dwarf_file_data *fd);
3637 static inline const char *AT_vms_delta1 (dw_attr_node *);
3638 static inline const char *AT_vms_delta2 (dw_attr_node *);
3639 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3640 const char *, const char *);
3641 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3642 static void gen_remaining_tmpl_value_param_die_attribute (void);
3643 static bool generic_type_p (tree);
3644 static void schedule_generic_params_dies_gen (tree t);
3645 static void gen_scheduled_generic_parms_dies (void);
3646 static void resolve_variable_values (void);
3648 static const char *comp_dir_string (void);
3650 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3652 /* enum for tracking thread-local variables whose address is really an offset
3653 relative to the TLS pointer, which will need link-time relocation, but will
3654 not need relocation by the DWARF consumer. */
3656 enum dtprel_bool
3658 dtprel_false = 0,
3659 dtprel_true = 1
3662 /* Return the operator to use for an address of a variable. For dtprel_true, we
3663 use DW_OP_const*. For regular variables, which need both link-time
3664 relocation and consumer-level relocation (e.g., to account for shared objects
3665 loaded at a random address), we use DW_OP_addr*. */
3667 static inline enum dwarf_location_atom
3668 dw_addr_op (enum dtprel_bool dtprel)
3670 if (dtprel == dtprel_true)
3671 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3672 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3673 else
3674 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3677 /* Return a pointer to a newly allocated address location description. If
3678 dwarf_split_debug_info is true, then record the address with the appropriate
3679 relocation. */
3680 static inline dw_loc_descr_ref
3681 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3683 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3685 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3686 ref->dw_loc_oprnd1.v.val_addr = addr;
3687 ref->dtprel = dtprel;
3688 if (dwarf_split_debug_info)
3689 ref->dw_loc_oprnd1.val_entry
3690 = add_addr_table_entry (addr,
3691 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3692 else
3693 ref->dw_loc_oprnd1.val_entry = NULL;
3695 return ref;
3698 /* Section names used to hold DWARF debugging information. */
3700 #ifndef DEBUG_INFO_SECTION
3701 #define DEBUG_INFO_SECTION ".debug_info"
3702 #endif
3703 #ifndef DEBUG_DWO_INFO_SECTION
3704 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3705 #endif
3706 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3707 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3708 #endif
3709 #ifndef DEBUG_LTO_INFO_SECTION
3710 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3711 #endif
3712 #ifndef DEBUG_ABBREV_SECTION
3713 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3714 #endif
3715 #ifndef DEBUG_DWO_ABBREV_SECTION
3716 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3717 #endif
3718 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3719 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3720 #endif
3721 #ifndef DEBUG_LTO_ABBREV_SECTION
3722 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3723 #endif
3724 #ifndef DEBUG_ARANGES_SECTION
3725 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3726 #endif
3727 #ifndef DEBUG_ADDR_SECTION
3728 #define DEBUG_ADDR_SECTION ".debug_addr"
3729 #endif
3730 #ifndef DEBUG_MACINFO_SECTION
3731 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3732 #endif
3733 #ifndef DEBUG_DWO_MACINFO_SECTION
3734 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3735 #endif
3736 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3737 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3738 #endif
3739 #ifndef DEBUG_LTO_MACINFO_SECTION
3740 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3741 #endif
3742 #ifndef DEBUG_DWO_MACRO_SECTION
3743 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3744 #endif
3745 #ifndef DEBUG_MACRO_SECTION
3746 #define DEBUG_MACRO_SECTION ".debug_macro"
3747 #endif
3748 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3749 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3750 #endif
3751 #ifndef DEBUG_LTO_MACRO_SECTION
3752 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3753 #endif
3754 #ifndef DEBUG_LINE_SECTION
3755 #define DEBUG_LINE_SECTION ".debug_line"
3756 #endif
3757 #ifndef DEBUG_DWO_LINE_SECTION
3758 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3759 #endif
3760 #ifndef DEBUG_LTO_LINE_SECTION
3761 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3762 #endif
3763 #ifndef DEBUG_LOC_SECTION
3764 #define DEBUG_LOC_SECTION ".debug_loc"
3765 #endif
3766 #ifndef DEBUG_DWO_LOC_SECTION
3767 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3768 #endif
3769 #ifndef DEBUG_LOCLISTS_SECTION
3770 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3771 #endif
3772 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3773 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3774 #endif
3775 #ifndef DEBUG_PUBNAMES_SECTION
3776 #define DEBUG_PUBNAMES_SECTION \
3777 ((debug_generate_pub_sections == 2) \
3778 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3779 #endif
3780 #ifndef DEBUG_PUBTYPES_SECTION
3781 #define DEBUG_PUBTYPES_SECTION \
3782 ((debug_generate_pub_sections == 2) \
3783 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3784 #endif
3785 #ifndef DEBUG_STR_OFFSETS_SECTION
3786 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3787 #endif
3788 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3789 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3790 #endif
3791 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3792 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3793 #endif
3794 #ifndef DEBUG_STR_DWO_SECTION
3795 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3796 #endif
3797 #ifndef DEBUG_LTO_STR_DWO_SECTION
3798 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3799 #endif
3800 #ifndef DEBUG_STR_SECTION
3801 #define DEBUG_STR_SECTION ".debug_str"
3802 #endif
3803 #ifndef DEBUG_LTO_STR_SECTION
3804 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3805 #endif
3806 #ifndef DEBUG_RANGES_SECTION
3807 #define DEBUG_RANGES_SECTION ".debug_ranges"
3808 #endif
3809 #ifndef DEBUG_RNGLISTS_SECTION
3810 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3811 #endif
3812 #ifndef DEBUG_LINE_STR_SECTION
3813 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3814 #endif
3816 /* Standard ELF section names for compiled code and data. */
3817 #ifndef TEXT_SECTION_NAME
3818 #define TEXT_SECTION_NAME ".text"
3819 #endif
3821 /* Section flags for .debug_str section. */
3822 #define DEBUG_STR_SECTION_FLAGS \
3823 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3824 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3825 : SECTION_DEBUG)
3827 /* Section flags for .debug_str.dwo section. */
3828 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3830 /* Attribute used to refer to the macro section. */
3831 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3832 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3834 /* Labels we insert at beginning sections we can reference instead of
3835 the section names themselves. */
3837 #ifndef TEXT_SECTION_LABEL
3838 #define TEXT_SECTION_LABEL "Ltext"
3839 #endif
3840 #ifndef COLD_TEXT_SECTION_LABEL
3841 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3842 #endif
3843 #ifndef DEBUG_LINE_SECTION_LABEL
3844 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3845 #endif
3846 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3847 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3848 #endif
3849 #ifndef DEBUG_INFO_SECTION_LABEL
3850 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3851 #endif
3852 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3853 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3854 #endif
3855 #ifndef DEBUG_ABBREV_SECTION_LABEL
3856 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3857 #endif
3858 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3859 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3860 #endif
3861 #ifndef DEBUG_ADDR_SECTION_LABEL
3862 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3863 #endif
3864 #ifndef DEBUG_LOC_SECTION_LABEL
3865 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3866 #endif
3867 #ifndef DEBUG_RANGES_SECTION_LABEL
3868 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3869 #endif
3870 #ifndef DEBUG_MACINFO_SECTION_LABEL
3871 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3872 #endif
3873 #ifndef DEBUG_MACRO_SECTION_LABEL
3874 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3875 #endif
3876 #define SKELETON_COMP_DIE_ABBREV 1
3877 #define SKELETON_TYPE_DIE_ABBREV 2
3879 /* Definitions of defaults for formats and names of various special
3880 (artificial) labels which may be generated within this file (when the -g
3881 options is used and DWARF2_DEBUGGING_INFO is in effect.
3882 If necessary, these may be overridden from within the tm.h file, but
3883 typically, overriding these defaults is unnecessary. */
3885 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3886 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3887 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3888 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3889 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3890 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3891 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3892 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3893 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3894 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3895 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3896 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3897 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3898 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3899 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3901 #ifndef TEXT_END_LABEL
3902 #define TEXT_END_LABEL "Letext"
3903 #endif
3904 #ifndef COLD_END_LABEL
3905 #define COLD_END_LABEL "Letext_cold"
3906 #endif
3907 #ifndef BLOCK_BEGIN_LABEL
3908 #define BLOCK_BEGIN_LABEL "LBB"
3909 #endif
3910 #ifndef BLOCK_END_LABEL
3911 #define BLOCK_END_LABEL "LBE"
3912 #endif
3913 #ifndef LINE_CODE_LABEL
3914 #define LINE_CODE_LABEL "LM"
3915 #endif
3918 /* Return the root of the DIE's built for the current compilation unit. */
3919 static dw_die_ref
3920 comp_unit_die (void)
3922 if (!single_comp_unit_die)
3923 single_comp_unit_die = gen_compile_unit_die (NULL);
3924 return single_comp_unit_die;
3927 /* We allow a language front-end to designate a function that is to be
3928 called to "demangle" any name before it is put into a DIE. */
3930 static const char *(*demangle_name_func) (const char *);
3932 void
3933 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3935 demangle_name_func = func;
3938 /* Test if rtl node points to a pseudo register. */
3940 static inline int
3941 is_pseudo_reg (const_rtx rtl)
3943 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3944 || (GET_CODE (rtl) == SUBREG
3945 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3948 /* Return a reference to a type, with its const and volatile qualifiers
3949 removed. */
3951 static inline tree
3952 type_main_variant (tree type)
3954 type = TYPE_MAIN_VARIANT (type);
3956 /* ??? There really should be only one main variant among any group of
3957 variants of a given type (and all of the MAIN_VARIANT values for all
3958 members of the group should point to that one type) but sometimes the C
3959 front-end messes this up for array types, so we work around that bug
3960 here. */
3961 if (TREE_CODE (type) == ARRAY_TYPE)
3962 while (type != TYPE_MAIN_VARIANT (type))
3963 type = TYPE_MAIN_VARIANT (type);
3965 return type;
3968 /* Return nonzero if the given type node represents a tagged type. */
3970 static inline int
3971 is_tagged_type (const_tree type)
3973 enum tree_code code = TREE_CODE (type);
3975 return (code == RECORD_TYPE || code == UNION_TYPE
3976 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3979 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3981 static void
3982 get_ref_die_offset_label (char *label, dw_die_ref ref)
3984 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3987 /* Return die_offset of a DIE reference to a base type. */
3989 static unsigned long int
3990 get_base_type_offset (dw_die_ref ref)
3992 if (ref->die_offset)
3993 return ref->die_offset;
3994 if (comp_unit_die ()->die_abbrev)
3996 calc_base_type_die_sizes ();
3997 gcc_assert (ref->die_offset);
3999 return ref->die_offset;
4002 /* Return die_offset of a DIE reference other than base type. */
4004 static unsigned long int
4005 get_ref_die_offset (dw_die_ref ref)
4007 gcc_assert (ref->die_offset);
4008 return ref->die_offset;
4011 /* Convert a DIE tag into its string name. */
4013 static const char *
4014 dwarf_tag_name (unsigned int tag)
4016 const char *name = get_DW_TAG_name (tag);
4018 if (name != NULL)
4019 return name;
4021 return "DW_TAG_<unknown>";
4024 /* Convert a DWARF attribute code into its string name. */
4026 static const char *
4027 dwarf_attr_name (unsigned int attr)
4029 const char *name;
4031 switch (attr)
4033 #if VMS_DEBUGGING_INFO
4034 case DW_AT_HP_prologue:
4035 return "DW_AT_HP_prologue";
4036 #else
4037 case DW_AT_MIPS_loop_unroll_factor:
4038 return "DW_AT_MIPS_loop_unroll_factor";
4039 #endif
4041 #if VMS_DEBUGGING_INFO
4042 case DW_AT_HP_epilogue:
4043 return "DW_AT_HP_epilogue";
4044 #else
4045 case DW_AT_MIPS_stride:
4046 return "DW_AT_MIPS_stride";
4047 #endif
4050 name = get_DW_AT_name (attr);
4052 if (name != NULL)
4053 return name;
4055 return "DW_AT_<unknown>";
4058 /* Convert a DWARF value form code into its string name. */
4060 static const char *
4061 dwarf_form_name (unsigned int form)
4063 const char *name = get_DW_FORM_name (form);
4065 if (name != NULL)
4066 return name;
4068 return "DW_FORM_<unknown>";
4071 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4072 instance of an inlined instance of a decl which is local to an inline
4073 function, so we have to trace all of the way back through the origin chain
4074 to find out what sort of node actually served as the original seed for the
4075 given block. */
4077 static tree
4078 decl_ultimate_origin (const_tree decl)
4080 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4081 return NULL_TREE;
4083 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4084 we're trying to output the abstract instance of this function. */
4085 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4086 return NULL_TREE;
4088 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4089 most distant ancestor, this should never happen. */
4090 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4092 return DECL_ABSTRACT_ORIGIN (decl);
4095 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4096 of a virtual function may refer to a base class, so we check the 'this'
4097 parameter. */
4099 static tree
4100 decl_class_context (tree decl)
4102 tree context = NULL_TREE;
4104 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4105 context = DECL_CONTEXT (decl);
4106 else
4107 context = TYPE_MAIN_VARIANT
4108 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4110 if (context && !TYPE_P (context))
4111 context = NULL_TREE;
4113 return context;
4116 /* Add an attribute/value pair to a DIE. */
4118 static inline void
4119 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4121 /* Maybe this should be an assert? */
4122 if (die == NULL)
4123 return;
4125 if (flag_checking)
4127 /* Check we do not add duplicate attrs. Can't use get_AT here
4128 because that recurses to the specification/abstract origin DIE. */
4129 dw_attr_node *a;
4130 unsigned ix;
4131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4132 gcc_assert (a->dw_attr != attr->dw_attr);
4135 vec_safe_reserve (die->die_attr, 1);
4136 vec_safe_push (die->die_attr, *attr);
4139 static inline enum dw_val_class
4140 AT_class (dw_attr_node *a)
4142 return a->dw_attr_val.val_class;
4145 /* Return the index for any attribute that will be referenced with a
4146 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4147 are stored in dw_attr_val.v.val_str for reference counting
4148 pruning. */
4150 static inline unsigned int
4151 AT_index (dw_attr_node *a)
4153 if (AT_class (a) == dw_val_class_str)
4154 return a->dw_attr_val.v.val_str->index;
4155 else if (a->dw_attr_val.val_entry != NULL)
4156 return a->dw_attr_val.val_entry->index;
4157 return NOT_INDEXED;
4160 /* Add a flag value attribute to a DIE. */
4162 static inline void
4163 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4165 dw_attr_node attr;
4167 attr.dw_attr = attr_kind;
4168 attr.dw_attr_val.val_class = dw_val_class_flag;
4169 attr.dw_attr_val.val_entry = NULL;
4170 attr.dw_attr_val.v.val_flag = flag;
4171 add_dwarf_attr (die, &attr);
4174 static inline unsigned
4175 AT_flag (dw_attr_node *a)
4177 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4178 return a->dw_attr_val.v.val_flag;
4181 /* Add a signed integer attribute value to a DIE. */
4183 static inline void
4184 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4186 dw_attr_node attr;
4188 attr.dw_attr = attr_kind;
4189 attr.dw_attr_val.val_class = dw_val_class_const;
4190 attr.dw_attr_val.val_entry = NULL;
4191 attr.dw_attr_val.v.val_int = int_val;
4192 add_dwarf_attr (die, &attr);
4195 static inline HOST_WIDE_INT
4196 AT_int (dw_attr_node *a)
4198 gcc_assert (a && (AT_class (a) == dw_val_class_const
4199 || AT_class (a) == dw_val_class_const_implicit));
4200 return a->dw_attr_val.v.val_int;
4203 /* Add an unsigned integer attribute value to a DIE. */
4205 static inline void
4206 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4207 unsigned HOST_WIDE_INT unsigned_val)
4209 dw_attr_node attr;
4211 attr.dw_attr = attr_kind;
4212 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4213 attr.dw_attr_val.val_entry = NULL;
4214 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4215 add_dwarf_attr (die, &attr);
4218 static inline unsigned HOST_WIDE_INT
4219 AT_unsigned (dw_attr_node *a)
4221 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4222 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4223 return a->dw_attr_val.v.val_unsigned;
4226 /* Add an unsigned wide integer attribute value to a DIE. */
4228 static inline void
4229 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4230 const wide_int& w)
4232 dw_attr_node attr;
4234 attr.dw_attr = attr_kind;
4235 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4236 attr.dw_attr_val.val_entry = NULL;
4237 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4238 *attr.dw_attr_val.v.val_wide = w;
4239 add_dwarf_attr (die, &attr);
4242 /* Add an unsigned double integer attribute value to a DIE. */
4244 static inline void
4245 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4246 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4248 dw_attr_node attr;
4250 attr.dw_attr = attr_kind;
4251 attr.dw_attr_val.val_class = dw_val_class_const_double;
4252 attr.dw_attr_val.val_entry = NULL;
4253 attr.dw_attr_val.v.val_double.high = high;
4254 attr.dw_attr_val.v.val_double.low = low;
4255 add_dwarf_attr (die, &attr);
4258 /* Add a floating point attribute value to a DIE and return it. */
4260 static inline void
4261 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4262 unsigned int length, unsigned int elt_size, unsigned char *array)
4264 dw_attr_node attr;
4266 attr.dw_attr = attr_kind;
4267 attr.dw_attr_val.val_class = dw_val_class_vec;
4268 attr.dw_attr_val.val_entry = NULL;
4269 attr.dw_attr_val.v.val_vec.length = length;
4270 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4271 attr.dw_attr_val.v.val_vec.array = array;
4272 add_dwarf_attr (die, &attr);
4275 /* Add an 8-byte data attribute value to a DIE. */
4277 static inline void
4278 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4279 unsigned char data8[8])
4281 dw_attr_node attr;
4283 attr.dw_attr = attr_kind;
4284 attr.dw_attr_val.val_class = dw_val_class_data8;
4285 attr.dw_attr_val.val_entry = NULL;
4286 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4287 add_dwarf_attr (die, &attr);
4290 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4291 dwarf_split_debug_info, address attributes in dies destined for the
4292 final executable have force_direct set to avoid using indexed
4293 references. */
4295 static inline void
4296 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4297 bool force_direct)
4299 dw_attr_node attr;
4300 char * lbl_id;
4302 lbl_id = xstrdup (lbl_low);
4303 attr.dw_attr = DW_AT_low_pc;
4304 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4305 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4306 if (dwarf_split_debug_info && !force_direct)
4307 attr.dw_attr_val.val_entry
4308 = add_addr_table_entry (lbl_id, ate_kind_label);
4309 else
4310 attr.dw_attr_val.val_entry = NULL;
4311 add_dwarf_attr (die, &attr);
4313 attr.dw_attr = DW_AT_high_pc;
4314 if (dwarf_version < 4)
4315 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4316 else
4317 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4318 lbl_id = xstrdup (lbl_high);
4319 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4320 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4321 && dwarf_split_debug_info && !force_direct)
4322 attr.dw_attr_val.val_entry
4323 = add_addr_table_entry (lbl_id, ate_kind_label);
4324 else
4325 attr.dw_attr_val.val_entry = NULL;
4326 add_dwarf_attr (die, &attr);
4329 /* Hash and equality functions for debug_str_hash. */
4331 hashval_t
4332 indirect_string_hasher::hash (indirect_string_node *x)
4334 return htab_hash_string (x->str);
4337 bool
4338 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4340 return strcmp (x1->str, x2) == 0;
4343 /* Add STR to the given string hash table. */
4345 static struct indirect_string_node *
4346 find_AT_string_in_table (const char *str,
4347 hash_table<indirect_string_hasher> *table)
4349 struct indirect_string_node *node;
4351 indirect_string_node **slot
4352 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4353 if (*slot == NULL)
4355 node = ggc_cleared_alloc<indirect_string_node> ();
4356 node->str = ggc_strdup (str);
4357 *slot = node;
4359 else
4360 node = *slot;
4362 node->refcount++;
4363 return node;
4366 /* Add STR to the indirect string hash table. */
4368 static struct indirect_string_node *
4369 find_AT_string (const char *str)
4371 if (! debug_str_hash)
4372 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4374 return find_AT_string_in_table (str, debug_str_hash);
4377 /* Add a string attribute value to a DIE. */
4379 static inline void
4380 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4382 dw_attr_node attr;
4383 struct indirect_string_node *node;
4385 node = find_AT_string (str);
4387 attr.dw_attr = attr_kind;
4388 attr.dw_attr_val.val_class = dw_val_class_str;
4389 attr.dw_attr_val.val_entry = NULL;
4390 attr.dw_attr_val.v.val_str = node;
4391 add_dwarf_attr (die, &attr);
4394 static inline const char *
4395 AT_string (dw_attr_node *a)
4397 gcc_assert (a && AT_class (a) == dw_val_class_str);
4398 return a->dw_attr_val.v.val_str->str;
4401 /* Call this function directly to bypass AT_string_form's logic to put
4402 the string inline in the die. */
4404 static void
4405 set_indirect_string (struct indirect_string_node *node)
4407 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4408 /* Already indirect is a no op. */
4409 if (node->form == DW_FORM_strp
4410 || node->form == DW_FORM_line_strp
4411 || node->form == DW_FORM_GNU_str_index)
4413 gcc_assert (node->label);
4414 return;
4416 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4417 ++dw2_string_counter;
4418 node->label = xstrdup (label);
4420 if (!dwarf_split_debug_info)
4422 node->form = DW_FORM_strp;
4423 node->index = NOT_INDEXED;
4425 else
4427 node->form = DW_FORM_GNU_str_index;
4428 node->index = NO_INDEX_ASSIGNED;
4432 /* A helper function for dwarf2out_finish, called to reset indirect
4433 string decisions done for early LTO dwarf output before fat object
4434 dwarf output. */
4437 reset_indirect_string (indirect_string_node **h, void *)
4439 struct indirect_string_node *node = *h;
4440 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4442 free (node->label);
4443 node->label = NULL;
4444 node->form = (dwarf_form) 0;
4445 node->index = 0;
4447 return 1;
4450 /* Find out whether a string should be output inline in DIE
4451 or out-of-line in .debug_str section. */
4453 static enum dwarf_form
4454 find_string_form (struct indirect_string_node *node)
4456 unsigned int len;
4458 if (node->form)
4459 return node->form;
4461 len = strlen (node->str) + 1;
4463 /* If the string is shorter or equal to the size of the reference, it is
4464 always better to put it inline. */
4465 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4466 return node->form = DW_FORM_string;
4468 /* If we cannot expect the linker to merge strings in .debug_str
4469 section, only put it into .debug_str if it is worth even in this
4470 single module. */
4471 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4472 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4473 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4474 return node->form = DW_FORM_string;
4476 set_indirect_string (node);
4478 return node->form;
4481 /* Find out whether the string referenced from the attribute should be
4482 output inline in DIE or out-of-line in .debug_str section. */
4484 static enum dwarf_form
4485 AT_string_form (dw_attr_node *a)
4487 gcc_assert (a && AT_class (a) == dw_val_class_str);
4488 return find_string_form (a->dw_attr_val.v.val_str);
4491 /* Add a DIE reference attribute value to a DIE. */
4493 static inline void
4494 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4496 dw_attr_node attr;
4497 gcc_checking_assert (targ_die != NULL);
4499 /* With LTO we can end up trying to reference something we didn't create
4500 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4501 if (targ_die == NULL)
4502 return;
4504 attr.dw_attr = attr_kind;
4505 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4506 attr.dw_attr_val.val_entry = NULL;
4507 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4508 attr.dw_attr_val.v.val_die_ref.external = 0;
4509 add_dwarf_attr (die, &attr);
4512 /* Change DIE reference REF to point to NEW_DIE instead. */
4514 static inline void
4515 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4517 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4518 ref->dw_attr_val.v.val_die_ref.die = new_die;
4519 ref->dw_attr_val.v.val_die_ref.external = 0;
4522 /* Add an AT_specification attribute to a DIE, and also make the back
4523 pointer from the specification to the definition. */
4525 static inline void
4526 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4528 add_AT_die_ref (die, DW_AT_specification, targ_die);
4529 gcc_assert (!targ_die->die_definition);
4530 targ_die->die_definition = die;
4533 static inline dw_die_ref
4534 AT_ref (dw_attr_node *a)
4536 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4537 return a->dw_attr_val.v.val_die_ref.die;
4540 static inline int
4541 AT_ref_external (dw_attr_node *a)
4543 if (a && AT_class (a) == dw_val_class_die_ref)
4544 return a->dw_attr_val.v.val_die_ref.external;
4546 return 0;
4549 static inline void
4550 set_AT_ref_external (dw_attr_node *a, int i)
4552 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4553 a->dw_attr_val.v.val_die_ref.external = i;
4556 /* Add an FDE reference attribute value to a DIE. */
4558 static inline void
4559 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4561 dw_attr_node attr;
4563 attr.dw_attr = attr_kind;
4564 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4565 attr.dw_attr_val.val_entry = NULL;
4566 attr.dw_attr_val.v.val_fde_index = targ_fde;
4567 add_dwarf_attr (die, &attr);
4570 /* Add a location description attribute value to a DIE. */
4572 static inline void
4573 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4575 dw_attr_node attr;
4577 attr.dw_attr = attr_kind;
4578 attr.dw_attr_val.val_class = dw_val_class_loc;
4579 attr.dw_attr_val.val_entry = NULL;
4580 attr.dw_attr_val.v.val_loc = loc;
4581 add_dwarf_attr (die, &attr);
4584 static inline dw_loc_descr_ref
4585 AT_loc (dw_attr_node *a)
4587 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4588 return a->dw_attr_val.v.val_loc;
4591 static inline void
4592 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4594 dw_attr_node attr;
4596 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4597 return;
4599 attr.dw_attr = attr_kind;
4600 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4601 attr.dw_attr_val.val_entry = NULL;
4602 attr.dw_attr_val.v.val_loc_list = loc_list;
4603 add_dwarf_attr (die, &attr);
4604 have_location_lists = true;
4607 static inline dw_loc_list_ref
4608 AT_loc_list (dw_attr_node *a)
4610 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4611 return a->dw_attr_val.v.val_loc_list;
4614 static inline dw_loc_list_ref *
4615 AT_loc_list_ptr (dw_attr_node *a)
4617 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4618 return &a->dw_attr_val.v.val_loc_list;
4621 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4623 static hashval_t hash (addr_table_entry *);
4624 static bool equal (addr_table_entry *, addr_table_entry *);
4627 /* Table of entries into the .debug_addr section. */
4629 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4631 /* Hash an address_table_entry. */
4633 hashval_t
4634 addr_hasher::hash (addr_table_entry *a)
4636 inchash::hash hstate;
4637 switch (a->kind)
4639 case ate_kind_rtx:
4640 hstate.add_int (0);
4641 break;
4642 case ate_kind_rtx_dtprel:
4643 hstate.add_int (1);
4644 break;
4645 case ate_kind_label:
4646 return htab_hash_string (a->addr.label);
4647 default:
4648 gcc_unreachable ();
4650 inchash::add_rtx (a->addr.rtl, hstate);
4651 return hstate.end ();
4654 /* Determine equality for two address_table_entries. */
4656 bool
4657 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4659 if (a1->kind != a2->kind)
4660 return 0;
4661 switch (a1->kind)
4663 case ate_kind_rtx:
4664 case ate_kind_rtx_dtprel:
4665 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4666 case ate_kind_label:
4667 return strcmp (a1->addr.label, a2->addr.label) == 0;
4668 default:
4669 gcc_unreachable ();
4673 /* Initialize an addr_table_entry. */
4675 void
4676 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4678 e->kind = kind;
4679 switch (kind)
4681 case ate_kind_rtx:
4682 case ate_kind_rtx_dtprel:
4683 e->addr.rtl = (rtx) addr;
4684 break;
4685 case ate_kind_label:
4686 e->addr.label = (char *) addr;
4687 break;
4689 e->refcount = 0;
4690 e->index = NO_INDEX_ASSIGNED;
4693 /* Add attr to the address table entry to the table. Defer setting an
4694 index until output time. */
4696 static addr_table_entry *
4697 add_addr_table_entry (void *addr, enum ate_kind kind)
4699 addr_table_entry *node;
4700 addr_table_entry finder;
4702 gcc_assert (dwarf_split_debug_info);
4703 if (! addr_index_table)
4704 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4705 init_addr_table_entry (&finder, kind, addr);
4706 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4708 if (*slot == HTAB_EMPTY_ENTRY)
4710 node = ggc_cleared_alloc<addr_table_entry> ();
4711 init_addr_table_entry (node, kind, addr);
4712 *slot = node;
4714 else
4715 node = *slot;
4717 node->refcount++;
4718 return node;
4721 /* Remove an entry from the addr table by decrementing its refcount.
4722 Strictly, decrementing the refcount would be enough, but the
4723 assertion that the entry is actually in the table has found
4724 bugs. */
4726 static void
4727 remove_addr_table_entry (addr_table_entry *entry)
4729 gcc_assert (dwarf_split_debug_info && addr_index_table);
4730 /* After an index is assigned, the table is frozen. */
4731 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4732 entry->refcount--;
4735 /* Given a location list, remove all addresses it refers to from the
4736 address_table. */
4738 static void
4739 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4741 for (; descr; descr = descr->dw_loc_next)
4742 if (descr->dw_loc_oprnd1.val_entry != NULL)
4744 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4745 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4749 /* A helper function for dwarf2out_finish called through
4750 htab_traverse. Assign an addr_table_entry its index. All entries
4751 must be collected into the table when this function is called,
4752 because the indexing code relies on htab_traverse to traverse nodes
4753 in the same order for each run. */
4756 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4758 addr_table_entry *node = *h;
4760 /* Don't index unreferenced nodes. */
4761 if (node->refcount == 0)
4762 return 1;
4764 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4765 node->index = *index;
4766 *index += 1;
4768 return 1;
4771 /* Add an address constant attribute value to a DIE. When using
4772 dwarf_split_debug_info, address attributes in dies destined for the
4773 final executable should be direct references--setting the parameter
4774 force_direct ensures this behavior. */
4776 static inline void
4777 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4778 bool force_direct)
4780 dw_attr_node attr;
4782 attr.dw_attr = attr_kind;
4783 attr.dw_attr_val.val_class = dw_val_class_addr;
4784 attr.dw_attr_val.v.val_addr = addr;
4785 if (dwarf_split_debug_info && !force_direct)
4786 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4787 else
4788 attr.dw_attr_val.val_entry = NULL;
4789 add_dwarf_attr (die, &attr);
4792 /* Get the RTX from to an address DIE attribute. */
4794 static inline rtx
4795 AT_addr (dw_attr_node *a)
4797 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4798 return a->dw_attr_val.v.val_addr;
4801 /* Add a file attribute value to a DIE. */
4803 static inline void
4804 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4805 struct dwarf_file_data *fd)
4807 dw_attr_node attr;
4809 attr.dw_attr = attr_kind;
4810 attr.dw_attr_val.val_class = dw_val_class_file;
4811 attr.dw_attr_val.val_entry = NULL;
4812 attr.dw_attr_val.v.val_file = fd;
4813 add_dwarf_attr (die, &attr);
4816 /* Get the dwarf_file_data from a file DIE attribute. */
4818 static inline struct dwarf_file_data *
4819 AT_file (dw_attr_node *a)
4821 gcc_assert (a && (AT_class (a) == dw_val_class_file
4822 || AT_class (a) == dw_val_class_file_implicit));
4823 return a->dw_attr_val.v.val_file;
4826 /* Add a vms delta attribute value to a DIE. */
4828 static inline void
4829 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4830 const char *lbl1, const char *lbl2)
4832 dw_attr_node attr;
4834 attr.dw_attr = attr_kind;
4835 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4836 attr.dw_attr_val.val_entry = NULL;
4837 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4838 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4839 add_dwarf_attr (die, &attr);
4842 /* Add a label identifier attribute value to a DIE. */
4844 static inline void
4845 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4846 const char *lbl_id)
4848 dw_attr_node attr;
4850 attr.dw_attr = attr_kind;
4851 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4852 attr.dw_attr_val.val_entry = NULL;
4853 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4854 if (dwarf_split_debug_info)
4855 attr.dw_attr_val.val_entry
4856 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4857 ate_kind_label);
4858 add_dwarf_attr (die, &attr);
4861 /* Add a section offset attribute value to a DIE, an offset into the
4862 debug_line section. */
4864 static inline void
4865 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4866 const char *label)
4868 dw_attr_node attr;
4870 attr.dw_attr = attr_kind;
4871 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4872 attr.dw_attr_val.val_entry = NULL;
4873 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4874 add_dwarf_attr (die, &attr);
4877 /* Add a section offset attribute value to a DIE, an offset into the
4878 debug_loclists section. */
4880 static inline void
4881 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4882 const char *label)
4884 dw_attr_node attr;
4886 attr.dw_attr = attr_kind;
4887 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4888 attr.dw_attr_val.val_entry = NULL;
4889 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4890 add_dwarf_attr (die, &attr);
4893 /* Add a section offset attribute value to a DIE, an offset into the
4894 debug_macinfo section. */
4896 static inline void
4897 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4898 const char *label)
4900 dw_attr_node attr;
4902 attr.dw_attr = attr_kind;
4903 attr.dw_attr_val.val_class = dw_val_class_macptr;
4904 attr.dw_attr_val.val_entry = NULL;
4905 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4906 add_dwarf_attr (die, &attr);
4909 /* Add an offset attribute value to a DIE. */
4911 static inline void
4912 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4913 unsigned HOST_WIDE_INT offset)
4915 dw_attr_node attr;
4917 attr.dw_attr = attr_kind;
4918 attr.dw_attr_val.val_class = dw_val_class_offset;
4919 attr.dw_attr_val.val_entry = NULL;
4920 attr.dw_attr_val.v.val_offset = offset;
4921 add_dwarf_attr (die, &attr);
4924 /* Add a range_list attribute value to a DIE. When using
4925 dwarf_split_debug_info, address attributes in dies destined for the
4926 final executable should be direct references--setting the parameter
4927 force_direct ensures this behavior. */
4929 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4930 #define RELOCATED_OFFSET (NULL)
4932 static void
4933 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4934 long unsigned int offset, bool force_direct)
4936 dw_attr_node attr;
4938 attr.dw_attr = attr_kind;
4939 attr.dw_attr_val.val_class = dw_val_class_range_list;
4940 /* For the range_list attribute, use val_entry to store whether the
4941 offset should follow split-debug-info or normal semantics. This
4942 value is read in output_range_list_offset. */
4943 if (dwarf_split_debug_info && !force_direct)
4944 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4945 else
4946 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4947 attr.dw_attr_val.v.val_offset = offset;
4948 add_dwarf_attr (die, &attr);
4951 /* Return the start label of a delta attribute. */
4953 static inline const char *
4954 AT_vms_delta1 (dw_attr_node *a)
4956 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4957 return a->dw_attr_val.v.val_vms_delta.lbl1;
4960 /* Return the end label of a delta attribute. */
4962 static inline const char *
4963 AT_vms_delta2 (dw_attr_node *a)
4965 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4966 return a->dw_attr_val.v.val_vms_delta.lbl2;
4969 static inline const char *
4970 AT_lbl (dw_attr_node *a)
4972 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4973 || AT_class (a) == dw_val_class_lineptr
4974 || AT_class (a) == dw_val_class_macptr
4975 || AT_class (a) == dw_val_class_loclistsptr
4976 || AT_class (a) == dw_val_class_high_pc));
4977 return a->dw_attr_val.v.val_lbl_id;
4980 /* Get the attribute of type attr_kind. */
4982 static dw_attr_node *
4983 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4985 dw_attr_node *a;
4986 unsigned ix;
4987 dw_die_ref spec = NULL;
4989 if (! die)
4990 return NULL;
4992 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4993 if (a->dw_attr == attr_kind)
4994 return a;
4995 else if (a->dw_attr == DW_AT_specification
4996 || a->dw_attr == DW_AT_abstract_origin)
4997 spec = AT_ref (a);
4999 if (spec)
5000 return get_AT (spec, attr_kind);
5002 return NULL;
5005 /* Returns the parent of the declaration of DIE. */
5007 static dw_die_ref
5008 get_die_parent (dw_die_ref die)
5010 dw_die_ref t;
5012 if (!die)
5013 return NULL;
5015 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5016 || (t = get_AT_ref (die, DW_AT_specification)))
5017 die = t;
5019 return die->die_parent;
5022 /* Return the "low pc" attribute value, typically associated with a subprogram
5023 DIE. Return null if the "low pc" attribute is either not present, or if it
5024 cannot be represented as an assembler label identifier. */
5026 static inline const char *
5027 get_AT_low_pc (dw_die_ref die)
5029 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5031 return a ? AT_lbl (a) : NULL;
5034 /* Return the "high pc" attribute value, typically associated with a subprogram
5035 DIE. Return null if the "high pc" attribute is either not present, or if it
5036 cannot be represented as an assembler label identifier. */
5038 static inline const char *
5039 get_AT_hi_pc (dw_die_ref die)
5041 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5043 return a ? AT_lbl (a) : NULL;
5046 /* Return the value of the string attribute designated by ATTR_KIND, or
5047 NULL if it is not present. */
5049 static inline const char *
5050 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5052 dw_attr_node *a = get_AT (die, attr_kind);
5054 return a ? AT_string (a) : NULL;
5057 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5058 if it is not present. */
5060 static inline int
5061 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5063 dw_attr_node *a = get_AT (die, attr_kind);
5065 return a ? AT_flag (a) : 0;
5068 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5069 if it is not present. */
5071 static inline unsigned
5072 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5074 dw_attr_node *a = get_AT (die, attr_kind);
5076 return a ? AT_unsigned (a) : 0;
5079 static inline dw_die_ref
5080 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5082 dw_attr_node *a = get_AT (die, attr_kind);
5084 return a ? AT_ref (a) : NULL;
5087 static inline struct dwarf_file_data *
5088 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5090 dw_attr_node *a = get_AT (die, attr_kind);
5092 return a ? AT_file (a) : NULL;
5095 /* Returns the ultimate TRANSLATION_UNIT_DECL context of DECL or NULL. */
5097 static const_tree
5098 get_ultimate_context (const_tree decl)
5100 while (decl && TREE_CODE (decl) != TRANSLATION_UNIT_DECL)
5102 if (TREE_CODE (decl) == BLOCK)
5103 decl = BLOCK_SUPERCONTEXT (decl);
5104 else
5105 decl = get_containing_scope (decl);
5107 return decl;
5110 /* Return TRUE if the language is C++. */
5112 static inline bool
5113 is_cxx (void)
5115 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5117 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5118 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5121 /* Return TRUE if DECL was created by the C++ frontend. */
5123 static bool
5124 is_cxx (const_tree decl)
5126 if (in_lto_p)
5128 const_tree context = get_ultimate_context (decl);
5129 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5130 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5132 return is_cxx ();
5135 /* Return TRUE if the language is Fortran. */
5137 static inline bool
5138 is_fortran (void)
5140 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5142 return (lang == DW_LANG_Fortran77
5143 || lang == DW_LANG_Fortran90
5144 || lang == DW_LANG_Fortran95
5145 || lang == DW_LANG_Fortran03
5146 || lang == DW_LANG_Fortran08);
5149 static inline bool
5150 is_fortran (const_tree decl)
5152 if (in_lto_p)
5154 const_tree context = get_ultimate_context (decl);
5155 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5156 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5157 "GNU Fortran", 11) == 0
5158 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5159 "GNU F77") == 0);
5161 return is_fortran ();
5164 /* Return TRUE if the language is Ada. */
5166 static inline bool
5167 is_ada (void)
5169 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5171 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5174 /* Remove the specified attribute if present. Return TRUE if removal
5175 was successful. */
5177 static bool
5178 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5180 dw_attr_node *a;
5181 unsigned ix;
5183 if (! die)
5184 return false;
5186 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5187 if (a->dw_attr == attr_kind)
5189 if (AT_class (a) == dw_val_class_str)
5190 if (a->dw_attr_val.v.val_str->refcount)
5191 a->dw_attr_val.v.val_str->refcount--;
5193 /* vec::ordered_remove should help reduce the number of abbrevs
5194 that are needed. */
5195 die->die_attr->ordered_remove (ix);
5196 return true;
5198 return false;
5201 /* Remove CHILD from its parent. PREV must have the property that
5202 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5204 static void
5205 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5207 gcc_assert (child->die_parent == prev->die_parent);
5208 gcc_assert (prev->die_sib == child);
5209 if (prev == child)
5211 gcc_assert (child->die_parent->die_child == child);
5212 prev = NULL;
5214 else
5215 prev->die_sib = child->die_sib;
5216 if (child->die_parent->die_child == child)
5217 child->die_parent->die_child = prev;
5218 child->die_sib = NULL;
5221 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5222 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5224 static void
5225 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5227 dw_die_ref parent = old_child->die_parent;
5229 gcc_assert (parent == prev->die_parent);
5230 gcc_assert (prev->die_sib == old_child);
5232 new_child->die_parent = parent;
5233 if (prev == old_child)
5235 gcc_assert (parent->die_child == old_child);
5236 new_child->die_sib = new_child;
5238 else
5240 prev->die_sib = new_child;
5241 new_child->die_sib = old_child->die_sib;
5243 if (old_child->die_parent->die_child == old_child)
5244 old_child->die_parent->die_child = new_child;
5245 old_child->die_sib = NULL;
5248 /* Move all children from OLD_PARENT to NEW_PARENT. */
5250 static void
5251 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5253 dw_die_ref c;
5254 new_parent->die_child = old_parent->die_child;
5255 old_parent->die_child = NULL;
5256 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5259 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5260 matches TAG. */
5262 static void
5263 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5265 dw_die_ref c;
5267 c = die->die_child;
5268 if (c) do {
5269 dw_die_ref prev = c;
5270 c = c->die_sib;
5271 while (c->die_tag == tag)
5273 remove_child_with_prev (c, prev);
5274 c->die_parent = NULL;
5275 /* Might have removed every child. */
5276 if (die->die_child == NULL)
5277 return;
5278 c = prev->die_sib;
5280 } while (c != die->die_child);
5283 /* Add a CHILD_DIE as the last child of DIE. */
5285 static void
5286 add_child_die (dw_die_ref die, dw_die_ref child_die)
5288 /* FIXME this should probably be an assert. */
5289 if (! die || ! child_die)
5290 return;
5291 gcc_assert (die != child_die);
5293 child_die->die_parent = die;
5294 if (die->die_child)
5296 child_die->die_sib = die->die_child->die_sib;
5297 die->die_child->die_sib = child_die;
5299 else
5300 child_die->die_sib = child_die;
5301 die->die_child = child_die;
5304 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5306 static void
5307 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5308 dw_die_ref after_die)
5310 gcc_assert (die
5311 && child_die
5312 && after_die
5313 && die->die_child
5314 && die != child_die);
5316 child_die->die_parent = die;
5317 child_die->die_sib = after_die->die_sib;
5318 after_die->die_sib = child_die;
5319 if (die->die_child == after_die)
5320 die->die_child = child_die;
5323 /* Unassociate CHILD from its parent, and make its parent be
5324 NEW_PARENT. */
5326 static void
5327 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5329 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5330 if (p->die_sib == child)
5332 remove_child_with_prev (child, p);
5333 break;
5335 add_child_die (new_parent, child);
5338 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5339 is the specification, to the end of PARENT's list of children.
5340 This is done by removing and re-adding it. */
5342 static void
5343 splice_child_die (dw_die_ref parent, dw_die_ref child)
5345 /* We want the declaration DIE from inside the class, not the
5346 specification DIE at toplevel. */
5347 if (child->die_parent != parent)
5349 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5351 if (tmp)
5352 child = tmp;
5355 gcc_assert (child->die_parent == parent
5356 || (child->die_parent
5357 == get_AT_ref (parent, DW_AT_specification)));
5359 reparent_child (child, parent);
5362 /* Create and return a new die with a parent of PARENT_DIE. If
5363 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5364 associated tree T must be supplied to determine parenthood
5365 later. */
5367 static inline dw_die_ref
5368 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5370 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5372 die->die_tag = tag_value;
5374 if (parent_die != NULL)
5375 add_child_die (parent_die, die);
5376 else
5378 limbo_die_node *limbo_node;
5380 /* No DIEs created after early dwarf should end up in limbo,
5381 because the limbo list should not persist past LTO
5382 streaming. */
5383 if (tag_value != DW_TAG_compile_unit
5384 /* These are allowed because they're generated while
5385 breaking out COMDAT units late. */
5386 && tag_value != DW_TAG_type_unit
5387 && tag_value != DW_TAG_skeleton_unit
5388 && !early_dwarf
5389 /* Allow nested functions to live in limbo because they will
5390 only temporarily live there, as decls_for_scope will fix
5391 them up. */
5392 && (TREE_CODE (t) != FUNCTION_DECL
5393 || !decl_function_context (t))
5394 /* Same as nested functions above but for types. Types that
5395 are local to a function will be fixed in
5396 decls_for_scope. */
5397 && (!RECORD_OR_UNION_TYPE_P (t)
5398 || !TYPE_CONTEXT (t)
5399 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5400 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5401 especially in the ltrans stage, but once we implement LTO
5402 dwarf streaming, we should remove this exception. */
5403 && !in_lto_p)
5405 fprintf (stderr, "symbol ended up in limbo too late:");
5406 debug_generic_stmt (t);
5407 gcc_unreachable ();
5410 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5411 limbo_node->die = die;
5412 limbo_node->created_for = t;
5413 limbo_node->next = limbo_die_list;
5414 limbo_die_list = limbo_node;
5417 return die;
5420 /* Return the DIE associated with the given type specifier. */
5422 static inline dw_die_ref
5423 lookup_type_die (tree type)
5425 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5426 if (die && die->removed)
5428 TYPE_SYMTAB_DIE (type) = NULL;
5429 return NULL;
5431 return die;
5434 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5435 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5436 anonymous type instead the one of the naming typedef. */
5438 static inline dw_die_ref
5439 strip_naming_typedef (tree type, dw_die_ref type_die)
5441 if (type
5442 && TREE_CODE (type) == RECORD_TYPE
5443 && type_die
5444 && type_die->die_tag == DW_TAG_typedef
5445 && is_naming_typedef_decl (TYPE_NAME (type)))
5446 type_die = get_AT_ref (type_die, DW_AT_type);
5447 return type_die;
5450 /* Like lookup_type_die, but if type is an anonymous type named by a
5451 typedef[1], return the DIE of the anonymous type instead the one of
5452 the naming typedef. This is because in gen_typedef_die, we did
5453 equate the anonymous struct named by the typedef with the DIE of
5454 the naming typedef. So by default, lookup_type_die on an anonymous
5455 struct yields the DIE of the naming typedef.
5457 [1]: Read the comment of is_naming_typedef_decl to learn about what
5458 a naming typedef is. */
5460 static inline dw_die_ref
5461 lookup_type_die_strip_naming_typedef (tree type)
5463 dw_die_ref die = lookup_type_die (type);
5464 return strip_naming_typedef (type, die);
5467 /* Equate a DIE to a given type specifier. */
5469 static inline void
5470 equate_type_number_to_die (tree type, dw_die_ref type_die)
5472 TYPE_SYMTAB_DIE (type) = type_die;
5475 /* Returns a hash value for X (which really is a die_struct). */
5477 inline hashval_t
5478 decl_die_hasher::hash (die_node *x)
5480 return (hashval_t) x->decl_id;
5483 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5485 inline bool
5486 decl_die_hasher::equal (die_node *x, tree y)
5488 return (x->decl_id == DECL_UID (y));
5491 /* Return the DIE associated with a given declaration. */
5493 static inline dw_die_ref
5494 lookup_decl_die (tree decl)
5496 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5497 NO_INSERT);
5498 if (!die)
5499 return NULL;
5500 if ((*die)->removed)
5502 decl_die_table->clear_slot (die);
5503 return NULL;
5505 return *die;
5509 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5510 style reference. Return true if we found one refering to a DIE for
5511 DECL, otherwise return false. */
5513 static bool
5514 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5515 unsigned HOST_WIDE_INT *off)
5517 dw_die_ref die;
5519 if (flag_wpa && !decl_die_table)
5520 return false;
5522 if (TREE_CODE (decl) == BLOCK)
5523 die = BLOCK_DIE (decl);
5524 else
5525 die = lookup_decl_die (decl);
5526 if (!die)
5527 return false;
5529 /* During WPA stage we currently use DIEs to store the
5530 decl <-> label + offset map. That's quite inefficient but it
5531 works for now. */
5532 if (flag_wpa)
5534 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5535 if (!ref)
5537 gcc_assert (die == comp_unit_die ());
5538 return false;
5540 *off = ref->die_offset;
5541 *sym = ref->die_id.die_symbol;
5542 return true;
5545 /* Similar to get_ref_die_offset_label, but using the "correct"
5546 label. */
5547 *off = die->die_offset;
5548 while (die->die_parent)
5549 die = die->die_parent;
5550 /* For the containing CU DIE we compute a die_symbol in
5551 compute_comp_unit_symbol. */
5552 gcc_assert (die->die_tag == DW_TAG_compile_unit
5553 && die->die_id.die_symbol != NULL);
5554 *sym = die->die_id.die_symbol;
5555 return true;
5558 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5560 static void
5561 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5562 const char *symbol, HOST_WIDE_INT offset)
5564 /* Create a fake DIE that contains the reference. Don't use
5565 new_die because we don't want to end up in the limbo list. */
5566 dw_die_ref ref = ggc_cleared_alloc<die_node> ();
5567 ref->die_tag = die->die_tag;
5568 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5569 ref->die_offset = offset;
5570 ref->with_offset = 1;
5571 add_AT_die_ref (die, attr_kind, ref);
5574 /* Create a DIE for DECL if required and add a reference to a DIE
5575 at SYMBOL + OFFSET which contains attributes dumped early. */
5577 static void
5578 dwarf2out_register_external_die (tree decl, const char *sym,
5579 unsigned HOST_WIDE_INT off)
5581 if (debug_info_level == DINFO_LEVEL_NONE)
5582 return;
5584 if (flag_wpa && !decl_die_table)
5585 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5587 dw_die_ref die
5588 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5589 gcc_assert (!die);
5591 tree ctx;
5592 dw_die_ref parent = NULL;
5593 /* Need to lookup a DIE for the decls context - the containing
5594 function or translation unit. */
5595 if (TREE_CODE (decl) == BLOCK)
5597 ctx = BLOCK_SUPERCONTEXT (decl);
5598 /* ??? We do not output DIEs for all scopes thus skip as
5599 many DIEs as needed. */
5600 while (TREE_CODE (ctx) == BLOCK
5601 && !BLOCK_DIE (ctx))
5602 ctx = BLOCK_SUPERCONTEXT (ctx);
5604 else
5605 ctx = DECL_CONTEXT (decl);
5606 while (ctx && TYPE_P (ctx))
5607 ctx = TYPE_CONTEXT (ctx);
5608 if (ctx)
5610 if (TREE_CODE (ctx) == BLOCK)
5611 parent = BLOCK_DIE (ctx);
5612 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5613 /* Keep the 1:1 association during WPA. */
5614 && !flag_wpa)
5615 /* Otherwise all late annotations go to the main CU which
5616 imports the original CUs. */
5617 parent = comp_unit_die ();
5618 else if (TREE_CODE (ctx) == FUNCTION_DECL
5619 && TREE_CODE (decl) != PARM_DECL
5620 && TREE_CODE (decl) != BLOCK)
5621 /* Leave function local entities parent determination to when
5622 we process scope vars. */
5624 else
5625 parent = lookup_decl_die (ctx);
5627 else
5628 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5629 Handle this case gracefully by globalizing stuff. */
5630 parent = comp_unit_die ();
5631 /* Create a DIE "stub". */
5632 switch (TREE_CODE (decl))
5634 case TRANSLATION_UNIT_DECL:
5635 if (! flag_wpa)
5637 die = comp_unit_die ();
5638 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5639 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5640 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5641 to create a DIE for the original CUs. */
5642 return;
5644 /* Keep the 1:1 association during WPA. */
5645 die = new_die (DW_TAG_compile_unit, NULL, decl);
5646 break;
5647 case NAMESPACE_DECL:
5648 if (is_fortran (decl))
5649 die = new_die (DW_TAG_module, parent, decl);
5650 else
5651 die = new_die (DW_TAG_namespace, parent, decl);
5652 break;
5653 case FUNCTION_DECL:
5654 die = new_die (DW_TAG_subprogram, parent, decl);
5655 break;
5656 case VAR_DECL:
5657 die = new_die (DW_TAG_variable, parent, decl);
5658 break;
5659 case RESULT_DECL:
5660 die = new_die (DW_TAG_variable, parent, decl);
5661 break;
5662 case PARM_DECL:
5663 die = new_die (DW_TAG_formal_parameter, parent, decl);
5664 break;
5665 case CONST_DECL:
5666 die = new_die (DW_TAG_constant, parent, decl);
5667 break;
5668 case LABEL_DECL:
5669 die = new_die (DW_TAG_label, parent, decl);
5670 break;
5671 case BLOCK:
5672 die = new_die (DW_TAG_lexical_block, parent, decl);
5673 break;
5674 default:
5675 gcc_unreachable ();
5677 if (TREE_CODE (decl) == BLOCK)
5678 BLOCK_DIE (decl) = die;
5679 else
5680 equate_decl_number_to_die (decl, die);
5682 /* Add a reference to the DIE providing early debug at $sym + off. */
5683 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5686 /* Returns a hash value for X (which really is a var_loc_list). */
5688 inline hashval_t
5689 decl_loc_hasher::hash (var_loc_list *x)
5691 return (hashval_t) x->decl_id;
5694 /* Return nonzero if decl_id of var_loc_list X is the same as
5695 UID of decl *Y. */
5697 inline bool
5698 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5700 return (x->decl_id == DECL_UID (y));
5703 /* Return the var_loc list associated with a given declaration. */
5705 static inline var_loc_list *
5706 lookup_decl_loc (const_tree decl)
5708 if (!decl_loc_table)
5709 return NULL;
5710 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5713 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5715 inline hashval_t
5716 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5718 return (hashval_t) x->decl_id;
5721 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5722 UID of decl *Y. */
5724 inline bool
5725 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5727 return (x->decl_id == DECL_UID (y));
5730 /* Equate a DIE to a particular declaration. */
5732 static void
5733 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5735 unsigned int decl_id = DECL_UID (decl);
5737 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5738 decl_die->decl_id = decl_id;
5741 /* Return how many bits covers PIECE EXPR_LIST. */
5743 static HOST_WIDE_INT
5744 decl_piece_bitsize (rtx piece)
5746 int ret = (int) GET_MODE (piece);
5747 if (ret)
5748 return ret;
5749 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5750 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5751 return INTVAL (XEXP (XEXP (piece, 0), 0));
5754 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5756 static rtx *
5757 decl_piece_varloc_ptr (rtx piece)
5759 if ((int) GET_MODE (piece))
5760 return &XEXP (piece, 0);
5761 else
5762 return &XEXP (XEXP (piece, 0), 1);
5765 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5766 Next is the chain of following piece nodes. */
5768 static rtx_expr_list *
5769 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5771 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5772 return alloc_EXPR_LIST (bitsize, loc_note, next);
5773 else
5774 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5775 GEN_INT (bitsize),
5776 loc_note), next);
5779 /* Return rtx that should be stored into loc field for
5780 LOC_NOTE and BITPOS/BITSIZE. */
5782 static rtx
5783 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5784 HOST_WIDE_INT bitsize)
5786 if (bitsize != -1)
5788 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5789 if (bitpos != 0)
5790 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5792 return loc_note;
5795 /* This function either modifies location piece list *DEST in
5796 place (if SRC and INNER is NULL), or copies location piece list
5797 *SRC to *DEST while modifying it. Location BITPOS is modified
5798 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5799 not copied and if needed some padding around it is added.
5800 When modifying in place, DEST should point to EXPR_LIST where
5801 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5802 to the start of the whole list and INNER points to the EXPR_LIST
5803 where earlier pieces cover PIECE_BITPOS bits. */
5805 static void
5806 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5807 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5808 HOST_WIDE_INT bitsize, rtx loc_note)
5810 HOST_WIDE_INT diff;
5811 bool copy = inner != NULL;
5813 if (copy)
5815 /* First copy all nodes preceding the current bitpos. */
5816 while (src != inner)
5818 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5819 decl_piece_bitsize (*src), NULL_RTX);
5820 dest = &XEXP (*dest, 1);
5821 src = &XEXP (*src, 1);
5824 /* Add padding if needed. */
5825 if (bitpos != piece_bitpos)
5827 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5828 copy ? NULL_RTX : *dest);
5829 dest = &XEXP (*dest, 1);
5831 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5833 gcc_assert (!copy);
5834 /* A piece with correct bitpos and bitsize already exist,
5835 just update the location for it and return. */
5836 *decl_piece_varloc_ptr (*dest) = loc_note;
5837 return;
5839 /* Add the piece that changed. */
5840 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5841 dest = &XEXP (*dest, 1);
5842 /* Skip over pieces that overlap it. */
5843 diff = bitpos - piece_bitpos + bitsize;
5844 if (!copy)
5845 src = dest;
5846 while (diff > 0 && *src)
5848 rtx piece = *src;
5849 diff -= decl_piece_bitsize (piece);
5850 if (copy)
5851 src = &XEXP (piece, 1);
5852 else
5854 *src = XEXP (piece, 1);
5855 free_EXPR_LIST_node (piece);
5858 /* Add padding if needed. */
5859 if (diff < 0 && *src)
5861 if (!copy)
5862 dest = src;
5863 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5864 dest = &XEXP (*dest, 1);
5866 if (!copy)
5867 return;
5868 /* Finally copy all nodes following it. */
5869 while (*src)
5871 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5872 decl_piece_bitsize (*src), NULL_RTX);
5873 dest = &XEXP (*dest, 1);
5874 src = &XEXP (*src, 1);
5878 /* Add a variable location node to the linked list for DECL. */
5880 static struct var_loc_node *
5881 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5883 unsigned int decl_id;
5884 var_loc_list *temp;
5885 struct var_loc_node *loc = NULL;
5886 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5888 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5890 tree realdecl = DECL_DEBUG_EXPR (decl);
5891 if (handled_component_p (realdecl)
5892 || (TREE_CODE (realdecl) == MEM_REF
5893 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5895 HOST_WIDE_INT maxsize;
5896 bool reverse;
5897 tree innerdecl
5898 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5899 &reverse);
5900 if (!DECL_P (innerdecl)
5901 || DECL_IGNORED_P (innerdecl)
5902 || TREE_STATIC (innerdecl)
5903 || bitsize <= 0
5904 || bitpos + bitsize > 256
5905 || bitsize != maxsize)
5906 return NULL;
5907 decl = innerdecl;
5911 decl_id = DECL_UID (decl);
5912 var_loc_list **slot
5913 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5914 if (*slot == NULL)
5916 temp = ggc_cleared_alloc<var_loc_list> ();
5917 temp->decl_id = decl_id;
5918 *slot = temp;
5920 else
5921 temp = *slot;
5923 /* For PARM_DECLs try to keep around the original incoming value,
5924 even if that means we'll emit a zero-range .debug_loc entry. */
5925 if (temp->last
5926 && temp->first == temp->last
5927 && TREE_CODE (decl) == PARM_DECL
5928 && NOTE_P (temp->first->loc)
5929 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5930 && DECL_INCOMING_RTL (decl)
5931 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5932 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5933 == GET_CODE (DECL_INCOMING_RTL (decl))
5934 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5935 && (bitsize != -1
5936 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5937 NOTE_VAR_LOCATION_LOC (loc_note))
5938 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5939 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5941 loc = ggc_cleared_alloc<var_loc_node> ();
5942 temp->first->next = loc;
5943 temp->last = loc;
5944 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5946 else if (temp->last)
5948 struct var_loc_node *last = temp->last, *unused = NULL;
5949 rtx *piece_loc = NULL, last_loc_note;
5950 HOST_WIDE_INT piece_bitpos = 0;
5951 if (last->next)
5953 last = last->next;
5954 gcc_assert (last->next == NULL);
5956 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5958 piece_loc = &last->loc;
5961 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5962 if (piece_bitpos + cur_bitsize > bitpos)
5963 break;
5964 piece_bitpos += cur_bitsize;
5965 piece_loc = &XEXP (*piece_loc, 1);
5967 while (*piece_loc);
5969 /* TEMP->LAST here is either pointer to the last but one or
5970 last element in the chained list, LAST is pointer to the
5971 last element. */
5972 if (label && strcmp (last->label, label) == 0)
5974 /* For SRA optimized variables if there weren't any real
5975 insns since last note, just modify the last node. */
5976 if (piece_loc != NULL)
5978 adjust_piece_list (piece_loc, NULL, NULL,
5979 bitpos, piece_bitpos, bitsize, loc_note);
5980 return NULL;
5982 /* If the last note doesn't cover any instructions, remove it. */
5983 if (temp->last != last)
5985 temp->last->next = NULL;
5986 unused = last;
5987 last = temp->last;
5988 gcc_assert (strcmp (last->label, label) != 0);
5990 else
5992 gcc_assert (temp->first == temp->last
5993 || (temp->first->next == temp->last
5994 && TREE_CODE (decl) == PARM_DECL));
5995 memset (temp->last, '\0', sizeof (*temp->last));
5996 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5997 return temp->last;
6000 if (bitsize == -1 && NOTE_P (last->loc))
6001 last_loc_note = last->loc;
6002 else if (piece_loc != NULL
6003 && *piece_loc != NULL_RTX
6004 && piece_bitpos == bitpos
6005 && decl_piece_bitsize (*piece_loc) == bitsize)
6006 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6007 else
6008 last_loc_note = NULL_RTX;
6009 /* If the current location is the same as the end of the list,
6010 and either both or neither of the locations is uninitialized,
6011 we have nothing to do. */
6012 if (last_loc_note == NULL_RTX
6013 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6014 NOTE_VAR_LOCATION_LOC (loc_note)))
6015 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6016 != NOTE_VAR_LOCATION_STATUS (loc_note))
6017 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6018 == VAR_INIT_STATUS_UNINITIALIZED)
6019 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6020 == VAR_INIT_STATUS_UNINITIALIZED))))
6022 /* Add LOC to the end of list and update LAST. If the last
6023 element of the list has been removed above, reuse its
6024 memory for the new node, otherwise allocate a new one. */
6025 if (unused)
6027 loc = unused;
6028 memset (loc, '\0', sizeof (*loc));
6030 else
6031 loc = ggc_cleared_alloc<var_loc_node> ();
6032 if (bitsize == -1 || piece_loc == NULL)
6033 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6034 else
6035 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6036 bitpos, piece_bitpos, bitsize, loc_note);
6037 last->next = loc;
6038 /* Ensure TEMP->LAST will point either to the new last but one
6039 element of the chain, or to the last element in it. */
6040 if (last != temp->last)
6041 temp->last = last;
6043 else if (unused)
6044 ggc_free (unused);
6046 else
6048 loc = ggc_cleared_alloc<var_loc_node> ();
6049 temp->first = loc;
6050 temp->last = loc;
6051 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6053 return loc;
6056 /* Keep track of the number of spaces used to indent the
6057 output of the debugging routines that print the structure of
6058 the DIE internal representation. */
6059 static int print_indent;
6061 /* Indent the line the number of spaces given by print_indent. */
6063 static inline void
6064 print_spaces (FILE *outfile)
6066 fprintf (outfile, "%*s", print_indent, "");
6069 /* Print a type signature in hex. */
6071 static inline void
6072 print_signature (FILE *outfile, char *sig)
6074 int i;
6076 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6077 fprintf (outfile, "%02x", sig[i] & 0xff);
6080 static inline void
6081 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6083 if (discr_value->pos)
6084 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6085 else
6086 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6089 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6091 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6092 RECURSE, output location descriptor operations. */
6094 static void
6095 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6097 switch (val->val_class)
6099 case dw_val_class_addr:
6100 fprintf (outfile, "address");
6101 break;
6102 case dw_val_class_offset:
6103 fprintf (outfile, "offset");
6104 break;
6105 case dw_val_class_loc:
6106 fprintf (outfile, "location descriptor");
6107 if (val->v.val_loc == NULL)
6108 fprintf (outfile, " -> <null>\n");
6109 else if (recurse)
6111 fprintf (outfile, ":\n");
6112 print_indent += 4;
6113 print_loc_descr (val->v.val_loc, outfile);
6114 print_indent -= 4;
6116 else
6117 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6118 break;
6119 case dw_val_class_loc_list:
6120 fprintf (outfile, "location list -> label:%s",
6121 val->v.val_loc_list->ll_symbol);
6122 break;
6123 case dw_val_class_range_list:
6124 fprintf (outfile, "range list");
6125 break;
6126 case dw_val_class_const:
6127 case dw_val_class_const_implicit:
6128 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6129 break;
6130 case dw_val_class_unsigned_const:
6131 case dw_val_class_unsigned_const_implicit:
6132 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6133 break;
6134 case dw_val_class_const_double:
6135 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6136 HOST_WIDE_INT_PRINT_UNSIGNED")",
6137 val->v.val_double.high,
6138 val->v.val_double.low);
6139 break;
6140 case dw_val_class_wide_int:
6142 int i = val->v.val_wide->get_len ();
6143 fprintf (outfile, "constant (");
6144 gcc_assert (i > 0);
6145 if (val->v.val_wide->elt (i - 1) == 0)
6146 fprintf (outfile, "0x");
6147 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6148 val->v.val_wide->elt (--i));
6149 while (--i >= 0)
6150 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6151 val->v.val_wide->elt (i));
6152 fprintf (outfile, ")");
6153 break;
6155 case dw_val_class_vec:
6156 fprintf (outfile, "floating-point or vector constant");
6157 break;
6158 case dw_val_class_flag:
6159 fprintf (outfile, "%u", val->v.val_flag);
6160 break;
6161 case dw_val_class_die_ref:
6162 if (val->v.val_die_ref.die != NULL)
6164 dw_die_ref die = val->v.val_die_ref.die;
6166 if (die->comdat_type_p)
6168 fprintf (outfile, "die -> signature: ");
6169 print_signature (outfile,
6170 die->die_id.die_type_node->signature);
6172 else if (die->die_id.die_symbol)
6174 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6175 if (die->with_offset)
6176 fprintf (outfile, " + %ld", die->die_offset);
6178 else
6179 fprintf (outfile, "die -> %ld", die->die_offset);
6180 fprintf (outfile, " (%p)", (void *) die);
6182 else
6183 fprintf (outfile, "die -> <null>");
6184 break;
6185 case dw_val_class_vms_delta:
6186 fprintf (outfile, "delta: @slotcount(%s-%s)",
6187 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6188 break;
6189 case dw_val_class_lbl_id:
6190 case dw_val_class_lineptr:
6191 case dw_val_class_macptr:
6192 case dw_val_class_loclistsptr:
6193 case dw_val_class_high_pc:
6194 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6195 break;
6196 case dw_val_class_str:
6197 if (val->v.val_str->str != NULL)
6198 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6199 else
6200 fprintf (outfile, "<null>");
6201 break;
6202 case dw_val_class_file:
6203 case dw_val_class_file_implicit:
6204 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6205 val->v.val_file->emitted_number);
6206 break;
6207 case dw_val_class_data8:
6209 int i;
6211 for (i = 0; i < 8; i++)
6212 fprintf (outfile, "%02x", val->v.val_data8[i]);
6213 break;
6215 case dw_val_class_discr_value:
6216 print_discr_value (outfile, &val->v.val_discr_value);
6217 break;
6218 case dw_val_class_discr_list:
6219 for (dw_discr_list_ref node = val->v.val_discr_list;
6220 node != NULL;
6221 node = node->dw_discr_next)
6223 if (node->dw_discr_range)
6225 fprintf (outfile, " .. ");
6226 print_discr_value (outfile, &node->dw_discr_lower_bound);
6227 print_discr_value (outfile, &node->dw_discr_upper_bound);
6229 else
6230 print_discr_value (outfile, &node->dw_discr_lower_bound);
6232 if (node->dw_discr_next != NULL)
6233 fprintf (outfile, " | ");
6235 default:
6236 break;
6240 /* Likewise, for a DIE attribute. */
6242 static void
6243 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6245 print_dw_val (&a->dw_attr_val, recurse, outfile);
6249 /* Print the list of operands in the LOC location description to OUTFILE. This
6250 routine is a debugging aid only. */
6252 static void
6253 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6255 dw_loc_descr_ref l = loc;
6257 if (loc == NULL)
6259 print_spaces (outfile);
6260 fprintf (outfile, "<null>\n");
6261 return;
6264 for (l = loc; l != NULL; l = l->dw_loc_next)
6266 print_spaces (outfile);
6267 fprintf (outfile, "(%p) %s",
6268 (void *) l,
6269 dwarf_stack_op_name (l->dw_loc_opc));
6270 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6272 fprintf (outfile, " ");
6273 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6275 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6277 fprintf (outfile, ", ");
6278 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6280 fprintf (outfile, "\n");
6284 /* Print the information associated with a given DIE, and its children.
6285 This routine is a debugging aid only. */
6287 static void
6288 print_die (dw_die_ref die, FILE *outfile)
6290 dw_attr_node *a;
6291 dw_die_ref c;
6292 unsigned ix;
6294 print_spaces (outfile);
6295 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6296 die->die_offset, dwarf_tag_name (die->die_tag),
6297 (void*) die);
6298 print_spaces (outfile);
6299 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6300 fprintf (outfile, " offset: %ld", die->die_offset);
6301 fprintf (outfile, " mark: %d\n", die->die_mark);
6303 if (die->comdat_type_p)
6305 print_spaces (outfile);
6306 fprintf (outfile, " signature: ");
6307 print_signature (outfile, die->die_id.die_type_node->signature);
6308 fprintf (outfile, "\n");
6311 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6313 print_spaces (outfile);
6314 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6316 print_attribute (a, true, outfile);
6317 fprintf (outfile, "\n");
6320 if (die->die_child != NULL)
6322 print_indent += 4;
6323 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6324 print_indent -= 4;
6326 if (print_indent == 0)
6327 fprintf (outfile, "\n");
6330 /* Print the list of operations in the LOC location description. */
6332 DEBUG_FUNCTION void
6333 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6335 print_loc_descr (loc, stderr);
6338 /* Print the information collected for a given DIE. */
6340 DEBUG_FUNCTION void
6341 debug_dwarf_die (dw_die_ref die)
6343 print_die (die, stderr);
6346 DEBUG_FUNCTION void
6347 debug (die_struct &ref)
6349 print_die (&ref, stderr);
6352 DEBUG_FUNCTION void
6353 debug (die_struct *ptr)
6355 if (ptr)
6356 debug (*ptr);
6357 else
6358 fprintf (stderr, "<nil>\n");
6362 /* Print all DWARF information collected for the compilation unit.
6363 This routine is a debugging aid only. */
6365 DEBUG_FUNCTION void
6366 debug_dwarf (void)
6368 print_indent = 0;
6369 print_die (comp_unit_die (), stderr);
6372 /* Verify the DIE tree structure. */
6374 DEBUG_FUNCTION void
6375 verify_die (dw_die_ref die)
6377 gcc_assert (!die->die_mark);
6378 if (die->die_parent == NULL
6379 && die->die_sib == NULL)
6380 return;
6381 /* Verify the die_sib list is cyclic. */
6382 dw_die_ref x = die;
6385 x->die_mark = 1;
6386 x = x->die_sib;
6388 while (x && !x->die_mark);
6389 gcc_assert (x == die);
6390 x = die;
6393 /* Verify all dies have the same parent. */
6394 gcc_assert (x->die_parent == die->die_parent);
6395 if (x->die_child)
6397 /* Verify the child has the proper parent and recurse. */
6398 gcc_assert (x->die_child->die_parent == x);
6399 verify_die (x->die_child);
6401 x->die_mark = 0;
6402 x = x->die_sib;
6404 while (x && x->die_mark);
6407 /* Sanity checks on DIEs. */
6409 static void
6410 check_die (dw_die_ref die)
6412 unsigned ix;
6413 dw_attr_node *a;
6414 bool inline_found = false;
6415 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6416 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6417 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6419 switch (a->dw_attr)
6421 case DW_AT_inline:
6422 if (a->dw_attr_val.v.val_unsigned)
6423 inline_found = true;
6424 break;
6425 case DW_AT_location:
6426 ++n_location;
6427 break;
6428 case DW_AT_low_pc:
6429 ++n_low_pc;
6430 break;
6431 case DW_AT_high_pc:
6432 ++n_high_pc;
6433 break;
6434 case DW_AT_artificial:
6435 ++n_artificial;
6436 break;
6437 case DW_AT_decl_column:
6438 ++n_decl_column;
6439 break;
6440 case DW_AT_decl_line:
6441 ++n_decl_line;
6442 break;
6443 case DW_AT_decl_file:
6444 ++n_decl_file;
6445 break;
6446 default:
6447 break;
6450 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6451 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6453 fprintf (stderr, "Duplicate attributes in DIE:\n");
6454 debug_dwarf_die (die);
6455 gcc_unreachable ();
6457 if (inline_found)
6459 /* A debugging information entry that is a member of an abstract
6460 instance tree [that has DW_AT_inline] should not contain any
6461 attributes which describe aspects of the subroutine which vary
6462 between distinct inlined expansions or distinct out-of-line
6463 expansions. */
6464 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6465 gcc_assert (a->dw_attr != DW_AT_low_pc
6466 && a->dw_attr != DW_AT_high_pc
6467 && a->dw_attr != DW_AT_location
6468 && a->dw_attr != DW_AT_frame_base
6469 && a->dw_attr != DW_AT_call_all_calls
6470 && a->dw_attr != DW_AT_GNU_all_call_sites);
6474 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6475 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6476 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6478 /* Calculate the checksum of a location expression. */
6480 static inline void
6481 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6483 int tem;
6484 inchash::hash hstate;
6485 hashval_t hash;
6487 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6488 CHECKSUM (tem);
6489 hash_loc_operands (loc, hstate);
6490 hash = hstate.end();
6491 CHECKSUM (hash);
6494 /* Calculate the checksum of an attribute. */
6496 static void
6497 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6499 dw_loc_descr_ref loc;
6500 rtx r;
6502 CHECKSUM (at->dw_attr);
6504 /* We don't care that this was compiled with a different compiler
6505 snapshot; if the output is the same, that's what matters. */
6506 if (at->dw_attr == DW_AT_producer)
6507 return;
6509 switch (AT_class (at))
6511 case dw_val_class_const:
6512 case dw_val_class_const_implicit:
6513 CHECKSUM (at->dw_attr_val.v.val_int);
6514 break;
6515 case dw_val_class_unsigned_const:
6516 case dw_val_class_unsigned_const_implicit:
6517 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6518 break;
6519 case dw_val_class_const_double:
6520 CHECKSUM (at->dw_attr_val.v.val_double);
6521 break;
6522 case dw_val_class_wide_int:
6523 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6524 get_full_len (*at->dw_attr_val.v.val_wide)
6525 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6526 break;
6527 case dw_val_class_vec:
6528 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6529 (at->dw_attr_val.v.val_vec.length
6530 * at->dw_attr_val.v.val_vec.elt_size));
6531 break;
6532 case dw_val_class_flag:
6533 CHECKSUM (at->dw_attr_val.v.val_flag);
6534 break;
6535 case dw_val_class_str:
6536 CHECKSUM_STRING (AT_string (at));
6537 break;
6539 case dw_val_class_addr:
6540 r = AT_addr (at);
6541 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6542 CHECKSUM_STRING (XSTR (r, 0));
6543 break;
6545 case dw_val_class_offset:
6546 CHECKSUM (at->dw_attr_val.v.val_offset);
6547 break;
6549 case dw_val_class_loc:
6550 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6551 loc_checksum (loc, ctx);
6552 break;
6554 case dw_val_class_die_ref:
6555 die_checksum (AT_ref (at), ctx, mark);
6556 break;
6558 case dw_val_class_fde_ref:
6559 case dw_val_class_vms_delta:
6560 case dw_val_class_lbl_id:
6561 case dw_val_class_lineptr:
6562 case dw_val_class_macptr:
6563 case dw_val_class_loclistsptr:
6564 case dw_val_class_high_pc:
6565 break;
6567 case dw_val_class_file:
6568 case dw_val_class_file_implicit:
6569 CHECKSUM_STRING (AT_file (at)->filename);
6570 break;
6572 case dw_val_class_data8:
6573 CHECKSUM (at->dw_attr_val.v.val_data8);
6574 break;
6576 default:
6577 break;
6581 /* Calculate the checksum of a DIE. */
6583 static void
6584 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6586 dw_die_ref c;
6587 dw_attr_node *a;
6588 unsigned ix;
6590 /* To avoid infinite recursion. */
6591 if (die->die_mark)
6593 CHECKSUM (die->die_mark);
6594 return;
6596 die->die_mark = ++(*mark);
6598 CHECKSUM (die->die_tag);
6600 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6601 attr_checksum (a, ctx, mark);
6603 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6606 #undef CHECKSUM
6607 #undef CHECKSUM_BLOCK
6608 #undef CHECKSUM_STRING
6610 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6611 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6612 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6613 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6614 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6615 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6616 #define CHECKSUM_ATTR(FOO) \
6617 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6619 /* Calculate the checksum of a number in signed LEB128 format. */
6621 static void
6622 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6624 unsigned char byte;
6625 bool more;
6627 while (1)
6629 byte = (value & 0x7f);
6630 value >>= 7;
6631 more = !((value == 0 && (byte & 0x40) == 0)
6632 || (value == -1 && (byte & 0x40) != 0));
6633 if (more)
6634 byte |= 0x80;
6635 CHECKSUM (byte);
6636 if (!more)
6637 break;
6641 /* Calculate the checksum of a number in unsigned LEB128 format. */
6643 static void
6644 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6646 while (1)
6648 unsigned char byte = (value & 0x7f);
6649 value >>= 7;
6650 if (value != 0)
6651 /* More bytes to follow. */
6652 byte |= 0x80;
6653 CHECKSUM (byte);
6654 if (value == 0)
6655 break;
6659 /* Checksum the context of the DIE. This adds the names of any
6660 surrounding namespaces or structures to the checksum. */
6662 static void
6663 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6665 const char *name;
6666 dw_die_ref spec;
6667 int tag = die->die_tag;
6669 if (tag != DW_TAG_namespace
6670 && tag != DW_TAG_structure_type
6671 && tag != DW_TAG_class_type)
6672 return;
6674 name = get_AT_string (die, DW_AT_name);
6676 spec = get_AT_ref (die, DW_AT_specification);
6677 if (spec != NULL)
6678 die = spec;
6680 if (die->die_parent != NULL)
6681 checksum_die_context (die->die_parent, ctx);
6683 CHECKSUM_ULEB128 ('C');
6684 CHECKSUM_ULEB128 (tag);
6685 if (name != NULL)
6686 CHECKSUM_STRING (name);
6689 /* Calculate the checksum of a location expression. */
6691 static inline void
6692 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6694 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6695 were emitted as a DW_FORM_sdata instead of a location expression. */
6696 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6698 CHECKSUM_ULEB128 (DW_FORM_sdata);
6699 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6700 return;
6703 /* Otherwise, just checksum the raw location expression. */
6704 while (loc != NULL)
6706 inchash::hash hstate;
6707 hashval_t hash;
6709 CHECKSUM_ULEB128 (loc->dtprel);
6710 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6711 hash_loc_operands (loc, hstate);
6712 hash = hstate.end ();
6713 CHECKSUM (hash);
6714 loc = loc->dw_loc_next;
6718 /* Calculate the checksum of an attribute. */
6720 static void
6721 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6722 struct md5_ctx *ctx, int *mark)
6724 dw_loc_descr_ref loc;
6725 rtx r;
6727 if (AT_class (at) == dw_val_class_die_ref)
6729 dw_die_ref target_die = AT_ref (at);
6731 /* For pointer and reference types, we checksum only the (qualified)
6732 name of the target type (if there is a name). For friend entries,
6733 we checksum only the (qualified) name of the target type or function.
6734 This allows the checksum to remain the same whether the target type
6735 is complete or not. */
6736 if ((at->dw_attr == DW_AT_type
6737 && (tag == DW_TAG_pointer_type
6738 || tag == DW_TAG_reference_type
6739 || tag == DW_TAG_rvalue_reference_type
6740 || tag == DW_TAG_ptr_to_member_type))
6741 || (at->dw_attr == DW_AT_friend
6742 && tag == DW_TAG_friend))
6744 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6746 if (name_attr != NULL)
6748 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6750 if (decl == NULL)
6751 decl = target_die;
6752 CHECKSUM_ULEB128 ('N');
6753 CHECKSUM_ULEB128 (at->dw_attr);
6754 if (decl->die_parent != NULL)
6755 checksum_die_context (decl->die_parent, ctx);
6756 CHECKSUM_ULEB128 ('E');
6757 CHECKSUM_STRING (AT_string (name_attr));
6758 return;
6762 /* For all other references to another DIE, we check to see if the
6763 target DIE has already been visited. If it has, we emit a
6764 backward reference; if not, we descend recursively. */
6765 if (target_die->die_mark > 0)
6767 CHECKSUM_ULEB128 ('R');
6768 CHECKSUM_ULEB128 (at->dw_attr);
6769 CHECKSUM_ULEB128 (target_die->die_mark);
6771 else
6773 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6775 if (decl == NULL)
6776 decl = target_die;
6777 target_die->die_mark = ++(*mark);
6778 CHECKSUM_ULEB128 ('T');
6779 CHECKSUM_ULEB128 (at->dw_attr);
6780 if (decl->die_parent != NULL)
6781 checksum_die_context (decl->die_parent, ctx);
6782 die_checksum_ordered (target_die, ctx, mark);
6784 return;
6787 CHECKSUM_ULEB128 ('A');
6788 CHECKSUM_ULEB128 (at->dw_attr);
6790 switch (AT_class (at))
6792 case dw_val_class_const:
6793 case dw_val_class_const_implicit:
6794 CHECKSUM_ULEB128 (DW_FORM_sdata);
6795 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6796 break;
6798 case dw_val_class_unsigned_const:
6799 case dw_val_class_unsigned_const_implicit:
6800 CHECKSUM_ULEB128 (DW_FORM_sdata);
6801 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6802 break;
6804 case dw_val_class_const_double:
6805 CHECKSUM_ULEB128 (DW_FORM_block);
6806 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6807 CHECKSUM (at->dw_attr_val.v.val_double);
6808 break;
6810 case dw_val_class_wide_int:
6811 CHECKSUM_ULEB128 (DW_FORM_block);
6812 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6813 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6814 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6815 get_full_len (*at->dw_attr_val.v.val_wide)
6816 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6817 break;
6819 case dw_val_class_vec:
6820 CHECKSUM_ULEB128 (DW_FORM_block);
6821 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6822 * at->dw_attr_val.v.val_vec.elt_size);
6823 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6824 (at->dw_attr_val.v.val_vec.length
6825 * at->dw_attr_val.v.val_vec.elt_size));
6826 break;
6828 case dw_val_class_flag:
6829 CHECKSUM_ULEB128 (DW_FORM_flag);
6830 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6831 break;
6833 case dw_val_class_str:
6834 CHECKSUM_ULEB128 (DW_FORM_string);
6835 CHECKSUM_STRING (AT_string (at));
6836 break;
6838 case dw_val_class_addr:
6839 r = AT_addr (at);
6840 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6841 CHECKSUM_ULEB128 (DW_FORM_string);
6842 CHECKSUM_STRING (XSTR (r, 0));
6843 break;
6845 case dw_val_class_offset:
6846 CHECKSUM_ULEB128 (DW_FORM_sdata);
6847 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6848 break;
6850 case dw_val_class_loc:
6851 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6852 loc_checksum_ordered (loc, ctx);
6853 break;
6855 case dw_val_class_fde_ref:
6856 case dw_val_class_lbl_id:
6857 case dw_val_class_lineptr:
6858 case dw_val_class_macptr:
6859 case dw_val_class_loclistsptr:
6860 case dw_val_class_high_pc:
6861 break;
6863 case dw_val_class_file:
6864 case dw_val_class_file_implicit:
6865 CHECKSUM_ULEB128 (DW_FORM_string);
6866 CHECKSUM_STRING (AT_file (at)->filename);
6867 break;
6869 case dw_val_class_data8:
6870 CHECKSUM (at->dw_attr_val.v.val_data8);
6871 break;
6873 default:
6874 break;
6878 struct checksum_attributes
6880 dw_attr_node *at_name;
6881 dw_attr_node *at_type;
6882 dw_attr_node *at_friend;
6883 dw_attr_node *at_accessibility;
6884 dw_attr_node *at_address_class;
6885 dw_attr_node *at_alignment;
6886 dw_attr_node *at_allocated;
6887 dw_attr_node *at_artificial;
6888 dw_attr_node *at_associated;
6889 dw_attr_node *at_binary_scale;
6890 dw_attr_node *at_bit_offset;
6891 dw_attr_node *at_bit_size;
6892 dw_attr_node *at_bit_stride;
6893 dw_attr_node *at_byte_size;
6894 dw_attr_node *at_byte_stride;
6895 dw_attr_node *at_const_value;
6896 dw_attr_node *at_containing_type;
6897 dw_attr_node *at_count;
6898 dw_attr_node *at_data_location;
6899 dw_attr_node *at_data_member_location;
6900 dw_attr_node *at_decimal_scale;
6901 dw_attr_node *at_decimal_sign;
6902 dw_attr_node *at_default_value;
6903 dw_attr_node *at_digit_count;
6904 dw_attr_node *at_discr;
6905 dw_attr_node *at_discr_list;
6906 dw_attr_node *at_discr_value;
6907 dw_attr_node *at_encoding;
6908 dw_attr_node *at_endianity;
6909 dw_attr_node *at_explicit;
6910 dw_attr_node *at_is_optional;
6911 dw_attr_node *at_location;
6912 dw_attr_node *at_lower_bound;
6913 dw_attr_node *at_mutable;
6914 dw_attr_node *at_ordering;
6915 dw_attr_node *at_picture_string;
6916 dw_attr_node *at_prototyped;
6917 dw_attr_node *at_small;
6918 dw_attr_node *at_segment;
6919 dw_attr_node *at_string_length;
6920 dw_attr_node *at_string_length_bit_size;
6921 dw_attr_node *at_string_length_byte_size;
6922 dw_attr_node *at_threads_scaled;
6923 dw_attr_node *at_upper_bound;
6924 dw_attr_node *at_use_location;
6925 dw_attr_node *at_use_UTF8;
6926 dw_attr_node *at_variable_parameter;
6927 dw_attr_node *at_virtuality;
6928 dw_attr_node *at_visibility;
6929 dw_attr_node *at_vtable_elem_location;
6932 /* Collect the attributes that we will want to use for the checksum. */
6934 static void
6935 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6937 dw_attr_node *a;
6938 unsigned ix;
6940 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6942 switch (a->dw_attr)
6944 case DW_AT_name:
6945 attrs->at_name = a;
6946 break;
6947 case DW_AT_type:
6948 attrs->at_type = a;
6949 break;
6950 case DW_AT_friend:
6951 attrs->at_friend = a;
6952 break;
6953 case DW_AT_accessibility:
6954 attrs->at_accessibility = a;
6955 break;
6956 case DW_AT_address_class:
6957 attrs->at_address_class = a;
6958 break;
6959 case DW_AT_alignment:
6960 attrs->at_alignment = a;
6961 break;
6962 case DW_AT_allocated:
6963 attrs->at_allocated = a;
6964 break;
6965 case DW_AT_artificial:
6966 attrs->at_artificial = a;
6967 break;
6968 case DW_AT_associated:
6969 attrs->at_associated = a;
6970 break;
6971 case DW_AT_binary_scale:
6972 attrs->at_binary_scale = a;
6973 break;
6974 case DW_AT_bit_offset:
6975 attrs->at_bit_offset = a;
6976 break;
6977 case DW_AT_bit_size:
6978 attrs->at_bit_size = a;
6979 break;
6980 case DW_AT_bit_stride:
6981 attrs->at_bit_stride = a;
6982 break;
6983 case DW_AT_byte_size:
6984 attrs->at_byte_size = a;
6985 break;
6986 case DW_AT_byte_stride:
6987 attrs->at_byte_stride = a;
6988 break;
6989 case DW_AT_const_value:
6990 attrs->at_const_value = a;
6991 break;
6992 case DW_AT_containing_type:
6993 attrs->at_containing_type = a;
6994 break;
6995 case DW_AT_count:
6996 attrs->at_count = a;
6997 break;
6998 case DW_AT_data_location:
6999 attrs->at_data_location = a;
7000 break;
7001 case DW_AT_data_member_location:
7002 attrs->at_data_member_location = a;
7003 break;
7004 case DW_AT_decimal_scale:
7005 attrs->at_decimal_scale = a;
7006 break;
7007 case DW_AT_decimal_sign:
7008 attrs->at_decimal_sign = a;
7009 break;
7010 case DW_AT_default_value:
7011 attrs->at_default_value = a;
7012 break;
7013 case DW_AT_digit_count:
7014 attrs->at_digit_count = a;
7015 break;
7016 case DW_AT_discr:
7017 attrs->at_discr = a;
7018 break;
7019 case DW_AT_discr_list:
7020 attrs->at_discr_list = a;
7021 break;
7022 case DW_AT_discr_value:
7023 attrs->at_discr_value = a;
7024 break;
7025 case DW_AT_encoding:
7026 attrs->at_encoding = a;
7027 break;
7028 case DW_AT_endianity:
7029 attrs->at_endianity = a;
7030 break;
7031 case DW_AT_explicit:
7032 attrs->at_explicit = a;
7033 break;
7034 case DW_AT_is_optional:
7035 attrs->at_is_optional = a;
7036 break;
7037 case DW_AT_location:
7038 attrs->at_location = a;
7039 break;
7040 case DW_AT_lower_bound:
7041 attrs->at_lower_bound = a;
7042 break;
7043 case DW_AT_mutable:
7044 attrs->at_mutable = a;
7045 break;
7046 case DW_AT_ordering:
7047 attrs->at_ordering = a;
7048 break;
7049 case DW_AT_picture_string:
7050 attrs->at_picture_string = a;
7051 break;
7052 case DW_AT_prototyped:
7053 attrs->at_prototyped = a;
7054 break;
7055 case DW_AT_small:
7056 attrs->at_small = a;
7057 break;
7058 case DW_AT_segment:
7059 attrs->at_segment = a;
7060 break;
7061 case DW_AT_string_length:
7062 attrs->at_string_length = a;
7063 break;
7064 case DW_AT_string_length_bit_size:
7065 attrs->at_string_length_bit_size = a;
7066 break;
7067 case DW_AT_string_length_byte_size:
7068 attrs->at_string_length_byte_size = a;
7069 break;
7070 case DW_AT_threads_scaled:
7071 attrs->at_threads_scaled = a;
7072 break;
7073 case DW_AT_upper_bound:
7074 attrs->at_upper_bound = a;
7075 break;
7076 case DW_AT_use_location:
7077 attrs->at_use_location = a;
7078 break;
7079 case DW_AT_use_UTF8:
7080 attrs->at_use_UTF8 = a;
7081 break;
7082 case DW_AT_variable_parameter:
7083 attrs->at_variable_parameter = a;
7084 break;
7085 case DW_AT_virtuality:
7086 attrs->at_virtuality = a;
7087 break;
7088 case DW_AT_visibility:
7089 attrs->at_visibility = a;
7090 break;
7091 case DW_AT_vtable_elem_location:
7092 attrs->at_vtable_elem_location = a;
7093 break;
7094 default:
7095 break;
7100 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7102 static void
7103 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7105 dw_die_ref c;
7106 dw_die_ref decl;
7107 struct checksum_attributes attrs;
7109 CHECKSUM_ULEB128 ('D');
7110 CHECKSUM_ULEB128 (die->die_tag);
7112 memset (&attrs, 0, sizeof (attrs));
7114 decl = get_AT_ref (die, DW_AT_specification);
7115 if (decl != NULL)
7116 collect_checksum_attributes (&attrs, decl);
7117 collect_checksum_attributes (&attrs, die);
7119 CHECKSUM_ATTR (attrs.at_name);
7120 CHECKSUM_ATTR (attrs.at_accessibility);
7121 CHECKSUM_ATTR (attrs.at_address_class);
7122 CHECKSUM_ATTR (attrs.at_allocated);
7123 CHECKSUM_ATTR (attrs.at_artificial);
7124 CHECKSUM_ATTR (attrs.at_associated);
7125 CHECKSUM_ATTR (attrs.at_binary_scale);
7126 CHECKSUM_ATTR (attrs.at_bit_offset);
7127 CHECKSUM_ATTR (attrs.at_bit_size);
7128 CHECKSUM_ATTR (attrs.at_bit_stride);
7129 CHECKSUM_ATTR (attrs.at_byte_size);
7130 CHECKSUM_ATTR (attrs.at_byte_stride);
7131 CHECKSUM_ATTR (attrs.at_const_value);
7132 CHECKSUM_ATTR (attrs.at_containing_type);
7133 CHECKSUM_ATTR (attrs.at_count);
7134 CHECKSUM_ATTR (attrs.at_data_location);
7135 CHECKSUM_ATTR (attrs.at_data_member_location);
7136 CHECKSUM_ATTR (attrs.at_decimal_scale);
7137 CHECKSUM_ATTR (attrs.at_decimal_sign);
7138 CHECKSUM_ATTR (attrs.at_default_value);
7139 CHECKSUM_ATTR (attrs.at_digit_count);
7140 CHECKSUM_ATTR (attrs.at_discr);
7141 CHECKSUM_ATTR (attrs.at_discr_list);
7142 CHECKSUM_ATTR (attrs.at_discr_value);
7143 CHECKSUM_ATTR (attrs.at_encoding);
7144 CHECKSUM_ATTR (attrs.at_endianity);
7145 CHECKSUM_ATTR (attrs.at_explicit);
7146 CHECKSUM_ATTR (attrs.at_is_optional);
7147 CHECKSUM_ATTR (attrs.at_location);
7148 CHECKSUM_ATTR (attrs.at_lower_bound);
7149 CHECKSUM_ATTR (attrs.at_mutable);
7150 CHECKSUM_ATTR (attrs.at_ordering);
7151 CHECKSUM_ATTR (attrs.at_picture_string);
7152 CHECKSUM_ATTR (attrs.at_prototyped);
7153 CHECKSUM_ATTR (attrs.at_small);
7154 CHECKSUM_ATTR (attrs.at_segment);
7155 CHECKSUM_ATTR (attrs.at_string_length);
7156 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7157 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7158 CHECKSUM_ATTR (attrs.at_threads_scaled);
7159 CHECKSUM_ATTR (attrs.at_upper_bound);
7160 CHECKSUM_ATTR (attrs.at_use_location);
7161 CHECKSUM_ATTR (attrs.at_use_UTF8);
7162 CHECKSUM_ATTR (attrs.at_variable_parameter);
7163 CHECKSUM_ATTR (attrs.at_virtuality);
7164 CHECKSUM_ATTR (attrs.at_visibility);
7165 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7166 CHECKSUM_ATTR (attrs.at_type);
7167 CHECKSUM_ATTR (attrs.at_friend);
7168 CHECKSUM_ATTR (attrs.at_alignment);
7170 /* Checksum the child DIEs. */
7171 c = die->die_child;
7172 if (c) do {
7173 dw_attr_node *name_attr;
7175 c = c->die_sib;
7176 name_attr = get_AT (c, DW_AT_name);
7177 if (is_template_instantiation (c))
7179 /* Ignore instantiations of member type and function templates. */
7181 else if (name_attr != NULL
7182 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7184 /* Use a shallow checksum for named nested types and member
7185 functions. */
7186 CHECKSUM_ULEB128 ('S');
7187 CHECKSUM_ULEB128 (c->die_tag);
7188 CHECKSUM_STRING (AT_string (name_attr));
7190 else
7192 /* Use a deep checksum for other children. */
7193 /* Mark this DIE so it gets processed when unmarking. */
7194 if (c->die_mark == 0)
7195 c->die_mark = -1;
7196 die_checksum_ordered (c, ctx, mark);
7198 } while (c != die->die_child);
7200 CHECKSUM_ULEB128 (0);
7203 /* Add a type name and tag to a hash. */
7204 static void
7205 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7207 CHECKSUM_ULEB128 (tag);
7208 CHECKSUM_STRING (name);
7211 #undef CHECKSUM
7212 #undef CHECKSUM_STRING
7213 #undef CHECKSUM_ATTR
7214 #undef CHECKSUM_LEB128
7215 #undef CHECKSUM_ULEB128
7217 /* Generate the type signature for DIE. This is computed by generating an
7218 MD5 checksum over the DIE's tag, its relevant attributes, and its
7219 children. Attributes that are references to other DIEs are processed
7220 by recursion, using the MARK field to prevent infinite recursion.
7221 If the DIE is nested inside a namespace or another type, we also
7222 need to include that context in the signature. The lower 64 bits
7223 of the resulting MD5 checksum comprise the signature. */
7225 static void
7226 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7228 int mark;
7229 const char *name;
7230 unsigned char checksum[16];
7231 struct md5_ctx ctx;
7232 dw_die_ref decl;
7233 dw_die_ref parent;
7235 name = get_AT_string (die, DW_AT_name);
7236 decl = get_AT_ref (die, DW_AT_specification);
7237 parent = get_die_parent (die);
7239 /* First, compute a signature for just the type name (and its surrounding
7240 context, if any. This is stored in the type unit DIE for link-time
7241 ODR (one-definition rule) checking. */
7243 if (is_cxx () && name != NULL)
7245 md5_init_ctx (&ctx);
7247 /* Checksum the names of surrounding namespaces and structures. */
7248 if (parent != NULL)
7249 checksum_die_context (parent, &ctx);
7251 /* Checksum the current DIE. */
7252 die_odr_checksum (die->die_tag, name, &ctx);
7253 md5_finish_ctx (&ctx, checksum);
7255 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7258 /* Next, compute the complete type signature. */
7260 md5_init_ctx (&ctx);
7261 mark = 1;
7262 die->die_mark = mark;
7264 /* Checksum the names of surrounding namespaces and structures. */
7265 if (parent != NULL)
7266 checksum_die_context (parent, &ctx);
7268 /* Checksum the DIE and its children. */
7269 die_checksum_ordered (die, &ctx, &mark);
7270 unmark_all_dies (die);
7271 md5_finish_ctx (&ctx, checksum);
7273 /* Store the signature in the type node and link the type DIE and the
7274 type node together. */
7275 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7276 DWARF_TYPE_SIGNATURE_SIZE);
7277 die->comdat_type_p = true;
7278 die->die_id.die_type_node = type_node;
7279 type_node->type_die = die;
7281 /* If the DIE is a specification, link its declaration to the type node
7282 as well. */
7283 if (decl != NULL)
7285 decl->comdat_type_p = true;
7286 decl->die_id.die_type_node = type_node;
7290 /* Do the location expressions look same? */
7291 static inline int
7292 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7294 return loc1->dw_loc_opc == loc2->dw_loc_opc
7295 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7296 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7299 /* Do the values look the same? */
7300 static int
7301 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7303 dw_loc_descr_ref loc1, loc2;
7304 rtx r1, r2;
7306 if (v1->val_class != v2->val_class)
7307 return 0;
7309 switch (v1->val_class)
7311 case dw_val_class_const:
7312 case dw_val_class_const_implicit:
7313 return v1->v.val_int == v2->v.val_int;
7314 case dw_val_class_unsigned_const:
7315 case dw_val_class_unsigned_const_implicit:
7316 return v1->v.val_unsigned == v2->v.val_unsigned;
7317 case dw_val_class_const_double:
7318 return v1->v.val_double.high == v2->v.val_double.high
7319 && v1->v.val_double.low == v2->v.val_double.low;
7320 case dw_val_class_wide_int:
7321 return *v1->v.val_wide == *v2->v.val_wide;
7322 case dw_val_class_vec:
7323 if (v1->v.val_vec.length != v2->v.val_vec.length
7324 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7325 return 0;
7326 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7327 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7328 return 0;
7329 return 1;
7330 case dw_val_class_flag:
7331 return v1->v.val_flag == v2->v.val_flag;
7332 case dw_val_class_str:
7333 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7335 case dw_val_class_addr:
7336 r1 = v1->v.val_addr;
7337 r2 = v2->v.val_addr;
7338 if (GET_CODE (r1) != GET_CODE (r2))
7339 return 0;
7340 return !rtx_equal_p (r1, r2);
7342 case dw_val_class_offset:
7343 return v1->v.val_offset == v2->v.val_offset;
7345 case dw_val_class_loc:
7346 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7347 loc1 && loc2;
7348 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7349 if (!same_loc_p (loc1, loc2, mark))
7350 return 0;
7351 return !loc1 && !loc2;
7353 case dw_val_class_die_ref:
7354 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7356 case dw_val_class_fde_ref:
7357 case dw_val_class_vms_delta:
7358 case dw_val_class_lbl_id:
7359 case dw_val_class_lineptr:
7360 case dw_val_class_macptr:
7361 case dw_val_class_loclistsptr:
7362 case dw_val_class_high_pc:
7363 return 1;
7365 case dw_val_class_file:
7366 case dw_val_class_file_implicit:
7367 return v1->v.val_file == v2->v.val_file;
7369 case dw_val_class_data8:
7370 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7372 default:
7373 return 1;
7377 /* Do the attributes look the same? */
7379 static int
7380 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7382 if (at1->dw_attr != at2->dw_attr)
7383 return 0;
7385 /* We don't care that this was compiled with a different compiler
7386 snapshot; if the output is the same, that's what matters. */
7387 if (at1->dw_attr == DW_AT_producer)
7388 return 1;
7390 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7393 /* Do the dies look the same? */
7395 static int
7396 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7398 dw_die_ref c1, c2;
7399 dw_attr_node *a1;
7400 unsigned ix;
7402 /* To avoid infinite recursion. */
7403 if (die1->die_mark)
7404 return die1->die_mark == die2->die_mark;
7405 die1->die_mark = die2->die_mark = ++(*mark);
7407 if (die1->die_tag != die2->die_tag)
7408 return 0;
7410 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7411 return 0;
7413 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7414 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7415 return 0;
7417 c1 = die1->die_child;
7418 c2 = die2->die_child;
7419 if (! c1)
7421 if (c2)
7422 return 0;
7424 else
7425 for (;;)
7427 if (!same_die_p (c1, c2, mark))
7428 return 0;
7429 c1 = c1->die_sib;
7430 c2 = c2->die_sib;
7431 if (c1 == die1->die_child)
7433 if (c2 == die2->die_child)
7434 break;
7435 else
7436 return 0;
7440 return 1;
7443 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7444 children, and set die_symbol. */
7446 static void
7447 compute_comp_unit_symbol (dw_die_ref unit_die)
7449 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7450 const char *base = die_name ? lbasename (die_name) : "anonymous";
7451 char *name = XALLOCAVEC (char, strlen (base) + 64);
7452 char *p;
7453 int i, mark;
7454 unsigned char checksum[16];
7455 struct md5_ctx ctx;
7457 /* Compute the checksum of the DIE, then append part of it as hex digits to
7458 the name filename of the unit. */
7460 md5_init_ctx (&ctx);
7461 mark = 0;
7462 die_checksum (unit_die, &ctx, &mark);
7463 unmark_all_dies (unit_die);
7464 md5_finish_ctx (&ctx, checksum);
7466 /* When we this for comp_unit_die () we have a DW_AT_name that might
7467 not start with a letter but with anything valid for filenames and
7468 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7469 character is not a letter. */
7470 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7471 clean_symbol_name (name);
7473 p = name + strlen (name);
7474 for (i = 0; i < 4; i++)
7476 sprintf (p, "%.2x", checksum[i]);
7477 p += 2;
7480 unit_die->die_id.die_symbol = xstrdup (name);
7483 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7485 static int
7486 is_type_die (dw_die_ref die)
7488 switch (die->die_tag)
7490 case DW_TAG_array_type:
7491 case DW_TAG_class_type:
7492 case DW_TAG_interface_type:
7493 case DW_TAG_enumeration_type:
7494 case DW_TAG_pointer_type:
7495 case DW_TAG_reference_type:
7496 case DW_TAG_rvalue_reference_type:
7497 case DW_TAG_string_type:
7498 case DW_TAG_structure_type:
7499 case DW_TAG_subroutine_type:
7500 case DW_TAG_union_type:
7501 case DW_TAG_ptr_to_member_type:
7502 case DW_TAG_set_type:
7503 case DW_TAG_subrange_type:
7504 case DW_TAG_base_type:
7505 case DW_TAG_const_type:
7506 case DW_TAG_file_type:
7507 case DW_TAG_packed_type:
7508 case DW_TAG_volatile_type:
7509 case DW_TAG_typedef:
7510 return 1;
7511 default:
7512 return 0;
7516 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7517 Basically, we want to choose the bits that are likely to be shared between
7518 compilations (types) and leave out the bits that are specific to individual
7519 compilations (functions). */
7521 static int
7522 is_comdat_die (dw_die_ref c)
7524 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7525 we do for stabs. The advantage is a greater likelihood of sharing between
7526 objects that don't include headers in the same order (and therefore would
7527 put the base types in a different comdat). jason 8/28/00 */
7529 if (c->die_tag == DW_TAG_base_type)
7530 return 0;
7532 if (c->die_tag == DW_TAG_pointer_type
7533 || c->die_tag == DW_TAG_reference_type
7534 || c->die_tag == DW_TAG_rvalue_reference_type
7535 || c->die_tag == DW_TAG_const_type
7536 || c->die_tag == DW_TAG_volatile_type)
7538 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7540 return t ? is_comdat_die (t) : 0;
7543 return is_type_die (c);
7546 /* Returns true iff C is a compile-unit DIE. */
7548 static inline bool
7549 is_cu_die (dw_die_ref c)
7551 return c && (c->die_tag == DW_TAG_compile_unit
7552 || c->die_tag == DW_TAG_skeleton_unit);
7555 /* Returns true iff C is a unit DIE of some sort. */
7557 static inline bool
7558 is_unit_die (dw_die_ref c)
7560 return c && (c->die_tag == DW_TAG_compile_unit
7561 || c->die_tag == DW_TAG_partial_unit
7562 || c->die_tag == DW_TAG_type_unit
7563 || c->die_tag == DW_TAG_skeleton_unit);
7566 /* Returns true iff C is a namespace DIE. */
7568 static inline bool
7569 is_namespace_die (dw_die_ref c)
7571 return c && c->die_tag == DW_TAG_namespace;
7574 /* Returns true iff C is a class or structure DIE. */
7576 static inline bool
7577 is_class_die (dw_die_ref c)
7579 return c && (c->die_tag == DW_TAG_class_type
7580 || c->die_tag == DW_TAG_structure_type);
7583 /* Return non-zero if this DIE is a template parameter. */
7585 static inline bool
7586 is_template_parameter (dw_die_ref die)
7588 switch (die->die_tag)
7590 case DW_TAG_template_type_param:
7591 case DW_TAG_template_value_param:
7592 case DW_TAG_GNU_template_template_param:
7593 case DW_TAG_GNU_template_parameter_pack:
7594 return true;
7595 default:
7596 return false;
7600 /* Return non-zero if this DIE represents a template instantiation. */
7602 static inline bool
7603 is_template_instantiation (dw_die_ref die)
7605 dw_die_ref c;
7607 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7608 return false;
7609 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7610 return false;
7613 static char *
7614 gen_internal_sym (const char *prefix)
7616 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7618 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7619 return xstrdup (buf);
7622 /* Return non-zero if this DIE is a declaration. */
7624 static int
7625 is_declaration_die (dw_die_ref die)
7627 dw_attr_node *a;
7628 unsigned ix;
7630 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7631 if (a->dw_attr == DW_AT_declaration)
7632 return 1;
7634 return 0;
7637 /* Return non-zero if this DIE is nested inside a subprogram. */
7639 static int
7640 is_nested_in_subprogram (dw_die_ref die)
7642 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7644 if (decl == NULL)
7645 decl = die;
7646 return local_scope_p (decl);
7649 /* Return non-zero if this DIE contains a defining declaration of a
7650 subprogram. */
7652 static int
7653 contains_subprogram_definition (dw_die_ref die)
7655 dw_die_ref c;
7657 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7658 return 1;
7659 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7660 return 0;
7663 /* Return non-zero if this is a type DIE that should be moved to a
7664 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7665 unit type. */
7667 static int
7668 should_move_die_to_comdat (dw_die_ref die)
7670 switch (die->die_tag)
7672 case DW_TAG_class_type:
7673 case DW_TAG_structure_type:
7674 case DW_TAG_enumeration_type:
7675 case DW_TAG_union_type:
7676 /* Don't move declarations, inlined instances, types nested in a
7677 subprogram, or types that contain subprogram definitions. */
7678 if (is_declaration_die (die)
7679 || get_AT (die, DW_AT_abstract_origin)
7680 || is_nested_in_subprogram (die)
7681 || contains_subprogram_definition (die))
7682 return 0;
7683 return 1;
7684 case DW_TAG_array_type:
7685 case DW_TAG_interface_type:
7686 case DW_TAG_pointer_type:
7687 case DW_TAG_reference_type:
7688 case DW_TAG_rvalue_reference_type:
7689 case DW_TAG_string_type:
7690 case DW_TAG_subroutine_type:
7691 case DW_TAG_ptr_to_member_type:
7692 case DW_TAG_set_type:
7693 case DW_TAG_subrange_type:
7694 case DW_TAG_base_type:
7695 case DW_TAG_const_type:
7696 case DW_TAG_file_type:
7697 case DW_TAG_packed_type:
7698 case DW_TAG_volatile_type:
7699 case DW_TAG_typedef:
7700 default:
7701 return 0;
7705 /* Make a clone of DIE. */
7707 static dw_die_ref
7708 clone_die (dw_die_ref die)
7710 dw_die_ref clone;
7711 dw_attr_node *a;
7712 unsigned ix;
7714 clone = ggc_cleared_alloc<die_node> ();
7715 clone->die_tag = die->die_tag;
7717 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7718 add_dwarf_attr (clone, a);
7720 return clone;
7723 /* Make a clone of the tree rooted at DIE. */
7725 static dw_die_ref
7726 clone_tree (dw_die_ref die)
7728 dw_die_ref c;
7729 dw_die_ref clone = clone_die (die);
7731 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7733 return clone;
7736 /* Make a clone of DIE as a declaration. */
7738 static dw_die_ref
7739 clone_as_declaration (dw_die_ref die)
7741 dw_die_ref clone;
7742 dw_die_ref decl;
7743 dw_attr_node *a;
7744 unsigned ix;
7746 /* If the DIE is already a declaration, just clone it. */
7747 if (is_declaration_die (die))
7748 return clone_die (die);
7750 /* If the DIE is a specification, just clone its declaration DIE. */
7751 decl = get_AT_ref (die, DW_AT_specification);
7752 if (decl != NULL)
7754 clone = clone_die (decl);
7755 if (die->comdat_type_p)
7756 add_AT_die_ref (clone, DW_AT_signature, die);
7757 return clone;
7760 clone = ggc_cleared_alloc<die_node> ();
7761 clone->die_tag = die->die_tag;
7763 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7765 /* We don't want to copy over all attributes.
7766 For example we don't want DW_AT_byte_size because otherwise we will no
7767 longer have a declaration and GDB will treat it as a definition. */
7769 switch (a->dw_attr)
7771 case DW_AT_abstract_origin:
7772 case DW_AT_artificial:
7773 case DW_AT_containing_type:
7774 case DW_AT_external:
7775 case DW_AT_name:
7776 case DW_AT_type:
7777 case DW_AT_virtuality:
7778 case DW_AT_linkage_name:
7779 case DW_AT_MIPS_linkage_name:
7780 add_dwarf_attr (clone, a);
7781 break;
7782 case DW_AT_byte_size:
7783 case DW_AT_alignment:
7784 default:
7785 break;
7789 if (die->comdat_type_p)
7790 add_AT_die_ref (clone, DW_AT_signature, die);
7792 add_AT_flag (clone, DW_AT_declaration, 1);
7793 return clone;
7797 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7799 struct decl_table_entry
7801 dw_die_ref orig;
7802 dw_die_ref copy;
7805 /* Helpers to manipulate hash table of copied declarations. */
7807 /* Hashtable helpers. */
7809 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7811 typedef die_struct *compare_type;
7812 static inline hashval_t hash (const decl_table_entry *);
7813 static inline bool equal (const decl_table_entry *, const die_struct *);
7816 inline hashval_t
7817 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7819 return htab_hash_pointer (entry->orig);
7822 inline bool
7823 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7824 const die_struct *entry2)
7826 return entry1->orig == entry2;
7829 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7831 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7832 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7833 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7834 to check if the ancestor has already been copied into UNIT. */
7836 static dw_die_ref
7837 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7838 decl_hash_type *decl_table)
7840 dw_die_ref parent = die->die_parent;
7841 dw_die_ref new_parent = unit;
7842 dw_die_ref copy;
7843 decl_table_entry **slot = NULL;
7844 struct decl_table_entry *entry = NULL;
7846 if (decl_table)
7848 /* Check if the entry has already been copied to UNIT. */
7849 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7850 INSERT);
7851 if (*slot != HTAB_EMPTY_ENTRY)
7853 entry = *slot;
7854 return entry->copy;
7857 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7858 entry = XCNEW (struct decl_table_entry);
7859 entry->orig = die;
7860 entry->copy = NULL;
7861 *slot = entry;
7864 if (parent != NULL)
7866 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7867 if (spec != NULL)
7868 parent = spec;
7869 if (!is_unit_die (parent))
7870 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7873 copy = clone_as_declaration (die);
7874 add_child_die (new_parent, copy);
7876 if (decl_table)
7878 /* Record the pointer to the copy. */
7879 entry->copy = copy;
7882 return copy;
7884 /* Copy the declaration context to the new type unit DIE. This includes
7885 any surrounding namespace or type declarations. If the DIE has an
7886 AT_specification attribute, it also includes attributes and children
7887 attached to the specification, and returns a pointer to the original
7888 parent of the declaration DIE. Returns NULL otherwise. */
7890 static dw_die_ref
7891 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7893 dw_die_ref decl;
7894 dw_die_ref new_decl;
7895 dw_die_ref orig_parent = NULL;
7897 decl = get_AT_ref (die, DW_AT_specification);
7898 if (decl == NULL)
7899 decl = die;
7900 else
7902 unsigned ix;
7903 dw_die_ref c;
7904 dw_attr_node *a;
7906 /* The original DIE will be changed to a declaration, and must
7907 be moved to be a child of the original declaration DIE. */
7908 orig_parent = decl->die_parent;
7910 /* Copy the type node pointer from the new DIE to the original
7911 declaration DIE so we can forward references later. */
7912 decl->comdat_type_p = true;
7913 decl->die_id.die_type_node = die->die_id.die_type_node;
7915 remove_AT (die, DW_AT_specification);
7917 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7919 if (a->dw_attr != DW_AT_name
7920 && a->dw_attr != DW_AT_declaration
7921 && a->dw_attr != DW_AT_external)
7922 add_dwarf_attr (die, a);
7925 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7928 if (decl->die_parent != NULL
7929 && !is_unit_die (decl->die_parent))
7931 new_decl = copy_ancestor_tree (unit, decl, NULL);
7932 if (new_decl != NULL)
7934 remove_AT (new_decl, DW_AT_signature);
7935 add_AT_specification (die, new_decl);
7939 return orig_parent;
7942 /* Generate the skeleton ancestor tree for the given NODE, then clone
7943 the DIE and add the clone into the tree. */
7945 static void
7946 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7948 if (node->new_die != NULL)
7949 return;
7951 node->new_die = clone_as_declaration (node->old_die);
7953 if (node->parent != NULL)
7955 generate_skeleton_ancestor_tree (node->parent);
7956 add_child_die (node->parent->new_die, node->new_die);
7960 /* Generate a skeleton tree of DIEs containing any declarations that are
7961 found in the original tree. We traverse the tree looking for declaration
7962 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7964 static void
7965 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7967 skeleton_chain_node node;
7968 dw_die_ref c;
7969 dw_die_ref first;
7970 dw_die_ref prev = NULL;
7971 dw_die_ref next = NULL;
7973 node.parent = parent;
7975 first = c = parent->old_die->die_child;
7976 if (c)
7977 next = c->die_sib;
7978 if (c) do {
7979 if (prev == NULL || prev->die_sib == c)
7980 prev = c;
7981 c = next;
7982 next = (c == first ? NULL : c->die_sib);
7983 node.old_die = c;
7984 node.new_die = NULL;
7985 if (is_declaration_die (c))
7987 if (is_template_instantiation (c))
7989 /* Instantiated templates do not need to be cloned into the
7990 type unit. Just move the DIE and its children back to
7991 the skeleton tree (in the main CU). */
7992 remove_child_with_prev (c, prev);
7993 add_child_die (parent->new_die, c);
7994 c = prev;
7996 else if (c->comdat_type_p)
7998 /* This is the skeleton of earlier break_out_comdat_types
7999 type. Clone the existing DIE, but keep the children
8000 under the original (which is in the main CU). */
8001 dw_die_ref clone = clone_die (c);
8003 replace_child (c, clone, prev);
8004 generate_skeleton_ancestor_tree (parent);
8005 add_child_die (parent->new_die, c);
8006 c = clone;
8007 continue;
8009 else
8011 /* Clone the existing DIE, move the original to the skeleton
8012 tree (which is in the main CU), and put the clone, with
8013 all the original's children, where the original came from
8014 (which is about to be moved to the type unit). */
8015 dw_die_ref clone = clone_die (c);
8016 move_all_children (c, clone);
8018 /* If the original has a DW_AT_object_pointer attribute,
8019 it would now point to a child DIE just moved to the
8020 cloned tree, so we need to remove that attribute from
8021 the original. */
8022 remove_AT (c, DW_AT_object_pointer);
8024 replace_child (c, clone, prev);
8025 generate_skeleton_ancestor_tree (parent);
8026 add_child_die (parent->new_die, c);
8027 node.old_die = clone;
8028 node.new_die = c;
8029 c = clone;
8032 generate_skeleton_bottom_up (&node);
8033 } while (next != NULL);
8036 /* Wrapper function for generate_skeleton_bottom_up. */
8038 static dw_die_ref
8039 generate_skeleton (dw_die_ref die)
8041 skeleton_chain_node node;
8043 node.old_die = die;
8044 node.new_die = NULL;
8045 node.parent = NULL;
8047 /* If this type definition is nested inside another type,
8048 and is not an instantiation of a template, always leave
8049 at least a declaration in its place. */
8050 if (die->die_parent != NULL
8051 && is_type_die (die->die_parent)
8052 && !is_template_instantiation (die))
8053 node.new_die = clone_as_declaration (die);
8055 generate_skeleton_bottom_up (&node);
8056 return node.new_die;
8059 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8060 declaration. The original DIE is moved to a new compile unit so that
8061 existing references to it follow it to the new location. If any of the
8062 original DIE's descendants is a declaration, we need to replace the
8063 original DIE with a skeleton tree and move the declarations back into the
8064 skeleton tree. */
8066 static dw_die_ref
8067 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8068 dw_die_ref prev)
8070 dw_die_ref skeleton, orig_parent;
8072 /* Copy the declaration context to the type unit DIE. If the returned
8073 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8074 that DIE. */
8075 orig_parent = copy_declaration_context (unit, child);
8077 skeleton = generate_skeleton (child);
8078 if (skeleton == NULL)
8079 remove_child_with_prev (child, prev);
8080 else
8082 skeleton->comdat_type_p = true;
8083 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8085 /* If the original DIE was a specification, we need to put
8086 the skeleton under the parent DIE of the declaration.
8087 This leaves the original declaration in the tree, but
8088 it will be pruned later since there are no longer any
8089 references to it. */
8090 if (orig_parent != NULL)
8092 remove_child_with_prev (child, prev);
8093 add_child_die (orig_parent, skeleton);
8095 else
8096 replace_child (child, skeleton, prev);
8099 return skeleton;
8102 static void
8103 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8104 comdat_type_node *type_node,
8105 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8107 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8108 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8109 DWARF procedure references in the DW_AT_location attribute. */
8111 static dw_die_ref
8112 copy_dwarf_procedure (dw_die_ref die,
8113 comdat_type_node *type_node,
8114 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8116 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8118 /* DWARF procedures are not supposed to have children... */
8119 gcc_assert (die->die_child == NULL);
8121 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8122 gcc_assert (vec_safe_length (die->die_attr) == 1
8123 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8125 /* Do not copy more than once DWARF procedures. */
8126 bool existed;
8127 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8128 if (existed)
8129 return die_copy;
8131 die_copy = clone_die (die);
8132 add_child_die (type_node->root_die, die_copy);
8133 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8134 return die_copy;
8137 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8138 procedures in DIE's attributes. */
8140 static void
8141 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8142 comdat_type_node *type_node,
8143 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8145 dw_attr_node *a;
8146 unsigned i;
8148 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8150 dw_loc_descr_ref loc;
8152 if (a->dw_attr_val.val_class != dw_val_class_loc)
8153 continue;
8155 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8157 switch (loc->dw_loc_opc)
8159 case DW_OP_call2:
8160 case DW_OP_call4:
8161 case DW_OP_call_ref:
8162 gcc_assert (loc->dw_loc_oprnd1.val_class
8163 == dw_val_class_die_ref);
8164 loc->dw_loc_oprnd1.v.val_die_ref.die
8165 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8166 type_node,
8167 copied_dwarf_procs);
8169 default:
8170 break;
8176 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8177 rewrite references to point to the copies.
8179 References are looked for in DIE's attributes and recursively in all its
8180 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8181 mapping from old DWARF procedures to their copy. It is used not to copy
8182 twice the same DWARF procedure under TYPE_NODE. */
8184 static void
8185 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8186 comdat_type_node *type_node,
8187 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8189 dw_die_ref c;
8191 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8192 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8193 type_node,
8194 copied_dwarf_procs));
8197 /* Traverse the DIE and set up additional .debug_types or .debug_info
8198 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8199 section. */
8201 static void
8202 break_out_comdat_types (dw_die_ref die)
8204 dw_die_ref c;
8205 dw_die_ref first;
8206 dw_die_ref prev = NULL;
8207 dw_die_ref next = NULL;
8208 dw_die_ref unit = NULL;
8210 first = c = die->die_child;
8211 if (c)
8212 next = c->die_sib;
8213 if (c) do {
8214 if (prev == NULL || prev->die_sib == c)
8215 prev = c;
8216 c = next;
8217 next = (c == first ? NULL : c->die_sib);
8218 if (should_move_die_to_comdat (c))
8220 dw_die_ref replacement;
8221 comdat_type_node *type_node;
8223 /* Break out nested types into their own type units. */
8224 break_out_comdat_types (c);
8226 /* Create a new type unit DIE as the root for the new tree, and
8227 add it to the list of comdat types. */
8228 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8229 add_AT_unsigned (unit, DW_AT_language,
8230 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8231 type_node = ggc_cleared_alloc<comdat_type_node> ();
8232 type_node->root_die = unit;
8233 type_node->next = comdat_type_list;
8234 comdat_type_list = type_node;
8236 /* Generate the type signature. */
8237 generate_type_signature (c, type_node);
8239 /* Copy the declaration context, attributes, and children of the
8240 declaration into the new type unit DIE, then remove this DIE
8241 from the main CU (or replace it with a skeleton if necessary). */
8242 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8243 type_node->skeleton_die = replacement;
8245 /* Add the DIE to the new compunit. */
8246 add_child_die (unit, c);
8248 /* Types can reference DWARF procedures for type size or data location
8249 expressions. Calls in DWARF expressions cannot target procedures
8250 that are not in the same section. So we must copy DWARF procedures
8251 along with this type and then rewrite references to them. */
8252 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8253 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8255 if (replacement != NULL)
8256 c = replacement;
8258 else if (c->die_tag == DW_TAG_namespace
8259 || c->die_tag == DW_TAG_class_type
8260 || c->die_tag == DW_TAG_structure_type
8261 || c->die_tag == DW_TAG_union_type)
8263 /* Look for nested types that can be broken out. */
8264 break_out_comdat_types (c);
8266 } while (next != NULL);
8269 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8270 Enter all the cloned children into the hash table decl_table. */
8272 static dw_die_ref
8273 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8275 dw_die_ref c;
8276 dw_die_ref clone;
8277 struct decl_table_entry *entry;
8278 decl_table_entry **slot;
8280 if (die->die_tag == DW_TAG_subprogram)
8281 clone = clone_as_declaration (die);
8282 else
8283 clone = clone_die (die);
8285 slot = decl_table->find_slot_with_hash (die,
8286 htab_hash_pointer (die), INSERT);
8288 /* Assert that DIE isn't in the hash table yet. If it would be there
8289 before, the ancestors would be necessarily there as well, therefore
8290 clone_tree_partial wouldn't be called. */
8291 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8293 entry = XCNEW (struct decl_table_entry);
8294 entry->orig = die;
8295 entry->copy = clone;
8296 *slot = entry;
8298 if (die->die_tag != DW_TAG_subprogram)
8299 FOR_EACH_CHILD (die, c,
8300 add_child_die (clone, clone_tree_partial (c, decl_table)));
8302 return clone;
8305 /* Walk the DIE and its children, looking for references to incomplete
8306 or trivial types that are unmarked (i.e., that are not in the current
8307 type_unit). */
8309 static void
8310 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8312 dw_die_ref c;
8313 dw_attr_node *a;
8314 unsigned ix;
8316 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8318 if (AT_class (a) == dw_val_class_die_ref)
8320 dw_die_ref targ = AT_ref (a);
8321 decl_table_entry **slot;
8322 struct decl_table_entry *entry;
8324 if (targ->die_mark != 0 || targ->comdat_type_p)
8325 continue;
8327 slot = decl_table->find_slot_with_hash (targ,
8328 htab_hash_pointer (targ),
8329 INSERT);
8331 if (*slot != HTAB_EMPTY_ENTRY)
8333 /* TARG has already been copied, so we just need to
8334 modify the reference to point to the copy. */
8335 entry = *slot;
8336 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8338 else
8340 dw_die_ref parent = unit;
8341 dw_die_ref copy = clone_die (targ);
8343 /* Record in DECL_TABLE that TARG has been copied.
8344 Need to do this now, before the recursive call,
8345 because DECL_TABLE may be expanded and SLOT
8346 would no longer be a valid pointer. */
8347 entry = XCNEW (struct decl_table_entry);
8348 entry->orig = targ;
8349 entry->copy = copy;
8350 *slot = entry;
8352 /* If TARG is not a declaration DIE, we need to copy its
8353 children. */
8354 if (!is_declaration_die (targ))
8356 FOR_EACH_CHILD (
8357 targ, c,
8358 add_child_die (copy,
8359 clone_tree_partial (c, decl_table)));
8362 /* Make sure the cloned tree is marked as part of the
8363 type unit. */
8364 mark_dies (copy);
8366 /* If TARG has surrounding context, copy its ancestor tree
8367 into the new type unit. */
8368 if (targ->die_parent != NULL
8369 && !is_unit_die (targ->die_parent))
8370 parent = copy_ancestor_tree (unit, targ->die_parent,
8371 decl_table);
8373 add_child_die (parent, copy);
8374 a->dw_attr_val.v.val_die_ref.die = copy;
8376 /* Make sure the newly-copied DIE is walked. If it was
8377 installed in a previously-added context, it won't
8378 get visited otherwise. */
8379 if (parent != unit)
8381 /* Find the highest point of the newly-added tree,
8382 mark each node along the way, and walk from there. */
8383 parent->die_mark = 1;
8384 while (parent->die_parent
8385 && parent->die_parent->die_mark == 0)
8387 parent = parent->die_parent;
8388 parent->die_mark = 1;
8390 copy_decls_walk (unit, parent, decl_table);
8396 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8399 /* Copy declarations for "unworthy" types into the new comdat section.
8400 Incomplete types, modified types, and certain other types aren't broken
8401 out into comdat sections of their own, so they don't have a signature,
8402 and we need to copy the declaration into the same section so that we
8403 don't have an external reference. */
8405 static void
8406 copy_decls_for_unworthy_types (dw_die_ref unit)
8408 mark_dies (unit);
8409 decl_hash_type decl_table (10);
8410 copy_decls_walk (unit, unit, &decl_table);
8411 unmark_dies (unit);
8414 /* Traverse the DIE and add a sibling attribute if it may have the
8415 effect of speeding up access to siblings. To save some space,
8416 avoid generating sibling attributes for DIE's without children. */
8418 static void
8419 add_sibling_attributes (dw_die_ref die)
8421 dw_die_ref c;
8423 if (! die->die_child)
8424 return;
8426 if (die->die_parent && die != die->die_parent->die_child)
8427 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8429 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8432 /* Output all location lists for the DIE and its children. */
8434 static void
8435 output_location_lists (dw_die_ref die)
8437 dw_die_ref c;
8438 dw_attr_node *a;
8439 unsigned ix;
8441 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8442 if (AT_class (a) == dw_val_class_loc_list)
8443 output_loc_list (AT_loc_list (a));
8445 FOR_EACH_CHILD (die, c, output_location_lists (c));
8448 /* During assign_location_list_indexes and output_loclists_offset the
8449 current index, after it the number of assigned indexes (i.e. how
8450 large the .debug_loclists* offset table should be). */
8451 static unsigned int loc_list_idx;
8453 /* Output all location list offsets for the DIE and its children. */
8455 static void
8456 output_loclists_offsets (dw_die_ref die)
8458 dw_die_ref c;
8459 dw_attr_node *a;
8460 unsigned ix;
8462 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8463 if (AT_class (a) == dw_val_class_loc_list)
8465 dw_loc_list_ref l = AT_loc_list (a);
8466 if (l->offset_emitted)
8467 continue;
8468 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8469 loc_section_label, NULL);
8470 gcc_assert (l->hash == loc_list_idx);
8471 loc_list_idx++;
8472 l->offset_emitted = true;
8475 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8478 /* Recursively set indexes of location lists. */
8480 static void
8481 assign_location_list_indexes (dw_die_ref die)
8483 dw_die_ref c;
8484 dw_attr_node *a;
8485 unsigned ix;
8487 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8488 if (AT_class (a) == dw_val_class_loc_list)
8490 dw_loc_list_ref list = AT_loc_list (a);
8491 if (!list->num_assigned)
8493 list->num_assigned = true;
8494 list->hash = loc_list_idx++;
8498 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8501 /* We want to limit the number of external references, because they are
8502 larger than local references: a relocation takes multiple words, and
8503 even a sig8 reference is always eight bytes, whereas a local reference
8504 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8505 So if we encounter multiple external references to the same type DIE, we
8506 make a local typedef stub for it and redirect all references there.
8508 This is the element of the hash table for keeping track of these
8509 references. */
8511 struct external_ref
8513 dw_die_ref type;
8514 dw_die_ref stub;
8515 unsigned n_refs;
8518 /* Hashtable helpers. */
8520 struct external_ref_hasher : free_ptr_hash <external_ref>
8522 static inline hashval_t hash (const external_ref *);
8523 static inline bool equal (const external_ref *, const external_ref *);
8526 inline hashval_t
8527 external_ref_hasher::hash (const external_ref *r)
8529 dw_die_ref die = r->type;
8530 hashval_t h = 0;
8532 /* We can't use the address of the DIE for hashing, because
8533 that will make the order of the stub DIEs non-deterministic. */
8534 if (! die->comdat_type_p)
8535 /* We have a symbol; use it to compute a hash. */
8536 h = htab_hash_string (die->die_id.die_symbol);
8537 else
8539 /* We have a type signature; use a subset of the bits as the hash.
8540 The 8-byte signature is at least as large as hashval_t. */
8541 comdat_type_node *type_node = die->die_id.die_type_node;
8542 memcpy (&h, type_node->signature, sizeof (h));
8544 return h;
8547 inline bool
8548 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8550 return r1->type == r2->type;
8553 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8555 /* Return a pointer to the external_ref for references to DIE. */
8557 static struct external_ref *
8558 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8560 struct external_ref ref, *ref_p;
8561 external_ref **slot;
8563 ref.type = die;
8564 slot = map->find_slot (&ref, INSERT);
8565 if (*slot != HTAB_EMPTY_ENTRY)
8566 return *slot;
8568 ref_p = XCNEW (struct external_ref);
8569 ref_p->type = die;
8570 *slot = ref_p;
8571 return ref_p;
8574 /* Subroutine of optimize_external_refs, below.
8576 If we see a type skeleton, record it as our stub. If we see external
8577 references, remember how many we've seen. */
8579 static void
8580 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8582 dw_die_ref c;
8583 dw_attr_node *a;
8584 unsigned ix;
8585 struct external_ref *ref_p;
8587 if (is_type_die (die)
8588 && (c = get_AT_ref (die, DW_AT_signature)))
8590 /* This is a local skeleton; use it for local references. */
8591 ref_p = lookup_external_ref (map, c);
8592 ref_p->stub = die;
8595 /* Scan the DIE references, and remember any that refer to DIEs from
8596 other CUs (i.e. those which are not marked). */
8597 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8598 if (AT_class (a) == dw_val_class_die_ref
8599 && (c = AT_ref (a))->die_mark == 0
8600 && is_type_die (c))
8602 ref_p = lookup_external_ref (map, c);
8603 ref_p->n_refs++;
8606 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8609 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8610 points to an external_ref, DATA is the CU we're processing. If we don't
8611 already have a local stub, and we have multiple refs, build a stub. */
8614 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8616 struct external_ref *ref_p = *slot;
8618 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8620 /* We have multiple references to this type, so build a small stub.
8621 Both of these forms are a bit dodgy from the perspective of the
8622 DWARF standard, since technically they should have names. */
8623 dw_die_ref cu = data;
8624 dw_die_ref type = ref_p->type;
8625 dw_die_ref stub = NULL;
8627 if (type->comdat_type_p)
8629 /* If we refer to this type via sig8, use AT_signature. */
8630 stub = new_die (type->die_tag, cu, NULL_TREE);
8631 add_AT_die_ref (stub, DW_AT_signature, type);
8633 else
8635 /* Otherwise, use a typedef with no name. */
8636 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8637 add_AT_die_ref (stub, DW_AT_type, type);
8640 stub->die_mark++;
8641 ref_p->stub = stub;
8643 return 1;
8646 /* DIE is a unit; look through all the DIE references to see if there are
8647 any external references to types, and if so, create local stubs for
8648 them which will be applied in build_abbrev_table. This is useful because
8649 references to local DIEs are smaller. */
8651 static external_ref_hash_type *
8652 optimize_external_refs (dw_die_ref die)
8654 external_ref_hash_type *map = new external_ref_hash_type (10);
8655 optimize_external_refs_1 (die, map);
8656 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8657 return map;
8660 /* The following 3 variables are temporaries that are computed only during the
8661 build_abbrev_table call and used and released during the following
8662 optimize_abbrev_table call. */
8664 /* First abbrev_id that can be optimized based on usage. */
8665 static unsigned int abbrev_opt_start;
8667 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8668 abbrev_id smaller than this, because they must be already sized
8669 during build_abbrev_table). */
8670 static unsigned int abbrev_opt_base_type_end;
8672 /* Vector of usage counts during build_abbrev_table. Indexed by
8673 abbrev_id - abbrev_opt_start. */
8674 static vec<unsigned int> abbrev_usage_count;
8676 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8677 static vec<dw_die_ref> sorted_abbrev_dies;
8679 /* The format of each DIE (and its attribute value pairs) is encoded in an
8680 abbreviation table. This routine builds the abbreviation table and assigns
8681 a unique abbreviation id for each abbreviation entry. The children of each
8682 die are visited recursively. */
8684 static void
8685 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8687 unsigned int abbrev_id = 0;
8688 dw_die_ref c;
8689 dw_attr_node *a;
8690 unsigned ix;
8691 dw_die_ref abbrev;
8693 /* Scan the DIE references, and replace any that refer to
8694 DIEs from other CUs (i.e. those which are not marked) with
8695 the local stubs we built in optimize_external_refs. */
8696 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8697 if (AT_class (a) == dw_val_class_die_ref
8698 && (c = AT_ref (a))->die_mark == 0)
8700 struct external_ref *ref_p;
8701 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8703 ref_p = lookup_external_ref (extern_map, c);
8704 if (ref_p->stub && ref_p->stub != die)
8705 change_AT_die_ref (a, ref_p->stub);
8706 else
8707 /* We aren't changing this reference, so mark it external. */
8708 set_AT_ref_external (a, 1);
8711 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8713 dw_attr_node *die_a, *abbrev_a;
8714 unsigned ix;
8715 bool ok = true;
8717 if (abbrev_id == 0)
8718 continue;
8719 if (abbrev->die_tag != die->die_tag)
8720 continue;
8721 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8722 continue;
8724 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8725 continue;
8727 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8729 abbrev_a = &(*abbrev->die_attr)[ix];
8730 if ((abbrev_a->dw_attr != die_a->dw_attr)
8731 || (value_format (abbrev_a) != value_format (die_a)))
8733 ok = false;
8734 break;
8737 if (ok)
8738 break;
8741 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8743 vec_safe_push (abbrev_die_table, die);
8744 if (abbrev_opt_start)
8745 abbrev_usage_count.safe_push (0);
8747 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8749 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8750 sorted_abbrev_dies.safe_push (die);
8753 die->die_abbrev = abbrev_id;
8754 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8757 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8758 by die_abbrev's usage count, from the most commonly used
8759 abbreviation to the least. */
8761 static int
8762 die_abbrev_cmp (const void *p1, const void *p2)
8764 dw_die_ref die1 = *(const dw_die_ref *) p1;
8765 dw_die_ref die2 = *(const dw_die_ref *) p2;
8767 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8768 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8770 if (die1->die_abbrev >= abbrev_opt_base_type_end
8771 && die2->die_abbrev >= abbrev_opt_base_type_end)
8773 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8774 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8775 return -1;
8776 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8777 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8778 return 1;
8781 /* Stabilize the sort. */
8782 if (die1->die_abbrev < die2->die_abbrev)
8783 return -1;
8784 if (die1->die_abbrev > die2->die_abbrev)
8785 return 1;
8787 return 0;
8790 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8791 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8792 into dw_val_class_const_implicit or
8793 dw_val_class_unsigned_const_implicit. */
8795 static void
8796 optimize_implicit_const (unsigned int first_id, unsigned int end,
8797 vec<bool> &implicit_consts)
8799 /* It never makes sense if there is just one DIE using the abbreviation. */
8800 if (end < first_id + 2)
8801 return;
8803 dw_attr_node *a;
8804 unsigned ix, i;
8805 dw_die_ref die = sorted_abbrev_dies[first_id];
8806 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8807 if (implicit_consts[ix])
8809 enum dw_val_class new_class = dw_val_class_none;
8810 switch (AT_class (a))
8812 case dw_val_class_unsigned_const:
8813 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8814 continue;
8816 /* The .debug_abbrev section will grow by
8817 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8818 in all the DIEs using that abbreviation. */
8819 if (constant_size (AT_unsigned (a)) * (end - first_id)
8820 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8821 continue;
8823 new_class = dw_val_class_unsigned_const_implicit;
8824 break;
8826 case dw_val_class_const:
8827 new_class = dw_val_class_const_implicit;
8828 break;
8830 case dw_val_class_file:
8831 new_class = dw_val_class_file_implicit;
8832 break;
8834 default:
8835 continue;
8837 for (i = first_id; i < end; i++)
8838 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8839 = new_class;
8843 /* Attempt to optimize abbreviation table from abbrev_opt_start
8844 abbreviation above. */
8846 static void
8847 optimize_abbrev_table (void)
8849 if (abbrev_opt_start
8850 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8851 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8853 auto_vec<bool, 32> implicit_consts;
8854 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8856 unsigned int abbrev_id = abbrev_opt_start - 1;
8857 unsigned int first_id = ~0U;
8858 unsigned int last_abbrev_id = 0;
8859 unsigned int i;
8860 dw_die_ref die;
8861 if (abbrev_opt_base_type_end > abbrev_opt_start)
8862 abbrev_id = abbrev_opt_base_type_end - 1;
8863 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8864 most commonly used abbreviations come first. */
8865 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8867 dw_attr_node *a;
8868 unsigned ix;
8870 /* If calc_base_type_die_sizes has been called, the CU and
8871 base types after it can't be optimized, because we've already
8872 calculated their DIE offsets. We've sorted them first. */
8873 if (die->die_abbrev < abbrev_opt_base_type_end)
8874 continue;
8875 if (die->die_abbrev != last_abbrev_id)
8877 last_abbrev_id = die->die_abbrev;
8878 if (dwarf_version >= 5 && first_id != ~0U)
8879 optimize_implicit_const (first_id, i, implicit_consts);
8880 abbrev_id++;
8881 (*abbrev_die_table)[abbrev_id] = die;
8882 if (dwarf_version >= 5)
8884 first_id = i;
8885 implicit_consts.truncate (0);
8887 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8888 switch (AT_class (a))
8890 case dw_val_class_const:
8891 case dw_val_class_unsigned_const:
8892 case dw_val_class_file:
8893 implicit_consts.safe_push (true);
8894 break;
8895 default:
8896 implicit_consts.safe_push (false);
8897 break;
8901 else if (dwarf_version >= 5)
8903 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8904 if (!implicit_consts[ix])
8905 continue;
8906 else
8908 dw_attr_node *other_a
8909 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8910 if (!dw_val_equal_p (&a->dw_attr_val,
8911 &other_a->dw_attr_val))
8912 implicit_consts[ix] = false;
8915 die->die_abbrev = abbrev_id;
8917 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8918 if (dwarf_version >= 5 && first_id != ~0U)
8919 optimize_implicit_const (first_id, i, implicit_consts);
8922 abbrev_opt_start = 0;
8923 abbrev_opt_base_type_end = 0;
8924 abbrev_usage_count.release ();
8925 sorted_abbrev_dies.release ();
8928 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8930 static int
8931 constant_size (unsigned HOST_WIDE_INT value)
8933 int log;
8935 if (value == 0)
8936 log = 0;
8937 else
8938 log = floor_log2 (value);
8940 log = log / 8;
8941 log = 1 << (floor_log2 (log) + 1);
8943 return log;
8946 /* Return the size of a DIE as it is represented in the
8947 .debug_info section. */
8949 static unsigned long
8950 size_of_die (dw_die_ref die)
8952 unsigned long size = 0;
8953 dw_attr_node *a;
8954 unsigned ix;
8955 enum dwarf_form form;
8957 size += size_of_uleb128 (die->die_abbrev);
8958 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8960 switch (AT_class (a))
8962 case dw_val_class_addr:
8963 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8965 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8966 size += size_of_uleb128 (AT_index (a));
8968 else
8969 size += DWARF2_ADDR_SIZE;
8970 break;
8971 case dw_val_class_offset:
8972 size += DWARF_OFFSET_SIZE;
8973 break;
8974 case dw_val_class_loc:
8976 unsigned long lsize = size_of_locs (AT_loc (a));
8978 /* Block length. */
8979 if (dwarf_version >= 4)
8980 size += size_of_uleb128 (lsize);
8981 else
8982 size += constant_size (lsize);
8983 size += lsize;
8985 break;
8986 case dw_val_class_loc_list:
8987 if (dwarf_split_debug_info && dwarf_version >= 5)
8989 gcc_assert (AT_loc_list (a)->num_assigned);
8990 size += size_of_uleb128 (AT_loc_list (a)->hash);
8992 else
8993 size += DWARF_OFFSET_SIZE;
8994 break;
8995 case dw_val_class_range_list:
8996 if (value_format (a) == DW_FORM_rnglistx)
8998 gcc_assert (rnglist_idx);
8999 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9000 size += size_of_uleb128 (r->idx);
9002 else
9003 size += DWARF_OFFSET_SIZE;
9004 break;
9005 case dw_val_class_const:
9006 size += size_of_sleb128 (AT_int (a));
9007 break;
9008 case dw_val_class_unsigned_const:
9010 int csize = constant_size (AT_unsigned (a));
9011 if (dwarf_version == 3
9012 && a->dw_attr == DW_AT_data_member_location
9013 && csize >= 4)
9014 size += size_of_uleb128 (AT_unsigned (a));
9015 else
9016 size += csize;
9018 break;
9019 case dw_val_class_const_implicit:
9020 case dw_val_class_unsigned_const_implicit:
9021 case dw_val_class_file_implicit:
9022 /* These occupy no size in the DIE, just an extra sleb128 in
9023 .debug_abbrev. */
9024 break;
9025 case dw_val_class_const_double:
9026 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9027 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9028 size++; /* block */
9029 break;
9030 case dw_val_class_wide_int:
9031 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9032 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9033 if (get_full_len (*a->dw_attr_val.v.val_wide)
9034 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9035 size++; /* block */
9036 break;
9037 case dw_val_class_vec:
9038 size += constant_size (a->dw_attr_val.v.val_vec.length
9039 * a->dw_attr_val.v.val_vec.elt_size)
9040 + a->dw_attr_val.v.val_vec.length
9041 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9042 break;
9043 case dw_val_class_flag:
9044 if (dwarf_version >= 4)
9045 /* Currently all add_AT_flag calls pass in 1 as last argument,
9046 so DW_FORM_flag_present can be used. If that ever changes,
9047 we'll need to use DW_FORM_flag and have some optimization
9048 in build_abbrev_table that will change those to
9049 DW_FORM_flag_present if it is set to 1 in all DIEs using
9050 the same abbrev entry. */
9051 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9052 else
9053 size += 1;
9054 break;
9055 case dw_val_class_die_ref:
9056 if (AT_ref_external (a))
9058 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9059 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9060 is sized by target address length, whereas in DWARF3
9061 it's always sized as an offset. */
9062 if (use_debug_types)
9063 size += DWARF_TYPE_SIGNATURE_SIZE;
9064 else if (dwarf_version == 2)
9065 size += DWARF2_ADDR_SIZE;
9066 else
9067 size += DWARF_OFFSET_SIZE;
9069 else
9070 size += DWARF_OFFSET_SIZE;
9071 break;
9072 case dw_val_class_fde_ref:
9073 size += DWARF_OFFSET_SIZE;
9074 break;
9075 case dw_val_class_lbl_id:
9076 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9078 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9079 size += size_of_uleb128 (AT_index (a));
9081 else
9082 size += DWARF2_ADDR_SIZE;
9083 break;
9084 case dw_val_class_lineptr:
9085 case dw_val_class_macptr:
9086 case dw_val_class_loclistsptr:
9087 size += DWARF_OFFSET_SIZE;
9088 break;
9089 case dw_val_class_str:
9090 form = AT_string_form (a);
9091 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9092 size += DWARF_OFFSET_SIZE;
9093 else if (form == DW_FORM_GNU_str_index)
9094 size += size_of_uleb128 (AT_index (a));
9095 else
9096 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9097 break;
9098 case dw_val_class_file:
9099 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9100 break;
9101 case dw_val_class_data8:
9102 size += 8;
9103 break;
9104 case dw_val_class_vms_delta:
9105 size += DWARF_OFFSET_SIZE;
9106 break;
9107 case dw_val_class_high_pc:
9108 size += DWARF2_ADDR_SIZE;
9109 break;
9110 case dw_val_class_discr_value:
9111 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9112 break;
9113 case dw_val_class_discr_list:
9115 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9117 /* This is a block, so we have the block length and then its
9118 data. */
9119 size += constant_size (block_size) + block_size;
9121 break;
9122 default:
9123 gcc_unreachable ();
9127 return size;
9130 /* Size the debugging information associated with a given DIE. Visits the
9131 DIE's children recursively. Updates the global variable next_die_offset, on
9132 each time through. Uses the current value of next_die_offset to update the
9133 die_offset field in each DIE. */
9135 static void
9136 calc_die_sizes (dw_die_ref die)
9138 dw_die_ref c;
9140 gcc_assert (die->die_offset == 0
9141 || (unsigned long int) die->die_offset == next_die_offset);
9142 die->die_offset = next_die_offset;
9143 next_die_offset += size_of_die (die);
9145 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9147 if (die->die_child != NULL)
9148 /* Count the null byte used to terminate sibling lists. */
9149 next_die_offset += 1;
9152 /* Size just the base type children at the start of the CU.
9153 This is needed because build_abbrev needs to size locs
9154 and sizing of type based stack ops needs to know die_offset
9155 values for the base types. */
9157 static void
9158 calc_base_type_die_sizes (void)
9160 unsigned long die_offset = (dwarf_split_debug_info
9161 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9162 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9163 unsigned int i;
9164 dw_die_ref base_type;
9165 #if ENABLE_ASSERT_CHECKING
9166 dw_die_ref prev = comp_unit_die ()->die_child;
9167 #endif
9169 die_offset += size_of_die (comp_unit_die ());
9170 for (i = 0; base_types.iterate (i, &base_type); i++)
9172 #if ENABLE_ASSERT_CHECKING
9173 gcc_assert (base_type->die_offset == 0
9174 && prev->die_sib == base_type
9175 && base_type->die_child == NULL
9176 && base_type->die_abbrev);
9177 prev = base_type;
9178 #endif
9179 if (abbrev_opt_start
9180 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9181 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9182 base_type->die_offset = die_offset;
9183 die_offset += size_of_die (base_type);
9187 /* Set the marks for a die and its children. We do this so
9188 that we know whether or not a reference needs to use FORM_ref_addr; only
9189 DIEs in the same CU will be marked. We used to clear out the offset
9190 and use that as the flag, but ran into ordering problems. */
9192 static void
9193 mark_dies (dw_die_ref die)
9195 dw_die_ref c;
9197 gcc_assert (!die->die_mark);
9199 die->die_mark = 1;
9200 FOR_EACH_CHILD (die, c, mark_dies (c));
9203 /* Clear the marks for a die and its children. */
9205 static void
9206 unmark_dies (dw_die_ref die)
9208 dw_die_ref c;
9210 if (! use_debug_types)
9211 gcc_assert (die->die_mark);
9213 die->die_mark = 0;
9214 FOR_EACH_CHILD (die, c, unmark_dies (c));
9217 /* Clear the marks for a die, its children and referred dies. */
9219 static void
9220 unmark_all_dies (dw_die_ref die)
9222 dw_die_ref c;
9223 dw_attr_node *a;
9224 unsigned ix;
9226 if (!die->die_mark)
9227 return;
9228 die->die_mark = 0;
9230 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9232 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9233 if (AT_class (a) == dw_val_class_die_ref)
9234 unmark_all_dies (AT_ref (a));
9237 /* Calculate if the entry should appear in the final output file. It may be
9238 from a pruned a type. */
9240 static bool
9241 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9243 /* By limiting gnu pubnames to definitions only, gold can generate a
9244 gdb index without entries for declarations, which don't include
9245 enough information to be useful. */
9246 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9247 return false;
9249 if (table == pubname_table)
9251 /* Enumerator names are part of the pubname table, but the
9252 parent DW_TAG_enumeration_type die may have been pruned.
9253 Don't output them if that is the case. */
9254 if (p->die->die_tag == DW_TAG_enumerator &&
9255 (p->die->die_parent == NULL
9256 || !p->die->die_parent->die_perennial_p))
9257 return false;
9259 /* Everything else in the pubname table is included. */
9260 return true;
9263 /* The pubtypes table shouldn't include types that have been
9264 pruned. */
9265 return (p->die->die_offset != 0
9266 || !flag_eliminate_unused_debug_types);
9269 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9270 generated for the compilation unit. */
9272 static unsigned long
9273 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9275 unsigned long size;
9276 unsigned i;
9277 pubname_entry *p;
9278 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9280 size = DWARF_PUBNAMES_HEADER_SIZE;
9281 FOR_EACH_VEC_ELT (*names, i, p)
9282 if (include_pubname_in_output (names, p))
9283 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9285 size += DWARF_OFFSET_SIZE;
9286 return size;
9289 /* Return the size of the information in the .debug_aranges section. */
9291 static unsigned long
9292 size_of_aranges (void)
9294 unsigned long size;
9296 size = DWARF_ARANGES_HEADER_SIZE;
9298 /* Count the address/length pair for this compilation unit. */
9299 if (text_section_used)
9300 size += 2 * DWARF2_ADDR_SIZE;
9301 if (cold_text_section_used)
9302 size += 2 * DWARF2_ADDR_SIZE;
9303 if (have_multiple_function_sections)
9305 unsigned fde_idx;
9306 dw_fde_ref fde;
9308 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9310 if (DECL_IGNORED_P (fde->decl))
9311 continue;
9312 if (!fde->in_std_section)
9313 size += 2 * DWARF2_ADDR_SIZE;
9314 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9315 size += 2 * DWARF2_ADDR_SIZE;
9319 /* Count the two zero words used to terminated the address range table. */
9320 size += 2 * DWARF2_ADDR_SIZE;
9321 return size;
9324 /* Select the encoding of an attribute value. */
9326 static enum dwarf_form
9327 value_format (dw_attr_node *a)
9329 switch (AT_class (a))
9331 case dw_val_class_addr:
9332 /* Only very few attributes allow DW_FORM_addr. */
9333 switch (a->dw_attr)
9335 case DW_AT_low_pc:
9336 case DW_AT_high_pc:
9337 case DW_AT_entry_pc:
9338 case DW_AT_trampoline:
9339 return (AT_index (a) == NOT_INDEXED
9340 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9341 default:
9342 break;
9344 switch (DWARF2_ADDR_SIZE)
9346 case 1:
9347 return DW_FORM_data1;
9348 case 2:
9349 return DW_FORM_data2;
9350 case 4:
9351 return DW_FORM_data4;
9352 case 8:
9353 return DW_FORM_data8;
9354 default:
9355 gcc_unreachable ();
9357 case dw_val_class_loc_list:
9358 if (dwarf_split_debug_info
9359 && dwarf_version >= 5
9360 && AT_loc_list (a)->num_assigned)
9361 return DW_FORM_loclistx;
9362 /* FALLTHRU */
9363 case dw_val_class_range_list:
9364 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9365 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9366 care about sizes of .debug* sections in shared libraries and
9367 executables and don't take into account relocations that affect just
9368 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9369 table in the .debug_rnglists section. */
9370 if (dwarf_split_debug_info
9371 && dwarf_version >= 5
9372 && AT_class (a) == dw_val_class_range_list
9373 && rnglist_idx
9374 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9375 return DW_FORM_rnglistx;
9376 if (dwarf_version >= 4)
9377 return DW_FORM_sec_offset;
9378 /* FALLTHRU */
9379 case dw_val_class_vms_delta:
9380 case dw_val_class_offset:
9381 switch (DWARF_OFFSET_SIZE)
9383 case 4:
9384 return DW_FORM_data4;
9385 case 8:
9386 return DW_FORM_data8;
9387 default:
9388 gcc_unreachable ();
9390 case dw_val_class_loc:
9391 if (dwarf_version >= 4)
9392 return DW_FORM_exprloc;
9393 switch (constant_size (size_of_locs (AT_loc (a))))
9395 case 1:
9396 return DW_FORM_block1;
9397 case 2:
9398 return DW_FORM_block2;
9399 case 4:
9400 return DW_FORM_block4;
9401 default:
9402 gcc_unreachable ();
9404 case dw_val_class_const:
9405 return DW_FORM_sdata;
9406 case dw_val_class_unsigned_const:
9407 switch (constant_size (AT_unsigned (a)))
9409 case 1:
9410 return DW_FORM_data1;
9411 case 2:
9412 return DW_FORM_data2;
9413 case 4:
9414 /* In DWARF3 DW_AT_data_member_location with
9415 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9416 constant, so we need to use DW_FORM_udata if we need
9417 a large constant. */
9418 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9419 return DW_FORM_udata;
9420 return DW_FORM_data4;
9421 case 8:
9422 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9423 return DW_FORM_udata;
9424 return DW_FORM_data8;
9425 default:
9426 gcc_unreachable ();
9428 case dw_val_class_const_implicit:
9429 case dw_val_class_unsigned_const_implicit:
9430 case dw_val_class_file_implicit:
9431 return DW_FORM_implicit_const;
9432 case dw_val_class_const_double:
9433 switch (HOST_BITS_PER_WIDE_INT)
9435 case 8:
9436 return DW_FORM_data2;
9437 case 16:
9438 return DW_FORM_data4;
9439 case 32:
9440 return DW_FORM_data8;
9441 case 64:
9442 if (dwarf_version >= 5)
9443 return DW_FORM_data16;
9444 /* FALLTHRU */
9445 default:
9446 return DW_FORM_block1;
9448 case dw_val_class_wide_int:
9449 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9451 case 8:
9452 return DW_FORM_data1;
9453 case 16:
9454 return DW_FORM_data2;
9455 case 32:
9456 return DW_FORM_data4;
9457 case 64:
9458 return DW_FORM_data8;
9459 case 128:
9460 if (dwarf_version >= 5)
9461 return DW_FORM_data16;
9462 /* FALLTHRU */
9463 default:
9464 return DW_FORM_block1;
9466 case dw_val_class_vec:
9467 switch (constant_size (a->dw_attr_val.v.val_vec.length
9468 * a->dw_attr_val.v.val_vec.elt_size))
9470 case 1:
9471 return DW_FORM_block1;
9472 case 2:
9473 return DW_FORM_block2;
9474 case 4:
9475 return DW_FORM_block4;
9476 default:
9477 gcc_unreachable ();
9479 case dw_val_class_flag:
9480 if (dwarf_version >= 4)
9482 /* Currently all add_AT_flag calls pass in 1 as last argument,
9483 so DW_FORM_flag_present can be used. If that ever changes,
9484 we'll need to use DW_FORM_flag and have some optimization
9485 in build_abbrev_table that will change those to
9486 DW_FORM_flag_present if it is set to 1 in all DIEs using
9487 the same abbrev entry. */
9488 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9489 return DW_FORM_flag_present;
9491 return DW_FORM_flag;
9492 case dw_val_class_die_ref:
9493 if (AT_ref_external (a))
9494 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9495 else
9496 return DW_FORM_ref;
9497 case dw_val_class_fde_ref:
9498 return DW_FORM_data;
9499 case dw_val_class_lbl_id:
9500 return (AT_index (a) == NOT_INDEXED
9501 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9502 case dw_val_class_lineptr:
9503 case dw_val_class_macptr:
9504 case dw_val_class_loclistsptr:
9505 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9506 case dw_val_class_str:
9507 return AT_string_form (a);
9508 case dw_val_class_file:
9509 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9511 case 1:
9512 return DW_FORM_data1;
9513 case 2:
9514 return DW_FORM_data2;
9515 case 4:
9516 return DW_FORM_data4;
9517 default:
9518 gcc_unreachable ();
9521 case dw_val_class_data8:
9522 return DW_FORM_data8;
9524 case dw_val_class_high_pc:
9525 switch (DWARF2_ADDR_SIZE)
9527 case 1:
9528 return DW_FORM_data1;
9529 case 2:
9530 return DW_FORM_data2;
9531 case 4:
9532 return DW_FORM_data4;
9533 case 8:
9534 return DW_FORM_data8;
9535 default:
9536 gcc_unreachable ();
9539 case dw_val_class_discr_value:
9540 return (a->dw_attr_val.v.val_discr_value.pos
9541 ? DW_FORM_udata
9542 : DW_FORM_sdata);
9543 case dw_val_class_discr_list:
9544 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9546 case 1:
9547 return DW_FORM_block1;
9548 case 2:
9549 return DW_FORM_block2;
9550 case 4:
9551 return DW_FORM_block4;
9552 default:
9553 gcc_unreachable ();
9556 default:
9557 gcc_unreachable ();
9561 /* Output the encoding of an attribute value. */
9563 static void
9564 output_value_format (dw_attr_node *a)
9566 enum dwarf_form form = value_format (a);
9568 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9571 /* Given a die and id, produce the appropriate abbreviations. */
9573 static void
9574 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9576 unsigned ix;
9577 dw_attr_node *a_attr;
9579 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9580 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9581 dwarf_tag_name (abbrev->die_tag));
9583 if (abbrev->die_child != NULL)
9584 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9585 else
9586 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9588 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9590 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9591 dwarf_attr_name (a_attr->dw_attr));
9592 output_value_format (a_attr);
9593 if (value_format (a_attr) == DW_FORM_implicit_const)
9595 if (AT_class (a_attr) == dw_val_class_file_implicit)
9597 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9598 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9599 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9601 else
9602 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9606 dw2_asm_output_data (1, 0, NULL);
9607 dw2_asm_output_data (1, 0, NULL);
9611 /* Output the .debug_abbrev section which defines the DIE abbreviation
9612 table. */
9614 static void
9615 output_abbrev_section (void)
9617 unsigned int abbrev_id;
9618 dw_die_ref abbrev;
9620 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9621 if (abbrev_id != 0)
9622 output_die_abbrevs (abbrev_id, abbrev);
9624 /* Terminate the table. */
9625 dw2_asm_output_data (1, 0, NULL);
9628 /* Output a symbol we can use to refer to this DIE from another CU. */
9630 static inline void
9631 output_die_symbol (dw_die_ref die)
9633 const char *sym = die->die_id.die_symbol;
9635 gcc_assert (!die->comdat_type_p);
9637 if (sym == 0)
9638 return;
9640 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9641 /* We make these global, not weak; if the target doesn't support
9642 .linkonce, it doesn't support combining the sections, so debugging
9643 will break. */
9644 targetm.asm_out.globalize_label (asm_out_file, sym);
9646 ASM_OUTPUT_LABEL (asm_out_file, sym);
9649 /* Return a new location list, given the begin and end range, and the
9650 expression. */
9652 static inline dw_loc_list_ref
9653 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9654 const char *section)
9656 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9658 retlist->begin = begin;
9659 retlist->begin_entry = NULL;
9660 retlist->end = end;
9661 retlist->expr = expr;
9662 retlist->section = section;
9664 return retlist;
9667 /* Generate a new internal symbol for this location list node, if it
9668 hasn't got one yet. */
9670 static inline void
9671 gen_llsym (dw_loc_list_ref list)
9673 gcc_assert (!list->ll_symbol);
9674 list->ll_symbol = gen_internal_sym ("LLST");
9677 /* Output the location list given to us. */
9679 static void
9680 output_loc_list (dw_loc_list_ref list_head)
9682 if (list_head->emitted)
9683 return;
9684 list_head->emitted = true;
9686 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9688 dw_loc_list_ref curr = list_head;
9689 const char *last_section = NULL;
9690 const char *base_label = NULL;
9692 /* Walk the location list, and output each range + expression. */
9693 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9695 unsigned long size;
9696 /* Don't output an entry that starts and ends at the same address. */
9697 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9698 continue;
9699 size = size_of_locs (curr->expr);
9700 /* If the expression is too large, drop it on the floor. We could
9701 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9702 in the expression, but >= 64KB expressions for a single value
9703 in a single range are unlikely very useful. */
9704 if (dwarf_version < 5 && size > 0xffff)
9705 continue;
9706 if (dwarf_version >= 5)
9708 if (dwarf_split_debug_info)
9710 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9711 uleb128 index into .debug_addr and uleb128 length. */
9712 dw2_asm_output_data (1, DW_LLE_startx_length,
9713 "DW_LLE_startx_length (%s)",
9714 list_head->ll_symbol);
9715 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9716 "Location list range start index "
9717 "(%s)", curr->begin);
9718 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9719 For that case we probably need to emit DW_LLE_startx_endx,
9720 but we'd need 2 .debug_addr entries rather than just one. */
9721 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9722 "Location list length (%s)",
9723 list_head->ll_symbol);
9725 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9727 /* If all code is in .text section, the base address is
9728 already provided by the CU attributes. Use
9729 DW_LLE_offset_pair where both addresses are uleb128 encoded
9730 offsets against that base. */
9731 dw2_asm_output_data (1, DW_LLE_offset_pair,
9732 "DW_LLE_offset_pair (%s)",
9733 list_head->ll_symbol);
9734 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9735 "Location list begin address (%s)",
9736 list_head->ll_symbol);
9737 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9738 "Location list end address (%s)",
9739 list_head->ll_symbol);
9741 else if (HAVE_AS_LEB128)
9743 /* Otherwise, find out how many consecutive entries could share
9744 the same base entry. If just one, emit DW_LLE_start_length,
9745 otherwise emit DW_LLE_base_address for the base address
9746 followed by a series of DW_LLE_offset_pair. */
9747 if (last_section == NULL || curr->section != last_section)
9749 dw_loc_list_ref curr2;
9750 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9751 curr2 = curr2->dw_loc_next)
9753 if (strcmp (curr2->begin, curr2->end) == 0
9754 && !curr2->force)
9755 continue;
9756 break;
9758 if (curr2 == NULL || curr->section != curr2->section)
9759 last_section = NULL;
9760 else
9762 last_section = curr->section;
9763 base_label = curr->begin;
9764 dw2_asm_output_data (1, DW_LLE_base_address,
9765 "DW_LLE_base_address (%s)",
9766 list_head->ll_symbol);
9767 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9768 "Base address (%s)",
9769 list_head->ll_symbol);
9772 /* Only one entry with the same base address. Use
9773 DW_LLE_start_length with absolute address and uleb128
9774 length. */
9775 if (last_section == NULL)
9777 dw2_asm_output_data (1, DW_LLE_start_length,
9778 "DW_LLE_start_length (%s)",
9779 list_head->ll_symbol);
9780 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9781 "Location list begin address (%s)",
9782 list_head->ll_symbol);
9783 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9784 "Location list length "
9785 "(%s)", list_head->ll_symbol);
9787 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9788 DW_LLE_base_address. */
9789 else
9791 dw2_asm_output_data (1, DW_LLE_offset_pair,
9792 "DW_LLE_offset_pair (%s)",
9793 list_head->ll_symbol);
9794 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9795 "Location list begin address "
9796 "(%s)", list_head->ll_symbol);
9797 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9798 "Location list end address "
9799 "(%s)", list_head->ll_symbol);
9802 /* The assembler does not support .uleb128 directive. Emit
9803 DW_LLE_start_end with a pair of absolute addresses. */
9804 else
9806 dw2_asm_output_data (1, DW_LLE_start_end,
9807 "DW_LLE_start_end (%s)",
9808 list_head->ll_symbol);
9809 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9810 "Location list begin address (%s)",
9811 list_head->ll_symbol);
9812 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9813 "Location list end address (%s)",
9814 list_head->ll_symbol);
9817 else if (dwarf_split_debug_info)
9819 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9820 and 4 byte length. */
9821 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9822 "Location list start/length entry (%s)",
9823 list_head->ll_symbol);
9824 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9825 "Location list range start index (%s)",
9826 curr->begin);
9827 /* The length field is 4 bytes. If we ever need to support
9828 an 8-byte length, we can add a new DW_LLE code or fall back
9829 to DW_LLE_GNU_start_end_entry. */
9830 dw2_asm_output_delta (4, curr->end, curr->begin,
9831 "Location list range length (%s)",
9832 list_head->ll_symbol);
9834 else if (!have_multiple_function_sections)
9836 /* Pair of relative addresses against start of text section. */
9837 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9838 "Location list begin address (%s)",
9839 list_head->ll_symbol);
9840 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9841 "Location list end address (%s)",
9842 list_head->ll_symbol);
9844 else
9846 /* Pair of absolute addresses. */
9847 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9848 "Location list begin address (%s)",
9849 list_head->ll_symbol);
9850 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9851 "Location list end address (%s)",
9852 list_head->ll_symbol);
9855 /* Output the block length for this list of location operations. */
9856 if (dwarf_version >= 5)
9857 dw2_asm_output_data_uleb128 (size, "Location expression size");
9858 else
9860 gcc_assert (size <= 0xffff);
9861 dw2_asm_output_data (2, size, "Location expression size");
9864 output_loc_sequence (curr->expr, -1);
9867 /* And finally list termination. */
9868 if (dwarf_version >= 5)
9869 dw2_asm_output_data (1, DW_LLE_end_of_list,
9870 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9871 else if (dwarf_split_debug_info)
9872 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9873 "Location list terminator (%s)",
9874 list_head->ll_symbol);
9875 else
9877 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9878 "Location list terminator begin (%s)",
9879 list_head->ll_symbol);
9880 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9881 "Location list terminator end (%s)",
9882 list_head->ll_symbol);
9886 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9887 section. Emit a relocated reference if val_entry is NULL, otherwise,
9888 emit an indirect reference. */
9890 static void
9891 output_range_list_offset (dw_attr_node *a)
9893 const char *name = dwarf_attr_name (a->dw_attr);
9895 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9897 if (dwarf_version >= 5)
9899 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9901 debug_ranges_section, "%s", name);
9903 else
9905 char *p = strchr (ranges_section_label, '\0');
9906 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9907 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9908 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9909 debug_ranges_section, "%s", name);
9910 *p = '\0';
9913 else if (dwarf_version >= 5)
9915 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9916 gcc_assert (rnglist_idx);
9917 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9919 else
9920 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9921 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9922 "%s (offset from %s)", name, ranges_section_label);
9925 /* Output the offset into the debug_loc section. */
9927 static void
9928 output_loc_list_offset (dw_attr_node *a)
9930 char *sym = AT_loc_list (a)->ll_symbol;
9932 gcc_assert (sym);
9933 if (!dwarf_split_debug_info)
9934 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9935 "%s", dwarf_attr_name (a->dw_attr));
9936 else if (dwarf_version >= 5)
9938 gcc_assert (AT_loc_list (a)->num_assigned);
9939 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9940 dwarf_attr_name (a->dw_attr),
9941 sym);
9943 else
9944 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9945 "%s", dwarf_attr_name (a->dw_attr));
9948 /* Output an attribute's index or value appropriately. */
9950 static void
9951 output_attr_index_or_value (dw_attr_node *a)
9953 const char *name = dwarf_attr_name (a->dw_attr);
9955 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9957 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9958 return;
9960 switch (AT_class (a))
9962 case dw_val_class_addr:
9963 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9964 break;
9965 case dw_val_class_high_pc:
9966 case dw_val_class_lbl_id:
9967 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9968 break;
9969 default:
9970 gcc_unreachable ();
9974 /* Output a type signature. */
9976 static inline void
9977 output_signature (const char *sig, const char *name)
9979 int i;
9981 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9982 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9985 /* Output a discriminant value. */
9987 static inline void
9988 output_discr_value (dw_discr_value *discr_value, const char *name)
9990 if (discr_value->pos)
9991 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9992 else
9993 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9996 /* Output the DIE and its attributes. Called recursively to generate
9997 the definitions of each child DIE. */
9999 static void
10000 output_die (dw_die_ref die)
10002 dw_attr_node *a;
10003 dw_die_ref c;
10004 unsigned long size;
10005 unsigned ix;
10007 /* If someone in another CU might refer to us, set up a symbol for
10008 them to point to. */
10009 if (! die->comdat_type_p && die->die_id.die_symbol
10010 /* Don't output the symbol twice. For LTO we want the label
10011 on the section beginning, not on the actual DIE. */
10012 && ((!flag_generate_lto && !flag_generate_offload)
10013 || die->die_tag != DW_TAG_compile_unit))
10014 output_die_symbol (die);
10016 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10017 (unsigned long)die->die_offset,
10018 dwarf_tag_name (die->die_tag));
10020 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10022 const char *name = dwarf_attr_name (a->dw_attr);
10024 switch (AT_class (a))
10026 case dw_val_class_addr:
10027 output_attr_index_or_value (a);
10028 break;
10030 case dw_val_class_offset:
10031 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10032 "%s", name);
10033 break;
10035 case dw_val_class_range_list:
10036 output_range_list_offset (a);
10037 break;
10039 case dw_val_class_loc:
10040 size = size_of_locs (AT_loc (a));
10042 /* Output the block length for this list of location operations. */
10043 if (dwarf_version >= 4)
10044 dw2_asm_output_data_uleb128 (size, "%s", name);
10045 else
10046 dw2_asm_output_data (constant_size (size), size, "%s", name);
10048 output_loc_sequence (AT_loc (a), -1);
10049 break;
10051 case dw_val_class_const:
10052 /* ??? It would be slightly more efficient to use a scheme like is
10053 used for unsigned constants below, but gdb 4.x does not sign
10054 extend. Gdb 5.x does sign extend. */
10055 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10056 break;
10058 case dw_val_class_unsigned_const:
10060 int csize = constant_size (AT_unsigned (a));
10061 if (dwarf_version == 3
10062 && a->dw_attr == DW_AT_data_member_location
10063 && csize >= 4)
10064 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10065 else
10066 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10068 break;
10070 case dw_val_class_const_implicit:
10071 if (flag_debug_asm)
10072 fprintf (asm_out_file, "\t\t\t%s %s ("
10073 HOST_WIDE_INT_PRINT_DEC ")\n",
10074 ASM_COMMENT_START, name, AT_int (a));
10075 break;
10077 case dw_val_class_unsigned_const_implicit:
10078 if (flag_debug_asm)
10079 fprintf (asm_out_file, "\t\t\t%s %s ("
10080 HOST_WIDE_INT_PRINT_HEX ")\n",
10081 ASM_COMMENT_START, name, AT_unsigned (a));
10082 break;
10084 case dw_val_class_const_double:
10086 unsigned HOST_WIDE_INT first, second;
10088 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10089 dw2_asm_output_data (1,
10090 HOST_BITS_PER_DOUBLE_INT
10091 / HOST_BITS_PER_CHAR,
10092 NULL);
10094 if (WORDS_BIG_ENDIAN)
10096 first = a->dw_attr_val.v.val_double.high;
10097 second = a->dw_attr_val.v.val_double.low;
10099 else
10101 first = a->dw_attr_val.v.val_double.low;
10102 second = a->dw_attr_val.v.val_double.high;
10105 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10106 first, "%s", name);
10107 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10108 second, NULL);
10110 break;
10112 case dw_val_class_wide_int:
10114 int i;
10115 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10116 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10117 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10118 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10119 * l, NULL);
10121 if (WORDS_BIG_ENDIAN)
10122 for (i = len - 1; i >= 0; --i)
10124 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10125 "%s", name);
10126 name = "";
10128 else
10129 for (i = 0; i < len; ++i)
10131 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10132 "%s", name);
10133 name = "";
10136 break;
10138 case dw_val_class_vec:
10140 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10141 unsigned int len = a->dw_attr_val.v.val_vec.length;
10142 unsigned int i;
10143 unsigned char *p;
10145 dw2_asm_output_data (constant_size (len * elt_size),
10146 len * elt_size, "%s", name);
10147 if (elt_size > sizeof (HOST_WIDE_INT))
10149 elt_size /= 2;
10150 len *= 2;
10152 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10153 i < len;
10154 i++, p += elt_size)
10155 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10156 "fp or vector constant word %u", i);
10157 break;
10160 case dw_val_class_flag:
10161 if (dwarf_version >= 4)
10163 /* Currently all add_AT_flag calls pass in 1 as last argument,
10164 so DW_FORM_flag_present can be used. If that ever changes,
10165 we'll need to use DW_FORM_flag and have some optimization
10166 in build_abbrev_table that will change those to
10167 DW_FORM_flag_present if it is set to 1 in all DIEs using
10168 the same abbrev entry. */
10169 gcc_assert (AT_flag (a) == 1);
10170 if (flag_debug_asm)
10171 fprintf (asm_out_file, "\t\t\t%s %s\n",
10172 ASM_COMMENT_START, name);
10173 break;
10175 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10176 break;
10178 case dw_val_class_loc_list:
10179 output_loc_list_offset (a);
10180 break;
10182 case dw_val_class_die_ref:
10183 if (AT_ref_external (a))
10185 if (AT_ref (a)->comdat_type_p)
10187 comdat_type_node *type_node
10188 = AT_ref (a)->die_id.die_type_node;
10190 gcc_assert (type_node);
10191 output_signature (type_node->signature, name);
10193 else
10195 const char *sym = AT_ref (a)->die_id.die_symbol;
10196 int size;
10198 gcc_assert (sym);
10199 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10200 length, whereas in DWARF3 it's always sized as an
10201 offset. */
10202 if (dwarf_version == 2)
10203 size = DWARF2_ADDR_SIZE;
10204 else
10205 size = DWARF_OFFSET_SIZE;
10206 /* ??? We cannot unconditionally output die_offset if
10207 non-zero - others might create references to those
10208 DIEs via symbols.
10209 And we do not clear its DIE offset after outputting it
10210 (and the label refers to the actual DIEs, not the
10211 DWARF CU unit header which is when using label + offset
10212 would be the correct thing to do).
10213 ??? This is the reason for the with_offset flag. */
10214 if (AT_ref (a)->with_offset)
10215 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10216 debug_info_section, "%s", name);
10217 else
10218 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10219 name);
10222 else
10224 gcc_assert (AT_ref (a)->die_offset);
10225 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10226 "%s", name);
10228 break;
10230 case dw_val_class_fde_ref:
10232 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10234 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10235 a->dw_attr_val.v.val_fde_index * 2);
10236 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10237 "%s", name);
10239 break;
10241 case dw_val_class_vms_delta:
10242 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10243 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10244 AT_vms_delta2 (a), AT_vms_delta1 (a),
10245 "%s", name);
10246 #else
10247 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10248 AT_vms_delta2 (a), AT_vms_delta1 (a),
10249 "%s", name);
10250 #endif
10251 break;
10253 case dw_val_class_lbl_id:
10254 output_attr_index_or_value (a);
10255 break;
10257 case dw_val_class_lineptr:
10258 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10259 debug_line_section, "%s", name);
10260 break;
10262 case dw_val_class_macptr:
10263 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10264 debug_macinfo_section, "%s", name);
10265 break;
10267 case dw_val_class_loclistsptr:
10268 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10269 debug_loc_section, "%s", name);
10270 break;
10272 case dw_val_class_str:
10273 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10274 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10275 a->dw_attr_val.v.val_str->label,
10276 debug_str_section,
10277 "%s: \"%s\"", name, AT_string (a));
10278 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10279 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10280 a->dw_attr_val.v.val_str->label,
10281 debug_line_str_section,
10282 "%s: \"%s\"", name, AT_string (a));
10283 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10284 dw2_asm_output_data_uleb128 (AT_index (a),
10285 "%s: \"%s\"", name, AT_string (a));
10286 else
10287 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10288 break;
10290 case dw_val_class_file:
10292 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10294 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10295 a->dw_attr_val.v.val_file->filename);
10296 break;
10299 case dw_val_class_file_implicit:
10300 if (flag_debug_asm)
10301 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10302 ASM_COMMENT_START, name,
10303 maybe_emit_file (a->dw_attr_val.v.val_file),
10304 a->dw_attr_val.v.val_file->filename);
10305 break;
10307 case dw_val_class_data8:
10309 int i;
10311 for (i = 0; i < 8; i++)
10312 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10313 i == 0 ? "%s" : NULL, name);
10314 break;
10317 case dw_val_class_high_pc:
10318 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10319 get_AT_low_pc (die), "DW_AT_high_pc");
10320 break;
10322 case dw_val_class_discr_value:
10323 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10324 break;
10326 case dw_val_class_discr_list:
10328 dw_discr_list_ref list = AT_discr_list (a);
10329 const int size = size_of_discr_list (list);
10331 /* This is a block, so output its length first. */
10332 dw2_asm_output_data (constant_size (size), size,
10333 "%s: block size", name);
10335 for (; list != NULL; list = list->dw_discr_next)
10337 /* One byte for the discriminant value descriptor, and then as
10338 many LEB128 numbers as required. */
10339 if (list->dw_discr_range)
10340 dw2_asm_output_data (1, DW_DSC_range,
10341 "%s: DW_DSC_range", name);
10342 else
10343 dw2_asm_output_data (1, DW_DSC_label,
10344 "%s: DW_DSC_label", name);
10346 output_discr_value (&list->dw_discr_lower_bound, name);
10347 if (list->dw_discr_range)
10348 output_discr_value (&list->dw_discr_upper_bound, name);
10350 break;
10353 default:
10354 gcc_unreachable ();
10358 FOR_EACH_CHILD (die, c, output_die (c));
10360 /* Add null byte to terminate sibling list. */
10361 if (die->die_child != NULL)
10362 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10363 (unsigned long) die->die_offset);
10366 /* Output the compilation unit that appears at the beginning of the
10367 .debug_info section, and precedes the DIE descriptions. */
10369 static void
10370 output_compilation_unit_header (enum dwarf_unit_type ut)
10372 if (!XCOFF_DEBUGGING_INFO)
10374 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10375 dw2_asm_output_data (4, 0xffffffff,
10376 "Initial length escape value indicating 64-bit DWARF extension");
10377 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10378 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10379 "Length of Compilation Unit Info");
10382 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10383 if (dwarf_version >= 5)
10385 const char *name;
10386 switch (ut)
10388 case DW_UT_compile: name = "DW_UT_compile"; break;
10389 case DW_UT_type: name = "DW_UT_type"; break;
10390 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10391 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10392 default: gcc_unreachable ();
10394 dw2_asm_output_data (1, ut, "%s", name);
10395 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10397 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10398 debug_abbrev_section,
10399 "Offset Into Abbrev. Section");
10400 if (dwarf_version < 5)
10401 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10404 /* Output the compilation unit DIE and its children. */
10406 static void
10407 output_comp_unit (dw_die_ref die, int output_if_empty,
10408 const unsigned char *dwo_id)
10410 const char *secname, *oldsym;
10411 char *tmp;
10413 /* Unless we are outputting main CU, we may throw away empty ones. */
10414 if (!output_if_empty && die->die_child == NULL)
10415 return;
10417 /* Even if there are no children of this DIE, we must output the information
10418 about the compilation unit. Otherwise, on an empty translation unit, we
10419 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10420 will then complain when examining the file. First mark all the DIEs in
10421 this CU so we know which get local refs. */
10422 mark_dies (die);
10424 external_ref_hash_type *extern_map = optimize_external_refs (die);
10426 /* For now, optimize only the main CU, in order to optimize the rest
10427 we'd need to see all of them earlier. Leave the rest for post-linking
10428 tools like DWZ. */
10429 if (die == comp_unit_die ())
10430 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10432 build_abbrev_table (die, extern_map);
10434 optimize_abbrev_table ();
10436 delete extern_map;
10438 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10439 next_die_offset = (dwo_id
10440 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10441 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10442 calc_die_sizes (die);
10444 oldsym = die->die_id.die_symbol;
10445 if (oldsym && die->comdat_type_p)
10447 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10449 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10450 secname = tmp;
10451 die->die_id.die_symbol = NULL;
10452 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10454 else
10456 switch_to_section (debug_info_section);
10457 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10458 info_section_emitted = true;
10461 /* For LTO cross unit DIE refs we want a symbol on the start of the
10462 debuginfo section, not on the CU DIE. */
10463 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10465 /* ??? No way to get visibility assembled without a decl. */
10466 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10467 get_identifier (oldsym), char_type_node);
10468 TREE_PUBLIC (decl) = true;
10469 TREE_STATIC (decl) = true;
10470 DECL_ARTIFICIAL (decl) = true;
10471 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10472 DECL_VISIBILITY_SPECIFIED (decl) = true;
10473 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10474 #ifdef ASM_WEAKEN_LABEL
10475 /* We prefer a .weak because that handles duplicates from duplicate
10476 archive members in a graceful way. */
10477 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10478 #else
10479 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10480 #endif
10481 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10484 /* Output debugging information. */
10485 output_compilation_unit_header (dwo_id
10486 ? DW_UT_split_compile : DW_UT_compile);
10487 if (dwarf_version >= 5)
10489 if (dwo_id != NULL)
10490 for (int i = 0; i < 8; i++)
10491 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10493 output_die (die);
10495 /* Leave the marks on the main CU, so we can check them in
10496 output_pubnames. */
10497 if (oldsym)
10499 unmark_dies (die);
10500 die->die_id.die_symbol = oldsym;
10504 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10505 and .debug_pubtypes. This is configured per-target, but can be
10506 overridden by the -gpubnames or -gno-pubnames options. */
10508 static inline bool
10509 want_pubnames (void)
10511 if (debug_info_level <= DINFO_LEVEL_TERSE)
10512 return false;
10513 if (debug_generate_pub_sections != -1)
10514 return debug_generate_pub_sections;
10515 return targetm.want_debug_pub_sections;
10518 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10520 static void
10521 add_AT_pubnames (dw_die_ref die)
10523 if (want_pubnames ())
10524 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10527 /* Add a string attribute value to a skeleton DIE. */
10529 static inline void
10530 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10531 const char *str)
10533 dw_attr_node attr;
10534 struct indirect_string_node *node;
10536 if (! skeleton_debug_str_hash)
10537 skeleton_debug_str_hash
10538 = hash_table<indirect_string_hasher>::create_ggc (10);
10540 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10541 find_string_form (node);
10542 if (node->form == DW_FORM_GNU_str_index)
10543 node->form = DW_FORM_strp;
10545 attr.dw_attr = attr_kind;
10546 attr.dw_attr_val.val_class = dw_val_class_str;
10547 attr.dw_attr_val.val_entry = NULL;
10548 attr.dw_attr_val.v.val_str = node;
10549 add_dwarf_attr (die, &attr);
10552 /* Helper function to generate top-level dies for skeleton debug_info and
10553 debug_types. */
10555 static void
10556 add_top_level_skeleton_die_attrs (dw_die_ref die)
10558 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10559 const char *comp_dir = comp_dir_string ();
10561 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10562 if (comp_dir != NULL)
10563 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10564 add_AT_pubnames (die);
10565 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10568 /* Output skeleton debug sections that point to the dwo file. */
10570 static void
10571 output_skeleton_debug_sections (dw_die_ref comp_unit,
10572 const unsigned char *dwo_id)
10574 /* These attributes will be found in the full debug_info section. */
10575 remove_AT (comp_unit, DW_AT_producer);
10576 remove_AT (comp_unit, DW_AT_language);
10578 switch_to_section (debug_skeleton_info_section);
10579 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10581 /* Produce the skeleton compilation-unit header. This one differs enough from
10582 a normal CU header that it's better not to call output_compilation_unit
10583 header. */
10584 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10585 dw2_asm_output_data (4, 0xffffffff,
10586 "Initial length escape value indicating 64-bit "
10587 "DWARF extension");
10589 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10590 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10591 - DWARF_INITIAL_LENGTH_SIZE
10592 + size_of_die (comp_unit),
10593 "Length of Compilation Unit Info");
10594 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10595 if (dwarf_version >= 5)
10597 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10598 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10600 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10601 debug_skeleton_abbrev_section,
10602 "Offset Into Abbrev. Section");
10603 if (dwarf_version < 5)
10604 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10605 else
10606 for (int i = 0; i < 8; i++)
10607 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10609 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10610 output_die (comp_unit);
10612 /* Build the skeleton debug_abbrev section. */
10613 switch_to_section (debug_skeleton_abbrev_section);
10614 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10616 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10618 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10621 /* Output a comdat type unit DIE and its children. */
10623 static void
10624 output_comdat_type_unit (comdat_type_node *node)
10626 const char *secname;
10627 char *tmp;
10628 int i;
10629 #if defined (OBJECT_FORMAT_ELF)
10630 tree comdat_key;
10631 #endif
10633 /* First mark all the DIEs in this CU so we know which get local refs. */
10634 mark_dies (node->root_die);
10636 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10638 build_abbrev_table (node->root_die, extern_map);
10640 delete extern_map;
10641 extern_map = NULL;
10643 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10644 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10645 calc_die_sizes (node->root_die);
10647 #if defined (OBJECT_FORMAT_ELF)
10648 if (dwarf_version >= 5)
10650 if (!dwarf_split_debug_info)
10651 secname = ".debug_info";
10652 else
10653 secname = ".debug_info.dwo";
10655 else if (!dwarf_split_debug_info)
10656 secname = ".debug_types";
10657 else
10658 secname = ".debug_types.dwo";
10660 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10661 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10662 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10663 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10664 comdat_key = get_identifier (tmp);
10665 targetm.asm_out.named_section (secname,
10666 SECTION_DEBUG | SECTION_LINKONCE,
10667 comdat_key);
10668 #else
10669 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10670 sprintf (tmp, (dwarf_version >= 5
10671 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10672 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10673 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10674 secname = tmp;
10675 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10676 #endif
10678 /* Output debugging information. */
10679 output_compilation_unit_header (dwarf_split_debug_info
10680 ? DW_UT_split_type : DW_UT_type);
10681 output_signature (node->signature, "Type Signature");
10682 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10683 "Offset to Type DIE");
10684 output_die (node->root_die);
10686 unmark_dies (node->root_die);
10689 /* Return the DWARF2/3 pubname associated with a decl. */
10691 static const char *
10692 dwarf2_name (tree decl, int scope)
10694 if (DECL_NAMELESS (decl))
10695 return NULL;
10696 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10699 /* Add a new entry to .debug_pubnames if appropriate. */
10701 static void
10702 add_pubname_string (const char *str, dw_die_ref die)
10704 pubname_entry e;
10706 e.die = die;
10707 e.name = xstrdup (str);
10708 vec_safe_push (pubname_table, e);
10711 static void
10712 add_pubname (tree decl, dw_die_ref die)
10714 if (!want_pubnames ())
10715 return;
10717 /* Don't add items to the table when we expect that the consumer will have
10718 just read the enclosing die. For example, if the consumer is looking at a
10719 class_member, it will either be inside the class already, or will have just
10720 looked up the class to find the member. Either way, searching the class is
10721 faster than searching the index. */
10722 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10723 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10725 const char *name = dwarf2_name (decl, 1);
10727 if (name)
10728 add_pubname_string (name, die);
10732 /* Add an enumerator to the pubnames section. */
10734 static void
10735 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10737 pubname_entry e;
10739 gcc_assert (scope_name);
10740 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10741 e.die = die;
10742 vec_safe_push (pubname_table, e);
10745 /* Add a new entry to .debug_pubtypes if appropriate. */
10747 static void
10748 add_pubtype (tree decl, dw_die_ref die)
10750 pubname_entry e;
10752 if (!want_pubnames ())
10753 return;
10755 if ((TREE_PUBLIC (decl)
10756 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10757 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10759 tree scope = NULL;
10760 const char *scope_name = "";
10761 const char *sep = is_cxx () ? "::" : ".";
10762 const char *name;
10764 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10765 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10767 scope_name = lang_hooks.dwarf_name (scope, 1);
10768 if (scope_name != NULL && scope_name[0] != '\0')
10769 scope_name = concat (scope_name, sep, NULL);
10770 else
10771 scope_name = "";
10774 if (TYPE_P (decl))
10775 name = type_tag (decl);
10776 else
10777 name = lang_hooks.dwarf_name (decl, 1);
10779 /* If we don't have a name for the type, there's no point in adding
10780 it to the table. */
10781 if (name != NULL && name[0] != '\0')
10783 e.die = die;
10784 e.name = concat (scope_name, name, NULL);
10785 vec_safe_push (pubtype_table, e);
10788 /* Although it might be more consistent to add the pubinfo for the
10789 enumerators as their dies are created, they should only be added if the
10790 enum type meets the criteria above. So rather than re-check the parent
10791 enum type whenever an enumerator die is created, just output them all
10792 here. This isn't protected by the name conditional because anonymous
10793 enums don't have names. */
10794 if (die->die_tag == DW_TAG_enumeration_type)
10796 dw_die_ref c;
10798 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10803 /* Output a single entry in the pubnames table. */
10805 static void
10806 output_pubname (dw_offset die_offset, pubname_entry *entry)
10808 dw_die_ref die = entry->die;
10809 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10811 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10813 if (debug_generate_pub_sections == 2)
10815 /* This logic follows gdb's method for determining the value of the flag
10816 byte. */
10817 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10818 switch (die->die_tag)
10820 case DW_TAG_typedef:
10821 case DW_TAG_base_type:
10822 case DW_TAG_subrange_type:
10823 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10824 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10825 break;
10826 case DW_TAG_enumerator:
10827 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10828 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10829 if (!is_cxx ())
10830 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10831 break;
10832 case DW_TAG_subprogram:
10833 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10834 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10835 if (!is_ada ())
10836 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10837 break;
10838 case DW_TAG_constant:
10839 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10840 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10841 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10842 break;
10843 case DW_TAG_variable:
10844 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10845 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10846 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10847 break;
10848 case DW_TAG_namespace:
10849 case DW_TAG_imported_declaration:
10850 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10851 break;
10852 case DW_TAG_class_type:
10853 case DW_TAG_interface_type:
10854 case DW_TAG_structure_type:
10855 case DW_TAG_union_type:
10856 case DW_TAG_enumeration_type:
10857 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10858 if (!is_cxx ())
10859 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10860 break;
10861 default:
10862 /* An unusual tag. Leave the flag-byte empty. */
10863 break;
10865 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10866 "GDB-index flags");
10869 dw2_asm_output_nstring (entry->name, -1, "external name");
10873 /* Output the public names table used to speed up access to externally
10874 visible names; or the public types table used to find type definitions. */
10876 static void
10877 output_pubnames (vec<pubname_entry, va_gc> *names)
10879 unsigned i;
10880 unsigned long pubnames_length = size_of_pubnames (names);
10881 pubname_entry *pub;
10883 if (!XCOFF_DEBUGGING_INFO)
10885 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10886 dw2_asm_output_data (4, 0xffffffff,
10887 "Initial length escape value indicating 64-bit DWARF extension");
10888 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10889 "Pub Info Length");
10892 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10893 dw2_asm_output_data (2, 2, "DWARF Version");
10895 if (dwarf_split_debug_info)
10896 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10897 debug_skeleton_info_section,
10898 "Offset of Compilation Unit Info");
10899 else
10900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10901 debug_info_section,
10902 "Offset of Compilation Unit Info");
10903 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10904 "Compilation Unit Length");
10906 FOR_EACH_VEC_ELT (*names, i, pub)
10908 if (include_pubname_in_output (names, pub))
10910 dw_offset die_offset = pub->die->die_offset;
10912 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10913 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10914 gcc_assert (pub->die->die_mark);
10916 /* If we're putting types in their own .debug_types sections,
10917 the .debug_pubtypes table will still point to the compile
10918 unit (not the type unit), so we want to use the offset of
10919 the skeleton DIE (if there is one). */
10920 if (pub->die->comdat_type_p && names == pubtype_table)
10922 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10924 if (type_node != NULL)
10925 die_offset = (type_node->skeleton_die != NULL
10926 ? type_node->skeleton_die->die_offset
10927 : comp_unit_die ()->die_offset);
10930 output_pubname (die_offset, pub);
10934 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10937 /* Output public names and types tables if necessary. */
10939 static void
10940 output_pubtables (void)
10942 if (!want_pubnames () || !info_section_emitted)
10943 return;
10945 switch_to_section (debug_pubnames_section);
10946 output_pubnames (pubname_table);
10947 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10948 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10949 simply won't look for the section. */
10950 switch_to_section (debug_pubtypes_section);
10951 output_pubnames (pubtype_table);
10955 /* Output the information that goes into the .debug_aranges table.
10956 Namely, define the beginning and ending address range of the
10957 text section generated for this compilation unit. */
10959 static void
10960 output_aranges (void)
10962 unsigned i;
10963 unsigned long aranges_length = size_of_aranges ();
10965 if (!XCOFF_DEBUGGING_INFO)
10967 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10968 dw2_asm_output_data (4, 0xffffffff,
10969 "Initial length escape value indicating 64-bit DWARF extension");
10970 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10971 "Length of Address Ranges Info");
10974 /* Version number for aranges is still 2, even up to DWARF5. */
10975 dw2_asm_output_data (2, 2, "DWARF Version");
10976 if (dwarf_split_debug_info)
10977 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10978 debug_skeleton_info_section,
10979 "Offset of Compilation Unit Info");
10980 else
10981 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10982 debug_info_section,
10983 "Offset of Compilation Unit Info");
10984 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10985 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10987 /* We need to align to twice the pointer size here. */
10988 if (DWARF_ARANGES_PAD_SIZE)
10990 /* Pad using a 2 byte words so that padding is correct for any
10991 pointer size. */
10992 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10993 2 * DWARF2_ADDR_SIZE);
10994 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10995 dw2_asm_output_data (2, 0, NULL);
10998 /* It is necessary not to output these entries if the sections were
10999 not used; if the sections were not used, the length will be 0 and
11000 the address may end up as 0 if the section is discarded by ld
11001 --gc-sections, leaving an invalid (0, 0) entry that can be
11002 confused with the terminator. */
11003 if (text_section_used)
11005 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11006 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11007 text_section_label, "Length");
11009 if (cold_text_section_used)
11011 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11012 "Address");
11013 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11014 cold_text_section_label, "Length");
11017 if (have_multiple_function_sections)
11019 unsigned fde_idx;
11020 dw_fde_ref fde;
11022 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11024 if (DECL_IGNORED_P (fde->decl))
11025 continue;
11026 if (!fde->in_std_section)
11028 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11029 "Address");
11030 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11031 fde->dw_fde_begin, "Length");
11033 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11035 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11036 "Address");
11037 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11038 fde->dw_fde_second_begin, "Length");
11043 /* Output the terminator words. */
11044 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11045 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11048 /* Add a new entry to .debug_ranges. Return its index into
11049 ranges_table vector. */
11051 static unsigned int
11052 add_ranges_num (int num, bool maybe_new_sec)
11054 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11055 vec_safe_push (ranges_table, r);
11056 return vec_safe_length (ranges_table) - 1;
11059 /* Add a new entry to .debug_ranges corresponding to a block, or a
11060 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11061 this entry might be in a different section from previous range. */
11063 static unsigned int
11064 add_ranges (const_tree block, bool maybe_new_sec)
11066 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11069 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11070 chain, or middle entry of a chain that will be directly referred to. */
11072 static void
11073 note_rnglist_head (unsigned int offset)
11075 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11076 return;
11077 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11080 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11081 When using dwarf_split_debug_info, address attributes in dies destined
11082 for the final executable should be direct references--setting the
11083 parameter force_direct ensures this behavior. */
11085 static void
11086 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11087 bool *added, bool force_direct)
11089 unsigned int in_use = vec_safe_length (ranges_by_label);
11090 unsigned int offset;
11091 dw_ranges_by_label rbl = { begin, end };
11092 vec_safe_push (ranges_by_label, rbl);
11093 offset = add_ranges_num (-(int)in_use - 1, true);
11094 if (!*added)
11096 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11097 *added = true;
11098 note_rnglist_head (offset);
11102 /* Emit .debug_ranges section. */
11104 static void
11105 output_ranges (void)
11107 unsigned i;
11108 static const char *const start_fmt = "Offset %#x";
11109 const char *fmt = start_fmt;
11110 dw_ranges *r;
11112 switch_to_section (debug_ranges_section);
11113 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11114 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11116 int block_num = r->num;
11118 if (block_num > 0)
11120 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11121 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11123 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11124 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11126 /* If all code is in the text section, then the compilation
11127 unit base address defaults to DW_AT_low_pc, which is the
11128 base of the text section. */
11129 if (!have_multiple_function_sections)
11131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11132 text_section_label,
11133 fmt, i * 2 * DWARF2_ADDR_SIZE);
11134 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11135 text_section_label, NULL);
11138 /* Otherwise, the compilation unit base address is zero,
11139 which allows us to use absolute addresses, and not worry
11140 about whether the target supports cross-section
11141 arithmetic. */
11142 else
11144 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11145 fmt, i * 2 * DWARF2_ADDR_SIZE);
11146 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11149 fmt = NULL;
11152 /* Negative block_num stands for an index into ranges_by_label. */
11153 else if (block_num < 0)
11155 int lab_idx = - block_num - 1;
11157 if (!have_multiple_function_sections)
11159 gcc_unreachable ();
11160 #if 0
11161 /* If we ever use add_ranges_by_labels () for a single
11162 function section, all we have to do is to take out
11163 the #if 0 above. */
11164 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11165 (*ranges_by_label)[lab_idx].begin,
11166 text_section_label,
11167 fmt, i * 2 * DWARF2_ADDR_SIZE);
11168 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11169 (*ranges_by_label)[lab_idx].end,
11170 text_section_label, NULL);
11171 #endif
11173 else
11175 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11176 (*ranges_by_label)[lab_idx].begin,
11177 fmt, i * 2 * DWARF2_ADDR_SIZE);
11178 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11179 (*ranges_by_label)[lab_idx].end,
11180 NULL);
11183 else
11185 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11186 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11187 fmt = start_fmt;
11192 /* Non-zero if .debug_line_str should be used for .debug_line section
11193 strings or strings that are likely shareable with those. */
11194 #define DWARF5_USE_DEBUG_LINE_STR \
11195 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11196 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11197 /* FIXME: there is no .debug_line_str.dwo section, \
11198 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11199 && !dwarf_split_debug_info)
11201 /* Assign .debug_rnglists indexes. */
11203 static void
11204 index_rnglists (void)
11206 unsigned i;
11207 dw_ranges *r;
11209 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11210 if (r->label)
11211 r->idx = rnglist_idx++;
11214 /* Emit .debug_rnglists section. */
11216 static void
11217 output_rnglists (void)
11219 unsigned i;
11220 dw_ranges *r;
11221 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11222 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11223 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11225 switch_to_section (debug_ranges_section);
11226 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11227 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL, 2);
11228 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL, 3);
11229 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11230 dw2_asm_output_data (4, 0xffffffff,
11231 "Initial length escape value indicating "
11232 "64-bit DWARF extension");
11233 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11234 "Length of Range Lists");
11235 ASM_OUTPUT_LABEL (asm_out_file, l1);
11236 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11237 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11238 dw2_asm_output_data (1, 0, "Segment Size");
11239 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11240 about relocation sizes and primarily care about the size of .debug*
11241 sections in linked shared libraries and executables, then
11242 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11243 into it are usually larger than just DW_FORM_sec_offset offsets
11244 into the .debug_rnglists section. */
11245 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11246 "Offset Entry Count");
11247 if (dwarf_split_debug_info)
11249 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11250 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11251 if (r->label)
11252 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11253 ranges_base_label, NULL);
11256 const char *lab = "";
11257 unsigned int len = vec_safe_length (ranges_table);
11258 const char *base = NULL;
11259 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11261 int block_num = r->num;
11263 if (r->label)
11265 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11266 lab = r->label;
11268 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11269 base = NULL;
11270 if (block_num > 0)
11272 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11273 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11275 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11276 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11278 if (HAVE_AS_LEB128)
11280 /* If all code is in the text section, then the compilation
11281 unit base address defaults to DW_AT_low_pc, which is the
11282 base of the text section. */
11283 if (!have_multiple_function_sections)
11285 dw2_asm_output_data (1, DW_RLE_offset_pair,
11286 "DW_RLE_offset_pair (%s)", lab);
11287 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11288 "Range begin address (%s)", lab);
11289 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11290 "Range end address (%s)", lab);
11291 continue;
11293 if (base == NULL)
11295 dw_ranges *r2 = NULL;
11296 if (i < len - 1)
11297 r2 = &(*ranges_table)[i + 1];
11298 if (r2
11299 && r2->num != 0
11300 && r2->label == NULL
11301 && !r2->maybe_new_sec)
11303 dw2_asm_output_data (1, DW_RLE_base_address,
11304 "DW_RLE_base_address (%s)", lab);
11305 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11306 "Base address (%s)", lab);
11307 strcpy (basebuf, blabel);
11308 base = basebuf;
11311 if (base)
11313 dw2_asm_output_data (1, DW_RLE_offset_pair,
11314 "DW_RLE_offset_pair (%s)", lab);
11315 dw2_asm_output_delta_uleb128 (blabel, base,
11316 "Range begin address (%s)", lab);
11317 dw2_asm_output_delta_uleb128 (elabel, base,
11318 "Range end address (%s)", lab);
11319 continue;
11321 dw2_asm_output_data (1, DW_RLE_start_length,
11322 "DW_RLE_start_length (%s)", lab);
11323 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11324 "Range begin address (%s)", lab);
11325 dw2_asm_output_delta_uleb128 (elabel, blabel,
11326 "Range length (%s)", lab);
11328 else
11330 dw2_asm_output_data (1, DW_RLE_start_end,
11331 "DW_RLE_start_end (%s)", lab);
11332 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11333 "Range begin address (%s)", lab);
11334 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11335 "Range end address (%s)", lab);
11339 /* Negative block_num stands for an index into ranges_by_label. */
11340 else if (block_num < 0)
11342 int lab_idx = - block_num - 1;
11343 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11344 const char *elabel = (*ranges_by_label)[lab_idx].end;
11346 if (!have_multiple_function_sections)
11347 gcc_unreachable ();
11348 if (HAVE_AS_LEB128)
11350 dw2_asm_output_data (1, DW_RLE_start_length,
11351 "DW_RLE_start_length (%s)", lab);
11352 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11353 "Range begin address (%s)", lab);
11354 dw2_asm_output_delta_uleb128 (elabel, blabel,
11355 "Range length (%s)", lab);
11357 else
11359 dw2_asm_output_data (1, DW_RLE_start_end,
11360 "DW_RLE_start_end (%s)", lab);
11361 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11362 "Range begin address (%s)", lab);
11363 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11364 "Range end address (%s)", lab);
11367 else
11368 dw2_asm_output_data (1, DW_RLE_end_of_list,
11369 "DW_RLE_end_of_list (%s)", lab);
11371 ASM_OUTPUT_LABEL (asm_out_file, l2);
11374 /* Data structure containing information about input files. */
11375 struct file_info
11377 const char *path; /* Complete file name. */
11378 const char *fname; /* File name part. */
11379 int length; /* Length of entire string. */
11380 struct dwarf_file_data * file_idx; /* Index in input file table. */
11381 int dir_idx; /* Index in directory table. */
11384 /* Data structure containing information about directories with source
11385 files. */
11386 struct dir_info
11388 const char *path; /* Path including directory name. */
11389 int length; /* Path length. */
11390 int prefix; /* Index of directory entry which is a prefix. */
11391 int count; /* Number of files in this directory. */
11392 int dir_idx; /* Index of directory used as base. */
11395 /* Callback function for file_info comparison. We sort by looking at
11396 the directories in the path. */
11398 static int
11399 file_info_cmp (const void *p1, const void *p2)
11401 const struct file_info *const s1 = (const struct file_info *) p1;
11402 const struct file_info *const s2 = (const struct file_info *) p2;
11403 const unsigned char *cp1;
11404 const unsigned char *cp2;
11406 /* Take care of file names without directories. We need to make sure that
11407 we return consistent values to qsort since some will get confused if
11408 we return the same value when identical operands are passed in opposite
11409 orders. So if neither has a directory, return 0 and otherwise return
11410 1 or -1 depending on which one has the directory. */
11411 if ((s1->path == s1->fname || s2->path == s2->fname))
11412 return (s2->path == s2->fname) - (s1->path == s1->fname);
11414 cp1 = (const unsigned char *) s1->path;
11415 cp2 = (const unsigned char *) s2->path;
11417 while (1)
11419 ++cp1;
11420 ++cp2;
11421 /* Reached the end of the first path? If so, handle like above. */
11422 if ((cp1 == (const unsigned char *) s1->fname)
11423 || (cp2 == (const unsigned char *) s2->fname))
11424 return ((cp2 == (const unsigned char *) s2->fname)
11425 - (cp1 == (const unsigned char *) s1->fname));
11427 /* Character of current path component the same? */
11428 else if (*cp1 != *cp2)
11429 return *cp1 - *cp2;
11433 struct file_name_acquire_data
11435 struct file_info *files;
11436 int used_files;
11437 int max_files;
11440 /* Traversal function for the hash table. */
11443 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11445 struct dwarf_file_data *d = *slot;
11446 struct file_info *fi;
11447 const char *f;
11449 gcc_assert (fnad->max_files >= d->emitted_number);
11451 if (! d->emitted_number)
11452 return 1;
11454 gcc_assert (fnad->max_files != fnad->used_files);
11456 fi = fnad->files + fnad->used_files++;
11458 /* Skip all leading "./". */
11459 f = d->filename;
11460 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11461 f += 2;
11463 /* Create a new array entry. */
11464 fi->path = f;
11465 fi->length = strlen (f);
11466 fi->file_idx = d;
11468 /* Search for the file name part. */
11469 f = strrchr (f, DIR_SEPARATOR);
11470 #if defined (DIR_SEPARATOR_2)
11472 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11474 if (g != NULL)
11476 if (f == NULL || f < g)
11477 f = g;
11480 #endif
11482 fi->fname = f == NULL ? fi->path : f + 1;
11483 return 1;
11486 /* Helper function for output_file_names. Emit a FORM encoded
11487 string STR, with assembly comment start ENTRY_KIND and
11488 index IDX */
11490 static void
11491 output_line_string (enum dwarf_form form, const char *str,
11492 const char *entry_kind, unsigned int idx)
11494 switch (form)
11496 case DW_FORM_string:
11497 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11498 break;
11499 case DW_FORM_line_strp:
11500 if (!debug_line_str_hash)
11501 debug_line_str_hash
11502 = hash_table<indirect_string_hasher>::create_ggc (10);
11504 struct indirect_string_node *node;
11505 node = find_AT_string_in_table (str, debug_line_str_hash);
11506 set_indirect_string (node);
11507 node->form = form;
11508 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11509 debug_line_str_section, "%s: %#x: \"%s\"",
11510 entry_kind, 0, node->str);
11511 break;
11512 default:
11513 gcc_unreachable ();
11517 /* Output the directory table and the file name table. We try to minimize
11518 the total amount of memory needed. A heuristic is used to avoid large
11519 slowdowns with many input files. */
11521 static void
11522 output_file_names (void)
11524 struct file_name_acquire_data fnad;
11525 int numfiles;
11526 struct file_info *files;
11527 struct dir_info *dirs;
11528 int *saved;
11529 int *savehere;
11530 int *backmap;
11531 int ndirs;
11532 int idx_offset;
11533 int i;
11535 if (!last_emitted_file)
11537 if (dwarf_version >= 5)
11539 dw2_asm_output_data (1, 0, "Directory entry format count");
11540 dw2_asm_output_data_uleb128 (0, "Directories count");
11541 dw2_asm_output_data (1, 0, "File name entry format count");
11542 dw2_asm_output_data_uleb128 (0, "File names count");
11544 else
11546 dw2_asm_output_data (1, 0, "End directory table");
11547 dw2_asm_output_data (1, 0, "End file name table");
11549 return;
11552 numfiles = last_emitted_file->emitted_number;
11554 /* Allocate the various arrays we need. */
11555 files = XALLOCAVEC (struct file_info, numfiles);
11556 dirs = XALLOCAVEC (struct dir_info, numfiles);
11558 fnad.files = files;
11559 fnad.used_files = 0;
11560 fnad.max_files = numfiles;
11561 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11562 gcc_assert (fnad.used_files == fnad.max_files);
11564 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11566 /* Find all the different directories used. */
11567 dirs[0].path = files[0].path;
11568 dirs[0].length = files[0].fname - files[0].path;
11569 dirs[0].prefix = -1;
11570 dirs[0].count = 1;
11571 dirs[0].dir_idx = 0;
11572 files[0].dir_idx = 0;
11573 ndirs = 1;
11575 for (i = 1; i < numfiles; i++)
11576 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11577 && memcmp (dirs[ndirs - 1].path, files[i].path,
11578 dirs[ndirs - 1].length) == 0)
11580 /* Same directory as last entry. */
11581 files[i].dir_idx = ndirs - 1;
11582 ++dirs[ndirs - 1].count;
11584 else
11586 int j;
11588 /* This is a new directory. */
11589 dirs[ndirs].path = files[i].path;
11590 dirs[ndirs].length = files[i].fname - files[i].path;
11591 dirs[ndirs].count = 1;
11592 dirs[ndirs].dir_idx = ndirs;
11593 files[i].dir_idx = ndirs;
11595 /* Search for a prefix. */
11596 dirs[ndirs].prefix = -1;
11597 for (j = 0; j < ndirs; j++)
11598 if (dirs[j].length < dirs[ndirs].length
11599 && dirs[j].length > 1
11600 && (dirs[ndirs].prefix == -1
11601 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11602 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11603 dirs[ndirs].prefix = j;
11605 ++ndirs;
11608 /* Now to the actual work. We have to find a subset of the directories which
11609 allow expressing the file name using references to the directory table
11610 with the least amount of characters. We do not do an exhaustive search
11611 where we would have to check out every combination of every single
11612 possible prefix. Instead we use a heuristic which provides nearly optimal
11613 results in most cases and never is much off. */
11614 saved = XALLOCAVEC (int, ndirs);
11615 savehere = XALLOCAVEC (int, ndirs);
11617 memset (saved, '\0', ndirs * sizeof (saved[0]));
11618 for (i = 0; i < ndirs; i++)
11620 int j;
11621 int total;
11623 /* We can always save some space for the current directory. But this
11624 does not mean it will be enough to justify adding the directory. */
11625 savehere[i] = dirs[i].length;
11626 total = (savehere[i] - saved[i]) * dirs[i].count;
11628 for (j = i + 1; j < ndirs; j++)
11630 savehere[j] = 0;
11631 if (saved[j] < dirs[i].length)
11633 /* Determine whether the dirs[i] path is a prefix of the
11634 dirs[j] path. */
11635 int k;
11637 k = dirs[j].prefix;
11638 while (k != -1 && k != (int) i)
11639 k = dirs[k].prefix;
11641 if (k == (int) i)
11643 /* Yes it is. We can possibly save some memory by
11644 writing the filenames in dirs[j] relative to
11645 dirs[i]. */
11646 savehere[j] = dirs[i].length;
11647 total += (savehere[j] - saved[j]) * dirs[j].count;
11652 /* Check whether we can save enough to justify adding the dirs[i]
11653 directory. */
11654 if (total > dirs[i].length + 1)
11656 /* It's worthwhile adding. */
11657 for (j = i; j < ndirs; j++)
11658 if (savehere[j] > 0)
11660 /* Remember how much we saved for this directory so far. */
11661 saved[j] = savehere[j];
11663 /* Remember the prefix directory. */
11664 dirs[j].dir_idx = i;
11669 /* Emit the directory name table. */
11670 idx_offset = dirs[0].length > 0 ? 1 : 0;
11671 enum dwarf_form str_form = DW_FORM_string;
11672 enum dwarf_form idx_form = DW_FORM_udata;
11673 if (dwarf_version >= 5)
11675 const char *comp_dir = comp_dir_string ();
11676 if (comp_dir == NULL)
11677 comp_dir = "";
11678 dw2_asm_output_data (1, 1, "Directory entry format count");
11679 if (DWARF5_USE_DEBUG_LINE_STR)
11680 str_form = DW_FORM_line_strp;
11681 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11682 dw2_asm_output_data_uleb128 (str_form, "%s",
11683 get_DW_FORM_name (str_form));
11684 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11685 if (str_form == DW_FORM_string)
11687 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
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 else
11696 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11697 for (i = 1 - idx_offset; i < ndirs; i++)
11699 const char *str
11700 = ggc_alloc_string (dirs[i].path,
11701 dirs[i].length
11702 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11703 output_line_string (str_form, str, "Directory Entry",
11704 (unsigned) i + idx_offset);
11708 else
11710 for (i = 1 - idx_offset; i < ndirs; i++)
11711 dw2_asm_output_nstring (dirs[i].path,
11712 dirs[i].length
11713 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11714 "Directory Entry: %#x", i + idx_offset);
11716 dw2_asm_output_data (1, 0, "End directory table");
11719 /* We have to emit them in the order of emitted_number since that's
11720 used in the debug info generation. To do this efficiently we
11721 generate a back-mapping of the indices first. */
11722 backmap = XALLOCAVEC (int, numfiles);
11723 for (i = 0; i < numfiles; i++)
11724 backmap[files[i].file_idx->emitted_number - 1] = i;
11726 if (dwarf_version >= 5)
11728 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11729 if (filename0 == NULL)
11730 filename0 = "";
11731 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11732 DW_FORM_data2. Choose one based on the number of directories
11733 and how much space would they occupy in each encoding.
11734 If we have at most 256 directories, all indexes fit into
11735 a single byte, so DW_FORM_data1 is most compact (if there
11736 are at most 128 directories, DW_FORM_udata would be as
11737 compact as that, but not shorter and slower to decode). */
11738 if (ndirs + idx_offset <= 256)
11739 idx_form = DW_FORM_data1;
11740 /* If there are more than 65536 directories, we have to use
11741 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11742 Otherwise, compute what space would occupy if all the indexes
11743 used DW_FORM_udata - sum - and compare that to how large would
11744 be DW_FORM_data2 encoding, and pick the more efficient one. */
11745 else if (ndirs + idx_offset <= 65536)
11747 unsigned HOST_WIDE_INT sum = 1;
11748 for (i = 0; i < numfiles; i++)
11750 int file_idx = backmap[i];
11751 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11752 sum += size_of_uleb128 (dir_idx);
11754 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11755 idx_form = DW_FORM_data2;
11757 #ifdef VMS_DEBUGGING_INFO
11758 dw2_asm_output_data (1, 4, "File name entry format count");
11759 #else
11760 dw2_asm_output_data (1, 2, "File name entry format count");
11761 #endif
11762 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11763 dw2_asm_output_data_uleb128 (str_form, "%s",
11764 get_DW_FORM_name (str_form));
11765 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11766 "DW_LNCT_directory_index");
11767 dw2_asm_output_data_uleb128 (idx_form, "%s",
11768 get_DW_FORM_name (idx_form));
11769 #ifdef VMS_DEBUGGING_INFO
11770 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11771 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11772 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11773 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11774 #endif
11775 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11777 output_line_string (str_form, filename0, "File Entry", 0);
11779 /* Include directory index. */
11780 if (idx_form != DW_FORM_udata)
11781 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11782 0, NULL);
11783 else
11784 dw2_asm_output_data_uleb128 (0, NULL);
11786 #ifdef VMS_DEBUGGING_INFO
11787 dw2_asm_output_data_uleb128 (0, NULL);
11788 dw2_asm_output_data_uleb128 (0, NULL);
11789 #endif
11792 /* Now write all the file names. */
11793 for (i = 0; i < numfiles; i++)
11795 int file_idx = backmap[i];
11796 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11798 #ifdef VMS_DEBUGGING_INFO
11799 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11801 /* Setting these fields can lead to debugger miscomparisons,
11802 but VMS Debug requires them to be set correctly. */
11804 int ver;
11805 long long cdt;
11806 long siz;
11807 int maxfilelen = (strlen (files[file_idx].path)
11808 + dirs[dir_idx].length
11809 + MAX_VMS_VERSION_LEN + 1);
11810 char *filebuf = XALLOCAVEC (char, maxfilelen);
11812 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11813 snprintf (filebuf, maxfilelen, "%s;%d",
11814 files[file_idx].path + dirs[dir_idx].length, ver);
11816 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11818 /* Include directory index. */
11819 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11820 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11821 dir_idx + idx_offset, NULL);
11822 else
11823 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11825 /* Modification time. */
11826 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11827 &cdt, 0, 0, 0) == 0)
11828 ? cdt : 0, NULL);
11830 /* File length in bytes. */
11831 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11832 0, &siz, 0, 0) == 0)
11833 ? siz : 0, NULL);
11834 #else
11835 output_line_string (str_form,
11836 files[file_idx].path + dirs[dir_idx].length,
11837 "File Entry", (unsigned) i + 1);
11839 /* Include directory index. */
11840 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11841 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11842 dir_idx + idx_offset, NULL);
11843 else
11844 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11846 if (dwarf_version >= 5)
11847 continue;
11849 /* Modification time. */
11850 dw2_asm_output_data_uleb128 (0, NULL);
11852 /* File length in bytes. */
11853 dw2_asm_output_data_uleb128 (0, NULL);
11854 #endif /* VMS_DEBUGGING_INFO */
11857 if (dwarf_version < 5)
11858 dw2_asm_output_data (1, 0, "End file name table");
11862 /* Output one line number table into the .debug_line section. */
11864 static void
11865 output_one_line_info_table (dw_line_info_table *table)
11867 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11868 unsigned int current_line = 1;
11869 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11870 dw_line_info_entry *ent;
11871 size_t i;
11873 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11875 switch (ent->opcode)
11877 case LI_set_address:
11878 /* ??? Unfortunately, we have little choice here currently, and
11879 must always use the most general form. GCC does not know the
11880 address delta itself, so we can't use DW_LNS_advance_pc. Many
11881 ports do have length attributes which will give an upper bound
11882 on the address range. We could perhaps use length attributes
11883 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11884 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11886 /* This can handle any delta. This takes
11887 4+DWARF2_ADDR_SIZE bytes. */
11888 dw2_asm_output_data (1, 0, "set address %s", line_label);
11889 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11890 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11891 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11892 break;
11894 case LI_set_line:
11895 if (ent->val == current_line)
11897 /* We still need to start a new row, so output a copy insn. */
11898 dw2_asm_output_data (1, DW_LNS_copy,
11899 "copy line %u", current_line);
11901 else
11903 int line_offset = ent->val - current_line;
11904 int line_delta = line_offset - DWARF_LINE_BASE;
11906 current_line = ent->val;
11907 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11909 /* This can handle deltas from -10 to 234, using the current
11910 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11911 This takes 1 byte. */
11912 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11913 "line %u", current_line);
11915 else
11917 /* This can handle any delta. This takes at least 4 bytes,
11918 depending on the value being encoded. */
11919 dw2_asm_output_data (1, DW_LNS_advance_line,
11920 "advance to line %u", current_line);
11921 dw2_asm_output_data_sleb128 (line_offset, NULL);
11922 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11925 break;
11927 case LI_set_file:
11928 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11929 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11930 break;
11932 case LI_set_column:
11933 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11934 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11935 break;
11937 case LI_negate_stmt:
11938 current_is_stmt = !current_is_stmt;
11939 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11940 "is_stmt %d", current_is_stmt);
11941 break;
11943 case LI_set_prologue_end:
11944 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11945 "set prologue end");
11946 break;
11948 case LI_set_epilogue_begin:
11949 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11950 "set epilogue begin");
11951 break;
11953 case LI_set_discriminator:
11954 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11955 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11956 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11957 dw2_asm_output_data_uleb128 (ent->val, NULL);
11958 break;
11962 /* Emit debug info for the address of the end of the table. */
11963 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11964 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11965 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11966 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11968 dw2_asm_output_data (1, 0, "end sequence");
11969 dw2_asm_output_data_uleb128 (1, NULL);
11970 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11973 /* Output the source line number correspondence information. This
11974 information goes into the .debug_line section. */
11976 static void
11977 output_line_info (bool prologue_only)
11979 static unsigned int generation;
11980 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11981 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11982 bool saw_one = false;
11983 int opc;
11985 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11986 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11987 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11988 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11990 if (!XCOFF_DEBUGGING_INFO)
11992 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11993 dw2_asm_output_data (4, 0xffffffff,
11994 "Initial length escape value indicating 64-bit DWARF extension");
11995 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11996 "Length of Source Line Info");
11999 ASM_OUTPUT_LABEL (asm_out_file, l1);
12001 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
12002 if (dwarf_version >= 5)
12004 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12005 dw2_asm_output_data (1, 0, "Segment Size");
12007 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12008 ASM_OUTPUT_LABEL (asm_out_file, p1);
12010 /* Define the architecture-dependent minimum instruction length (in bytes).
12011 In this implementation of DWARF, this field is used for information
12012 purposes only. Since GCC generates assembly language, we have no
12013 a priori knowledge of how many instruction bytes are generated for each
12014 source line, and therefore can use only the DW_LNE_set_address and
12015 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12016 this as '1', which is "correct enough" for all architectures,
12017 and don't let the target override. */
12018 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12020 if (dwarf_version >= 4)
12021 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12022 "Maximum Operations Per Instruction");
12023 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12024 "Default is_stmt_start flag");
12025 dw2_asm_output_data (1, DWARF_LINE_BASE,
12026 "Line Base Value (Special Opcodes)");
12027 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12028 "Line Range Value (Special Opcodes)");
12029 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12030 "Special Opcode Base");
12032 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12034 int n_op_args;
12035 switch (opc)
12037 case DW_LNS_advance_pc:
12038 case DW_LNS_advance_line:
12039 case DW_LNS_set_file:
12040 case DW_LNS_set_column:
12041 case DW_LNS_fixed_advance_pc:
12042 case DW_LNS_set_isa:
12043 n_op_args = 1;
12044 break;
12045 default:
12046 n_op_args = 0;
12047 break;
12050 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12051 opc, n_op_args);
12054 /* Write out the information about the files we use. */
12055 output_file_names ();
12056 ASM_OUTPUT_LABEL (asm_out_file, p2);
12057 if (prologue_only)
12059 /* Output the marker for the end of the line number info. */
12060 ASM_OUTPUT_LABEL (asm_out_file, l2);
12061 return;
12064 if (separate_line_info)
12066 dw_line_info_table *table;
12067 size_t i;
12069 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12070 if (table->in_use)
12072 output_one_line_info_table (table);
12073 saw_one = true;
12076 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12078 output_one_line_info_table (cold_text_section_line_info);
12079 saw_one = true;
12082 /* ??? Some Darwin linkers crash on a .debug_line section with no
12083 sequences. Further, merely a DW_LNE_end_sequence entry is not
12084 sufficient -- the address column must also be initialized.
12085 Make sure to output at least one set_address/end_sequence pair,
12086 choosing .text since that section is always present. */
12087 if (text_section_line_info->in_use || !saw_one)
12088 output_one_line_info_table (text_section_line_info);
12090 /* Output the marker for the end of the line number info. */
12091 ASM_OUTPUT_LABEL (asm_out_file, l2);
12094 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12096 static inline bool
12097 need_endianity_attribute_p (bool reverse)
12099 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12102 /* Given a pointer to a tree node for some base type, return a pointer to
12103 a DIE that describes the given type. REVERSE is true if the type is
12104 to be interpreted in the reverse storage order wrt the target order.
12106 This routine must only be called for GCC type nodes that correspond to
12107 Dwarf base (fundamental) types. */
12109 static dw_die_ref
12110 base_type_die (tree type, bool reverse)
12112 dw_die_ref base_type_result;
12113 enum dwarf_type encoding;
12114 bool fpt_used = false;
12115 struct fixed_point_type_info fpt_info;
12116 tree type_bias = NULL_TREE;
12118 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12119 return 0;
12121 /* If this is a subtype that should not be emitted as a subrange type,
12122 use the base type. See subrange_type_for_debug_p. */
12123 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12124 type = TREE_TYPE (type);
12126 switch (TREE_CODE (type))
12128 case INTEGER_TYPE:
12129 if ((dwarf_version >= 4 || !dwarf_strict)
12130 && TYPE_NAME (type)
12131 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12132 && DECL_IS_BUILTIN (TYPE_NAME (type))
12133 && DECL_NAME (TYPE_NAME (type)))
12135 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12136 if (strcmp (name, "char16_t") == 0
12137 || strcmp (name, "char32_t") == 0)
12139 encoding = DW_ATE_UTF;
12140 break;
12143 if ((dwarf_version >= 3 || !dwarf_strict)
12144 && lang_hooks.types.get_fixed_point_type_info)
12146 memset (&fpt_info, 0, sizeof (fpt_info));
12147 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12149 fpt_used = true;
12150 encoding = ((TYPE_UNSIGNED (type))
12151 ? DW_ATE_unsigned_fixed
12152 : DW_ATE_signed_fixed);
12153 break;
12156 if (TYPE_STRING_FLAG (type))
12158 if (TYPE_UNSIGNED (type))
12159 encoding = DW_ATE_unsigned_char;
12160 else
12161 encoding = DW_ATE_signed_char;
12163 else if (TYPE_UNSIGNED (type))
12164 encoding = DW_ATE_unsigned;
12165 else
12166 encoding = DW_ATE_signed;
12168 if (!dwarf_strict
12169 && lang_hooks.types.get_type_bias)
12170 type_bias = lang_hooks.types.get_type_bias (type);
12171 break;
12173 case REAL_TYPE:
12174 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12176 if (dwarf_version >= 3 || !dwarf_strict)
12177 encoding = DW_ATE_decimal_float;
12178 else
12179 encoding = DW_ATE_lo_user;
12181 else
12182 encoding = DW_ATE_float;
12183 break;
12185 case FIXED_POINT_TYPE:
12186 if (!(dwarf_version >= 3 || !dwarf_strict))
12187 encoding = DW_ATE_lo_user;
12188 else if (TYPE_UNSIGNED (type))
12189 encoding = DW_ATE_unsigned_fixed;
12190 else
12191 encoding = DW_ATE_signed_fixed;
12192 break;
12194 /* Dwarf2 doesn't know anything about complex ints, so use
12195 a user defined type for it. */
12196 case COMPLEX_TYPE:
12197 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12198 encoding = DW_ATE_complex_float;
12199 else
12200 encoding = DW_ATE_lo_user;
12201 break;
12203 case BOOLEAN_TYPE:
12204 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12205 encoding = DW_ATE_boolean;
12206 break;
12208 default:
12209 /* No other TREE_CODEs are Dwarf fundamental types. */
12210 gcc_unreachable ();
12213 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12215 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12216 int_size_in_bytes (type));
12217 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12219 if (need_endianity_attribute_p (reverse))
12220 add_AT_unsigned (base_type_result, DW_AT_endianity,
12221 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12223 add_alignment_attribute (base_type_result, type);
12225 if (fpt_used)
12227 switch (fpt_info.scale_factor_kind)
12229 case fixed_point_scale_factor_binary:
12230 add_AT_int (base_type_result, DW_AT_binary_scale,
12231 fpt_info.scale_factor.binary);
12232 break;
12234 case fixed_point_scale_factor_decimal:
12235 add_AT_int (base_type_result, DW_AT_decimal_scale,
12236 fpt_info.scale_factor.decimal);
12237 break;
12239 case fixed_point_scale_factor_arbitrary:
12240 /* Arbitrary scale factors cannot be described in standard DWARF,
12241 yet. */
12242 if (!dwarf_strict)
12244 /* Describe the scale factor as a rational constant. */
12245 const dw_die_ref scale_factor
12246 = new_die (DW_TAG_constant, comp_unit_die (), type);
12248 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12249 fpt_info.scale_factor.arbitrary.numerator);
12250 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12251 fpt_info.scale_factor.arbitrary.denominator);
12253 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12255 break;
12257 default:
12258 gcc_unreachable ();
12262 if (type_bias)
12263 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12264 dw_scalar_form_constant
12265 | dw_scalar_form_exprloc
12266 | dw_scalar_form_reference,
12267 NULL);
12269 add_pubtype (type, base_type_result);
12271 return base_type_result;
12274 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12275 named 'auto' in its type: return true for it, false otherwise. */
12277 static inline bool
12278 is_cxx_auto (tree type)
12280 if (is_cxx ())
12282 tree name = TYPE_IDENTIFIER (type);
12283 if (name == get_identifier ("auto")
12284 || name == get_identifier ("decltype(auto)"))
12285 return true;
12287 return false;
12290 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12291 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12293 static inline int
12294 is_base_type (tree type)
12296 switch (TREE_CODE (type))
12298 case ERROR_MARK:
12299 case VOID_TYPE:
12300 case INTEGER_TYPE:
12301 case REAL_TYPE:
12302 case FIXED_POINT_TYPE:
12303 case COMPLEX_TYPE:
12304 case BOOLEAN_TYPE:
12305 case POINTER_BOUNDS_TYPE:
12306 return 1;
12308 case ARRAY_TYPE:
12309 case RECORD_TYPE:
12310 case UNION_TYPE:
12311 case QUAL_UNION_TYPE:
12312 case ENUMERAL_TYPE:
12313 case FUNCTION_TYPE:
12314 case METHOD_TYPE:
12315 case POINTER_TYPE:
12316 case REFERENCE_TYPE:
12317 case NULLPTR_TYPE:
12318 case OFFSET_TYPE:
12319 case LANG_TYPE:
12320 case VECTOR_TYPE:
12321 return 0;
12323 default:
12324 if (is_cxx_auto (type))
12325 return 0;
12326 gcc_unreachable ();
12329 return 0;
12332 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12333 node, return the size in bits for the type if it is a constant, or else
12334 return the alignment for the type if the type's size is not constant, or
12335 else return BITS_PER_WORD if the type actually turns out to be an
12336 ERROR_MARK node. */
12338 static inline unsigned HOST_WIDE_INT
12339 simple_type_size_in_bits (const_tree type)
12341 if (TREE_CODE (type) == ERROR_MARK)
12342 return BITS_PER_WORD;
12343 else if (TYPE_SIZE (type) == NULL_TREE)
12344 return 0;
12345 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12346 return tree_to_uhwi (TYPE_SIZE (type));
12347 else
12348 return TYPE_ALIGN (type);
12351 /* Similarly, but return an offset_int instead of UHWI. */
12353 static inline offset_int
12354 offset_int_type_size_in_bits (const_tree type)
12356 if (TREE_CODE (type) == ERROR_MARK)
12357 return BITS_PER_WORD;
12358 else if (TYPE_SIZE (type) == NULL_TREE)
12359 return 0;
12360 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12361 return wi::to_offset (TYPE_SIZE (type));
12362 else
12363 return TYPE_ALIGN (type);
12366 /* Given a pointer to a tree node for a subrange type, return a pointer
12367 to a DIE that describes the given type. */
12369 static dw_die_ref
12370 subrange_type_die (tree type, tree low, tree high, tree bias,
12371 dw_die_ref context_die)
12373 dw_die_ref subrange_die;
12374 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12376 if (context_die == NULL)
12377 context_die = comp_unit_die ();
12379 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12381 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12383 /* The size of the subrange type and its base type do not match,
12384 so we need to generate a size attribute for the subrange type. */
12385 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12388 add_alignment_attribute (subrange_die, type);
12390 if (low)
12391 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12392 if (high)
12393 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12394 if (bias && !dwarf_strict)
12395 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12396 dw_scalar_form_constant
12397 | dw_scalar_form_exprloc
12398 | dw_scalar_form_reference,
12399 NULL);
12401 return subrange_die;
12404 /* Returns the (const and/or volatile) cv_qualifiers associated with
12405 the decl node. This will normally be augmented with the
12406 cv_qualifiers of the underlying type in add_type_attribute. */
12408 static int
12409 decl_quals (const_tree decl)
12411 return ((TREE_READONLY (decl)
12412 /* The C++ front-end correctly marks reference-typed
12413 variables as readonly, but from a language (and debug
12414 info) standpoint they are not const-qualified. */
12415 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12416 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12417 | (TREE_THIS_VOLATILE (decl)
12418 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12421 /* Determine the TYPE whose qualifiers match the largest strict subset
12422 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12423 qualifiers outside QUAL_MASK. */
12425 static int
12426 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12428 tree t;
12429 int best_rank = 0, best_qual = 0, max_rank;
12431 type_quals &= qual_mask;
12432 max_rank = popcount_hwi (type_quals) - 1;
12434 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12435 t = TYPE_NEXT_VARIANT (t))
12437 int q = TYPE_QUALS (t) & qual_mask;
12439 if ((q & type_quals) == q && q != type_quals
12440 && check_base_type (t, type))
12442 int rank = popcount_hwi (q);
12444 if (rank > best_rank)
12446 best_rank = rank;
12447 best_qual = q;
12452 return best_qual;
12455 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12456 static const dwarf_qual_info_t dwarf_qual_info[] =
12458 { TYPE_QUAL_CONST, DW_TAG_const_type },
12459 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12460 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12461 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12463 static const unsigned int dwarf_qual_info_size
12464 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12466 /* If DIE is a qualified DIE of some base DIE with the same parent,
12467 return the base DIE, otherwise return NULL. Set MASK to the
12468 qualifiers added compared to the returned DIE. */
12470 static dw_die_ref
12471 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12473 unsigned int i;
12474 for (i = 0; i < dwarf_qual_info_size; i++)
12475 if (die->die_tag == dwarf_qual_info[i].t)
12476 break;
12477 if (i == dwarf_qual_info_size)
12478 return NULL;
12479 if (vec_safe_length (die->die_attr) != 1)
12480 return NULL;
12481 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12482 if (type == NULL || type->die_parent != die->die_parent)
12483 return NULL;
12484 *mask |= dwarf_qual_info[i].q;
12485 if (depth)
12487 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12488 if (ret)
12489 return ret;
12491 return type;
12494 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12495 entry that chains the modifiers specified by CV_QUALS in front of the
12496 given type. REVERSE is true if the type is to be interpreted in the
12497 reverse storage order wrt the target order. */
12499 static dw_die_ref
12500 modified_type_die (tree type, int cv_quals, bool reverse,
12501 dw_die_ref context_die)
12503 enum tree_code code = TREE_CODE (type);
12504 dw_die_ref mod_type_die;
12505 dw_die_ref sub_die = NULL;
12506 tree item_type = NULL;
12507 tree qualified_type;
12508 tree name, low, high;
12509 dw_die_ref mod_scope;
12510 /* Only these cv-qualifiers are currently handled. */
12511 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12512 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
12514 if (code == ERROR_MARK)
12515 return NULL;
12517 if (lang_hooks.types.get_debug_type)
12519 tree debug_type = lang_hooks.types.get_debug_type (type);
12521 if (debug_type != NULL_TREE && debug_type != type)
12522 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12525 cv_quals &= cv_qual_mask;
12527 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12528 tag modifier (and not an attribute) old consumers won't be able
12529 to handle it. */
12530 if (dwarf_version < 3)
12531 cv_quals &= ~TYPE_QUAL_RESTRICT;
12533 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12534 if (dwarf_version < 5)
12535 cv_quals &= ~TYPE_QUAL_ATOMIC;
12537 /* See if we already have the appropriately qualified variant of
12538 this type. */
12539 qualified_type = get_qualified_type (type, cv_quals);
12541 if (qualified_type == sizetype)
12543 /* Try not to expose the internal sizetype type's name. */
12544 if (TYPE_NAME (qualified_type)
12545 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12547 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12549 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12550 && (TYPE_PRECISION (t)
12551 == TYPE_PRECISION (qualified_type))
12552 && (TYPE_UNSIGNED (t)
12553 == TYPE_UNSIGNED (qualified_type)));
12554 qualified_type = t;
12556 else if (qualified_type == sizetype
12557 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12558 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12559 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12560 qualified_type = size_type_node;
12564 /* If we do, then we can just use its DIE, if it exists. */
12565 if (qualified_type)
12567 mod_type_die = lookup_type_die (qualified_type);
12569 /* DW_AT_endianity doesn't come from a qualifier on the type. */
12570 if (mod_type_die
12571 && (!need_endianity_attribute_p (reverse)
12572 || !is_base_type (type)
12573 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
12574 return mod_type_die;
12577 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12579 /* Handle C typedef types. */
12580 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12581 && !DECL_ARTIFICIAL (name))
12583 tree dtype = TREE_TYPE (name);
12585 if (qualified_type == dtype)
12587 tree origin = decl_ultimate_origin (name);
12589 /* Typedef variants that have an abstract origin don't get their own
12590 type DIE (see gen_typedef_die), so fall back on the ultimate
12591 abstract origin instead. */
12592 if (origin != NULL && origin != name)
12593 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12594 context_die);
12596 /* For a named type, use the typedef. */
12597 gen_type_die (qualified_type, context_die);
12598 return lookup_type_die (qualified_type);
12600 else
12602 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12603 dquals &= cv_qual_mask;
12604 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12605 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12606 /* cv-unqualified version of named type. Just use
12607 the unnamed type to which it refers. */
12608 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12609 reverse, context_die);
12610 /* Else cv-qualified version of named type; fall through. */
12614 mod_scope = scope_die_for (type, context_die);
12616 if (cv_quals)
12618 int sub_quals = 0, first_quals = 0;
12619 unsigned i;
12620 dw_die_ref first = NULL, last = NULL;
12622 /* Determine a lesser qualified type that most closely matches
12623 this one. Then generate DW_TAG_* entries for the remaining
12624 qualifiers. */
12625 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12626 cv_qual_mask);
12627 if (sub_quals && use_debug_types)
12629 bool needed = false;
12630 /* If emitting type units, make sure the order of qualifiers
12631 is canonical. Thus, start from unqualified type if
12632 an earlier qualifier is missing in sub_quals, but some later
12633 one is present there. */
12634 for (i = 0; i < dwarf_qual_info_size; i++)
12635 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12636 needed = true;
12637 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12639 sub_quals = 0;
12640 break;
12643 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12644 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12646 /* As not all intermediate qualified DIEs have corresponding
12647 tree types, ensure that qualified DIEs in the same scope
12648 as their DW_AT_type are emitted after their DW_AT_type,
12649 only with other qualified DIEs for the same type possibly
12650 in between them. Determine the range of such qualified
12651 DIEs now (first being the base type, last being corresponding
12652 last qualified DIE for it). */
12653 unsigned int count = 0;
12654 first = qualified_die_p (mod_type_die, &first_quals,
12655 dwarf_qual_info_size);
12656 if (first == NULL)
12657 first = mod_type_die;
12658 gcc_assert ((first_quals & ~sub_quals) == 0);
12659 for (count = 0, last = first;
12660 count < (1U << dwarf_qual_info_size);
12661 count++, last = last->die_sib)
12663 int quals = 0;
12664 if (last == mod_scope->die_child)
12665 break;
12666 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12667 != first)
12668 break;
12672 for (i = 0; i < dwarf_qual_info_size; i++)
12673 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12675 dw_die_ref d;
12676 if (first && first != last)
12678 for (d = first->die_sib; ; d = d->die_sib)
12680 int quals = 0;
12681 qualified_die_p (d, &quals, dwarf_qual_info_size);
12682 if (quals == (first_quals | dwarf_qual_info[i].q))
12683 break;
12684 if (d == last)
12686 d = NULL;
12687 break;
12690 if (d)
12692 mod_type_die = d;
12693 continue;
12696 if (first)
12698 d = ggc_cleared_alloc<die_node> ();
12699 d->die_tag = dwarf_qual_info[i].t;
12700 add_child_die_after (mod_scope, d, last);
12701 last = d;
12703 else
12704 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12705 if (mod_type_die)
12706 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12707 mod_type_die = d;
12708 first_quals |= dwarf_qual_info[i].q;
12711 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12713 dwarf_tag tag = DW_TAG_pointer_type;
12714 if (code == REFERENCE_TYPE)
12716 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12717 tag = DW_TAG_rvalue_reference_type;
12718 else
12719 tag = DW_TAG_reference_type;
12721 mod_type_die = new_die (tag, mod_scope, type);
12723 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12724 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12725 add_alignment_attribute (mod_type_die, type);
12726 item_type = TREE_TYPE (type);
12728 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12729 if (!ADDR_SPACE_GENERIC_P (as))
12731 int action = targetm.addr_space.debug (as);
12732 if (action >= 0)
12734 /* Positive values indicate an address_class. */
12735 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12737 else
12739 /* Negative values indicate an (inverted) segment base reg. */
12740 dw_loc_descr_ref d
12741 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12742 add_AT_loc (mod_type_die, DW_AT_segment, d);
12746 else if (code == INTEGER_TYPE
12747 && TREE_TYPE (type) != NULL_TREE
12748 && subrange_type_for_debug_p (type, &low, &high))
12750 tree bias = NULL_TREE;
12751 if (lang_hooks.types.get_type_bias)
12752 bias = lang_hooks.types.get_type_bias (type);
12753 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12754 item_type = TREE_TYPE (type);
12756 else if (is_base_type (type))
12757 mod_type_die = base_type_die (type, reverse);
12758 else
12760 gen_type_die (type, context_die);
12762 /* We have to get the type_main_variant here (and pass that to the
12763 `lookup_type_die' routine) because the ..._TYPE node we have
12764 might simply be a *copy* of some original type node (where the
12765 copy was created to help us keep track of typedef names) and
12766 that copy might have a different TYPE_UID from the original
12767 ..._TYPE node. */
12768 if (TREE_CODE (type) == FUNCTION_TYPE
12769 || TREE_CODE (type) == METHOD_TYPE)
12771 /* For function/method types, can't just use type_main_variant here,
12772 because that can have different ref-qualifiers for C++,
12773 but try to canonicalize. */
12774 tree main = TYPE_MAIN_VARIANT (type);
12775 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12776 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12777 && check_base_type (t, main)
12778 && check_lang_type (t, type))
12779 return lookup_type_die (t);
12780 return lookup_type_die (type);
12782 else if (TREE_CODE (type) != VECTOR_TYPE
12783 && TREE_CODE (type) != ARRAY_TYPE)
12784 return lookup_type_die (type_main_variant (type));
12785 else
12786 /* Vectors have the debugging information in the type,
12787 not the main variant. */
12788 return lookup_type_die (type);
12791 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12792 don't output a DW_TAG_typedef, since there isn't one in the
12793 user's program; just attach a DW_AT_name to the type.
12794 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12795 if the base type already has the same name. */
12796 if (name
12797 && ((TREE_CODE (name) != TYPE_DECL
12798 && (qualified_type == TYPE_MAIN_VARIANT (type)
12799 || (cv_quals == TYPE_UNQUALIFIED)))
12800 || (TREE_CODE (name) == TYPE_DECL
12801 && TREE_TYPE (name) == qualified_type
12802 && DECL_NAME (name))))
12804 if (TREE_CODE (name) == TYPE_DECL)
12805 /* Could just call add_name_and_src_coords_attributes here,
12806 but since this is a builtin type it doesn't have any
12807 useful source coordinates anyway. */
12808 name = DECL_NAME (name);
12809 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12811 /* This probably indicates a bug. */
12812 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12814 name = TYPE_IDENTIFIER (type);
12815 add_name_attribute (mod_type_die,
12816 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12819 if (qualified_type)
12820 equate_type_number_to_die (qualified_type, mod_type_die);
12822 if (item_type)
12823 /* We must do this after the equate_type_number_to_die call, in case
12824 this is a recursive type. This ensures that the modified_type_die
12825 recursion will terminate even if the type is recursive. Recursive
12826 types are possible in Ada. */
12827 sub_die = modified_type_die (item_type,
12828 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12829 reverse,
12830 context_die);
12832 if (sub_die != NULL)
12833 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12835 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12836 if (TYPE_ARTIFICIAL (type))
12837 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12839 return mod_type_die;
12842 /* Generate DIEs for the generic parameters of T.
12843 T must be either a generic type or a generic function.
12844 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12846 static void
12847 gen_generic_params_dies (tree t)
12849 tree parms, args;
12850 int parms_num, i;
12851 dw_die_ref die = NULL;
12852 int non_default;
12854 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12855 return;
12857 if (TYPE_P (t))
12858 die = lookup_type_die (t);
12859 else if (DECL_P (t))
12860 die = lookup_decl_die (t);
12862 gcc_assert (die);
12864 parms = lang_hooks.get_innermost_generic_parms (t);
12865 if (!parms)
12866 /* T has no generic parameter. It means T is neither a generic type
12867 or function. End of story. */
12868 return;
12870 parms_num = TREE_VEC_LENGTH (parms);
12871 args = lang_hooks.get_innermost_generic_args (t);
12872 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12873 non_default = int_cst_value (TREE_CHAIN (args));
12874 else
12875 non_default = TREE_VEC_LENGTH (args);
12876 for (i = 0; i < parms_num; i++)
12878 tree parm, arg, arg_pack_elems;
12879 dw_die_ref parm_die;
12881 parm = TREE_VEC_ELT (parms, i);
12882 arg = TREE_VEC_ELT (args, i);
12883 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12884 gcc_assert (parm && TREE_VALUE (parm) && arg);
12886 if (parm && TREE_VALUE (parm) && arg)
12888 /* If PARM represents a template parameter pack,
12889 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12890 by DW_TAG_template_*_parameter DIEs for the argument
12891 pack elements of ARG. Note that ARG would then be
12892 an argument pack. */
12893 if (arg_pack_elems)
12894 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12895 arg_pack_elems,
12896 die);
12897 else
12898 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12899 true /* emit name */, die);
12900 if (i >= non_default)
12901 add_AT_flag (parm_die, DW_AT_default_value, 1);
12906 /* Create and return a DIE for PARM which should be
12907 the representation of a generic type parameter.
12908 For instance, in the C++ front end, PARM would be a template parameter.
12909 ARG is the argument to PARM.
12910 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12911 name of the PARM.
12912 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12913 as a child node. */
12915 static dw_die_ref
12916 generic_parameter_die (tree parm, tree arg,
12917 bool emit_name_p,
12918 dw_die_ref parent_die)
12920 dw_die_ref tmpl_die = NULL;
12921 const char *name = NULL;
12923 if (!parm || !DECL_NAME (parm) || !arg)
12924 return NULL;
12926 /* We support non-type generic parameters and arguments,
12927 type generic parameters and arguments, as well as
12928 generic generic parameters (a.k.a. template template parameters in C++)
12929 and arguments. */
12930 if (TREE_CODE (parm) == PARM_DECL)
12931 /* PARM is a nontype generic parameter */
12932 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12933 else if (TREE_CODE (parm) == TYPE_DECL)
12934 /* PARM is a type generic parameter. */
12935 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12936 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12937 /* PARM is a generic generic parameter.
12938 Its DIE is a GNU extension. It shall have a
12939 DW_AT_name attribute to represent the name of the template template
12940 parameter, and a DW_AT_GNU_template_name attribute to represent the
12941 name of the template template argument. */
12942 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12943 parent_die, parm);
12944 else
12945 gcc_unreachable ();
12947 if (tmpl_die)
12949 tree tmpl_type;
12951 /* If PARM is a generic parameter pack, it means we are
12952 emitting debug info for a template argument pack element.
12953 In other terms, ARG is a template argument pack element.
12954 In that case, we don't emit any DW_AT_name attribute for
12955 the die. */
12956 if (emit_name_p)
12958 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12959 gcc_assert (name);
12960 add_AT_string (tmpl_die, DW_AT_name, name);
12963 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12965 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12966 TMPL_DIE should have a child DW_AT_type attribute that is set
12967 to the type of the argument to PARM, which is ARG.
12968 If PARM is a type generic parameter, TMPL_DIE should have a
12969 child DW_AT_type that is set to ARG. */
12970 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12971 add_type_attribute (tmpl_die, tmpl_type,
12972 (TREE_THIS_VOLATILE (tmpl_type)
12973 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12974 false, parent_die);
12976 else
12978 /* So TMPL_DIE is a DIE representing a
12979 a generic generic template parameter, a.k.a template template
12980 parameter in C++ and arg is a template. */
12982 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12983 to the name of the argument. */
12984 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12985 if (name)
12986 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12989 if (TREE_CODE (parm) == PARM_DECL)
12990 /* So PARM is a non-type generic parameter.
12991 DWARF3 5.6.8 says we must set a DW_AT_const_value child
12992 attribute of TMPL_DIE which value represents the value
12993 of ARG.
12994 We must be careful here:
12995 The value of ARG might reference some function decls.
12996 We might currently be emitting debug info for a generic
12997 type and types are emitted before function decls, we don't
12998 know if the function decls referenced by ARG will actually be
12999 emitted after cgraph computations.
13000 So must defer the generation of the DW_AT_const_value to
13001 after cgraph is ready. */
13002 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13005 return tmpl_die;
13008 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13009 PARM_PACK must be a template parameter pack. The returned DIE
13010 will be child DIE of PARENT_DIE. */
13012 static dw_die_ref
13013 template_parameter_pack_die (tree parm_pack,
13014 tree parm_pack_args,
13015 dw_die_ref parent_die)
13017 dw_die_ref die;
13018 int j;
13020 gcc_assert (parent_die && parm_pack);
13022 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13023 add_name_and_src_coords_attributes (die, parm_pack);
13024 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13025 generic_parameter_die (parm_pack,
13026 TREE_VEC_ELT (parm_pack_args, j),
13027 false /* Don't emit DW_AT_name */,
13028 die);
13029 return die;
13032 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13033 an enumerated type. */
13035 static inline int
13036 type_is_enum (const_tree type)
13038 return TREE_CODE (type) == ENUMERAL_TYPE;
13041 /* Return the DBX register number described by a given RTL node. */
13043 static unsigned int
13044 dbx_reg_number (const_rtx rtl)
13046 unsigned regno = REGNO (rtl);
13048 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13050 #ifdef LEAF_REG_REMAP
13051 if (crtl->uses_only_leaf_regs)
13053 int leaf_reg = LEAF_REG_REMAP (regno);
13054 if (leaf_reg != -1)
13055 regno = (unsigned) leaf_reg;
13057 #endif
13059 regno = DBX_REGISTER_NUMBER (regno);
13060 gcc_assert (regno != INVALID_REGNUM);
13061 return regno;
13064 /* Optionally add a DW_OP_piece term to a location description expression.
13065 DW_OP_piece is only added if the location description expression already
13066 doesn't end with DW_OP_piece. */
13068 static void
13069 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13071 dw_loc_descr_ref loc;
13073 if (*list_head != NULL)
13075 /* Find the end of the chain. */
13076 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13079 if (loc->dw_loc_opc != DW_OP_piece)
13080 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13084 /* Return a location descriptor that designates a machine register or
13085 zero if there is none. */
13087 static dw_loc_descr_ref
13088 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13090 rtx regs;
13092 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13093 return 0;
13095 /* We only use "frame base" when we're sure we're talking about the
13096 post-prologue local stack frame. We do this by *not* running
13097 register elimination until this point, and recognizing the special
13098 argument pointer and soft frame pointer rtx's.
13099 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13100 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13101 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13103 dw_loc_descr_ref result = NULL;
13105 if (dwarf_version >= 4 || !dwarf_strict)
13107 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13108 initialized);
13109 if (result)
13110 add_loc_descr (&result,
13111 new_loc_descr (DW_OP_stack_value, 0, 0));
13113 return result;
13116 regs = targetm.dwarf_register_span (rtl);
13118 if (REG_NREGS (rtl) > 1 || regs)
13119 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13120 else
13122 unsigned int dbx_regnum = dbx_reg_number (rtl);
13123 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13124 return 0;
13125 return one_reg_loc_descriptor (dbx_regnum, initialized);
13129 /* Return a location descriptor that designates a machine register for
13130 a given hard register number. */
13132 static dw_loc_descr_ref
13133 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13135 dw_loc_descr_ref reg_loc_descr;
13137 if (regno <= 31)
13138 reg_loc_descr
13139 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13140 else
13141 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13143 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13144 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13146 return reg_loc_descr;
13149 /* Given an RTL of a register, return a location descriptor that
13150 designates a value that spans more than one register. */
13152 static dw_loc_descr_ref
13153 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13154 enum var_init_status initialized)
13156 int size, i;
13157 dw_loc_descr_ref loc_result = NULL;
13159 /* Simple, contiguous registers. */
13160 if (regs == NULL_RTX)
13162 unsigned reg = REGNO (rtl);
13163 int nregs;
13165 #ifdef LEAF_REG_REMAP
13166 if (crtl->uses_only_leaf_regs)
13168 int leaf_reg = LEAF_REG_REMAP (reg);
13169 if (leaf_reg != -1)
13170 reg = (unsigned) leaf_reg;
13172 #endif
13174 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13175 nregs = REG_NREGS (rtl);
13177 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13179 loc_result = NULL;
13180 while (nregs--)
13182 dw_loc_descr_ref t;
13184 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13185 VAR_INIT_STATUS_INITIALIZED);
13186 add_loc_descr (&loc_result, t);
13187 add_loc_descr_op_piece (&loc_result, size);
13188 ++reg;
13190 return loc_result;
13193 /* Now onto stupid register sets in non contiguous locations. */
13195 gcc_assert (GET_CODE (regs) == PARALLEL);
13197 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13198 loc_result = NULL;
13200 for (i = 0; i < XVECLEN (regs, 0); ++i)
13202 dw_loc_descr_ref t;
13204 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13205 VAR_INIT_STATUS_INITIALIZED);
13206 add_loc_descr (&loc_result, t);
13207 add_loc_descr_op_piece (&loc_result, size);
13210 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13211 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13212 return loc_result;
13215 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13217 /* Return a location descriptor that designates a constant i,
13218 as a compound operation from constant (i >> shift), constant shift
13219 and DW_OP_shl. */
13221 static dw_loc_descr_ref
13222 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13224 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13225 add_loc_descr (&ret, int_loc_descriptor (shift));
13226 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13227 return ret;
13230 /* Return a location descriptor that designates a constant. */
13232 static dw_loc_descr_ref
13233 int_loc_descriptor (HOST_WIDE_INT i)
13235 enum dwarf_location_atom op;
13237 /* Pick the smallest representation of a constant, rather than just
13238 defaulting to the LEB encoding. */
13239 if (i >= 0)
13241 int clz = clz_hwi (i);
13242 int ctz = ctz_hwi (i);
13243 if (i <= 31)
13244 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13245 else if (i <= 0xff)
13246 op = DW_OP_const1u;
13247 else if (i <= 0xffff)
13248 op = DW_OP_const2u;
13249 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13250 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13251 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13252 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13253 while DW_OP_const4u is 5 bytes. */
13254 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13255 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13256 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13257 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13258 while DW_OP_const4u is 5 bytes. */
13259 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13261 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13262 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13263 <= 4)
13265 /* As i >= 2**31, the double cast above will yield a negative number.
13266 Since wrapping is defined in DWARF expressions we can output big
13267 positive integers as small negative ones, regardless of the size
13268 of host wide ints.
13270 Here, since the evaluator will handle 32-bit values and since i >=
13271 2**31, we know it's going to be interpreted as a negative literal:
13272 store it this way if we can do better than 5 bytes this way. */
13273 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13275 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13276 op = DW_OP_const4u;
13278 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13279 least 6 bytes: see if we can do better before falling back to it. */
13280 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13281 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13282 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13283 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13284 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13285 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13286 >= HOST_BITS_PER_WIDE_INT)
13287 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13288 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13289 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13290 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13291 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13292 && size_of_uleb128 (i) > 6)
13293 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13294 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13295 else
13296 op = DW_OP_constu;
13298 else
13300 if (i >= -0x80)
13301 op = DW_OP_const1s;
13302 else if (i >= -0x8000)
13303 op = DW_OP_const2s;
13304 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13306 if (size_of_int_loc_descriptor (i) < 5)
13308 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13309 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13310 return ret;
13312 op = DW_OP_const4s;
13314 else
13316 if (size_of_int_loc_descriptor (i)
13317 < (unsigned long) 1 + size_of_sleb128 (i))
13319 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13320 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13321 return ret;
13323 op = DW_OP_consts;
13327 return new_loc_descr (op, i, 0);
13330 /* Likewise, for unsigned constants. */
13332 static dw_loc_descr_ref
13333 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13335 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13336 const unsigned HOST_WIDE_INT max_uint
13337 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13339 /* If possible, use the clever signed constants handling. */
13340 if (i <= max_int)
13341 return int_loc_descriptor ((HOST_WIDE_INT) i);
13343 /* Here, we are left with positive numbers that cannot be represented as
13344 HOST_WIDE_INT, i.e.:
13345 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13347 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13348 whereas may be better to output a negative integer: thanks to integer
13349 wrapping, we know that:
13350 x = x - 2 ** DWARF2_ADDR_SIZE
13351 = x - 2 * (max (HOST_WIDE_INT) + 1)
13352 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13353 small negative integers. Let's try that in cases it will clearly improve
13354 the encoding: there is no gain turning DW_OP_const4u into
13355 DW_OP_const4s. */
13356 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13357 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13358 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13360 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13362 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13363 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13364 const HOST_WIDE_INT second_shift
13365 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13367 /* So we finally have:
13368 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13369 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13370 return int_loc_descriptor (second_shift);
13373 /* Last chance: fallback to a simple constant operation. */
13374 return new_loc_descr
13375 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13376 ? DW_OP_const4u
13377 : DW_OP_const8u,
13378 i, 0);
13381 /* Generate and return a location description that computes the unsigned
13382 comparison of the two stack top entries (a OP b where b is the top-most
13383 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13384 LE_EXPR, GT_EXPR or GE_EXPR. */
13386 static dw_loc_descr_ref
13387 uint_comparison_loc_list (enum tree_code kind)
13389 enum dwarf_location_atom op, flip_op;
13390 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13392 switch (kind)
13394 case LT_EXPR:
13395 op = DW_OP_lt;
13396 break;
13397 case LE_EXPR:
13398 op = DW_OP_le;
13399 break;
13400 case GT_EXPR:
13401 op = DW_OP_gt;
13402 break;
13403 case GE_EXPR:
13404 op = DW_OP_ge;
13405 break;
13406 default:
13407 gcc_unreachable ();
13410 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13411 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13413 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13414 possible to perform unsigned comparisons: we just have to distinguish
13415 three cases:
13417 1. when a and b have the same sign (as signed integers); then we should
13418 return: a OP(signed) b;
13420 2. when a is a negative signed integer while b is a positive one, then a
13421 is a greater unsigned integer than b; likewise when a and b's roles
13422 are flipped.
13424 So first, compare the sign of the two operands. */
13425 ret = new_loc_descr (DW_OP_over, 0, 0);
13426 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13427 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13428 /* If they have different signs (i.e. they have different sign bits), then
13429 the stack top value has now the sign bit set and thus it's smaller than
13430 zero. */
13431 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13432 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13433 add_loc_descr (&ret, bra_node);
13435 /* We are in case 1. At this point, we know both operands have the same
13436 sign, to it's safe to use the built-in signed comparison. */
13437 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13438 add_loc_descr (&ret, jmp_node);
13440 /* We are in case 2. Here, we know both operands do not have the same sign,
13441 so we have to flip the signed comparison. */
13442 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13443 tmp = new_loc_descr (flip_op, 0, 0);
13444 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13445 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13446 add_loc_descr (&ret, tmp);
13448 /* This dummy operation is necessary to make the two branches join. */
13449 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13450 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13451 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13452 add_loc_descr (&ret, tmp);
13454 return ret;
13457 /* Likewise, but takes the location description lists (might be destructive on
13458 them). Return NULL if either is NULL or if concatenation fails. */
13460 static dw_loc_list_ref
13461 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13462 enum tree_code kind)
13464 if (left == NULL || right == NULL)
13465 return NULL;
13467 add_loc_list (&left, right);
13468 if (left == NULL)
13469 return NULL;
13471 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13472 return left;
13475 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13476 without actually allocating it. */
13478 static unsigned long
13479 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13481 return size_of_int_loc_descriptor (i >> shift)
13482 + size_of_int_loc_descriptor (shift)
13483 + 1;
13486 /* Return size_of_locs (int_loc_descriptor (i)) without
13487 actually allocating it. */
13489 static unsigned long
13490 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13492 unsigned long s;
13494 if (i >= 0)
13496 int clz, ctz;
13497 if (i <= 31)
13498 return 1;
13499 else if (i <= 0xff)
13500 return 2;
13501 else if (i <= 0xffff)
13502 return 3;
13503 clz = clz_hwi (i);
13504 ctz = ctz_hwi (i);
13505 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13506 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13507 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13508 - clz - 5);
13509 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13510 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13511 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13512 - clz - 8);
13513 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13514 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13515 <= 4)
13516 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13517 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13518 return 5;
13519 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13520 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13521 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13522 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13523 - clz - 8);
13524 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13525 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13526 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13527 - clz - 16);
13528 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13529 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13530 && s > 6)
13531 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13532 - clz - 32);
13533 else
13534 return 1 + s;
13536 else
13538 if (i >= -0x80)
13539 return 2;
13540 else if (i >= -0x8000)
13541 return 3;
13542 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13544 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13546 s = size_of_int_loc_descriptor (-i) + 1;
13547 if (s < 5)
13548 return s;
13550 return 5;
13552 else
13554 unsigned long r = 1 + size_of_sleb128 (i);
13555 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13557 s = size_of_int_loc_descriptor (-i) + 1;
13558 if (s < r)
13559 return s;
13561 return r;
13566 /* Return loc description representing "address" of integer value.
13567 This can appear only as toplevel expression. */
13569 static dw_loc_descr_ref
13570 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13572 int litsize;
13573 dw_loc_descr_ref loc_result = NULL;
13575 if (!(dwarf_version >= 4 || !dwarf_strict))
13576 return NULL;
13578 litsize = size_of_int_loc_descriptor (i);
13579 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13580 is more compact. For DW_OP_stack_value we need:
13581 litsize + 1 (DW_OP_stack_value)
13582 and for DW_OP_implicit_value:
13583 1 (DW_OP_implicit_value) + 1 (length) + size. */
13584 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13586 loc_result = int_loc_descriptor (i);
13587 add_loc_descr (&loc_result,
13588 new_loc_descr (DW_OP_stack_value, 0, 0));
13589 return loc_result;
13592 loc_result = new_loc_descr (DW_OP_implicit_value,
13593 size, 0);
13594 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13595 loc_result->dw_loc_oprnd2.v.val_int = i;
13596 return loc_result;
13599 /* Return a location descriptor that designates a base+offset location. */
13601 static dw_loc_descr_ref
13602 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13603 enum var_init_status initialized)
13605 unsigned int regno;
13606 dw_loc_descr_ref result;
13607 dw_fde_ref fde = cfun->fde;
13609 /* We only use "frame base" when we're sure we're talking about the
13610 post-prologue local stack frame. We do this by *not* running
13611 register elimination until this point, and recognizing the special
13612 argument pointer and soft frame pointer rtx's. */
13613 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13615 rtx elim = (ira_use_lra_p
13616 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13617 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13619 if (elim != reg)
13621 if (GET_CODE (elim) == PLUS)
13623 offset += INTVAL (XEXP (elim, 1));
13624 elim = XEXP (elim, 0);
13626 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13627 && (elim == hard_frame_pointer_rtx
13628 || elim == stack_pointer_rtx))
13629 || elim == (frame_pointer_needed
13630 ? hard_frame_pointer_rtx
13631 : stack_pointer_rtx));
13633 /* If drap register is used to align stack, use frame
13634 pointer + offset to access stack variables. If stack
13635 is aligned without drap, use stack pointer + offset to
13636 access stack variables. */
13637 if (crtl->stack_realign_tried
13638 && reg == frame_pointer_rtx)
13640 int base_reg
13641 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13642 ? HARD_FRAME_POINTER_REGNUM
13643 : REGNO (elim));
13644 return new_reg_loc_descr (base_reg, offset);
13647 gcc_assert (frame_pointer_fb_offset_valid);
13648 offset += frame_pointer_fb_offset;
13649 return new_loc_descr (DW_OP_fbreg, offset, 0);
13653 regno = REGNO (reg);
13654 #ifdef LEAF_REG_REMAP
13655 if (crtl->uses_only_leaf_regs)
13657 int leaf_reg = LEAF_REG_REMAP (regno);
13658 if (leaf_reg != -1)
13659 regno = (unsigned) leaf_reg;
13661 #endif
13662 regno = DWARF_FRAME_REGNUM (regno);
13664 if (!optimize && fde
13665 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
13667 /* Use cfa+offset to represent the location of arguments passed
13668 on the stack when drap is used to align stack.
13669 Only do this when not optimizing, for optimized code var-tracking
13670 is supposed to track where the arguments live and the register
13671 used as vdrap or drap in some spot might be used for something
13672 else in other part of the routine. */
13673 return new_loc_descr (DW_OP_fbreg, offset, 0);
13676 if (regno <= 31)
13677 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13678 offset, 0);
13679 else
13680 result = new_loc_descr (DW_OP_bregx, regno, offset);
13682 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13683 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13685 return result;
13688 /* Return true if this RTL expression describes a base+offset calculation. */
13690 static inline int
13691 is_based_loc (const_rtx rtl)
13693 return (GET_CODE (rtl) == PLUS
13694 && ((REG_P (XEXP (rtl, 0))
13695 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13696 && CONST_INT_P (XEXP (rtl, 1)))));
13699 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13700 failed. */
13702 static dw_loc_descr_ref
13703 tls_mem_loc_descriptor (rtx mem)
13705 tree base;
13706 dw_loc_descr_ref loc_result;
13708 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13709 return NULL;
13711 base = get_base_address (MEM_EXPR (mem));
13712 if (base == NULL
13713 || !VAR_P (base)
13714 || !DECL_THREAD_LOCAL_P (base))
13715 return NULL;
13717 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13718 if (loc_result == NULL)
13719 return NULL;
13721 if (MEM_OFFSET (mem))
13722 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13724 return loc_result;
13727 /* Output debug info about reason why we failed to expand expression as dwarf
13728 expression. */
13730 static void
13731 expansion_failed (tree expr, rtx rtl, char const *reason)
13733 if (dump_file && (dump_flags & TDF_DETAILS))
13735 fprintf (dump_file, "Failed to expand as dwarf: ");
13736 if (expr)
13737 print_generic_expr (dump_file, expr, dump_flags);
13738 if (rtl)
13740 fprintf (dump_file, "\n");
13741 print_rtl (dump_file, rtl);
13743 fprintf (dump_file, "\nReason: %s\n", reason);
13747 /* Helper function for const_ok_for_output. */
13749 static bool
13750 const_ok_for_output_1 (rtx rtl)
13752 if (GET_CODE (rtl) == UNSPEC)
13754 /* If delegitimize_address couldn't do anything with the UNSPEC, 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 if (targetm.const_not_ok_for_debug_p (rtl))
13783 expansion_failed (NULL_TREE, rtl,
13784 "Expression rejected for debug by the backend.\n");
13785 return false;
13788 /* FIXME: Refer to PR60655. It is possible for simplification
13789 of rtl expressions in var tracking to produce such expressions.
13790 We should really identify / validate expressions
13791 enclosed in CONST that can be handled by assemblers on various
13792 targets and only handle legitimate cases here. */
13793 if (GET_CODE (rtl) != SYMBOL_REF)
13795 if (GET_CODE (rtl) == NOT)
13796 return false;
13797 return true;
13800 if (CONSTANT_POOL_ADDRESS_P (rtl))
13802 bool marked;
13803 get_pool_constant_mark (rtl, &marked);
13804 /* If all references to this pool constant were optimized away,
13805 it was not output and thus we can't represent it. */
13806 if (!marked)
13808 expansion_failed (NULL_TREE, rtl,
13809 "Constant was removed from constant pool.\n");
13810 return false;
13814 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13815 return false;
13817 /* Avoid references to external symbols in debug info, on several targets
13818 the linker might even refuse to link when linking a shared library,
13819 and in many other cases the relocations for .debug_info/.debug_loc are
13820 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13821 to be defined within the same shared library or executable are fine. */
13822 if (SYMBOL_REF_EXTERNAL_P (rtl))
13824 tree decl = SYMBOL_REF_DECL (rtl);
13826 if (decl == NULL || !targetm.binds_local_p (decl))
13828 expansion_failed (NULL_TREE, rtl,
13829 "Symbol not defined in current TU.\n");
13830 return false;
13834 return true;
13837 /* Return true if constant RTL can be emitted in DW_OP_addr or
13838 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13839 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13841 static bool
13842 const_ok_for_output (rtx rtl)
13844 if (GET_CODE (rtl) == SYMBOL_REF)
13845 return const_ok_for_output_1 (rtl);
13847 if (GET_CODE (rtl) == CONST)
13849 subrtx_var_iterator::array_type array;
13850 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13851 if (!const_ok_for_output_1 (*iter))
13852 return false;
13853 return true;
13856 return true;
13859 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13860 if possible, NULL otherwise. */
13862 static dw_die_ref
13863 base_type_for_mode (machine_mode mode, bool unsignedp)
13865 dw_die_ref type_die;
13866 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13868 if (type == NULL)
13869 return NULL;
13870 switch (TREE_CODE (type))
13872 case INTEGER_TYPE:
13873 case REAL_TYPE:
13874 break;
13875 default:
13876 return NULL;
13878 type_die = lookup_type_die (type);
13879 if (!type_die)
13880 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13881 comp_unit_die ());
13882 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13883 return NULL;
13884 return type_die;
13887 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13888 type matching MODE, or, if MODE is narrower than or as wide as
13889 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13890 possible. */
13892 static dw_loc_descr_ref
13893 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13895 machine_mode outer_mode = mode;
13896 dw_die_ref type_die;
13897 dw_loc_descr_ref cvt;
13899 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13901 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13902 return op;
13904 type_die = base_type_for_mode (outer_mode, 1);
13905 if (type_die == NULL)
13906 return NULL;
13907 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13908 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13909 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13910 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13911 add_loc_descr (&op, cvt);
13912 return op;
13915 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13917 static dw_loc_descr_ref
13918 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13919 dw_loc_descr_ref op1)
13921 dw_loc_descr_ref ret = op0;
13922 add_loc_descr (&ret, op1);
13923 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13924 if (STORE_FLAG_VALUE != 1)
13926 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13927 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13929 return ret;
13932 /* Subroutine of scompare_loc_descriptor for the case in which we're
13933 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13934 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
13936 static dw_loc_descr_ref
13937 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
13938 scalar_int_mode op_mode,
13939 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13941 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13942 dw_loc_descr_ref cvt;
13944 if (type_die == NULL)
13945 return NULL;
13946 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13947 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13948 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13949 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13950 add_loc_descr (&op0, cvt);
13951 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13952 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13953 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13954 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13955 add_loc_descr (&op1, cvt);
13956 return compare_loc_descriptor (op, op0, op1);
13959 /* Subroutine of scompare_loc_descriptor for the case in which we're
13960 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
13961 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
13963 static dw_loc_descr_ref
13964 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
13965 scalar_int_mode op_mode,
13966 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
13968 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13969 /* For eq/ne, if the operands are known to be zero-extended,
13970 there is no need to do the fancy shifting up. */
13971 if (op == DW_OP_eq || op == DW_OP_ne)
13973 dw_loc_descr_ref last0, last1;
13974 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13976 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13978 /* deref_size zero extends, and for constants we can check
13979 whether they are zero extended or not. */
13980 if (((last0->dw_loc_opc == DW_OP_deref_size
13981 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13982 || (CONST_INT_P (XEXP (rtl, 0))
13983 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13984 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13985 && ((last1->dw_loc_opc == DW_OP_deref_size
13986 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13987 || (CONST_INT_P (XEXP (rtl, 1))
13988 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13989 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13990 return compare_loc_descriptor (op, op0, op1);
13992 /* EQ/NE comparison against constant in narrower type than
13993 DWARF2_ADDR_SIZE can be performed either as
13994 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
13995 DW_OP_{eq,ne}
13997 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
13998 DW_OP_{eq,ne}. Pick whatever is shorter. */
13999 if (CONST_INT_P (XEXP (rtl, 1))
14000 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14001 && (size_of_int_loc_descriptor (shift) + 1
14002 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14003 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14004 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14005 & GET_MODE_MASK (op_mode))))
14007 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14008 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14009 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14010 & GET_MODE_MASK (op_mode));
14011 return compare_loc_descriptor (op, op0, op1);
14014 add_loc_descr (&op0, int_loc_descriptor (shift));
14015 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14016 if (CONST_INT_P (XEXP (rtl, 1)))
14017 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14018 else
14020 add_loc_descr (&op1, int_loc_descriptor (shift));
14021 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14023 return compare_loc_descriptor (op, op0, op1);
14026 /* Return location descriptor for unsigned comparison OP RTL. */
14028 static dw_loc_descr_ref
14029 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14030 machine_mode mem_mode)
14032 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14033 dw_loc_descr_ref op0, op1;
14035 if (op_mode == VOIDmode)
14036 op_mode = GET_MODE (XEXP (rtl, 1));
14037 if (op_mode == VOIDmode)
14038 return NULL;
14040 scalar_int_mode int_op_mode;
14041 if (dwarf_strict
14042 && dwarf_version < 5
14043 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14044 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14045 return NULL;
14047 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14048 VAR_INIT_STATUS_INITIALIZED);
14049 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14050 VAR_INIT_STATUS_INITIALIZED);
14052 if (op0 == NULL || op1 == NULL)
14053 return NULL;
14055 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14057 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14058 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14060 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14061 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14063 return compare_loc_descriptor (op, op0, op1);
14066 /* Return location descriptor for unsigned comparison OP RTL. */
14068 static dw_loc_descr_ref
14069 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14070 machine_mode mem_mode)
14072 dw_loc_descr_ref op0, op1;
14074 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14075 if (test_op_mode == VOIDmode)
14076 test_op_mode = GET_MODE (XEXP (rtl, 1));
14078 scalar_int_mode op_mode;
14079 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14080 return NULL;
14082 if (dwarf_strict
14083 && dwarf_version < 5
14084 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14085 return NULL;
14087 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14088 VAR_INIT_STATUS_INITIALIZED);
14089 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14090 VAR_INIT_STATUS_INITIALIZED);
14092 if (op0 == NULL || op1 == NULL)
14093 return NULL;
14095 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14097 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14098 dw_loc_descr_ref last0, last1;
14099 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14101 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14103 if (CONST_INT_P (XEXP (rtl, 0)))
14104 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14105 /* deref_size zero extends, so no need to mask it again. */
14106 else if (last0->dw_loc_opc != DW_OP_deref_size
14107 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14109 add_loc_descr (&op0, int_loc_descriptor (mask));
14110 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14112 if (CONST_INT_P (XEXP (rtl, 1)))
14113 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14114 /* deref_size zero extends, so no need to mask it again. */
14115 else if (last1->dw_loc_opc != DW_OP_deref_size
14116 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14118 add_loc_descr (&op1, int_loc_descriptor (mask));
14119 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14122 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14124 HOST_WIDE_INT bias = 1;
14125 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14126 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14127 if (CONST_INT_P (XEXP (rtl, 1)))
14128 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14129 + INTVAL (XEXP (rtl, 1)));
14130 else
14131 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14132 bias, 0));
14134 return compare_loc_descriptor (op, op0, op1);
14137 /* Return location descriptor for {U,S}{MIN,MAX}. */
14139 static dw_loc_descr_ref
14140 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14141 machine_mode mem_mode)
14143 enum dwarf_location_atom op;
14144 dw_loc_descr_ref op0, op1, ret;
14145 dw_loc_descr_ref bra_node, drop_node;
14147 scalar_int_mode int_mode;
14148 if (dwarf_strict
14149 && dwarf_version < 5
14150 && (!is_a <scalar_int_mode> (mode, &int_mode)
14151 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14152 return NULL;
14154 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14155 VAR_INIT_STATUS_INITIALIZED);
14156 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14157 VAR_INIT_STATUS_INITIALIZED);
14159 if (op0 == NULL || op1 == NULL)
14160 return NULL;
14162 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14163 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14164 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14165 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14167 /* Checked by the caller. */
14168 int_mode = as_a <scalar_int_mode> (mode);
14169 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14171 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14172 add_loc_descr (&op0, int_loc_descriptor (mask));
14173 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14174 add_loc_descr (&op1, int_loc_descriptor (mask));
14175 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14177 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14179 HOST_WIDE_INT bias = 1;
14180 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14181 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14182 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14185 else if (is_a <scalar_int_mode> (mode, &int_mode)
14186 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14188 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14189 add_loc_descr (&op0, int_loc_descriptor (shift));
14190 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14191 add_loc_descr (&op1, int_loc_descriptor (shift));
14192 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14194 else if (is_a <scalar_int_mode> (mode, &int_mode)
14195 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14197 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14198 dw_loc_descr_ref cvt;
14199 if (type_die == NULL)
14200 return NULL;
14201 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14202 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14203 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14204 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14205 add_loc_descr (&op0, cvt);
14206 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14207 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14208 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14209 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14210 add_loc_descr (&op1, cvt);
14213 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14214 op = DW_OP_lt;
14215 else
14216 op = DW_OP_gt;
14217 ret = op0;
14218 add_loc_descr (&ret, op1);
14219 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14220 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14221 add_loc_descr (&ret, bra_node);
14222 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14223 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14224 add_loc_descr (&ret, drop_node);
14225 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14226 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14227 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14228 && is_a <scalar_int_mode> (mode, &int_mode)
14229 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14230 ret = convert_descriptor_to_mode (int_mode, ret);
14231 return ret;
14234 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14235 but after converting arguments to type_die, afterwards
14236 convert back to unsigned. */
14238 static dw_loc_descr_ref
14239 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14240 scalar_int_mode mode, machine_mode mem_mode)
14242 dw_loc_descr_ref cvt, op0, op1;
14244 if (type_die == NULL)
14245 return NULL;
14246 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14247 VAR_INIT_STATUS_INITIALIZED);
14248 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14249 VAR_INIT_STATUS_INITIALIZED);
14250 if (op0 == NULL || op1 == NULL)
14251 return NULL;
14252 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14253 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14254 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14255 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14256 add_loc_descr (&op0, cvt);
14257 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14258 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14259 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14260 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14261 add_loc_descr (&op1, cvt);
14262 add_loc_descr (&op0, op1);
14263 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14264 return convert_descriptor_to_mode (mode, op0);
14267 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14268 const0 is DW_OP_lit0 or corresponding typed constant,
14269 const1 is DW_OP_lit1 or corresponding typed constant
14270 and constMSB is constant with just the MSB bit set
14271 for the mode):
14272 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14273 L1: const0 DW_OP_swap
14274 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14275 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14276 L3: DW_OP_drop
14277 L4: DW_OP_nop
14279 CTZ is similar:
14280 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14281 L1: const0 DW_OP_swap
14282 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14283 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14284 L3: DW_OP_drop
14285 L4: DW_OP_nop
14287 FFS is similar:
14288 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14289 L1: const1 DW_OP_swap
14290 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14291 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14292 L3: DW_OP_drop
14293 L4: DW_OP_nop */
14295 static dw_loc_descr_ref
14296 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14297 machine_mode mem_mode)
14299 dw_loc_descr_ref op0, ret, tmp;
14300 HOST_WIDE_INT valv;
14301 dw_loc_descr_ref l1jump, l1label;
14302 dw_loc_descr_ref l2jump, l2label;
14303 dw_loc_descr_ref l3jump, l3label;
14304 dw_loc_descr_ref l4jump, l4label;
14305 rtx msb;
14307 if (GET_MODE (XEXP (rtl, 0)) != mode)
14308 return NULL;
14310 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14311 VAR_INIT_STATUS_INITIALIZED);
14312 if (op0 == NULL)
14313 return NULL;
14314 ret = op0;
14315 if (GET_CODE (rtl) == CLZ)
14317 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14318 valv = GET_MODE_BITSIZE (mode);
14320 else if (GET_CODE (rtl) == FFS)
14321 valv = 0;
14322 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14323 valv = GET_MODE_BITSIZE (mode);
14324 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14325 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14326 add_loc_descr (&ret, l1jump);
14327 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14328 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14329 VAR_INIT_STATUS_INITIALIZED);
14330 if (tmp == NULL)
14331 return NULL;
14332 add_loc_descr (&ret, tmp);
14333 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14334 add_loc_descr (&ret, l4jump);
14335 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14336 ? const1_rtx : const0_rtx,
14337 mode, mem_mode,
14338 VAR_INIT_STATUS_INITIALIZED);
14339 if (l1label == NULL)
14340 return NULL;
14341 add_loc_descr (&ret, l1label);
14342 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14343 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14344 add_loc_descr (&ret, l2label);
14345 if (GET_CODE (rtl) != CLZ)
14346 msb = const1_rtx;
14347 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14348 msb = GEN_INT (HOST_WIDE_INT_1U
14349 << (GET_MODE_BITSIZE (mode) - 1));
14350 else
14351 msb = immed_wide_int_const
14352 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14353 GET_MODE_PRECISION (mode)), mode);
14354 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14355 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14356 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14357 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14358 else
14359 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14360 VAR_INIT_STATUS_INITIALIZED);
14361 if (tmp == NULL)
14362 return NULL;
14363 add_loc_descr (&ret, tmp);
14364 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14365 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14366 add_loc_descr (&ret, l3jump);
14367 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14368 VAR_INIT_STATUS_INITIALIZED);
14369 if (tmp == NULL)
14370 return NULL;
14371 add_loc_descr (&ret, tmp);
14372 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14373 ? DW_OP_shl : DW_OP_shr, 0, 0));
14374 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14375 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14376 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14377 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14378 add_loc_descr (&ret, l2jump);
14379 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14380 add_loc_descr (&ret, l3label);
14381 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14382 add_loc_descr (&ret, l4label);
14383 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14384 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14385 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14386 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14387 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14388 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14389 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14390 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14391 return ret;
14394 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14395 const1 is DW_OP_lit1 or corresponding typed constant):
14396 const0 DW_OP_swap
14397 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14398 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14399 L2: DW_OP_drop
14401 PARITY is similar:
14402 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14403 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14404 L2: DW_OP_drop */
14406 static dw_loc_descr_ref
14407 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14408 machine_mode mem_mode)
14410 dw_loc_descr_ref op0, ret, tmp;
14411 dw_loc_descr_ref l1jump, l1label;
14412 dw_loc_descr_ref l2jump, l2label;
14414 if (GET_MODE (XEXP (rtl, 0)) != mode)
14415 return NULL;
14417 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14418 VAR_INIT_STATUS_INITIALIZED);
14419 if (op0 == NULL)
14420 return NULL;
14421 ret = op0;
14422 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14423 VAR_INIT_STATUS_INITIALIZED);
14424 if (tmp == NULL)
14425 return NULL;
14426 add_loc_descr (&ret, tmp);
14427 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14428 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14429 add_loc_descr (&ret, l1label);
14430 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14431 add_loc_descr (&ret, l2jump);
14432 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14433 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14434 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14435 VAR_INIT_STATUS_INITIALIZED);
14436 if (tmp == NULL)
14437 return NULL;
14438 add_loc_descr (&ret, tmp);
14439 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14440 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14441 ? DW_OP_plus : DW_OP_xor, 0, 0));
14442 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14443 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14444 VAR_INIT_STATUS_INITIALIZED);
14445 add_loc_descr (&ret, tmp);
14446 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14447 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14448 add_loc_descr (&ret, l1jump);
14449 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14450 add_loc_descr (&ret, l2label);
14451 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14452 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14453 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14454 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14455 return ret;
14458 /* BSWAP (constS is initial shift count, either 56 or 24):
14459 constS const0
14460 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14461 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14462 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14463 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14464 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14466 static dw_loc_descr_ref
14467 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14468 machine_mode mem_mode)
14470 dw_loc_descr_ref op0, ret, tmp;
14471 dw_loc_descr_ref l1jump, l1label;
14472 dw_loc_descr_ref l2jump, l2label;
14474 if (BITS_PER_UNIT != 8
14475 || (GET_MODE_BITSIZE (mode) != 32
14476 && GET_MODE_BITSIZE (mode) != 64))
14477 return NULL;
14479 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14480 VAR_INIT_STATUS_INITIALIZED);
14481 if (op0 == NULL)
14482 return NULL;
14484 ret = op0;
14485 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14486 mode, mem_mode,
14487 VAR_INIT_STATUS_INITIALIZED);
14488 if (tmp == NULL)
14489 return NULL;
14490 add_loc_descr (&ret, tmp);
14491 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14492 VAR_INIT_STATUS_INITIALIZED);
14493 if (tmp == NULL)
14494 return NULL;
14495 add_loc_descr (&ret, tmp);
14496 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14497 add_loc_descr (&ret, l1label);
14498 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14499 mode, mem_mode,
14500 VAR_INIT_STATUS_INITIALIZED);
14501 add_loc_descr (&ret, tmp);
14502 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14503 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14505 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14506 VAR_INIT_STATUS_INITIALIZED);
14507 if (tmp == NULL)
14508 return NULL;
14509 add_loc_descr (&ret, tmp);
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14511 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14512 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14513 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14514 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14515 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14516 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14517 VAR_INIT_STATUS_INITIALIZED);
14518 add_loc_descr (&ret, tmp);
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14520 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14521 add_loc_descr (&ret, l2jump);
14522 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14523 VAR_INIT_STATUS_INITIALIZED);
14524 add_loc_descr (&ret, tmp);
14525 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14526 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14527 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14528 add_loc_descr (&ret, l1jump);
14529 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14530 add_loc_descr (&ret, l2label);
14531 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14532 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14533 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14534 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14535 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14536 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14537 return ret;
14540 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14541 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14542 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14543 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14545 ROTATERT is similar:
14546 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14547 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14548 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14550 static dw_loc_descr_ref
14551 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14552 machine_mode mem_mode)
14554 rtx rtlop1 = XEXP (rtl, 1);
14555 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14556 int i;
14558 if (GET_MODE (rtlop1) != VOIDmode
14559 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14560 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14561 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14562 VAR_INIT_STATUS_INITIALIZED);
14563 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14564 VAR_INIT_STATUS_INITIALIZED);
14565 if (op0 == NULL || op1 == NULL)
14566 return NULL;
14567 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14568 for (i = 0; i < 2; i++)
14570 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14571 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14572 mode, mem_mode,
14573 VAR_INIT_STATUS_INITIALIZED);
14574 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14575 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14576 ? DW_OP_const4u
14577 : HOST_BITS_PER_WIDE_INT == 64
14578 ? DW_OP_const8u : DW_OP_constu,
14579 GET_MODE_MASK (mode), 0);
14580 else
14581 mask[i] = NULL;
14582 if (mask[i] == NULL)
14583 return NULL;
14584 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14586 ret = op0;
14587 add_loc_descr (&ret, op1);
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14590 if (GET_CODE (rtl) == ROTATERT)
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14593 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14594 GET_MODE_BITSIZE (mode), 0));
14596 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14597 if (mask[0] != NULL)
14598 add_loc_descr (&ret, mask[0]);
14599 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14600 if (mask[1] != NULL)
14602 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14603 add_loc_descr (&ret, mask[1]);
14604 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14606 if (GET_CODE (rtl) == ROTATE)
14608 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14610 GET_MODE_BITSIZE (mode), 0));
14612 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14613 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14614 return ret;
14617 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14618 for DEBUG_PARAMETER_REF RTL. */
14620 static dw_loc_descr_ref
14621 parameter_ref_descriptor (rtx rtl)
14623 dw_loc_descr_ref ret;
14624 dw_die_ref ref;
14626 if (dwarf_strict)
14627 return NULL;
14628 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14629 /* With LTO during LTRANS we get the late DIE that refers to the early
14630 DIE, thus we add another indirection here. This seems to confuse
14631 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14632 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14633 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14634 if (ref)
14636 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14637 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14638 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14640 else
14642 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14643 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14645 return ret;
14648 /* The following routine converts the RTL for a variable or parameter
14649 (resident in memory) into an equivalent Dwarf representation of a
14650 mechanism for getting the address of that same variable onto the top of a
14651 hypothetical "address evaluation" stack.
14653 When creating memory location descriptors, we are effectively transforming
14654 the RTL for a memory-resident object into its Dwarf postfix expression
14655 equivalent. This routine recursively descends an RTL tree, turning
14656 it into Dwarf postfix code as it goes.
14658 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14660 MEM_MODE is the mode of the memory reference, needed to handle some
14661 autoincrement addressing modes.
14663 Return 0 if we can't represent the location. */
14665 dw_loc_descr_ref
14666 mem_loc_descriptor (rtx rtl, machine_mode mode,
14667 machine_mode mem_mode,
14668 enum var_init_status initialized)
14670 dw_loc_descr_ref mem_loc_result = NULL;
14671 enum dwarf_location_atom op;
14672 dw_loc_descr_ref op0, op1;
14673 rtx inner = NULL_RTX;
14675 if (mode == VOIDmode)
14676 mode = GET_MODE (rtl);
14678 /* Note that for a dynamically sized array, the location we will generate a
14679 description of here will be the lowest numbered location which is
14680 actually within the array. That's *not* necessarily the same as the
14681 zeroth element of the array. */
14683 rtl = targetm.delegitimize_address (rtl);
14685 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14686 return NULL;
14688 scalar_int_mode int_mode, inner_mode, op1_mode;
14689 switch (GET_CODE (rtl))
14691 case POST_INC:
14692 case POST_DEC:
14693 case POST_MODIFY:
14694 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14696 case SUBREG:
14697 /* The case of a subreg may arise when we have a local (register)
14698 variable or a formal (register) parameter which doesn't quite fill
14699 up an entire register. For now, just assume that it is
14700 legitimate to make the Dwarf info refer to the whole register which
14701 contains the given subreg. */
14702 if (!subreg_lowpart_p (rtl))
14703 break;
14704 inner = SUBREG_REG (rtl);
14705 /* FALLTHRU */
14706 case TRUNCATE:
14707 if (inner == NULL_RTX)
14708 inner = XEXP (rtl, 0);
14709 if (is_a <scalar_int_mode> (mode, &int_mode)
14710 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14711 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14712 #ifdef POINTERS_EXTEND_UNSIGNED
14713 || (int_mode == Pmode && mem_mode != VOIDmode)
14714 #endif
14716 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14718 mem_loc_result = mem_loc_descriptor (inner,
14719 inner_mode,
14720 mem_mode, initialized);
14721 break;
14723 if (dwarf_strict && dwarf_version < 5)
14724 break;
14725 if (is_a <scalar_int_mode> (mode, &int_mode)
14726 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14727 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14728 : GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (inner)))
14730 dw_die_ref type_die;
14731 dw_loc_descr_ref cvt;
14733 mem_loc_result = mem_loc_descriptor (inner,
14734 GET_MODE (inner),
14735 mem_mode, initialized);
14736 if (mem_loc_result == NULL)
14737 break;
14738 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14739 if (type_die == NULL)
14741 mem_loc_result = NULL;
14742 break;
14744 if (GET_MODE_SIZE (mode)
14745 != GET_MODE_SIZE (GET_MODE (inner)))
14746 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14747 else
14748 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14749 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14750 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14751 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14752 add_loc_descr (&mem_loc_result, cvt);
14753 if (is_a <scalar_int_mode> (mode, &int_mode)
14754 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14756 /* Convert it to untyped afterwards. */
14757 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14758 add_loc_descr (&mem_loc_result, cvt);
14761 break;
14763 case REG:
14764 if (!is_a <scalar_int_mode> (mode, &int_mode)
14765 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14766 && rtl != arg_pointer_rtx
14767 && rtl != frame_pointer_rtx
14768 #ifdef POINTERS_EXTEND_UNSIGNED
14769 && (int_mode != Pmode || mem_mode == VOIDmode)
14770 #endif
14773 dw_die_ref type_die;
14774 unsigned int dbx_regnum;
14776 if (dwarf_strict && dwarf_version < 5)
14777 break;
14778 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14779 break;
14780 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14781 if (type_die == NULL)
14782 break;
14784 dbx_regnum = dbx_reg_number (rtl);
14785 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14786 break;
14787 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14788 dbx_regnum, 0);
14789 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14790 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14791 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14792 break;
14794 /* Whenever a register number forms a part of the description of the
14795 method for calculating the (dynamic) address of a memory resident
14796 object, DWARF rules require the register number be referred to as
14797 a "base register". This distinction is not based in any way upon
14798 what category of register the hardware believes the given register
14799 belongs to. This is strictly DWARF terminology we're dealing with
14800 here. Note that in cases where the location of a memory-resident
14801 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14802 OP_CONST (0)) the actual DWARF location descriptor that we generate
14803 may just be OP_BASEREG (basereg). This may look deceptively like
14804 the object in question was allocated to a register (rather than in
14805 memory) so DWARF consumers need to be aware of the subtle
14806 distinction between OP_REG and OP_BASEREG. */
14807 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14808 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14809 else if (stack_realign_drap
14810 && crtl->drap_reg
14811 && crtl->args.internal_arg_pointer == rtl
14812 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14814 /* If RTL is internal_arg_pointer, which has been optimized
14815 out, use DRAP instead. */
14816 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14817 VAR_INIT_STATUS_INITIALIZED);
14819 break;
14821 case SIGN_EXTEND:
14822 case ZERO_EXTEND:
14823 if (!is_a <scalar_int_mode> (mode, &int_mode)
14824 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14825 break;
14826 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14827 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14828 if (op0 == 0)
14829 break;
14830 else if (GET_CODE (rtl) == ZERO_EXTEND
14831 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14832 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14833 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14834 to expand zero extend as two shifts instead of
14835 masking. */
14836 && GET_MODE_SIZE (inner_mode) <= 4)
14838 mem_loc_result = op0;
14839 add_loc_descr (&mem_loc_result,
14840 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14841 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14843 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14845 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14846 shift *= BITS_PER_UNIT;
14847 if (GET_CODE (rtl) == SIGN_EXTEND)
14848 op = DW_OP_shra;
14849 else
14850 op = DW_OP_shr;
14851 mem_loc_result = op0;
14852 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14853 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14854 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14855 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14857 else if (!dwarf_strict || dwarf_version >= 5)
14859 dw_die_ref type_die1, type_die2;
14860 dw_loc_descr_ref cvt;
14862 type_die1 = base_type_for_mode (inner_mode,
14863 GET_CODE (rtl) == ZERO_EXTEND);
14864 if (type_die1 == NULL)
14865 break;
14866 type_die2 = base_type_for_mode (int_mode, 1);
14867 if (type_die2 == NULL)
14868 break;
14869 mem_loc_result = op0;
14870 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14871 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14872 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14873 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14874 add_loc_descr (&mem_loc_result, cvt);
14875 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14876 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14877 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14878 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14879 add_loc_descr (&mem_loc_result, cvt);
14881 break;
14883 case MEM:
14885 rtx new_rtl = avoid_constant_pool_reference (rtl);
14886 if (new_rtl != rtl)
14888 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14889 initialized);
14890 if (mem_loc_result != NULL)
14891 return mem_loc_result;
14894 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14895 get_address_mode (rtl), mode,
14896 VAR_INIT_STATUS_INITIALIZED);
14897 if (mem_loc_result == NULL)
14898 mem_loc_result = tls_mem_loc_descriptor (rtl);
14899 if (mem_loc_result != NULL)
14901 if (!is_a <scalar_int_mode> (mode, &int_mode)
14902 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14904 dw_die_ref type_die;
14905 dw_loc_descr_ref deref;
14907 if (dwarf_strict && dwarf_version < 5)
14908 return NULL;
14909 type_die
14910 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14911 if (type_die == NULL)
14912 return NULL;
14913 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type),
14914 GET_MODE_SIZE (mode), 0);
14915 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14916 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14917 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14918 add_loc_descr (&mem_loc_result, deref);
14920 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14921 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14922 else
14923 add_loc_descr (&mem_loc_result,
14924 new_loc_descr (DW_OP_deref_size,
14925 GET_MODE_SIZE (int_mode), 0));
14927 break;
14929 case LO_SUM:
14930 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14932 case LABEL_REF:
14933 /* Some ports can transform a symbol ref into a label ref, because
14934 the symbol ref is too far away and has to be dumped into a constant
14935 pool. */
14936 case CONST:
14937 case SYMBOL_REF:
14938 if (!is_a <scalar_int_mode> (mode, &int_mode)
14939 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14940 #ifdef POINTERS_EXTEND_UNSIGNED
14941 && (int_mode != Pmode || mem_mode == VOIDmode)
14942 #endif
14944 break;
14945 if (GET_CODE (rtl) == SYMBOL_REF
14946 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14948 dw_loc_descr_ref temp;
14950 /* If this is not defined, we have no way to emit the data. */
14951 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14952 break;
14954 temp = new_addr_loc_descr (rtl, dtprel_true);
14956 /* We check for DWARF 5 here because gdb did not implement
14957 DW_OP_form_tls_address until after 7.12. */
14958 mem_loc_result = new_loc_descr ((dwarf_version >= 5
14959 ? DW_OP_form_tls_address
14960 : DW_OP_GNU_push_tls_address),
14961 0, 0);
14962 add_loc_descr (&mem_loc_result, temp);
14964 break;
14967 if (!const_ok_for_output (rtl))
14969 if (GET_CODE (rtl) == CONST)
14970 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
14971 mem_mode, initialized);
14972 break;
14975 symref:
14976 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
14977 vec_safe_push (used_rtx_array, rtl);
14978 break;
14980 case CONCAT:
14981 case CONCATN:
14982 case VAR_LOCATION:
14983 case DEBUG_IMPLICIT_PTR:
14984 expansion_failed (NULL_TREE, rtl,
14985 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14986 return 0;
14988 case ENTRY_VALUE:
14989 if (dwarf_strict && dwarf_version < 5)
14990 return NULL;
14991 if (REG_P (ENTRY_VALUE_EXP (rtl)))
14993 if (!is_a <scalar_int_mode> (mode, &int_mode)
14994 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14995 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14996 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14997 else
14999 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15000 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15001 return NULL;
15002 op0 = one_reg_loc_descriptor (dbx_regnum,
15003 VAR_INIT_STATUS_INITIALIZED);
15006 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15007 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15009 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15010 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15011 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15012 return NULL;
15014 else
15015 gcc_unreachable ();
15016 if (op0 == NULL)
15017 return NULL;
15018 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15019 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15020 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15021 break;
15023 case DEBUG_PARAMETER_REF:
15024 mem_loc_result = parameter_ref_descriptor (rtl);
15025 break;
15027 case PRE_MODIFY:
15028 /* Extract the PLUS expression nested inside and fall into
15029 PLUS code below. */
15030 rtl = XEXP (rtl, 1);
15031 goto plus;
15033 case PRE_INC:
15034 case PRE_DEC:
15035 /* Turn these into a PLUS expression and fall into the PLUS code
15036 below. */
15037 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15038 gen_int_mode (GET_CODE (rtl) == PRE_INC
15039 ? GET_MODE_UNIT_SIZE (mem_mode)
15040 : -GET_MODE_UNIT_SIZE (mem_mode),
15041 mode));
15043 /* fall through */
15045 case PLUS:
15046 plus:
15047 if (is_based_loc (rtl)
15048 && is_a <scalar_int_mode> (mode, &int_mode)
15049 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15050 || XEXP (rtl, 0) == arg_pointer_rtx
15051 || XEXP (rtl, 0) == frame_pointer_rtx))
15052 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15053 INTVAL (XEXP (rtl, 1)),
15054 VAR_INIT_STATUS_INITIALIZED);
15055 else
15057 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15058 VAR_INIT_STATUS_INITIALIZED);
15059 if (mem_loc_result == 0)
15060 break;
15062 if (CONST_INT_P (XEXP (rtl, 1))
15063 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15064 <= DWARF2_ADDR_SIZE))
15065 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15066 else
15068 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15069 VAR_INIT_STATUS_INITIALIZED);
15070 if (op1 == 0)
15071 return NULL;
15072 add_loc_descr (&mem_loc_result, op1);
15073 add_loc_descr (&mem_loc_result,
15074 new_loc_descr (DW_OP_plus, 0, 0));
15077 break;
15079 /* If a pseudo-reg is optimized away, it is possible for it to
15080 be replaced with a MEM containing a multiply or shift. */
15081 case MINUS:
15082 op = DW_OP_minus;
15083 goto do_binop;
15085 case MULT:
15086 op = DW_OP_mul;
15087 goto do_binop;
15089 case DIV:
15090 if ((!dwarf_strict || dwarf_version >= 5)
15091 && is_a <scalar_int_mode> (mode, &int_mode)
15092 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15094 mem_loc_result = typed_binop (DW_OP_div, rtl,
15095 base_type_for_mode (mode, 0),
15096 int_mode, mem_mode);
15097 break;
15099 op = DW_OP_div;
15100 goto do_binop;
15102 case UMOD:
15103 op = DW_OP_mod;
15104 goto do_binop;
15106 case ASHIFT:
15107 op = DW_OP_shl;
15108 goto do_shift;
15110 case ASHIFTRT:
15111 op = DW_OP_shra;
15112 goto do_shift;
15114 case LSHIFTRT:
15115 op = DW_OP_shr;
15116 goto do_shift;
15118 do_shift:
15119 if (!is_a <scalar_int_mode> (mode, &int_mode))
15120 break;
15121 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15122 VAR_INIT_STATUS_INITIALIZED);
15124 rtx rtlop1 = XEXP (rtl, 1);
15125 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15126 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15127 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15128 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15129 VAR_INIT_STATUS_INITIALIZED);
15132 if (op0 == 0 || op1 == 0)
15133 break;
15135 mem_loc_result = op0;
15136 add_loc_descr (&mem_loc_result, op1);
15137 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15138 break;
15140 case AND:
15141 op = DW_OP_and;
15142 goto do_binop;
15144 case IOR:
15145 op = DW_OP_or;
15146 goto do_binop;
15148 case XOR:
15149 op = DW_OP_xor;
15150 goto do_binop;
15152 do_binop:
15153 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15154 VAR_INIT_STATUS_INITIALIZED);
15155 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15156 VAR_INIT_STATUS_INITIALIZED);
15158 if (op0 == 0 || op1 == 0)
15159 break;
15161 mem_loc_result = op0;
15162 add_loc_descr (&mem_loc_result, op1);
15163 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15164 break;
15166 case MOD:
15167 if ((!dwarf_strict || dwarf_version >= 5)
15168 && is_a <scalar_int_mode> (mode, &int_mode)
15169 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15171 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15172 base_type_for_mode (mode, 0),
15173 int_mode, mem_mode);
15174 break;
15177 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15178 VAR_INIT_STATUS_INITIALIZED);
15179 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15180 VAR_INIT_STATUS_INITIALIZED);
15182 if (op0 == 0 || op1 == 0)
15183 break;
15185 mem_loc_result = op0;
15186 add_loc_descr (&mem_loc_result, op1);
15187 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15188 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15189 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15190 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15191 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15192 break;
15194 case UDIV:
15195 if ((!dwarf_strict || dwarf_version >= 5)
15196 && is_a <scalar_int_mode> (mode, &int_mode))
15198 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15200 op = DW_OP_div;
15201 goto do_binop;
15203 mem_loc_result = typed_binop (DW_OP_div, rtl,
15204 base_type_for_mode (int_mode, 1),
15205 int_mode, mem_mode);
15207 break;
15209 case NOT:
15210 op = DW_OP_not;
15211 goto do_unop;
15213 case ABS:
15214 op = DW_OP_abs;
15215 goto do_unop;
15217 case NEG:
15218 op = DW_OP_neg;
15219 goto do_unop;
15221 do_unop:
15222 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15223 VAR_INIT_STATUS_INITIALIZED);
15225 if (op0 == 0)
15226 break;
15228 mem_loc_result = op0;
15229 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15230 break;
15232 case CONST_INT:
15233 if (!is_a <scalar_int_mode> (mode, &int_mode)
15234 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15235 #ifdef POINTERS_EXTEND_UNSIGNED
15236 || (int_mode == Pmode
15237 && mem_mode != VOIDmode
15238 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15239 #endif
15242 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15243 break;
15245 if ((!dwarf_strict || dwarf_version >= 5)
15246 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15247 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15249 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15250 scalar_int_mode amode;
15251 if (type_die == NULL)
15252 return NULL;
15253 if (INTVAL (rtl) >= 0
15254 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15255 .exists (&amode))
15256 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15257 /* const DW_OP_convert <XXX> vs.
15258 DW_OP_const_type <XXX, 1, const>. */
15259 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15260 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15262 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15263 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15264 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15265 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15266 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15267 add_loc_descr (&mem_loc_result, op0);
15268 return mem_loc_result;
15270 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15271 INTVAL (rtl));
15272 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15273 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15274 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15275 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15276 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15277 else
15279 mem_loc_result->dw_loc_oprnd2.val_class
15280 = dw_val_class_const_double;
15281 mem_loc_result->dw_loc_oprnd2.v.val_double
15282 = double_int::from_shwi (INTVAL (rtl));
15285 break;
15287 case CONST_DOUBLE:
15288 if (!dwarf_strict || dwarf_version >= 5)
15290 dw_die_ref type_die;
15292 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15293 CONST_DOUBLE rtx could represent either a large integer
15294 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15295 the value is always a floating point constant.
15297 When it is an integer, a CONST_DOUBLE is used whenever
15298 the constant requires 2 HWIs to be adequately represented.
15299 We output CONST_DOUBLEs as blocks. */
15300 if (mode == VOIDmode
15301 || (GET_MODE (rtl) == VOIDmode
15302 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
15303 break;
15304 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15305 if (type_die == NULL)
15306 return NULL;
15307 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15308 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15309 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15310 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15311 #if TARGET_SUPPORTS_WIDE_INT == 0
15312 if (!SCALAR_FLOAT_MODE_P (mode))
15314 mem_loc_result->dw_loc_oprnd2.val_class
15315 = dw_val_class_const_double;
15316 mem_loc_result->dw_loc_oprnd2.v.val_double
15317 = rtx_to_double_int (rtl);
15319 else
15320 #endif
15322 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15323 unsigned int length = GET_MODE_SIZE (float_mode);
15324 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15326 insert_float (rtl, array);
15327 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15328 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15329 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15330 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15333 break;
15335 case CONST_WIDE_INT:
15336 if (!dwarf_strict || dwarf_version >= 5)
15338 dw_die_ref type_die;
15340 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15341 if (type_die == NULL)
15342 return NULL;
15343 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15344 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15345 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15346 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15347 mem_loc_result->dw_loc_oprnd2.val_class
15348 = dw_val_class_wide_int;
15349 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15350 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15352 break;
15354 case EQ:
15355 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15356 break;
15358 case GE:
15359 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15360 break;
15362 case GT:
15363 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15364 break;
15366 case LE:
15367 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15368 break;
15370 case LT:
15371 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15372 break;
15374 case NE:
15375 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15376 break;
15378 case GEU:
15379 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15380 break;
15382 case GTU:
15383 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15384 break;
15386 case LEU:
15387 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15388 break;
15390 case LTU:
15391 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15392 break;
15394 case UMIN:
15395 case UMAX:
15396 if (!SCALAR_INT_MODE_P (mode))
15397 break;
15398 /* FALLTHRU */
15399 case SMIN:
15400 case SMAX:
15401 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15402 break;
15404 case ZERO_EXTRACT:
15405 case SIGN_EXTRACT:
15406 if (CONST_INT_P (XEXP (rtl, 1))
15407 && CONST_INT_P (XEXP (rtl, 2))
15408 && is_a <scalar_int_mode> (mode, &int_mode)
15409 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15410 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15411 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15412 && ((unsigned) INTVAL (XEXP (rtl, 1))
15413 + (unsigned) INTVAL (XEXP (rtl, 2))
15414 <= GET_MODE_BITSIZE (int_mode)))
15416 int shift, size;
15417 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15418 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15419 if (op0 == 0)
15420 break;
15421 if (GET_CODE (rtl) == SIGN_EXTRACT)
15422 op = DW_OP_shra;
15423 else
15424 op = DW_OP_shr;
15425 mem_loc_result = op0;
15426 size = INTVAL (XEXP (rtl, 1));
15427 shift = INTVAL (XEXP (rtl, 2));
15428 if (BITS_BIG_ENDIAN)
15429 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15430 if (shift + size != (int) DWARF2_ADDR_SIZE)
15432 add_loc_descr (&mem_loc_result,
15433 int_loc_descriptor (DWARF2_ADDR_SIZE
15434 - shift - size));
15435 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15437 if (size != (int) DWARF2_ADDR_SIZE)
15439 add_loc_descr (&mem_loc_result,
15440 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15441 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15444 break;
15446 case IF_THEN_ELSE:
15448 dw_loc_descr_ref op2, bra_node, drop_node;
15449 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15450 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15451 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15452 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15453 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15454 VAR_INIT_STATUS_INITIALIZED);
15455 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15456 VAR_INIT_STATUS_INITIALIZED);
15457 if (op0 == NULL || op1 == NULL || op2 == NULL)
15458 break;
15460 mem_loc_result = op1;
15461 add_loc_descr (&mem_loc_result, op2);
15462 add_loc_descr (&mem_loc_result, op0);
15463 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15464 add_loc_descr (&mem_loc_result, bra_node);
15465 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15466 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15467 add_loc_descr (&mem_loc_result, drop_node);
15468 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15469 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15471 break;
15473 case FLOAT_EXTEND:
15474 case FLOAT_TRUNCATE:
15475 case FLOAT:
15476 case UNSIGNED_FLOAT:
15477 case FIX:
15478 case UNSIGNED_FIX:
15479 if (!dwarf_strict || dwarf_version >= 5)
15481 dw_die_ref type_die;
15482 dw_loc_descr_ref cvt;
15484 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15485 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15486 if (op0 == NULL)
15487 break;
15488 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15489 && (GET_CODE (rtl) == FLOAT
15490 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15492 type_die = base_type_for_mode (int_mode,
15493 GET_CODE (rtl) == UNSIGNED_FLOAT);
15494 if (type_die == NULL)
15495 break;
15496 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15497 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15498 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15499 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15500 add_loc_descr (&op0, cvt);
15502 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15503 if (type_die == NULL)
15504 break;
15505 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15506 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15507 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15508 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15509 add_loc_descr (&op0, cvt);
15510 if (is_a <scalar_int_mode> (mode, &int_mode)
15511 && (GET_CODE (rtl) == FIX
15512 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15514 op0 = convert_descriptor_to_mode (int_mode, op0);
15515 if (op0 == NULL)
15516 break;
15518 mem_loc_result = op0;
15520 break;
15522 case CLZ:
15523 case CTZ:
15524 case FFS:
15525 if (is_a <scalar_int_mode> (mode, &int_mode))
15526 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15527 break;
15529 case POPCOUNT:
15530 case PARITY:
15531 if (is_a <scalar_int_mode> (mode, &int_mode))
15532 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15533 break;
15535 case BSWAP:
15536 if (is_a <scalar_int_mode> (mode, &int_mode))
15537 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15538 break;
15540 case ROTATE:
15541 case ROTATERT:
15542 if (is_a <scalar_int_mode> (mode, &int_mode))
15543 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15544 break;
15546 case COMPARE:
15547 /* In theory, we could implement the above. */
15548 /* DWARF cannot represent the unsigned compare operations
15549 natively. */
15550 case SS_MULT:
15551 case US_MULT:
15552 case SS_DIV:
15553 case US_DIV:
15554 case SS_PLUS:
15555 case US_PLUS:
15556 case SS_MINUS:
15557 case US_MINUS:
15558 case SS_NEG:
15559 case US_NEG:
15560 case SS_ABS:
15561 case SS_ASHIFT:
15562 case US_ASHIFT:
15563 case SS_TRUNCATE:
15564 case US_TRUNCATE:
15565 case UNORDERED:
15566 case ORDERED:
15567 case UNEQ:
15568 case UNGE:
15569 case UNGT:
15570 case UNLE:
15571 case UNLT:
15572 case LTGT:
15573 case FRACT_CONVERT:
15574 case UNSIGNED_FRACT_CONVERT:
15575 case SAT_FRACT:
15576 case UNSIGNED_SAT_FRACT:
15577 case SQRT:
15578 case ASM_OPERANDS:
15579 case VEC_MERGE:
15580 case VEC_SELECT:
15581 case VEC_CONCAT:
15582 case VEC_DUPLICATE:
15583 case UNSPEC:
15584 case HIGH:
15585 case FMA:
15586 case STRICT_LOW_PART:
15587 case CONST_VECTOR:
15588 case CONST_FIXED:
15589 case CLRSB:
15590 case CLOBBER:
15591 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15592 can't express it in the debug info. This can happen e.g. with some
15593 TLS UNSPECs. */
15594 break;
15596 case CONST_STRING:
15597 resolve_one_addr (&rtl);
15598 goto symref;
15600 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15601 the expression. An UNSPEC rtx represents a raw DWARF operation,
15602 new_loc_descr is called for it to build the operation directly.
15603 Otherwise mem_loc_descriptor is called recursively. */
15604 case PARALLEL:
15606 int index = 0;
15607 dw_loc_descr_ref exp_result = NULL;
15609 for (; index < XVECLEN (rtl, 0); index++)
15611 rtx elem = XVECEXP (rtl, 0, index);
15612 if (GET_CODE (elem) == UNSPEC)
15614 /* Each DWARF operation UNSPEC contain two operands, if
15615 one operand is not used for the operation, const0_rtx is
15616 passed. */
15617 gcc_assert (XVECLEN (elem, 0) == 2);
15619 HOST_WIDE_INT dw_op = XINT (elem, 1);
15620 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15621 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15622 exp_result
15623 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15624 oprnd2);
15626 else
15627 exp_result
15628 = mem_loc_descriptor (elem, mode, mem_mode,
15629 VAR_INIT_STATUS_INITIALIZED);
15631 if (!mem_loc_result)
15632 mem_loc_result = exp_result;
15633 else
15634 add_loc_descr (&mem_loc_result, exp_result);
15637 break;
15640 default:
15641 if (flag_checking)
15643 print_rtl (stderr, rtl);
15644 gcc_unreachable ();
15646 break;
15649 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15650 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15652 return mem_loc_result;
15655 /* Return a descriptor that describes the concatenation of two locations.
15656 This is typically a complex variable. */
15658 static dw_loc_descr_ref
15659 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15661 dw_loc_descr_ref cc_loc_result = NULL;
15662 dw_loc_descr_ref x0_ref
15663 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15664 dw_loc_descr_ref x1_ref
15665 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15667 if (x0_ref == 0 || x1_ref == 0)
15668 return 0;
15670 cc_loc_result = x0_ref;
15671 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15673 add_loc_descr (&cc_loc_result, x1_ref);
15674 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15676 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15677 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15679 return cc_loc_result;
15682 /* Return a descriptor that describes the concatenation of N
15683 locations. */
15685 static dw_loc_descr_ref
15686 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15688 unsigned int i;
15689 dw_loc_descr_ref cc_loc_result = NULL;
15690 unsigned int n = XVECLEN (concatn, 0);
15692 for (i = 0; i < n; ++i)
15694 dw_loc_descr_ref ref;
15695 rtx x = XVECEXP (concatn, 0, i);
15697 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15698 if (ref == NULL)
15699 return NULL;
15701 add_loc_descr (&cc_loc_result, ref);
15702 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15705 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15706 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15708 return cc_loc_result;
15711 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15712 for DEBUG_IMPLICIT_PTR RTL. */
15714 static dw_loc_descr_ref
15715 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15717 dw_loc_descr_ref ret;
15718 dw_die_ref ref;
15720 if (dwarf_strict && dwarf_version < 5)
15721 return NULL;
15722 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15723 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15724 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15725 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15726 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15727 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15728 if (ref)
15730 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15731 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15732 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15734 else
15736 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15737 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15739 return ret;
15742 /* Output a proper Dwarf location descriptor for a variable or parameter
15743 which is either allocated in a register or in a memory location. For a
15744 register, we just generate an OP_REG and the register number. For a
15745 memory location we provide a Dwarf postfix expression describing how to
15746 generate the (dynamic) address of the object onto the address stack.
15748 MODE is mode of the decl if this loc_descriptor is going to be used in
15749 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15750 allowed, VOIDmode otherwise.
15752 If we don't know how to describe it, return 0. */
15754 static dw_loc_descr_ref
15755 loc_descriptor (rtx rtl, machine_mode mode,
15756 enum var_init_status initialized)
15758 dw_loc_descr_ref loc_result = NULL;
15759 scalar_int_mode int_mode;
15761 switch (GET_CODE (rtl))
15763 case SUBREG:
15764 /* The case of a subreg may arise when we have a local (register)
15765 variable or a formal (register) parameter which doesn't quite fill
15766 up an entire register. For now, just assume that it is
15767 legitimate to make the Dwarf info refer to the whole register which
15768 contains the given subreg. */
15769 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15770 loc_result = loc_descriptor (SUBREG_REG (rtl),
15771 GET_MODE (SUBREG_REG (rtl)), initialized);
15772 else
15773 goto do_default;
15774 break;
15776 case REG:
15777 loc_result = reg_loc_descriptor (rtl, initialized);
15778 break;
15780 case MEM:
15781 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15782 GET_MODE (rtl), initialized);
15783 if (loc_result == NULL)
15784 loc_result = tls_mem_loc_descriptor (rtl);
15785 if (loc_result == NULL)
15787 rtx new_rtl = avoid_constant_pool_reference (rtl);
15788 if (new_rtl != rtl)
15789 loc_result = loc_descriptor (new_rtl, mode, initialized);
15791 break;
15793 case CONCAT:
15794 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15795 initialized);
15796 break;
15798 case CONCATN:
15799 loc_result = concatn_loc_descriptor (rtl, initialized);
15800 break;
15802 case VAR_LOCATION:
15803 /* Single part. */
15804 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15806 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15807 if (GET_CODE (loc) == EXPR_LIST)
15808 loc = XEXP (loc, 0);
15809 loc_result = loc_descriptor (loc, mode, initialized);
15810 break;
15813 rtl = XEXP (rtl, 1);
15814 /* FALLTHRU */
15816 case PARALLEL:
15818 rtvec par_elems = XVEC (rtl, 0);
15819 int num_elem = GET_NUM_ELEM (par_elems);
15820 machine_mode mode;
15821 int i;
15823 /* Create the first one, so we have something to add to. */
15824 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15825 VOIDmode, initialized);
15826 if (loc_result == NULL)
15827 return NULL;
15828 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15829 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15830 for (i = 1; i < num_elem; i++)
15832 dw_loc_descr_ref temp;
15834 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15835 VOIDmode, initialized);
15836 if (temp == NULL)
15837 return NULL;
15838 add_loc_descr (&loc_result, temp);
15839 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15840 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15843 break;
15845 case CONST_INT:
15846 if (mode != VOIDmode && mode != BLKmode)
15848 int_mode = as_a <scalar_int_mode> (mode);
15849 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15850 INTVAL (rtl));
15852 break;
15854 case CONST_DOUBLE:
15855 if (mode == VOIDmode)
15856 mode = GET_MODE (rtl);
15858 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15860 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15862 /* Note that a CONST_DOUBLE rtx could represent either an integer
15863 or a floating-point constant. A CONST_DOUBLE is used whenever
15864 the constant requires more than one word in order to be
15865 adequately represented. We output CONST_DOUBLEs as blocks. */
15866 scalar_mode smode = as_a <scalar_mode> (mode);
15867 loc_result = new_loc_descr (DW_OP_implicit_value,
15868 GET_MODE_SIZE (smode), 0);
15869 #if TARGET_SUPPORTS_WIDE_INT == 0
15870 if (!SCALAR_FLOAT_MODE_P (smode))
15872 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15873 loc_result->dw_loc_oprnd2.v.val_double
15874 = rtx_to_double_int (rtl);
15876 else
15877 #endif
15879 unsigned int length = GET_MODE_SIZE (smode);
15880 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15882 insert_float (rtl, array);
15883 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15884 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15885 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15886 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15889 break;
15891 case CONST_WIDE_INT:
15892 if (mode == VOIDmode)
15893 mode = GET_MODE (rtl);
15895 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15897 int_mode = as_a <scalar_int_mode> (mode);
15898 loc_result = new_loc_descr (DW_OP_implicit_value,
15899 GET_MODE_SIZE (int_mode), 0);
15900 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
15901 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15902 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
15904 break;
15906 case CONST_VECTOR:
15907 if (mode == VOIDmode)
15908 mode = GET_MODE (rtl);
15910 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15912 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15913 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15914 unsigned char *array
15915 = ggc_vec_alloc<unsigned char> (length * elt_size);
15916 unsigned int i;
15917 unsigned char *p;
15918 machine_mode imode = GET_MODE_INNER (mode);
15920 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15921 switch (GET_MODE_CLASS (mode))
15923 case MODE_VECTOR_INT:
15924 for (i = 0, p = array; i < length; i++, p += elt_size)
15926 rtx elt = CONST_VECTOR_ELT (rtl, i);
15927 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
15929 break;
15931 case MODE_VECTOR_FLOAT:
15932 for (i = 0, p = array; i < length; i++, p += elt_size)
15934 rtx elt = CONST_VECTOR_ELT (rtl, i);
15935 insert_float (elt, p);
15937 break;
15939 default:
15940 gcc_unreachable ();
15943 loc_result = new_loc_descr (DW_OP_implicit_value,
15944 length * elt_size, 0);
15945 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15946 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15947 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15948 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15950 break;
15952 case CONST:
15953 if (mode == VOIDmode
15954 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
15955 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
15956 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15958 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15959 break;
15961 /* FALLTHROUGH */
15962 case SYMBOL_REF:
15963 if (!const_ok_for_output (rtl))
15964 break;
15965 /* FALLTHROUGH */
15966 case LABEL_REF:
15967 if (is_a <scalar_int_mode> (mode, &int_mode)
15968 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
15969 && (dwarf_version >= 4 || !dwarf_strict))
15971 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15972 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15973 vec_safe_push (used_rtx_array, rtl);
15975 break;
15977 case DEBUG_IMPLICIT_PTR:
15978 loc_result = implicit_ptr_descriptor (rtl, 0);
15979 break;
15981 case PLUS:
15982 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15983 && CONST_INT_P (XEXP (rtl, 1)))
15985 loc_result
15986 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15987 break;
15989 /* FALLTHRU */
15990 do_default:
15991 default:
15992 if ((is_a <scalar_int_mode> (mode, &int_mode)
15993 && GET_MODE (rtl) == int_mode
15994 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15995 && dwarf_version >= 4)
15996 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15998 /* Value expression. */
15999 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16000 if (loc_result)
16001 add_loc_descr (&loc_result,
16002 new_loc_descr (DW_OP_stack_value, 0, 0));
16004 break;
16007 return loc_result;
16010 /* We need to figure out what section we should use as the base for the
16011 address ranges where a given location is valid.
16012 1. If this particular DECL has a section associated with it, use that.
16013 2. If this function has a section associated with it, use that.
16014 3. Otherwise, use the text section.
16015 XXX: If you split a variable across multiple sections, we won't notice. */
16017 static const char *
16018 secname_for_decl (const_tree decl)
16020 const char *secname;
16022 if (VAR_OR_FUNCTION_DECL_P (decl)
16023 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16024 && DECL_SECTION_NAME (decl))
16025 secname = DECL_SECTION_NAME (decl);
16026 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16027 secname = DECL_SECTION_NAME (current_function_decl);
16028 else if (cfun && in_cold_section_p)
16029 secname = crtl->subsections.cold_section_label;
16030 else
16031 secname = text_section_label;
16033 return secname;
16036 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16038 static bool
16039 decl_by_reference_p (tree decl)
16041 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16042 || VAR_P (decl))
16043 && DECL_BY_REFERENCE (decl));
16046 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16047 for VARLOC. */
16049 static dw_loc_descr_ref
16050 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16051 enum var_init_status initialized)
16053 int have_address = 0;
16054 dw_loc_descr_ref descr;
16055 machine_mode mode;
16057 if (want_address != 2)
16059 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16060 /* Single part. */
16061 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16063 varloc = PAT_VAR_LOCATION_LOC (varloc);
16064 if (GET_CODE (varloc) == EXPR_LIST)
16065 varloc = XEXP (varloc, 0);
16066 mode = GET_MODE (varloc);
16067 if (MEM_P (varloc))
16069 rtx addr = XEXP (varloc, 0);
16070 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16071 mode, initialized);
16072 if (descr)
16073 have_address = 1;
16074 else
16076 rtx x = avoid_constant_pool_reference (varloc);
16077 if (x != varloc)
16078 descr = mem_loc_descriptor (x, mode, VOIDmode,
16079 initialized);
16082 else
16083 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16085 else
16086 return 0;
16088 else
16090 if (GET_CODE (varloc) == VAR_LOCATION)
16091 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16092 else
16093 mode = DECL_MODE (loc);
16094 descr = loc_descriptor (varloc, mode, initialized);
16095 have_address = 1;
16098 if (!descr)
16099 return 0;
16101 if (want_address == 2 && !have_address
16102 && (dwarf_version >= 4 || !dwarf_strict))
16104 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16106 expansion_failed (loc, NULL_RTX,
16107 "DWARF address size mismatch");
16108 return 0;
16110 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16111 have_address = 1;
16113 /* Show if we can't fill the request for an address. */
16114 if (want_address && !have_address)
16116 expansion_failed (loc, NULL_RTX,
16117 "Want address and only have value");
16118 return 0;
16121 /* If we've got an address and don't want one, dereference. */
16122 if (!want_address && have_address)
16124 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16125 enum dwarf_location_atom op;
16127 if (size > DWARF2_ADDR_SIZE || size == -1)
16129 expansion_failed (loc, NULL_RTX,
16130 "DWARF address size mismatch");
16131 return 0;
16133 else if (size == DWARF2_ADDR_SIZE)
16134 op = DW_OP_deref;
16135 else
16136 op = DW_OP_deref_size;
16138 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16141 return descr;
16144 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16145 if it is not possible. */
16147 static dw_loc_descr_ref
16148 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16150 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16151 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16152 else if (dwarf_version >= 3 || !dwarf_strict)
16153 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16154 else
16155 return NULL;
16158 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16159 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16161 static dw_loc_descr_ref
16162 dw_sra_loc_expr (tree decl, rtx loc)
16164 rtx p;
16165 unsigned HOST_WIDE_INT padsize = 0;
16166 dw_loc_descr_ref descr, *descr_tail;
16167 unsigned HOST_WIDE_INT decl_size;
16168 rtx varloc;
16169 enum var_init_status initialized;
16171 if (DECL_SIZE (decl) == NULL
16172 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16173 return NULL;
16175 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16176 descr = NULL;
16177 descr_tail = &descr;
16179 for (p = loc; p; p = XEXP (p, 1))
16181 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16182 rtx loc_note = *decl_piece_varloc_ptr (p);
16183 dw_loc_descr_ref cur_descr;
16184 dw_loc_descr_ref *tail, last = NULL;
16185 unsigned HOST_WIDE_INT opsize = 0;
16187 if (loc_note == NULL_RTX
16188 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16190 padsize += bitsize;
16191 continue;
16193 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16194 varloc = NOTE_VAR_LOCATION (loc_note);
16195 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16196 if (cur_descr == NULL)
16198 padsize += bitsize;
16199 continue;
16202 /* Check that cur_descr either doesn't use
16203 DW_OP_*piece operations, or their sum is equal
16204 to bitsize. Otherwise we can't embed it. */
16205 for (tail = &cur_descr; *tail != NULL;
16206 tail = &(*tail)->dw_loc_next)
16207 if ((*tail)->dw_loc_opc == DW_OP_piece)
16209 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16210 * BITS_PER_UNIT;
16211 last = *tail;
16213 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16215 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16216 last = *tail;
16219 if (last != NULL && opsize != bitsize)
16221 padsize += bitsize;
16222 /* Discard the current piece of the descriptor and release any
16223 addr_table entries it uses. */
16224 remove_loc_list_addr_table_entries (cur_descr);
16225 continue;
16228 /* If there is a hole, add DW_OP_*piece after empty DWARF
16229 expression, which means that those bits are optimized out. */
16230 if (padsize)
16232 if (padsize > decl_size)
16234 remove_loc_list_addr_table_entries (cur_descr);
16235 goto discard_descr;
16237 decl_size -= padsize;
16238 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16239 if (*descr_tail == NULL)
16241 remove_loc_list_addr_table_entries (cur_descr);
16242 goto discard_descr;
16244 descr_tail = &(*descr_tail)->dw_loc_next;
16245 padsize = 0;
16247 *descr_tail = cur_descr;
16248 descr_tail = tail;
16249 if (bitsize > decl_size)
16250 goto discard_descr;
16251 decl_size -= bitsize;
16252 if (last == NULL)
16254 HOST_WIDE_INT offset = 0;
16255 if (GET_CODE (varloc) == VAR_LOCATION
16256 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16258 varloc = PAT_VAR_LOCATION_LOC (varloc);
16259 if (GET_CODE (varloc) == EXPR_LIST)
16260 varloc = XEXP (varloc, 0);
16264 if (GET_CODE (varloc) == CONST
16265 || GET_CODE (varloc) == SIGN_EXTEND
16266 || GET_CODE (varloc) == ZERO_EXTEND)
16267 varloc = XEXP (varloc, 0);
16268 else if (GET_CODE (varloc) == SUBREG)
16269 varloc = SUBREG_REG (varloc);
16270 else
16271 break;
16273 while (1);
16274 /* DW_OP_bit_size offset should be zero for register
16275 or implicit location descriptions and empty location
16276 descriptions, but for memory addresses needs big endian
16277 adjustment. */
16278 if (MEM_P (varloc))
16280 unsigned HOST_WIDE_INT memsize
16281 = MEM_SIZE (varloc) * BITS_PER_UNIT;
16282 if (memsize != bitsize)
16284 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16285 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16286 goto discard_descr;
16287 if (memsize < bitsize)
16288 goto discard_descr;
16289 if (BITS_BIG_ENDIAN)
16290 offset = memsize - bitsize;
16294 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16295 if (*descr_tail == NULL)
16296 goto discard_descr;
16297 descr_tail = &(*descr_tail)->dw_loc_next;
16301 /* If there were any non-empty expressions, add padding till the end of
16302 the decl. */
16303 if (descr != NULL && decl_size != 0)
16305 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16306 if (*descr_tail == NULL)
16307 goto discard_descr;
16309 return descr;
16311 discard_descr:
16312 /* Discard the descriptor and release any addr_table entries it uses. */
16313 remove_loc_list_addr_table_entries (descr);
16314 return NULL;
16317 /* Return the dwarf representation of the location list LOC_LIST of
16318 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16319 function. */
16321 static dw_loc_list_ref
16322 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16324 const char *endname, *secname;
16325 rtx varloc;
16326 enum var_init_status initialized;
16327 struct var_loc_node *node;
16328 dw_loc_descr_ref descr;
16329 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16330 dw_loc_list_ref list = NULL;
16331 dw_loc_list_ref *listp = &list;
16333 /* Now that we know what section we are using for a base,
16334 actually construct the list of locations.
16335 The first location information is what is passed to the
16336 function that creates the location list, and the remaining
16337 locations just get added on to that list.
16338 Note that we only know the start address for a location
16339 (IE location changes), so to build the range, we use
16340 the range [current location start, next location start].
16341 This means we have to special case the last node, and generate
16342 a range of [last location start, end of function label]. */
16344 secname = secname_for_decl (decl);
16346 for (node = loc_list->first; node; node = node->next)
16347 if (GET_CODE (node->loc) == EXPR_LIST
16348 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16350 if (GET_CODE (node->loc) == EXPR_LIST)
16352 /* This requires DW_OP_{,bit_}piece, which is not usable
16353 inside DWARF expressions. */
16354 if (want_address != 2)
16355 continue;
16356 descr = dw_sra_loc_expr (decl, node->loc);
16357 if (descr == NULL)
16358 continue;
16360 else
16362 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16363 varloc = NOTE_VAR_LOCATION (node->loc);
16364 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16366 if (descr)
16368 bool range_across_switch = false;
16369 /* If section switch happens in between node->label
16370 and node->next->label (or end of function) and
16371 we can't emit it as a single entry list,
16372 emit two ranges, first one ending at the end
16373 of first partition and second one starting at the
16374 beginning of second partition. */
16375 if (node == loc_list->last_before_switch
16376 && (node != loc_list->first || loc_list->first->next)
16377 && current_function_decl)
16379 endname = cfun->fde->dw_fde_end;
16380 range_across_switch = true;
16382 /* The variable has a location between NODE->LABEL and
16383 NODE->NEXT->LABEL. */
16384 else if (node->next)
16385 endname = node->next->label;
16386 /* If the variable has a location at the last label
16387 it keeps its location until the end of function. */
16388 else if (!current_function_decl)
16389 endname = text_end_label;
16390 else
16392 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16393 current_function_funcdef_no);
16394 endname = ggc_strdup (label_id);
16397 *listp = new_loc_list (descr, node->label, endname, secname);
16398 if (TREE_CODE (decl) == PARM_DECL
16399 && node == loc_list->first
16400 && NOTE_P (node->loc)
16401 && strcmp (node->label, endname) == 0)
16402 (*listp)->force = true;
16403 listp = &(*listp)->dw_loc_next;
16405 if (range_across_switch)
16407 if (GET_CODE (node->loc) == EXPR_LIST)
16408 descr = dw_sra_loc_expr (decl, node->loc);
16409 else
16411 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16412 varloc = NOTE_VAR_LOCATION (node->loc);
16413 descr = dw_loc_list_1 (decl, varloc, want_address,
16414 initialized);
16416 gcc_assert (descr);
16417 /* The variable has a location between NODE->LABEL and
16418 NODE->NEXT->LABEL. */
16419 if (node->next)
16420 endname = node->next->label;
16421 else
16422 endname = cfun->fde->dw_fde_second_end;
16423 *listp = new_loc_list (descr,
16424 cfun->fde->dw_fde_second_begin,
16425 endname, secname);
16426 listp = &(*listp)->dw_loc_next;
16431 /* Try to avoid the overhead of a location list emitting a location
16432 expression instead, but only if we didn't have more than one
16433 location entry in the first place. If some entries were not
16434 representable, we don't want to pretend a single entry that was
16435 applies to the entire scope in which the variable is
16436 available. */
16437 if (list && loc_list->first->next)
16438 gen_llsym (list);
16440 return list;
16443 /* Return if the loc_list has only single element and thus can be represented
16444 as location description. */
16446 static bool
16447 single_element_loc_list_p (dw_loc_list_ref list)
16449 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16450 return !list->ll_symbol;
16453 /* Duplicate a single element of location list. */
16455 static inline dw_loc_descr_ref
16456 copy_loc_descr (dw_loc_descr_ref ref)
16458 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16459 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16460 return copy;
16463 /* To each location in list LIST append loc descr REF. */
16465 static void
16466 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16468 dw_loc_descr_ref copy;
16469 add_loc_descr (&list->expr, ref);
16470 list = list->dw_loc_next;
16471 while (list)
16473 copy = copy_loc_descr (ref);
16474 add_loc_descr (&list->expr, copy);
16475 while (copy->dw_loc_next)
16476 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16477 list = list->dw_loc_next;
16481 /* To each location in list LIST prepend loc descr REF. */
16483 static void
16484 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16486 dw_loc_descr_ref copy;
16487 dw_loc_descr_ref ref_end = list->expr;
16488 add_loc_descr (&ref, list->expr);
16489 list->expr = ref;
16490 list = list->dw_loc_next;
16491 while (list)
16493 dw_loc_descr_ref end = list->expr;
16494 list->expr = copy = copy_loc_descr (ref);
16495 while (copy->dw_loc_next != ref_end)
16496 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16497 copy->dw_loc_next = end;
16498 list = list->dw_loc_next;
16502 /* Given two lists RET and LIST
16503 produce location list that is result of adding expression in LIST
16504 to expression in RET on each position in program.
16505 Might be destructive on both RET and LIST.
16507 TODO: We handle only simple cases of RET or LIST having at most one
16508 element. General case would involve sorting the lists in program order
16509 and merging them that will need some additional work.
16510 Adding that will improve quality of debug info especially for SRA-ed
16511 structures. */
16513 static void
16514 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16516 if (!list)
16517 return;
16518 if (!*ret)
16520 *ret = list;
16521 return;
16523 if (!list->dw_loc_next)
16525 add_loc_descr_to_each (*ret, list->expr);
16526 return;
16528 if (!(*ret)->dw_loc_next)
16530 prepend_loc_descr_to_each (list, (*ret)->expr);
16531 *ret = list;
16532 return;
16534 expansion_failed (NULL_TREE, NULL_RTX,
16535 "Don't know how to merge two non-trivial"
16536 " location lists.\n");
16537 *ret = NULL;
16538 return;
16541 /* LOC is constant expression. Try a luck, look it up in constant
16542 pool and return its loc_descr of its address. */
16544 static dw_loc_descr_ref
16545 cst_pool_loc_descr (tree loc)
16547 /* Get an RTL for this, if something has been emitted. */
16548 rtx rtl = lookup_constant_def (loc);
16550 if (!rtl || !MEM_P (rtl))
16552 gcc_assert (!rtl);
16553 return 0;
16555 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16557 /* TODO: We might get more coverage if we was actually delaying expansion
16558 of all expressions till end of compilation when constant pools are fully
16559 populated. */
16560 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16562 expansion_failed (loc, NULL_RTX,
16563 "CST value in contant pool but not marked.");
16564 return 0;
16566 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16567 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16570 /* Return dw_loc_list representing address of addr_expr LOC
16571 by looking for inner INDIRECT_REF expression and turning
16572 it into simple arithmetics.
16574 See loc_list_from_tree for the meaning of CONTEXT. */
16576 static dw_loc_list_ref
16577 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16578 loc_descr_context *context)
16580 tree obj, offset;
16581 HOST_WIDE_INT bitsize, bitpos, bytepos;
16582 machine_mode mode;
16583 int unsignedp, reversep, volatilep = 0;
16584 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16586 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16587 &bitsize, &bitpos, &offset, &mode,
16588 &unsignedp, &reversep, &volatilep);
16589 STRIP_NOPS (obj);
16590 if (bitpos % BITS_PER_UNIT)
16592 expansion_failed (loc, NULL_RTX, "bitfield access");
16593 return 0;
16595 if (!INDIRECT_REF_P (obj))
16597 expansion_failed (obj,
16598 NULL_RTX, "no indirect ref in inner refrence");
16599 return 0;
16601 if (!offset && !bitpos)
16602 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16603 context);
16604 else if (toplev
16605 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16606 && (dwarf_version >= 4 || !dwarf_strict))
16608 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16609 if (!list_ret)
16610 return 0;
16611 if (offset)
16613 /* Variable offset. */
16614 list_ret1 = loc_list_from_tree (offset, 0, context);
16615 if (list_ret1 == 0)
16616 return 0;
16617 add_loc_list (&list_ret, list_ret1);
16618 if (!list_ret)
16619 return 0;
16620 add_loc_descr_to_each (list_ret,
16621 new_loc_descr (DW_OP_plus, 0, 0));
16623 bytepos = bitpos / BITS_PER_UNIT;
16624 if (bytepos > 0)
16625 add_loc_descr_to_each (list_ret,
16626 new_loc_descr (DW_OP_plus_uconst,
16627 bytepos, 0));
16628 else if (bytepos < 0)
16629 loc_list_plus_const (list_ret, bytepos);
16630 add_loc_descr_to_each (list_ret,
16631 new_loc_descr (DW_OP_stack_value, 0, 0));
16633 return list_ret;
16636 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16637 all operations from LOC are nops, move to the last one. Insert in NOPS all
16638 operations that are skipped. */
16640 static void
16641 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16642 hash_set<dw_loc_descr_ref> &nops)
16644 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16646 nops.add (loc);
16647 loc = loc->dw_loc_next;
16651 /* Helper for loc_descr_without_nops: free the location description operation
16652 P. */
16654 bool
16655 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16657 ggc_free (loc);
16658 return true;
16661 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16662 finishes LOC. */
16664 static void
16665 loc_descr_without_nops (dw_loc_descr_ref &loc)
16667 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16668 return;
16670 /* Set of all DW_OP_nop operations we remove. */
16671 hash_set<dw_loc_descr_ref> nops;
16673 /* First, strip all prefix NOP operations in order to keep the head of the
16674 operations list. */
16675 loc_descr_to_next_no_nop (loc, nops);
16677 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16679 /* For control flow operations: strip "prefix" nops in destination
16680 labels. */
16681 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16682 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16683 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16684 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16686 /* Do the same for the operations that follow, then move to the next
16687 iteration. */
16688 if (cur->dw_loc_next != NULL)
16689 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16690 cur = cur->dw_loc_next;
16693 nops.traverse<void *, free_loc_descr> (NULL);
16697 struct dwarf_procedure_info;
16699 /* Helper structure for location descriptions generation. */
16700 struct loc_descr_context
16702 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16703 NULL_TREE if DW_OP_push_object_address in invalid for this location
16704 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16705 tree context_type;
16706 /* The ..._DECL node that should be translated as a
16707 DW_OP_push_object_address operation. */
16708 tree base_decl;
16709 /* Information about the DWARF procedure we are currently generating. NULL if
16710 we are not generating a DWARF procedure. */
16711 struct dwarf_procedure_info *dpi;
16712 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16713 by consumer. Used for DW_TAG_generic_subrange attributes. */
16714 bool placeholder_arg;
16715 /* True if PLACEHOLDER_EXPR has been seen. */
16716 bool placeholder_seen;
16719 /* DWARF procedures generation
16721 DWARF expressions (aka. location descriptions) are used to encode variable
16722 things such as sizes or offsets. Such computations can have redundant parts
16723 that can be factorized in order to reduce the size of the output debug
16724 information. This is the whole point of DWARF procedures.
16726 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16727 already factorized into functions ("size functions") in order to handle very
16728 big and complex types. Such functions are quite simple: they have integral
16729 arguments, they return an integral result and their body contains only a
16730 return statement with arithmetic expressions. This is the only kind of
16731 function we are interested in translating into DWARF procedures, here.
16733 DWARF expressions and DWARF procedure are executed using a stack, so we have
16734 to define some calling convention for them to interact. Let's say that:
16736 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16737 all arguments in reverse order (right-to-left) so that when the DWARF
16738 procedure execution starts, the first argument is the top of the stack.
16740 - Then, when returning, the DWARF procedure must have consumed all arguments
16741 on the stack, must have pushed the result and touched nothing else.
16743 - Each integral argument and the result are integral types can be hold in a
16744 single stack slot.
16746 - We call "frame offset" the number of stack slots that are "under DWARF
16747 procedure control": it includes the arguments slots, the temporaries and
16748 the result slot. Thus, it is equal to the number of arguments when the
16749 procedure execution starts and must be equal to one (the result) when it
16750 returns. */
16752 /* Helper structure used when generating operations for a DWARF procedure. */
16753 struct dwarf_procedure_info
16755 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16756 currently translated. */
16757 tree fndecl;
16758 /* The number of arguments FNDECL takes. */
16759 unsigned args_count;
16762 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16763 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16764 equate it to this DIE. */
16766 static dw_die_ref
16767 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16768 dw_die_ref parent_die)
16770 dw_die_ref dwarf_proc_die;
16772 if ((dwarf_version < 3 && dwarf_strict)
16773 || location == NULL)
16774 return NULL;
16776 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16777 if (fndecl)
16778 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16779 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16780 return dwarf_proc_die;
16783 /* Return whether TYPE is a supported type as a DWARF procedure argument
16784 type or return type (we handle only scalar types and pointer types that
16785 aren't wider than the DWARF expression evaluation stack. */
16787 static bool
16788 is_handled_procedure_type (tree type)
16790 return ((INTEGRAL_TYPE_P (type)
16791 || TREE_CODE (type) == OFFSET_TYPE
16792 || TREE_CODE (type) == POINTER_TYPE)
16793 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16796 /* Helper for resolve_args_picking: do the same but stop when coming across
16797 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16798 offset *before* evaluating the corresponding operation. */
16800 static bool
16801 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16802 struct dwarf_procedure_info *dpi,
16803 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16805 /* The "frame_offset" identifier is already used to name a macro... */
16806 unsigned frame_offset_ = initial_frame_offset;
16807 dw_loc_descr_ref l;
16809 for (l = loc; l != NULL;)
16811 bool existed;
16812 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16814 /* If we already met this node, there is nothing to compute anymore. */
16815 if (existed)
16817 /* Make sure that the stack size is consistent wherever the execution
16818 flow comes from. */
16819 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16820 break;
16822 l_frame_offset = frame_offset_;
16824 /* If needed, relocate the picking offset with respect to the frame
16825 offset. */
16826 if (l->frame_offset_rel)
16828 unsigned HOST_WIDE_INT off;
16829 switch (l->dw_loc_opc)
16831 case DW_OP_pick:
16832 off = l->dw_loc_oprnd1.v.val_unsigned;
16833 break;
16834 case DW_OP_dup:
16835 off = 0;
16836 break;
16837 case DW_OP_over:
16838 off = 1;
16839 break;
16840 default:
16841 gcc_unreachable ();
16843 /* frame_offset_ is the size of the current stack frame, including
16844 incoming arguments. Besides, the arguments are pushed
16845 right-to-left. Thus, in order to access the Nth argument from
16846 this operation node, the picking has to skip temporaries *plus*
16847 one stack slot per argument (0 for the first one, 1 for the second
16848 one, etc.).
16850 The targetted argument number (N) is already set as the operand,
16851 and the number of temporaries can be computed with:
16852 frame_offsets_ - dpi->args_count */
16853 off += frame_offset_ - dpi->args_count;
16855 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
16856 if (off > 255)
16857 return false;
16859 if (off == 0)
16861 l->dw_loc_opc = DW_OP_dup;
16862 l->dw_loc_oprnd1.v.val_unsigned = 0;
16864 else if (off == 1)
16866 l->dw_loc_opc = DW_OP_over;
16867 l->dw_loc_oprnd1.v.val_unsigned = 0;
16869 else
16871 l->dw_loc_opc = DW_OP_pick;
16872 l->dw_loc_oprnd1.v.val_unsigned = off;
16876 /* Update frame_offset according to the effect the current operation has
16877 on the stack. */
16878 switch (l->dw_loc_opc)
16880 case DW_OP_deref:
16881 case DW_OP_swap:
16882 case DW_OP_rot:
16883 case DW_OP_abs:
16884 case DW_OP_neg:
16885 case DW_OP_not:
16886 case DW_OP_plus_uconst:
16887 case DW_OP_skip:
16888 case DW_OP_reg0:
16889 case DW_OP_reg1:
16890 case DW_OP_reg2:
16891 case DW_OP_reg3:
16892 case DW_OP_reg4:
16893 case DW_OP_reg5:
16894 case DW_OP_reg6:
16895 case DW_OP_reg7:
16896 case DW_OP_reg8:
16897 case DW_OP_reg9:
16898 case DW_OP_reg10:
16899 case DW_OP_reg11:
16900 case DW_OP_reg12:
16901 case DW_OP_reg13:
16902 case DW_OP_reg14:
16903 case DW_OP_reg15:
16904 case DW_OP_reg16:
16905 case DW_OP_reg17:
16906 case DW_OP_reg18:
16907 case DW_OP_reg19:
16908 case DW_OP_reg20:
16909 case DW_OP_reg21:
16910 case DW_OP_reg22:
16911 case DW_OP_reg23:
16912 case DW_OP_reg24:
16913 case DW_OP_reg25:
16914 case DW_OP_reg26:
16915 case DW_OP_reg27:
16916 case DW_OP_reg28:
16917 case DW_OP_reg29:
16918 case DW_OP_reg30:
16919 case DW_OP_reg31:
16920 case DW_OP_bregx:
16921 case DW_OP_piece:
16922 case DW_OP_deref_size:
16923 case DW_OP_nop:
16924 case DW_OP_bit_piece:
16925 case DW_OP_implicit_value:
16926 case DW_OP_stack_value:
16927 break;
16929 case DW_OP_addr:
16930 case DW_OP_const1u:
16931 case DW_OP_const1s:
16932 case DW_OP_const2u:
16933 case DW_OP_const2s:
16934 case DW_OP_const4u:
16935 case DW_OP_const4s:
16936 case DW_OP_const8u:
16937 case DW_OP_const8s:
16938 case DW_OP_constu:
16939 case DW_OP_consts:
16940 case DW_OP_dup:
16941 case DW_OP_over:
16942 case DW_OP_pick:
16943 case DW_OP_lit0:
16944 case DW_OP_lit1:
16945 case DW_OP_lit2:
16946 case DW_OP_lit3:
16947 case DW_OP_lit4:
16948 case DW_OP_lit5:
16949 case DW_OP_lit6:
16950 case DW_OP_lit7:
16951 case DW_OP_lit8:
16952 case DW_OP_lit9:
16953 case DW_OP_lit10:
16954 case DW_OP_lit11:
16955 case DW_OP_lit12:
16956 case DW_OP_lit13:
16957 case DW_OP_lit14:
16958 case DW_OP_lit15:
16959 case DW_OP_lit16:
16960 case DW_OP_lit17:
16961 case DW_OP_lit18:
16962 case DW_OP_lit19:
16963 case DW_OP_lit20:
16964 case DW_OP_lit21:
16965 case DW_OP_lit22:
16966 case DW_OP_lit23:
16967 case DW_OP_lit24:
16968 case DW_OP_lit25:
16969 case DW_OP_lit26:
16970 case DW_OP_lit27:
16971 case DW_OP_lit28:
16972 case DW_OP_lit29:
16973 case DW_OP_lit30:
16974 case DW_OP_lit31:
16975 case DW_OP_breg0:
16976 case DW_OP_breg1:
16977 case DW_OP_breg2:
16978 case DW_OP_breg3:
16979 case DW_OP_breg4:
16980 case DW_OP_breg5:
16981 case DW_OP_breg6:
16982 case DW_OP_breg7:
16983 case DW_OP_breg8:
16984 case DW_OP_breg9:
16985 case DW_OP_breg10:
16986 case DW_OP_breg11:
16987 case DW_OP_breg12:
16988 case DW_OP_breg13:
16989 case DW_OP_breg14:
16990 case DW_OP_breg15:
16991 case DW_OP_breg16:
16992 case DW_OP_breg17:
16993 case DW_OP_breg18:
16994 case DW_OP_breg19:
16995 case DW_OP_breg20:
16996 case DW_OP_breg21:
16997 case DW_OP_breg22:
16998 case DW_OP_breg23:
16999 case DW_OP_breg24:
17000 case DW_OP_breg25:
17001 case DW_OP_breg26:
17002 case DW_OP_breg27:
17003 case DW_OP_breg28:
17004 case DW_OP_breg29:
17005 case DW_OP_breg30:
17006 case DW_OP_breg31:
17007 case DW_OP_fbreg:
17008 case DW_OP_push_object_address:
17009 case DW_OP_call_frame_cfa:
17010 case DW_OP_GNU_variable_value:
17011 ++frame_offset_;
17012 break;
17014 case DW_OP_drop:
17015 case DW_OP_xderef:
17016 case DW_OP_and:
17017 case DW_OP_div:
17018 case DW_OP_minus:
17019 case DW_OP_mod:
17020 case DW_OP_mul:
17021 case DW_OP_or:
17022 case DW_OP_plus:
17023 case DW_OP_shl:
17024 case DW_OP_shr:
17025 case DW_OP_shra:
17026 case DW_OP_xor:
17027 case DW_OP_bra:
17028 case DW_OP_eq:
17029 case DW_OP_ge:
17030 case DW_OP_gt:
17031 case DW_OP_le:
17032 case DW_OP_lt:
17033 case DW_OP_ne:
17034 case DW_OP_regx:
17035 case DW_OP_xderef_size:
17036 --frame_offset_;
17037 break;
17039 case DW_OP_call2:
17040 case DW_OP_call4:
17041 case DW_OP_call_ref:
17043 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17044 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17046 if (stack_usage == NULL)
17047 return false;
17048 frame_offset_ += *stack_usage;
17049 break;
17052 case DW_OP_implicit_pointer:
17053 case DW_OP_entry_value:
17054 case DW_OP_const_type:
17055 case DW_OP_regval_type:
17056 case DW_OP_deref_type:
17057 case DW_OP_convert:
17058 case DW_OP_reinterpret:
17059 case DW_OP_form_tls_address:
17060 case DW_OP_GNU_push_tls_address:
17061 case DW_OP_GNU_uninit:
17062 case DW_OP_GNU_encoded_addr:
17063 case DW_OP_GNU_implicit_pointer:
17064 case DW_OP_GNU_entry_value:
17065 case DW_OP_GNU_const_type:
17066 case DW_OP_GNU_regval_type:
17067 case DW_OP_GNU_deref_type:
17068 case DW_OP_GNU_convert:
17069 case DW_OP_GNU_reinterpret:
17070 case DW_OP_GNU_parameter_ref:
17071 /* loc_list_from_tree will probably not output these operations for
17072 size functions, so assume they will not appear here. */
17073 /* Fall through... */
17075 default:
17076 gcc_unreachable ();
17079 /* Now, follow the control flow (except subroutine calls). */
17080 switch (l->dw_loc_opc)
17082 case DW_OP_bra:
17083 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17084 frame_offsets))
17085 return false;
17086 /* Fall through. */
17088 case DW_OP_skip:
17089 l = l->dw_loc_oprnd1.v.val_loc;
17090 break;
17092 case DW_OP_stack_value:
17093 return true;
17095 default:
17096 l = l->dw_loc_next;
17097 break;
17101 return true;
17104 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17105 operations) in order to resolve the operand of DW_OP_pick operations that
17106 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17107 offset *before* LOC is executed. Return if all relocations were
17108 successful. */
17110 static bool
17111 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17112 struct dwarf_procedure_info *dpi)
17114 /* Associate to all visited operations the frame offset *before* evaluating
17115 this operation. */
17116 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17118 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17119 frame_offsets);
17122 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17123 Return NULL if it is not possible. */
17125 static dw_die_ref
17126 function_to_dwarf_procedure (tree fndecl)
17128 struct loc_descr_context ctx;
17129 struct dwarf_procedure_info dpi;
17130 dw_die_ref dwarf_proc_die;
17131 tree tree_body = DECL_SAVED_TREE (fndecl);
17132 dw_loc_descr_ref loc_body, epilogue;
17134 tree cursor;
17135 unsigned i;
17137 /* Do not generate multiple DWARF procedures for the same function
17138 declaration. */
17139 dwarf_proc_die = lookup_decl_die (fndecl);
17140 if (dwarf_proc_die != NULL)
17141 return dwarf_proc_die;
17143 /* DWARF procedures are available starting with the DWARFv3 standard. */
17144 if (dwarf_version < 3 && dwarf_strict)
17145 return NULL;
17147 /* We handle only functions for which we still have a body, that return a
17148 supported type and that takes arguments with supported types. Note that
17149 there is no point translating functions that return nothing. */
17150 if (tree_body == NULL_TREE
17151 || DECL_RESULT (fndecl) == NULL_TREE
17152 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17153 return NULL;
17155 for (cursor = DECL_ARGUMENTS (fndecl);
17156 cursor != NULL_TREE;
17157 cursor = TREE_CHAIN (cursor))
17158 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17159 return NULL;
17161 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17162 if (TREE_CODE (tree_body) != RETURN_EXPR)
17163 return NULL;
17164 tree_body = TREE_OPERAND (tree_body, 0);
17165 if (TREE_CODE (tree_body) != MODIFY_EXPR
17166 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17167 return NULL;
17168 tree_body = TREE_OPERAND (tree_body, 1);
17170 /* Try to translate the body expression itself. Note that this will probably
17171 cause an infinite recursion if its call graph has a cycle. This is very
17172 unlikely for size functions, however, so don't bother with such things at
17173 the moment. */
17174 ctx.context_type = NULL_TREE;
17175 ctx.base_decl = NULL_TREE;
17176 ctx.dpi = &dpi;
17177 ctx.placeholder_arg = false;
17178 ctx.placeholder_seen = false;
17179 dpi.fndecl = fndecl;
17180 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17181 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17182 if (!loc_body)
17183 return NULL;
17185 /* After evaluating all operands in "loc_body", we should still have on the
17186 stack all arguments plus the desired function result (top of the stack).
17187 Generate code in order to keep only the result in our stack frame. */
17188 epilogue = NULL;
17189 for (i = 0; i < dpi.args_count; ++i)
17191 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17192 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17193 op_couple->dw_loc_next->dw_loc_next = epilogue;
17194 epilogue = op_couple;
17196 add_loc_descr (&loc_body, epilogue);
17197 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17198 return NULL;
17200 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17201 because they are considered useful. Now there is an epilogue, they are
17202 not anymore, so give it another try. */
17203 loc_descr_without_nops (loc_body);
17205 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17206 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17207 though, given that size functions do not come from source, so they should
17208 not have a dedicated DW_TAG_subprogram DIE. */
17209 dwarf_proc_die
17210 = new_dwarf_proc_die (loc_body, fndecl,
17211 get_context_die (DECL_CONTEXT (fndecl)));
17213 /* The called DWARF procedure consumes one stack slot per argument and
17214 returns one stack slot. */
17215 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17217 return dwarf_proc_die;
17221 /* Generate Dwarf location list representing LOC.
17222 If WANT_ADDRESS is false, expression computing LOC will be computed
17223 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17224 if WANT_ADDRESS is 2, expression computing address useable in location
17225 will be returned (i.e. DW_OP_reg can be used
17226 to refer to register values).
17228 CONTEXT provides information to customize the location descriptions
17229 generation. Its context_type field specifies what type is implicitly
17230 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17231 will not be generated.
17233 Its DPI field determines whether we are generating a DWARF expression for a
17234 DWARF procedure, so PARM_DECL references are processed specifically.
17236 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17237 and dpi fields were null. */
17239 static dw_loc_list_ref
17240 loc_list_from_tree_1 (tree loc, int want_address,
17241 struct loc_descr_context *context)
17243 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17244 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17245 int have_address = 0;
17246 enum dwarf_location_atom op;
17248 /* ??? Most of the time we do not take proper care for sign/zero
17249 extending the values properly. Hopefully this won't be a real
17250 problem... */
17252 if (context != NULL
17253 && context->base_decl == loc
17254 && want_address == 0)
17256 if (dwarf_version >= 3 || !dwarf_strict)
17257 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17258 NULL, NULL, NULL);
17259 else
17260 return NULL;
17263 switch (TREE_CODE (loc))
17265 case ERROR_MARK:
17266 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17267 return 0;
17269 case PLACEHOLDER_EXPR:
17270 /* This case involves extracting fields from an object to determine the
17271 position of other fields. It is supposed to appear only as the first
17272 operand of COMPONENT_REF nodes and to reference precisely the type
17273 that the context allows. */
17274 if (context != NULL
17275 && TREE_TYPE (loc) == context->context_type
17276 && want_address >= 1)
17278 if (dwarf_version >= 3 || !dwarf_strict)
17280 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17281 have_address = 1;
17282 break;
17284 else
17285 return NULL;
17287 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17288 the single argument passed by consumer. */
17289 else if (context != NULL
17290 && context->placeholder_arg
17291 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17292 && want_address == 0)
17294 ret = new_loc_descr (DW_OP_pick, 0, 0);
17295 ret->frame_offset_rel = 1;
17296 context->placeholder_seen = true;
17297 break;
17299 else
17300 expansion_failed (loc, NULL_RTX,
17301 "PLACEHOLDER_EXPR for an unexpected type");
17302 break;
17304 case CALL_EXPR:
17306 const int nargs = call_expr_nargs (loc);
17307 tree callee = get_callee_fndecl (loc);
17308 int i;
17309 dw_die_ref dwarf_proc;
17311 if (callee == NULL_TREE)
17312 goto call_expansion_failed;
17314 /* We handle only functions that return an integer. */
17315 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17316 goto call_expansion_failed;
17318 dwarf_proc = function_to_dwarf_procedure (callee);
17319 if (dwarf_proc == NULL)
17320 goto call_expansion_failed;
17322 /* Evaluate arguments right-to-left so that the first argument will
17323 be the top-most one on the stack. */
17324 for (i = nargs - 1; i >= 0; --i)
17326 dw_loc_descr_ref loc_descr
17327 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17328 context);
17330 if (loc_descr == NULL)
17331 goto call_expansion_failed;
17333 add_loc_descr (&ret, loc_descr);
17336 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17337 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17338 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17339 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17340 add_loc_descr (&ret, ret1);
17341 break;
17343 call_expansion_failed:
17344 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17345 /* There are no opcodes for these operations. */
17346 return 0;
17349 case PREINCREMENT_EXPR:
17350 case PREDECREMENT_EXPR:
17351 case POSTINCREMENT_EXPR:
17352 case POSTDECREMENT_EXPR:
17353 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17354 /* There are no opcodes for these operations. */
17355 return 0;
17357 case ADDR_EXPR:
17358 /* If we already want an address, see if there is INDIRECT_REF inside
17359 e.g. for &this->field. */
17360 if (want_address)
17362 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17363 (loc, want_address == 2, context);
17364 if (list_ret)
17365 have_address = 1;
17366 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17367 && (ret = cst_pool_loc_descr (loc)))
17368 have_address = 1;
17370 /* Otherwise, process the argument and look for the address. */
17371 if (!list_ret && !ret)
17372 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17373 else
17375 if (want_address)
17376 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17377 return NULL;
17379 break;
17381 case VAR_DECL:
17382 if (DECL_THREAD_LOCAL_P (loc))
17384 rtx rtl;
17385 enum dwarf_location_atom tls_op;
17386 enum dtprel_bool dtprel = dtprel_false;
17388 if (targetm.have_tls)
17390 /* If this is not defined, we have no way to emit the
17391 data. */
17392 if (!targetm.asm_out.output_dwarf_dtprel)
17393 return 0;
17395 /* The way DW_OP_GNU_push_tls_address is specified, we
17396 can only look up addresses of objects in the current
17397 module. We used DW_OP_addr as first op, but that's
17398 wrong, because DW_OP_addr is relocated by the debug
17399 info consumer, while DW_OP_GNU_push_tls_address
17400 operand shouldn't be. */
17401 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17402 return 0;
17403 dtprel = dtprel_true;
17404 /* We check for DWARF 5 here because gdb did not implement
17405 DW_OP_form_tls_address until after 7.12. */
17406 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17407 : DW_OP_GNU_push_tls_address);
17409 else
17411 if (!targetm.emutls.debug_form_tls_address
17412 || !(dwarf_version >= 3 || !dwarf_strict))
17413 return 0;
17414 /* We stuffed the control variable into the DECL_VALUE_EXPR
17415 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17416 no longer appear in gimple code. We used the control
17417 variable in specific so that we could pick it up here. */
17418 loc = DECL_VALUE_EXPR (loc);
17419 tls_op = DW_OP_form_tls_address;
17422 rtl = rtl_for_decl_location (loc);
17423 if (rtl == NULL_RTX)
17424 return 0;
17426 if (!MEM_P (rtl))
17427 return 0;
17428 rtl = XEXP (rtl, 0);
17429 if (! CONSTANT_P (rtl))
17430 return 0;
17432 ret = new_addr_loc_descr (rtl, dtprel);
17433 ret1 = new_loc_descr (tls_op, 0, 0);
17434 add_loc_descr (&ret, ret1);
17436 have_address = 1;
17437 break;
17439 /* FALLTHRU */
17441 case PARM_DECL:
17442 if (context != NULL && context->dpi != NULL
17443 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17445 /* We are generating code for a DWARF procedure and we want to access
17446 one of its arguments: find the appropriate argument offset and let
17447 the resolve_args_picking pass compute the offset that complies
17448 with the stack frame size. */
17449 unsigned i = 0;
17450 tree cursor;
17452 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17453 cursor != NULL_TREE && cursor != loc;
17454 cursor = TREE_CHAIN (cursor), ++i)
17456 /* If we are translating a DWARF procedure, all referenced parameters
17457 must belong to the current function. */
17458 gcc_assert (cursor != NULL_TREE);
17460 ret = new_loc_descr (DW_OP_pick, i, 0);
17461 ret->frame_offset_rel = 1;
17462 break;
17464 /* FALLTHRU */
17466 case RESULT_DECL:
17467 if (DECL_HAS_VALUE_EXPR_P (loc))
17468 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17469 want_address, context);
17470 /* FALLTHRU */
17472 case FUNCTION_DECL:
17474 rtx rtl;
17475 var_loc_list *loc_list = lookup_decl_loc (loc);
17477 if (loc_list && loc_list->first)
17479 list_ret = dw_loc_list (loc_list, loc, want_address);
17480 have_address = want_address != 0;
17481 break;
17483 rtl = rtl_for_decl_location (loc);
17484 if (rtl == NULL_RTX)
17486 if (TREE_CODE (loc) != FUNCTION_DECL
17487 && early_dwarf
17488 && current_function_decl
17489 && want_address != 1
17490 && ! DECL_IGNORED_P (loc)
17491 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17492 || POINTER_TYPE_P (TREE_TYPE (loc)))
17493 && DECL_CONTEXT (loc) == current_function_decl
17494 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (loc)))
17495 <= DWARF2_ADDR_SIZE))
17497 dw_die_ref ref = lookup_decl_die (loc);
17498 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17499 if (ref)
17501 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17502 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17503 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17505 else
17507 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17508 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17510 break;
17512 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17513 return 0;
17515 else if (CONST_INT_P (rtl))
17517 HOST_WIDE_INT val = INTVAL (rtl);
17518 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17519 val &= GET_MODE_MASK (DECL_MODE (loc));
17520 ret = int_loc_descriptor (val);
17522 else if (GET_CODE (rtl) == CONST_STRING)
17524 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17525 return 0;
17527 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17528 ret = new_addr_loc_descr (rtl, dtprel_false);
17529 else
17531 machine_mode mode, mem_mode;
17533 /* Certain constructs can only be represented at top-level. */
17534 if (want_address == 2)
17536 ret = loc_descriptor (rtl, VOIDmode,
17537 VAR_INIT_STATUS_INITIALIZED);
17538 have_address = 1;
17540 else
17542 mode = GET_MODE (rtl);
17543 mem_mode = VOIDmode;
17544 if (MEM_P (rtl))
17546 mem_mode = mode;
17547 mode = get_address_mode (rtl);
17548 rtl = XEXP (rtl, 0);
17549 have_address = 1;
17551 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17552 VAR_INIT_STATUS_INITIALIZED);
17554 if (!ret)
17555 expansion_failed (loc, rtl,
17556 "failed to produce loc descriptor for rtl");
17559 break;
17561 case MEM_REF:
17562 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17564 have_address = 1;
17565 goto do_plus;
17567 /* Fallthru. */
17568 case INDIRECT_REF:
17569 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17570 have_address = 1;
17571 break;
17573 case TARGET_MEM_REF:
17574 case SSA_NAME:
17575 case DEBUG_EXPR_DECL:
17576 return NULL;
17578 case COMPOUND_EXPR:
17579 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17580 context);
17582 CASE_CONVERT:
17583 case VIEW_CONVERT_EXPR:
17584 case SAVE_EXPR:
17585 case MODIFY_EXPR:
17586 case NON_LVALUE_EXPR:
17587 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17588 context);
17590 case COMPONENT_REF:
17591 case BIT_FIELD_REF:
17592 case ARRAY_REF:
17593 case ARRAY_RANGE_REF:
17594 case REALPART_EXPR:
17595 case IMAGPART_EXPR:
17597 tree obj, offset;
17598 HOST_WIDE_INT bitsize, bitpos, bytepos;
17599 machine_mode mode;
17600 int unsignedp, reversep, volatilep = 0;
17602 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17603 &unsignedp, &reversep, &volatilep);
17605 gcc_assert (obj != loc);
17607 list_ret = loc_list_from_tree_1 (obj,
17608 want_address == 2
17609 && !bitpos && !offset ? 2 : 1,
17610 context);
17611 /* TODO: We can extract value of the small expression via shifting even
17612 for nonzero bitpos. */
17613 if (list_ret == 0)
17614 return 0;
17615 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
17617 expansion_failed (loc, NULL_RTX,
17618 "bitfield access");
17619 return 0;
17622 if (offset != NULL_TREE)
17624 /* Variable offset. */
17625 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17626 if (list_ret1 == 0)
17627 return 0;
17628 add_loc_list (&list_ret, list_ret1);
17629 if (!list_ret)
17630 return 0;
17631 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17634 bytepos = bitpos / BITS_PER_UNIT;
17635 if (bytepos > 0)
17636 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
17637 else if (bytepos < 0)
17638 loc_list_plus_const (list_ret, bytepos);
17640 have_address = 1;
17641 break;
17644 case INTEGER_CST:
17645 if ((want_address || !tree_fits_shwi_p (loc))
17646 && (ret = cst_pool_loc_descr (loc)))
17647 have_address = 1;
17648 else if (want_address == 2
17649 && tree_fits_shwi_p (loc)
17650 && (ret = address_of_int_loc_descriptor
17651 (int_size_in_bytes (TREE_TYPE (loc)),
17652 tree_to_shwi (loc))))
17653 have_address = 1;
17654 else if (tree_fits_shwi_p (loc))
17655 ret = int_loc_descriptor (tree_to_shwi (loc));
17656 else if (tree_fits_uhwi_p (loc))
17657 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17658 else
17660 expansion_failed (loc, NULL_RTX,
17661 "Integer operand is not host integer");
17662 return 0;
17664 break;
17666 case CONSTRUCTOR:
17667 case REAL_CST:
17668 case STRING_CST:
17669 case COMPLEX_CST:
17670 if ((ret = cst_pool_loc_descr (loc)))
17671 have_address = 1;
17672 else if (TREE_CODE (loc) == CONSTRUCTOR)
17674 tree type = TREE_TYPE (loc);
17675 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17676 unsigned HOST_WIDE_INT offset = 0;
17677 unsigned HOST_WIDE_INT cnt;
17678 constructor_elt *ce;
17680 if (TREE_CODE (type) == RECORD_TYPE)
17682 /* This is very limited, but it's enough to output
17683 pointers to member functions, as long as the
17684 referenced function is defined in the current
17685 translation unit. */
17686 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17688 tree val = ce->value;
17690 tree field = ce->index;
17692 if (val)
17693 STRIP_NOPS (val);
17695 if (!field || DECL_BIT_FIELD (field))
17697 expansion_failed (loc, NULL_RTX,
17698 "bitfield in record type constructor");
17699 size = offset = (unsigned HOST_WIDE_INT)-1;
17700 ret = NULL;
17701 break;
17704 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17705 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17706 gcc_assert (pos + fieldsize <= size);
17707 if (pos < offset)
17709 expansion_failed (loc, NULL_RTX,
17710 "out-of-order fields in record constructor");
17711 size = offset = (unsigned HOST_WIDE_INT)-1;
17712 ret = NULL;
17713 break;
17715 if (pos > offset)
17717 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17718 add_loc_descr (&ret, ret1);
17719 offset = pos;
17721 if (val && fieldsize != 0)
17723 ret1 = loc_descriptor_from_tree (val, want_address, context);
17724 if (!ret1)
17726 expansion_failed (loc, NULL_RTX,
17727 "unsupported expression in field");
17728 size = offset = (unsigned HOST_WIDE_INT)-1;
17729 ret = NULL;
17730 break;
17732 add_loc_descr (&ret, ret1);
17734 if (fieldsize)
17736 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17737 add_loc_descr (&ret, ret1);
17738 offset = pos + fieldsize;
17742 if (offset != size)
17744 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17745 add_loc_descr (&ret, ret1);
17746 offset = size;
17749 have_address = !!want_address;
17751 else
17752 expansion_failed (loc, NULL_RTX,
17753 "constructor of non-record type");
17755 else
17756 /* We can construct small constants here using int_loc_descriptor. */
17757 expansion_failed (loc, NULL_RTX,
17758 "constructor or constant not in constant pool");
17759 break;
17761 case TRUTH_AND_EXPR:
17762 case TRUTH_ANDIF_EXPR:
17763 case BIT_AND_EXPR:
17764 op = DW_OP_and;
17765 goto do_binop;
17767 case TRUTH_XOR_EXPR:
17768 case BIT_XOR_EXPR:
17769 op = DW_OP_xor;
17770 goto do_binop;
17772 case TRUTH_OR_EXPR:
17773 case TRUTH_ORIF_EXPR:
17774 case BIT_IOR_EXPR:
17775 op = DW_OP_or;
17776 goto do_binop;
17778 case FLOOR_DIV_EXPR:
17779 case CEIL_DIV_EXPR:
17780 case ROUND_DIV_EXPR:
17781 case TRUNC_DIV_EXPR:
17782 case EXACT_DIV_EXPR:
17783 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17784 return 0;
17785 op = DW_OP_div;
17786 goto do_binop;
17788 case MINUS_EXPR:
17789 op = DW_OP_minus;
17790 goto do_binop;
17792 case FLOOR_MOD_EXPR:
17793 case CEIL_MOD_EXPR:
17794 case ROUND_MOD_EXPR:
17795 case TRUNC_MOD_EXPR:
17796 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17798 op = DW_OP_mod;
17799 goto do_binop;
17801 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17802 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17803 if (list_ret == 0 || list_ret1 == 0)
17804 return 0;
17806 add_loc_list (&list_ret, list_ret1);
17807 if (list_ret == 0)
17808 return 0;
17809 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17810 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17811 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17812 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17813 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17814 break;
17816 case MULT_EXPR:
17817 op = DW_OP_mul;
17818 goto do_binop;
17820 case LSHIFT_EXPR:
17821 op = DW_OP_shl;
17822 goto do_binop;
17824 case RSHIFT_EXPR:
17825 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17826 goto do_binop;
17828 case POINTER_PLUS_EXPR:
17829 case PLUS_EXPR:
17830 do_plus:
17831 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17833 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17834 smarter to encode their opposite. The DW_OP_plus_uconst operation
17835 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17836 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17837 bytes, Y being the size of the operation that pushes the opposite
17838 of the addend. So let's choose the smallest representation. */
17839 const tree tree_addend = TREE_OPERAND (loc, 1);
17840 offset_int wi_addend;
17841 HOST_WIDE_INT shwi_addend;
17842 dw_loc_descr_ref loc_naddend;
17844 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17845 if (list_ret == 0)
17846 return 0;
17848 /* Try to get the literal to push. It is the opposite of the addend,
17849 so as we rely on wrapping during DWARF evaluation, first decode
17850 the literal as a "DWARF-sized" signed number. */
17851 wi_addend = wi::to_offset (tree_addend);
17852 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
17853 shwi_addend = wi_addend.to_shwi ();
17854 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
17855 ? int_loc_descriptor (-shwi_addend)
17856 : NULL;
17858 if (loc_naddend != NULL
17859 && ((unsigned) size_of_uleb128 (shwi_addend)
17860 > size_of_loc_descr (loc_naddend)))
17862 add_loc_descr_to_each (list_ret, loc_naddend);
17863 add_loc_descr_to_each (list_ret,
17864 new_loc_descr (DW_OP_minus, 0, 0));
17866 else
17868 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
17870 loc_naddend = loc_cur;
17871 loc_cur = loc_cur->dw_loc_next;
17872 ggc_free (loc_naddend);
17874 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
17876 break;
17879 op = DW_OP_plus;
17880 goto do_binop;
17882 case LE_EXPR:
17883 op = DW_OP_le;
17884 goto do_comp_binop;
17886 case GE_EXPR:
17887 op = DW_OP_ge;
17888 goto do_comp_binop;
17890 case LT_EXPR:
17891 op = DW_OP_lt;
17892 goto do_comp_binop;
17894 case GT_EXPR:
17895 op = DW_OP_gt;
17896 goto do_comp_binop;
17898 do_comp_binop:
17899 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
17901 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
17902 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
17903 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
17904 TREE_CODE (loc));
17905 break;
17907 else
17908 goto do_binop;
17910 case EQ_EXPR:
17911 op = DW_OP_eq;
17912 goto do_binop;
17914 case NE_EXPR:
17915 op = DW_OP_ne;
17916 goto do_binop;
17918 do_binop:
17919 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17920 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17921 if (list_ret == 0 || list_ret1 == 0)
17922 return 0;
17924 add_loc_list (&list_ret, list_ret1);
17925 if (list_ret == 0)
17926 return 0;
17927 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17928 break;
17930 case TRUTH_NOT_EXPR:
17931 case BIT_NOT_EXPR:
17932 op = DW_OP_not;
17933 goto do_unop;
17935 case ABS_EXPR:
17936 op = DW_OP_abs;
17937 goto do_unop;
17939 case NEGATE_EXPR:
17940 op = DW_OP_neg;
17941 goto do_unop;
17943 do_unop:
17944 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17945 if (list_ret == 0)
17946 return 0;
17948 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
17949 break;
17951 case MIN_EXPR:
17952 case MAX_EXPR:
17954 const enum tree_code code =
17955 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
17957 loc = build3 (COND_EXPR, TREE_TYPE (loc),
17958 build2 (code, integer_type_node,
17959 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
17960 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
17963 /* fall through */
17965 case COND_EXPR:
17967 dw_loc_descr_ref lhs
17968 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
17969 dw_loc_list_ref rhs
17970 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
17971 dw_loc_descr_ref bra_node, jump_node, tmp;
17973 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17974 if (list_ret == 0 || lhs == 0 || rhs == 0)
17975 return 0;
17977 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
17978 add_loc_descr_to_each (list_ret, bra_node);
17980 add_loc_list (&list_ret, rhs);
17981 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
17982 add_loc_descr_to_each (list_ret, jump_node);
17984 add_loc_descr_to_each (list_ret, lhs);
17985 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17986 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
17988 /* ??? Need a node to point the skip at. Use a nop. */
17989 tmp = new_loc_descr (DW_OP_nop, 0, 0);
17990 add_loc_descr_to_each (list_ret, tmp);
17991 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
17992 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
17994 break;
17996 case FIX_TRUNC_EXPR:
17997 return 0;
17999 default:
18000 /* Leave front-end specific codes as simply unknown. This comes
18001 up, for instance, with the C STMT_EXPR. */
18002 if ((unsigned int) TREE_CODE (loc)
18003 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18005 expansion_failed (loc, NULL_RTX,
18006 "language specific tree node");
18007 return 0;
18010 /* Otherwise this is a generic code; we should just lists all of
18011 these explicitly. We forgot one. */
18012 if (flag_checking)
18013 gcc_unreachable ();
18015 /* In a release build, we want to degrade gracefully: better to
18016 generate incomplete debugging information than to crash. */
18017 return NULL;
18020 if (!ret && !list_ret)
18021 return 0;
18023 if (want_address == 2 && !have_address
18024 && (dwarf_version >= 4 || !dwarf_strict))
18026 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18028 expansion_failed (loc, NULL_RTX,
18029 "DWARF address size mismatch");
18030 return 0;
18032 if (ret)
18033 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18034 else
18035 add_loc_descr_to_each (list_ret,
18036 new_loc_descr (DW_OP_stack_value, 0, 0));
18037 have_address = 1;
18039 /* Show if we can't fill the request for an address. */
18040 if (want_address && !have_address)
18042 expansion_failed (loc, NULL_RTX,
18043 "Want address and only have value");
18044 return 0;
18047 gcc_assert (!ret || !list_ret);
18049 /* If we've got an address and don't want one, dereference. */
18050 if (!want_address && have_address)
18052 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18054 if (size > DWARF2_ADDR_SIZE || size == -1)
18056 expansion_failed (loc, NULL_RTX,
18057 "DWARF address size mismatch");
18058 return 0;
18060 else if (size == DWARF2_ADDR_SIZE)
18061 op = DW_OP_deref;
18062 else
18063 op = DW_OP_deref_size;
18065 if (ret)
18066 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18067 else
18068 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18070 if (ret)
18071 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18073 return list_ret;
18076 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18077 expressions. */
18079 static dw_loc_list_ref
18080 loc_list_from_tree (tree loc, int want_address,
18081 struct loc_descr_context *context)
18083 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18085 for (dw_loc_list_ref loc_cur = result;
18086 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18087 loc_descr_without_nops (loc_cur->expr);
18088 return result;
18091 /* Same as above but return only single location expression. */
18092 static dw_loc_descr_ref
18093 loc_descriptor_from_tree (tree loc, int want_address,
18094 struct loc_descr_context *context)
18096 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18097 if (!ret)
18098 return NULL;
18099 if (ret->dw_loc_next)
18101 expansion_failed (loc, NULL_RTX,
18102 "Location list where only loc descriptor needed");
18103 return NULL;
18105 return ret->expr;
18108 /* Given a value, round it up to the lowest multiple of `boundary'
18109 which is not less than the value itself. */
18111 static inline HOST_WIDE_INT
18112 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18114 return (((value + boundary - 1) / boundary) * boundary);
18117 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18118 pointer to the declared type for the relevant field variable, or return
18119 `integer_type_node' if the given node turns out to be an
18120 ERROR_MARK node. */
18122 static inline tree
18123 field_type (const_tree decl)
18125 tree type;
18127 if (TREE_CODE (decl) == ERROR_MARK)
18128 return integer_type_node;
18130 type = DECL_BIT_FIELD_TYPE (decl);
18131 if (type == NULL_TREE)
18132 type = TREE_TYPE (decl);
18134 return type;
18137 /* Given a pointer to a tree node, return the alignment in bits for
18138 it, or else return BITS_PER_WORD if the node actually turns out to
18139 be an ERROR_MARK node. */
18141 static inline unsigned
18142 simple_type_align_in_bits (const_tree type)
18144 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18147 static inline unsigned
18148 simple_decl_align_in_bits (const_tree decl)
18150 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18153 /* Return the result of rounding T up to ALIGN. */
18155 static inline offset_int
18156 round_up_to_align (const offset_int &t, unsigned int align)
18158 return wi::udiv_trunc (t + align - 1, align) * align;
18161 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18162 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18163 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18164 if we fail to return the size in one of these two forms. */
18166 static dw_loc_descr_ref
18167 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18169 tree tree_size;
18170 struct loc_descr_context ctx;
18172 /* Return a constant integer in priority, if possible. */
18173 *cst_size = int_size_in_bytes (type);
18174 if (*cst_size != -1)
18175 return NULL;
18177 ctx.context_type = const_cast<tree> (type);
18178 ctx.base_decl = NULL_TREE;
18179 ctx.dpi = NULL;
18180 ctx.placeholder_arg = false;
18181 ctx.placeholder_seen = false;
18183 type = TYPE_MAIN_VARIANT (type);
18184 tree_size = TYPE_SIZE_UNIT (type);
18185 return ((tree_size != NULL_TREE)
18186 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18187 : NULL);
18190 /* Helper structure for RECORD_TYPE processing. */
18191 struct vlr_context
18193 /* Root RECORD_TYPE. It is needed to generate data member location
18194 descriptions in variable-length records (VLR), but also to cope with
18195 variants, which are composed of nested structures multiplexed with
18196 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18197 function processing a FIELD_DECL, it is required to be non null. */
18198 tree struct_type;
18199 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18200 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18201 this variant part as part of the root record (in storage units). For
18202 regular records, it must be NULL_TREE. */
18203 tree variant_part_offset;
18206 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18207 addressed byte of the "containing object" for the given FIELD_DECL. If
18208 possible, return a native constant through CST_OFFSET (in which case NULL is
18209 returned); otherwise return a DWARF expression that computes the offset.
18211 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18212 that offset is, either because the argument turns out to be a pointer to an
18213 ERROR_MARK node, or because the offset expression is too complex for us.
18215 CTX is required: see the comment for VLR_CONTEXT. */
18217 static dw_loc_descr_ref
18218 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18219 HOST_WIDE_INT *cst_offset)
18221 tree tree_result;
18222 dw_loc_list_ref loc_result;
18224 *cst_offset = 0;
18226 if (TREE_CODE (decl) == ERROR_MARK)
18227 return NULL;
18228 else
18229 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18231 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18232 case. */
18233 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18234 return NULL;
18236 #ifdef PCC_BITFIELD_TYPE_MATTERS
18237 /* We used to handle only constant offsets in all cases. Now, we handle
18238 properly dynamic byte offsets only when PCC bitfield type doesn't
18239 matter. */
18240 if (PCC_BITFIELD_TYPE_MATTERS
18241 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18243 offset_int object_offset_in_bits;
18244 offset_int object_offset_in_bytes;
18245 offset_int bitpos_int;
18246 tree type;
18247 tree field_size_tree;
18248 offset_int deepest_bitpos;
18249 offset_int field_size_in_bits;
18250 unsigned int type_align_in_bits;
18251 unsigned int decl_align_in_bits;
18252 offset_int type_size_in_bits;
18254 bitpos_int = wi::to_offset (bit_position (decl));
18255 type = field_type (decl);
18256 type_size_in_bits = offset_int_type_size_in_bits (type);
18257 type_align_in_bits = simple_type_align_in_bits (type);
18259 field_size_tree = DECL_SIZE (decl);
18261 /* The size could be unspecified if there was an error, or for
18262 a flexible array member. */
18263 if (!field_size_tree)
18264 field_size_tree = bitsize_zero_node;
18266 /* If the size of the field is not constant, use the type size. */
18267 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18268 field_size_in_bits = wi::to_offset (field_size_tree);
18269 else
18270 field_size_in_bits = type_size_in_bits;
18272 decl_align_in_bits = simple_decl_align_in_bits (decl);
18274 /* The GCC front-end doesn't make any attempt to keep track of the
18275 starting bit offset (relative to the start of the containing
18276 structure type) of the hypothetical "containing object" for a
18277 bit-field. Thus, when computing the byte offset value for the
18278 start of the "containing object" of a bit-field, we must deduce
18279 this information on our own. This can be rather tricky to do in
18280 some cases. For example, handling the following structure type
18281 definition when compiling for an i386/i486 target (which only
18282 aligns long long's to 32-bit boundaries) can be very tricky:
18284 struct S { int field1; long long field2:31; };
18286 Fortunately, there is a simple rule-of-thumb which can be used
18287 in such cases. When compiling for an i386/i486, GCC will
18288 allocate 8 bytes for the structure shown above. It decides to
18289 do this based upon one simple rule for bit-field allocation.
18290 GCC allocates each "containing object" for each bit-field at
18291 the first (i.e. lowest addressed) legitimate alignment boundary
18292 (based upon the required minimum alignment for the declared
18293 type of the field) which it can possibly use, subject to the
18294 condition that there is still enough available space remaining
18295 in the containing object (when allocated at the selected point)
18296 to fully accommodate all of the bits of the bit-field itself.
18298 This simple rule makes it obvious why GCC allocates 8 bytes for
18299 each object of the structure type shown above. When looking
18300 for a place to allocate the "containing object" for `field2',
18301 the compiler simply tries to allocate a 64-bit "containing
18302 object" at each successive 32-bit boundary (starting at zero)
18303 until it finds a place to allocate that 64- bit field such that
18304 at least 31 contiguous (and previously unallocated) bits remain
18305 within that selected 64 bit field. (As it turns out, for the
18306 example above, the compiler finds it is OK to allocate the
18307 "containing object" 64-bit field at bit-offset zero within the
18308 structure type.)
18310 Here we attempt to work backwards from the limited set of facts
18311 we're given, and we try to deduce from those facts, where GCC
18312 must have believed that the containing object started (within
18313 the structure type). The value we deduce is then used (by the
18314 callers of this routine) to generate DW_AT_location and
18315 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18316 the case of DW_AT_location, regular fields as well). */
18318 /* Figure out the bit-distance from the start of the structure to
18319 the "deepest" bit of the bit-field. */
18320 deepest_bitpos = bitpos_int + field_size_in_bits;
18322 /* This is the tricky part. Use some fancy footwork to deduce
18323 where the lowest addressed bit of the containing object must
18324 be. */
18325 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18327 /* Round up to type_align by default. This works best for
18328 bitfields. */
18329 object_offset_in_bits
18330 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18332 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18334 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18336 /* Round up to decl_align instead. */
18337 object_offset_in_bits
18338 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18341 object_offset_in_bytes
18342 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18343 if (ctx->variant_part_offset == NULL_TREE)
18345 *cst_offset = object_offset_in_bytes.to_shwi ();
18346 return NULL;
18348 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18350 else
18351 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18352 tree_result = byte_position (decl);
18354 if (ctx->variant_part_offset != NULL_TREE)
18355 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18356 ctx->variant_part_offset, tree_result);
18358 /* If the byte offset is a constant, it's simplier to handle a native
18359 constant rather than a DWARF expression. */
18360 if (TREE_CODE (tree_result) == INTEGER_CST)
18362 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18363 return NULL;
18365 struct loc_descr_context loc_ctx = {
18366 ctx->struct_type, /* context_type */
18367 NULL_TREE, /* base_decl */
18368 NULL, /* dpi */
18369 false, /* placeholder_arg */
18370 false /* placeholder_seen */
18372 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18374 /* We want a DWARF expression: abort if we only have a location list with
18375 multiple elements. */
18376 if (!loc_result || !single_element_loc_list_p (loc_result))
18377 return NULL;
18378 else
18379 return loc_result->expr;
18382 /* The following routines define various Dwarf attributes and any data
18383 associated with them. */
18385 /* Add a location description attribute value to a DIE.
18387 This emits location attributes suitable for whole variables and
18388 whole parameters. Note that the location attributes for struct fields are
18389 generated by the routine `data_member_location_attribute' below. */
18391 static inline void
18392 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18393 dw_loc_list_ref descr)
18395 if (descr == 0)
18396 return;
18397 if (single_element_loc_list_p (descr))
18398 add_AT_loc (die, attr_kind, descr->expr);
18399 else
18400 add_AT_loc_list (die, attr_kind, descr);
18403 /* Add DW_AT_accessibility attribute to DIE if needed. */
18405 static void
18406 add_accessibility_attribute (dw_die_ref die, tree decl)
18408 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18409 children, otherwise the default is DW_ACCESS_public. In DWARF2
18410 the default has always been DW_ACCESS_public. */
18411 if (TREE_PROTECTED (decl))
18412 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18413 else if (TREE_PRIVATE (decl))
18415 if (dwarf_version == 2
18416 || die->die_parent == NULL
18417 || die->die_parent->die_tag != DW_TAG_class_type)
18418 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18420 else if (dwarf_version > 2
18421 && die->die_parent
18422 && die->die_parent->die_tag == DW_TAG_class_type)
18423 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18426 /* Attach the specialized form of location attribute used for data members of
18427 struct and union types. In the special case of a FIELD_DECL node which
18428 represents a bit-field, the "offset" part of this special location
18429 descriptor must indicate the distance in bytes from the lowest-addressed
18430 byte of the containing struct or union type to the lowest-addressed byte of
18431 the "containing object" for the bit-field. (See the `field_byte_offset'
18432 function above).
18434 For any given bit-field, the "containing object" is a hypothetical object
18435 (of some integral or enum type) within which the given bit-field lives. The
18436 type of this hypothetical "containing object" is always the same as the
18437 declared type of the individual bit-field itself (for GCC anyway... the
18438 DWARF spec doesn't actually mandate this). Note that it is the size (in
18439 bytes) of the hypothetical "containing object" which will be given in the
18440 DW_AT_byte_size attribute for this bit-field. (See the
18441 `byte_size_attribute' function below.) It is also used when calculating the
18442 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18443 function below.)
18445 CTX is required: see the comment for VLR_CONTEXT. */
18447 static void
18448 add_data_member_location_attribute (dw_die_ref die,
18449 tree decl,
18450 struct vlr_context *ctx)
18452 HOST_WIDE_INT offset;
18453 dw_loc_descr_ref loc_descr = 0;
18455 if (TREE_CODE (decl) == TREE_BINFO)
18457 /* We're working on the TAG_inheritance for a base class. */
18458 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18460 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18461 aren't at a fixed offset from all (sub)objects of the same
18462 type. We need to extract the appropriate offset from our
18463 vtable. The following dwarf expression means
18465 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18467 This is specific to the V3 ABI, of course. */
18469 dw_loc_descr_ref tmp;
18471 /* Make a copy of the object address. */
18472 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18473 add_loc_descr (&loc_descr, tmp);
18475 /* Extract the vtable address. */
18476 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18477 add_loc_descr (&loc_descr, tmp);
18479 /* Calculate the address of the offset. */
18480 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18481 gcc_assert (offset < 0);
18483 tmp = int_loc_descriptor (-offset);
18484 add_loc_descr (&loc_descr, tmp);
18485 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18486 add_loc_descr (&loc_descr, tmp);
18488 /* Extract the offset. */
18489 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18490 add_loc_descr (&loc_descr, tmp);
18492 /* Add it to the object address. */
18493 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18494 add_loc_descr (&loc_descr, tmp);
18496 else
18497 offset = tree_to_shwi (BINFO_OFFSET (decl));
18499 else
18501 loc_descr = field_byte_offset (decl, ctx, &offset);
18503 /* If loc_descr is available then we know the field offset is dynamic.
18504 However, GDB does not handle dynamic field offsets very well at the
18505 moment. */
18506 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18508 loc_descr = NULL;
18509 offset = 0;
18512 /* Data member location evalutation starts with the base address on the
18513 stack. Compute the field offset and add it to this base address. */
18514 else if (loc_descr != NULL)
18515 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18518 if (! loc_descr)
18520 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18521 e.g. GDB only added support to it in November 2016. For DWARF5
18522 we need newer debug info consumers anyway. We might change this
18523 to dwarf_version >= 4 once most consumers catched up. */
18524 if (dwarf_version >= 5
18525 && TREE_CODE (decl) == FIELD_DECL
18526 && DECL_BIT_FIELD_TYPE (decl))
18528 tree off = bit_position (decl);
18529 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18531 remove_AT (die, DW_AT_byte_size);
18532 remove_AT (die, DW_AT_bit_offset);
18533 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18534 return;
18537 if (dwarf_version > 2)
18539 /* Don't need to output a location expression, just the constant. */
18540 if (offset < 0)
18541 add_AT_int (die, DW_AT_data_member_location, offset);
18542 else
18543 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18544 return;
18546 else
18548 enum dwarf_location_atom op;
18550 /* The DWARF2 standard says that we should assume that the structure
18551 address is already on the stack, so we can specify a structure
18552 field address by using DW_OP_plus_uconst. */
18553 op = DW_OP_plus_uconst;
18554 loc_descr = new_loc_descr (op, offset, 0);
18558 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18561 /* Writes integer values to dw_vec_const array. */
18563 static void
18564 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18566 while (size != 0)
18568 *dest++ = val & 0xff;
18569 val >>= 8;
18570 --size;
18574 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18576 static HOST_WIDE_INT
18577 extract_int (const unsigned char *src, unsigned int size)
18579 HOST_WIDE_INT val = 0;
18581 src += size;
18582 while (size != 0)
18584 val <<= 8;
18585 val |= *--src & 0xff;
18586 --size;
18588 return val;
18591 /* Writes wide_int values to dw_vec_const array. */
18593 static void
18594 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18596 int i;
18598 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18600 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18601 return;
18604 /* We'd have to extend this code to support odd sizes. */
18605 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18607 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18609 if (WORDS_BIG_ENDIAN)
18610 for (i = n - 1; i >= 0; i--)
18612 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18613 dest += sizeof (HOST_WIDE_INT);
18615 else
18616 for (i = 0; i < n; i++)
18618 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18619 dest += sizeof (HOST_WIDE_INT);
18623 /* Writes floating point values to dw_vec_const array. */
18625 static void
18626 insert_float (const_rtx rtl, unsigned char *array)
18628 long val[4];
18629 int i;
18630 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18632 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18634 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18635 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18637 insert_int (val[i], 4, array);
18638 array += 4;
18642 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18643 does not have a "location" either in memory or in a register. These
18644 things can arise in GNU C when a constant is passed as an actual parameter
18645 to an inlined function. They can also arise in C++ where declared
18646 constants do not necessarily get memory "homes". */
18648 static bool
18649 add_const_value_attribute (dw_die_ref die, rtx rtl)
18651 switch (GET_CODE (rtl))
18653 case CONST_INT:
18655 HOST_WIDE_INT val = INTVAL (rtl);
18657 if (val < 0)
18658 add_AT_int (die, DW_AT_const_value, val);
18659 else
18660 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18662 return true;
18664 case CONST_WIDE_INT:
18666 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18667 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18668 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18669 wide_int w = wi::zext (w1, prec);
18670 add_AT_wide (die, DW_AT_const_value, w);
18672 return true;
18674 case CONST_DOUBLE:
18675 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18676 floating-point constant. A CONST_DOUBLE is used whenever the
18677 constant requires more than one word in order to be adequately
18678 represented. */
18679 if (TARGET_SUPPORTS_WIDE_INT == 0
18680 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18681 add_AT_double (die, DW_AT_const_value,
18682 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18683 else
18685 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18686 unsigned int length = GET_MODE_SIZE (mode);
18687 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18689 insert_float (rtl, array);
18690 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18692 return true;
18694 case CONST_VECTOR:
18696 machine_mode mode = GET_MODE (rtl);
18697 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18698 unsigned int length = CONST_VECTOR_NUNITS (rtl);
18699 unsigned char *array
18700 = ggc_vec_alloc<unsigned char> (length * elt_size);
18701 unsigned int i;
18702 unsigned char *p;
18703 machine_mode imode = GET_MODE_INNER (mode);
18705 switch (GET_MODE_CLASS (mode))
18707 case MODE_VECTOR_INT:
18708 for (i = 0, p = array; i < length; i++, p += elt_size)
18710 rtx elt = CONST_VECTOR_ELT (rtl, i);
18711 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18713 break;
18715 case MODE_VECTOR_FLOAT:
18716 for (i = 0, p = array; i < length; i++, p += elt_size)
18718 rtx elt = CONST_VECTOR_ELT (rtl, i);
18719 insert_float (elt, p);
18721 break;
18723 default:
18724 gcc_unreachable ();
18727 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18729 return true;
18731 case CONST_STRING:
18732 if (dwarf_version >= 4 || !dwarf_strict)
18734 dw_loc_descr_ref loc_result;
18735 resolve_one_addr (&rtl);
18736 rtl_addr:
18737 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18738 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18739 add_AT_loc (die, DW_AT_location, loc_result);
18740 vec_safe_push (used_rtx_array, rtl);
18741 return true;
18743 return false;
18745 case CONST:
18746 if (CONSTANT_P (XEXP (rtl, 0)))
18747 return add_const_value_attribute (die, XEXP (rtl, 0));
18748 /* FALLTHROUGH */
18749 case SYMBOL_REF:
18750 if (!const_ok_for_output (rtl))
18751 return false;
18752 /* FALLTHROUGH */
18753 case LABEL_REF:
18754 if (dwarf_version >= 4 || !dwarf_strict)
18755 goto rtl_addr;
18756 return false;
18758 case PLUS:
18759 /* In cases where an inlined instance of an inline function is passed
18760 the address of an `auto' variable (which is local to the caller) we
18761 can get a situation where the DECL_RTL of the artificial local
18762 variable (for the inlining) which acts as a stand-in for the
18763 corresponding formal parameter (of the inline function) will look
18764 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18765 exactly a compile-time constant expression, but it isn't the address
18766 of the (artificial) local variable either. Rather, it represents the
18767 *value* which the artificial local variable always has during its
18768 lifetime. We currently have no way to represent such quasi-constant
18769 values in Dwarf, so for now we just punt and generate nothing. */
18770 return false;
18772 case HIGH:
18773 case CONST_FIXED:
18774 return false;
18776 case MEM:
18777 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18778 && MEM_READONLY_P (rtl)
18779 && GET_MODE (rtl) == BLKmode)
18781 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18782 return true;
18784 return false;
18786 default:
18787 /* No other kinds of rtx should be possible here. */
18788 gcc_unreachable ();
18790 return false;
18793 /* Determine whether the evaluation of EXPR references any variables
18794 or functions which aren't otherwise used (and therefore may not be
18795 output). */
18796 static tree
18797 reference_to_unused (tree * tp, int * walk_subtrees,
18798 void * data ATTRIBUTE_UNUSED)
18800 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18801 *walk_subtrees = 0;
18803 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18804 && ! TREE_ASM_WRITTEN (*tp))
18805 return *tp;
18806 /* ??? The C++ FE emits debug information for using decls, so
18807 putting gcc_unreachable here falls over. See PR31899. For now
18808 be conservative. */
18809 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18810 return *tp;
18811 else if (VAR_P (*tp))
18813 varpool_node *node = varpool_node::get (*tp);
18814 if (!node || !node->definition)
18815 return *tp;
18817 else if (TREE_CODE (*tp) == FUNCTION_DECL
18818 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18820 /* The call graph machinery must have finished analyzing,
18821 optimizing and gimplifying the CU by now.
18822 So if *TP has no call graph node associated
18823 to it, it means *TP will not be emitted. */
18824 if (!cgraph_node::get (*tp))
18825 return *tp;
18827 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18828 return *tp;
18830 return NULL_TREE;
18833 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18834 for use in a later add_const_value_attribute call. */
18836 static rtx
18837 rtl_for_decl_init (tree init, tree type)
18839 rtx rtl = NULL_RTX;
18841 STRIP_NOPS (init);
18843 /* If a variable is initialized with a string constant without embedded
18844 zeros, build CONST_STRING. */
18845 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
18847 tree enttype = TREE_TYPE (type);
18848 tree domain = TYPE_DOMAIN (type);
18849 scalar_int_mode mode;
18851 if (is_int_mode (TYPE_MODE (enttype), &mode)
18852 && GET_MODE_SIZE (mode) == 1
18853 && domain
18854 && integer_zerop (TYPE_MIN_VALUE (domain))
18855 && compare_tree_int (TYPE_MAX_VALUE (domain),
18856 TREE_STRING_LENGTH (init) - 1) == 0
18857 && ((size_t) TREE_STRING_LENGTH (init)
18858 == strlen (TREE_STRING_POINTER (init)) + 1))
18860 rtl = gen_rtx_CONST_STRING (VOIDmode,
18861 ggc_strdup (TREE_STRING_POINTER (init)));
18862 rtl = gen_rtx_MEM (BLKmode, rtl);
18863 MEM_READONLY_P (rtl) = 1;
18866 /* Other aggregates, and complex values, could be represented using
18867 CONCAT: FIXME! */
18868 else if (AGGREGATE_TYPE_P (type)
18869 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
18870 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
18871 || TREE_CODE (type) == COMPLEX_TYPE)
18873 /* Vectors only work if their mode is supported by the target.
18874 FIXME: generic vectors ought to work too. */
18875 else if (TREE_CODE (type) == VECTOR_TYPE
18876 && !VECTOR_MODE_P (TYPE_MODE (type)))
18878 /* If the initializer is something that we know will expand into an
18879 immediate RTL constant, expand it now. We must be careful not to
18880 reference variables which won't be output. */
18881 else if (initializer_constant_valid_p (init, type)
18882 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
18884 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
18885 possible. */
18886 if (TREE_CODE (type) == VECTOR_TYPE)
18887 switch (TREE_CODE (init))
18889 case VECTOR_CST:
18890 break;
18891 case CONSTRUCTOR:
18892 if (TREE_CONSTANT (init))
18894 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
18895 bool constant_p = true;
18896 tree value;
18897 unsigned HOST_WIDE_INT ix;
18899 /* Even when ctor is constant, it might contain non-*_CST
18900 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
18901 belong into VECTOR_CST nodes. */
18902 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
18903 if (!CONSTANT_CLASS_P (value))
18905 constant_p = false;
18906 break;
18909 if (constant_p)
18911 init = build_vector_from_ctor (type, elts);
18912 break;
18915 /* FALLTHRU */
18917 default:
18918 return NULL;
18921 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
18923 /* If expand_expr returns a MEM, it wasn't immediate. */
18924 gcc_assert (!rtl || !MEM_P (rtl));
18927 return rtl;
18930 /* Generate RTL for the variable DECL to represent its location. */
18932 static rtx
18933 rtl_for_decl_location (tree decl)
18935 rtx rtl;
18937 /* Here we have to decide where we are going to say the parameter "lives"
18938 (as far as the debugger is concerned). We only have a couple of
18939 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
18941 DECL_RTL normally indicates where the parameter lives during most of the
18942 activation of the function. If optimization is enabled however, this
18943 could be either NULL or else a pseudo-reg. Both of those cases indicate
18944 that the parameter doesn't really live anywhere (as far as the code
18945 generation parts of GCC are concerned) during most of the function's
18946 activation. That will happen (for example) if the parameter is never
18947 referenced within the function.
18949 We could just generate a location descriptor here for all non-NULL
18950 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
18951 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
18952 where DECL_RTL is NULL or is a pseudo-reg.
18954 Note however that we can only get away with using DECL_INCOMING_RTL as
18955 a backup substitute for DECL_RTL in certain limited cases. In cases
18956 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
18957 we can be sure that the parameter was passed using the same type as it is
18958 declared to have within the function, and that its DECL_INCOMING_RTL
18959 points us to a place where a value of that type is passed.
18961 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
18962 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
18963 because in these cases DECL_INCOMING_RTL points us to a value of some
18964 type which is *different* from the type of the parameter itself. Thus,
18965 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
18966 such cases, the debugger would end up (for example) trying to fetch a
18967 `float' from a place which actually contains the first part of a
18968 `double'. That would lead to really incorrect and confusing
18969 output at debug-time.
18971 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
18972 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
18973 are a couple of exceptions however. On little-endian machines we can
18974 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
18975 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
18976 an integral type that is smaller than TREE_TYPE (decl). These cases arise
18977 when (on a little-endian machine) a non-prototyped function has a
18978 parameter declared to be of type `short' or `char'. In such cases,
18979 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
18980 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
18981 passed `int' value. If the debugger then uses that address to fetch
18982 a `short' or a `char' (on a little-endian machine) the result will be
18983 the correct data, so we allow for such exceptional cases below.
18985 Note that our goal here is to describe the place where the given formal
18986 parameter lives during most of the function's activation (i.e. between the
18987 end of the prologue and the start of the epilogue). We'll do that as best
18988 as we can. Note however that if the given formal parameter is modified
18989 sometime during the execution of the function, then a stack backtrace (at
18990 debug-time) will show the function as having been called with the *new*
18991 value rather than the value which was originally passed in. This happens
18992 rarely enough that it is not a major problem, but it *is* a problem, and
18993 I'd like to fix it.
18995 A future version of dwarf2out.c may generate two additional attributes for
18996 any given DW_TAG_formal_parameter DIE which will describe the "passed
18997 type" and the "passed location" for the given formal parameter in addition
18998 to the attributes we now generate to indicate the "declared type" and the
18999 "active location" for each parameter. This additional set of attributes
19000 could be used by debuggers for stack backtraces. Separately, note that
19001 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19002 This happens (for example) for inlined-instances of inline function formal
19003 parameters which are never referenced. This really shouldn't be
19004 happening. All PARM_DECL nodes should get valid non-NULL
19005 DECL_INCOMING_RTL values. FIXME. */
19007 /* Use DECL_RTL as the "location" unless we find something better. */
19008 rtl = DECL_RTL_IF_SET (decl);
19010 /* When generating abstract instances, ignore everything except
19011 constants, symbols living in memory, and symbols living in
19012 fixed registers. */
19013 if (! reload_completed)
19015 if (rtl
19016 && (CONSTANT_P (rtl)
19017 || (MEM_P (rtl)
19018 && CONSTANT_P (XEXP (rtl, 0)))
19019 || (REG_P (rtl)
19020 && VAR_P (decl)
19021 && TREE_STATIC (decl))))
19023 rtl = targetm.delegitimize_address (rtl);
19024 return rtl;
19026 rtl = NULL_RTX;
19028 else if (TREE_CODE (decl) == PARM_DECL)
19030 if (rtl == NULL_RTX
19031 || is_pseudo_reg (rtl)
19032 || (MEM_P (rtl)
19033 && is_pseudo_reg (XEXP (rtl, 0))
19034 && DECL_INCOMING_RTL (decl)
19035 && MEM_P (DECL_INCOMING_RTL (decl))
19036 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19038 tree declared_type = TREE_TYPE (decl);
19039 tree passed_type = DECL_ARG_TYPE (decl);
19040 machine_mode dmode = TYPE_MODE (declared_type);
19041 machine_mode pmode = TYPE_MODE (passed_type);
19043 /* This decl represents a formal parameter which was optimized out.
19044 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19045 all cases where (rtl == NULL_RTX) just below. */
19046 if (dmode == pmode)
19047 rtl = DECL_INCOMING_RTL (decl);
19048 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19049 && SCALAR_INT_MODE_P (dmode)
19050 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
19051 && DECL_INCOMING_RTL (decl))
19053 rtx inc = DECL_INCOMING_RTL (decl);
19054 if (REG_P (inc))
19055 rtl = inc;
19056 else if (MEM_P (inc))
19058 if (BYTES_BIG_ENDIAN)
19059 rtl = adjust_address_nv (inc, dmode,
19060 GET_MODE_SIZE (pmode)
19061 - GET_MODE_SIZE (dmode));
19062 else
19063 rtl = inc;
19068 /* If the parm was passed in registers, but lives on the stack, then
19069 make a big endian correction if the mode of the type of the
19070 parameter is not the same as the mode of the rtl. */
19071 /* ??? This is the same series of checks that are made in dbxout.c before
19072 we reach the big endian correction code there. It isn't clear if all
19073 of these checks are necessary here, but keeping them all is the safe
19074 thing to do. */
19075 else if (MEM_P (rtl)
19076 && XEXP (rtl, 0) != const0_rtx
19077 && ! CONSTANT_P (XEXP (rtl, 0))
19078 /* Not passed in memory. */
19079 && !MEM_P (DECL_INCOMING_RTL (decl))
19080 /* Not passed by invisible reference. */
19081 && (!REG_P (XEXP (rtl, 0))
19082 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19083 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19084 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19085 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19086 #endif
19088 /* Big endian correction check. */
19089 && BYTES_BIG_ENDIAN
19090 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19091 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
19092 < UNITS_PER_WORD))
19094 machine_mode addr_mode = get_address_mode (rtl);
19095 int offset = (UNITS_PER_WORD
19096 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19098 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19099 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19102 else if (VAR_P (decl)
19103 && rtl
19104 && MEM_P (rtl)
19105 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
19106 && BYTES_BIG_ENDIAN)
19108 machine_mode addr_mode = get_address_mode (rtl);
19109 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
19110 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
19112 /* If a variable is declared "register" yet is smaller than
19113 a register, then if we store the variable to memory, it
19114 looks like we're storing a register-sized value, when in
19115 fact we are not. We need to adjust the offset of the
19116 storage location to reflect the actual value's bytes,
19117 else gdb will not be able to display it. */
19118 if (rsize > dsize)
19119 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19120 plus_constant (addr_mode, XEXP (rtl, 0),
19121 rsize - dsize));
19124 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19125 and will have been substituted directly into all expressions that use it.
19126 C does not have such a concept, but C++ and other languages do. */
19127 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19128 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19130 if (rtl)
19131 rtl = targetm.delegitimize_address (rtl);
19133 /* If we don't look past the constant pool, we risk emitting a
19134 reference to a constant pool entry that isn't referenced from
19135 code, and thus is not emitted. */
19136 if (rtl)
19137 rtl = avoid_constant_pool_reference (rtl);
19139 /* Try harder to get a rtl. If this symbol ends up not being emitted
19140 in the current CU, resolve_addr will remove the expression referencing
19141 it. */
19142 if (rtl == NULL_RTX
19143 && VAR_P (decl)
19144 && !DECL_EXTERNAL (decl)
19145 && TREE_STATIC (decl)
19146 && DECL_NAME (decl)
19147 && !DECL_HARD_REGISTER (decl)
19148 && DECL_MODE (decl) != VOIDmode)
19150 rtl = make_decl_rtl_for_debug (decl);
19151 if (!MEM_P (rtl)
19152 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19153 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19154 rtl = NULL_RTX;
19157 return rtl;
19160 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19161 returned. If so, the decl for the COMMON block is returned, and the
19162 value is the offset into the common block for the symbol. */
19164 static tree
19165 fortran_common (tree decl, HOST_WIDE_INT *value)
19167 tree val_expr, cvar;
19168 machine_mode mode;
19169 HOST_WIDE_INT bitsize, bitpos;
19170 tree offset;
19171 int unsignedp, reversep, volatilep = 0;
19173 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19174 it does not have a value (the offset into the common area), or if it
19175 is thread local (as opposed to global) then it isn't common, and shouldn't
19176 be handled as such. */
19177 if (!VAR_P (decl)
19178 || !TREE_STATIC (decl)
19179 || !DECL_HAS_VALUE_EXPR_P (decl)
19180 || !is_fortran ())
19181 return NULL_TREE;
19183 val_expr = DECL_VALUE_EXPR (decl);
19184 if (TREE_CODE (val_expr) != COMPONENT_REF)
19185 return NULL_TREE;
19187 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19188 &unsignedp, &reversep, &volatilep);
19190 if (cvar == NULL_TREE
19191 || !VAR_P (cvar)
19192 || DECL_ARTIFICIAL (cvar)
19193 || !TREE_PUBLIC (cvar))
19194 return NULL_TREE;
19196 *value = 0;
19197 if (offset != NULL)
19199 if (!tree_fits_shwi_p (offset))
19200 return NULL_TREE;
19201 *value = tree_to_shwi (offset);
19203 if (bitpos != 0)
19204 *value += bitpos / BITS_PER_UNIT;
19206 return cvar;
19209 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19210 data attribute for a variable or a parameter. We generate the
19211 DW_AT_const_value attribute only in those cases where the given variable
19212 or parameter does not have a true "location" either in memory or in a
19213 register. This can happen (for example) when a constant is passed as an
19214 actual argument in a call to an inline function. (It's possible that
19215 these things can crop up in other ways also.) Note that one type of
19216 constant value which can be passed into an inlined function is a constant
19217 pointer. This can happen for example if an actual argument in an inlined
19218 function call evaluates to a compile-time constant address.
19220 CACHE_P is true if it is worth caching the location list for DECL,
19221 so that future calls can reuse it rather than regenerate it from scratch.
19222 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19223 since we will need to refer to them each time the function is inlined. */
19225 static bool
19226 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19228 rtx rtl;
19229 dw_loc_list_ref list;
19230 var_loc_list *loc_list;
19231 cached_dw_loc_list *cache;
19233 if (early_dwarf)
19234 return false;
19236 if (TREE_CODE (decl) == ERROR_MARK)
19237 return false;
19239 if (get_AT (die, DW_AT_location)
19240 || get_AT (die, DW_AT_const_value))
19241 return true;
19243 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19244 || TREE_CODE (decl) == RESULT_DECL);
19246 /* Try to get some constant RTL for this decl, and use that as the value of
19247 the location. */
19249 rtl = rtl_for_decl_location (decl);
19250 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19251 && add_const_value_attribute (die, rtl))
19252 return true;
19254 /* See if we have single element location list that is equivalent to
19255 a constant value. That way we are better to use add_const_value_attribute
19256 rather than expanding constant value equivalent. */
19257 loc_list = lookup_decl_loc (decl);
19258 if (loc_list
19259 && loc_list->first
19260 && loc_list->first->next == NULL
19261 && NOTE_P (loc_list->first->loc)
19262 && NOTE_VAR_LOCATION (loc_list->first->loc)
19263 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19265 struct var_loc_node *node;
19267 node = loc_list->first;
19268 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19269 if (GET_CODE (rtl) == EXPR_LIST)
19270 rtl = XEXP (rtl, 0);
19271 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19272 && add_const_value_attribute (die, rtl))
19273 return true;
19275 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19276 list several times. See if we've already cached the contents. */
19277 list = NULL;
19278 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19279 cache_p = false;
19280 if (cache_p)
19282 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19283 if (cache)
19284 list = cache->loc_list;
19286 if (list == NULL)
19288 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19289 NULL);
19290 /* It is usually worth caching this result if the decl is from
19291 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19292 if (cache_p && list && list->dw_loc_next)
19294 cached_dw_loc_list **slot
19295 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19296 DECL_UID (decl),
19297 INSERT);
19298 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19299 cache->decl_id = DECL_UID (decl);
19300 cache->loc_list = list;
19301 *slot = cache;
19304 if (list)
19306 add_AT_location_description (die, DW_AT_location, list);
19307 return true;
19309 /* None of that worked, so it must not really have a location;
19310 try adding a constant value attribute from the DECL_INITIAL. */
19311 return tree_add_const_value_attribute_for_decl (die, decl);
19314 /* Helper function for tree_add_const_value_attribute. Natively encode
19315 initializer INIT into an array. Return true if successful. */
19317 static bool
19318 native_encode_initializer (tree init, unsigned char *array, int size)
19320 tree type;
19322 if (init == NULL_TREE)
19323 return false;
19325 STRIP_NOPS (init);
19326 switch (TREE_CODE (init))
19328 case STRING_CST:
19329 type = TREE_TYPE (init);
19330 if (TREE_CODE (type) == ARRAY_TYPE)
19332 tree enttype = TREE_TYPE (type);
19333 scalar_int_mode mode;
19335 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19336 || GET_MODE_SIZE (mode) != 1)
19337 return false;
19338 if (int_size_in_bytes (type) != size)
19339 return false;
19340 if (size > TREE_STRING_LENGTH (init))
19342 memcpy (array, TREE_STRING_POINTER (init),
19343 TREE_STRING_LENGTH (init));
19344 memset (array + TREE_STRING_LENGTH (init),
19345 '\0', size - TREE_STRING_LENGTH (init));
19347 else
19348 memcpy (array, TREE_STRING_POINTER (init), size);
19349 return true;
19351 return false;
19352 case CONSTRUCTOR:
19353 type = TREE_TYPE (init);
19354 if (int_size_in_bytes (type) != size)
19355 return false;
19356 if (TREE_CODE (type) == ARRAY_TYPE)
19358 HOST_WIDE_INT min_index;
19359 unsigned HOST_WIDE_INT cnt;
19360 int curpos = 0, fieldsize;
19361 constructor_elt *ce;
19363 if (TYPE_DOMAIN (type) == NULL_TREE
19364 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19365 return false;
19367 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19368 if (fieldsize <= 0)
19369 return false;
19371 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19372 memset (array, '\0', size);
19373 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19375 tree val = ce->value;
19376 tree index = ce->index;
19377 int pos = curpos;
19378 if (index && TREE_CODE (index) == RANGE_EXPR)
19379 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19380 * fieldsize;
19381 else if (index)
19382 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19384 if (val)
19386 STRIP_NOPS (val);
19387 if (!native_encode_initializer (val, array + pos, fieldsize))
19388 return false;
19390 curpos = pos + fieldsize;
19391 if (index && TREE_CODE (index) == RANGE_EXPR)
19393 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19394 - tree_to_shwi (TREE_OPERAND (index, 0));
19395 while (count-- > 0)
19397 if (val)
19398 memcpy (array + curpos, array + pos, fieldsize);
19399 curpos += fieldsize;
19402 gcc_assert (curpos <= size);
19404 return true;
19406 else if (TREE_CODE (type) == RECORD_TYPE
19407 || TREE_CODE (type) == UNION_TYPE)
19409 tree field = NULL_TREE;
19410 unsigned HOST_WIDE_INT cnt;
19411 constructor_elt *ce;
19413 if (int_size_in_bytes (type) != size)
19414 return false;
19416 if (TREE_CODE (type) == RECORD_TYPE)
19417 field = TYPE_FIELDS (type);
19419 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19421 tree val = ce->value;
19422 int pos, fieldsize;
19424 if (ce->index != 0)
19425 field = ce->index;
19427 if (val)
19428 STRIP_NOPS (val);
19430 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19431 return false;
19433 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19434 && TYPE_DOMAIN (TREE_TYPE (field))
19435 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19436 return false;
19437 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19438 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19439 return false;
19440 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19441 pos = int_byte_position (field);
19442 gcc_assert (pos + fieldsize <= size);
19443 if (val && fieldsize != 0
19444 && !native_encode_initializer (val, array + pos, fieldsize))
19445 return false;
19447 return true;
19449 return false;
19450 case VIEW_CONVERT_EXPR:
19451 case NON_LVALUE_EXPR:
19452 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19453 default:
19454 return native_encode_expr (init, array, size) == size;
19458 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19459 attribute is the const value T. */
19461 static bool
19462 tree_add_const_value_attribute (dw_die_ref die, tree t)
19464 tree init;
19465 tree type = TREE_TYPE (t);
19466 rtx rtl;
19468 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19469 return false;
19471 init = t;
19472 gcc_assert (!DECL_P (init));
19474 if (! early_dwarf)
19476 rtl = rtl_for_decl_init (init, type);
19477 if (rtl)
19478 return add_const_value_attribute (die, rtl);
19480 /* If the host and target are sane, try harder. */
19481 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19482 && initializer_constant_valid_p (init, type))
19484 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19485 if (size > 0 && (int) size == size)
19487 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19489 if (native_encode_initializer (init, array, size))
19491 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19492 return true;
19494 ggc_free (array);
19497 return false;
19500 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19501 attribute is the const value of T, where T is an integral constant
19502 variable with static storage duration
19503 (so it can't be a PARM_DECL or a RESULT_DECL). */
19505 static bool
19506 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19509 if (!decl
19510 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19511 || (VAR_P (decl) && !TREE_STATIC (decl)))
19512 return false;
19514 if (TREE_READONLY (decl)
19515 && ! TREE_THIS_VOLATILE (decl)
19516 && DECL_INITIAL (decl))
19517 /* OK */;
19518 else
19519 return false;
19521 /* Don't add DW_AT_const_value if abstract origin already has one. */
19522 if (get_AT (var_die, DW_AT_const_value))
19523 return false;
19525 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19528 /* Convert the CFI instructions for the current function into a
19529 location list. This is used for DW_AT_frame_base when we targeting
19530 a dwarf2 consumer that does not support the dwarf3
19531 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19532 expressions. */
19534 static dw_loc_list_ref
19535 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19537 int ix;
19538 dw_fde_ref fde;
19539 dw_loc_list_ref list, *list_tail;
19540 dw_cfi_ref cfi;
19541 dw_cfa_location last_cfa, next_cfa;
19542 const char *start_label, *last_label, *section;
19543 dw_cfa_location remember;
19545 fde = cfun->fde;
19546 gcc_assert (fde != NULL);
19548 section = secname_for_decl (current_function_decl);
19549 list_tail = &list;
19550 list = NULL;
19552 memset (&next_cfa, 0, sizeof (next_cfa));
19553 next_cfa.reg = INVALID_REGNUM;
19554 remember = next_cfa;
19556 start_label = fde->dw_fde_begin;
19558 /* ??? Bald assumption that the CIE opcode list does not contain
19559 advance opcodes. */
19560 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19561 lookup_cfa_1 (cfi, &next_cfa, &remember);
19563 last_cfa = next_cfa;
19564 last_label = start_label;
19566 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19568 /* If the first partition contained no CFI adjustments, the
19569 CIE opcodes apply to the whole first partition. */
19570 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19571 fde->dw_fde_begin, fde->dw_fde_end, section);
19572 list_tail =&(*list_tail)->dw_loc_next;
19573 start_label = last_label = fde->dw_fde_second_begin;
19576 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19578 switch (cfi->dw_cfi_opc)
19580 case DW_CFA_set_loc:
19581 case DW_CFA_advance_loc1:
19582 case DW_CFA_advance_loc2:
19583 case DW_CFA_advance_loc4:
19584 if (!cfa_equal_p (&last_cfa, &next_cfa))
19586 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19587 start_label, last_label, section);
19589 list_tail = &(*list_tail)->dw_loc_next;
19590 last_cfa = next_cfa;
19591 start_label = last_label;
19593 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19594 break;
19596 case DW_CFA_advance_loc:
19597 /* The encoding is complex enough that we should never emit this. */
19598 gcc_unreachable ();
19600 default:
19601 lookup_cfa_1 (cfi, &next_cfa, &remember);
19602 break;
19604 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19606 if (!cfa_equal_p (&last_cfa, &next_cfa))
19608 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19609 start_label, last_label, section);
19611 list_tail = &(*list_tail)->dw_loc_next;
19612 last_cfa = next_cfa;
19613 start_label = last_label;
19615 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19616 start_label, fde->dw_fde_end, section);
19617 list_tail = &(*list_tail)->dw_loc_next;
19618 start_label = last_label = fde->dw_fde_second_begin;
19622 if (!cfa_equal_p (&last_cfa, &next_cfa))
19624 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19625 start_label, last_label, section);
19626 list_tail = &(*list_tail)->dw_loc_next;
19627 start_label = last_label;
19630 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19631 start_label,
19632 fde->dw_fde_second_begin
19633 ? fde->dw_fde_second_end : fde->dw_fde_end,
19634 section);
19636 if (list && list->dw_loc_next)
19637 gen_llsym (list);
19639 return list;
19642 /* Compute a displacement from the "steady-state frame pointer" to the
19643 frame base (often the same as the CFA), and store it in
19644 frame_pointer_fb_offset. OFFSET is added to the displacement
19645 before the latter is negated. */
19647 static void
19648 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
19650 rtx reg, elim;
19652 #ifdef FRAME_POINTER_CFA_OFFSET
19653 reg = frame_pointer_rtx;
19654 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19655 #else
19656 reg = arg_pointer_rtx;
19657 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19658 #endif
19660 elim = (ira_use_lra_p
19661 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19662 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19663 if (GET_CODE (elim) == PLUS)
19665 offset += INTVAL (XEXP (elim, 1));
19666 elim = XEXP (elim, 0);
19669 frame_pointer_fb_offset = -offset;
19671 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19672 in which to eliminate. This is because it's stack pointer isn't
19673 directly accessible as a register within the ISA. To work around
19674 this, assume that while we cannot provide a proper value for
19675 frame_pointer_fb_offset, we won't need one either. */
19676 frame_pointer_fb_offset_valid
19677 = ((SUPPORTS_STACK_ALIGNMENT
19678 && (elim == hard_frame_pointer_rtx
19679 || elim == stack_pointer_rtx))
19680 || elim == (frame_pointer_needed
19681 ? hard_frame_pointer_rtx
19682 : stack_pointer_rtx));
19685 /* Generate a DW_AT_name attribute given some string value to be included as
19686 the value of the attribute. */
19688 static void
19689 add_name_attribute (dw_die_ref die, const char *name_string)
19691 if (name_string != NULL && *name_string != 0)
19693 if (demangle_name_func)
19694 name_string = (*demangle_name_func) (name_string);
19696 add_AT_string (die, DW_AT_name, name_string);
19700 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19701 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19702 of TYPE accordingly.
19704 ??? This is a temporary measure until after we're able to generate
19705 regular DWARF for the complex Ada type system. */
19707 static void
19708 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19709 dw_die_ref context_die)
19711 tree dtype;
19712 dw_die_ref dtype_die;
19714 if (!lang_hooks.types.descriptive_type)
19715 return;
19717 dtype = lang_hooks.types.descriptive_type (type);
19718 if (!dtype)
19719 return;
19721 dtype_die = lookup_type_die (dtype);
19722 if (!dtype_die)
19724 gen_type_die (dtype, context_die);
19725 dtype_die = lookup_type_die (dtype);
19726 gcc_assert (dtype_die);
19729 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19732 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19734 static const char *
19735 comp_dir_string (void)
19737 const char *wd;
19738 char *wd1;
19739 static const char *cached_wd = NULL;
19741 if (cached_wd != NULL)
19742 return cached_wd;
19744 wd = get_src_pwd ();
19745 if (wd == NULL)
19746 return NULL;
19748 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19750 int wdlen;
19752 wdlen = strlen (wd);
19753 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19754 strcpy (wd1, wd);
19755 wd1 [wdlen] = DIR_SEPARATOR;
19756 wd1 [wdlen + 1] = 0;
19757 wd = wd1;
19760 cached_wd = remap_debug_filename (wd);
19761 return cached_wd;
19764 /* Generate a DW_AT_comp_dir attribute for DIE. */
19766 static void
19767 add_comp_dir_attribute (dw_die_ref die)
19769 const char * wd = comp_dir_string ();
19770 if (wd != NULL)
19771 add_AT_string (die, DW_AT_comp_dir, wd);
19774 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19775 pointer computation, ...), output a representation for that bound according
19776 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19777 loc_list_from_tree for the meaning of CONTEXT. */
19779 static void
19780 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19781 int forms, struct loc_descr_context *context)
19783 dw_die_ref context_die, decl_die;
19784 dw_loc_list_ref list;
19785 bool strip_conversions = true;
19786 bool placeholder_seen = false;
19788 while (strip_conversions)
19789 switch (TREE_CODE (value))
19791 case ERROR_MARK:
19792 case SAVE_EXPR:
19793 return;
19795 CASE_CONVERT:
19796 case VIEW_CONVERT_EXPR:
19797 value = TREE_OPERAND (value, 0);
19798 break;
19800 default:
19801 strip_conversions = false;
19802 break;
19805 /* If possible and permitted, output the attribute as a constant. */
19806 if ((forms & dw_scalar_form_constant) != 0
19807 && TREE_CODE (value) == INTEGER_CST)
19809 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19811 /* If HOST_WIDE_INT is big enough then represent the bound as
19812 a constant value. We need to choose a form based on
19813 whether the type is signed or unsigned. We cannot just
19814 call add_AT_unsigned if the value itself is positive
19815 (add_AT_unsigned might add the unsigned value encoded as
19816 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19817 bounds type and then sign extend any unsigned values found
19818 for signed types. This is needed only for
19819 DW_AT_{lower,upper}_bound, since for most other attributes,
19820 consumers will treat DW_FORM_data[1248] as unsigned values,
19821 regardless of the underlying type. */
19822 if (prec <= HOST_BITS_PER_WIDE_INT
19823 || tree_fits_uhwi_p (value))
19825 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19826 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19827 else
19828 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19830 else
19831 /* Otherwise represent the bound as an unsigned value with
19832 the precision of its type. The precision and signedness
19833 of the type will be necessary to re-interpret it
19834 unambiguously. */
19835 add_AT_wide (die, attr, value);
19836 return;
19839 /* Otherwise, if it's possible and permitted too, output a reference to
19840 another DIE. */
19841 if ((forms & dw_scalar_form_reference) != 0)
19843 tree decl = NULL_TREE;
19845 /* Some type attributes reference an outer type. For instance, the upper
19846 bound of an array may reference an embedding record (this happens in
19847 Ada). */
19848 if (TREE_CODE (value) == COMPONENT_REF
19849 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
19850 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
19851 decl = TREE_OPERAND (value, 1);
19853 else if (VAR_P (value)
19854 || TREE_CODE (value) == PARM_DECL
19855 || TREE_CODE (value) == RESULT_DECL)
19856 decl = value;
19858 if (decl != NULL_TREE)
19860 dw_die_ref decl_die = lookup_decl_die (decl);
19862 /* ??? Can this happen, or should the variable have been bound
19863 first? Probably it can, since I imagine that we try to create
19864 the types of parameters in the order in which they exist in
19865 the list, and won't have created a forward reference to a
19866 later parameter. */
19867 if (decl_die != NULL)
19869 add_AT_die_ref (die, attr, decl_die);
19870 return;
19875 /* Last chance: try to create a stack operation procedure to evaluate the
19876 value. Do nothing if even that is not possible or permitted. */
19877 if ((forms & dw_scalar_form_exprloc) == 0)
19878 return;
19880 list = loc_list_from_tree (value, 2, context);
19881 if (context && context->placeholder_arg)
19883 placeholder_seen = context->placeholder_seen;
19884 context->placeholder_seen = false;
19886 if (list == NULL || single_element_loc_list_p (list))
19888 /* If this attribute is not a reference nor constant, it is
19889 a DWARF expression rather than location description. For that
19890 loc_list_from_tree (value, 0, &context) is needed. */
19891 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
19892 if (list2 && single_element_loc_list_p (list2))
19894 if (placeholder_seen)
19896 struct dwarf_procedure_info dpi;
19897 dpi.fndecl = NULL_TREE;
19898 dpi.args_count = 1;
19899 if (!resolve_args_picking (list2->expr, 1, &dpi))
19900 return;
19902 add_AT_loc (die, attr, list2->expr);
19903 return;
19907 /* If that failed to give a single element location list, fall back to
19908 outputting this as a reference... still if permitted. */
19909 if (list == NULL
19910 || (forms & dw_scalar_form_reference) == 0
19911 || placeholder_seen)
19912 return;
19914 if (current_function_decl == 0)
19915 context_die = comp_unit_die ();
19916 else
19917 context_die = lookup_decl_die (current_function_decl);
19919 decl_die = new_die (DW_TAG_variable, context_die, value);
19920 add_AT_flag (decl_die, DW_AT_artificial, 1);
19921 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
19922 context_die);
19923 add_AT_location_description (decl_die, DW_AT_location, list);
19924 add_AT_die_ref (die, attr, decl_die);
19927 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
19928 default. */
19930 static int
19931 lower_bound_default (void)
19933 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
19935 case DW_LANG_C:
19936 case DW_LANG_C89:
19937 case DW_LANG_C99:
19938 case DW_LANG_C11:
19939 case DW_LANG_C_plus_plus:
19940 case DW_LANG_C_plus_plus_11:
19941 case DW_LANG_C_plus_plus_14:
19942 case DW_LANG_ObjC:
19943 case DW_LANG_ObjC_plus_plus:
19944 return 0;
19945 case DW_LANG_Fortran77:
19946 case DW_LANG_Fortran90:
19947 case DW_LANG_Fortran95:
19948 case DW_LANG_Fortran03:
19949 case DW_LANG_Fortran08:
19950 return 1;
19951 case DW_LANG_UPC:
19952 case DW_LANG_D:
19953 case DW_LANG_Python:
19954 return dwarf_version >= 4 ? 0 : -1;
19955 case DW_LANG_Ada95:
19956 case DW_LANG_Ada83:
19957 case DW_LANG_Cobol74:
19958 case DW_LANG_Cobol85:
19959 case DW_LANG_Modula2:
19960 case DW_LANG_PLI:
19961 return dwarf_version >= 4 ? 1 : -1;
19962 default:
19963 return -1;
19967 /* Given a tree node describing an array bound (either lower or upper) output
19968 a representation for that bound. */
19970 static void
19971 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
19972 tree bound, struct loc_descr_context *context)
19974 int dflt;
19976 while (1)
19977 switch (TREE_CODE (bound))
19979 /* Strip all conversions. */
19980 CASE_CONVERT:
19981 case VIEW_CONVERT_EXPR:
19982 bound = TREE_OPERAND (bound, 0);
19983 break;
19985 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
19986 are even omitted when they are the default. */
19987 case INTEGER_CST:
19988 /* If the value for this bound is the default one, we can even omit the
19989 attribute. */
19990 if (bound_attr == DW_AT_lower_bound
19991 && tree_fits_shwi_p (bound)
19992 && (dflt = lower_bound_default ()) != -1
19993 && tree_to_shwi (bound) == dflt)
19994 return;
19996 /* FALLTHRU */
19998 default:
19999 /* Because of the complex interaction there can be with other GNAT
20000 encodings, GDB isn't ready yet to handle proper DWARF description
20001 for self-referencial subrange bounds: let GNAT encodings do the
20002 magic in such a case. */
20003 if (is_ada ()
20004 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20005 && contains_placeholder_p (bound))
20006 return;
20008 add_scalar_info (subrange_die, bound_attr, bound,
20009 dw_scalar_form_constant
20010 | dw_scalar_form_exprloc
20011 | dw_scalar_form_reference,
20012 context);
20013 return;
20017 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20018 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20019 Note that the block of subscript information for an array type also
20020 includes information about the element type of the given array type.
20022 This function reuses previously set type and bound information if
20023 available. */
20025 static void
20026 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20028 unsigned dimension_number;
20029 tree lower, upper;
20030 dw_die_ref child = type_die->die_child;
20032 for (dimension_number = 0;
20033 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20034 type = TREE_TYPE (type), dimension_number++)
20036 tree domain = TYPE_DOMAIN (type);
20038 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20039 break;
20041 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20042 and (in GNU C only) variable bounds. Handle all three forms
20043 here. */
20045 /* Find and reuse a previously generated DW_TAG_subrange_type if
20046 available.
20048 For multi-dimensional arrays, as we iterate through the
20049 various dimensions in the enclosing for loop above, we also
20050 iterate through the DIE children and pick at each
20051 DW_TAG_subrange_type previously generated (if available).
20052 Each child DW_TAG_subrange_type DIE describes the range of
20053 the current dimension. At this point we should have as many
20054 DW_TAG_subrange_type's as we have dimensions in the
20055 array. */
20056 dw_die_ref subrange_die = NULL;
20057 if (child)
20058 while (1)
20060 child = child->die_sib;
20061 if (child->die_tag == DW_TAG_subrange_type)
20062 subrange_die = child;
20063 if (child == type_die->die_child)
20065 /* If we wrapped around, stop looking next time. */
20066 child = NULL;
20067 break;
20069 if (child->die_tag == DW_TAG_subrange_type)
20070 break;
20072 if (!subrange_die)
20073 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20075 if (domain)
20077 /* We have an array type with specified bounds. */
20078 lower = TYPE_MIN_VALUE (domain);
20079 upper = TYPE_MAX_VALUE (domain);
20081 /* Define the index type. */
20082 if (TREE_TYPE (domain)
20083 && !get_AT (subrange_die, DW_AT_type))
20085 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20086 TREE_TYPE field. We can't emit debug info for this
20087 because it is an unnamed integral type. */
20088 if (TREE_CODE (domain) == INTEGER_TYPE
20089 && TYPE_NAME (domain) == NULL_TREE
20090 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20091 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20093 else
20094 add_type_attribute (subrange_die, TREE_TYPE (domain),
20095 TYPE_UNQUALIFIED, false, type_die);
20098 /* ??? If upper is NULL, the array has unspecified length,
20099 but it does have a lower bound. This happens with Fortran
20100 dimension arr(N:*)
20101 Since the debugger is definitely going to need to know N
20102 to produce useful results, go ahead and output the lower
20103 bound solo, and hope the debugger can cope. */
20105 if (!get_AT (subrange_die, DW_AT_lower_bound))
20106 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20107 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20108 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20111 /* Otherwise we have an array type with an unspecified length. The
20112 DWARF-2 spec does not say how to handle this; let's just leave out the
20113 bounds. */
20117 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20119 static void
20120 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20122 dw_die_ref decl_die;
20123 HOST_WIDE_INT size;
20124 dw_loc_descr_ref size_expr = NULL;
20126 switch (TREE_CODE (tree_node))
20128 case ERROR_MARK:
20129 size = 0;
20130 break;
20131 case ENUMERAL_TYPE:
20132 case RECORD_TYPE:
20133 case UNION_TYPE:
20134 case QUAL_UNION_TYPE:
20135 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20136 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20138 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20139 return;
20141 size_expr = type_byte_size (tree_node, &size);
20142 break;
20143 case FIELD_DECL:
20144 /* For a data member of a struct or union, the DW_AT_byte_size is
20145 generally given as the number of bytes normally allocated for an
20146 object of the *declared* type of the member itself. This is true
20147 even for bit-fields. */
20148 size = int_size_in_bytes (field_type (tree_node));
20149 break;
20150 default:
20151 gcc_unreachable ();
20154 /* Support for dynamically-sized objects was introduced by DWARFv3.
20155 At the moment, GDB does not handle variable byte sizes very well,
20156 though. */
20157 if ((dwarf_version >= 3 || !dwarf_strict)
20158 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20159 && size_expr != NULL)
20160 add_AT_loc (die, DW_AT_byte_size, size_expr);
20162 /* Note that `size' might be -1 when we get to this point. If it is, that
20163 indicates that the byte size of the entity in question is variable and
20164 that we could not generate a DWARF expression that computes it. */
20165 if (size >= 0)
20166 add_AT_unsigned (die, DW_AT_byte_size, size);
20169 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20170 alignment. */
20172 static void
20173 add_alignment_attribute (dw_die_ref die, tree tree_node)
20175 if (dwarf_version < 5 && dwarf_strict)
20176 return;
20178 unsigned align;
20180 if (DECL_P (tree_node))
20182 if (!DECL_USER_ALIGN (tree_node))
20183 return;
20185 align = DECL_ALIGN_UNIT (tree_node);
20187 else if (TYPE_P (tree_node))
20189 if (!TYPE_USER_ALIGN (tree_node))
20190 return;
20192 align = TYPE_ALIGN_UNIT (tree_node);
20194 else
20195 gcc_unreachable ();
20197 add_AT_unsigned (die, DW_AT_alignment, align);
20200 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20201 which specifies the distance in bits from the highest order bit of the
20202 "containing object" for the bit-field to the highest order bit of the
20203 bit-field itself.
20205 For any given bit-field, the "containing object" is a hypothetical object
20206 (of some integral or enum type) within which the given bit-field lives. The
20207 type of this hypothetical "containing object" is always the same as the
20208 declared type of the individual bit-field itself. The determination of the
20209 exact location of the "containing object" for a bit-field is rather
20210 complicated. It's handled by the `field_byte_offset' function (above).
20212 CTX is required: see the comment for VLR_CONTEXT.
20214 Note that it is the size (in bytes) of the hypothetical "containing object"
20215 which will be given in the DW_AT_byte_size attribute for this bit-field.
20216 (See `byte_size_attribute' above). */
20218 static inline void
20219 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20221 HOST_WIDE_INT object_offset_in_bytes;
20222 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20223 HOST_WIDE_INT bitpos_int;
20224 HOST_WIDE_INT highest_order_object_bit_offset;
20225 HOST_WIDE_INT highest_order_field_bit_offset;
20226 HOST_WIDE_INT bit_offset;
20228 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20230 /* Must be a field and a bit field. */
20231 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20233 /* We can't yet handle bit-fields whose offsets are variable, so if we
20234 encounter such things, just return without generating any attribute
20235 whatsoever. Likewise for variable or too large size. */
20236 if (! tree_fits_shwi_p (bit_position (decl))
20237 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20238 return;
20240 bitpos_int = int_bit_position (decl);
20242 /* Note that the bit offset is always the distance (in bits) from the
20243 highest-order bit of the "containing object" to the highest-order bit of
20244 the bit-field itself. Since the "high-order end" of any object or field
20245 is different on big-endian and little-endian machines, the computation
20246 below must take account of these differences. */
20247 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20248 highest_order_field_bit_offset = bitpos_int;
20250 if (! BYTES_BIG_ENDIAN)
20252 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20253 highest_order_object_bit_offset +=
20254 simple_type_size_in_bits (original_type);
20257 bit_offset
20258 = (! BYTES_BIG_ENDIAN
20259 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20260 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20262 if (bit_offset < 0)
20263 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20264 else
20265 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20268 /* For a FIELD_DECL node which represents a bit field, output an attribute
20269 which specifies the length in bits of the given field. */
20271 static inline void
20272 add_bit_size_attribute (dw_die_ref die, tree decl)
20274 /* Must be a field and a bit field. */
20275 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20276 && DECL_BIT_FIELD_TYPE (decl));
20278 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20279 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20282 /* If the compiled language is ANSI C, then add a 'prototyped'
20283 attribute, if arg types are given for the parameters of a function. */
20285 static inline void
20286 add_prototyped_attribute (dw_die_ref die, tree func_type)
20288 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20290 case DW_LANG_C:
20291 case DW_LANG_C89:
20292 case DW_LANG_C99:
20293 case DW_LANG_C11:
20294 case DW_LANG_ObjC:
20295 if (prototype_p (func_type))
20296 add_AT_flag (die, DW_AT_prototyped, 1);
20297 break;
20298 default:
20299 break;
20303 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20304 by looking in the type declaration, the object declaration equate table or
20305 the block mapping. */
20307 static inline dw_die_ref
20308 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20310 dw_die_ref origin_die = NULL;
20312 if (DECL_P (origin))
20314 dw_die_ref c;
20315 origin_die = lookup_decl_die (origin);
20316 /* "Unwrap" the decls DIE which we put in the imported unit context.
20317 We are looking for the abstract copy here. */
20318 if (in_lto_p
20319 && origin_die
20320 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20321 /* ??? Identify this better. */
20322 && c->with_offset)
20323 origin_die = c;
20325 else if (TYPE_P (origin))
20326 origin_die = lookup_type_die (origin);
20327 else if (TREE_CODE (origin) == BLOCK)
20328 origin_die = BLOCK_DIE (origin);
20330 /* XXX: Functions that are never lowered don't always have correct block
20331 trees (in the case of java, they simply have no block tree, in some other
20332 languages). For these functions, there is nothing we can really do to
20333 output correct debug info for inlined functions in all cases. Rather
20334 than die, we'll just produce deficient debug info now, in that we will
20335 have variables without a proper abstract origin. In the future, when all
20336 functions are lowered, we should re-add a gcc_assert (origin_die)
20337 here. */
20339 if (origin_die)
20340 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20341 return origin_die;
20344 /* We do not currently support the pure_virtual attribute. */
20346 static inline void
20347 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20349 if (DECL_VINDEX (func_decl))
20351 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20353 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20354 add_AT_loc (die, DW_AT_vtable_elem_location,
20355 new_loc_descr (DW_OP_constu,
20356 tree_to_shwi (DECL_VINDEX (func_decl)),
20357 0));
20359 /* GNU extension: Record what type this method came from originally. */
20360 if (debug_info_level > DINFO_LEVEL_TERSE
20361 && DECL_CONTEXT (func_decl))
20362 add_AT_die_ref (die, DW_AT_containing_type,
20363 lookup_type_die (DECL_CONTEXT (func_decl)));
20367 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20368 given decl. This used to be a vendor extension until after DWARF 4
20369 standardized it. */
20371 static void
20372 add_linkage_attr (dw_die_ref die, tree decl)
20374 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20376 /* Mimic what assemble_name_raw does with a leading '*'. */
20377 if (name[0] == '*')
20378 name = &name[1];
20380 if (dwarf_version >= 4)
20381 add_AT_string (die, DW_AT_linkage_name, name);
20382 else
20383 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20386 /* Add source coordinate attributes for the given decl. */
20388 static void
20389 add_src_coords_attributes (dw_die_ref die, tree decl)
20391 expanded_location s;
20393 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20394 return;
20395 s = expand_location (DECL_SOURCE_LOCATION (decl));
20396 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20397 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20398 if (debug_column_info && s.column)
20399 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20402 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20404 static void
20405 add_linkage_name_raw (dw_die_ref die, tree decl)
20407 /* Defer until we have an assembler name set. */
20408 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20410 limbo_die_node *asm_name;
20412 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20413 asm_name->die = die;
20414 asm_name->created_for = decl;
20415 asm_name->next = deferred_asm_name;
20416 deferred_asm_name = asm_name;
20418 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20419 add_linkage_attr (die, decl);
20422 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20424 static void
20425 add_linkage_name (dw_die_ref die, tree decl)
20427 if (debug_info_level > DINFO_LEVEL_NONE
20428 && VAR_OR_FUNCTION_DECL_P (decl)
20429 && TREE_PUBLIC (decl)
20430 && !(VAR_P (decl) && DECL_REGISTER (decl))
20431 && die->die_tag != DW_TAG_member)
20432 add_linkage_name_raw (die, decl);
20435 /* Add a DW_AT_name attribute and source coordinate attribute for the
20436 given decl, but only if it actually has a name. */
20438 static void
20439 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20440 bool no_linkage_name)
20442 tree decl_name;
20444 decl_name = DECL_NAME (decl);
20445 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20447 const char *name = dwarf2_name (decl, 0);
20448 if (name)
20449 add_name_attribute (die, name);
20450 if (! DECL_ARTIFICIAL (decl))
20451 add_src_coords_attributes (die, decl);
20453 if (!no_linkage_name)
20454 add_linkage_name (die, decl);
20457 #ifdef VMS_DEBUGGING_INFO
20458 /* Get the function's name, as described by its RTL. This may be different
20459 from the DECL_NAME name used in the source file. */
20460 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20462 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20463 XEXP (DECL_RTL (decl), 0), false);
20464 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20466 #endif /* VMS_DEBUGGING_INFO */
20469 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20471 static void
20472 add_discr_value (dw_die_ref die, dw_discr_value *value)
20474 dw_attr_node attr;
20476 attr.dw_attr = DW_AT_discr_value;
20477 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20478 attr.dw_attr_val.val_entry = NULL;
20479 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20480 if (value->pos)
20481 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20482 else
20483 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20484 add_dwarf_attr (die, &attr);
20487 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20489 static void
20490 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20492 dw_attr_node attr;
20494 attr.dw_attr = DW_AT_discr_list;
20495 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20496 attr.dw_attr_val.val_entry = NULL;
20497 attr.dw_attr_val.v.val_discr_list = discr_list;
20498 add_dwarf_attr (die, &attr);
20501 static inline dw_discr_list_ref
20502 AT_discr_list (dw_attr_node *attr)
20504 return attr->dw_attr_val.v.val_discr_list;
20507 #ifdef VMS_DEBUGGING_INFO
20508 /* Output the debug main pointer die for VMS */
20510 void
20511 dwarf2out_vms_debug_main_pointer (void)
20513 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20514 dw_die_ref die;
20516 /* Allocate the VMS debug main subprogram die. */
20517 die = ggc_cleared_alloc<die_node> ();
20518 die->die_tag = DW_TAG_subprogram;
20519 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20520 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20521 current_function_funcdef_no);
20522 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20524 /* Make it the first child of comp_unit_die (). */
20525 die->die_parent = comp_unit_die ();
20526 if (comp_unit_die ()->die_child)
20528 die->die_sib = comp_unit_die ()->die_child->die_sib;
20529 comp_unit_die ()->die_child->die_sib = die;
20531 else
20533 die->die_sib = die;
20534 comp_unit_die ()->die_child = die;
20537 #endif /* VMS_DEBUGGING_INFO */
20539 /* Push a new declaration scope. */
20541 static void
20542 push_decl_scope (tree scope)
20544 vec_safe_push (decl_scope_table, scope);
20547 /* Pop a declaration scope. */
20549 static inline void
20550 pop_decl_scope (void)
20552 decl_scope_table->pop ();
20555 /* walk_tree helper function for uses_local_type, below. */
20557 static tree
20558 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20560 if (!TYPE_P (*tp))
20561 *walk_subtrees = 0;
20562 else
20564 tree name = TYPE_NAME (*tp);
20565 if (name && DECL_P (name) && decl_function_context (name))
20566 return *tp;
20568 return NULL_TREE;
20571 /* If TYPE involves a function-local type (including a local typedef to a
20572 non-local type), returns that type; otherwise returns NULL_TREE. */
20574 static tree
20575 uses_local_type (tree type)
20577 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20578 return used;
20581 /* Return the DIE for the scope that immediately contains this type.
20582 Non-named types that do not involve a function-local type get global
20583 scope. Named types nested in namespaces or other types get their
20584 containing scope. All other types (i.e. function-local named types) get
20585 the current active scope. */
20587 static dw_die_ref
20588 scope_die_for (tree t, dw_die_ref context_die)
20590 dw_die_ref scope_die = NULL;
20591 tree containing_scope;
20593 /* Non-types always go in the current scope. */
20594 gcc_assert (TYPE_P (t));
20596 /* Use the scope of the typedef, rather than the scope of the type
20597 it refers to. */
20598 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20599 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20600 else
20601 containing_scope = TYPE_CONTEXT (t);
20603 /* Use the containing namespace if there is one. */
20604 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20606 if (context_die == lookup_decl_die (containing_scope))
20607 /* OK */;
20608 else if (debug_info_level > DINFO_LEVEL_TERSE)
20609 context_die = get_context_die (containing_scope);
20610 else
20611 containing_scope = NULL_TREE;
20614 /* Ignore function type "scopes" from the C frontend. They mean that
20615 a tagged type is local to a parmlist of a function declarator, but
20616 that isn't useful to DWARF. */
20617 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20618 containing_scope = NULL_TREE;
20620 if (SCOPE_FILE_SCOPE_P (containing_scope))
20622 /* If T uses a local type keep it local as well, to avoid references
20623 to function-local DIEs from outside the function. */
20624 if (current_function_decl && uses_local_type (t))
20625 scope_die = context_die;
20626 else
20627 scope_die = comp_unit_die ();
20629 else if (TYPE_P (containing_scope))
20631 /* For types, we can just look up the appropriate DIE. */
20632 if (debug_info_level > DINFO_LEVEL_TERSE)
20633 scope_die = get_context_die (containing_scope);
20634 else
20636 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20637 if (scope_die == NULL)
20638 scope_die = comp_unit_die ();
20641 else
20642 scope_die = context_die;
20644 return scope_die;
20647 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20649 static inline int
20650 local_scope_p (dw_die_ref context_die)
20652 for (; context_die; context_die = context_die->die_parent)
20653 if (context_die->die_tag == DW_TAG_inlined_subroutine
20654 || context_die->die_tag == DW_TAG_subprogram)
20655 return 1;
20657 return 0;
20660 /* Returns nonzero if CONTEXT_DIE is a class. */
20662 static inline int
20663 class_scope_p (dw_die_ref context_die)
20665 return (context_die
20666 && (context_die->die_tag == DW_TAG_structure_type
20667 || context_die->die_tag == DW_TAG_class_type
20668 || context_die->die_tag == DW_TAG_interface_type
20669 || context_die->die_tag == DW_TAG_union_type));
20672 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20673 whether or not to treat a DIE in this context as a declaration. */
20675 static inline int
20676 class_or_namespace_scope_p (dw_die_ref context_die)
20678 return (class_scope_p (context_die)
20679 || (context_die && context_die->die_tag == DW_TAG_namespace));
20682 /* Many forms of DIEs require a "type description" attribute. This
20683 routine locates the proper "type descriptor" die for the type given
20684 by 'type' plus any additional qualifiers given by 'cv_quals', and
20685 adds a DW_AT_type attribute below the given die. */
20687 static void
20688 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20689 bool reverse, dw_die_ref context_die)
20691 enum tree_code code = TREE_CODE (type);
20692 dw_die_ref type_die = NULL;
20694 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20695 or fixed-point type, use the inner type. This is because we have no
20696 support for unnamed types in base_type_die. This can happen if this is
20697 an Ada subrange type. Correct solution is emit a subrange type die. */
20698 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20699 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20700 type = TREE_TYPE (type), code = TREE_CODE (type);
20702 if (code == ERROR_MARK
20703 /* Handle a special case. For functions whose return type is void, we
20704 generate *no* type attribute. (Note that no object may have type
20705 `void', so this only applies to function return types). */
20706 || code == VOID_TYPE)
20707 return;
20709 type_die = modified_type_die (type,
20710 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
20711 reverse,
20712 context_die);
20714 if (type_die != NULL)
20715 add_AT_die_ref (object_die, DW_AT_type, type_die);
20718 /* Given an object die, add the calling convention attribute for the
20719 function call type. */
20720 static void
20721 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20723 enum dwarf_calling_convention value = DW_CC_normal;
20725 value = ((enum dwarf_calling_convention)
20726 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20728 if (is_fortran ()
20729 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20731 /* DWARF 2 doesn't provide a way to identify a program's source-level
20732 entry point. DW_AT_calling_convention attributes are only meant
20733 to describe functions' calling conventions. However, lacking a
20734 better way to signal the Fortran main program, we used this for
20735 a long time, following existing custom. Now, DWARF 4 has
20736 DW_AT_main_subprogram, which we add below, but some tools still
20737 rely on the old way, which we thus keep. */
20738 value = DW_CC_program;
20740 if (dwarf_version >= 4 || !dwarf_strict)
20741 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20744 /* Only add the attribute if the backend requests it, and
20745 is not DW_CC_normal. */
20746 if (value && (value != DW_CC_normal))
20747 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20750 /* Given a tree pointer to a struct, class, union, or enum type node, return
20751 a pointer to the (string) tag name for the given type, or zero if the type
20752 was declared without a tag. */
20754 static const char *
20755 type_tag (const_tree type)
20757 const char *name = 0;
20759 if (TYPE_NAME (type) != 0)
20761 tree t = 0;
20763 /* Find the IDENTIFIER_NODE for the type name. */
20764 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20765 && !TYPE_NAMELESS (type))
20766 t = TYPE_NAME (type);
20768 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20769 a TYPE_DECL node, regardless of whether or not a `typedef' was
20770 involved. */
20771 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20772 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20774 /* We want to be extra verbose. Don't call dwarf_name if
20775 DECL_NAME isn't set. The default hook for decl_printable_name
20776 doesn't like that, and in this context it's correct to return
20777 0, instead of "<anonymous>" or the like. */
20778 if (DECL_NAME (TYPE_NAME (type))
20779 && !DECL_NAMELESS (TYPE_NAME (type)))
20780 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20783 /* Now get the name as a string, or invent one. */
20784 if (!name && t != 0)
20785 name = IDENTIFIER_POINTER (t);
20788 return (name == 0 || *name == '\0') ? 0 : name;
20791 /* Return the type associated with a data member, make a special check
20792 for bit field types. */
20794 static inline tree
20795 member_declared_type (const_tree member)
20797 return (DECL_BIT_FIELD_TYPE (member)
20798 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20801 /* Get the decl's label, as described by its RTL. This may be different
20802 from the DECL_NAME name used in the source file. */
20804 #if 0
20805 static const char *
20806 decl_start_label (tree decl)
20808 rtx x;
20809 const char *fnname;
20811 x = DECL_RTL (decl);
20812 gcc_assert (MEM_P (x));
20814 x = XEXP (x, 0);
20815 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20817 fnname = XSTR (x, 0);
20818 return fnname;
20820 #endif
20822 /* For variable-length arrays that have been previously generated, but
20823 may be incomplete due to missing subscript info, fill the subscript
20824 info. Return TRUE if this is one of those cases. */
20825 static bool
20826 fill_variable_array_bounds (tree type)
20828 if (TREE_ASM_WRITTEN (type)
20829 && TREE_CODE (type) == ARRAY_TYPE
20830 && variably_modified_type_p (type, NULL))
20832 dw_die_ref array_die = lookup_type_die (type);
20833 if (!array_die)
20834 return false;
20835 add_subscript_info (array_die, type, !is_ada ());
20836 return true;
20838 return false;
20841 /* These routines generate the internal representation of the DIE's for
20842 the compilation unit. Debugging information is collected by walking
20843 the declaration trees passed in from dwarf2out_decl(). */
20845 static void
20846 gen_array_type_die (tree type, dw_die_ref context_die)
20848 dw_die_ref array_die;
20850 /* GNU compilers represent multidimensional array types as sequences of one
20851 dimensional array types whose element types are themselves array types.
20852 We sometimes squish that down to a single array_type DIE with multiple
20853 subscripts in the Dwarf debugging info. The draft Dwarf specification
20854 say that we are allowed to do this kind of compression in C, because
20855 there is no difference between an array of arrays and a multidimensional
20856 array. We don't do this for Ada to remain as close as possible to the
20857 actual representation, which is especially important against the language
20858 flexibilty wrt arrays of variable size. */
20860 bool collapse_nested_arrays = !is_ada ();
20862 if (fill_variable_array_bounds (type))
20863 return;
20865 dw_die_ref scope_die = scope_die_for (type, context_die);
20866 tree element_type;
20868 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
20869 DW_TAG_string_type doesn't have DW_AT_type attribute). */
20870 if (TYPE_STRING_FLAG (type)
20871 && TREE_CODE (type) == ARRAY_TYPE
20872 && is_fortran ()
20873 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
20875 HOST_WIDE_INT size;
20877 array_die = new_die (DW_TAG_string_type, scope_die, type);
20878 add_name_attribute (array_die, type_tag (type));
20879 equate_type_number_to_die (type, array_die);
20880 size = int_size_in_bytes (type);
20881 if (size >= 0)
20882 add_AT_unsigned (array_die, DW_AT_byte_size, size);
20883 /* ??? We can't annotate types late, but for LTO we may not
20884 generate a location early either (gfortran.dg/save_6.f90). */
20885 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
20886 && TYPE_DOMAIN (type) != NULL_TREE
20887 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
20889 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
20890 tree rszdecl = szdecl;
20892 size = int_size_in_bytes (TREE_TYPE (szdecl));
20893 if (!DECL_P (szdecl))
20895 if (TREE_CODE (szdecl) == INDIRECT_REF
20896 && DECL_P (TREE_OPERAND (szdecl, 0)))
20898 rszdecl = TREE_OPERAND (szdecl, 0);
20899 if (int_size_in_bytes (TREE_TYPE (rszdecl))
20900 != DWARF2_ADDR_SIZE)
20901 size = 0;
20903 else
20904 size = 0;
20906 if (size > 0)
20908 dw_loc_list_ref loc
20909 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
20910 NULL);
20911 if (loc)
20913 add_AT_location_description (array_die, DW_AT_string_length,
20914 loc);
20915 if (size != DWARF2_ADDR_SIZE)
20916 add_AT_unsigned (array_die, dwarf_version >= 5
20917 ? DW_AT_string_length_byte_size
20918 : DW_AT_byte_size, size);
20922 return;
20925 array_die = new_die (DW_TAG_array_type, scope_die, type);
20926 add_name_attribute (array_die, type_tag (type));
20927 equate_type_number_to_die (type, array_die);
20929 if (TREE_CODE (type) == VECTOR_TYPE)
20930 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
20932 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
20933 if (is_fortran ()
20934 && TREE_CODE (type) == ARRAY_TYPE
20935 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
20936 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
20937 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
20939 #if 0
20940 /* We default the array ordering. SDB will probably do
20941 the right things even if DW_AT_ordering is not present. It's not even
20942 an issue until we start to get into multidimensional arrays anyway. If
20943 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
20944 then we'll have to put the DW_AT_ordering attribute back in. (But if
20945 and when we find out that we need to put these in, we will only do so
20946 for multidimensional arrays. */
20947 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
20948 #endif
20950 if (TREE_CODE (type) == VECTOR_TYPE)
20952 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
20953 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
20954 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
20955 add_bound_info (subrange_die, DW_AT_upper_bound,
20956 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
20958 else
20959 add_subscript_info (array_die, type, collapse_nested_arrays);
20961 /* Add representation of the type of the elements of this array type and
20962 emit the corresponding DIE if we haven't done it already. */
20963 element_type = TREE_TYPE (type);
20964 if (collapse_nested_arrays)
20965 while (TREE_CODE (element_type) == ARRAY_TYPE)
20967 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
20968 break;
20969 element_type = TREE_TYPE (element_type);
20972 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
20973 TREE_CODE (type) == ARRAY_TYPE
20974 && TYPE_REVERSE_STORAGE_ORDER (type),
20975 context_die);
20977 add_gnat_descriptive_type_attribute (array_die, type, context_die);
20978 if (TYPE_ARTIFICIAL (type))
20979 add_AT_flag (array_die, DW_AT_artificial, 1);
20981 if (get_AT (array_die, DW_AT_name))
20982 add_pubtype (type, array_die);
20984 add_alignment_attribute (array_die, type);
20987 /* This routine generates DIE for array with hidden descriptor, details
20988 are filled into *info by a langhook. */
20990 static void
20991 gen_descr_array_type_die (tree type, struct array_descr_info *info,
20992 dw_die_ref context_die)
20994 const dw_die_ref scope_die = scope_die_for (type, context_die);
20995 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
20996 struct loc_descr_context context = { type, info->base_decl, NULL,
20997 false, false };
20998 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
20999 int dim;
21001 add_name_attribute (array_die, type_tag (type));
21002 equate_type_number_to_die (type, array_die);
21004 if (info->ndimensions > 1)
21005 switch (info->ordering)
21007 case array_descr_ordering_row_major:
21008 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21009 break;
21010 case array_descr_ordering_column_major:
21011 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21012 break;
21013 default:
21014 break;
21017 if (dwarf_version >= 3 || !dwarf_strict)
21019 if (info->data_location)
21020 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21021 dw_scalar_form_exprloc, &context);
21022 if (info->associated)
21023 add_scalar_info (array_die, DW_AT_associated, info->associated,
21024 dw_scalar_form_constant
21025 | dw_scalar_form_exprloc
21026 | dw_scalar_form_reference, &context);
21027 if (info->allocated)
21028 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21029 dw_scalar_form_constant
21030 | dw_scalar_form_exprloc
21031 | dw_scalar_form_reference, &context);
21032 if (info->stride)
21034 const enum dwarf_attribute attr
21035 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21036 const int forms
21037 = (info->stride_in_bits)
21038 ? dw_scalar_form_constant
21039 : (dw_scalar_form_constant
21040 | dw_scalar_form_exprloc
21041 | dw_scalar_form_reference);
21043 add_scalar_info (array_die, attr, info->stride, forms, &context);
21046 if (dwarf_version >= 5)
21048 if (info->rank)
21050 add_scalar_info (array_die, DW_AT_rank, info->rank,
21051 dw_scalar_form_constant
21052 | dw_scalar_form_exprloc, &context);
21053 subrange_tag = DW_TAG_generic_subrange;
21054 context.placeholder_arg = true;
21058 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21060 for (dim = 0; dim < info->ndimensions; dim++)
21062 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21064 if (info->dimen[dim].bounds_type)
21065 add_type_attribute (subrange_die,
21066 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21067 false, context_die);
21068 if (info->dimen[dim].lower_bound)
21069 add_bound_info (subrange_die, DW_AT_lower_bound,
21070 info->dimen[dim].lower_bound, &context);
21071 if (info->dimen[dim].upper_bound)
21072 add_bound_info (subrange_die, DW_AT_upper_bound,
21073 info->dimen[dim].upper_bound, &context);
21074 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21075 add_scalar_info (subrange_die, DW_AT_byte_stride,
21076 info->dimen[dim].stride,
21077 dw_scalar_form_constant
21078 | dw_scalar_form_exprloc
21079 | dw_scalar_form_reference,
21080 &context);
21083 gen_type_die (info->element_type, context_die);
21084 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21085 TREE_CODE (type) == ARRAY_TYPE
21086 && TYPE_REVERSE_STORAGE_ORDER (type),
21087 context_die);
21089 if (get_AT (array_die, DW_AT_name))
21090 add_pubtype (type, array_die);
21092 add_alignment_attribute (array_die, type);
21095 #if 0
21096 static void
21097 gen_entry_point_die (tree decl, dw_die_ref context_die)
21099 tree origin = decl_ultimate_origin (decl);
21100 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21102 if (origin != NULL)
21103 add_abstract_origin_attribute (decl_die, origin);
21104 else
21106 add_name_and_src_coords_attributes (decl_die, decl);
21107 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21108 TYPE_UNQUALIFIED, false, context_die);
21111 if (DECL_ABSTRACT_P (decl))
21112 equate_decl_number_to_die (decl, decl_die);
21113 else
21114 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21116 #endif
21118 /* Walk through the list of incomplete types again, trying once more to
21119 emit full debugging info for them. */
21121 static void
21122 retry_incomplete_types (void)
21124 set_early_dwarf s;
21125 int i;
21127 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21128 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21129 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21130 vec_safe_truncate (incomplete_types, 0);
21133 /* Determine what tag to use for a record type. */
21135 static enum dwarf_tag
21136 record_type_tag (tree type)
21138 if (! lang_hooks.types.classify_record)
21139 return DW_TAG_structure_type;
21141 switch (lang_hooks.types.classify_record (type))
21143 case RECORD_IS_STRUCT:
21144 return DW_TAG_structure_type;
21146 case RECORD_IS_CLASS:
21147 return DW_TAG_class_type;
21149 case RECORD_IS_INTERFACE:
21150 if (dwarf_version >= 3 || !dwarf_strict)
21151 return DW_TAG_interface_type;
21152 return DW_TAG_structure_type;
21154 default:
21155 gcc_unreachable ();
21159 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21160 include all of the information about the enumeration values also. Each
21161 enumerated type name/value is listed as a child of the enumerated type
21162 DIE. */
21164 static dw_die_ref
21165 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21167 dw_die_ref type_die = lookup_type_die (type);
21169 if (type_die == NULL)
21171 type_die = new_die (DW_TAG_enumeration_type,
21172 scope_die_for (type, context_die), type);
21173 equate_type_number_to_die (type, type_die);
21174 add_name_attribute (type_die, type_tag (type));
21175 if (dwarf_version >= 4 || !dwarf_strict)
21177 if (ENUM_IS_SCOPED (type))
21178 add_AT_flag (type_die, DW_AT_enum_class, 1);
21179 if (ENUM_IS_OPAQUE (type))
21180 add_AT_flag (type_die, DW_AT_declaration, 1);
21182 if (!dwarf_strict)
21183 add_AT_unsigned (type_die, DW_AT_encoding,
21184 TYPE_UNSIGNED (type)
21185 ? DW_ATE_unsigned
21186 : DW_ATE_signed);
21188 else if (! TYPE_SIZE (type))
21189 return type_die;
21190 else
21191 remove_AT (type_die, DW_AT_declaration);
21193 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21194 given enum type is incomplete, do not generate the DW_AT_byte_size
21195 attribute or the DW_AT_element_list attribute. */
21196 if (TYPE_SIZE (type))
21198 tree link;
21200 TREE_ASM_WRITTEN (type) = 1;
21201 add_byte_size_attribute (type_die, type);
21202 add_alignment_attribute (type_die, type);
21203 if (dwarf_version >= 3 || !dwarf_strict)
21205 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21206 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21207 context_die);
21209 if (TYPE_STUB_DECL (type) != NULL_TREE)
21211 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21212 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21215 /* If the first reference to this type was as the return type of an
21216 inline function, then it may not have a parent. Fix this now. */
21217 if (type_die->die_parent == NULL)
21218 add_child_die (scope_die_for (type, context_die), type_die);
21220 for (link = TYPE_VALUES (type);
21221 link != NULL; link = TREE_CHAIN (link))
21223 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21224 tree value = TREE_VALUE (link);
21226 add_name_attribute (enum_die,
21227 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21229 if (TREE_CODE (value) == CONST_DECL)
21230 value = DECL_INITIAL (value);
21232 if (simple_type_size_in_bits (TREE_TYPE (value))
21233 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21235 /* For constant forms created by add_AT_unsigned DWARF
21236 consumers (GDB, elfutils, etc.) always zero extend
21237 the value. Only when the actual value is negative
21238 do we need to use add_AT_int to generate a constant
21239 form that can represent negative values. */
21240 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21241 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21242 add_AT_unsigned (enum_die, DW_AT_const_value,
21243 (unsigned HOST_WIDE_INT) val);
21244 else
21245 add_AT_int (enum_die, DW_AT_const_value, val);
21247 else
21248 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21249 that here. TODO: This should be re-worked to use correct
21250 signed/unsigned double tags for all cases. */
21251 add_AT_wide (enum_die, DW_AT_const_value, value);
21254 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21255 if (TYPE_ARTIFICIAL (type))
21256 add_AT_flag (type_die, DW_AT_artificial, 1);
21258 else
21259 add_AT_flag (type_die, DW_AT_declaration, 1);
21261 add_alignment_attribute (type_die, type);
21263 add_pubtype (type, type_die);
21265 return type_die;
21268 /* Generate a DIE to represent either a real live formal parameter decl or to
21269 represent just the type of some formal parameter position in some function
21270 type.
21272 Note that this routine is a bit unusual because its argument may be a
21273 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21274 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21275 node. If it's the former then this function is being called to output a
21276 DIE to represent a formal parameter object (or some inlining thereof). If
21277 it's the latter, then this function is only being called to output a
21278 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21279 argument type of some subprogram type.
21280 If EMIT_NAME_P is true, name and source coordinate attributes
21281 are emitted. */
21283 static dw_die_ref
21284 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21285 dw_die_ref context_die)
21287 tree node_or_origin = node ? node : origin;
21288 tree ultimate_origin;
21289 dw_die_ref parm_die = NULL;
21291 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
21293 parm_die = lookup_decl_die (node);
21295 /* If the contexts differ, we may not be talking about the same
21296 thing.
21297 ??? When in LTO the DIE parent is the "abstract" copy and the
21298 context_die is the specification "copy". But this whole block
21299 should eventually be no longer needed. */
21300 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21302 if (!DECL_ABSTRACT_P (node))
21304 /* This can happen when creating an inlined instance, in
21305 which case we need to create a new DIE that will get
21306 annotated with DW_AT_abstract_origin. */
21307 parm_die = NULL;
21309 else
21310 gcc_unreachable ();
21313 if (parm_die && parm_die->die_parent == NULL)
21315 /* Check that parm_die already has the right attributes that
21316 we would have added below. If any attributes are
21317 missing, fall through to add them. */
21318 if (! DECL_ABSTRACT_P (node_or_origin)
21319 && !get_AT (parm_die, DW_AT_location)
21320 && !get_AT (parm_die, DW_AT_const_value))
21321 /* We are missing location info, and are about to add it. */
21323 else
21325 add_child_die (context_die, parm_die);
21326 return parm_die;
21331 /* If we have a previously generated DIE, use it, unless this is an
21332 concrete instance (origin != NULL), in which case we need a new
21333 DIE with a corresponding DW_AT_abstract_origin. */
21334 bool reusing_die;
21335 if (parm_die && origin == NULL)
21336 reusing_die = true;
21337 else
21339 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21340 reusing_die = false;
21343 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21345 case tcc_declaration:
21346 ultimate_origin = decl_ultimate_origin (node_or_origin);
21347 if (node || ultimate_origin)
21348 origin = ultimate_origin;
21350 if (reusing_die)
21351 goto add_location;
21353 if (origin != NULL)
21354 add_abstract_origin_attribute (parm_die, origin);
21355 else if (emit_name_p)
21356 add_name_and_src_coords_attributes (parm_die, node);
21357 if (origin == NULL
21358 || (! DECL_ABSTRACT_P (node_or_origin)
21359 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21360 decl_function_context
21361 (node_or_origin))))
21363 tree type = TREE_TYPE (node_or_origin);
21364 if (decl_by_reference_p (node_or_origin))
21365 add_type_attribute (parm_die, TREE_TYPE (type),
21366 TYPE_UNQUALIFIED,
21367 false, context_die);
21368 else
21369 add_type_attribute (parm_die, type,
21370 decl_quals (node_or_origin),
21371 false, context_die);
21373 if (origin == NULL && DECL_ARTIFICIAL (node))
21374 add_AT_flag (parm_die, DW_AT_artificial, 1);
21375 add_location:
21376 if (node && node != origin)
21377 equate_decl_number_to_die (node, parm_die);
21378 if (! DECL_ABSTRACT_P (node_or_origin))
21379 add_location_or_const_value_attribute (parm_die, node_or_origin,
21380 node == NULL);
21382 break;
21384 case tcc_type:
21385 /* We were called with some kind of a ..._TYPE node. */
21386 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21387 context_die);
21388 break;
21390 default:
21391 gcc_unreachable ();
21394 return parm_die;
21397 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21398 children DW_TAG_formal_parameter DIEs representing the arguments of the
21399 parameter pack.
21401 PARM_PACK must be a function parameter pack.
21402 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21403 must point to the subsequent arguments of the function PACK_ARG belongs to.
21404 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21405 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21406 following the last one for which a DIE was generated. */
21408 static dw_die_ref
21409 gen_formal_parameter_pack_die (tree parm_pack,
21410 tree pack_arg,
21411 dw_die_ref subr_die,
21412 tree *next_arg)
21414 tree arg;
21415 dw_die_ref parm_pack_die;
21417 gcc_assert (parm_pack
21418 && lang_hooks.function_parameter_pack_p (parm_pack)
21419 && subr_die);
21421 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21422 add_src_coords_attributes (parm_pack_die, parm_pack);
21424 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21426 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21427 parm_pack))
21428 break;
21429 gen_formal_parameter_die (arg, NULL,
21430 false /* Don't emit name attribute. */,
21431 parm_pack_die);
21433 if (next_arg)
21434 *next_arg = arg;
21435 return parm_pack_die;
21438 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21439 at the end of an (ANSI prototyped) formal parameters list. */
21441 static void
21442 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21444 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21447 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21448 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21449 parameters as specified in some function type specification (except for
21450 those which appear as part of a function *definition*). */
21452 static void
21453 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21455 tree link;
21456 tree formal_type = NULL;
21457 tree first_parm_type;
21458 tree arg;
21460 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21462 arg = DECL_ARGUMENTS (function_or_method_type);
21463 function_or_method_type = TREE_TYPE (function_or_method_type);
21465 else
21466 arg = NULL_TREE;
21468 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21470 /* Make our first pass over the list of formal parameter types and output a
21471 DW_TAG_formal_parameter DIE for each one. */
21472 for (link = first_parm_type; link; )
21474 dw_die_ref parm_die;
21476 formal_type = TREE_VALUE (link);
21477 if (formal_type == void_type_node)
21478 break;
21480 /* Output a (nameless) DIE to represent the formal parameter itself. */
21481 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21483 parm_die = gen_formal_parameter_die (formal_type, NULL,
21484 true /* Emit name attribute. */,
21485 context_die);
21486 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21487 && link == first_parm_type)
21489 add_AT_flag (parm_die, DW_AT_artificial, 1);
21490 if (dwarf_version >= 3 || !dwarf_strict)
21491 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21493 else if (arg && DECL_ARTIFICIAL (arg))
21494 add_AT_flag (parm_die, DW_AT_artificial, 1);
21497 link = TREE_CHAIN (link);
21498 if (arg)
21499 arg = DECL_CHAIN (arg);
21502 /* If this function type has an ellipsis, add a
21503 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21504 if (formal_type != void_type_node)
21505 gen_unspecified_parameters_die (function_or_method_type, context_die);
21507 /* Make our second (and final) pass over the list of formal parameter types
21508 and output DIEs to represent those types (as necessary). */
21509 for (link = TYPE_ARG_TYPES (function_or_method_type);
21510 link && TREE_VALUE (link);
21511 link = TREE_CHAIN (link))
21512 gen_type_die (TREE_VALUE (link), context_die);
21515 /* We want to generate the DIE for TYPE so that we can generate the
21516 die for MEMBER, which has been defined; we will need to refer back
21517 to the member declaration nested within TYPE. If we're trying to
21518 generate minimal debug info for TYPE, processing TYPE won't do the
21519 trick; we need to attach the member declaration by hand. */
21521 static void
21522 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21524 gen_type_die (type, context_die);
21526 /* If we're trying to avoid duplicate debug info, we may not have
21527 emitted the member decl for this function. Emit it now. */
21528 if (TYPE_STUB_DECL (type)
21529 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21530 && ! lookup_decl_die (member))
21532 dw_die_ref type_die;
21533 gcc_assert (!decl_ultimate_origin (member));
21535 push_decl_scope (type);
21536 type_die = lookup_type_die_strip_naming_typedef (type);
21537 if (TREE_CODE (member) == FUNCTION_DECL)
21538 gen_subprogram_die (member, type_die);
21539 else if (TREE_CODE (member) == FIELD_DECL)
21541 /* Ignore the nameless fields that are used to skip bits but handle
21542 C++ anonymous unions and structs. */
21543 if (DECL_NAME (member) != NULL_TREE
21544 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21545 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21547 struct vlr_context vlr_ctx = {
21548 DECL_CONTEXT (member), /* struct_type */
21549 NULL_TREE /* variant_part_offset */
21551 gen_type_die (member_declared_type (member), type_die);
21552 gen_field_die (member, &vlr_ctx, type_die);
21555 else
21556 gen_variable_die (member, NULL_TREE, type_die);
21558 pop_decl_scope ();
21562 /* Forward declare these functions, because they are mutually recursive
21563 with their set_block_* pairing functions. */
21564 static void set_decl_origin_self (tree);
21566 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21567 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21568 that it points to the node itself, thus indicating that the node is its
21569 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21570 the given node is NULL, recursively descend the decl/block tree which
21571 it is the root of, and for each other ..._DECL or BLOCK node contained
21572 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21573 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21574 values to point to themselves. */
21576 static void
21577 set_block_origin_self (tree stmt)
21579 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21581 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21584 tree local_decl;
21586 for (local_decl = BLOCK_VARS (stmt);
21587 local_decl != NULL_TREE;
21588 local_decl = DECL_CHAIN (local_decl))
21589 /* Do not recurse on nested functions since the inlining status
21590 of parent and child can be different as per the DWARF spec. */
21591 if (TREE_CODE (local_decl) != FUNCTION_DECL
21592 && !DECL_EXTERNAL (local_decl))
21593 set_decl_origin_self (local_decl);
21597 tree subblock;
21599 for (subblock = BLOCK_SUBBLOCKS (stmt);
21600 subblock != NULL_TREE;
21601 subblock = BLOCK_CHAIN (subblock))
21602 set_block_origin_self (subblock); /* Recurse. */
21607 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21608 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21609 node to so that it points to the node itself, thus indicating that the
21610 node represents its own (abstract) origin. Additionally, if the
21611 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21612 the decl/block tree of which the given node is the root of, and for
21613 each other ..._DECL or BLOCK node contained therein whose
21614 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21615 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21616 point to themselves. */
21618 static void
21619 set_decl_origin_self (tree decl)
21621 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21623 DECL_ABSTRACT_ORIGIN (decl) = decl;
21624 if (TREE_CODE (decl) == FUNCTION_DECL)
21626 tree arg;
21628 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21629 DECL_ABSTRACT_ORIGIN (arg) = arg;
21630 if (DECL_INITIAL (decl) != NULL_TREE
21631 && DECL_INITIAL (decl) != error_mark_node)
21632 set_block_origin_self (DECL_INITIAL (decl));
21637 /* Mark the early DIE for DECL as the abstract instance. */
21639 static void
21640 dwarf2out_abstract_function (tree decl)
21642 dw_die_ref old_die;
21644 /* Make sure we have the actual abstract inline, not a clone. */
21645 decl = DECL_ORIGIN (decl);
21647 if (DECL_IGNORED_P (decl))
21648 return;
21650 old_die = lookup_decl_die (decl);
21651 /* With early debug we always have an old DIE unless we are in LTO
21652 and the user did not compile but only link with debug. */
21653 if (in_lto_p && ! old_die)
21654 return;
21655 gcc_assert (old_die != NULL);
21656 if (get_AT (old_die, DW_AT_inline)
21657 || get_AT (old_die, DW_AT_abstract_origin))
21658 /* We've already generated the abstract instance. */
21659 return;
21661 /* Go ahead and put DW_AT_inline on the DIE. */
21662 if (DECL_DECLARED_INLINE_P (decl))
21664 if (cgraph_function_possibly_inlined_p (decl))
21665 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21666 else
21667 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21669 else
21671 if (cgraph_function_possibly_inlined_p (decl))
21672 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21673 else
21674 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21677 if (DECL_DECLARED_INLINE_P (decl)
21678 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21679 add_AT_flag (old_die, DW_AT_artificial, 1);
21681 set_decl_origin_self (decl);
21684 /* Helper function of premark_used_types() which gets called through
21685 htab_traverse.
21687 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21688 marked as unused by prune_unused_types. */
21690 bool
21691 premark_used_types_helper (tree const &type, void *)
21693 dw_die_ref die;
21695 die = lookup_type_die (type);
21696 if (die != NULL)
21697 die->die_perennial_p = 1;
21698 return true;
21701 /* Helper function of premark_types_used_by_global_vars which gets called
21702 through htab_traverse.
21704 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21705 marked as unused by prune_unused_types. The DIE of the type is marked
21706 only if the global variable using the type will actually be emitted. */
21709 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21710 void *)
21712 struct types_used_by_vars_entry *entry;
21713 dw_die_ref die;
21715 entry = (struct types_used_by_vars_entry *) *slot;
21716 gcc_assert (entry->type != NULL
21717 && entry->var_decl != NULL);
21718 die = lookup_type_die (entry->type);
21719 if (die)
21721 /* Ask cgraph if the global variable really is to be emitted.
21722 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21723 varpool_node *node = varpool_node::get (entry->var_decl);
21724 if (node && node->definition)
21726 die->die_perennial_p = 1;
21727 /* Keep the parent DIEs as well. */
21728 while ((die = die->die_parent) && die->die_perennial_p == 0)
21729 die->die_perennial_p = 1;
21732 return 1;
21735 /* Mark all members of used_types_hash as perennial. */
21737 static void
21738 premark_used_types (struct function *fun)
21740 if (fun && fun->used_types_hash)
21741 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21744 /* Mark all members of types_used_by_vars_entry as perennial. */
21746 static void
21747 premark_types_used_by_global_vars (void)
21749 if (types_used_by_vars_hash)
21750 types_used_by_vars_hash
21751 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21754 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21755 for CA_LOC call arg loc node. */
21757 static dw_die_ref
21758 gen_call_site_die (tree decl, dw_die_ref subr_die,
21759 struct call_arg_loc_node *ca_loc)
21761 dw_die_ref stmt_die = NULL, die;
21762 tree block = ca_loc->block;
21764 while (block
21765 && block != DECL_INITIAL (decl)
21766 && TREE_CODE (block) == BLOCK)
21768 stmt_die = BLOCK_DIE (block);
21769 if (stmt_die)
21770 break;
21771 block = BLOCK_SUPERCONTEXT (block);
21773 if (stmt_die == NULL)
21774 stmt_die = subr_die;
21775 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21776 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21777 if (ca_loc->tail_call_p)
21778 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21779 if (ca_loc->symbol_ref)
21781 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21782 if (tdie)
21783 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21784 else
21785 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21786 false);
21788 return die;
21791 /* Generate a DIE to represent a declared function (either file-scope or
21792 block-local). */
21794 static void
21795 gen_subprogram_die (tree decl, dw_die_ref context_die)
21797 tree origin = decl_ultimate_origin (decl);
21798 dw_die_ref subr_die;
21799 dw_die_ref old_die = lookup_decl_die (decl);
21801 /* This function gets called multiple times for different stages of
21802 the debug process. For example, for func() in this code:
21804 namespace S
21806 void func() { ... }
21809 ...we get called 4 times. Twice in early debug and twice in
21810 late debug:
21812 Early debug
21813 -----------
21815 1. Once while generating func() within the namespace. This is
21816 the declaration. The declaration bit below is set, as the
21817 context is the namespace.
21819 A new DIE will be generated with DW_AT_declaration set.
21821 2. Once for func() itself. This is the specification. The
21822 declaration bit below is clear as the context is the CU.
21824 We will use the cached DIE from (1) to create a new DIE with
21825 DW_AT_specification pointing to the declaration in (1).
21827 Late debug via rest_of_handle_final()
21828 -------------------------------------
21830 3. Once generating func() within the namespace. This is also the
21831 declaration, as in (1), but this time we will early exit below
21832 as we have a cached DIE and a declaration needs no additional
21833 annotations (no locations), as the source declaration line
21834 info is enough.
21836 4. Once for func() itself. As in (2), this is the specification,
21837 but this time we will re-use the cached DIE, and just annotate
21838 it with the location information that should now be available.
21840 For something without namespaces, but with abstract instances, we
21841 are also called a multiple times:
21843 class Base
21845 public:
21846 Base (); // constructor declaration (1)
21849 Base::Base () { } // constructor specification (2)
21851 Early debug
21852 -----------
21854 1. Once for the Base() constructor by virtue of it being a
21855 member of the Base class. This is done via
21856 rest_of_type_compilation.
21858 This is a declaration, so a new DIE will be created with
21859 DW_AT_declaration.
21861 2. Once for the Base() constructor definition, but this time
21862 while generating the abstract instance of the base
21863 constructor (__base_ctor) which is being generated via early
21864 debug of reachable functions.
21866 Even though we have a cached version of the declaration (1),
21867 we will create a DW_AT_specification of the declaration DIE
21868 in (1).
21870 3. Once for the __base_ctor itself, but this time, we generate
21871 an DW_AT_abstract_origin version of the DW_AT_specification in
21872 (2).
21874 Late debug via rest_of_handle_final
21875 -----------------------------------
21877 4. One final time for the __base_ctor (which will have a cached
21878 DIE with DW_AT_abstract_origin created in (3). This time,
21879 we will just annotate the location information now
21880 available.
21882 int declaration = (current_function_decl != decl
21883 || class_or_namespace_scope_p (context_die));
21885 /* Now that the C++ front end lazily declares artificial member fns, we
21886 might need to retrofit the declaration into its class. */
21887 if (!declaration && !origin && !old_die
21888 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
21889 && !class_or_namespace_scope_p (context_die)
21890 && debug_info_level > DINFO_LEVEL_TERSE)
21891 old_die = force_decl_die (decl);
21893 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
21894 if (origin != NULL)
21896 gcc_assert (!declaration || local_scope_p (context_die));
21898 /* Fixup die_parent for the abstract instance of a nested
21899 inline function. */
21900 if (old_die && old_die->die_parent == NULL)
21901 add_child_die (context_die, old_die);
21903 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
21905 /* If we have a DW_AT_abstract_origin we have a working
21906 cached version. */
21907 subr_die = old_die;
21909 else
21911 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
21912 add_abstract_origin_attribute (subr_die, origin);
21913 /* This is where the actual code for a cloned function is.
21914 Let's emit linkage name attribute for it. This helps
21915 debuggers to e.g, set breakpoints into
21916 constructors/destructors when the user asks "break
21917 K::K". */
21918 add_linkage_name (subr_die, decl);
21921 /* A cached copy, possibly from early dwarf generation. Reuse as
21922 much as possible. */
21923 else if (old_die)
21925 /* A declaration that has been previously dumped needs no
21926 additional information. */
21927 if (declaration)
21928 return;
21930 if (!get_AT_flag (old_die, DW_AT_declaration)
21931 /* We can have a normal definition following an inline one in the
21932 case of redefinition of GNU C extern inlines.
21933 It seems reasonable to use AT_specification in this case. */
21934 && !get_AT (old_die, DW_AT_inline))
21936 /* Detect and ignore this case, where we are trying to output
21937 something we have already output. */
21938 if (get_AT (old_die, DW_AT_low_pc)
21939 || get_AT (old_die, DW_AT_ranges))
21940 return;
21942 /* If we have no location information, this must be a
21943 partially generated DIE from early dwarf generation.
21944 Fall through and generate it. */
21947 /* If the definition comes from the same place as the declaration,
21948 maybe use the old DIE. We always want the DIE for this function
21949 that has the *_pc attributes to be under comp_unit_die so the
21950 debugger can find it. We also need to do this for abstract
21951 instances of inlines, since the spec requires the out-of-line copy
21952 to have the same parent. For local class methods, this doesn't
21953 apply; we just use the old DIE. */
21954 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21955 struct dwarf_file_data * file_index = lookup_filename (s.file);
21956 if ((is_cu_die (old_die->die_parent)
21957 /* This condition fixes the inconsistency/ICE with the
21958 following Fortran test (or some derivative thereof) while
21959 building libgfortran:
21961 module some_m
21962 contains
21963 logical function funky (FLAG)
21964 funky = .true.
21965 end function
21966 end module
21968 || (old_die->die_parent
21969 && old_die->die_parent->die_tag == DW_TAG_module)
21970 || context_die == NULL)
21971 && (DECL_ARTIFICIAL (decl)
21972 /* The location attributes may be in the abstract origin
21973 which in the case of LTO might be not available to
21974 look at. */
21975 || get_AT (old_die, DW_AT_abstract_origin)
21976 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
21977 && (get_AT_unsigned (old_die, DW_AT_decl_line)
21978 == (unsigned) s.line)
21979 && (!debug_column_info
21980 || s.column == 0
21981 || (get_AT_unsigned (old_die, DW_AT_decl_column)
21982 == (unsigned) s.column)))))
21984 subr_die = old_die;
21986 /* Clear out the declaration attribute, but leave the
21987 parameters so they can be augmented with location
21988 information later. Unless this was a declaration, in
21989 which case, wipe out the nameless parameters and recreate
21990 them further down. */
21991 if (remove_AT (subr_die, DW_AT_declaration))
21994 remove_AT (subr_die, DW_AT_object_pointer);
21995 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
21998 /* Make a specification pointing to the previously built
21999 declaration. */
22000 else
22002 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22003 add_AT_specification (subr_die, old_die);
22004 add_pubname (decl, subr_die);
22005 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22006 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22007 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22008 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22009 if (debug_column_info
22010 && s.column
22011 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22012 != (unsigned) s.column))
22013 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22015 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22016 emit the real type on the definition die. */
22017 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22019 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22020 if (die == auto_die || die == decltype_auto_die)
22021 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22022 TYPE_UNQUALIFIED, false, context_die);
22025 /* When we process the method declaration, we haven't seen
22026 the out-of-class defaulted definition yet, so we have to
22027 recheck now. */
22028 if ((dwarf_version >= 5 || ! dwarf_strict)
22029 && !get_AT (subr_die, DW_AT_defaulted))
22031 int defaulted
22032 = lang_hooks.decls.decl_dwarf_attribute (decl,
22033 DW_AT_defaulted);
22034 if (defaulted != -1)
22036 /* Other values must have been handled before. */
22037 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22038 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22043 /* Create a fresh DIE for anything else. */
22044 else
22046 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22048 if (TREE_PUBLIC (decl))
22049 add_AT_flag (subr_die, DW_AT_external, 1);
22051 add_name_and_src_coords_attributes (subr_die, decl);
22052 add_pubname (decl, subr_die);
22053 if (debug_info_level > DINFO_LEVEL_TERSE)
22055 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22056 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22057 TYPE_UNQUALIFIED, false, context_die);
22060 add_pure_or_virtual_attribute (subr_die, decl);
22061 if (DECL_ARTIFICIAL (decl))
22062 add_AT_flag (subr_die, DW_AT_artificial, 1);
22064 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22065 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22067 add_alignment_attribute (subr_die, decl);
22069 add_accessibility_attribute (subr_die, decl);
22072 /* Unless we have an existing non-declaration DIE, equate the new
22073 DIE. */
22074 if (!old_die || is_declaration_die (old_die))
22075 equate_decl_number_to_die (decl, subr_die);
22077 if (declaration)
22079 if (!old_die || !get_AT (old_die, DW_AT_inline))
22081 add_AT_flag (subr_die, DW_AT_declaration, 1);
22083 /* If this is an explicit function declaration then generate
22084 a DW_AT_explicit attribute. */
22085 if ((dwarf_version >= 3 || !dwarf_strict)
22086 && lang_hooks.decls.decl_dwarf_attribute (decl,
22087 DW_AT_explicit) == 1)
22088 add_AT_flag (subr_die, DW_AT_explicit, 1);
22090 /* If this is a C++11 deleted special function member then generate
22091 a DW_AT_deleted attribute. */
22092 if ((dwarf_version >= 5 || !dwarf_strict)
22093 && lang_hooks.decls.decl_dwarf_attribute (decl,
22094 DW_AT_deleted) == 1)
22095 add_AT_flag (subr_die, DW_AT_deleted, 1);
22097 /* If this is a C++11 defaulted special function member then
22098 generate a DW_AT_defaulted attribute. */
22099 if (dwarf_version >= 5 || !dwarf_strict)
22101 int defaulted
22102 = lang_hooks.decls.decl_dwarf_attribute (decl,
22103 DW_AT_defaulted);
22104 if (defaulted != -1)
22105 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22108 /* If this is a C++11 non-static member function with & ref-qualifier
22109 then generate a DW_AT_reference attribute. */
22110 if ((dwarf_version >= 5 || !dwarf_strict)
22111 && lang_hooks.decls.decl_dwarf_attribute (decl,
22112 DW_AT_reference) == 1)
22113 add_AT_flag (subr_die, DW_AT_reference, 1);
22115 /* If this is a C++11 non-static member function with &&
22116 ref-qualifier then generate a DW_AT_reference attribute. */
22117 if ((dwarf_version >= 5 || !dwarf_strict)
22118 && lang_hooks.decls.decl_dwarf_attribute (decl,
22119 DW_AT_rvalue_reference)
22120 == 1)
22121 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22124 /* For non DECL_EXTERNALs, if range information is available, fill
22125 the DIE with it. */
22126 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22128 HOST_WIDE_INT cfa_fb_offset;
22130 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22132 if (!crtl->has_bb_partition)
22134 dw_fde_ref fde = fun->fde;
22135 if (fde->dw_fde_begin)
22137 /* We have already generated the labels. */
22138 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22139 fde->dw_fde_end, false);
22141 else
22143 /* Create start/end labels and add the range. */
22144 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22145 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22146 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22147 current_function_funcdef_no);
22148 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22149 current_function_funcdef_no);
22150 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22151 false);
22154 #if VMS_DEBUGGING_INFO
22155 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22156 Section 2.3 Prologue and Epilogue Attributes:
22157 When a breakpoint is set on entry to a function, it is generally
22158 desirable for execution to be suspended, not on the very first
22159 instruction of the function, but rather at a point after the
22160 function's frame has been set up, after any language defined local
22161 declaration processing has been completed, and before execution of
22162 the first statement of the function begins. Debuggers generally
22163 cannot properly determine where this point is. Similarly for a
22164 breakpoint set on exit from a function. The prologue and epilogue
22165 attributes allow a compiler to communicate the location(s) to use. */
22168 if (fde->dw_fde_vms_end_prologue)
22169 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22170 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22172 if (fde->dw_fde_vms_begin_epilogue)
22173 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22174 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22176 #endif
22179 else
22181 /* Generate pubnames entries for the split function code ranges. */
22182 dw_fde_ref fde = fun->fde;
22184 if (fde->dw_fde_second_begin)
22186 if (dwarf_version >= 3 || !dwarf_strict)
22188 /* We should use ranges for non-contiguous code section
22189 addresses. Use the actual code range for the initial
22190 section, since the HOT/COLD labels might precede an
22191 alignment offset. */
22192 bool range_list_added = false;
22193 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22194 fde->dw_fde_end, &range_list_added,
22195 false);
22196 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22197 fde->dw_fde_second_end,
22198 &range_list_added, false);
22199 if (range_list_added)
22200 add_ranges (NULL);
22202 else
22204 /* There is no real support in DW2 for this .. so we make
22205 a work-around. First, emit the pub name for the segment
22206 containing the function label. Then make and emit a
22207 simplified subprogram DIE for the second segment with the
22208 name pre-fixed by __hot/cold_sect_of_. We use the same
22209 linkage name for the second die so that gdb will find both
22210 sections when given "b foo". */
22211 const char *name = NULL;
22212 tree decl_name = DECL_NAME (decl);
22213 dw_die_ref seg_die;
22215 /* Do the 'primary' section. */
22216 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22217 fde->dw_fde_end, false);
22219 /* Build a minimal DIE for the secondary section. */
22220 seg_die = new_die (DW_TAG_subprogram,
22221 subr_die->die_parent, decl);
22223 if (TREE_PUBLIC (decl))
22224 add_AT_flag (seg_die, DW_AT_external, 1);
22226 if (decl_name != NULL
22227 && IDENTIFIER_POINTER (decl_name) != NULL)
22229 name = dwarf2_name (decl, 1);
22230 if (! DECL_ARTIFICIAL (decl))
22231 add_src_coords_attributes (seg_die, decl);
22233 add_linkage_name (seg_die, decl);
22235 gcc_assert (name != NULL);
22236 add_pure_or_virtual_attribute (seg_die, decl);
22237 if (DECL_ARTIFICIAL (decl))
22238 add_AT_flag (seg_die, DW_AT_artificial, 1);
22240 name = concat ("__second_sect_of_", name, NULL);
22241 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22242 fde->dw_fde_second_end, false);
22243 add_name_attribute (seg_die, name);
22244 if (want_pubnames ())
22245 add_pubname_string (name, seg_die);
22248 else
22249 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22250 false);
22253 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22255 /* We define the "frame base" as the function's CFA. This is more
22256 convenient for several reasons: (1) It's stable across the prologue
22257 and epilogue, which makes it better than just a frame pointer,
22258 (2) With dwarf3, there exists a one-byte encoding that allows us
22259 to reference the .debug_frame data by proxy, but failing that,
22260 (3) We can at least reuse the code inspection and interpretation
22261 code that determines the CFA position at various points in the
22262 function. */
22263 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22265 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22266 add_AT_loc (subr_die, DW_AT_frame_base, op);
22268 else
22270 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22271 if (list->dw_loc_next)
22272 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22273 else
22274 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22277 /* Compute a displacement from the "steady-state frame pointer" to
22278 the CFA. The former is what all stack slots and argument slots
22279 will reference in the rtl; the latter is what we've told the
22280 debugger about. We'll need to adjust all frame_base references
22281 by this displacement. */
22282 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22284 if (fun->static_chain_decl)
22286 /* DWARF requires here a location expression that computes the
22287 address of the enclosing subprogram's frame base. The machinery
22288 in tree-nested.c is supposed to store this specific address in the
22289 last field of the FRAME record. */
22290 const tree frame_type
22291 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22292 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22294 tree fb_expr
22295 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22296 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22297 fb_expr, fb_decl, NULL_TREE);
22299 add_AT_location_description (subr_die, DW_AT_static_link,
22300 loc_list_from_tree (fb_expr, 0, NULL));
22303 resolve_variable_values ();
22306 /* Generate child dies for template paramaters. */
22307 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22308 gen_generic_params_dies (decl);
22310 /* Now output descriptions of the arguments for this function. This gets
22311 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22312 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22313 `...' at the end of the formal parameter list. In order to find out if
22314 there was a trailing ellipsis or not, we must instead look at the type
22315 associated with the FUNCTION_DECL. This will be a node of type
22316 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22317 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22318 an ellipsis at the end. */
22320 /* In the case where we are describing a mere function declaration, all we
22321 need to do here (and all we *can* do here) is to describe the *types* of
22322 its formal parameters. */
22323 if (debug_info_level <= DINFO_LEVEL_TERSE)
22325 else if (declaration)
22326 gen_formal_types_die (decl, subr_die);
22327 else
22329 /* Generate DIEs to represent all known formal parameters. */
22330 tree parm = DECL_ARGUMENTS (decl);
22331 tree generic_decl = early_dwarf
22332 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22333 tree generic_decl_parm = generic_decl
22334 ? DECL_ARGUMENTS (generic_decl)
22335 : NULL;
22337 /* Now we want to walk the list of parameters of the function and
22338 emit their relevant DIEs.
22340 We consider the case of DECL being an instance of a generic function
22341 as well as it being a normal function.
22343 If DECL is an instance of a generic function we walk the
22344 parameters of the generic function declaration _and_ the parameters of
22345 DECL itself. This is useful because we want to emit specific DIEs for
22346 function parameter packs and those are declared as part of the
22347 generic function declaration. In that particular case,
22348 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22349 That DIE has children DIEs representing the set of arguments
22350 of the pack. Note that the set of pack arguments can be empty.
22351 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22352 children DIE.
22354 Otherwise, we just consider the parameters of DECL. */
22355 while (generic_decl_parm || parm)
22357 if (generic_decl_parm
22358 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22359 gen_formal_parameter_pack_die (generic_decl_parm,
22360 parm, subr_die,
22361 &parm);
22362 else if (parm && !POINTER_BOUNDS_P (parm))
22364 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22366 if (early_dwarf
22367 && parm == DECL_ARGUMENTS (decl)
22368 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22369 && parm_die
22370 && (dwarf_version >= 3 || !dwarf_strict))
22371 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22373 parm = DECL_CHAIN (parm);
22375 else if (parm)
22376 parm = DECL_CHAIN (parm);
22378 if (generic_decl_parm)
22379 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22382 /* Decide whether we need an unspecified_parameters DIE at the end.
22383 There are 2 more cases to do this for: 1) the ansi ... declaration -
22384 this is detectable when the end of the arg list is not a
22385 void_type_node 2) an unprototyped function declaration (not a
22386 definition). This just means that we have no info about the
22387 parameters at all. */
22388 if (early_dwarf)
22390 if (prototype_p (TREE_TYPE (decl)))
22392 /* This is the prototyped case, check for.... */
22393 if (stdarg_p (TREE_TYPE (decl)))
22394 gen_unspecified_parameters_die (decl, subr_die);
22396 else if (DECL_INITIAL (decl) == NULL_TREE)
22397 gen_unspecified_parameters_die (decl, subr_die);
22401 if (subr_die != old_die)
22402 /* Add the calling convention attribute if requested. */
22403 add_calling_convention_attribute (subr_die, decl);
22405 /* Output Dwarf info for all of the stuff within the body of the function
22406 (if it has one - it may be just a declaration).
22408 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22409 a function. This BLOCK actually represents the outermost binding contour
22410 for the function, i.e. the contour in which the function's formal
22411 parameters and labels get declared. Curiously, it appears that the front
22412 end doesn't actually put the PARM_DECL nodes for the current function onto
22413 the BLOCK_VARS list for this outer scope, but are strung off of the
22414 DECL_ARGUMENTS list for the function instead.
22416 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22417 the LABEL_DECL nodes for the function however, and we output DWARF info
22418 for those in decls_for_scope. Just within the `outer_scope' there will be
22419 a BLOCK node representing the function's outermost pair of curly braces,
22420 and any blocks used for the base and member initializers of a C++
22421 constructor function. */
22422 tree outer_scope = DECL_INITIAL (decl);
22423 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22425 int call_site_note_count = 0;
22426 int tail_call_site_note_count = 0;
22428 /* Emit a DW_TAG_variable DIE for a named return value. */
22429 if (DECL_NAME (DECL_RESULT (decl)))
22430 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22432 /* The first time through decls_for_scope we will generate the
22433 DIEs for the locals. The second time, we fill in the
22434 location info. */
22435 decls_for_scope (outer_scope, subr_die);
22437 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22439 struct call_arg_loc_node *ca_loc;
22440 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22442 dw_die_ref die = NULL;
22443 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22444 rtx arg, next_arg;
22446 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22447 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22448 : NULL_RTX);
22449 arg; arg = next_arg)
22451 dw_loc_descr_ref reg, val;
22452 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22453 dw_die_ref cdie, tdie = NULL;
22455 next_arg = XEXP (arg, 1);
22456 if (REG_P (XEXP (XEXP (arg, 0), 0))
22457 && next_arg
22458 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22459 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22460 && REGNO (XEXP (XEXP (arg, 0), 0))
22461 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22462 next_arg = XEXP (next_arg, 1);
22463 if (mode == VOIDmode)
22465 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22466 if (mode == VOIDmode)
22467 mode = GET_MODE (XEXP (arg, 0));
22469 if (mode == VOIDmode || mode == BLKmode)
22470 continue;
22471 /* Get dynamic information about call target only if we
22472 have no static information: we cannot generate both
22473 DW_AT_call_origin and DW_AT_call_target
22474 attributes. */
22475 if (ca_loc->symbol_ref == NULL_RTX)
22477 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22479 tloc = XEXP (XEXP (arg, 0), 1);
22480 continue;
22482 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22483 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22485 tlocc = XEXP (XEXP (arg, 0), 1);
22486 continue;
22489 reg = NULL;
22490 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22491 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22492 VAR_INIT_STATUS_INITIALIZED);
22493 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22495 rtx mem = XEXP (XEXP (arg, 0), 0);
22496 reg = mem_loc_descriptor (XEXP (mem, 0),
22497 get_address_mode (mem),
22498 GET_MODE (mem),
22499 VAR_INIT_STATUS_INITIALIZED);
22501 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22502 == DEBUG_PARAMETER_REF)
22504 tree tdecl
22505 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22506 tdie = lookup_decl_die (tdecl);
22507 if (tdie == NULL)
22508 continue;
22510 else
22511 continue;
22512 if (reg == NULL
22513 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22514 != DEBUG_PARAMETER_REF)
22515 continue;
22516 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22517 VOIDmode,
22518 VAR_INIT_STATUS_INITIALIZED);
22519 if (val == NULL)
22520 continue;
22521 if (die == NULL)
22522 die = gen_call_site_die (decl, subr_die, ca_loc);
22523 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22524 NULL_TREE);
22525 if (reg != NULL)
22526 add_AT_loc (cdie, DW_AT_location, reg);
22527 else if (tdie != NULL)
22528 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22529 tdie);
22530 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22531 if (next_arg != XEXP (arg, 1))
22533 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22534 if (mode == VOIDmode)
22535 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22536 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22537 0), 1),
22538 mode, VOIDmode,
22539 VAR_INIT_STATUS_INITIALIZED);
22540 if (val != NULL)
22541 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22542 val);
22545 if (die == NULL
22546 && (ca_loc->symbol_ref || tloc))
22547 die = gen_call_site_die (decl, subr_die, ca_loc);
22548 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22550 dw_loc_descr_ref tval = NULL;
22552 if (tloc != NULL_RTX)
22553 tval = mem_loc_descriptor (tloc,
22554 GET_MODE (tloc) == VOIDmode
22555 ? Pmode : GET_MODE (tloc),
22556 VOIDmode,
22557 VAR_INIT_STATUS_INITIALIZED);
22558 if (tval)
22559 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22560 else if (tlocc != NULL_RTX)
22562 tval = mem_loc_descriptor (tlocc,
22563 GET_MODE (tlocc) == VOIDmode
22564 ? Pmode : GET_MODE (tlocc),
22565 VOIDmode,
22566 VAR_INIT_STATUS_INITIALIZED);
22567 if (tval)
22568 add_AT_loc (die,
22569 dwarf_AT (DW_AT_call_target_clobbered),
22570 tval);
22573 if (die != NULL)
22575 call_site_note_count++;
22576 if (ca_loc->tail_call_p)
22577 tail_call_site_note_count++;
22581 call_arg_locations = NULL;
22582 call_arg_loc_last = NULL;
22583 if (tail_call_site_count >= 0
22584 && tail_call_site_count == tail_call_site_note_count
22585 && (!dwarf_strict || dwarf_version >= 5))
22587 if (call_site_count >= 0
22588 && call_site_count == call_site_note_count)
22589 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22590 else
22591 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22593 call_site_count = -1;
22594 tail_call_site_count = -1;
22597 /* Mark used types after we have created DIEs for the functions scopes. */
22598 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22601 /* Returns a hash value for X (which really is a die_struct). */
22603 hashval_t
22604 block_die_hasher::hash (die_struct *d)
22606 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22609 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22610 as decl_id and die_parent of die_struct Y. */
22612 bool
22613 block_die_hasher::equal (die_struct *x, die_struct *y)
22615 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22618 /* Return TRUE if DECL, which may have been previously generated as
22619 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22620 true if decl (or its origin) is either an extern declaration or a
22621 class/namespace scoped declaration.
22623 The declare_in_namespace support causes us to get two DIEs for one
22624 variable, both of which are declarations. We want to avoid
22625 considering one to be a specification, so we must test for
22626 DECLARATION and DW_AT_declaration. */
22627 static inline bool
22628 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22630 return (old_die && TREE_STATIC (decl) && !declaration
22631 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22634 /* Return true if DECL is a local static. */
22636 static inline bool
22637 local_function_static (tree decl)
22639 gcc_assert (VAR_P (decl));
22640 return TREE_STATIC (decl)
22641 && DECL_CONTEXT (decl)
22642 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22645 /* Generate a DIE to represent a declared data object.
22646 Either DECL or ORIGIN must be non-null. */
22648 static void
22649 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22651 HOST_WIDE_INT off = 0;
22652 tree com_decl;
22653 tree decl_or_origin = decl ? decl : origin;
22654 tree ultimate_origin;
22655 dw_die_ref var_die;
22656 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22657 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22658 || class_or_namespace_scope_p (context_die));
22659 bool specialization_p = false;
22660 bool no_linkage_name = false;
22662 /* While C++ inline static data members have definitions inside of the
22663 class, force the first DIE to be a declaration, then let gen_member_die
22664 reparent it to the class context and call gen_variable_die again
22665 to create the outside of the class DIE for the definition. */
22666 if (!declaration
22667 && old_die == NULL
22668 && decl
22669 && DECL_CONTEXT (decl)
22670 && TYPE_P (DECL_CONTEXT (decl))
22671 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22673 declaration = true;
22674 if (dwarf_version < 5)
22675 no_linkage_name = true;
22678 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22679 if (decl || ultimate_origin)
22680 origin = ultimate_origin;
22681 com_decl = fortran_common (decl_or_origin, &off);
22683 /* Symbol in common gets emitted as a child of the common block, in the form
22684 of a data member. */
22685 if (com_decl)
22687 dw_die_ref com_die;
22688 dw_loc_list_ref loc = NULL;
22689 die_node com_die_arg;
22691 var_die = lookup_decl_die (decl_or_origin);
22692 if (var_die)
22694 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22696 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22697 if (loc)
22699 if (off)
22701 /* Optimize the common case. */
22702 if (single_element_loc_list_p (loc)
22703 && loc->expr->dw_loc_opc == DW_OP_addr
22704 && loc->expr->dw_loc_next == NULL
22705 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22706 == SYMBOL_REF)
22708 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22709 loc->expr->dw_loc_oprnd1.v.val_addr
22710 = plus_constant (GET_MODE (x), x , off);
22712 else
22713 loc_list_plus_const (loc, off);
22715 add_AT_location_description (var_die, DW_AT_location, loc);
22716 remove_AT (var_die, DW_AT_declaration);
22719 return;
22722 if (common_block_die_table == NULL)
22723 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22725 com_die_arg.decl_id = DECL_UID (com_decl);
22726 com_die_arg.die_parent = context_die;
22727 com_die = common_block_die_table->find (&com_die_arg);
22728 if (! early_dwarf)
22729 loc = loc_list_from_tree (com_decl, 2, NULL);
22730 if (com_die == NULL)
22732 const char *cnam
22733 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22734 die_node **slot;
22736 com_die = new_die (DW_TAG_common_block, context_die, decl);
22737 add_name_and_src_coords_attributes (com_die, com_decl);
22738 if (loc)
22740 add_AT_location_description (com_die, DW_AT_location, loc);
22741 /* Avoid sharing the same loc descriptor between
22742 DW_TAG_common_block and DW_TAG_variable. */
22743 loc = loc_list_from_tree (com_decl, 2, NULL);
22745 else if (DECL_EXTERNAL (decl_or_origin))
22746 add_AT_flag (com_die, DW_AT_declaration, 1);
22747 if (want_pubnames ())
22748 add_pubname_string (cnam, com_die); /* ??? needed? */
22749 com_die->decl_id = DECL_UID (com_decl);
22750 slot = common_block_die_table->find_slot (com_die, INSERT);
22751 *slot = com_die;
22753 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22755 add_AT_location_description (com_die, DW_AT_location, loc);
22756 loc = loc_list_from_tree (com_decl, 2, NULL);
22757 remove_AT (com_die, DW_AT_declaration);
22759 var_die = new_die (DW_TAG_variable, com_die, decl);
22760 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22761 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22762 decl_quals (decl_or_origin), false,
22763 context_die);
22764 add_alignment_attribute (var_die, decl);
22765 add_AT_flag (var_die, DW_AT_external, 1);
22766 if (loc)
22768 if (off)
22770 /* Optimize the common case. */
22771 if (single_element_loc_list_p (loc)
22772 && loc->expr->dw_loc_opc == DW_OP_addr
22773 && loc->expr->dw_loc_next == NULL
22774 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22776 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22777 loc->expr->dw_loc_oprnd1.v.val_addr
22778 = plus_constant (GET_MODE (x), x, off);
22780 else
22781 loc_list_plus_const (loc, off);
22783 add_AT_location_description (var_die, DW_AT_location, loc);
22785 else if (DECL_EXTERNAL (decl_or_origin))
22786 add_AT_flag (var_die, DW_AT_declaration, 1);
22787 if (decl)
22788 equate_decl_number_to_die (decl, var_die);
22789 return;
22792 if (old_die)
22794 if (declaration)
22796 /* A declaration that has been previously dumped, needs no
22797 further annotations, since it doesn't need location on
22798 the second pass. */
22799 return;
22801 else if (decl_will_get_specification_p (old_die, decl, declaration)
22802 && !get_AT (old_die, DW_AT_specification))
22804 /* Fall-thru so we can make a new variable die along with a
22805 DW_AT_specification. */
22807 else if (origin && old_die->die_parent != context_die)
22809 /* If we will be creating an inlined instance, we need a
22810 new DIE that will get annotated with
22811 DW_AT_abstract_origin. Clear things so we can get a
22812 new DIE. */
22813 gcc_assert (!DECL_ABSTRACT_P (decl));
22814 old_die = NULL;
22816 else
22818 /* If a DIE was dumped early, it still needs location info.
22819 Skip to where we fill the location bits. */
22820 var_die = old_die;
22822 /* ??? In LTRANS we cannot annotate early created variably
22823 modified type DIEs without copying them and adjusting all
22824 references to them. Thus we dumped them again, also add a
22825 reference to them. */
22826 tree type = TREE_TYPE (decl_or_origin);
22827 if (in_lto_p
22828 && variably_modified_type_p
22829 (type, decl_function_context (decl_or_origin)))
22831 if (decl_by_reference_p (decl_or_origin))
22832 add_type_attribute (var_die, TREE_TYPE (type),
22833 TYPE_UNQUALIFIED, false, context_die);
22834 else
22835 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22836 false, context_die);
22839 goto gen_variable_die_location;
22843 /* For static data members, the declaration in the class is supposed
22844 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
22845 also in DWARF2; the specification should still be DW_TAG_variable
22846 referencing the DW_TAG_member DIE. */
22847 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
22848 var_die = new_die (DW_TAG_member, context_die, decl);
22849 else
22850 var_die = new_die (DW_TAG_variable, context_die, decl);
22852 if (origin != NULL)
22853 add_abstract_origin_attribute (var_die, origin);
22855 /* Loop unrolling can create multiple blocks that refer to the same
22856 static variable, so we must test for the DW_AT_declaration flag.
22858 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
22859 copy decls and set the DECL_ABSTRACT_P flag on them instead of
22860 sharing them.
22862 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
22863 else if (decl_will_get_specification_p (old_die, decl, declaration))
22865 /* This is a definition of a C++ class level static. */
22866 add_AT_specification (var_die, old_die);
22867 specialization_p = true;
22868 if (DECL_NAME (decl))
22870 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22871 struct dwarf_file_data * file_index = lookup_filename (s.file);
22873 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22874 add_AT_file (var_die, DW_AT_decl_file, file_index);
22876 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22877 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
22879 if (debug_column_info
22880 && s.column
22881 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22882 != (unsigned) s.column))
22883 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
22885 if (old_die->die_tag == DW_TAG_member)
22886 add_linkage_name (var_die, decl);
22889 else
22890 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
22892 if ((origin == NULL && !specialization_p)
22893 || (origin != NULL
22894 && !DECL_ABSTRACT_P (decl_or_origin)
22895 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
22896 decl_function_context
22897 (decl_or_origin))))
22899 tree type = TREE_TYPE (decl_or_origin);
22901 if (decl_by_reference_p (decl_or_origin))
22902 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
22903 context_die);
22904 else
22905 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
22906 context_die);
22909 if (origin == NULL && !specialization_p)
22911 if (TREE_PUBLIC (decl))
22912 add_AT_flag (var_die, DW_AT_external, 1);
22914 if (DECL_ARTIFICIAL (decl))
22915 add_AT_flag (var_die, DW_AT_artificial, 1);
22917 add_alignment_attribute (var_die, decl);
22919 add_accessibility_attribute (var_die, decl);
22922 if (declaration)
22923 add_AT_flag (var_die, DW_AT_declaration, 1);
22925 if (decl && (DECL_ABSTRACT_P (decl)
22926 || !old_die || is_declaration_die (old_die)))
22927 equate_decl_number_to_die (decl, var_die);
22929 gen_variable_die_location:
22930 if (! declaration
22931 && (! DECL_ABSTRACT_P (decl_or_origin)
22932 /* Local static vars are shared between all clones/inlines,
22933 so emit DW_AT_location on the abstract DIE if DECL_RTL is
22934 already set. */
22935 || (VAR_P (decl_or_origin)
22936 && TREE_STATIC (decl_or_origin)
22937 && DECL_RTL_SET_P (decl_or_origin))))
22939 if (early_dwarf)
22940 add_pubname (decl_or_origin, var_die);
22941 else
22942 add_location_or_const_value_attribute (var_die, decl_or_origin,
22943 decl == NULL);
22945 else
22946 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
22948 if ((dwarf_version >= 4 || !dwarf_strict)
22949 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22950 DW_AT_const_expr) == 1
22951 && !get_AT (var_die, DW_AT_const_expr)
22952 && !specialization_p)
22953 add_AT_flag (var_die, DW_AT_const_expr, 1);
22955 if (!dwarf_strict)
22957 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
22958 DW_AT_inline);
22959 if (inl != -1
22960 && !get_AT (var_die, DW_AT_inline)
22961 && !specialization_p)
22962 add_AT_unsigned (var_die, DW_AT_inline, inl);
22966 /* Generate a DIE to represent a named constant. */
22968 static void
22969 gen_const_die (tree decl, dw_die_ref context_die)
22971 dw_die_ref const_die;
22972 tree type = TREE_TYPE (decl);
22974 const_die = lookup_decl_die (decl);
22975 if (const_die)
22976 return;
22978 const_die = new_die (DW_TAG_constant, context_die, decl);
22979 equate_decl_number_to_die (decl, const_die);
22980 add_name_and_src_coords_attributes (const_die, decl);
22981 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
22982 if (TREE_PUBLIC (decl))
22983 add_AT_flag (const_die, DW_AT_external, 1);
22984 if (DECL_ARTIFICIAL (decl))
22985 add_AT_flag (const_die, DW_AT_artificial, 1);
22986 tree_add_const_value_attribute_for_decl (const_die, decl);
22989 /* Generate a DIE to represent a label identifier. */
22991 static void
22992 gen_label_die (tree decl, dw_die_ref context_die)
22994 tree origin = decl_ultimate_origin (decl);
22995 dw_die_ref lbl_die = lookup_decl_die (decl);
22996 rtx insn;
22997 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22999 if (!lbl_die)
23001 lbl_die = new_die (DW_TAG_label, context_die, decl);
23002 equate_decl_number_to_die (decl, lbl_die);
23004 if (origin != NULL)
23005 add_abstract_origin_attribute (lbl_die, origin);
23006 else
23007 add_name_and_src_coords_attributes (lbl_die, decl);
23010 if (DECL_ABSTRACT_P (decl))
23011 equate_decl_number_to_die (decl, lbl_die);
23012 else if (! early_dwarf)
23014 insn = DECL_RTL_IF_SET (decl);
23016 /* Deleted labels are programmer specified labels which have been
23017 eliminated because of various optimizations. We still emit them
23018 here so that it is possible to put breakpoints on them. */
23019 if (insn
23020 && (LABEL_P (insn)
23021 || ((NOTE_P (insn)
23022 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23024 /* When optimization is enabled (via -O) some parts of the compiler
23025 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23026 represent source-level labels which were explicitly declared by
23027 the user. This really shouldn't be happening though, so catch
23028 it if it ever does happen. */
23029 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23031 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23032 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23034 else if (insn
23035 && NOTE_P (insn)
23036 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23037 && CODE_LABEL_NUMBER (insn) != -1)
23039 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23040 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23045 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23046 attributes to the DIE for a block STMT, to describe where the inlined
23047 function was called from. This is similar to add_src_coords_attributes. */
23049 static inline void
23050 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23052 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23054 if (dwarf_version >= 3 || !dwarf_strict)
23056 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23057 add_AT_unsigned (die, DW_AT_call_line, s.line);
23058 if (debug_column_info && s.column)
23059 add_AT_unsigned (die, DW_AT_call_column, s.column);
23064 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23065 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23067 static inline void
23068 add_high_low_attributes (tree stmt, dw_die_ref die)
23070 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23072 if (BLOCK_FRAGMENT_CHAIN (stmt)
23073 && (dwarf_version >= 3 || !dwarf_strict))
23075 tree chain, superblock = NULL_TREE;
23076 dw_die_ref pdie;
23077 dw_attr_node *attr = NULL;
23079 if (inlined_function_outer_scope_p (stmt))
23081 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23082 BLOCK_NUMBER (stmt));
23083 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23086 /* Optimize duplicate .debug_ranges lists or even tails of
23087 lists. If this BLOCK has same ranges as its supercontext,
23088 lookup DW_AT_ranges attribute in the supercontext (and
23089 recursively so), verify that the ranges_table contains the
23090 right values and use it instead of adding a new .debug_range. */
23091 for (chain = stmt, pdie = die;
23092 BLOCK_SAME_RANGE (chain);
23093 chain = BLOCK_SUPERCONTEXT (chain))
23095 dw_attr_node *new_attr;
23097 pdie = pdie->die_parent;
23098 if (pdie == NULL)
23099 break;
23100 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23101 break;
23102 new_attr = get_AT (pdie, DW_AT_ranges);
23103 if (new_attr == NULL
23104 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23105 break;
23106 attr = new_attr;
23107 superblock = BLOCK_SUPERCONTEXT (chain);
23109 if (attr != NULL
23110 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23111 == BLOCK_NUMBER (superblock))
23112 && BLOCK_FRAGMENT_CHAIN (superblock))
23114 unsigned long off = attr->dw_attr_val.v.val_offset;
23115 unsigned long supercnt = 0, thiscnt = 0;
23116 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23117 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23119 ++supercnt;
23120 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23121 == BLOCK_NUMBER (chain));
23123 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23124 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23125 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23126 ++thiscnt;
23127 gcc_assert (supercnt >= thiscnt);
23128 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23129 false);
23130 note_rnglist_head (off + supercnt - thiscnt);
23131 return;
23134 unsigned int offset = add_ranges (stmt, true);
23135 add_AT_range_list (die, DW_AT_ranges, offset, false);
23136 note_rnglist_head (offset);
23138 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23139 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23142 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23143 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23144 chain = BLOCK_FRAGMENT_CHAIN (chain);
23146 while (chain);
23147 add_ranges (NULL);
23149 else
23151 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23152 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23153 BLOCK_NUMBER (stmt));
23154 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23155 BLOCK_NUMBER (stmt));
23156 add_AT_low_high_pc (die, label, label_high, false);
23160 /* Generate a DIE for a lexical block. */
23162 static void
23163 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23165 dw_die_ref old_die = BLOCK_DIE (stmt);
23166 dw_die_ref stmt_die = NULL;
23167 if (!old_die)
23169 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23170 BLOCK_DIE (stmt) = stmt_die;
23173 if (BLOCK_ABSTRACT (stmt))
23175 if (old_die)
23177 /* This must have been generated early and it won't even
23178 need location information since it's a DW_AT_inline
23179 function. */
23180 if (flag_checking)
23181 for (dw_die_ref c = context_die; c; c = c->die_parent)
23182 if (c->die_tag == DW_TAG_inlined_subroutine
23183 || c->die_tag == DW_TAG_subprogram)
23185 gcc_assert (get_AT (c, DW_AT_inline));
23186 break;
23188 return;
23191 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23193 /* If this is an inlined instance, create a new lexical die for
23194 anything below to attach DW_AT_abstract_origin to. */
23195 if (old_die)
23197 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23198 BLOCK_DIE (stmt) = stmt_die;
23199 old_die = NULL;
23202 tree origin = block_ultimate_origin (stmt);
23203 if (origin != NULL_TREE && origin != stmt)
23204 add_abstract_origin_attribute (stmt_die, origin);
23207 if (old_die)
23208 stmt_die = old_die;
23210 /* A non abstract block whose blocks have already been reordered
23211 should have the instruction range for this block. If so, set the
23212 high/low attributes. */
23213 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23215 gcc_assert (stmt_die);
23216 add_high_low_attributes (stmt, stmt_die);
23219 decls_for_scope (stmt, stmt_die);
23222 /* Generate a DIE for an inlined subprogram. */
23224 static void
23225 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23227 tree decl;
23229 /* The instance of function that is effectively being inlined shall not
23230 be abstract. */
23231 gcc_assert (! BLOCK_ABSTRACT (stmt));
23233 decl = block_ultimate_origin (stmt);
23235 /* Make sure any inlined functions are known to be inlineable. */
23236 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23237 || cgraph_function_possibly_inlined_p (decl));
23239 if (! BLOCK_ABSTRACT (stmt))
23241 dw_die_ref subr_die
23242 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23244 if (call_arg_locations)
23245 BLOCK_DIE (stmt) = subr_die;
23246 add_abstract_origin_attribute (subr_die, decl);
23247 if (TREE_ASM_WRITTEN (stmt))
23248 add_high_low_attributes (stmt, subr_die);
23249 add_call_src_coords_attributes (stmt, subr_die);
23251 decls_for_scope (stmt, subr_die);
23255 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23256 the comment for VLR_CONTEXT. */
23258 static void
23259 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23261 dw_die_ref decl_die;
23263 if (TREE_TYPE (decl) == error_mark_node)
23264 return;
23266 decl_die = new_die (DW_TAG_member, context_die, decl);
23267 add_name_and_src_coords_attributes (decl_die, decl);
23268 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23269 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23270 context_die);
23272 if (DECL_BIT_FIELD_TYPE (decl))
23274 add_byte_size_attribute (decl_die, decl);
23275 add_bit_size_attribute (decl_die, decl);
23276 add_bit_offset_attribute (decl_die, decl, ctx);
23279 add_alignment_attribute (decl_die, decl);
23281 /* If we have a variant part offset, then we are supposed to process a member
23282 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23283 trees. */
23284 gcc_assert (ctx->variant_part_offset == NULL_TREE
23285 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23286 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23287 add_data_member_location_attribute (decl_die, decl, ctx);
23289 if (DECL_ARTIFICIAL (decl))
23290 add_AT_flag (decl_die, DW_AT_artificial, 1);
23292 add_accessibility_attribute (decl_die, decl);
23294 /* Equate decl number to die, so that we can look up this decl later on. */
23295 equate_decl_number_to_die (decl, decl_die);
23298 /* Generate a DIE for a pointer to a member type. TYPE can be an
23299 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23300 pointer to member function. */
23302 static void
23303 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23305 if (lookup_type_die (type))
23306 return;
23308 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23309 scope_die_for (type, context_die), type);
23311 equate_type_number_to_die (type, ptr_die);
23312 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23313 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23314 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23315 context_die);
23316 add_alignment_attribute (ptr_die, type);
23318 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23319 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23321 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23322 add_AT_loc (ptr_die, DW_AT_use_location, op);
23326 static char *producer_string;
23328 /* Return a heap allocated producer string including command line options
23329 if -grecord-gcc-switches. */
23331 static char *
23332 gen_producer_string (void)
23334 size_t j;
23335 auto_vec<const char *> switches;
23336 const char *language_string = lang_hooks.name;
23337 char *producer, *tail;
23338 const char *p;
23339 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23340 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23342 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23343 switch (save_decoded_options[j].opt_index)
23345 case OPT_o:
23346 case OPT_d:
23347 case OPT_dumpbase:
23348 case OPT_dumpdir:
23349 case OPT_auxbase:
23350 case OPT_auxbase_strip:
23351 case OPT_quiet:
23352 case OPT_version:
23353 case OPT_v:
23354 case OPT_w:
23355 case OPT_L:
23356 case OPT_D:
23357 case OPT_I:
23358 case OPT_U:
23359 case OPT_SPECIAL_unknown:
23360 case OPT_SPECIAL_ignore:
23361 case OPT_SPECIAL_program_name:
23362 case OPT_SPECIAL_input_file:
23363 case OPT_grecord_gcc_switches:
23364 case OPT_gno_record_gcc_switches:
23365 case OPT__output_pch_:
23366 case OPT_fdiagnostics_show_location_:
23367 case OPT_fdiagnostics_show_option:
23368 case OPT_fdiagnostics_show_caret:
23369 case OPT_fdiagnostics_color_:
23370 case OPT_fverbose_asm:
23371 case OPT____:
23372 case OPT__sysroot_:
23373 case OPT_nostdinc:
23374 case OPT_nostdinc__:
23375 case OPT_fpreprocessed:
23376 case OPT_fltrans_output_list_:
23377 case OPT_fresolution_:
23378 case OPT_fdebug_prefix_map_:
23379 /* Ignore these. */
23380 continue;
23381 default:
23382 if (cl_options[save_decoded_options[j].opt_index].flags
23383 & CL_NO_DWARF_RECORD)
23384 continue;
23385 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23386 == '-');
23387 switch (save_decoded_options[j].canonical_option[0][1])
23389 case 'M':
23390 case 'i':
23391 case 'W':
23392 continue;
23393 case 'f':
23394 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23395 "dump", 4) == 0)
23396 continue;
23397 break;
23398 default:
23399 break;
23401 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23402 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23403 break;
23406 producer = XNEWVEC (char, plen + 1 + len + 1);
23407 tail = producer;
23408 sprintf (tail, "%s %s", language_string, version_string);
23409 tail += plen;
23411 FOR_EACH_VEC_ELT (switches, j, p)
23413 len = strlen (p);
23414 *tail = ' ';
23415 memcpy (tail + 1, p, len);
23416 tail += len + 1;
23419 *tail = '\0';
23420 return producer;
23423 /* Given a C and/or C++ language/version string return the "highest".
23424 C++ is assumed to be "higher" than C in this case. Used for merging
23425 LTO translation unit languages. */
23426 static const char *
23427 highest_c_language (const char *lang1, const char *lang2)
23429 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23430 return "GNU C++14";
23431 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23432 return "GNU C++11";
23433 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23434 return "GNU C++98";
23436 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23437 return "GNU C11";
23438 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23439 return "GNU C99";
23440 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23441 return "GNU C89";
23443 gcc_unreachable ();
23447 /* Generate the DIE for the compilation unit. */
23449 static dw_die_ref
23450 gen_compile_unit_die (const char *filename)
23452 dw_die_ref die;
23453 const char *language_string = lang_hooks.name;
23454 int language;
23456 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23458 if (filename)
23460 add_name_attribute (die, filename);
23461 /* Don't add cwd for <built-in>. */
23462 if (filename[0] != '<')
23463 add_comp_dir_attribute (die);
23466 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23468 /* If our producer is LTO try to figure out a common language to use
23469 from the global list of translation units. */
23470 if (strcmp (language_string, "GNU GIMPLE") == 0)
23472 unsigned i;
23473 tree t;
23474 const char *common_lang = NULL;
23476 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23478 if (!TRANSLATION_UNIT_LANGUAGE (t))
23479 continue;
23480 if (!common_lang)
23481 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23482 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23484 else if (strncmp (common_lang, "GNU C", 5) == 0
23485 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23486 /* Mixing C and C++ is ok, use C++ in that case. */
23487 common_lang = highest_c_language (common_lang,
23488 TRANSLATION_UNIT_LANGUAGE (t));
23489 else
23491 /* Fall back to C. */
23492 common_lang = NULL;
23493 break;
23497 if (common_lang)
23498 language_string = common_lang;
23501 language = DW_LANG_C;
23502 if (strncmp (language_string, "GNU C", 5) == 0
23503 && ISDIGIT (language_string[5]))
23505 language = DW_LANG_C89;
23506 if (dwarf_version >= 3 || !dwarf_strict)
23508 if (strcmp (language_string, "GNU C89") != 0)
23509 language = DW_LANG_C99;
23511 if (dwarf_version >= 5 /* || !dwarf_strict */)
23512 if (strcmp (language_string, "GNU C11") == 0)
23513 language = DW_LANG_C11;
23516 else if (strncmp (language_string, "GNU C++", 7) == 0)
23518 language = DW_LANG_C_plus_plus;
23519 if (dwarf_version >= 5 /* || !dwarf_strict */)
23521 if (strcmp (language_string, "GNU C++11") == 0)
23522 language = DW_LANG_C_plus_plus_11;
23523 else if (strcmp (language_string, "GNU C++14") == 0)
23524 language = DW_LANG_C_plus_plus_14;
23527 else if (strcmp (language_string, "GNU F77") == 0)
23528 language = DW_LANG_Fortran77;
23529 else if (dwarf_version >= 3 || !dwarf_strict)
23531 if (strcmp (language_string, "GNU Ada") == 0)
23532 language = DW_LANG_Ada95;
23533 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23535 language = DW_LANG_Fortran95;
23536 if (dwarf_version >= 5 /* || !dwarf_strict */)
23538 if (strcmp (language_string, "GNU Fortran2003") == 0)
23539 language = DW_LANG_Fortran03;
23540 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23541 language = DW_LANG_Fortran08;
23544 else if (strcmp (language_string, "GNU Objective-C") == 0)
23545 language = DW_LANG_ObjC;
23546 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23547 language = DW_LANG_ObjC_plus_plus;
23548 else if (dwarf_version >= 5 || !dwarf_strict)
23550 if (strcmp (language_string, "GNU Go") == 0)
23551 language = DW_LANG_Go;
23554 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23555 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23556 language = DW_LANG_Fortran90;
23558 add_AT_unsigned (die, DW_AT_language, language);
23560 switch (language)
23562 case DW_LANG_Fortran77:
23563 case DW_LANG_Fortran90:
23564 case DW_LANG_Fortran95:
23565 case DW_LANG_Fortran03:
23566 case DW_LANG_Fortran08:
23567 /* Fortran has case insensitive identifiers and the front-end
23568 lowercases everything. */
23569 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23570 break;
23571 default:
23572 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23573 break;
23575 return die;
23578 /* Generate the DIE for a base class. */
23580 static void
23581 gen_inheritance_die (tree binfo, tree access, tree type,
23582 dw_die_ref context_die)
23584 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23585 struct vlr_context ctx = { type, NULL };
23587 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23588 context_die);
23589 add_data_member_location_attribute (die, binfo, &ctx);
23591 if (BINFO_VIRTUAL_P (binfo))
23592 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23594 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23595 children, otherwise the default is DW_ACCESS_public. In DWARF2
23596 the default has always been DW_ACCESS_private. */
23597 if (access == access_public_node)
23599 if (dwarf_version == 2
23600 || context_die->die_tag == DW_TAG_class_type)
23601 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23603 else if (access == access_protected_node)
23604 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23605 else if (dwarf_version > 2
23606 && context_die->die_tag != DW_TAG_class_type)
23607 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23610 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23611 structure. */
23612 static bool
23613 is_variant_part (tree decl)
23615 return (TREE_CODE (decl) == FIELD_DECL
23616 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23619 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23620 return the FIELD_DECL. Return NULL_TREE otherwise. */
23622 static tree
23623 analyze_discr_in_predicate (tree operand, tree struct_type)
23625 bool continue_stripping = true;
23626 while (continue_stripping)
23627 switch (TREE_CODE (operand))
23629 CASE_CONVERT:
23630 operand = TREE_OPERAND (operand, 0);
23631 break;
23632 default:
23633 continue_stripping = false;
23634 break;
23637 /* Match field access to members of struct_type only. */
23638 if (TREE_CODE (operand) == COMPONENT_REF
23639 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23640 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23641 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23642 return TREE_OPERAND (operand, 1);
23643 else
23644 return NULL_TREE;
23647 /* Check that SRC is a constant integer that can be represented as a native
23648 integer constant (either signed or unsigned). If so, store it into DEST and
23649 return true. Return false otherwise. */
23651 static bool
23652 get_discr_value (tree src, dw_discr_value *dest)
23654 tree discr_type = TREE_TYPE (src);
23656 if (lang_hooks.types.get_debug_type)
23658 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23659 if (debug_type != NULL)
23660 discr_type = debug_type;
23663 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23664 return false;
23666 /* Signedness can vary between the original type and the debug type. This
23667 can happen for character types in Ada for instance: the character type
23668 used for code generation can be signed, to be compatible with the C one,
23669 but from a debugger point of view, it must be unsigned. */
23670 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23671 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23673 if (is_orig_unsigned != is_debug_unsigned)
23674 src = fold_convert (discr_type, src);
23676 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23677 return false;
23679 dest->pos = is_debug_unsigned;
23680 if (is_debug_unsigned)
23681 dest->v.uval = tree_to_uhwi (src);
23682 else
23683 dest->v.sval = tree_to_shwi (src);
23685 return true;
23688 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23689 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23690 store NULL_TREE in DISCR_DECL. Otherwise:
23692 - store the discriminant field in STRUCT_TYPE that controls the variant
23693 part to *DISCR_DECL
23695 - put in *DISCR_LISTS_P an array where for each variant, the item
23696 represents the corresponding matching list of discriminant values.
23698 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23699 the above array.
23701 Note that when the array is allocated (i.e. when the analysis is
23702 successful), it is up to the caller to free the array. */
23704 static void
23705 analyze_variants_discr (tree variant_part_decl,
23706 tree struct_type,
23707 tree *discr_decl,
23708 dw_discr_list_ref **discr_lists_p,
23709 unsigned *discr_lists_length)
23711 tree variant_part_type = TREE_TYPE (variant_part_decl);
23712 tree variant;
23713 dw_discr_list_ref *discr_lists;
23714 unsigned i;
23716 /* Compute how many variants there are in this variant part. */
23717 *discr_lists_length = 0;
23718 for (variant = TYPE_FIELDS (variant_part_type);
23719 variant != NULL_TREE;
23720 variant = DECL_CHAIN (variant))
23721 ++*discr_lists_length;
23723 *discr_decl = NULL_TREE;
23724 *discr_lists_p
23725 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23726 sizeof (**discr_lists_p));
23727 discr_lists = *discr_lists_p;
23729 /* And then analyze all variants to extract discriminant information for all
23730 of them. This analysis is conservative: as soon as we detect something we
23731 do not support, abort everything and pretend we found nothing. */
23732 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23733 variant != NULL_TREE;
23734 variant = DECL_CHAIN (variant), ++i)
23736 tree match_expr = DECL_QUALIFIER (variant);
23738 /* Now, try to analyze the predicate and deduce a discriminant for
23739 it. */
23740 if (match_expr == boolean_true_node)
23741 /* Typically happens for the default variant: it matches all cases that
23742 previous variants rejected. Don't output any matching value for
23743 this one. */
23744 continue;
23746 /* The following loop tries to iterate over each discriminant
23747 possibility: single values or ranges. */
23748 while (match_expr != NULL_TREE)
23750 tree next_round_match_expr;
23751 tree candidate_discr = NULL_TREE;
23752 dw_discr_list_ref new_node = NULL;
23754 /* Possibilities are matched one after the other by nested
23755 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23756 continue with the rest at next iteration. */
23757 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23759 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23760 match_expr = TREE_OPERAND (match_expr, 1);
23762 else
23763 next_round_match_expr = NULL_TREE;
23765 if (match_expr == boolean_false_node)
23766 /* This sub-expression matches nothing: just wait for the next
23767 one. */
23770 else if (TREE_CODE (match_expr) == EQ_EXPR)
23772 /* We are matching: <discr_field> == <integer_cst>
23773 This sub-expression matches a single value. */
23774 tree integer_cst = TREE_OPERAND (match_expr, 1);
23776 candidate_discr
23777 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23778 struct_type);
23780 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23781 if (!get_discr_value (integer_cst,
23782 &new_node->dw_discr_lower_bound))
23783 goto abort;
23784 new_node->dw_discr_range = false;
23787 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23789 /* We are matching:
23790 <discr_field> > <integer_cst>
23791 && <discr_field> < <integer_cst>.
23792 This sub-expression matches the range of values between the
23793 two matched integer constants. Note that comparisons can be
23794 inclusive or exclusive. */
23795 tree candidate_discr_1, candidate_discr_2;
23796 tree lower_cst, upper_cst;
23797 bool lower_cst_included, upper_cst_included;
23798 tree lower_op = TREE_OPERAND (match_expr, 0);
23799 tree upper_op = TREE_OPERAND (match_expr, 1);
23801 /* When the comparison is exclusive, the integer constant is not
23802 the discriminant range bound we are looking for: we will have
23803 to increment or decrement it. */
23804 if (TREE_CODE (lower_op) == GE_EXPR)
23805 lower_cst_included = true;
23806 else if (TREE_CODE (lower_op) == GT_EXPR)
23807 lower_cst_included = false;
23808 else
23809 goto abort;
23811 if (TREE_CODE (upper_op) == LE_EXPR)
23812 upper_cst_included = true;
23813 else if (TREE_CODE (upper_op) == LT_EXPR)
23814 upper_cst_included = false;
23815 else
23816 goto abort;
23818 /* Extract the discriminant from the first operand and check it
23819 is consistant with the same analysis in the second
23820 operand. */
23821 candidate_discr_1
23822 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23823 struct_type);
23824 candidate_discr_2
23825 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23826 struct_type);
23827 if (candidate_discr_1 == candidate_discr_2)
23828 candidate_discr = candidate_discr_1;
23829 else
23830 goto abort;
23832 /* Extract bounds from both. */
23833 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23834 lower_cst = TREE_OPERAND (lower_op, 1);
23835 upper_cst = TREE_OPERAND (upper_op, 1);
23837 if (!lower_cst_included)
23838 lower_cst
23839 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
23840 build_int_cst (TREE_TYPE (lower_cst), 1));
23841 if (!upper_cst_included)
23842 upper_cst
23843 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
23844 build_int_cst (TREE_TYPE (upper_cst), 1));
23846 if (!get_discr_value (lower_cst,
23847 &new_node->dw_discr_lower_bound)
23848 || !get_discr_value (upper_cst,
23849 &new_node->dw_discr_upper_bound))
23850 goto abort;
23852 new_node->dw_discr_range = true;
23855 else
23856 /* Unsupported sub-expression: we cannot determine the set of
23857 matching discriminant values. Abort everything. */
23858 goto abort;
23860 /* If the discriminant info is not consistant with what we saw so
23861 far, consider the analysis failed and abort everything. */
23862 if (candidate_discr == NULL_TREE
23863 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
23864 goto abort;
23865 else
23866 *discr_decl = candidate_discr;
23868 if (new_node != NULL)
23870 new_node->dw_discr_next = discr_lists[i];
23871 discr_lists[i] = new_node;
23873 match_expr = next_round_match_expr;
23877 /* If we reach this point, we could match everything we were interested
23878 in. */
23879 return;
23881 abort:
23882 /* Clean all data structure and return no result. */
23883 free (*discr_lists_p);
23884 *discr_lists_p = NULL;
23885 *discr_decl = NULL_TREE;
23888 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
23889 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
23890 under CONTEXT_DIE.
23892 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
23893 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
23894 this type, which are record types, represent the available variants and each
23895 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
23896 values are inferred from these attributes.
23898 In trees, the offsets for the fields inside these sub-records are relative
23899 to the variant part itself, whereas the corresponding DIEs should have
23900 offset attributes that are relative to the embedding record base address.
23901 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
23902 must be an expression that computes the offset of the variant part to
23903 describe in DWARF. */
23905 static void
23906 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
23907 dw_die_ref context_die)
23909 const tree variant_part_type = TREE_TYPE (variant_part_decl);
23910 tree variant_part_offset = vlr_ctx->variant_part_offset;
23911 struct loc_descr_context ctx = {
23912 vlr_ctx->struct_type, /* context_type */
23913 NULL_TREE, /* base_decl */
23914 NULL, /* dpi */
23915 false, /* placeholder_arg */
23916 false /* placeholder_seen */
23919 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
23920 NULL_TREE if there is no such field. */
23921 tree discr_decl = NULL_TREE;
23922 dw_discr_list_ref *discr_lists;
23923 unsigned discr_lists_length = 0;
23924 unsigned i;
23926 dw_die_ref dwarf_proc_die = NULL;
23927 dw_die_ref variant_part_die
23928 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
23930 equate_decl_number_to_die (variant_part_decl, variant_part_die);
23932 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
23933 &discr_decl, &discr_lists, &discr_lists_length);
23935 if (discr_decl != NULL_TREE)
23937 dw_die_ref discr_die = lookup_decl_die (discr_decl);
23939 if (discr_die)
23940 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
23941 else
23942 /* We have no DIE for the discriminant, so just discard all
23943 discrimimant information in the output. */
23944 discr_decl = NULL_TREE;
23947 /* If the offset for this variant part is more complex than a constant,
23948 create a DWARF procedure for it so that we will not have to generate DWARF
23949 expressions for it for each member. */
23950 if (TREE_CODE (variant_part_offset) != INTEGER_CST
23951 && (dwarf_version >= 3 || !dwarf_strict))
23953 const tree dwarf_proc_fndecl
23954 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
23955 build_function_type (TREE_TYPE (variant_part_offset),
23956 NULL_TREE));
23957 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
23958 const dw_loc_descr_ref dwarf_proc_body
23959 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
23961 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
23962 dwarf_proc_fndecl, context_die);
23963 if (dwarf_proc_die != NULL)
23964 variant_part_offset = dwarf_proc_call;
23967 /* Output DIEs for all variants. */
23968 i = 0;
23969 for (tree variant = TYPE_FIELDS (variant_part_type);
23970 variant != NULL_TREE;
23971 variant = DECL_CHAIN (variant), ++i)
23973 tree variant_type = TREE_TYPE (variant);
23974 dw_die_ref variant_die;
23976 /* All variants (i.e. members of a variant part) are supposed to be
23977 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
23978 under these records. */
23979 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
23981 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
23982 equate_decl_number_to_die (variant, variant_die);
23984 /* Output discriminant values this variant matches, if any. */
23985 if (discr_decl == NULL || discr_lists[i] == NULL)
23986 /* In the case we have discriminant information at all, this is
23987 probably the default variant: as the standard says, don't
23988 output any discriminant value/list attribute. */
23990 else if (discr_lists[i]->dw_discr_next == NULL
23991 && !discr_lists[i]->dw_discr_range)
23992 /* If there is only one accepted value, don't bother outputting a
23993 list. */
23994 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
23995 else
23996 add_discr_list (variant_die, discr_lists[i]);
23998 for (tree member = TYPE_FIELDS (variant_type);
23999 member != NULL_TREE;
24000 member = DECL_CHAIN (member))
24002 struct vlr_context vlr_sub_ctx = {
24003 vlr_ctx->struct_type, /* struct_type */
24004 NULL /* variant_part_offset */
24006 if (is_variant_part (member))
24008 /* All offsets for fields inside variant parts are relative to
24009 the top-level embedding RECORD_TYPE's base address. On the
24010 other hand, offsets in GCC's types are relative to the
24011 nested-most variant part. So we have to sum offsets each time
24012 we recurse. */
24014 vlr_sub_ctx.variant_part_offset
24015 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24016 variant_part_offset, byte_position (member));
24017 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24019 else
24021 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24022 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24027 free (discr_lists);
24030 /* Generate a DIE for a class member. */
24032 static void
24033 gen_member_die (tree type, dw_die_ref context_die)
24035 tree member;
24036 tree binfo = TYPE_BINFO (type);
24038 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24040 /* If this is not an incomplete type, output descriptions of each of its
24041 members. Note that as we output the DIEs necessary to represent the
24042 members of this record or union type, we will also be trying to output
24043 DIEs to represent the *types* of those members. However the `type'
24044 function (above) will specifically avoid generating type DIEs for member
24045 types *within* the list of member DIEs for this (containing) type except
24046 for those types (of members) which are explicitly marked as also being
24047 members of this (containing) type themselves. The g++ front- end can
24048 force any given type to be treated as a member of some other (containing)
24049 type by setting the TYPE_CONTEXT of the given (member) type to point to
24050 the TREE node representing the appropriate (containing) type. */
24052 /* First output info about the base classes. */
24053 if (binfo)
24055 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24056 int i;
24057 tree base;
24059 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24060 gen_inheritance_die (base,
24061 (accesses ? (*accesses)[i] : access_public_node),
24062 type,
24063 context_die);
24066 /* Now output info about the data members and type members. */
24067 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24069 struct vlr_context vlr_ctx = { type, NULL_TREE };
24070 bool static_inline_p
24071 = (TREE_STATIC (member)
24072 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24073 != -1));
24075 /* Ignore clones. */
24076 if (DECL_ABSTRACT_ORIGIN (member))
24077 continue;
24079 /* If we thought we were generating minimal debug info for TYPE
24080 and then changed our minds, some of the member declarations
24081 may have already been defined. Don't define them again, but
24082 do put them in the right order. */
24084 if (dw_die_ref child = lookup_decl_die (member))
24086 /* Handle inline static data members, which only have in-class
24087 declarations. */
24088 dw_die_ref ref = NULL;
24089 if (child->die_tag == DW_TAG_variable
24090 && child->die_parent == comp_unit_die ())
24092 ref = get_AT_ref (child, DW_AT_specification);
24093 /* For C++17 inline static data members followed by redundant
24094 out of class redeclaration, we might get here with
24095 child being the DIE created for the out of class
24096 redeclaration and with its DW_AT_specification being
24097 the DIE created for in-class definition. We want to
24098 reparent the latter, and don't want to create another
24099 DIE with DW_AT_specification in that case, because
24100 we already have one. */
24101 if (ref
24102 && static_inline_p
24103 && ref->die_tag == DW_TAG_variable
24104 && ref->die_parent == comp_unit_die ()
24105 && get_AT (ref, DW_AT_specification) == NULL)
24107 child = ref;
24108 ref = NULL;
24109 static_inline_p = false;
24113 if (child->die_tag == DW_TAG_variable
24114 && child->die_parent == comp_unit_die ()
24115 && ref == NULL)
24117 reparent_child (child, context_die);
24118 if (dwarf_version < 5)
24119 child->die_tag = DW_TAG_member;
24121 else
24122 splice_child_die (context_die, child);
24125 /* Do not generate standard DWARF for variant parts if we are generating
24126 the corresponding GNAT encodings: DIEs generated for both would
24127 conflict in our mappings. */
24128 else if (is_variant_part (member)
24129 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24131 vlr_ctx.variant_part_offset = byte_position (member);
24132 gen_variant_part (member, &vlr_ctx, context_die);
24134 else
24136 vlr_ctx.variant_part_offset = NULL_TREE;
24137 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24140 /* For C++ inline static data members emit immediately a DW_TAG_variable
24141 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24142 DW_AT_specification. */
24143 if (static_inline_p)
24145 int old_extern = DECL_EXTERNAL (member);
24146 DECL_EXTERNAL (member) = 0;
24147 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24148 DECL_EXTERNAL (member) = old_extern;
24153 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24154 is set, we pretend that the type was never defined, so we only get the
24155 member DIEs needed by later specification DIEs. */
24157 static void
24158 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24159 enum debug_info_usage usage)
24161 if (TREE_ASM_WRITTEN (type))
24163 /* Fill in the bound of variable-length fields in late dwarf if
24164 still incomplete. */
24165 if (!early_dwarf && variably_modified_type_p (type, NULL))
24166 for (tree member = TYPE_FIELDS (type);
24167 member;
24168 member = DECL_CHAIN (member))
24169 fill_variable_array_bounds (TREE_TYPE (member));
24170 return;
24173 dw_die_ref type_die = lookup_type_die (type);
24174 dw_die_ref scope_die = 0;
24175 int nested = 0;
24176 int complete = (TYPE_SIZE (type)
24177 && (! TYPE_STUB_DECL (type)
24178 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24179 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24180 complete = complete && should_emit_struct_debug (type, usage);
24182 if (type_die && ! complete)
24183 return;
24185 if (TYPE_CONTEXT (type) != NULL_TREE
24186 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24187 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24188 nested = 1;
24190 scope_die = scope_die_for (type, context_die);
24192 /* Generate child dies for template paramaters. */
24193 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24194 schedule_generic_params_dies_gen (type);
24196 if (! type_die || (nested && is_cu_die (scope_die)))
24197 /* First occurrence of type or toplevel definition of nested class. */
24199 dw_die_ref old_die = type_die;
24201 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24202 ? record_type_tag (type) : DW_TAG_union_type,
24203 scope_die, type);
24204 equate_type_number_to_die (type, type_die);
24205 if (old_die)
24206 add_AT_specification (type_die, old_die);
24207 else
24208 add_name_attribute (type_die, type_tag (type));
24210 else
24211 remove_AT (type_die, DW_AT_declaration);
24213 /* If this type has been completed, then give it a byte_size attribute and
24214 then give a list of members. */
24215 if (complete && !ns_decl)
24217 /* Prevent infinite recursion in cases where the type of some member of
24218 this type is expressed in terms of this type itself. */
24219 TREE_ASM_WRITTEN (type) = 1;
24220 add_byte_size_attribute (type_die, type);
24221 add_alignment_attribute (type_die, type);
24222 if (TYPE_STUB_DECL (type) != NULL_TREE)
24224 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24225 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24228 /* If the first reference to this type was as the return type of an
24229 inline function, then it may not have a parent. Fix this now. */
24230 if (type_die->die_parent == NULL)
24231 add_child_die (scope_die, type_die);
24233 push_decl_scope (type);
24234 gen_member_die (type, type_die);
24235 pop_decl_scope ();
24237 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24238 if (TYPE_ARTIFICIAL (type))
24239 add_AT_flag (type_die, DW_AT_artificial, 1);
24241 /* GNU extension: Record what type our vtable lives in. */
24242 if (TYPE_VFIELD (type))
24244 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24246 gen_type_die (vtype, context_die);
24247 add_AT_die_ref (type_die, DW_AT_containing_type,
24248 lookup_type_die (vtype));
24251 else
24253 add_AT_flag (type_die, DW_AT_declaration, 1);
24255 /* We don't need to do this for function-local types. */
24256 if (TYPE_STUB_DECL (type)
24257 && ! decl_function_context (TYPE_STUB_DECL (type)))
24258 vec_safe_push (incomplete_types, type);
24261 if (get_AT (type_die, DW_AT_name))
24262 add_pubtype (type, type_die);
24265 /* Generate a DIE for a subroutine _type_. */
24267 static void
24268 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24270 tree return_type = TREE_TYPE (type);
24271 dw_die_ref subr_die
24272 = new_die (DW_TAG_subroutine_type,
24273 scope_die_for (type, context_die), type);
24275 equate_type_number_to_die (type, subr_die);
24276 add_prototyped_attribute (subr_die, type);
24277 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24278 context_die);
24279 add_alignment_attribute (subr_die, type);
24280 gen_formal_types_die (type, subr_die);
24282 if (get_AT (subr_die, DW_AT_name))
24283 add_pubtype (type, subr_die);
24284 if ((dwarf_version >= 5 || !dwarf_strict)
24285 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24286 add_AT_flag (subr_die, DW_AT_reference, 1);
24287 if ((dwarf_version >= 5 || !dwarf_strict)
24288 && lang_hooks.types.type_dwarf_attribute (type,
24289 DW_AT_rvalue_reference) != -1)
24290 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24293 /* Generate a DIE for a type definition. */
24295 static void
24296 gen_typedef_die (tree decl, dw_die_ref context_die)
24298 dw_die_ref type_die;
24299 tree type;
24301 if (TREE_ASM_WRITTEN (decl))
24303 if (DECL_ORIGINAL_TYPE (decl))
24304 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24305 return;
24308 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24309 checks in process_scope_var and modified_type_die), this should be called
24310 only for original types. */
24311 gcc_assert (decl_ultimate_origin (decl) == NULL
24312 || decl_ultimate_origin (decl) == decl);
24314 TREE_ASM_WRITTEN (decl) = 1;
24315 type_die = new_die (DW_TAG_typedef, context_die, decl);
24317 add_name_and_src_coords_attributes (type_die, decl);
24318 if (DECL_ORIGINAL_TYPE (decl))
24320 type = DECL_ORIGINAL_TYPE (decl);
24321 if (type == error_mark_node)
24322 return;
24324 gcc_assert (type != TREE_TYPE (decl));
24325 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24327 else
24329 type = TREE_TYPE (decl);
24330 if (type == error_mark_node)
24331 return;
24333 if (is_naming_typedef_decl (TYPE_NAME (type)))
24335 /* Here, we are in the case of decl being a typedef naming
24336 an anonymous type, e.g:
24337 typedef struct {...} foo;
24338 In that case TREE_TYPE (decl) is not a typedef variant
24339 type and TYPE_NAME of the anonymous type is set to the
24340 TYPE_DECL of the typedef. This construct is emitted by
24341 the C++ FE.
24343 TYPE is the anonymous struct named by the typedef
24344 DECL. As we need the DW_AT_type attribute of the
24345 DW_TAG_typedef to point to the DIE of TYPE, let's
24346 generate that DIE right away. add_type_attribute
24347 called below will then pick (via lookup_type_die) that
24348 anonymous struct DIE. */
24349 if (!TREE_ASM_WRITTEN (type))
24350 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24352 /* This is a GNU Extension. We are adding a
24353 DW_AT_linkage_name attribute to the DIE of the
24354 anonymous struct TYPE. The value of that attribute
24355 is the name of the typedef decl naming the anonymous
24356 struct. This greatly eases the work of consumers of
24357 this debug info. */
24358 add_linkage_name_raw (lookup_type_die (type), decl);
24362 add_type_attribute (type_die, type, decl_quals (decl), false,
24363 context_die);
24365 if (is_naming_typedef_decl (decl))
24366 /* We want that all subsequent calls to lookup_type_die with
24367 TYPE in argument yield the DW_TAG_typedef we have just
24368 created. */
24369 equate_type_number_to_die (type, type_die);
24371 add_alignment_attribute (type_die, TREE_TYPE (decl));
24373 add_accessibility_attribute (type_die, decl);
24375 if (DECL_ABSTRACT_P (decl))
24376 equate_decl_number_to_die (decl, type_die);
24378 if (get_AT (type_die, DW_AT_name))
24379 add_pubtype (decl, type_die);
24382 /* Generate a DIE for a struct, class, enum or union type. */
24384 static void
24385 gen_tagged_type_die (tree type,
24386 dw_die_ref context_die,
24387 enum debug_info_usage usage)
24389 int need_pop;
24391 if (type == NULL_TREE
24392 || !is_tagged_type (type))
24393 return;
24395 if (TREE_ASM_WRITTEN (type))
24396 need_pop = 0;
24397 /* If this is a nested type whose containing class hasn't been written
24398 out yet, writing it out will cover this one, too. This does not apply
24399 to instantiations of member class templates; they need to be added to
24400 the containing class as they are generated. FIXME: This hurts the
24401 idea of combining type decls from multiple TUs, since we can't predict
24402 what set of template instantiations we'll get. */
24403 else if (TYPE_CONTEXT (type)
24404 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24405 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24407 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24409 if (TREE_ASM_WRITTEN (type))
24410 return;
24412 /* If that failed, attach ourselves to the stub. */
24413 push_decl_scope (TYPE_CONTEXT (type));
24414 context_die = lookup_type_die (TYPE_CONTEXT (type));
24415 need_pop = 1;
24417 else if (TYPE_CONTEXT (type) != NULL_TREE
24418 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24420 /* If this type is local to a function that hasn't been written
24421 out yet, use a NULL context for now; it will be fixed up in
24422 decls_for_scope. */
24423 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24424 /* A declaration DIE doesn't count; nested types need to go in the
24425 specification. */
24426 if (context_die && is_declaration_die (context_die))
24427 context_die = NULL;
24428 need_pop = 0;
24430 else
24432 context_die = declare_in_namespace (type, context_die);
24433 need_pop = 0;
24436 if (TREE_CODE (type) == ENUMERAL_TYPE)
24438 /* This might have been written out by the call to
24439 declare_in_namespace. */
24440 if (!TREE_ASM_WRITTEN (type))
24441 gen_enumeration_type_die (type, context_die);
24443 else
24444 gen_struct_or_union_type_die (type, context_die, usage);
24446 if (need_pop)
24447 pop_decl_scope ();
24449 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24450 it up if it is ever completed. gen_*_type_die will set it for us
24451 when appropriate. */
24454 /* Generate a type description DIE. */
24456 static void
24457 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24458 enum debug_info_usage usage)
24460 struct array_descr_info info;
24462 if (type == NULL_TREE || type == error_mark_node)
24463 return;
24465 if (flag_checking && type)
24466 verify_type (type);
24468 if (TYPE_NAME (type) != NULL_TREE
24469 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24470 && is_redundant_typedef (TYPE_NAME (type))
24471 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24472 /* The DECL of this type is a typedef we don't want to emit debug
24473 info for but we want debug info for its underlying typedef.
24474 This can happen for e.g, the injected-class-name of a C++
24475 type. */
24476 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24478 /* If TYPE is a typedef type variant, let's generate debug info
24479 for the parent typedef which TYPE is a type of. */
24480 if (typedef_variant_p (type))
24482 if (TREE_ASM_WRITTEN (type))
24483 return;
24485 tree name = TYPE_NAME (type);
24486 tree origin = decl_ultimate_origin (name);
24487 if (origin != NULL && origin != name)
24489 gen_decl_die (origin, NULL, NULL, context_die);
24490 return;
24493 /* Prevent broken recursion; we can't hand off to the same type. */
24494 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24496 /* Give typedefs the right scope. */
24497 context_die = scope_die_for (type, context_die);
24499 TREE_ASM_WRITTEN (type) = 1;
24501 gen_decl_die (name, NULL, NULL, context_die);
24502 return;
24505 /* If type is an anonymous tagged type named by a typedef, let's
24506 generate debug info for the typedef. */
24507 if (is_naming_typedef_decl (TYPE_NAME (type)))
24509 /* Use the DIE of the containing namespace as the parent DIE of
24510 the type description DIE we want to generate. */
24511 if (DECL_CONTEXT (TYPE_NAME (type))
24512 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24513 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24515 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24516 return;
24519 if (lang_hooks.types.get_debug_type)
24521 tree debug_type = lang_hooks.types.get_debug_type (type);
24523 if (debug_type != NULL_TREE && debug_type != type)
24525 gen_type_die_with_usage (debug_type, context_die, usage);
24526 return;
24530 /* We are going to output a DIE to represent the unqualified version
24531 of this type (i.e. without any const or volatile qualifiers) so
24532 get the main variant (i.e. the unqualified version) of this type
24533 now. (Vectors and arrays are special because the debugging info is in the
24534 cloned type itself. Similarly function/method types can contain extra
24535 ref-qualification). */
24536 if (TREE_CODE (type) == FUNCTION_TYPE
24537 || TREE_CODE (type) == METHOD_TYPE)
24539 /* For function/method types, can't use type_main_variant here,
24540 because that can have different ref-qualifiers for C++,
24541 but try to canonicalize. */
24542 tree main = TYPE_MAIN_VARIANT (type);
24543 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24544 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24545 && check_base_type (t, main)
24546 && check_lang_type (t, type))
24548 type = t;
24549 break;
24552 else if (TREE_CODE (type) != VECTOR_TYPE
24553 && TREE_CODE (type) != ARRAY_TYPE)
24554 type = type_main_variant (type);
24556 /* If this is an array type with hidden descriptor, handle it first. */
24557 if (!TREE_ASM_WRITTEN (type)
24558 && lang_hooks.types.get_array_descr_info)
24560 memset (&info, 0, sizeof (info));
24561 if (lang_hooks.types.get_array_descr_info (type, &info))
24563 /* Fortran sometimes emits array types with no dimension. */
24564 gcc_assert (info.ndimensions >= 0
24565 && (info.ndimensions
24566 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24567 gen_descr_array_type_die (type, &info, context_die);
24568 TREE_ASM_WRITTEN (type) = 1;
24569 return;
24573 if (TREE_ASM_WRITTEN (type))
24575 /* Variable-length types may be incomplete even if
24576 TREE_ASM_WRITTEN. For such types, fall through to
24577 gen_array_type_die() and possibly fill in
24578 DW_AT_{upper,lower}_bound attributes. */
24579 if ((TREE_CODE (type) != ARRAY_TYPE
24580 && TREE_CODE (type) != RECORD_TYPE
24581 && TREE_CODE (type) != UNION_TYPE
24582 && TREE_CODE (type) != QUAL_UNION_TYPE)
24583 || !variably_modified_type_p (type, NULL))
24584 return;
24587 switch (TREE_CODE (type))
24589 case ERROR_MARK:
24590 break;
24592 case POINTER_TYPE:
24593 case REFERENCE_TYPE:
24594 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24595 ensures that the gen_type_die recursion will terminate even if the
24596 type is recursive. Recursive types are possible in Ada. */
24597 /* ??? We could perhaps do this for all types before the switch
24598 statement. */
24599 TREE_ASM_WRITTEN (type) = 1;
24601 /* For these types, all that is required is that we output a DIE (or a
24602 set of DIEs) to represent the "basis" type. */
24603 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24604 DINFO_USAGE_IND_USE);
24605 break;
24607 case OFFSET_TYPE:
24608 /* This code is used for C++ pointer-to-data-member types.
24609 Output a description of the relevant class type. */
24610 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24611 DINFO_USAGE_IND_USE);
24613 /* Output a description of the type of the object pointed to. */
24614 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24615 DINFO_USAGE_IND_USE);
24617 /* Now output a DIE to represent this pointer-to-data-member type
24618 itself. */
24619 gen_ptr_to_mbr_type_die (type, context_die);
24620 break;
24622 case FUNCTION_TYPE:
24623 /* Force out return type (in case it wasn't forced out already). */
24624 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24625 DINFO_USAGE_DIR_USE);
24626 gen_subroutine_type_die (type, context_die);
24627 break;
24629 case METHOD_TYPE:
24630 /* Force out return type (in case it wasn't forced out already). */
24631 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24632 DINFO_USAGE_DIR_USE);
24633 gen_subroutine_type_die (type, context_die);
24634 break;
24636 case ARRAY_TYPE:
24637 case VECTOR_TYPE:
24638 gen_array_type_die (type, context_die);
24639 break;
24641 case ENUMERAL_TYPE:
24642 case RECORD_TYPE:
24643 case UNION_TYPE:
24644 case QUAL_UNION_TYPE:
24645 gen_tagged_type_die (type, context_die, usage);
24646 return;
24648 case VOID_TYPE:
24649 case INTEGER_TYPE:
24650 case REAL_TYPE:
24651 case FIXED_POINT_TYPE:
24652 case COMPLEX_TYPE:
24653 case BOOLEAN_TYPE:
24654 case POINTER_BOUNDS_TYPE:
24655 /* No DIEs needed for fundamental types. */
24656 break;
24658 case NULLPTR_TYPE:
24659 case LANG_TYPE:
24660 /* Just use DW_TAG_unspecified_type. */
24662 dw_die_ref type_die = lookup_type_die (type);
24663 if (type_die == NULL)
24665 tree name = TYPE_IDENTIFIER (type);
24666 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24667 type);
24668 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24669 equate_type_number_to_die (type, type_die);
24672 break;
24674 default:
24675 if (is_cxx_auto (type))
24677 tree name = TYPE_IDENTIFIER (type);
24678 dw_die_ref *die = (name == get_identifier ("auto")
24679 ? &auto_die : &decltype_auto_die);
24680 if (!*die)
24682 *die = new_die (DW_TAG_unspecified_type,
24683 comp_unit_die (), NULL_TREE);
24684 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24686 equate_type_number_to_die (type, *die);
24687 break;
24689 gcc_unreachable ();
24692 TREE_ASM_WRITTEN (type) = 1;
24695 static void
24696 gen_type_die (tree type, dw_die_ref context_die)
24698 if (type != error_mark_node)
24700 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24701 if (flag_checking)
24703 dw_die_ref die = lookup_type_die (type);
24704 if (die)
24705 check_die (die);
24710 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24711 things which are local to the given block. */
24713 static void
24714 gen_block_die (tree stmt, dw_die_ref context_die)
24716 int must_output_die = 0;
24717 bool inlined_func;
24719 /* Ignore blocks that are NULL. */
24720 if (stmt == NULL_TREE)
24721 return;
24723 inlined_func = inlined_function_outer_scope_p (stmt);
24725 /* If the block is one fragment of a non-contiguous block, do not
24726 process the variables, since they will have been done by the
24727 origin block. Do process subblocks. */
24728 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24730 tree sub;
24732 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24733 gen_block_die (sub, context_die);
24735 return;
24738 /* Determine if we need to output any Dwarf DIEs at all to represent this
24739 block. */
24740 if (inlined_func)
24741 /* The outer scopes for inlinings *must* always be represented. We
24742 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24743 must_output_die = 1;
24744 else
24746 /* Determine if this block directly contains any "significant"
24747 local declarations which we will need to output DIEs for. */
24748 if (debug_info_level > DINFO_LEVEL_TERSE)
24749 /* We are not in terse mode so *any* local declaration counts
24750 as being a "significant" one. */
24751 must_output_die = ((BLOCK_VARS (stmt) != NULL
24752 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24753 && (TREE_USED (stmt)
24754 || TREE_ASM_WRITTEN (stmt)
24755 || BLOCK_ABSTRACT (stmt)));
24756 else if ((TREE_USED (stmt)
24757 || TREE_ASM_WRITTEN (stmt)
24758 || BLOCK_ABSTRACT (stmt))
24759 && !dwarf2out_ignore_block (stmt))
24760 must_output_die = 1;
24763 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24764 DIE for any block which contains no significant local declarations at
24765 all. Rather, in such cases we just call `decls_for_scope' so that any
24766 needed Dwarf info for any sub-blocks will get properly generated. Note
24767 that in terse mode, our definition of what constitutes a "significant"
24768 local declaration gets restricted to include only inlined function
24769 instances and local (nested) function definitions. */
24770 if (must_output_die)
24772 if (inlined_func)
24774 /* If STMT block is abstract, that means we have been called
24775 indirectly from dwarf2out_abstract_function.
24776 That function rightfully marks the descendent blocks (of
24777 the abstract function it is dealing with) as being abstract,
24778 precisely to prevent us from emitting any
24779 DW_TAG_inlined_subroutine DIE as a descendent
24780 of an abstract function instance. So in that case, we should
24781 not call gen_inlined_subroutine_die.
24783 Later though, when cgraph asks dwarf2out to emit info
24784 for the concrete instance of the function decl into which
24785 the concrete instance of STMT got inlined, the later will lead
24786 to the generation of a DW_TAG_inlined_subroutine DIE. */
24787 if (! BLOCK_ABSTRACT (stmt))
24788 gen_inlined_subroutine_die (stmt, context_die);
24790 else
24791 gen_lexical_block_die (stmt, context_die);
24793 else
24794 decls_for_scope (stmt, context_die);
24797 /* Process variable DECL (or variable with origin ORIGIN) within
24798 block STMT and add it to CONTEXT_DIE. */
24799 static void
24800 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24802 dw_die_ref die;
24803 tree decl_or_origin = decl ? decl : origin;
24805 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24806 die = lookup_decl_die (decl_or_origin);
24807 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24809 if (TYPE_DECL_IS_STUB (decl_or_origin))
24810 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24811 else
24812 die = lookup_decl_die (decl_or_origin);
24813 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24814 if (! die && ! early_dwarf)
24815 return;
24817 else
24818 die = NULL;
24820 /* Avoid creating DIEs for local typedefs and concrete static variables that
24821 will only be pruned later. */
24822 if ((origin || decl_ultimate_origin (decl))
24823 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24824 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24826 origin = decl_ultimate_origin (decl_or_origin);
24827 if (decl && VAR_P (decl) && die != NULL)
24829 die = lookup_decl_die (origin);
24830 if (die != NULL)
24831 equate_decl_number_to_die (decl, die);
24833 return;
24836 if (die != NULL && die->die_parent == NULL)
24837 add_child_die (context_die, die);
24838 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
24840 if (early_dwarf)
24841 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
24842 stmt, context_die);
24844 else
24846 if (decl && DECL_P (decl))
24848 die = lookup_decl_die (decl);
24850 /* Early created DIEs do not have a parent as the decls refer
24851 to the function as DECL_CONTEXT rather than the BLOCK. */
24852 if (die && die->die_parent == NULL)
24854 gcc_assert (in_lto_p);
24855 add_child_die (context_die, die);
24859 gen_decl_die (decl, origin, NULL, context_die);
24863 /* Generate all of the decls declared within a given scope and (recursively)
24864 all of its sub-blocks. */
24866 static void
24867 decls_for_scope (tree stmt, dw_die_ref context_die)
24869 tree decl;
24870 unsigned int i;
24871 tree subblocks;
24873 /* Ignore NULL blocks. */
24874 if (stmt == NULL_TREE)
24875 return;
24877 /* Output the DIEs to represent all of the data objects and typedefs
24878 declared directly within this block but not within any nested
24879 sub-blocks. Also, nested function and tag DIEs have been
24880 generated with a parent of NULL; fix that up now. We don't
24881 have to do this if we're at -g1. */
24882 if (debug_info_level > DINFO_LEVEL_TERSE)
24884 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
24885 process_scope_var (stmt, decl, NULL_TREE, context_die);
24886 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
24887 origin - avoid doing this twice as we have no good way to see
24888 if we've done it once already. */
24889 if (! early_dwarf)
24890 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
24892 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
24893 if (decl == current_function_decl)
24894 /* Ignore declarations of the current function, while they
24895 are declarations, gen_subprogram_die would treat them
24896 as definitions again, because they are equal to
24897 current_function_decl and endlessly recurse. */;
24898 else if (TREE_CODE (decl) == FUNCTION_DECL)
24899 process_scope_var (stmt, decl, NULL_TREE, context_die);
24900 else
24901 process_scope_var (stmt, NULL_TREE, decl, context_die);
24905 /* Even if we're at -g1, we need to process the subblocks in order to get
24906 inlined call information. */
24908 /* Output the DIEs to represent all sub-blocks (and the items declared
24909 therein) of this block. */
24910 for (subblocks = BLOCK_SUBBLOCKS (stmt);
24911 subblocks != NULL;
24912 subblocks = BLOCK_CHAIN (subblocks))
24913 gen_block_die (subblocks, context_die);
24916 /* Is this a typedef we can avoid emitting? */
24918 bool
24919 is_redundant_typedef (const_tree decl)
24921 if (TYPE_DECL_IS_STUB (decl))
24922 return true;
24924 if (DECL_ARTIFICIAL (decl)
24925 && DECL_CONTEXT (decl)
24926 && is_tagged_type (DECL_CONTEXT (decl))
24927 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
24928 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
24929 /* Also ignore the artificial member typedef for the class name. */
24930 return true;
24932 return false;
24935 /* Return TRUE if TYPE is a typedef that names a type for linkage
24936 purposes. This kind of typedefs is produced by the C++ FE for
24937 constructs like:
24939 typedef struct {...} foo;
24941 In that case, there is no typedef variant type produced for foo.
24942 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
24943 struct type. */
24945 static bool
24946 is_naming_typedef_decl (const_tree decl)
24948 if (decl == NULL_TREE
24949 || TREE_CODE (decl) != TYPE_DECL
24950 || DECL_NAMELESS (decl)
24951 || !is_tagged_type (TREE_TYPE (decl))
24952 || DECL_IS_BUILTIN (decl)
24953 || is_redundant_typedef (decl)
24954 /* It looks like Ada produces TYPE_DECLs that are very similar
24955 to C++ naming typedefs but that have different
24956 semantics. Let's be specific to c++ for now. */
24957 || !is_cxx (decl))
24958 return FALSE;
24960 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
24961 && TYPE_NAME (TREE_TYPE (decl)) == decl
24962 && (TYPE_STUB_DECL (TREE_TYPE (decl))
24963 != TYPE_NAME (TREE_TYPE (decl))));
24966 /* Looks up the DIE for a context. */
24968 static inline dw_die_ref
24969 lookup_context_die (tree context)
24971 if (context)
24973 /* Find die that represents this context. */
24974 if (TYPE_P (context))
24976 context = TYPE_MAIN_VARIANT (context);
24977 dw_die_ref ctx = lookup_type_die (context);
24978 if (!ctx)
24979 return NULL;
24980 return strip_naming_typedef (context, ctx);
24982 else
24983 return lookup_decl_die (context);
24985 return comp_unit_die ();
24988 /* Returns the DIE for a context. */
24990 static inline dw_die_ref
24991 get_context_die (tree context)
24993 if (context)
24995 /* Find die that represents this context. */
24996 if (TYPE_P (context))
24998 context = TYPE_MAIN_VARIANT (context);
24999 return strip_naming_typedef (context, force_type_die (context));
25001 else
25002 return force_decl_die (context);
25004 return comp_unit_die ();
25007 /* Returns the DIE for decl. A DIE will always be returned. */
25009 static dw_die_ref
25010 force_decl_die (tree decl)
25012 dw_die_ref decl_die;
25013 unsigned saved_external_flag;
25014 tree save_fn = NULL_TREE;
25015 decl_die = lookup_decl_die (decl);
25016 if (!decl_die)
25018 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25020 decl_die = lookup_decl_die (decl);
25021 if (decl_die)
25022 return decl_die;
25024 switch (TREE_CODE (decl))
25026 case FUNCTION_DECL:
25027 /* Clear current_function_decl, so that gen_subprogram_die thinks
25028 that this is a declaration. At this point, we just want to force
25029 declaration die. */
25030 save_fn = current_function_decl;
25031 current_function_decl = NULL_TREE;
25032 gen_subprogram_die (decl, context_die);
25033 current_function_decl = save_fn;
25034 break;
25036 case VAR_DECL:
25037 /* Set external flag to force declaration die. Restore it after
25038 gen_decl_die() call. */
25039 saved_external_flag = DECL_EXTERNAL (decl);
25040 DECL_EXTERNAL (decl) = 1;
25041 gen_decl_die (decl, NULL, NULL, context_die);
25042 DECL_EXTERNAL (decl) = saved_external_flag;
25043 break;
25045 case NAMESPACE_DECL:
25046 if (dwarf_version >= 3 || !dwarf_strict)
25047 dwarf2out_decl (decl);
25048 else
25049 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25050 decl_die = comp_unit_die ();
25051 break;
25053 case TRANSLATION_UNIT_DECL:
25054 decl_die = comp_unit_die ();
25055 break;
25057 default:
25058 gcc_unreachable ();
25061 /* We should be able to find the DIE now. */
25062 if (!decl_die)
25063 decl_die = lookup_decl_die (decl);
25064 gcc_assert (decl_die);
25067 return decl_die;
25070 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25071 always returned. */
25073 static dw_die_ref
25074 force_type_die (tree type)
25076 dw_die_ref type_die;
25078 type_die = lookup_type_die (type);
25079 if (!type_die)
25081 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25083 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25084 false, context_die);
25085 gcc_assert (type_die);
25087 return type_die;
25090 /* Force out any required namespaces to be able to output DECL,
25091 and return the new context_die for it, if it's changed. */
25093 static dw_die_ref
25094 setup_namespace_context (tree thing, dw_die_ref context_die)
25096 tree context = (DECL_P (thing)
25097 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25098 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25099 /* Force out the namespace. */
25100 context_die = force_decl_die (context);
25102 return context_die;
25105 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25106 type) within its namespace, if appropriate.
25108 For compatibility with older debuggers, namespace DIEs only contain
25109 declarations; all definitions are emitted at CU scope, with
25110 DW_AT_specification pointing to the declaration (like with class
25111 members). */
25113 static dw_die_ref
25114 declare_in_namespace (tree thing, dw_die_ref context_die)
25116 dw_die_ref ns_context;
25118 if (debug_info_level <= DINFO_LEVEL_TERSE)
25119 return context_die;
25121 /* External declarations in the local scope only need to be emitted
25122 once, not once in the namespace and once in the scope.
25124 This avoids declaring the `extern' below in the
25125 namespace DIE as well as in the innermost scope:
25127 namespace S
25129 int i=5;
25130 int foo()
25132 int i=8;
25133 extern int i;
25134 return i;
25138 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25139 return context_die;
25141 /* If this decl is from an inlined function, then don't try to emit it in its
25142 namespace, as we will get confused. It would have already been emitted
25143 when the abstract instance of the inline function was emitted anyways. */
25144 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25145 return context_die;
25147 ns_context = setup_namespace_context (thing, context_die);
25149 if (ns_context != context_die)
25151 if (is_fortran ())
25152 return ns_context;
25153 if (DECL_P (thing))
25154 gen_decl_die (thing, NULL, NULL, ns_context);
25155 else
25156 gen_type_die (thing, ns_context);
25158 return context_die;
25161 /* Generate a DIE for a namespace or namespace alias. */
25163 static void
25164 gen_namespace_die (tree decl, dw_die_ref context_die)
25166 dw_die_ref namespace_die;
25168 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25169 they are an alias of. */
25170 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25172 /* Output a real namespace or module. */
25173 context_die = setup_namespace_context (decl, comp_unit_die ());
25174 namespace_die = new_die (is_fortran ()
25175 ? DW_TAG_module : DW_TAG_namespace,
25176 context_die, decl);
25177 /* For Fortran modules defined in different CU don't add src coords. */
25178 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25180 const char *name = dwarf2_name (decl, 0);
25181 if (name)
25182 add_name_attribute (namespace_die, name);
25184 else
25185 add_name_and_src_coords_attributes (namespace_die, decl);
25186 if (DECL_EXTERNAL (decl))
25187 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25188 equate_decl_number_to_die (decl, namespace_die);
25190 else
25192 /* Output a namespace alias. */
25194 /* Force out the namespace we are an alias of, if necessary. */
25195 dw_die_ref origin_die
25196 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25198 if (DECL_FILE_SCOPE_P (decl)
25199 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25200 context_die = setup_namespace_context (decl, comp_unit_die ());
25201 /* Now create the namespace alias DIE. */
25202 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25203 add_name_and_src_coords_attributes (namespace_die, decl);
25204 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25205 equate_decl_number_to_die (decl, namespace_die);
25207 if ((dwarf_version >= 5 || !dwarf_strict)
25208 && lang_hooks.decls.decl_dwarf_attribute (decl,
25209 DW_AT_export_symbols) == 1)
25210 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25212 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25213 if (want_pubnames ())
25214 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25217 /* Generate Dwarf debug information for a decl described by DECL.
25218 The return value is currently only meaningful for PARM_DECLs,
25219 for all other decls it returns NULL.
25221 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25222 It can be NULL otherwise. */
25224 static dw_die_ref
25225 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25226 dw_die_ref context_die)
25228 tree decl_or_origin = decl ? decl : origin;
25229 tree class_origin = NULL, ultimate_origin;
25231 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25232 return NULL;
25234 /* Ignore pointer bounds decls. */
25235 if (DECL_P (decl_or_origin)
25236 && TREE_TYPE (decl_or_origin)
25237 && POINTER_BOUNDS_P (decl_or_origin))
25238 return NULL;
25240 switch (TREE_CODE (decl_or_origin))
25242 case ERROR_MARK:
25243 break;
25245 case CONST_DECL:
25246 if (!is_fortran () && !is_ada ())
25248 /* The individual enumerators of an enum type get output when we output
25249 the Dwarf representation of the relevant enum type itself. */
25250 break;
25253 /* Emit its type. */
25254 gen_type_die (TREE_TYPE (decl), context_die);
25256 /* And its containing namespace. */
25257 context_die = declare_in_namespace (decl, context_die);
25259 gen_const_die (decl, context_die);
25260 break;
25262 case FUNCTION_DECL:
25263 #if 0
25264 /* FIXME */
25265 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25266 on local redeclarations of global functions. That seems broken. */
25267 if (current_function_decl != decl)
25268 /* This is only a declaration. */;
25269 #endif
25271 /* We should have abstract copies already and should not generate
25272 stray type DIEs in late LTO dumping. */
25273 if (! early_dwarf)
25276 /* If we're emitting a clone, emit info for the abstract instance. */
25277 else if (origin || DECL_ORIGIN (decl) != decl)
25278 dwarf2out_abstract_function (origin
25279 ? DECL_ORIGIN (origin)
25280 : DECL_ABSTRACT_ORIGIN (decl));
25282 /* If we're emitting a possibly inlined function emit it as
25283 abstract instance. */
25284 else if (cgraph_function_possibly_inlined_p (decl)
25285 && ! DECL_ABSTRACT_P (decl)
25286 && ! class_or_namespace_scope_p (context_die)
25287 /* dwarf2out_abstract_function won't emit a die if this is just
25288 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25289 that case, because that works only if we have a die. */
25290 && DECL_INITIAL (decl) != NULL_TREE)
25291 dwarf2out_abstract_function (decl);
25293 /* Otherwise we're emitting the primary DIE for this decl. */
25294 else if (debug_info_level > DINFO_LEVEL_TERSE)
25296 /* Before we describe the FUNCTION_DECL itself, make sure that we
25297 have its containing type. */
25298 if (!origin)
25299 origin = decl_class_context (decl);
25300 if (origin != NULL_TREE)
25301 gen_type_die (origin, context_die);
25303 /* And its return type. */
25304 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25306 /* And its virtual context. */
25307 if (DECL_VINDEX (decl) != NULL_TREE)
25308 gen_type_die (DECL_CONTEXT (decl), context_die);
25310 /* Make sure we have a member DIE for decl. */
25311 if (origin != NULL_TREE)
25312 gen_type_die_for_member (origin, decl, context_die);
25314 /* And its containing namespace. */
25315 context_die = declare_in_namespace (decl, context_die);
25318 /* Now output a DIE to represent the function itself. */
25319 if (decl)
25320 gen_subprogram_die (decl, context_die);
25321 break;
25323 case TYPE_DECL:
25324 /* If we are in terse mode, don't generate any DIEs to represent any
25325 actual typedefs. */
25326 if (debug_info_level <= DINFO_LEVEL_TERSE)
25327 break;
25329 /* In the special case of a TYPE_DECL node representing the declaration
25330 of some type tag, if the given TYPE_DECL is marked as having been
25331 instantiated from some other (original) TYPE_DECL node (e.g. one which
25332 was generated within the original definition of an inline function) we
25333 used to generate a special (abbreviated) DW_TAG_structure_type,
25334 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25335 should be actually referencing those DIEs, as variable DIEs with that
25336 type would be emitted already in the abstract origin, so it was always
25337 removed during unused type prunning. Don't add anything in this
25338 case. */
25339 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25340 break;
25342 if (is_redundant_typedef (decl))
25343 gen_type_die (TREE_TYPE (decl), context_die);
25344 else
25345 /* Output a DIE to represent the typedef itself. */
25346 gen_typedef_die (decl, context_die);
25347 break;
25349 case LABEL_DECL:
25350 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25351 gen_label_die (decl, context_die);
25352 break;
25354 case VAR_DECL:
25355 case RESULT_DECL:
25356 /* If we are in terse mode, don't generate any DIEs to represent any
25357 variable declarations or definitions. */
25358 if (debug_info_level <= DINFO_LEVEL_TERSE)
25359 break;
25361 /* Avoid generating stray type DIEs during late dwarf dumping.
25362 All types have been dumped early. */
25363 if (early_dwarf
25364 /* ??? But in LTRANS we cannot annotate early created variably
25365 modified type DIEs without copying them and adjusting all
25366 references to them. Dump them again as happens for inlining
25367 which copies both the decl and the types. */
25368 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25369 in VLA bound information for example. */
25370 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25371 current_function_decl)))
25373 /* Output any DIEs that are needed to specify the type of this data
25374 object. */
25375 if (decl_by_reference_p (decl_or_origin))
25376 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25377 else
25378 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25381 if (early_dwarf)
25383 /* And its containing type. */
25384 class_origin = decl_class_context (decl_or_origin);
25385 if (class_origin != NULL_TREE)
25386 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25388 /* And its containing namespace. */
25389 context_die = declare_in_namespace (decl_or_origin, context_die);
25392 /* Now output the DIE to represent the data object itself. This gets
25393 complicated because of the possibility that the VAR_DECL really
25394 represents an inlined instance of a formal parameter for an inline
25395 function. */
25396 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25397 if (ultimate_origin != NULL_TREE
25398 && TREE_CODE (ultimate_origin) == PARM_DECL)
25399 gen_formal_parameter_die (decl, origin,
25400 true /* Emit name attribute. */,
25401 context_die);
25402 else
25403 gen_variable_die (decl, origin, context_die);
25404 break;
25406 case FIELD_DECL:
25407 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25408 /* Ignore the nameless fields that are used to skip bits but handle C++
25409 anonymous unions and structs. */
25410 if (DECL_NAME (decl) != NULL_TREE
25411 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25412 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25414 gen_type_die (member_declared_type (decl), context_die);
25415 gen_field_die (decl, ctx, context_die);
25417 break;
25419 case PARM_DECL:
25420 /* Avoid generating stray type DIEs during late dwarf dumping.
25421 All types have been dumped early. */
25422 if (early_dwarf
25423 /* ??? But in LTRANS we cannot annotate early created variably
25424 modified type DIEs without copying them and adjusting all
25425 references to them. Dump them again as happens for inlining
25426 which copies both the decl and the types. */
25427 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25428 in VLA bound information for example. */
25429 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25430 current_function_decl)))
25432 if (DECL_BY_REFERENCE (decl_or_origin))
25433 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25434 else
25435 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25437 return gen_formal_parameter_die (decl, origin,
25438 true /* Emit name attribute. */,
25439 context_die);
25441 case NAMESPACE_DECL:
25442 if (dwarf_version >= 3 || !dwarf_strict)
25443 gen_namespace_die (decl, context_die);
25444 break;
25446 case IMPORTED_DECL:
25447 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25448 DECL_CONTEXT (decl), context_die);
25449 break;
25451 case NAMELIST_DECL:
25452 gen_namelist_decl (DECL_NAME (decl), context_die,
25453 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25454 break;
25456 default:
25457 /* Probably some frontend-internal decl. Assume we don't care. */
25458 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25459 break;
25462 return NULL;
25465 /* Output initial debug information for global DECL. Called at the
25466 end of the parsing process.
25468 This is the initial debug generation process. As such, the DIEs
25469 generated may be incomplete. A later debug generation pass
25470 (dwarf2out_late_global_decl) will augment the information generated
25471 in this pass (e.g., with complete location info). */
25473 static void
25474 dwarf2out_early_global_decl (tree decl)
25476 set_early_dwarf s;
25478 /* gen_decl_die() will set DECL_ABSTRACT because
25479 cgraph_function_possibly_inlined_p() returns true. This is in
25480 turn will cause DW_AT_inline attributes to be set.
25482 This happens because at early dwarf generation, there is no
25483 cgraph information, causing cgraph_function_possibly_inlined_p()
25484 to return true. Trick cgraph_function_possibly_inlined_p()
25485 while we generate dwarf early. */
25486 bool save = symtab->global_info_ready;
25487 symtab->global_info_ready = true;
25489 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25490 other DECLs and they can point to template types or other things
25491 that dwarf2out can't handle when done via dwarf2out_decl. */
25492 if (TREE_CODE (decl) != TYPE_DECL
25493 && TREE_CODE (decl) != PARM_DECL)
25495 if (TREE_CODE (decl) == FUNCTION_DECL)
25497 tree save_fndecl = current_function_decl;
25499 /* For nested functions, make sure we have DIEs for the parents first
25500 so that all nested DIEs are generated at the proper scope in the
25501 first shot. */
25502 tree context = decl_function_context (decl);
25503 if (context != NULL && lookup_decl_die (context) == NULL)
25505 current_function_decl = context;
25506 dwarf2out_decl (context);
25509 /* Emit an abstract origin of a function first. This happens
25510 with C++ constructor clones for example and makes
25511 dwarf2out_abstract_function happy which requires the early
25512 DIE of the abstract instance to be present. */
25513 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25514 dw_die_ref origin_die;
25515 if (origin != NULL
25516 /* Do not emit the DIE multiple times but make sure to
25517 process it fully here in case we just saw a declaration. */
25518 && ((origin_die = lookup_decl_die (origin)) == NULL
25519 || is_declaration_die (origin_die)))
25521 current_function_decl = origin;
25522 dwarf2out_decl (origin);
25525 /* Emit the DIE for decl but avoid doing that multiple times. */
25526 dw_die_ref old_die;
25527 if ((old_die = lookup_decl_die (decl)) == NULL
25528 || is_declaration_die (old_die))
25530 current_function_decl = decl;
25531 dwarf2out_decl (decl);
25534 current_function_decl = save_fndecl;
25536 else
25537 dwarf2out_decl (decl);
25539 symtab->global_info_ready = save;
25542 /* Output debug information for global decl DECL. Called from
25543 toplev.c after compilation proper has finished. */
25545 static void
25546 dwarf2out_late_global_decl (tree decl)
25548 /* Fill-in any location information we were unable to determine
25549 on the first pass. */
25550 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25552 dw_die_ref die = lookup_decl_die (decl);
25554 /* We may have to generate early debug late for LTO in case debug
25555 was not enabled at compile-time or the target doesn't support
25556 the LTO early debug scheme. */
25557 if (! die && in_lto_p)
25559 dwarf2out_decl (decl);
25560 die = lookup_decl_die (decl);
25563 if (die)
25565 /* We get called via the symtab code invoking late_global_decl
25566 for symbols that are optimized out. Do not add locations
25567 for those, except if they have a DECL_VALUE_EXPR, in which case
25568 they are relevant for debuggers. */
25569 varpool_node *node = varpool_node::get (decl);
25570 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25571 tree_add_const_value_attribute_for_decl (die, decl);
25572 else
25573 add_location_or_const_value_attribute (die, decl, false);
25578 /* Output debug information for type decl DECL. Called from toplev.c
25579 and from language front ends (to record built-in types). */
25580 static void
25581 dwarf2out_type_decl (tree decl, int local)
25583 if (!local)
25585 set_early_dwarf s;
25586 dwarf2out_decl (decl);
25590 /* Output debug information for imported module or decl DECL.
25591 NAME is non-NULL name in the lexical block if the decl has been renamed.
25592 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25593 that DECL belongs to.
25594 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25595 static void
25596 dwarf2out_imported_module_or_decl_1 (tree decl,
25597 tree name,
25598 tree lexical_block,
25599 dw_die_ref lexical_block_die)
25601 expanded_location xloc;
25602 dw_die_ref imported_die = NULL;
25603 dw_die_ref at_import_die;
25605 if (TREE_CODE (decl) == IMPORTED_DECL)
25607 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25608 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25609 gcc_assert (decl);
25611 else
25612 xloc = expand_location (input_location);
25614 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25616 at_import_die = force_type_die (TREE_TYPE (decl));
25617 /* For namespace N { typedef void T; } using N::T; base_type_die
25618 returns NULL, but DW_TAG_imported_declaration requires
25619 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25620 if (!at_import_die)
25622 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25623 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25624 at_import_die = lookup_type_die (TREE_TYPE (decl));
25625 gcc_assert (at_import_die);
25628 else
25630 at_import_die = lookup_decl_die (decl);
25631 if (!at_import_die)
25633 /* If we're trying to avoid duplicate debug info, we may not have
25634 emitted the member decl for this field. Emit it now. */
25635 if (TREE_CODE (decl) == FIELD_DECL)
25637 tree type = DECL_CONTEXT (decl);
25639 if (TYPE_CONTEXT (type)
25640 && TYPE_P (TYPE_CONTEXT (type))
25641 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25642 DINFO_USAGE_DIR_USE))
25643 return;
25644 gen_type_die_for_member (type, decl,
25645 get_context_die (TYPE_CONTEXT (type)));
25647 if (TREE_CODE (decl) == NAMELIST_DECL)
25648 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25649 get_context_die (DECL_CONTEXT (decl)),
25650 NULL_TREE);
25651 else
25652 at_import_die = force_decl_die (decl);
25656 if (TREE_CODE (decl) == NAMESPACE_DECL)
25658 if (dwarf_version >= 3 || !dwarf_strict)
25659 imported_die = new_die (DW_TAG_imported_module,
25660 lexical_block_die,
25661 lexical_block);
25662 else
25663 return;
25665 else
25666 imported_die = new_die (DW_TAG_imported_declaration,
25667 lexical_block_die,
25668 lexical_block);
25670 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25671 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25672 if (debug_column_info && xloc.column)
25673 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25674 if (name)
25675 add_AT_string (imported_die, DW_AT_name,
25676 IDENTIFIER_POINTER (name));
25677 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25680 /* Output debug information for imported module or decl DECL.
25681 NAME is non-NULL name in context if the decl has been renamed.
25682 CHILD is true if decl is one of the renamed decls as part of
25683 importing whole module.
25684 IMPLICIT is set if this hook is called for an implicit import
25685 such as inline namespace. */
25687 static void
25688 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25689 bool child, bool implicit)
25691 /* dw_die_ref at_import_die; */
25692 dw_die_ref scope_die;
25694 if (debug_info_level <= DINFO_LEVEL_TERSE)
25695 return;
25697 gcc_assert (decl);
25699 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25700 should be enough, for DWARF4 and older even if we emit as extension
25701 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25702 for the benefit of consumers unaware of DW_AT_export_symbols. */
25703 if (implicit
25704 && dwarf_version >= 5
25705 && lang_hooks.decls.decl_dwarf_attribute (decl,
25706 DW_AT_export_symbols) == 1)
25707 return;
25709 set_early_dwarf s;
25711 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25712 We need decl DIE for reference and scope die. First, get DIE for the decl
25713 itself. */
25715 /* Get the scope die for decl context. Use comp_unit_die for global module
25716 or decl. If die is not found for non globals, force new die. */
25717 if (context
25718 && TYPE_P (context)
25719 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25720 return;
25722 scope_die = get_context_die (context);
25724 if (child)
25726 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25727 there is nothing we can do, here. */
25728 if (dwarf_version < 3 && dwarf_strict)
25729 return;
25731 gcc_assert (scope_die->die_child);
25732 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25733 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25734 scope_die = scope_die->die_child;
25737 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25738 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25741 /* Output debug information for namelists. */
25743 static dw_die_ref
25744 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25746 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25747 tree value;
25748 unsigned i;
25750 if (debug_info_level <= DINFO_LEVEL_TERSE)
25751 return NULL;
25753 gcc_assert (scope_die != NULL);
25754 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25755 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25757 /* If there are no item_decls, we have a nondefining namelist, e.g.
25758 with USE association; hence, set DW_AT_declaration. */
25759 if (item_decls == NULL_TREE)
25761 add_AT_flag (nml_die, DW_AT_declaration, 1);
25762 return nml_die;
25765 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25767 nml_item_ref_die = lookup_decl_die (value);
25768 if (!nml_item_ref_die)
25769 nml_item_ref_die = force_decl_die (value);
25771 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25772 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25774 return nml_die;
25778 /* Write the debugging output for DECL and return the DIE. */
25780 static void
25781 dwarf2out_decl (tree decl)
25783 dw_die_ref context_die = comp_unit_die ();
25785 switch (TREE_CODE (decl))
25787 case ERROR_MARK:
25788 return;
25790 case FUNCTION_DECL:
25791 /* If we're a nested function, initially use a parent of NULL; if we're
25792 a plain function, this will be fixed up in decls_for_scope. If
25793 we're a method, it will be ignored, since we already have a DIE. */
25794 if (decl_function_context (decl)
25795 /* But if we're in terse mode, we don't care about scope. */
25796 && debug_info_level > DINFO_LEVEL_TERSE)
25797 context_die = NULL;
25798 break;
25800 case VAR_DECL:
25801 /* For local statics lookup proper context die. */
25802 if (local_function_static (decl))
25803 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25805 /* If we are in terse mode, don't generate any DIEs to represent any
25806 variable declarations or definitions. */
25807 if (debug_info_level <= DINFO_LEVEL_TERSE)
25808 return;
25809 break;
25811 case CONST_DECL:
25812 if (debug_info_level <= DINFO_LEVEL_TERSE)
25813 return;
25814 if (!is_fortran () && !is_ada ())
25815 return;
25816 if (TREE_STATIC (decl) && decl_function_context (decl))
25817 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25818 break;
25820 case NAMESPACE_DECL:
25821 case IMPORTED_DECL:
25822 if (debug_info_level <= DINFO_LEVEL_TERSE)
25823 return;
25824 if (lookup_decl_die (decl) != NULL)
25825 return;
25826 break;
25828 case TYPE_DECL:
25829 /* Don't emit stubs for types unless they are needed by other DIEs. */
25830 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
25831 return;
25833 /* Don't bother trying to generate any DIEs to represent any of the
25834 normal built-in types for the language we are compiling. */
25835 if (DECL_IS_BUILTIN (decl))
25836 return;
25838 /* If we are in terse mode, don't generate any DIEs for types. */
25839 if (debug_info_level <= DINFO_LEVEL_TERSE)
25840 return;
25842 /* If we're a function-scope tag, initially use a parent of NULL;
25843 this will be fixed up in decls_for_scope. */
25844 if (decl_function_context (decl))
25845 context_die = NULL;
25847 break;
25849 case NAMELIST_DECL:
25850 break;
25852 default:
25853 return;
25856 gen_decl_die (decl, NULL, NULL, context_die);
25858 if (flag_checking)
25860 dw_die_ref die = lookup_decl_die (decl);
25861 if (die)
25862 check_die (die);
25866 /* Write the debugging output for DECL. */
25868 static void
25869 dwarf2out_function_decl (tree decl)
25871 dwarf2out_decl (decl);
25872 call_arg_locations = NULL;
25873 call_arg_loc_last = NULL;
25874 call_site_count = -1;
25875 tail_call_site_count = -1;
25876 decl_loc_table->empty ();
25877 cached_dw_loc_list_table->empty ();
25880 /* Output a marker (i.e. a label) for the beginning of the generated code for
25881 a lexical block. */
25883 static void
25884 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
25885 unsigned int blocknum)
25887 switch_to_section (current_function_section ());
25888 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
25891 /* Output a marker (i.e. a label) for the end of the generated code for a
25892 lexical block. */
25894 static void
25895 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
25897 switch_to_section (current_function_section ());
25898 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
25901 /* Returns nonzero if it is appropriate not to emit any debugging
25902 information for BLOCK, because it doesn't contain any instructions.
25904 Don't allow this for blocks with nested functions or local classes
25905 as we would end up with orphans, and in the presence of scheduling
25906 we may end up calling them anyway. */
25908 static bool
25909 dwarf2out_ignore_block (const_tree block)
25911 tree decl;
25912 unsigned int i;
25914 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
25915 if (TREE_CODE (decl) == FUNCTION_DECL
25916 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25917 return 0;
25918 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
25920 decl = BLOCK_NONLOCALIZED_VAR (block, i);
25921 if (TREE_CODE (decl) == FUNCTION_DECL
25922 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
25923 return 0;
25926 return 1;
25929 /* Hash table routines for file_hash. */
25931 bool
25932 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
25934 return filename_cmp (p1->filename, p2) == 0;
25937 hashval_t
25938 dwarf_file_hasher::hash (dwarf_file_data *p)
25940 return htab_hash_string (p->filename);
25943 /* Lookup FILE_NAME (in the list of filenames that we know about here in
25944 dwarf2out.c) and return its "index". The index of each (known) filename is
25945 just a unique number which is associated with only that one filename. We
25946 need such numbers for the sake of generating labels (in the .debug_sfnames
25947 section) and references to those files numbers (in the .debug_srcinfo
25948 and .debug_macinfo sections). If the filename given as an argument is not
25949 found in our current list, add it to the list and assign it the next
25950 available unique index number. */
25952 static struct dwarf_file_data *
25953 lookup_filename (const char *file_name)
25955 struct dwarf_file_data * created;
25957 if (!file_name)
25958 return NULL;
25960 dwarf_file_data **slot
25961 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
25962 INSERT);
25963 if (*slot)
25964 return *slot;
25966 created = ggc_alloc<dwarf_file_data> ();
25967 created->filename = file_name;
25968 created->emitted_number = 0;
25969 *slot = created;
25970 return created;
25973 /* If the assembler will construct the file table, then translate the compiler
25974 internal file table number into the assembler file table number, and emit
25975 a .file directive if we haven't already emitted one yet. The file table
25976 numbers are different because we prune debug info for unused variables and
25977 types, which may include filenames. */
25979 static int
25980 maybe_emit_file (struct dwarf_file_data * fd)
25982 if (! fd->emitted_number)
25984 if (last_emitted_file)
25985 fd->emitted_number = last_emitted_file->emitted_number + 1;
25986 else
25987 fd->emitted_number = 1;
25988 last_emitted_file = fd;
25990 if (DWARF2_ASM_LINE_DEBUG_INFO)
25992 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
25993 output_quoted_string (asm_out_file,
25994 remap_debug_filename (fd->filename));
25995 fputc ('\n', asm_out_file);
25999 return fd->emitted_number;
26002 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26003 That generation should happen after function debug info has been
26004 generated. The value of the attribute is the constant value of ARG. */
26006 static void
26007 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26009 die_arg_entry entry;
26011 if (!die || !arg)
26012 return;
26014 gcc_assert (early_dwarf);
26016 if (!tmpl_value_parm_die_table)
26017 vec_alloc (tmpl_value_parm_die_table, 32);
26019 entry.die = die;
26020 entry.arg = arg;
26021 vec_safe_push (tmpl_value_parm_die_table, entry);
26024 /* Return TRUE if T is an instance of generic type, FALSE
26025 otherwise. */
26027 static bool
26028 generic_type_p (tree t)
26030 if (t == NULL_TREE || !TYPE_P (t))
26031 return false;
26032 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26035 /* Schedule the generation of the generic parameter dies for the
26036 instance of generic type T. The proper generation itself is later
26037 done by gen_scheduled_generic_parms_dies. */
26039 static void
26040 schedule_generic_params_dies_gen (tree t)
26042 if (!generic_type_p (t))
26043 return;
26045 gcc_assert (early_dwarf);
26047 if (!generic_type_instances)
26048 vec_alloc (generic_type_instances, 256);
26050 vec_safe_push (generic_type_instances, t);
26053 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26054 by append_entry_to_tmpl_value_parm_die_table. This function must
26055 be called after function DIEs have been generated. */
26057 static void
26058 gen_remaining_tmpl_value_param_die_attribute (void)
26060 if (tmpl_value_parm_die_table)
26062 unsigned i, j;
26063 die_arg_entry *e;
26065 /* We do this in two phases - first get the cases we can
26066 handle during early-finish, preserving those we cannot
26067 (containing symbolic constants where we don't yet know
26068 whether we are going to output the referenced symbols).
26069 For those we try again at late-finish. */
26070 j = 0;
26071 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26073 if (!e->die->removed
26074 && !tree_add_const_value_attribute (e->die, e->arg))
26076 dw_loc_descr_ref loc = NULL;
26077 if (! early_dwarf
26078 && (dwarf_version >= 5 || !dwarf_strict))
26079 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26080 if (loc)
26081 add_AT_loc (e->die, DW_AT_location, loc);
26082 else
26083 (*tmpl_value_parm_die_table)[j++] = *e;
26086 tmpl_value_parm_die_table->truncate (j);
26090 /* Generate generic parameters DIEs for instances of generic types
26091 that have been previously scheduled by
26092 schedule_generic_params_dies_gen. This function must be called
26093 after all the types of the CU have been laid out. */
26095 static void
26096 gen_scheduled_generic_parms_dies (void)
26098 unsigned i;
26099 tree t;
26101 if (!generic_type_instances)
26102 return;
26104 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26105 if (COMPLETE_TYPE_P (t))
26106 gen_generic_params_dies (t);
26108 generic_type_instances = NULL;
26112 /* Replace DW_AT_name for the decl with name. */
26114 static void
26115 dwarf2out_set_name (tree decl, tree name)
26117 dw_die_ref die;
26118 dw_attr_node *attr;
26119 const char *dname;
26121 die = TYPE_SYMTAB_DIE (decl);
26122 if (!die)
26123 return;
26125 dname = dwarf2_name (name, 0);
26126 if (!dname)
26127 return;
26129 attr = get_AT (die, DW_AT_name);
26130 if (attr)
26132 struct indirect_string_node *node;
26134 node = find_AT_string (dname);
26135 /* replace the string. */
26136 attr->dw_attr_val.v.val_str = node;
26139 else
26140 add_name_attribute (die, dname);
26143 /* True if before or during processing of the first function being emitted. */
26144 static bool in_first_function_p = true;
26145 /* True if loc_note during dwarf2out_var_location call might still be
26146 before first real instruction at address equal to .Ltext0. */
26147 static bool maybe_at_text_label_p = true;
26148 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26149 static unsigned int first_loclabel_num_not_at_text_label;
26151 /* Called by the final INSN scan whenever we see a var location. We
26152 use it to drop labels in the right places, and throw the location in
26153 our lookup table. */
26155 static void
26156 dwarf2out_var_location (rtx_insn *loc_note)
26158 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26159 struct var_loc_node *newloc;
26160 rtx_insn *next_real, *next_note;
26161 rtx_insn *call_insn = NULL;
26162 static const char *last_label;
26163 static const char *last_postcall_label;
26164 static bool last_in_cold_section_p;
26165 static rtx_insn *expected_next_loc_note;
26166 tree decl;
26167 bool var_loc_p;
26169 if (!NOTE_P (loc_note))
26171 if (CALL_P (loc_note))
26173 call_site_count++;
26174 if (SIBLING_CALL_P (loc_note))
26175 tail_call_site_count++;
26176 if (optimize == 0 && !flag_var_tracking)
26178 /* When the var-tracking pass is not running, there is no note
26179 for indirect calls whose target is compile-time known. In this
26180 case, process such calls specifically so that we generate call
26181 sites for them anyway. */
26182 rtx x = PATTERN (loc_note);
26183 if (GET_CODE (x) == PARALLEL)
26184 x = XVECEXP (x, 0, 0);
26185 if (GET_CODE (x) == SET)
26186 x = SET_SRC (x);
26187 if (GET_CODE (x) == CALL)
26188 x = XEXP (x, 0);
26189 if (!MEM_P (x)
26190 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26191 || !SYMBOL_REF_DECL (XEXP (x, 0))
26192 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26193 != FUNCTION_DECL))
26195 call_insn = loc_note;
26196 loc_note = NULL;
26197 var_loc_p = false;
26199 next_real = next_real_insn (call_insn);
26200 next_note = NULL;
26201 cached_next_real_insn = NULL;
26202 goto create_label;
26206 return;
26209 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26210 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26211 return;
26213 /* Optimize processing a large consecutive sequence of location
26214 notes so we don't spend too much time in next_real_insn. If the
26215 next insn is another location note, remember the next_real_insn
26216 calculation for next time. */
26217 next_real = cached_next_real_insn;
26218 if (next_real)
26220 if (expected_next_loc_note != loc_note)
26221 next_real = NULL;
26224 next_note = NEXT_INSN (loc_note);
26225 if (! next_note
26226 || next_note->deleted ()
26227 || ! NOTE_P (next_note)
26228 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26229 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26230 next_note = NULL;
26232 if (! next_real)
26233 next_real = next_real_insn (loc_note);
26235 if (next_note)
26237 expected_next_loc_note = next_note;
26238 cached_next_real_insn = next_real;
26240 else
26241 cached_next_real_insn = NULL;
26243 /* If there are no instructions which would be affected by this note,
26244 don't do anything. */
26245 if (var_loc_p
26246 && next_real == NULL_RTX
26247 && !NOTE_DURING_CALL_P (loc_note))
26248 return;
26250 create_label:
26252 if (next_real == NULL_RTX)
26253 next_real = get_last_insn ();
26255 /* If there were any real insns between note we processed last time
26256 and this note (or if it is the first note), clear
26257 last_{,postcall_}label so that they are not reused this time. */
26258 if (last_var_location_insn == NULL_RTX
26259 || last_var_location_insn != next_real
26260 || last_in_cold_section_p != in_cold_section_p)
26262 last_label = NULL;
26263 last_postcall_label = NULL;
26266 if (var_loc_p)
26268 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26269 newloc = add_var_loc_to_decl (decl, loc_note,
26270 NOTE_DURING_CALL_P (loc_note)
26271 ? last_postcall_label : last_label);
26272 if (newloc == NULL)
26273 return;
26275 else
26277 decl = NULL_TREE;
26278 newloc = NULL;
26281 /* If there were no real insns between note we processed last time
26282 and this note, use the label we emitted last time. Otherwise
26283 create a new label and emit it. */
26284 if (last_label == NULL)
26286 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26287 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26288 loclabel_num++;
26289 last_label = ggc_strdup (loclabel);
26290 /* See if loclabel might be equal to .Ltext0. If yes,
26291 bump first_loclabel_num_not_at_text_label. */
26292 if (!have_multiple_function_sections
26293 && in_first_function_p
26294 && maybe_at_text_label_p)
26296 static rtx_insn *last_start;
26297 rtx_insn *insn;
26298 for (insn = loc_note; insn; insn = previous_insn (insn))
26299 if (insn == last_start)
26300 break;
26301 else if (!NONDEBUG_INSN_P (insn))
26302 continue;
26303 else
26305 rtx body = PATTERN (insn);
26306 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26307 continue;
26308 /* Inline asm could occupy zero bytes. */
26309 else if (GET_CODE (body) == ASM_INPUT
26310 || asm_noperands (body) >= 0)
26311 continue;
26312 #ifdef HAVE_attr_length
26313 else if (get_attr_min_length (insn) == 0)
26314 continue;
26315 #endif
26316 else
26318 /* Assume insn has non-zero length. */
26319 maybe_at_text_label_p = false;
26320 break;
26323 if (maybe_at_text_label_p)
26325 last_start = loc_note;
26326 first_loclabel_num_not_at_text_label = loclabel_num;
26331 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26332 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26334 if (!var_loc_p)
26336 struct call_arg_loc_node *ca_loc
26337 = ggc_cleared_alloc<call_arg_loc_node> ();
26338 rtx_insn *prev
26339 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26341 ca_loc->call_arg_loc_note = loc_note;
26342 ca_loc->next = NULL;
26343 ca_loc->label = last_label;
26344 gcc_assert (prev
26345 && (CALL_P (prev)
26346 || (NONJUMP_INSN_P (prev)
26347 && GET_CODE (PATTERN (prev)) == SEQUENCE
26348 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26349 if (!CALL_P (prev))
26350 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26351 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26353 /* Look for a SYMBOL_REF in the "prev" instruction. */
26354 rtx x = get_call_rtx_from (PATTERN (prev));
26355 if (x)
26357 /* Try to get the call symbol, if any. */
26358 if (MEM_P (XEXP (x, 0)))
26359 x = XEXP (x, 0);
26360 /* First, look for a memory access to a symbol_ref. */
26361 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26362 && SYMBOL_REF_DECL (XEXP (x, 0))
26363 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26364 ca_loc->symbol_ref = XEXP (x, 0);
26365 /* Otherwise, look at a compile-time known user-level function
26366 declaration. */
26367 else if (MEM_P (x)
26368 && MEM_EXPR (x)
26369 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26370 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26373 ca_loc->block = insn_scope (prev);
26374 if (call_arg_locations)
26375 call_arg_loc_last->next = ca_loc;
26376 else
26377 call_arg_locations = ca_loc;
26378 call_arg_loc_last = ca_loc;
26380 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26381 newloc->label = last_label;
26382 else
26384 if (!last_postcall_label)
26386 sprintf (loclabel, "%s-1", last_label);
26387 last_postcall_label = ggc_strdup (loclabel);
26389 newloc->label = last_postcall_label;
26392 last_var_location_insn = next_real;
26393 last_in_cold_section_p = in_cold_section_p;
26396 /* Called from finalize_size_functions for size functions so that their body
26397 can be encoded in the debug info to describe the layout of variable-length
26398 structures. */
26400 static void
26401 dwarf2out_size_function (tree decl)
26403 function_to_dwarf_procedure (decl);
26406 /* Note in one location list that text section has changed. */
26409 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26411 var_loc_list *list = *slot;
26412 if (list->first)
26413 list->last_before_switch
26414 = list->last->next ? list->last->next : list->last;
26415 return 1;
26418 /* Note in all location lists that text section has changed. */
26420 static void
26421 var_location_switch_text_section (void)
26423 if (decl_loc_table == NULL)
26424 return;
26426 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26429 /* Create a new line number table. */
26431 static dw_line_info_table *
26432 new_line_info_table (void)
26434 dw_line_info_table *table;
26436 table = ggc_cleared_alloc<dw_line_info_table> ();
26437 table->file_num = 1;
26438 table->line_num = 1;
26439 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26441 return table;
26444 /* Lookup the "current" table into which we emit line info, so
26445 that we don't have to do it for every source line. */
26447 static void
26448 set_cur_line_info_table (section *sec)
26450 dw_line_info_table *table;
26452 if (sec == text_section)
26453 table = text_section_line_info;
26454 else if (sec == cold_text_section)
26456 table = cold_text_section_line_info;
26457 if (!table)
26459 cold_text_section_line_info = table = new_line_info_table ();
26460 table->end_label = cold_end_label;
26463 else
26465 const char *end_label;
26467 if (crtl->has_bb_partition)
26469 if (in_cold_section_p)
26470 end_label = crtl->subsections.cold_section_end_label;
26471 else
26472 end_label = crtl->subsections.hot_section_end_label;
26474 else
26476 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26477 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26478 current_function_funcdef_no);
26479 end_label = ggc_strdup (label);
26482 table = new_line_info_table ();
26483 table->end_label = end_label;
26485 vec_safe_push (separate_line_info, table);
26488 if (DWARF2_ASM_LINE_DEBUG_INFO)
26489 table->is_stmt = (cur_line_info_table
26490 ? cur_line_info_table->is_stmt
26491 : DWARF_LINE_DEFAULT_IS_STMT_START);
26492 cur_line_info_table = table;
26496 /* We need to reset the locations at the beginning of each
26497 function. We can't do this in the end_function hook, because the
26498 declarations that use the locations won't have been output when
26499 that hook is called. Also compute have_multiple_function_sections here. */
26501 static void
26502 dwarf2out_begin_function (tree fun)
26504 section *sec = function_section (fun);
26506 if (sec != text_section)
26507 have_multiple_function_sections = true;
26509 if (crtl->has_bb_partition && !cold_text_section)
26511 gcc_assert (current_function_decl == fun);
26512 cold_text_section = unlikely_text_section ();
26513 switch_to_section (cold_text_section);
26514 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26515 switch_to_section (sec);
26518 dwarf2out_note_section_used ();
26519 call_site_count = 0;
26520 tail_call_site_count = 0;
26522 set_cur_line_info_table (sec);
26525 /* Helper function of dwarf2out_end_function, called only after emitting
26526 the very first function into assembly. Check if some .debug_loc range
26527 might end with a .LVL* label that could be equal to .Ltext0.
26528 In that case we must force using absolute addresses in .debug_loc ranges,
26529 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26530 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26531 list terminator.
26532 Set have_multiple_function_sections to true in that case and
26533 terminate htab traversal. */
26536 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26538 var_loc_list *entry = *slot;
26539 struct var_loc_node *node;
26541 node = entry->first;
26542 if (node && node->next && node->next->label)
26544 unsigned int i;
26545 const char *label = node->next->label;
26546 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26548 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26550 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26551 if (strcmp (label, loclabel) == 0)
26553 have_multiple_function_sections = true;
26554 return 0;
26558 return 1;
26561 /* Hook called after emitting a function into assembly.
26562 This does something only for the very first function emitted. */
26564 static void
26565 dwarf2out_end_function (unsigned int)
26567 if (in_first_function_p
26568 && !have_multiple_function_sections
26569 && first_loclabel_num_not_at_text_label
26570 && decl_loc_table)
26571 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26572 in_first_function_p = false;
26573 maybe_at_text_label_p = false;
26576 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26577 front-ends register a translation unit even before dwarf2out_init is
26578 called. */
26579 static tree main_translation_unit = NULL_TREE;
26581 /* Hook called by front-ends after they built their main translation unit.
26582 Associate comp_unit_die to UNIT. */
26584 static void
26585 dwarf2out_register_main_translation_unit (tree unit)
26587 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26588 && main_translation_unit == NULL_TREE);
26589 main_translation_unit = unit;
26590 /* If dwarf2out_init has not been called yet, it will perform the association
26591 itself looking at main_translation_unit. */
26592 if (decl_die_table != NULL)
26593 equate_decl_number_to_die (unit, comp_unit_die ());
26596 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26598 static void
26599 push_dw_line_info_entry (dw_line_info_table *table,
26600 enum dw_line_info_opcode opcode, unsigned int val)
26602 dw_line_info_entry e;
26603 e.opcode = opcode;
26604 e.val = val;
26605 vec_safe_push (table->entries, e);
26608 /* Output a label to mark the beginning of a source code line entry
26609 and record information relating to this source line, in
26610 'line_info_table' for later output of the .debug_line section. */
26611 /* ??? The discriminator parameter ought to be unsigned. */
26613 static void
26614 dwarf2out_source_line (unsigned int line, unsigned int column,
26615 const char *filename,
26616 int discriminator, bool is_stmt)
26618 unsigned int file_num;
26619 dw_line_info_table *table;
26621 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26622 return;
26624 /* The discriminator column was added in dwarf4. Simplify the below
26625 by simply removing it if we're not supposed to output it. */
26626 if (dwarf_version < 4 && dwarf_strict)
26627 discriminator = 0;
26629 if (!debug_column_info)
26630 column = 0;
26632 table = cur_line_info_table;
26633 file_num = maybe_emit_file (lookup_filename (filename));
26635 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26636 the debugger has used the second (possibly duplicate) line number
26637 at the beginning of the function to mark the end of the prologue.
26638 We could eliminate any other duplicates within the function. For
26639 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26640 that second line number entry. */
26641 /* Recall that this end-of-prologue indication is *not* the same thing
26642 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26643 to which the hook corresponds, follows the last insn that was
26644 emitted by gen_prologue. What we need is to precede the first insn
26645 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26646 insn that corresponds to something the user wrote. These may be
26647 very different locations once scheduling is enabled. */
26649 if (0 && file_num == table->file_num
26650 && line == table->line_num
26651 && column == table->column_num
26652 && discriminator == table->discrim_num
26653 && is_stmt == table->is_stmt)
26654 return;
26656 switch_to_section (current_function_section ());
26658 /* If requested, emit something human-readable. */
26659 if (flag_debug_asm)
26661 if (debug_column_info)
26662 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26663 filename, line, column);
26664 else
26665 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26666 filename, line);
26669 if (DWARF2_ASM_LINE_DEBUG_INFO)
26671 /* Emit the .loc directive understood by GNU as. */
26672 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26673 file_num, line, is_stmt, discriminator */
26674 fputs ("\t.loc ", asm_out_file);
26675 fprint_ul (asm_out_file, file_num);
26676 putc (' ', asm_out_file);
26677 fprint_ul (asm_out_file, line);
26678 putc (' ', asm_out_file);
26679 if (debug_column_info)
26680 fprint_ul (asm_out_file, column);
26681 else
26682 putc ('0', asm_out_file);
26684 if (is_stmt != table->is_stmt)
26686 fputs (" is_stmt ", asm_out_file);
26687 putc (is_stmt ? '1' : '0', asm_out_file);
26689 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26691 gcc_assert (discriminator > 0);
26692 fputs (" discriminator ", asm_out_file);
26693 fprint_ul (asm_out_file, (unsigned long) discriminator);
26695 putc ('\n', asm_out_file);
26697 else
26699 unsigned int label_num = ++line_info_label_num;
26701 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26703 push_dw_line_info_entry (table, LI_set_address, label_num);
26704 if (file_num != table->file_num)
26705 push_dw_line_info_entry (table, LI_set_file, file_num);
26706 if (discriminator != table->discrim_num)
26707 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26708 if (is_stmt != table->is_stmt)
26709 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26710 push_dw_line_info_entry (table, LI_set_line, line);
26711 if (debug_column_info)
26712 push_dw_line_info_entry (table, LI_set_column, column);
26715 table->file_num = file_num;
26716 table->line_num = line;
26717 table->column_num = column;
26718 table->discrim_num = discriminator;
26719 table->is_stmt = is_stmt;
26720 table->in_use = true;
26723 /* Record the beginning of a new source file. */
26725 static void
26726 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26728 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26730 macinfo_entry e;
26731 e.code = DW_MACINFO_start_file;
26732 e.lineno = lineno;
26733 e.info = ggc_strdup (filename);
26734 vec_safe_push (macinfo_table, e);
26738 /* Record the end of a source file. */
26740 static void
26741 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26743 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26745 macinfo_entry e;
26746 e.code = DW_MACINFO_end_file;
26747 e.lineno = lineno;
26748 e.info = NULL;
26749 vec_safe_push (macinfo_table, e);
26753 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26754 the tail part of the directive line, i.e. the part which is past the
26755 initial whitespace, #, whitespace, directive-name, whitespace part. */
26757 static void
26758 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26759 const char *buffer ATTRIBUTE_UNUSED)
26761 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26763 macinfo_entry e;
26764 /* Insert a dummy first entry to be able to optimize the whole
26765 predefined macro block using DW_MACRO_import. */
26766 if (macinfo_table->is_empty () && lineno <= 1)
26768 e.code = 0;
26769 e.lineno = 0;
26770 e.info = NULL;
26771 vec_safe_push (macinfo_table, e);
26773 e.code = DW_MACINFO_define;
26774 e.lineno = lineno;
26775 e.info = ggc_strdup (buffer);
26776 vec_safe_push (macinfo_table, e);
26780 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26781 the tail part of the directive line, i.e. the part which is past the
26782 initial whitespace, #, whitespace, directive-name, whitespace part. */
26784 static void
26785 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26786 const char *buffer ATTRIBUTE_UNUSED)
26788 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26790 macinfo_entry e;
26791 /* Insert a dummy first entry to be able to optimize the whole
26792 predefined macro block using DW_MACRO_import. */
26793 if (macinfo_table->is_empty () && lineno <= 1)
26795 e.code = 0;
26796 e.lineno = 0;
26797 e.info = NULL;
26798 vec_safe_push (macinfo_table, e);
26800 e.code = DW_MACINFO_undef;
26801 e.lineno = lineno;
26802 e.info = ggc_strdup (buffer);
26803 vec_safe_push (macinfo_table, e);
26807 /* Helpers to manipulate hash table of CUs. */
26809 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
26811 static inline hashval_t hash (const macinfo_entry *);
26812 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
26815 inline hashval_t
26816 macinfo_entry_hasher::hash (const macinfo_entry *entry)
26818 return htab_hash_string (entry->info);
26821 inline bool
26822 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
26823 const macinfo_entry *entry2)
26825 return !strcmp (entry1->info, entry2->info);
26828 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
26830 /* Output a single .debug_macinfo entry. */
26832 static void
26833 output_macinfo_op (macinfo_entry *ref)
26835 int file_num;
26836 size_t len;
26837 struct indirect_string_node *node;
26838 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26839 struct dwarf_file_data *fd;
26841 switch (ref->code)
26843 case DW_MACINFO_start_file:
26844 fd = lookup_filename (ref->info);
26845 file_num = maybe_emit_file (fd);
26846 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
26847 dw2_asm_output_data_uleb128 (ref->lineno,
26848 "Included from line number %lu",
26849 (unsigned long) ref->lineno);
26850 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
26851 break;
26852 case DW_MACINFO_end_file:
26853 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
26854 break;
26855 case DW_MACINFO_define:
26856 case DW_MACINFO_undef:
26857 len = strlen (ref->info) + 1;
26858 if (!dwarf_strict
26859 && len > DWARF_OFFSET_SIZE
26860 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
26861 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
26863 ref->code = ref->code == DW_MACINFO_define
26864 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
26865 output_macinfo_op (ref);
26866 return;
26868 dw2_asm_output_data (1, ref->code,
26869 ref->code == DW_MACINFO_define
26870 ? "Define macro" : "Undefine macro");
26871 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26872 (unsigned long) ref->lineno);
26873 dw2_asm_output_nstring (ref->info, -1, "The macro");
26874 break;
26875 case DW_MACRO_define_strp:
26876 case DW_MACRO_undef_strp:
26877 node = find_AT_string (ref->info);
26878 gcc_assert (node
26879 && (node->form == DW_FORM_strp
26880 || node->form == DW_FORM_GNU_str_index));
26881 dw2_asm_output_data (1, ref->code,
26882 ref->code == DW_MACRO_define_strp
26883 ? "Define macro strp"
26884 : "Undefine macro strp");
26885 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
26886 (unsigned long) ref->lineno);
26887 if (node->form == DW_FORM_strp)
26888 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
26889 debug_str_section, "The macro: \"%s\"",
26890 ref->info);
26891 else
26892 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
26893 ref->info);
26894 break;
26895 case DW_MACRO_import:
26896 dw2_asm_output_data (1, ref->code, "Import");
26897 ASM_GENERATE_INTERNAL_LABEL (label,
26898 DEBUG_MACRO_SECTION_LABEL,
26899 ref->lineno + macinfo_label_base);
26900 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
26901 break;
26902 default:
26903 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
26904 ASM_COMMENT_START, (unsigned long) ref->code);
26905 break;
26909 /* Attempt to make a sequence of define/undef macinfo ops shareable with
26910 other compilation unit .debug_macinfo sections. IDX is the first
26911 index of a define/undef, return the number of ops that should be
26912 emitted in a comdat .debug_macinfo section and emit
26913 a DW_MACRO_import entry referencing it.
26914 If the define/undef entry should be emitted normally, return 0. */
26916 static unsigned
26917 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
26918 macinfo_hash_type **macinfo_htab)
26920 macinfo_entry *first, *second, *cur, *inc;
26921 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
26922 unsigned char checksum[16];
26923 struct md5_ctx ctx;
26924 char *grp_name, *tail;
26925 const char *base;
26926 unsigned int i, count, encoded_filename_len, linebuf_len;
26927 macinfo_entry **slot;
26929 first = &(*macinfo_table)[idx];
26930 second = &(*macinfo_table)[idx + 1];
26932 /* Optimize only if there are at least two consecutive define/undef ops,
26933 and either all of them are before first DW_MACINFO_start_file
26934 with lineno {0,1} (i.e. predefined macro block), or all of them are
26935 in some included header file. */
26936 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
26937 return 0;
26938 if (vec_safe_is_empty (files))
26940 if (first->lineno > 1 || second->lineno > 1)
26941 return 0;
26943 else if (first->lineno == 0)
26944 return 0;
26946 /* Find the last define/undef entry that can be grouped together
26947 with first and at the same time compute md5 checksum of their
26948 codes, linenumbers and strings. */
26949 md5_init_ctx (&ctx);
26950 for (i = idx; macinfo_table->iterate (i, &cur); i++)
26951 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
26952 break;
26953 else if (vec_safe_is_empty (files) && cur->lineno > 1)
26954 break;
26955 else
26957 unsigned char code = cur->code;
26958 md5_process_bytes (&code, 1, &ctx);
26959 checksum_uleb128 (cur->lineno, &ctx);
26960 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
26962 md5_finish_ctx (&ctx, checksum);
26963 count = i - idx;
26965 /* From the containing include filename (if any) pick up just
26966 usable characters from its basename. */
26967 if (vec_safe_is_empty (files))
26968 base = "";
26969 else
26970 base = lbasename (files->last ().info);
26971 for (encoded_filename_len = 0, i = 0; base[i]; i++)
26972 if (ISIDNUM (base[i]) || base[i] == '.')
26973 encoded_filename_len++;
26974 /* Count . at the end. */
26975 if (encoded_filename_len)
26976 encoded_filename_len++;
26978 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
26979 linebuf_len = strlen (linebuf);
26981 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
26982 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
26983 + 16 * 2 + 1);
26984 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
26985 tail = grp_name + 4;
26986 if (encoded_filename_len)
26988 for (i = 0; base[i]; i++)
26989 if (ISIDNUM (base[i]) || base[i] == '.')
26990 *tail++ = base[i];
26991 *tail++ = '.';
26993 memcpy (tail, linebuf, linebuf_len);
26994 tail += linebuf_len;
26995 *tail++ = '.';
26996 for (i = 0; i < 16; i++)
26997 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
26999 /* Construct a macinfo_entry for DW_MACRO_import
27000 in the empty vector entry before the first define/undef. */
27001 inc = &(*macinfo_table)[idx - 1];
27002 inc->code = DW_MACRO_import;
27003 inc->lineno = 0;
27004 inc->info = ggc_strdup (grp_name);
27005 if (!*macinfo_htab)
27006 *macinfo_htab = new macinfo_hash_type (10);
27007 /* Avoid emitting duplicates. */
27008 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27009 if (*slot != NULL)
27011 inc->code = 0;
27012 inc->info = NULL;
27013 /* If such an entry has been used before, just emit
27014 a DW_MACRO_import op. */
27015 inc = *slot;
27016 output_macinfo_op (inc);
27017 /* And clear all macinfo_entry in the range to avoid emitting them
27018 in the second pass. */
27019 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27021 cur->code = 0;
27022 cur->info = NULL;
27025 else
27027 *slot = inc;
27028 inc->lineno = (*macinfo_htab)->elements ();
27029 output_macinfo_op (inc);
27031 return count;
27034 /* Save any strings needed by the macinfo table in the debug str
27035 table. All strings must be collected into the table by the time
27036 index_string is called. */
27038 static void
27039 save_macinfo_strings (void)
27041 unsigned len;
27042 unsigned i;
27043 macinfo_entry *ref;
27045 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27047 switch (ref->code)
27049 /* Match the logic in output_macinfo_op to decide on
27050 indirect strings. */
27051 case DW_MACINFO_define:
27052 case DW_MACINFO_undef:
27053 len = strlen (ref->info) + 1;
27054 if (!dwarf_strict
27055 && len > DWARF_OFFSET_SIZE
27056 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27057 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27058 set_indirect_string (find_AT_string (ref->info));
27059 break;
27060 case DW_MACRO_define_strp:
27061 case DW_MACRO_undef_strp:
27062 set_indirect_string (find_AT_string (ref->info));
27063 break;
27064 default:
27065 break;
27070 /* Output macinfo section(s). */
27072 static void
27073 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27075 unsigned i;
27076 unsigned long length = vec_safe_length (macinfo_table);
27077 macinfo_entry *ref;
27078 vec<macinfo_entry, va_gc> *files = NULL;
27079 macinfo_hash_type *macinfo_htab = NULL;
27080 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27082 if (! length)
27083 return;
27085 /* output_macinfo* uses these interchangeably. */
27086 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27087 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27088 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27089 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27091 /* AIX Assembler inserts the length, so adjust the reference to match the
27092 offset expected by debuggers. */
27093 strcpy (dl_section_ref, debug_line_label);
27094 if (XCOFF_DEBUGGING_INFO)
27095 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27097 /* For .debug_macro emit the section header. */
27098 if (!dwarf_strict || dwarf_version >= 5)
27100 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27101 "DWARF macro version number");
27102 if (DWARF_OFFSET_SIZE == 8)
27103 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27104 else
27105 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27106 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27107 debug_line_section, NULL);
27110 /* In the first loop, it emits the primary .debug_macinfo section
27111 and after each emitted op the macinfo_entry is cleared.
27112 If a longer range of define/undef ops can be optimized using
27113 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27114 the vector before the first define/undef in the range and the
27115 whole range of define/undef ops is not emitted and kept. */
27116 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27118 switch (ref->code)
27120 case DW_MACINFO_start_file:
27121 vec_safe_push (files, *ref);
27122 break;
27123 case DW_MACINFO_end_file:
27124 if (!vec_safe_is_empty (files))
27125 files->pop ();
27126 break;
27127 case DW_MACINFO_define:
27128 case DW_MACINFO_undef:
27129 if ((!dwarf_strict || dwarf_version >= 5)
27130 && HAVE_COMDAT_GROUP
27131 && vec_safe_length (files) != 1
27132 && i > 0
27133 && i + 1 < length
27134 && (*macinfo_table)[i - 1].code == 0)
27136 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27137 if (count)
27139 i += count - 1;
27140 continue;
27143 break;
27144 case 0:
27145 /* A dummy entry may be inserted at the beginning to be able
27146 to optimize the whole block of predefined macros. */
27147 if (i == 0)
27148 continue;
27149 default:
27150 break;
27152 output_macinfo_op (ref);
27153 ref->info = NULL;
27154 ref->code = 0;
27157 if (!macinfo_htab)
27158 return;
27160 /* Save the number of transparent includes so we can adjust the
27161 label number for the fat LTO object DWARF. */
27162 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27164 delete macinfo_htab;
27165 macinfo_htab = NULL;
27167 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27168 terminate the current chain and switch to a new comdat .debug_macinfo
27169 section and emit the define/undef entries within it. */
27170 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27171 switch (ref->code)
27173 case 0:
27174 continue;
27175 case DW_MACRO_import:
27177 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27178 tree comdat_key = get_identifier (ref->info);
27179 /* Terminate the previous .debug_macinfo section. */
27180 dw2_asm_output_data (1, 0, "End compilation unit");
27181 targetm.asm_out.named_section (debug_macinfo_section_name,
27182 SECTION_DEBUG
27183 | SECTION_LINKONCE
27184 | (early_lto_debug
27185 ? SECTION_EXCLUDE : 0),
27186 comdat_key);
27187 ASM_GENERATE_INTERNAL_LABEL (label,
27188 DEBUG_MACRO_SECTION_LABEL,
27189 ref->lineno + macinfo_label_base);
27190 ASM_OUTPUT_LABEL (asm_out_file, label);
27191 ref->code = 0;
27192 ref->info = NULL;
27193 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27194 "DWARF macro version number");
27195 if (DWARF_OFFSET_SIZE == 8)
27196 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27197 else
27198 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27200 break;
27201 case DW_MACINFO_define:
27202 case DW_MACINFO_undef:
27203 output_macinfo_op (ref);
27204 ref->code = 0;
27205 ref->info = NULL;
27206 break;
27207 default:
27208 gcc_unreachable ();
27211 macinfo_label_base += macinfo_label_base_adj;
27214 /* Initialize the various sections and labels for dwarf output and prefix
27215 them with PREFIX if non-NULL. */
27217 static void
27218 init_sections_and_labels (bool early_lto_debug)
27220 /* As we may get called multiple times have a generation count for labels. */
27221 static unsigned generation = 0;
27223 if (early_lto_debug)
27225 if (!dwarf_split_debug_info)
27227 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27228 SECTION_DEBUG | SECTION_EXCLUDE,
27229 NULL);
27230 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27231 SECTION_DEBUG | SECTION_EXCLUDE,
27232 NULL);
27233 debug_macinfo_section_name = ((dwarf_strict && dwarf_version < 5)
27234 ? DEBUG_LTO_MACINFO_SECTION
27235 : DEBUG_LTO_MACRO_SECTION);
27236 debug_macinfo_section = get_section (debug_macinfo_section_name,
27237 SECTION_DEBUG
27238 | SECTION_EXCLUDE, NULL);
27239 /* For macro info we have to refer to a debug_line section, so similar
27240 to split-dwarf emit a skeleton one for early debug. */
27241 debug_skeleton_line_section
27242 = get_section (DEBUG_LTO_LINE_SECTION,
27243 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27244 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27245 DEBUG_SKELETON_LINE_SECTION_LABEL,
27246 generation);
27248 else
27250 /* ??? Which of the following do we need early? */
27251 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27252 SECTION_DEBUG | SECTION_EXCLUDE,
27253 NULL);
27254 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27255 SECTION_DEBUG | SECTION_EXCLUDE,
27256 NULL);
27257 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27258 SECTION_DEBUG
27259 | SECTION_EXCLUDE, NULL);
27260 debug_skeleton_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27261 SECTION_DEBUG
27262 | SECTION_EXCLUDE, NULL);
27263 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27264 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27265 generation);
27267 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
27268 the main .o, but the skeleton_line goes into the split off dwo. */
27269 debug_skeleton_line_section
27270 = get_section (DEBUG_LTO_LINE_SECTION,
27271 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27272 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27273 DEBUG_SKELETON_LINE_SECTION_LABEL,
27274 generation);
27275 debug_str_offsets_section
27276 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27277 SECTION_DEBUG | SECTION_EXCLUDE,
27278 NULL);
27279 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27280 DEBUG_SKELETON_INFO_SECTION_LABEL,
27281 generation);
27282 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27283 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
27284 debug_macinfo_section_name
27285 = (dwarf_strict
27286 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27287 debug_macinfo_section = get_section (debug_macinfo_section_name,
27288 SECTION_DEBUG | SECTION_EXCLUDE,
27289 NULL);
27291 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27292 DEBUG_STR_SECTION_FLAGS
27293 | SECTION_EXCLUDE, NULL);
27295 else
27297 if (!dwarf_split_debug_info)
27299 debug_info_section = get_section (DEBUG_INFO_SECTION,
27300 SECTION_DEBUG, NULL);
27301 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27302 SECTION_DEBUG, NULL);
27303 debug_loc_section = get_section (DEBUG_LOC_SECTION,
27304 SECTION_DEBUG, NULL);
27305 debug_macinfo_section_name
27306 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
27307 debug_macinfo_section = get_section (debug_macinfo_section_name,
27308 SECTION_DEBUG, NULL);
27310 else
27312 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27313 SECTION_DEBUG | SECTION_EXCLUDE,
27314 NULL);
27315 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27316 SECTION_DEBUG | SECTION_EXCLUDE,
27317 NULL);
27318 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27319 SECTION_DEBUG, NULL);
27320 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27321 SECTION_DEBUG, NULL);
27322 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27323 SECTION_DEBUG, NULL);
27324 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27325 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27326 generation);
27328 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27329 stay in the main .o, but the skeleton_line goes into the
27330 split off dwo. */
27331 debug_skeleton_line_section
27332 = get_section (DEBUG_DWO_LINE_SECTION,
27333 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27334 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27335 DEBUG_SKELETON_LINE_SECTION_LABEL,
27336 generation);
27337 debug_str_offsets_section
27338 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27339 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27340 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27341 DEBUG_SKELETON_INFO_SECTION_LABEL,
27342 generation);
27343 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
27344 SECTION_DEBUG | SECTION_EXCLUDE,
27345 NULL);
27346 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27347 DEBUG_STR_DWO_SECTION_FLAGS,
27348 NULL);
27349 debug_macinfo_section_name
27350 = (dwarf_strict && dwarf_version < 5)
27351 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
27352 debug_macinfo_section = get_section (debug_macinfo_section_name,
27353 SECTION_DEBUG | SECTION_EXCLUDE,
27354 NULL);
27356 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27357 SECTION_DEBUG, NULL);
27358 debug_line_section = get_section (DEBUG_LINE_SECTION,
27359 SECTION_DEBUG, NULL);
27360 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27361 SECTION_DEBUG, NULL);
27362 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27363 SECTION_DEBUG, NULL);
27364 debug_str_section = get_section (DEBUG_STR_SECTION,
27365 DEBUG_STR_SECTION_FLAGS, NULL);
27366 debug_ranges_section = get_section (dwarf_version >= 5
27367 ? DEBUG_RNGLISTS_SECTION
27368 : DEBUG_RANGES_SECTION,
27369 SECTION_DEBUG, NULL);
27370 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27371 SECTION_DEBUG, NULL);
27374 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27375 DEBUG_ABBREV_SECTION_LABEL, generation);
27376 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27377 DEBUG_INFO_SECTION_LABEL, generation);
27378 info_section_emitted = false;
27379 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27380 DEBUG_LINE_SECTION_LABEL, generation);
27381 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27382 DEBUG_RANGES_SECTION_LABEL, generation);
27383 if (dwarf_version >= 5 && dwarf_split_debug_info)
27384 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27385 DEBUG_RANGES_SECTION_LABEL, 2 + generation);
27386 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27387 DEBUG_ADDR_SECTION_LABEL, generation);
27388 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27389 (dwarf_strict && dwarf_version < 5)
27390 ? DEBUG_MACINFO_SECTION_LABEL
27391 : DEBUG_MACRO_SECTION_LABEL, generation);
27392 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27393 generation);
27395 ++generation;
27398 /* Set up for Dwarf output at the start of compilation. */
27400 static void
27401 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27403 /* Allocate the file_table. */
27404 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27406 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27407 /* Allocate the decl_die_table. */
27408 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27410 /* Allocate the decl_loc_table. */
27411 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27413 /* Allocate the cached_dw_loc_list_table. */
27414 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27416 /* Allocate the initial hunk of the decl_scope_table. */
27417 vec_alloc (decl_scope_table, 256);
27419 /* Allocate the initial hunk of the abbrev_die_table. */
27420 vec_alloc (abbrev_die_table, 256);
27421 /* Zero-th entry is allocated, but unused. */
27422 abbrev_die_table->quick_push (NULL);
27424 /* Allocate the dwarf_proc_stack_usage_map. */
27425 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27427 /* Allocate the pubtypes and pubnames vectors. */
27428 vec_alloc (pubname_table, 32);
27429 vec_alloc (pubtype_table, 32);
27431 vec_alloc (incomplete_types, 64);
27433 vec_alloc (used_rtx_array, 32);
27435 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27436 vec_alloc (macinfo_table, 64);
27437 #endif
27439 /* If front-ends already registered a main translation unit but we were not
27440 ready to perform the association, do this now. */
27441 if (main_translation_unit != NULL_TREE)
27442 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27445 /* Called before compile () starts outputtting functions, variables
27446 and toplevel asms into assembly. */
27448 static void
27449 dwarf2out_assembly_start (void)
27451 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27452 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27453 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27454 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27455 COLD_TEXT_SECTION_LABEL, 0);
27456 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27458 switch_to_section (text_section);
27459 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27460 #endif
27462 /* Make sure the line number table for .text always exists. */
27463 text_section_line_info = new_line_info_table ();
27464 text_section_line_info->end_label = text_end_label;
27466 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27467 cur_line_info_table = text_section_line_info;
27468 #endif
27470 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27471 && dwarf2out_do_cfi_asm ()
27472 && (!(flag_unwind_tables || flag_exceptions)
27473 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
27474 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27477 /* A helper function for dwarf2out_finish called through
27478 htab_traverse. Assign a string its index. All strings must be
27479 collected into the table by the time index_string is called,
27480 because the indexing code relies on htab_traverse to traverse nodes
27481 in the same order for each run. */
27484 index_string (indirect_string_node **h, unsigned int *index)
27486 indirect_string_node *node = *h;
27488 find_string_form (node);
27489 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27491 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27492 node->index = *index;
27493 *index += 1;
27495 return 1;
27498 /* A helper function for output_indirect_strings called through
27499 htab_traverse. Output the offset to a string and update the
27500 current offset. */
27503 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27505 indirect_string_node *node = *h;
27507 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27509 /* Assert that this node has been assigned an index. */
27510 gcc_assert (node->index != NO_INDEX_ASSIGNED
27511 && node->index != NOT_INDEXED);
27512 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27513 "indexed string 0x%x: %s", node->index, node->str);
27514 *offset += strlen (node->str) + 1;
27516 return 1;
27519 /* A helper function for dwarf2out_finish called through
27520 htab_traverse. Output the indexed string. */
27523 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27525 struct indirect_string_node *node = *h;
27527 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27529 /* Assert that the strings are output in the same order as their
27530 indexes were assigned. */
27531 gcc_assert (*cur_idx == node->index);
27532 assemble_string (node->str, strlen (node->str) + 1);
27533 *cur_idx += 1;
27535 return 1;
27538 /* A helper function for dwarf2out_finish called through
27539 htab_traverse. Emit one queued .debug_str string. */
27542 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27544 struct indirect_string_node *node = *h;
27546 node->form = find_string_form (node);
27547 if (node->form == form && node->refcount > 0)
27549 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27550 assemble_string (node->str, strlen (node->str) + 1);
27553 return 1;
27556 /* Output the indexed string table. */
27558 static void
27559 output_indirect_strings (void)
27561 switch_to_section (debug_str_section);
27562 if (!dwarf_split_debug_info)
27563 debug_str_hash->traverse<enum dwarf_form,
27564 output_indirect_string> (DW_FORM_strp);
27565 else
27567 unsigned int offset = 0;
27568 unsigned int cur_idx = 0;
27570 skeleton_debug_str_hash->traverse<enum dwarf_form,
27571 output_indirect_string> (DW_FORM_strp);
27573 switch_to_section (debug_str_offsets_section);
27574 debug_str_hash->traverse_noresize
27575 <unsigned int *, output_index_string_offset> (&offset);
27576 switch_to_section (debug_str_dwo_section);
27577 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27578 (&cur_idx);
27582 /* Callback for htab_traverse to assign an index to an entry in the
27583 table, and to write that entry to the .debug_addr section. */
27586 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27588 addr_table_entry *entry = *slot;
27590 if (entry->refcount == 0)
27592 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27593 || entry->index == NOT_INDEXED);
27594 return 1;
27597 gcc_assert (entry->index == *cur_index);
27598 (*cur_index)++;
27600 switch (entry->kind)
27602 case ate_kind_rtx:
27603 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27604 "0x%x", entry->index);
27605 break;
27606 case ate_kind_rtx_dtprel:
27607 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27608 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27609 DWARF2_ADDR_SIZE,
27610 entry->addr.rtl);
27611 fputc ('\n', asm_out_file);
27612 break;
27613 case ate_kind_label:
27614 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27615 "0x%x", entry->index);
27616 break;
27617 default:
27618 gcc_unreachable ();
27620 return 1;
27623 /* Produce the .debug_addr section. */
27625 static void
27626 output_addr_table (void)
27628 unsigned int index = 0;
27629 if (addr_index_table == NULL || addr_index_table->size () == 0)
27630 return;
27632 switch_to_section (debug_addr_section);
27633 addr_index_table
27634 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27637 #if ENABLE_ASSERT_CHECKING
27638 /* Verify that all marks are clear. */
27640 static void
27641 verify_marks_clear (dw_die_ref die)
27643 dw_die_ref c;
27645 gcc_assert (! die->die_mark);
27646 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27648 #endif /* ENABLE_ASSERT_CHECKING */
27650 /* Clear the marks for a die and its children.
27651 Be cool if the mark isn't set. */
27653 static void
27654 prune_unmark_dies (dw_die_ref die)
27656 dw_die_ref c;
27658 if (die->die_mark)
27659 die->die_mark = 0;
27660 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27663 /* Given LOC that is referenced by a DIE we're marking as used, find all
27664 referenced DWARF procedures it references and mark them as used. */
27666 static void
27667 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27669 for (; loc != NULL; loc = loc->dw_loc_next)
27670 switch (loc->dw_loc_opc)
27672 case DW_OP_implicit_pointer:
27673 case DW_OP_convert:
27674 case DW_OP_reinterpret:
27675 case DW_OP_GNU_implicit_pointer:
27676 case DW_OP_GNU_convert:
27677 case DW_OP_GNU_reinterpret:
27678 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27679 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27680 break;
27681 case DW_OP_GNU_variable_value:
27682 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27684 dw_die_ref ref
27685 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27686 if (ref == NULL)
27687 break;
27688 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27689 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27690 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27692 /* FALLTHRU */
27693 case DW_OP_call2:
27694 case DW_OP_call4:
27695 case DW_OP_call_ref:
27696 case DW_OP_const_type:
27697 case DW_OP_GNU_const_type:
27698 case DW_OP_GNU_parameter_ref:
27699 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27700 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27701 break;
27702 case DW_OP_regval_type:
27703 case DW_OP_deref_type:
27704 case DW_OP_GNU_regval_type:
27705 case DW_OP_GNU_deref_type:
27706 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27707 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27708 break;
27709 case DW_OP_entry_value:
27710 case DW_OP_GNU_entry_value:
27711 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27712 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27713 break;
27714 default:
27715 break;
27719 /* Given DIE that we're marking as used, find any other dies
27720 it references as attributes and mark them as used. */
27722 static void
27723 prune_unused_types_walk_attribs (dw_die_ref die)
27725 dw_attr_node *a;
27726 unsigned ix;
27728 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27730 switch (AT_class (a))
27732 /* Make sure DWARF procedures referenced by location descriptions will
27733 get emitted. */
27734 case dw_val_class_loc:
27735 prune_unused_types_walk_loc_descr (AT_loc (a));
27736 break;
27737 case dw_val_class_loc_list:
27738 for (dw_loc_list_ref list = AT_loc_list (a);
27739 list != NULL;
27740 list = list->dw_loc_next)
27741 prune_unused_types_walk_loc_descr (list->expr);
27742 break;
27744 case dw_val_class_die_ref:
27745 /* A reference to another DIE.
27746 Make sure that it will get emitted.
27747 If it was broken out into a comdat group, don't follow it. */
27748 if (! AT_ref (a)->comdat_type_p
27749 || a->dw_attr == DW_AT_specification)
27750 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27751 break;
27753 case dw_val_class_str:
27754 /* Set the string's refcount to 0 so that prune_unused_types_mark
27755 accounts properly for it. */
27756 a->dw_attr_val.v.val_str->refcount = 0;
27757 break;
27759 default:
27760 break;
27765 /* Mark the generic parameters and arguments children DIEs of DIE. */
27767 static void
27768 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
27770 dw_die_ref c;
27772 if (die == NULL || die->die_child == NULL)
27773 return;
27774 c = die->die_child;
27777 if (is_template_parameter (c))
27778 prune_unused_types_mark (c, 1);
27779 c = c->die_sib;
27780 } while (c && c != die->die_child);
27783 /* Mark DIE as being used. If DOKIDS is true, then walk down
27784 to DIE's children. */
27786 static void
27787 prune_unused_types_mark (dw_die_ref die, int dokids)
27789 dw_die_ref c;
27791 if (die->die_mark == 0)
27793 /* We haven't done this node yet. Mark it as used. */
27794 die->die_mark = 1;
27795 /* If this is the DIE of a generic type instantiation,
27796 mark the children DIEs that describe its generic parms and
27797 args. */
27798 prune_unused_types_mark_generic_parms_dies (die);
27800 /* We also have to mark its parents as used.
27801 (But we don't want to mark our parent's kids due to this,
27802 unless it is a class.) */
27803 if (die->die_parent)
27804 prune_unused_types_mark (die->die_parent,
27805 class_scope_p (die->die_parent));
27807 /* Mark any referenced nodes. */
27808 prune_unused_types_walk_attribs (die);
27810 /* If this node is a specification,
27811 also mark the definition, if it exists. */
27812 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
27813 prune_unused_types_mark (die->die_definition, 1);
27816 if (dokids && die->die_mark != 2)
27818 /* We need to walk the children, but haven't done so yet.
27819 Remember that we've walked the kids. */
27820 die->die_mark = 2;
27822 /* If this is an array type, we need to make sure our
27823 kids get marked, even if they're types. If we're
27824 breaking out types into comdat sections, do this
27825 for all type definitions. */
27826 if (die->die_tag == DW_TAG_array_type
27827 || (use_debug_types
27828 && is_type_die (die) && ! is_declaration_die (die)))
27829 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
27830 else
27831 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27835 /* For local classes, look if any static member functions were emitted
27836 and if so, mark them. */
27838 static void
27839 prune_unused_types_walk_local_classes (dw_die_ref die)
27841 dw_die_ref c;
27843 if (die->die_mark == 2)
27844 return;
27846 switch (die->die_tag)
27848 case DW_TAG_structure_type:
27849 case DW_TAG_union_type:
27850 case DW_TAG_class_type:
27851 break;
27853 case DW_TAG_subprogram:
27854 if (!get_AT_flag (die, DW_AT_declaration)
27855 || die->die_definition != NULL)
27856 prune_unused_types_mark (die, 1);
27857 return;
27859 default:
27860 return;
27863 /* Mark children. */
27864 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
27867 /* Walk the tree DIE and mark types that we actually use. */
27869 static void
27870 prune_unused_types_walk (dw_die_ref die)
27872 dw_die_ref c;
27874 /* Don't do anything if this node is already marked and
27875 children have been marked as well. */
27876 if (die->die_mark == 2)
27877 return;
27879 switch (die->die_tag)
27881 case DW_TAG_structure_type:
27882 case DW_TAG_union_type:
27883 case DW_TAG_class_type:
27884 if (die->die_perennial_p)
27885 break;
27887 for (c = die->die_parent; c; c = c->die_parent)
27888 if (c->die_tag == DW_TAG_subprogram)
27889 break;
27891 /* Finding used static member functions inside of classes
27892 is needed just for local classes, because for other classes
27893 static member function DIEs with DW_AT_specification
27894 are emitted outside of the DW_TAG_*_type. If we ever change
27895 it, we'd need to call this even for non-local classes. */
27896 if (c)
27897 prune_unused_types_walk_local_classes (die);
27899 /* It's a type node --- don't mark it. */
27900 return;
27902 case DW_TAG_const_type:
27903 case DW_TAG_packed_type:
27904 case DW_TAG_pointer_type:
27905 case DW_TAG_reference_type:
27906 case DW_TAG_rvalue_reference_type:
27907 case DW_TAG_volatile_type:
27908 case DW_TAG_typedef:
27909 case DW_TAG_array_type:
27910 case DW_TAG_interface_type:
27911 case DW_TAG_friend:
27912 case DW_TAG_enumeration_type:
27913 case DW_TAG_subroutine_type:
27914 case DW_TAG_string_type:
27915 case DW_TAG_set_type:
27916 case DW_TAG_subrange_type:
27917 case DW_TAG_ptr_to_member_type:
27918 case DW_TAG_file_type:
27919 /* Type nodes are useful only when other DIEs reference them --- don't
27920 mark them. */
27921 /* FALLTHROUGH */
27923 case DW_TAG_dwarf_procedure:
27924 /* Likewise for DWARF procedures. */
27926 if (die->die_perennial_p)
27927 break;
27929 return;
27931 default:
27932 /* Mark everything else. */
27933 break;
27936 if (die->die_mark == 0)
27938 die->die_mark = 1;
27940 /* Now, mark any dies referenced from here. */
27941 prune_unused_types_walk_attribs (die);
27944 die->die_mark = 2;
27946 /* Mark children. */
27947 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
27950 /* Increment the string counts on strings referred to from DIE's
27951 attributes. */
27953 static void
27954 prune_unused_types_update_strings (dw_die_ref die)
27956 dw_attr_node *a;
27957 unsigned ix;
27959 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27960 if (AT_class (a) == dw_val_class_str)
27962 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
27963 s->refcount++;
27964 /* Avoid unnecessarily putting strings that are used less than
27965 twice in the hash table. */
27966 if (s->refcount
27967 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
27969 indirect_string_node **slot
27970 = debug_str_hash->find_slot_with_hash (s->str,
27971 htab_hash_string (s->str),
27972 INSERT);
27973 gcc_assert (*slot == NULL);
27974 *slot = s;
27979 /* Mark DIE and its children as removed. */
27981 static void
27982 mark_removed (dw_die_ref die)
27984 dw_die_ref c;
27985 die->removed = true;
27986 FOR_EACH_CHILD (die, c, mark_removed (c));
27989 /* Remove from the tree DIE any dies that aren't marked. */
27991 static void
27992 prune_unused_types_prune (dw_die_ref die)
27994 dw_die_ref c;
27996 gcc_assert (die->die_mark);
27997 prune_unused_types_update_strings (die);
27999 if (! die->die_child)
28000 return;
28002 c = die->die_child;
28003 do {
28004 dw_die_ref prev = c, next;
28005 for (c = c->die_sib; ! c->die_mark; c = next)
28006 if (c == die->die_child)
28008 /* No marked children between 'prev' and the end of the list. */
28009 if (prev == c)
28010 /* No marked children at all. */
28011 die->die_child = NULL;
28012 else
28014 prev->die_sib = c->die_sib;
28015 die->die_child = prev;
28017 c->die_sib = NULL;
28018 mark_removed (c);
28019 return;
28021 else
28023 next = c->die_sib;
28024 c->die_sib = NULL;
28025 mark_removed (c);
28028 if (c != prev->die_sib)
28029 prev->die_sib = c;
28030 prune_unused_types_prune (c);
28031 } while (c != die->die_child);
28034 /* Remove dies representing declarations that we never use. */
28036 static void
28037 prune_unused_types (void)
28039 unsigned int i;
28040 limbo_die_node *node;
28041 comdat_type_node *ctnode;
28042 pubname_entry *pub;
28043 dw_die_ref base_type;
28045 #if ENABLE_ASSERT_CHECKING
28046 /* All the marks should already be clear. */
28047 verify_marks_clear (comp_unit_die ());
28048 for (node = limbo_die_list; node; node = node->next)
28049 verify_marks_clear (node->die);
28050 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28051 verify_marks_clear (ctnode->root_die);
28052 #endif /* ENABLE_ASSERT_CHECKING */
28054 /* Mark types that are used in global variables. */
28055 premark_types_used_by_global_vars ();
28057 /* Set the mark on nodes that are actually used. */
28058 prune_unused_types_walk (comp_unit_die ());
28059 for (node = limbo_die_list; node; node = node->next)
28060 prune_unused_types_walk (node->die);
28061 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28063 prune_unused_types_walk (ctnode->root_die);
28064 prune_unused_types_mark (ctnode->type_die, 1);
28067 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28068 are unusual in that they are pubnames that are the children of pubtypes.
28069 They should only be marked via their parent DW_TAG_enumeration_type die,
28070 not as roots in themselves. */
28071 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28072 if (pub->die->die_tag != DW_TAG_enumerator)
28073 prune_unused_types_mark (pub->die, 1);
28074 for (i = 0; base_types.iterate (i, &base_type); i++)
28075 prune_unused_types_mark (base_type, 1);
28077 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28078 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28079 callees). */
28080 cgraph_node *cnode;
28081 FOR_EACH_FUNCTION (cnode)
28082 if (cnode->referred_to_p (false))
28084 dw_die_ref die = lookup_decl_die (cnode->decl);
28085 if (die == NULL || die->die_mark)
28086 continue;
28087 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28088 if (e->caller != cnode
28089 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28091 prune_unused_types_mark (die, 1);
28092 break;
28096 if (debug_str_hash)
28097 debug_str_hash->empty ();
28098 if (skeleton_debug_str_hash)
28099 skeleton_debug_str_hash->empty ();
28100 prune_unused_types_prune (comp_unit_die ());
28101 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28103 node = *pnode;
28104 if (!node->die->die_mark)
28105 *pnode = node->next;
28106 else
28108 prune_unused_types_prune (node->die);
28109 pnode = &node->next;
28112 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28113 prune_unused_types_prune (ctnode->root_die);
28115 /* Leave the marks clear. */
28116 prune_unmark_dies (comp_unit_die ());
28117 for (node = limbo_die_list; node; node = node->next)
28118 prune_unmark_dies (node->die);
28119 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28120 prune_unmark_dies (ctnode->root_die);
28123 /* Helpers to manipulate hash table of comdat type units. */
28125 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28127 static inline hashval_t hash (const comdat_type_node *);
28128 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28131 inline hashval_t
28132 comdat_type_hasher::hash (const comdat_type_node *type_node)
28134 hashval_t h;
28135 memcpy (&h, type_node->signature, sizeof (h));
28136 return h;
28139 inline bool
28140 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28141 const comdat_type_node *type_node_2)
28143 return (! memcmp (type_node_1->signature, type_node_2->signature,
28144 DWARF_TYPE_SIGNATURE_SIZE));
28147 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28148 to the location it would have been added, should we know its
28149 DECL_ASSEMBLER_NAME when we added other attributes. This will
28150 probably improve compactness of debug info, removing equivalent
28151 abbrevs, and hide any differences caused by deferring the
28152 computation of the assembler name, triggered by e.g. PCH. */
28154 static inline void
28155 move_linkage_attr (dw_die_ref die)
28157 unsigned ix = vec_safe_length (die->die_attr);
28158 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28160 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28161 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28163 while (--ix > 0)
28165 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28167 if (prev->dw_attr == DW_AT_decl_line
28168 || prev->dw_attr == DW_AT_decl_column
28169 || prev->dw_attr == DW_AT_name)
28170 break;
28173 if (ix != vec_safe_length (die->die_attr) - 1)
28175 die->die_attr->pop ();
28176 die->die_attr->quick_insert (ix, linkage);
28180 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28181 referenced from typed stack ops and count how often they are used. */
28183 static void
28184 mark_base_types (dw_loc_descr_ref loc)
28186 dw_die_ref base_type = NULL;
28188 for (; loc; loc = loc->dw_loc_next)
28190 switch (loc->dw_loc_opc)
28192 case DW_OP_regval_type:
28193 case DW_OP_deref_type:
28194 case DW_OP_GNU_regval_type:
28195 case DW_OP_GNU_deref_type:
28196 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28197 break;
28198 case DW_OP_convert:
28199 case DW_OP_reinterpret:
28200 case DW_OP_GNU_convert:
28201 case DW_OP_GNU_reinterpret:
28202 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28203 continue;
28204 /* FALLTHRU */
28205 case DW_OP_const_type:
28206 case DW_OP_GNU_const_type:
28207 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28208 break;
28209 case DW_OP_entry_value:
28210 case DW_OP_GNU_entry_value:
28211 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28212 continue;
28213 default:
28214 continue;
28216 gcc_assert (base_type->die_parent == comp_unit_die ());
28217 if (base_type->die_mark)
28218 base_type->die_mark++;
28219 else
28221 base_types.safe_push (base_type);
28222 base_type->die_mark = 1;
28227 /* Comparison function for sorting marked base types. */
28229 static int
28230 base_type_cmp (const void *x, const void *y)
28232 dw_die_ref dx = *(const dw_die_ref *) x;
28233 dw_die_ref dy = *(const dw_die_ref *) y;
28234 unsigned int byte_size1, byte_size2;
28235 unsigned int encoding1, encoding2;
28236 unsigned int align1, align2;
28237 if (dx->die_mark > dy->die_mark)
28238 return -1;
28239 if (dx->die_mark < dy->die_mark)
28240 return 1;
28241 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28242 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28243 if (byte_size1 < byte_size2)
28244 return 1;
28245 if (byte_size1 > byte_size2)
28246 return -1;
28247 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28248 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28249 if (encoding1 < encoding2)
28250 return 1;
28251 if (encoding1 > encoding2)
28252 return -1;
28253 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28254 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28255 if (align1 < align2)
28256 return 1;
28257 if (align1 > align2)
28258 return -1;
28259 return 0;
28262 /* Move base types marked by mark_base_types as early as possible
28263 in the CU, sorted by decreasing usage count both to make the
28264 uleb128 references as small as possible and to make sure they
28265 will have die_offset already computed by calc_die_sizes when
28266 sizes of typed stack loc ops is computed. */
28268 static void
28269 move_marked_base_types (void)
28271 unsigned int i;
28272 dw_die_ref base_type, die, c;
28274 if (base_types.is_empty ())
28275 return;
28277 /* Sort by decreasing usage count, they will be added again in that
28278 order later on. */
28279 base_types.qsort (base_type_cmp);
28280 die = comp_unit_die ();
28281 c = die->die_child;
28284 dw_die_ref prev = c;
28285 c = c->die_sib;
28286 while (c->die_mark)
28288 remove_child_with_prev (c, prev);
28289 /* As base types got marked, there must be at least
28290 one node other than DW_TAG_base_type. */
28291 gcc_assert (die->die_child != NULL);
28292 c = prev->die_sib;
28295 while (c != die->die_child);
28296 gcc_assert (die->die_child);
28297 c = die->die_child;
28298 for (i = 0; base_types.iterate (i, &base_type); i++)
28300 base_type->die_mark = 0;
28301 base_type->die_sib = c->die_sib;
28302 c->die_sib = base_type;
28303 c = base_type;
28307 /* Helper function for resolve_addr, attempt to resolve
28308 one CONST_STRING, return true if successful. Similarly verify that
28309 SYMBOL_REFs refer to variables emitted in the current CU. */
28311 static bool
28312 resolve_one_addr (rtx *addr)
28314 rtx rtl = *addr;
28316 if (GET_CODE (rtl) == CONST_STRING)
28318 size_t len = strlen (XSTR (rtl, 0)) + 1;
28319 tree t = build_string (len, XSTR (rtl, 0));
28320 tree tlen = size_int (len - 1);
28321 TREE_TYPE (t)
28322 = build_array_type (char_type_node, build_index_type (tlen));
28323 rtl = lookup_constant_def (t);
28324 if (!rtl || !MEM_P (rtl))
28325 return false;
28326 rtl = XEXP (rtl, 0);
28327 if (GET_CODE (rtl) == SYMBOL_REF
28328 && SYMBOL_REF_DECL (rtl)
28329 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28330 return false;
28331 vec_safe_push (used_rtx_array, rtl);
28332 *addr = rtl;
28333 return true;
28336 if (GET_CODE (rtl) == SYMBOL_REF
28337 && SYMBOL_REF_DECL (rtl))
28339 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28341 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28342 return false;
28344 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28345 return false;
28348 if (GET_CODE (rtl) == CONST)
28350 subrtx_ptr_iterator::array_type array;
28351 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28352 if (!resolve_one_addr (*iter))
28353 return false;
28356 return true;
28359 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28360 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28361 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28363 static rtx
28364 string_cst_pool_decl (tree t)
28366 rtx rtl = output_constant_def (t, 1);
28367 unsigned char *array;
28368 dw_loc_descr_ref l;
28369 tree decl;
28370 size_t len;
28371 dw_die_ref ref;
28373 if (!rtl || !MEM_P (rtl))
28374 return NULL_RTX;
28375 rtl = XEXP (rtl, 0);
28376 if (GET_CODE (rtl) != SYMBOL_REF
28377 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28378 return NULL_RTX;
28380 decl = SYMBOL_REF_DECL (rtl);
28381 if (!lookup_decl_die (decl))
28383 len = TREE_STRING_LENGTH (t);
28384 vec_safe_push (used_rtx_array, rtl);
28385 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28386 array = ggc_vec_alloc<unsigned char> (len);
28387 memcpy (array, TREE_STRING_POINTER (t), len);
28388 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28389 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28390 l->dw_loc_oprnd2.v.val_vec.length = len;
28391 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28392 l->dw_loc_oprnd2.v.val_vec.array = array;
28393 add_AT_loc (ref, DW_AT_location, l);
28394 equate_decl_number_to_die (decl, ref);
28396 return rtl;
28399 /* Helper function of resolve_addr_in_expr. LOC is
28400 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28401 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28402 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28403 with DW_OP_implicit_pointer if possible
28404 and return true, if unsuccessful, return false. */
28406 static bool
28407 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28409 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28410 HOST_WIDE_INT offset = 0;
28411 dw_die_ref ref = NULL;
28412 tree decl;
28414 if (GET_CODE (rtl) == CONST
28415 && GET_CODE (XEXP (rtl, 0)) == PLUS
28416 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28418 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28419 rtl = XEXP (XEXP (rtl, 0), 0);
28421 if (GET_CODE (rtl) == CONST_STRING)
28423 size_t len = strlen (XSTR (rtl, 0)) + 1;
28424 tree t = build_string (len, XSTR (rtl, 0));
28425 tree tlen = size_int (len - 1);
28427 TREE_TYPE (t)
28428 = build_array_type (char_type_node, build_index_type (tlen));
28429 rtl = string_cst_pool_decl (t);
28430 if (!rtl)
28431 return false;
28433 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28435 decl = SYMBOL_REF_DECL (rtl);
28436 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28438 ref = lookup_decl_die (decl);
28439 if (ref && (get_AT (ref, DW_AT_location)
28440 || get_AT (ref, DW_AT_const_value)))
28442 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28443 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28444 loc->dw_loc_oprnd1.val_entry = NULL;
28445 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28446 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28447 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28448 loc->dw_loc_oprnd2.v.val_int = offset;
28449 return true;
28453 return false;
28456 /* Helper function for resolve_addr, handle one location
28457 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28458 the location list couldn't be resolved. */
28460 static bool
28461 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28463 dw_loc_descr_ref keep = NULL;
28464 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28465 switch (loc->dw_loc_opc)
28467 case DW_OP_addr:
28468 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28470 if ((prev == NULL
28471 || prev->dw_loc_opc == DW_OP_piece
28472 || prev->dw_loc_opc == DW_OP_bit_piece)
28473 && loc->dw_loc_next
28474 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28475 && (!dwarf_strict || dwarf_version >= 5)
28476 && optimize_one_addr_into_implicit_ptr (loc))
28477 break;
28478 return false;
28480 break;
28481 case DW_OP_GNU_addr_index:
28482 case DW_OP_GNU_const_index:
28483 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28484 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28486 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28487 if (!resolve_one_addr (&rtl))
28488 return false;
28489 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28490 loc->dw_loc_oprnd1.val_entry
28491 = add_addr_table_entry (rtl, ate_kind_rtx);
28493 break;
28494 case DW_OP_const4u:
28495 case DW_OP_const8u:
28496 if (loc->dtprel
28497 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28498 return false;
28499 break;
28500 case DW_OP_plus_uconst:
28501 if (size_of_loc_descr (loc)
28502 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28504 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28506 dw_loc_descr_ref repl
28507 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28508 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28509 add_loc_descr (&repl, loc->dw_loc_next);
28510 *loc = *repl;
28512 break;
28513 case DW_OP_implicit_value:
28514 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28515 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28516 return false;
28517 break;
28518 case DW_OP_implicit_pointer:
28519 case DW_OP_GNU_implicit_pointer:
28520 case DW_OP_GNU_parameter_ref:
28521 case DW_OP_GNU_variable_value:
28522 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28524 dw_die_ref ref
28525 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28526 if (ref == NULL)
28527 return false;
28528 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28529 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28530 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28532 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28534 if (prev == NULL
28535 && loc->dw_loc_next == NULL
28536 && AT_class (a) == dw_val_class_loc)
28537 switch (a->dw_attr)
28539 /* Following attributes allow both exprloc and reference,
28540 so if the whole expression is DW_OP_GNU_variable_value
28541 alone we could transform it into reference. */
28542 case DW_AT_byte_size:
28543 case DW_AT_bit_size:
28544 case DW_AT_lower_bound:
28545 case DW_AT_upper_bound:
28546 case DW_AT_bit_stride:
28547 case DW_AT_count:
28548 case DW_AT_allocated:
28549 case DW_AT_associated:
28550 case DW_AT_byte_stride:
28551 a->dw_attr_val.val_class = dw_val_class_die_ref;
28552 a->dw_attr_val.val_entry = NULL;
28553 a->dw_attr_val.v.val_die_ref.die
28554 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28555 a->dw_attr_val.v.val_die_ref.external = 0;
28556 return true;
28557 default:
28558 break;
28560 if (dwarf_strict)
28561 return false;
28563 break;
28564 case DW_OP_const_type:
28565 case DW_OP_regval_type:
28566 case DW_OP_deref_type:
28567 case DW_OP_convert:
28568 case DW_OP_reinterpret:
28569 case DW_OP_GNU_const_type:
28570 case DW_OP_GNU_regval_type:
28571 case DW_OP_GNU_deref_type:
28572 case DW_OP_GNU_convert:
28573 case DW_OP_GNU_reinterpret:
28574 while (loc->dw_loc_next
28575 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28576 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28578 dw_die_ref base1, base2;
28579 unsigned enc1, enc2, size1, size2;
28580 if (loc->dw_loc_opc == DW_OP_regval_type
28581 || loc->dw_loc_opc == DW_OP_deref_type
28582 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28583 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28584 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28585 else if (loc->dw_loc_oprnd1.val_class
28586 == dw_val_class_unsigned_const)
28587 break;
28588 else
28589 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28590 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28591 == dw_val_class_unsigned_const)
28592 break;
28593 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28594 gcc_assert (base1->die_tag == DW_TAG_base_type
28595 && base2->die_tag == DW_TAG_base_type);
28596 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28597 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28598 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28599 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28600 if (size1 == size2
28601 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28602 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28603 && loc != keep)
28604 || enc1 == enc2))
28606 /* Optimize away next DW_OP_convert after
28607 adjusting LOC's base type die reference. */
28608 if (loc->dw_loc_opc == DW_OP_regval_type
28609 || loc->dw_loc_opc == DW_OP_deref_type
28610 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28611 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28612 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28613 else
28614 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28615 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28616 continue;
28618 /* Don't change integer DW_OP_convert after e.g. floating
28619 point typed stack entry. */
28620 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28621 keep = loc->dw_loc_next;
28622 break;
28624 break;
28625 default:
28626 break;
28628 return true;
28631 /* Helper function of resolve_addr. DIE had DW_AT_location of
28632 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28633 and DW_OP_addr couldn't be resolved. resolve_addr has already
28634 removed the DW_AT_location attribute. This function attempts to
28635 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28636 to it or DW_AT_const_value attribute, if possible. */
28638 static void
28639 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28641 if (!VAR_P (decl)
28642 || lookup_decl_die (decl) != die
28643 || DECL_EXTERNAL (decl)
28644 || !TREE_STATIC (decl)
28645 || DECL_INITIAL (decl) == NULL_TREE
28646 || DECL_P (DECL_INITIAL (decl))
28647 || get_AT (die, DW_AT_const_value))
28648 return;
28650 tree init = DECL_INITIAL (decl);
28651 HOST_WIDE_INT offset = 0;
28652 /* For variables that have been optimized away and thus
28653 don't have a memory location, see if we can emit
28654 DW_AT_const_value instead. */
28655 if (tree_add_const_value_attribute (die, init))
28656 return;
28657 if (dwarf_strict && dwarf_version < 5)
28658 return;
28659 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28660 and ADDR_EXPR refers to a decl that has DW_AT_location or
28661 DW_AT_const_value (but isn't addressable, otherwise
28662 resolving the original DW_OP_addr wouldn't fail), see if
28663 we can add DW_OP_implicit_pointer. */
28664 STRIP_NOPS (init);
28665 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28666 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28668 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28669 init = TREE_OPERAND (init, 0);
28670 STRIP_NOPS (init);
28672 if (TREE_CODE (init) != ADDR_EXPR)
28673 return;
28674 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28675 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28676 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28677 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28678 && TREE_OPERAND (init, 0) != decl))
28680 dw_die_ref ref;
28681 dw_loc_descr_ref l;
28683 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28685 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28686 if (!rtl)
28687 return;
28688 decl = SYMBOL_REF_DECL (rtl);
28690 else
28691 decl = TREE_OPERAND (init, 0);
28692 ref = lookup_decl_die (decl);
28693 if (ref == NULL
28694 || (!get_AT (ref, DW_AT_location)
28695 && !get_AT (ref, DW_AT_const_value)))
28696 return;
28697 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28698 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28699 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28700 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28701 add_AT_loc (die, DW_AT_location, l);
28705 /* Return NULL if l is a DWARF expression, or first op that is not
28706 valid DWARF expression. */
28708 static dw_loc_descr_ref
28709 non_dwarf_expression (dw_loc_descr_ref l)
28711 while (l)
28713 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28714 return l;
28715 switch (l->dw_loc_opc)
28717 case DW_OP_regx:
28718 case DW_OP_implicit_value:
28719 case DW_OP_stack_value:
28720 case DW_OP_implicit_pointer:
28721 case DW_OP_GNU_implicit_pointer:
28722 case DW_OP_GNU_parameter_ref:
28723 case DW_OP_piece:
28724 case DW_OP_bit_piece:
28725 return l;
28726 default:
28727 break;
28729 l = l->dw_loc_next;
28731 return NULL;
28734 /* Return adjusted copy of EXPR:
28735 If it is empty DWARF expression, return it.
28736 If it is valid non-empty DWARF expression,
28737 return copy of EXPR with DW_OP_deref appended to it.
28738 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28739 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28740 If it is DWARF expression followed by DW_OP_stack_value, return
28741 copy of the DWARF expression without anything appended.
28742 Otherwise, return NULL. */
28744 static dw_loc_descr_ref
28745 copy_deref_exprloc (dw_loc_descr_ref expr)
28747 dw_loc_descr_ref tail = NULL;
28749 if (expr == NULL)
28750 return NULL;
28752 dw_loc_descr_ref l = non_dwarf_expression (expr);
28753 if (l && l->dw_loc_next)
28754 return NULL;
28756 if (l)
28758 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28759 tail = new_loc_descr ((enum dwarf_location_atom)
28760 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28761 0, 0);
28762 else
28763 switch (l->dw_loc_opc)
28765 case DW_OP_regx:
28766 tail = new_loc_descr (DW_OP_bregx,
28767 l->dw_loc_oprnd1.v.val_unsigned, 0);
28768 break;
28769 case DW_OP_stack_value:
28770 break;
28771 default:
28772 return NULL;
28775 else
28776 tail = new_loc_descr (DW_OP_deref, 0, 0);
28778 dw_loc_descr_ref ret = NULL, *p = &ret;
28779 while (expr != l)
28781 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
28782 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
28783 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
28784 p = &(*p)->dw_loc_next;
28785 expr = expr->dw_loc_next;
28787 *p = tail;
28788 return ret;
28791 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
28792 reference to a variable or argument, adjust it if needed and return:
28793 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
28794 attribute if present should be removed
28795 0 keep the attribute perhaps with minor modifications, no need to rescan
28796 1 if the attribute has been successfully adjusted. */
28798 static int
28799 optimize_string_length (dw_attr_node *a)
28801 dw_loc_descr_ref l = AT_loc (a), lv;
28802 dw_die_ref die;
28803 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28805 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
28806 die = lookup_decl_die (decl);
28807 if (die)
28809 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28810 l->dw_loc_oprnd1.v.val_die_ref.die = die;
28811 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28813 else
28814 return -1;
28816 else
28817 die = l->dw_loc_oprnd1.v.val_die_ref.die;
28819 /* DWARF5 allows reference class, so we can then reference the DIE.
28820 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
28821 if (l->dw_loc_next != NULL && dwarf_version >= 5)
28823 a->dw_attr_val.val_class = dw_val_class_die_ref;
28824 a->dw_attr_val.val_entry = NULL;
28825 a->dw_attr_val.v.val_die_ref.die = die;
28826 a->dw_attr_val.v.val_die_ref.external = 0;
28827 return 0;
28830 dw_attr_node *av = get_AT (die, DW_AT_location);
28831 dw_loc_list_ref d;
28832 bool non_dwarf_expr = false;
28834 if (av == NULL)
28835 return dwarf_strict ? -1 : 0;
28836 switch (AT_class (av))
28838 case dw_val_class_loc_list:
28839 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28840 if (d->expr && non_dwarf_expression (d->expr))
28841 non_dwarf_expr = true;
28842 break;
28843 case dw_val_class_loc:
28844 lv = AT_loc (av);
28845 if (lv == NULL)
28846 return dwarf_strict ? -1 : 0;
28847 if (non_dwarf_expression (lv))
28848 non_dwarf_expr = true;
28849 break;
28850 default:
28851 return dwarf_strict ? -1 : 0;
28854 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
28855 into DW_OP_call4 or DW_OP_GNU_variable_value into
28856 DW_OP_call4 DW_OP_deref, do so. */
28857 if (!non_dwarf_expr
28858 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
28860 l->dw_loc_opc = DW_OP_call4;
28861 if (l->dw_loc_next)
28862 l->dw_loc_next = NULL;
28863 else
28864 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
28865 return 0;
28868 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
28869 copy over the DW_AT_location attribute from die to a. */
28870 if (l->dw_loc_next != NULL)
28872 a->dw_attr_val = av->dw_attr_val;
28873 return 1;
28876 dw_loc_list_ref list, *p;
28877 switch (AT_class (av))
28879 case dw_val_class_loc_list:
28880 p = &list;
28881 list = NULL;
28882 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
28884 lv = copy_deref_exprloc (d->expr);
28885 if (lv)
28887 *p = new_loc_list (lv, d->begin, d->end, d->section);
28888 p = &(*p)->dw_loc_next;
28890 else if (!dwarf_strict && d->expr)
28891 return 0;
28893 if (list == NULL)
28894 return dwarf_strict ? -1 : 0;
28895 a->dw_attr_val.val_class = dw_val_class_loc_list;
28896 gen_llsym (list);
28897 *AT_loc_list_ptr (a) = list;
28898 return 1;
28899 case dw_val_class_loc:
28900 lv = copy_deref_exprloc (AT_loc (av));
28901 if (lv == NULL)
28902 return dwarf_strict ? -1 : 0;
28903 a->dw_attr_val.v.val_loc = lv;
28904 return 1;
28905 default:
28906 gcc_unreachable ();
28910 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
28911 an address in .rodata section if the string literal is emitted there,
28912 or remove the containing location list or replace DW_AT_const_value
28913 with DW_AT_location and empty location expression, if it isn't found
28914 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
28915 to something that has been emitted in the current CU. */
28917 static void
28918 resolve_addr (dw_die_ref die)
28920 dw_die_ref c;
28921 dw_attr_node *a;
28922 dw_loc_list_ref *curr, *start, loc;
28923 unsigned ix;
28924 bool remove_AT_byte_size = false;
28926 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28927 switch (AT_class (a))
28929 case dw_val_class_loc_list:
28930 start = curr = AT_loc_list_ptr (a);
28931 loc = *curr;
28932 gcc_assert (loc);
28933 /* The same list can be referenced more than once. See if we have
28934 already recorded the result from a previous pass. */
28935 if (loc->replaced)
28936 *curr = loc->dw_loc_next;
28937 else if (!loc->resolved_addr)
28939 /* As things stand, we do not expect or allow one die to
28940 reference a suffix of another die's location list chain.
28941 References must be identical or completely separate.
28942 There is therefore no need to cache the result of this
28943 pass on any list other than the first; doing so
28944 would lead to unnecessary writes. */
28945 while (*curr)
28947 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
28948 if (!resolve_addr_in_expr (a, (*curr)->expr))
28950 dw_loc_list_ref next = (*curr)->dw_loc_next;
28951 dw_loc_descr_ref l = (*curr)->expr;
28953 if (next && (*curr)->ll_symbol)
28955 gcc_assert (!next->ll_symbol);
28956 next->ll_symbol = (*curr)->ll_symbol;
28958 if (dwarf_split_debug_info)
28959 remove_loc_list_addr_table_entries (l);
28960 *curr = next;
28962 else
28964 mark_base_types ((*curr)->expr);
28965 curr = &(*curr)->dw_loc_next;
28968 if (loc == *start)
28969 loc->resolved_addr = 1;
28970 else
28972 loc->replaced = 1;
28973 loc->dw_loc_next = *start;
28976 if (!*start)
28978 remove_AT (die, a->dw_attr);
28979 ix--;
28981 break;
28982 case dw_val_class_loc:
28984 dw_loc_descr_ref l = AT_loc (a);
28985 /* DW_OP_GNU_variable_value DW_OP_stack_value or
28986 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
28987 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
28988 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
28989 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
28990 with DW_FORM_ref referencing the same DIE as
28991 DW_OP_GNU_variable_value used to reference. */
28992 if (a->dw_attr == DW_AT_string_length
28993 && l
28994 && l->dw_loc_opc == DW_OP_GNU_variable_value
28995 && (l->dw_loc_next == NULL
28996 || (l->dw_loc_next->dw_loc_next == NULL
28997 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
28999 switch (optimize_string_length (a))
29001 case -1:
29002 remove_AT (die, a->dw_attr);
29003 ix--;
29004 /* If we drop DW_AT_string_length, we need to drop also
29005 DW_AT_{string_length_,}byte_size. */
29006 remove_AT_byte_size = true;
29007 continue;
29008 default:
29009 break;
29010 case 1:
29011 /* Even if we keep the optimized DW_AT_string_length,
29012 it might have changed AT_class, so process it again. */
29013 ix--;
29014 continue;
29017 /* For -gdwarf-2 don't attempt to optimize
29018 DW_AT_data_member_location containing
29019 DW_OP_plus_uconst - older consumers might
29020 rely on it being that op instead of a more complex,
29021 but shorter, location description. */
29022 if ((dwarf_version > 2
29023 || a->dw_attr != DW_AT_data_member_location
29024 || l == NULL
29025 || l->dw_loc_opc != DW_OP_plus_uconst
29026 || l->dw_loc_next != NULL)
29027 && !resolve_addr_in_expr (a, l))
29029 if (dwarf_split_debug_info)
29030 remove_loc_list_addr_table_entries (l);
29031 if (l != NULL
29032 && l->dw_loc_next == NULL
29033 && l->dw_loc_opc == DW_OP_addr
29034 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29035 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29036 && a->dw_attr == DW_AT_location)
29038 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29039 remove_AT (die, a->dw_attr);
29040 ix--;
29041 optimize_location_into_implicit_ptr (die, decl);
29042 break;
29044 if (a->dw_attr == DW_AT_string_length)
29045 /* If we drop DW_AT_string_length, we need to drop also
29046 DW_AT_{string_length_,}byte_size. */
29047 remove_AT_byte_size = true;
29048 remove_AT (die, a->dw_attr);
29049 ix--;
29051 else
29052 mark_base_types (l);
29054 break;
29055 case dw_val_class_addr:
29056 if (a->dw_attr == DW_AT_const_value
29057 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29059 if (AT_index (a) != NOT_INDEXED)
29060 remove_addr_table_entry (a->dw_attr_val.val_entry);
29061 remove_AT (die, a->dw_attr);
29062 ix--;
29064 if ((die->die_tag == DW_TAG_call_site
29065 && a->dw_attr == DW_AT_call_origin)
29066 || (die->die_tag == DW_TAG_GNU_call_site
29067 && a->dw_attr == DW_AT_abstract_origin))
29069 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29070 dw_die_ref tdie = lookup_decl_die (tdecl);
29071 dw_die_ref cdie;
29072 if (tdie == NULL
29073 && DECL_EXTERNAL (tdecl)
29074 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29075 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29077 dw_die_ref pdie = cdie;
29078 /* Make sure we don't add these DIEs into type units.
29079 We could emit skeleton DIEs for context (namespaces,
29080 outer structs/classes) and a skeleton DIE for the
29081 innermost context with DW_AT_signature pointing to the
29082 type unit. See PR78835. */
29083 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29084 pdie = pdie->die_parent;
29085 if (pdie == NULL)
29087 /* Creating a full DIE for tdecl is overly expensive and
29088 at this point even wrong when in the LTO phase
29089 as it can end up generating new type DIEs we didn't
29090 output and thus optimize_external_refs will crash. */
29091 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29092 add_AT_flag (tdie, DW_AT_external, 1);
29093 add_AT_flag (tdie, DW_AT_declaration, 1);
29094 add_linkage_attr (tdie, tdecl);
29095 add_name_and_src_coords_attributes (tdie, tdecl, true);
29096 equate_decl_number_to_die (tdecl, tdie);
29099 if (tdie)
29101 a->dw_attr_val.val_class = dw_val_class_die_ref;
29102 a->dw_attr_val.v.val_die_ref.die = tdie;
29103 a->dw_attr_val.v.val_die_ref.external = 0;
29105 else
29107 if (AT_index (a) != NOT_INDEXED)
29108 remove_addr_table_entry (a->dw_attr_val.val_entry);
29109 remove_AT (die, a->dw_attr);
29110 ix--;
29113 break;
29114 default:
29115 break;
29118 if (remove_AT_byte_size)
29119 remove_AT (die, dwarf_version >= 5
29120 ? DW_AT_string_length_byte_size
29121 : DW_AT_byte_size);
29123 FOR_EACH_CHILD (die, c, resolve_addr (c));
29126 /* Helper routines for optimize_location_lists.
29127 This pass tries to share identical local lists in .debug_loc
29128 section. */
29130 /* Iteratively hash operands of LOC opcode into HSTATE. */
29132 static void
29133 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29135 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29136 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29138 switch (loc->dw_loc_opc)
29140 case DW_OP_const4u:
29141 case DW_OP_const8u:
29142 if (loc->dtprel)
29143 goto hash_addr;
29144 /* FALLTHRU */
29145 case DW_OP_const1u:
29146 case DW_OP_const1s:
29147 case DW_OP_const2u:
29148 case DW_OP_const2s:
29149 case DW_OP_const4s:
29150 case DW_OP_const8s:
29151 case DW_OP_constu:
29152 case DW_OP_consts:
29153 case DW_OP_pick:
29154 case DW_OP_plus_uconst:
29155 case DW_OP_breg0:
29156 case DW_OP_breg1:
29157 case DW_OP_breg2:
29158 case DW_OP_breg3:
29159 case DW_OP_breg4:
29160 case DW_OP_breg5:
29161 case DW_OP_breg6:
29162 case DW_OP_breg7:
29163 case DW_OP_breg8:
29164 case DW_OP_breg9:
29165 case DW_OP_breg10:
29166 case DW_OP_breg11:
29167 case DW_OP_breg12:
29168 case DW_OP_breg13:
29169 case DW_OP_breg14:
29170 case DW_OP_breg15:
29171 case DW_OP_breg16:
29172 case DW_OP_breg17:
29173 case DW_OP_breg18:
29174 case DW_OP_breg19:
29175 case DW_OP_breg20:
29176 case DW_OP_breg21:
29177 case DW_OP_breg22:
29178 case DW_OP_breg23:
29179 case DW_OP_breg24:
29180 case DW_OP_breg25:
29181 case DW_OP_breg26:
29182 case DW_OP_breg27:
29183 case DW_OP_breg28:
29184 case DW_OP_breg29:
29185 case DW_OP_breg30:
29186 case DW_OP_breg31:
29187 case DW_OP_regx:
29188 case DW_OP_fbreg:
29189 case DW_OP_piece:
29190 case DW_OP_deref_size:
29191 case DW_OP_xderef_size:
29192 hstate.add_object (val1->v.val_int);
29193 break;
29194 case DW_OP_skip:
29195 case DW_OP_bra:
29197 int offset;
29199 gcc_assert (val1->val_class == dw_val_class_loc);
29200 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29201 hstate.add_object (offset);
29203 break;
29204 case DW_OP_implicit_value:
29205 hstate.add_object (val1->v.val_unsigned);
29206 switch (val2->val_class)
29208 case dw_val_class_const:
29209 hstate.add_object (val2->v.val_int);
29210 break;
29211 case dw_val_class_vec:
29213 unsigned int elt_size = val2->v.val_vec.elt_size;
29214 unsigned int len = val2->v.val_vec.length;
29216 hstate.add_int (elt_size);
29217 hstate.add_int (len);
29218 hstate.add (val2->v.val_vec.array, len * elt_size);
29220 break;
29221 case dw_val_class_const_double:
29222 hstate.add_object (val2->v.val_double.low);
29223 hstate.add_object (val2->v.val_double.high);
29224 break;
29225 case dw_val_class_wide_int:
29226 hstate.add (val2->v.val_wide->get_val (),
29227 get_full_len (*val2->v.val_wide)
29228 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29229 break;
29230 case dw_val_class_addr:
29231 inchash::add_rtx (val2->v.val_addr, hstate);
29232 break;
29233 default:
29234 gcc_unreachable ();
29236 break;
29237 case DW_OP_bregx:
29238 case DW_OP_bit_piece:
29239 hstate.add_object (val1->v.val_int);
29240 hstate.add_object (val2->v.val_int);
29241 break;
29242 case DW_OP_addr:
29243 hash_addr:
29244 if (loc->dtprel)
29246 unsigned char dtprel = 0xd1;
29247 hstate.add_object (dtprel);
29249 inchash::add_rtx (val1->v.val_addr, hstate);
29250 break;
29251 case DW_OP_GNU_addr_index:
29252 case DW_OP_GNU_const_index:
29254 if (loc->dtprel)
29256 unsigned char dtprel = 0xd1;
29257 hstate.add_object (dtprel);
29259 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29261 break;
29262 case DW_OP_implicit_pointer:
29263 case DW_OP_GNU_implicit_pointer:
29264 hstate.add_int (val2->v.val_int);
29265 break;
29266 case DW_OP_entry_value:
29267 case DW_OP_GNU_entry_value:
29268 hstate.add_object (val1->v.val_loc);
29269 break;
29270 case DW_OP_regval_type:
29271 case DW_OP_deref_type:
29272 case DW_OP_GNU_regval_type:
29273 case DW_OP_GNU_deref_type:
29275 unsigned int byte_size
29276 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29277 unsigned int encoding
29278 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29279 hstate.add_object (val1->v.val_int);
29280 hstate.add_object (byte_size);
29281 hstate.add_object (encoding);
29283 break;
29284 case DW_OP_convert:
29285 case DW_OP_reinterpret:
29286 case DW_OP_GNU_convert:
29287 case DW_OP_GNU_reinterpret:
29288 if (val1->val_class == dw_val_class_unsigned_const)
29290 hstate.add_object (val1->v.val_unsigned);
29291 break;
29293 /* FALLTHRU */
29294 case DW_OP_const_type:
29295 case DW_OP_GNU_const_type:
29297 unsigned int byte_size
29298 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29299 unsigned int encoding
29300 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29301 hstate.add_object (byte_size);
29302 hstate.add_object (encoding);
29303 if (loc->dw_loc_opc != DW_OP_const_type
29304 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29305 break;
29306 hstate.add_object (val2->val_class);
29307 switch (val2->val_class)
29309 case dw_val_class_const:
29310 hstate.add_object (val2->v.val_int);
29311 break;
29312 case dw_val_class_vec:
29314 unsigned int elt_size = val2->v.val_vec.elt_size;
29315 unsigned int len = val2->v.val_vec.length;
29317 hstate.add_object (elt_size);
29318 hstate.add_object (len);
29319 hstate.add (val2->v.val_vec.array, len * elt_size);
29321 break;
29322 case dw_val_class_const_double:
29323 hstate.add_object (val2->v.val_double.low);
29324 hstate.add_object (val2->v.val_double.high);
29325 break;
29326 case dw_val_class_wide_int:
29327 hstate.add (val2->v.val_wide->get_val (),
29328 get_full_len (*val2->v.val_wide)
29329 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29330 break;
29331 default:
29332 gcc_unreachable ();
29335 break;
29337 default:
29338 /* Other codes have no operands. */
29339 break;
29343 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29345 static inline void
29346 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29348 dw_loc_descr_ref l;
29349 bool sizes_computed = false;
29350 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29351 size_of_locs (loc);
29353 for (l = loc; l != NULL; l = l->dw_loc_next)
29355 enum dwarf_location_atom opc = l->dw_loc_opc;
29356 hstate.add_object (opc);
29357 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29359 size_of_locs (loc);
29360 sizes_computed = true;
29362 hash_loc_operands (l, hstate);
29366 /* Compute hash of the whole location list LIST_HEAD. */
29368 static inline void
29369 hash_loc_list (dw_loc_list_ref list_head)
29371 dw_loc_list_ref curr = list_head;
29372 inchash::hash hstate;
29374 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29376 hstate.add (curr->begin, strlen (curr->begin) + 1);
29377 hstate.add (curr->end, strlen (curr->end) + 1);
29378 if (curr->section)
29379 hstate.add (curr->section, strlen (curr->section) + 1);
29380 hash_locs (curr->expr, hstate);
29382 list_head->hash = hstate.end ();
29385 /* Return true if X and Y opcodes have the same operands. */
29387 static inline bool
29388 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29390 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29391 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29392 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29393 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29395 switch (x->dw_loc_opc)
29397 case DW_OP_const4u:
29398 case DW_OP_const8u:
29399 if (x->dtprel)
29400 goto hash_addr;
29401 /* FALLTHRU */
29402 case DW_OP_const1u:
29403 case DW_OP_const1s:
29404 case DW_OP_const2u:
29405 case DW_OP_const2s:
29406 case DW_OP_const4s:
29407 case DW_OP_const8s:
29408 case DW_OP_constu:
29409 case DW_OP_consts:
29410 case DW_OP_pick:
29411 case DW_OP_plus_uconst:
29412 case DW_OP_breg0:
29413 case DW_OP_breg1:
29414 case DW_OP_breg2:
29415 case DW_OP_breg3:
29416 case DW_OP_breg4:
29417 case DW_OP_breg5:
29418 case DW_OP_breg6:
29419 case DW_OP_breg7:
29420 case DW_OP_breg8:
29421 case DW_OP_breg9:
29422 case DW_OP_breg10:
29423 case DW_OP_breg11:
29424 case DW_OP_breg12:
29425 case DW_OP_breg13:
29426 case DW_OP_breg14:
29427 case DW_OP_breg15:
29428 case DW_OP_breg16:
29429 case DW_OP_breg17:
29430 case DW_OP_breg18:
29431 case DW_OP_breg19:
29432 case DW_OP_breg20:
29433 case DW_OP_breg21:
29434 case DW_OP_breg22:
29435 case DW_OP_breg23:
29436 case DW_OP_breg24:
29437 case DW_OP_breg25:
29438 case DW_OP_breg26:
29439 case DW_OP_breg27:
29440 case DW_OP_breg28:
29441 case DW_OP_breg29:
29442 case DW_OP_breg30:
29443 case DW_OP_breg31:
29444 case DW_OP_regx:
29445 case DW_OP_fbreg:
29446 case DW_OP_piece:
29447 case DW_OP_deref_size:
29448 case DW_OP_xderef_size:
29449 return valx1->v.val_int == valy1->v.val_int;
29450 case DW_OP_skip:
29451 case DW_OP_bra:
29452 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29453 can cause irrelevant differences in dw_loc_addr. */
29454 gcc_assert (valx1->val_class == dw_val_class_loc
29455 && valy1->val_class == dw_val_class_loc
29456 && (dwarf_split_debug_info
29457 || x->dw_loc_addr == y->dw_loc_addr));
29458 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29459 case DW_OP_implicit_value:
29460 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29461 || valx2->val_class != valy2->val_class)
29462 return false;
29463 switch (valx2->val_class)
29465 case dw_val_class_const:
29466 return valx2->v.val_int == valy2->v.val_int;
29467 case dw_val_class_vec:
29468 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29469 && valx2->v.val_vec.length == valy2->v.val_vec.length
29470 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29471 valx2->v.val_vec.elt_size
29472 * valx2->v.val_vec.length) == 0;
29473 case dw_val_class_const_double:
29474 return valx2->v.val_double.low == valy2->v.val_double.low
29475 && valx2->v.val_double.high == valy2->v.val_double.high;
29476 case dw_val_class_wide_int:
29477 return *valx2->v.val_wide == *valy2->v.val_wide;
29478 case dw_val_class_addr:
29479 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29480 default:
29481 gcc_unreachable ();
29483 case DW_OP_bregx:
29484 case DW_OP_bit_piece:
29485 return valx1->v.val_int == valy1->v.val_int
29486 && valx2->v.val_int == valy2->v.val_int;
29487 case DW_OP_addr:
29488 hash_addr:
29489 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29490 case DW_OP_GNU_addr_index:
29491 case DW_OP_GNU_const_index:
29493 rtx ax1 = valx1->val_entry->addr.rtl;
29494 rtx ay1 = valy1->val_entry->addr.rtl;
29495 return rtx_equal_p (ax1, ay1);
29497 case DW_OP_implicit_pointer:
29498 case DW_OP_GNU_implicit_pointer:
29499 return valx1->val_class == dw_val_class_die_ref
29500 && valx1->val_class == valy1->val_class
29501 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29502 && valx2->v.val_int == valy2->v.val_int;
29503 case DW_OP_entry_value:
29504 case DW_OP_GNU_entry_value:
29505 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29506 case DW_OP_const_type:
29507 case DW_OP_GNU_const_type:
29508 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29509 || valx2->val_class != valy2->val_class)
29510 return false;
29511 switch (valx2->val_class)
29513 case dw_val_class_const:
29514 return valx2->v.val_int == valy2->v.val_int;
29515 case dw_val_class_vec:
29516 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29517 && valx2->v.val_vec.length == valy2->v.val_vec.length
29518 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29519 valx2->v.val_vec.elt_size
29520 * valx2->v.val_vec.length) == 0;
29521 case dw_val_class_const_double:
29522 return valx2->v.val_double.low == valy2->v.val_double.low
29523 && valx2->v.val_double.high == valy2->v.val_double.high;
29524 case dw_val_class_wide_int:
29525 return *valx2->v.val_wide == *valy2->v.val_wide;
29526 default:
29527 gcc_unreachable ();
29529 case DW_OP_regval_type:
29530 case DW_OP_deref_type:
29531 case DW_OP_GNU_regval_type:
29532 case DW_OP_GNU_deref_type:
29533 return valx1->v.val_int == valy1->v.val_int
29534 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29535 case DW_OP_convert:
29536 case DW_OP_reinterpret:
29537 case DW_OP_GNU_convert:
29538 case DW_OP_GNU_reinterpret:
29539 if (valx1->val_class != valy1->val_class)
29540 return false;
29541 if (valx1->val_class == dw_val_class_unsigned_const)
29542 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29543 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29544 case DW_OP_GNU_parameter_ref:
29545 return valx1->val_class == dw_val_class_die_ref
29546 && valx1->val_class == valy1->val_class
29547 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29548 default:
29549 /* Other codes have no operands. */
29550 return true;
29554 /* Return true if DWARF location expressions X and Y are the same. */
29556 static inline bool
29557 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29559 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29560 if (x->dw_loc_opc != y->dw_loc_opc
29561 || x->dtprel != y->dtprel
29562 || !compare_loc_operands (x, y))
29563 break;
29564 return x == NULL && y == NULL;
29567 /* Hashtable helpers. */
29569 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29571 static inline hashval_t hash (const dw_loc_list_struct *);
29572 static inline bool equal (const dw_loc_list_struct *,
29573 const dw_loc_list_struct *);
29576 /* Return precomputed hash of location list X. */
29578 inline hashval_t
29579 loc_list_hasher::hash (const dw_loc_list_struct *x)
29581 return x->hash;
29584 /* Return true if location lists A and B are the same. */
29586 inline bool
29587 loc_list_hasher::equal (const dw_loc_list_struct *a,
29588 const dw_loc_list_struct *b)
29590 if (a == b)
29591 return 1;
29592 if (a->hash != b->hash)
29593 return 0;
29594 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29595 if (strcmp (a->begin, b->begin) != 0
29596 || strcmp (a->end, b->end) != 0
29597 || (a->section == NULL) != (b->section == NULL)
29598 || (a->section && strcmp (a->section, b->section) != 0)
29599 || !compare_locs (a->expr, b->expr))
29600 break;
29601 return a == NULL && b == NULL;
29604 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29607 /* Recursively optimize location lists referenced from DIE
29608 children and share them whenever possible. */
29610 static void
29611 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29613 dw_die_ref c;
29614 dw_attr_node *a;
29615 unsigned ix;
29616 dw_loc_list_struct **slot;
29618 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29619 if (AT_class (a) == dw_val_class_loc_list)
29621 dw_loc_list_ref list = AT_loc_list (a);
29622 /* TODO: perform some optimizations here, before hashing
29623 it and storing into the hash table. */
29624 hash_loc_list (list);
29625 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29626 if (*slot == NULL)
29627 *slot = list;
29628 else
29629 a->dw_attr_val.v.val_loc_list = *slot;
29632 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29636 /* Recursively assign each location list a unique index into the debug_addr
29637 section. */
29639 static void
29640 index_location_lists (dw_die_ref die)
29642 dw_die_ref c;
29643 dw_attr_node *a;
29644 unsigned ix;
29646 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29647 if (AT_class (a) == dw_val_class_loc_list)
29649 dw_loc_list_ref list = AT_loc_list (a);
29650 dw_loc_list_ref curr;
29651 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29653 /* Don't index an entry that has already been indexed
29654 or won't be output. */
29655 if (curr->begin_entry != NULL
29656 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29657 continue;
29659 curr->begin_entry
29660 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29664 FOR_EACH_CHILD (die, c, index_location_lists (c));
29667 /* Optimize location lists referenced from DIE
29668 children and share them whenever possible. */
29670 static void
29671 optimize_location_lists (dw_die_ref die)
29673 loc_list_hash_type htab (500);
29674 optimize_location_lists_1 (die, &htab);
29677 /* Traverse the limbo die list, and add parent/child links. The only
29678 dies without parents that should be here are concrete instances of
29679 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29680 For concrete instances, we can get the parent die from the abstract
29681 instance. */
29683 static void
29684 flush_limbo_die_list (void)
29686 limbo_die_node *node;
29688 /* get_context_die calls force_decl_die, which can put new DIEs on the
29689 limbo list in LTO mode when nested functions are put in a different
29690 partition than that of their parent function. */
29691 while ((node = limbo_die_list))
29693 dw_die_ref die = node->die;
29694 limbo_die_list = node->next;
29696 if (die->die_parent == NULL)
29698 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29700 if (origin && origin->die_parent)
29701 add_child_die (origin->die_parent, die);
29702 else if (is_cu_die (die))
29704 else if (seen_error ())
29705 /* It's OK to be confused by errors in the input. */
29706 add_child_die (comp_unit_die (), die);
29707 else
29709 /* In certain situations, the lexical block containing a
29710 nested function can be optimized away, which results
29711 in the nested function die being orphaned. Likewise
29712 with the return type of that nested function. Force
29713 this to be a child of the containing function.
29715 It may happen that even the containing function got fully
29716 inlined and optimized out. In that case we are lost and
29717 assign the empty child. This should not be big issue as
29718 the function is likely unreachable too. */
29719 gcc_assert (node->created_for);
29721 if (DECL_P (node->created_for))
29722 origin = get_context_die (DECL_CONTEXT (node->created_for));
29723 else if (TYPE_P (node->created_for))
29724 origin = scope_die_for (node->created_for, comp_unit_die ());
29725 else
29726 origin = comp_unit_die ();
29728 add_child_die (origin, die);
29734 /* Reset DIEs so we can output them again. */
29736 static void
29737 reset_dies (dw_die_ref die)
29739 dw_die_ref c;
29741 /* Remove stuff we re-generate. */
29742 die->die_mark = 0;
29743 die->die_offset = 0;
29744 die->die_abbrev = 0;
29745 remove_AT (die, DW_AT_sibling);
29747 FOR_EACH_CHILD (die, c, reset_dies (c));
29750 /* Output stuff that dwarf requires at the end of every file,
29751 and generate the DWARF-2 debugging info. */
29753 static void
29754 dwarf2out_finish (const char *)
29756 comdat_type_node *ctnode;
29757 dw_die_ref main_comp_unit_die;
29758 unsigned char checksum[16];
29759 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29761 /* Flush out any latecomers to the limbo party. */
29762 flush_limbo_die_list ();
29764 if (flag_checking)
29766 verify_die (comp_unit_die ());
29767 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29768 verify_die (node->die);
29771 /* We shouldn't have any symbols with delayed asm names for
29772 DIEs generated after early finish. */
29773 gcc_assert (deferred_asm_name == NULL);
29775 gen_remaining_tmpl_value_param_die_attribute ();
29777 if (flag_generate_lto || flag_generate_offload)
29779 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
29781 /* Prune stuff so that dwarf2out_finish runs successfully
29782 for the fat part of the object. */
29783 reset_dies (comp_unit_die ());
29784 for (limbo_die_node *node = cu_die_list; node; node = node->next)
29785 reset_dies (node->die);
29787 hash_table<comdat_type_hasher> comdat_type_table (100);
29788 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29790 comdat_type_node **slot
29791 = comdat_type_table.find_slot (ctnode, INSERT);
29793 /* Don't reset types twice. */
29794 if (*slot != HTAB_EMPTY_ENTRY)
29795 continue;
29797 /* Add a pointer to the line table for the main compilation unit
29798 so that the debugger can make sense of DW_AT_decl_file
29799 attributes. */
29800 if (debug_info_level >= DINFO_LEVEL_TERSE)
29801 reset_dies (ctnode->root_die);
29803 *slot = ctnode;
29806 /* Reset die CU symbol so we don't output it twice. */
29807 comp_unit_die ()->die_id.die_symbol = NULL;
29809 /* Remove DW_AT_macro from the early output. */
29810 if (have_macinfo)
29811 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
29813 /* Remove indirect string decisions. */
29814 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
29817 #if ENABLE_ASSERT_CHECKING
29819 dw_die_ref die = comp_unit_die (), c;
29820 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
29822 #endif
29823 resolve_addr (comp_unit_die ());
29824 move_marked_base_types ();
29826 /* Initialize sections and labels used for actual assembler output. */
29827 init_sections_and_labels (false);
29829 /* Traverse the DIE's and add sibling attributes to those DIE's that
29830 have children. */
29831 add_sibling_attributes (comp_unit_die ());
29832 limbo_die_node *node;
29833 for (node = cu_die_list; node; node = node->next)
29834 add_sibling_attributes (node->die);
29835 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29836 add_sibling_attributes (ctnode->root_die);
29838 /* When splitting DWARF info, we put some attributes in the
29839 skeleton compile_unit DIE that remains in the .o, while
29840 most attributes go in the DWO compile_unit_die. */
29841 if (dwarf_split_debug_info)
29843 limbo_die_node *cu;
29844 main_comp_unit_die = gen_compile_unit_die (NULL);
29845 if (dwarf_version >= 5)
29846 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
29847 cu = limbo_die_list;
29848 gcc_assert (cu->die == main_comp_unit_die);
29849 limbo_die_list = limbo_die_list->next;
29850 cu->next = cu_die_list;
29851 cu_die_list = cu;
29853 else
29854 main_comp_unit_die = comp_unit_die ();
29856 /* Output a terminator label for the .text section. */
29857 switch_to_section (text_section);
29858 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
29859 if (cold_text_section)
29861 switch_to_section (cold_text_section);
29862 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
29865 /* We can only use the low/high_pc attributes if all of the code was
29866 in .text. */
29867 if (!have_multiple_function_sections
29868 || (dwarf_version < 3 && dwarf_strict))
29870 /* Don't add if the CU has no associated code. */
29871 if (text_section_used)
29872 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
29873 text_end_label, true);
29875 else
29877 unsigned fde_idx;
29878 dw_fde_ref fde;
29879 bool range_list_added = false;
29881 if (text_section_used)
29882 add_ranges_by_labels (main_comp_unit_die, text_section_label,
29883 text_end_label, &range_list_added, true);
29884 if (cold_text_section_used)
29885 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
29886 cold_end_label, &range_list_added, true);
29888 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
29890 if (DECL_IGNORED_P (fde->decl))
29891 continue;
29892 if (!fde->in_std_section)
29893 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
29894 fde->dw_fde_end, &range_list_added,
29895 true);
29896 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
29897 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
29898 fde->dw_fde_second_end, &range_list_added,
29899 true);
29902 if (range_list_added)
29904 /* We need to give .debug_loc and .debug_ranges an appropriate
29905 "base address". Use zero so that these addresses become
29906 absolute. Historically, we've emitted the unexpected
29907 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
29908 Emit both to give time for other tools to adapt. */
29909 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
29910 if (! dwarf_strict && dwarf_version < 4)
29911 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
29913 add_ranges (NULL);
29917 /* AIX Assembler inserts the length, so adjust the reference to match the
29918 offset expected by debuggers. */
29919 strcpy (dl_section_ref, debug_line_section_label);
29920 if (XCOFF_DEBUGGING_INFO)
29921 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
29923 if (debug_info_level >= DINFO_LEVEL_TERSE)
29924 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
29925 dl_section_ref);
29927 if (have_macinfo)
29928 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
29929 macinfo_section_label);
29931 if (dwarf_split_debug_info)
29933 if (have_location_lists)
29935 if (dwarf_version >= 5)
29936 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
29937 loc_section_label);
29938 /* optimize_location_lists calculates the size of the lists,
29939 so index them first, and assign indices to the entries.
29940 Although optimize_location_lists will remove entries from
29941 the table, it only does so for duplicates, and therefore
29942 only reduces ref_counts to 1. */
29943 index_location_lists (comp_unit_die ());
29946 if (addr_index_table != NULL)
29948 unsigned int index = 0;
29949 addr_index_table
29950 ->traverse_noresize<unsigned int *, index_addr_table_entry>
29951 (&index);
29955 loc_list_idx = 0;
29956 if (have_location_lists)
29958 optimize_location_lists (comp_unit_die ());
29959 /* And finally assign indexes to the entries for -gsplit-dwarf. */
29960 if (dwarf_version >= 5 && dwarf_split_debug_info)
29961 assign_location_list_indexes (comp_unit_die ());
29964 save_macinfo_strings ();
29966 if (dwarf_split_debug_info)
29968 unsigned int index = 0;
29970 /* Add attributes common to skeleton compile_units and
29971 type_units. Because these attributes include strings, it
29972 must be done before freezing the string table. Top-level
29973 skeleton die attrs are added when the skeleton type unit is
29974 created, so ensure it is created by this point. */
29975 add_top_level_skeleton_die_attrs (main_comp_unit_die);
29976 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
29979 /* Output all of the compilation units. We put the main one last so that
29980 the offsets are available to output_pubnames. */
29981 for (node = cu_die_list; node; node = node->next)
29982 output_comp_unit (node->die, 0, NULL);
29984 hash_table<comdat_type_hasher> comdat_type_table (100);
29985 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
29987 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
29989 /* Don't output duplicate types. */
29990 if (*slot != HTAB_EMPTY_ENTRY)
29991 continue;
29993 /* Add a pointer to the line table for the main compilation unit
29994 so that the debugger can make sense of DW_AT_decl_file
29995 attributes. */
29996 if (debug_info_level >= DINFO_LEVEL_TERSE)
29997 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
29998 (!dwarf_split_debug_info
29999 ? dl_section_ref
30000 : debug_skeleton_line_section_label));
30002 output_comdat_type_unit (ctnode);
30003 *slot = ctnode;
30006 if (dwarf_split_debug_info)
30008 int mark;
30009 struct md5_ctx ctx;
30011 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30012 index_rnglists ();
30014 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30015 md5_init_ctx (&ctx);
30016 mark = 0;
30017 die_checksum (comp_unit_die (), &ctx, &mark);
30018 unmark_all_dies (comp_unit_die ());
30019 md5_finish_ctx (&ctx, checksum);
30021 if (dwarf_version < 5)
30023 /* Use the first 8 bytes of the checksum as the dwo_id,
30024 and add it to both comp-unit DIEs. */
30025 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30026 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30029 /* Add the base offset of the ranges table to the skeleton
30030 comp-unit DIE. */
30031 if (!vec_safe_is_empty (ranges_table))
30033 if (dwarf_version >= 5)
30034 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30035 ranges_base_label);
30036 else
30037 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30038 ranges_section_label);
30041 switch_to_section (debug_addr_section);
30042 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30043 output_addr_table ();
30046 /* Output the main compilation unit if non-empty or if .debug_macinfo
30047 or .debug_macro will be emitted. */
30048 output_comp_unit (comp_unit_die (), have_macinfo,
30049 dwarf_split_debug_info ? checksum : NULL);
30051 if (dwarf_split_debug_info && info_section_emitted)
30052 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30054 /* Output the abbreviation table. */
30055 if (vec_safe_length (abbrev_die_table) != 1)
30057 switch_to_section (debug_abbrev_section);
30058 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30059 output_abbrev_section ();
30062 /* Output location list section if necessary. */
30063 if (have_location_lists)
30065 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30066 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30067 /* Output the location lists info. */
30068 switch_to_section (debug_loc_section);
30069 if (dwarf_version >= 5)
30071 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30072 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30073 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30074 dw2_asm_output_data (4, 0xffffffff,
30075 "Initial length escape value indicating "
30076 "64-bit DWARF extension");
30077 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30078 "Length of Location Lists");
30079 ASM_OUTPUT_LABEL (asm_out_file, l1);
30080 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30081 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30082 dw2_asm_output_data (1, 0, "Segment Size");
30083 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30084 "Offset Entry Count");
30086 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30087 if (dwarf_version >= 5 && dwarf_split_debug_info)
30089 unsigned int save_loc_list_idx = loc_list_idx;
30090 loc_list_idx = 0;
30091 output_loclists_offsets (comp_unit_die ());
30092 gcc_assert (save_loc_list_idx == loc_list_idx);
30094 output_location_lists (comp_unit_die ());
30095 if (dwarf_version >= 5)
30096 ASM_OUTPUT_LABEL (asm_out_file, l2);
30099 output_pubtables ();
30101 /* Output the address range information if a CU (.debug_info section)
30102 was emitted. We output an empty table even if we had no functions
30103 to put in it. This because the consumer has no way to tell the
30104 difference between an empty table that we omitted and failure to
30105 generate a table that would have contained data. */
30106 if (info_section_emitted)
30108 switch_to_section (debug_aranges_section);
30109 output_aranges ();
30112 /* Output ranges section if necessary. */
30113 if (!vec_safe_is_empty (ranges_table))
30115 if (dwarf_version >= 5)
30116 output_rnglists ();
30117 else
30118 output_ranges ();
30121 /* Have to end the macro section. */
30122 if (have_macinfo)
30124 switch_to_section (debug_macinfo_section);
30125 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30126 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30127 : debug_skeleton_line_section_label, false);
30128 dw2_asm_output_data (1, 0, "End compilation unit");
30131 /* Output the source line correspondence table. We must do this
30132 even if there is no line information. Otherwise, on an empty
30133 translation unit, we will generate a present, but empty,
30134 .debug_info section. IRIX 6.5 `nm' will then complain when
30135 examining the file. This is done late so that any filenames
30136 used by the debug_info section are marked as 'used'. */
30137 switch_to_section (debug_line_section);
30138 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30139 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30140 output_line_info (false);
30142 if (dwarf_split_debug_info && info_section_emitted)
30144 switch_to_section (debug_skeleton_line_section);
30145 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30146 output_line_info (true);
30149 /* If we emitted any indirect strings, output the string table too. */
30150 if (debug_str_hash || skeleton_debug_str_hash)
30151 output_indirect_strings ();
30152 if (debug_line_str_hash)
30154 switch_to_section (debug_line_str_section);
30155 const enum dwarf_form form = DW_FORM_line_strp;
30156 debug_line_str_hash->traverse<enum dwarf_form,
30157 output_indirect_string> (form);
30161 /* Returns a hash value for X (which really is a variable_value_struct). */
30163 inline hashval_t
30164 variable_value_hasher::hash (variable_value_struct *x)
30166 return (hashval_t) x->decl_id;
30169 /* Return nonzero if decl_id of variable_value_struct X is the same as
30170 UID of decl Y. */
30172 inline bool
30173 variable_value_hasher::equal (variable_value_struct *x, tree y)
30175 return x->decl_id == DECL_UID (y);
30178 /* Helper function for resolve_variable_value, handle
30179 DW_OP_GNU_variable_value in one location expression.
30180 Return true if exprloc has been changed into loclist. */
30182 static bool
30183 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30185 dw_loc_descr_ref next;
30186 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30188 next = loc->dw_loc_next;
30189 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30190 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30191 continue;
30193 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30194 if (DECL_CONTEXT (decl) != current_function_decl)
30195 continue;
30197 dw_die_ref ref = lookup_decl_die (decl);
30198 if (ref)
30200 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30201 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30202 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30203 continue;
30205 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30206 if (l == NULL)
30207 continue;
30208 if (l->dw_loc_next)
30210 if (AT_class (a) != dw_val_class_loc)
30211 continue;
30212 switch (a->dw_attr)
30214 /* Following attributes allow both exprloc and loclist
30215 classes, so we can change them into a loclist. */
30216 case DW_AT_location:
30217 case DW_AT_string_length:
30218 case DW_AT_return_addr:
30219 case DW_AT_data_member_location:
30220 case DW_AT_frame_base:
30221 case DW_AT_segment:
30222 case DW_AT_static_link:
30223 case DW_AT_use_location:
30224 case DW_AT_vtable_elem_location:
30225 if (prev)
30227 prev->dw_loc_next = NULL;
30228 prepend_loc_descr_to_each (l, AT_loc (a));
30230 if (next)
30231 add_loc_descr_to_each (l, next);
30232 a->dw_attr_val.val_class = dw_val_class_loc_list;
30233 a->dw_attr_val.val_entry = NULL;
30234 a->dw_attr_val.v.val_loc_list = l;
30235 have_location_lists = true;
30236 return true;
30237 /* Following attributes allow both exprloc and reference,
30238 so if the whole expression is DW_OP_GNU_variable_value alone
30239 we could transform it into reference. */
30240 case DW_AT_byte_size:
30241 case DW_AT_bit_size:
30242 case DW_AT_lower_bound:
30243 case DW_AT_upper_bound:
30244 case DW_AT_bit_stride:
30245 case DW_AT_count:
30246 case DW_AT_allocated:
30247 case DW_AT_associated:
30248 case DW_AT_byte_stride:
30249 if (prev == NULL && next == NULL)
30250 break;
30251 /* FALLTHRU */
30252 default:
30253 if (dwarf_strict)
30254 continue;
30255 break;
30257 /* Create DW_TAG_variable that we can refer to. */
30258 gen_decl_die (decl, NULL_TREE, NULL,
30259 lookup_decl_die (current_function_decl));
30260 ref = lookup_decl_die (decl);
30261 if (ref)
30263 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30264 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30265 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30267 continue;
30269 if (prev)
30271 prev->dw_loc_next = l->expr;
30272 add_loc_descr (&prev->dw_loc_next, next);
30273 free_loc_descr (loc, NULL);
30274 next = prev->dw_loc_next;
30276 else
30278 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30279 add_loc_descr (&loc, next);
30280 next = loc;
30282 loc = prev;
30284 return false;
30287 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30289 static void
30290 resolve_variable_value (dw_die_ref die)
30292 dw_attr_node *a;
30293 dw_loc_list_ref loc;
30294 unsigned ix;
30296 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30297 switch (AT_class (a))
30299 case dw_val_class_loc:
30300 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30301 break;
30302 /* FALLTHRU */
30303 case dw_val_class_loc_list:
30304 loc = AT_loc_list (a);
30305 gcc_assert (loc);
30306 for (; loc; loc = loc->dw_loc_next)
30307 resolve_variable_value_in_expr (a, loc->expr);
30308 break;
30309 default:
30310 break;
30314 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30315 temporaries in the current function. */
30317 static void
30318 resolve_variable_values (void)
30320 if (!variable_value_hash || !current_function_decl)
30321 return;
30323 struct variable_value_struct *node
30324 = variable_value_hash->find_with_hash (current_function_decl,
30325 DECL_UID (current_function_decl));
30327 if (node == NULL)
30328 return;
30330 unsigned int i;
30331 dw_die_ref die;
30332 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30333 resolve_variable_value (die);
30336 /* Helper function for note_variable_value, handle one location
30337 expression. */
30339 static void
30340 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30342 for (; loc; loc = loc->dw_loc_next)
30343 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30344 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30346 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30347 dw_die_ref ref = lookup_decl_die (decl);
30348 if (! ref && (flag_generate_lto || flag_generate_offload))
30350 /* ??? This is somewhat a hack because we do not create DIEs
30351 for variables not in BLOCK trees early but when generating
30352 early LTO output we need the dw_val_class_decl_ref to be
30353 fully resolved. For fat LTO objects we'd also like to
30354 undo this after LTO dwarf output. */
30355 gcc_assert (DECL_CONTEXT (decl));
30356 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30357 gcc_assert (ctx != NULL);
30358 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30359 ref = lookup_decl_die (decl);
30360 gcc_assert (ref != NULL);
30362 if (ref)
30364 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30365 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30366 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30367 continue;
30369 if (VAR_P (decl)
30370 && DECL_CONTEXT (decl)
30371 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30372 && lookup_decl_die (DECL_CONTEXT (decl)))
30374 if (!variable_value_hash)
30375 variable_value_hash
30376 = hash_table<variable_value_hasher>::create_ggc (10);
30378 tree fndecl = DECL_CONTEXT (decl);
30379 struct variable_value_struct *node;
30380 struct variable_value_struct **slot
30381 = variable_value_hash->find_slot_with_hash (fndecl,
30382 DECL_UID (fndecl),
30383 INSERT);
30384 if (*slot == NULL)
30386 node = ggc_cleared_alloc<variable_value_struct> ();
30387 node->decl_id = DECL_UID (fndecl);
30388 *slot = node;
30390 else
30391 node = *slot;
30393 vec_safe_push (node->dies, die);
30398 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30399 with dw_val_class_decl_ref operand. */
30401 static void
30402 note_variable_value (dw_die_ref die)
30404 dw_die_ref c;
30405 dw_attr_node *a;
30406 dw_loc_list_ref loc;
30407 unsigned ix;
30409 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30410 switch (AT_class (a))
30412 case dw_val_class_loc_list:
30413 loc = AT_loc_list (a);
30414 gcc_assert (loc);
30415 if (!loc->noted_variable_value)
30417 loc->noted_variable_value = 1;
30418 for (; loc; loc = loc->dw_loc_next)
30419 note_variable_value_in_expr (die, loc->expr);
30421 break;
30422 case dw_val_class_loc:
30423 note_variable_value_in_expr (die, AT_loc (a));
30424 break;
30425 default:
30426 break;
30429 /* Mark children. */
30430 FOR_EACH_CHILD (die, c, note_variable_value (c));
30433 /* Perform any cleanups needed after the early debug generation pass
30434 has run. */
30436 static void
30437 dwarf2out_early_finish (const char *filename)
30439 set_early_dwarf s;
30441 /* PCH might result in DW_AT_producer string being restored from the
30442 header compilation, so always fill it with empty string initially
30443 and overwrite only here. */
30444 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30445 producer_string = gen_producer_string ();
30446 producer->dw_attr_val.v.val_str->refcount--;
30447 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30449 /* Add the name for the main input file now. We delayed this from
30450 dwarf2out_init to avoid complications with PCH. */
30451 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30452 add_comp_dir_attribute (comp_unit_die ());
30454 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30455 DW_AT_comp_dir into .debug_line_str section. */
30456 if (!DWARF2_ASM_LINE_DEBUG_INFO
30457 && dwarf_version >= 5
30458 && DWARF5_USE_DEBUG_LINE_STR)
30460 for (int i = 0; i < 2; i++)
30462 dw_attr_node *a = get_AT (comp_unit_die (),
30463 i ? DW_AT_comp_dir : DW_AT_name);
30464 if (a == NULL
30465 || AT_class (a) != dw_val_class_str
30466 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30467 continue;
30469 if (! debug_line_str_hash)
30470 debug_line_str_hash
30471 = hash_table<indirect_string_hasher>::create_ggc (10);
30473 struct indirect_string_node *node
30474 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30475 set_indirect_string (node);
30476 node->form = DW_FORM_line_strp;
30477 a->dw_attr_val.v.val_str->refcount--;
30478 a->dw_attr_val.v.val_str = node;
30482 /* With LTO early dwarf was really finished at compile-time, so make
30483 sure to adjust the phase after annotating the LTRANS CU DIE. */
30484 if (in_lto_p)
30486 early_dwarf_finished = true;
30487 return;
30490 /* Walk through the list of incomplete types again, trying once more to
30491 emit full debugging info for them. */
30492 retry_incomplete_types ();
30494 /* The point here is to flush out the limbo list so that it is empty
30495 and we don't need to stream it for LTO. */
30496 flush_limbo_die_list ();
30498 gen_scheduled_generic_parms_dies ();
30499 gen_remaining_tmpl_value_param_die_attribute ();
30501 /* Add DW_AT_linkage_name for all deferred DIEs. */
30502 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30504 tree decl = node->created_for;
30505 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30506 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30507 ended up in deferred_asm_name before we knew it was
30508 constant and never written to disk. */
30509 && DECL_ASSEMBLER_NAME (decl))
30511 add_linkage_attr (node->die, decl);
30512 move_linkage_attr (node->die);
30515 deferred_asm_name = NULL;
30517 if (flag_eliminate_unused_debug_types)
30518 prune_unused_types ();
30520 /* Generate separate COMDAT sections for type DIEs. */
30521 if (use_debug_types)
30523 break_out_comdat_types (comp_unit_die ());
30525 /* Each new type_unit DIE was added to the limbo die list when created.
30526 Since these have all been added to comdat_type_list, clear the
30527 limbo die list. */
30528 limbo_die_list = NULL;
30530 /* For each new comdat type unit, copy declarations for incomplete
30531 types to make the new unit self-contained (i.e., no direct
30532 references to the main compile unit). */
30533 for (comdat_type_node *ctnode = comdat_type_list;
30534 ctnode != NULL; ctnode = ctnode->next)
30535 copy_decls_for_unworthy_types (ctnode->root_die);
30536 copy_decls_for_unworthy_types (comp_unit_die ());
30538 /* In the process of copying declarations from one unit to another,
30539 we may have left some declarations behind that are no longer
30540 referenced. Prune them. */
30541 prune_unused_types ();
30544 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30545 with dw_val_class_decl_ref operand. */
30546 note_variable_value (comp_unit_die ());
30547 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30548 note_variable_value (node->die);
30549 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30550 ctnode = ctnode->next)
30551 note_variable_value (ctnode->root_die);
30552 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30553 note_variable_value (node->die);
30555 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30556 both the main_cu and all skeleton TUs. Making this call unconditional
30557 would end up either adding a second copy of the AT_pubnames attribute, or
30558 requiring a special case in add_top_level_skeleton_die_attrs. */
30559 if (!dwarf_split_debug_info)
30560 add_AT_pubnames (comp_unit_die ());
30562 /* The early debug phase is now finished. */
30563 early_dwarf_finished = true;
30565 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30566 if (!flag_generate_lto && !flag_generate_offload)
30567 return;
30569 /* Now as we are going to output for LTO initialize sections and labels
30570 to the LTO variants. We don't need a random-seed postfix as other
30571 LTO sections as linking the LTO debug sections into one in a partial
30572 link is fine. */
30573 init_sections_and_labels (true);
30575 /* The output below is modeled after dwarf2out_finish with all
30576 location related output removed and some LTO specific changes.
30577 Some refactoring might make both smaller and easier to match up. */
30579 /* Traverse the DIE's and add add sibling attributes to those DIE's
30580 that have children. */
30581 add_sibling_attributes (comp_unit_die ());
30582 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30583 add_sibling_attributes (node->die);
30584 for (comdat_type_node *ctnode = comdat_type_list;
30585 ctnode != NULL; ctnode = ctnode->next)
30586 add_sibling_attributes (ctnode->root_die);
30588 if (have_macinfo)
30589 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30590 macinfo_section_label);
30592 save_macinfo_strings ();
30594 /* Output all of the compilation units. We put the main one last so that
30595 the offsets are available to output_pubnames. */
30596 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30597 output_comp_unit (node->die, 0, NULL);
30599 hash_table<comdat_type_hasher> comdat_type_table (100);
30600 for (comdat_type_node *ctnode = comdat_type_list;
30601 ctnode != NULL; ctnode = ctnode->next)
30603 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30605 /* Don't output duplicate types. */
30606 if (*slot != HTAB_EMPTY_ENTRY)
30607 continue;
30609 /* Add a pointer to the line table for the main compilation unit
30610 so that the debugger can make sense of DW_AT_decl_file
30611 attributes. */
30612 if (debug_info_level >= DINFO_LEVEL_TERSE)
30613 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30614 (!dwarf_split_debug_info
30615 ? debug_line_section_label
30616 : debug_skeleton_line_section_label));
30618 output_comdat_type_unit (ctnode);
30619 *slot = ctnode;
30622 /* Stick a unique symbol to the main debuginfo section. */
30623 compute_comp_unit_symbol (comp_unit_die ());
30625 /* Output the main compilation unit. We always need it if only for
30626 the CU symbol. */
30627 output_comp_unit (comp_unit_die (), true, NULL);
30629 /* Output the abbreviation table. */
30630 if (vec_safe_length (abbrev_die_table) != 1)
30632 switch_to_section (debug_abbrev_section);
30633 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30634 output_abbrev_section ();
30637 /* Have to end the macro section. */
30638 if (have_macinfo)
30640 /* We have to save macinfo state if we need to output it again
30641 for the FAT part of the object. */
30642 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30643 if (flag_fat_lto_objects)
30644 macinfo_table = macinfo_table->copy ();
30646 switch_to_section (debug_macinfo_section);
30647 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30648 output_macinfo (debug_skeleton_line_section_label, true);
30649 dw2_asm_output_data (1, 0, "End compilation unit");
30651 /* Emit a skeleton debug_line section. */
30652 switch_to_section (debug_skeleton_line_section);
30653 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30654 output_line_info (true);
30656 if (flag_fat_lto_objects)
30658 vec_free (macinfo_table);
30659 macinfo_table = saved_macinfo_table;
30664 /* If we emitted any indirect strings, output the string table too. */
30665 if (debug_str_hash || skeleton_debug_str_hash)
30666 output_indirect_strings ();
30668 /* Switch back to the text section. */
30669 switch_to_section (text_section);
30672 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30673 within the same process. For use by toplev::finalize. */
30675 void
30676 dwarf2out_c_finalize (void)
30678 last_var_location_insn = NULL;
30679 cached_next_real_insn = NULL;
30680 used_rtx_array = NULL;
30681 incomplete_types = NULL;
30682 decl_scope_table = NULL;
30683 debug_info_section = NULL;
30684 debug_skeleton_info_section = NULL;
30685 debug_abbrev_section = NULL;
30686 debug_skeleton_abbrev_section = NULL;
30687 debug_aranges_section = NULL;
30688 debug_addr_section = NULL;
30689 debug_macinfo_section = NULL;
30690 debug_line_section = NULL;
30691 debug_skeleton_line_section = NULL;
30692 debug_loc_section = NULL;
30693 debug_pubnames_section = NULL;
30694 debug_pubtypes_section = NULL;
30695 debug_str_section = NULL;
30696 debug_line_str_section = NULL;
30697 debug_str_dwo_section = NULL;
30698 debug_str_offsets_section = NULL;
30699 debug_ranges_section = NULL;
30700 debug_frame_section = NULL;
30701 fde_vec = NULL;
30702 debug_str_hash = NULL;
30703 debug_line_str_hash = NULL;
30704 skeleton_debug_str_hash = NULL;
30705 dw2_string_counter = 0;
30706 have_multiple_function_sections = false;
30707 text_section_used = false;
30708 cold_text_section_used = false;
30709 cold_text_section = NULL;
30710 current_unit_personality = NULL;
30712 early_dwarf = false;
30713 early_dwarf_finished = false;
30715 next_die_offset = 0;
30716 single_comp_unit_die = NULL;
30717 comdat_type_list = NULL;
30718 limbo_die_list = NULL;
30719 file_table = NULL;
30720 decl_die_table = NULL;
30721 common_block_die_table = NULL;
30722 decl_loc_table = NULL;
30723 call_arg_locations = NULL;
30724 call_arg_loc_last = NULL;
30725 call_site_count = -1;
30726 tail_call_site_count = -1;
30727 cached_dw_loc_list_table = NULL;
30728 abbrev_die_table = NULL;
30729 delete dwarf_proc_stack_usage_map;
30730 dwarf_proc_stack_usage_map = NULL;
30731 line_info_label_num = 0;
30732 cur_line_info_table = NULL;
30733 text_section_line_info = NULL;
30734 cold_text_section_line_info = NULL;
30735 separate_line_info = NULL;
30736 info_section_emitted = false;
30737 pubname_table = NULL;
30738 pubtype_table = NULL;
30739 macinfo_table = NULL;
30740 ranges_table = NULL;
30741 ranges_by_label = NULL;
30742 rnglist_idx = 0;
30743 have_location_lists = false;
30744 loclabel_num = 0;
30745 poc_label_num = 0;
30746 last_emitted_file = NULL;
30747 label_num = 0;
30748 tmpl_value_parm_die_table = NULL;
30749 generic_type_instances = NULL;
30750 frame_pointer_fb_offset = 0;
30751 frame_pointer_fb_offset_valid = false;
30752 base_types.release ();
30753 XDELETEVEC (producer_string);
30754 producer_string = NULL;
30757 #include "gt-dwarf2out.h"