Daily bump.
[official-gcc.git] / gcc / dwarf2out.c
bloba92b0ce0b98c20d629f77c51ddfbe81144aa9a29
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "memmodel.h"
66 #include "tm_p.h"
67 #include "stringpool.h"
68 #include "insn-config.h"
69 #include "ira.h"
70 #include "cgraph.h"
71 #include "diagnostic.h"
72 #include "fold-const.h"
73 #include "stor-layout.h"
74 #include "varasm.h"
75 #include "version.h"
76 #include "flags.h"
77 #include "rtlhash.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tree-pretty-print.h"
86 #include "print-rtl.h"
87 #include "debug.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "lra.h"
91 #include "dumpfile.h"
92 #include "opts.h"
93 #include "tree-dfa.h"
94 #include "gdb/gdb-index.h"
95 #include "rtl-iter.h"
96 #include "stringpool.h"
97 #include "attribs.h"
98 #include "file-prefix-map.h" /* remap_debug_filename() */
100 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
101 int, bool);
102 static rtx_insn *last_var_location_insn;
103 static rtx_insn *cached_next_real_insn;
104 static void dwarf2out_decl (tree);
106 #ifndef XCOFF_DEBUGGING_INFO
107 #define XCOFF_DEBUGGING_INFO 0
108 #endif
110 #ifndef HAVE_XCOFF_DWARF_EXTRAS
111 #define HAVE_XCOFF_DWARF_EXTRAS 0
112 #endif
114 #ifdef VMS_DEBUGGING_INFO
115 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
117 /* Define this macro to be a nonzero value if the directory specifications
118 which are output in the debug info should end with a separator. */
119 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
120 /* Define this macro to evaluate to a nonzero value if GCC should refrain
121 from generating indirect strings in DWARF2 debug information, for instance
122 if your target is stuck with an old version of GDB that is unable to
123 process them properly or uses VMS Debug. */
124 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
125 #else
126 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
127 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
128 #endif
130 /* ??? Poison these here until it can be done generically. They've been
131 totally replaced in this file; make sure it stays that way. */
132 #undef DWARF2_UNWIND_INFO
133 #undef DWARF2_FRAME_INFO
134 #if (GCC_VERSION >= 3000)
135 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
136 #endif
138 /* The size of the target's pointer type. */
139 #ifndef PTR_SIZE
140 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
141 #endif
143 /* Array of RTXes referenced by the debugging information, which therefore
144 must be kept around forever. */
145 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
147 /* A pointer to the base of a list of incomplete types which might be
148 completed at some later time. incomplete_types_list needs to be a
149 vec<tree, va_gc> *because we want to tell the garbage collector about
150 it. */
151 static GTY(()) vec<tree, va_gc> *incomplete_types;
153 /* A pointer to the base of a table of references to declaration
154 scopes. This table is a display which tracks the nesting
155 of declaration scopes at the current scope and containing
156 scopes. This table is used to find the proper place to
157 define type declaration DIE's. */
158 static GTY(()) vec<tree, va_gc> *decl_scope_table;
160 /* Pointers to various DWARF2 sections. */
161 static GTY(()) section *debug_info_section;
162 static GTY(()) section *debug_skeleton_info_section;
163 static GTY(()) section *debug_abbrev_section;
164 static GTY(()) section *debug_skeleton_abbrev_section;
165 static GTY(()) section *debug_aranges_section;
166 static GTY(()) section *debug_addr_section;
167 static GTY(()) section *debug_macinfo_section;
168 static const char *debug_macinfo_section_name;
169 static unsigned macinfo_label_base = 1;
170 static GTY(()) section *debug_line_section;
171 static GTY(()) section *debug_skeleton_line_section;
172 static GTY(()) section *debug_loc_section;
173 static GTY(()) section *debug_pubnames_section;
174 static GTY(()) section *debug_pubtypes_section;
175 static GTY(()) section *debug_str_section;
176 static GTY(()) section *debug_line_str_section;
177 static GTY(()) section *debug_str_dwo_section;
178 static GTY(()) section *debug_str_offsets_section;
179 static GTY(()) section *debug_ranges_section;
180 static GTY(()) section *debug_frame_section;
182 /* Maximum size (in bytes) of an artificially generated label. */
183 #define MAX_ARTIFICIAL_LABEL_BYTES 40
185 /* According to the (draft) DWARF 3 specification, the initial length
186 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
187 bytes are 0xffffffff, followed by the length stored in the next 8
188 bytes.
190 However, the SGI/MIPS ABI uses an initial length which is equal to
191 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
193 #ifndef DWARF_INITIAL_LENGTH_SIZE
194 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
195 #endif
197 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
198 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
199 #endif
201 /* Round SIZE up to the nearest BOUNDARY. */
202 #define DWARF_ROUND(SIZE,BOUNDARY) \
203 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
205 /* CIE identifier. */
206 #if HOST_BITS_PER_WIDE_INT >= 64
207 #define DWARF_CIE_ID \
208 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
209 #else
210 #define DWARF_CIE_ID DW_CIE_ID
211 #endif
214 /* A vector for a table that contains frame description
215 information for each routine. */
216 #define NOT_INDEXED (-1U)
217 #define NO_INDEX_ASSIGNED (-2U)
219 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
221 struct GTY((for_user)) indirect_string_node {
222 const char *str;
223 unsigned int refcount;
224 enum dwarf_form form;
225 char *label;
226 unsigned int index;
229 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
231 typedef const char *compare_type;
233 static hashval_t hash (indirect_string_node *);
234 static bool equal (indirect_string_node *, const char *);
237 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
239 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
241 /* With split_debug_info, both the comp_dir and dwo_name go in the
242 main object file, rather than the dwo, similar to the force_direct
243 parameter elsewhere but with additional complications:
245 1) The string is needed in both the main object file and the dwo.
246 That is, the comp_dir and dwo_name will appear in both places.
248 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
249 DW_FORM_line_strp or DW_FORM_GNU_str_index.
251 3) GCC chooses the form to use late, depending on the size and
252 reference count.
254 Rather than forcing the all debug string handling functions and
255 callers to deal with these complications, simply use a separate,
256 special-cased string table for any attribute that should go in the
257 main object file. This limits the complexity to just the places
258 that need it. */
260 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
262 static GTY(()) int dw2_string_counter;
264 /* True if the compilation unit places functions in more than one section. */
265 static GTY(()) bool have_multiple_function_sections = false;
267 /* Whether the default text and cold text sections have been used at all. */
268 static GTY(()) bool text_section_used = false;
269 static GTY(()) bool cold_text_section_used = false;
271 /* The default cold text section. */
272 static GTY(()) section *cold_text_section;
274 /* The DIE for C++14 'auto' in a function return type. */
275 static GTY(()) dw_die_ref auto_die;
277 /* The DIE for C++14 'decltype(auto)' in a function return type. */
278 static GTY(()) dw_die_ref decltype_auto_die;
280 /* Forward declarations for functions defined in this file. */
282 static void output_call_frame_info (int);
283 static void dwarf2out_note_section_used (void);
285 /* Personality decl of current unit. Used only when assembler does not support
286 personality CFI. */
287 static GTY(()) rtx current_unit_personality;
289 /* Whether an eh_frame section is required. */
290 static GTY(()) bool do_eh_frame = false;
292 /* .debug_rnglists next index. */
293 static unsigned int rnglist_idx;
295 /* Data and reference forms for relocatable data. */
296 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
297 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
299 #ifndef DEBUG_FRAME_SECTION
300 #define DEBUG_FRAME_SECTION ".debug_frame"
301 #endif
303 #ifndef FUNC_BEGIN_LABEL
304 #define FUNC_BEGIN_LABEL "LFB"
305 #endif
307 #ifndef FUNC_END_LABEL
308 #define FUNC_END_LABEL "LFE"
309 #endif
311 #ifndef PROLOGUE_END_LABEL
312 #define PROLOGUE_END_LABEL "LPE"
313 #endif
315 #ifndef EPILOGUE_BEGIN_LABEL
316 #define EPILOGUE_BEGIN_LABEL "LEB"
317 #endif
319 #ifndef FRAME_BEGIN_LABEL
320 #define FRAME_BEGIN_LABEL "Lframe"
321 #endif
322 #define CIE_AFTER_SIZE_LABEL "LSCIE"
323 #define CIE_END_LABEL "LECIE"
324 #define FDE_LABEL "LSFDE"
325 #define FDE_AFTER_SIZE_LABEL "LASFDE"
326 #define FDE_END_LABEL "LEFDE"
327 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
328 #define LINE_NUMBER_END_LABEL "LELT"
329 #define LN_PROLOG_AS_LABEL "LASLTP"
330 #define LN_PROLOG_END_LABEL "LELTP"
331 #define DIE_LABEL_PREFIX "DW"
333 /* Match the base name of a file to the base name of a compilation unit. */
335 static int
336 matches_main_base (const char *path)
338 /* Cache the last query. */
339 static const char *last_path = NULL;
340 static int last_match = 0;
341 if (path != last_path)
343 const char *base;
344 int length = base_of_path (path, &base);
345 last_path = path;
346 last_match = (length == main_input_baselength
347 && memcmp (base, main_input_basename, length) == 0);
349 return last_match;
352 #ifdef DEBUG_DEBUG_STRUCT
354 static int
355 dump_struct_debug (tree type, enum debug_info_usage usage,
356 enum debug_struct_file criterion, int generic,
357 int matches, int result)
359 /* Find the type name. */
360 tree type_decl = TYPE_STUB_DECL (type);
361 tree t = type_decl;
362 const char *name = 0;
363 if (TREE_CODE (t) == TYPE_DECL)
364 t = DECL_NAME (t);
365 if (t)
366 name = IDENTIFIER_POINTER (t);
368 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
369 criterion,
370 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
371 matches ? "bas" : "hdr",
372 generic ? "gen" : "ord",
373 usage == DINFO_USAGE_DFN ? ";" :
374 usage == DINFO_USAGE_DIR_USE ? "." : "*",
375 result,
376 (void*) type_decl, name);
377 return result;
379 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
380 dump_struct_debug (type, usage, criterion, generic, matches, result)
382 #else
384 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
385 (result)
387 #endif
389 /* Get the number of HOST_WIDE_INTs needed to represent the precision
390 of the number. Some constants have a large uniform precision, so
391 we get the precision needed for the actual value of the number. */
393 static unsigned int
394 get_full_len (const wide_int &op)
396 int prec = wi::min_precision (op, UNSIGNED);
397 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
398 / HOST_BITS_PER_WIDE_INT);
401 static bool
402 should_emit_struct_debug (tree type, enum debug_info_usage usage)
404 enum debug_struct_file criterion;
405 tree type_decl;
406 bool generic = lang_hooks.types.generic_p (type);
408 if (generic)
409 criterion = debug_struct_generic[usage];
410 else
411 criterion = debug_struct_ordinary[usage];
413 if (criterion == DINFO_STRUCT_FILE_NONE)
414 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
415 if (criterion == DINFO_STRUCT_FILE_ANY)
416 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
418 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
420 if (type_decl != NULL)
422 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
423 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
425 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
426 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
429 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
432 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
433 switch to the data section instead, and write out a synthetic start label
434 for collect2 the first time around. */
436 static void
437 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
439 if (eh_frame_section == 0)
441 int flags;
443 if (EH_TABLES_CAN_BE_READ_ONLY)
445 int fde_encoding;
446 int per_encoding;
447 int lsda_encoding;
449 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
450 /*global=*/0);
451 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
452 /*global=*/1);
453 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
454 /*global=*/0);
455 flags = ((! flag_pic
456 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
457 && (fde_encoding & 0x70) != DW_EH_PE_aligned
458 && (per_encoding & 0x70) != DW_EH_PE_absptr
459 && (per_encoding & 0x70) != DW_EH_PE_aligned
460 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
461 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
462 ? 0 : SECTION_WRITE);
464 else
465 flags = SECTION_WRITE;
467 #ifdef EH_FRAME_SECTION_NAME
468 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
469 #else
470 eh_frame_section = ((flags == SECTION_WRITE)
471 ? data_section : readonly_data_section);
472 #endif /* EH_FRAME_SECTION_NAME */
475 switch_to_section (eh_frame_section);
477 #ifdef EH_FRAME_THROUGH_COLLECT2
478 /* We have no special eh_frame section. Emit special labels to guide
479 collect2. */
480 if (!back)
482 tree label = get_file_function_name ("F");
483 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
484 targetm.asm_out.globalize_label (asm_out_file,
485 IDENTIFIER_POINTER (label));
486 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
488 #endif
491 /* Switch [BACK] to the eh or debug frame table section, depending on
492 FOR_EH. */
494 static void
495 switch_to_frame_table_section (int for_eh, bool back)
497 if (for_eh)
498 switch_to_eh_frame_section (back);
499 else
501 if (!debug_frame_section)
502 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
503 SECTION_DEBUG, NULL);
504 switch_to_section (debug_frame_section);
508 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
510 enum dw_cfi_oprnd_type
511 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
513 switch (cfi)
515 case DW_CFA_nop:
516 case DW_CFA_GNU_window_save:
517 case DW_CFA_remember_state:
518 case DW_CFA_restore_state:
519 return dw_cfi_oprnd_unused;
521 case DW_CFA_set_loc:
522 case DW_CFA_advance_loc1:
523 case DW_CFA_advance_loc2:
524 case DW_CFA_advance_loc4:
525 case DW_CFA_MIPS_advance_loc8:
526 return dw_cfi_oprnd_addr;
528 case DW_CFA_offset:
529 case DW_CFA_offset_extended:
530 case DW_CFA_def_cfa:
531 case DW_CFA_offset_extended_sf:
532 case DW_CFA_def_cfa_sf:
533 case DW_CFA_restore:
534 case DW_CFA_restore_extended:
535 case DW_CFA_undefined:
536 case DW_CFA_same_value:
537 case DW_CFA_def_cfa_register:
538 case DW_CFA_register:
539 case DW_CFA_expression:
540 case DW_CFA_val_expression:
541 return dw_cfi_oprnd_reg_num;
543 case DW_CFA_def_cfa_offset:
544 case DW_CFA_GNU_args_size:
545 case DW_CFA_def_cfa_offset_sf:
546 return dw_cfi_oprnd_offset;
548 case DW_CFA_def_cfa_expression:
549 return dw_cfi_oprnd_loc;
551 default:
552 gcc_unreachable ();
556 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
558 enum dw_cfi_oprnd_type
559 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
561 switch (cfi)
563 case DW_CFA_def_cfa:
564 case DW_CFA_def_cfa_sf:
565 case DW_CFA_offset:
566 case DW_CFA_offset_extended_sf:
567 case DW_CFA_offset_extended:
568 return dw_cfi_oprnd_offset;
570 case DW_CFA_register:
571 return dw_cfi_oprnd_reg_num;
573 case DW_CFA_expression:
574 case DW_CFA_val_expression:
575 return dw_cfi_oprnd_loc;
577 case DW_CFA_def_cfa_expression:
578 return dw_cfi_oprnd_cfa_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (!XCOFF_DEBUGGING_INFO || for_eh)
604 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
605 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
606 " indicating 64-bit DWARF extension");
607 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
608 "FDE Length");
610 ASM_OUTPUT_LABEL (asm_out_file, l1);
612 if (for_eh)
613 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
614 else
615 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
616 debug_frame_section, "FDE CIE offset");
618 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
619 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
621 if (for_eh)
623 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
624 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
625 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
626 "FDE initial location");
627 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
628 end, begin, "FDE address range");
630 else
632 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
633 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
636 if (augmentation[0])
638 if (any_lsda_needed)
640 int size = size_of_encoded_value (lsda_encoding);
642 if (lsda_encoding == DW_EH_PE_aligned)
644 int offset = ( 4 /* Length */
645 + 4 /* CIE offset */
646 + 2 * size_of_encoded_value (fde_encoding)
647 + 1 /* Augmentation size */ );
648 int pad = -offset & (PTR_SIZE - 1);
650 size += pad;
651 gcc_assert (size_of_uleb128 (size) == 1);
654 dw2_asm_output_data_uleb128 (size, "Augmentation size");
656 if (fde->uses_eh_lsda)
658 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
659 fde->funcdef_number);
660 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
661 gen_rtx_SYMBOL_REF (Pmode, l1),
662 false,
663 "Language Specific Data Area");
665 else
667 if (lsda_encoding == DW_EH_PE_aligned)
668 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
669 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
670 "Language Specific Data Area (none)");
673 else
674 dw2_asm_output_data_uleb128 (0, "Augmentation size");
677 /* Loop through the Call Frame Instructions associated with this FDE. */
678 fde->dw_fde_current_label = begin;
680 size_t from, until, i;
682 from = 0;
683 until = vec_safe_length (fde->dw_fde_cfi);
685 if (fde->dw_fde_second_begin == NULL)
687 else if (!second)
688 until = fde->dw_fde_switch_cfi_index;
689 else
690 from = fde->dw_fde_switch_cfi_index;
692 for (i = from; i < until; i++)
693 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
696 /* If we are to emit a ref/link from function bodies to their frame tables,
697 do it now. This is typically performed to make sure that tables
698 associated with functions are dragged with them and not discarded in
699 garbage collecting links. We need to do this on a per function basis to
700 cope with -ffunction-sections. */
702 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
703 /* Switch to the function section, emit the ref to the tables, and
704 switch *back* into the table section. */
705 switch_to_section (function_section (fde->decl));
706 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
707 switch_to_frame_table_section (for_eh, true);
708 #endif
710 /* Pad the FDE out to an address sized boundary. */
711 ASM_OUTPUT_ALIGN (asm_out_file,
712 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
713 ASM_OUTPUT_LABEL (asm_out_file, l2);
715 j += 2;
718 /* Return true if frame description entry FDE is needed for EH. */
720 static bool
721 fde_needed_for_eh_p (dw_fde_ref fde)
723 if (flag_asynchronous_unwind_tables)
724 return true;
726 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
727 return true;
729 if (fde->uses_eh_lsda)
730 return true;
732 /* If exceptions are enabled, we have collected nothrow info. */
733 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
734 return false;
736 return true;
739 /* Output the call frame information used to record information
740 that relates to calculating the frame pointer, and records the
741 location of saved registers. */
743 static void
744 output_call_frame_info (int for_eh)
746 unsigned int i;
747 dw_fde_ref fde;
748 dw_cfi_ref cfi;
749 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
750 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
751 bool any_lsda_needed = false;
752 char augmentation[6];
753 int augmentation_size;
754 int fde_encoding = DW_EH_PE_absptr;
755 int per_encoding = DW_EH_PE_absptr;
756 int lsda_encoding = DW_EH_PE_absptr;
757 int return_reg;
758 rtx personality = NULL;
759 int dw_cie_version;
761 /* Don't emit a CIE if there won't be any FDEs. */
762 if (!fde_vec)
763 return;
765 /* Nothing to do if the assembler's doing it all. */
766 if (dwarf2out_do_cfi_asm ())
767 return;
769 /* If we don't have any functions we'll want to unwind out of, don't emit
770 any EH unwind information. If we make FDEs linkonce, we may have to
771 emit an empty label for an FDE that wouldn't otherwise be emitted. We
772 want to avoid having an FDE kept around when the function it refers to
773 is discarded. Example where this matters: a primary function template
774 in C++ requires EH information, an explicit specialization doesn't. */
775 if (for_eh)
777 bool any_eh_needed = false;
779 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
781 if (fde->uses_eh_lsda)
782 any_eh_needed = any_lsda_needed = true;
783 else if (fde_needed_for_eh_p (fde))
784 any_eh_needed = true;
785 else if (TARGET_USES_WEAK_UNWIND_INFO)
786 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
789 if (!any_eh_needed)
790 return;
793 /* We're going to be generating comments, so turn on app. */
794 if (flag_debug_asm)
795 app_enable ();
797 /* Switch to the proper frame section, first time. */
798 switch_to_frame_table_section (for_eh, false);
800 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
801 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
803 /* Output the CIE. */
804 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
805 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
806 if (!XCOFF_DEBUGGING_INFO || for_eh)
808 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
809 dw2_asm_output_data (4, 0xffffffff,
810 "Initial length escape value indicating 64-bit DWARF extension");
811 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
812 "Length of Common Information Entry");
814 ASM_OUTPUT_LABEL (asm_out_file, l1);
816 /* Now that the CIE pointer is PC-relative for EH,
817 use 0 to identify the CIE. */
818 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
819 (for_eh ? 0 : DWARF_CIE_ID),
820 "CIE Identifier Tag");
822 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
823 use CIE version 1, unless that would produce incorrect results
824 due to overflowing the return register column. */
825 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
826 dw_cie_version = 1;
827 if (return_reg >= 256 || dwarf_version > 2)
828 dw_cie_version = 3;
829 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
831 augmentation[0] = 0;
832 augmentation_size = 0;
834 personality = current_unit_personality;
835 if (for_eh)
837 char *p;
839 /* Augmentation:
840 z Indicates that a uleb128 is present to size the
841 augmentation section.
842 L Indicates the encoding (and thus presence) of
843 an LSDA pointer in the FDE augmentation.
844 R Indicates a non-default pointer encoding for
845 FDE code pointers.
846 P Indicates the presence of an encoding + language
847 personality routine in the CIE augmentation. */
849 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
850 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
851 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
853 p = augmentation + 1;
854 if (personality)
856 *p++ = 'P';
857 augmentation_size += 1 + size_of_encoded_value (per_encoding);
858 assemble_external_libcall (personality);
860 if (any_lsda_needed)
862 *p++ = 'L';
863 augmentation_size += 1;
865 if (fde_encoding != DW_EH_PE_absptr)
867 *p++ = 'R';
868 augmentation_size += 1;
870 if (p > augmentation + 1)
872 augmentation[0] = 'z';
873 *p = '\0';
876 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
877 if (personality && per_encoding == DW_EH_PE_aligned)
879 int offset = ( 4 /* Length */
880 + 4 /* CIE Id */
881 + 1 /* CIE version */
882 + strlen (augmentation) + 1 /* Augmentation */
883 + size_of_uleb128 (1) /* Code alignment */
884 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
885 + 1 /* RA column */
886 + 1 /* Augmentation size */
887 + 1 /* Personality encoding */ );
888 int pad = -offset & (PTR_SIZE - 1);
890 augmentation_size += pad;
892 /* Augmentations should be small, so there's scarce need to
893 iterate for a solution. Die if we exceed one uleb128 byte. */
894 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
898 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
899 if (dw_cie_version >= 4)
901 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
902 dw2_asm_output_data (1, 0, "CIE Segment Size");
904 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
905 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
906 "CIE Data Alignment Factor");
908 if (dw_cie_version == 1)
909 dw2_asm_output_data (1, return_reg, "CIE RA Column");
910 else
911 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
913 if (augmentation[0])
915 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
916 if (personality)
918 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
919 eh_data_format_name (per_encoding));
920 dw2_asm_output_encoded_addr_rtx (per_encoding,
921 personality,
922 true, NULL);
925 if (any_lsda_needed)
926 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
927 eh_data_format_name (lsda_encoding));
929 if (fde_encoding != DW_EH_PE_absptr)
930 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
931 eh_data_format_name (fde_encoding));
934 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
935 output_cfi (cfi, NULL, for_eh);
937 /* Pad the CIE out to an address sized boundary. */
938 ASM_OUTPUT_ALIGN (asm_out_file,
939 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
940 ASM_OUTPUT_LABEL (asm_out_file, l2);
942 /* Loop through all of the FDE's. */
943 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
945 unsigned int k;
947 /* Don't emit EH unwind info for leaf functions that don't need it. */
948 if (for_eh && !fde_needed_for_eh_p (fde))
949 continue;
951 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
952 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
953 augmentation, any_lsda_needed, lsda_encoding);
956 if (for_eh && targetm.terminate_dw2_eh_frame_info)
957 dw2_asm_output_data (4, 0, "End of Table");
959 /* Turn off app to make assembly quicker. */
960 if (flag_debug_asm)
961 app_disable ();
964 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
966 static void
967 dwarf2out_do_cfi_startproc (bool second)
969 int enc;
970 rtx ref;
972 fprintf (asm_out_file, "\t.cfi_startproc\n");
974 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
975 eh unwinders. */
976 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
977 return;
979 rtx personality = get_personality_function (current_function_decl);
981 if (personality)
983 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
984 ref = personality;
986 /* ??? The GAS support isn't entirely consistent. We have to
987 handle indirect support ourselves, but PC-relative is done
988 in the assembler. Further, the assembler can't handle any
989 of the weirder relocation types. */
990 if (enc & DW_EH_PE_indirect)
991 ref = dw2_force_const_mem (ref, true);
993 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
994 output_addr_const (asm_out_file, ref);
995 fputc ('\n', asm_out_file);
998 if (crtl->uses_eh_lsda)
1000 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1002 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1003 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1004 current_function_funcdef_no);
1005 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1006 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1008 if (enc & DW_EH_PE_indirect)
1009 ref = dw2_force_const_mem (ref, true);
1011 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1012 output_addr_const (asm_out_file, ref);
1013 fputc ('\n', asm_out_file);
1017 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1018 this allocation may be done before pass_final. */
1020 dw_fde_ref
1021 dwarf2out_alloc_current_fde (void)
1023 dw_fde_ref fde;
1025 fde = ggc_cleared_alloc<dw_fde_node> ();
1026 fde->decl = current_function_decl;
1027 fde->funcdef_number = current_function_funcdef_no;
1028 fde->fde_index = vec_safe_length (fde_vec);
1029 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1030 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1031 fde->nothrow = crtl->nothrow;
1032 fde->drap_reg = INVALID_REGNUM;
1033 fde->vdrap_reg = INVALID_REGNUM;
1035 /* Record the FDE associated with this function. */
1036 cfun->fde = fde;
1037 vec_safe_push (fde_vec, fde);
1039 return fde;
1042 /* Output a marker (i.e. a label) for the beginning of a function, before
1043 the prologue. */
1045 void
1046 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1047 unsigned int column ATTRIBUTE_UNUSED,
1048 const char *file ATTRIBUTE_UNUSED)
1050 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1051 char * dup_label;
1052 dw_fde_ref fde;
1053 section *fnsec;
1054 bool do_frame;
1056 current_function_func_begin_label = NULL;
1058 do_frame = dwarf2out_do_frame ();
1060 /* ??? current_function_func_begin_label is also used by except.c for
1061 call-site information. We must emit this label if it might be used. */
1062 if (!do_frame
1063 && (!flag_exceptions
1064 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1065 return;
1067 fnsec = function_section (current_function_decl);
1068 switch_to_section (fnsec);
1069 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1070 current_function_funcdef_no);
1071 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1072 current_function_funcdef_no);
1073 dup_label = xstrdup (label);
1074 current_function_func_begin_label = dup_label;
1076 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1077 if (!do_frame)
1078 return;
1080 /* Unlike the debug version, the EH version of frame unwind info is a per-
1081 function setting so we need to record whether we need it for the unit. */
1082 do_eh_frame |= dwarf2out_do_eh_frame ();
1084 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1085 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1086 would include pass_dwarf2_frame. If we've not created the FDE yet,
1087 do so now. */
1088 fde = cfun->fde;
1089 if (fde == NULL)
1090 fde = dwarf2out_alloc_current_fde ();
1092 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1093 fde->dw_fde_begin = dup_label;
1094 fde->dw_fde_current_label = dup_label;
1095 fde->in_std_section = (fnsec == text_section
1096 || (cold_text_section && fnsec == cold_text_section));
1098 /* We only want to output line number information for the genuine dwarf2
1099 prologue case, not the eh frame case. */
1100 #ifdef DWARF2_DEBUGGING_INFO
1101 if (file)
1102 dwarf2out_source_line (line, column, file, 0, true);
1103 #endif
1105 if (dwarf2out_do_cfi_asm ())
1106 dwarf2out_do_cfi_startproc (false);
1107 else
1109 rtx personality = get_personality_function (current_function_decl);
1110 if (!current_unit_personality)
1111 current_unit_personality = personality;
1113 /* We cannot keep a current personality per function as without CFI
1114 asm, at the point where we emit the CFI data, there is no current
1115 function anymore. */
1116 if (personality && current_unit_personality != personality)
1117 sorry ("multiple EH personalities are supported only with assemblers "
1118 "supporting .cfi_personality directive");
1122 /* Output a marker (i.e. a label) for the end of the generated code
1123 for a function prologue. This gets called *after* the prologue code has
1124 been generated. */
1126 void
1127 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1128 const char *file ATTRIBUTE_UNUSED)
1130 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1132 /* Output a label to mark the endpoint of the code generated for this
1133 function. */
1134 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1135 current_function_funcdef_no);
1136 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1137 current_function_funcdef_no);
1138 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1141 /* Output a marker (i.e. a label) for the beginning of the generated code
1142 for a function epilogue. This gets called *before* the prologue code has
1143 been generated. */
1145 void
1146 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1147 const char *file ATTRIBUTE_UNUSED)
1149 dw_fde_ref fde = cfun->fde;
1150 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1152 if (fde->dw_fde_vms_begin_epilogue)
1153 return;
1155 /* Output a label to mark the endpoint of the code generated for this
1156 function. */
1157 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1158 current_function_funcdef_no);
1159 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1160 current_function_funcdef_no);
1161 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1164 /* Output a marker (i.e. a label) for the absolute end of the generated code
1165 for a function definition. This gets called *after* the epilogue code has
1166 been generated. */
1168 void
1169 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1170 const char *file ATTRIBUTE_UNUSED)
1172 dw_fde_ref fde;
1173 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1175 last_var_location_insn = NULL;
1176 cached_next_real_insn = NULL;
1178 if (dwarf2out_do_cfi_asm ())
1179 fprintf (asm_out_file, "\t.cfi_endproc\n");
1181 /* Output a label to mark the endpoint of the code generated for this
1182 function. */
1183 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1184 current_function_funcdef_no);
1185 ASM_OUTPUT_LABEL (asm_out_file, label);
1186 fde = cfun->fde;
1187 gcc_assert (fde != NULL);
1188 if (fde->dw_fde_second_begin == NULL)
1189 fde->dw_fde_end = xstrdup (label);
1192 void
1193 dwarf2out_frame_finish (void)
1195 /* Output call frame information. */
1196 if (targetm.debug_unwind_info () == UI_DWARF2)
1197 output_call_frame_info (0);
1199 /* Output another copy for the unwinder. */
1200 if (do_eh_frame)
1201 output_call_frame_info (1);
1204 /* Note that the current function section is being used for code. */
1206 static void
1207 dwarf2out_note_section_used (void)
1209 section *sec = current_function_section ();
1210 if (sec == text_section)
1211 text_section_used = true;
1212 else if (sec == cold_text_section)
1213 cold_text_section_used = true;
1216 static void var_location_switch_text_section (void);
1217 static void set_cur_line_info_table (section *);
1219 void
1220 dwarf2out_switch_text_section (void)
1222 section *sect;
1223 dw_fde_ref fde = cfun->fde;
1225 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1227 if (!in_cold_section_p)
1229 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1230 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1231 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1233 else
1235 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1236 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1237 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1239 have_multiple_function_sections = true;
1241 /* There is no need to mark used sections when not debugging. */
1242 if (cold_text_section != NULL)
1243 dwarf2out_note_section_used ();
1245 if (dwarf2out_do_cfi_asm ())
1246 fprintf (asm_out_file, "\t.cfi_endproc\n");
1248 /* Now do the real section switch. */
1249 sect = current_function_section ();
1250 switch_to_section (sect);
1252 fde->second_in_std_section
1253 = (sect == text_section
1254 || (cold_text_section && sect == cold_text_section));
1256 if (dwarf2out_do_cfi_asm ())
1257 dwarf2out_do_cfi_startproc (true);
1259 var_location_switch_text_section ();
1261 if (cold_text_section != NULL)
1262 set_cur_line_info_table (sect);
1265 /* And now, the subset of the debugging information support code necessary
1266 for emitting location expressions. */
1268 /* Data about a single source file. */
1269 struct GTY((for_user)) dwarf_file_data {
1270 const char * filename;
1271 int emitted_number;
1274 /* Describe an entry into the .debug_addr section. */
1276 enum ate_kind {
1277 ate_kind_rtx,
1278 ate_kind_rtx_dtprel,
1279 ate_kind_label
1282 struct GTY((for_user)) addr_table_entry {
1283 enum ate_kind kind;
1284 unsigned int refcount;
1285 unsigned int index;
1286 union addr_table_entry_struct_union
1288 rtx GTY ((tag ("0"))) rtl;
1289 char * GTY ((tag ("1"))) label;
1291 GTY ((desc ("%1.kind"))) addr;
1294 typedef unsigned int var_loc_view;
1296 /* Location lists are ranges + location descriptions for that range,
1297 so you can track variables that are in different places over
1298 their entire life. */
1299 typedef struct GTY(()) dw_loc_list_struct {
1300 dw_loc_list_ref dw_loc_next;
1301 const char *begin; /* Label and addr_entry for start of range */
1302 addr_table_entry *begin_entry;
1303 const char *end; /* Label for end of range */
1304 char *ll_symbol; /* Label for beginning of location list.
1305 Only on head of list. */
1306 char *vl_symbol; /* Label for beginning of view list. Ditto. */
1307 const char *section; /* Section this loclist is relative to */
1308 dw_loc_descr_ref expr;
1309 var_loc_view vbegin, vend;
1310 hashval_t hash;
1311 /* True if all addresses in this and subsequent lists are known to be
1312 resolved. */
1313 bool resolved_addr;
1314 /* True if this list has been replaced by dw_loc_next. */
1315 bool replaced;
1316 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1317 section. */
1318 unsigned char emitted : 1;
1319 /* True if hash field is index rather than hash value. */
1320 unsigned char num_assigned : 1;
1321 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1322 unsigned char offset_emitted : 1;
1323 /* True if note_variable_value_in_expr has been called on it. */
1324 unsigned char noted_variable_value : 1;
1325 /* True if the range should be emitted even if begin and end
1326 are the same. */
1327 bool force;
1328 } dw_loc_list_node;
1330 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1331 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1333 /* Convert a DWARF stack opcode into its string name. */
1335 static const char *
1336 dwarf_stack_op_name (unsigned int op)
1338 const char *name = get_DW_OP_name (op);
1340 if (name != NULL)
1341 return name;
1343 return "OP_<unknown>";
1346 /* Return TRUE iff we're to output location view lists as a separate
1347 attribute next to the location lists, as an extension compatible
1348 with DWARF 2 and above. */
1350 static inline bool
1351 dwarf2out_locviews_in_attribute ()
1353 return debug_variable_location_views == 1;
1356 /* Return TRUE iff we're to output location view lists as part of the
1357 location lists, as proposed for standardization after DWARF 5. */
1359 static inline bool
1360 dwarf2out_locviews_in_loclist ()
1362 #ifndef DW_LLE_view_pair
1363 return false;
1364 #else
1365 return debug_variable_location_views == -1;
1366 #endif
1369 /* Return a pointer to a newly allocated location description. Location
1370 descriptions are simple expression terms that can be strung
1371 together to form more complicated location (address) descriptions. */
1373 static inline dw_loc_descr_ref
1374 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1375 unsigned HOST_WIDE_INT oprnd2)
1377 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1379 descr->dw_loc_opc = op;
1380 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1381 descr->dw_loc_oprnd1.val_entry = NULL;
1382 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1383 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1384 descr->dw_loc_oprnd2.val_entry = NULL;
1385 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1387 return descr;
1390 /* Add a location description term to a location description expression. */
1392 static inline void
1393 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1395 dw_loc_descr_ref *d;
1397 /* Find the end of the chain. */
1398 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1401 *d = descr;
1404 /* Compare two location operands for exact equality. */
1406 static bool
1407 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1409 if (a->val_class != b->val_class)
1410 return false;
1411 switch (a->val_class)
1413 case dw_val_class_none:
1414 return true;
1415 case dw_val_class_addr:
1416 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1418 case dw_val_class_offset:
1419 case dw_val_class_unsigned_const:
1420 case dw_val_class_const:
1421 case dw_val_class_unsigned_const_implicit:
1422 case dw_val_class_const_implicit:
1423 case dw_val_class_range_list:
1424 /* These are all HOST_WIDE_INT, signed or unsigned. */
1425 return a->v.val_unsigned == b->v.val_unsigned;
1427 case dw_val_class_loc:
1428 return a->v.val_loc == b->v.val_loc;
1429 case dw_val_class_loc_list:
1430 return a->v.val_loc_list == b->v.val_loc_list;
1431 case dw_val_class_view_list:
1432 return a->v.val_view_list == b->v.val_view_list;
1433 case dw_val_class_die_ref:
1434 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1435 case dw_val_class_fde_ref:
1436 return a->v.val_fde_index == b->v.val_fde_index;
1437 case dw_val_class_symview:
1438 return strcmp (a->v.val_symbolic_view, b->v.val_symbolic_view) == 0;
1439 case dw_val_class_lbl_id:
1440 case dw_val_class_lineptr:
1441 case dw_val_class_macptr:
1442 case dw_val_class_loclistsptr:
1443 case dw_val_class_high_pc:
1444 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1445 case dw_val_class_str:
1446 return a->v.val_str == b->v.val_str;
1447 case dw_val_class_flag:
1448 return a->v.val_flag == b->v.val_flag;
1449 case dw_val_class_file:
1450 case dw_val_class_file_implicit:
1451 return a->v.val_file == b->v.val_file;
1452 case dw_val_class_decl_ref:
1453 return a->v.val_decl_ref == b->v.val_decl_ref;
1455 case dw_val_class_const_double:
1456 return (a->v.val_double.high == b->v.val_double.high
1457 && a->v.val_double.low == b->v.val_double.low);
1459 case dw_val_class_wide_int:
1460 return *a->v.val_wide == *b->v.val_wide;
1462 case dw_val_class_vec:
1464 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1465 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1467 return (a_len == b_len
1468 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1471 case dw_val_class_data8:
1472 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1474 case dw_val_class_vms_delta:
1475 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1476 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1478 case dw_val_class_discr_value:
1479 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1480 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1481 case dw_val_class_discr_list:
1482 /* It makes no sense comparing two discriminant value lists. */
1483 return false;
1485 gcc_unreachable ();
1488 /* Compare two location atoms for exact equality. */
1490 static bool
1491 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1493 if (a->dw_loc_opc != b->dw_loc_opc)
1494 return false;
1496 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1497 address size, but since we always allocate cleared storage it
1498 should be zero for other types of locations. */
1499 if (a->dtprel != b->dtprel)
1500 return false;
1502 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1503 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1506 /* Compare two complete location expressions for exact equality. */
1508 bool
1509 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1511 while (1)
1513 if (a == b)
1514 return true;
1515 if (a == NULL || b == NULL)
1516 return false;
1517 if (!loc_descr_equal_p_1 (a, b))
1518 return false;
1520 a = a->dw_loc_next;
1521 b = b->dw_loc_next;
1526 /* Add a constant POLY_OFFSET to a location expression. */
1528 static void
1529 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1531 dw_loc_descr_ref loc;
1532 HOST_WIDE_INT *p;
1534 gcc_assert (*list_head != NULL);
1536 if (known_eq (poly_offset, 0))
1537 return;
1539 /* Find the end of the chain. */
1540 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1543 HOST_WIDE_INT offset;
1544 if (!poly_offset.is_constant (&offset))
1546 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1547 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1548 return;
1551 p = NULL;
1552 if (loc->dw_loc_opc == DW_OP_fbreg
1553 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1554 p = &loc->dw_loc_oprnd1.v.val_int;
1555 else if (loc->dw_loc_opc == DW_OP_bregx)
1556 p = &loc->dw_loc_oprnd2.v.val_int;
1558 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1559 offset. Don't optimize if an signed integer overflow would happen. */
1560 if (p != NULL
1561 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1562 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1563 *p += offset;
1565 else if (offset > 0)
1566 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1568 else
1570 loc->dw_loc_next
1571 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1572 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1576 /* Return a pointer to a newly allocated location description for
1577 REG and OFFSET. */
1579 static inline dw_loc_descr_ref
1580 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1582 HOST_WIDE_INT const_offset;
1583 if (offset.is_constant (&const_offset))
1585 if (reg <= 31)
1586 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1587 const_offset, 0);
1588 else
1589 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1591 else
1593 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1594 loc_descr_plus_const (&ret, offset);
1595 return ret;
1599 /* Add a constant OFFSET to a location list. */
1601 static void
1602 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1604 dw_loc_list_ref d;
1605 for (d = list_head; d != NULL; d = d->dw_loc_next)
1606 loc_descr_plus_const (&d->expr, offset);
1609 #define DWARF_REF_SIZE \
1610 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1612 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1613 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1614 DW_FORM_data16 with 128 bits. */
1615 #define DWARF_LARGEST_DATA_FORM_BITS \
1616 (dwarf_version >= 5 ? 128 : 64)
1618 /* Utility inline function for construction of ops that were GNU extension
1619 before DWARF 5. */
1620 static inline enum dwarf_location_atom
1621 dwarf_OP (enum dwarf_location_atom op)
1623 switch (op)
1625 case DW_OP_implicit_pointer:
1626 if (dwarf_version < 5)
1627 return DW_OP_GNU_implicit_pointer;
1628 break;
1630 case DW_OP_entry_value:
1631 if (dwarf_version < 5)
1632 return DW_OP_GNU_entry_value;
1633 break;
1635 case DW_OP_const_type:
1636 if (dwarf_version < 5)
1637 return DW_OP_GNU_const_type;
1638 break;
1640 case DW_OP_regval_type:
1641 if (dwarf_version < 5)
1642 return DW_OP_GNU_regval_type;
1643 break;
1645 case DW_OP_deref_type:
1646 if (dwarf_version < 5)
1647 return DW_OP_GNU_deref_type;
1648 break;
1650 case DW_OP_convert:
1651 if (dwarf_version < 5)
1652 return DW_OP_GNU_convert;
1653 break;
1655 case DW_OP_reinterpret:
1656 if (dwarf_version < 5)
1657 return DW_OP_GNU_reinterpret;
1658 break;
1660 default:
1661 break;
1663 return op;
1666 /* Similarly for attributes. */
1667 static inline enum dwarf_attribute
1668 dwarf_AT (enum dwarf_attribute at)
1670 switch (at)
1672 case DW_AT_call_return_pc:
1673 if (dwarf_version < 5)
1674 return DW_AT_low_pc;
1675 break;
1677 case DW_AT_call_tail_call:
1678 if (dwarf_version < 5)
1679 return DW_AT_GNU_tail_call;
1680 break;
1682 case DW_AT_call_origin:
1683 if (dwarf_version < 5)
1684 return DW_AT_abstract_origin;
1685 break;
1687 case DW_AT_call_target:
1688 if (dwarf_version < 5)
1689 return DW_AT_GNU_call_site_target;
1690 break;
1692 case DW_AT_call_target_clobbered:
1693 if (dwarf_version < 5)
1694 return DW_AT_GNU_call_site_target_clobbered;
1695 break;
1697 case DW_AT_call_parameter:
1698 if (dwarf_version < 5)
1699 return DW_AT_abstract_origin;
1700 break;
1702 case DW_AT_call_value:
1703 if (dwarf_version < 5)
1704 return DW_AT_GNU_call_site_value;
1705 break;
1707 case DW_AT_call_data_value:
1708 if (dwarf_version < 5)
1709 return DW_AT_GNU_call_site_data_value;
1710 break;
1712 case DW_AT_call_all_calls:
1713 if (dwarf_version < 5)
1714 return DW_AT_GNU_all_call_sites;
1715 break;
1717 case DW_AT_call_all_tail_calls:
1718 if (dwarf_version < 5)
1719 return DW_AT_GNU_all_tail_call_sites;
1720 break;
1722 case DW_AT_dwo_name:
1723 if (dwarf_version < 5)
1724 return DW_AT_GNU_dwo_name;
1725 break;
1727 case DW_AT_addr_base:
1728 if (dwarf_version < 5)
1729 return DW_AT_GNU_addr_base;
1730 break;
1732 default:
1733 break;
1735 return at;
1738 /* And similarly for tags. */
1739 static inline enum dwarf_tag
1740 dwarf_TAG (enum dwarf_tag tag)
1742 switch (tag)
1744 case DW_TAG_call_site:
1745 if (dwarf_version < 5)
1746 return DW_TAG_GNU_call_site;
1747 break;
1749 case DW_TAG_call_site_parameter:
1750 if (dwarf_version < 5)
1751 return DW_TAG_GNU_call_site_parameter;
1752 break;
1754 default:
1755 break;
1757 return tag;
1760 static unsigned long int get_base_type_offset (dw_die_ref);
1762 /* Return the size of a location descriptor. */
1764 static unsigned long
1765 size_of_loc_descr (dw_loc_descr_ref loc)
1767 unsigned long size = 1;
1769 switch (loc->dw_loc_opc)
1771 case DW_OP_addr:
1772 size += DWARF2_ADDR_SIZE;
1773 break;
1774 case DW_OP_GNU_addr_index:
1775 case DW_OP_GNU_const_index:
1776 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1777 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1778 break;
1779 case DW_OP_const1u:
1780 case DW_OP_const1s:
1781 size += 1;
1782 break;
1783 case DW_OP_const2u:
1784 case DW_OP_const2s:
1785 size += 2;
1786 break;
1787 case DW_OP_const4u:
1788 case DW_OP_const4s:
1789 size += 4;
1790 break;
1791 case DW_OP_const8u:
1792 case DW_OP_const8s:
1793 size += 8;
1794 break;
1795 case DW_OP_constu:
1796 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1797 break;
1798 case DW_OP_consts:
1799 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1800 break;
1801 case DW_OP_pick:
1802 size += 1;
1803 break;
1804 case DW_OP_plus_uconst:
1805 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1806 break;
1807 case DW_OP_skip:
1808 case DW_OP_bra:
1809 size += 2;
1810 break;
1811 case DW_OP_breg0:
1812 case DW_OP_breg1:
1813 case DW_OP_breg2:
1814 case DW_OP_breg3:
1815 case DW_OP_breg4:
1816 case DW_OP_breg5:
1817 case DW_OP_breg6:
1818 case DW_OP_breg7:
1819 case DW_OP_breg8:
1820 case DW_OP_breg9:
1821 case DW_OP_breg10:
1822 case DW_OP_breg11:
1823 case DW_OP_breg12:
1824 case DW_OP_breg13:
1825 case DW_OP_breg14:
1826 case DW_OP_breg15:
1827 case DW_OP_breg16:
1828 case DW_OP_breg17:
1829 case DW_OP_breg18:
1830 case DW_OP_breg19:
1831 case DW_OP_breg20:
1832 case DW_OP_breg21:
1833 case DW_OP_breg22:
1834 case DW_OP_breg23:
1835 case DW_OP_breg24:
1836 case DW_OP_breg25:
1837 case DW_OP_breg26:
1838 case DW_OP_breg27:
1839 case DW_OP_breg28:
1840 case DW_OP_breg29:
1841 case DW_OP_breg30:
1842 case DW_OP_breg31:
1843 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1844 break;
1845 case DW_OP_regx:
1846 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1847 break;
1848 case DW_OP_fbreg:
1849 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1850 break;
1851 case DW_OP_bregx:
1852 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1853 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1854 break;
1855 case DW_OP_piece:
1856 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1857 break;
1858 case DW_OP_bit_piece:
1859 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1860 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1861 break;
1862 case DW_OP_deref_size:
1863 case DW_OP_xderef_size:
1864 size += 1;
1865 break;
1866 case DW_OP_call2:
1867 size += 2;
1868 break;
1869 case DW_OP_call4:
1870 size += 4;
1871 break;
1872 case DW_OP_call_ref:
1873 case DW_OP_GNU_variable_value:
1874 size += DWARF_REF_SIZE;
1875 break;
1876 case DW_OP_implicit_value:
1877 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1878 + loc->dw_loc_oprnd1.v.val_unsigned;
1879 break;
1880 case DW_OP_implicit_pointer:
1881 case DW_OP_GNU_implicit_pointer:
1882 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1883 break;
1884 case DW_OP_entry_value:
1885 case DW_OP_GNU_entry_value:
1887 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1888 size += size_of_uleb128 (op_size) + op_size;
1889 break;
1891 case DW_OP_const_type:
1892 case DW_OP_GNU_const_type:
1894 unsigned long o
1895 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1896 size += size_of_uleb128 (o) + 1;
1897 switch (loc->dw_loc_oprnd2.val_class)
1899 case dw_val_class_vec:
1900 size += loc->dw_loc_oprnd2.v.val_vec.length
1901 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1902 break;
1903 case dw_val_class_const:
1904 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1905 break;
1906 case dw_val_class_const_double:
1907 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1908 break;
1909 case dw_val_class_wide_int:
1910 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1911 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1912 break;
1913 default:
1914 gcc_unreachable ();
1916 break;
1918 case DW_OP_regval_type:
1919 case DW_OP_GNU_regval_type:
1921 unsigned long o
1922 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1923 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1924 + size_of_uleb128 (o);
1926 break;
1927 case DW_OP_deref_type:
1928 case DW_OP_GNU_deref_type:
1930 unsigned long o
1931 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1932 size += 1 + size_of_uleb128 (o);
1934 break;
1935 case DW_OP_convert:
1936 case DW_OP_reinterpret:
1937 case DW_OP_GNU_convert:
1938 case DW_OP_GNU_reinterpret:
1939 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1940 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1941 else
1943 unsigned long o
1944 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1945 size += size_of_uleb128 (o);
1947 break;
1948 case DW_OP_GNU_parameter_ref:
1949 size += 4;
1950 break;
1951 default:
1952 break;
1955 return size;
1958 /* Return the size of a series of location descriptors. */
1960 unsigned long
1961 size_of_locs (dw_loc_descr_ref loc)
1963 dw_loc_descr_ref l;
1964 unsigned long size;
1966 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1967 field, to avoid writing to a PCH file. */
1968 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1970 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1971 break;
1972 size += size_of_loc_descr (l);
1974 if (! l)
1975 return size;
1977 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1979 l->dw_loc_addr = size;
1980 size += size_of_loc_descr (l);
1983 return size;
1986 /* Return the size of the value in a DW_AT_discr_value attribute. */
1988 static int
1989 size_of_discr_value (dw_discr_value *discr_value)
1991 if (discr_value->pos)
1992 return size_of_uleb128 (discr_value->v.uval);
1993 else
1994 return size_of_sleb128 (discr_value->v.sval);
1997 /* Return the size of the value in a DW_AT_discr_list attribute. */
1999 static int
2000 size_of_discr_list (dw_discr_list_ref discr_list)
2002 int size = 0;
2004 for (dw_discr_list_ref list = discr_list;
2005 list != NULL;
2006 list = list->dw_discr_next)
2008 /* One byte for the discriminant value descriptor, and then one or two
2009 LEB128 numbers, depending on whether it's a single case label or a
2010 range label. */
2011 size += 1;
2012 size += size_of_discr_value (&list->dw_discr_lower_bound);
2013 if (list->dw_discr_range != 0)
2014 size += size_of_discr_value (&list->dw_discr_upper_bound);
2016 return size;
2019 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2020 static void get_ref_die_offset_label (char *, dw_die_ref);
2021 static unsigned long int get_ref_die_offset (dw_die_ref);
2023 /* Output location description stack opcode's operands (if any).
2024 The for_eh_or_skip parameter controls whether register numbers are
2025 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2026 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2027 info). This should be suppressed for the cases that have not been converted
2028 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2030 static void
2031 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2033 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2034 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2036 switch (loc->dw_loc_opc)
2038 #ifdef DWARF2_DEBUGGING_INFO
2039 case DW_OP_const2u:
2040 case DW_OP_const2s:
2041 dw2_asm_output_data (2, val1->v.val_int, NULL);
2042 break;
2043 case DW_OP_const4u:
2044 if (loc->dtprel)
2046 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2047 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2048 val1->v.val_addr);
2049 fputc ('\n', asm_out_file);
2050 break;
2052 /* FALLTHRU */
2053 case DW_OP_const4s:
2054 dw2_asm_output_data (4, val1->v.val_int, NULL);
2055 break;
2056 case DW_OP_const8u:
2057 if (loc->dtprel)
2059 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2060 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2061 val1->v.val_addr);
2062 fputc ('\n', asm_out_file);
2063 break;
2065 /* FALLTHRU */
2066 case DW_OP_const8s:
2067 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2068 dw2_asm_output_data (8, val1->v.val_int, NULL);
2069 break;
2070 case DW_OP_skip:
2071 case DW_OP_bra:
2073 int offset;
2075 gcc_assert (val1->val_class == dw_val_class_loc);
2076 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2078 dw2_asm_output_data (2, offset, NULL);
2080 break;
2081 case DW_OP_implicit_value:
2082 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2083 switch (val2->val_class)
2085 case dw_val_class_const:
2086 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2087 break;
2088 case dw_val_class_vec:
2090 unsigned int elt_size = val2->v.val_vec.elt_size;
2091 unsigned int len = val2->v.val_vec.length;
2092 unsigned int i;
2093 unsigned char *p;
2095 if (elt_size > sizeof (HOST_WIDE_INT))
2097 elt_size /= 2;
2098 len *= 2;
2100 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2101 i < len;
2102 i++, p += elt_size)
2103 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2104 "fp or vector constant word %u", i);
2106 break;
2107 case dw_val_class_const_double:
2109 unsigned HOST_WIDE_INT first, second;
2111 if (WORDS_BIG_ENDIAN)
2113 first = val2->v.val_double.high;
2114 second = val2->v.val_double.low;
2116 else
2118 first = val2->v.val_double.low;
2119 second = val2->v.val_double.high;
2121 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2122 first, NULL);
2123 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2124 second, NULL);
2126 break;
2127 case dw_val_class_wide_int:
2129 int i;
2130 int len = get_full_len (*val2->v.val_wide);
2131 if (WORDS_BIG_ENDIAN)
2132 for (i = len - 1; i >= 0; --i)
2133 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2134 val2->v.val_wide->elt (i), NULL);
2135 else
2136 for (i = 0; i < len; ++i)
2137 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2138 val2->v.val_wide->elt (i), NULL);
2140 break;
2141 case dw_val_class_addr:
2142 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2143 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2144 break;
2145 default:
2146 gcc_unreachable ();
2148 break;
2149 #else
2150 case DW_OP_const2u:
2151 case DW_OP_const2s:
2152 case DW_OP_const4u:
2153 case DW_OP_const4s:
2154 case DW_OP_const8u:
2155 case DW_OP_const8s:
2156 case DW_OP_skip:
2157 case DW_OP_bra:
2158 case DW_OP_implicit_value:
2159 /* We currently don't make any attempt to make sure these are
2160 aligned properly like we do for the main unwind info, so
2161 don't support emitting things larger than a byte if we're
2162 only doing unwinding. */
2163 gcc_unreachable ();
2164 #endif
2165 case DW_OP_const1u:
2166 case DW_OP_const1s:
2167 dw2_asm_output_data (1, val1->v.val_int, NULL);
2168 break;
2169 case DW_OP_constu:
2170 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2171 break;
2172 case DW_OP_consts:
2173 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2174 break;
2175 case DW_OP_pick:
2176 dw2_asm_output_data (1, val1->v.val_int, NULL);
2177 break;
2178 case DW_OP_plus_uconst:
2179 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2180 break;
2181 case DW_OP_breg0:
2182 case DW_OP_breg1:
2183 case DW_OP_breg2:
2184 case DW_OP_breg3:
2185 case DW_OP_breg4:
2186 case DW_OP_breg5:
2187 case DW_OP_breg6:
2188 case DW_OP_breg7:
2189 case DW_OP_breg8:
2190 case DW_OP_breg9:
2191 case DW_OP_breg10:
2192 case DW_OP_breg11:
2193 case DW_OP_breg12:
2194 case DW_OP_breg13:
2195 case DW_OP_breg14:
2196 case DW_OP_breg15:
2197 case DW_OP_breg16:
2198 case DW_OP_breg17:
2199 case DW_OP_breg18:
2200 case DW_OP_breg19:
2201 case DW_OP_breg20:
2202 case DW_OP_breg21:
2203 case DW_OP_breg22:
2204 case DW_OP_breg23:
2205 case DW_OP_breg24:
2206 case DW_OP_breg25:
2207 case DW_OP_breg26:
2208 case DW_OP_breg27:
2209 case DW_OP_breg28:
2210 case DW_OP_breg29:
2211 case DW_OP_breg30:
2212 case DW_OP_breg31:
2213 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2214 break;
2215 case DW_OP_regx:
2217 unsigned r = val1->v.val_unsigned;
2218 if (for_eh_or_skip >= 0)
2219 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2220 gcc_assert (size_of_uleb128 (r)
2221 == size_of_uleb128 (val1->v.val_unsigned));
2222 dw2_asm_output_data_uleb128 (r, NULL);
2224 break;
2225 case DW_OP_fbreg:
2226 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2227 break;
2228 case DW_OP_bregx:
2230 unsigned r = val1->v.val_unsigned;
2231 if (for_eh_or_skip >= 0)
2232 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2233 gcc_assert (size_of_uleb128 (r)
2234 == size_of_uleb128 (val1->v.val_unsigned));
2235 dw2_asm_output_data_uleb128 (r, NULL);
2236 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2238 break;
2239 case DW_OP_piece:
2240 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2241 break;
2242 case DW_OP_bit_piece:
2243 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2244 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2245 break;
2246 case DW_OP_deref_size:
2247 case DW_OP_xderef_size:
2248 dw2_asm_output_data (1, val1->v.val_int, NULL);
2249 break;
2251 case DW_OP_addr:
2252 if (loc->dtprel)
2254 if (targetm.asm_out.output_dwarf_dtprel)
2256 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2257 DWARF2_ADDR_SIZE,
2258 val1->v.val_addr);
2259 fputc ('\n', asm_out_file);
2261 else
2262 gcc_unreachable ();
2264 else
2266 #ifdef DWARF2_DEBUGGING_INFO
2267 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2268 #else
2269 gcc_unreachable ();
2270 #endif
2272 break;
2274 case DW_OP_GNU_addr_index:
2275 case DW_OP_GNU_const_index:
2276 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2277 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2278 "(index into .debug_addr)");
2279 break;
2281 case DW_OP_call2:
2282 case DW_OP_call4:
2284 unsigned long die_offset
2285 = get_ref_die_offset (val1->v.val_die_ref.die);
2286 /* Make sure the offset has been computed and that we can encode it as
2287 an operand. */
2288 gcc_assert (die_offset > 0
2289 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2290 ? 0xffff
2291 : 0xffffffff));
2292 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2293 die_offset, NULL);
2295 break;
2297 case DW_OP_call_ref:
2298 case DW_OP_GNU_variable_value:
2300 char label[MAX_ARTIFICIAL_LABEL_BYTES
2301 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2302 gcc_assert (val1->val_class == dw_val_class_die_ref);
2303 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2304 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2306 break;
2308 case DW_OP_implicit_pointer:
2309 case DW_OP_GNU_implicit_pointer:
2311 char label[MAX_ARTIFICIAL_LABEL_BYTES
2312 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2313 gcc_assert (val1->val_class == dw_val_class_die_ref);
2314 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2315 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2316 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2318 break;
2320 case DW_OP_entry_value:
2321 case DW_OP_GNU_entry_value:
2322 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2323 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2324 break;
2326 case DW_OP_const_type:
2327 case DW_OP_GNU_const_type:
2329 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2330 gcc_assert (o);
2331 dw2_asm_output_data_uleb128 (o, NULL);
2332 switch (val2->val_class)
2334 case dw_val_class_const:
2335 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2336 dw2_asm_output_data (1, l, NULL);
2337 dw2_asm_output_data (l, val2->v.val_int, NULL);
2338 break;
2339 case dw_val_class_vec:
2341 unsigned int elt_size = val2->v.val_vec.elt_size;
2342 unsigned int len = val2->v.val_vec.length;
2343 unsigned int i;
2344 unsigned char *p;
2346 l = len * elt_size;
2347 dw2_asm_output_data (1, l, NULL);
2348 if (elt_size > sizeof (HOST_WIDE_INT))
2350 elt_size /= 2;
2351 len *= 2;
2353 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2354 i < len;
2355 i++, p += elt_size)
2356 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2357 "fp or vector constant word %u", i);
2359 break;
2360 case dw_val_class_const_double:
2362 unsigned HOST_WIDE_INT first, second;
2363 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2365 dw2_asm_output_data (1, 2 * l, NULL);
2366 if (WORDS_BIG_ENDIAN)
2368 first = val2->v.val_double.high;
2369 second = val2->v.val_double.low;
2371 else
2373 first = val2->v.val_double.low;
2374 second = val2->v.val_double.high;
2376 dw2_asm_output_data (l, first, NULL);
2377 dw2_asm_output_data (l, second, NULL);
2379 break;
2380 case dw_val_class_wide_int:
2382 int i;
2383 int len = get_full_len (*val2->v.val_wide);
2384 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2386 dw2_asm_output_data (1, len * l, NULL);
2387 if (WORDS_BIG_ENDIAN)
2388 for (i = len - 1; i >= 0; --i)
2389 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2390 else
2391 for (i = 0; i < len; ++i)
2392 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2394 break;
2395 default:
2396 gcc_unreachable ();
2399 break;
2400 case DW_OP_regval_type:
2401 case DW_OP_GNU_regval_type:
2403 unsigned r = val1->v.val_unsigned;
2404 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2405 gcc_assert (o);
2406 if (for_eh_or_skip >= 0)
2408 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2409 gcc_assert (size_of_uleb128 (r)
2410 == size_of_uleb128 (val1->v.val_unsigned));
2412 dw2_asm_output_data_uleb128 (r, NULL);
2413 dw2_asm_output_data_uleb128 (o, NULL);
2415 break;
2416 case DW_OP_deref_type:
2417 case DW_OP_GNU_deref_type:
2419 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2420 gcc_assert (o);
2421 dw2_asm_output_data (1, val1->v.val_int, NULL);
2422 dw2_asm_output_data_uleb128 (o, NULL);
2424 break;
2425 case DW_OP_convert:
2426 case DW_OP_reinterpret:
2427 case DW_OP_GNU_convert:
2428 case DW_OP_GNU_reinterpret:
2429 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2430 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2431 else
2433 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2434 gcc_assert (o);
2435 dw2_asm_output_data_uleb128 (o, NULL);
2437 break;
2439 case DW_OP_GNU_parameter_ref:
2441 unsigned long o;
2442 gcc_assert (val1->val_class == dw_val_class_die_ref);
2443 o = get_ref_die_offset (val1->v.val_die_ref.die);
2444 dw2_asm_output_data (4, o, NULL);
2446 break;
2448 default:
2449 /* Other codes have no operands. */
2450 break;
2454 /* Output a sequence of location operations.
2455 The for_eh_or_skip parameter controls whether register numbers are
2456 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2457 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2458 info). This should be suppressed for the cases that have not been converted
2459 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2461 void
2462 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2464 for (; loc != NULL; loc = loc->dw_loc_next)
2466 enum dwarf_location_atom opc = loc->dw_loc_opc;
2467 /* Output the opcode. */
2468 if (for_eh_or_skip >= 0
2469 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2471 unsigned r = (opc - DW_OP_breg0);
2472 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2473 gcc_assert (r <= 31);
2474 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2476 else if (for_eh_or_skip >= 0
2477 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2479 unsigned r = (opc - DW_OP_reg0);
2480 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2481 gcc_assert (r <= 31);
2482 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2485 dw2_asm_output_data (1, opc,
2486 "%s", dwarf_stack_op_name (opc));
2488 /* Output the operand(s) (if any). */
2489 output_loc_operands (loc, for_eh_or_skip);
2493 /* Output location description stack opcode's operands (if any).
2494 The output is single bytes on a line, suitable for .cfi_escape. */
2496 static void
2497 output_loc_operands_raw (dw_loc_descr_ref loc)
2499 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2500 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2502 switch (loc->dw_loc_opc)
2504 case DW_OP_addr:
2505 case DW_OP_GNU_addr_index:
2506 case DW_OP_GNU_const_index:
2507 case DW_OP_implicit_value:
2508 /* We cannot output addresses in .cfi_escape, only bytes. */
2509 gcc_unreachable ();
2511 case DW_OP_const1u:
2512 case DW_OP_const1s:
2513 case DW_OP_pick:
2514 case DW_OP_deref_size:
2515 case DW_OP_xderef_size:
2516 fputc (',', asm_out_file);
2517 dw2_asm_output_data_raw (1, val1->v.val_int);
2518 break;
2520 case DW_OP_const2u:
2521 case DW_OP_const2s:
2522 fputc (',', asm_out_file);
2523 dw2_asm_output_data_raw (2, val1->v.val_int);
2524 break;
2526 case DW_OP_const4u:
2527 case DW_OP_const4s:
2528 fputc (',', asm_out_file);
2529 dw2_asm_output_data_raw (4, val1->v.val_int);
2530 break;
2532 case DW_OP_const8u:
2533 case DW_OP_const8s:
2534 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2535 fputc (',', asm_out_file);
2536 dw2_asm_output_data_raw (8, val1->v.val_int);
2537 break;
2539 case DW_OP_skip:
2540 case DW_OP_bra:
2542 int offset;
2544 gcc_assert (val1->val_class == dw_val_class_loc);
2545 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2547 fputc (',', asm_out_file);
2548 dw2_asm_output_data_raw (2, offset);
2550 break;
2552 case DW_OP_regx:
2554 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2555 gcc_assert (size_of_uleb128 (r)
2556 == size_of_uleb128 (val1->v.val_unsigned));
2557 fputc (',', asm_out_file);
2558 dw2_asm_output_data_uleb128_raw (r);
2560 break;
2562 case DW_OP_constu:
2563 case DW_OP_plus_uconst:
2564 case DW_OP_piece:
2565 fputc (',', asm_out_file);
2566 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2567 break;
2569 case DW_OP_bit_piece:
2570 fputc (',', asm_out_file);
2571 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2572 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2573 break;
2575 case DW_OP_consts:
2576 case DW_OP_breg0:
2577 case DW_OP_breg1:
2578 case DW_OP_breg2:
2579 case DW_OP_breg3:
2580 case DW_OP_breg4:
2581 case DW_OP_breg5:
2582 case DW_OP_breg6:
2583 case DW_OP_breg7:
2584 case DW_OP_breg8:
2585 case DW_OP_breg9:
2586 case DW_OP_breg10:
2587 case DW_OP_breg11:
2588 case DW_OP_breg12:
2589 case DW_OP_breg13:
2590 case DW_OP_breg14:
2591 case DW_OP_breg15:
2592 case DW_OP_breg16:
2593 case DW_OP_breg17:
2594 case DW_OP_breg18:
2595 case DW_OP_breg19:
2596 case DW_OP_breg20:
2597 case DW_OP_breg21:
2598 case DW_OP_breg22:
2599 case DW_OP_breg23:
2600 case DW_OP_breg24:
2601 case DW_OP_breg25:
2602 case DW_OP_breg26:
2603 case DW_OP_breg27:
2604 case DW_OP_breg28:
2605 case DW_OP_breg29:
2606 case DW_OP_breg30:
2607 case DW_OP_breg31:
2608 case DW_OP_fbreg:
2609 fputc (',', asm_out_file);
2610 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2611 break;
2613 case DW_OP_bregx:
2615 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2616 gcc_assert (size_of_uleb128 (r)
2617 == size_of_uleb128 (val1->v.val_unsigned));
2618 fputc (',', asm_out_file);
2619 dw2_asm_output_data_uleb128_raw (r);
2620 fputc (',', asm_out_file);
2621 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2623 break;
2625 case DW_OP_implicit_pointer:
2626 case DW_OP_entry_value:
2627 case DW_OP_const_type:
2628 case DW_OP_regval_type:
2629 case DW_OP_deref_type:
2630 case DW_OP_convert:
2631 case DW_OP_reinterpret:
2632 case DW_OP_GNU_implicit_pointer:
2633 case DW_OP_GNU_entry_value:
2634 case DW_OP_GNU_const_type:
2635 case DW_OP_GNU_regval_type:
2636 case DW_OP_GNU_deref_type:
2637 case DW_OP_GNU_convert:
2638 case DW_OP_GNU_reinterpret:
2639 case DW_OP_GNU_parameter_ref:
2640 gcc_unreachable ();
2641 break;
2643 default:
2644 /* Other codes have no operands. */
2645 break;
2649 void
2650 output_loc_sequence_raw (dw_loc_descr_ref loc)
2652 while (1)
2654 enum dwarf_location_atom opc = loc->dw_loc_opc;
2655 /* Output the opcode. */
2656 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2658 unsigned r = (opc - DW_OP_breg0);
2659 r = DWARF2_FRAME_REG_OUT (r, 1);
2660 gcc_assert (r <= 31);
2661 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2663 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2665 unsigned r = (opc - DW_OP_reg0);
2666 r = DWARF2_FRAME_REG_OUT (r, 1);
2667 gcc_assert (r <= 31);
2668 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2670 /* Output the opcode. */
2671 fprintf (asm_out_file, "%#x", opc);
2672 output_loc_operands_raw (loc);
2674 if (!loc->dw_loc_next)
2675 break;
2676 loc = loc->dw_loc_next;
2678 fputc (',', asm_out_file);
2682 /* This function builds a dwarf location descriptor sequence from a
2683 dw_cfa_location, adding the given OFFSET to the result of the
2684 expression. */
2686 struct dw_loc_descr_node *
2687 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2689 struct dw_loc_descr_node *head, *tmp;
2691 offset += cfa->offset;
2693 if (cfa->indirect)
2695 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2696 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2697 head->dw_loc_oprnd1.val_entry = NULL;
2698 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2699 add_loc_descr (&head, tmp);
2700 loc_descr_plus_const (&head, offset);
2702 else
2703 head = new_reg_loc_descr (cfa->reg, offset);
2705 return head;
2708 /* This function builds a dwarf location descriptor sequence for
2709 the address at OFFSET from the CFA when stack is aligned to
2710 ALIGNMENT byte. */
2712 struct dw_loc_descr_node *
2713 build_cfa_aligned_loc (dw_cfa_location *cfa,
2714 poly_int64 offset, HOST_WIDE_INT alignment)
2716 struct dw_loc_descr_node *head;
2717 unsigned int dwarf_fp
2718 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2720 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2721 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2723 head = new_reg_loc_descr (dwarf_fp, 0);
2724 add_loc_descr (&head, int_loc_descriptor (alignment));
2725 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2726 loc_descr_plus_const (&head, offset);
2728 else
2729 head = new_reg_loc_descr (dwarf_fp, offset);
2730 return head;
2733 /* And now, the support for symbolic debugging information. */
2735 /* .debug_str support. */
2737 static void dwarf2out_init (const char *);
2738 static void dwarf2out_finish (const char *);
2739 static void dwarf2out_early_finish (const char *);
2740 static void dwarf2out_assembly_start (void);
2741 static void dwarf2out_define (unsigned int, const char *);
2742 static void dwarf2out_undef (unsigned int, const char *);
2743 static void dwarf2out_start_source_file (unsigned, const char *);
2744 static void dwarf2out_end_source_file (unsigned);
2745 static void dwarf2out_function_decl (tree);
2746 static void dwarf2out_begin_block (unsigned, unsigned);
2747 static void dwarf2out_end_block (unsigned, unsigned);
2748 static bool dwarf2out_ignore_block (const_tree);
2749 static void dwarf2out_early_global_decl (tree);
2750 static void dwarf2out_late_global_decl (tree);
2751 static void dwarf2out_type_decl (tree, int);
2752 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2753 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2754 dw_die_ref);
2755 static void dwarf2out_abstract_function (tree);
2756 static void dwarf2out_var_location (rtx_insn *);
2757 static void dwarf2out_inline_entry (tree);
2758 static void dwarf2out_size_function (tree);
2759 static void dwarf2out_begin_function (tree);
2760 static void dwarf2out_end_function (unsigned int);
2761 static void dwarf2out_register_main_translation_unit (tree unit);
2762 static void dwarf2out_set_name (tree, tree);
2763 static void dwarf2out_register_external_die (tree decl, const char *sym,
2764 unsigned HOST_WIDE_INT off);
2765 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2766 unsigned HOST_WIDE_INT *off);
2768 /* The debug hooks structure. */
2770 const struct gcc_debug_hooks dwarf2_debug_hooks =
2772 dwarf2out_init,
2773 dwarf2out_finish,
2774 dwarf2out_early_finish,
2775 dwarf2out_assembly_start,
2776 dwarf2out_define,
2777 dwarf2out_undef,
2778 dwarf2out_start_source_file,
2779 dwarf2out_end_source_file,
2780 dwarf2out_begin_block,
2781 dwarf2out_end_block,
2782 dwarf2out_ignore_block,
2783 dwarf2out_source_line,
2784 dwarf2out_begin_prologue,
2785 #if VMS_DEBUGGING_INFO
2786 dwarf2out_vms_end_prologue,
2787 dwarf2out_vms_begin_epilogue,
2788 #else
2789 debug_nothing_int_charstar,
2790 debug_nothing_int_charstar,
2791 #endif
2792 dwarf2out_end_epilogue,
2793 dwarf2out_begin_function,
2794 dwarf2out_end_function, /* end_function */
2795 dwarf2out_register_main_translation_unit,
2796 dwarf2out_function_decl, /* function_decl */
2797 dwarf2out_early_global_decl,
2798 dwarf2out_late_global_decl,
2799 dwarf2out_type_decl, /* type_decl */
2800 dwarf2out_imported_module_or_decl,
2801 dwarf2out_die_ref_for_decl,
2802 dwarf2out_register_external_die,
2803 debug_nothing_tree, /* deferred_inline_function */
2804 /* The DWARF 2 backend tries to reduce debugging bloat by not
2805 emitting the abstract description of inline functions until
2806 something tries to reference them. */
2807 dwarf2out_abstract_function, /* outlining_inline_function */
2808 debug_nothing_rtx_code_label, /* label */
2809 debug_nothing_int, /* handle_pch */
2810 dwarf2out_var_location,
2811 dwarf2out_inline_entry, /* inline_entry */
2812 dwarf2out_size_function, /* size_function */
2813 dwarf2out_switch_text_section,
2814 dwarf2out_set_name,
2815 1, /* start_end_main_source_file */
2816 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2819 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2821 dwarf2out_init,
2822 debug_nothing_charstar,
2823 debug_nothing_charstar,
2824 dwarf2out_assembly_start,
2825 debug_nothing_int_charstar,
2826 debug_nothing_int_charstar,
2827 debug_nothing_int_charstar,
2828 debug_nothing_int,
2829 debug_nothing_int_int, /* begin_block */
2830 debug_nothing_int_int, /* end_block */
2831 debug_true_const_tree, /* ignore_block */
2832 dwarf2out_source_line, /* source_line */
2833 debug_nothing_int_int_charstar, /* begin_prologue */
2834 debug_nothing_int_charstar, /* end_prologue */
2835 debug_nothing_int_charstar, /* begin_epilogue */
2836 debug_nothing_int_charstar, /* end_epilogue */
2837 debug_nothing_tree, /* begin_function */
2838 debug_nothing_int, /* end_function */
2839 debug_nothing_tree, /* register_main_translation_unit */
2840 debug_nothing_tree, /* function_decl */
2841 debug_nothing_tree, /* early_global_decl */
2842 debug_nothing_tree, /* late_global_decl */
2843 debug_nothing_tree_int, /* type_decl */
2844 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2845 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2846 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2847 debug_nothing_tree, /* deferred_inline_function */
2848 debug_nothing_tree, /* outlining_inline_function */
2849 debug_nothing_rtx_code_label, /* label */
2850 debug_nothing_int, /* handle_pch */
2851 debug_nothing_rtx_insn, /* var_location */
2852 debug_nothing_tree, /* inline_entry */
2853 debug_nothing_tree, /* size_function */
2854 debug_nothing_void, /* switch_text_section */
2855 debug_nothing_tree_tree, /* set_name */
2856 0, /* start_end_main_source_file */
2857 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2860 /* NOTE: In the comments in this file, many references are made to
2861 "Debugging Information Entries". This term is abbreviated as `DIE'
2862 throughout the remainder of this file. */
2864 /* An internal representation of the DWARF output is built, and then
2865 walked to generate the DWARF debugging info. The walk of the internal
2866 representation is done after the entire program has been compiled.
2867 The types below are used to describe the internal representation. */
2869 /* Whether to put type DIEs into their own section .debug_types instead
2870 of making them part of the .debug_info section. Only supported for
2871 Dwarf V4 or higher and the user didn't disable them through
2872 -fno-debug-types-section. It is more efficient to put them in a
2873 separate comdat sections since the linker will then be able to
2874 remove duplicates. But not all tools support .debug_types sections
2875 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2876 it is DW_UT_type unit type in .debug_info section. */
2878 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2880 /* Various DIE's use offsets relative to the beginning of the
2881 .debug_info section to refer to each other. */
2883 typedef long int dw_offset;
2885 struct comdat_type_node;
2887 /* The entries in the line_info table more-or-less mirror the opcodes
2888 that are used in the real dwarf line table. Arrays of these entries
2889 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2890 supported. */
2892 enum dw_line_info_opcode {
2893 /* Emit DW_LNE_set_address; the operand is the label index. */
2894 LI_set_address,
2896 /* Emit a row to the matrix with the given line. This may be done
2897 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2898 special opcodes. */
2899 LI_set_line,
2901 /* Emit a DW_LNS_set_file. */
2902 LI_set_file,
2904 /* Emit a DW_LNS_set_column. */
2905 LI_set_column,
2907 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2908 LI_negate_stmt,
2910 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2911 LI_set_prologue_end,
2912 LI_set_epilogue_begin,
2914 /* Emit a DW_LNE_set_discriminator. */
2915 LI_set_discriminator,
2917 /* Output a Fixed Advance PC; the target PC is the label index; the
2918 base PC is the previous LI_adv_address or LI_set_address entry.
2919 We only use this when emitting debug views without assembler
2920 support, at explicit user request. Ideally, we should only use
2921 it when the offset might be zero but we can't tell: it's the only
2922 way to maybe change the PC without resetting the view number. */
2923 LI_adv_address
2926 typedef struct GTY(()) dw_line_info_struct {
2927 enum dw_line_info_opcode opcode;
2928 unsigned int val;
2929 } dw_line_info_entry;
2932 struct GTY(()) dw_line_info_table {
2933 /* The label that marks the end of this section. */
2934 const char *end_label;
2936 /* The values for the last row of the matrix, as collected in the table.
2937 These are used to minimize the changes to the next row. */
2938 unsigned int file_num;
2939 unsigned int line_num;
2940 unsigned int column_num;
2941 int discrim_num;
2942 bool is_stmt;
2943 bool in_use;
2945 /* This denotes the NEXT view number.
2947 If it is 0, it is known that the NEXT view will be the first view
2948 at the given PC.
2950 If it is -1, we're forcing the view number to be reset, e.g. at a
2951 function entry.
2953 The meaning of other nonzero values depends on whether we're
2954 computing views internally or leaving it for the assembler to do
2955 so. If we're emitting them internally, view denotes the view
2956 number since the last known advance of PC. If we're leaving it
2957 for the assembler, it denotes the LVU label number that we're
2958 going to ask the assembler to assign. */
2959 var_loc_view view;
2961 /* This counts the number of symbolic views emitted in this table
2962 since the latest view reset. Its max value, over all tables,
2963 sets symview_upper_bound. */
2964 var_loc_view symviews_since_reset;
2966 #define FORCE_RESET_NEXT_VIEW(x) ((x) = (var_loc_view)-1)
2967 #define RESET_NEXT_VIEW(x) ((x) = (var_loc_view)0)
2968 #define FORCE_RESETTING_VIEW_P(x) ((x) == (var_loc_view)-1)
2969 #define RESETTING_VIEW_P(x) ((x) == (var_loc_view)0 || FORCE_RESETTING_VIEW_P (x))
2971 vec<dw_line_info_entry, va_gc> *entries;
2974 /* This is an upper bound for view numbers that the assembler may
2975 assign to symbolic views output in this translation. It is used to
2976 decide how big a field to use to represent view numbers in
2977 symview-classed attributes. */
2979 static var_loc_view symview_upper_bound;
2981 /* If we're keep track of location views and their reset points, and
2982 INSN is a reset point (i.e., it necessarily advances the PC), mark
2983 the next view in TABLE as reset. */
2985 static void
2986 maybe_reset_location_view (rtx_insn *insn, dw_line_info_table *table)
2988 if (!debug_internal_reset_location_views)
2989 return;
2991 /* Maybe turn (part of?) this test into a default target hook. */
2992 int reset = 0;
2994 if (targetm.reset_location_view)
2995 reset = targetm.reset_location_view (insn);
2997 if (reset)
2999 else if (JUMP_TABLE_DATA_P (insn))
3000 reset = 1;
3001 else if (GET_CODE (insn) == USE
3002 || GET_CODE (insn) == CLOBBER
3003 || GET_CODE (insn) == ASM_INPUT
3004 || asm_noperands (insn) >= 0)
3006 else if (get_attr_min_length (insn) > 0)
3007 reset = 1;
3009 if (reset > 0 && !RESETTING_VIEW_P (table->view))
3010 RESET_NEXT_VIEW (table->view);
3013 /* Each DIE attribute has a field specifying the attribute kind,
3014 a link to the next attribute in the chain, and an attribute value.
3015 Attributes are typically linked below the DIE they modify. */
3017 typedef struct GTY(()) dw_attr_struct {
3018 enum dwarf_attribute dw_attr;
3019 dw_val_node dw_attr_val;
3021 dw_attr_node;
3024 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
3025 The children of each node form a circular list linked by
3026 die_sib. die_child points to the node *before* the "first" child node. */
3028 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
3029 union die_symbol_or_type_node
3031 const char * GTY ((tag ("0"))) die_symbol;
3032 comdat_type_node *GTY ((tag ("1"))) die_type_node;
3034 GTY ((desc ("%0.comdat_type_p"))) die_id;
3035 vec<dw_attr_node, va_gc> *die_attr;
3036 dw_die_ref die_parent;
3037 dw_die_ref die_child;
3038 dw_die_ref die_sib;
3039 dw_die_ref die_definition; /* ref from a specification to its definition */
3040 dw_offset die_offset;
3041 unsigned long die_abbrev;
3042 int die_mark;
3043 unsigned int decl_id;
3044 enum dwarf_tag die_tag;
3045 /* Die is used and must not be pruned as unused. */
3046 BOOL_BITFIELD die_perennial_p : 1;
3047 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
3048 /* For an external ref to die_symbol if die_offset contains an extra
3049 offset to that symbol. */
3050 BOOL_BITFIELD with_offset : 1;
3051 /* Whether this DIE was removed from the DIE tree, for example via
3052 prune_unused_types. We don't consider those present from the
3053 DIE lookup routines. */
3054 BOOL_BITFIELD removed : 1;
3055 /* Lots of spare bits. */
3057 die_node;
3059 /* Set to TRUE while dwarf2out_early_global_decl is running. */
3060 static bool early_dwarf;
3061 static bool early_dwarf_finished;
3062 struct set_early_dwarf {
3063 bool saved;
3064 set_early_dwarf () : saved(early_dwarf)
3066 gcc_assert (! early_dwarf_finished);
3067 early_dwarf = true;
3069 ~set_early_dwarf () { early_dwarf = saved; }
3072 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
3073 #define FOR_EACH_CHILD(die, c, expr) do { \
3074 c = die->die_child; \
3075 if (c) do { \
3076 c = c->die_sib; \
3077 expr; \
3078 } while (c != die->die_child); \
3079 } while (0)
3081 /* The pubname structure */
3083 typedef struct GTY(()) pubname_struct {
3084 dw_die_ref die;
3085 const char *name;
3087 pubname_entry;
3090 struct GTY(()) dw_ranges {
3091 const char *label;
3092 /* If this is positive, it's a block number, otherwise it's a
3093 bitwise-negated index into dw_ranges_by_label. */
3094 int num;
3095 /* Index for the range list for DW_FORM_rnglistx. */
3096 unsigned int idx : 31;
3097 /* True if this range might be possibly in a different section
3098 from previous entry. */
3099 unsigned int maybe_new_sec : 1;
3102 /* A structure to hold a macinfo entry. */
3104 typedef struct GTY(()) macinfo_struct {
3105 unsigned char code;
3106 unsigned HOST_WIDE_INT lineno;
3107 const char *info;
3109 macinfo_entry;
3112 struct GTY(()) dw_ranges_by_label {
3113 const char *begin;
3114 const char *end;
3117 /* The comdat type node structure. */
3118 struct GTY(()) comdat_type_node
3120 dw_die_ref root_die;
3121 dw_die_ref type_die;
3122 dw_die_ref skeleton_die;
3123 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3124 comdat_type_node *next;
3127 /* A list of DIEs for which we can't determine ancestry (parent_die
3128 field) just yet. Later in dwarf2out_finish we will fill in the
3129 missing bits. */
3130 typedef struct GTY(()) limbo_die_struct {
3131 dw_die_ref die;
3132 /* The tree for which this DIE was created. We use this to
3133 determine ancestry later. */
3134 tree created_for;
3135 struct limbo_die_struct *next;
3137 limbo_die_node;
3139 typedef struct skeleton_chain_struct
3141 dw_die_ref old_die;
3142 dw_die_ref new_die;
3143 struct skeleton_chain_struct *parent;
3145 skeleton_chain_node;
3147 /* Define a macro which returns nonzero for a TYPE_DECL which was
3148 implicitly generated for a type.
3150 Note that, unlike the C front-end (which generates a NULL named
3151 TYPE_DECL node for each complete tagged type, each array type,
3152 and each function type node created) the C++ front-end generates
3153 a _named_ TYPE_DECL node for each tagged type node created.
3154 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3155 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3156 front-end, but for each type, tagged or not. */
3158 #define TYPE_DECL_IS_STUB(decl) \
3159 (DECL_NAME (decl) == NULL_TREE \
3160 || (DECL_ARTIFICIAL (decl) \
3161 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3162 /* This is necessary for stub decls that \
3163 appear in nested inline functions. */ \
3164 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3165 && (decl_ultimate_origin (decl) \
3166 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3168 /* Information concerning the compilation unit's programming
3169 language, and compiler version. */
3171 /* Fixed size portion of the DWARF compilation unit header. */
3172 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3173 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3174 + (dwarf_version >= 5 ? 4 : 3))
3176 /* Fixed size portion of the DWARF comdat type unit header. */
3177 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3178 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3179 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3181 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3182 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3183 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3185 /* Fixed size portion of public names info. */
3186 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3188 /* Fixed size portion of the address range info. */
3189 #define DWARF_ARANGES_HEADER_SIZE \
3190 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3191 DWARF2_ADDR_SIZE * 2) \
3192 - DWARF_INITIAL_LENGTH_SIZE)
3194 /* Size of padding portion in the address range info. It must be
3195 aligned to twice the pointer size. */
3196 #define DWARF_ARANGES_PAD_SIZE \
3197 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3198 DWARF2_ADDR_SIZE * 2) \
3199 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3201 /* Use assembler line directives if available. */
3202 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3203 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3204 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3205 #else
3206 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3207 #endif
3208 #endif
3210 /* Use assembler views in line directives if available. */
3211 #ifndef DWARF2_ASM_VIEW_DEBUG_INFO
3212 #ifdef HAVE_AS_DWARF2_DEBUG_VIEW
3213 #define DWARF2_ASM_VIEW_DEBUG_INFO 1
3214 #else
3215 #define DWARF2_ASM_VIEW_DEBUG_INFO 0
3216 #endif
3217 #endif
3219 /* Return true if GCC configure detected assembler support for .loc. */
3221 bool
3222 dwarf2out_default_as_loc_support (void)
3224 return DWARF2_ASM_LINE_DEBUG_INFO;
3225 #if (GCC_VERSION >= 3000)
3226 # undef DWARF2_ASM_LINE_DEBUG_INFO
3227 # pragma GCC poison DWARF2_ASM_LINE_DEBUG_INFO
3228 #endif
3231 /* Return true if GCC configure detected assembler support for views
3232 in .loc directives. */
3234 bool
3235 dwarf2out_default_as_locview_support (void)
3237 return DWARF2_ASM_VIEW_DEBUG_INFO;
3238 #if (GCC_VERSION >= 3000)
3239 # undef DWARF2_ASM_VIEW_DEBUG_INFO
3240 # pragma GCC poison DWARF2_ASM_VIEW_DEBUG_INFO
3241 #endif
3244 /* A bit is set in ZERO_VIEW_P if we are using the assembler-supported
3245 view computation, and it refers to a view identifier for which we
3246 will not emit a label because it is known to map to a view number
3247 zero. We won't allocate the bitmap if we're not using assembler
3248 support for location views, but we have to make the variable
3249 visible for GGC and for code that will be optimized out for lack of
3250 support but that's still parsed and compiled. We could abstract it
3251 out with macros, but it's not worth it. */
3252 static GTY(()) bitmap zero_view_p;
3254 /* Evaluate to TRUE iff N is known to identify the first location view
3255 at its PC. When not using assembler location view computation,
3256 that must be view number zero. Otherwise, ZERO_VIEW_P is allocated
3257 and views label numbers recorded in it are the ones known to be
3258 zero. */
3259 #define ZERO_VIEW_P(N) ((N) == (var_loc_view)0 \
3260 || (N) == (var_loc_view)-1 \
3261 || (zero_view_p \
3262 && bitmap_bit_p (zero_view_p, (N))))
3264 /* Return true iff we're to emit .loc directives for the assembler to
3265 generate line number sections.
3267 When we're not emitting views, all we need from the assembler is
3268 support for .loc directives.
3270 If we are emitting views, we can only use the assembler's .loc
3271 support if it also supports views.
3273 When the compiler is emitting the line number programs and
3274 computing view numbers itself, it resets view numbers at known PC
3275 changes and counts from that, and then it emits view numbers as
3276 literal constants in locviewlists. There are cases in which the
3277 compiler is not sure about PC changes, e.g. when extra alignment is
3278 requested for a label. In these cases, the compiler may not reset
3279 the view counter, and the potential PC advance in the line number
3280 program will use an opcode that does not reset the view counter
3281 even if the PC actually changes, so that compiler and debug info
3282 consumer can keep view numbers in sync.
3284 When the compiler defers view computation to the assembler, it
3285 emits symbolic view numbers in locviewlists, with the exception of
3286 views known to be zero (forced resets, or reset after
3287 compiler-visible PC changes): instead of emitting symbols for
3288 these, we emit literal zero and assert the assembler agrees with
3289 the compiler's assessment. We could use symbolic views everywhere,
3290 instead of special-casing zero views, but then we'd be unable to
3291 optimize out locviewlists that contain only zeros. */
3293 static bool
3294 output_asm_line_debug_info (void)
3296 return (dwarf2out_as_loc_support
3297 && (dwarf2out_as_locview_support
3298 || !debug_variable_location_views));
3301 /* Minimum line offset in a special line info. opcode.
3302 This value was chosen to give a reasonable range of values. */
3303 #define DWARF_LINE_BASE -10
3305 /* First special line opcode - leave room for the standard opcodes. */
3306 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3308 /* Range of line offsets in a special line info. opcode. */
3309 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3311 /* Flag that indicates the initial value of the is_stmt_start flag.
3312 In the present implementation, we do not mark any lines as
3313 the beginning of a source statement, because that information
3314 is not made available by the GCC front-end. */
3315 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3317 /* Maximum number of operations per instruction bundle. */
3318 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3319 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3320 #endif
3322 /* This location is used by calc_die_sizes() to keep track
3323 the offset of each DIE within the .debug_info section. */
3324 static unsigned long next_die_offset;
3326 /* Record the root of the DIE's built for the current compilation unit. */
3327 static GTY(()) dw_die_ref single_comp_unit_die;
3329 /* A list of type DIEs that have been separated into comdat sections. */
3330 static GTY(()) comdat_type_node *comdat_type_list;
3332 /* A list of CU DIEs that have been separated. */
3333 static GTY(()) limbo_die_node *cu_die_list;
3335 /* A list of DIEs with a NULL parent waiting to be relocated. */
3336 static GTY(()) limbo_die_node *limbo_die_list;
3338 /* A list of DIEs for which we may have to generate
3339 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3340 static GTY(()) limbo_die_node *deferred_asm_name;
3342 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3344 typedef const char *compare_type;
3346 static hashval_t hash (dwarf_file_data *);
3347 static bool equal (dwarf_file_data *, const char *);
3350 /* Filenames referenced by this compilation unit. */
3351 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3353 struct decl_die_hasher : ggc_ptr_hash<die_node>
3355 typedef tree compare_type;
3357 static hashval_t hash (die_node *);
3358 static bool equal (die_node *, tree);
3360 /* A hash table of references to DIE's that describe declarations.
3361 The key is a DECL_UID() which is a unique number identifying each decl. */
3362 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3364 struct GTY ((for_user)) variable_value_struct {
3365 unsigned int decl_id;
3366 vec<dw_die_ref, va_gc> *dies;
3369 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3371 typedef tree compare_type;
3373 static hashval_t hash (variable_value_struct *);
3374 static bool equal (variable_value_struct *, tree);
3376 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3377 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3378 DECL_CONTEXT of the referenced VAR_DECLs. */
3379 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3381 struct block_die_hasher : ggc_ptr_hash<die_struct>
3383 static hashval_t hash (die_struct *);
3384 static bool equal (die_struct *, die_struct *);
3387 /* A hash table of references to DIE's that describe COMMON blocks.
3388 The key is DECL_UID() ^ die_parent. */
3389 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3391 typedef struct GTY(()) die_arg_entry_struct {
3392 dw_die_ref die;
3393 tree arg;
3394 } die_arg_entry;
3397 /* Node of the variable location list. */
3398 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3399 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3400 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3401 in mode of the EXPR_LIST node and first EXPR_LIST operand
3402 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3403 location or NULL for padding. For larger bitsizes,
3404 mode is 0 and first operand is a CONCAT with bitsize
3405 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3406 NULL as second operand. */
3407 rtx GTY (()) loc;
3408 const char * GTY (()) label;
3409 struct var_loc_node * GTY (()) next;
3410 var_loc_view view;
3413 /* Variable location list. */
3414 struct GTY ((for_user)) var_loc_list_def {
3415 struct var_loc_node * GTY (()) first;
3417 /* Pointer to the last but one or last element of the
3418 chained list. If the list is empty, both first and
3419 last are NULL, if the list contains just one node
3420 or the last node certainly is not redundant, it points
3421 to the last node, otherwise points to the last but one.
3422 Do not mark it for GC because it is marked through the chain. */
3423 struct var_loc_node * GTY ((skip ("%h"))) last;
3425 /* Pointer to the last element before section switch,
3426 if NULL, either sections weren't switched or first
3427 is after section switch. */
3428 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3430 /* DECL_UID of the variable decl. */
3431 unsigned int decl_id;
3433 typedef struct var_loc_list_def var_loc_list;
3435 /* Call argument location list. */
3436 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3437 rtx GTY (()) call_arg_loc_note;
3438 const char * GTY (()) label;
3439 tree GTY (()) block;
3440 bool tail_call_p;
3441 rtx GTY (()) symbol_ref;
3442 struct call_arg_loc_node * GTY (()) next;
3446 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3448 typedef const_tree compare_type;
3450 static hashval_t hash (var_loc_list *);
3451 static bool equal (var_loc_list *, const_tree);
3454 /* Table of decl location linked lists. */
3455 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3457 /* Head and tail of call_arg_loc chain. */
3458 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3459 static struct call_arg_loc_node *call_arg_loc_last;
3461 /* Number of call sites in the current function. */
3462 static int call_site_count = -1;
3463 /* Number of tail call sites in the current function. */
3464 static int tail_call_site_count = -1;
3466 /* A cached location list. */
3467 struct GTY ((for_user)) cached_dw_loc_list_def {
3468 /* The DECL_UID of the decl that this entry describes. */
3469 unsigned int decl_id;
3471 /* The cached location list. */
3472 dw_loc_list_ref loc_list;
3474 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3476 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3479 typedef const_tree compare_type;
3481 static hashval_t hash (cached_dw_loc_list *);
3482 static bool equal (cached_dw_loc_list *, const_tree);
3485 /* Table of cached location lists. */
3486 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3488 /* A vector of references to DIE's that are uniquely identified by their tag,
3489 presence/absence of children DIE's, and list of attribute/value pairs. */
3490 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3492 /* A hash map to remember the stack usage for DWARF procedures. The value
3493 stored is the stack size difference between before the DWARF procedure
3494 invokation and after it returned. In other words, for a DWARF procedure
3495 that consumes N stack slots and that pushes M ones, this stores M - N. */
3496 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3498 /* A global counter for generating labels for line number data. */
3499 static unsigned int line_info_label_num;
3501 /* The current table to which we should emit line number information
3502 for the current function. This will be set up at the beginning of
3503 assembly for the function. */
3504 static GTY(()) dw_line_info_table *cur_line_info_table;
3506 /* The two default tables of line number info. */
3507 static GTY(()) dw_line_info_table *text_section_line_info;
3508 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3510 /* The set of all non-default tables of line number info. */
3511 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3513 /* A flag to tell pubnames/types export if there is an info section to
3514 refer to. */
3515 static bool info_section_emitted;
3517 /* A pointer to the base of a table that contains a list of publicly
3518 accessible names. */
3519 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3521 /* A pointer to the base of a table that contains a list of publicly
3522 accessible types. */
3523 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3525 /* A pointer to the base of a table that contains a list of macro
3526 defines/undefines (and file start/end markers). */
3527 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3529 /* True if .debug_macinfo or .debug_macros section is going to be
3530 emitted. */
3531 #define have_macinfo \
3532 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3533 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3534 && !macinfo_table->is_empty ())
3536 /* Vector of dies for which we should generate .debug_ranges info. */
3537 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3539 /* Vector of pairs of labels referenced in ranges_table. */
3540 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3542 /* Whether we have location lists that need outputting */
3543 static GTY(()) bool have_location_lists;
3545 /* Unique label counter. */
3546 static GTY(()) unsigned int loclabel_num;
3548 /* Unique label counter for point-of-call tables. */
3549 static GTY(()) unsigned int poc_label_num;
3551 /* The last file entry emitted by maybe_emit_file(). */
3552 static GTY(()) struct dwarf_file_data * last_emitted_file;
3554 /* Number of internal labels generated by gen_internal_sym(). */
3555 static GTY(()) int label_num;
3557 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3559 /* Instances of generic types for which we need to generate debug
3560 info that describe their generic parameters and arguments. That
3561 generation needs to happen once all types are properly laid out so
3562 we do it at the end of compilation. */
3563 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3565 /* Offset from the "steady-state frame pointer" to the frame base,
3566 within the current function. */
3567 static poly_int64 frame_pointer_fb_offset;
3568 static bool frame_pointer_fb_offset_valid;
3570 static vec<dw_die_ref> base_types;
3572 /* Flags to represent a set of attribute classes for attributes that represent
3573 a scalar value (bounds, pointers, ...). */
3574 enum dw_scalar_form
3576 dw_scalar_form_constant = 0x01,
3577 dw_scalar_form_exprloc = 0x02,
3578 dw_scalar_form_reference = 0x04
3581 /* Forward declarations for functions defined in this file. */
3583 static int is_pseudo_reg (const_rtx);
3584 static tree type_main_variant (tree);
3585 static int is_tagged_type (const_tree);
3586 static const char *dwarf_tag_name (unsigned);
3587 static const char *dwarf_attr_name (unsigned);
3588 static const char *dwarf_form_name (unsigned);
3589 static tree decl_ultimate_origin (const_tree);
3590 static tree decl_class_context (tree);
3591 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3592 static inline enum dw_val_class AT_class (dw_attr_node *);
3593 static inline unsigned int AT_index (dw_attr_node *);
3594 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3595 static inline unsigned AT_flag (dw_attr_node *);
3596 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3597 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3598 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3599 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3600 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3601 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3602 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3603 unsigned int, unsigned char *);
3604 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3605 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3606 static inline const char *AT_string (dw_attr_node *);
3607 static enum dwarf_form AT_string_form (dw_attr_node *);
3608 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3609 static void add_AT_specification (dw_die_ref, dw_die_ref);
3610 static inline dw_die_ref AT_ref (dw_attr_node *);
3611 static inline int AT_ref_external (dw_attr_node *);
3612 static inline void set_AT_ref_external (dw_attr_node *, int);
3613 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3614 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3615 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3616 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3617 dw_loc_list_ref);
3618 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3619 static void add_AT_view_list (dw_die_ref, enum dwarf_attribute);
3620 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3621 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3622 static void remove_addr_table_entry (addr_table_entry *);
3623 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3624 static inline rtx AT_addr (dw_attr_node *);
3625 static void add_AT_symview (dw_die_ref, enum dwarf_attribute, const char *);
3626 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3627 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3628 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3629 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3630 const char *);
3631 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3632 unsigned HOST_WIDE_INT);
3633 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3634 unsigned long, bool);
3635 static inline const char *AT_lbl (dw_attr_node *);
3636 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3637 static const char *get_AT_low_pc (dw_die_ref);
3638 static const char *get_AT_hi_pc (dw_die_ref);
3639 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3640 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3641 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3642 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3643 static bool is_cxx (void);
3644 static bool is_cxx (const_tree);
3645 static bool is_fortran (void);
3646 static bool is_ada (void);
3647 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3648 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3649 static void add_child_die (dw_die_ref, dw_die_ref);
3650 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3651 static dw_die_ref lookup_type_die (tree);
3652 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3653 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3654 static void equate_type_number_to_die (tree, dw_die_ref);
3655 static dw_die_ref lookup_decl_die (tree);
3656 static var_loc_list *lookup_decl_loc (const_tree);
3657 static void equate_decl_number_to_die (tree, dw_die_ref);
3658 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *, var_loc_view);
3659 static void print_spaces (FILE *);
3660 static void print_die (dw_die_ref, FILE *);
3661 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3662 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3663 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3664 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3665 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3666 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3667 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3668 struct md5_ctx *, int *);
3669 struct checksum_attributes;
3670 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3671 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3672 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3673 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3674 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3675 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3676 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3677 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3678 static int is_type_die (dw_die_ref);
3679 static int is_comdat_die (dw_die_ref);
3680 static inline bool is_template_instantiation (dw_die_ref);
3681 static int is_declaration_die (dw_die_ref);
3682 static int should_move_die_to_comdat (dw_die_ref);
3683 static dw_die_ref clone_as_declaration (dw_die_ref);
3684 static dw_die_ref clone_die (dw_die_ref);
3685 static dw_die_ref clone_tree (dw_die_ref);
3686 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3687 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3688 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3689 static dw_die_ref generate_skeleton (dw_die_ref);
3690 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3691 dw_die_ref,
3692 dw_die_ref);
3693 static void break_out_comdat_types (dw_die_ref);
3694 static void copy_decls_for_unworthy_types (dw_die_ref);
3696 static void add_sibling_attributes (dw_die_ref);
3697 static void output_location_lists (dw_die_ref);
3698 static int constant_size (unsigned HOST_WIDE_INT);
3699 static unsigned long size_of_die (dw_die_ref);
3700 static void calc_die_sizes (dw_die_ref);
3701 static void calc_base_type_die_sizes (void);
3702 static void mark_dies (dw_die_ref);
3703 static void unmark_dies (dw_die_ref);
3704 static void unmark_all_dies (dw_die_ref);
3705 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3706 static unsigned long size_of_aranges (void);
3707 static enum dwarf_form value_format (dw_attr_node *);
3708 static void output_value_format (dw_attr_node *);
3709 static void output_abbrev_section (void);
3710 static void output_die_abbrevs (unsigned long, dw_die_ref);
3711 static void output_die (dw_die_ref);
3712 static void output_compilation_unit_header (enum dwarf_unit_type);
3713 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3714 static void output_comdat_type_unit (comdat_type_node *);
3715 static const char *dwarf2_name (tree, int);
3716 static void add_pubname (tree, dw_die_ref);
3717 static void add_enumerator_pubname (const char *, dw_die_ref);
3718 static void add_pubname_string (const char *, dw_die_ref);
3719 static void add_pubtype (tree, dw_die_ref);
3720 static void output_pubnames (vec<pubname_entry, va_gc> *);
3721 static void output_aranges (void);
3722 static unsigned int add_ranges (const_tree, bool = false);
3723 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3724 bool *, bool);
3725 static void output_ranges (void);
3726 static dw_line_info_table *new_line_info_table (void);
3727 static void output_line_info (bool);
3728 static void output_file_names (void);
3729 static dw_die_ref base_type_die (tree, bool);
3730 static int is_base_type (tree);
3731 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3732 static int decl_quals (const_tree);
3733 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3734 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3735 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3736 static int type_is_enum (const_tree);
3737 static unsigned int dbx_reg_number (const_rtx);
3738 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3739 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3740 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3741 enum var_init_status);
3742 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3743 enum var_init_status);
3744 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3745 enum var_init_status);
3746 static int is_based_loc (const_rtx);
3747 static bool resolve_one_addr (rtx *);
3748 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3749 enum var_init_status);
3750 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3751 enum var_init_status);
3752 struct loc_descr_context;
3753 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3754 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3755 static dw_loc_list_ref loc_list_from_tree (tree, int,
3756 struct loc_descr_context *);
3757 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3758 struct loc_descr_context *);
3759 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3760 static tree field_type (const_tree);
3761 static unsigned int simple_type_align_in_bits (const_tree);
3762 static unsigned int simple_decl_align_in_bits (const_tree);
3763 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3764 struct vlr_context;
3765 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3766 HOST_WIDE_INT *);
3767 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3768 dw_loc_list_ref);
3769 static void add_data_member_location_attribute (dw_die_ref, tree,
3770 struct vlr_context *);
3771 static bool add_const_value_attribute (dw_die_ref, rtx);
3772 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3773 static void insert_wide_int (const wide_int &, unsigned char *, int);
3774 static void insert_float (const_rtx, unsigned char *);
3775 static rtx rtl_for_decl_location (tree);
3776 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3777 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3778 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3779 static void add_name_attribute (dw_die_ref, const char *);
3780 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3781 static void add_comp_dir_attribute (dw_die_ref);
3782 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3783 struct loc_descr_context *);
3784 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3785 struct loc_descr_context *);
3786 static void add_subscript_info (dw_die_ref, tree, bool);
3787 static void add_byte_size_attribute (dw_die_ref, tree);
3788 static void add_alignment_attribute (dw_die_ref, tree);
3789 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3790 struct vlr_context *);
3791 static void add_bit_size_attribute (dw_die_ref, tree);
3792 static void add_prototyped_attribute (dw_die_ref, tree);
3793 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3794 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3795 static void add_src_coords_attributes (dw_die_ref, tree);
3796 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3797 static void add_discr_value (dw_die_ref, dw_discr_value *);
3798 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3799 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3800 static void push_decl_scope (tree);
3801 static void pop_decl_scope (void);
3802 static dw_die_ref scope_die_for (tree, dw_die_ref);
3803 static inline int local_scope_p (dw_die_ref);
3804 static inline int class_scope_p (dw_die_ref);
3805 static inline int class_or_namespace_scope_p (dw_die_ref);
3806 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3807 static void add_calling_convention_attribute (dw_die_ref, tree);
3808 static const char *type_tag (const_tree);
3809 static tree member_declared_type (const_tree);
3810 #if 0
3811 static const char *decl_start_label (tree);
3812 #endif
3813 static void gen_array_type_die (tree, dw_die_ref);
3814 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3815 #if 0
3816 static void gen_entry_point_die (tree, dw_die_ref);
3817 #endif
3818 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3819 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3820 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3821 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3822 static void gen_formal_types_die (tree, dw_die_ref);
3823 static void gen_subprogram_die (tree, dw_die_ref);
3824 static void gen_variable_die (tree, tree, dw_die_ref);
3825 static void gen_const_die (tree, dw_die_ref);
3826 static void gen_label_die (tree, dw_die_ref);
3827 static void gen_lexical_block_die (tree, dw_die_ref);
3828 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3829 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3830 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3831 static dw_die_ref gen_compile_unit_die (const char *);
3832 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3833 static void gen_member_die (tree, dw_die_ref);
3834 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3835 enum debug_info_usage);
3836 static void gen_subroutine_type_die (tree, dw_die_ref);
3837 static void gen_typedef_die (tree, dw_die_ref);
3838 static void gen_type_die (tree, dw_die_ref);
3839 static void gen_block_die (tree, dw_die_ref);
3840 static void decls_for_scope (tree, dw_die_ref);
3841 static bool is_naming_typedef_decl (const_tree);
3842 static inline dw_die_ref get_context_die (tree);
3843 static void gen_namespace_die (tree, dw_die_ref);
3844 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3845 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3846 static dw_die_ref force_decl_die (tree);
3847 static dw_die_ref force_type_die (tree);
3848 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3849 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3850 static struct dwarf_file_data * lookup_filename (const char *);
3851 static void retry_incomplete_types (void);
3852 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3853 static void gen_generic_params_dies (tree);
3854 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3855 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3856 static void splice_child_die (dw_die_ref, dw_die_ref);
3857 static int file_info_cmp (const void *, const void *);
3858 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *, var_loc_view,
3859 const char *, var_loc_view, const char *);
3860 static void output_loc_list (dw_loc_list_ref);
3861 static char *gen_internal_sym (const char *);
3862 static bool want_pubnames (void);
3864 static void prune_unmark_dies (dw_die_ref);
3865 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3866 static void prune_unused_types_mark (dw_die_ref, int);
3867 static void prune_unused_types_walk (dw_die_ref);
3868 static void prune_unused_types_walk_attribs (dw_die_ref);
3869 static void prune_unused_types_prune (dw_die_ref);
3870 static void prune_unused_types (void);
3871 static int maybe_emit_file (struct dwarf_file_data *fd);
3872 static inline const char *AT_vms_delta1 (dw_attr_node *);
3873 static inline const char *AT_vms_delta2 (dw_attr_node *);
3874 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3875 const char *, const char *);
3876 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3877 static void gen_remaining_tmpl_value_param_die_attribute (void);
3878 static bool generic_type_p (tree);
3879 static void schedule_generic_params_dies_gen (tree t);
3880 static void gen_scheduled_generic_parms_dies (void);
3881 static void resolve_variable_values (void);
3883 static const char *comp_dir_string (void);
3885 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3887 /* enum for tracking thread-local variables whose address is really an offset
3888 relative to the TLS pointer, which will need link-time relocation, but will
3889 not need relocation by the DWARF consumer. */
3891 enum dtprel_bool
3893 dtprel_false = 0,
3894 dtprel_true = 1
3897 /* Return the operator to use for an address of a variable. For dtprel_true, we
3898 use DW_OP_const*. For regular variables, which need both link-time
3899 relocation and consumer-level relocation (e.g., to account for shared objects
3900 loaded at a random address), we use DW_OP_addr*. */
3902 static inline enum dwarf_location_atom
3903 dw_addr_op (enum dtprel_bool dtprel)
3905 if (dtprel == dtprel_true)
3906 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3907 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3908 else
3909 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3912 /* Return a pointer to a newly allocated address location description. If
3913 dwarf_split_debug_info is true, then record the address with the appropriate
3914 relocation. */
3915 static inline dw_loc_descr_ref
3916 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3918 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3920 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3921 ref->dw_loc_oprnd1.v.val_addr = addr;
3922 ref->dtprel = dtprel;
3923 if (dwarf_split_debug_info)
3924 ref->dw_loc_oprnd1.val_entry
3925 = add_addr_table_entry (addr,
3926 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3927 else
3928 ref->dw_loc_oprnd1.val_entry = NULL;
3930 return ref;
3933 /* Section names used to hold DWARF debugging information. */
3935 #ifndef DEBUG_INFO_SECTION
3936 #define DEBUG_INFO_SECTION ".debug_info"
3937 #endif
3938 #ifndef DEBUG_DWO_INFO_SECTION
3939 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3940 #endif
3941 #ifndef DEBUG_LTO_INFO_SECTION
3942 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3943 #endif
3944 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3945 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3946 #endif
3947 #ifndef DEBUG_ABBREV_SECTION
3948 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3949 #endif
3950 #ifndef DEBUG_LTO_ABBREV_SECTION
3951 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3952 #endif
3953 #ifndef DEBUG_DWO_ABBREV_SECTION
3954 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3955 #endif
3956 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3957 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3958 #endif
3959 #ifndef DEBUG_ARANGES_SECTION
3960 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3961 #endif
3962 #ifndef DEBUG_ADDR_SECTION
3963 #define DEBUG_ADDR_SECTION ".debug_addr"
3964 #endif
3965 #ifndef DEBUG_MACINFO_SECTION
3966 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3967 #endif
3968 #ifndef DEBUG_LTO_MACINFO_SECTION
3969 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3970 #endif
3971 #ifndef DEBUG_DWO_MACINFO_SECTION
3972 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3973 #endif
3974 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3975 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3976 #endif
3977 #ifndef DEBUG_MACRO_SECTION
3978 #define DEBUG_MACRO_SECTION ".debug_macro"
3979 #endif
3980 #ifndef DEBUG_LTO_MACRO_SECTION
3981 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3982 #endif
3983 #ifndef DEBUG_DWO_MACRO_SECTION
3984 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3985 #endif
3986 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3987 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3988 #endif
3989 #ifndef DEBUG_LINE_SECTION
3990 #define DEBUG_LINE_SECTION ".debug_line"
3991 #endif
3992 #ifndef DEBUG_LTO_LINE_SECTION
3993 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3994 #endif
3995 #ifndef DEBUG_DWO_LINE_SECTION
3996 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3997 #endif
3998 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3999 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
4000 #endif
4001 #ifndef DEBUG_LOC_SECTION
4002 #define DEBUG_LOC_SECTION ".debug_loc"
4003 #endif
4004 #ifndef DEBUG_DWO_LOC_SECTION
4005 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
4006 #endif
4007 #ifndef DEBUG_LOCLISTS_SECTION
4008 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
4009 #endif
4010 #ifndef DEBUG_DWO_LOCLISTS_SECTION
4011 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
4012 #endif
4013 #ifndef DEBUG_PUBNAMES_SECTION
4014 #define DEBUG_PUBNAMES_SECTION \
4015 ((debug_generate_pub_sections == 2) \
4016 ? ".debug_gnu_pubnames" : ".debug_pubnames")
4017 #endif
4018 #ifndef DEBUG_PUBTYPES_SECTION
4019 #define DEBUG_PUBTYPES_SECTION \
4020 ((debug_generate_pub_sections == 2) \
4021 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
4022 #endif
4023 #ifndef DEBUG_STR_OFFSETS_SECTION
4024 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
4025 #endif
4026 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
4027 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
4028 #endif
4029 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
4030 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
4031 #endif
4032 #ifndef DEBUG_STR_SECTION
4033 #define DEBUG_STR_SECTION ".debug_str"
4034 #endif
4035 #ifndef DEBUG_LTO_STR_SECTION
4036 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
4037 #endif
4038 #ifndef DEBUG_STR_DWO_SECTION
4039 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
4040 #endif
4041 #ifndef DEBUG_LTO_STR_DWO_SECTION
4042 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
4043 #endif
4044 #ifndef DEBUG_RANGES_SECTION
4045 #define DEBUG_RANGES_SECTION ".debug_ranges"
4046 #endif
4047 #ifndef DEBUG_RNGLISTS_SECTION
4048 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
4049 #endif
4050 #ifndef DEBUG_LINE_STR_SECTION
4051 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
4052 #endif
4053 #ifndef DEBUG_LTO_LINE_STR_SECTION
4054 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
4055 #endif
4057 /* Standard ELF section names for compiled code and data. */
4058 #ifndef TEXT_SECTION_NAME
4059 #define TEXT_SECTION_NAME ".text"
4060 #endif
4062 /* Section flags for .debug_str section. */
4063 #define DEBUG_STR_SECTION_FLAGS \
4064 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
4065 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
4066 : SECTION_DEBUG)
4068 /* Section flags for .debug_str.dwo section. */
4069 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
4071 /* Attribute used to refer to the macro section. */
4072 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
4073 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
4075 /* Labels we insert at beginning sections we can reference instead of
4076 the section names themselves. */
4078 #ifndef TEXT_SECTION_LABEL
4079 #define TEXT_SECTION_LABEL "Ltext"
4080 #endif
4081 #ifndef COLD_TEXT_SECTION_LABEL
4082 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
4083 #endif
4084 #ifndef DEBUG_LINE_SECTION_LABEL
4085 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
4086 #endif
4087 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
4088 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
4089 #endif
4090 #ifndef DEBUG_INFO_SECTION_LABEL
4091 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
4092 #endif
4093 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
4094 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
4095 #endif
4096 #ifndef DEBUG_ABBREV_SECTION_LABEL
4097 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
4098 #endif
4099 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
4100 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
4101 #endif
4102 #ifndef DEBUG_ADDR_SECTION_LABEL
4103 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
4104 #endif
4105 #ifndef DEBUG_LOC_SECTION_LABEL
4106 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
4107 #endif
4108 #ifndef DEBUG_RANGES_SECTION_LABEL
4109 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
4110 #endif
4111 #ifndef DEBUG_MACINFO_SECTION_LABEL
4112 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
4113 #endif
4114 #ifndef DEBUG_MACRO_SECTION_LABEL
4115 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
4116 #endif
4117 #define SKELETON_COMP_DIE_ABBREV 1
4118 #define SKELETON_TYPE_DIE_ABBREV 2
4120 /* Definitions of defaults for formats and names of various special
4121 (artificial) labels which may be generated within this file (when the -g
4122 options is used and DWARF2_DEBUGGING_INFO is in effect.
4123 If necessary, these may be overridden from within the tm.h file, but
4124 typically, overriding these defaults is unnecessary. */
4126 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4127 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4128 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4129 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4130 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4131 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4132 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4133 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4134 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4135 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4136 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4137 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4138 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4139 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4140 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4142 #ifndef TEXT_END_LABEL
4143 #define TEXT_END_LABEL "Letext"
4144 #endif
4145 #ifndef COLD_END_LABEL
4146 #define COLD_END_LABEL "Letext_cold"
4147 #endif
4148 #ifndef BLOCK_BEGIN_LABEL
4149 #define BLOCK_BEGIN_LABEL "LBB"
4150 #endif
4151 #ifndef BLOCK_INLINE_ENTRY_LABEL
4152 #define BLOCK_INLINE_ENTRY_LABEL "LBI"
4153 #endif
4154 #ifndef BLOCK_END_LABEL
4155 #define BLOCK_END_LABEL "LBE"
4156 #endif
4157 #ifndef LINE_CODE_LABEL
4158 #define LINE_CODE_LABEL "LM"
4159 #endif
4162 /* Return the root of the DIE's built for the current compilation unit. */
4163 static dw_die_ref
4164 comp_unit_die (void)
4166 if (!single_comp_unit_die)
4167 single_comp_unit_die = gen_compile_unit_die (NULL);
4168 return single_comp_unit_die;
4171 /* We allow a language front-end to designate a function that is to be
4172 called to "demangle" any name before it is put into a DIE. */
4174 static const char *(*demangle_name_func) (const char *);
4176 void
4177 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4179 demangle_name_func = func;
4182 /* Test if rtl node points to a pseudo register. */
4184 static inline int
4185 is_pseudo_reg (const_rtx rtl)
4187 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4188 || (GET_CODE (rtl) == SUBREG
4189 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4192 /* Return a reference to a type, with its const and volatile qualifiers
4193 removed. */
4195 static inline tree
4196 type_main_variant (tree type)
4198 type = TYPE_MAIN_VARIANT (type);
4200 /* ??? There really should be only one main variant among any group of
4201 variants of a given type (and all of the MAIN_VARIANT values for all
4202 members of the group should point to that one type) but sometimes the C
4203 front-end messes this up for array types, so we work around that bug
4204 here. */
4205 if (TREE_CODE (type) == ARRAY_TYPE)
4206 while (type != TYPE_MAIN_VARIANT (type))
4207 type = TYPE_MAIN_VARIANT (type);
4209 return type;
4212 /* Return nonzero if the given type node represents a tagged type. */
4214 static inline int
4215 is_tagged_type (const_tree type)
4217 enum tree_code code = TREE_CODE (type);
4219 return (code == RECORD_TYPE || code == UNION_TYPE
4220 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4223 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4225 static void
4226 get_ref_die_offset_label (char *label, dw_die_ref ref)
4228 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4231 /* Return die_offset of a DIE reference to a base type. */
4233 static unsigned long int
4234 get_base_type_offset (dw_die_ref ref)
4236 if (ref->die_offset)
4237 return ref->die_offset;
4238 if (comp_unit_die ()->die_abbrev)
4240 calc_base_type_die_sizes ();
4241 gcc_assert (ref->die_offset);
4243 return ref->die_offset;
4246 /* Return die_offset of a DIE reference other than base type. */
4248 static unsigned long int
4249 get_ref_die_offset (dw_die_ref ref)
4251 gcc_assert (ref->die_offset);
4252 return ref->die_offset;
4255 /* Convert a DIE tag into its string name. */
4257 static const char *
4258 dwarf_tag_name (unsigned int tag)
4260 const char *name = get_DW_TAG_name (tag);
4262 if (name != NULL)
4263 return name;
4265 return "DW_TAG_<unknown>";
4268 /* Convert a DWARF attribute code into its string name. */
4270 static const char *
4271 dwarf_attr_name (unsigned int attr)
4273 const char *name;
4275 switch (attr)
4277 #if VMS_DEBUGGING_INFO
4278 case DW_AT_HP_prologue:
4279 return "DW_AT_HP_prologue";
4280 #else
4281 case DW_AT_MIPS_loop_unroll_factor:
4282 return "DW_AT_MIPS_loop_unroll_factor";
4283 #endif
4285 #if VMS_DEBUGGING_INFO
4286 case DW_AT_HP_epilogue:
4287 return "DW_AT_HP_epilogue";
4288 #else
4289 case DW_AT_MIPS_stride:
4290 return "DW_AT_MIPS_stride";
4291 #endif
4294 name = get_DW_AT_name (attr);
4296 if (name != NULL)
4297 return name;
4299 return "DW_AT_<unknown>";
4302 /* Convert a DWARF value form code into its string name. */
4304 static const char *
4305 dwarf_form_name (unsigned int form)
4307 const char *name = get_DW_FORM_name (form);
4309 if (name != NULL)
4310 return name;
4312 return "DW_FORM_<unknown>";
4315 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4316 instance of an inlined instance of a decl which is local to an inline
4317 function, so we have to trace all of the way back through the origin chain
4318 to find out what sort of node actually served as the original seed for the
4319 given block. */
4321 static tree
4322 decl_ultimate_origin (const_tree decl)
4324 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4325 return NULL_TREE;
4327 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4328 we're trying to output the abstract instance of this function. */
4329 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4330 return NULL_TREE;
4332 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4333 most distant ancestor, this should never happen. */
4334 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4336 return DECL_ABSTRACT_ORIGIN (decl);
4339 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4340 of a virtual function may refer to a base class, so we check the 'this'
4341 parameter. */
4343 static tree
4344 decl_class_context (tree decl)
4346 tree context = NULL_TREE;
4348 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4349 context = DECL_CONTEXT (decl);
4350 else
4351 context = TYPE_MAIN_VARIANT
4352 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4354 if (context && !TYPE_P (context))
4355 context = NULL_TREE;
4357 return context;
4360 /* Add an attribute/value pair to a DIE. */
4362 static inline void
4363 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4365 /* Maybe this should be an assert? */
4366 if (die == NULL)
4367 return;
4369 if (flag_checking)
4371 /* Check we do not add duplicate attrs. Can't use get_AT here
4372 because that recurses to the specification/abstract origin DIE. */
4373 dw_attr_node *a;
4374 unsigned ix;
4375 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4376 gcc_assert (a->dw_attr != attr->dw_attr);
4379 vec_safe_reserve (die->die_attr, 1);
4380 vec_safe_push (die->die_attr, *attr);
4383 static inline enum dw_val_class
4384 AT_class (dw_attr_node *a)
4386 return a->dw_attr_val.val_class;
4389 /* Return the index for any attribute that will be referenced with a
4390 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4391 are stored in dw_attr_val.v.val_str for reference counting
4392 pruning. */
4394 static inline unsigned int
4395 AT_index (dw_attr_node *a)
4397 if (AT_class (a) == dw_val_class_str)
4398 return a->dw_attr_val.v.val_str->index;
4399 else if (a->dw_attr_val.val_entry != NULL)
4400 return a->dw_attr_val.val_entry->index;
4401 return NOT_INDEXED;
4404 /* Add a flag value attribute to a DIE. */
4406 static inline void
4407 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4409 dw_attr_node attr;
4411 attr.dw_attr = attr_kind;
4412 attr.dw_attr_val.val_class = dw_val_class_flag;
4413 attr.dw_attr_val.val_entry = NULL;
4414 attr.dw_attr_val.v.val_flag = flag;
4415 add_dwarf_attr (die, &attr);
4418 static inline unsigned
4419 AT_flag (dw_attr_node *a)
4421 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4422 return a->dw_attr_val.v.val_flag;
4425 /* Add a signed integer attribute value to a DIE. */
4427 static inline void
4428 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4430 dw_attr_node attr;
4432 attr.dw_attr = attr_kind;
4433 attr.dw_attr_val.val_class = dw_val_class_const;
4434 attr.dw_attr_val.val_entry = NULL;
4435 attr.dw_attr_val.v.val_int = int_val;
4436 add_dwarf_attr (die, &attr);
4439 static inline HOST_WIDE_INT
4440 AT_int (dw_attr_node *a)
4442 gcc_assert (a && (AT_class (a) == dw_val_class_const
4443 || AT_class (a) == dw_val_class_const_implicit));
4444 return a->dw_attr_val.v.val_int;
4447 /* Add an unsigned integer attribute value to a DIE. */
4449 static inline void
4450 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4451 unsigned HOST_WIDE_INT unsigned_val)
4453 dw_attr_node attr;
4455 attr.dw_attr = attr_kind;
4456 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4457 attr.dw_attr_val.val_entry = NULL;
4458 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4459 add_dwarf_attr (die, &attr);
4462 static inline unsigned HOST_WIDE_INT
4463 AT_unsigned (dw_attr_node *a)
4465 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4466 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4467 return a->dw_attr_val.v.val_unsigned;
4470 /* Add an unsigned wide integer attribute value to a DIE. */
4472 static inline void
4473 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4474 const wide_int& w)
4476 dw_attr_node attr;
4478 attr.dw_attr = attr_kind;
4479 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4480 attr.dw_attr_val.val_entry = NULL;
4481 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4482 *attr.dw_attr_val.v.val_wide = w;
4483 add_dwarf_attr (die, &attr);
4486 /* Add an unsigned double integer attribute value to a DIE. */
4488 static inline void
4489 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4490 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4492 dw_attr_node attr;
4494 attr.dw_attr = attr_kind;
4495 attr.dw_attr_val.val_class = dw_val_class_const_double;
4496 attr.dw_attr_val.val_entry = NULL;
4497 attr.dw_attr_val.v.val_double.high = high;
4498 attr.dw_attr_val.v.val_double.low = low;
4499 add_dwarf_attr (die, &attr);
4502 /* Add a floating point attribute value to a DIE and return it. */
4504 static inline void
4505 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4506 unsigned int length, unsigned int elt_size, unsigned char *array)
4508 dw_attr_node attr;
4510 attr.dw_attr = attr_kind;
4511 attr.dw_attr_val.val_class = dw_val_class_vec;
4512 attr.dw_attr_val.val_entry = NULL;
4513 attr.dw_attr_val.v.val_vec.length = length;
4514 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4515 attr.dw_attr_val.v.val_vec.array = array;
4516 add_dwarf_attr (die, &attr);
4519 /* Add an 8-byte data attribute value to a DIE. */
4521 static inline void
4522 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4523 unsigned char data8[8])
4525 dw_attr_node attr;
4527 attr.dw_attr = attr_kind;
4528 attr.dw_attr_val.val_class = dw_val_class_data8;
4529 attr.dw_attr_val.val_entry = NULL;
4530 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4531 add_dwarf_attr (die, &attr);
4534 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4535 dwarf_split_debug_info, address attributes in dies destined for the
4536 final executable have force_direct set to avoid using indexed
4537 references. */
4539 static inline void
4540 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4541 bool force_direct)
4543 dw_attr_node attr;
4544 char * lbl_id;
4546 lbl_id = xstrdup (lbl_low);
4547 attr.dw_attr = DW_AT_low_pc;
4548 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4549 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4550 if (dwarf_split_debug_info && !force_direct)
4551 attr.dw_attr_val.val_entry
4552 = add_addr_table_entry (lbl_id, ate_kind_label);
4553 else
4554 attr.dw_attr_val.val_entry = NULL;
4555 add_dwarf_attr (die, &attr);
4557 attr.dw_attr = DW_AT_high_pc;
4558 if (dwarf_version < 4)
4559 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4560 else
4561 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4562 lbl_id = xstrdup (lbl_high);
4563 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4564 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4565 && dwarf_split_debug_info && !force_direct)
4566 attr.dw_attr_val.val_entry
4567 = add_addr_table_entry (lbl_id, ate_kind_label);
4568 else
4569 attr.dw_attr_val.val_entry = NULL;
4570 add_dwarf_attr (die, &attr);
4573 /* Hash and equality functions for debug_str_hash. */
4575 hashval_t
4576 indirect_string_hasher::hash (indirect_string_node *x)
4578 return htab_hash_string (x->str);
4581 bool
4582 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4584 return strcmp (x1->str, x2) == 0;
4587 /* Add STR to the given string hash table. */
4589 static struct indirect_string_node *
4590 find_AT_string_in_table (const char *str,
4591 hash_table<indirect_string_hasher> *table)
4593 struct indirect_string_node *node;
4595 indirect_string_node **slot
4596 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4597 if (*slot == NULL)
4599 node = ggc_cleared_alloc<indirect_string_node> ();
4600 node->str = ggc_strdup (str);
4601 *slot = node;
4603 else
4604 node = *slot;
4606 node->refcount++;
4607 return node;
4610 /* Add STR to the indirect string hash table. */
4612 static struct indirect_string_node *
4613 find_AT_string (const char *str)
4615 if (! debug_str_hash)
4616 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4618 return find_AT_string_in_table (str, debug_str_hash);
4621 /* Add a string attribute value to a DIE. */
4623 static inline void
4624 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4626 dw_attr_node attr;
4627 struct indirect_string_node *node;
4629 node = find_AT_string (str);
4631 attr.dw_attr = attr_kind;
4632 attr.dw_attr_val.val_class = dw_val_class_str;
4633 attr.dw_attr_val.val_entry = NULL;
4634 attr.dw_attr_val.v.val_str = node;
4635 add_dwarf_attr (die, &attr);
4638 static inline const char *
4639 AT_string (dw_attr_node *a)
4641 gcc_assert (a && AT_class (a) == dw_val_class_str);
4642 return a->dw_attr_val.v.val_str->str;
4645 /* Call this function directly to bypass AT_string_form's logic to put
4646 the string inline in the die. */
4648 static void
4649 set_indirect_string (struct indirect_string_node *node)
4651 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4652 /* Already indirect is a no op. */
4653 if (node->form == DW_FORM_strp
4654 || node->form == DW_FORM_line_strp
4655 || node->form == DW_FORM_GNU_str_index)
4657 gcc_assert (node->label);
4658 return;
4660 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4661 ++dw2_string_counter;
4662 node->label = xstrdup (label);
4664 if (!dwarf_split_debug_info)
4666 node->form = DW_FORM_strp;
4667 node->index = NOT_INDEXED;
4669 else
4671 node->form = DW_FORM_GNU_str_index;
4672 node->index = NO_INDEX_ASSIGNED;
4676 /* A helper function for dwarf2out_finish, called to reset indirect
4677 string decisions done for early LTO dwarf output before fat object
4678 dwarf output. */
4681 reset_indirect_string (indirect_string_node **h, void *)
4683 struct indirect_string_node *node = *h;
4684 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4686 free (node->label);
4687 node->label = NULL;
4688 node->form = (dwarf_form) 0;
4689 node->index = 0;
4691 return 1;
4694 /* Find out whether a string should be output inline in DIE
4695 or out-of-line in .debug_str section. */
4697 static enum dwarf_form
4698 find_string_form (struct indirect_string_node *node)
4700 unsigned int len;
4702 if (node->form)
4703 return node->form;
4705 len = strlen (node->str) + 1;
4707 /* If the string is shorter or equal to the size of the reference, it is
4708 always better to put it inline. */
4709 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4710 return node->form = DW_FORM_string;
4712 /* If we cannot expect the linker to merge strings in .debug_str
4713 section, only put it into .debug_str if it is worth even in this
4714 single module. */
4715 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4716 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4717 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4718 return node->form = DW_FORM_string;
4720 set_indirect_string (node);
4722 return node->form;
4725 /* Find out whether the string referenced from the attribute should be
4726 output inline in DIE or out-of-line in .debug_str section. */
4728 static enum dwarf_form
4729 AT_string_form (dw_attr_node *a)
4731 gcc_assert (a && AT_class (a) == dw_val_class_str);
4732 return find_string_form (a->dw_attr_val.v.val_str);
4735 /* Add a DIE reference attribute value to a DIE. */
4737 static inline void
4738 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4740 dw_attr_node attr;
4741 gcc_checking_assert (targ_die != NULL);
4743 /* With LTO we can end up trying to reference something we didn't create
4744 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4745 if (targ_die == NULL)
4746 return;
4748 attr.dw_attr = attr_kind;
4749 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4750 attr.dw_attr_val.val_entry = NULL;
4751 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4752 attr.dw_attr_val.v.val_die_ref.external = 0;
4753 add_dwarf_attr (die, &attr);
4756 /* Change DIE reference REF to point to NEW_DIE instead. */
4758 static inline void
4759 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4761 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4762 ref->dw_attr_val.v.val_die_ref.die = new_die;
4763 ref->dw_attr_val.v.val_die_ref.external = 0;
4766 /* Add an AT_specification attribute to a DIE, and also make the back
4767 pointer from the specification to the definition. */
4769 static inline void
4770 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4772 add_AT_die_ref (die, DW_AT_specification, targ_die);
4773 gcc_assert (!targ_die->die_definition);
4774 targ_die->die_definition = die;
4777 static inline dw_die_ref
4778 AT_ref (dw_attr_node *a)
4780 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4781 return a->dw_attr_val.v.val_die_ref.die;
4784 static inline int
4785 AT_ref_external (dw_attr_node *a)
4787 if (a && AT_class (a) == dw_val_class_die_ref)
4788 return a->dw_attr_val.v.val_die_ref.external;
4790 return 0;
4793 static inline void
4794 set_AT_ref_external (dw_attr_node *a, int i)
4796 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4797 a->dw_attr_val.v.val_die_ref.external = i;
4800 /* Add an FDE reference attribute value to a DIE. */
4802 static inline void
4803 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4805 dw_attr_node attr;
4807 attr.dw_attr = attr_kind;
4808 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4809 attr.dw_attr_val.val_entry = NULL;
4810 attr.dw_attr_val.v.val_fde_index = targ_fde;
4811 add_dwarf_attr (die, &attr);
4814 /* Add a location description attribute value to a DIE. */
4816 static inline void
4817 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4819 dw_attr_node attr;
4821 attr.dw_attr = attr_kind;
4822 attr.dw_attr_val.val_class = dw_val_class_loc;
4823 attr.dw_attr_val.val_entry = NULL;
4824 attr.dw_attr_val.v.val_loc = loc;
4825 add_dwarf_attr (die, &attr);
4828 static inline dw_loc_descr_ref
4829 AT_loc (dw_attr_node *a)
4831 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4832 return a->dw_attr_val.v.val_loc;
4835 static inline void
4836 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4838 dw_attr_node attr;
4840 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4841 return;
4843 attr.dw_attr = attr_kind;
4844 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4845 attr.dw_attr_val.val_entry = NULL;
4846 attr.dw_attr_val.v.val_loc_list = loc_list;
4847 add_dwarf_attr (die, &attr);
4848 have_location_lists = true;
4851 static inline dw_loc_list_ref
4852 AT_loc_list (dw_attr_node *a)
4854 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4855 return a->dw_attr_val.v.val_loc_list;
4858 /* Add a view list attribute to DIE. It must have a DW_AT_location
4859 attribute, because the view list complements the location list. */
4861 static inline void
4862 add_AT_view_list (dw_die_ref die, enum dwarf_attribute attr_kind)
4864 dw_attr_node attr;
4866 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4867 return;
4869 attr.dw_attr = attr_kind;
4870 attr.dw_attr_val.val_class = dw_val_class_view_list;
4871 attr.dw_attr_val.val_entry = NULL;
4872 attr.dw_attr_val.v.val_view_list = die;
4873 add_dwarf_attr (die, &attr);
4874 gcc_checking_assert (get_AT (die, DW_AT_location));
4875 gcc_assert (have_location_lists);
4878 /* Return a pointer to the location list referenced by the attribute.
4879 If the named attribute is a view list, look up the corresponding
4880 DW_AT_location attribute and return its location list. */
4882 static inline dw_loc_list_ref *
4883 AT_loc_list_ptr (dw_attr_node *a)
4885 gcc_assert (a);
4886 switch (AT_class (a))
4888 case dw_val_class_loc_list:
4889 return &a->dw_attr_val.v.val_loc_list;
4890 case dw_val_class_view_list:
4892 dw_attr_node *l;
4893 l = get_AT (a->dw_attr_val.v.val_view_list, DW_AT_location);
4894 if (!l)
4895 return NULL;
4896 gcc_checking_assert (l + 1 == a);
4897 return AT_loc_list_ptr (l);
4899 default:
4900 gcc_unreachable ();
4904 /* Return the location attribute value associated with a view list
4905 attribute value. */
4907 static inline dw_val_node *
4908 view_list_to_loc_list_val_node (dw_val_node *val)
4910 gcc_assert (val->val_class == dw_val_class_view_list);
4911 dw_attr_node *loc = get_AT (val->v.val_view_list, DW_AT_location);
4912 if (!loc)
4913 return NULL;
4914 gcc_checking_assert (&(loc + 1)->dw_attr_val == val);
4915 gcc_assert (AT_class (loc) == dw_val_class_loc_list);
4916 return &loc->dw_attr_val;
4919 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4921 static hashval_t hash (addr_table_entry *);
4922 static bool equal (addr_table_entry *, addr_table_entry *);
4925 /* Table of entries into the .debug_addr section. */
4927 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4929 /* Hash an address_table_entry. */
4931 hashval_t
4932 addr_hasher::hash (addr_table_entry *a)
4934 inchash::hash hstate;
4935 switch (a->kind)
4937 case ate_kind_rtx:
4938 hstate.add_int (0);
4939 break;
4940 case ate_kind_rtx_dtprel:
4941 hstate.add_int (1);
4942 break;
4943 case ate_kind_label:
4944 return htab_hash_string (a->addr.label);
4945 default:
4946 gcc_unreachable ();
4948 inchash::add_rtx (a->addr.rtl, hstate);
4949 return hstate.end ();
4952 /* Determine equality for two address_table_entries. */
4954 bool
4955 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4957 if (a1->kind != a2->kind)
4958 return 0;
4959 switch (a1->kind)
4961 case ate_kind_rtx:
4962 case ate_kind_rtx_dtprel:
4963 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4964 case ate_kind_label:
4965 return strcmp (a1->addr.label, a2->addr.label) == 0;
4966 default:
4967 gcc_unreachable ();
4971 /* Initialize an addr_table_entry. */
4973 void
4974 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4976 e->kind = kind;
4977 switch (kind)
4979 case ate_kind_rtx:
4980 case ate_kind_rtx_dtprel:
4981 e->addr.rtl = (rtx) addr;
4982 break;
4983 case ate_kind_label:
4984 e->addr.label = (char *) addr;
4985 break;
4987 e->refcount = 0;
4988 e->index = NO_INDEX_ASSIGNED;
4991 /* Add attr to the address table entry to the table. Defer setting an
4992 index until output time. */
4994 static addr_table_entry *
4995 add_addr_table_entry (void *addr, enum ate_kind kind)
4997 addr_table_entry *node;
4998 addr_table_entry finder;
5000 gcc_assert (dwarf_split_debug_info);
5001 if (! addr_index_table)
5002 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
5003 init_addr_table_entry (&finder, kind, addr);
5004 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
5006 if (*slot == HTAB_EMPTY_ENTRY)
5008 node = ggc_cleared_alloc<addr_table_entry> ();
5009 init_addr_table_entry (node, kind, addr);
5010 *slot = node;
5012 else
5013 node = *slot;
5015 node->refcount++;
5016 return node;
5019 /* Remove an entry from the addr table by decrementing its refcount.
5020 Strictly, decrementing the refcount would be enough, but the
5021 assertion that the entry is actually in the table has found
5022 bugs. */
5024 static void
5025 remove_addr_table_entry (addr_table_entry *entry)
5027 gcc_assert (dwarf_split_debug_info && addr_index_table);
5028 /* After an index is assigned, the table is frozen. */
5029 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
5030 entry->refcount--;
5033 /* Given a location list, remove all addresses it refers to from the
5034 address_table. */
5036 static void
5037 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
5039 for (; descr; descr = descr->dw_loc_next)
5040 if (descr->dw_loc_oprnd1.val_entry != NULL)
5042 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
5043 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
5047 /* A helper function for dwarf2out_finish called through
5048 htab_traverse. Assign an addr_table_entry its index. All entries
5049 must be collected into the table when this function is called,
5050 because the indexing code relies on htab_traverse to traverse nodes
5051 in the same order for each run. */
5054 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
5056 addr_table_entry *node = *h;
5058 /* Don't index unreferenced nodes. */
5059 if (node->refcount == 0)
5060 return 1;
5062 gcc_assert (node->index == NO_INDEX_ASSIGNED);
5063 node->index = *index;
5064 *index += 1;
5066 return 1;
5069 /* Add an address constant attribute value to a DIE. When using
5070 dwarf_split_debug_info, address attributes in dies destined for the
5071 final executable should be direct references--setting the parameter
5072 force_direct ensures this behavior. */
5074 static inline void
5075 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
5076 bool force_direct)
5078 dw_attr_node attr;
5080 attr.dw_attr = attr_kind;
5081 attr.dw_attr_val.val_class = dw_val_class_addr;
5082 attr.dw_attr_val.v.val_addr = addr;
5083 if (dwarf_split_debug_info && !force_direct)
5084 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
5085 else
5086 attr.dw_attr_val.val_entry = NULL;
5087 add_dwarf_attr (die, &attr);
5090 /* Get the RTX from to an address DIE attribute. */
5092 static inline rtx
5093 AT_addr (dw_attr_node *a)
5095 gcc_assert (a && AT_class (a) == dw_val_class_addr);
5096 return a->dw_attr_val.v.val_addr;
5099 /* Add a file attribute value to a DIE. */
5101 static inline void
5102 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5103 struct dwarf_file_data *fd)
5105 dw_attr_node attr;
5107 attr.dw_attr = attr_kind;
5108 attr.dw_attr_val.val_class = dw_val_class_file;
5109 attr.dw_attr_val.val_entry = NULL;
5110 attr.dw_attr_val.v.val_file = fd;
5111 add_dwarf_attr (die, &attr);
5114 /* Get the dwarf_file_data from a file DIE attribute. */
5116 static inline struct dwarf_file_data *
5117 AT_file (dw_attr_node *a)
5119 gcc_assert (a && (AT_class (a) == dw_val_class_file
5120 || AT_class (a) == dw_val_class_file_implicit));
5121 return a->dw_attr_val.v.val_file;
5124 /* Add a vms delta attribute value to a DIE. */
5126 static inline void
5127 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5128 const char *lbl1, const char *lbl2)
5130 dw_attr_node attr;
5132 attr.dw_attr = attr_kind;
5133 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5134 attr.dw_attr_val.val_entry = NULL;
5135 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5136 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5137 add_dwarf_attr (die, &attr);
5140 /* Add a symbolic view identifier attribute value to a DIE. */
5142 static inline void
5143 add_AT_symview (dw_die_ref die, enum dwarf_attribute attr_kind,
5144 const char *view_label)
5146 dw_attr_node attr;
5148 attr.dw_attr = attr_kind;
5149 attr.dw_attr_val.val_class = dw_val_class_symview;
5150 attr.dw_attr_val.val_entry = NULL;
5151 attr.dw_attr_val.v.val_symbolic_view = xstrdup (view_label);
5152 add_dwarf_attr (die, &attr);
5155 /* Add a label identifier attribute value to a DIE. */
5157 static inline void
5158 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
5159 const char *lbl_id)
5161 dw_attr_node attr;
5163 attr.dw_attr = attr_kind;
5164 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5165 attr.dw_attr_val.val_entry = NULL;
5166 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5167 if (dwarf_split_debug_info)
5168 attr.dw_attr_val.val_entry
5169 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
5170 ate_kind_label);
5171 add_dwarf_attr (die, &attr);
5174 /* Add a section offset attribute value to a DIE, an offset into the
5175 debug_line section. */
5177 static inline void
5178 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5179 const char *label)
5181 dw_attr_node attr;
5183 attr.dw_attr = attr_kind;
5184 attr.dw_attr_val.val_class = dw_val_class_lineptr;
5185 attr.dw_attr_val.val_entry = NULL;
5186 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5187 add_dwarf_attr (die, &attr);
5190 /* Add a section offset attribute value to a DIE, an offset into the
5191 debug_loclists section. */
5193 static inline void
5194 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5195 const char *label)
5197 dw_attr_node attr;
5199 attr.dw_attr = attr_kind;
5200 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
5201 attr.dw_attr_val.val_entry = NULL;
5202 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5203 add_dwarf_attr (die, &attr);
5206 /* Add a section offset attribute value to a DIE, an offset into the
5207 debug_macinfo section. */
5209 static inline void
5210 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5211 const char *label)
5213 dw_attr_node attr;
5215 attr.dw_attr = attr_kind;
5216 attr.dw_attr_val.val_class = dw_val_class_macptr;
5217 attr.dw_attr_val.val_entry = NULL;
5218 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5219 add_dwarf_attr (die, &attr);
5222 /* Add an offset attribute value to a DIE. */
5224 static inline void
5225 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5226 unsigned HOST_WIDE_INT offset)
5228 dw_attr_node attr;
5230 attr.dw_attr = attr_kind;
5231 attr.dw_attr_val.val_class = dw_val_class_offset;
5232 attr.dw_attr_val.val_entry = NULL;
5233 attr.dw_attr_val.v.val_offset = offset;
5234 add_dwarf_attr (die, &attr);
5237 /* Add a range_list attribute value to a DIE. When using
5238 dwarf_split_debug_info, address attributes in dies destined for the
5239 final executable should be direct references--setting the parameter
5240 force_direct ensures this behavior. */
5242 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
5243 #define RELOCATED_OFFSET (NULL)
5245 static void
5246 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5247 long unsigned int offset, bool force_direct)
5249 dw_attr_node attr;
5251 attr.dw_attr = attr_kind;
5252 attr.dw_attr_val.val_class = dw_val_class_range_list;
5253 /* For the range_list attribute, use val_entry to store whether the
5254 offset should follow split-debug-info or normal semantics. This
5255 value is read in output_range_list_offset. */
5256 if (dwarf_split_debug_info && !force_direct)
5257 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
5258 else
5259 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
5260 attr.dw_attr_val.v.val_offset = offset;
5261 add_dwarf_attr (die, &attr);
5264 /* Return the start label of a delta attribute. */
5266 static inline const char *
5267 AT_vms_delta1 (dw_attr_node *a)
5269 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5270 return a->dw_attr_val.v.val_vms_delta.lbl1;
5273 /* Return the end label of a delta attribute. */
5275 static inline const char *
5276 AT_vms_delta2 (dw_attr_node *a)
5278 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5279 return a->dw_attr_val.v.val_vms_delta.lbl2;
5282 static inline const char *
5283 AT_lbl (dw_attr_node *a)
5285 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5286 || AT_class (a) == dw_val_class_lineptr
5287 || AT_class (a) == dw_val_class_macptr
5288 || AT_class (a) == dw_val_class_loclistsptr
5289 || AT_class (a) == dw_val_class_high_pc));
5290 return a->dw_attr_val.v.val_lbl_id;
5293 /* Get the attribute of type attr_kind. */
5295 static dw_attr_node *
5296 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5298 dw_attr_node *a;
5299 unsigned ix;
5300 dw_die_ref spec = NULL;
5302 if (! die)
5303 return NULL;
5305 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5306 if (a->dw_attr == attr_kind)
5307 return a;
5308 else if (a->dw_attr == DW_AT_specification
5309 || a->dw_attr == DW_AT_abstract_origin)
5310 spec = AT_ref (a);
5312 if (spec)
5313 return get_AT (spec, attr_kind);
5315 return NULL;
5318 /* Returns the parent of the declaration of DIE. */
5320 static dw_die_ref
5321 get_die_parent (dw_die_ref die)
5323 dw_die_ref t;
5325 if (!die)
5326 return NULL;
5328 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5329 || (t = get_AT_ref (die, DW_AT_specification)))
5330 die = t;
5332 return die->die_parent;
5335 /* Return the "low pc" attribute value, typically associated with a subprogram
5336 DIE. Return null if the "low pc" attribute is either not present, or if it
5337 cannot be represented as an assembler label identifier. */
5339 static inline const char *
5340 get_AT_low_pc (dw_die_ref die)
5342 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5344 return a ? AT_lbl (a) : NULL;
5347 /* Return the "high pc" attribute value, typically associated with a subprogram
5348 DIE. Return null if the "high pc" attribute is either not present, or if it
5349 cannot be represented as an assembler label identifier. */
5351 static inline const char *
5352 get_AT_hi_pc (dw_die_ref die)
5354 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5356 return a ? AT_lbl (a) : NULL;
5359 /* Return the value of the string attribute designated by ATTR_KIND, or
5360 NULL if it is not present. */
5362 static inline const char *
5363 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5365 dw_attr_node *a = get_AT (die, attr_kind);
5367 return a ? AT_string (a) : NULL;
5370 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5371 if it is not present. */
5373 static inline int
5374 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5376 dw_attr_node *a = get_AT (die, attr_kind);
5378 return a ? AT_flag (a) : 0;
5381 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5382 if it is not present. */
5384 static inline unsigned
5385 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5387 dw_attr_node *a = get_AT (die, attr_kind);
5389 return a ? AT_unsigned (a) : 0;
5392 static inline dw_die_ref
5393 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5395 dw_attr_node *a = get_AT (die, attr_kind);
5397 return a ? AT_ref (a) : NULL;
5400 static inline struct dwarf_file_data *
5401 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5403 dw_attr_node *a = get_AT (die, attr_kind);
5405 return a ? AT_file (a) : NULL;
5408 /* Return TRUE if the language is C++. */
5410 static inline bool
5411 is_cxx (void)
5413 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5415 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5416 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5419 /* Return TRUE if DECL was created by the C++ frontend. */
5421 static bool
5422 is_cxx (const_tree decl)
5424 if (in_lto_p)
5426 const_tree context = get_ultimate_context (decl);
5427 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5428 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5430 return is_cxx ();
5433 /* Return TRUE if the language is Fortran. */
5435 static inline bool
5436 is_fortran (void)
5438 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5440 return (lang == DW_LANG_Fortran77
5441 || lang == DW_LANG_Fortran90
5442 || lang == DW_LANG_Fortran95
5443 || lang == DW_LANG_Fortran03
5444 || lang == DW_LANG_Fortran08);
5447 static inline bool
5448 is_fortran (const_tree decl)
5450 if (in_lto_p)
5452 const_tree context = get_ultimate_context (decl);
5453 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5454 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5455 "GNU Fortran", 11) == 0
5456 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5457 "GNU F77") == 0);
5459 return is_fortran ();
5462 /* Return TRUE if the language is Ada. */
5464 static inline bool
5465 is_ada (void)
5467 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5469 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5472 /* Remove the specified attribute if present. Return TRUE if removal
5473 was successful. */
5475 static bool
5476 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5478 dw_attr_node *a;
5479 unsigned ix;
5481 if (! die)
5482 return false;
5484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5485 if (a->dw_attr == attr_kind)
5487 if (AT_class (a) == dw_val_class_str)
5488 if (a->dw_attr_val.v.val_str->refcount)
5489 a->dw_attr_val.v.val_str->refcount--;
5491 /* vec::ordered_remove should help reduce the number of abbrevs
5492 that are needed. */
5493 die->die_attr->ordered_remove (ix);
5494 return true;
5496 return false;
5499 /* Remove CHILD from its parent. PREV must have the property that
5500 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5502 static void
5503 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5505 gcc_assert (child->die_parent == prev->die_parent);
5506 gcc_assert (prev->die_sib == child);
5507 if (prev == child)
5509 gcc_assert (child->die_parent->die_child == child);
5510 prev = NULL;
5512 else
5513 prev->die_sib = child->die_sib;
5514 if (child->die_parent->die_child == child)
5515 child->die_parent->die_child = prev;
5516 child->die_sib = NULL;
5519 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5520 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5522 static void
5523 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5525 dw_die_ref parent = old_child->die_parent;
5527 gcc_assert (parent == prev->die_parent);
5528 gcc_assert (prev->die_sib == old_child);
5530 new_child->die_parent = parent;
5531 if (prev == old_child)
5533 gcc_assert (parent->die_child == old_child);
5534 new_child->die_sib = new_child;
5536 else
5538 prev->die_sib = new_child;
5539 new_child->die_sib = old_child->die_sib;
5541 if (old_child->die_parent->die_child == old_child)
5542 old_child->die_parent->die_child = new_child;
5543 old_child->die_sib = NULL;
5546 /* Move all children from OLD_PARENT to NEW_PARENT. */
5548 static void
5549 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5551 dw_die_ref c;
5552 new_parent->die_child = old_parent->die_child;
5553 old_parent->die_child = NULL;
5554 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5557 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5558 matches TAG. */
5560 static void
5561 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5563 dw_die_ref c;
5565 c = die->die_child;
5566 if (c) do {
5567 dw_die_ref prev = c;
5568 c = c->die_sib;
5569 while (c->die_tag == tag)
5571 remove_child_with_prev (c, prev);
5572 c->die_parent = NULL;
5573 /* Might have removed every child. */
5574 if (die->die_child == NULL)
5575 return;
5576 c = prev->die_sib;
5578 } while (c != die->die_child);
5581 /* Add a CHILD_DIE as the last child of DIE. */
5583 static void
5584 add_child_die (dw_die_ref die, dw_die_ref child_die)
5586 /* FIXME this should probably be an assert. */
5587 if (! die || ! child_die)
5588 return;
5589 gcc_assert (die != child_die);
5591 child_die->die_parent = die;
5592 if (die->die_child)
5594 child_die->die_sib = die->die_child->die_sib;
5595 die->die_child->die_sib = child_die;
5597 else
5598 child_die->die_sib = child_die;
5599 die->die_child = child_die;
5602 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5604 static void
5605 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5606 dw_die_ref after_die)
5608 gcc_assert (die
5609 && child_die
5610 && after_die
5611 && die->die_child
5612 && die != child_die);
5614 child_die->die_parent = die;
5615 child_die->die_sib = after_die->die_sib;
5616 after_die->die_sib = child_die;
5617 if (die->die_child == after_die)
5618 die->die_child = child_die;
5621 /* Unassociate CHILD from its parent, and make its parent be
5622 NEW_PARENT. */
5624 static void
5625 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5627 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5628 if (p->die_sib == child)
5630 remove_child_with_prev (child, p);
5631 break;
5633 add_child_die (new_parent, child);
5636 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5637 is the specification, to the end of PARENT's list of children.
5638 This is done by removing and re-adding it. */
5640 static void
5641 splice_child_die (dw_die_ref parent, dw_die_ref child)
5643 /* We want the declaration DIE from inside the class, not the
5644 specification DIE at toplevel. */
5645 if (child->die_parent != parent)
5647 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5649 if (tmp)
5650 child = tmp;
5653 gcc_assert (child->die_parent == parent
5654 || (child->die_parent
5655 == get_AT_ref (parent, DW_AT_specification)));
5657 reparent_child (child, parent);
5660 /* Create and return a new die with TAG_VALUE as tag. */
5662 static inline dw_die_ref
5663 new_die_raw (enum dwarf_tag tag_value)
5665 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5666 die->die_tag = tag_value;
5667 return die;
5670 /* Create and return a new die with a parent of PARENT_DIE. If
5671 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5672 associated tree T must be supplied to determine parenthood
5673 later. */
5675 static inline dw_die_ref
5676 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5678 dw_die_ref die = new_die_raw (tag_value);
5680 if (parent_die != NULL)
5681 add_child_die (parent_die, die);
5682 else
5684 limbo_die_node *limbo_node;
5686 /* No DIEs created after early dwarf should end up in limbo,
5687 because the limbo list should not persist past LTO
5688 streaming. */
5689 if (tag_value != DW_TAG_compile_unit
5690 /* These are allowed because they're generated while
5691 breaking out COMDAT units late. */
5692 && tag_value != DW_TAG_type_unit
5693 && tag_value != DW_TAG_skeleton_unit
5694 && !early_dwarf
5695 /* Allow nested functions to live in limbo because they will
5696 only temporarily live there, as decls_for_scope will fix
5697 them up. */
5698 && (TREE_CODE (t) != FUNCTION_DECL
5699 || !decl_function_context (t))
5700 /* Same as nested functions above but for types. Types that
5701 are local to a function will be fixed in
5702 decls_for_scope. */
5703 && (!RECORD_OR_UNION_TYPE_P (t)
5704 || !TYPE_CONTEXT (t)
5705 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5706 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5707 especially in the ltrans stage, but once we implement LTO
5708 dwarf streaming, we should remove this exception. */
5709 && !in_lto_p)
5711 fprintf (stderr, "symbol ended up in limbo too late:");
5712 debug_generic_stmt (t);
5713 gcc_unreachable ();
5716 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5717 limbo_node->die = die;
5718 limbo_node->created_for = t;
5719 limbo_node->next = limbo_die_list;
5720 limbo_die_list = limbo_node;
5723 return die;
5726 /* Return the DIE associated with the given type specifier. */
5728 static inline dw_die_ref
5729 lookup_type_die (tree type)
5731 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5732 if (die && die->removed)
5734 TYPE_SYMTAB_DIE (type) = NULL;
5735 return NULL;
5737 return die;
5740 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5741 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5742 anonymous type instead the one of the naming typedef. */
5744 static inline dw_die_ref
5745 strip_naming_typedef (tree type, dw_die_ref type_die)
5747 if (type
5748 && TREE_CODE (type) == RECORD_TYPE
5749 && type_die
5750 && type_die->die_tag == DW_TAG_typedef
5751 && is_naming_typedef_decl (TYPE_NAME (type)))
5752 type_die = get_AT_ref (type_die, DW_AT_type);
5753 return type_die;
5756 /* Like lookup_type_die, but if type is an anonymous type named by a
5757 typedef[1], return the DIE of the anonymous type instead the one of
5758 the naming typedef. This is because in gen_typedef_die, we did
5759 equate the anonymous struct named by the typedef with the DIE of
5760 the naming typedef. So by default, lookup_type_die on an anonymous
5761 struct yields the DIE of the naming typedef.
5763 [1]: Read the comment of is_naming_typedef_decl to learn about what
5764 a naming typedef is. */
5766 static inline dw_die_ref
5767 lookup_type_die_strip_naming_typedef (tree type)
5769 dw_die_ref die = lookup_type_die (type);
5770 return strip_naming_typedef (type, die);
5773 /* Equate a DIE to a given type specifier. */
5775 static inline void
5776 equate_type_number_to_die (tree type, dw_die_ref type_die)
5778 TYPE_SYMTAB_DIE (type) = type_die;
5781 /* Returns a hash value for X (which really is a die_struct). */
5783 inline hashval_t
5784 decl_die_hasher::hash (die_node *x)
5786 return (hashval_t) x->decl_id;
5789 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5791 inline bool
5792 decl_die_hasher::equal (die_node *x, tree y)
5794 return (x->decl_id == DECL_UID (y));
5797 /* Return the DIE associated with a given declaration. */
5799 static inline dw_die_ref
5800 lookup_decl_die (tree decl)
5802 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5803 NO_INSERT);
5804 if (!die)
5805 return NULL;
5806 if ((*die)->removed)
5808 decl_die_table->clear_slot (die);
5809 return NULL;
5811 return *die;
5815 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5816 style reference. Return true if we found one refering to a DIE for
5817 DECL, otherwise return false. */
5819 static bool
5820 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5821 unsigned HOST_WIDE_INT *off)
5823 dw_die_ref die;
5825 if (flag_wpa && !decl_die_table)
5826 return false;
5828 if (TREE_CODE (decl) == BLOCK)
5829 die = BLOCK_DIE (decl);
5830 else
5831 die = lookup_decl_die (decl);
5832 if (!die)
5833 return false;
5835 /* During WPA stage we currently use DIEs to store the
5836 decl <-> label + offset map. That's quite inefficient but it
5837 works for now. */
5838 if (flag_wpa)
5840 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5841 if (!ref)
5843 gcc_assert (die == comp_unit_die ());
5844 return false;
5846 *off = ref->die_offset;
5847 *sym = ref->die_id.die_symbol;
5848 return true;
5851 /* Similar to get_ref_die_offset_label, but using the "correct"
5852 label. */
5853 *off = die->die_offset;
5854 while (die->die_parent)
5855 die = die->die_parent;
5856 /* For the containing CU DIE we compute a die_symbol in
5857 compute_comp_unit_symbol. */
5858 gcc_assert (die->die_tag == DW_TAG_compile_unit
5859 && die->die_id.die_symbol != NULL);
5860 *sym = die->die_id.die_symbol;
5861 return true;
5864 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5866 static void
5867 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5868 const char *symbol, HOST_WIDE_INT offset)
5870 /* Create a fake DIE that contains the reference. Don't use
5871 new_die because we don't want to end up in the limbo list. */
5872 dw_die_ref ref = new_die_raw (die->die_tag);
5873 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5874 ref->die_offset = offset;
5875 ref->with_offset = 1;
5876 add_AT_die_ref (die, attr_kind, ref);
5879 /* Create a DIE for DECL if required and add a reference to a DIE
5880 at SYMBOL + OFFSET which contains attributes dumped early. */
5882 static void
5883 dwarf2out_register_external_die (tree decl, const char *sym,
5884 unsigned HOST_WIDE_INT off)
5886 if (debug_info_level == DINFO_LEVEL_NONE)
5887 return;
5889 if (flag_wpa && !decl_die_table)
5890 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5892 dw_die_ref die
5893 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5894 gcc_assert (!die);
5896 tree ctx;
5897 dw_die_ref parent = NULL;
5898 /* Need to lookup a DIE for the decls context - the containing
5899 function or translation unit. */
5900 if (TREE_CODE (decl) == BLOCK)
5902 ctx = BLOCK_SUPERCONTEXT (decl);
5903 /* ??? We do not output DIEs for all scopes thus skip as
5904 many DIEs as needed. */
5905 while (TREE_CODE (ctx) == BLOCK
5906 && !BLOCK_DIE (ctx))
5907 ctx = BLOCK_SUPERCONTEXT (ctx);
5909 else
5910 ctx = DECL_CONTEXT (decl);
5911 /* Peel types in the context stack. */
5912 while (ctx && TYPE_P (ctx))
5913 ctx = TYPE_CONTEXT (ctx);
5914 /* Likewise namespaces in case we do not want to emit DIEs for them. */
5915 if (debug_info_level <= DINFO_LEVEL_TERSE)
5916 while (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5917 ctx = DECL_CONTEXT (ctx);
5918 if (ctx)
5920 if (TREE_CODE (ctx) == BLOCK)
5921 parent = BLOCK_DIE (ctx);
5922 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5923 /* Keep the 1:1 association during WPA. */
5924 && !flag_wpa)
5925 /* Otherwise all late annotations go to the main CU which
5926 imports the original CUs. */
5927 parent = comp_unit_die ();
5928 else if (TREE_CODE (ctx) == FUNCTION_DECL
5929 && TREE_CODE (decl) != PARM_DECL
5930 && TREE_CODE (decl) != BLOCK)
5931 /* Leave function local entities parent determination to when
5932 we process scope vars. */
5934 else
5935 parent = lookup_decl_die (ctx);
5937 else
5938 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5939 Handle this case gracefully by globalizing stuff. */
5940 parent = comp_unit_die ();
5941 /* Create a DIE "stub". */
5942 switch (TREE_CODE (decl))
5944 case TRANSLATION_UNIT_DECL:
5945 if (! flag_wpa)
5947 die = comp_unit_die ();
5948 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5949 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5950 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5951 to create a DIE for the original CUs. */
5952 return;
5954 /* Keep the 1:1 association during WPA. */
5955 die = new_die (DW_TAG_compile_unit, NULL, decl);
5956 break;
5957 case NAMESPACE_DECL:
5958 if (is_fortran (decl))
5959 die = new_die (DW_TAG_module, parent, decl);
5960 else
5961 die = new_die (DW_TAG_namespace, parent, decl);
5962 break;
5963 case FUNCTION_DECL:
5964 die = new_die (DW_TAG_subprogram, parent, decl);
5965 break;
5966 case VAR_DECL:
5967 die = new_die (DW_TAG_variable, parent, decl);
5968 break;
5969 case RESULT_DECL:
5970 die = new_die (DW_TAG_variable, parent, decl);
5971 break;
5972 case PARM_DECL:
5973 die = new_die (DW_TAG_formal_parameter, parent, decl);
5974 break;
5975 case CONST_DECL:
5976 die = new_die (DW_TAG_constant, parent, decl);
5977 break;
5978 case LABEL_DECL:
5979 die = new_die (DW_TAG_label, parent, decl);
5980 break;
5981 case BLOCK:
5982 die = new_die (DW_TAG_lexical_block, parent, decl);
5983 break;
5984 default:
5985 gcc_unreachable ();
5987 if (TREE_CODE (decl) == BLOCK)
5988 BLOCK_DIE (decl) = die;
5989 else
5990 equate_decl_number_to_die (decl, die);
5992 /* Add a reference to the DIE providing early debug at $sym + off. */
5993 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5996 /* Returns a hash value for X (which really is a var_loc_list). */
5998 inline hashval_t
5999 decl_loc_hasher::hash (var_loc_list *x)
6001 return (hashval_t) x->decl_id;
6004 /* Return nonzero if decl_id of var_loc_list X is the same as
6005 UID of decl *Y. */
6007 inline bool
6008 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
6010 return (x->decl_id == DECL_UID (y));
6013 /* Return the var_loc list associated with a given declaration. */
6015 static inline var_loc_list *
6016 lookup_decl_loc (const_tree decl)
6018 if (!decl_loc_table)
6019 return NULL;
6020 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
6023 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
6025 inline hashval_t
6026 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
6028 return (hashval_t) x->decl_id;
6031 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
6032 UID of decl *Y. */
6034 inline bool
6035 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
6037 return (x->decl_id == DECL_UID (y));
6040 /* Equate a DIE to a particular declaration. */
6042 static void
6043 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6045 unsigned int decl_id = DECL_UID (decl);
6047 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
6048 decl_die->decl_id = decl_id;
6051 /* Return how many bits covers PIECE EXPR_LIST. */
6053 static HOST_WIDE_INT
6054 decl_piece_bitsize (rtx piece)
6056 int ret = (int) GET_MODE (piece);
6057 if (ret)
6058 return ret;
6059 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
6060 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
6061 return INTVAL (XEXP (XEXP (piece, 0), 0));
6064 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
6066 static rtx *
6067 decl_piece_varloc_ptr (rtx piece)
6069 if ((int) GET_MODE (piece))
6070 return &XEXP (piece, 0);
6071 else
6072 return &XEXP (XEXP (piece, 0), 1);
6075 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
6076 Next is the chain of following piece nodes. */
6078 static rtx_expr_list *
6079 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
6081 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
6082 return alloc_EXPR_LIST (bitsize, loc_note, next);
6083 else
6084 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
6085 GEN_INT (bitsize),
6086 loc_note), next);
6089 /* Return rtx that should be stored into loc field for
6090 LOC_NOTE and BITPOS/BITSIZE. */
6092 static rtx
6093 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
6094 HOST_WIDE_INT bitsize)
6096 if (bitsize != -1)
6098 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
6099 if (bitpos != 0)
6100 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
6102 return loc_note;
6105 /* This function either modifies location piece list *DEST in
6106 place (if SRC and INNER is NULL), or copies location piece list
6107 *SRC to *DEST while modifying it. Location BITPOS is modified
6108 to contain LOC_NOTE, any pieces overlapping it are removed resp.
6109 not copied and if needed some padding around it is added.
6110 When modifying in place, DEST should point to EXPR_LIST where
6111 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
6112 to the start of the whole list and INNER points to the EXPR_LIST
6113 where earlier pieces cover PIECE_BITPOS bits. */
6115 static void
6116 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
6117 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
6118 HOST_WIDE_INT bitsize, rtx loc_note)
6120 HOST_WIDE_INT diff;
6121 bool copy = inner != NULL;
6123 if (copy)
6125 /* First copy all nodes preceding the current bitpos. */
6126 while (src != inner)
6128 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6129 decl_piece_bitsize (*src), NULL_RTX);
6130 dest = &XEXP (*dest, 1);
6131 src = &XEXP (*src, 1);
6134 /* Add padding if needed. */
6135 if (bitpos != piece_bitpos)
6137 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
6138 copy ? NULL_RTX : *dest);
6139 dest = &XEXP (*dest, 1);
6141 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
6143 gcc_assert (!copy);
6144 /* A piece with correct bitpos and bitsize already exist,
6145 just update the location for it and return. */
6146 *decl_piece_varloc_ptr (*dest) = loc_note;
6147 return;
6149 /* Add the piece that changed. */
6150 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
6151 dest = &XEXP (*dest, 1);
6152 /* Skip over pieces that overlap it. */
6153 diff = bitpos - piece_bitpos + bitsize;
6154 if (!copy)
6155 src = dest;
6156 while (diff > 0 && *src)
6158 rtx piece = *src;
6159 diff -= decl_piece_bitsize (piece);
6160 if (copy)
6161 src = &XEXP (piece, 1);
6162 else
6164 *src = XEXP (piece, 1);
6165 free_EXPR_LIST_node (piece);
6168 /* Add padding if needed. */
6169 if (diff < 0 && *src)
6171 if (!copy)
6172 dest = src;
6173 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
6174 dest = &XEXP (*dest, 1);
6176 if (!copy)
6177 return;
6178 /* Finally copy all nodes following it. */
6179 while (*src)
6181 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
6182 decl_piece_bitsize (*src), NULL_RTX);
6183 dest = &XEXP (*dest, 1);
6184 src = &XEXP (*src, 1);
6188 /* Add a variable location node to the linked list for DECL. */
6190 static struct var_loc_node *
6191 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label, var_loc_view view)
6193 unsigned int decl_id;
6194 var_loc_list *temp;
6195 struct var_loc_node *loc = NULL;
6196 HOST_WIDE_INT bitsize = -1, bitpos = -1;
6198 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
6200 tree realdecl = DECL_DEBUG_EXPR (decl);
6201 if (handled_component_p (realdecl)
6202 || (TREE_CODE (realdecl) == MEM_REF
6203 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
6205 bool reverse;
6206 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
6207 &bitsize, &reverse);
6208 if (!innerdecl
6209 || !DECL_P (innerdecl)
6210 || DECL_IGNORED_P (innerdecl)
6211 || TREE_STATIC (innerdecl)
6212 || bitsize == 0
6213 || bitpos + bitsize > 256)
6214 return NULL;
6215 decl = innerdecl;
6219 decl_id = DECL_UID (decl);
6220 var_loc_list **slot
6221 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
6222 if (*slot == NULL)
6224 temp = ggc_cleared_alloc<var_loc_list> ();
6225 temp->decl_id = decl_id;
6226 *slot = temp;
6228 else
6229 temp = *slot;
6231 /* For PARM_DECLs try to keep around the original incoming value,
6232 even if that means we'll emit a zero-range .debug_loc entry. */
6233 if (temp->last
6234 && temp->first == temp->last
6235 && TREE_CODE (decl) == PARM_DECL
6236 && NOTE_P (temp->first->loc)
6237 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
6238 && DECL_INCOMING_RTL (decl)
6239 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
6240 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
6241 == GET_CODE (DECL_INCOMING_RTL (decl))
6242 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
6243 && (bitsize != -1
6244 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
6245 NOTE_VAR_LOCATION_LOC (loc_note))
6246 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
6247 != NOTE_VAR_LOCATION_STATUS (loc_note))))
6249 loc = ggc_cleared_alloc<var_loc_node> ();
6250 temp->first->next = loc;
6251 temp->last = loc;
6252 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6254 else if (temp->last)
6256 struct var_loc_node *last = temp->last, *unused = NULL;
6257 rtx *piece_loc = NULL, last_loc_note;
6258 HOST_WIDE_INT piece_bitpos = 0;
6259 if (last->next)
6261 last = last->next;
6262 gcc_assert (last->next == NULL);
6264 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
6266 piece_loc = &last->loc;
6269 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
6270 if (piece_bitpos + cur_bitsize > bitpos)
6271 break;
6272 piece_bitpos += cur_bitsize;
6273 piece_loc = &XEXP (*piece_loc, 1);
6275 while (*piece_loc);
6277 /* TEMP->LAST here is either pointer to the last but one or
6278 last element in the chained list, LAST is pointer to the
6279 last element. */
6280 if (label && strcmp (last->label, label) == 0 && last->view == view)
6282 /* For SRA optimized variables if there weren't any real
6283 insns since last note, just modify the last node. */
6284 if (piece_loc != NULL)
6286 adjust_piece_list (piece_loc, NULL, NULL,
6287 bitpos, piece_bitpos, bitsize, loc_note);
6288 return NULL;
6290 /* If the last note doesn't cover any instructions, remove it. */
6291 if (temp->last != last)
6293 temp->last->next = NULL;
6294 unused = last;
6295 last = temp->last;
6296 gcc_assert (strcmp (last->label, label) != 0 || last->view != view);
6298 else
6300 gcc_assert (temp->first == temp->last
6301 || (temp->first->next == temp->last
6302 && TREE_CODE (decl) == PARM_DECL));
6303 memset (temp->last, '\0', sizeof (*temp->last));
6304 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6305 return temp->last;
6308 if (bitsize == -1 && NOTE_P (last->loc))
6309 last_loc_note = last->loc;
6310 else if (piece_loc != NULL
6311 && *piece_loc != NULL_RTX
6312 && piece_bitpos == bitpos
6313 && decl_piece_bitsize (*piece_loc) == bitsize)
6314 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6315 else
6316 last_loc_note = NULL_RTX;
6317 /* If the current location is the same as the end of the list,
6318 and either both or neither of the locations is uninitialized,
6319 we have nothing to do. */
6320 if (last_loc_note == NULL_RTX
6321 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6322 NOTE_VAR_LOCATION_LOC (loc_note)))
6323 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6324 != NOTE_VAR_LOCATION_STATUS (loc_note))
6325 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6326 == VAR_INIT_STATUS_UNINITIALIZED)
6327 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6328 == VAR_INIT_STATUS_UNINITIALIZED))))
6330 /* Add LOC to the end of list and update LAST. If the last
6331 element of the list has been removed above, reuse its
6332 memory for the new node, otherwise allocate a new one. */
6333 if (unused)
6335 loc = unused;
6336 memset (loc, '\0', sizeof (*loc));
6338 else
6339 loc = ggc_cleared_alloc<var_loc_node> ();
6340 if (bitsize == -1 || piece_loc == NULL)
6341 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6342 else
6343 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6344 bitpos, piece_bitpos, bitsize, loc_note);
6345 last->next = loc;
6346 /* Ensure TEMP->LAST will point either to the new last but one
6347 element of the chain, or to the last element in it. */
6348 if (last != temp->last)
6349 temp->last = last;
6351 else if (unused)
6352 ggc_free (unused);
6354 else
6356 loc = ggc_cleared_alloc<var_loc_node> ();
6357 temp->first = loc;
6358 temp->last = loc;
6359 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6361 return loc;
6364 /* Keep track of the number of spaces used to indent the
6365 output of the debugging routines that print the structure of
6366 the DIE internal representation. */
6367 static int print_indent;
6369 /* Indent the line the number of spaces given by print_indent. */
6371 static inline void
6372 print_spaces (FILE *outfile)
6374 fprintf (outfile, "%*s", print_indent, "");
6377 /* Print a type signature in hex. */
6379 static inline void
6380 print_signature (FILE *outfile, char *sig)
6382 int i;
6384 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6385 fprintf (outfile, "%02x", sig[i] & 0xff);
6388 static inline void
6389 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6391 if (discr_value->pos)
6392 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6393 else
6394 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6397 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6399 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6400 RECURSE, output location descriptor operations. */
6402 static void
6403 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6405 switch (val->val_class)
6407 case dw_val_class_addr:
6408 fprintf (outfile, "address");
6409 break;
6410 case dw_val_class_offset:
6411 fprintf (outfile, "offset");
6412 break;
6413 case dw_val_class_loc:
6414 fprintf (outfile, "location descriptor");
6415 if (val->v.val_loc == NULL)
6416 fprintf (outfile, " -> <null>\n");
6417 else if (recurse)
6419 fprintf (outfile, ":\n");
6420 print_indent += 4;
6421 print_loc_descr (val->v.val_loc, outfile);
6422 print_indent -= 4;
6424 else
6425 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6426 break;
6427 case dw_val_class_loc_list:
6428 fprintf (outfile, "location list -> label:%s",
6429 val->v.val_loc_list->ll_symbol);
6430 break;
6431 case dw_val_class_view_list:
6432 val = view_list_to_loc_list_val_node (val);
6433 fprintf (outfile, "location list with views -> labels:%s and %s",
6434 val->v.val_loc_list->ll_symbol,
6435 val->v.val_loc_list->vl_symbol);
6436 break;
6437 case dw_val_class_range_list:
6438 fprintf (outfile, "range list");
6439 break;
6440 case dw_val_class_const:
6441 case dw_val_class_const_implicit:
6442 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6443 break;
6444 case dw_val_class_unsigned_const:
6445 case dw_val_class_unsigned_const_implicit:
6446 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6447 break;
6448 case dw_val_class_const_double:
6449 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6450 HOST_WIDE_INT_PRINT_UNSIGNED")",
6451 val->v.val_double.high,
6452 val->v.val_double.low);
6453 break;
6454 case dw_val_class_wide_int:
6456 int i = val->v.val_wide->get_len ();
6457 fprintf (outfile, "constant (");
6458 gcc_assert (i > 0);
6459 if (val->v.val_wide->elt (i - 1) == 0)
6460 fprintf (outfile, "0x");
6461 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6462 val->v.val_wide->elt (--i));
6463 while (--i >= 0)
6464 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6465 val->v.val_wide->elt (i));
6466 fprintf (outfile, ")");
6467 break;
6469 case dw_val_class_vec:
6470 fprintf (outfile, "floating-point or vector constant");
6471 break;
6472 case dw_val_class_flag:
6473 fprintf (outfile, "%u", val->v.val_flag);
6474 break;
6475 case dw_val_class_die_ref:
6476 if (val->v.val_die_ref.die != NULL)
6478 dw_die_ref die = val->v.val_die_ref.die;
6480 if (die->comdat_type_p)
6482 fprintf (outfile, "die -> signature: ");
6483 print_signature (outfile,
6484 die->die_id.die_type_node->signature);
6486 else if (die->die_id.die_symbol)
6488 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6489 if (die->with_offset)
6490 fprintf (outfile, " + %ld", die->die_offset);
6492 else
6493 fprintf (outfile, "die -> %ld", die->die_offset);
6494 fprintf (outfile, " (%p)", (void *) die);
6496 else
6497 fprintf (outfile, "die -> <null>");
6498 break;
6499 case dw_val_class_vms_delta:
6500 fprintf (outfile, "delta: @slotcount(%s-%s)",
6501 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6502 break;
6503 case dw_val_class_symview:
6504 fprintf (outfile, "view: %s", val->v.val_symbolic_view);
6505 break;
6506 case dw_val_class_lbl_id:
6507 case dw_val_class_lineptr:
6508 case dw_val_class_macptr:
6509 case dw_val_class_loclistsptr:
6510 case dw_val_class_high_pc:
6511 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6512 break;
6513 case dw_val_class_str:
6514 if (val->v.val_str->str != NULL)
6515 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6516 else
6517 fprintf (outfile, "<null>");
6518 break;
6519 case dw_val_class_file:
6520 case dw_val_class_file_implicit:
6521 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6522 val->v.val_file->emitted_number);
6523 break;
6524 case dw_val_class_data8:
6526 int i;
6528 for (i = 0; i < 8; i++)
6529 fprintf (outfile, "%02x", val->v.val_data8[i]);
6530 break;
6532 case dw_val_class_discr_value:
6533 print_discr_value (outfile, &val->v.val_discr_value);
6534 break;
6535 case dw_val_class_discr_list:
6536 for (dw_discr_list_ref node = val->v.val_discr_list;
6537 node != NULL;
6538 node = node->dw_discr_next)
6540 if (node->dw_discr_range)
6542 fprintf (outfile, " .. ");
6543 print_discr_value (outfile, &node->dw_discr_lower_bound);
6544 print_discr_value (outfile, &node->dw_discr_upper_bound);
6546 else
6547 print_discr_value (outfile, &node->dw_discr_lower_bound);
6549 if (node->dw_discr_next != NULL)
6550 fprintf (outfile, " | ");
6552 default:
6553 break;
6557 /* Likewise, for a DIE attribute. */
6559 static void
6560 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6562 print_dw_val (&a->dw_attr_val, recurse, outfile);
6566 /* Print the list of operands in the LOC location description to OUTFILE. This
6567 routine is a debugging aid only. */
6569 static void
6570 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6572 dw_loc_descr_ref l = loc;
6574 if (loc == NULL)
6576 print_spaces (outfile);
6577 fprintf (outfile, "<null>\n");
6578 return;
6581 for (l = loc; l != NULL; l = l->dw_loc_next)
6583 print_spaces (outfile);
6584 fprintf (outfile, "(%p) %s",
6585 (void *) l,
6586 dwarf_stack_op_name (l->dw_loc_opc));
6587 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6589 fprintf (outfile, " ");
6590 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6592 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6594 fprintf (outfile, ", ");
6595 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6597 fprintf (outfile, "\n");
6601 /* Print the information associated with a given DIE, and its children.
6602 This routine is a debugging aid only. */
6604 static void
6605 print_die (dw_die_ref die, FILE *outfile)
6607 dw_attr_node *a;
6608 dw_die_ref c;
6609 unsigned ix;
6611 print_spaces (outfile);
6612 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6613 die->die_offset, dwarf_tag_name (die->die_tag),
6614 (void*) die);
6615 print_spaces (outfile);
6616 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6617 fprintf (outfile, " offset: %ld", die->die_offset);
6618 fprintf (outfile, " mark: %d\n", die->die_mark);
6620 if (die->comdat_type_p)
6622 print_spaces (outfile);
6623 fprintf (outfile, " signature: ");
6624 print_signature (outfile, die->die_id.die_type_node->signature);
6625 fprintf (outfile, "\n");
6628 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6630 print_spaces (outfile);
6631 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6633 print_attribute (a, true, outfile);
6634 fprintf (outfile, "\n");
6637 if (die->die_child != NULL)
6639 print_indent += 4;
6640 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6641 print_indent -= 4;
6643 if (print_indent == 0)
6644 fprintf (outfile, "\n");
6647 /* Print the list of operations in the LOC location description. */
6649 DEBUG_FUNCTION void
6650 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6652 print_loc_descr (loc, stderr);
6655 /* Print the information collected for a given DIE. */
6657 DEBUG_FUNCTION void
6658 debug_dwarf_die (dw_die_ref die)
6660 print_die (die, stderr);
6663 DEBUG_FUNCTION void
6664 debug (die_struct &ref)
6666 print_die (&ref, stderr);
6669 DEBUG_FUNCTION void
6670 debug (die_struct *ptr)
6672 if (ptr)
6673 debug (*ptr);
6674 else
6675 fprintf (stderr, "<nil>\n");
6679 /* Print all DWARF information collected for the compilation unit.
6680 This routine is a debugging aid only. */
6682 DEBUG_FUNCTION void
6683 debug_dwarf (void)
6685 print_indent = 0;
6686 print_die (comp_unit_die (), stderr);
6689 /* Verify the DIE tree structure. */
6691 DEBUG_FUNCTION void
6692 verify_die (dw_die_ref die)
6694 gcc_assert (!die->die_mark);
6695 if (die->die_parent == NULL
6696 && die->die_sib == NULL)
6697 return;
6698 /* Verify the die_sib list is cyclic. */
6699 dw_die_ref x = die;
6702 x->die_mark = 1;
6703 x = x->die_sib;
6705 while (x && !x->die_mark);
6706 gcc_assert (x == die);
6707 x = die;
6710 /* Verify all dies have the same parent. */
6711 gcc_assert (x->die_parent == die->die_parent);
6712 if (x->die_child)
6714 /* Verify the child has the proper parent and recurse. */
6715 gcc_assert (x->die_child->die_parent == x);
6716 verify_die (x->die_child);
6718 x->die_mark = 0;
6719 x = x->die_sib;
6721 while (x && x->die_mark);
6724 /* Sanity checks on DIEs. */
6726 static void
6727 check_die (dw_die_ref die)
6729 unsigned ix;
6730 dw_attr_node *a;
6731 bool inline_found = false;
6732 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6733 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6734 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6736 switch (a->dw_attr)
6738 case DW_AT_inline:
6739 if (a->dw_attr_val.v.val_unsigned)
6740 inline_found = true;
6741 break;
6742 case DW_AT_location:
6743 ++n_location;
6744 break;
6745 case DW_AT_low_pc:
6746 ++n_low_pc;
6747 break;
6748 case DW_AT_high_pc:
6749 ++n_high_pc;
6750 break;
6751 case DW_AT_artificial:
6752 ++n_artificial;
6753 break;
6754 case DW_AT_decl_column:
6755 ++n_decl_column;
6756 break;
6757 case DW_AT_decl_line:
6758 ++n_decl_line;
6759 break;
6760 case DW_AT_decl_file:
6761 ++n_decl_file;
6762 break;
6763 default:
6764 break;
6767 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6768 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6770 fprintf (stderr, "Duplicate attributes in DIE:\n");
6771 debug_dwarf_die (die);
6772 gcc_unreachable ();
6774 if (inline_found)
6776 /* A debugging information entry that is a member of an abstract
6777 instance tree [that has DW_AT_inline] should not contain any
6778 attributes which describe aspects of the subroutine which vary
6779 between distinct inlined expansions or distinct out-of-line
6780 expansions. */
6781 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6782 gcc_assert (a->dw_attr != DW_AT_low_pc
6783 && a->dw_attr != DW_AT_high_pc
6784 && a->dw_attr != DW_AT_location
6785 && a->dw_attr != DW_AT_frame_base
6786 && a->dw_attr != DW_AT_call_all_calls
6787 && a->dw_attr != DW_AT_GNU_all_call_sites);
6791 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6792 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6793 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6795 /* Calculate the checksum of a location expression. */
6797 static inline void
6798 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6800 int tem;
6801 inchash::hash hstate;
6802 hashval_t hash;
6804 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6805 CHECKSUM (tem);
6806 hash_loc_operands (loc, hstate);
6807 hash = hstate.end();
6808 CHECKSUM (hash);
6811 /* Calculate the checksum of an attribute. */
6813 static void
6814 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6816 dw_loc_descr_ref loc;
6817 rtx r;
6819 CHECKSUM (at->dw_attr);
6821 /* We don't care that this was compiled with a different compiler
6822 snapshot; if the output is the same, that's what matters. */
6823 if (at->dw_attr == DW_AT_producer)
6824 return;
6826 switch (AT_class (at))
6828 case dw_val_class_const:
6829 case dw_val_class_const_implicit:
6830 CHECKSUM (at->dw_attr_val.v.val_int);
6831 break;
6832 case dw_val_class_unsigned_const:
6833 case dw_val_class_unsigned_const_implicit:
6834 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6835 break;
6836 case dw_val_class_const_double:
6837 CHECKSUM (at->dw_attr_val.v.val_double);
6838 break;
6839 case dw_val_class_wide_int:
6840 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6841 get_full_len (*at->dw_attr_val.v.val_wide)
6842 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6843 break;
6844 case dw_val_class_vec:
6845 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6846 (at->dw_attr_val.v.val_vec.length
6847 * at->dw_attr_val.v.val_vec.elt_size));
6848 break;
6849 case dw_val_class_flag:
6850 CHECKSUM (at->dw_attr_val.v.val_flag);
6851 break;
6852 case dw_val_class_str:
6853 CHECKSUM_STRING (AT_string (at));
6854 break;
6856 case dw_val_class_addr:
6857 r = AT_addr (at);
6858 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6859 CHECKSUM_STRING (XSTR (r, 0));
6860 break;
6862 case dw_val_class_offset:
6863 CHECKSUM (at->dw_attr_val.v.val_offset);
6864 break;
6866 case dw_val_class_loc:
6867 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6868 loc_checksum (loc, ctx);
6869 break;
6871 case dw_val_class_die_ref:
6872 die_checksum (AT_ref (at), ctx, mark);
6873 break;
6875 case dw_val_class_fde_ref:
6876 case dw_val_class_vms_delta:
6877 case dw_val_class_symview:
6878 case dw_val_class_lbl_id:
6879 case dw_val_class_lineptr:
6880 case dw_val_class_macptr:
6881 case dw_val_class_loclistsptr:
6882 case dw_val_class_high_pc:
6883 break;
6885 case dw_val_class_file:
6886 case dw_val_class_file_implicit:
6887 CHECKSUM_STRING (AT_file (at)->filename);
6888 break;
6890 case dw_val_class_data8:
6891 CHECKSUM (at->dw_attr_val.v.val_data8);
6892 break;
6894 default:
6895 break;
6899 /* Calculate the checksum of a DIE. */
6901 static void
6902 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6904 dw_die_ref c;
6905 dw_attr_node *a;
6906 unsigned ix;
6908 /* To avoid infinite recursion. */
6909 if (die->die_mark)
6911 CHECKSUM (die->die_mark);
6912 return;
6914 die->die_mark = ++(*mark);
6916 CHECKSUM (die->die_tag);
6918 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6919 attr_checksum (a, ctx, mark);
6921 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6924 #undef CHECKSUM
6925 #undef CHECKSUM_BLOCK
6926 #undef CHECKSUM_STRING
6928 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6929 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6930 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6931 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6932 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6933 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6934 #define CHECKSUM_ATTR(FOO) \
6935 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6937 /* Calculate the checksum of a number in signed LEB128 format. */
6939 static void
6940 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6942 unsigned char byte;
6943 bool more;
6945 while (1)
6947 byte = (value & 0x7f);
6948 value >>= 7;
6949 more = !((value == 0 && (byte & 0x40) == 0)
6950 || (value == -1 && (byte & 0x40) != 0));
6951 if (more)
6952 byte |= 0x80;
6953 CHECKSUM (byte);
6954 if (!more)
6955 break;
6959 /* Calculate the checksum of a number in unsigned LEB128 format. */
6961 static void
6962 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6964 while (1)
6966 unsigned char byte = (value & 0x7f);
6967 value >>= 7;
6968 if (value != 0)
6969 /* More bytes to follow. */
6970 byte |= 0x80;
6971 CHECKSUM (byte);
6972 if (value == 0)
6973 break;
6977 /* Checksum the context of the DIE. This adds the names of any
6978 surrounding namespaces or structures to the checksum. */
6980 static void
6981 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6983 const char *name;
6984 dw_die_ref spec;
6985 int tag = die->die_tag;
6987 if (tag != DW_TAG_namespace
6988 && tag != DW_TAG_structure_type
6989 && tag != DW_TAG_class_type)
6990 return;
6992 name = get_AT_string (die, DW_AT_name);
6994 spec = get_AT_ref (die, DW_AT_specification);
6995 if (spec != NULL)
6996 die = spec;
6998 if (die->die_parent != NULL)
6999 checksum_die_context (die->die_parent, ctx);
7001 CHECKSUM_ULEB128 ('C');
7002 CHECKSUM_ULEB128 (tag);
7003 if (name != NULL)
7004 CHECKSUM_STRING (name);
7007 /* Calculate the checksum of a location expression. */
7009 static inline void
7010 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7012 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
7013 were emitted as a DW_FORM_sdata instead of a location expression. */
7014 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
7016 CHECKSUM_ULEB128 (DW_FORM_sdata);
7017 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
7018 return;
7021 /* Otherwise, just checksum the raw location expression. */
7022 while (loc != NULL)
7024 inchash::hash hstate;
7025 hashval_t hash;
7027 CHECKSUM_ULEB128 (loc->dtprel);
7028 CHECKSUM_ULEB128 (loc->dw_loc_opc);
7029 hash_loc_operands (loc, hstate);
7030 hash = hstate.end ();
7031 CHECKSUM (hash);
7032 loc = loc->dw_loc_next;
7036 /* Calculate the checksum of an attribute. */
7038 static void
7039 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
7040 struct md5_ctx *ctx, int *mark)
7042 dw_loc_descr_ref loc;
7043 rtx r;
7045 if (AT_class (at) == dw_val_class_die_ref)
7047 dw_die_ref target_die = AT_ref (at);
7049 /* For pointer and reference types, we checksum only the (qualified)
7050 name of the target type (if there is a name). For friend entries,
7051 we checksum only the (qualified) name of the target type or function.
7052 This allows the checksum to remain the same whether the target type
7053 is complete or not. */
7054 if ((at->dw_attr == DW_AT_type
7055 && (tag == DW_TAG_pointer_type
7056 || tag == DW_TAG_reference_type
7057 || tag == DW_TAG_rvalue_reference_type
7058 || tag == DW_TAG_ptr_to_member_type))
7059 || (at->dw_attr == DW_AT_friend
7060 && tag == DW_TAG_friend))
7062 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
7064 if (name_attr != NULL)
7066 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7068 if (decl == NULL)
7069 decl = target_die;
7070 CHECKSUM_ULEB128 ('N');
7071 CHECKSUM_ULEB128 (at->dw_attr);
7072 if (decl->die_parent != NULL)
7073 checksum_die_context (decl->die_parent, ctx);
7074 CHECKSUM_ULEB128 ('E');
7075 CHECKSUM_STRING (AT_string (name_attr));
7076 return;
7080 /* For all other references to another DIE, we check to see if the
7081 target DIE has already been visited. If it has, we emit a
7082 backward reference; if not, we descend recursively. */
7083 if (target_die->die_mark > 0)
7085 CHECKSUM_ULEB128 ('R');
7086 CHECKSUM_ULEB128 (at->dw_attr);
7087 CHECKSUM_ULEB128 (target_die->die_mark);
7089 else
7091 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
7093 if (decl == NULL)
7094 decl = target_die;
7095 target_die->die_mark = ++(*mark);
7096 CHECKSUM_ULEB128 ('T');
7097 CHECKSUM_ULEB128 (at->dw_attr);
7098 if (decl->die_parent != NULL)
7099 checksum_die_context (decl->die_parent, ctx);
7100 die_checksum_ordered (target_die, ctx, mark);
7102 return;
7105 CHECKSUM_ULEB128 ('A');
7106 CHECKSUM_ULEB128 (at->dw_attr);
7108 switch (AT_class (at))
7110 case dw_val_class_const:
7111 case dw_val_class_const_implicit:
7112 CHECKSUM_ULEB128 (DW_FORM_sdata);
7113 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
7114 break;
7116 case dw_val_class_unsigned_const:
7117 case dw_val_class_unsigned_const_implicit:
7118 CHECKSUM_ULEB128 (DW_FORM_sdata);
7119 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
7120 break;
7122 case dw_val_class_const_double:
7123 CHECKSUM_ULEB128 (DW_FORM_block);
7124 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
7125 CHECKSUM (at->dw_attr_val.v.val_double);
7126 break;
7128 case dw_val_class_wide_int:
7129 CHECKSUM_ULEB128 (DW_FORM_block);
7130 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
7131 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
7132 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
7133 get_full_len (*at->dw_attr_val.v.val_wide)
7134 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7135 break;
7137 case dw_val_class_vec:
7138 CHECKSUM_ULEB128 (DW_FORM_block);
7139 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
7140 * at->dw_attr_val.v.val_vec.elt_size);
7141 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
7142 (at->dw_attr_val.v.val_vec.length
7143 * at->dw_attr_val.v.val_vec.elt_size));
7144 break;
7146 case dw_val_class_flag:
7147 CHECKSUM_ULEB128 (DW_FORM_flag);
7148 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
7149 break;
7151 case dw_val_class_str:
7152 CHECKSUM_ULEB128 (DW_FORM_string);
7153 CHECKSUM_STRING (AT_string (at));
7154 break;
7156 case dw_val_class_addr:
7157 r = AT_addr (at);
7158 gcc_assert (GET_CODE (r) == SYMBOL_REF);
7159 CHECKSUM_ULEB128 (DW_FORM_string);
7160 CHECKSUM_STRING (XSTR (r, 0));
7161 break;
7163 case dw_val_class_offset:
7164 CHECKSUM_ULEB128 (DW_FORM_sdata);
7165 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
7166 break;
7168 case dw_val_class_loc:
7169 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7170 loc_checksum_ordered (loc, ctx);
7171 break;
7173 case dw_val_class_fde_ref:
7174 case dw_val_class_symview:
7175 case dw_val_class_lbl_id:
7176 case dw_val_class_lineptr:
7177 case dw_val_class_macptr:
7178 case dw_val_class_loclistsptr:
7179 case dw_val_class_high_pc:
7180 break;
7182 case dw_val_class_file:
7183 case dw_val_class_file_implicit:
7184 CHECKSUM_ULEB128 (DW_FORM_string);
7185 CHECKSUM_STRING (AT_file (at)->filename);
7186 break;
7188 case dw_val_class_data8:
7189 CHECKSUM (at->dw_attr_val.v.val_data8);
7190 break;
7192 default:
7193 break;
7197 struct checksum_attributes
7199 dw_attr_node *at_name;
7200 dw_attr_node *at_type;
7201 dw_attr_node *at_friend;
7202 dw_attr_node *at_accessibility;
7203 dw_attr_node *at_address_class;
7204 dw_attr_node *at_alignment;
7205 dw_attr_node *at_allocated;
7206 dw_attr_node *at_artificial;
7207 dw_attr_node *at_associated;
7208 dw_attr_node *at_binary_scale;
7209 dw_attr_node *at_bit_offset;
7210 dw_attr_node *at_bit_size;
7211 dw_attr_node *at_bit_stride;
7212 dw_attr_node *at_byte_size;
7213 dw_attr_node *at_byte_stride;
7214 dw_attr_node *at_const_value;
7215 dw_attr_node *at_containing_type;
7216 dw_attr_node *at_count;
7217 dw_attr_node *at_data_location;
7218 dw_attr_node *at_data_member_location;
7219 dw_attr_node *at_decimal_scale;
7220 dw_attr_node *at_decimal_sign;
7221 dw_attr_node *at_default_value;
7222 dw_attr_node *at_digit_count;
7223 dw_attr_node *at_discr;
7224 dw_attr_node *at_discr_list;
7225 dw_attr_node *at_discr_value;
7226 dw_attr_node *at_encoding;
7227 dw_attr_node *at_endianity;
7228 dw_attr_node *at_explicit;
7229 dw_attr_node *at_is_optional;
7230 dw_attr_node *at_location;
7231 dw_attr_node *at_lower_bound;
7232 dw_attr_node *at_mutable;
7233 dw_attr_node *at_ordering;
7234 dw_attr_node *at_picture_string;
7235 dw_attr_node *at_prototyped;
7236 dw_attr_node *at_small;
7237 dw_attr_node *at_segment;
7238 dw_attr_node *at_string_length;
7239 dw_attr_node *at_string_length_bit_size;
7240 dw_attr_node *at_string_length_byte_size;
7241 dw_attr_node *at_threads_scaled;
7242 dw_attr_node *at_upper_bound;
7243 dw_attr_node *at_use_location;
7244 dw_attr_node *at_use_UTF8;
7245 dw_attr_node *at_variable_parameter;
7246 dw_attr_node *at_virtuality;
7247 dw_attr_node *at_visibility;
7248 dw_attr_node *at_vtable_elem_location;
7251 /* Collect the attributes that we will want to use for the checksum. */
7253 static void
7254 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
7256 dw_attr_node *a;
7257 unsigned ix;
7259 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7261 switch (a->dw_attr)
7263 case DW_AT_name:
7264 attrs->at_name = a;
7265 break;
7266 case DW_AT_type:
7267 attrs->at_type = a;
7268 break;
7269 case DW_AT_friend:
7270 attrs->at_friend = a;
7271 break;
7272 case DW_AT_accessibility:
7273 attrs->at_accessibility = a;
7274 break;
7275 case DW_AT_address_class:
7276 attrs->at_address_class = a;
7277 break;
7278 case DW_AT_alignment:
7279 attrs->at_alignment = a;
7280 break;
7281 case DW_AT_allocated:
7282 attrs->at_allocated = a;
7283 break;
7284 case DW_AT_artificial:
7285 attrs->at_artificial = a;
7286 break;
7287 case DW_AT_associated:
7288 attrs->at_associated = a;
7289 break;
7290 case DW_AT_binary_scale:
7291 attrs->at_binary_scale = a;
7292 break;
7293 case DW_AT_bit_offset:
7294 attrs->at_bit_offset = a;
7295 break;
7296 case DW_AT_bit_size:
7297 attrs->at_bit_size = a;
7298 break;
7299 case DW_AT_bit_stride:
7300 attrs->at_bit_stride = a;
7301 break;
7302 case DW_AT_byte_size:
7303 attrs->at_byte_size = a;
7304 break;
7305 case DW_AT_byte_stride:
7306 attrs->at_byte_stride = a;
7307 break;
7308 case DW_AT_const_value:
7309 attrs->at_const_value = a;
7310 break;
7311 case DW_AT_containing_type:
7312 attrs->at_containing_type = a;
7313 break;
7314 case DW_AT_count:
7315 attrs->at_count = a;
7316 break;
7317 case DW_AT_data_location:
7318 attrs->at_data_location = a;
7319 break;
7320 case DW_AT_data_member_location:
7321 attrs->at_data_member_location = a;
7322 break;
7323 case DW_AT_decimal_scale:
7324 attrs->at_decimal_scale = a;
7325 break;
7326 case DW_AT_decimal_sign:
7327 attrs->at_decimal_sign = a;
7328 break;
7329 case DW_AT_default_value:
7330 attrs->at_default_value = a;
7331 break;
7332 case DW_AT_digit_count:
7333 attrs->at_digit_count = a;
7334 break;
7335 case DW_AT_discr:
7336 attrs->at_discr = a;
7337 break;
7338 case DW_AT_discr_list:
7339 attrs->at_discr_list = a;
7340 break;
7341 case DW_AT_discr_value:
7342 attrs->at_discr_value = a;
7343 break;
7344 case DW_AT_encoding:
7345 attrs->at_encoding = a;
7346 break;
7347 case DW_AT_endianity:
7348 attrs->at_endianity = a;
7349 break;
7350 case DW_AT_explicit:
7351 attrs->at_explicit = a;
7352 break;
7353 case DW_AT_is_optional:
7354 attrs->at_is_optional = a;
7355 break;
7356 case DW_AT_location:
7357 attrs->at_location = a;
7358 break;
7359 case DW_AT_lower_bound:
7360 attrs->at_lower_bound = a;
7361 break;
7362 case DW_AT_mutable:
7363 attrs->at_mutable = a;
7364 break;
7365 case DW_AT_ordering:
7366 attrs->at_ordering = a;
7367 break;
7368 case DW_AT_picture_string:
7369 attrs->at_picture_string = a;
7370 break;
7371 case DW_AT_prototyped:
7372 attrs->at_prototyped = a;
7373 break;
7374 case DW_AT_small:
7375 attrs->at_small = a;
7376 break;
7377 case DW_AT_segment:
7378 attrs->at_segment = a;
7379 break;
7380 case DW_AT_string_length:
7381 attrs->at_string_length = a;
7382 break;
7383 case DW_AT_string_length_bit_size:
7384 attrs->at_string_length_bit_size = a;
7385 break;
7386 case DW_AT_string_length_byte_size:
7387 attrs->at_string_length_byte_size = a;
7388 break;
7389 case DW_AT_threads_scaled:
7390 attrs->at_threads_scaled = a;
7391 break;
7392 case DW_AT_upper_bound:
7393 attrs->at_upper_bound = a;
7394 break;
7395 case DW_AT_use_location:
7396 attrs->at_use_location = a;
7397 break;
7398 case DW_AT_use_UTF8:
7399 attrs->at_use_UTF8 = a;
7400 break;
7401 case DW_AT_variable_parameter:
7402 attrs->at_variable_parameter = a;
7403 break;
7404 case DW_AT_virtuality:
7405 attrs->at_virtuality = a;
7406 break;
7407 case DW_AT_visibility:
7408 attrs->at_visibility = a;
7409 break;
7410 case DW_AT_vtable_elem_location:
7411 attrs->at_vtable_elem_location = a;
7412 break;
7413 default:
7414 break;
7419 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7421 static void
7422 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7424 dw_die_ref c;
7425 dw_die_ref decl;
7426 struct checksum_attributes attrs;
7428 CHECKSUM_ULEB128 ('D');
7429 CHECKSUM_ULEB128 (die->die_tag);
7431 memset (&attrs, 0, sizeof (attrs));
7433 decl = get_AT_ref (die, DW_AT_specification);
7434 if (decl != NULL)
7435 collect_checksum_attributes (&attrs, decl);
7436 collect_checksum_attributes (&attrs, die);
7438 CHECKSUM_ATTR (attrs.at_name);
7439 CHECKSUM_ATTR (attrs.at_accessibility);
7440 CHECKSUM_ATTR (attrs.at_address_class);
7441 CHECKSUM_ATTR (attrs.at_allocated);
7442 CHECKSUM_ATTR (attrs.at_artificial);
7443 CHECKSUM_ATTR (attrs.at_associated);
7444 CHECKSUM_ATTR (attrs.at_binary_scale);
7445 CHECKSUM_ATTR (attrs.at_bit_offset);
7446 CHECKSUM_ATTR (attrs.at_bit_size);
7447 CHECKSUM_ATTR (attrs.at_bit_stride);
7448 CHECKSUM_ATTR (attrs.at_byte_size);
7449 CHECKSUM_ATTR (attrs.at_byte_stride);
7450 CHECKSUM_ATTR (attrs.at_const_value);
7451 CHECKSUM_ATTR (attrs.at_containing_type);
7452 CHECKSUM_ATTR (attrs.at_count);
7453 CHECKSUM_ATTR (attrs.at_data_location);
7454 CHECKSUM_ATTR (attrs.at_data_member_location);
7455 CHECKSUM_ATTR (attrs.at_decimal_scale);
7456 CHECKSUM_ATTR (attrs.at_decimal_sign);
7457 CHECKSUM_ATTR (attrs.at_default_value);
7458 CHECKSUM_ATTR (attrs.at_digit_count);
7459 CHECKSUM_ATTR (attrs.at_discr);
7460 CHECKSUM_ATTR (attrs.at_discr_list);
7461 CHECKSUM_ATTR (attrs.at_discr_value);
7462 CHECKSUM_ATTR (attrs.at_encoding);
7463 CHECKSUM_ATTR (attrs.at_endianity);
7464 CHECKSUM_ATTR (attrs.at_explicit);
7465 CHECKSUM_ATTR (attrs.at_is_optional);
7466 CHECKSUM_ATTR (attrs.at_location);
7467 CHECKSUM_ATTR (attrs.at_lower_bound);
7468 CHECKSUM_ATTR (attrs.at_mutable);
7469 CHECKSUM_ATTR (attrs.at_ordering);
7470 CHECKSUM_ATTR (attrs.at_picture_string);
7471 CHECKSUM_ATTR (attrs.at_prototyped);
7472 CHECKSUM_ATTR (attrs.at_small);
7473 CHECKSUM_ATTR (attrs.at_segment);
7474 CHECKSUM_ATTR (attrs.at_string_length);
7475 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7476 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7477 CHECKSUM_ATTR (attrs.at_threads_scaled);
7478 CHECKSUM_ATTR (attrs.at_upper_bound);
7479 CHECKSUM_ATTR (attrs.at_use_location);
7480 CHECKSUM_ATTR (attrs.at_use_UTF8);
7481 CHECKSUM_ATTR (attrs.at_variable_parameter);
7482 CHECKSUM_ATTR (attrs.at_virtuality);
7483 CHECKSUM_ATTR (attrs.at_visibility);
7484 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7485 CHECKSUM_ATTR (attrs.at_type);
7486 CHECKSUM_ATTR (attrs.at_friend);
7487 CHECKSUM_ATTR (attrs.at_alignment);
7489 /* Checksum the child DIEs. */
7490 c = die->die_child;
7491 if (c) do {
7492 dw_attr_node *name_attr;
7494 c = c->die_sib;
7495 name_attr = get_AT (c, DW_AT_name);
7496 if (is_template_instantiation (c))
7498 /* Ignore instantiations of member type and function templates. */
7500 else if (name_attr != NULL
7501 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7503 /* Use a shallow checksum for named nested types and member
7504 functions. */
7505 CHECKSUM_ULEB128 ('S');
7506 CHECKSUM_ULEB128 (c->die_tag);
7507 CHECKSUM_STRING (AT_string (name_attr));
7509 else
7511 /* Use a deep checksum for other children. */
7512 /* Mark this DIE so it gets processed when unmarking. */
7513 if (c->die_mark == 0)
7514 c->die_mark = -1;
7515 die_checksum_ordered (c, ctx, mark);
7517 } while (c != die->die_child);
7519 CHECKSUM_ULEB128 (0);
7522 /* Add a type name and tag to a hash. */
7523 static void
7524 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7526 CHECKSUM_ULEB128 (tag);
7527 CHECKSUM_STRING (name);
7530 #undef CHECKSUM
7531 #undef CHECKSUM_STRING
7532 #undef CHECKSUM_ATTR
7533 #undef CHECKSUM_LEB128
7534 #undef CHECKSUM_ULEB128
7536 /* Generate the type signature for DIE. This is computed by generating an
7537 MD5 checksum over the DIE's tag, its relevant attributes, and its
7538 children. Attributes that are references to other DIEs are processed
7539 by recursion, using the MARK field to prevent infinite recursion.
7540 If the DIE is nested inside a namespace or another type, we also
7541 need to include that context in the signature. The lower 64 bits
7542 of the resulting MD5 checksum comprise the signature. */
7544 static void
7545 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7547 int mark;
7548 const char *name;
7549 unsigned char checksum[16];
7550 struct md5_ctx ctx;
7551 dw_die_ref decl;
7552 dw_die_ref parent;
7554 name = get_AT_string (die, DW_AT_name);
7555 decl = get_AT_ref (die, DW_AT_specification);
7556 parent = get_die_parent (die);
7558 /* First, compute a signature for just the type name (and its surrounding
7559 context, if any. This is stored in the type unit DIE for link-time
7560 ODR (one-definition rule) checking. */
7562 if (is_cxx () && name != NULL)
7564 md5_init_ctx (&ctx);
7566 /* Checksum the names of surrounding namespaces and structures. */
7567 if (parent != NULL)
7568 checksum_die_context (parent, &ctx);
7570 /* Checksum the current DIE. */
7571 die_odr_checksum (die->die_tag, name, &ctx);
7572 md5_finish_ctx (&ctx, checksum);
7574 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7577 /* Next, compute the complete type signature. */
7579 md5_init_ctx (&ctx);
7580 mark = 1;
7581 die->die_mark = mark;
7583 /* Checksum the names of surrounding namespaces and structures. */
7584 if (parent != NULL)
7585 checksum_die_context (parent, &ctx);
7587 /* Checksum the DIE and its children. */
7588 die_checksum_ordered (die, &ctx, &mark);
7589 unmark_all_dies (die);
7590 md5_finish_ctx (&ctx, checksum);
7592 /* Store the signature in the type node and link the type DIE and the
7593 type node together. */
7594 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7595 DWARF_TYPE_SIGNATURE_SIZE);
7596 die->comdat_type_p = true;
7597 die->die_id.die_type_node = type_node;
7598 type_node->type_die = die;
7600 /* If the DIE is a specification, link its declaration to the type node
7601 as well. */
7602 if (decl != NULL)
7604 decl->comdat_type_p = true;
7605 decl->die_id.die_type_node = type_node;
7609 /* Do the location expressions look same? */
7610 static inline int
7611 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7613 return loc1->dw_loc_opc == loc2->dw_loc_opc
7614 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7615 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7618 /* Do the values look the same? */
7619 static int
7620 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7622 dw_loc_descr_ref loc1, loc2;
7623 rtx r1, r2;
7625 if (v1->val_class != v2->val_class)
7626 return 0;
7628 switch (v1->val_class)
7630 case dw_val_class_const:
7631 case dw_val_class_const_implicit:
7632 return v1->v.val_int == v2->v.val_int;
7633 case dw_val_class_unsigned_const:
7634 case dw_val_class_unsigned_const_implicit:
7635 return v1->v.val_unsigned == v2->v.val_unsigned;
7636 case dw_val_class_const_double:
7637 return v1->v.val_double.high == v2->v.val_double.high
7638 && v1->v.val_double.low == v2->v.val_double.low;
7639 case dw_val_class_wide_int:
7640 return *v1->v.val_wide == *v2->v.val_wide;
7641 case dw_val_class_vec:
7642 if (v1->v.val_vec.length != v2->v.val_vec.length
7643 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7644 return 0;
7645 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7646 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7647 return 0;
7648 return 1;
7649 case dw_val_class_flag:
7650 return v1->v.val_flag == v2->v.val_flag;
7651 case dw_val_class_str:
7652 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7654 case dw_val_class_addr:
7655 r1 = v1->v.val_addr;
7656 r2 = v2->v.val_addr;
7657 if (GET_CODE (r1) != GET_CODE (r2))
7658 return 0;
7659 return !rtx_equal_p (r1, r2);
7661 case dw_val_class_offset:
7662 return v1->v.val_offset == v2->v.val_offset;
7664 case dw_val_class_loc:
7665 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7666 loc1 && loc2;
7667 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7668 if (!same_loc_p (loc1, loc2, mark))
7669 return 0;
7670 return !loc1 && !loc2;
7672 case dw_val_class_die_ref:
7673 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7675 case dw_val_class_symview:
7676 return strcmp (v1->v.val_symbolic_view, v2->v.val_symbolic_view) == 0;
7678 case dw_val_class_fde_ref:
7679 case dw_val_class_vms_delta:
7680 case dw_val_class_lbl_id:
7681 case dw_val_class_lineptr:
7682 case dw_val_class_macptr:
7683 case dw_val_class_loclistsptr:
7684 case dw_val_class_high_pc:
7685 return 1;
7687 case dw_val_class_file:
7688 case dw_val_class_file_implicit:
7689 return v1->v.val_file == v2->v.val_file;
7691 case dw_val_class_data8:
7692 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7694 default:
7695 return 1;
7699 /* Do the attributes look the same? */
7701 static int
7702 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7704 if (at1->dw_attr != at2->dw_attr)
7705 return 0;
7707 /* We don't care that this was compiled with a different compiler
7708 snapshot; if the output is the same, that's what matters. */
7709 if (at1->dw_attr == DW_AT_producer)
7710 return 1;
7712 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7715 /* Do the dies look the same? */
7717 static int
7718 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7720 dw_die_ref c1, c2;
7721 dw_attr_node *a1;
7722 unsigned ix;
7724 /* To avoid infinite recursion. */
7725 if (die1->die_mark)
7726 return die1->die_mark == die2->die_mark;
7727 die1->die_mark = die2->die_mark = ++(*mark);
7729 if (die1->die_tag != die2->die_tag)
7730 return 0;
7732 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7733 return 0;
7735 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7736 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7737 return 0;
7739 c1 = die1->die_child;
7740 c2 = die2->die_child;
7741 if (! c1)
7743 if (c2)
7744 return 0;
7746 else
7747 for (;;)
7749 if (!same_die_p (c1, c2, mark))
7750 return 0;
7751 c1 = c1->die_sib;
7752 c2 = c2->die_sib;
7753 if (c1 == die1->die_child)
7755 if (c2 == die2->die_child)
7756 break;
7757 else
7758 return 0;
7762 return 1;
7765 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7766 children, and set die_symbol. */
7768 static void
7769 compute_comp_unit_symbol (dw_die_ref unit_die)
7771 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7772 const char *base = die_name ? lbasename (die_name) : "anonymous";
7773 char *name = XALLOCAVEC (char, strlen (base) + 64);
7774 char *p;
7775 int i, mark;
7776 unsigned char checksum[16];
7777 struct md5_ctx ctx;
7779 /* Compute the checksum of the DIE, then append part of it as hex digits to
7780 the name filename of the unit. */
7782 md5_init_ctx (&ctx);
7783 mark = 0;
7784 die_checksum (unit_die, &ctx, &mark);
7785 unmark_all_dies (unit_die);
7786 md5_finish_ctx (&ctx, checksum);
7788 /* When we this for comp_unit_die () we have a DW_AT_name that might
7789 not start with a letter but with anything valid for filenames and
7790 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7791 character is not a letter. */
7792 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7793 clean_symbol_name (name);
7795 p = name + strlen (name);
7796 for (i = 0; i < 4; i++)
7798 sprintf (p, "%.2x", checksum[i]);
7799 p += 2;
7802 unit_die->die_id.die_symbol = xstrdup (name);
7805 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7807 static int
7808 is_type_die (dw_die_ref die)
7810 switch (die->die_tag)
7812 case DW_TAG_array_type:
7813 case DW_TAG_class_type:
7814 case DW_TAG_interface_type:
7815 case DW_TAG_enumeration_type:
7816 case DW_TAG_pointer_type:
7817 case DW_TAG_reference_type:
7818 case DW_TAG_rvalue_reference_type:
7819 case DW_TAG_string_type:
7820 case DW_TAG_structure_type:
7821 case DW_TAG_subroutine_type:
7822 case DW_TAG_union_type:
7823 case DW_TAG_ptr_to_member_type:
7824 case DW_TAG_set_type:
7825 case DW_TAG_subrange_type:
7826 case DW_TAG_base_type:
7827 case DW_TAG_const_type:
7828 case DW_TAG_file_type:
7829 case DW_TAG_packed_type:
7830 case DW_TAG_volatile_type:
7831 case DW_TAG_typedef:
7832 return 1;
7833 default:
7834 return 0;
7838 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7839 Basically, we want to choose the bits that are likely to be shared between
7840 compilations (types) and leave out the bits that are specific to individual
7841 compilations (functions). */
7843 static int
7844 is_comdat_die (dw_die_ref c)
7846 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7847 we do for stabs. The advantage is a greater likelihood of sharing between
7848 objects that don't include headers in the same order (and therefore would
7849 put the base types in a different comdat). jason 8/28/00 */
7851 if (c->die_tag == DW_TAG_base_type)
7852 return 0;
7854 if (c->die_tag == DW_TAG_pointer_type
7855 || c->die_tag == DW_TAG_reference_type
7856 || c->die_tag == DW_TAG_rvalue_reference_type
7857 || c->die_tag == DW_TAG_const_type
7858 || c->die_tag == DW_TAG_volatile_type)
7860 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7862 return t ? is_comdat_die (t) : 0;
7865 return is_type_die (c);
7868 /* Returns true iff C is a compile-unit DIE. */
7870 static inline bool
7871 is_cu_die (dw_die_ref c)
7873 return c && (c->die_tag == DW_TAG_compile_unit
7874 || c->die_tag == DW_TAG_skeleton_unit);
7877 /* Returns true iff C is a unit DIE of some sort. */
7879 static inline bool
7880 is_unit_die (dw_die_ref c)
7882 return c && (c->die_tag == DW_TAG_compile_unit
7883 || c->die_tag == DW_TAG_partial_unit
7884 || c->die_tag == DW_TAG_type_unit
7885 || c->die_tag == DW_TAG_skeleton_unit);
7888 /* Returns true iff C is a namespace DIE. */
7890 static inline bool
7891 is_namespace_die (dw_die_ref c)
7893 return c && c->die_tag == DW_TAG_namespace;
7896 /* Returns true iff C is a class or structure DIE. */
7898 static inline bool
7899 is_class_die (dw_die_ref c)
7901 return c && (c->die_tag == DW_TAG_class_type
7902 || c->die_tag == DW_TAG_structure_type);
7905 /* Return non-zero if this DIE is a template parameter. */
7907 static inline bool
7908 is_template_parameter (dw_die_ref die)
7910 switch (die->die_tag)
7912 case DW_TAG_template_type_param:
7913 case DW_TAG_template_value_param:
7914 case DW_TAG_GNU_template_template_param:
7915 case DW_TAG_GNU_template_parameter_pack:
7916 return true;
7917 default:
7918 return false;
7922 /* Return non-zero if this DIE represents a template instantiation. */
7924 static inline bool
7925 is_template_instantiation (dw_die_ref die)
7927 dw_die_ref c;
7929 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7930 return false;
7931 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7932 return false;
7935 static char *
7936 gen_internal_sym (const char *prefix)
7938 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7940 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7941 return xstrdup (buf);
7944 /* Return non-zero if this DIE is a declaration. */
7946 static int
7947 is_declaration_die (dw_die_ref die)
7949 dw_attr_node *a;
7950 unsigned ix;
7952 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7953 if (a->dw_attr == DW_AT_declaration)
7954 return 1;
7956 return 0;
7959 /* Return non-zero if this DIE is nested inside a subprogram. */
7961 static int
7962 is_nested_in_subprogram (dw_die_ref die)
7964 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7966 if (decl == NULL)
7967 decl = die;
7968 return local_scope_p (decl);
7971 /* Return non-zero if this DIE contains a defining declaration of a
7972 subprogram. */
7974 static int
7975 contains_subprogram_definition (dw_die_ref die)
7977 dw_die_ref c;
7979 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7980 return 1;
7981 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7982 return 0;
7985 /* Return non-zero if this is a type DIE that should be moved to a
7986 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7987 unit type. */
7989 static int
7990 should_move_die_to_comdat (dw_die_ref die)
7992 switch (die->die_tag)
7994 case DW_TAG_class_type:
7995 case DW_TAG_structure_type:
7996 case DW_TAG_enumeration_type:
7997 case DW_TAG_union_type:
7998 /* Don't move declarations, inlined instances, types nested in a
7999 subprogram, or types that contain subprogram definitions. */
8000 if (is_declaration_die (die)
8001 || get_AT (die, DW_AT_abstract_origin)
8002 || is_nested_in_subprogram (die)
8003 || contains_subprogram_definition (die))
8004 return 0;
8005 return 1;
8006 case DW_TAG_array_type:
8007 case DW_TAG_interface_type:
8008 case DW_TAG_pointer_type:
8009 case DW_TAG_reference_type:
8010 case DW_TAG_rvalue_reference_type:
8011 case DW_TAG_string_type:
8012 case DW_TAG_subroutine_type:
8013 case DW_TAG_ptr_to_member_type:
8014 case DW_TAG_set_type:
8015 case DW_TAG_subrange_type:
8016 case DW_TAG_base_type:
8017 case DW_TAG_const_type:
8018 case DW_TAG_file_type:
8019 case DW_TAG_packed_type:
8020 case DW_TAG_volatile_type:
8021 case DW_TAG_typedef:
8022 default:
8023 return 0;
8027 /* Make a clone of DIE. */
8029 static dw_die_ref
8030 clone_die (dw_die_ref die)
8032 dw_die_ref clone = new_die_raw (die->die_tag);
8033 dw_attr_node *a;
8034 unsigned ix;
8036 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8037 add_dwarf_attr (clone, a);
8039 return clone;
8042 /* Make a clone of the tree rooted at DIE. */
8044 static dw_die_ref
8045 clone_tree (dw_die_ref die)
8047 dw_die_ref c;
8048 dw_die_ref clone = clone_die (die);
8050 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
8052 return clone;
8055 /* Make a clone of DIE as a declaration. */
8057 static dw_die_ref
8058 clone_as_declaration (dw_die_ref die)
8060 dw_die_ref clone;
8061 dw_die_ref decl;
8062 dw_attr_node *a;
8063 unsigned ix;
8065 /* If the DIE is already a declaration, just clone it. */
8066 if (is_declaration_die (die))
8067 return clone_die (die);
8069 /* If the DIE is a specification, just clone its declaration DIE. */
8070 decl = get_AT_ref (die, DW_AT_specification);
8071 if (decl != NULL)
8073 clone = clone_die (decl);
8074 if (die->comdat_type_p)
8075 add_AT_die_ref (clone, DW_AT_signature, die);
8076 return clone;
8079 clone = new_die_raw (die->die_tag);
8081 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8083 /* We don't want to copy over all attributes.
8084 For example we don't want DW_AT_byte_size because otherwise we will no
8085 longer have a declaration and GDB will treat it as a definition. */
8087 switch (a->dw_attr)
8089 case DW_AT_abstract_origin:
8090 case DW_AT_artificial:
8091 case DW_AT_containing_type:
8092 case DW_AT_external:
8093 case DW_AT_name:
8094 case DW_AT_type:
8095 case DW_AT_virtuality:
8096 case DW_AT_linkage_name:
8097 case DW_AT_MIPS_linkage_name:
8098 add_dwarf_attr (clone, a);
8099 break;
8100 case DW_AT_byte_size:
8101 case DW_AT_alignment:
8102 default:
8103 break;
8107 if (die->comdat_type_p)
8108 add_AT_die_ref (clone, DW_AT_signature, die);
8110 add_AT_flag (clone, DW_AT_declaration, 1);
8111 return clone;
8115 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
8117 struct decl_table_entry
8119 dw_die_ref orig;
8120 dw_die_ref copy;
8123 /* Helpers to manipulate hash table of copied declarations. */
8125 /* Hashtable helpers. */
8127 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
8129 typedef die_struct *compare_type;
8130 static inline hashval_t hash (const decl_table_entry *);
8131 static inline bool equal (const decl_table_entry *, const die_struct *);
8134 inline hashval_t
8135 decl_table_entry_hasher::hash (const decl_table_entry *entry)
8137 return htab_hash_pointer (entry->orig);
8140 inline bool
8141 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
8142 const die_struct *entry2)
8144 return entry1->orig == entry2;
8147 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
8149 /* Copy DIE and its ancestors, up to, but not including, the compile unit
8150 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
8151 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
8152 to check if the ancestor has already been copied into UNIT. */
8154 static dw_die_ref
8155 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
8156 decl_hash_type *decl_table)
8158 dw_die_ref parent = die->die_parent;
8159 dw_die_ref new_parent = unit;
8160 dw_die_ref copy;
8161 decl_table_entry **slot = NULL;
8162 struct decl_table_entry *entry = NULL;
8164 if (decl_table)
8166 /* Check if the entry has already been copied to UNIT. */
8167 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
8168 INSERT);
8169 if (*slot != HTAB_EMPTY_ENTRY)
8171 entry = *slot;
8172 return entry->copy;
8175 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
8176 entry = XCNEW (struct decl_table_entry);
8177 entry->orig = die;
8178 entry->copy = NULL;
8179 *slot = entry;
8182 if (parent != NULL)
8184 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
8185 if (spec != NULL)
8186 parent = spec;
8187 if (!is_unit_die (parent))
8188 new_parent = copy_ancestor_tree (unit, parent, decl_table);
8191 copy = clone_as_declaration (die);
8192 add_child_die (new_parent, copy);
8194 if (decl_table)
8196 /* Record the pointer to the copy. */
8197 entry->copy = copy;
8200 return copy;
8202 /* Copy the declaration context to the new type unit DIE. This includes
8203 any surrounding namespace or type declarations. If the DIE has an
8204 AT_specification attribute, it also includes attributes and children
8205 attached to the specification, and returns a pointer to the original
8206 parent of the declaration DIE. Returns NULL otherwise. */
8208 static dw_die_ref
8209 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
8211 dw_die_ref decl;
8212 dw_die_ref new_decl;
8213 dw_die_ref orig_parent = NULL;
8215 decl = get_AT_ref (die, DW_AT_specification);
8216 if (decl == NULL)
8217 decl = die;
8218 else
8220 unsigned ix;
8221 dw_die_ref c;
8222 dw_attr_node *a;
8224 /* The original DIE will be changed to a declaration, and must
8225 be moved to be a child of the original declaration DIE. */
8226 orig_parent = decl->die_parent;
8228 /* Copy the type node pointer from the new DIE to the original
8229 declaration DIE so we can forward references later. */
8230 decl->comdat_type_p = true;
8231 decl->die_id.die_type_node = die->die_id.die_type_node;
8233 remove_AT (die, DW_AT_specification);
8235 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
8237 if (a->dw_attr != DW_AT_name
8238 && a->dw_attr != DW_AT_declaration
8239 && a->dw_attr != DW_AT_external)
8240 add_dwarf_attr (die, a);
8243 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
8246 if (decl->die_parent != NULL
8247 && !is_unit_die (decl->die_parent))
8249 new_decl = copy_ancestor_tree (unit, decl, NULL);
8250 if (new_decl != NULL)
8252 remove_AT (new_decl, DW_AT_signature);
8253 add_AT_specification (die, new_decl);
8257 return orig_parent;
8260 /* Generate the skeleton ancestor tree for the given NODE, then clone
8261 the DIE and add the clone into the tree. */
8263 static void
8264 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
8266 if (node->new_die != NULL)
8267 return;
8269 node->new_die = clone_as_declaration (node->old_die);
8271 if (node->parent != NULL)
8273 generate_skeleton_ancestor_tree (node->parent);
8274 add_child_die (node->parent->new_die, node->new_die);
8278 /* Generate a skeleton tree of DIEs containing any declarations that are
8279 found in the original tree. We traverse the tree looking for declaration
8280 DIEs, and construct the skeleton from the bottom up whenever we find one. */
8282 static void
8283 generate_skeleton_bottom_up (skeleton_chain_node *parent)
8285 skeleton_chain_node node;
8286 dw_die_ref c;
8287 dw_die_ref first;
8288 dw_die_ref prev = NULL;
8289 dw_die_ref next = NULL;
8291 node.parent = parent;
8293 first = c = parent->old_die->die_child;
8294 if (c)
8295 next = c->die_sib;
8296 if (c) do {
8297 if (prev == NULL || prev->die_sib == c)
8298 prev = c;
8299 c = next;
8300 next = (c == first ? NULL : c->die_sib);
8301 node.old_die = c;
8302 node.new_die = NULL;
8303 if (is_declaration_die (c))
8305 if (is_template_instantiation (c))
8307 /* Instantiated templates do not need to be cloned into the
8308 type unit. Just move the DIE and its children back to
8309 the skeleton tree (in the main CU). */
8310 remove_child_with_prev (c, prev);
8311 add_child_die (parent->new_die, c);
8312 c = prev;
8314 else if (c->comdat_type_p)
8316 /* This is the skeleton of earlier break_out_comdat_types
8317 type. Clone the existing DIE, but keep the children
8318 under the original (which is in the main CU). */
8319 dw_die_ref clone = clone_die (c);
8321 replace_child (c, clone, prev);
8322 generate_skeleton_ancestor_tree (parent);
8323 add_child_die (parent->new_die, c);
8324 c = clone;
8325 continue;
8327 else
8329 /* Clone the existing DIE, move the original to the skeleton
8330 tree (which is in the main CU), and put the clone, with
8331 all the original's children, where the original came from
8332 (which is about to be moved to the type unit). */
8333 dw_die_ref clone = clone_die (c);
8334 move_all_children (c, clone);
8336 /* If the original has a DW_AT_object_pointer attribute,
8337 it would now point to a child DIE just moved to the
8338 cloned tree, so we need to remove that attribute from
8339 the original. */
8340 remove_AT (c, DW_AT_object_pointer);
8342 replace_child (c, clone, prev);
8343 generate_skeleton_ancestor_tree (parent);
8344 add_child_die (parent->new_die, c);
8345 node.old_die = clone;
8346 node.new_die = c;
8347 c = clone;
8350 generate_skeleton_bottom_up (&node);
8351 } while (next != NULL);
8354 /* Wrapper function for generate_skeleton_bottom_up. */
8356 static dw_die_ref
8357 generate_skeleton (dw_die_ref die)
8359 skeleton_chain_node node;
8361 node.old_die = die;
8362 node.new_die = NULL;
8363 node.parent = NULL;
8365 /* If this type definition is nested inside another type,
8366 and is not an instantiation of a template, always leave
8367 at least a declaration in its place. */
8368 if (die->die_parent != NULL
8369 && is_type_die (die->die_parent)
8370 && !is_template_instantiation (die))
8371 node.new_die = clone_as_declaration (die);
8373 generate_skeleton_bottom_up (&node);
8374 return node.new_die;
8377 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8378 declaration. The original DIE is moved to a new compile unit so that
8379 existing references to it follow it to the new location. If any of the
8380 original DIE's descendants is a declaration, we need to replace the
8381 original DIE with a skeleton tree and move the declarations back into the
8382 skeleton tree. */
8384 static dw_die_ref
8385 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8386 dw_die_ref prev)
8388 dw_die_ref skeleton, orig_parent;
8390 /* Copy the declaration context to the type unit DIE. If the returned
8391 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8392 that DIE. */
8393 orig_parent = copy_declaration_context (unit, child);
8395 skeleton = generate_skeleton (child);
8396 if (skeleton == NULL)
8397 remove_child_with_prev (child, prev);
8398 else
8400 skeleton->comdat_type_p = true;
8401 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8403 /* If the original DIE was a specification, we need to put
8404 the skeleton under the parent DIE of the declaration.
8405 This leaves the original declaration in the tree, but
8406 it will be pruned later since there are no longer any
8407 references to it. */
8408 if (orig_parent != NULL)
8410 remove_child_with_prev (child, prev);
8411 add_child_die (orig_parent, skeleton);
8413 else
8414 replace_child (child, skeleton, prev);
8417 return skeleton;
8420 static void
8421 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8422 comdat_type_node *type_node,
8423 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8425 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8426 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8427 DWARF procedure references in the DW_AT_location attribute. */
8429 static dw_die_ref
8430 copy_dwarf_procedure (dw_die_ref die,
8431 comdat_type_node *type_node,
8432 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8434 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8436 /* DWARF procedures are not supposed to have children... */
8437 gcc_assert (die->die_child == NULL);
8439 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8440 gcc_assert (vec_safe_length (die->die_attr) == 1
8441 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8443 /* Do not copy more than once DWARF procedures. */
8444 bool existed;
8445 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8446 if (existed)
8447 return die_copy;
8449 die_copy = clone_die (die);
8450 add_child_die (type_node->root_die, die_copy);
8451 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8452 return die_copy;
8455 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8456 procedures in DIE's attributes. */
8458 static void
8459 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8460 comdat_type_node *type_node,
8461 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8463 dw_attr_node *a;
8464 unsigned i;
8466 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8468 dw_loc_descr_ref loc;
8470 if (a->dw_attr_val.val_class != dw_val_class_loc)
8471 continue;
8473 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8475 switch (loc->dw_loc_opc)
8477 case DW_OP_call2:
8478 case DW_OP_call4:
8479 case DW_OP_call_ref:
8480 gcc_assert (loc->dw_loc_oprnd1.val_class
8481 == dw_val_class_die_ref);
8482 loc->dw_loc_oprnd1.v.val_die_ref.die
8483 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8484 type_node,
8485 copied_dwarf_procs);
8487 default:
8488 break;
8494 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8495 rewrite references to point to the copies.
8497 References are looked for in DIE's attributes and recursively in all its
8498 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8499 mapping from old DWARF procedures to their copy. It is used not to copy
8500 twice the same DWARF procedure under TYPE_NODE. */
8502 static void
8503 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8504 comdat_type_node *type_node,
8505 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8507 dw_die_ref c;
8509 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8510 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8511 type_node,
8512 copied_dwarf_procs));
8515 /* Traverse the DIE and set up additional .debug_types or .debug_info
8516 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8517 section. */
8519 static void
8520 break_out_comdat_types (dw_die_ref die)
8522 dw_die_ref c;
8523 dw_die_ref first;
8524 dw_die_ref prev = NULL;
8525 dw_die_ref next = NULL;
8526 dw_die_ref unit = NULL;
8528 first = c = die->die_child;
8529 if (c)
8530 next = c->die_sib;
8531 if (c) do {
8532 if (prev == NULL || prev->die_sib == c)
8533 prev = c;
8534 c = next;
8535 next = (c == first ? NULL : c->die_sib);
8536 if (should_move_die_to_comdat (c))
8538 dw_die_ref replacement;
8539 comdat_type_node *type_node;
8541 /* Break out nested types into their own type units. */
8542 break_out_comdat_types (c);
8544 /* Create a new type unit DIE as the root for the new tree, and
8545 add it to the list of comdat types. */
8546 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8547 add_AT_unsigned (unit, DW_AT_language,
8548 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8549 type_node = ggc_cleared_alloc<comdat_type_node> ();
8550 type_node->root_die = unit;
8551 type_node->next = comdat_type_list;
8552 comdat_type_list = type_node;
8554 /* Generate the type signature. */
8555 generate_type_signature (c, type_node);
8557 /* Copy the declaration context, attributes, and children of the
8558 declaration into the new type unit DIE, then remove this DIE
8559 from the main CU (or replace it with a skeleton if necessary). */
8560 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8561 type_node->skeleton_die = replacement;
8563 /* Add the DIE to the new compunit. */
8564 add_child_die (unit, c);
8566 /* Types can reference DWARF procedures for type size or data location
8567 expressions. Calls in DWARF expressions cannot target procedures
8568 that are not in the same section. So we must copy DWARF procedures
8569 along with this type and then rewrite references to them. */
8570 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8571 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8573 if (replacement != NULL)
8574 c = replacement;
8576 else if (c->die_tag == DW_TAG_namespace
8577 || c->die_tag == DW_TAG_class_type
8578 || c->die_tag == DW_TAG_structure_type
8579 || c->die_tag == DW_TAG_union_type)
8581 /* Look for nested types that can be broken out. */
8582 break_out_comdat_types (c);
8584 } while (next != NULL);
8587 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8588 Enter all the cloned children into the hash table decl_table. */
8590 static dw_die_ref
8591 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8593 dw_die_ref c;
8594 dw_die_ref clone;
8595 struct decl_table_entry *entry;
8596 decl_table_entry **slot;
8598 if (die->die_tag == DW_TAG_subprogram)
8599 clone = clone_as_declaration (die);
8600 else
8601 clone = clone_die (die);
8603 slot = decl_table->find_slot_with_hash (die,
8604 htab_hash_pointer (die), INSERT);
8606 /* Assert that DIE isn't in the hash table yet. If it would be there
8607 before, the ancestors would be necessarily there as well, therefore
8608 clone_tree_partial wouldn't be called. */
8609 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8611 entry = XCNEW (struct decl_table_entry);
8612 entry->orig = die;
8613 entry->copy = clone;
8614 *slot = entry;
8616 if (die->die_tag != DW_TAG_subprogram)
8617 FOR_EACH_CHILD (die, c,
8618 add_child_die (clone, clone_tree_partial (c, decl_table)));
8620 return clone;
8623 /* Walk the DIE and its children, looking for references to incomplete
8624 or trivial types that are unmarked (i.e., that are not in the current
8625 type_unit). */
8627 static void
8628 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8630 dw_die_ref c;
8631 dw_attr_node *a;
8632 unsigned ix;
8634 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8636 if (AT_class (a) == dw_val_class_die_ref)
8638 dw_die_ref targ = AT_ref (a);
8639 decl_table_entry **slot;
8640 struct decl_table_entry *entry;
8642 if (targ->die_mark != 0 || targ->comdat_type_p)
8643 continue;
8645 slot = decl_table->find_slot_with_hash (targ,
8646 htab_hash_pointer (targ),
8647 INSERT);
8649 if (*slot != HTAB_EMPTY_ENTRY)
8651 /* TARG has already been copied, so we just need to
8652 modify the reference to point to the copy. */
8653 entry = *slot;
8654 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8656 else
8658 dw_die_ref parent = unit;
8659 dw_die_ref copy = clone_die (targ);
8661 /* Record in DECL_TABLE that TARG has been copied.
8662 Need to do this now, before the recursive call,
8663 because DECL_TABLE may be expanded and SLOT
8664 would no longer be a valid pointer. */
8665 entry = XCNEW (struct decl_table_entry);
8666 entry->orig = targ;
8667 entry->copy = copy;
8668 *slot = entry;
8670 /* If TARG is not a declaration DIE, we need to copy its
8671 children. */
8672 if (!is_declaration_die (targ))
8674 FOR_EACH_CHILD (
8675 targ, c,
8676 add_child_die (copy,
8677 clone_tree_partial (c, decl_table)));
8680 /* Make sure the cloned tree is marked as part of the
8681 type unit. */
8682 mark_dies (copy);
8684 /* If TARG has surrounding context, copy its ancestor tree
8685 into the new type unit. */
8686 if (targ->die_parent != NULL
8687 && !is_unit_die (targ->die_parent))
8688 parent = copy_ancestor_tree (unit, targ->die_parent,
8689 decl_table);
8691 add_child_die (parent, copy);
8692 a->dw_attr_val.v.val_die_ref.die = copy;
8694 /* Make sure the newly-copied DIE is walked. If it was
8695 installed in a previously-added context, it won't
8696 get visited otherwise. */
8697 if (parent != unit)
8699 /* Find the highest point of the newly-added tree,
8700 mark each node along the way, and walk from there. */
8701 parent->die_mark = 1;
8702 while (parent->die_parent
8703 && parent->die_parent->die_mark == 0)
8705 parent = parent->die_parent;
8706 parent->die_mark = 1;
8708 copy_decls_walk (unit, parent, decl_table);
8714 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8717 /* Copy declarations for "unworthy" types into the new comdat section.
8718 Incomplete types, modified types, and certain other types aren't broken
8719 out into comdat sections of their own, so they don't have a signature,
8720 and we need to copy the declaration into the same section so that we
8721 don't have an external reference. */
8723 static void
8724 copy_decls_for_unworthy_types (dw_die_ref unit)
8726 mark_dies (unit);
8727 decl_hash_type decl_table (10);
8728 copy_decls_walk (unit, unit, &decl_table);
8729 unmark_dies (unit);
8732 /* Traverse the DIE and add a sibling attribute if it may have the
8733 effect of speeding up access to siblings. To save some space,
8734 avoid generating sibling attributes for DIE's without children. */
8736 static void
8737 add_sibling_attributes (dw_die_ref die)
8739 dw_die_ref c;
8741 if (! die->die_child)
8742 return;
8744 if (die->die_parent && die != die->die_parent->die_child)
8745 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8747 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8750 /* Output all location lists for the DIE and its children. */
8752 static void
8753 output_location_lists (dw_die_ref die)
8755 dw_die_ref c;
8756 dw_attr_node *a;
8757 unsigned ix;
8759 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8760 if (AT_class (a) == dw_val_class_loc_list)
8761 output_loc_list (AT_loc_list (a));
8763 FOR_EACH_CHILD (die, c, output_location_lists (c));
8766 /* During assign_location_list_indexes and output_loclists_offset the
8767 current index, after it the number of assigned indexes (i.e. how
8768 large the .debug_loclists* offset table should be). */
8769 static unsigned int loc_list_idx;
8771 /* Output all location list offsets for the DIE and its children. */
8773 static void
8774 output_loclists_offsets (dw_die_ref die)
8776 dw_die_ref c;
8777 dw_attr_node *a;
8778 unsigned ix;
8780 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8781 if (AT_class (a) == dw_val_class_loc_list)
8783 dw_loc_list_ref l = AT_loc_list (a);
8784 if (l->offset_emitted)
8785 continue;
8786 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8787 loc_section_label, NULL);
8788 gcc_assert (l->hash == loc_list_idx);
8789 loc_list_idx++;
8790 l->offset_emitted = true;
8793 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8796 /* Recursively set indexes of location lists. */
8798 static void
8799 assign_location_list_indexes (dw_die_ref die)
8801 dw_die_ref c;
8802 dw_attr_node *a;
8803 unsigned ix;
8805 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8806 if (AT_class (a) == dw_val_class_loc_list)
8808 dw_loc_list_ref list = AT_loc_list (a);
8809 if (!list->num_assigned)
8811 list->num_assigned = true;
8812 list->hash = loc_list_idx++;
8816 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8819 /* We want to limit the number of external references, because they are
8820 larger than local references: a relocation takes multiple words, and
8821 even a sig8 reference is always eight bytes, whereas a local reference
8822 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8823 So if we encounter multiple external references to the same type DIE, we
8824 make a local typedef stub for it and redirect all references there.
8826 This is the element of the hash table for keeping track of these
8827 references. */
8829 struct external_ref
8831 dw_die_ref type;
8832 dw_die_ref stub;
8833 unsigned n_refs;
8836 /* Hashtable helpers. */
8838 struct external_ref_hasher : free_ptr_hash <external_ref>
8840 static inline hashval_t hash (const external_ref *);
8841 static inline bool equal (const external_ref *, const external_ref *);
8844 inline hashval_t
8845 external_ref_hasher::hash (const external_ref *r)
8847 dw_die_ref die = r->type;
8848 hashval_t h = 0;
8850 /* We can't use the address of the DIE for hashing, because
8851 that will make the order of the stub DIEs non-deterministic. */
8852 if (! die->comdat_type_p)
8853 /* We have a symbol; use it to compute a hash. */
8854 h = htab_hash_string (die->die_id.die_symbol);
8855 else
8857 /* We have a type signature; use a subset of the bits as the hash.
8858 The 8-byte signature is at least as large as hashval_t. */
8859 comdat_type_node *type_node = die->die_id.die_type_node;
8860 memcpy (&h, type_node->signature, sizeof (h));
8862 return h;
8865 inline bool
8866 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8868 return r1->type == r2->type;
8871 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8873 /* Return a pointer to the external_ref for references to DIE. */
8875 static struct external_ref *
8876 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8878 struct external_ref ref, *ref_p;
8879 external_ref **slot;
8881 ref.type = die;
8882 slot = map->find_slot (&ref, INSERT);
8883 if (*slot != HTAB_EMPTY_ENTRY)
8884 return *slot;
8886 ref_p = XCNEW (struct external_ref);
8887 ref_p->type = die;
8888 *slot = ref_p;
8889 return ref_p;
8892 /* Subroutine of optimize_external_refs, below.
8894 If we see a type skeleton, record it as our stub. If we see external
8895 references, remember how many we've seen. */
8897 static void
8898 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8900 dw_die_ref c;
8901 dw_attr_node *a;
8902 unsigned ix;
8903 struct external_ref *ref_p;
8905 if (is_type_die (die)
8906 && (c = get_AT_ref (die, DW_AT_signature)))
8908 /* This is a local skeleton; use it for local references. */
8909 ref_p = lookup_external_ref (map, c);
8910 ref_p->stub = die;
8913 /* Scan the DIE references, and remember any that refer to DIEs from
8914 other CUs (i.e. those which are not marked). */
8915 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8916 if (AT_class (a) == dw_val_class_die_ref
8917 && (c = AT_ref (a))->die_mark == 0
8918 && is_type_die (c))
8920 ref_p = lookup_external_ref (map, c);
8921 ref_p->n_refs++;
8924 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8927 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8928 points to an external_ref, DATA is the CU we're processing. If we don't
8929 already have a local stub, and we have multiple refs, build a stub. */
8932 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8934 struct external_ref *ref_p = *slot;
8936 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8938 /* We have multiple references to this type, so build a small stub.
8939 Both of these forms are a bit dodgy from the perspective of the
8940 DWARF standard, since technically they should have names. */
8941 dw_die_ref cu = data;
8942 dw_die_ref type = ref_p->type;
8943 dw_die_ref stub = NULL;
8945 if (type->comdat_type_p)
8947 /* If we refer to this type via sig8, use AT_signature. */
8948 stub = new_die (type->die_tag, cu, NULL_TREE);
8949 add_AT_die_ref (stub, DW_AT_signature, type);
8951 else
8953 /* Otherwise, use a typedef with no name. */
8954 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8955 add_AT_die_ref (stub, DW_AT_type, type);
8958 stub->die_mark++;
8959 ref_p->stub = stub;
8961 return 1;
8964 /* DIE is a unit; look through all the DIE references to see if there are
8965 any external references to types, and if so, create local stubs for
8966 them which will be applied in build_abbrev_table. This is useful because
8967 references to local DIEs are smaller. */
8969 static external_ref_hash_type *
8970 optimize_external_refs (dw_die_ref die)
8972 external_ref_hash_type *map = new external_ref_hash_type (10);
8973 optimize_external_refs_1 (die, map);
8974 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8975 return map;
8978 /* The following 3 variables are temporaries that are computed only during the
8979 build_abbrev_table call and used and released during the following
8980 optimize_abbrev_table call. */
8982 /* First abbrev_id that can be optimized based on usage. */
8983 static unsigned int abbrev_opt_start;
8985 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8986 abbrev_id smaller than this, because they must be already sized
8987 during build_abbrev_table). */
8988 static unsigned int abbrev_opt_base_type_end;
8990 /* Vector of usage counts during build_abbrev_table. Indexed by
8991 abbrev_id - abbrev_opt_start. */
8992 static vec<unsigned int> abbrev_usage_count;
8994 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8995 static vec<dw_die_ref> sorted_abbrev_dies;
8997 /* The format of each DIE (and its attribute value pairs) is encoded in an
8998 abbreviation table. This routine builds the abbreviation table and assigns
8999 a unique abbreviation id for each abbreviation entry. The children of each
9000 die are visited recursively. */
9002 static void
9003 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
9005 unsigned int abbrev_id = 0;
9006 dw_die_ref c;
9007 dw_attr_node *a;
9008 unsigned ix;
9009 dw_die_ref abbrev;
9011 /* Scan the DIE references, and replace any that refer to
9012 DIEs from other CUs (i.e. those which are not marked) with
9013 the local stubs we built in optimize_external_refs. */
9014 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9015 if (AT_class (a) == dw_val_class_die_ref
9016 && (c = AT_ref (a))->die_mark == 0)
9018 struct external_ref *ref_p;
9019 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
9021 ref_p = lookup_external_ref (extern_map, c);
9022 if (ref_p->stub && ref_p->stub != die)
9023 change_AT_die_ref (a, ref_p->stub);
9024 else
9025 /* We aren't changing this reference, so mark it external. */
9026 set_AT_ref_external (a, 1);
9029 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9031 dw_attr_node *die_a, *abbrev_a;
9032 unsigned ix;
9033 bool ok = true;
9035 if (abbrev_id == 0)
9036 continue;
9037 if (abbrev->die_tag != die->die_tag)
9038 continue;
9039 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9040 continue;
9042 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
9043 continue;
9045 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
9047 abbrev_a = &(*abbrev->die_attr)[ix];
9048 if ((abbrev_a->dw_attr != die_a->dw_attr)
9049 || (value_format (abbrev_a) != value_format (die_a)))
9051 ok = false;
9052 break;
9055 if (ok)
9056 break;
9059 if (abbrev_id >= vec_safe_length (abbrev_die_table))
9061 vec_safe_push (abbrev_die_table, die);
9062 if (abbrev_opt_start)
9063 abbrev_usage_count.safe_push (0);
9065 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
9067 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
9068 sorted_abbrev_dies.safe_push (die);
9071 die->die_abbrev = abbrev_id;
9072 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
9075 /* Callback function for sorted_abbrev_dies vector sorting. We sort
9076 by die_abbrev's usage count, from the most commonly used
9077 abbreviation to the least. */
9079 static int
9080 die_abbrev_cmp (const void *p1, const void *p2)
9082 dw_die_ref die1 = *(const dw_die_ref *) p1;
9083 dw_die_ref die2 = *(const dw_die_ref *) p2;
9085 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
9086 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
9088 if (die1->die_abbrev >= abbrev_opt_base_type_end
9089 && die2->die_abbrev >= abbrev_opt_base_type_end)
9091 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9092 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9093 return -1;
9094 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
9095 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
9096 return 1;
9099 /* Stabilize the sort. */
9100 if (die1->die_abbrev < die2->die_abbrev)
9101 return -1;
9102 if (die1->die_abbrev > die2->die_abbrev)
9103 return 1;
9105 return 0;
9108 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
9109 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
9110 into dw_val_class_const_implicit or
9111 dw_val_class_unsigned_const_implicit. */
9113 static void
9114 optimize_implicit_const (unsigned int first_id, unsigned int end,
9115 vec<bool> &implicit_consts)
9117 /* It never makes sense if there is just one DIE using the abbreviation. */
9118 if (end < first_id + 2)
9119 return;
9121 dw_attr_node *a;
9122 unsigned ix, i;
9123 dw_die_ref die = sorted_abbrev_dies[first_id];
9124 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9125 if (implicit_consts[ix])
9127 enum dw_val_class new_class = dw_val_class_none;
9128 switch (AT_class (a))
9130 case dw_val_class_unsigned_const:
9131 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
9132 continue;
9134 /* The .debug_abbrev section will grow by
9135 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
9136 in all the DIEs using that abbreviation. */
9137 if (constant_size (AT_unsigned (a)) * (end - first_id)
9138 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
9139 continue;
9141 new_class = dw_val_class_unsigned_const_implicit;
9142 break;
9144 case dw_val_class_const:
9145 new_class = dw_val_class_const_implicit;
9146 break;
9148 case dw_val_class_file:
9149 new_class = dw_val_class_file_implicit;
9150 break;
9152 default:
9153 continue;
9155 for (i = first_id; i < end; i++)
9156 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
9157 = new_class;
9161 /* Attempt to optimize abbreviation table from abbrev_opt_start
9162 abbreviation above. */
9164 static void
9165 optimize_abbrev_table (void)
9167 if (abbrev_opt_start
9168 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
9169 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
9171 auto_vec<bool, 32> implicit_consts;
9172 sorted_abbrev_dies.qsort (die_abbrev_cmp);
9174 unsigned int abbrev_id = abbrev_opt_start - 1;
9175 unsigned int first_id = ~0U;
9176 unsigned int last_abbrev_id = 0;
9177 unsigned int i;
9178 dw_die_ref die;
9179 if (abbrev_opt_base_type_end > abbrev_opt_start)
9180 abbrev_id = abbrev_opt_base_type_end - 1;
9181 /* Reassign abbreviation ids from abbrev_opt_start above, so that
9182 most commonly used abbreviations come first. */
9183 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
9185 dw_attr_node *a;
9186 unsigned ix;
9188 /* If calc_base_type_die_sizes has been called, the CU and
9189 base types after it can't be optimized, because we've already
9190 calculated their DIE offsets. We've sorted them first. */
9191 if (die->die_abbrev < abbrev_opt_base_type_end)
9192 continue;
9193 if (die->die_abbrev != last_abbrev_id)
9195 last_abbrev_id = die->die_abbrev;
9196 if (dwarf_version >= 5 && first_id != ~0U)
9197 optimize_implicit_const (first_id, i, implicit_consts);
9198 abbrev_id++;
9199 (*abbrev_die_table)[abbrev_id] = die;
9200 if (dwarf_version >= 5)
9202 first_id = i;
9203 implicit_consts.truncate (0);
9205 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9206 switch (AT_class (a))
9208 case dw_val_class_const:
9209 case dw_val_class_unsigned_const:
9210 case dw_val_class_file:
9211 implicit_consts.safe_push (true);
9212 break;
9213 default:
9214 implicit_consts.safe_push (false);
9215 break;
9219 else if (dwarf_version >= 5)
9221 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9222 if (!implicit_consts[ix])
9223 continue;
9224 else
9226 dw_attr_node *other_a
9227 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
9228 if (!dw_val_equal_p (&a->dw_attr_val,
9229 &other_a->dw_attr_val))
9230 implicit_consts[ix] = false;
9233 die->die_abbrev = abbrev_id;
9235 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
9236 if (dwarf_version >= 5 && first_id != ~0U)
9237 optimize_implicit_const (first_id, i, implicit_consts);
9240 abbrev_opt_start = 0;
9241 abbrev_opt_base_type_end = 0;
9242 abbrev_usage_count.release ();
9243 sorted_abbrev_dies.release ();
9246 /* Return the power-of-two number of bytes necessary to represent VALUE. */
9248 static int
9249 constant_size (unsigned HOST_WIDE_INT value)
9251 int log;
9253 if (value == 0)
9254 log = 0;
9255 else
9256 log = floor_log2 (value);
9258 log = log / 8;
9259 log = 1 << (floor_log2 (log) + 1);
9261 return log;
9264 /* Return the size of a DIE as it is represented in the
9265 .debug_info section. */
9267 static unsigned long
9268 size_of_die (dw_die_ref die)
9270 unsigned long size = 0;
9271 dw_attr_node *a;
9272 unsigned ix;
9273 enum dwarf_form form;
9275 size += size_of_uleb128 (die->die_abbrev);
9276 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9278 switch (AT_class (a))
9280 case dw_val_class_addr:
9281 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9283 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9284 size += size_of_uleb128 (AT_index (a));
9286 else
9287 size += DWARF2_ADDR_SIZE;
9288 break;
9289 case dw_val_class_offset:
9290 size += DWARF_OFFSET_SIZE;
9291 break;
9292 case dw_val_class_loc:
9294 unsigned long lsize = size_of_locs (AT_loc (a));
9296 /* Block length. */
9297 if (dwarf_version >= 4)
9298 size += size_of_uleb128 (lsize);
9299 else
9300 size += constant_size (lsize);
9301 size += lsize;
9303 break;
9304 case dw_val_class_loc_list:
9305 case dw_val_class_view_list:
9306 if (dwarf_split_debug_info && dwarf_version >= 5)
9308 gcc_assert (AT_loc_list (a)->num_assigned);
9309 size += size_of_uleb128 (AT_loc_list (a)->hash);
9311 else
9312 size += DWARF_OFFSET_SIZE;
9313 break;
9314 case dw_val_class_range_list:
9315 if (value_format (a) == DW_FORM_rnglistx)
9317 gcc_assert (rnglist_idx);
9318 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9319 size += size_of_uleb128 (r->idx);
9321 else
9322 size += DWARF_OFFSET_SIZE;
9323 break;
9324 case dw_val_class_const:
9325 size += size_of_sleb128 (AT_int (a));
9326 break;
9327 case dw_val_class_unsigned_const:
9329 int csize = constant_size (AT_unsigned (a));
9330 if (dwarf_version == 3
9331 && a->dw_attr == DW_AT_data_member_location
9332 && csize >= 4)
9333 size += size_of_uleb128 (AT_unsigned (a));
9334 else
9335 size += csize;
9337 break;
9338 case dw_val_class_symview:
9339 if (symview_upper_bound <= 0xff)
9340 size += 1;
9341 else if (symview_upper_bound <= 0xffff)
9342 size += 2;
9343 else if (symview_upper_bound <= 0xffffffff)
9344 size += 4;
9345 else
9346 size += 8;
9347 break;
9348 case dw_val_class_const_implicit:
9349 case dw_val_class_unsigned_const_implicit:
9350 case dw_val_class_file_implicit:
9351 /* These occupy no size in the DIE, just an extra sleb128 in
9352 .debug_abbrev. */
9353 break;
9354 case dw_val_class_const_double:
9355 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9356 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9357 size++; /* block */
9358 break;
9359 case dw_val_class_wide_int:
9360 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9361 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9362 if (get_full_len (*a->dw_attr_val.v.val_wide)
9363 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9364 size++; /* block */
9365 break;
9366 case dw_val_class_vec:
9367 size += constant_size (a->dw_attr_val.v.val_vec.length
9368 * a->dw_attr_val.v.val_vec.elt_size)
9369 + a->dw_attr_val.v.val_vec.length
9370 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9371 break;
9372 case dw_val_class_flag:
9373 if (dwarf_version >= 4)
9374 /* Currently all add_AT_flag calls pass in 1 as last argument,
9375 so DW_FORM_flag_present can be used. If that ever changes,
9376 we'll need to use DW_FORM_flag and have some optimization
9377 in build_abbrev_table that will change those to
9378 DW_FORM_flag_present if it is set to 1 in all DIEs using
9379 the same abbrev entry. */
9380 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9381 else
9382 size += 1;
9383 break;
9384 case dw_val_class_die_ref:
9385 if (AT_ref_external (a))
9387 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9388 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9389 is sized by target address length, whereas in DWARF3
9390 it's always sized as an offset. */
9391 if (use_debug_types)
9392 size += DWARF_TYPE_SIGNATURE_SIZE;
9393 else if (dwarf_version == 2)
9394 size += DWARF2_ADDR_SIZE;
9395 else
9396 size += DWARF_OFFSET_SIZE;
9398 else
9399 size += DWARF_OFFSET_SIZE;
9400 break;
9401 case dw_val_class_fde_ref:
9402 size += DWARF_OFFSET_SIZE;
9403 break;
9404 case dw_val_class_lbl_id:
9405 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9407 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9408 size += size_of_uleb128 (AT_index (a));
9410 else
9411 size += DWARF2_ADDR_SIZE;
9412 break;
9413 case dw_val_class_lineptr:
9414 case dw_val_class_macptr:
9415 case dw_val_class_loclistsptr:
9416 size += DWARF_OFFSET_SIZE;
9417 break;
9418 case dw_val_class_str:
9419 form = AT_string_form (a);
9420 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9421 size += DWARF_OFFSET_SIZE;
9422 else if (form == DW_FORM_GNU_str_index)
9423 size += size_of_uleb128 (AT_index (a));
9424 else
9425 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9426 break;
9427 case dw_val_class_file:
9428 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9429 break;
9430 case dw_val_class_data8:
9431 size += 8;
9432 break;
9433 case dw_val_class_vms_delta:
9434 size += DWARF_OFFSET_SIZE;
9435 break;
9436 case dw_val_class_high_pc:
9437 size += DWARF2_ADDR_SIZE;
9438 break;
9439 case dw_val_class_discr_value:
9440 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9441 break;
9442 case dw_val_class_discr_list:
9444 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9446 /* This is a block, so we have the block length and then its
9447 data. */
9448 size += constant_size (block_size) + block_size;
9450 break;
9451 default:
9452 gcc_unreachable ();
9456 return size;
9459 /* Size the debugging information associated with a given DIE. Visits the
9460 DIE's children recursively. Updates the global variable next_die_offset, on
9461 each time through. Uses the current value of next_die_offset to update the
9462 die_offset field in each DIE. */
9464 static void
9465 calc_die_sizes (dw_die_ref die)
9467 dw_die_ref c;
9469 gcc_assert (die->die_offset == 0
9470 || (unsigned long int) die->die_offset == next_die_offset);
9471 die->die_offset = next_die_offset;
9472 next_die_offset += size_of_die (die);
9474 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9476 if (die->die_child != NULL)
9477 /* Count the null byte used to terminate sibling lists. */
9478 next_die_offset += 1;
9481 /* Size just the base type children at the start of the CU.
9482 This is needed because build_abbrev needs to size locs
9483 and sizing of type based stack ops needs to know die_offset
9484 values for the base types. */
9486 static void
9487 calc_base_type_die_sizes (void)
9489 unsigned long die_offset = (dwarf_split_debug_info
9490 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9491 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9492 unsigned int i;
9493 dw_die_ref base_type;
9494 #if ENABLE_ASSERT_CHECKING
9495 dw_die_ref prev = comp_unit_die ()->die_child;
9496 #endif
9498 die_offset += size_of_die (comp_unit_die ());
9499 for (i = 0; base_types.iterate (i, &base_type); i++)
9501 #if ENABLE_ASSERT_CHECKING
9502 gcc_assert (base_type->die_offset == 0
9503 && prev->die_sib == base_type
9504 && base_type->die_child == NULL
9505 && base_type->die_abbrev);
9506 prev = base_type;
9507 #endif
9508 if (abbrev_opt_start
9509 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9510 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9511 base_type->die_offset = die_offset;
9512 die_offset += size_of_die (base_type);
9516 /* Set the marks for a die and its children. We do this so
9517 that we know whether or not a reference needs to use FORM_ref_addr; only
9518 DIEs in the same CU will be marked. We used to clear out the offset
9519 and use that as the flag, but ran into ordering problems. */
9521 static void
9522 mark_dies (dw_die_ref die)
9524 dw_die_ref c;
9526 gcc_assert (!die->die_mark);
9528 die->die_mark = 1;
9529 FOR_EACH_CHILD (die, c, mark_dies (c));
9532 /* Clear the marks for a die and its children. */
9534 static void
9535 unmark_dies (dw_die_ref die)
9537 dw_die_ref c;
9539 if (! use_debug_types)
9540 gcc_assert (die->die_mark);
9542 die->die_mark = 0;
9543 FOR_EACH_CHILD (die, c, unmark_dies (c));
9546 /* Clear the marks for a die, its children and referred dies. */
9548 static void
9549 unmark_all_dies (dw_die_ref die)
9551 dw_die_ref c;
9552 dw_attr_node *a;
9553 unsigned ix;
9555 if (!die->die_mark)
9556 return;
9557 die->die_mark = 0;
9559 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9561 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9562 if (AT_class (a) == dw_val_class_die_ref)
9563 unmark_all_dies (AT_ref (a));
9566 /* Calculate if the entry should appear in the final output file. It may be
9567 from a pruned a type. */
9569 static bool
9570 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9572 /* By limiting gnu pubnames to definitions only, gold can generate a
9573 gdb index without entries for declarations, which don't include
9574 enough information to be useful. */
9575 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9576 return false;
9578 if (table == pubname_table)
9580 /* Enumerator names are part of the pubname table, but the
9581 parent DW_TAG_enumeration_type die may have been pruned.
9582 Don't output them if that is the case. */
9583 if (p->die->die_tag == DW_TAG_enumerator &&
9584 (p->die->die_parent == NULL
9585 || !p->die->die_parent->die_perennial_p))
9586 return false;
9588 /* Everything else in the pubname table is included. */
9589 return true;
9592 /* The pubtypes table shouldn't include types that have been
9593 pruned. */
9594 return (p->die->die_offset != 0
9595 || !flag_eliminate_unused_debug_types);
9598 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9599 generated for the compilation unit. */
9601 static unsigned long
9602 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9604 unsigned long size;
9605 unsigned i;
9606 pubname_entry *p;
9607 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9609 size = DWARF_PUBNAMES_HEADER_SIZE;
9610 FOR_EACH_VEC_ELT (*names, i, p)
9611 if (include_pubname_in_output (names, p))
9612 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9614 size += DWARF_OFFSET_SIZE;
9615 return size;
9618 /* Return the size of the information in the .debug_aranges section. */
9620 static unsigned long
9621 size_of_aranges (void)
9623 unsigned long size;
9625 size = DWARF_ARANGES_HEADER_SIZE;
9627 /* Count the address/length pair for this compilation unit. */
9628 if (text_section_used)
9629 size += 2 * DWARF2_ADDR_SIZE;
9630 if (cold_text_section_used)
9631 size += 2 * DWARF2_ADDR_SIZE;
9632 if (have_multiple_function_sections)
9634 unsigned fde_idx;
9635 dw_fde_ref fde;
9637 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9639 if (DECL_IGNORED_P (fde->decl))
9640 continue;
9641 if (!fde->in_std_section)
9642 size += 2 * DWARF2_ADDR_SIZE;
9643 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9644 size += 2 * DWARF2_ADDR_SIZE;
9648 /* Count the two zero words used to terminated the address range table. */
9649 size += 2 * DWARF2_ADDR_SIZE;
9650 return size;
9653 /* Select the encoding of an attribute value. */
9655 static enum dwarf_form
9656 value_format (dw_attr_node *a)
9658 switch (AT_class (a))
9660 case dw_val_class_addr:
9661 /* Only very few attributes allow DW_FORM_addr. */
9662 switch (a->dw_attr)
9664 case DW_AT_low_pc:
9665 case DW_AT_high_pc:
9666 case DW_AT_entry_pc:
9667 case DW_AT_trampoline:
9668 return (AT_index (a) == NOT_INDEXED
9669 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9670 default:
9671 break;
9673 switch (DWARF2_ADDR_SIZE)
9675 case 1:
9676 return DW_FORM_data1;
9677 case 2:
9678 return DW_FORM_data2;
9679 case 4:
9680 return DW_FORM_data4;
9681 case 8:
9682 return DW_FORM_data8;
9683 default:
9684 gcc_unreachable ();
9686 case dw_val_class_loc_list:
9687 case dw_val_class_view_list:
9688 if (dwarf_split_debug_info
9689 && dwarf_version >= 5
9690 && AT_loc_list (a)->num_assigned)
9691 return DW_FORM_loclistx;
9692 /* FALLTHRU */
9693 case dw_val_class_range_list:
9694 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9695 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9696 care about sizes of .debug* sections in shared libraries and
9697 executables and don't take into account relocations that affect just
9698 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9699 table in the .debug_rnglists section. */
9700 if (dwarf_split_debug_info
9701 && dwarf_version >= 5
9702 && AT_class (a) == dw_val_class_range_list
9703 && rnglist_idx
9704 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9705 return DW_FORM_rnglistx;
9706 if (dwarf_version >= 4)
9707 return DW_FORM_sec_offset;
9708 /* FALLTHRU */
9709 case dw_val_class_vms_delta:
9710 case dw_val_class_offset:
9711 switch (DWARF_OFFSET_SIZE)
9713 case 4:
9714 return DW_FORM_data4;
9715 case 8:
9716 return DW_FORM_data8;
9717 default:
9718 gcc_unreachable ();
9720 case dw_val_class_loc:
9721 if (dwarf_version >= 4)
9722 return DW_FORM_exprloc;
9723 switch (constant_size (size_of_locs (AT_loc (a))))
9725 case 1:
9726 return DW_FORM_block1;
9727 case 2:
9728 return DW_FORM_block2;
9729 case 4:
9730 return DW_FORM_block4;
9731 default:
9732 gcc_unreachable ();
9734 case dw_val_class_const:
9735 return DW_FORM_sdata;
9736 case dw_val_class_unsigned_const:
9737 switch (constant_size (AT_unsigned (a)))
9739 case 1:
9740 return DW_FORM_data1;
9741 case 2:
9742 return DW_FORM_data2;
9743 case 4:
9744 /* In DWARF3 DW_AT_data_member_location with
9745 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9746 constant, so we need to use DW_FORM_udata if we need
9747 a large constant. */
9748 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9749 return DW_FORM_udata;
9750 return DW_FORM_data4;
9751 case 8:
9752 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9753 return DW_FORM_udata;
9754 return DW_FORM_data8;
9755 default:
9756 gcc_unreachable ();
9758 case dw_val_class_const_implicit:
9759 case dw_val_class_unsigned_const_implicit:
9760 case dw_val_class_file_implicit:
9761 return DW_FORM_implicit_const;
9762 case dw_val_class_const_double:
9763 switch (HOST_BITS_PER_WIDE_INT)
9765 case 8:
9766 return DW_FORM_data2;
9767 case 16:
9768 return DW_FORM_data4;
9769 case 32:
9770 return DW_FORM_data8;
9771 case 64:
9772 if (dwarf_version >= 5)
9773 return DW_FORM_data16;
9774 /* FALLTHRU */
9775 default:
9776 return DW_FORM_block1;
9778 case dw_val_class_wide_int:
9779 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9781 case 8:
9782 return DW_FORM_data1;
9783 case 16:
9784 return DW_FORM_data2;
9785 case 32:
9786 return DW_FORM_data4;
9787 case 64:
9788 return DW_FORM_data8;
9789 case 128:
9790 if (dwarf_version >= 5)
9791 return DW_FORM_data16;
9792 /* FALLTHRU */
9793 default:
9794 return DW_FORM_block1;
9796 case dw_val_class_symview:
9797 /* ??? We might use uleb128, but then we'd have to compute
9798 .debug_info offsets in the assembler. */
9799 if (symview_upper_bound <= 0xff)
9800 return DW_FORM_data1;
9801 else if (symview_upper_bound <= 0xffff)
9802 return DW_FORM_data2;
9803 else if (symview_upper_bound <= 0xffffffff)
9804 return DW_FORM_data4;
9805 else
9806 return DW_FORM_data8;
9807 case dw_val_class_vec:
9808 switch (constant_size (a->dw_attr_val.v.val_vec.length
9809 * a->dw_attr_val.v.val_vec.elt_size))
9811 case 1:
9812 return DW_FORM_block1;
9813 case 2:
9814 return DW_FORM_block2;
9815 case 4:
9816 return DW_FORM_block4;
9817 default:
9818 gcc_unreachable ();
9820 case dw_val_class_flag:
9821 if (dwarf_version >= 4)
9823 /* Currently all add_AT_flag calls pass in 1 as last argument,
9824 so DW_FORM_flag_present can be used. If that ever changes,
9825 we'll need to use DW_FORM_flag and have some optimization
9826 in build_abbrev_table that will change those to
9827 DW_FORM_flag_present if it is set to 1 in all DIEs using
9828 the same abbrev entry. */
9829 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9830 return DW_FORM_flag_present;
9832 return DW_FORM_flag;
9833 case dw_val_class_die_ref:
9834 if (AT_ref_external (a))
9835 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9836 else
9837 return DW_FORM_ref;
9838 case dw_val_class_fde_ref:
9839 return DW_FORM_data;
9840 case dw_val_class_lbl_id:
9841 return (AT_index (a) == NOT_INDEXED
9842 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9843 case dw_val_class_lineptr:
9844 case dw_val_class_macptr:
9845 case dw_val_class_loclistsptr:
9846 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9847 case dw_val_class_str:
9848 return AT_string_form (a);
9849 case dw_val_class_file:
9850 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9852 case 1:
9853 return DW_FORM_data1;
9854 case 2:
9855 return DW_FORM_data2;
9856 case 4:
9857 return DW_FORM_data4;
9858 default:
9859 gcc_unreachable ();
9862 case dw_val_class_data8:
9863 return DW_FORM_data8;
9865 case dw_val_class_high_pc:
9866 switch (DWARF2_ADDR_SIZE)
9868 case 1:
9869 return DW_FORM_data1;
9870 case 2:
9871 return DW_FORM_data2;
9872 case 4:
9873 return DW_FORM_data4;
9874 case 8:
9875 return DW_FORM_data8;
9876 default:
9877 gcc_unreachable ();
9880 case dw_val_class_discr_value:
9881 return (a->dw_attr_val.v.val_discr_value.pos
9882 ? DW_FORM_udata
9883 : DW_FORM_sdata);
9884 case dw_val_class_discr_list:
9885 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9887 case 1:
9888 return DW_FORM_block1;
9889 case 2:
9890 return DW_FORM_block2;
9891 case 4:
9892 return DW_FORM_block4;
9893 default:
9894 gcc_unreachable ();
9897 default:
9898 gcc_unreachable ();
9902 /* Output the encoding of an attribute value. */
9904 static void
9905 output_value_format (dw_attr_node *a)
9907 enum dwarf_form form = value_format (a);
9909 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9912 /* Given a die and id, produce the appropriate abbreviations. */
9914 static void
9915 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9917 unsigned ix;
9918 dw_attr_node *a_attr;
9920 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9921 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9922 dwarf_tag_name (abbrev->die_tag));
9924 if (abbrev->die_child != NULL)
9925 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9926 else
9927 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9929 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9931 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9932 dwarf_attr_name (a_attr->dw_attr));
9933 output_value_format (a_attr);
9934 if (value_format (a_attr) == DW_FORM_implicit_const)
9936 if (AT_class (a_attr) == dw_val_class_file_implicit)
9938 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9939 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9940 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9942 else
9943 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9947 dw2_asm_output_data (1, 0, NULL);
9948 dw2_asm_output_data (1, 0, NULL);
9952 /* Output the .debug_abbrev section which defines the DIE abbreviation
9953 table. */
9955 static void
9956 output_abbrev_section (void)
9958 unsigned int abbrev_id;
9959 dw_die_ref abbrev;
9961 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9962 if (abbrev_id != 0)
9963 output_die_abbrevs (abbrev_id, abbrev);
9965 /* Terminate the table. */
9966 dw2_asm_output_data (1, 0, NULL);
9969 /* Return a new location list, given the begin and end range, and the
9970 expression. */
9972 static inline dw_loc_list_ref
9973 new_loc_list (dw_loc_descr_ref expr, const char *begin, var_loc_view vbegin,
9974 const char *end, var_loc_view vend,
9975 const char *section)
9977 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9979 retlist->begin = begin;
9980 retlist->begin_entry = NULL;
9981 retlist->end = end;
9982 retlist->expr = expr;
9983 retlist->section = section;
9984 retlist->vbegin = vbegin;
9985 retlist->vend = vend;
9987 return retlist;
9990 /* Return true iff there's any nonzero view number in the loc list. */
9992 static bool
9993 loc_list_has_views (dw_loc_list_ref list)
9995 if (!debug_variable_location_views)
9996 return false;
9998 for (dw_loc_list_ref loc = list;
9999 loc != NULL; loc = loc->dw_loc_next)
10000 if (!ZERO_VIEW_P (loc->vbegin) || !ZERO_VIEW_P (loc->vend))
10001 return true;
10003 return false;
10006 /* Generate a new internal symbol for this location list node, if it
10007 hasn't got one yet. */
10009 static inline void
10010 gen_llsym (dw_loc_list_ref list)
10012 gcc_assert (!list->ll_symbol);
10013 list->ll_symbol = gen_internal_sym ("LLST");
10015 if (!loc_list_has_views (list))
10016 return;
10018 if (dwarf2out_locviews_in_attribute ())
10020 /* Use the same label_num for the view list. */
10021 label_num--;
10022 list->vl_symbol = gen_internal_sym ("LVUS");
10024 else
10025 list->vl_symbol = list->ll_symbol;
10028 /* Generate a symbol for the list, but only if we really want to emit
10029 it as a list. */
10031 static inline void
10032 maybe_gen_llsym (dw_loc_list_ref list)
10034 if (!list || (!list->dw_loc_next && !loc_list_has_views (list)))
10035 return;
10037 gen_llsym (list);
10040 /* Determine whether or not to skip loc_list entry CURR. If SIZEP is
10041 NULL, don't consider size of the location expression. If we're not
10042 to skip it, and SIZEP is non-null, store the size of CURR->expr's
10043 representation in *SIZEP. */
10045 static bool
10046 skip_loc_list_entry (dw_loc_list_ref curr, unsigned long *sizep = NULL)
10048 /* Don't output an entry that starts and ends at the same address. */
10049 if (strcmp (curr->begin, curr->end) == 0
10050 && curr->vbegin == curr->vend && !curr->force)
10051 return true;
10053 if (!sizep)
10054 return false;
10056 unsigned long size = size_of_locs (curr->expr);
10058 /* If the expression is too large, drop it on the floor. We could
10059 perhaps put it into DW_TAG_dwarf_procedure and refer to that
10060 in the expression, but >= 64KB expressions for a single value
10061 in a single range are unlikely very useful. */
10062 if (dwarf_version < 5 && size > 0xffff)
10063 return true;
10065 *sizep = size;
10067 return false;
10070 /* Output a view pair loclist entry for CURR, if it requires one. */
10072 static void
10073 dwarf2out_maybe_output_loclist_view_pair (dw_loc_list_ref curr)
10075 if (!dwarf2out_locviews_in_loclist ())
10076 return;
10078 if (ZERO_VIEW_P (curr->vbegin) && ZERO_VIEW_P (curr->vend))
10079 return;
10081 #ifdef DW_LLE_view_pair
10082 dw2_asm_output_data (1, DW_LLE_view_pair, "DW_LLE_view_pair");
10084 if (dwarf2out_as_locview_support)
10086 if (ZERO_VIEW_P (curr->vbegin))
10087 dw2_asm_output_data_uleb128 (0, "Location view begin");
10088 else
10090 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10091 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10092 dw2_asm_output_symname_uleb128 (label, "Location view begin");
10095 if (ZERO_VIEW_P (curr->vend))
10096 dw2_asm_output_data_uleb128 (0, "Location view end");
10097 else
10099 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10100 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10101 dw2_asm_output_symname_uleb128 (label, "Location view end");
10104 else
10106 dw2_asm_output_data_uleb128 (curr->vbegin, "Location view begin");
10107 dw2_asm_output_data_uleb128 (curr->vend, "Location view end");
10109 #endif /* DW_LLE_view_pair */
10111 return;
10114 /* Output the location list given to us. */
10116 static void
10117 output_loc_list (dw_loc_list_ref list_head)
10119 int vcount = 0, lcount = 0;
10121 if (list_head->emitted)
10122 return;
10123 list_head->emitted = true;
10125 if (list_head->vl_symbol && dwarf2out_locviews_in_attribute ())
10127 ASM_OUTPUT_LABEL (asm_out_file, list_head->vl_symbol);
10129 for (dw_loc_list_ref curr = list_head; curr != NULL;
10130 curr = curr->dw_loc_next)
10132 unsigned long size;
10134 if (skip_loc_list_entry (curr, &size))
10135 continue;
10137 vcount++;
10139 /* ?? dwarf_split_debug_info? */
10140 if (dwarf2out_as_locview_support)
10142 char label[MAX_ARTIFICIAL_LABEL_BYTES];
10144 if (!ZERO_VIEW_P (curr->vbegin))
10146 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vbegin);
10147 dw2_asm_output_symname_uleb128 (label,
10148 "View list begin (%s)",
10149 list_head->vl_symbol);
10151 else
10152 dw2_asm_output_data_uleb128 (0,
10153 "View list begin (%s)",
10154 list_head->vl_symbol);
10156 if (!ZERO_VIEW_P (curr->vend))
10158 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", curr->vend);
10159 dw2_asm_output_symname_uleb128 (label,
10160 "View list end (%s)",
10161 list_head->vl_symbol);
10163 else
10164 dw2_asm_output_data_uleb128 (0,
10165 "View list end (%s)",
10166 list_head->vl_symbol);
10168 else
10170 dw2_asm_output_data_uleb128 (curr->vbegin,
10171 "View list begin (%s)",
10172 list_head->vl_symbol);
10173 dw2_asm_output_data_uleb128 (curr->vend,
10174 "View list end (%s)",
10175 list_head->vl_symbol);
10180 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10182 const char *last_section = NULL;
10183 const char *base_label = NULL;
10185 /* Walk the location list, and output each range + expression. */
10186 for (dw_loc_list_ref curr = list_head; curr != NULL;
10187 curr = curr->dw_loc_next)
10189 unsigned long size;
10191 /* Skip this entry? If we skip it here, we must skip it in the
10192 view list above as well. */
10193 if (skip_loc_list_entry (curr, &size))
10194 continue;
10196 lcount++;
10198 if (dwarf_version >= 5)
10200 if (dwarf_split_debug_info)
10202 dwarf2out_maybe_output_loclist_view_pair (curr);
10203 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
10204 uleb128 index into .debug_addr and uleb128 length. */
10205 dw2_asm_output_data (1, DW_LLE_startx_length,
10206 "DW_LLE_startx_length (%s)",
10207 list_head->ll_symbol);
10208 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10209 "Location list range start index "
10210 "(%s)", curr->begin);
10211 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
10212 For that case we probably need to emit DW_LLE_startx_endx,
10213 but we'd need 2 .debug_addr entries rather than just one. */
10214 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10215 "Location list length (%s)",
10216 list_head->ll_symbol);
10218 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
10220 dwarf2out_maybe_output_loclist_view_pair (curr);
10221 /* If all code is in .text section, the base address is
10222 already provided by the CU attributes. Use
10223 DW_LLE_offset_pair where both addresses are uleb128 encoded
10224 offsets against that base. */
10225 dw2_asm_output_data (1, DW_LLE_offset_pair,
10226 "DW_LLE_offset_pair (%s)",
10227 list_head->ll_symbol);
10228 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
10229 "Location list begin address (%s)",
10230 list_head->ll_symbol);
10231 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
10232 "Location list end address (%s)",
10233 list_head->ll_symbol);
10235 else if (HAVE_AS_LEB128)
10237 /* Otherwise, find out how many consecutive entries could share
10238 the same base entry. If just one, emit DW_LLE_start_length,
10239 otherwise emit DW_LLE_base_address for the base address
10240 followed by a series of DW_LLE_offset_pair. */
10241 if (last_section == NULL || curr->section != last_section)
10243 dw_loc_list_ref curr2;
10244 for (curr2 = curr->dw_loc_next; curr2 != NULL;
10245 curr2 = curr2->dw_loc_next)
10247 if (strcmp (curr2->begin, curr2->end) == 0
10248 && !curr2->force)
10249 continue;
10250 break;
10252 if (curr2 == NULL || curr->section != curr2->section)
10253 last_section = NULL;
10254 else
10256 last_section = curr->section;
10257 base_label = curr->begin;
10258 dw2_asm_output_data (1, DW_LLE_base_address,
10259 "DW_LLE_base_address (%s)",
10260 list_head->ll_symbol);
10261 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
10262 "Base address (%s)",
10263 list_head->ll_symbol);
10266 /* Only one entry with the same base address. Use
10267 DW_LLE_start_length with absolute address and uleb128
10268 length. */
10269 if (last_section == NULL)
10271 dwarf2out_maybe_output_loclist_view_pair (curr);
10272 dw2_asm_output_data (1, DW_LLE_start_length,
10273 "DW_LLE_start_length (%s)",
10274 list_head->ll_symbol);
10275 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10276 "Location list begin address (%s)",
10277 list_head->ll_symbol);
10278 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
10279 "Location list length "
10280 "(%s)", list_head->ll_symbol);
10282 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
10283 DW_LLE_base_address. */
10284 else
10286 dwarf2out_maybe_output_loclist_view_pair (curr);
10287 dw2_asm_output_data (1, DW_LLE_offset_pair,
10288 "DW_LLE_offset_pair (%s)",
10289 list_head->ll_symbol);
10290 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
10291 "Location list begin address "
10292 "(%s)", list_head->ll_symbol);
10293 dw2_asm_output_delta_uleb128 (curr->end, base_label,
10294 "Location list end address "
10295 "(%s)", list_head->ll_symbol);
10298 /* The assembler does not support .uleb128 directive. Emit
10299 DW_LLE_start_end with a pair of absolute addresses. */
10300 else
10302 dwarf2out_maybe_output_loclist_view_pair (curr);
10303 dw2_asm_output_data (1, DW_LLE_start_end,
10304 "DW_LLE_start_end (%s)",
10305 list_head->ll_symbol);
10306 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10307 "Location list begin address (%s)",
10308 list_head->ll_symbol);
10309 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10310 "Location list end address (%s)",
10311 list_head->ll_symbol);
10314 else if (dwarf_split_debug_info)
10316 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
10317 and 4 byte length. */
10318 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
10319 "Location list start/length entry (%s)",
10320 list_head->ll_symbol);
10321 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
10322 "Location list range start index (%s)",
10323 curr->begin);
10324 /* The length field is 4 bytes. If we ever need to support
10325 an 8-byte length, we can add a new DW_LLE code or fall back
10326 to DW_LLE_GNU_start_end_entry. */
10327 dw2_asm_output_delta (4, curr->end, curr->begin,
10328 "Location list range length (%s)",
10329 list_head->ll_symbol);
10331 else if (!have_multiple_function_sections)
10333 /* Pair of relative addresses against start of text section. */
10334 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10335 "Location list begin address (%s)",
10336 list_head->ll_symbol);
10337 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10338 "Location list end address (%s)",
10339 list_head->ll_symbol);
10341 else
10343 /* Pair of absolute addresses. */
10344 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10345 "Location list begin address (%s)",
10346 list_head->ll_symbol);
10347 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10348 "Location list end address (%s)",
10349 list_head->ll_symbol);
10352 /* Output the block length for this list of location operations. */
10353 if (dwarf_version >= 5)
10354 dw2_asm_output_data_uleb128 (size, "Location expression size");
10355 else
10357 gcc_assert (size <= 0xffff);
10358 dw2_asm_output_data (2, size, "Location expression size");
10361 output_loc_sequence (curr->expr, -1);
10364 /* And finally list termination. */
10365 if (dwarf_version >= 5)
10366 dw2_asm_output_data (1, DW_LLE_end_of_list,
10367 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
10368 else if (dwarf_split_debug_info)
10369 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
10370 "Location list terminator (%s)",
10371 list_head->ll_symbol);
10372 else
10374 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10375 "Location list terminator begin (%s)",
10376 list_head->ll_symbol);
10377 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10378 "Location list terminator end (%s)",
10379 list_head->ll_symbol);
10382 gcc_assert (!list_head->vl_symbol
10383 || vcount == lcount * (dwarf2out_locviews_in_attribute () ? 1 : 0));
10386 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
10387 section. Emit a relocated reference if val_entry is NULL, otherwise,
10388 emit an indirect reference. */
10390 static void
10391 output_range_list_offset (dw_attr_node *a)
10393 const char *name = dwarf_attr_name (a->dw_attr);
10395 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
10397 if (dwarf_version >= 5)
10399 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10400 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
10401 debug_ranges_section, "%s", name);
10403 else
10405 char *p = strchr (ranges_section_label, '\0');
10406 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10407 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
10408 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10409 debug_ranges_section, "%s", name);
10410 *p = '\0';
10413 else if (dwarf_version >= 5)
10415 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
10416 gcc_assert (rnglist_idx);
10417 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
10419 else
10420 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10421 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
10422 "%s (offset from %s)", name, ranges_section_label);
10425 /* Output the offset into the debug_loc section. */
10427 static void
10428 output_loc_list_offset (dw_attr_node *a)
10430 char *sym = AT_loc_list (a)->ll_symbol;
10432 gcc_assert (sym);
10433 if (!dwarf_split_debug_info)
10434 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10435 "%s", dwarf_attr_name (a->dw_attr));
10436 else if (dwarf_version >= 5)
10438 gcc_assert (AT_loc_list (a)->num_assigned);
10439 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
10440 dwarf_attr_name (a->dw_attr),
10441 sym);
10443 else
10444 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10445 "%s", dwarf_attr_name (a->dw_attr));
10448 /* Output the offset into the debug_loc section. */
10450 static void
10451 output_view_list_offset (dw_attr_node *a)
10453 char *sym = (*AT_loc_list_ptr (a))->vl_symbol;
10455 gcc_assert (sym);
10456 if (dwarf_split_debug_info)
10457 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
10458 "%s", dwarf_attr_name (a->dw_attr));
10459 else
10460 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10461 "%s", dwarf_attr_name (a->dw_attr));
10464 /* Output an attribute's index or value appropriately. */
10466 static void
10467 output_attr_index_or_value (dw_attr_node *a)
10469 const char *name = dwarf_attr_name (a->dw_attr);
10471 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
10473 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
10474 return;
10476 switch (AT_class (a))
10478 case dw_val_class_addr:
10479 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10480 break;
10481 case dw_val_class_high_pc:
10482 case dw_val_class_lbl_id:
10483 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10484 break;
10485 default:
10486 gcc_unreachable ();
10490 /* Output a type signature. */
10492 static inline void
10493 output_signature (const char *sig, const char *name)
10495 int i;
10497 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10498 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10501 /* Output a discriminant value. */
10503 static inline void
10504 output_discr_value (dw_discr_value *discr_value, const char *name)
10506 if (discr_value->pos)
10507 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
10508 else
10509 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
10512 /* Output the DIE and its attributes. Called recursively to generate
10513 the definitions of each child DIE. */
10515 static void
10516 output_die (dw_die_ref die)
10518 dw_attr_node *a;
10519 dw_die_ref c;
10520 unsigned long size;
10521 unsigned ix;
10523 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10524 (unsigned long)die->die_offset,
10525 dwarf_tag_name (die->die_tag));
10527 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10529 const char *name = dwarf_attr_name (a->dw_attr);
10531 switch (AT_class (a))
10533 case dw_val_class_addr:
10534 output_attr_index_or_value (a);
10535 break;
10537 case dw_val_class_offset:
10538 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10539 "%s", name);
10540 break;
10542 case dw_val_class_range_list:
10543 output_range_list_offset (a);
10544 break;
10546 case dw_val_class_loc:
10547 size = size_of_locs (AT_loc (a));
10549 /* Output the block length for this list of location operations. */
10550 if (dwarf_version >= 4)
10551 dw2_asm_output_data_uleb128 (size, "%s", name);
10552 else
10553 dw2_asm_output_data (constant_size (size), size, "%s", name);
10555 output_loc_sequence (AT_loc (a), -1);
10556 break;
10558 case dw_val_class_const:
10559 /* ??? It would be slightly more efficient to use a scheme like is
10560 used for unsigned constants below, but gdb 4.x does not sign
10561 extend. Gdb 5.x does sign extend. */
10562 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10563 break;
10565 case dw_val_class_unsigned_const:
10567 int csize = constant_size (AT_unsigned (a));
10568 if (dwarf_version == 3
10569 && a->dw_attr == DW_AT_data_member_location
10570 && csize >= 4)
10571 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10572 else
10573 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10575 break;
10577 case dw_val_class_symview:
10579 int vsize;
10580 if (symview_upper_bound <= 0xff)
10581 vsize = 1;
10582 else if (symview_upper_bound <= 0xffff)
10583 vsize = 2;
10584 else if (symview_upper_bound <= 0xffffffff)
10585 vsize = 4;
10586 else
10587 vsize = 8;
10588 dw2_asm_output_addr (vsize, a->dw_attr_val.v.val_symbolic_view,
10589 "%s", name);
10591 break;
10593 case dw_val_class_const_implicit:
10594 if (flag_debug_asm)
10595 fprintf (asm_out_file, "\t\t\t%s %s ("
10596 HOST_WIDE_INT_PRINT_DEC ")\n",
10597 ASM_COMMENT_START, name, AT_int (a));
10598 break;
10600 case dw_val_class_unsigned_const_implicit:
10601 if (flag_debug_asm)
10602 fprintf (asm_out_file, "\t\t\t%s %s ("
10603 HOST_WIDE_INT_PRINT_HEX ")\n",
10604 ASM_COMMENT_START, name, AT_unsigned (a));
10605 break;
10607 case dw_val_class_const_double:
10609 unsigned HOST_WIDE_INT first, second;
10611 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10612 dw2_asm_output_data (1,
10613 HOST_BITS_PER_DOUBLE_INT
10614 / HOST_BITS_PER_CHAR,
10615 NULL);
10617 if (WORDS_BIG_ENDIAN)
10619 first = a->dw_attr_val.v.val_double.high;
10620 second = a->dw_attr_val.v.val_double.low;
10622 else
10624 first = a->dw_attr_val.v.val_double.low;
10625 second = a->dw_attr_val.v.val_double.high;
10628 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10629 first, "%s", name);
10630 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10631 second, NULL);
10633 break;
10635 case dw_val_class_wide_int:
10637 int i;
10638 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10639 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10640 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10641 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10642 * l, NULL);
10644 if (WORDS_BIG_ENDIAN)
10645 for (i = len - 1; i >= 0; --i)
10647 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10648 "%s", name);
10649 name = "";
10651 else
10652 for (i = 0; i < len; ++i)
10654 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10655 "%s", name);
10656 name = "";
10659 break;
10661 case dw_val_class_vec:
10663 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10664 unsigned int len = a->dw_attr_val.v.val_vec.length;
10665 unsigned int i;
10666 unsigned char *p;
10668 dw2_asm_output_data (constant_size (len * elt_size),
10669 len * elt_size, "%s", name);
10670 if (elt_size > sizeof (HOST_WIDE_INT))
10672 elt_size /= 2;
10673 len *= 2;
10675 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10676 i < len;
10677 i++, p += elt_size)
10678 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10679 "fp or vector constant word %u", i);
10680 break;
10683 case dw_val_class_flag:
10684 if (dwarf_version >= 4)
10686 /* Currently all add_AT_flag calls pass in 1 as last argument,
10687 so DW_FORM_flag_present can be used. If that ever changes,
10688 we'll need to use DW_FORM_flag and have some optimization
10689 in build_abbrev_table that will change those to
10690 DW_FORM_flag_present if it is set to 1 in all DIEs using
10691 the same abbrev entry. */
10692 gcc_assert (AT_flag (a) == 1);
10693 if (flag_debug_asm)
10694 fprintf (asm_out_file, "\t\t\t%s %s\n",
10695 ASM_COMMENT_START, name);
10696 break;
10698 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10699 break;
10701 case dw_val_class_loc_list:
10702 output_loc_list_offset (a);
10703 break;
10705 case dw_val_class_view_list:
10706 output_view_list_offset (a);
10707 break;
10709 case dw_val_class_die_ref:
10710 if (AT_ref_external (a))
10712 if (AT_ref (a)->comdat_type_p)
10714 comdat_type_node *type_node
10715 = AT_ref (a)->die_id.die_type_node;
10717 gcc_assert (type_node);
10718 output_signature (type_node->signature, name);
10720 else
10722 const char *sym = AT_ref (a)->die_id.die_symbol;
10723 int size;
10725 gcc_assert (sym);
10726 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10727 length, whereas in DWARF3 it's always sized as an
10728 offset. */
10729 if (dwarf_version == 2)
10730 size = DWARF2_ADDR_SIZE;
10731 else
10732 size = DWARF_OFFSET_SIZE;
10733 /* ??? We cannot unconditionally output die_offset if
10734 non-zero - others might create references to those
10735 DIEs via symbols.
10736 And we do not clear its DIE offset after outputting it
10737 (and the label refers to the actual DIEs, not the
10738 DWARF CU unit header which is when using label + offset
10739 would be the correct thing to do).
10740 ??? This is the reason for the with_offset flag. */
10741 if (AT_ref (a)->with_offset)
10742 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10743 debug_info_section, "%s", name);
10744 else
10745 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10746 name);
10749 else
10751 gcc_assert (AT_ref (a)->die_offset);
10752 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10753 "%s", name);
10755 break;
10757 case dw_val_class_fde_ref:
10759 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10761 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10762 a->dw_attr_val.v.val_fde_index * 2);
10763 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10764 "%s", name);
10766 break;
10768 case dw_val_class_vms_delta:
10769 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10770 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10771 AT_vms_delta2 (a), AT_vms_delta1 (a),
10772 "%s", name);
10773 #else
10774 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10775 AT_vms_delta2 (a), AT_vms_delta1 (a),
10776 "%s", name);
10777 #endif
10778 break;
10780 case dw_val_class_lbl_id:
10781 output_attr_index_or_value (a);
10782 break;
10784 case dw_val_class_lineptr:
10785 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10786 debug_line_section, "%s", name);
10787 break;
10789 case dw_val_class_macptr:
10790 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10791 debug_macinfo_section, "%s", name);
10792 break;
10794 case dw_val_class_loclistsptr:
10795 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10796 debug_loc_section, "%s", name);
10797 break;
10799 case dw_val_class_str:
10800 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10801 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10802 a->dw_attr_val.v.val_str->label,
10803 debug_str_section,
10804 "%s: \"%s\"", name, AT_string (a));
10805 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10806 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10807 a->dw_attr_val.v.val_str->label,
10808 debug_line_str_section,
10809 "%s: \"%s\"", name, AT_string (a));
10810 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10811 dw2_asm_output_data_uleb128 (AT_index (a),
10812 "%s: \"%s\"", name, AT_string (a));
10813 else
10814 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10815 break;
10817 case dw_val_class_file:
10819 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10821 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10822 a->dw_attr_val.v.val_file->filename);
10823 break;
10826 case dw_val_class_file_implicit:
10827 if (flag_debug_asm)
10828 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10829 ASM_COMMENT_START, name,
10830 maybe_emit_file (a->dw_attr_val.v.val_file),
10831 a->dw_attr_val.v.val_file->filename);
10832 break;
10834 case dw_val_class_data8:
10836 int i;
10838 for (i = 0; i < 8; i++)
10839 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10840 i == 0 ? "%s" : NULL, name);
10841 break;
10844 case dw_val_class_high_pc:
10845 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10846 get_AT_low_pc (die), "DW_AT_high_pc");
10847 break;
10849 case dw_val_class_discr_value:
10850 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10851 break;
10853 case dw_val_class_discr_list:
10855 dw_discr_list_ref list = AT_discr_list (a);
10856 const int size = size_of_discr_list (list);
10858 /* This is a block, so output its length first. */
10859 dw2_asm_output_data (constant_size (size), size,
10860 "%s: block size", name);
10862 for (; list != NULL; list = list->dw_discr_next)
10864 /* One byte for the discriminant value descriptor, and then as
10865 many LEB128 numbers as required. */
10866 if (list->dw_discr_range)
10867 dw2_asm_output_data (1, DW_DSC_range,
10868 "%s: DW_DSC_range", name);
10869 else
10870 dw2_asm_output_data (1, DW_DSC_label,
10871 "%s: DW_DSC_label", name);
10873 output_discr_value (&list->dw_discr_lower_bound, name);
10874 if (list->dw_discr_range)
10875 output_discr_value (&list->dw_discr_upper_bound, name);
10877 break;
10880 default:
10881 gcc_unreachable ();
10885 FOR_EACH_CHILD (die, c, output_die (c));
10887 /* Add null byte to terminate sibling list. */
10888 if (die->die_child != NULL)
10889 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10890 (unsigned long) die->die_offset);
10893 /* Output the dwarf version number. */
10895 static void
10896 output_dwarf_version ()
10898 /* ??? For now, if -gdwarf-6 is specified, we output version 5 with
10899 views in loclist. That will change eventually. */
10900 if (dwarf_version == 6)
10902 static bool once;
10903 if (!once)
10905 warning (0,
10906 "-gdwarf-6 is output as version 5 with incompatibilities");
10907 once = true;
10909 dw2_asm_output_data (2, 5, "DWARF version number");
10911 else
10912 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10915 /* Output the compilation unit that appears at the beginning of the
10916 .debug_info section, and precedes the DIE descriptions. */
10918 static void
10919 output_compilation_unit_header (enum dwarf_unit_type ut)
10921 if (!XCOFF_DEBUGGING_INFO)
10923 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10924 dw2_asm_output_data (4, 0xffffffff,
10925 "Initial length escape value indicating 64-bit DWARF extension");
10926 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10927 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10928 "Length of Compilation Unit Info");
10931 output_dwarf_version ();
10932 if (dwarf_version >= 5)
10934 const char *name;
10935 switch (ut)
10937 case DW_UT_compile: name = "DW_UT_compile"; break;
10938 case DW_UT_type: name = "DW_UT_type"; break;
10939 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10940 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10941 default: gcc_unreachable ();
10943 dw2_asm_output_data (1, ut, "%s", name);
10944 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10947 debug_abbrev_section,
10948 "Offset Into Abbrev. Section");
10949 if (dwarf_version < 5)
10950 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10953 /* Output the compilation unit DIE and its children. */
10955 static void
10956 output_comp_unit (dw_die_ref die, int output_if_empty,
10957 const unsigned char *dwo_id)
10959 const char *secname, *oldsym;
10960 char *tmp;
10962 /* Unless we are outputting main CU, we may throw away empty ones. */
10963 if (!output_if_empty && die->die_child == NULL)
10964 return;
10966 /* Even if there are no children of this DIE, we must output the information
10967 about the compilation unit. Otherwise, on an empty translation unit, we
10968 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10969 will then complain when examining the file. First mark all the DIEs in
10970 this CU so we know which get local refs. */
10971 mark_dies (die);
10973 external_ref_hash_type *extern_map = optimize_external_refs (die);
10975 /* For now, optimize only the main CU, in order to optimize the rest
10976 we'd need to see all of them earlier. Leave the rest for post-linking
10977 tools like DWZ. */
10978 if (die == comp_unit_die ())
10979 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10981 build_abbrev_table (die, extern_map);
10983 optimize_abbrev_table ();
10985 delete extern_map;
10987 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10988 next_die_offset = (dwo_id
10989 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10990 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10991 calc_die_sizes (die);
10993 oldsym = die->die_id.die_symbol;
10994 if (oldsym && die->comdat_type_p)
10996 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10998 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10999 secname = tmp;
11000 die->die_id.die_symbol = NULL;
11001 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11003 else
11005 switch_to_section (debug_info_section);
11006 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11007 info_section_emitted = true;
11010 /* For LTO cross unit DIE refs we want a symbol on the start of the
11011 debuginfo section, not on the CU DIE. */
11012 if ((flag_generate_lto || flag_generate_offload) && oldsym)
11014 /* ??? No way to get visibility assembled without a decl. */
11015 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
11016 get_identifier (oldsym), char_type_node);
11017 TREE_PUBLIC (decl) = true;
11018 TREE_STATIC (decl) = true;
11019 DECL_ARTIFICIAL (decl) = true;
11020 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11021 DECL_VISIBILITY_SPECIFIED (decl) = true;
11022 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
11023 #ifdef ASM_WEAKEN_LABEL
11024 /* We prefer a .weak because that handles duplicates from duplicate
11025 archive members in a graceful way. */
11026 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
11027 #else
11028 targetm.asm_out.globalize_label (asm_out_file, oldsym);
11029 #endif
11030 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
11033 /* Output debugging information. */
11034 output_compilation_unit_header (dwo_id
11035 ? DW_UT_split_compile : DW_UT_compile);
11036 if (dwarf_version >= 5)
11038 if (dwo_id != NULL)
11039 for (int i = 0; i < 8; i++)
11040 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11042 output_die (die);
11044 /* Leave the marks on the main CU, so we can check them in
11045 output_pubnames. */
11046 if (oldsym)
11048 unmark_dies (die);
11049 die->die_id.die_symbol = oldsym;
11053 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
11054 and .debug_pubtypes. This is configured per-target, but can be
11055 overridden by the -gpubnames or -gno-pubnames options. */
11057 static inline bool
11058 want_pubnames (void)
11060 if (debug_info_level <= DINFO_LEVEL_TERSE)
11061 return false;
11062 if (debug_generate_pub_sections != -1)
11063 return debug_generate_pub_sections;
11064 return targetm.want_debug_pub_sections;
11067 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
11069 static void
11070 add_AT_pubnames (dw_die_ref die)
11072 if (want_pubnames ())
11073 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
11076 /* Add a string attribute value to a skeleton DIE. */
11078 static inline void
11079 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
11080 const char *str)
11082 dw_attr_node attr;
11083 struct indirect_string_node *node;
11085 if (! skeleton_debug_str_hash)
11086 skeleton_debug_str_hash
11087 = hash_table<indirect_string_hasher>::create_ggc (10);
11089 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
11090 find_string_form (node);
11091 if (node->form == DW_FORM_GNU_str_index)
11092 node->form = DW_FORM_strp;
11094 attr.dw_attr = attr_kind;
11095 attr.dw_attr_val.val_class = dw_val_class_str;
11096 attr.dw_attr_val.val_entry = NULL;
11097 attr.dw_attr_val.v.val_str = node;
11098 add_dwarf_attr (die, &attr);
11101 /* Helper function to generate top-level dies for skeleton debug_info and
11102 debug_types. */
11104 static void
11105 add_top_level_skeleton_die_attrs (dw_die_ref die)
11107 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
11108 const char *comp_dir = comp_dir_string ();
11110 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
11111 if (comp_dir != NULL)
11112 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
11113 add_AT_pubnames (die);
11114 add_AT_lineptr (die, dwarf_AT (DW_AT_addr_base), debug_addr_section_label);
11117 /* Output skeleton debug sections that point to the dwo file. */
11119 static void
11120 output_skeleton_debug_sections (dw_die_ref comp_unit,
11121 const unsigned char *dwo_id)
11123 /* These attributes will be found in the full debug_info section. */
11124 remove_AT (comp_unit, DW_AT_producer);
11125 remove_AT (comp_unit, DW_AT_language);
11127 switch_to_section (debug_skeleton_info_section);
11128 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
11130 /* Produce the skeleton compilation-unit header. This one differs enough from
11131 a normal CU header that it's better not to call output_compilation_unit
11132 header. */
11133 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11134 dw2_asm_output_data (4, 0xffffffff,
11135 "Initial length escape value indicating 64-bit "
11136 "DWARF extension");
11138 dw2_asm_output_data (DWARF_OFFSET_SIZE,
11139 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
11140 - DWARF_INITIAL_LENGTH_SIZE
11141 + size_of_die (comp_unit),
11142 "Length of Compilation Unit Info");
11143 output_dwarf_version ();
11144 if (dwarf_version >= 5)
11146 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
11147 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11149 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
11150 debug_skeleton_abbrev_section,
11151 "Offset Into Abbrev. Section");
11152 if (dwarf_version < 5)
11153 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11154 else
11155 for (int i = 0; i < 8; i++)
11156 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
11158 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
11159 output_die (comp_unit);
11161 /* Build the skeleton debug_abbrev section. */
11162 switch_to_section (debug_skeleton_abbrev_section);
11163 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
11165 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
11167 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
11170 /* Output a comdat type unit DIE and its children. */
11172 static void
11173 output_comdat_type_unit (comdat_type_node *node)
11175 const char *secname;
11176 char *tmp;
11177 int i;
11178 #if defined (OBJECT_FORMAT_ELF)
11179 tree comdat_key;
11180 #endif
11182 /* First mark all the DIEs in this CU so we know which get local refs. */
11183 mark_dies (node->root_die);
11185 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
11187 build_abbrev_table (node->root_die, extern_map);
11189 delete extern_map;
11190 extern_map = NULL;
11192 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
11193 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11194 calc_die_sizes (node->root_die);
11196 #if defined (OBJECT_FORMAT_ELF)
11197 if (dwarf_version >= 5)
11199 if (!dwarf_split_debug_info)
11200 secname = ".debug_info";
11201 else
11202 secname = ".debug_info.dwo";
11204 else if (!dwarf_split_debug_info)
11205 secname = ".debug_types";
11206 else
11207 secname = ".debug_types.dwo";
11209 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11210 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
11211 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11212 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11213 comdat_key = get_identifier (tmp);
11214 targetm.asm_out.named_section (secname,
11215 SECTION_DEBUG | SECTION_LINKONCE,
11216 comdat_key);
11217 #else
11218 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11219 sprintf (tmp, (dwarf_version >= 5
11220 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
11221 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11222 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11223 secname = tmp;
11224 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11225 #endif
11227 /* Output debugging information. */
11228 output_compilation_unit_header (dwarf_split_debug_info
11229 ? DW_UT_split_type : DW_UT_type);
11230 output_signature (node->signature, "Type Signature");
11231 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11232 "Offset to Type DIE");
11233 output_die (node->root_die);
11235 unmark_dies (node->root_die);
11238 /* Return the DWARF2/3 pubname associated with a decl. */
11240 static const char *
11241 dwarf2_name (tree decl, int scope)
11243 if (DECL_NAMELESS (decl))
11244 return NULL;
11245 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11248 /* Add a new entry to .debug_pubnames if appropriate. */
11250 static void
11251 add_pubname_string (const char *str, dw_die_ref die)
11253 pubname_entry e;
11255 e.die = die;
11256 e.name = xstrdup (str);
11257 vec_safe_push (pubname_table, e);
11260 static void
11261 add_pubname (tree decl, dw_die_ref die)
11263 if (!want_pubnames ())
11264 return;
11266 /* Don't add items to the table when we expect that the consumer will have
11267 just read the enclosing die. For example, if the consumer is looking at a
11268 class_member, it will either be inside the class already, or will have just
11269 looked up the class to find the member. Either way, searching the class is
11270 faster than searching the index. */
11271 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
11272 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11274 const char *name = dwarf2_name (decl, 1);
11276 if (name)
11277 add_pubname_string (name, die);
11281 /* Add an enumerator to the pubnames section. */
11283 static void
11284 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
11286 pubname_entry e;
11288 gcc_assert (scope_name);
11289 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
11290 e.die = die;
11291 vec_safe_push (pubname_table, e);
11294 /* Add a new entry to .debug_pubtypes if appropriate. */
11296 static void
11297 add_pubtype (tree decl, dw_die_ref die)
11299 pubname_entry e;
11301 if (!want_pubnames ())
11302 return;
11304 if ((TREE_PUBLIC (decl)
11305 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
11306 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11308 tree scope = NULL;
11309 const char *scope_name = "";
11310 const char *sep = is_cxx () ? "::" : ".";
11311 const char *name;
11313 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
11314 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
11316 scope_name = lang_hooks.dwarf_name (scope, 1);
11317 if (scope_name != NULL && scope_name[0] != '\0')
11318 scope_name = concat (scope_name, sep, NULL);
11319 else
11320 scope_name = "";
11323 if (TYPE_P (decl))
11324 name = type_tag (decl);
11325 else
11326 name = lang_hooks.dwarf_name (decl, 1);
11328 /* If we don't have a name for the type, there's no point in adding
11329 it to the table. */
11330 if (name != NULL && name[0] != '\0')
11332 e.die = die;
11333 e.name = concat (scope_name, name, NULL);
11334 vec_safe_push (pubtype_table, e);
11337 /* Although it might be more consistent to add the pubinfo for the
11338 enumerators as their dies are created, they should only be added if the
11339 enum type meets the criteria above. So rather than re-check the parent
11340 enum type whenever an enumerator die is created, just output them all
11341 here. This isn't protected by the name conditional because anonymous
11342 enums don't have names. */
11343 if (die->die_tag == DW_TAG_enumeration_type)
11345 dw_die_ref c;
11347 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
11352 /* Output a single entry in the pubnames table. */
11354 static void
11355 output_pubname (dw_offset die_offset, pubname_entry *entry)
11357 dw_die_ref die = entry->die;
11358 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
11360 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
11362 if (debug_generate_pub_sections == 2)
11364 /* This logic follows gdb's method for determining the value of the flag
11365 byte. */
11366 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
11367 switch (die->die_tag)
11369 case DW_TAG_typedef:
11370 case DW_TAG_base_type:
11371 case DW_TAG_subrange_type:
11372 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11373 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11374 break;
11375 case DW_TAG_enumerator:
11376 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11377 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11378 if (!is_cxx ())
11379 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11380 break;
11381 case DW_TAG_subprogram:
11382 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11383 GDB_INDEX_SYMBOL_KIND_FUNCTION);
11384 if (!is_ada ())
11385 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11386 break;
11387 case DW_TAG_constant:
11388 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11389 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11390 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11391 break;
11392 case DW_TAG_variable:
11393 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
11394 GDB_INDEX_SYMBOL_KIND_VARIABLE);
11395 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
11396 break;
11397 case DW_TAG_namespace:
11398 case DW_TAG_imported_declaration:
11399 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11400 break;
11401 case DW_TAG_class_type:
11402 case DW_TAG_interface_type:
11403 case DW_TAG_structure_type:
11404 case DW_TAG_union_type:
11405 case DW_TAG_enumeration_type:
11406 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
11407 if (!is_cxx ())
11408 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
11409 break;
11410 default:
11411 /* An unusual tag. Leave the flag-byte empty. */
11412 break;
11414 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
11415 "GDB-index flags");
11418 dw2_asm_output_nstring (entry->name, -1, "external name");
11422 /* Output the public names table used to speed up access to externally
11423 visible names; or the public types table used to find type definitions. */
11425 static void
11426 output_pubnames (vec<pubname_entry, va_gc> *names)
11428 unsigned i;
11429 unsigned long pubnames_length = size_of_pubnames (names);
11430 pubname_entry *pub;
11432 if (!XCOFF_DEBUGGING_INFO)
11434 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11435 dw2_asm_output_data (4, 0xffffffff,
11436 "Initial length escape value indicating 64-bit DWARF extension");
11437 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11438 "Pub Info Length");
11441 /* Version number for pubnames/pubtypes is independent of dwarf version. */
11442 dw2_asm_output_data (2, 2, "DWARF pubnames/pubtypes version");
11444 if (dwarf_split_debug_info)
11445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11446 debug_skeleton_info_section,
11447 "Offset of Compilation Unit Info");
11448 else
11449 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11450 debug_info_section,
11451 "Offset of Compilation Unit Info");
11452 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11453 "Compilation Unit Length");
11455 FOR_EACH_VEC_ELT (*names, i, pub)
11457 if (include_pubname_in_output (names, pub))
11459 dw_offset die_offset = pub->die->die_offset;
11461 /* We shouldn't see pubnames for DIEs outside of the main CU. */
11462 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
11463 gcc_assert (pub->die->die_mark);
11465 /* If we're putting types in their own .debug_types sections,
11466 the .debug_pubtypes table will still point to the compile
11467 unit (not the type unit), so we want to use the offset of
11468 the skeleton DIE (if there is one). */
11469 if (pub->die->comdat_type_p && names == pubtype_table)
11471 comdat_type_node *type_node = pub->die->die_id.die_type_node;
11473 if (type_node != NULL)
11474 die_offset = (type_node->skeleton_die != NULL
11475 ? type_node->skeleton_die->die_offset
11476 : comp_unit_die ()->die_offset);
11479 output_pubname (die_offset, pub);
11483 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11486 /* Output public names and types tables if necessary. */
11488 static void
11489 output_pubtables (void)
11491 if (!want_pubnames () || !info_section_emitted)
11492 return;
11494 switch_to_section (debug_pubnames_section);
11495 output_pubnames (pubname_table);
11496 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
11497 It shouldn't hurt to emit it always, since pure DWARF2 consumers
11498 simply won't look for the section. */
11499 switch_to_section (debug_pubtypes_section);
11500 output_pubnames (pubtype_table);
11504 /* Output the information that goes into the .debug_aranges table.
11505 Namely, define the beginning and ending address range of the
11506 text section generated for this compilation unit. */
11508 static void
11509 output_aranges (void)
11511 unsigned i;
11512 unsigned long aranges_length = size_of_aranges ();
11514 if (!XCOFF_DEBUGGING_INFO)
11516 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11517 dw2_asm_output_data (4, 0xffffffff,
11518 "Initial length escape value indicating 64-bit DWARF extension");
11519 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11520 "Length of Address Ranges Info");
11523 /* Version number for aranges is still 2, even up to DWARF5. */
11524 dw2_asm_output_data (2, 2, "DWARF aranges version");
11525 if (dwarf_split_debug_info)
11526 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
11527 debug_skeleton_info_section,
11528 "Offset of Compilation Unit Info");
11529 else
11530 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11531 debug_info_section,
11532 "Offset of Compilation Unit Info");
11533 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11534 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11536 /* We need to align to twice the pointer size here. */
11537 if (DWARF_ARANGES_PAD_SIZE)
11539 /* Pad using a 2 byte words so that padding is correct for any
11540 pointer size. */
11541 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11542 2 * DWARF2_ADDR_SIZE);
11543 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11544 dw2_asm_output_data (2, 0, NULL);
11547 /* It is necessary not to output these entries if the sections were
11548 not used; if the sections were not used, the length will be 0 and
11549 the address may end up as 0 if the section is discarded by ld
11550 --gc-sections, leaving an invalid (0, 0) entry that can be
11551 confused with the terminator. */
11552 if (text_section_used)
11554 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11555 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11556 text_section_label, "Length");
11558 if (cold_text_section_used)
11560 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11561 "Address");
11562 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11563 cold_text_section_label, "Length");
11566 if (have_multiple_function_sections)
11568 unsigned fde_idx;
11569 dw_fde_ref fde;
11571 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11573 if (DECL_IGNORED_P (fde->decl))
11574 continue;
11575 if (!fde->in_std_section)
11577 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11578 "Address");
11579 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11580 fde->dw_fde_begin, "Length");
11582 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11584 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11585 "Address");
11586 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11587 fde->dw_fde_second_begin, "Length");
11592 /* Output the terminator words. */
11593 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11594 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11597 /* Add a new entry to .debug_ranges. Return its index into
11598 ranges_table vector. */
11600 static unsigned int
11601 add_ranges_num (int num, bool maybe_new_sec)
11603 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11604 vec_safe_push (ranges_table, r);
11605 return vec_safe_length (ranges_table) - 1;
11608 /* Add a new entry to .debug_ranges corresponding to a block, or a
11609 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11610 this entry might be in a different section from previous range. */
11612 static unsigned int
11613 add_ranges (const_tree block, bool maybe_new_sec)
11615 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11618 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11619 chain, or middle entry of a chain that will be directly referred to. */
11621 static void
11622 note_rnglist_head (unsigned int offset)
11624 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11625 return;
11626 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11629 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11630 When using dwarf_split_debug_info, address attributes in dies destined
11631 for the final executable should be direct references--setting the
11632 parameter force_direct ensures this behavior. */
11634 static void
11635 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11636 bool *added, bool force_direct)
11638 unsigned int in_use = vec_safe_length (ranges_by_label);
11639 unsigned int offset;
11640 dw_ranges_by_label rbl = { begin, end };
11641 vec_safe_push (ranges_by_label, rbl);
11642 offset = add_ranges_num (-(int)in_use - 1, true);
11643 if (!*added)
11645 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11646 *added = true;
11647 note_rnglist_head (offset);
11651 /* Emit .debug_ranges section. */
11653 static void
11654 output_ranges (void)
11656 unsigned i;
11657 static const char *const start_fmt = "Offset %#x";
11658 const char *fmt = start_fmt;
11659 dw_ranges *r;
11661 switch_to_section (debug_ranges_section);
11662 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11663 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11665 int block_num = r->num;
11667 if (block_num > 0)
11669 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11670 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11672 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11673 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11675 /* If all code is in the text section, then the compilation
11676 unit base address defaults to DW_AT_low_pc, which is the
11677 base of the text section. */
11678 if (!have_multiple_function_sections)
11680 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11681 text_section_label,
11682 fmt, i * 2 * DWARF2_ADDR_SIZE);
11683 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11684 text_section_label, NULL);
11687 /* Otherwise, the compilation unit base address is zero,
11688 which allows us to use absolute addresses, and not worry
11689 about whether the target supports cross-section
11690 arithmetic. */
11691 else
11693 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11694 fmt, i * 2 * DWARF2_ADDR_SIZE);
11695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11698 fmt = NULL;
11701 /* Negative block_num stands for an index into ranges_by_label. */
11702 else if (block_num < 0)
11704 int lab_idx = - block_num - 1;
11706 if (!have_multiple_function_sections)
11708 gcc_unreachable ();
11709 #if 0
11710 /* If we ever use add_ranges_by_labels () for a single
11711 function section, all we have to do is to take out
11712 the #if 0 above. */
11713 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11714 (*ranges_by_label)[lab_idx].begin,
11715 text_section_label,
11716 fmt, i * 2 * DWARF2_ADDR_SIZE);
11717 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11718 (*ranges_by_label)[lab_idx].end,
11719 text_section_label, NULL);
11720 #endif
11722 else
11724 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11725 (*ranges_by_label)[lab_idx].begin,
11726 fmt, i * 2 * DWARF2_ADDR_SIZE);
11727 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11728 (*ranges_by_label)[lab_idx].end,
11729 NULL);
11732 else
11734 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11735 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11736 fmt = start_fmt;
11741 /* Non-zero if .debug_line_str should be used for .debug_line section
11742 strings or strings that are likely shareable with those. */
11743 #define DWARF5_USE_DEBUG_LINE_STR \
11744 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11745 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11746 /* FIXME: there is no .debug_line_str.dwo section, \
11747 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11748 && !dwarf_split_debug_info)
11750 /* Assign .debug_rnglists indexes. */
11752 static void
11753 index_rnglists (void)
11755 unsigned i;
11756 dw_ranges *r;
11758 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11759 if (r->label)
11760 r->idx = rnglist_idx++;
11763 /* Emit .debug_rnglists section. */
11765 static void
11766 output_rnglists (unsigned generation)
11768 unsigned i;
11769 dw_ranges *r;
11770 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11771 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11772 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11774 switch_to_section (debug_ranges_section);
11775 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11776 /* There are up to 4 unique ranges labels per generation.
11777 See also init_sections_and_labels. */
11778 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11779 2 + generation * 4);
11780 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11781 3 + generation * 4);
11782 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11783 dw2_asm_output_data (4, 0xffffffff,
11784 "Initial length escape value indicating "
11785 "64-bit DWARF extension");
11786 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11787 "Length of Range Lists");
11788 ASM_OUTPUT_LABEL (asm_out_file, l1);
11789 output_dwarf_version ();
11790 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11791 dw2_asm_output_data (1, 0, "Segment Size");
11792 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11793 about relocation sizes and primarily care about the size of .debug*
11794 sections in linked shared libraries and executables, then
11795 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11796 into it are usually larger than just DW_FORM_sec_offset offsets
11797 into the .debug_rnglists section. */
11798 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11799 "Offset Entry Count");
11800 if (dwarf_split_debug_info)
11802 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11803 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11804 if (r->label)
11805 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11806 ranges_base_label, NULL);
11809 const char *lab = "";
11810 unsigned int len = vec_safe_length (ranges_table);
11811 const char *base = NULL;
11812 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11814 int block_num = r->num;
11816 if (r->label)
11818 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11819 lab = r->label;
11821 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11822 base = NULL;
11823 if (block_num > 0)
11825 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11826 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11828 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11829 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11831 if (HAVE_AS_LEB128)
11833 /* If all code is in the text section, then the compilation
11834 unit base address defaults to DW_AT_low_pc, which is the
11835 base of the text section. */
11836 if (!have_multiple_function_sections)
11838 dw2_asm_output_data (1, DW_RLE_offset_pair,
11839 "DW_RLE_offset_pair (%s)", lab);
11840 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11841 "Range begin address (%s)", lab);
11842 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11843 "Range end address (%s)", lab);
11844 continue;
11846 if (base == NULL)
11848 dw_ranges *r2 = NULL;
11849 if (i < len - 1)
11850 r2 = &(*ranges_table)[i + 1];
11851 if (r2
11852 && r2->num != 0
11853 && r2->label == NULL
11854 && !r2->maybe_new_sec)
11856 dw2_asm_output_data (1, DW_RLE_base_address,
11857 "DW_RLE_base_address (%s)", lab);
11858 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11859 "Base address (%s)", lab);
11860 strcpy (basebuf, blabel);
11861 base = basebuf;
11864 if (base)
11866 dw2_asm_output_data (1, DW_RLE_offset_pair,
11867 "DW_RLE_offset_pair (%s)", lab);
11868 dw2_asm_output_delta_uleb128 (blabel, base,
11869 "Range begin address (%s)", lab);
11870 dw2_asm_output_delta_uleb128 (elabel, base,
11871 "Range end address (%s)", lab);
11872 continue;
11874 dw2_asm_output_data (1, DW_RLE_start_length,
11875 "DW_RLE_start_length (%s)", lab);
11876 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11877 "Range begin address (%s)", lab);
11878 dw2_asm_output_delta_uleb128 (elabel, blabel,
11879 "Range length (%s)", lab);
11881 else
11883 dw2_asm_output_data (1, DW_RLE_start_end,
11884 "DW_RLE_start_end (%s)", lab);
11885 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11886 "Range begin address (%s)", lab);
11887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11888 "Range end address (%s)", lab);
11892 /* Negative block_num stands for an index into ranges_by_label. */
11893 else if (block_num < 0)
11895 int lab_idx = - block_num - 1;
11896 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11897 const char *elabel = (*ranges_by_label)[lab_idx].end;
11899 if (!have_multiple_function_sections)
11900 gcc_unreachable ();
11901 if (HAVE_AS_LEB128)
11903 dw2_asm_output_data (1, DW_RLE_start_length,
11904 "DW_RLE_start_length (%s)", lab);
11905 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11906 "Range begin address (%s)", lab);
11907 dw2_asm_output_delta_uleb128 (elabel, blabel,
11908 "Range length (%s)", lab);
11910 else
11912 dw2_asm_output_data (1, DW_RLE_start_end,
11913 "DW_RLE_start_end (%s)", lab);
11914 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11915 "Range begin address (%s)", lab);
11916 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11917 "Range end address (%s)", lab);
11920 else
11921 dw2_asm_output_data (1, DW_RLE_end_of_list,
11922 "DW_RLE_end_of_list (%s)", lab);
11924 ASM_OUTPUT_LABEL (asm_out_file, l2);
11927 /* Data structure containing information about input files. */
11928 struct file_info
11930 const char *path; /* Complete file name. */
11931 const char *fname; /* File name part. */
11932 int length; /* Length of entire string. */
11933 struct dwarf_file_data * file_idx; /* Index in input file table. */
11934 int dir_idx; /* Index in directory table. */
11937 /* Data structure containing information about directories with source
11938 files. */
11939 struct dir_info
11941 const char *path; /* Path including directory name. */
11942 int length; /* Path length. */
11943 int prefix; /* Index of directory entry which is a prefix. */
11944 int count; /* Number of files in this directory. */
11945 int dir_idx; /* Index of directory used as base. */
11948 /* Callback function for file_info comparison. We sort by looking at
11949 the directories in the path. */
11951 static int
11952 file_info_cmp (const void *p1, const void *p2)
11954 const struct file_info *const s1 = (const struct file_info *) p1;
11955 const struct file_info *const s2 = (const struct file_info *) p2;
11956 const unsigned char *cp1;
11957 const unsigned char *cp2;
11959 /* Take care of file names without directories. We need to make sure that
11960 we return consistent values to qsort since some will get confused if
11961 we return the same value when identical operands are passed in opposite
11962 orders. So if neither has a directory, return 0 and otherwise return
11963 1 or -1 depending on which one has the directory. We want the one with
11964 the directory to sort after the one without, so all no directory files
11965 are at the start (normally only the compilation unit file). */
11966 if ((s1->path == s1->fname || s2->path == s2->fname))
11967 return (s2->path == s2->fname) - (s1->path == s1->fname);
11969 cp1 = (const unsigned char *) s1->path;
11970 cp2 = (const unsigned char *) s2->path;
11972 while (1)
11974 ++cp1;
11975 ++cp2;
11976 /* Reached the end of the first path? If so, handle like above,
11977 but now we want longer directory prefixes before shorter ones. */
11978 if ((cp1 == (const unsigned char *) s1->fname)
11979 || (cp2 == (const unsigned char *) s2->fname))
11980 return ((cp1 == (const unsigned char *) s1->fname)
11981 - (cp2 == (const unsigned char *) s2->fname));
11983 /* Character of current path component the same? */
11984 else if (*cp1 != *cp2)
11985 return *cp1 - *cp2;
11989 struct file_name_acquire_data
11991 struct file_info *files;
11992 int used_files;
11993 int max_files;
11996 /* Traversal function for the hash table. */
11999 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
12001 struct dwarf_file_data *d = *slot;
12002 struct file_info *fi;
12003 const char *f;
12005 gcc_assert (fnad->max_files >= d->emitted_number);
12007 if (! d->emitted_number)
12008 return 1;
12010 gcc_assert (fnad->max_files != fnad->used_files);
12012 fi = fnad->files + fnad->used_files++;
12014 /* Skip all leading "./". */
12015 f = d->filename;
12016 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12017 f += 2;
12019 /* Create a new array entry. */
12020 fi->path = f;
12021 fi->length = strlen (f);
12022 fi->file_idx = d;
12024 /* Search for the file name part. */
12025 f = strrchr (f, DIR_SEPARATOR);
12026 #if defined (DIR_SEPARATOR_2)
12028 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12030 if (g != NULL)
12032 if (f == NULL || f < g)
12033 f = g;
12036 #endif
12038 fi->fname = f == NULL ? fi->path : f + 1;
12039 return 1;
12042 /* Helper function for output_file_names. Emit a FORM encoded
12043 string STR, with assembly comment start ENTRY_KIND and
12044 index IDX */
12046 static void
12047 output_line_string (enum dwarf_form form, const char *str,
12048 const char *entry_kind, unsigned int idx)
12050 switch (form)
12052 case DW_FORM_string:
12053 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
12054 break;
12055 case DW_FORM_line_strp:
12056 if (!debug_line_str_hash)
12057 debug_line_str_hash
12058 = hash_table<indirect_string_hasher>::create_ggc (10);
12060 struct indirect_string_node *node;
12061 node = find_AT_string_in_table (str, debug_line_str_hash);
12062 set_indirect_string (node);
12063 node->form = form;
12064 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
12065 debug_line_str_section, "%s: %#x: \"%s\"",
12066 entry_kind, 0, node->str);
12067 break;
12068 default:
12069 gcc_unreachable ();
12073 /* Output the directory table and the file name table. We try to minimize
12074 the total amount of memory needed. A heuristic is used to avoid large
12075 slowdowns with many input files. */
12077 static void
12078 output_file_names (void)
12080 struct file_name_acquire_data fnad;
12081 int numfiles;
12082 struct file_info *files;
12083 struct dir_info *dirs;
12084 int *saved;
12085 int *savehere;
12086 int *backmap;
12087 int ndirs;
12088 int idx_offset;
12089 int i;
12091 if (!last_emitted_file)
12093 if (dwarf_version >= 5)
12095 dw2_asm_output_data (1, 0, "Directory entry format count");
12096 dw2_asm_output_data_uleb128 (0, "Directories count");
12097 dw2_asm_output_data (1, 0, "File name entry format count");
12098 dw2_asm_output_data_uleb128 (0, "File names count");
12100 else
12102 dw2_asm_output_data (1, 0, "End directory table");
12103 dw2_asm_output_data (1, 0, "End file name table");
12105 return;
12108 numfiles = last_emitted_file->emitted_number;
12110 /* Allocate the various arrays we need. */
12111 files = XALLOCAVEC (struct file_info, numfiles);
12112 dirs = XALLOCAVEC (struct dir_info, numfiles);
12114 fnad.files = files;
12115 fnad.used_files = 0;
12116 fnad.max_files = numfiles;
12117 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
12118 gcc_assert (fnad.used_files == fnad.max_files);
12120 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12122 /* Find all the different directories used. */
12123 dirs[0].path = files[0].path;
12124 dirs[0].length = files[0].fname - files[0].path;
12125 dirs[0].prefix = -1;
12126 dirs[0].count = 1;
12127 dirs[0].dir_idx = 0;
12128 files[0].dir_idx = 0;
12129 ndirs = 1;
12131 for (i = 1; i < numfiles; i++)
12132 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12133 && memcmp (dirs[ndirs - 1].path, files[i].path,
12134 dirs[ndirs - 1].length) == 0)
12136 /* Same directory as last entry. */
12137 files[i].dir_idx = ndirs - 1;
12138 ++dirs[ndirs - 1].count;
12140 else
12142 int j;
12144 /* This is a new directory. */
12145 dirs[ndirs].path = files[i].path;
12146 dirs[ndirs].length = files[i].fname - files[i].path;
12147 dirs[ndirs].count = 1;
12148 dirs[ndirs].dir_idx = ndirs;
12149 files[i].dir_idx = ndirs;
12151 /* Search for a prefix. */
12152 dirs[ndirs].prefix = -1;
12153 for (j = 0; j < ndirs; j++)
12154 if (dirs[j].length < dirs[ndirs].length
12155 && dirs[j].length > 1
12156 && (dirs[ndirs].prefix == -1
12157 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12158 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12159 dirs[ndirs].prefix = j;
12161 ++ndirs;
12164 /* Now to the actual work. We have to find a subset of the directories which
12165 allow expressing the file name using references to the directory table
12166 with the least amount of characters. We do not do an exhaustive search
12167 where we would have to check out every combination of every single
12168 possible prefix. Instead we use a heuristic which provides nearly optimal
12169 results in most cases and never is much off. */
12170 saved = XALLOCAVEC (int, ndirs);
12171 savehere = XALLOCAVEC (int, ndirs);
12173 memset (saved, '\0', ndirs * sizeof (saved[0]));
12174 for (i = 0; i < ndirs; i++)
12176 int j;
12177 int total;
12179 /* We can always save some space for the current directory. But this
12180 does not mean it will be enough to justify adding the directory. */
12181 savehere[i] = dirs[i].length;
12182 total = (savehere[i] - saved[i]) * dirs[i].count;
12184 for (j = i + 1; j < ndirs; j++)
12186 savehere[j] = 0;
12187 if (saved[j] < dirs[i].length)
12189 /* Determine whether the dirs[i] path is a prefix of the
12190 dirs[j] path. */
12191 int k;
12193 k = dirs[j].prefix;
12194 while (k != -1 && k != (int) i)
12195 k = dirs[k].prefix;
12197 if (k == (int) i)
12199 /* Yes it is. We can possibly save some memory by
12200 writing the filenames in dirs[j] relative to
12201 dirs[i]. */
12202 savehere[j] = dirs[i].length;
12203 total += (savehere[j] - saved[j]) * dirs[j].count;
12208 /* Check whether we can save enough to justify adding the dirs[i]
12209 directory. */
12210 if (total > dirs[i].length + 1)
12212 /* It's worthwhile adding. */
12213 for (j = i; j < ndirs; j++)
12214 if (savehere[j] > 0)
12216 /* Remember how much we saved for this directory so far. */
12217 saved[j] = savehere[j];
12219 /* Remember the prefix directory. */
12220 dirs[j].dir_idx = i;
12225 /* Emit the directory name table. */
12226 idx_offset = dirs[0].length > 0 ? 1 : 0;
12227 enum dwarf_form str_form = DW_FORM_string;
12228 enum dwarf_form idx_form = DW_FORM_udata;
12229 if (dwarf_version >= 5)
12231 const char *comp_dir = comp_dir_string ();
12232 if (comp_dir == NULL)
12233 comp_dir = "";
12234 dw2_asm_output_data (1, 1, "Directory entry format count");
12235 if (DWARF5_USE_DEBUG_LINE_STR)
12236 str_form = DW_FORM_line_strp;
12237 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12238 dw2_asm_output_data_uleb128 (str_form, "%s",
12239 get_DW_FORM_name (str_form));
12240 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
12241 if (str_form == DW_FORM_string)
12243 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
12244 for (i = 1 - idx_offset; i < ndirs; i++)
12245 dw2_asm_output_nstring (dirs[i].path,
12246 dirs[i].length
12247 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12248 "Directory Entry: %#x", i + idx_offset);
12250 else
12252 output_line_string (str_form, comp_dir, "Directory Entry", 0);
12253 for (i = 1 - idx_offset; i < ndirs; i++)
12255 const char *str
12256 = ggc_alloc_string (dirs[i].path,
12257 dirs[i].length
12258 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
12259 output_line_string (str_form, str, "Directory Entry",
12260 (unsigned) i + idx_offset);
12264 else
12266 for (i = 1 - idx_offset; i < ndirs; i++)
12267 dw2_asm_output_nstring (dirs[i].path,
12268 dirs[i].length
12269 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12270 "Directory Entry: %#x", i + idx_offset);
12272 dw2_asm_output_data (1, 0, "End directory table");
12275 /* We have to emit them in the order of emitted_number since that's
12276 used in the debug info generation. To do this efficiently we
12277 generate a back-mapping of the indices first. */
12278 backmap = XALLOCAVEC (int, numfiles);
12279 for (i = 0; i < numfiles; i++)
12280 backmap[files[i].file_idx->emitted_number - 1] = i;
12282 if (dwarf_version >= 5)
12284 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
12285 if (filename0 == NULL)
12286 filename0 = "";
12287 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
12288 DW_FORM_data2. Choose one based on the number of directories
12289 and how much space would they occupy in each encoding.
12290 If we have at most 256 directories, all indexes fit into
12291 a single byte, so DW_FORM_data1 is most compact (if there
12292 are at most 128 directories, DW_FORM_udata would be as
12293 compact as that, but not shorter and slower to decode). */
12294 if (ndirs + idx_offset <= 256)
12295 idx_form = DW_FORM_data1;
12296 /* If there are more than 65536 directories, we have to use
12297 DW_FORM_udata, DW_FORM_data2 can't refer to them.
12298 Otherwise, compute what space would occupy if all the indexes
12299 used DW_FORM_udata - sum - and compare that to how large would
12300 be DW_FORM_data2 encoding, and pick the more efficient one. */
12301 else if (ndirs + idx_offset <= 65536)
12303 unsigned HOST_WIDE_INT sum = 1;
12304 for (i = 0; i < numfiles; i++)
12306 int file_idx = backmap[i];
12307 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12308 sum += size_of_uleb128 (dir_idx);
12310 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
12311 idx_form = DW_FORM_data2;
12313 #ifdef VMS_DEBUGGING_INFO
12314 dw2_asm_output_data (1, 4, "File name entry format count");
12315 #else
12316 dw2_asm_output_data (1, 2, "File name entry format count");
12317 #endif
12318 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
12319 dw2_asm_output_data_uleb128 (str_form, "%s",
12320 get_DW_FORM_name (str_form));
12321 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
12322 "DW_LNCT_directory_index");
12323 dw2_asm_output_data_uleb128 (idx_form, "%s",
12324 get_DW_FORM_name (idx_form));
12325 #ifdef VMS_DEBUGGING_INFO
12326 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
12327 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12328 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
12329 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
12330 #endif
12331 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
12333 output_line_string (str_form, filename0, "File Entry", 0);
12335 /* Include directory index. */
12336 if (idx_form != DW_FORM_udata)
12337 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12338 0, NULL);
12339 else
12340 dw2_asm_output_data_uleb128 (0, NULL);
12342 #ifdef VMS_DEBUGGING_INFO
12343 dw2_asm_output_data_uleb128 (0, NULL);
12344 dw2_asm_output_data_uleb128 (0, NULL);
12345 #endif
12348 /* Now write all the file names. */
12349 for (i = 0; i < numfiles; i++)
12351 int file_idx = backmap[i];
12352 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12354 #ifdef VMS_DEBUGGING_INFO
12355 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12357 /* Setting these fields can lead to debugger miscomparisons,
12358 but VMS Debug requires them to be set correctly. */
12360 int ver;
12361 long long cdt;
12362 long siz;
12363 int maxfilelen = (strlen (files[file_idx].path)
12364 + dirs[dir_idx].length
12365 + MAX_VMS_VERSION_LEN + 1);
12366 char *filebuf = XALLOCAVEC (char, maxfilelen);
12368 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12369 snprintf (filebuf, maxfilelen, "%s;%d",
12370 files[file_idx].path + dirs[dir_idx].length, ver);
12372 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
12374 /* Include directory index. */
12375 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12376 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12377 dir_idx + idx_offset, NULL);
12378 else
12379 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12381 /* Modification time. */
12382 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12383 &cdt, 0, 0, 0) == 0)
12384 ? cdt : 0, NULL);
12386 /* File length in bytes. */
12387 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
12388 0, &siz, 0, 0) == 0)
12389 ? siz : 0, NULL);
12390 #else
12391 output_line_string (str_form,
12392 files[file_idx].path + dirs[dir_idx].length,
12393 "File Entry", (unsigned) i + 1);
12395 /* Include directory index. */
12396 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
12397 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
12398 dir_idx + idx_offset, NULL);
12399 else
12400 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12402 if (dwarf_version >= 5)
12403 continue;
12405 /* Modification time. */
12406 dw2_asm_output_data_uleb128 (0, NULL);
12408 /* File length in bytes. */
12409 dw2_asm_output_data_uleb128 (0, NULL);
12410 #endif /* VMS_DEBUGGING_INFO */
12413 if (dwarf_version < 5)
12414 dw2_asm_output_data (1, 0, "End file name table");
12418 /* Output one line number table into the .debug_line section. */
12420 static void
12421 output_one_line_info_table (dw_line_info_table *table)
12423 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12424 unsigned int current_line = 1;
12425 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12426 dw_line_info_entry *ent, *prev_addr;
12427 size_t i;
12428 unsigned int view;
12430 view = 0;
12432 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
12434 switch (ent->opcode)
12436 case LI_set_address:
12437 /* ??? Unfortunately, we have little choice here currently, and
12438 must always use the most general form. GCC does not know the
12439 address delta itself, so we can't use DW_LNS_advance_pc. Many
12440 ports do have length attributes which will give an upper bound
12441 on the address range. We could perhaps use length attributes
12442 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
12443 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12445 view = 0;
12447 /* This can handle any delta. This takes
12448 4+DWARF2_ADDR_SIZE bytes. */
12449 dw2_asm_output_data (1, 0, "set address %s%s", line_label,
12450 debug_variable_location_views
12451 ? ", reset view to 0" : "");
12452 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12453 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12454 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12456 prev_addr = ent;
12457 break;
12459 case LI_adv_address:
12461 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12462 char prev_label[MAX_ARTIFICIAL_LABEL_BYTES];
12463 ASM_GENERATE_INTERNAL_LABEL (prev_label, LINE_CODE_LABEL, prev_addr->val);
12465 view++;
12467 dw2_asm_output_data (1, DW_LNS_fixed_advance_pc, "fixed advance PC, increment view to %i", view);
12468 dw2_asm_output_delta (2, line_label, prev_label,
12469 "from %s to %s", prev_label, line_label);
12471 prev_addr = ent;
12472 break;
12475 case LI_set_line:
12476 if (ent->val == current_line)
12478 /* We still need to start a new row, so output a copy insn. */
12479 dw2_asm_output_data (1, DW_LNS_copy,
12480 "copy line %u", current_line);
12482 else
12484 int line_offset = ent->val - current_line;
12485 int line_delta = line_offset - DWARF_LINE_BASE;
12487 current_line = ent->val;
12488 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12490 /* This can handle deltas from -10 to 234, using the current
12491 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12492 This takes 1 byte. */
12493 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12494 "line %u", current_line);
12496 else
12498 /* This can handle any delta. This takes at least 4 bytes,
12499 depending on the value being encoded. */
12500 dw2_asm_output_data (1, DW_LNS_advance_line,
12501 "advance to line %u", current_line);
12502 dw2_asm_output_data_sleb128 (line_offset, NULL);
12503 dw2_asm_output_data (1, DW_LNS_copy, NULL);
12506 break;
12508 case LI_set_file:
12509 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12510 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12511 break;
12513 case LI_set_column:
12514 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12515 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12516 break;
12518 case LI_negate_stmt:
12519 current_is_stmt = !current_is_stmt;
12520 dw2_asm_output_data (1, DW_LNS_negate_stmt,
12521 "is_stmt %d", current_is_stmt);
12522 break;
12524 case LI_set_prologue_end:
12525 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12526 "set prologue end");
12527 break;
12529 case LI_set_epilogue_begin:
12530 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12531 "set epilogue begin");
12532 break;
12534 case LI_set_discriminator:
12535 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12536 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12537 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12538 dw2_asm_output_data_uleb128 (ent->val, NULL);
12539 break;
12543 /* Emit debug info for the address of the end of the table. */
12544 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12545 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12546 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12547 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12549 dw2_asm_output_data (1, 0, "end sequence");
12550 dw2_asm_output_data_uleb128 (1, NULL);
12551 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12554 /* Output the source line number correspondence information. This
12555 information goes into the .debug_line section. */
12557 static void
12558 output_line_info (bool prologue_only)
12560 static unsigned int generation;
12561 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
12562 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
12563 bool saw_one = false;
12564 int opc;
12566 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
12567 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
12568 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
12569 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
12571 if (!XCOFF_DEBUGGING_INFO)
12573 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12574 dw2_asm_output_data (4, 0xffffffff,
12575 "Initial length escape value indicating 64-bit DWARF extension");
12576 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12577 "Length of Source Line Info");
12580 ASM_OUTPUT_LABEL (asm_out_file, l1);
12582 output_dwarf_version ();
12583 if (dwarf_version >= 5)
12585 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12586 dw2_asm_output_data (1, 0, "Segment Size");
12588 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12589 ASM_OUTPUT_LABEL (asm_out_file, p1);
12591 /* Define the architecture-dependent minimum instruction length (in bytes).
12592 In this implementation of DWARF, this field is used for information
12593 purposes only. Since GCC generates assembly language, we have no
12594 a priori knowledge of how many instruction bytes are generated for each
12595 source line, and therefore can use only the DW_LNE_set_address and
12596 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12597 this as '1', which is "correct enough" for all architectures,
12598 and don't let the target override. */
12599 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12601 if (dwarf_version >= 4)
12602 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12603 "Maximum Operations Per Instruction");
12604 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12605 "Default is_stmt_start flag");
12606 dw2_asm_output_data (1, DWARF_LINE_BASE,
12607 "Line Base Value (Special Opcodes)");
12608 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12609 "Line Range Value (Special Opcodes)");
12610 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12611 "Special Opcode Base");
12613 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12615 int n_op_args;
12616 switch (opc)
12618 case DW_LNS_advance_pc:
12619 case DW_LNS_advance_line:
12620 case DW_LNS_set_file:
12621 case DW_LNS_set_column:
12622 case DW_LNS_fixed_advance_pc:
12623 case DW_LNS_set_isa:
12624 n_op_args = 1;
12625 break;
12626 default:
12627 n_op_args = 0;
12628 break;
12631 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12632 opc, n_op_args);
12635 /* Write out the information about the files we use. */
12636 output_file_names ();
12637 ASM_OUTPUT_LABEL (asm_out_file, p2);
12638 if (prologue_only)
12640 /* Output the marker for the end of the line number info. */
12641 ASM_OUTPUT_LABEL (asm_out_file, l2);
12642 return;
12645 if (separate_line_info)
12647 dw_line_info_table *table;
12648 size_t i;
12650 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12651 if (table->in_use)
12653 output_one_line_info_table (table);
12654 saw_one = true;
12657 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12659 output_one_line_info_table (cold_text_section_line_info);
12660 saw_one = true;
12663 /* ??? Some Darwin linkers crash on a .debug_line section with no
12664 sequences. Further, merely a DW_LNE_end_sequence entry is not
12665 sufficient -- the address column must also be initialized.
12666 Make sure to output at least one set_address/end_sequence pair,
12667 choosing .text since that section is always present. */
12668 if (text_section_line_info->in_use || !saw_one)
12669 output_one_line_info_table (text_section_line_info);
12671 /* Output the marker for the end of the line number info. */
12672 ASM_OUTPUT_LABEL (asm_out_file, l2);
12675 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12677 static inline bool
12678 need_endianity_attribute_p (bool reverse)
12680 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12683 /* Given a pointer to a tree node for some base type, return a pointer to
12684 a DIE that describes the given type. REVERSE is true if the type is
12685 to be interpreted in the reverse storage order wrt the target order.
12687 This routine must only be called for GCC type nodes that correspond to
12688 Dwarf base (fundamental) types. */
12690 static dw_die_ref
12691 base_type_die (tree type, bool reverse)
12693 dw_die_ref base_type_result;
12694 enum dwarf_type encoding;
12695 bool fpt_used = false;
12696 struct fixed_point_type_info fpt_info;
12697 tree type_bias = NULL_TREE;
12699 /* If this is a subtype that should not be emitted as a subrange type,
12700 use the base type. See subrange_type_for_debug_p. */
12701 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12702 type = TREE_TYPE (type);
12704 switch (TREE_CODE (type))
12706 case INTEGER_TYPE:
12707 if ((dwarf_version >= 4 || !dwarf_strict)
12708 && TYPE_NAME (type)
12709 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12710 && DECL_IS_BUILTIN (TYPE_NAME (type))
12711 && DECL_NAME (TYPE_NAME (type)))
12713 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12714 if (strcmp (name, "char16_t") == 0
12715 || strcmp (name, "char32_t") == 0)
12717 encoding = DW_ATE_UTF;
12718 break;
12721 if ((dwarf_version >= 3 || !dwarf_strict)
12722 && lang_hooks.types.get_fixed_point_type_info)
12724 memset (&fpt_info, 0, sizeof (fpt_info));
12725 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12727 fpt_used = true;
12728 encoding = ((TYPE_UNSIGNED (type))
12729 ? DW_ATE_unsigned_fixed
12730 : DW_ATE_signed_fixed);
12731 break;
12734 if (TYPE_STRING_FLAG (type))
12736 if (TYPE_UNSIGNED (type))
12737 encoding = DW_ATE_unsigned_char;
12738 else
12739 encoding = DW_ATE_signed_char;
12741 else if (TYPE_UNSIGNED (type))
12742 encoding = DW_ATE_unsigned;
12743 else
12744 encoding = DW_ATE_signed;
12746 if (!dwarf_strict
12747 && lang_hooks.types.get_type_bias)
12748 type_bias = lang_hooks.types.get_type_bias (type);
12749 break;
12751 case REAL_TYPE:
12752 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12754 if (dwarf_version >= 3 || !dwarf_strict)
12755 encoding = DW_ATE_decimal_float;
12756 else
12757 encoding = DW_ATE_lo_user;
12759 else
12760 encoding = DW_ATE_float;
12761 break;
12763 case FIXED_POINT_TYPE:
12764 if (!(dwarf_version >= 3 || !dwarf_strict))
12765 encoding = DW_ATE_lo_user;
12766 else if (TYPE_UNSIGNED (type))
12767 encoding = DW_ATE_unsigned_fixed;
12768 else
12769 encoding = DW_ATE_signed_fixed;
12770 break;
12772 /* Dwarf2 doesn't know anything about complex ints, so use
12773 a user defined type for it. */
12774 case COMPLEX_TYPE:
12775 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12776 encoding = DW_ATE_complex_float;
12777 else
12778 encoding = DW_ATE_lo_user;
12779 break;
12781 case BOOLEAN_TYPE:
12782 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12783 encoding = DW_ATE_boolean;
12784 break;
12786 default:
12787 /* No other TREE_CODEs are Dwarf fundamental types. */
12788 gcc_unreachable ();
12791 base_type_result = new_die_raw (DW_TAG_base_type);
12793 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12794 int_size_in_bytes (type));
12795 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12797 if (need_endianity_attribute_p (reverse))
12798 add_AT_unsigned (base_type_result, DW_AT_endianity,
12799 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12801 add_alignment_attribute (base_type_result, type);
12803 if (fpt_used)
12805 switch (fpt_info.scale_factor_kind)
12807 case fixed_point_scale_factor_binary:
12808 add_AT_int (base_type_result, DW_AT_binary_scale,
12809 fpt_info.scale_factor.binary);
12810 break;
12812 case fixed_point_scale_factor_decimal:
12813 add_AT_int (base_type_result, DW_AT_decimal_scale,
12814 fpt_info.scale_factor.decimal);
12815 break;
12817 case fixed_point_scale_factor_arbitrary:
12818 /* Arbitrary scale factors cannot be described in standard DWARF,
12819 yet. */
12820 if (!dwarf_strict)
12822 /* Describe the scale factor as a rational constant. */
12823 const dw_die_ref scale_factor
12824 = new_die (DW_TAG_constant, comp_unit_die (), type);
12826 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12827 fpt_info.scale_factor.arbitrary.numerator);
12828 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12829 fpt_info.scale_factor.arbitrary.denominator);
12831 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12833 break;
12835 default:
12836 gcc_unreachable ();
12840 if (type_bias)
12841 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12842 dw_scalar_form_constant
12843 | dw_scalar_form_exprloc
12844 | dw_scalar_form_reference,
12845 NULL);
12847 return base_type_result;
12850 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12851 named 'auto' in its type: return true for it, false otherwise. */
12853 static inline bool
12854 is_cxx_auto (tree type)
12856 if (is_cxx ())
12858 tree name = TYPE_IDENTIFIER (type);
12859 if (name == get_identifier ("auto")
12860 || name == get_identifier ("decltype(auto)"))
12861 return true;
12863 return false;
12866 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12867 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12869 static inline int
12870 is_base_type (tree type)
12872 switch (TREE_CODE (type))
12874 case INTEGER_TYPE:
12875 case REAL_TYPE:
12876 case FIXED_POINT_TYPE:
12877 case COMPLEX_TYPE:
12878 case BOOLEAN_TYPE:
12879 case POINTER_BOUNDS_TYPE:
12880 return 1;
12882 case VOID_TYPE:
12883 case ARRAY_TYPE:
12884 case RECORD_TYPE:
12885 case UNION_TYPE:
12886 case QUAL_UNION_TYPE:
12887 case ENUMERAL_TYPE:
12888 case FUNCTION_TYPE:
12889 case METHOD_TYPE:
12890 case POINTER_TYPE:
12891 case REFERENCE_TYPE:
12892 case NULLPTR_TYPE:
12893 case OFFSET_TYPE:
12894 case LANG_TYPE:
12895 case VECTOR_TYPE:
12896 return 0;
12898 default:
12899 if (is_cxx_auto (type))
12900 return 0;
12901 gcc_unreachable ();
12904 return 0;
12907 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12908 node, return the size in bits for the type if it is a constant, or else
12909 return the alignment for the type if the type's size is not constant, or
12910 else return BITS_PER_WORD if the type actually turns out to be an
12911 ERROR_MARK node. */
12913 static inline unsigned HOST_WIDE_INT
12914 simple_type_size_in_bits (const_tree type)
12916 if (TREE_CODE (type) == ERROR_MARK)
12917 return BITS_PER_WORD;
12918 else if (TYPE_SIZE (type) == NULL_TREE)
12919 return 0;
12920 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12921 return tree_to_uhwi (TYPE_SIZE (type));
12922 else
12923 return TYPE_ALIGN (type);
12926 /* Similarly, but return an offset_int instead of UHWI. */
12928 static inline offset_int
12929 offset_int_type_size_in_bits (const_tree type)
12931 if (TREE_CODE (type) == ERROR_MARK)
12932 return BITS_PER_WORD;
12933 else if (TYPE_SIZE (type) == NULL_TREE)
12934 return 0;
12935 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12936 return wi::to_offset (TYPE_SIZE (type));
12937 else
12938 return TYPE_ALIGN (type);
12941 /* Given a pointer to a tree node for a subrange type, return a pointer
12942 to a DIE that describes the given type. */
12944 static dw_die_ref
12945 subrange_type_die (tree type, tree low, tree high, tree bias,
12946 dw_die_ref context_die)
12948 dw_die_ref subrange_die;
12949 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12951 if (context_die == NULL)
12952 context_die = comp_unit_die ();
12954 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12956 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12958 /* The size of the subrange type and its base type do not match,
12959 so we need to generate a size attribute for the subrange type. */
12960 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12963 add_alignment_attribute (subrange_die, type);
12965 if (low)
12966 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12967 if (high)
12968 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12969 if (bias && !dwarf_strict)
12970 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12971 dw_scalar_form_constant
12972 | dw_scalar_form_exprloc
12973 | dw_scalar_form_reference,
12974 NULL);
12976 return subrange_die;
12979 /* Returns the (const and/or volatile) cv_qualifiers associated with
12980 the decl node. This will normally be augmented with the
12981 cv_qualifiers of the underlying type in add_type_attribute. */
12983 static int
12984 decl_quals (const_tree decl)
12986 return ((TREE_READONLY (decl)
12987 /* The C++ front-end correctly marks reference-typed
12988 variables as readonly, but from a language (and debug
12989 info) standpoint they are not const-qualified. */
12990 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12991 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12992 | (TREE_THIS_VOLATILE (decl)
12993 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12996 /* Determine the TYPE whose qualifiers match the largest strict subset
12997 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12998 qualifiers outside QUAL_MASK. */
13000 static int
13001 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
13003 tree t;
13004 int best_rank = 0, best_qual = 0, max_rank;
13006 type_quals &= qual_mask;
13007 max_rank = popcount_hwi (type_quals) - 1;
13009 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
13010 t = TYPE_NEXT_VARIANT (t))
13012 int q = TYPE_QUALS (t) & qual_mask;
13014 if ((q & type_quals) == q && q != type_quals
13015 && check_base_type (t, type))
13017 int rank = popcount_hwi (q);
13019 if (rank > best_rank)
13021 best_rank = rank;
13022 best_qual = q;
13027 return best_qual;
13030 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
13031 static const dwarf_qual_info_t dwarf_qual_info[] =
13033 { TYPE_QUAL_CONST, DW_TAG_const_type },
13034 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
13035 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
13036 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
13038 static const unsigned int dwarf_qual_info_size
13039 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
13041 /* If DIE is a qualified DIE of some base DIE with the same parent,
13042 return the base DIE, otherwise return NULL. Set MASK to the
13043 qualifiers added compared to the returned DIE. */
13045 static dw_die_ref
13046 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
13048 unsigned int i;
13049 for (i = 0; i < dwarf_qual_info_size; i++)
13050 if (die->die_tag == dwarf_qual_info[i].t)
13051 break;
13052 if (i == dwarf_qual_info_size)
13053 return NULL;
13054 if (vec_safe_length (die->die_attr) != 1)
13055 return NULL;
13056 dw_die_ref type = get_AT_ref (die, DW_AT_type);
13057 if (type == NULL || type->die_parent != die->die_parent)
13058 return NULL;
13059 *mask |= dwarf_qual_info[i].q;
13060 if (depth)
13062 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
13063 if (ret)
13064 return ret;
13066 return type;
13069 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13070 entry that chains the modifiers specified by CV_QUALS in front of the
13071 given type. REVERSE is true if the type is to be interpreted in the
13072 reverse storage order wrt the target order. */
13074 static dw_die_ref
13075 modified_type_die (tree type, int cv_quals, bool reverse,
13076 dw_die_ref context_die)
13078 enum tree_code code = TREE_CODE (type);
13079 dw_die_ref mod_type_die;
13080 dw_die_ref sub_die = NULL;
13081 tree item_type = NULL;
13082 tree qualified_type;
13083 tree name, low, high;
13084 dw_die_ref mod_scope;
13085 /* Only these cv-qualifiers are currently handled. */
13086 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
13087 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
13088 ENCODE_QUAL_ADDR_SPACE(~0U));
13089 const bool reverse_base_type
13090 = need_endianity_attribute_p (reverse) && is_base_type (type);
13092 if (code == ERROR_MARK)
13093 return NULL;
13095 if (lang_hooks.types.get_debug_type)
13097 tree debug_type = lang_hooks.types.get_debug_type (type);
13099 if (debug_type != NULL_TREE && debug_type != type)
13100 return modified_type_die (debug_type, cv_quals, reverse, context_die);
13103 cv_quals &= cv_qual_mask;
13105 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
13106 tag modifier (and not an attribute) old consumers won't be able
13107 to handle it. */
13108 if (dwarf_version < 3)
13109 cv_quals &= ~TYPE_QUAL_RESTRICT;
13111 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
13112 if (dwarf_version < 5)
13113 cv_quals &= ~TYPE_QUAL_ATOMIC;
13115 /* See if we already have the appropriately qualified variant of
13116 this type. */
13117 qualified_type = get_qualified_type (type, cv_quals);
13119 if (qualified_type == sizetype)
13121 /* Try not to expose the internal sizetype type's name. */
13122 if (TYPE_NAME (qualified_type)
13123 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13125 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13127 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13128 && (TYPE_PRECISION (t)
13129 == TYPE_PRECISION (qualified_type))
13130 && (TYPE_UNSIGNED (t)
13131 == TYPE_UNSIGNED (qualified_type)));
13132 qualified_type = t;
13134 else if (qualified_type == sizetype
13135 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
13136 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
13137 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
13138 qualified_type = size_type_node;
13141 /* If we do, then we can just use its DIE, if it exists. */
13142 if (qualified_type)
13144 mod_type_die = lookup_type_die (qualified_type);
13146 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
13147 dealt with specially: the DIE with the attribute, if it exists, is
13148 placed immediately after the regular DIE for the same base type. */
13149 if (mod_type_die
13150 && (!reverse_base_type
13151 || ((mod_type_die = mod_type_die->die_sib) != NULL
13152 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
13153 return mod_type_die;
13156 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13158 /* Handle C typedef types. */
13159 if (name
13160 && TREE_CODE (name) == TYPE_DECL
13161 && DECL_ORIGINAL_TYPE (name)
13162 && !DECL_ARTIFICIAL (name))
13164 tree dtype = TREE_TYPE (name);
13166 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
13167 if (qualified_type == dtype && !reverse_base_type)
13169 tree origin = decl_ultimate_origin (name);
13171 /* Typedef variants that have an abstract origin don't get their own
13172 type DIE (see gen_typedef_die), so fall back on the ultimate
13173 abstract origin instead. */
13174 if (origin != NULL && origin != name)
13175 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
13176 context_die);
13178 /* For a named type, use the typedef. */
13179 gen_type_die (qualified_type, context_die);
13180 return lookup_type_die (qualified_type);
13182 else
13184 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
13185 dquals &= cv_qual_mask;
13186 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
13187 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
13188 /* cv-unqualified version of named type. Just use
13189 the unnamed type to which it refers. */
13190 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
13191 reverse, context_die);
13192 /* Else cv-qualified version of named type; fall through. */
13196 mod_scope = scope_die_for (type, context_die);
13198 if (cv_quals)
13200 int sub_quals = 0, first_quals = 0;
13201 unsigned i;
13202 dw_die_ref first = NULL, last = NULL;
13204 /* Determine a lesser qualified type that most closely matches
13205 this one. Then generate DW_TAG_* entries for the remaining
13206 qualifiers. */
13207 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
13208 cv_qual_mask);
13209 if (sub_quals && use_debug_types)
13211 bool needed = false;
13212 /* If emitting type units, make sure the order of qualifiers
13213 is canonical. Thus, start from unqualified type if
13214 an earlier qualifier is missing in sub_quals, but some later
13215 one is present there. */
13216 for (i = 0; i < dwarf_qual_info_size; i++)
13217 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13218 needed = true;
13219 else if (needed && (dwarf_qual_info[i].q & cv_quals))
13221 sub_quals = 0;
13222 break;
13225 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
13226 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
13228 /* As not all intermediate qualified DIEs have corresponding
13229 tree types, ensure that qualified DIEs in the same scope
13230 as their DW_AT_type are emitted after their DW_AT_type,
13231 only with other qualified DIEs for the same type possibly
13232 in between them. Determine the range of such qualified
13233 DIEs now (first being the base type, last being corresponding
13234 last qualified DIE for it). */
13235 unsigned int count = 0;
13236 first = qualified_die_p (mod_type_die, &first_quals,
13237 dwarf_qual_info_size);
13238 if (first == NULL)
13239 first = mod_type_die;
13240 gcc_assert ((first_quals & ~sub_quals) == 0);
13241 for (count = 0, last = first;
13242 count < (1U << dwarf_qual_info_size);
13243 count++, last = last->die_sib)
13245 int quals = 0;
13246 if (last == mod_scope->die_child)
13247 break;
13248 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
13249 != first)
13250 break;
13254 for (i = 0; i < dwarf_qual_info_size; i++)
13255 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
13257 dw_die_ref d;
13258 if (first && first != last)
13260 for (d = first->die_sib; ; d = d->die_sib)
13262 int quals = 0;
13263 qualified_die_p (d, &quals, dwarf_qual_info_size);
13264 if (quals == (first_quals | dwarf_qual_info[i].q))
13265 break;
13266 if (d == last)
13268 d = NULL;
13269 break;
13272 if (d)
13274 mod_type_die = d;
13275 continue;
13278 if (first)
13280 d = new_die_raw (dwarf_qual_info[i].t);
13281 add_child_die_after (mod_scope, d, last);
13282 last = d;
13284 else
13285 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
13286 if (mod_type_die)
13287 add_AT_die_ref (d, DW_AT_type, mod_type_die);
13288 mod_type_die = d;
13289 first_quals |= dwarf_qual_info[i].q;
13292 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
13294 dwarf_tag tag = DW_TAG_pointer_type;
13295 if (code == REFERENCE_TYPE)
13297 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13298 tag = DW_TAG_rvalue_reference_type;
13299 else
13300 tag = DW_TAG_reference_type;
13302 mod_type_die = new_die (tag, mod_scope, type);
13304 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13305 simple_type_size_in_bits (type) / BITS_PER_UNIT);
13306 add_alignment_attribute (mod_type_die, type);
13307 item_type = TREE_TYPE (type);
13309 addr_space_t as = TYPE_ADDR_SPACE (item_type);
13310 if (!ADDR_SPACE_GENERIC_P (as))
13312 int action = targetm.addr_space.debug (as);
13313 if (action >= 0)
13315 /* Positive values indicate an address_class. */
13316 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
13318 else
13320 /* Negative values indicate an (inverted) segment base reg. */
13321 dw_loc_descr_ref d
13322 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
13323 add_AT_loc (mod_type_die, DW_AT_segment, d);
13327 else if (code == INTEGER_TYPE
13328 && TREE_TYPE (type) != NULL_TREE
13329 && subrange_type_for_debug_p (type, &low, &high))
13331 tree bias = NULL_TREE;
13332 if (lang_hooks.types.get_type_bias)
13333 bias = lang_hooks.types.get_type_bias (type);
13334 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
13335 item_type = TREE_TYPE (type);
13337 else if (is_base_type (type))
13339 mod_type_die = base_type_die (type, reverse);
13341 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
13342 if (reverse_base_type)
13344 dw_die_ref after_die
13345 = modified_type_die (type, cv_quals, false, context_die);
13346 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
13348 else
13349 add_child_die (comp_unit_die (), mod_type_die);
13351 add_pubtype (type, mod_type_die);
13353 else
13355 gen_type_die (type, context_die);
13357 /* We have to get the type_main_variant here (and pass that to the
13358 `lookup_type_die' routine) because the ..._TYPE node we have
13359 might simply be a *copy* of some original type node (where the
13360 copy was created to help us keep track of typedef names) and
13361 that copy might have a different TYPE_UID from the original
13362 ..._TYPE node. */
13363 if (TREE_CODE (type) == FUNCTION_TYPE
13364 || TREE_CODE (type) == METHOD_TYPE)
13366 /* For function/method types, can't just use type_main_variant here,
13367 because that can have different ref-qualifiers for C++,
13368 but try to canonicalize. */
13369 tree main = TYPE_MAIN_VARIANT (type);
13370 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
13371 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
13372 && check_base_type (t, main)
13373 && check_lang_type (t, type))
13374 return lookup_type_die (t);
13375 return lookup_type_die (type);
13377 else if (TREE_CODE (type) != VECTOR_TYPE
13378 && TREE_CODE (type) != ARRAY_TYPE)
13379 return lookup_type_die (type_main_variant (type));
13380 else
13381 /* Vectors have the debugging information in the type,
13382 not the main variant. */
13383 return lookup_type_die (type);
13386 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
13387 don't output a DW_TAG_typedef, since there isn't one in the
13388 user's program; just attach a DW_AT_name to the type.
13389 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13390 if the base type already has the same name. */
13391 if (name
13392 && ((TREE_CODE (name) != TYPE_DECL
13393 && (qualified_type == TYPE_MAIN_VARIANT (type)
13394 || (cv_quals == TYPE_UNQUALIFIED)))
13395 || (TREE_CODE (name) == TYPE_DECL
13396 && TREE_TYPE (name) == qualified_type
13397 && DECL_NAME (name))))
13399 if (TREE_CODE (name) == TYPE_DECL)
13400 /* Could just call add_name_and_src_coords_attributes here,
13401 but since this is a builtin type it doesn't have any
13402 useful source coordinates anyway. */
13403 name = DECL_NAME (name);
13404 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13406 /* This probably indicates a bug. */
13407 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13409 name = TYPE_IDENTIFIER (type);
13410 add_name_attribute (mod_type_die,
13411 name ? IDENTIFIER_POINTER (name) : "__unknown__");
13414 if (qualified_type && !reverse_base_type)
13415 equate_type_number_to_die (qualified_type, mod_type_die);
13417 if (item_type)
13418 /* We must do this after the equate_type_number_to_die call, in case
13419 this is a recursive type. This ensures that the modified_type_die
13420 recursion will terminate even if the type is recursive. Recursive
13421 types are possible in Ada. */
13422 sub_die = modified_type_die (item_type,
13423 TYPE_QUALS_NO_ADDR_SPACE (item_type),
13424 reverse,
13425 context_die);
13427 if (sub_die != NULL)
13428 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13430 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13431 if (TYPE_ARTIFICIAL (type))
13432 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
13434 return mod_type_die;
13437 /* Generate DIEs for the generic parameters of T.
13438 T must be either a generic type or a generic function.
13439 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
13441 static void
13442 gen_generic_params_dies (tree t)
13444 tree parms, args;
13445 int parms_num, i;
13446 dw_die_ref die = NULL;
13447 int non_default;
13449 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13450 return;
13452 if (TYPE_P (t))
13453 die = lookup_type_die (t);
13454 else if (DECL_P (t))
13455 die = lookup_decl_die (t);
13457 gcc_assert (die);
13459 parms = lang_hooks.get_innermost_generic_parms (t);
13460 if (!parms)
13461 /* T has no generic parameter. It means T is neither a generic type
13462 or function. End of story. */
13463 return;
13465 parms_num = TREE_VEC_LENGTH (parms);
13466 args = lang_hooks.get_innermost_generic_args (t);
13467 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
13468 non_default = int_cst_value (TREE_CHAIN (args));
13469 else
13470 non_default = TREE_VEC_LENGTH (args);
13471 for (i = 0; i < parms_num; i++)
13473 tree parm, arg, arg_pack_elems;
13474 dw_die_ref parm_die;
13476 parm = TREE_VEC_ELT (parms, i);
13477 arg = TREE_VEC_ELT (args, i);
13478 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13479 gcc_assert (parm && TREE_VALUE (parm) && arg);
13481 if (parm && TREE_VALUE (parm) && arg)
13483 /* If PARM represents a template parameter pack,
13484 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13485 by DW_TAG_template_*_parameter DIEs for the argument
13486 pack elements of ARG. Note that ARG would then be
13487 an argument pack. */
13488 if (arg_pack_elems)
13489 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
13490 arg_pack_elems,
13491 die);
13492 else
13493 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
13494 true /* emit name */, die);
13495 if (i >= non_default)
13496 add_AT_flag (parm_die, DW_AT_default_value, 1);
13501 /* Create and return a DIE for PARM which should be
13502 the representation of a generic type parameter.
13503 For instance, in the C++ front end, PARM would be a template parameter.
13504 ARG is the argument to PARM.
13505 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13506 name of the PARM.
13507 PARENT_DIE is the parent DIE which the new created DIE should be added to,
13508 as a child node. */
13510 static dw_die_ref
13511 generic_parameter_die (tree parm, tree arg,
13512 bool emit_name_p,
13513 dw_die_ref parent_die)
13515 dw_die_ref tmpl_die = NULL;
13516 const char *name = NULL;
13518 if (!parm || !DECL_NAME (parm) || !arg)
13519 return NULL;
13521 /* We support non-type generic parameters and arguments,
13522 type generic parameters and arguments, as well as
13523 generic generic parameters (a.k.a. template template parameters in C++)
13524 and arguments. */
13525 if (TREE_CODE (parm) == PARM_DECL)
13526 /* PARM is a nontype generic parameter */
13527 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13528 else if (TREE_CODE (parm) == TYPE_DECL)
13529 /* PARM is a type generic parameter. */
13530 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13531 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13532 /* PARM is a generic generic parameter.
13533 Its DIE is a GNU extension. It shall have a
13534 DW_AT_name attribute to represent the name of the template template
13535 parameter, and a DW_AT_GNU_template_name attribute to represent the
13536 name of the template template argument. */
13537 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13538 parent_die, parm);
13539 else
13540 gcc_unreachable ();
13542 if (tmpl_die)
13544 tree tmpl_type;
13546 /* If PARM is a generic parameter pack, it means we are
13547 emitting debug info for a template argument pack element.
13548 In other terms, ARG is a template argument pack element.
13549 In that case, we don't emit any DW_AT_name attribute for
13550 the die. */
13551 if (emit_name_p)
13553 name = IDENTIFIER_POINTER (DECL_NAME (parm));
13554 gcc_assert (name);
13555 add_AT_string (tmpl_die, DW_AT_name, name);
13558 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13560 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13561 TMPL_DIE should have a child DW_AT_type attribute that is set
13562 to the type of the argument to PARM, which is ARG.
13563 If PARM is a type generic parameter, TMPL_DIE should have a
13564 child DW_AT_type that is set to ARG. */
13565 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13566 add_type_attribute (tmpl_die, tmpl_type,
13567 (TREE_THIS_VOLATILE (tmpl_type)
13568 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
13569 false, parent_die);
13571 else
13573 /* So TMPL_DIE is a DIE representing a
13574 a generic generic template parameter, a.k.a template template
13575 parameter in C++ and arg is a template. */
13577 /* The DW_AT_GNU_template_name attribute of the DIE must be set
13578 to the name of the argument. */
13579 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13580 if (name)
13581 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13584 if (TREE_CODE (parm) == PARM_DECL)
13585 /* So PARM is a non-type generic parameter.
13586 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13587 attribute of TMPL_DIE which value represents the value
13588 of ARG.
13589 We must be careful here:
13590 The value of ARG might reference some function decls.
13591 We might currently be emitting debug info for a generic
13592 type and types are emitted before function decls, we don't
13593 know if the function decls referenced by ARG will actually be
13594 emitted after cgraph computations.
13595 So must defer the generation of the DW_AT_const_value to
13596 after cgraph is ready. */
13597 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13600 return tmpl_die;
13603 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13604 PARM_PACK must be a template parameter pack. The returned DIE
13605 will be child DIE of PARENT_DIE. */
13607 static dw_die_ref
13608 template_parameter_pack_die (tree parm_pack,
13609 tree parm_pack_args,
13610 dw_die_ref parent_die)
13612 dw_die_ref die;
13613 int j;
13615 gcc_assert (parent_die && parm_pack);
13617 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13618 add_name_and_src_coords_attributes (die, parm_pack);
13619 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13620 generic_parameter_die (parm_pack,
13621 TREE_VEC_ELT (parm_pack_args, j),
13622 false /* Don't emit DW_AT_name */,
13623 die);
13624 return die;
13627 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13628 an enumerated type. */
13630 static inline int
13631 type_is_enum (const_tree type)
13633 return TREE_CODE (type) == ENUMERAL_TYPE;
13636 /* Return the DBX register number described by a given RTL node. */
13638 static unsigned int
13639 dbx_reg_number (const_rtx rtl)
13641 unsigned regno = REGNO (rtl);
13643 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13645 #ifdef LEAF_REG_REMAP
13646 if (crtl->uses_only_leaf_regs)
13648 int leaf_reg = LEAF_REG_REMAP (regno);
13649 if (leaf_reg != -1)
13650 regno = (unsigned) leaf_reg;
13652 #endif
13654 regno = DBX_REGISTER_NUMBER (regno);
13655 gcc_assert (regno != INVALID_REGNUM);
13656 return regno;
13659 /* Optionally add a DW_OP_piece term to a location description expression.
13660 DW_OP_piece is only added if the location description expression already
13661 doesn't end with DW_OP_piece. */
13663 static void
13664 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13666 dw_loc_descr_ref loc;
13668 if (*list_head != NULL)
13670 /* Find the end of the chain. */
13671 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13674 if (loc->dw_loc_opc != DW_OP_piece)
13675 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13679 /* Return a location descriptor that designates a machine register or
13680 zero if there is none. */
13682 static dw_loc_descr_ref
13683 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13685 rtx regs;
13687 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13688 return 0;
13690 /* We only use "frame base" when we're sure we're talking about the
13691 post-prologue local stack frame. We do this by *not* running
13692 register elimination until this point, and recognizing the special
13693 argument pointer and soft frame pointer rtx's.
13694 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13695 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13696 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13698 dw_loc_descr_ref result = NULL;
13700 if (dwarf_version >= 4 || !dwarf_strict)
13702 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13703 initialized);
13704 if (result)
13705 add_loc_descr (&result,
13706 new_loc_descr (DW_OP_stack_value, 0, 0));
13708 return result;
13711 regs = targetm.dwarf_register_span (rtl);
13713 if (REG_NREGS (rtl) > 1 || regs)
13714 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13715 else
13717 unsigned int dbx_regnum = dbx_reg_number (rtl);
13718 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13719 return 0;
13720 return one_reg_loc_descriptor (dbx_regnum, initialized);
13724 /* Return a location descriptor that designates a machine register for
13725 a given hard register number. */
13727 static dw_loc_descr_ref
13728 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13730 dw_loc_descr_ref reg_loc_descr;
13732 if (regno <= 31)
13733 reg_loc_descr
13734 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13735 else
13736 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13738 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13739 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13741 return reg_loc_descr;
13744 /* Given an RTL of a register, return a location descriptor that
13745 designates a value that spans more than one register. */
13747 static dw_loc_descr_ref
13748 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13749 enum var_init_status initialized)
13751 int size, i;
13752 dw_loc_descr_ref loc_result = NULL;
13754 /* Simple, contiguous registers. */
13755 if (regs == NULL_RTX)
13757 unsigned reg = REGNO (rtl);
13758 int nregs;
13760 #ifdef LEAF_REG_REMAP
13761 if (crtl->uses_only_leaf_regs)
13763 int leaf_reg = LEAF_REG_REMAP (reg);
13764 if (leaf_reg != -1)
13765 reg = (unsigned) leaf_reg;
13767 #endif
13769 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13770 nregs = REG_NREGS (rtl);
13772 /* At present we only track constant-sized pieces. */
13773 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13774 return NULL;
13775 size /= nregs;
13777 loc_result = NULL;
13778 while (nregs--)
13780 dw_loc_descr_ref t;
13782 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13783 VAR_INIT_STATUS_INITIALIZED);
13784 add_loc_descr (&loc_result, t);
13785 add_loc_descr_op_piece (&loc_result, size);
13786 ++reg;
13788 return loc_result;
13791 /* Now onto stupid register sets in non contiguous locations. */
13793 gcc_assert (GET_CODE (regs) == PARALLEL);
13795 /* At present we only track constant-sized pieces. */
13796 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13797 return NULL;
13798 loc_result = NULL;
13800 for (i = 0; i < XVECLEN (regs, 0); ++i)
13802 dw_loc_descr_ref t;
13804 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13805 VAR_INIT_STATUS_INITIALIZED);
13806 add_loc_descr (&loc_result, t);
13807 add_loc_descr_op_piece (&loc_result, size);
13810 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13811 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13812 return loc_result;
13815 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13817 /* Return a location descriptor that designates a constant i,
13818 as a compound operation from constant (i >> shift), constant shift
13819 and DW_OP_shl. */
13821 static dw_loc_descr_ref
13822 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13824 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13825 add_loc_descr (&ret, int_loc_descriptor (shift));
13826 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13827 return ret;
13830 /* Return a location descriptor that designates constant POLY_I. */
13832 static dw_loc_descr_ref
13833 int_loc_descriptor (poly_int64 poly_i)
13835 enum dwarf_location_atom op;
13837 HOST_WIDE_INT i;
13838 if (!poly_i.is_constant (&i))
13840 /* Create location descriptions for the non-constant part and
13841 add any constant offset at the end. */
13842 dw_loc_descr_ref ret = NULL;
13843 HOST_WIDE_INT constant = poly_i.coeffs[0];
13844 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13846 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13847 if (coeff != 0)
13849 dw_loc_descr_ref start = ret;
13850 unsigned int factor;
13851 int bias;
13852 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13853 (j, &factor, &bias);
13855 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13856 add COEFF * (REGNO / FACTOR) now and subtract
13857 COEFF * BIAS from the final constant part. */
13858 constant -= coeff * bias;
13859 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13860 if (coeff % factor == 0)
13861 coeff /= factor;
13862 else
13864 int amount = exact_log2 (factor);
13865 gcc_assert (amount >= 0);
13866 add_loc_descr (&ret, int_loc_descriptor (amount));
13867 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13869 if (coeff != 1)
13871 add_loc_descr (&ret, int_loc_descriptor (coeff));
13872 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13874 if (start)
13875 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13878 loc_descr_plus_const (&ret, constant);
13879 return ret;
13882 /* Pick the smallest representation of a constant, rather than just
13883 defaulting to the LEB encoding. */
13884 if (i >= 0)
13886 int clz = clz_hwi (i);
13887 int ctz = ctz_hwi (i);
13888 if (i <= 31)
13889 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13890 else if (i <= 0xff)
13891 op = DW_OP_const1u;
13892 else if (i <= 0xffff)
13893 op = DW_OP_const2u;
13894 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13895 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13896 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13897 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13898 while DW_OP_const4u is 5 bytes. */
13899 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13900 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13901 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13902 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13903 while DW_OP_const4u is 5 bytes. */
13904 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13906 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13907 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13908 <= 4)
13910 /* As i >= 2**31, the double cast above will yield a negative number.
13911 Since wrapping is defined in DWARF expressions we can output big
13912 positive integers as small negative ones, regardless of the size
13913 of host wide ints.
13915 Here, since the evaluator will handle 32-bit values and since i >=
13916 2**31, we know it's going to be interpreted as a negative literal:
13917 store it this way if we can do better than 5 bytes this way. */
13918 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13920 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13921 op = DW_OP_const4u;
13923 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13924 least 6 bytes: see if we can do better before falling back to it. */
13925 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13926 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13927 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13928 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13929 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13930 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13931 >= HOST_BITS_PER_WIDE_INT)
13932 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13933 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13934 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13935 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13936 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13937 && size_of_uleb128 (i) > 6)
13938 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13939 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13940 else
13941 op = DW_OP_constu;
13943 else
13945 if (i >= -0x80)
13946 op = DW_OP_const1s;
13947 else if (i >= -0x8000)
13948 op = DW_OP_const2s;
13949 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13951 if (size_of_int_loc_descriptor (i) < 5)
13953 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13954 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13955 return ret;
13957 op = DW_OP_const4s;
13959 else
13961 if (size_of_int_loc_descriptor (i)
13962 < (unsigned long) 1 + size_of_sleb128 (i))
13964 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13965 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13966 return ret;
13968 op = DW_OP_consts;
13972 return new_loc_descr (op, i, 0);
13975 /* Likewise, for unsigned constants. */
13977 static dw_loc_descr_ref
13978 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13980 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13981 const unsigned HOST_WIDE_INT max_uint
13982 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13984 /* If possible, use the clever signed constants handling. */
13985 if (i <= max_int)
13986 return int_loc_descriptor ((HOST_WIDE_INT) i);
13988 /* Here, we are left with positive numbers that cannot be represented as
13989 HOST_WIDE_INT, i.e.:
13990 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13992 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13993 whereas may be better to output a negative integer: thanks to integer
13994 wrapping, we know that:
13995 x = x - 2 ** DWARF2_ADDR_SIZE
13996 = x - 2 * (max (HOST_WIDE_INT) + 1)
13997 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13998 small negative integers. Let's try that in cases it will clearly improve
13999 the encoding: there is no gain turning DW_OP_const4u into
14000 DW_OP_const4s. */
14001 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
14002 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
14003 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
14005 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
14007 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
14008 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
14009 const HOST_WIDE_INT second_shift
14010 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
14012 /* So we finally have:
14013 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
14014 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
14015 return int_loc_descriptor (second_shift);
14018 /* Last chance: fallback to a simple constant operation. */
14019 return new_loc_descr
14020 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14021 ? DW_OP_const4u
14022 : DW_OP_const8u,
14023 i, 0);
14026 /* Generate and return a location description that computes the unsigned
14027 comparison of the two stack top entries (a OP b where b is the top-most
14028 entry and a is the second one). The KIND of comparison can be LT_EXPR,
14029 LE_EXPR, GT_EXPR or GE_EXPR. */
14031 static dw_loc_descr_ref
14032 uint_comparison_loc_list (enum tree_code kind)
14034 enum dwarf_location_atom op, flip_op;
14035 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
14037 switch (kind)
14039 case LT_EXPR:
14040 op = DW_OP_lt;
14041 break;
14042 case LE_EXPR:
14043 op = DW_OP_le;
14044 break;
14045 case GT_EXPR:
14046 op = DW_OP_gt;
14047 break;
14048 case GE_EXPR:
14049 op = DW_OP_ge;
14050 break;
14051 default:
14052 gcc_unreachable ();
14055 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14056 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
14058 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
14059 possible to perform unsigned comparisons: we just have to distinguish
14060 three cases:
14062 1. when a and b have the same sign (as signed integers); then we should
14063 return: a OP(signed) b;
14065 2. when a is a negative signed integer while b is a positive one, then a
14066 is a greater unsigned integer than b; likewise when a and b's roles
14067 are flipped.
14069 So first, compare the sign of the two operands. */
14070 ret = new_loc_descr (DW_OP_over, 0, 0);
14071 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14072 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
14073 /* If they have different signs (i.e. they have different sign bits), then
14074 the stack top value has now the sign bit set and thus it's smaller than
14075 zero. */
14076 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
14077 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
14078 add_loc_descr (&ret, bra_node);
14080 /* We are in case 1. At this point, we know both operands have the same
14081 sign, to it's safe to use the built-in signed comparison. */
14082 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14083 add_loc_descr (&ret, jmp_node);
14085 /* We are in case 2. Here, we know both operands do not have the same sign,
14086 so we have to flip the signed comparison. */
14087 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
14088 tmp = new_loc_descr (flip_op, 0, 0);
14089 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14090 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
14091 add_loc_descr (&ret, tmp);
14093 /* This dummy operation is necessary to make the two branches join. */
14094 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14095 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14096 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
14097 add_loc_descr (&ret, tmp);
14099 return ret;
14102 /* Likewise, but takes the location description lists (might be destructive on
14103 them). Return NULL if either is NULL or if concatenation fails. */
14105 static dw_loc_list_ref
14106 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
14107 enum tree_code kind)
14109 if (left == NULL || right == NULL)
14110 return NULL;
14112 add_loc_list (&left, right);
14113 if (left == NULL)
14114 return NULL;
14116 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
14117 return left;
14120 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
14121 without actually allocating it. */
14123 static unsigned long
14124 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
14126 return size_of_int_loc_descriptor (i >> shift)
14127 + size_of_int_loc_descriptor (shift)
14128 + 1;
14131 /* Return size_of_locs (int_loc_descriptor (i)) without
14132 actually allocating it. */
14134 static unsigned long
14135 size_of_int_loc_descriptor (HOST_WIDE_INT i)
14137 unsigned long s;
14139 if (i >= 0)
14141 int clz, ctz;
14142 if (i <= 31)
14143 return 1;
14144 else if (i <= 0xff)
14145 return 2;
14146 else if (i <= 0xffff)
14147 return 3;
14148 clz = clz_hwi (i);
14149 ctz = ctz_hwi (i);
14150 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
14151 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
14152 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14153 - clz - 5);
14154 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14155 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
14156 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14157 - clz - 8);
14158 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
14159 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
14160 <= 4)
14161 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
14162 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
14163 return 5;
14164 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
14165 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
14166 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
14167 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14168 - clz - 8);
14169 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
14170 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
14171 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14172 - clz - 16);
14173 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
14174 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
14175 && s > 6)
14176 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
14177 - clz - 32);
14178 else
14179 return 1 + s;
14181 else
14183 if (i >= -0x80)
14184 return 2;
14185 else if (i >= -0x8000)
14186 return 3;
14187 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
14189 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14191 s = size_of_int_loc_descriptor (-i) + 1;
14192 if (s < 5)
14193 return s;
14195 return 5;
14197 else
14199 unsigned long r = 1 + size_of_sleb128 (i);
14200 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
14202 s = size_of_int_loc_descriptor (-i) + 1;
14203 if (s < r)
14204 return s;
14206 return r;
14211 /* Return loc description representing "address" of integer value.
14212 This can appear only as toplevel expression. */
14214 static dw_loc_descr_ref
14215 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
14217 int litsize;
14218 dw_loc_descr_ref loc_result = NULL;
14220 if (!(dwarf_version >= 4 || !dwarf_strict))
14221 return NULL;
14223 litsize = size_of_int_loc_descriptor (i);
14224 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
14225 is more compact. For DW_OP_stack_value we need:
14226 litsize + 1 (DW_OP_stack_value)
14227 and for DW_OP_implicit_value:
14228 1 (DW_OP_implicit_value) + 1 (length) + size. */
14229 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
14231 loc_result = int_loc_descriptor (i);
14232 add_loc_descr (&loc_result,
14233 new_loc_descr (DW_OP_stack_value, 0, 0));
14234 return loc_result;
14237 loc_result = new_loc_descr (DW_OP_implicit_value,
14238 size, 0);
14239 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14240 loc_result->dw_loc_oprnd2.v.val_int = i;
14241 return loc_result;
14244 /* Return a location descriptor that designates a base+offset location. */
14246 static dw_loc_descr_ref
14247 based_loc_descr (rtx reg, poly_int64 offset,
14248 enum var_init_status initialized)
14250 unsigned int regno;
14251 dw_loc_descr_ref result;
14252 dw_fde_ref fde = cfun->fde;
14254 /* We only use "frame base" when we're sure we're talking about the
14255 post-prologue local stack frame. We do this by *not* running
14256 register elimination until this point, and recognizing the special
14257 argument pointer and soft frame pointer rtx's. */
14258 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
14260 rtx elim = (ira_use_lra_p
14261 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
14262 : eliminate_regs (reg, VOIDmode, NULL_RTX));
14264 if (elim != reg)
14266 elim = strip_offset_and_add (elim, &offset);
14267 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14268 && (elim == hard_frame_pointer_rtx
14269 || elim == stack_pointer_rtx))
14270 || elim == (frame_pointer_needed
14271 ? hard_frame_pointer_rtx
14272 : stack_pointer_rtx));
14274 /* If drap register is used to align stack, use frame
14275 pointer + offset to access stack variables. If stack
14276 is aligned without drap, use stack pointer + offset to
14277 access stack variables. */
14278 if (crtl->stack_realign_tried
14279 && reg == frame_pointer_rtx)
14281 int base_reg
14282 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
14283 ? HARD_FRAME_POINTER_REGNUM
14284 : REGNO (elim));
14285 return new_reg_loc_descr (base_reg, offset);
14288 gcc_assert (frame_pointer_fb_offset_valid);
14289 offset += frame_pointer_fb_offset;
14290 HOST_WIDE_INT const_offset;
14291 if (offset.is_constant (&const_offset))
14292 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14293 else
14295 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
14296 loc_descr_plus_const (&ret, offset);
14297 return ret;
14302 regno = REGNO (reg);
14303 #ifdef LEAF_REG_REMAP
14304 if (crtl->uses_only_leaf_regs)
14306 int leaf_reg = LEAF_REG_REMAP (regno);
14307 if (leaf_reg != -1)
14308 regno = (unsigned) leaf_reg;
14310 #endif
14311 regno = DWARF_FRAME_REGNUM (regno);
14313 HOST_WIDE_INT const_offset;
14314 if (!optimize && fde
14315 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
14316 && offset.is_constant (&const_offset))
14318 /* Use cfa+offset to represent the location of arguments passed
14319 on the stack when drap is used to align stack.
14320 Only do this when not optimizing, for optimized code var-tracking
14321 is supposed to track where the arguments live and the register
14322 used as vdrap or drap in some spot might be used for something
14323 else in other part of the routine. */
14324 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
14327 result = new_reg_loc_descr (regno, offset);
14329 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14330 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14332 return result;
14335 /* Return true if this RTL expression describes a base+offset calculation. */
14337 static inline int
14338 is_based_loc (const_rtx rtl)
14340 return (GET_CODE (rtl) == PLUS
14341 && ((REG_P (XEXP (rtl, 0))
14342 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
14343 && CONST_INT_P (XEXP (rtl, 1)))));
14346 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
14347 failed. */
14349 static dw_loc_descr_ref
14350 tls_mem_loc_descriptor (rtx mem)
14352 tree base;
14353 dw_loc_descr_ref loc_result;
14355 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
14356 return NULL;
14358 base = get_base_address (MEM_EXPR (mem));
14359 if (base == NULL
14360 || !VAR_P (base)
14361 || !DECL_THREAD_LOCAL_P (base))
14362 return NULL;
14364 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
14365 if (loc_result == NULL)
14366 return NULL;
14368 if (maybe_ne (MEM_OFFSET (mem), 0))
14369 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
14371 return loc_result;
14374 /* Output debug info about reason why we failed to expand expression as dwarf
14375 expression. */
14377 static void
14378 expansion_failed (tree expr, rtx rtl, char const *reason)
14380 if (dump_file && (dump_flags & TDF_DETAILS))
14382 fprintf (dump_file, "Failed to expand as dwarf: ");
14383 if (expr)
14384 print_generic_expr (dump_file, expr, dump_flags);
14385 if (rtl)
14387 fprintf (dump_file, "\n");
14388 print_rtl (dump_file, rtl);
14390 fprintf (dump_file, "\nReason: %s\n", reason);
14394 /* Helper function for const_ok_for_output. */
14396 static bool
14397 const_ok_for_output_1 (rtx rtl)
14399 if (targetm.const_not_ok_for_debug_p (rtl))
14401 if (GET_CODE (rtl) != UNSPEC)
14403 expansion_failed (NULL_TREE, rtl,
14404 "Expression rejected for debug by the backend.\n");
14405 return false;
14408 /* If delegitimize_address couldn't do anything with the UNSPEC, and
14409 the target hook doesn't explicitly allow it in debug info, assume
14410 we can't express it in the debug info. */
14411 /* Don't complain about TLS UNSPECs, those are just too hard to
14412 delegitimize. Note this could be a non-decl SYMBOL_REF such as
14413 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
14414 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
14415 if (flag_checking
14416 && (XVECLEN (rtl, 0) == 0
14417 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
14418 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
14419 inform (current_function_decl
14420 ? DECL_SOURCE_LOCATION (current_function_decl)
14421 : UNKNOWN_LOCATION,
14422 #if NUM_UNSPEC_VALUES > 0
14423 "non-delegitimized UNSPEC %s (%d) found in variable location",
14424 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
14425 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
14426 XINT (rtl, 1));
14427 #else
14428 "non-delegitimized UNSPEC %d found in variable location",
14429 XINT (rtl, 1));
14430 #endif
14431 expansion_failed (NULL_TREE, rtl,
14432 "UNSPEC hasn't been delegitimized.\n");
14433 return false;
14436 if (CONST_POLY_INT_P (rtl))
14437 return false;
14439 if (targetm.const_not_ok_for_debug_p (rtl))
14441 expansion_failed (NULL_TREE, rtl,
14442 "Expression rejected for debug by the backend.\n");
14443 return false;
14446 /* FIXME: Refer to PR60655. It is possible for simplification
14447 of rtl expressions in var tracking to produce such expressions.
14448 We should really identify / validate expressions
14449 enclosed in CONST that can be handled by assemblers on various
14450 targets and only handle legitimate cases here. */
14451 switch (GET_CODE (rtl))
14453 case SYMBOL_REF:
14454 break;
14455 case NOT:
14456 case NEG:
14457 return false;
14458 default:
14459 return true;
14462 if (CONSTANT_POOL_ADDRESS_P (rtl))
14464 bool marked;
14465 get_pool_constant_mark (rtl, &marked);
14466 /* If all references to this pool constant were optimized away,
14467 it was not output and thus we can't represent it. */
14468 if (!marked)
14470 expansion_failed (NULL_TREE, rtl,
14471 "Constant was removed from constant pool.\n");
14472 return false;
14476 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14477 return false;
14479 /* Avoid references to external symbols in debug info, on several targets
14480 the linker might even refuse to link when linking a shared library,
14481 and in many other cases the relocations for .debug_info/.debug_loc are
14482 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
14483 to be defined within the same shared library or executable are fine. */
14484 if (SYMBOL_REF_EXTERNAL_P (rtl))
14486 tree decl = SYMBOL_REF_DECL (rtl);
14488 if (decl == NULL || !targetm.binds_local_p (decl))
14490 expansion_failed (NULL_TREE, rtl,
14491 "Symbol not defined in current TU.\n");
14492 return false;
14496 return true;
14499 /* Return true if constant RTL can be emitted in DW_OP_addr or
14500 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
14501 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
14503 static bool
14504 const_ok_for_output (rtx rtl)
14506 if (GET_CODE (rtl) == SYMBOL_REF)
14507 return const_ok_for_output_1 (rtl);
14509 if (GET_CODE (rtl) == CONST)
14511 subrtx_var_iterator::array_type array;
14512 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
14513 if (!const_ok_for_output_1 (*iter))
14514 return false;
14515 return true;
14518 return true;
14521 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
14522 if possible, NULL otherwise. */
14524 static dw_die_ref
14525 base_type_for_mode (machine_mode mode, bool unsignedp)
14527 dw_die_ref type_die;
14528 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
14530 if (type == NULL)
14531 return NULL;
14532 switch (TREE_CODE (type))
14534 case INTEGER_TYPE:
14535 case REAL_TYPE:
14536 break;
14537 default:
14538 return NULL;
14540 type_die = lookup_type_die (type);
14541 if (!type_die)
14542 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
14543 comp_unit_die ());
14544 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
14545 return NULL;
14546 return type_die;
14549 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
14550 type matching MODE, or, if MODE is narrower than or as wide as
14551 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
14552 possible. */
14554 static dw_loc_descr_ref
14555 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
14557 machine_mode outer_mode = mode;
14558 dw_die_ref type_die;
14559 dw_loc_descr_ref cvt;
14561 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14563 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
14564 return op;
14566 type_die = base_type_for_mode (outer_mode, 1);
14567 if (type_die == NULL)
14568 return NULL;
14569 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14570 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14571 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14572 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14573 add_loc_descr (&op, cvt);
14574 return op;
14577 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
14579 static dw_loc_descr_ref
14580 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
14581 dw_loc_descr_ref op1)
14583 dw_loc_descr_ref ret = op0;
14584 add_loc_descr (&ret, op1);
14585 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14586 if (STORE_FLAG_VALUE != 1)
14588 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14591 return ret;
14594 /* Subroutine of scompare_loc_descriptor for the case in which we're
14595 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14596 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14598 static dw_loc_descr_ref
14599 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14600 scalar_int_mode op_mode,
14601 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14603 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14604 dw_loc_descr_ref cvt;
14606 if (type_die == NULL)
14607 return NULL;
14608 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14609 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14610 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14611 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14612 add_loc_descr (&op0, cvt);
14613 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14614 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14615 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14616 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14617 add_loc_descr (&op1, cvt);
14618 return compare_loc_descriptor (op, op0, op1);
14621 /* Subroutine of scompare_loc_descriptor for the case in which we're
14622 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14623 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14625 static dw_loc_descr_ref
14626 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14627 scalar_int_mode op_mode,
14628 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14630 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14631 /* For eq/ne, if the operands are known to be zero-extended,
14632 there is no need to do the fancy shifting up. */
14633 if (op == DW_OP_eq || op == DW_OP_ne)
14635 dw_loc_descr_ref last0, last1;
14636 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14638 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14640 /* deref_size zero extends, and for constants we can check
14641 whether they are zero extended or not. */
14642 if (((last0->dw_loc_opc == DW_OP_deref_size
14643 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14644 || (CONST_INT_P (XEXP (rtl, 0))
14645 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14646 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14647 && ((last1->dw_loc_opc == DW_OP_deref_size
14648 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14649 || (CONST_INT_P (XEXP (rtl, 1))
14650 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14651 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14652 return compare_loc_descriptor (op, op0, op1);
14654 /* EQ/NE comparison against constant in narrower type than
14655 DWARF2_ADDR_SIZE can be performed either as
14656 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14657 DW_OP_{eq,ne}
14659 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14660 DW_OP_{eq,ne}. Pick whatever is shorter. */
14661 if (CONST_INT_P (XEXP (rtl, 1))
14662 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14663 && (size_of_int_loc_descriptor (shift) + 1
14664 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14665 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14666 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14667 & GET_MODE_MASK (op_mode))))
14669 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14670 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14671 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14672 & GET_MODE_MASK (op_mode));
14673 return compare_loc_descriptor (op, op0, op1);
14676 add_loc_descr (&op0, int_loc_descriptor (shift));
14677 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14678 if (CONST_INT_P (XEXP (rtl, 1)))
14679 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14680 else
14682 add_loc_descr (&op1, int_loc_descriptor (shift));
14683 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14685 return compare_loc_descriptor (op, op0, op1);
14688 /* Return location descriptor for unsigned comparison OP RTL. */
14690 static dw_loc_descr_ref
14691 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14692 machine_mode mem_mode)
14694 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14695 dw_loc_descr_ref op0, op1;
14697 if (op_mode == VOIDmode)
14698 op_mode = GET_MODE (XEXP (rtl, 1));
14699 if (op_mode == VOIDmode)
14700 return NULL;
14702 scalar_int_mode int_op_mode;
14703 if (dwarf_strict
14704 && dwarf_version < 5
14705 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14706 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14707 return NULL;
14709 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14710 VAR_INIT_STATUS_INITIALIZED);
14711 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14712 VAR_INIT_STATUS_INITIALIZED);
14714 if (op0 == NULL || op1 == NULL)
14715 return NULL;
14717 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14719 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14720 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14722 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14723 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14725 return compare_loc_descriptor (op, op0, op1);
14728 /* Return location descriptor for unsigned comparison OP RTL. */
14730 static dw_loc_descr_ref
14731 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14732 machine_mode mem_mode)
14734 dw_loc_descr_ref op0, op1;
14736 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14737 if (test_op_mode == VOIDmode)
14738 test_op_mode = GET_MODE (XEXP (rtl, 1));
14740 scalar_int_mode op_mode;
14741 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14742 return NULL;
14744 if (dwarf_strict
14745 && dwarf_version < 5
14746 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14747 return NULL;
14749 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14750 VAR_INIT_STATUS_INITIALIZED);
14751 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14752 VAR_INIT_STATUS_INITIALIZED);
14754 if (op0 == NULL || op1 == NULL)
14755 return NULL;
14757 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14759 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14760 dw_loc_descr_ref last0, last1;
14761 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14763 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14765 if (CONST_INT_P (XEXP (rtl, 0)))
14766 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14767 /* deref_size zero extends, so no need to mask it again. */
14768 else if (last0->dw_loc_opc != DW_OP_deref_size
14769 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14771 add_loc_descr (&op0, int_loc_descriptor (mask));
14772 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14774 if (CONST_INT_P (XEXP (rtl, 1)))
14775 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14776 /* deref_size zero extends, so no need to mask it again. */
14777 else if (last1->dw_loc_opc != DW_OP_deref_size
14778 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14780 add_loc_descr (&op1, int_loc_descriptor (mask));
14781 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14784 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14786 HOST_WIDE_INT bias = 1;
14787 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14788 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14789 if (CONST_INT_P (XEXP (rtl, 1)))
14790 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14791 + INTVAL (XEXP (rtl, 1)));
14792 else
14793 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14794 bias, 0));
14796 return compare_loc_descriptor (op, op0, op1);
14799 /* Return location descriptor for {U,S}{MIN,MAX}. */
14801 static dw_loc_descr_ref
14802 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14803 machine_mode mem_mode)
14805 enum dwarf_location_atom op;
14806 dw_loc_descr_ref op0, op1, ret;
14807 dw_loc_descr_ref bra_node, drop_node;
14809 scalar_int_mode int_mode;
14810 if (dwarf_strict
14811 && dwarf_version < 5
14812 && (!is_a <scalar_int_mode> (mode, &int_mode)
14813 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14814 return NULL;
14816 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14817 VAR_INIT_STATUS_INITIALIZED);
14818 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14819 VAR_INIT_STATUS_INITIALIZED);
14821 if (op0 == NULL || op1 == NULL)
14822 return NULL;
14824 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14825 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14826 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14827 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14829 /* Checked by the caller. */
14830 int_mode = as_a <scalar_int_mode> (mode);
14831 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14833 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14834 add_loc_descr (&op0, int_loc_descriptor (mask));
14835 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14836 add_loc_descr (&op1, int_loc_descriptor (mask));
14837 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14839 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14841 HOST_WIDE_INT bias = 1;
14842 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14843 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14844 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14847 else if (is_a <scalar_int_mode> (mode, &int_mode)
14848 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14850 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14851 add_loc_descr (&op0, int_loc_descriptor (shift));
14852 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14853 add_loc_descr (&op1, int_loc_descriptor (shift));
14854 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14856 else if (is_a <scalar_int_mode> (mode, &int_mode)
14857 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14859 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14860 dw_loc_descr_ref cvt;
14861 if (type_die == NULL)
14862 return NULL;
14863 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14864 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14865 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14866 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14867 add_loc_descr (&op0, cvt);
14868 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14869 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14870 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14871 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14872 add_loc_descr (&op1, cvt);
14875 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14876 op = DW_OP_lt;
14877 else
14878 op = DW_OP_gt;
14879 ret = op0;
14880 add_loc_descr (&ret, op1);
14881 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14882 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14883 add_loc_descr (&ret, bra_node);
14884 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14885 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14886 add_loc_descr (&ret, drop_node);
14887 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14888 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14889 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14890 && is_a <scalar_int_mode> (mode, &int_mode)
14891 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14892 ret = convert_descriptor_to_mode (int_mode, ret);
14893 return ret;
14896 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14897 but after converting arguments to type_die, afterwards
14898 convert back to unsigned. */
14900 static dw_loc_descr_ref
14901 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14902 scalar_int_mode mode, machine_mode mem_mode)
14904 dw_loc_descr_ref cvt, op0, op1;
14906 if (type_die == NULL)
14907 return NULL;
14908 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14909 VAR_INIT_STATUS_INITIALIZED);
14910 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14911 VAR_INIT_STATUS_INITIALIZED);
14912 if (op0 == NULL || op1 == NULL)
14913 return NULL;
14914 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14915 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14916 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14917 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14918 add_loc_descr (&op0, cvt);
14919 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14920 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14921 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14922 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14923 add_loc_descr (&op1, cvt);
14924 add_loc_descr (&op0, op1);
14925 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14926 return convert_descriptor_to_mode (mode, op0);
14929 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14930 const0 is DW_OP_lit0 or corresponding typed constant,
14931 const1 is DW_OP_lit1 or corresponding typed constant
14932 and constMSB is constant with just the MSB bit set
14933 for the mode):
14934 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14935 L1: const0 DW_OP_swap
14936 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14937 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14938 L3: DW_OP_drop
14939 L4: DW_OP_nop
14941 CTZ is similar:
14942 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14943 L1: const0 DW_OP_swap
14944 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14945 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14946 L3: DW_OP_drop
14947 L4: DW_OP_nop
14949 FFS is similar:
14950 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14951 L1: const1 DW_OP_swap
14952 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14953 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14954 L3: DW_OP_drop
14955 L4: DW_OP_nop */
14957 static dw_loc_descr_ref
14958 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14959 machine_mode mem_mode)
14961 dw_loc_descr_ref op0, ret, tmp;
14962 HOST_WIDE_INT valv;
14963 dw_loc_descr_ref l1jump, l1label;
14964 dw_loc_descr_ref l2jump, l2label;
14965 dw_loc_descr_ref l3jump, l3label;
14966 dw_loc_descr_ref l4jump, l4label;
14967 rtx msb;
14969 if (GET_MODE (XEXP (rtl, 0)) != mode)
14970 return NULL;
14972 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14973 VAR_INIT_STATUS_INITIALIZED);
14974 if (op0 == NULL)
14975 return NULL;
14976 ret = op0;
14977 if (GET_CODE (rtl) == CLZ)
14979 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14980 valv = GET_MODE_BITSIZE (mode);
14982 else if (GET_CODE (rtl) == FFS)
14983 valv = 0;
14984 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14985 valv = GET_MODE_BITSIZE (mode);
14986 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14987 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14988 add_loc_descr (&ret, l1jump);
14989 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14990 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14991 VAR_INIT_STATUS_INITIALIZED);
14992 if (tmp == NULL)
14993 return NULL;
14994 add_loc_descr (&ret, tmp);
14995 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14996 add_loc_descr (&ret, l4jump);
14997 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14998 ? const1_rtx : const0_rtx,
14999 mode, mem_mode,
15000 VAR_INIT_STATUS_INITIALIZED);
15001 if (l1label == NULL)
15002 return NULL;
15003 add_loc_descr (&ret, l1label);
15004 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15005 l2label = new_loc_descr (DW_OP_dup, 0, 0);
15006 add_loc_descr (&ret, l2label);
15007 if (GET_CODE (rtl) != CLZ)
15008 msb = const1_rtx;
15009 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
15010 msb = GEN_INT (HOST_WIDE_INT_1U
15011 << (GET_MODE_BITSIZE (mode) - 1));
15012 else
15013 msb = immed_wide_int_const
15014 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
15015 GET_MODE_PRECISION (mode)), mode);
15016 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
15017 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15018 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
15019 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
15020 else
15021 tmp = mem_loc_descriptor (msb, mode, mem_mode,
15022 VAR_INIT_STATUS_INITIALIZED);
15023 if (tmp == NULL)
15024 return NULL;
15025 add_loc_descr (&ret, tmp);
15026 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15027 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
15028 add_loc_descr (&ret, l3jump);
15029 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15030 VAR_INIT_STATUS_INITIALIZED);
15031 if (tmp == NULL)
15032 return NULL;
15033 add_loc_descr (&ret, tmp);
15034 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
15035 ? DW_OP_shl : DW_OP_shr, 0, 0));
15036 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15037 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
15038 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15039 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
15040 add_loc_descr (&ret, l2jump);
15041 l3label = new_loc_descr (DW_OP_drop, 0, 0);
15042 add_loc_descr (&ret, l3label);
15043 l4label = new_loc_descr (DW_OP_nop, 0, 0);
15044 add_loc_descr (&ret, l4label);
15045 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15046 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15047 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15048 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15049 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15050 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
15051 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15052 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
15053 return ret;
15056 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
15057 const1 is DW_OP_lit1 or corresponding typed constant):
15058 const0 DW_OP_swap
15059 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15060 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15061 L2: DW_OP_drop
15063 PARITY is similar:
15064 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
15065 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
15066 L2: DW_OP_drop */
15068 static dw_loc_descr_ref
15069 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
15070 machine_mode mem_mode)
15072 dw_loc_descr_ref op0, ret, tmp;
15073 dw_loc_descr_ref l1jump, l1label;
15074 dw_loc_descr_ref l2jump, l2label;
15076 if (GET_MODE (XEXP (rtl, 0)) != mode)
15077 return NULL;
15079 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15080 VAR_INIT_STATUS_INITIALIZED);
15081 if (op0 == NULL)
15082 return NULL;
15083 ret = op0;
15084 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15085 VAR_INIT_STATUS_INITIALIZED);
15086 if (tmp == NULL)
15087 return NULL;
15088 add_loc_descr (&ret, tmp);
15089 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15090 l1label = new_loc_descr (DW_OP_dup, 0, 0);
15091 add_loc_descr (&ret, l1label);
15092 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15093 add_loc_descr (&ret, l2jump);
15094 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15095 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15096 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15097 VAR_INIT_STATUS_INITIALIZED);
15098 if (tmp == NULL)
15099 return NULL;
15100 add_loc_descr (&ret, tmp);
15101 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15102 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
15103 ? DW_OP_plus : DW_OP_xor, 0, 0));
15104 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15105 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
15106 VAR_INIT_STATUS_INITIALIZED);
15107 add_loc_descr (&ret, tmp);
15108 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15109 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15110 add_loc_descr (&ret, l1jump);
15111 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15112 add_loc_descr (&ret, l2label);
15113 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15114 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15115 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15116 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15117 return ret;
15120 /* BSWAP (constS is initial shift count, either 56 or 24):
15121 constS const0
15122 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
15123 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
15124 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
15125 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
15126 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
15128 static dw_loc_descr_ref
15129 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
15130 machine_mode mem_mode)
15132 dw_loc_descr_ref op0, ret, tmp;
15133 dw_loc_descr_ref l1jump, l1label;
15134 dw_loc_descr_ref l2jump, l2label;
15136 if (BITS_PER_UNIT != 8
15137 || (GET_MODE_BITSIZE (mode) != 32
15138 && GET_MODE_BITSIZE (mode) != 64))
15139 return NULL;
15141 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15142 VAR_INIT_STATUS_INITIALIZED);
15143 if (op0 == NULL)
15144 return NULL;
15146 ret = op0;
15147 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15148 mode, mem_mode,
15149 VAR_INIT_STATUS_INITIALIZED);
15150 if (tmp == NULL)
15151 return NULL;
15152 add_loc_descr (&ret, tmp);
15153 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15154 VAR_INIT_STATUS_INITIALIZED);
15155 if (tmp == NULL)
15156 return NULL;
15157 add_loc_descr (&ret, tmp);
15158 l1label = new_loc_descr (DW_OP_pick, 2, 0);
15159 add_loc_descr (&ret, l1label);
15160 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
15161 mode, mem_mode,
15162 VAR_INIT_STATUS_INITIALIZED);
15163 add_loc_descr (&ret, tmp);
15164 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
15165 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15166 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15167 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
15168 VAR_INIT_STATUS_INITIALIZED);
15169 if (tmp == NULL)
15170 return NULL;
15171 add_loc_descr (&ret, tmp);
15172 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
15173 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
15174 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15175 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15176 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15177 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
15178 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
15179 VAR_INIT_STATUS_INITIALIZED);
15180 add_loc_descr (&ret, tmp);
15181 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
15182 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
15183 add_loc_descr (&ret, l2jump);
15184 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
15185 VAR_INIT_STATUS_INITIALIZED);
15186 add_loc_descr (&ret, tmp);
15187 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
15188 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15189 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
15190 add_loc_descr (&ret, l1jump);
15191 l2label = new_loc_descr (DW_OP_drop, 0, 0);
15192 add_loc_descr (&ret, l2label);
15193 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15194 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
15195 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15196 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
15197 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
15198 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
15199 return ret;
15202 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
15203 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15204 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
15205 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
15207 ROTATERT is similar:
15208 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
15209 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
15210 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
15212 static dw_loc_descr_ref
15213 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
15214 machine_mode mem_mode)
15216 rtx rtlop1 = XEXP (rtl, 1);
15217 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
15218 int i;
15220 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
15221 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15222 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15223 VAR_INIT_STATUS_INITIALIZED);
15224 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15225 VAR_INIT_STATUS_INITIALIZED);
15226 if (op0 == NULL || op1 == NULL)
15227 return NULL;
15228 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
15229 for (i = 0; i < 2; i++)
15231 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
15232 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
15233 mode, mem_mode,
15234 VAR_INIT_STATUS_INITIALIZED);
15235 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15236 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
15237 ? DW_OP_const4u
15238 : HOST_BITS_PER_WIDE_INT == 64
15239 ? DW_OP_const8u : DW_OP_constu,
15240 GET_MODE_MASK (mode), 0);
15241 else
15242 mask[i] = NULL;
15243 if (mask[i] == NULL)
15244 return NULL;
15245 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
15247 ret = op0;
15248 add_loc_descr (&ret, op1);
15249 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15250 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
15251 if (GET_CODE (rtl) == ROTATERT)
15253 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15254 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15255 GET_MODE_BITSIZE (mode), 0));
15257 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
15258 if (mask[0] != NULL)
15259 add_loc_descr (&ret, mask[0]);
15260 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
15261 if (mask[1] != NULL)
15263 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15264 add_loc_descr (&ret, mask[1]);
15265 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
15267 if (GET_CODE (rtl) == ROTATE)
15269 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
15270 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
15271 GET_MODE_BITSIZE (mode), 0));
15273 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
15274 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
15275 return ret;
15278 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
15279 for DEBUG_PARAMETER_REF RTL. */
15281 static dw_loc_descr_ref
15282 parameter_ref_descriptor (rtx rtl)
15284 dw_loc_descr_ref ret;
15285 dw_die_ref ref;
15287 if (dwarf_strict)
15288 return NULL;
15289 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
15290 /* With LTO during LTRANS we get the late DIE that refers to the early
15291 DIE, thus we add another indirection here. This seems to confuse
15292 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
15293 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
15294 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
15295 if (ref)
15297 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15298 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15299 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15301 else
15303 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15304 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
15306 return ret;
15309 /* The following routine converts the RTL for a variable or parameter
15310 (resident in memory) into an equivalent Dwarf representation of a
15311 mechanism for getting the address of that same variable onto the top of a
15312 hypothetical "address evaluation" stack.
15314 When creating memory location descriptors, we are effectively transforming
15315 the RTL for a memory-resident object into its Dwarf postfix expression
15316 equivalent. This routine recursively descends an RTL tree, turning
15317 it into Dwarf postfix code as it goes.
15319 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
15321 MEM_MODE is the mode of the memory reference, needed to handle some
15322 autoincrement addressing modes.
15324 Return 0 if we can't represent the location. */
15326 dw_loc_descr_ref
15327 mem_loc_descriptor (rtx rtl, machine_mode mode,
15328 machine_mode mem_mode,
15329 enum var_init_status initialized)
15331 dw_loc_descr_ref mem_loc_result = NULL;
15332 enum dwarf_location_atom op;
15333 dw_loc_descr_ref op0, op1;
15334 rtx inner = NULL_RTX;
15335 poly_int64 offset;
15337 if (mode == VOIDmode)
15338 mode = GET_MODE (rtl);
15340 /* Note that for a dynamically sized array, the location we will generate a
15341 description of here will be the lowest numbered location which is
15342 actually within the array. That's *not* necessarily the same as the
15343 zeroth element of the array. */
15345 rtl = targetm.delegitimize_address (rtl);
15347 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
15348 return NULL;
15350 scalar_int_mode int_mode, inner_mode, op1_mode;
15351 switch (GET_CODE (rtl))
15353 case POST_INC:
15354 case POST_DEC:
15355 case POST_MODIFY:
15356 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
15358 case SUBREG:
15359 /* The case of a subreg may arise when we have a local (register)
15360 variable or a formal (register) parameter which doesn't quite fill
15361 up an entire register. For now, just assume that it is
15362 legitimate to make the Dwarf info refer to the whole register which
15363 contains the given subreg. */
15364 if (!subreg_lowpart_p (rtl))
15365 break;
15366 inner = SUBREG_REG (rtl);
15367 /* FALLTHRU */
15368 case TRUNCATE:
15369 if (inner == NULL_RTX)
15370 inner = XEXP (rtl, 0);
15371 if (is_a <scalar_int_mode> (mode, &int_mode)
15372 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15373 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15374 #ifdef POINTERS_EXTEND_UNSIGNED
15375 || (int_mode == Pmode && mem_mode != VOIDmode)
15376 #endif
15378 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
15380 mem_loc_result = mem_loc_descriptor (inner,
15381 inner_mode,
15382 mem_mode, initialized);
15383 break;
15385 if (dwarf_strict && dwarf_version < 5)
15386 break;
15387 if (is_a <scalar_int_mode> (mode, &int_mode)
15388 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
15389 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
15390 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15392 dw_die_ref type_die;
15393 dw_loc_descr_ref cvt;
15395 mem_loc_result = mem_loc_descriptor (inner,
15396 GET_MODE (inner),
15397 mem_mode, initialized);
15398 if (mem_loc_result == NULL)
15399 break;
15400 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15401 if (type_die == NULL)
15403 mem_loc_result = NULL;
15404 break;
15406 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
15407 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15408 else
15409 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
15410 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15411 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15412 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15413 add_loc_descr (&mem_loc_result, cvt);
15414 if (is_a <scalar_int_mode> (mode, &int_mode)
15415 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15417 /* Convert it to untyped afterwards. */
15418 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15419 add_loc_descr (&mem_loc_result, cvt);
15422 break;
15424 case REG:
15425 if (!is_a <scalar_int_mode> (mode, &int_mode)
15426 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15427 && rtl != arg_pointer_rtx
15428 && rtl != frame_pointer_rtx
15429 #ifdef POINTERS_EXTEND_UNSIGNED
15430 && (int_mode != Pmode || mem_mode == VOIDmode)
15431 #endif
15434 dw_die_ref type_die;
15435 unsigned int dbx_regnum;
15437 if (dwarf_strict && dwarf_version < 5)
15438 break;
15439 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
15440 break;
15441 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15442 if (type_die == NULL)
15443 break;
15445 dbx_regnum = dbx_reg_number (rtl);
15446 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15447 break;
15448 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
15449 dbx_regnum, 0);
15450 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15451 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15452 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
15453 break;
15455 /* Whenever a register number forms a part of the description of the
15456 method for calculating the (dynamic) address of a memory resident
15457 object, DWARF rules require the register number be referred to as
15458 a "base register". This distinction is not based in any way upon
15459 what category of register the hardware believes the given register
15460 belongs to. This is strictly DWARF terminology we're dealing with
15461 here. Note that in cases where the location of a memory-resident
15462 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
15463 OP_CONST (0)) the actual DWARF location descriptor that we generate
15464 may just be OP_BASEREG (basereg). This may look deceptively like
15465 the object in question was allocated to a register (rather than in
15466 memory) so DWARF consumers need to be aware of the subtle
15467 distinction between OP_REG and OP_BASEREG. */
15468 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
15469 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
15470 else if (stack_realign_drap
15471 && crtl->drap_reg
15472 && crtl->args.internal_arg_pointer == rtl
15473 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
15475 /* If RTL is internal_arg_pointer, which has been optimized
15476 out, use DRAP instead. */
15477 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
15478 VAR_INIT_STATUS_INITIALIZED);
15480 break;
15482 case SIGN_EXTEND:
15483 case ZERO_EXTEND:
15484 if (!is_a <scalar_int_mode> (mode, &int_mode)
15485 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
15486 break;
15487 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15488 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15489 if (op0 == 0)
15490 break;
15491 else if (GET_CODE (rtl) == ZERO_EXTEND
15492 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15493 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
15494 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
15495 to expand zero extend as two shifts instead of
15496 masking. */
15497 && GET_MODE_SIZE (inner_mode) <= 4)
15499 mem_loc_result = op0;
15500 add_loc_descr (&mem_loc_result,
15501 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
15502 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
15504 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
15506 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
15507 shift *= BITS_PER_UNIT;
15508 if (GET_CODE (rtl) == SIGN_EXTEND)
15509 op = DW_OP_shra;
15510 else
15511 op = DW_OP_shr;
15512 mem_loc_result = op0;
15513 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15514 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15515 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
15516 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15518 else if (!dwarf_strict || dwarf_version >= 5)
15520 dw_die_ref type_die1, type_die2;
15521 dw_loc_descr_ref cvt;
15523 type_die1 = base_type_for_mode (inner_mode,
15524 GET_CODE (rtl) == ZERO_EXTEND);
15525 if (type_die1 == NULL)
15526 break;
15527 type_die2 = base_type_for_mode (int_mode, 1);
15528 if (type_die2 == NULL)
15529 break;
15530 mem_loc_result = op0;
15531 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15532 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15533 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
15534 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15535 add_loc_descr (&mem_loc_result, cvt);
15536 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15537 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15538 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
15539 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15540 add_loc_descr (&mem_loc_result, cvt);
15542 break;
15544 case MEM:
15546 rtx new_rtl = avoid_constant_pool_reference (rtl);
15547 if (new_rtl != rtl)
15549 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
15550 initialized);
15551 if (mem_loc_result != NULL)
15552 return mem_loc_result;
15555 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
15556 get_address_mode (rtl), mode,
15557 VAR_INIT_STATUS_INITIALIZED);
15558 if (mem_loc_result == NULL)
15559 mem_loc_result = tls_mem_loc_descriptor (rtl);
15560 if (mem_loc_result != NULL)
15562 if (!is_a <scalar_int_mode> (mode, &int_mode)
15563 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15565 dw_die_ref type_die;
15566 dw_loc_descr_ref deref;
15567 HOST_WIDE_INT size;
15569 if (dwarf_strict && dwarf_version < 5)
15570 return NULL;
15571 if (!GET_MODE_SIZE (mode).is_constant (&size))
15572 return NULL;
15573 type_die
15574 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15575 if (type_die == NULL)
15576 return NULL;
15577 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
15578 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
15579 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
15580 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
15581 add_loc_descr (&mem_loc_result, deref);
15583 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
15584 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15585 else
15586 add_loc_descr (&mem_loc_result,
15587 new_loc_descr (DW_OP_deref_size,
15588 GET_MODE_SIZE (int_mode), 0));
15590 break;
15592 case LO_SUM:
15593 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15595 case LABEL_REF:
15596 /* Some ports can transform a symbol ref into a label ref, because
15597 the symbol ref is too far away and has to be dumped into a constant
15598 pool. */
15599 case CONST:
15600 case SYMBOL_REF:
15601 if (!is_a <scalar_int_mode> (mode, &int_mode)
15602 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15603 #ifdef POINTERS_EXTEND_UNSIGNED
15604 && (int_mode != Pmode || mem_mode == VOIDmode)
15605 #endif
15607 break;
15608 if (GET_CODE (rtl) == SYMBOL_REF
15609 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15611 dw_loc_descr_ref temp;
15613 /* If this is not defined, we have no way to emit the data. */
15614 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15615 break;
15617 temp = new_addr_loc_descr (rtl, dtprel_true);
15619 /* We check for DWARF 5 here because gdb did not implement
15620 DW_OP_form_tls_address until after 7.12. */
15621 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15622 ? DW_OP_form_tls_address
15623 : DW_OP_GNU_push_tls_address),
15624 0, 0);
15625 add_loc_descr (&mem_loc_result, temp);
15627 break;
15630 if (!const_ok_for_output (rtl))
15632 if (GET_CODE (rtl) == CONST)
15633 switch (GET_CODE (XEXP (rtl, 0)))
15635 case NOT:
15636 op = DW_OP_not;
15637 goto try_const_unop;
15638 case NEG:
15639 op = DW_OP_neg;
15640 goto try_const_unop;
15641 try_const_unop:
15642 rtx arg;
15643 arg = XEXP (XEXP (rtl, 0), 0);
15644 if (!CONSTANT_P (arg))
15645 arg = gen_rtx_CONST (int_mode, arg);
15646 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15647 initialized);
15648 if (op0)
15650 mem_loc_result = op0;
15651 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15653 break;
15654 default:
15655 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15656 mem_mode, initialized);
15657 break;
15659 break;
15662 symref:
15663 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15664 vec_safe_push (used_rtx_array, rtl);
15665 break;
15667 case CONCAT:
15668 case CONCATN:
15669 case VAR_LOCATION:
15670 case DEBUG_IMPLICIT_PTR:
15671 expansion_failed (NULL_TREE, rtl,
15672 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15673 return 0;
15675 case ENTRY_VALUE:
15676 if (dwarf_strict && dwarf_version < 5)
15677 return NULL;
15678 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15680 if (!is_a <scalar_int_mode> (mode, &int_mode)
15681 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15682 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15683 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15684 else
15686 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15687 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15688 return NULL;
15689 op0 = one_reg_loc_descriptor (dbx_regnum,
15690 VAR_INIT_STATUS_INITIALIZED);
15693 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15694 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15696 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15697 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15698 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15699 return NULL;
15701 else
15702 gcc_unreachable ();
15703 if (op0 == NULL)
15704 return NULL;
15705 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15706 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15707 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15708 break;
15710 case DEBUG_PARAMETER_REF:
15711 mem_loc_result = parameter_ref_descriptor (rtl);
15712 break;
15714 case PRE_MODIFY:
15715 /* Extract the PLUS expression nested inside and fall into
15716 PLUS code below. */
15717 rtl = XEXP (rtl, 1);
15718 goto plus;
15720 case PRE_INC:
15721 case PRE_DEC:
15722 /* Turn these into a PLUS expression and fall into the PLUS code
15723 below. */
15724 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15725 gen_int_mode (GET_CODE (rtl) == PRE_INC
15726 ? GET_MODE_UNIT_SIZE (mem_mode)
15727 : -GET_MODE_UNIT_SIZE (mem_mode),
15728 mode));
15730 /* fall through */
15732 case PLUS:
15733 plus:
15734 if (is_based_loc (rtl)
15735 && is_a <scalar_int_mode> (mode, &int_mode)
15736 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15737 || XEXP (rtl, 0) == arg_pointer_rtx
15738 || XEXP (rtl, 0) == frame_pointer_rtx))
15739 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15740 INTVAL (XEXP (rtl, 1)),
15741 VAR_INIT_STATUS_INITIALIZED);
15742 else
15744 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15745 VAR_INIT_STATUS_INITIALIZED);
15746 if (mem_loc_result == 0)
15747 break;
15749 if (CONST_INT_P (XEXP (rtl, 1))
15750 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15751 <= DWARF2_ADDR_SIZE))
15752 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15753 else
15755 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15756 VAR_INIT_STATUS_INITIALIZED);
15757 if (op1 == 0)
15758 return NULL;
15759 add_loc_descr (&mem_loc_result, op1);
15760 add_loc_descr (&mem_loc_result,
15761 new_loc_descr (DW_OP_plus, 0, 0));
15764 break;
15766 /* If a pseudo-reg is optimized away, it is possible for it to
15767 be replaced with a MEM containing a multiply or shift. */
15768 case MINUS:
15769 op = DW_OP_minus;
15770 goto do_binop;
15772 case MULT:
15773 op = DW_OP_mul;
15774 goto do_binop;
15776 case DIV:
15777 if ((!dwarf_strict || dwarf_version >= 5)
15778 && is_a <scalar_int_mode> (mode, &int_mode)
15779 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15781 mem_loc_result = typed_binop (DW_OP_div, rtl,
15782 base_type_for_mode (mode, 0),
15783 int_mode, mem_mode);
15784 break;
15786 op = DW_OP_div;
15787 goto do_binop;
15789 case UMOD:
15790 op = DW_OP_mod;
15791 goto do_binop;
15793 case ASHIFT:
15794 op = DW_OP_shl;
15795 goto do_shift;
15797 case ASHIFTRT:
15798 op = DW_OP_shra;
15799 goto do_shift;
15801 case LSHIFTRT:
15802 op = DW_OP_shr;
15803 goto do_shift;
15805 do_shift:
15806 if (!is_a <scalar_int_mode> (mode, &int_mode))
15807 break;
15808 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15809 VAR_INIT_STATUS_INITIALIZED);
15811 rtx rtlop1 = XEXP (rtl, 1);
15812 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15813 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15814 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15815 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15816 VAR_INIT_STATUS_INITIALIZED);
15819 if (op0 == 0 || op1 == 0)
15820 break;
15822 mem_loc_result = op0;
15823 add_loc_descr (&mem_loc_result, op1);
15824 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15825 break;
15827 case AND:
15828 op = DW_OP_and;
15829 goto do_binop;
15831 case IOR:
15832 op = DW_OP_or;
15833 goto do_binop;
15835 case XOR:
15836 op = DW_OP_xor;
15837 goto do_binop;
15839 do_binop:
15840 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15841 VAR_INIT_STATUS_INITIALIZED);
15842 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15843 VAR_INIT_STATUS_INITIALIZED);
15845 if (op0 == 0 || op1 == 0)
15846 break;
15848 mem_loc_result = op0;
15849 add_loc_descr (&mem_loc_result, op1);
15850 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15851 break;
15853 case MOD:
15854 if ((!dwarf_strict || dwarf_version >= 5)
15855 && is_a <scalar_int_mode> (mode, &int_mode)
15856 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15858 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15859 base_type_for_mode (mode, 0),
15860 int_mode, mem_mode);
15861 break;
15864 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15865 VAR_INIT_STATUS_INITIALIZED);
15866 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15867 VAR_INIT_STATUS_INITIALIZED);
15869 if (op0 == 0 || op1 == 0)
15870 break;
15872 mem_loc_result = op0;
15873 add_loc_descr (&mem_loc_result, op1);
15874 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15875 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15876 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15877 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15878 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15879 break;
15881 case UDIV:
15882 if ((!dwarf_strict || dwarf_version >= 5)
15883 && is_a <scalar_int_mode> (mode, &int_mode))
15885 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15887 op = DW_OP_div;
15888 goto do_binop;
15890 mem_loc_result = typed_binop (DW_OP_div, rtl,
15891 base_type_for_mode (int_mode, 1),
15892 int_mode, mem_mode);
15894 break;
15896 case NOT:
15897 op = DW_OP_not;
15898 goto do_unop;
15900 case ABS:
15901 op = DW_OP_abs;
15902 goto do_unop;
15904 case NEG:
15905 op = DW_OP_neg;
15906 goto do_unop;
15908 do_unop:
15909 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15910 VAR_INIT_STATUS_INITIALIZED);
15912 if (op0 == 0)
15913 break;
15915 mem_loc_result = op0;
15916 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15917 break;
15919 case CONST_INT:
15920 if (!is_a <scalar_int_mode> (mode, &int_mode)
15921 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15922 #ifdef POINTERS_EXTEND_UNSIGNED
15923 || (int_mode == Pmode
15924 && mem_mode != VOIDmode
15925 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15926 #endif
15929 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15930 break;
15932 if ((!dwarf_strict || dwarf_version >= 5)
15933 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15934 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15936 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15937 scalar_int_mode amode;
15938 if (type_die == NULL)
15939 return NULL;
15940 if (INTVAL (rtl) >= 0
15941 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15942 .exists (&amode))
15943 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15944 /* const DW_OP_convert <XXX> vs.
15945 DW_OP_const_type <XXX, 1, const>. */
15946 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15947 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15949 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15950 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15951 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15952 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15953 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15954 add_loc_descr (&mem_loc_result, op0);
15955 return mem_loc_result;
15957 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15958 INTVAL (rtl));
15959 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15960 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15961 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15962 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15963 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15964 else
15966 mem_loc_result->dw_loc_oprnd2.val_class
15967 = dw_val_class_const_double;
15968 mem_loc_result->dw_loc_oprnd2.v.val_double
15969 = double_int::from_shwi (INTVAL (rtl));
15972 break;
15974 case CONST_DOUBLE:
15975 if (!dwarf_strict || dwarf_version >= 5)
15977 dw_die_ref type_die;
15979 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15980 CONST_DOUBLE rtx could represent either a large integer
15981 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15982 the value is always a floating point constant.
15984 When it is an integer, a CONST_DOUBLE is used whenever
15985 the constant requires 2 HWIs to be adequately represented.
15986 We output CONST_DOUBLEs as blocks. */
15987 if (mode == VOIDmode
15988 || (GET_MODE (rtl) == VOIDmode
15989 && maybe_ne (GET_MODE_BITSIZE (mode),
15990 HOST_BITS_PER_DOUBLE_INT)))
15991 break;
15992 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15993 if (type_die == NULL)
15994 return NULL;
15995 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15996 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15997 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15998 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15999 #if TARGET_SUPPORTS_WIDE_INT == 0
16000 if (!SCALAR_FLOAT_MODE_P (mode))
16002 mem_loc_result->dw_loc_oprnd2.val_class
16003 = dw_val_class_const_double;
16004 mem_loc_result->dw_loc_oprnd2.v.val_double
16005 = rtx_to_double_int (rtl);
16007 else
16008 #endif
16010 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
16011 unsigned int length = GET_MODE_SIZE (float_mode);
16012 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16014 insert_float (rtl, array);
16015 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16016 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16017 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16018 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16021 break;
16023 case CONST_WIDE_INT:
16024 if (!dwarf_strict || dwarf_version >= 5)
16026 dw_die_ref type_die;
16028 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
16029 if (type_die == NULL)
16030 return NULL;
16031 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
16032 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16033 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16034 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
16035 mem_loc_result->dw_loc_oprnd2.val_class
16036 = dw_val_class_wide_int;
16037 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16038 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
16040 break;
16042 case CONST_POLY_INT:
16043 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
16044 break;
16046 case EQ:
16047 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
16048 break;
16050 case GE:
16051 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16052 break;
16054 case GT:
16055 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16056 break;
16058 case LE:
16059 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16060 break;
16062 case LT:
16063 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16064 break;
16066 case NE:
16067 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
16068 break;
16070 case GEU:
16071 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
16072 break;
16074 case GTU:
16075 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
16076 break;
16078 case LEU:
16079 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
16080 break;
16082 case LTU:
16083 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
16084 break;
16086 case UMIN:
16087 case UMAX:
16088 if (!SCALAR_INT_MODE_P (mode))
16089 break;
16090 /* FALLTHRU */
16091 case SMIN:
16092 case SMAX:
16093 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
16094 break;
16096 case ZERO_EXTRACT:
16097 case SIGN_EXTRACT:
16098 if (CONST_INT_P (XEXP (rtl, 1))
16099 && CONST_INT_P (XEXP (rtl, 2))
16100 && is_a <scalar_int_mode> (mode, &int_mode)
16101 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
16102 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16103 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
16104 && ((unsigned) INTVAL (XEXP (rtl, 1))
16105 + (unsigned) INTVAL (XEXP (rtl, 2))
16106 <= GET_MODE_BITSIZE (int_mode)))
16108 int shift, size;
16109 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
16110 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16111 if (op0 == 0)
16112 break;
16113 if (GET_CODE (rtl) == SIGN_EXTRACT)
16114 op = DW_OP_shra;
16115 else
16116 op = DW_OP_shr;
16117 mem_loc_result = op0;
16118 size = INTVAL (XEXP (rtl, 1));
16119 shift = INTVAL (XEXP (rtl, 2));
16120 if (BITS_BIG_ENDIAN)
16121 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
16122 if (shift + size != (int) DWARF2_ADDR_SIZE)
16124 add_loc_descr (&mem_loc_result,
16125 int_loc_descriptor (DWARF2_ADDR_SIZE
16126 - shift - size));
16127 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
16129 if (size != (int) DWARF2_ADDR_SIZE)
16131 add_loc_descr (&mem_loc_result,
16132 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
16133 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
16136 break;
16138 case IF_THEN_ELSE:
16140 dw_loc_descr_ref op2, bra_node, drop_node;
16141 op0 = mem_loc_descriptor (XEXP (rtl, 0),
16142 GET_MODE (XEXP (rtl, 0)) == VOIDmode
16143 ? word_mode : GET_MODE (XEXP (rtl, 0)),
16144 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16145 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
16146 VAR_INIT_STATUS_INITIALIZED);
16147 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
16148 VAR_INIT_STATUS_INITIALIZED);
16149 if (op0 == NULL || op1 == NULL || op2 == NULL)
16150 break;
16152 mem_loc_result = op1;
16153 add_loc_descr (&mem_loc_result, op2);
16154 add_loc_descr (&mem_loc_result, op0);
16155 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16156 add_loc_descr (&mem_loc_result, bra_node);
16157 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
16158 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
16159 add_loc_descr (&mem_loc_result, drop_node);
16160 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16161 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
16163 break;
16165 case FLOAT_EXTEND:
16166 case FLOAT_TRUNCATE:
16167 case FLOAT:
16168 case UNSIGNED_FLOAT:
16169 case FIX:
16170 case UNSIGNED_FIX:
16171 if (!dwarf_strict || dwarf_version >= 5)
16173 dw_die_ref type_die;
16174 dw_loc_descr_ref cvt;
16176 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
16177 mem_mode, VAR_INIT_STATUS_INITIALIZED);
16178 if (op0 == NULL)
16179 break;
16180 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
16181 && (GET_CODE (rtl) == FLOAT
16182 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
16184 type_die = base_type_for_mode (int_mode,
16185 GET_CODE (rtl) == UNSIGNED_FLOAT);
16186 if (type_die == NULL)
16187 break;
16188 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16189 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16190 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16191 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16192 add_loc_descr (&op0, cvt);
16194 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
16195 if (type_die == NULL)
16196 break;
16197 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
16198 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16199 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
16200 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
16201 add_loc_descr (&op0, cvt);
16202 if (is_a <scalar_int_mode> (mode, &int_mode)
16203 && (GET_CODE (rtl) == FIX
16204 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
16206 op0 = convert_descriptor_to_mode (int_mode, op0);
16207 if (op0 == NULL)
16208 break;
16210 mem_loc_result = op0;
16212 break;
16214 case CLZ:
16215 case CTZ:
16216 case FFS:
16217 if (is_a <scalar_int_mode> (mode, &int_mode))
16218 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
16219 break;
16221 case POPCOUNT:
16222 case PARITY:
16223 if (is_a <scalar_int_mode> (mode, &int_mode))
16224 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
16225 break;
16227 case BSWAP:
16228 if (is_a <scalar_int_mode> (mode, &int_mode))
16229 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
16230 break;
16232 case ROTATE:
16233 case ROTATERT:
16234 if (is_a <scalar_int_mode> (mode, &int_mode))
16235 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
16236 break;
16238 case COMPARE:
16239 /* In theory, we could implement the above. */
16240 /* DWARF cannot represent the unsigned compare operations
16241 natively. */
16242 case SS_MULT:
16243 case US_MULT:
16244 case SS_DIV:
16245 case US_DIV:
16246 case SS_PLUS:
16247 case US_PLUS:
16248 case SS_MINUS:
16249 case US_MINUS:
16250 case SS_NEG:
16251 case US_NEG:
16252 case SS_ABS:
16253 case SS_ASHIFT:
16254 case US_ASHIFT:
16255 case SS_TRUNCATE:
16256 case US_TRUNCATE:
16257 case UNORDERED:
16258 case ORDERED:
16259 case UNEQ:
16260 case UNGE:
16261 case UNGT:
16262 case UNLE:
16263 case UNLT:
16264 case LTGT:
16265 case FRACT_CONVERT:
16266 case UNSIGNED_FRACT_CONVERT:
16267 case SAT_FRACT:
16268 case UNSIGNED_SAT_FRACT:
16269 case SQRT:
16270 case ASM_OPERANDS:
16271 case VEC_MERGE:
16272 case VEC_SELECT:
16273 case VEC_CONCAT:
16274 case VEC_DUPLICATE:
16275 case VEC_SERIES:
16276 case UNSPEC:
16277 case HIGH:
16278 case FMA:
16279 case STRICT_LOW_PART:
16280 case CONST_VECTOR:
16281 case CONST_FIXED:
16282 case CLRSB:
16283 case CLOBBER:
16284 /* If delegitimize_address couldn't do anything with the UNSPEC, we
16285 can't express it in the debug info. This can happen e.g. with some
16286 TLS UNSPECs. */
16287 break;
16289 case CONST_STRING:
16290 resolve_one_addr (&rtl);
16291 goto symref;
16293 /* RTL sequences inside PARALLEL record a series of DWARF operations for
16294 the expression. An UNSPEC rtx represents a raw DWARF operation,
16295 new_loc_descr is called for it to build the operation directly.
16296 Otherwise mem_loc_descriptor is called recursively. */
16297 case PARALLEL:
16299 int index = 0;
16300 dw_loc_descr_ref exp_result = NULL;
16302 for (; index < XVECLEN (rtl, 0); index++)
16304 rtx elem = XVECEXP (rtl, 0, index);
16305 if (GET_CODE (elem) == UNSPEC)
16307 /* Each DWARF operation UNSPEC contain two operands, if
16308 one operand is not used for the operation, const0_rtx is
16309 passed. */
16310 gcc_assert (XVECLEN (elem, 0) == 2);
16312 HOST_WIDE_INT dw_op = XINT (elem, 1);
16313 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
16314 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
16315 exp_result
16316 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
16317 oprnd2);
16319 else
16320 exp_result
16321 = mem_loc_descriptor (elem, mode, mem_mode,
16322 VAR_INIT_STATUS_INITIALIZED);
16324 if (!mem_loc_result)
16325 mem_loc_result = exp_result;
16326 else
16327 add_loc_descr (&mem_loc_result, exp_result);
16330 break;
16333 default:
16334 if (flag_checking)
16336 print_rtl (stderr, rtl);
16337 gcc_unreachable ();
16339 break;
16342 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16343 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16345 return mem_loc_result;
16348 /* Return a descriptor that describes the concatenation of two locations.
16349 This is typically a complex variable. */
16351 static dw_loc_descr_ref
16352 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
16354 /* At present we only track constant-sized pieces. */
16355 unsigned int size0, size1;
16356 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
16357 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
16358 return 0;
16360 dw_loc_descr_ref cc_loc_result = NULL;
16361 dw_loc_descr_ref x0_ref
16362 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16363 dw_loc_descr_ref x1_ref
16364 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16366 if (x0_ref == 0 || x1_ref == 0)
16367 return 0;
16369 cc_loc_result = x0_ref;
16370 add_loc_descr_op_piece (&cc_loc_result, size0);
16372 add_loc_descr (&cc_loc_result, x1_ref);
16373 add_loc_descr_op_piece (&cc_loc_result, size1);
16375 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
16376 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16378 return cc_loc_result;
16381 /* Return a descriptor that describes the concatenation of N
16382 locations. */
16384 static dw_loc_descr_ref
16385 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
16387 unsigned int i;
16388 dw_loc_descr_ref cc_loc_result = NULL;
16389 unsigned int n = XVECLEN (concatn, 0);
16390 unsigned int size;
16392 for (i = 0; i < n; ++i)
16394 dw_loc_descr_ref ref;
16395 rtx x = XVECEXP (concatn, 0, i);
16397 /* At present we only track constant-sized pieces. */
16398 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
16399 return NULL;
16401 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
16402 if (ref == NULL)
16403 return NULL;
16405 add_loc_descr (&cc_loc_result, ref);
16406 add_loc_descr_op_piece (&cc_loc_result, size);
16409 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
16410 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
16412 return cc_loc_result;
16415 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
16416 for DEBUG_IMPLICIT_PTR RTL. */
16418 static dw_loc_descr_ref
16419 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
16421 dw_loc_descr_ref ret;
16422 dw_die_ref ref;
16424 if (dwarf_strict && dwarf_version < 5)
16425 return NULL;
16426 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
16427 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
16428 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
16429 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
16430 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
16431 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
16432 if (ref)
16434 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
16435 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
16436 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
16438 else
16440 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
16441 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
16443 return ret;
16446 /* Output a proper Dwarf location descriptor for a variable or parameter
16447 which is either allocated in a register or in a memory location. For a
16448 register, we just generate an OP_REG and the register number. For a
16449 memory location we provide a Dwarf postfix expression describing how to
16450 generate the (dynamic) address of the object onto the address stack.
16452 MODE is mode of the decl if this loc_descriptor is going to be used in
16453 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
16454 allowed, VOIDmode otherwise.
16456 If we don't know how to describe it, return 0. */
16458 static dw_loc_descr_ref
16459 loc_descriptor (rtx rtl, machine_mode mode,
16460 enum var_init_status initialized)
16462 dw_loc_descr_ref loc_result = NULL;
16463 scalar_int_mode int_mode;
16465 switch (GET_CODE (rtl))
16467 case SUBREG:
16468 /* The case of a subreg may arise when we have a local (register)
16469 variable or a formal (register) parameter which doesn't quite fill
16470 up an entire register. For now, just assume that it is
16471 legitimate to make the Dwarf info refer to the whole register which
16472 contains the given subreg. */
16473 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
16474 loc_result = loc_descriptor (SUBREG_REG (rtl),
16475 GET_MODE (SUBREG_REG (rtl)), initialized);
16476 else
16477 goto do_default;
16478 break;
16480 case REG:
16481 loc_result = reg_loc_descriptor (rtl, initialized);
16482 break;
16484 case MEM:
16485 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16486 GET_MODE (rtl), initialized);
16487 if (loc_result == NULL)
16488 loc_result = tls_mem_loc_descriptor (rtl);
16489 if (loc_result == NULL)
16491 rtx new_rtl = avoid_constant_pool_reference (rtl);
16492 if (new_rtl != rtl)
16493 loc_result = loc_descriptor (new_rtl, mode, initialized);
16495 break;
16497 case CONCAT:
16498 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
16499 initialized);
16500 break;
16502 case CONCATN:
16503 loc_result = concatn_loc_descriptor (rtl, initialized);
16504 break;
16506 case VAR_LOCATION:
16507 /* Single part. */
16508 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
16510 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
16511 if (GET_CODE (loc) == EXPR_LIST)
16512 loc = XEXP (loc, 0);
16513 loc_result = loc_descriptor (loc, mode, initialized);
16514 break;
16517 rtl = XEXP (rtl, 1);
16518 /* FALLTHRU */
16520 case PARALLEL:
16522 rtvec par_elems = XVEC (rtl, 0);
16523 int num_elem = GET_NUM_ELEM (par_elems);
16524 machine_mode mode;
16525 int i, size;
16527 /* Create the first one, so we have something to add to. */
16528 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
16529 VOIDmode, initialized);
16530 if (loc_result == NULL)
16531 return NULL;
16532 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
16533 /* At present we only track constant-sized pieces. */
16534 if (!GET_MODE_SIZE (mode).is_constant (&size))
16535 return NULL;
16536 add_loc_descr_op_piece (&loc_result, size);
16537 for (i = 1; i < num_elem; i++)
16539 dw_loc_descr_ref temp;
16541 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
16542 VOIDmode, initialized);
16543 if (temp == NULL)
16544 return NULL;
16545 add_loc_descr (&loc_result, temp);
16546 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
16547 /* At present we only track constant-sized pieces. */
16548 if (!GET_MODE_SIZE (mode).is_constant (&size))
16549 return NULL;
16550 add_loc_descr_op_piece (&loc_result, size);
16553 break;
16555 case CONST_INT:
16556 if (mode != VOIDmode && mode != BLKmode)
16558 int_mode = as_a <scalar_int_mode> (mode);
16559 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
16560 INTVAL (rtl));
16562 break;
16564 case CONST_DOUBLE:
16565 if (mode == VOIDmode)
16566 mode = GET_MODE (rtl);
16568 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16570 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16572 /* Note that a CONST_DOUBLE rtx could represent either an integer
16573 or a floating-point constant. A CONST_DOUBLE is used whenever
16574 the constant requires more than one word in order to be
16575 adequately represented. We output CONST_DOUBLEs as blocks. */
16576 scalar_mode smode = as_a <scalar_mode> (mode);
16577 loc_result = new_loc_descr (DW_OP_implicit_value,
16578 GET_MODE_SIZE (smode), 0);
16579 #if TARGET_SUPPORTS_WIDE_INT == 0
16580 if (!SCALAR_FLOAT_MODE_P (smode))
16582 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
16583 loc_result->dw_loc_oprnd2.v.val_double
16584 = rtx_to_double_int (rtl);
16586 else
16587 #endif
16589 unsigned int length = GET_MODE_SIZE (smode);
16590 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16592 insert_float (rtl, array);
16593 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16594 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16595 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16596 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16599 break;
16601 case CONST_WIDE_INT:
16602 if (mode == VOIDmode)
16603 mode = GET_MODE (rtl);
16605 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16607 int_mode = as_a <scalar_int_mode> (mode);
16608 loc_result = new_loc_descr (DW_OP_implicit_value,
16609 GET_MODE_SIZE (int_mode), 0);
16610 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16611 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16612 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16614 break;
16616 case CONST_VECTOR:
16617 if (mode == VOIDmode)
16618 mode = GET_MODE (rtl);
16620 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16622 unsigned int length;
16623 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16624 return NULL;
16626 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16627 unsigned char *array
16628 = ggc_vec_alloc<unsigned char> (length * elt_size);
16629 unsigned int i;
16630 unsigned char *p;
16631 machine_mode imode = GET_MODE_INNER (mode);
16633 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16634 switch (GET_MODE_CLASS (mode))
16636 case MODE_VECTOR_INT:
16637 for (i = 0, p = array; i < length; i++, p += elt_size)
16639 rtx elt = CONST_VECTOR_ELT (rtl, i);
16640 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16642 break;
16644 case MODE_VECTOR_FLOAT:
16645 for (i = 0, p = array; i < length; i++, p += elt_size)
16647 rtx elt = CONST_VECTOR_ELT (rtl, i);
16648 insert_float (elt, p);
16650 break;
16652 default:
16653 gcc_unreachable ();
16656 loc_result = new_loc_descr (DW_OP_implicit_value,
16657 length * elt_size, 0);
16658 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16659 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16660 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16661 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16663 break;
16665 case CONST:
16666 if (mode == VOIDmode
16667 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16668 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16669 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16671 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16672 break;
16674 /* FALLTHROUGH */
16675 case SYMBOL_REF:
16676 if (!const_ok_for_output (rtl))
16677 break;
16678 /* FALLTHROUGH */
16679 case LABEL_REF:
16680 if (is_a <scalar_int_mode> (mode, &int_mode)
16681 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16682 && (dwarf_version >= 4 || !dwarf_strict))
16684 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16685 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16686 vec_safe_push (used_rtx_array, rtl);
16688 break;
16690 case DEBUG_IMPLICIT_PTR:
16691 loc_result = implicit_ptr_descriptor (rtl, 0);
16692 break;
16694 case PLUS:
16695 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16696 && CONST_INT_P (XEXP (rtl, 1)))
16698 loc_result
16699 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16700 break;
16702 /* FALLTHRU */
16703 do_default:
16704 default:
16705 if ((is_a <scalar_int_mode> (mode, &int_mode)
16706 && GET_MODE (rtl) == int_mode
16707 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16708 && dwarf_version >= 4)
16709 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16711 /* Value expression. */
16712 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16713 if (loc_result)
16714 add_loc_descr (&loc_result,
16715 new_loc_descr (DW_OP_stack_value, 0, 0));
16717 break;
16720 return loc_result;
16723 /* We need to figure out what section we should use as the base for the
16724 address ranges where a given location is valid.
16725 1. If this particular DECL has a section associated with it, use that.
16726 2. If this function has a section associated with it, use that.
16727 3. Otherwise, use the text section.
16728 XXX: If you split a variable across multiple sections, we won't notice. */
16730 static const char *
16731 secname_for_decl (const_tree decl)
16733 const char *secname;
16735 if (VAR_OR_FUNCTION_DECL_P (decl)
16736 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16737 && DECL_SECTION_NAME (decl))
16738 secname = DECL_SECTION_NAME (decl);
16739 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16740 secname = DECL_SECTION_NAME (current_function_decl);
16741 else if (cfun && in_cold_section_p)
16742 secname = crtl->subsections.cold_section_label;
16743 else
16744 secname = text_section_label;
16746 return secname;
16749 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16751 static bool
16752 decl_by_reference_p (tree decl)
16754 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16755 || VAR_P (decl))
16756 && DECL_BY_REFERENCE (decl));
16759 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16760 for VARLOC. */
16762 static dw_loc_descr_ref
16763 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16764 enum var_init_status initialized)
16766 int have_address = 0;
16767 dw_loc_descr_ref descr;
16768 machine_mode mode;
16770 if (want_address != 2)
16772 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16773 /* Single part. */
16774 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16776 varloc = PAT_VAR_LOCATION_LOC (varloc);
16777 if (GET_CODE (varloc) == EXPR_LIST)
16778 varloc = XEXP (varloc, 0);
16779 mode = GET_MODE (varloc);
16780 if (MEM_P (varloc))
16782 rtx addr = XEXP (varloc, 0);
16783 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16784 mode, initialized);
16785 if (descr)
16786 have_address = 1;
16787 else
16789 rtx x = avoid_constant_pool_reference (varloc);
16790 if (x != varloc)
16791 descr = mem_loc_descriptor (x, mode, VOIDmode,
16792 initialized);
16795 else
16796 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16798 else
16799 return 0;
16801 else
16803 if (GET_CODE (varloc) == VAR_LOCATION)
16804 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16805 else
16806 mode = DECL_MODE (loc);
16807 descr = loc_descriptor (varloc, mode, initialized);
16808 have_address = 1;
16811 if (!descr)
16812 return 0;
16814 if (want_address == 2 && !have_address
16815 && (dwarf_version >= 4 || !dwarf_strict))
16817 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16819 expansion_failed (loc, NULL_RTX,
16820 "DWARF address size mismatch");
16821 return 0;
16823 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16824 have_address = 1;
16826 /* Show if we can't fill the request for an address. */
16827 if (want_address && !have_address)
16829 expansion_failed (loc, NULL_RTX,
16830 "Want address and only have value");
16831 return 0;
16834 /* If we've got an address and don't want one, dereference. */
16835 if (!want_address && have_address)
16837 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16838 enum dwarf_location_atom op;
16840 if (size > DWARF2_ADDR_SIZE || size == -1)
16842 expansion_failed (loc, NULL_RTX,
16843 "DWARF address size mismatch");
16844 return 0;
16846 else if (size == DWARF2_ADDR_SIZE)
16847 op = DW_OP_deref;
16848 else
16849 op = DW_OP_deref_size;
16851 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16854 return descr;
16857 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16858 if it is not possible. */
16860 static dw_loc_descr_ref
16861 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16863 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16864 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16865 else if (dwarf_version >= 3 || !dwarf_strict)
16866 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16867 else
16868 return NULL;
16871 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16872 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16874 static dw_loc_descr_ref
16875 dw_sra_loc_expr (tree decl, rtx loc)
16877 rtx p;
16878 unsigned HOST_WIDE_INT padsize = 0;
16879 dw_loc_descr_ref descr, *descr_tail;
16880 unsigned HOST_WIDE_INT decl_size;
16881 rtx varloc;
16882 enum var_init_status initialized;
16884 if (DECL_SIZE (decl) == NULL
16885 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16886 return NULL;
16888 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16889 descr = NULL;
16890 descr_tail = &descr;
16892 for (p = loc; p; p = XEXP (p, 1))
16894 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16895 rtx loc_note = *decl_piece_varloc_ptr (p);
16896 dw_loc_descr_ref cur_descr;
16897 dw_loc_descr_ref *tail, last = NULL;
16898 unsigned HOST_WIDE_INT opsize = 0;
16900 if (loc_note == NULL_RTX
16901 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16903 padsize += bitsize;
16904 continue;
16906 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16907 varloc = NOTE_VAR_LOCATION (loc_note);
16908 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16909 if (cur_descr == NULL)
16911 padsize += bitsize;
16912 continue;
16915 /* Check that cur_descr either doesn't use
16916 DW_OP_*piece operations, or their sum is equal
16917 to bitsize. Otherwise we can't embed it. */
16918 for (tail = &cur_descr; *tail != NULL;
16919 tail = &(*tail)->dw_loc_next)
16920 if ((*tail)->dw_loc_opc == DW_OP_piece)
16922 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16923 * BITS_PER_UNIT;
16924 last = *tail;
16926 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16928 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16929 last = *tail;
16932 if (last != NULL && opsize != bitsize)
16934 padsize += bitsize;
16935 /* Discard the current piece of the descriptor and release any
16936 addr_table entries it uses. */
16937 remove_loc_list_addr_table_entries (cur_descr);
16938 continue;
16941 /* If there is a hole, add DW_OP_*piece after empty DWARF
16942 expression, which means that those bits are optimized out. */
16943 if (padsize)
16945 if (padsize > decl_size)
16947 remove_loc_list_addr_table_entries (cur_descr);
16948 goto discard_descr;
16950 decl_size -= padsize;
16951 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16952 if (*descr_tail == NULL)
16954 remove_loc_list_addr_table_entries (cur_descr);
16955 goto discard_descr;
16957 descr_tail = &(*descr_tail)->dw_loc_next;
16958 padsize = 0;
16960 *descr_tail = cur_descr;
16961 descr_tail = tail;
16962 if (bitsize > decl_size)
16963 goto discard_descr;
16964 decl_size -= bitsize;
16965 if (last == NULL)
16967 HOST_WIDE_INT offset = 0;
16968 if (GET_CODE (varloc) == VAR_LOCATION
16969 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16971 varloc = PAT_VAR_LOCATION_LOC (varloc);
16972 if (GET_CODE (varloc) == EXPR_LIST)
16973 varloc = XEXP (varloc, 0);
16977 if (GET_CODE (varloc) == CONST
16978 || GET_CODE (varloc) == SIGN_EXTEND
16979 || GET_CODE (varloc) == ZERO_EXTEND)
16980 varloc = XEXP (varloc, 0);
16981 else if (GET_CODE (varloc) == SUBREG)
16982 varloc = SUBREG_REG (varloc);
16983 else
16984 break;
16986 while (1);
16987 /* DW_OP_bit_size offset should be zero for register
16988 or implicit location descriptions and empty location
16989 descriptions, but for memory addresses needs big endian
16990 adjustment. */
16991 if (MEM_P (varloc))
16993 unsigned HOST_WIDE_INT memsize;
16994 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16995 goto discard_descr;
16996 memsize *= BITS_PER_UNIT;
16997 if (memsize != bitsize)
16999 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
17000 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
17001 goto discard_descr;
17002 if (memsize < bitsize)
17003 goto discard_descr;
17004 if (BITS_BIG_ENDIAN)
17005 offset = memsize - bitsize;
17009 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
17010 if (*descr_tail == NULL)
17011 goto discard_descr;
17012 descr_tail = &(*descr_tail)->dw_loc_next;
17016 /* If there were any non-empty expressions, add padding till the end of
17017 the decl. */
17018 if (descr != NULL && decl_size != 0)
17020 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
17021 if (*descr_tail == NULL)
17022 goto discard_descr;
17024 return descr;
17026 discard_descr:
17027 /* Discard the descriptor and release any addr_table entries it uses. */
17028 remove_loc_list_addr_table_entries (descr);
17029 return NULL;
17032 /* Return the dwarf representation of the location list LOC_LIST of
17033 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
17034 function. */
17036 static dw_loc_list_ref
17037 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
17039 const char *endname, *secname;
17040 var_loc_view endview;
17041 rtx varloc;
17042 enum var_init_status initialized;
17043 struct var_loc_node *node;
17044 dw_loc_descr_ref descr;
17045 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17046 dw_loc_list_ref list = NULL;
17047 dw_loc_list_ref *listp = &list;
17049 /* Now that we know what section we are using for a base,
17050 actually construct the list of locations.
17051 The first location information is what is passed to the
17052 function that creates the location list, and the remaining
17053 locations just get added on to that list.
17054 Note that we only know the start address for a location
17055 (IE location changes), so to build the range, we use
17056 the range [current location start, next location start].
17057 This means we have to special case the last node, and generate
17058 a range of [last location start, end of function label]. */
17060 if (cfun && crtl->has_bb_partition)
17062 bool save_in_cold_section_p = in_cold_section_p;
17063 in_cold_section_p = first_function_block_is_cold;
17064 if (loc_list->last_before_switch == NULL)
17065 in_cold_section_p = !in_cold_section_p;
17066 secname = secname_for_decl (decl);
17067 in_cold_section_p = save_in_cold_section_p;
17069 else
17070 secname = secname_for_decl (decl);
17072 for (node = loc_list->first; node; node = node->next)
17074 bool range_across_switch = false;
17075 if (GET_CODE (node->loc) == EXPR_LIST
17076 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
17078 if (GET_CODE (node->loc) == EXPR_LIST)
17080 descr = NULL;
17081 /* This requires DW_OP_{,bit_}piece, which is not usable
17082 inside DWARF expressions. */
17083 if (want_address == 2)
17084 descr = dw_sra_loc_expr (decl, node->loc);
17086 else
17088 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17089 varloc = NOTE_VAR_LOCATION (node->loc);
17090 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
17092 if (descr)
17094 /* If section switch happens in between node->label
17095 and node->next->label (or end of function) and
17096 we can't emit it as a single entry list,
17097 emit two ranges, first one ending at the end
17098 of first partition and second one starting at the
17099 beginning of second partition. */
17100 if (node == loc_list->last_before_switch
17101 && (node != loc_list->first || loc_list->first->next)
17102 && current_function_decl)
17104 endname = cfun->fde->dw_fde_end;
17105 endview = 0;
17106 range_across_switch = true;
17108 /* The variable has a location between NODE->LABEL and
17109 NODE->NEXT->LABEL. */
17110 else if (node->next)
17111 endname = node->next->label, endview = node->next->view;
17112 /* If the variable has a location at the last label
17113 it keeps its location until the end of function. */
17114 else if (!current_function_decl)
17115 endname = text_end_label, endview = 0;
17116 else
17118 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17119 current_function_funcdef_no);
17120 endname = ggc_strdup (label_id);
17121 endview = 0;
17124 *listp = new_loc_list (descr, node->label, node->view,
17125 endname, endview, secname);
17126 if (TREE_CODE (decl) == PARM_DECL
17127 && node == loc_list->first
17128 && NOTE_P (node->loc)
17129 && strcmp (node->label, endname) == 0)
17130 (*listp)->force = true;
17131 listp = &(*listp)->dw_loc_next;
17135 if (cfun
17136 && crtl->has_bb_partition
17137 && node == loc_list->last_before_switch)
17139 bool save_in_cold_section_p = in_cold_section_p;
17140 in_cold_section_p = !first_function_block_is_cold;
17141 secname = secname_for_decl (decl);
17142 in_cold_section_p = save_in_cold_section_p;
17145 if (range_across_switch)
17147 if (GET_CODE (node->loc) == EXPR_LIST)
17148 descr = dw_sra_loc_expr (decl, node->loc);
17149 else
17151 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
17152 varloc = NOTE_VAR_LOCATION (node->loc);
17153 descr = dw_loc_list_1 (decl, varloc, want_address,
17154 initialized);
17156 gcc_assert (descr);
17157 /* The variable has a location between NODE->LABEL and
17158 NODE->NEXT->LABEL. */
17159 if (node->next)
17160 endname = node->next->label, endview = node->next->view;
17161 else
17162 endname = cfun->fde->dw_fde_second_end, endview = 0;
17163 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin, 0,
17164 endname, endview, secname);
17165 listp = &(*listp)->dw_loc_next;
17169 /* Try to avoid the overhead of a location list emitting a location
17170 expression instead, but only if we didn't have more than one
17171 location entry in the first place. If some entries were not
17172 representable, we don't want to pretend a single entry that was
17173 applies to the entire scope in which the variable is
17174 available. */
17175 if (list && loc_list->first->next)
17176 gen_llsym (list);
17177 else
17178 maybe_gen_llsym (list);
17180 return list;
17183 /* Return if the loc_list has only single element and thus can be represented
17184 as location description. */
17186 static bool
17187 single_element_loc_list_p (dw_loc_list_ref list)
17189 gcc_assert (!list->dw_loc_next || list->ll_symbol);
17190 return !list->ll_symbol;
17193 /* Duplicate a single element of location list. */
17195 static inline dw_loc_descr_ref
17196 copy_loc_descr (dw_loc_descr_ref ref)
17198 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
17199 memcpy (copy, ref, sizeof (dw_loc_descr_node));
17200 return copy;
17203 /* To each location in list LIST append loc descr REF. */
17205 static void
17206 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17208 dw_loc_descr_ref copy;
17209 add_loc_descr (&list->expr, ref);
17210 list = list->dw_loc_next;
17211 while (list)
17213 copy = copy_loc_descr (ref);
17214 add_loc_descr (&list->expr, copy);
17215 while (copy->dw_loc_next)
17216 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17217 list = list->dw_loc_next;
17221 /* To each location in list LIST prepend loc descr REF. */
17223 static void
17224 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
17226 dw_loc_descr_ref copy;
17227 dw_loc_descr_ref ref_end = list->expr;
17228 add_loc_descr (&ref, list->expr);
17229 list->expr = ref;
17230 list = list->dw_loc_next;
17231 while (list)
17233 dw_loc_descr_ref end = list->expr;
17234 list->expr = copy = copy_loc_descr (ref);
17235 while (copy->dw_loc_next != ref_end)
17236 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
17237 copy->dw_loc_next = end;
17238 list = list->dw_loc_next;
17242 /* Given two lists RET and LIST
17243 produce location list that is result of adding expression in LIST
17244 to expression in RET on each position in program.
17245 Might be destructive on both RET and LIST.
17247 TODO: We handle only simple cases of RET or LIST having at most one
17248 element. General case would involve sorting the lists in program order
17249 and merging them that will need some additional work.
17250 Adding that will improve quality of debug info especially for SRA-ed
17251 structures. */
17253 static void
17254 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
17256 if (!list)
17257 return;
17258 if (!*ret)
17260 *ret = list;
17261 return;
17263 if (!list->dw_loc_next)
17265 add_loc_descr_to_each (*ret, list->expr);
17266 return;
17268 if (!(*ret)->dw_loc_next)
17270 prepend_loc_descr_to_each (list, (*ret)->expr);
17271 *ret = list;
17272 return;
17274 expansion_failed (NULL_TREE, NULL_RTX,
17275 "Don't know how to merge two non-trivial"
17276 " location lists.\n");
17277 *ret = NULL;
17278 return;
17281 /* LOC is constant expression. Try a luck, look it up in constant
17282 pool and return its loc_descr of its address. */
17284 static dw_loc_descr_ref
17285 cst_pool_loc_descr (tree loc)
17287 /* Get an RTL for this, if something has been emitted. */
17288 rtx rtl = lookup_constant_def (loc);
17290 if (!rtl || !MEM_P (rtl))
17292 gcc_assert (!rtl);
17293 return 0;
17295 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
17297 /* TODO: We might get more coverage if we was actually delaying expansion
17298 of all expressions till end of compilation when constant pools are fully
17299 populated. */
17300 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
17302 expansion_failed (loc, NULL_RTX,
17303 "CST value in contant pool but not marked.");
17304 return 0;
17306 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
17307 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
17310 /* Return dw_loc_list representing address of addr_expr LOC
17311 by looking for inner INDIRECT_REF expression and turning
17312 it into simple arithmetics.
17314 See loc_list_from_tree for the meaning of CONTEXT. */
17316 static dw_loc_list_ref
17317 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
17318 loc_descr_context *context)
17320 tree obj, offset;
17321 poly_int64 bitsize, bitpos, bytepos;
17322 machine_mode mode;
17323 int unsignedp, reversep, volatilep = 0;
17324 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17326 obj = get_inner_reference (TREE_OPERAND (loc, 0),
17327 &bitsize, &bitpos, &offset, &mode,
17328 &unsignedp, &reversep, &volatilep);
17329 STRIP_NOPS (obj);
17330 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
17332 expansion_failed (loc, NULL_RTX, "bitfield access");
17333 return 0;
17335 if (!INDIRECT_REF_P (obj))
17337 expansion_failed (obj,
17338 NULL_RTX, "no indirect ref in inner refrence");
17339 return 0;
17341 if (!offset && known_eq (bitpos, 0))
17342 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
17343 context);
17344 else if (toplev
17345 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
17346 && (dwarf_version >= 4 || !dwarf_strict))
17348 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
17349 if (!list_ret)
17350 return 0;
17351 if (offset)
17353 /* Variable offset. */
17354 list_ret1 = loc_list_from_tree (offset, 0, context);
17355 if (list_ret1 == 0)
17356 return 0;
17357 add_loc_list (&list_ret, list_ret1);
17358 if (!list_ret)
17359 return 0;
17360 add_loc_descr_to_each (list_ret,
17361 new_loc_descr (DW_OP_plus, 0, 0));
17363 HOST_WIDE_INT value;
17364 if (bytepos.is_constant (&value) && value > 0)
17365 add_loc_descr_to_each (list_ret,
17366 new_loc_descr (DW_OP_plus_uconst, value, 0));
17367 else if (maybe_ne (bytepos, 0))
17368 loc_list_plus_const (list_ret, bytepos);
17369 add_loc_descr_to_each (list_ret,
17370 new_loc_descr (DW_OP_stack_value, 0, 0));
17372 return list_ret;
17375 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
17376 all operations from LOC are nops, move to the last one. Insert in NOPS all
17377 operations that are skipped. */
17379 static void
17380 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
17381 hash_set<dw_loc_descr_ref> &nops)
17383 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
17385 nops.add (loc);
17386 loc = loc->dw_loc_next;
17390 /* Helper for loc_descr_without_nops: free the location description operation
17391 P. */
17393 bool
17394 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
17396 ggc_free (loc);
17397 return true;
17400 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
17401 finishes LOC. */
17403 static void
17404 loc_descr_without_nops (dw_loc_descr_ref &loc)
17406 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
17407 return;
17409 /* Set of all DW_OP_nop operations we remove. */
17410 hash_set<dw_loc_descr_ref> nops;
17412 /* First, strip all prefix NOP operations in order to keep the head of the
17413 operations list. */
17414 loc_descr_to_next_no_nop (loc, nops);
17416 for (dw_loc_descr_ref cur = loc; cur != NULL;)
17418 /* For control flow operations: strip "prefix" nops in destination
17419 labels. */
17420 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
17421 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
17422 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
17423 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
17425 /* Do the same for the operations that follow, then move to the next
17426 iteration. */
17427 if (cur->dw_loc_next != NULL)
17428 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
17429 cur = cur->dw_loc_next;
17432 nops.traverse<void *, free_loc_descr> (NULL);
17436 struct dwarf_procedure_info;
17438 /* Helper structure for location descriptions generation. */
17439 struct loc_descr_context
17441 /* The type that is implicitly referenced by DW_OP_push_object_address, or
17442 NULL_TREE if DW_OP_push_object_address in invalid for this location
17443 description. This is used when processing PLACEHOLDER_EXPR nodes. */
17444 tree context_type;
17445 /* The ..._DECL node that should be translated as a
17446 DW_OP_push_object_address operation. */
17447 tree base_decl;
17448 /* Information about the DWARF procedure we are currently generating. NULL if
17449 we are not generating a DWARF procedure. */
17450 struct dwarf_procedure_info *dpi;
17451 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
17452 by consumer. Used for DW_TAG_generic_subrange attributes. */
17453 bool placeholder_arg;
17454 /* True if PLACEHOLDER_EXPR has been seen. */
17455 bool placeholder_seen;
17458 /* DWARF procedures generation
17460 DWARF expressions (aka. location descriptions) are used to encode variable
17461 things such as sizes or offsets. Such computations can have redundant parts
17462 that can be factorized in order to reduce the size of the output debug
17463 information. This is the whole point of DWARF procedures.
17465 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
17466 already factorized into functions ("size functions") in order to handle very
17467 big and complex types. Such functions are quite simple: they have integral
17468 arguments, they return an integral result and their body contains only a
17469 return statement with arithmetic expressions. This is the only kind of
17470 function we are interested in translating into DWARF procedures, here.
17472 DWARF expressions and DWARF procedure are executed using a stack, so we have
17473 to define some calling convention for them to interact. Let's say that:
17475 - Before calling a DWARF procedure, DWARF expressions must push on the stack
17476 all arguments in reverse order (right-to-left) so that when the DWARF
17477 procedure execution starts, the first argument is the top of the stack.
17479 - Then, when returning, the DWARF procedure must have consumed all arguments
17480 on the stack, must have pushed the result and touched nothing else.
17482 - Each integral argument and the result are integral types can be hold in a
17483 single stack slot.
17485 - We call "frame offset" the number of stack slots that are "under DWARF
17486 procedure control": it includes the arguments slots, the temporaries and
17487 the result slot. Thus, it is equal to the number of arguments when the
17488 procedure execution starts and must be equal to one (the result) when it
17489 returns. */
17491 /* Helper structure used when generating operations for a DWARF procedure. */
17492 struct dwarf_procedure_info
17494 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
17495 currently translated. */
17496 tree fndecl;
17497 /* The number of arguments FNDECL takes. */
17498 unsigned args_count;
17501 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
17502 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
17503 equate it to this DIE. */
17505 static dw_die_ref
17506 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
17507 dw_die_ref parent_die)
17509 dw_die_ref dwarf_proc_die;
17511 if ((dwarf_version < 3 && dwarf_strict)
17512 || location == NULL)
17513 return NULL;
17515 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
17516 if (fndecl)
17517 equate_decl_number_to_die (fndecl, dwarf_proc_die);
17518 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
17519 return dwarf_proc_die;
17522 /* Return whether TYPE is a supported type as a DWARF procedure argument
17523 type or return type (we handle only scalar types and pointer types that
17524 aren't wider than the DWARF expression evaluation stack. */
17526 static bool
17527 is_handled_procedure_type (tree type)
17529 return ((INTEGRAL_TYPE_P (type)
17530 || TREE_CODE (type) == OFFSET_TYPE
17531 || TREE_CODE (type) == POINTER_TYPE)
17532 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
17535 /* Helper for resolve_args_picking: do the same but stop when coming across
17536 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
17537 offset *before* evaluating the corresponding operation. */
17539 static bool
17540 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17541 struct dwarf_procedure_info *dpi,
17542 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
17544 /* The "frame_offset" identifier is already used to name a macro... */
17545 unsigned frame_offset_ = initial_frame_offset;
17546 dw_loc_descr_ref l;
17548 for (l = loc; l != NULL;)
17550 bool existed;
17551 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
17553 /* If we already met this node, there is nothing to compute anymore. */
17554 if (existed)
17556 /* Make sure that the stack size is consistent wherever the execution
17557 flow comes from. */
17558 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
17559 break;
17561 l_frame_offset = frame_offset_;
17563 /* If needed, relocate the picking offset with respect to the frame
17564 offset. */
17565 if (l->frame_offset_rel)
17567 unsigned HOST_WIDE_INT off;
17568 switch (l->dw_loc_opc)
17570 case DW_OP_pick:
17571 off = l->dw_loc_oprnd1.v.val_unsigned;
17572 break;
17573 case DW_OP_dup:
17574 off = 0;
17575 break;
17576 case DW_OP_over:
17577 off = 1;
17578 break;
17579 default:
17580 gcc_unreachable ();
17582 /* frame_offset_ is the size of the current stack frame, including
17583 incoming arguments. Besides, the arguments are pushed
17584 right-to-left. Thus, in order to access the Nth argument from
17585 this operation node, the picking has to skip temporaries *plus*
17586 one stack slot per argument (0 for the first one, 1 for the second
17587 one, etc.).
17589 The targetted argument number (N) is already set as the operand,
17590 and the number of temporaries can be computed with:
17591 frame_offsets_ - dpi->args_count */
17592 off += frame_offset_ - dpi->args_count;
17594 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17595 if (off > 255)
17596 return false;
17598 if (off == 0)
17600 l->dw_loc_opc = DW_OP_dup;
17601 l->dw_loc_oprnd1.v.val_unsigned = 0;
17603 else if (off == 1)
17605 l->dw_loc_opc = DW_OP_over;
17606 l->dw_loc_oprnd1.v.val_unsigned = 0;
17608 else
17610 l->dw_loc_opc = DW_OP_pick;
17611 l->dw_loc_oprnd1.v.val_unsigned = off;
17615 /* Update frame_offset according to the effect the current operation has
17616 on the stack. */
17617 switch (l->dw_loc_opc)
17619 case DW_OP_deref:
17620 case DW_OP_swap:
17621 case DW_OP_rot:
17622 case DW_OP_abs:
17623 case DW_OP_neg:
17624 case DW_OP_not:
17625 case DW_OP_plus_uconst:
17626 case DW_OP_skip:
17627 case DW_OP_reg0:
17628 case DW_OP_reg1:
17629 case DW_OP_reg2:
17630 case DW_OP_reg3:
17631 case DW_OP_reg4:
17632 case DW_OP_reg5:
17633 case DW_OP_reg6:
17634 case DW_OP_reg7:
17635 case DW_OP_reg8:
17636 case DW_OP_reg9:
17637 case DW_OP_reg10:
17638 case DW_OP_reg11:
17639 case DW_OP_reg12:
17640 case DW_OP_reg13:
17641 case DW_OP_reg14:
17642 case DW_OP_reg15:
17643 case DW_OP_reg16:
17644 case DW_OP_reg17:
17645 case DW_OP_reg18:
17646 case DW_OP_reg19:
17647 case DW_OP_reg20:
17648 case DW_OP_reg21:
17649 case DW_OP_reg22:
17650 case DW_OP_reg23:
17651 case DW_OP_reg24:
17652 case DW_OP_reg25:
17653 case DW_OP_reg26:
17654 case DW_OP_reg27:
17655 case DW_OP_reg28:
17656 case DW_OP_reg29:
17657 case DW_OP_reg30:
17658 case DW_OP_reg31:
17659 case DW_OP_bregx:
17660 case DW_OP_piece:
17661 case DW_OP_deref_size:
17662 case DW_OP_nop:
17663 case DW_OP_bit_piece:
17664 case DW_OP_implicit_value:
17665 case DW_OP_stack_value:
17666 break;
17668 case DW_OP_addr:
17669 case DW_OP_const1u:
17670 case DW_OP_const1s:
17671 case DW_OP_const2u:
17672 case DW_OP_const2s:
17673 case DW_OP_const4u:
17674 case DW_OP_const4s:
17675 case DW_OP_const8u:
17676 case DW_OP_const8s:
17677 case DW_OP_constu:
17678 case DW_OP_consts:
17679 case DW_OP_dup:
17680 case DW_OP_over:
17681 case DW_OP_pick:
17682 case DW_OP_lit0:
17683 case DW_OP_lit1:
17684 case DW_OP_lit2:
17685 case DW_OP_lit3:
17686 case DW_OP_lit4:
17687 case DW_OP_lit5:
17688 case DW_OP_lit6:
17689 case DW_OP_lit7:
17690 case DW_OP_lit8:
17691 case DW_OP_lit9:
17692 case DW_OP_lit10:
17693 case DW_OP_lit11:
17694 case DW_OP_lit12:
17695 case DW_OP_lit13:
17696 case DW_OP_lit14:
17697 case DW_OP_lit15:
17698 case DW_OP_lit16:
17699 case DW_OP_lit17:
17700 case DW_OP_lit18:
17701 case DW_OP_lit19:
17702 case DW_OP_lit20:
17703 case DW_OP_lit21:
17704 case DW_OP_lit22:
17705 case DW_OP_lit23:
17706 case DW_OP_lit24:
17707 case DW_OP_lit25:
17708 case DW_OP_lit26:
17709 case DW_OP_lit27:
17710 case DW_OP_lit28:
17711 case DW_OP_lit29:
17712 case DW_OP_lit30:
17713 case DW_OP_lit31:
17714 case DW_OP_breg0:
17715 case DW_OP_breg1:
17716 case DW_OP_breg2:
17717 case DW_OP_breg3:
17718 case DW_OP_breg4:
17719 case DW_OP_breg5:
17720 case DW_OP_breg6:
17721 case DW_OP_breg7:
17722 case DW_OP_breg8:
17723 case DW_OP_breg9:
17724 case DW_OP_breg10:
17725 case DW_OP_breg11:
17726 case DW_OP_breg12:
17727 case DW_OP_breg13:
17728 case DW_OP_breg14:
17729 case DW_OP_breg15:
17730 case DW_OP_breg16:
17731 case DW_OP_breg17:
17732 case DW_OP_breg18:
17733 case DW_OP_breg19:
17734 case DW_OP_breg20:
17735 case DW_OP_breg21:
17736 case DW_OP_breg22:
17737 case DW_OP_breg23:
17738 case DW_OP_breg24:
17739 case DW_OP_breg25:
17740 case DW_OP_breg26:
17741 case DW_OP_breg27:
17742 case DW_OP_breg28:
17743 case DW_OP_breg29:
17744 case DW_OP_breg30:
17745 case DW_OP_breg31:
17746 case DW_OP_fbreg:
17747 case DW_OP_push_object_address:
17748 case DW_OP_call_frame_cfa:
17749 case DW_OP_GNU_variable_value:
17750 ++frame_offset_;
17751 break;
17753 case DW_OP_drop:
17754 case DW_OP_xderef:
17755 case DW_OP_and:
17756 case DW_OP_div:
17757 case DW_OP_minus:
17758 case DW_OP_mod:
17759 case DW_OP_mul:
17760 case DW_OP_or:
17761 case DW_OP_plus:
17762 case DW_OP_shl:
17763 case DW_OP_shr:
17764 case DW_OP_shra:
17765 case DW_OP_xor:
17766 case DW_OP_bra:
17767 case DW_OP_eq:
17768 case DW_OP_ge:
17769 case DW_OP_gt:
17770 case DW_OP_le:
17771 case DW_OP_lt:
17772 case DW_OP_ne:
17773 case DW_OP_regx:
17774 case DW_OP_xderef_size:
17775 --frame_offset_;
17776 break;
17778 case DW_OP_call2:
17779 case DW_OP_call4:
17780 case DW_OP_call_ref:
17782 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17783 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17785 if (stack_usage == NULL)
17786 return false;
17787 frame_offset_ += *stack_usage;
17788 break;
17791 case DW_OP_implicit_pointer:
17792 case DW_OP_entry_value:
17793 case DW_OP_const_type:
17794 case DW_OP_regval_type:
17795 case DW_OP_deref_type:
17796 case DW_OP_convert:
17797 case DW_OP_reinterpret:
17798 case DW_OP_form_tls_address:
17799 case DW_OP_GNU_push_tls_address:
17800 case DW_OP_GNU_uninit:
17801 case DW_OP_GNU_encoded_addr:
17802 case DW_OP_GNU_implicit_pointer:
17803 case DW_OP_GNU_entry_value:
17804 case DW_OP_GNU_const_type:
17805 case DW_OP_GNU_regval_type:
17806 case DW_OP_GNU_deref_type:
17807 case DW_OP_GNU_convert:
17808 case DW_OP_GNU_reinterpret:
17809 case DW_OP_GNU_parameter_ref:
17810 /* loc_list_from_tree will probably not output these operations for
17811 size functions, so assume they will not appear here. */
17812 /* Fall through... */
17814 default:
17815 gcc_unreachable ();
17818 /* Now, follow the control flow (except subroutine calls). */
17819 switch (l->dw_loc_opc)
17821 case DW_OP_bra:
17822 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17823 frame_offsets))
17824 return false;
17825 /* Fall through. */
17827 case DW_OP_skip:
17828 l = l->dw_loc_oprnd1.v.val_loc;
17829 break;
17831 case DW_OP_stack_value:
17832 return true;
17834 default:
17835 l = l->dw_loc_next;
17836 break;
17840 return true;
17843 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17844 operations) in order to resolve the operand of DW_OP_pick operations that
17845 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17846 offset *before* LOC is executed. Return if all relocations were
17847 successful. */
17849 static bool
17850 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17851 struct dwarf_procedure_info *dpi)
17853 /* Associate to all visited operations the frame offset *before* evaluating
17854 this operation. */
17855 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17857 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17858 frame_offsets);
17861 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17862 Return NULL if it is not possible. */
17864 static dw_die_ref
17865 function_to_dwarf_procedure (tree fndecl)
17867 struct loc_descr_context ctx;
17868 struct dwarf_procedure_info dpi;
17869 dw_die_ref dwarf_proc_die;
17870 tree tree_body = DECL_SAVED_TREE (fndecl);
17871 dw_loc_descr_ref loc_body, epilogue;
17873 tree cursor;
17874 unsigned i;
17876 /* Do not generate multiple DWARF procedures for the same function
17877 declaration. */
17878 dwarf_proc_die = lookup_decl_die (fndecl);
17879 if (dwarf_proc_die != NULL)
17880 return dwarf_proc_die;
17882 /* DWARF procedures are available starting with the DWARFv3 standard. */
17883 if (dwarf_version < 3 && dwarf_strict)
17884 return NULL;
17886 /* We handle only functions for which we still have a body, that return a
17887 supported type and that takes arguments with supported types. Note that
17888 there is no point translating functions that return nothing. */
17889 if (tree_body == NULL_TREE
17890 || DECL_RESULT (fndecl) == NULL_TREE
17891 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17892 return NULL;
17894 for (cursor = DECL_ARGUMENTS (fndecl);
17895 cursor != NULL_TREE;
17896 cursor = TREE_CHAIN (cursor))
17897 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17898 return NULL;
17900 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17901 if (TREE_CODE (tree_body) != RETURN_EXPR)
17902 return NULL;
17903 tree_body = TREE_OPERAND (tree_body, 0);
17904 if (TREE_CODE (tree_body) != MODIFY_EXPR
17905 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17906 return NULL;
17907 tree_body = TREE_OPERAND (tree_body, 1);
17909 /* Try to translate the body expression itself. Note that this will probably
17910 cause an infinite recursion if its call graph has a cycle. This is very
17911 unlikely for size functions, however, so don't bother with such things at
17912 the moment. */
17913 ctx.context_type = NULL_TREE;
17914 ctx.base_decl = NULL_TREE;
17915 ctx.dpi = &dpi;
17916 ctx.placeholder_arg = false;
17917 ctx.placeholder_seen = false;
17918 dpi.fndecl = fndecl;
17919 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17920 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17921 if (!loc_body)
17922 return NULL;
17924 /* After evaluating all operands in "loc_body", we should still have on the
17925 stack all arguments plus the desired function result (top of the stack).
17926 Generate code in order to keep only the result in our stack frame. */
17927 epilogue = NULL;
17928 for (i = 0; i < dpi.args_count; ++i)
17930 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17931 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17932 op_couple->dw_loc_next->dw_loc_next = epilogue;
17933 epilogue = op_couple;
17935 add_loc_descr (&loc_body, epilogue);
17936 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17937 return NULL;
17939 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17940 because they are considered useful. Now there is an epilogue, they are
17941 not anymore, so give it another try. */
17942 loc_descr_without_nops (loc_body);
17944 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17945 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17946 though, given that size functions do not come from source, so they should
17947 not have a dedicated DW_TAG_subprogram DIE. */
17948 dwarf_proc_die
17949 = new_dwarf_proc_die (loc_body, fndecl,
17950 get_context_die (DECL_CONTEXT (fndecl)));
17952 /* The called DWARF procedure consumes one stack slot per argument and
17953 returns one stack slot. */
17954 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17956 return dwarf_proc_die;
17960 /* Generate Dwarf location list representing LOC.
17961 If WANT_ADDRESS is false, expression computing LOC will be computed
17962 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17963 if WANT_ADDRESS is 2, expression computing address useable in location
17964 will be returned (i.e. DW_OP_reg can be used
17965 to refer to register values).
17967 CONTEXT provides information to customize the location descriptions
17968 generation. Its context_type field specifies what type is implicitly
17969 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17970 will not be generated.
17972 Its DPI field determines whether we are generating a DWARF expression for a
17973 DWARF procedure, so PARM_DECL references are processed specifically.
17975 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17976 and dpi fields were null. */
17978 static dw_loc_list_ref
17979 loc_list_from_tree_1 (tree loc, int want_address,
17980 struct loc_descr_context *context)
17982 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17983 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17984 int have_address = 0;
17985 enum dwarf_location_atom op;
17987 /* ??? Most of the time we do not take proper care for sign/zero
17988 extending the values properly. Hopefully this won't be a real
17989 problem... */
17991 if (context != NULL
17992 && context->base_decl == loc
17993 && want_address == 0)
17995 if (dwarf_version >= 3 || !dwarf_strict)
17996 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17997 NULL, 0, NULL, 0, NULL);
17998 else
17999 return NULL;
18002 switch (TREE_CODE (loc))
18004 case ERROR_MARK:
18005 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
18006 return 0;
18008 case PLACEHOLDER_EXPR:
18009 /* This case involves extracting fields from an object to determine the
18010 position of other fields. It is supposed to appear only as the first
18011 operand of COMPONENT_REF nodes and to reference precisely the type
18012 that the context allows. */
18013 if (context != NULL
18014 && TREE_TYPE (loc) == context->context_type
18015 && want_address >= 1)
18017 if (dwarf_version >= 3 || !dwarf_strict)
18019 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
18020 have_address = 1;
18021 break;
18023 else
18024 return NULL;
18026 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
18027 the single argument passed by consumer. */
18028 else if (context != NULL
18029 && context->placeholder_arg
18030 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
18031 && want_address == 0)
18033 ret = new_loc_descr (DW_OP_pick, 0, 0);
18034 ret->frame_offset_rel = 1;
18035 context->placeholder_seen = true;
18036 break;
18038 else
18039 expansion_failed (loc, NULL_RTX,
18040 "PLACEHOLDER_EXPR for an unexpected type");
18041 break;
18043 case CALL_EXPR:
18045 const int nargs = call_expr_nargs (loc);
18046 tree callee = get_callee_fndecl (loc);
18047 int i;
18048 dw_die_ref dwarf_proc;
18050 if (callee == NULL_TREE)
18051 goto call_expansion_failed;
18053 /* We handle only functions that return an integer. */
18054 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
18055 goto call_expansion_failed;
18057 dwarf_proc = function_to_dwarf_procedure (callee);
18058 if (dwarf_proc == NULL)
18059 goto call_expansion_failed;
18061 /* Evaluate arguments right-to-left so that the first argument will
18062 be the top-most one on the stack. */
18063 for (i = nargs - 1; i >= 0; --i)
18065 dw_loc_descr_ref loc_descr
18066 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
18067 context);
18069 if (loc_descr == NULL)
18070 goto call_expansion_failed;
18072 add_loc_descr (&ret, loc_descr);
18075 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
18076 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18077 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
18078 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
18079 add_loc_descr (&ret, ret1);
18080 break;
18082 call_expansion_failed:
18083 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
18084 /* There are no opcodes for these operations. */
18085 return 0;
18088 case PREINCREMENT_EXPR:
18089 case PREDECREMENT_EXPR:
18090 case POSTINCREMENT_EXPR:
18091 case POSTDECREMENT_EXPR:
18092 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
18093 /* There are no opcodes for these operations. */
18094 return 0;
18096 case ADDR_EXPR:
18097 /* If we already want an address, see if there is INDIRECT_REF inside
18098 e.g. for &this->field. */
18099 if (want_address)
18101 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
18102 (loc, want_address == 2, context);
18103 if (list_ret)
18104 have_address = 1;
18105 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
18106 && (ret = cst_pool_loc_descr (loc)))
18107 have_address = 1;
18109 /* Otherwise, process the argument and look for the address. */
18110 if (!list_ret && !ret)
18111 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
18112 else
18114 if (want_address)
18115 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
18116 return NULL;
18118 break;
18120 case VAR_DECL:
18121 if (DECL_THREAD_LOCAL_P (loc))
18123 rtx rtl;
18124 enum dwarf_location_atom tls_op;
18125 enum dtprel_bool dtprel = dtprel_false;
18127 if (targetm.have_tls)
18129 /* If this is not defined, we have no way to emit the
18130 data. */
18131 if (!targetm.asm_out.output_dwarf_dtprel)
18132 return 0;
18134 /* The way DW_OP_GNU_push_tls_address is specified, we
18135 can only look up addresses of objects in the current
18136 module. We used DW_OP_addr as first op, but that's
18137 wrong, because DW_OP_addr is relocated by the debug
18138 info consumer, while DW_OP_GNU_push_tls_address
18139 operand shouldn't be. */
18140 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
18141 return 0;
18142 dtprel = dtprel_true;
18143 /* We check for DWARF 5 here because gdb did not implement
18144 DW_OP_form_tls_address until after 7.12. */
18145 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
18146 : DW_OP_GNU_push_tls_address);
18148 else
18150 if (!targetm.emutls.debug_form_tls_address
18151 || !(dwarf_version >= 3 || !dwarf_strict))
18152 return 0;
18153 /* We stuffed the control variable into the DECL_VALUE_EXPR
18154 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
18155 no longer appear in gimple code. We used the control
18156 variable in specific so that we could pick it up here. */
18157 loc = DECL_VALUE_EXPR (loc);
18158 tls_op = DW_OP_form_tls_address;
18161 rtl = rtl_for_decl_location (loc);
18162 if (rtl == NULL_RTX)
18163 return 0;
18165 if (!MEM_P (rtl))
18166 return 0;
18167 rtl = XEXP (rtl, 0);
18168 if (! CONSTANT_P (rtl))
18169 return 0;
18171 ret = new_addr_loc_descr (rtl, dtprel);
18172 ret1 = new_loc_descr (tls_op, 0, 0);
18173 add_loc_descr (&ret, ret1);
18175 have_address = 1;
18176 break;
18178 /* FALLTHRU */
18180 case PARM_DECL:
18181 if (context != NULL && context->dpi != NULL
18182 && DECL_CONTEXT (loc) == context->dpi->fndecl)
18184 /* We are generating code for a DWARF procedure and we want to access
18185 one of its arguments: find the appropriate argument offset and let
18186 the resolve_args_picking pass compute the offset that complies
18187 with the stack frame size. */
18188 unsigned i = 0;
18189 tree cursor;
18191 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
18192 cursor != NULL_TREE && cursor != loc;
18193 cursor = TREE_CHAIN (cursor), ++i)
18195 /* If we are translating a DWARF procedure, all referenced parameters
18196 must belong to the current function. */
18197 gcc_assert (cursor != NULL_TREE);
18199 ret = new_loc_descr (DW_OP_pick, i, 0);
18200 ret->frame_offset_rel = 1;
18201 break;
18203 /* FALLTHRU */
18205 case RESULT_DECL:
18206 if (DECL_HAS_VALUE_EXPR_P (loc))
18207 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
18208 want_address, context);
18209 /* FALLTHRU */
18211 case FUNCTION_DECL:
18213 rtx rtl;
18214 var_loc_list *loc_list = lookup_decl_loc (loc);
18216 if (loc_list && loc_list->first)
18218 list_ret = dw_loc_list (loc_list, loc, want_address);
18219 have_address = want_address != 0;
18220 break;
18222 rtl = rtl_for_decl_location (loc);
18223 if (rtl == NULL_RTX)
18225 if (TREE_CODE (loc) != FUNCTION_DECL
18226 && early_dwarf
18227 && current_function_decl
18228 && want_address != 1
18229 && ! DECL_IGNORED_P (loc)
18230 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
18231 || POINTER_TYPE_P (TREE_TYPE (loc)))
18232 && DECL_CONTEXT (loc) == current_function_decl
18233 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
18234 <= DWARF2_ADDR_SIZE))
18236 dw_die_ref ref = lookup_decl_die (loc);
18237 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
18238 if (ref)
18240 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
18241 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
18242 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
18244 else
18246 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
18247 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
18249 break;
18251 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
18252 return 0;
18254 else if (CONST_INT_P (rtl))
18256 HOST_WIDE_INT val = INTVAL (rtl);
18257 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18258 val &= GET_MODE_MASK (DECL_MODE (loc));
18259 ret = int_loc_descriptor (val);
18261 else if (GET_CODE (rtl) == CONST_STRING)
18263 expansion_failed (loc, NULL_RTX, "CONST_STRING");
18264 return 0;
18266 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
18267 ret = new_addr_loc_descr (rtl, dtprel_false);
18268 else
18270 machine_mode mode, mem_mode;
18272 /* Certain constructs can only be represented at top-level. */
18273 if (want_address == 2)
18275 ret = loc_descriptor (rtl, VOIDmode,
18276 VAR_INIT_STATUS_INITIALIZED);
18277 have_address = 1;
18279 else
18281 mode = GET_MODE (rtl);
18282 mem_mode = VOIDmode;
18283 if (MEM_P (rtl))
18285 mem_mode = mode;
18286 mode = get_address_mode (rtl);
18287 rtl = XEXP (rtl, 0);
18288 have_address = 1;
18290 ret = mem_loc_descriptor (rtl, mode, mem_mode,
18291 VAR_INIT_STATUS_INITIALIZED);
18293 if (!ret)
18294 expansion_failed (loc, rtl,
18295 "failed to produce loc descriptor for rtl");
18298 break;
18300 case MEM_REF:
18301 if (!integer_zerop (TREE_OPERAND (loc, 1)))
18303 have_address = 1;
18304 goto do_plus;
18306 /* Fallthru. */
18307 case INDIRECT_REF:
18308 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18309 have_address = 1;
18310 break;
18312 case TARGET_MEM_REF:
18313 case SSA_NAME:
18314 case DEBUG_EXPR_DECL:
18315 return NULL;
18317 case COMPOUND_EXPR:
18318 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
18319 context);
18321 CASE_CONVERT:
18322 case VIEW_CONVERT_EXPR:
18323 case SAVE_EXPR:
18324 case MODIFY_EXPR:
18325 case NON_LVALUE_EXPR:
18326 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
18327 context);
18329 case COMPONENT_REF:
18330 case BIT_FIELD_REF:
18331 case ARRAY_REF:
18332 case ARRAY_RANGE_REF:
18333 case REALPART_EXPR:
18334 case IMAGPART_EXPR:
18336 tree obj, offset;
18337 poly_int64 bitsize, bitpos, bytepos;
18338 machine_mode mode;
18339 int unsignedp, reversep, volatilep = 0;
18341 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
18342 &unsignedp, &reversep, &volatilep);
18344 gcc_assert (obj != loc);
18346 list_ret = loc_list_from_tree_1 (obj,
18347 want_address == 2
18348 && known_eq (bitpos, 0)
18349 && !offset ? 2 : 1,
18350 context);
18351 /* TODO: We can extract value of the small expression via shifting even
18352 for nonzero bitpos. */
18353 if (list_ret == 0)
18354 return 0;
18355 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
18356 || !multiple_p (bitsize, BITS_PER_UNIT))
18358 expansion_failed (loc, NULL_RTX,
18359 "bitfield access");
18360 return 0;
18363 if (offset != NULL_TREE)
18365 /* Variable offset. */
18366 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
18367 if (list_ret1 == 0)
18368 return 0;
18369 add_loc_list (&list_ret, list_ret1);
18370 if (!list_ret)
18371 return 0;
18372 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
18375 HOST_WIDE_INT value;
18376 if (bytepos.is_constant (&value) && value > 0)
18377 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
18378 value, 0));
18379 else if (maybe_ne (bytepos, 0))
18380 loc_list_plus_const (list_ret, bytepos);
18382 have_address = 1;
18383 break;
18386 case INTEGER_CST:
18387 if ((want_address || !tree_fits_shwi_p (loc))
18388 && (ret = cst_pool_loc_descr (loc)))
18389 have_address = 1;
18390 else if (want_address == 2
18391 && tree_fits_shwi_p (loc)
18392 && (ret = address_of_int_loc_descriptor
18393 (int_size_in_bytes (TREE_TYPE (loc)),
18394 tree_to_shwi (loc))))
18395 have_address = 1;
18396 else if (tree_fits_shwi_p (loc))
18397 ret = int_loc_descriptor (tree_to_shwi (loc));
18398 else if (tree_fits_uhwi_p (loc))
18399 ret = uint_loc_descriptor (tree_to_uhwi (loc));
18400 else
18402 expansion_failed (loc, NULL_RTX,
18403 "Integer operand is not host integer");
18404 return 0;
18406 break;
18408 case CONSTRUCTOR:
18409 case REAL_CST:
18410 case STRING_CST:
18411 case COMPLEX_CST:
18412 if ((ret = cst_pool_loc_descr (loc)))
18413 have_address = 1;
18414 else if (TREE_CODE (loc) == CONSTRUCTOR)
18416 tree type = TREE_TYPE (loc);
18417 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
18418 unsigned HOST_WIDE_INT offset = 0;
18419 unsigned HOST_WIDE_INT cnt;
18420 constructor_elt *ce;
18422 if (TREE_CODE (type) == RECORD_TYPE)
18424 /* This is very limited, but it's enough to output
18425 pointers to member functions, as long as the
18426 referenced function is defined in the current
18427 translation unit. */
18428 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
18430 tree val = ce->value;
18432 tree field = ce->index;
18434 if (val)
18435 STRIP_NOPS (val);
18437 if (!field || DECL_BIT_FIELD (field))
18439 expansion_failed (loc, NULL_RTX,
18440 "bitfield in record type constructor");
18441 size = offset = (unsigned HOST_WIDE_INT)-1;
18442 ret = NULL;
18443 break;
18446 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
18447 unsigned HOST_WIDE_INT pos = int_byte_position (field);
18448 gcc_assert (pos + fieldsize <= size);
18449 if (pos < offset)
18451 expansion_failed (loc, NULL_RTX,
18452 "out-of-order fields in record constructor");
18453 size = offset = (unsigned HOST_WIDE_INT)-1;
18454 ret = NULL;
18455 break;
18457 if (pos > offset)
18459 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
18460 add_loc_descr (&ret, ret1);
18461 offset = pos;
18463 if (val && fieldsize != 0)
18465 ret1 = loc_descriptor_from_tree (val, want_address, context);
18466 if (!ret1)
18468 expansion_failed (loc, NULL_RTX,
18469 "unsupported expression in field");
18470 size = offset = (unsigned HOST_WIDE_INT)-1;
18471 ret = NULL;
18472 break;
18474 add_loc_descr (&ret, ret1);
18476 if (fieldsize)
18478 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
18479 add_loc_descr (&ret, ret1);
18480 offset = pos + fieldsize;
18484 if (offset != size)
18486 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
18487 add_loc_descr (&ret, ret1);
18488 offset = size;
18491 have_address = !!want_address;
18493 else
18494 expansion_failed (loc, NULL_RTX,
18495 "constructor of non-record type");
18497 else
18498 /* We can construct small constants here using int_loc_descriptor. */
18499 expansion_failed (loc, NULL_RTX,
18500 "constructor or constant not in constant pool");
18501 break;
18503 case TRUTH_AND_EXPR:
18504 case TRUTH_ANDIF_EXPR:
18505 case BIT_AND_EXPR:
18506 op = DW_OP_and;
18507 goto do_binop;
18509 case TRUTH_XOR_EXPR:
18510 case BIT_XOR_EXPR:
18511 op = DW_OP_xor;
18512 goto do_binop;
18514 case TRUTH_OR_EXPR:
18515 case TRUTH_ORIF_EXPR:
18516 case BIT_IOR_EXPR:
18517 op = DW_OP_or;
18518 goto do_binop;
18520 case FLOOR_DIV_EXPR:
18521 case CEIL_DIV_EXPR:
18522 case ROUND_DIV_EXPR:
18523 case TRUNC_DIV_EXPR:
18524 case EXACT_DIV_EXPR:
18525 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18526 return 0;
18527 op = DW_OP_div;
18528 goto do_binop;
18530 case MINUS_EXPR:
18531 op = DW_OP_minus;
18532 goto do_binop;
18534 case FLOOR_MOD_EXPR:
18535 case CEIL_MOD_EXPR:
18536 case ROUND_MOD_EXPR:
18537 case TRUNC_MOD_EXPR:
18538 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
18540 op = DW_OP_mod;
18541 goto do_binop;
18543 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18544 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18545 if (list_ret == 0 || list_ret1 == 0)
18546 return 0;
18548 add_loc_list (&list_ret, list_ret1);
18549 if (list_ret == 0)
18550 return 0;
18551 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18552 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
18553 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
18554 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
18555 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
18556 break;
18558 case MULT_EXPR:
18559 op = DW_OP_mul;
18560 goto do_binop;
18562 case LSHIFT_EXPR:
18563 op = DW_OP_shl;
18564 goto do_binop;
18566 case RSHIFT_EXPR:
18567 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
18568 goto do_binop;
18570 case POINTER_PLUS_EXPR:
18571 case PLUS_EXPR:
18572 do_plus:
18573 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
18575 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
18576 smarter to encode their opposite. The DW_OP_plus_uconst operation
18577 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
18578 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
18579 bytes, Y being the size of the operation that pushes the opposite
18580 of the addend. So let's choose the smallest representation. */
18581 const tree tree_addend = TREE_OPERAND (loc, 1);
18582 offset_int wi_addend;
18583 HOST_WIDE_INT shwi_addend;
18584 dw_loc_descr_ref loc_naddend;
18586 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18587 if (list_ret == 0)
18588 return 0;
18590 /* Try to get the literal to push. It is the opposite of the addend,
18591 so as we rely on wrapping during DWARF evaluation, first decode
18592 the literal as a "DWARF-sized" signed number. */
18593 wi_addend = wi::to_offset (tree_addend);
18594 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18595 shwi_addend = wi_addend.to_shwi ();
18596 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18597 ? int_loc_descriptor (-shwi_addend)
18598 : NULL;
18600 if (loc_naddend != NULL
18601 && ((unsigned) size_of_uleb128 (shwi_addend)
18602 > size_of_loc_descr (loc_naddend)))
18604 add_loc_descr_to_each (list_ret, loc_naddend);
18605 add_loc_descr_to_each (list_ret,
18606 new_loc_descr (DW_OP_minus, 0, 0));
18608 else
18610 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18612 loc_naddend = loc_cur;
18613 loc_cur = loc_cur->dw_loc_next;
18614 ggc_free (loc_naddend);
18616 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18618 break;
18621 op = DW_OP_plus;
18622 goto do_binop;
18624 case LE_EXPR:
18625 op = DW_OP_le;
18626 goto do_comp_binop;
18628 case GE_EXPR:
18629 op = DW_OP_ge;
18630 goto do_comp_binop;
18632 case LT_EXPR:
18633 op = DW_OP_lt;
18634 goto do_comp_binop;
18636 case GT_EXPR:
18637 op = DW_OP_gt;
18638 goto do_comp_binop;
18640 do_comp_binop:
18641 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18643 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18644 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18645 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18646 TREE_CODE (loc));
18647 break;
18649 else
18650 goto do_binop;
18652 case EQ_EXPR:
18653 op = DW_OP_eq;
18654 goto do_binop;
18656 case NE_EXPR:
18657 op = DW_OP_ne;
18658 goto do_binop;
18660 do_binop:
18661 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18662 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18663 if (list_ret == 0 || list_ret1 == 0)
18664 return 0;
18666 add_loc_list (&list_ret, list_ret1);
18667 if (list_ret == 0)
18668 return 0;
18669 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18670 break;
18672 case TRUTH_NOT_EXPR:
18673 case BIT_NOT_EXPR:
18674 op = DW_OP_not;
18675 goto do_unop;
18677 case ABS_EXPR:
18678 op = DW_OP_abs;
18679 goto do_unop;
18681 case NEGATE_EXPR:
18682 op = DW_OP_neg;
18683 goto do_unop;
18685 do_unop:
18686 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18687 if (list_ret == 0)
18688 return 0;
18690 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18691 break;
18693 case MIN_EXPR:
18694 case MAX_EXPR:
18696 const enum tree_code code =
18697 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18699 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18700 build2 (code, integer_type_node,
18701 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18702 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18705 /* fall through */
18707 case COND_EXPR:
18709 dw_loc_descr_ref lhs
18710 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18711 dw_loc_list_ref rhs
18712 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18713 dw_loc_descr_ref bra_node, jump_node, tmp;
18715 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18716 if (list_ret == 0 || lhs == 0 || rhs == 0)
18717 return 0;
18719 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18720 add_loc_descr_to_each (list_ret, bra_node);
18722 add_loc_list (&list_ret, rhs);
18723 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18724 add_loc_descr_to_each (list_ret, jump_node);
18726 add_loc_descr_to_each (list_ret, lhs);
18727 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18728 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18730 /* ??? Need a node to point the skip at. Use a nop. */
18731 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18732 add_loc_descr_to_each (list_ret, tmp);
18733 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18734 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18736 break;
18738 case FIX_TRUNC_EXPR:
18739 return 0;
18741 default:
18742 /* Leave front-end specific codes as simply unknown. This comes
18743 up, for instance, with the C STMT_EXPR. */
18744 if ((unsigned int) TREE_CODE (loc)
18745 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18747 expansion_failed (loc, NULL_RTX,
18748 "language specific tree node");
18749 return 0;
18752 /* Otherwise this is a generic code; we should just lists all of
18753 these explicitly. We forgot one. */
18754 if (flag_checking)
18755 gcc_unreachable ();
18757 /* In a release build, we want to degrade gracefully: better to
18758 generate incomplete debugging information than to crash. */
18759 return NULL;
18762 if (!ret && !list_ret)
18763 return 0;
18765 if (want_address == 2 && !have_address
18766 && (dwarf_version >= 4 || !dwarf_strict))
18768 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18770 expansion_failed (loc, NULL_RTX,
18771 "DWARF address size mismatch");
18772 return 0;
18774 if (ret)
18775 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18776 else
18777 add_loc_descr_to_each (list_ret,
18778 new_loc_descr (DW_OP_stack_value, 0, 0));
18779 have_address = 1;
18781 /* Show if we can't fill the request for an address. */
18782 if (want_address && !have_address)
18784 expansion_failed (loc, NULL_RTX,
18785 "Want address and only have value");
18786 return 0;
18789 gcc_assert (!ret || !list_ret);
18791 /* If we've got an address and don't want one, dereference. */
18792 if (!want_address && have_address)
18794 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18796 if (size > DWARF2_ADDR_SIZE || size == -1)
18798 expansion_failed (loc, NULL_RTX,
18799 "DWARF address size mismatch");
18800 return 0;
18802 else if (size == DWARF2_ADDR_SIZE)
18803 op = DW_OP_deref;
18804 else
18805 op = DW_OP_deref_size;
18807 if (ret)
18808 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18809 else
18810 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18812 if (ret)
18813 list_ret = new_loc_list (ret, NULL, 0, NULL, 0, NULL);
18815 return list_ret;
18818 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18819 expressions. */
18821 static dw_loc_list_ref
18822 loc_list_from_tree (tree loc, int want_address,
18823 struct loc_descr_context *context)
18825 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18827 for (dw_loc_list_ref loc_cur = result;
18828 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18829 loc_descr_without_nops (loc_cur->expr);
18830 return result;
18833 /* Same as above but return only single location expression. */
18834 static dw_loc_descr_ref
18835 loc_descriptor_from_tree (tree loc, int want_address,
18836 struct loc_descr_context *context)
18838 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18839 if (!ret)
18840 return NULL;
18841 if (ret->dw_loc_next)
18843 expansion_failed (loc, NULL_RTX,
18844 "Location list where only loc descriptor needed");
18845 return NULL;
18847 return ret->expr;
18850 /* Given a value, round it up to the lowest multiple of `boundary'
18851 which is not less than the value itself. */
18853 static inline HOST_WIDE_INT
18854 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18856 return (((value + boundary - 1) / boundary) * boundary);
18859 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18860 pointer to the declared type for the relevant field variable, or return
18861 `integer_type_node' if the given node turns out to be an
18862 ERROR_MARK node. */
18864 static inline tree
18865 field_type (const_tree decl)
18867 tree type;
18869 if (TREE_CODE (decl) == ERROR_MARK)
18870 return integer_type_node;
18872 type = DECL_BIT_FIELD_TYPE (decl);
18873 if (type == NULL_TREE)
18874 type = TREE_TYPE (decl);
18876 return type;
18879 /* Given a pointer to a tree node, return the alignment in bits for
18880 it, or else return BITS_PER_WORD if the node actually turns out to
18881 be an ERROR_MARK node. */
18883 static inline unsigned
18884 simple_type_align_in_bits (const_tree type)
18886 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18889 static inline unsigned
18890 simple_decl_align_in_bits (const_tree decl)
18892 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18895 /* Return the result of rounding T up to ALIGN. */
18897 static inline offset_int
18898 round_up_to_align (const offset_int &t, unsigned int align)
18900 return wi::udiv_trunc (t + align - 1, align) * align;
18903 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18904 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18905 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18906 if we fail to return the size in one of these two forms. */
18908 static dw_loc_descr_ref
18909 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18911 tree tree_size;
18912 struct loc_descr_context ctx;
18914 /* Return a constant integer in priority, if possible. */
18915 *cst_size = int_size_in_bytes (type);
18916 if (*cst_size != -1)
18917 return NULL;
18919 ctx.context_type = const_cast<tree> (type);
18920 ctx.base_decl = NULL_TREE;
18921 ctx.dpi = NULL;
18922 ctx.placeholder_arg = false;
18923 ctx.placeholder_seen = false;
18925 type = TYPE_MAIN_VARIANT (type);
18926 tree_size = TYPE_SIZE_UNIT (type);
18927 return ((tree_size != NULL_TREE)
18928 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18929 : NULL);
18932 /* Helper structure for RECORD_TYPE processing. */
18933 struct vlr_context
18935 /* Root RECORD_TYPE. It is needed to generate data member location
18936 descriptions in variable-length records (VLR), but also to cope with
18937 variants, which are composed of nested structures multiplexed with
18938 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18939 function processing a FIELD_DECL, it is required to be non null. */
18940 tree struct_type;
18941 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18942 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18943 this variant part as part of the root record (in storage units). For
18944 regular records, it must be NULL_TREE. */
18945 tree variant_part_offset;
18948 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18949 addressed byte of the "containing object" for the given FIELD_DECL. If
18950 possible, return a native constant through CST_OFFSET (in which case NULL is
18951 returned); otherwise return a DWARF expression that computes the offset.
18953 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18954 that offset is, either because the argument turns out to be a pointer to an
18955 ERROR_MARK node, or because the offset expression is too complex for us.
18957 CTX is required: see the comment for VLR_CONTEXT. */
18959 static dw_loc_descr_ref
18960 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18961 HOST_WIDE_INT *cst_offset)
18963 tree tree_result;
18964 dw_loc_list_ref loc_result;
18966 *cst_offset = 0;
18968 if (TREE_CODE (decl) == ERROR_MARK)
18969 return NULL;
18970 else
18971 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18973 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18974 case. */
18975 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18976 return NULL;
18978 #ifdef PCC_BITFIELD_TYPE_MATTERS
18979 /* We used to handle only constant offsets in all cases. Now, we handle
18980 properly dynamic byte offsets only when PCC bitfield type doesn't
18981 matter. */
18982 if (PCC_BITFIELD_TYPE_MATTERS
18983 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18985 offset_int object_offset_in_bits;
18986 offset_int object_offset_in_bytes;
18987 offset_int bitpos_int;
18988 tree type;
18989 tree field_size_tree;
18990 offset_int deepest_bitpos;
18991 offset_int field_size_in_bits;
18992 unsigned int type_align_in_bits;
18993 unsigned int decl_align_in_bits;
18994 offset_int type_size_in_bits;
18996 bitpos_int = wi::to_offset (bit_position (decl));
18997 type = field_type (decl);
18998 type_size_in_bits = offset_int_type_size_in_bits (type);
18999 type_align_in_bits = simple_type_align_in_bits (type);
19001 field_size_tree = DECL_SIZE (decl);
19003 /* The size could be unspecified if there was an error, or for
19004 a flexible array member. */
19005 if (!field_size_tree)
19006 field_size_tree = bitsize_zero_node;
19008 /* If the size of the field is not constant, use the type size. */
19009 if (TREE_CODE (field_size_tree) == INTEGER_CST)
19010 field_size_in_bits = wi::to_offset (field_size_tree);
19011 else
19012 field_size_in_bits = type_size_in_bits;
19014 decl_align_in_bits = simple_decl_align_in_bits (decl);
19016 /* The GCC front-end doesn't make any attempt to keep track of the
19017 starting bit offset (relative to the start of the containing
19018 structure type) of the hypothetical "containing object" for a
19019 bit-field. Thus, when computing the byte offset value for the
19020 start of the "containing object" of a bit-field, we must deduce
19021 this information on our own. This can be rather tricky to do in
19022 some cases. For example, handling the following structure type
19023 definition when compiling for an i386/i486 target (which only
19024 aligns long long's to 32-bit boundaries) can be very tricky:
19026 struct S { int field1; long long field2:31; };
19028 Fortunately, there is a simple rule-of-thumb which can be used
19029 in such cases. When compiling for an i386/i486, GCC will
19030 allocate 8 bytes for the structure shown above. It decides to
19031 do this based upon one simple rule for bit-field allocation.
19032 GCC allocates each "containing object" for each bit-field at
19033 the first (i.e. lowest addressed) legitimate alignment boundary
19034 (based upon the required minimum alignment for the declared
19035 type of the field) which it can possibly use, subject to the
19036 condition that there is still enough available space remaining
19037 in the containing object (when allocated at the selected point)
19038 to fully accommodate all of the bits of the bit-field itself.
19040 This simple rule makes it obvious why GCC allocates 8 bytes for
19041 each object of the structure type shown above. When looking
19042 for a place to allocate the "containing object" for `field2',
19043 the compiler simply tries to allocate a 64-bit "containing
19044 object" at each successive 32-bit boundary (starting at zero)
19045 until it finds a place to allocate that 64- bit field such that
19046 at least 31 contiguous (and previously unallocated) bits remain
19047 within that selected 64 bit field. (As it turns out, for the
19048 example above, the compiler finds it is OK to allocate the
19049 "containing object" 64-bit field at bit-offset zero within the
19050 structure type.)
19052 Here we attempt to work backwards from the limited set of facts
19053 we're given, and we try to deduce from those facts, where GCC
19054 must have believed that the containing object started (within
19055 the structure type). The value we deduce is then used (by the
19056 callers of this routine) to generate DW_AT_location and
19057 DW_AT_bit_offset attributes for fields (both bit-fields and, in
19058 the case of DW_AT_location, regular fields as well). */
19060 /* Figure out the bit-distance from the start of the structure to
19061 the "deepest" bit of the bit-field. */
19062 deepest_bitpos = bitpos_int + field_size_in_bits;
19064 /* This is the tricky part. Use some fancy footwork to deduce
19065 where the lowest addressed bit of the containing object must
19066 be. */
19067 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19069 /* Round up to type_align by default. This works best for
19070 bitfields. */
19071 object_offset_in_bits
19072 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
19074 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
19076 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
19078 /* Round up to decl_align instead. */
19079 object_offset_in_bits
19080 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
19083 object_offset_in_bytes
19084 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
19085 if (ctx->variant_part_offset == NULL_TREE)
19087 *cst_offset = object_offset_in_bytes.to_shwi ();
19088 return NULL;
19090 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
19092 else
19093 #endif /* PCC_BITFIELD_TYPE_MATTERS */
19094 tree_result = byte_position (decl);
19096 if (ctx->variant_part_offset != NULL_TREE)
19097 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
19098 ctx->variant_part_offset, tree_result);
19100 /* If the byte offset is a constant, it's simplier to handle a native
19101 constant rather than a DWARF expression. */
19102 if (TREE_CODE (tree_result) == INTEGER_CST)
19104 *cst_offset = wi::to_offset (tree_result).to_shwi ();
19105 return NULL;
19107 struct loc_descr_context loc_ctx = {
19108 ctx->struct_type, /* context_type */
19109 NULL_TREE, /* base_decl */
19110 NULL, /* dpi */
19111 false, /* placeholder_arg */
19112 false /* placeholder_seen */
19114 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
19116 /* We want a DWARF expression: abort if we only have a location list with
19117 multiple elements. */
19118 if (!loc_result || !single_element_loc_list_p (loc_result))
19119 return NULL;
19120 else
19121 return loc_result->expr;
19124 /* The following routines define various Dwarf attributes and any data
19125 associated with them. */
19127 /* Add a location description attribute value to a DIE.
19129 This emits location attributes suitable for whole variables and
19130 whole parameters. Note that the location attributes for struct fields are
19131 generated by the routine `data_member_location_attribute' below. */
19133 static inline void
19134 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
19135 dw_loc_list_ref descr)
19137 bool check_no_locviews = true;
19138 if (descr == 0)
19139 return;
19140 if (single_element_loc_list_p (descr))
19141 add_AT_loc (die, attr_kind, descr->expr);
19142 else
19144 add_AT_loc_list (die, attr_kind, descr);
19145 gcc_assert (descr->ll_symbol);
19146 if (attr_kind == DW_AT_location && descr->vl_symbol
19147 && dwarf2out_locviews_in_attribute ())
19149 add_AT_view_list (die, DW_AT_GNU_locviews);
19150 check_no_locviews = false;
19154 if (check_no_locviews)
19155 gcc_assert (!get_AT (die, DW_AT_GNU_locviews));
19158 /* Add DW_AT_accessibility attribute to DIE if needed. */
19160 static void
19161 add_accessibility_attribute (dw_die_ref die, tree decl)
19163 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19164 children, otherwise the default is DW_ACCESS_public. In DWARF2
19165 the default has always been DW_ACCESS_public. */
19166 if (TREE_PROTECTED (decl))
19167 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19168 else if (TREE_PRIVATE (decl))
19170 if (dwarf_version == 2
19171 || die->die_parent == NULL
19172 || die->die_parent->die_tag != DW_TAG_class_type)
19173 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19175 else if (dwarf_version > 2
19176 && die->die_parent
19177 && die->die_parent->die_tag == DW_TAG_class_type)
19178 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19181 /* Attach the specialized form of location attribute used for data members of
19182 struct and union types. In the special case of a FIELD_DECL node which
19183 represents a bit-field, the "offset" part of this special location
19184 descriptor must indicate the distance in bytes from the lowest-addressed
19185 byte of the containing struct or union type to the lowest-addressed byte of
19186 the "containing object" for the bit-field. (See the `field_byte_offset'
19187 function above).
19189 For any given bit-field, the "containing object" is a hypothetical object
19190 (of some integral or enum type) within which the given bit-field lives. The
19191 type of this hypothetical "containing object" is always the same as the
19192 declared type of the individual bit-field itself (for GCC anyway... the
19193 DWARF spec doesn't actually mandate this). Note that it is the size (in
19194 bytes) of the hypothetical "containing object" which will be given in the
19195 DW_AT_byte_size attribute for this bit-field. (See the
19196 `byte_size_attribute' function below.) It is also used when calculating the
19197 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
19198 function below.)
19200 CTX is required: see the comment for VLR_CONTEXT. */
19202 static void
19203 add_data_member_location_attribute (dw_die_ref die,
19204 tree decl,
19205 struct vlr_context *ctx)
19207 HOST_WIDE_INT offset;
19208 dw_loc_descr_ref loc_descr = 0;
19210 if (TREE_CODE (decl) == TREE_BINFO)
19212 /* We're working on the TAG_inheritance for a base class. */
19213 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
19215 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
19216 aren't at a fixed offset from all (sub)objects of the same
19217 type. We need to extract the appropriate offset from our
19218 vtable. The following dwarf expression means
19220 BaseAddr = ObAddr + *((*ObAddr) - Offset)
19222 This is specific to the V3 ABI, of course. */
19224 dw_loc_descr_ref tmp;
19226 /* Make a copy of the object address. */
19227 tmp = new_loc_descr (DW_OP_dup, 0, 0);
19228 add_loc_descr (&loc_descr, tmp);
19230 /* Extract the vtable address. */
19231 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19232 add_loc_descr (&loc_descr, tmp);
19234 /* Calculate the address of the offset. */
19235 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
19236 gcc_assert (offset < 0);
19238 tmp = int_loc_descriptor (-offset);
19239 add_loc_descr (&loc_descr, tmp);
19240 tmp = new_loc_descr (DW_OP_minus, 0, 0);
19241 add_loc_descr (&loc_descr, tmp);
19243 /* Extract the offset. */
19244 tmp = new_loc_descr (DW_OP_deref, 0, 0);
19245 add_loc_descr (&loc_descr, tmp);
19247 /* Add it to the object address. */
19248 tmp = new_loc_descr (DW_OP_plus, 0, 0);
19249 add_loc_descr (&loc_descr, tmp);
19251 else
19252 offset = tree_to_shwi (BINFO_OFFSET (decl));
19254 else
19256 loc_descr = field_byte_offset (decl, ctx, &offset);
19258 /* If loc_descr is available then we know the field offset is dynamic.
19259 However, GDB does not handle dynamic field offsets very well at the
19260 moment. */
19261 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
19263 loc_descr = NULL;
19264 offset = 0;
19267 /* Data member location evalutation starts with the base address on the
19268 stack. Compute the field offset and add it to this base address. */
19269 else if (loc_descr != NULL)
19270 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
19273 if (! loc_descr)
19275 /* While DW_AT_data_bit_offset has been added already in DWARF4,
19276 e.g. GDB only added support to it in November 2016. For DWARF5
19277 we need newer debug info consumers anyway. We might change this
19278 to dwarf_version >= 4 once most consumers catched up. */
19279 if (dwarf_version >= 5
19280 && TREE_CODE (decl) == FIELD_DECL
19281 && DECL_BIT_FIELD_TYPE (decl))
19283 tree off = bit_position (decl);
19284 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
19286 remove_AT (die, DW_AT_byte_size);
19287 remove_AT (die, DW_AT_bit_offset);
19288 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
19289 return;
19292 if (dwarf_version > 2)
19294 /* Don't need to output a location expression, just the constant. */
19295 if (offset < 0)
19296 add_AT_int (die, DW_AT_data_member_location, offset);
19297 else
19298 add_AT_unsigned (die, DW_AT_data_member_location, offset);
19299 return;
19301 else
19303 enum dwarf_location_atom op;
19305 /* The DWARF2 standard says that we should assume that the structure
19306 address is already on the stack, so we can specify a structure
19307 field address by using DW_OP_plus_uconst. */
19308 op = DW_OP_plus_uconst;
19309 loc_descr = new_loc_descr (op, offset, 0);
19313 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
19316 /* Writes integer values to dw_vec_const array. */
19318 static void
19319 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
19321 while (size != 0)
19323 *dest++ = val & 0xff;
19324 val >>= 8;
19325 --size;
19329 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
19331 static HOST_WIDE_INT
19332 extract_int (const unsigned char *src, unsigned int size)
19334 HOST_WIDE_INT val = 0;
19336 src += size;
19337 while (size != 0)
19339 val <<= 8;
19340 val |= *--src & 0xff;
19341 --size;
19343 return val;
19346 /* Writes wide_int values to dw_vec_const array. */
19348 static void
19349 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
19351 int i;
19353 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
19355 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
19356 return;
19359 /* We'd have to extend this code to support odd sizes. */
19360 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
19362 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
19364 if (WORDS_BIG_ENDIAN)
19365 for (i = n - 1; i >= 0; i--)
19367 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19368 dest += sizeof (HOST_WIDE_INT);
19370 else
19371 for (i = 0; i < n; i++)
19373 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
19374 dest += sizeof (HOST_WIDE_INT);
19378 /* Writes floating point values to dw_vec_const array. */
19380 static void
19381 insert_float (const_rtx rtl, unsigned char *array)
19383 long val[4];
19384 int i;
19385 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19387 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
19389 /* real_to_target puts 32-bit pieces in each long. Pack them. */
19390 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
19392 insert_int (val[i], 4, array);
19393 array += 4;
19397 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
19398 does not have a "location" either in memory or in a register. These
19399 things can arise in GNU C when a constant is passed as an actual parameter
19400 to an inlined function. They can also arise in C++ where declared
19401 constants do not necessarily get memory "homes". */
19403 static bool
19404 add_const_value_attribute (dw_die_ref die, rtx rtl)
19406 switch (GET_CODE (rtl))
19408 case CONST_INT:
19410 HOST_WIDE_INT val = INTVAL (rtl);
19412 if (val < 0)
19413 add_AT_int (die, DW_AT_const_value, val);
19414 else
19415 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
19417 return true;
19419 case CONST_WIDE_INT:
19421 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
19422 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
19423 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
19424 wide_int w = wi::zext (w1, prec);
19425 add_AT_wide (die, DW_AT_const_value, w);
19427 return true;
19429 case CONST_DOUBLE:
19430 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
19431 floating-point constant. A CONST_DOUBLE is used whenever the
19432 constant requires more than one word in order to be adequately
19433 represented. */
19434 if (TARGET_SUPPORTS_WIDE_INT == 0
19435 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
19436 add_AT_double (die, DW_AT_const_value,
19437 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
19438 else
19440 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
19441 unsigned int length = GET_MODE_SIZE (mode);
19442 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
19444 insert_float (rtl, array);
19445 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
19447 return true;
19449 case CONST_VECTOR:
19451 unsigned int length;
19452 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
19453 return false;
19455 machine_mode mode = GET_MODE (rtl);
19456 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
19457 unsigned char *array
19458 = ggc_vec_alloc<unsigned char> (length * elt_size);
19459 unsigned int i;
19460 unsigned char *p;
19461 machine_mode imode = GET_MODE_INNER (mode);
19463 switch (GET_MODE_CLASS (mode))
19465 case MODE_VECTOR_INT:
19466 for (i = 0, p = array; i < length; i++, p += elt_size)
19468 rtx elt = CONST_VECTOR_ELT (rtl, i);
19469 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
19471 break;
19473 case MODE_VECTOR_FLOAT:
19474 for (i = 0, p = array; i < length; i++, p += elt_size)
19476 rtx elt = CONST_VECTOR_ELT (rtl, i);
19477 insert_float (elt, p);
19479 break;
19481 default:
19482 gcc_unreachable ();
19485 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
19487 return true;
19489 case CONST_STRING:
19490 if (dwarf_version >= 4 || !dwarf_strict)
19492 dw_loc_descr_ref loc_result;
19493 resolve_one_addr (&rtl);
19494 rtl_addr:
19495 loc_result = new_addr_loc_descr (rtl, dtprel_false);
19496 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
19497 add_AT_loc (die, DW_AT_location, loc_result);
19498 vec_safe_push (used_rtx_array, rtl);
19499 return true;
19501 return false;
19503 case CONST:
19504 if (CONSTANT_P (XEXP (rtl, 0)))
19505 return add_const_value_attribute (die, XEXP (rtl, 0));
19506 /* FALLTHROUGH */
19507 case SYMBOL_REF:
19508 if (!const_ok_for_output (rtl))
19509 return false;
19510 /* FALLTHROUGH */
19511 case LABEL_REF:
19512 if (dwarf_version >= 4 || !dwarf_strict)
19513 goto rtl_addr;
19514 return false;
19516 case PLUS:
19517 /* In cases where an inlined instance of an inline function is passed
19518 the address of an `auto' variable (which is local to the caller) we
19519 can get a situation where the DECL_RTL of the artificial local
19520 variable (for the inlining) which acts as a stand-in for the
19521 corresponding formal parameter (of the inline function) will look
19522 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
19523 exactly a compile-time constant expression, but it isn't the address
19524 of the (artificial) local variable either. Rather, it represents the
19525 *value* which the artificial local variable always has during its
19526 lifetime. We currently have no way to represent such quasi-constant
19527 values in Dwarf, so for now we just punt and generate nothing. */
19528 return false;
19530 case HIGH:
19531 case CONST_FIXED:
19532 return false;
19534 case MEM:
19535 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
19536 && MEM_READONLY_P (rtl)
19537 && GET_MODE (rtl) == BLKmode)
19539 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
19540 return true;
19542 return false;
19544 default:
19545 /* No other kinds of rtx should be possible here. */
19546 gcc_unreachable ();
19548 return false;
19551 /* Determine whether the evaluation of EXPR references any variables
19552 or functions which aren't otherwise used (and therefore may not be
19553 output). */
19554 static tree
19555 reference_to_unused (tree * tp, int * walk_subtrees,
19556 void * data ATTRIBUTE_UNUSED)
19558 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
19559 *walk_subtrees = 0;
19561 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
19562 && ! TREE_ASM_WRITTEN (*tp))
19563 return *tp;
19564 /* ??? The C++ FE emits debug information for using decls, so
19565 putting gcc_unreachable here falls over. See PR31899. For now
19566 be conservative. */
19567 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
19568 return *tp;
19569 else if (VAR_P (*tp))
19571 varpool_node *node = varpool_node::get (*tp);
19572 if (!node || !node->definition)
19573 return *tp;
19575 else if (TREE_CODE (*tp) == FUNCTION_DECL
19576 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
19578 /* The call graph machinery must have finished analyzing,
19579 optimizing and gimplifying the CU by now.
19580 So if *TP has no call graph node associated
19581 to it, it means *TP will not be emitted. */
19582 if (!cgraph_node::get (*tp))
19583 return *tp;
19585 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
19586 return *tp;
19588 return NULL_TREE;
19591 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
19592 for use in a later add_const_value_attribute call. */
19594 static rtx
19595 rtl_for_decl_init (tree init, tree type)
19597 rtx rtl = NULL_RTX;
19599 STRIP_NOPS (init);
19601 /* If a variable is initialized with a string constant without embedded
19602 zeros, build CONST_STRING. */
19603 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19605 tree enttype = TREE_TYPE (type);
19606 tree domain = TYPE_DOMAIN (type);
19607 scalar_int_mode mode;
19609 if (is_int_mode (TYPE_MODE (enttype), &mode)
19610 && GET_MODE_SIZE (mode) == 1
19611 && domain
19612 && TYPE_MAX_VALUE (domain)
19613 && TREE_CODE (TYPE_MAX_VALUE (domain)) == INTEGER_CST
19614 && integer_zerop (TYPE_MIN_VALUE (domain))
19615 && compare_tree_int (TYPE_MAX_VALUE (domain),
19616 TREE_STRING_LENGTH (init) - 1) == 0
19617 && ((size_t) TREE_STRING_LENGTH (init)
19618 == strlen (TREE_STRING_POINTER (init)) + 1))
19620 rtl = gen_rtx_CONST_STRING (VOIDmode,
19621 ggc_strdup (TREE_STRING_POINTER (init)));
19622 rtl = gen_rtx_MEM (BLKmode, rtl);
19623 MEM_READONLY_P (rtl) = 1;
19626 /* Other aggregates, and complex values, could be represented using
19627 CONCAT: FIXME! */
19628 else if (AGGREGATE_TYPE_P (type)
19629 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19630 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19631 || TREE_CODE (type) == COMPLEX_TYPE)
19633 /* Vectors only work if their mode is supported by the target.
19634 FIXME: generic vectors ought to work too. */
19635 else if (TREE_CODE (type) == VECTOR_TYPE
19636 && !VECTOR_MODE_P (TYPE_MODE (type)))
19638 /* If the initializer is something that we know will expand into an
19639 immediate RTL constant, expand it now. We must be careful not to
19640 reference variables which won't be output. */
19641 else if (initializer_constant_valid_p (init, type)
19642 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19644 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19645 possible. */
19646 if (TREE_CODE (type) == VECTOR_TYPE)
19647 switch (TREE_CODE (init))
19649 case VECTOR_CST:
19650 break;
19651 case CONSTRUCTOR:
19652 if (TREE_CONSTANT (init))
19654 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19655 bool constant_p = true;
19656 tree value;
19657 unsigned HOST_WIDE_INT ix;
19659 /* Even when ctor is constant, it might contain non-*_CST
19660 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19661 belong into VECTOR_CST nodes. */
19662 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19663 if (!CONSTANT_CLASS_P (value))
19665 constant_p = false;
19666 break;
19669 if (constant_p)
19671 init = build_vector_from_ctor (type, elts);
19672 break;
19675 /* FALLTHRU */
19677 default:
19678 return NULL;
19681 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19683 /* If expand_expr returns a MEM, it wasn't immediate. */
19684 gcc_assert (!rtl || !MEM_P (rtl));
19687 return rtl;
19690 /* Generate RTL for the variable DECL to represent its location. */
19692 static rtx
19693 rtl_for_decl_location (tree decl)
19695 rtx rtl;
19697 /* Here we have to decide where we are going to say the parameter "lives"
19698 (as far as the debugger is concerned). We only have a couple of
19699 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19701 DECL_RTL normally indicates where the parameter lives during most of the
19702 activation of the function. If optimization is enabled however, this
19703 could be either NULL or else a pseudo-reg. Both of those cases indicate
19704 that the parameter doesn't really live anywhere (as far as the code
19705 generation parts of GCC are concerned) during most of the function's
19706 activation. That will happen (for example) if the parameter is never
19707 referenced within the function.
19709 We could just generate a location descriptor here for all non-NULL
19710 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19711 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19712 where DECL_RTL is NULL or is a pseudo-reg.
19714 Note however that we can only get away with using DECL_INCOMING_RTL as
19715 a backup substitute for DECL_RTL in certain limited cases. In cases
19716 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19717 we can be sure that the parameter was passed using the same type as it is
19718 declared to have within the function, and that its DECL_INCOMING_RTL
19719 points us to a place where a value of that type is passed.
19721 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19722 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19723 because in these cases DECL_INCOMING_RTL points us to a value of some
19724 type which is *different* from the type of the parameter itself. Thus,
19725 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19726 such cases, the debugger would end up (for example) trying to fetch a
19727 `float' from a place which actually contains the first part of a
19728 `double'. That would lead to really incorrect and confusing
19729 output at debug-time.
19731 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19732 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19733 are a couple of exceptions however. On little-endian machines we can
19734 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19735 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19736 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19737 when (on a little-endian machine) a non-prototyped function has a
19738 parameter declared to be of type `short' or `char'. In such cases,
19739 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19740 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19741 passed `int' value. If the debugger then uses that address to fetch
19742 a `short' or a `char' (on a little-endian machine) the result will be
19743 the correct data, so we allow for such exceptional cases below.
19745 Note that our goal here is to describe the place where the given formal
19746 parameter lives during most of the function's activation (i.e. between the
19747 end of the prologue and the start of the epilogue). We'll do that as best
19748 as we can. Note however that if the given formal parameter is modified
19749 sometime during the execution of the function, then a stack backtrace (at
19750 debug-time) will show the function as having been called with the *new*
19751 value rather than the value which was originally passed in. This happens
19752 rarely enough that it is not a major problem, but it *is* a problem, and
19753 I'd like to fix it.
19755 A future version of dwarf2out.c may generate two additional attributes for
19756 any given DW_TAG_formal_parameter DIE which will describe the "passed
19757 type" and the "passed location" for the given formal parameter in addition
19758 to the attributes we now generate to indicate the "declared type" and the
19759 "active location" for each parameter. This additional set of attributes
19760 could be used by debuggers for stack backtraces. Separately, note that
19761 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19762 This happens (for example) for inlined-instances of inline function formal
19763 parameters which are never referenced. This really shouldn't be
19764 happening. All PARM_DECL nodes should get valid non-NULL
19765 DECL_INCOMING_RTL values. FIXME. */
19767 /* Use DECL_RTL as the "location" unless we find something better. */
19768 rtl = DECL_RTL_IF_SET (decl);
19770 /* When generating abstract instances, ignore everything except
19771 constants, symbols living in memory, and symbols living in
19772 fixed registers. */
19773 if (! reload_completed)
19775 if (rtl
19776 && (CONSTANT_P (rtl)
19777 || (MEM_P (rtl)
19778 && CONSTANT_P (XEXP (rtl, 0)))
19779 || (REG_P (rtl)
19780 && VAR_P (decl)
19781 && TREE_STATIC (decl))))
19783 rtl = targetm.delegitimize_address (rtl);
19784 return rtl;
19786 rtl = NULL_RTX;
19788 else if (TREE_CODE (decl) == PARM_DECL)
19790 if (rtl == NULL_RTX
19791 || is_pseudo_reg (rtl)
19792 || (MEM_P (rtl)
19793 && is_pseudo_reg (XEXP (rtl, 0))
19794 && DECL_INCOMING_RTL (decl)
19795 && MEM_P (DECL_INCOMING_RTL (decl))
19796 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19798 tree declared_type = TREE_TYPE (decl);
19799 tree passed_type = DECL_ARG_TYPE (decl);
19800 machine_mode dmode = TYPE_MODE (declared_type);
19801 machine_mode pmode = TYPE_MODE (passed_type);
19803 /* This decl represents a formal parameter which was optimized out.
19804 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19805 all cases where (rtl == NULL_RTX) just below. */
19806 if (dmode == pmode)
19807 rtl = DECL_INCOMING_RTL (decl);
19808 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19809 && SCALAR_INT_MODE_P (dmode)
19810 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19811 && DECL_INCOMING_RTL (decl))
19813 rtx inc = DECL_INCOMING_RTL (decl);
19814 if (REG_P (inc))
19815 rtl = inc;
19816 else if (MEM_P (inc))
19818 if (BYTES_BIG_ENDIAN)
19819 rtl = adjust_address_nv (inc, dmode,
19820 GET_MODE_SIZE (pmode)
19821 - GET_MODE_SIZE (dmode));
19822 else
19823 rtl = inc;
19828 /* If the parm was passed in registers, but lives on the stack, then
19829 make a big endian correction if the mode of the type of the
19830 parameter is not the same as the mode of the rtl. */
19831 /* ??? This is the same series of checks that are made in dbxout.c before
19832 we reach the big endian correction code there. It isn't clear if all
19833 of these checks are necessary here, but keeping them all is the safe
19834 thing to do. */
19835 else if (MEM_P (rtl)
19836 && XEXP (rtl, 0) != const0_rtx
19837 && ! CONSTANT_P (XEXP (rtl, 0))
19838 /* Not passed in memory. */
19839 && !MEM_P (DECL_INCOMING_RTL (decl))
19840 /* Not passed by invisible reference. */
19841 && (!REG_P (XEXP (rtl, 0))
19842 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19843 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19844 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19845 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19846 #endif
19848 /* Big endian correction check. */
19849 && BYTES_BIG_ENDIAN
19850 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19851 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19852 UNITS_PER_WORD))
19854 machine_mode addr_mode = get_address_mode (rtl);
19855 poly_int64 offset = (UNITS_PER_WORD
19856 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19858 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19859 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19862 else if (VAR_P (decl)
19863 && rtl
19864 && MEM_P (rtl)
19865 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19867 machine_mode addr_mode = get_address_mode (rtl);
19868 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19869 GET_MODE (rtl));
19871 /* If a variable is declared "register" yet is smaller than
19872 a register, then if we store the variable to memory, it
19873 looks like we're storing a register-sized value, when in
19874 fact we are not. We need to adjust the offset of the
19875 storage location to reflect the actual value's bytes,
19876 else gdb will not be able to display it. */
19877 if (maybe_ne (offset, 0))
19878 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19879 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19882 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19883 and will have been substituted directly into all expressions that use it.
19884 C does not have such a concept, but C++ and other languages do. */
19885 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19886 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19888 if (rtl)
19889 rtl = targetm.delegitimize_address (rtl);
19891 /* If we don't look past the constant pool, we risk emitting a
19892 reference to a constant pool entry that isn't referenced from
19893 code, and thus is not emitted. */
19894 if (rtl)
19895 rtl = avoid_constant_pool_reference (rtl);
19897 /* Try harder to get a rtl. If this symbol ends up not being emitted
19898 in the current CU, resolve_addr will remove the expression referencing
19899 it. */
19900 if (rtl == NULL_RTX
19901 && !(early_dwarf && (flag_generate_lto || flag_generate_offload))
19902 && VAR_P (decl)
19903 && !DECL_EXTERNAL (decl)
19904 && TREE_STATIC (decl)
19905 && DECL_NAME (decl)
19906 && !DECL_HARD_REGISTER (decl)
19907 && DECL_MODE (decl) != VOIDmode)
19909 rtl = make_decl_rtl_for_debug (decl);
19910 if (!MEM_P (rtl)
19911 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19912 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19913 rtl = NULL_RTX;
19916 return rtl;
19919 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19920 returned. If so, the decl for the COMMON block is returned, and the
19921 value is the offset into the common block for the symbol. */
19923 static tree
19924 fortran_common (tree decl, HOST_WIDE_INT *value)
19926 tree val_expr, cvar;
19927 machine_mode mode;
19928 poly_int64 bitsize, bitpos;
19929 tree offset;
19930 HOST_WIDE_INT cbitpos;
19931 int unsignedp, reversep, volatilep = 0;
19933 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19934 it does not have a value (the offset into the common area), or if it
19935 is thread local (as opposed to global) then it isn't common, and shouldn't
19936 be handled as such. */
19937 if (!VAR_P (decl)
19938 || !TREE_STATIC (decl)
19939 || !DECL_HAS_VALUE_EXPR_P (decl)
19940 || !is_fortran ())
19941 return NULL_TREE;
19943 val_expr = DECL_VALUE_EXPR (decl);
19944 if (TREE_CODE (val_expr) != COMPONENT_REF)
19945 return NULL_TREE;
19947 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19948 &unsignedp, &reversep, &volatilep);
19950 if (cvar == NULL_TREE
19951 || !VAR_P (cvar)
19952 || DECL_ARTIFICIAL (cvar)
19953 || !TREE_PUBLIC (cvar)
19954 /* We don't expect to have to cope with variable offsets,
19955 since at present all static data must have a constant size. */
19956 || !bitpos.is_constant (&cbitpos))
19957 return NULL_TREE;
19959 *value = 0;
19960 if (offset != NULL)
19962 if (!tree_fits_shwi_p (offset))
19963 return NULL_TREE;
19964 *value = tree_to_shwi (offset);
19966 if (cbitpos != 0)
19967 *value += cbitpos / BITS_PER_UNIT;
19969 return cvar;
19972 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19973 data attribute for a variable or a parameter. We generate the
19974 DW_AT_const_value attribute only in those cases where the given variable
19975 or parameter does not have a true "location" either in memory or in a
19976 register. This can happen (for example) when a constant is passed as an
19977 actual argument in a call to an inline function. (It's possible that
19978 these things can crop up in other ways also.) Note that one type of
19979 constant value which can be passed into an inlined function is a constant
19980 pointer. This can happen for example if an actual argument in an inlined
19981 function call evaluates to a compile-time constant address.
19983 CACHE_P is true if it is worth caching the location list for DECL,
19984 so that future calls can reuse it rather than regenerate it from scratch.
19985 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19986 since we will need to refer to them each time the function is inlined. */
19988 static bool
19989 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19991 rtx rtl;
19992 dw_loc_list_ref list;
19993 var_loc_list *loc_list;
19994 cached_dw_loc_list *cache;
19996 if (early_dwarf)
19997 return false;
19999 if (TREE_CODE (decl) == ERROR_MARK)
20000 return false;
20002 if (get_AT (die, DW_AT_location)
20003 || get_AT (die, DW_AT_const_value))
20004 return true;
20006 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
20007 || TREE_CODE (decl) == RESULT_DECL);
20009 /* Try to get some constant RTL for this decl, and use that as the value of
20010 the location. */
20012 rtl = rtl_for_decl_location (decl);
20013 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20014 && add_const_value_attribute (die, rtl))
20015 return true;
20017 /* See if we have single element location list that is equivalent to
20018 a constant value. That way we are better to use add_const_value_attribute
20019 rather than expanding constant value equivalent. */
20020 loc_list = lookup_decl_loc (decl);
20021 if (loc_list
20022 && loc_list->first
20023 && loc_list->first->next == NULL
20024 && NOTE_P (loc_list->first->loc)
20025 && NOTE_VAR_LOCATION (loc_list->first->loc)
20026 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
20028 struct var_loc_node *node;
20030 node = loc_list->first;
20031 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
20032 if (GET_CODE (rtl) == EXPR_LIST)
20033 rtl = XEXP (rtl, 0);
20034 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
20035 && add_const_value_attribute (die, rtl))
20036 return true;
20038 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
20039 list several times. See if we've already cached the contents. */
20040 list = NULL;
20041 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
20042 cache_p = false;
20043 if (cache_p)
20045 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
20046 if (cache)
20047 list = cache->loc_list;
20049 if (list == NULL)
20051 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
20052 NULL);
20053 /* It is usually worth caching this result if the decl is from
20054 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
20055 if (cache_p && list && list->dw_loc_next)
20057 cached_dw_loc_list **slot
20058 = cached_dw_loc_list_table->find_slot_with_hash (decl,
20059 DECL_UID (decl),
20060 INSERT);
20061 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
20062 cache->decl_id = DECL_UID (decl);
20063 cache->loc_list = list;
20064 *slot = cache;
20067 if (list)
20069 add_AT_location_description (die, DW_AT_location, list);
20070 return true;
20072 /* None of that worked, so it must not really have a location;
20073 try adding a constant value attribute from the DECL_INITIAL. */
20074 return tree_add_const_value_attribute_for_decl (die, decl);
20077 /* Helper function for tree_add_const_value_attribute. Natively encode
20078 initializer INIT into an array. Return true if successful. */
20080 static bool
20081 native_encode_initializer (tree init, unsigned char *array, int size)
20083 tree type;
20085 if (init == NULL_TREE)
20086 return false;
20088 STRIP_NOPS (init);
20089 switch (TREE_CODE (init))
20091 case STRING_CST:
20092 type = TREE_TYPE (init);
20093 if (TREE_CODE (type) == ARRAY_TYPE)
20095 tree enttype = TREE_TYPE (type);
20096 scalar_int_mode mode;
20098 if (!is_int_mode (TYPE_MODE (enttype), &mode)
20099 || GET_MODE_SIZE (mode) != 1)
20100 return false;
20101 if (int_size_in_bytes (type) != size)
20102 return false;
20103 if (size > TREE_STRING_LENGTH (init))
20105 memcpy (array, TREE_STRING_POINTER (init),
20106 TREE_STRING_LENGTH (init));
20107 memset (array + TREE_STRING_LENGTH (init),
20108 '\0', size - TREE_STRING_LENGTH (init));
20110 else
20111 memcpy (array, TREE_STRING_POINTER (init), size);
20112 return true;
20114 return false;
20115 case CONSTRUCTOR:
20116 type = TREE_TYPE (init);
20117 if (int_size_in_bytes (type) != size)
20118 return false;
20119 if (TREE_CODE (type) == ARRAY_TYPE)
20121 HOST_WIDE_INT min_index;
20122 unsigned HOST_WIDE_INT cnt;
20123 int curpos = 0, fieldsize;
20124 constructor_elt *ce;
20126 if (TYPE_DOMAIN (type) == NULL_TREE
20127 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
20128 return false;
20130 fieldsize = int_size_in_bytes (TREE_TYPE (type));
20131 if (fieldsize <= 0)
20132 return false;
20134 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
20135 memset (array, '\0', size);
20136 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20138 tree val = ce->value;
20139 tree index = ce->index;
20140 int pos = curpos;
20141 if (index && TREE_CODE (index) == RANGE_EXPR)
20142 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
20143 * fieldsize;
20144 else if (index)
20145 pos = (tree_to_shwi (index) - min_index) * fieldsize;
20147 if (val)
20149 STRIP_NOPS (val);
20150 if (!native_encode_initializer (val, array + pos, fieldsize))
20151 return false;
20153 curpos = pos + fieldsize;
20154 if (index && TREE_CODE (index) == RANGE_EXPR)
20156 int count = tree_to_shwi (TREE_OPERAND (index, 1))
20157 - tree_to_shwi (TREE_OPERAND (index, 0));
20158 while (count-- > 0)
20160 if (val)
20161 memcpy (array + curpos, array + pos, fieldsize);
20162 curpos += fieldsize;
20165 gcc_assert (curpos <= size);
20167 return true;
20169 else if (TREE_CODE (type) == RECORD_TYPE
20170 || TREE_CODE (type) == UNION_TYPE)
20172 tree field = NULL_TREE;
20173 unsigned HOST_WIDE_INT cnt;
20174 constructor_elt *ce;
20176 if (int_size_in_bytes (type) != size)
20177 return false;
20179 if (TREE_CODE (type) == RECORD_TYPE)
20180 field = TYPE_FIELDS (type);
20182 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
20184 tree val = ce->value;
20185 int pos, fieldsize;
20187 if (ce->index != 0)
20188 field = ce->index;
20190 if (val)
20191 STRIP_NOPS (val);
20193 if (field == NULL_TREE || DECL_BIT_FIELD (field))
20194 return false;
20196 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
20197 && TYPE_DOMAIN (TREE_TYPE (field))
20198 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
20199 return false;
20200 else if (DECL_SIZE_UNIT (field) == NULL_TREE
20201 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
20202 return false;
20203 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
20204 pos = int_byte_position (field);
20205 gcc_assert (pos + fieldsize <= size);
20206 if (val && fieldsize != 0
20207 && !native_encode_initializer (val, array + pos, fieldsize))
20208 return false;
20210 return true;
20212 return false;
20213 case VIEW_CONVERT_EXPR:
20214 case NON_LVALUE_EXPR:
20215 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
20216 default:
20217 return native_encode_expr (init, array, size) == size;
20221 /* Attach a DW_AT_const_value attribute to DIE. The value of the
20222 attribute is the const value T. */
20224 static bool
20225 tree_add_const_value_attribute (dw_die_ref die, tree t)
20227 tree init;
20228 tree type = TREE_TYPE (t);
20229 rtx rtl;
20231 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
20232 return false;
20234 init = t;
20235 gcc_assert (!DECL_P (init));
20237 if (TREE_CODE (init) == INTEGER_CST)
20239 if (tree_fits_uhwi_p (init))
20241 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
20242 return true;
20244 if (tree_fits_shwi_p (init))
20246 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
20247 return true;
20250 if (! early_dwarf)
20252 rtl = rtl_for_decl_init (init, type);
20253 if (rtl)
20254 return add_const_value_attribute (die, rtl);
20256 /* If the host and target are sane, try harder. */
20257 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
20258 && initializer_constant_valid_p (init, type))
20260 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
20261 if (size > 0 && (int) size == size)
20263 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
20265 if (native_encode_initializer (init, array, size))
20267 add_AT_vec (die, DW_AT_const_value, size, 1, array);
20268 return true;
20270 ggc_free (array);
20273 return false;
20276 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
20277 attribute is the const value of T, where T is an integral constant
20278 variable with static storage duration
20279 (so it can't be a PARM_DECL or a RESULT_DECL). */
20281 static bool
20282 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
20285 if (!decl
20286 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
20287 || (VAR_P (decl) && !TREE_STATIC (decl)))
20288 return false;
20290 if (TREE_READONLY (decl)
20291 && ! TREE_THIS_VOLATILE (decl)
20292 && DECL_INITIAL (decl))
20293 /* OK */;
20294 else
20295 return false;
20297 /* Don't add DW_AT_const_value if abstract origin already has one. */
20298 if (get_AT (var_die, DW_AT_const_value))
20299 return false;
20301 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
20304 /* Convert the CFI instructions for the current function into a
20305 location list. This is used for DW_AT_frame_base when we targeting
20306 a dwarf2 consumer that does not support the dwarf3
20307 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
20308 expressions. */
20310 static dw_loc_list_ref
20311 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
20313 int ix;
20314 dw_fde_ref fde;
20315 dw_loc_list_ref list, *list_tail;
20316 dw_cfi_ref cfi;
20317 dw_cfa_location last_cfa, next_cfa;
20318 const char *start_label, *last_label, *section;
20319 dw_cfa_location remember;
20321 fde = cfun->fde;
20322 gcc_assert (fde != NULL);
20324 section = secname_for_decl (current_function_decl);
20325 list_tail = &list;
20326 list = NULL;
20328 memset (&next_cfa, 0, sizeof (next_cfa));
20329 next_cfa.reg = INVALID_REGNUM;
20330 remember = next_cfa;
20332 start_label = fde->dw_fde_begin;
20334 /* ??? Bald assumption that the CIE opcode list does not contain
20335 advance opcodes. */
20336 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
20337 lookup_cfa_1 (cfi, &next_cfa, &remember);
20339 last_cfa = next_cfa;
20340 last_label = start_label;
20342 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
20344 /* If the first partition contained no CFI adjustments, the
20345 CIE opcodes apply to the whole first partition. */
20346 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20347 fde->dw_fde_begin, 0, fde->dw_fde_end, 0, section);
20348 list_tail =&(*list_tail)->dw_loc_next;
20349 start_label = last_label = fde->dw_fde_second_begin;
20352 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
20354 switch (cfi->dw_cfi_opc)
20356 case DW_CFA_set_loc:
20357 case DW_CFA_advance_loc1:
20358 case DW_CFA_advance_loc2:
20359 case DW_CFA_advance_loc4:
20360 if (!cfa_equal_p (&last_cfa, &next_cfa))
20362 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20363 start_label, 0, last_label, 0, section);
20365 list_tail = &(*list_tail)->dw_loc_next;
20366 last_cfa = next_cfa;
20367 start_label = last_label;
20369 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
20370 break;
20372 case DW_CFA_advance_loc:
20373 /* The encoding is complex enough that we should never emit this. */
20374 gcc_unreachable ();
20376 default:
20377 lookup_cfa_1 (cfi, &next_cfa, &remember);
20378 break;
20380 if (ix + 1 == fde->dw_fde_switch_cfi_index)
20382 if (!cfa_equal_p (&last_cfa, &next_cfa))
20384 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20385 start_label, 0, last_label, 0, section);
20387 list_tail = &(*list_tail)->dw_loc_next;
20388 last_cfa = next_cfa;
20389 start_label = last_label;
20391 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20392 start_label, 0, fde->dw_fde_end, 0, section);
20393 list_tail = &(*list_tail)->dw_loc_next;
20394 start_label = last_label = fde->dw_fde_second_begin;
20398 if (!cfa_equal_p (&last_cfa, &next_cfa))
20400 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
20401 start_label, 0, last_label, 0, section);
20402 list_tail = &(*list_tail)->dw_loc_next;
20403 start_label = last_label;
20406 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
20407 start_label, 0,
20408 fde->dw_fde_second_begin
20409 ? fde->dw_fde_second_end : fde->dw_fde_end, 0,
20410 section);
20412 maybe_gen_llsym (list);
20414 return list;
20417 /* Compute a displacement from the "steady-state frame pointer" to the
20418 frame base (often the same as the CFA), and store it in
20419 frame_pointer_fb_offset. OFFSET is added to the displacement
20420 before the latter is negated. */
20422 static void
20423 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
20425 rtx reg, elim;
20427 #ifdef FRAME_POINTER_CFA_OFFSET
20428 reg = frame_pointer_rtx;
20429 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
20430 #else
20431 reg = arg_pointer_rtx;
20432 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
20433 #endif
20435 elim = (ira_use_lra_p
20436 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
20437 : eliminate_regs (reg, VOIDmode, NULL_RTX));
20438 elim = strip_offset_and_add (elim, &offset);
20440 frame_pointer_fb_offset = -offset;
20442 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
20443 in which to eliminate. This is because it's stack pointer isn't
20444 directly accessible as a register within the ISA. To work around
20445 this, assume that while we cannot provide a proper value for
20446 frame_pointer_fb_offset, we won't need one either. */
20447 frame_pointer_fb_offset_valid
20448 = ((SUPPORTS_STACK_ALIGNMENT
20449 && (elim == hard_frame_pointer_rtx
20450 || elim == stack_pointer_rtx))
20451 || elim == (frame_pointer_needed
20452 ? hard_frame_pointer_rtx
20453 : stack_pointer_rtx));
20456 /* Generate a DW_AT_name attribute given some string value to be included as
20457 the value of the attribute. */
20459 static void
20460 add_name_attribute (dw_die_ref die, const char *name_string)
20462 if (name_string != NULL && *name_string != 0)
20464 if (demangle_name_func)
20465 name_string = (*demangle_name_func) (name_string);
20467 add_AT_string (die, DW_AT_name, name_string);
20471 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
20472 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
20473 of TYPE accordingly.
20475 ??? This is a temporary measure until after we're able to generate
20476 regular DWARF for the complex Ada type system. */
20478 static void
20479 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
20480 dw_die_ref context_die)
20482 tree dtype;
20483 dw_die_ref dtype_die;
20485 if (!lang_hooks.types.descriptive_type)
20486 return;
20488 dtype = lang_hooks.types.descriptive_type (type);
20489 if (!dtype)
20490 return;
20492 dtype_die = lookup_type_die (dtype);
20493 if (!dtype_die)
20495 gen_type_die (dtype, context_die);
20496 dtype_die = lookup_type_die (dtype);
20497 gcc_assert (dtype_die);
20500 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
20503 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
20505 static const char *
20506 comp_dir_string (void)
20508 const char *wd;
20509 char *wd1;
20510 static const char *cached_wd = NULL;
20512 if (cached_wd != NULL)
20513 return cached_wd;
20515 wd = get_src_pwd ();
20516 if (wd == NULL)
20517 return NULL;
20519 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
20521 int wdlen;
20523 wdlen = strlen (wd);
20524 wd1 = ggc_vec_alloc<char> (wdlen + 2);
20525 strcpy (wd1, wd);
20526 wd1 [wdlen] = DIR_SEPARATOR;
20527 wd1 [wdlen + 1] = 0;
20528 wd = wd1;
20531 cached_wd = remap_debug_filename (wd);
20532 return cached_wd;
20535 /* Generate a DW_AT_comp_dir attribute for DIE. */
20537 static void
20538 add_comp_dir_attribute (dw_die_ref die)
20540 const char * wd = comp_dir_string ();
20541 if (wd != NULL)
20542 add_AT_string (die, DW_AT_comp_dir, wd);
20545 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
20546 pointer computation, ...), output a representation for that bound according
20547 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
20548 loc_list_from_tree for the meaning of CONTEXT. */
20550 static void
20551 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
20552 int forms, struct loc_descr_context *context)
20554 dw_die_ref context_die, decl_die;
20555 dw_loc_list_ref list;
20556 bool strip_conversions = true;
20557 bool placeholder_seen = false;
20559 while (strip_conversions)
20560 switch (TREE_CODE (value))
20562 case ERROR_MARK:
20563 case SAVE_EXPR:
20564 return;
20566 CASE_CONVERT:
20567 case VIEW_CONVERT_EXPR:
20568 value = TREE_OPERAND (value, 0);
20569 break;
20571 default:
20572 strip_conversions = false;
20573 break;
20576 /* If possible and permitted, output the attribute as a constant. */
20577 if ((forms & dw_scalar_form_constant) != 0
20578 && TREE_CODE (value) == INTEGER_CST)
20580 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
20582 /* If HOST_WIDE_INT is big enough then represent the bound as
20583 a constant value. We need to choose a form based on
20584 whether the type is signed or unsigned. We cannot just
20585 call add_AT_unsigned if the value itself is positive
20586 (add_AT_unsigned might add the unsigned value encoded as
20587 DW_FORM_data[1248]). Some DWARF consumers will lookup the
20588 bounds type and then sign extend any unsigned values found
20589 for signed types. This is needed only for
20590 DW_AT_{lower,upper}_bound, since for most other attributes,
20591 consumers will treat DW_FORM_data[1248] as unsigned values,
20592 regardless of the underlying type. */
20593 if (prec <= HOST_BITS_PER_WIDE_INT
20594 || tree_fits_uhwi_p (value))
20596 if (TYPE_UNSIGNED (TREE_TYPE (value)))
20597 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
20598 else
20599 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
20601 else
20602 /* Otherwise represent the bound as an unsigned value with
20603 the precision of its type. The precision and signedness
20604 of the type will be necessary to re-interpret it
20605 unambiguously. */
20606 add_AT_wide (die, attr, wi::to_wide (value));
20607 return;
20610 /* Otherwise, if it's possible and permitted too, output a reference to
20611 another DIE. */
20612 if ((forms & dw_scalar_form_reference) != 0)
20614 tree decl = NULL_TREE;
20616 /* Some type attributes reference an outer type. For instance, the upper
20617 bound of an array may reference an embedding record (this happens in
20618 Ada). */
20619 if (TREE_CODE (value) == COMPONENT_REF
20620 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20621 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20622 decl = TREE_OPERAND (value, 1);
20624 else if (VAR_P (value)
20625 || TREE_CODE (value) == PARM_DECL
20626 || TREE_CODE (value) == RESULT_DECL)
20627 decl = value;
20629 if (decl != NULL_TREE)
20631 dw_die_ref decl_die = lookup_decl_die (decl);
20633 /* ??? Can this happen, or should the variable have been bound
20634 first? Probably it can, since I imagine that we try to create
20635 the types of parameters in the order in which they exist in
20636 the list, and won't have created a forward reference to a
20637 later parameter. */
20638 if (decl_die != NULL)
20640 add_AT_die_ref (die, attr, decl_die);
20641 return;
20646 /* Last chance: try to create a stack operation procedure to evaluate the
20647 value. Do nothing if even that is not possible or permitted. */
20648 if ((forms & dw_scalar_form_exprloc) == 0)
20649 return;
20651 list = loc_list_from_tree (value, 2, context);
20652 if (context && context->placeholder_arg)
20654 placeholder_seen = context->placeholder_seen;
20655 context->placeholder_seen = false;
20657 if (list == NULL || single_element_loc_list_p (list))
20659 /* If this attribute is not a reference nor constant, it is
20660 a DWARF expression rather than location description. For that
20661 loc_list_from_tree (value, 0, &context) is needed. */
20662 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20663 if (list2 && single_element_loc_list_p (list2))
20665 if (placeholder_seen)
20667 struct dwarf_procedure_info dpi;
20668 dpi.fndecl = NULL_TREE;
20669 dpi.args_count = 1;
20670 if (!resolve_args_picking (list2->expr, 1, &dpi))
20671 return;
20673 add_AT_loc (die, attr, list2->expr);
20674 return;
20678 /* If that failed to give a single element location list, fall back to
20679 outputting this as a reference... still if permitted. */
20680 if (list == NULL
20681 || (forms & dw_scalar_form_reference) == 0
20682 || placeholder_seen)
20683 return;
20685 if (current_function_decl == 0)
20686 context_die = comp_unit_die ();
20687 else
20688 context_die = lookup_decl_die (current_function_decl);
20690 decl_die = new_die (DW_TAG_variable, context_die, value);
20691 add_AT_flag (decl_die, DW_AT_artificial, 1);
20692 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20693 context_die);
20694 add_AT_location_description (decl_die, DW_AT_location, list);
20695 add_AT_die_ref (die, attr, decl_die);
20698 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20699 default. */
20701 static int
20702 lower_bound_default (void)
20704 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20706 case DW_LANG_C:
20707 case DW_LANG_C89:
20708 case DW_LANG_C99:
20709 case DW_LANG_C11:
20710 case DW_LANG_C_plus_plus:
20711 case DW_LANG_C_plus_plus_11:
20712 case DW_LANG_C_plus_plus_14:
20713 case DW_LANG_ObjC:
20714 case DW_LANG_ObjC_plus_plus:
20715 return 0;
20716 case DW_LANG_Fortran77:
20717 case DW_LANG_Fortran90:
20718 case DW_LANG_Fortran95:
20719 case DW_LANG_Fortran03:
20720 case DW_LANG_Fortran08:
20721 return 1;
20722 case DW_LANG_UPC:
20723 case DW_LANG_D:
20724 case DW_LANG_Python:
20725 return dwarf_version >= 4 ? 0 : -1;
20726 case DW_LANG_Ada95:
20727 case DW_LANG_Ada83:
20728 case DW_LANG_Cobol74:
20729 case DW_LANG_Cobol85:
20730 case DW_LANG_Modula2:
20731 case DW_LANG_PLI:
20732 return dwarf_version >= 4 ? 1 : -1;
20733 default:
20734 return -1;
20738 /* Given a tree node describing an array bound (either lower or upper) output
20739 a representation for that bound. */
20741 static void
20742 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20743 tree bound, struct loc_descr_context *context)
20745 int dflt;
20747 while (1)
20748 switch (TREE_CODE (bound))
20750 /* Strip all conversions. */
20751 CASE_CONVERT:
20752 case VIEW_CONVERT_EXPR:
20753 bound = TREE_OPERAND (bound, 0);
20754 break;
20756 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20757 are even omitted when they are the default. */
20758 case INTEGER_CST:
20759 /* If the value for this bound is the default one, we can even omit the
20760 attribute. */
20761 if (bound_attr == DW_AT_lower_bound
20762 && tree_fits_shwi_p (bound)
20763 && (dflt = lower_bound_default ()) != -1
20764 && tree_to_shwi (bound) == dflt)
20765 return;
20767 /* FALLTHRU */
20769 default:
20770 /* Because of the complex interaction there can be with other GNAT
20771 encodings, GDB isn't ready yet to handle proper DWARF description
20772 for self-referencial subrange bounds: let GNAT encodings do the
20773 magic in such a case. */
20774 if (is_ada ()
20775 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20776 && contains_placeholder_p (bound))
20777 return;
20779 add_scalar_info (subrange_die, bound_attr, bound,
20780 dw_scalar_form_constant
20781 | dw_scalar_form_exprloc
20782 | dw_scalar_form_reference,
20783 context);
20784 return;
20788 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20789 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20790 Note that the block of subscript information for an array type also
20791 includes information about the element type of the given array type.
20793 This function reuses previously set type and bound information if
20794 available. */
20796 static void
20797 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20799 unsigned dimension_number;
20800 tree lower, upper;
20801 dw_die_ref child = type_die->die_child;
20803 for (dimension_number = 0;
20804 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20805 type = TREE_TYPE (type), dimension_number++)
20807 tree domain = TYPE_DOMAIN (type);
20809 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20810 break;
20812 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20813 and (in GNU C only) variable bounds. Handle all three forms
20814 here. */
20816 /* Find and reuse a previously generated DW_TAG_subrange_type if
20817 available.
20819 For multi-dimensional arrays, as we iterate through the
20820 various dimensions in the enclosing for loop above, we also
20821 iterate through the DIE children and pick at each
20822 DW_TAG_subrange_type previously generated (if available).
20823 Each child DW_TAG_subrange_type DIE describes the range of
20824 the current dimension. At this point we should have as many
20825 DW_TAG_subrange_type's as we have dimensions in the
20826 array. */
20827 dw_die_ref subrange_die = NULL;
20828 if (child)
20829 while (1)
20831 child = child->die_sib;
20832 if (child->die_tag == DW_TAG_subrange_type)
20833 subrange_die = child;
20834 if (child == type_die->die_child)
20836 /* If we wrapped around, stop looking next time. */
20837 child = NULL;
20838 break;
20840 if (child->die_tag == DW_TAG_subrange_type)
20841 break;
20843 if (!subrange_die)
20844 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20846 if (domain)
20848 /* We have an array type with specified bounds. */
20849 lower = TYPE_MIN_VALUE (domain);
20850 upper = TYPE_MAX_VALUE (domain);
20852 /* Define the index type. */
20853 if (TREE_TYPE (domain)
20854 && !get_AT (subrange_die, DW_AT_type))
20856 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20857 TREE_TYPE field. We can't emit debug info for this
20858 because it is an unnamed integral type. */
20859 if (TREE_CODE (domain) == INTEGER_TYPE
20860 && TYPE_NAME (domain) == NULL_TREE
20861 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20862 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20864 else
20865 add_type_attribute (subrange_die, TREE_TYPE (domain),
20866 TYPE_UNQUALIFIED, false, type_die);
20869 /* ??? If upper is NULL, the array has unspecified length,
20870 but it does have a lower bound. This happens with Fortran
20871 dimension arr(N:*)
20872 Since the debugger is definitely going to need to know N
20873 to produce useful results, go ahead and output the lower
20874 bound solo, and hope the debugger can cope. */
20876 if (!get_AT (subrange_die, DW_AT_lower_bound))
20877 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20878 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20879 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20882 /* Otherwise we have an array type with an unspecified length. The
20883 DWARF-2 spec does not say how to handle this; let's just leave out the
20884 bounds. */
20888 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20890 static void
20891 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20893 dw_die_ref decl_die;
20894 HOST_WIDE_INT size;
20895 dw_loc_descr_ref size_expr = NULL;
20897 switch (TREE_CODE (tree_node))
20899 case ERROR_MARK:
20900 size = 0;
20901 break;
20902 case ENUMERAL_TYPE:
20903 case RECORD_TYPE:
20904 case UNION_TYPE:
20905 case QUAL_UNION_TYPE:
20906 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20907 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20909 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20910 return;
20912 size_expr = type_byte_size (tree_node, &size);
20913 break;
20914 case FIELD_DECL:
20915 /* For a data member of a struct or union, the DW_AT_byte_size is
20916 generally given as the number of bytes normally allocated for an
20917 object of the *declared* type of the member itself. This is true
20918 even for bit-fields. */
20919 size = int_size_in_bytes (field_type (tree_node));
20920 break;
20921 default:
20922 gcc_unreachable ();
20925 /* Support for dynamically-sized objects was introduced by DWARFv3.
20926 At the moment, GDB does not handle variable byte sizes very well,
20927 though. */
20928 if ((dwarf_version >= 3 || !dwarf_strict)
20929 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20930 && size_expr != NULL)
20931 add_AT_loc (die, DW_AT_byte_size, size_expr);
20933 /* Note that `size' might be -1 when we get to this point. If it is, that
20934 indicates that the byte size of the entity in question is variable and
20935 that we could not generate a DWARF expression that computes it. */
20936 if (size >= 0)
20937 add_AT_unsigned (die, DW_AT_byte_size, size);
20940 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20941 alignment. */
20943 static void
20944 add_alignment_attribute (dw_die_ref die, tree tree_node)
20946 if (dwarf_version < 5 && dwarf_strict)
20947 return;
20949 unsigned align;
20951 if (DECL_P (tree_node))
20953 if (!DECL_USER_ALIGN (tree_node))
20954 return;
20956 align = DECL_ALIGN_UNIT (tree_node);
20958 else if (TYPE_P (tree_node))
20960 if (!TYPE_USER_ALIGN (tree_node))
20961 return;
20963 align = TYPE_ALIGN_UNIT (tree_node);
20965 else
20966 gcc_unreachable ();
20968 add_AT_unsigned (die, DW_AT_alignment, align);
20971 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20972 which specifies the distance in bits from the highest order bit of the
20973 "containing object" for the bit-field to the highest order bit of the
20974 bit-field itself.
20976 For any given bit-field, the "containing object" is a hypothetical object
20977 (of some integral or enum type) within which the given bit-field lives. The
20978 type of this hypothetical "containing object" is always the same as the
20979 declared type of the individual bit-field itself. The determination of the
20980 exact location of the "containing object" for a bit-field is rather
20981 complicated. It's handled by the `field_byte_offset' function (above).
20983 CTX is required: see the comment for VLR_CONTEXT.
20985 Note that it is the size (in bytes) of the hypothetical "containing object"
20986 which will be given in the DW_AT_byte_size attribute for this bit-field.
20987 (See `byte_size_attribute' above). */
20989 static inline void
20990 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20992 HOST_WIDE_INT object_offset_in_bytes;
20993 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20994 HOST_WIDE_INT bitpos_int;
20995 HOST_WIDE_INT highest_order_object_bit_offset;
20996 HOST_WIDE_INT highest_order_field_bit_offset;
20997 HOST_WIDE_INT bit_offset;
20999 field_byte_offset (decl, ctx, &object_offset_in_bytes);
21001 /* Must be a field and a bit field. */
21002 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
21004 /* We can't yet handle bit-fields whose offsets are variable, so if we
21005 encounter such things, just return without generating any attribute
21006 whatsoever. Likewise for variable or too large size. */
21007 if (! tree_fits_shwi_p (bit_position (decl))
21008 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
21009 return;
21011 bitpos_int = int_bit_position (decl);
21013 /* Note that the bit offset is always the distance (in bits) from the
21014 highest-order bit of the "containing object" to the highest-order bit of
21015 the bit-field itself. Since the "high-order end" of any object or field
21016 is different on big-endian and little-endian machines, the computation
21017 below must take account of these differences. */
21018 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
21019 highest_order_field_bit_offset = bitpos_int;
21021 if (! BYTES_BIG_ENDIAN)
21023 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
21024 highest_order_object_bit_offset +=
21025 simple_type_size_in_bits (original_type);
21028 bit_offset
21029 = (! BYTES_BIG_ENDIAN
21030 ? highest_order_object_bit_offset - highest_order_field_bit_offset
21031 : highest_order_field_bit_offset - highest_order_object_bit_offset);
21033 if (bit_offset < 0)
21034 add_AT_int (die, DW_AT_bit_offset, bit_offset);
21035 else
21036 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
21039 /* For a FIELD_DECL node which represents a bit field, output an attribute
21040 which specifies the length in bits of the given field. */
21042 static inline void
21043 add_bit_size_attribute (dw_die_ref die, tree decl)
21045 /* Must be a field and a bit field. */
21046 gcc_assert (TREE_CODE (decl) == FIELD_DECL
21047 && DECL_BIT_FIELD_TYPE (decl));
21049 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
21050 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
21053 /* If the compiled language is ANSI C, then add a 'prototyped'
21054 attribute, if arg types are given for the parameters of a function. */
21056 static inline void
21057 add_prototyped_attribute (dw_die_ref die, tree func_type)
21059 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
21061 case DW_LANG_C:
21062 case DW_LANG_C89:
21063 case DW_LANG_C99:
21064 case DW_LANG_C11:
21065 case DW_LANG_ObjC:
21066 if (prototype_p (func_type))
21067 add_AT_flag (die, DW_AT_prototyped, 1);
21068 break;
21069 default:
21070 break;
21074 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
21075 by looking in the type declaration, the object declaration equate table or
21076 the block mapping. */
21078 static inline dw_die_ref
21079 add_abstract_origin_attribute (dw_die_ref die, tree origin)
21081 dw_die_ref origin_die = NULL;
21083 if (DECL_P (origin))
21085 dw_die_ref c;
21086 origin_die = lookup_decl_die (origin);
21087 /* "Unwrap" the decls DIE which we put in the imported unit context.
21088 We are looking for the abstract copy here. */
21089 if (in_lto_p
21090 && origin_die
21091 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
21092 /* ??? Identify this better. */
21093 && c->with_offset)
21094 origin_die = c;
21096 else if (TYPE_P (origin))
21097 origin_die = lookup_type_die (origin);
21098 else if (TREE_CODE (origin) == BLOCK)
21099 origin_die = BLOCK_DIE (origin);
21101 /* XXX: Functions that are never lowered don't always have correct block
21102 trees (in the case of java, they simply have no block tree, in some other
21103 languages). For these functions, there is nothing we can really do to
21104 output correct debug info for inlined functions in all cases. Rather
21105 than die, we'll just produce deficient debug info now, in that we will
21106 have variables without a proper abstract origin. In the future, when all
21107 functions are lowered, we should re-add a gcc_assert (origin_die)
21108 here. */
21110 if (origin_die)
21111 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
21112 return origin_die;
21115 /* We do not currently support the pure_virtual attribute. */
21117 static inline void
21118 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
21120 if (DECL_VINDEX (func_decl))
21122 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21124 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
21125 add_AT_loc (die, DW_AT_vtable_elem_location,
21126 new_loc_descr (DW_OP_constu,
21127 tree_to_shwi (DECL_VINDEX (func_decl)),
21128 0));
21130 /* GNU extension: Record what type this method came from originally. */
21131 if (debug_info_level > DINFO_LEVEL_TERSE
21132 && DECL_CONTEXT (func_decl))
21133 add_AT_die_ref (die, DW_AT_containing_type,
21134 lookup_type_die (DECL_CONTEXT (func_decl)));
21138 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
21139 given decl. This used to be a vendor extension until after DWARF 4
21140 standardized it. */
21142 static void
21143 add_linkage_attr (dw_die_ref die, tree decl)
21145 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
21147 /* Mimic what assemble_name_raw does with a leading '*'. */
21148 if (name[0] == '*')
21149 name = &name[1];
21151 if (dwarf_version >= 4)
21152 add_AT_string (die, DW_AT_linkage_name, name);
21153 else
21154 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
21157 /* Add source coordinate attributes for the given decl. */
21159 static void
21160 add_src_coords_attributes (dw_die_ref die, tree decl)
21162 expanded_location s;
21164 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
21165 return;
21166 s = expand_location (DECL_SOURCE_LOCATION (decl));
21167 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
21168 add_AT_unsigned (die, DW_AT_decl_line, s.line);
21169 if (debug_column_info && s.column)
21170 add_AT_unsigned (die, DW_AT_decl_column, s.column);
21173 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
21175 static void
21176 add_linkage_name_raw (dw_die_ref die, tree decl)
21178 /* Defer until we have an assembler name set. */
21179 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
21181 limbo_die_node *asm_name;
21183 asm_name = ggc_cleared_alloc<limbo_die_node> ();
21184 asm_name->die = die;
21185 asm_name->created_for = decl;
21186 asm_name->next = deferred_asm_name;
21187 deferred_asm_name = asm_name;
21189 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21190 add_linkage_attr (die, decl);
21193 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
21195 static void
21196 add_linkage_name (dw_die_ref die, tree decl)
21198 if (debug_info_level > DINFO_LEVEL_NONE
21199 && VAR_OR_FUNCTION_DECL_P (decl)
21200 && TREE_PUBLIC (decl)
21201 && !(VAR_P (decl) && DECL_REGISTER (decl))
21202 && die->die_tag != DW_TAG_member)
21203 add_linkage_name_raw (die, decl);
21206 /* Add a DW_AT_name attribute and source coordinate attribute for the
21207 given decl, but only if it actually has a name. */
21209 static void
21210 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
21211 bool no_linkage_name)
21213 tree decl_name;
21215 decl_name = DECL_NAME (decl);
21216 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
21218 const char *name = dwarf2_name (decl, 0);
21219 if (name)
21220 add_name_attribute (die, name);
21221 if (! DECL_ARTIFICIAL (decl))
21222 add_src_coords_attributes (die, decl);
21224 if (!no_linkage_name)
21225 add_linkage_name (die, decl);
21228 #ifdef VMS_DEBUGGING_INFO
21229 /* Get the function's name, as described by its RTL. This may be different
21230 from the DECL_NAME name used in the source file. */
21231 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
21233 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
21234 XEXP (DECL_RTL (decl), 0), false);
21235 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
21237 #endif /* VMS_DEBUGGING_INFO */
21240 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
21242 static void
21243 add_discr_value (dw_die_ref die, dw_discr_value *value)
21245 dw_attr_node attr;
21247 attr.dw_attr = DW_AT_discr_value;
21248 attr.dw_attr_val.val_class = dw_val_class_discr_value;
21249 attr.dw_attr_val.val_entry = NULL;
21250 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
21251 if (value->pos)
21252 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
21253 else
21254 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
21255 add_dwarf_attr (die, &attr);
21258 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
21260 static void
21261 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
21263 dw_attr_node attr;
21265 attr.dw_attr = DW_AT_discr_list;
21266 attr.dw_attr_val.val_class = dw_val_class_discr_list;
21267 attr.dw_attr_val.val_entry = NULL;
21268 attr.dw_attr_val.v.val_discr_list = discr_list;
21269 add_dwarf_attr (die, &attr);
21272 static inline dw_discr_list_ref
21273 AT_discr_list (dw_attr_node *attr)
21275 return attr->dw_attr_val.v.val_discr_list;
21278 #ifdef VMS_DEBUGGING_INFO
21279 /* Output the debug main pointer die for VMS */
21281 void
21282 dwarf2out_vms_debug_main_pointer (void)
21284 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21285 dw_die_ref die;
21287 /* Allocate the VMS debug main subprogram die. */
21288 die = new_die_raw (DW_TAG_subprogram);
21289 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
21290 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
21291 current_function_funcdef_no);
21292 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21294 /* Make it the first child of comp_unit_die (). */
21295 die->die_parent = comp_unit_die ();
21296 if (comp_unit_die ()->die_child)
21298 die->die_sib = comp_unit_die ()->die_child->die_sib;
21299 comp_unit_die ()->die_child->die_sib = die;
21301 else
21303 die->die_sib = die;
21304 comp_unit_die ()->die_child = die;
21307 #endif /* VMS_DEBUGGING_INFO */
21309 /* Push a new declaration scope. */
21311 static void
21312 push_decl_scope (tree scope)
21314 vec_safe_push (decl_scope_table, scope);
21317 /* Pop a declaration scope. */
21319 static inline void
21320 pop_decl_scope (void)
21322 decl_scope_table->pop ();
21325 /* walk_tree helper function for uses_local_type, below. */
21327 static tree
21328 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
21330 if (!TYPE_P (*tp))
21331 *walk_subtrees = 0;
21332 else
21334 tree name = TYPE_NAME (*tp);
21335 if (name && DECL_P (name) && decl_function_context (name))
21336 return *tp;
21338 return NULL_TREE;
21341 /* If TYPE involves a function-local type (including a local typedef to a
21342 non-local type), returns that type; otherwise returns NULL_TREE. */
21344 static tree
21345 uses_local_type (tree type)
21347 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
21348 return used;
21351 /* Return the DIE for the scope that immediately contains this type.
21352 Non-named types that do not involve a function-local type get global
21353 scope. Named types nested in namespaces or other types get their
21354 containing scope. All other types (i.e. function-local named types) get
21355 the current active scope. */
21357 static dw_die_ref
21358 scope_die_for (tree t, dw_die_ref context_die)
21360 dw_die_ref scope_die = NULL;
21361 tree containing_scope;
21363 /* Non-types always go in the current scope. */
21364 gcc_assert (TYPE_P (t));
21366 /* Use the scope of the typedef, rather than the scope of the type
21367 it refers to. */
21368 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
21369 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
21370 else
21371 containing_scope = TYPE_CONTEXT (t);
21373 /* Use the containing namespace if there is one. */
21374 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
21376 if (context_die == lookup_decl_die (containing_scope))
21377 /* OK */;
21378 else if (debug_info_level > DINFO_LEVEL_TERSE)
21379 context_die = get_context_die (containing_scope);
21380 else
21381 containing_scope = NULL_TREE;
21384 /* Ignore function type "scopes" from the C frontend. They mean that
21385 a tagged type is local to a parmlist of a function declarator, but
21386 that isn't useful to DWARF. */
21387 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
21388 containing_scope = NULL_TREE;
21390 if (SCOPE_FILE_SCOPE_P (containing_scope))
21392 /* If T uses a local type keep it local as well, to avoid references
21393 to function-local DIEs from outside the function. */
21394 if (current_function_decl && uses_local_type (t))
21395 scope_die = context_die;
21396 else
21397 scope_die = comp_unit_die ();
21399 else if (TYPE_P (containing_scope))
21401 /* For types, we can just look up the appropriate DIE. */
21402 if (debug_info_level > DINFO_LEVEL_TERSE)
21403 scope_die = get_context_die (containing_scope);
21404 else
21406 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
21407 if (scope_die == NULL)
21408 scope_die = comp_unit_die ();
21411 else
21412 scope_die = context_die;
21414 return scope_die;
21417 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
21419 static inline int
21420 local_scope_p (dw_die_ref context_die)
21422 for (; context_die; context_die = context_die->die_parent)
21423 if (context_die->die_tag == DW_TAG_inlined_subroutine
21424 || context_die->die_tag == DW_TAG_subprogram)
21425 return 1;
21427 return 0;
21430 /* Returns nonzero if CONTEXT_DIE is a class. */
21432 static inline int
21433 class_scope_p (dw_die_ref context_die)
21435 return (context_die
21436 && (context_die->die_tag == DW_TAG_structure_type
21437 || context_die->die_tag == DW_TAG_class_type
21438 || context_die->die_tag == DW_TAG_interface_type
21439 || context_die->die_tag == DW_TAG_union_type));
21442 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
21443 whether or not to treat a DIE in this context as a declaration. */
21445 static inline int
21446 class_or_namespace_scope_p (dw_die_ref context_die)
21448 return (class_scope_p (context_die)
21449 || (context_die && context_die->die_tag == DW_TAG_namespace));
21452 /* Many forms of DIEs require a "type description" attribute. This
21453 routine locates the proper "type descriptor" die for the type given
21454 by 'type' plus any additional qualifiers given by 'cv_quals', and
21455 adds a DW_AT_type attribute below the given die. */
21457 static void
21458 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
21459 bool reverse, dw_die_ref context_die)
21461 enum tree_code code = TREE_CODE (type);
21462 dw_die_ref type_die = NULL;
21464 /* ??? If this type is an unnamed subrange type of an integral, floating-point
21465 or fixed-point type, use the inner type. This is because we have no
21466 support for unnamed types in base_type_die. This can happen if this is
21467 an Ada subrange type. Correct solution is emit a subrange type die. */
21468 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
21469 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
21470 type = TREE_TYPE (type), code = TREE_CODE (type);
21472 if (code == ERROR_MARK
21473 /* Handle a special case. For functions whose return type is void, we
21474 generate *no* type attribute. (Note that no object may have type
21475 `void', so this only applies to function return types). */
21476 || code == VOID_TYPE)
21477 return;
21479 type_die = modified_type_die (type,
21480 cv_quals | TYPE_QUALS (type),
21481 reverse,
21482 context_die);
21484 if (type_die != NULL)
21485 add_AT_die_ref (object_die, DW_AT_type, type_die);
21488 /* Given an object die, add the calling convention attribute for the
21489 function call type. */
21490 static void
21491 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
21493 enum dwarf_calling_convention value = DW_CC_normal;
21495 value = ((enum dwarf_calling_convention)
21496 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
21498 if (is_fortran ()
21499 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
21501 /* DWARF 2 doesn't provide a way to identify a program's source-level
21502 entry point. DW_AT_calling_convention attributes are only meant
21503 to describe functions' calling conventions. However, lacking a
21504 better way to signal the Fortran main program, we used this for
21505 a long time, following existing custom. Now, DWARF 4 has
21506 DW_AT_main_subprogram, which we add below, but some tools still
21507 rely on the old way, which we thus keep. */
21508 value = DW_CC_program;
21510 if (dwarf_version >= 4 || !dwarf_strict)
21511 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
21514 /* Only add the attribute if the backend requests it, and
21515 is not DW_CC_normal. */
21516 if (value && (value != DW_CC_normal))
21517 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
21520 /* Given a tree pointer to a struct, class, union, or enum type node, return
21521 a pointer to the (string) tag name for the given type, or zero if the type
21522 was declared without a tag. */
21524 static const char *
21525 type_tag (const_tree type)
21527 const char *name = 0;
21529 if (TYPE_NAME (type) != 0)
21531 tree t = 0;
21533 /* Find the IDENTIFIER_NODE for the type name. */
21534 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
21535 && !TYPE_NAMELESS (type))
21536 t = TYPE_NAME (type);
21538 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
21539 a TYPE_DECL node, regardless of whether or not a `typedef' was
21540 involved. */
21541 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21542 && ! DECL_IGNORED_P (TYPE_NAME (type)))
21544 /* We want to be extra verbose. Don't call dwarf_name if
21545 DECL_NAME isn't set. The default hook for decl_printable_name
21546 doesn't like that, and in this context it's correct to return
21547 0, instead of "<anonymous>" or the like. */
21548 if (DECL_NAME (TYPE_NAME (type))
21549 && !DECL_NAMELESS (TYPE_NAME (type)))
21550 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
21553 /* Now get the name as a string, or invent one. */
21554 if (!name && t != 0)
21555 name = IDENTIFIER_POINTER (t);
21558 return (name == 0 || *name == '\0') ? 0 : name;
21561 /* Return the type associated with a data member, make a special check
21562 for bit field types. */
21564 static inline tree
21565 member_declared_type (const_tree member)
21567 return (DECL_BIT_FIELD_TYPE (member)
21568 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
21571 /* Get the decl's label, as described by its RTL. This may be different
21572 from the DECL_NAME name used in the source file. */
21574 #if 0
21575 static const char *
21576 decl_start_label (tree decl)
21578 rtx x;
21579 const char *fnname;
21581 x = DECL_RTL (decl);
21582 gcc_assert (MEM_P (x));
21584 x = XEXP (x, 0);
21585 gcc_assert (GET_CODE (x) == SYMBOL_REF);
21587 fnname = XSTR (x, 0);
21588 return fnname;
21590 #endif
21592 /* For variable-length arrays that have been previously generated, but
21593 may be incomplete due to missing subscript info, fill the subscript
21594 info. Return TRUE if this is one of those cases. */
21595 static bool
21596 fill_variable_array_bounds (tree type)
21598 if (TREE_ASM_WRITTEN (type)
21599 && TREE_CODE (type) == ARRAY_TYPE
21600 && variably_modified_type_p (type, NULL))
21602 dw_die_ref array_die = lookup_type_die (type);
21603 if (!array_die)
21604 return false;
21605 add_subscript_info (array_die, type, !is_ada ());
21606 return true;
21608 return false;
21611 /* These routines generate the internal representation of the DIE's for
21612 the compilation unit. Debugging information is collected by walking
21613 the declaration trees passed in from dwarf2out_decl(). */
21615 static void
21616 gen_array_type_die (tree type, dw_die_ref context_die)
21618 dw_die_ref array_die;
21620 /* GNU compilers represent multidimensional array types as sequences of one
21621 dimensional array types whose element types are themselves array types.
21622 We sometimes squish that down to a single array_type DIE with multiple
21623 subscripts in the Dwarf debugging info. The draft Dwarf specification
21624 say that we are allowed to do this kind of compression in C, because
21625 there is no difference between an array of arrays and a multidimensional
21626 array. We don't do this for Ada to remain as close as possible to the
21627 actual representation, which is especially important against the language
21628 flexibilty wrt arrays of variable size. */
21630 bool collapse_nested_arrays = !is_ada ();
21632 if (fill_variable_array_bounds (type))
21633 return;
21635 dw_die_ref scope_die = scope_die_for (type, context_die);
21636 tree element_type;
21638 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21639 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21640 if (TYPE_STRING_FLAG (type)
21641 && TREE_CODE (type) == ARRAY_TYPE
21642 && is_fortran ()
21643 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21645 HOST_WIDE_INT size;
21647 array_die = new_die (DW_TAG_string_type, scope_die, type);
21648 add_name_attribute (array_die, type_tag (type));
21649 equate_type_number_to_die (type, array_die);
21650 size = int_size_in_bytes (type);
21651 if (size >= 0)
21652 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21653 /* ??? We can't annotate types late, but for LTO we may not
21654 generate a location early either (gfortran.dg/save_6.f90). */
21655 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21656 && TYPE_DOMAIN (type) != NULL_TREE
21657 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21659 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21660 tree rszdecl = szdecl;
21662 size = int_size_in_bytes (TREE_TYPE (szdecl));
21663 if (!DECL_P (szdecl))
21665 if (TREE_CODE (szdecl) == INDIRECT_REF
21666 && DECL_P (TREE_OPERAND (szdecl, 0)))
21668 rszdecl = TREE_OPERAND (szdecl, 0);
21669 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21670 != DWARF2_ADDR_SIZE)
21671 size = 0;
21673 else
21674 size = 0;
21676 if (size > 0)
21678 dw_loc_list_ref loc
21679 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21680 NULL);
21681 if (loc)
21683 add_AT_location_description (array_die, DW_AT_string_length,
21684 loc);
21685 if (size != DWARF2_ADDR_SIZE)
21686 add_AT_unsigned (array_die, dwarf_version >= 5
21687 ? DW_AT_string_length_byte_size
21688 : DW_AT_byte_size, size);
21692 return;
21695 array_die = new_die (DW_TAG_array_type, scope_die, type);
21696 add_name_attribute (array_die, type_tag (type));
21697 equate_type_number_to_die (type, array_die);
21699 if (TREE_CODE (type) == VECTOR_TYPE)
21700 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21702 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21703 if (is_fortran ()
21704 && TREE_CODE (type) == ARRAY_TYPE
21705 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21706 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21707 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21709 #if 0
21710 /* We default the array ordering. Debuggers will probably do the right
21711 things even if DW_AT_ordering is not present. It's not even an issue
21712 until we start to get into multidimensional arrays anyway. If a debugger
21713 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21714 then we'll have to put the DW_AT_ordering attribute back in. (But if
21715 and when we find out that we need to put these in, we will only do so
21716 for multidimensional arrays. */
21717 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21718 #endif
21720 if (TREE_CODE (type) == VECTOR_TYPE)
21722 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21723 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21724 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21725 add_bound_info (subrange_die, DW_AT_upper_bound,
21726 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21728 else
21729 add_subscript_info (array_die, type, collapse_nested_arrays);
21731 /* Add representation of the type of the elements of this array type and
21732 emit the corresponding DIE if we haven't done it already. */
21733 element_type = TREE_TYPE (type);
21734 if (collapse_nested_arrays)
21735 while (TREE_CODE (element_type) == ARRAY_TYPE)
21737 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21738 break;
21739 element_type = TREE_TYPE (element_type);
21742 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21743 TREE_CODE (type) == ARRAY_TYPE
21744 && TYPE_REVERSE_STORAGE_ORDER (type),
21745 context_die);
21747 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21748 if (TYPE_ARTIFICIAL (type))
21749 add_AT_flag (array_die, DW_AT_artificial, 1);
21751 if (get_AT (array_die, DW_AT_name))
21752 add_pubtype (type, array_die);
21754 add_alignment_attribute (array_die, type);
21757 /* This routine generates DIE for array with hidden descriptor, details
21758 are filled into *info by a langhook. */
21760 static void
21761 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21762 dw_die_ref context_die)
21764 const dw_die_ref scope_die = scope_die_for (type, context_die);
21765 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21766 struct loc_descr_context context = { type, info->base_decl, NULL,
21767 false, false };
21768 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21769 int dim;
21771 add_name_attribute (array_die, type_tag (type));
21772 equate_type_number_to_die (type, array_die);
21774 if (info->ndimensions > 1)
21775 switch (info->ordering)
21777 case array_descr_ordering_row_major:
21778 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21779 break;
21780 case array_descr_ordering_column_major:
21781 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21782 break;
21783 default:
21784 break;
21787 if (dwarf_version >= 3 || !dwarf_strict)
21789 if (info->data_location)
21790 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21791 dw_scalar_form_exprloc, &context);
21792 if (info->associated)
21793 add_scalar_info (array_die, DW_AT_associated, info->associated,
21794 dw_scalar_form_constant
21795 | dw_scalar_form_exprloc
21796 | dw_scalar_form_reference, &context);
21797 if (info->allocated)
21798 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21799 dw_scalar_form_constant
21800 | dw_scalar_form_exprloc
21801 | dw_scalar_form_reference, &context);
21802 if (info->stride)
21804 const enum dwarf_attribute attr
21805 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21806 const int forms
21807 = (info->stride_in_bits)
21808 ? dw_scalar_form_constant
21809 : (dw_scalar_form_constant
21810 | dw_scalar_form_exprloc
21811 | dw_scalar_form_reference);
21813 add_scalar_info (array_die, attr, info->stride, forms, &context);
21816 if (dwarf_version >= 5)
21818 if (info->rank)
21820 add_scalar_info (array_die, DW_AT_rank, info->rank,
21821 dw_scalar_form_constant
21822 | dw_scalar_form_exprloc, &context);
21823 subrange_tag = DW_TAG_generic_subrange;
21824 context.placeholder_arg = true;
21828 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21830 for (dim = 0; dim < info->ndimensions; dim++)
21832 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21834 if (info->dimen[dim].bounds_type)
21835 add_type_attribute (subrange_die,
21836 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21837 false, context_die);
21838 if (info->dimen[dim].lower_bound)
21839 add_bound_info (subrange_die, DW_AT_lower_bound,
21840 info->dimen[dim].lower_bound, &context);
21841 if (info->dimen[dim].upper_bound)
21842 add_bound_info (subrange_die, DW_AT_upper_bound,
21843 info->dimen[dim].upper_bound, &context);
21844 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21845 add_scalar_info (subrange_die, DW_AT_byte_stride,
21846 info->dimen[dim].stride,
21847 dw_scalar_form_constant
21848 | dw_scalar_form_exprloc
21849 | dw_scalar_form_reference,
21850 &context);
21853 gen_type_die (info->element_type, context_die);
21854 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21855 TREE_CODE (type) == ARRAY_TYPE
21856 && TYPE_REVERSE_STORAGE_ORDER (type),
21857 context_die);
21859 if (get_AT (array_die, DW_AT_name))
21860 add_pubtype (type, array_die);
21862 add_alignment_attribute (array_die, type);
21865 #if 0
21866 static void
21867 gen_entry_point_die (tree decl, dw_die_ref context_die)
21869 tree origin = decl_ultimate_origin (decl);
21870 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21872 if (origin != NULL)
21873 add_abstract_origin_attribute (decl_die, origin);
21874 else
21876 add_name_and_src_coords_attributes (decl_die, decl);
21877 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21878 TYPE_UNQUALIFIED, false, context_die);
21881 if (DECL_ABSTRACT_P (decl))
21882 equate_decl_number_to_die (decl, decl_die);
21883 else
21884 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21886 #endif
21888 /* Walk through the list of incomplete types again, trying once more to
21889 emit full debugging info for them. */
21891 static void
21892 retry_incomplete_types (void)
21894 set_early_dwarf s;
21895 int i;
21897 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21898 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21899 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21900 vec_safe_truncate (incomplete_types, 0);
21903 /* Determine what tag to use for a record type. */
21905 static enum dwarf_tag
21906 record_type_tag (tree type)
21908 if (! lang_hooks.types.classify_record)
21909 return DW_TAG_structure_type;
21911 switch (lang_hooks.types.classify_record (type))
21913 case RECORD_IS_STRUCT:
21914 return DW_TAG_structure_type;
21916 case RECORD_IS_CLASS:
21917 return DW_TAG_class_type;
21919 case RECORD_IS_INTERFACE:
21920 if (dwarf_version >= 3 || !dwarf_strict)
21921 return DW_TAG_interface_type;
21922 return DW_TAG_structure_type;
21924 default:
21925 gcc_unreachable ();
21929 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21930 include all of the information about the enumeration values also. Each
21931 enumerated type name/value is listed as a child of the enumerated type
21932 DIE. */
21934 static dw_die_ref
21935 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21937 dw_die_ref type_die = lookup_type_die (type);
21938 dw_die_ref orig_type_die = type_die;
21940 if (type_die == NULL)
21942 type_die = new_die (DW_TAG_enumeration_type,
21943 scope_die_for (type, context_die), type);
21944 equate_type_number_to_die (type, type_die);
21945 add_name_attribute (type_die, type_tag (type));
21946 if ((dwarf_version >= 4 || !dwarf_strict)
21947 && ENUM_IS_SCOPED (type))
21948 add_AT_flag (type_die, DW_AT_enum_class, 1);
21949 if (ENUM_IS_OPAQUE (type) && TYPE_SIZE (type))
21950 add_AT_flag (type_die, DW_AT_declaration, 1);
21951 if (!dwarf_strict)
21952 add_AT_unsigned (type_die, DW_AT_encoding,
21953 TYPE_UNSIGNED (type)
21954 ? DW_ATE_unsigned
21955 : DW_ATE_signed);
21957 else if (! TYPE_SIZE (type) || ENUM_IS_OPAQUE (type))
21958 return type_die;
21959 else
21960 remove_AT (type_die, DW_AT_declaration);
21962 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21963 given enum type is incomplete, do not generate the DW_AT_byte_size
21964 attribute or the DW_AT_element_list attribute. */
21965 if (TYPE_SIZE (type))
21967 tree link;
21969 if (!ENUM_IS_OPAQUE (type))
21970 TREE_ASM_WRITTEN (type) = 1;
21971 if (!orig_type_die || !get_AT (type_die, DW_AT_byte_size))
21972 add_byte_size_attribute (type_die, type);
21973 if (!orig_type_die || !get_AT (type_die, DW_AT_alignment))
21974 add_alignment_attribute (type_die, type);
21975 if ((dwarf_version >= 3 || !dwarf_strict)
21976 && (!orig_type_die || !get_AT (type_die, DW_AT_type)))
21978 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21979 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21980 context_die);
21982 if (TYPE_STUB_DECL (type) != NULL_TREE)
21984 if (!orig_type_die || !get_AT (type_die, DW_AT_decl_file))
21985 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21986 if (!orig_type_die || !get_AT (type_die, DW_AT_accessibility))
21987 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21990 /* If the first reference to this type was as the return type of an
21991 inline function, then it may not have a parent. Fix this now. */
21992 if (type_die->die_parent == NULL)
21993 add_child_die (scope_die_for (type, context_die), type_die);
21995 for (link = TYPE_VALUES (type);
21996 link != NULL; link = TREE_CHAIN (link))
21998 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21999 tree value = TREE_VALUE (link);
22001 gcc_assert (!ENUM_IS_OPAQUE (type));
22002 add_name_attribute (enum_die,
22003 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
22005 if (TREE_CODE (value) == CONST_DECL)
22006 value = DECL_INITIAL (value);
22008 if (simple_type_size_in_bits (TREE_TYPE (value))
22009 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
22011 /* For constant forms created by add_AT_unsigned DWARF
22012 consumers (GDB, elfutils, etc.) always zero extend
22013 the value. Only when the actual value is negative
22014 do we need to use add_AT_int to generate a constant
22015 form that can represent negative values. */
22016 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
22017 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
22018 add_AT_unsigned (enum_die, DW_AT_const_value,
22019 (unsigned HOST_WIDE_INT) val);
22020 else
22021 add_AT_int (enum_die, DW_AT_const_value, val);
22023 else
22024 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
22025 that here. TODO: This should be re-worked to use correct
22026 signed/unsigned double tags for all cases. */
22027 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
22030 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22031 if (TYPE_ARTIFICIAL (type)
22032 && (!orig_type_die || !get_AT (type_die, DW_AT_artificial)))
22033 add_AT_flag (type_die, DW_AT_artificial, 1);
22035 else
22036 add_AT_flag (type_die, DW_AT_declaration, 1);
22038 add_pubtype (type, type_die);
22040 return type_die;
22043 /* Generate a DIE to represent either a real live formal parameter decl or to
22044 represent just the type of some formal parameter position in some function
22045 type.
22047 Note that this routine is a bit unusual because its argument may be a
22048 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
22049 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
22050 node. If it's the former then this function is being called to output a
22051 DIE to represent a formal parameter object (or some inlining thereof). If
22052 it's the latter, then this function is only being called to output a
22053 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
22054 argument type of some subprogram type.
22055 If EMIT_NAME_P is true, name and source coordinate attributes
22056 are emitted. */
22058 static dw_die_ref
22059 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
22060 dw_die_ref context_die)
22062 tree node_or_origin = node ? node : origin;
22063 tree ultimate_origin;
22064 dw_die_ref parm_die = NULL;
22066 if (DECL_P (node_or_origin))
22068 parm_die = lookup_decl_die (node);
22070 /* If the contexts differ, we may not be talking about the same
22071 thing.
22072 ??? When in LTO the DIE parent is the "abstract" copy and the
22073 context_die is the specification "copy". But this whole block
22074 should eventually be no longer needed. */
22075 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
22077 if (!DECL_ABSTRACT_P (node))
22079 /* This can happen when creating an inlined instance, in
22080 which case we need to create a new DIE that will get
22081 annotated with DW_AT_abstract_origin. */
22082 parm_die = NULL;
22084 else
22085 gcc_unreachable ();
22088 if (parm_die && parm_die->die_parent == NULL)
22090 /* Check that parm_die already has the right attributes that
22091 we would have added below. If any attributes are
22092 missing, fall through to add them. */
22093 if (! DECL_ABSTRACT_P (node_or_origin)
22094 && !get_AT (parm_die, DW_AT_location)
22095 && !get_AT (parm_die, DW_AT_const_value))
22096 /* We are missing location info, and are about to add it. */
22098 else
22100 add_child_die (context_die, parm_die);
22101 return parm_die;
22106 /* If we have a previously generated DIE, use it, unless this is an
22107 concrete instance (origin != NULL), in which case we need a new
22108 DIE with a corresponding DW_AT_abstract_origin. */
22109 bool reusing_die;
22110 if (parm_die && origin == NULL)
22111 reusing_die = true;
22112 else
22114 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
22115 reusing_die = false;
22118 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
22120 case tcc_declaration:
22121 ultimate_origin = decl_ultimate_origin (node_or_origin);
22122 if (node || ultimate_origin)
22123 origin = ultimate_origin;
22125 if (reusing_die)
22126 goto add_location;
22128 if (origin != NULL)
22129 add_abstract_origin_attribute (parm_die, origin);
22130 else if (emit_name_p)
22131 add_name_and_src_coords_attributes (parm_die, node);
22132 if (origin == NULL
22133 || (! DECL_ABSTRACT_P (node_or_origin)
22134 && variably_modified_type_p (TREE_TYPE (node_or_origin),
22135 decl_function_context
22136 (node_or_origin))))
22138 tree type = TREE_TYPE (node_or_origin);
22139 if (decl_by_reference_p (node_or_origin))
22140 add_type_attribute (parm_die, TREE_TYPE (type),
22141 TYPE_UNQUALIFIED,
22142 false, context_die);
22143 else
22144 add_type_attribute (parm_die, type,
22145 decl_quals (node_or_origin),
22146 false, context_die);
22148 if (origin == NULL && DECL_ARTIFICIAL (node))
22149 add_AT_flag (parm_die, DW_AT_artificial, 1);
22150 add_location:
22151 if (node && node != origin)
22152 equate_decl_number_to_die (node, parm_die);
22153 if (! DECL_ABSTRACT_P (node_or_origin))
22154 add_location_or_const_value_attribute (parm_die, node_or_origin,
22155 node == NULL);
22157 break;
22159 case tcc_type:
22160 /* We were called with some kind of a ..._TYPE node. */
22161 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
22162 context_die);
22163 break;
22165 default:
22166 gcc_unreachable ();
22169 return parm_die;
22172 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
22173 children DW_TAG_formal_parameter DIEs representing the arguments of the
22174 parameter pack.
22176 PARM_PACK must be a function parameter pack.
22177 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
22178 must point to the subsequent arguments of the function PACK_ARG belongs to.
22179 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
22180 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
22181 following the last one for which a DIE was generated. */
22183 static dw_die_ref
22184 gen_formal_parameter_pack_die (tree parm_pack,
22185 tree pack_arg,
22186 dw_die_ref subr_die,
22187 tree *next_arg)
22189 tree arg;
22190 dw_die_ref parm_pack_die;
22192 gcc_assert (parm_pack
22193 && lang_hooks.function_parameter_pack_p (parm_pack)
22194 && subr_die);
22196 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
22197 add_src_coords_attributes (parm_pack_die, parm_pack);
22199 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
22201 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
22202 parm_pack))
22203 break;
22204 gen_formal_parameter_die (arg, NULL,
22205 false /* Don't emit name attribute. */,
22206 parm_pack_die);
22208 if (next_arg)
22209 *next_arg = arg;
22210 return parm_pack_die;
22213 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
22214 at the end of an (ANSI prototyped) formal parameters list. */
22216 static void
22217 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
22219 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
22222 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
22223 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
22224 parameters as specified in some function type specification (except for
22225 those which appear as part of a function *definition*). */
22227 static void
22228 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
22230 tree link;
22231 tree formal_type = NULL;
22232 tree first_parm_type;
22233 tree arg;
22235 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
22237 arg = DECL_ARGUMENTS (function_or_method_type);
22238 function_or_method_type = TREE_TYPE (function_or_method_type);
22240 else
22241 arg = NULL_TREE;
22243 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
22245 /* Make our first pass over the list of formal parameter types and output a
22246 DW_TAG_formal_parameter DIE for each one. */
22247 for (link = first_parm_type; link; )
22249 dw_die_ref parm_die;
22251 formal_type = TREE_VALUE (link);
22252 if (formal_type == void_type_node)
22253 break;
22255 /* Output a (nameless) DIE to represent the formal parameter itself. */
22256 if (!POINTER_BOUNDS_TYPE_P (formal_type))
22258 parm_die = gen_formal_parameter_die (formal_type, NULL,
22259 true /* Emit name attribute. */,
22260 context_die);
22261 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
22262 && link == first_parm_type)
22264 add_AT_flag (parm_die, DW_AT_artificial, 1);
22265 if (dwarf_version >= 3 || !dwarf_strict)
22266 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
22268 else if (arg && DECL_ARTIFICIAL (arg))
22269 add_AT_flag (parm_die, DW_AT_artificial, 1);
22272 link = TREE_CHAIN (link);
22273 if (arg)
22274 arg = DECL_CHAIN (arg);
22277 /* If this function type has an ellipsis, add a
22278 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
22279 if (formal_type != void_type_node)
22280 gen_unspecified_parameters_die (function_or_method_type, context_die);
22282 /* Make our second (and final) pass over the list of formal parameter types
22283 and output DIEs to represent those types (as necessary). */
22284 for (link = TYPE_ARG_TYPES (function_or_method_type);
22285 link && TREE_VALUE (link);
22286 link = TREE_CHAIN (link))
22287 gen_type_die (TREE_VALUE (link), context_die);
22290 /* We want to generate the DIE for TYPE so that we can generate the
22291 die for MEMBER, which has been defined; we will need to refer back
22292 to the member declaration nested within TYPE. If we're trying to
22293 generate minimal debug info for TYPE, processing TYPE won't do the
22294 trick; we need to attach the member declaration by hand. */
22296 static void
22297 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
22299 gen_type_die (type, context_die);
22301 /* If we're trying to avoid duplicate debug info, we may not have
22302 emitted the member decl for this function. Emit it now. */
22303 if (TYPE_STUB_DECL (type)
22304 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
22305 && ! lookup_decl_die (member))
22307 dw_die_ref type_die;
22308 gcc_assert (!decl_ultimate_origin (member));
22310 push_decl_scope (type);
22311 type_die = lookup_type_die_strip_naming_typedef (type);
22312 if (TREE_CODE (member) == FUNCTION_DECL)
22313 gen_subprogram_die (member, type_die);
22314 else if (TREE_CODE (member) == FIELD_DECL)
22316 /* Ignore the nameless fields that are used to skip bits but handle
22317 C++ anonymous unions and structs. */
22318 if (DECL_NAME (member) != NULL_TREE
22319 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
22320 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
22322 struct vlr_context vlr_ctx = {
22323 DECL_CONTEXT (member), /* struct_type */
22324 NULL_TREE /* variant_part_offset */
22326 gen_type_die (member_declared_type (member), type_die);
22327 gen_field_die (member, &vlr_ctx, type_die);
22330 else
22331 gen_variable_die (member, NULL_TREE, type_die);
22333 pop_decl_scope ();
22337 /* Forward declare these functions, because they are mutually recursive
22338 with their set_block_* pairing functions. */
22339 static void set_decl_origin_self (tree);
22341 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
22342 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
22343 that it points to the node itself, thus indicating that the node is its
22344 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
22345 the given node is NULL, recursively descend the decl/block tree which
22346 it is the root of, and for each other ..._DECL or BLOCK node contained
22347 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
22348 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
22349 values to point to themselves. */
22351 static void
22352 set_block_origin_self (tree stmt)
22354 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
22356 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
22359 tree local_decl;
22361 for (local_decl = BLOCK_VARS (stmt);
22362 local_decl != NULL_TREE;
22363 local_decl = DECL_CHAIN (local_decl))
22364 /* Do not recurse on nested functions since the inlining status
22365 of parent and child can be different as per the DWARF spec. */
22366 if (TREE_CODE (local_decl) != FUNCTION_DECL
22367 && !DECL_EXTERNAL (local_decl))
22368 set_decl_origin_self (local_decl);
22372 tree subblock;
22374 for (subblock = BLOCK_SUBBLOCKS (stmt);
22375 subblock != NULL_TREE;
22376 subblock = BLOCK_CHAIN (subblock))
22377 set_block_origin_self (subblock); /* Recurse. */
22382 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
22383 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
22384 node to so that it points to the node itself, thus indicating that the
22385 node represents its own (abstract) origin. Additionally, if the
22386 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
22387 the decl/block tree of which the given node is the root of, and for
22388 each other ..._DECL or BLOCK node contained therein whose
22389 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
22390 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
22391 point to themselves. */
22393 static void
22394 set_decl_origin_self (tree decl)
22396 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
22398 DECL_ABSTRACT_ORIGIN (decl) = decl;
22399 if (TREE_CODE (decl) == FUNCTION_DECL)
22401 tree arg;
22403 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
22404 DECL_ABSTRACT_ORIGIN (arg) = arg;
22405 if (DECL_INITIAL (decl) != NULL_TREE
22406 && DECL_INITIAL (decl) != error_mark_node)
22407 set_block_origin_self (DECL_INITIAL (decl));
22412 /* Mark the early DIE for DECL as the abstract instance. */
22414 static void
22415 dwarf2out_abstract_function (tree decl)
22417 dw_die_ref old_die;
22419 /* Make sure we have the actual abstract inline, not a clone. */
22420 decl = DECL_ORIGIN (decl);
22422 if (DECL_IGNORED_P (decl))
22423 return;
22425 old_die = lookup_decl_die (decl);
22426 /* With early debug we always have an old DIE unless we are in LTO
22427 and the user did not compile but only link with debug. */
22428 if (in_lto_p && ! old_die)
22429 return;
22430 gcc_assert (old_die != NULL);
22431 if (get_AT (old_die, DW_AT_inline)
22432 || get_AT (old_die, DW_AT_abstract_origin))
22433 /* We've already generated the abstract instance. */
22434 return;
22436 /* Go ahead and put DW_AT_inline on the DIE. */
22437 if (DECL_DECLARED_INLINE_P (decl))
22439 if (cgraph_function_possibly_inlined_p (decl))
22440 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
22441 else
22442 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
22444 else
22446 if (cgraph_function_possibly_inlined_p (decl))
22447 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
22448 else
22449 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
22452 if (DECL_DECLARED_INLINE_P (decl)
22453 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
22454 add_AT_flag (old_die, DW_AT_artificial, 1);
22456 set_decl_origin_self (decl);
22459 /* Helper function of premark_used_types() which gets called through
22460 htab_traverse.
22462 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22463 marked as unused by prune_unused_types. */
22465 bool
22466 premark_used_types_helper (tree const &type, void *)
22468 dw_die_ref die;
22470 die = lookup_type_die (type);
22471 if (die != NULL)
22472 die->die_perennial_p = 1;
22473 return true;
22476 /* Helper function of premark_types_used_by_global_vars which gets called
22477 through htab_traverse.
22479 Marks the DIE of a given type in *SLOT as perennial, so it never gets
22480 marked as unused by prune_unused_types. The DIE of the type is marked
22481 only if the global variable using the type will actually be emitted. */
22484 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
22485 void *)
22487 struct types_used_by_vars_entry *entry;
22488 dw_die_ref die;
22490 entry = (struct types_used_by_vars_entry *) *slot;
22491 gcc_assert (entry->type != NULL
22492 && entry->var_decl != NULL);
22493 die = lookup_type_die (entry->type);
22494 if (die)
22496 /* Ask cgraph if the global variable really is to be emitted.
22497 If yes, then we'll keep the DIE of ENTRY->TYPE. */
22498 varpool_node *node = varpool_node::get (entry->var_decl);
22499 if (node && node->definition)
22501 die->die_perennial_p = 1;
22502 /* Keep the parent DIEs as well. */
22503 while ((die = die->die_parent) && die->die_perennial_p == 0)
22504 die->die_perennial_p = 1;
22507 return 1;
22510 /* Mark all members of used_types_hash as perennial. */
22512 static void
22513 premark_used_types (struct function *fun)
22515 if (fun && fun->used_types_hash)
22516 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
22519 /* Mark all members of types_used_by_vars_entry as perennial. */
22521 static void
22522 premark_types_used_by_global_vars (void)
22524 if (types_used_by_vars_hash)
22525 types_used_by_vars_hash
22526 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
22529 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
22530 for CA_LOC call arg loc node. */
22532 static dw_die_ref
22533 gen_call_site_die (tree decl, dw_die_ref subr_die,
22534 struct call_arg_loc_node *ca_loc)
22536 dw_die_ref stmt_die = NULL, die;
22537 tree block = ca_loc->block;
22539 while (block
22540 && block != DECL_INITIAL (decl)
22541 && TREE_CODE (block) == BLOCK)
22543 stmt_die = BLOCK_DIE (block);
22544 if (stmt_die)
22545 break;
22546 block = BLOCK_SUPERCONTEXT (block);
22548 if (stmt_die == NULL)
22549 stmt_die = subr_die;
22550 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
22551 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
22552 if (ca_loc->tail_call_p)
22553 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
22554 if (ca_loc->symbol_ref)
22556 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
22557 if (tdie)
22558 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
22559 else
22560 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
22561 false);
22563 return die;
22566 /* Generate a DIE to represent a declared function (either file-scope or
22567 block-local). */
22569 static void
22570 gen_subprogram_die (tree decl, dw_die_ref context_die)
22572 tree origin = decl_ultimate_origin (decl);
22573 dw_die_ref subr_die;
22574 dw_die_ref old_die = lookup_decl_die (decl);
22576 /* This function gets called multiple times for different stages of
22577 the debug process. For example, for func() in this code:
22579 namespace S
22581 void func() { ... }
22584 ...we get called 4 times. Twice in early debug and twice in
22585 late debug:
22587 Early debug
22588 -----------
22590 1. Once while generating func() within the namespace. This is
22591 the declaration. The declaration bit below is set, as the
22592 context is the namespace.
22594 A new DIE will be generated with DW_AT_declaration set.
22596 2. Once for func() itself. This is the specification. The
22597 declaration bit below is clear as the context is the CU.
22599 We will use the cached DIE from (1) to create a new DIE with
22600 DW_AT_specification pointing to the declaration in (1).
22602 Late debug via rest_of_handle_final()
22603 -------------------------------------
22605 3. Once generating func() within the namespace. This is also the
22606 declaration, as in (1), but this time we will early exit below
22607 as we have a cached DIE and a declaration needs no additional
22608 annotations (no locations), as the source declaration line
22609 info is enough.
22611 4. Once for func() itself. As in (2), this is the specification,
22612 but this time we will re-use the cached DIE, and just annotate
22613 it with the location information that should now be available.
22615 For something without namespaces, but with abstract instances, we
22616 are also called a multiple times:
22618 class Base
22620 public:
22621 Base (); // constructor declaration (1)
22624 Base::Base () { } // constructor specification (2)
22626 Early debug
22627 -----------
22629 1. Once for the Base() constructor by virtue of it being a
22630 member of the Base class. This is done via
22631 rest_of_type_compilation.
22633 This is a declaration, so a new DIE will be created with
22634 DW_AT_declaration.
22636 2. Once for the Base() constructor definition, but this time
22637 while generating the abstract instance of the base
22638 constructor (__base_ctor) which is being generated via early
22639 debug of reachable functions.
22641 Even though we have a cached version of the declaration (1),
22642 we will create a DW_AT_specification of the declaration DIE
22643 in (1).
22645 3. Once for the __base_ctor itself, but this time, we generate
22646 an DW_AT_abstract_origin version of the DW_AT_specification in
22647 (2).
22649 Late debug via rest_of_handle_final
22650 -----------------------------------
22652 4. One final time for the __base_ctor (which will have a cached
22653 DIE with DW_AT_abstract_origin created in (3). This time,
22654 we will just annotate the location information now
22655 available.
22657 int declaration = (current_function_decl != decl
22658 || class_or_namespace_scope_p (context_die));
22660 /* A declaration that has been previously dumped needs no
22661 additional information. */
22662 if (old_die && declaration)
22663 return;
22665 /* Now that the C++ front end lazily declares artificial member fns, we
22666 might need to retrofit the declaration into its class. */
22667 if (!declaration && !origin && !old_die
22668 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22669 && !class_or_namespace_scope_p (context_die)
22670 && debug_info_level > DINFO_LEVEL_TERSE)
22671 old_die = force_decl_die (decl);
22673 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22674 if (origin != NULL)
22676 gcc_assert (!declaration || local_scope_p (context_die));
22678 /* Fixup die_parent for the abstract instance of a nested
22679 inline function. */
22680 if (old_die && old_die->die_parent == NULL)
22681 add_child_die (context_die, old_die);
22683 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22685 /* If we have a DW_AT_abstract_origin we have a working
22686 cached version. */
22687 subr_die = old_die;
22689 else
22691 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22692 add_abstract_origin_attribute (subr_die, origin);
22693 /* This is where the actual code for a cloned function is.
22694 Let's emit linkage name attribute for it. This helps
22695 debuggers to e.g, set breakpoints into
22696 constructors/destructors when the user asks "break
22697 K::K". */
22698 add_linkage_name (subr_die, decl);
22701 /* A cached copy, possibly from early dwarf generation. Reuse as
22702 much as possible. */
22703 else if (old_die)
22705 if (!get_AT_flag (old_die, DW_AT_declaration)
22706 /* We can have a normal definition following an inline one in the
22707 case of redefinition of GNU C extern inlines.
22708 It seems reasonable to use AT_specification in this case. */
22709 && !get_AT (old_die, DW_AT_inline))
22711 /* Detect and ignore this case, where we are trying to output
22712 something we have already output. */
22713 if (get_AT (old_die, DW_AT_low_pc)
22714 || get_AT (old_die, DW_AT_ranges))
22715 return;
22717 /* If we have no location information, this must be a
22718 partially generated DIE from early dwarf generation.
22719 Fall through and generate it. */
22722 /* If the definition comes from the same place as the declaration,
22723 maybe use the old DIE. We always want the DIE for this function
22724 that has the *_pc attributes to be under comp_unit_die so the
22725 debugger can find it. We also need to do this for abstract
22726 instances of inlines, since the spec requires the out-of-line copy
22727 to have the same parent. For local class methods, this doesn't
22728 apply; we just use the old DIE. */
22729 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22730 struct dwarf_file_data * file_index = lookup_filename (s.file);
22731 if ((is_cu_die (old_die->die_parent)
22732 /* This condition fixes the inconsistency/ICE with the
22733 following Fortran test (or some derivative thereof) while
22734 building libgfortran:
22736 module some_m
22737 contains
22738 logical function funky (FLAG)
22739 funky = .true.
22740 end function
22741 end module
22743 || (old_die->die_parent
22744 && old_die->die_parent->die_tag == DW_TAG_module)
22745 || context_die == NULL)
22746 && (DECL_ARTIFICIAL (decl)
22747 /* The location attributes may be in the abstract origin
22748 which in the case of LTO might be not available to
22749 look at. */
22750 || get_AT (old_die, DW_AT_abstract_origin)
22751 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22752 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22753 == (unsigned) s.line)
22754 && (!debug_column_info
22755 || s.column == 0
22756 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22757 == (unsigned) s.column)))))
22759 subr_die = old_die;
22761 /* Clear out the declaration attribute, but leave the
22762 parameters so they can be augmented with location
22763 information later. Unless this was a declaration, in
22764 which case, wipe out the nameless parameters and recreate
22765 them further down. */
22766 if (remove_AT (subr_die, DW_AT_declaration))
22769 remove_AT (subr_die, DW_AT_object_pointer);
22770 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22773 /* Make a specification pointing to the previously built
22774 declaration. */
22775 else
22777 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22778 add_AT_specification (subr_die, old_die);
22779 add_pubname (decl, subr_die);
22780 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22781 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22782 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22783 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22784 if (debug_column_info
22785 && s.column
22786 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22787 != (unsigned) s.column))
22788 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22790 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22791 emit the real type on the definition die. */
22792 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22794 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22795 if (die == auto_die || die == decltype_auto_die)
22796 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22797 TYPE_UNQUALIFIED, false, context_die);
22800 /* When we process the method declaration, we haven't seen
22801 the out-of-class defaulted definition yet, so we have to
22802 recheck now. */
22803 if ((dwarf_version >= 5 || ! dwarf_strict)
22804 && !get_AT (subr_die, DW_AT_defaulted))
22806 int defaulted
22807 = lang_hooks.decls.decl_dwarf_attribute (decl,
22808 DW_AT_defaulted);
22809 if (defaulted != -1)
22811 /* Other values must have been handled before. */
22812 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22813 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22818 /* Create a fresh DIE for anything else. */
22819 else
22821 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22823 if (TREE_PUBLIC (decl))
22824 add_AT_flag (subr_die, DW_AT_external, 1);
22826 add_name_and_src_coords_attributes (subr_die, decl);
22827 add_pubname (decl, subr_die);
22828 if (debug_info_level > DINFO_LEVEL_TERSE)
22830 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22831 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22832 TYPE_UNQUALIFIED, false, context_die);
22835 add_pure_or_virtual_attribute (subr_die, decl);
22836 if (DECL_ARTIFICIAL (decl))
22837 add_AT_flag (subr_die, DW_AT_artificial, 1);
22839 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22840 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22842 add_alignment_attribute (subr_die, decl);
22844 add_accessibility_attribute (subr_die, decl);
22847 /* Unless we have an existing non-declaration DIE, equate the new
22848 DIE. */
22849 if (!old_die || is_declaration_die (old_die))
22850 equate_decl_number_to_die (decl, subr_die);
22852 if (declaration)
22854 if (!old_die || !get_AT (old_die, DW_AT_inline))
22856 add_AT_flag (subr_die, DW_AT_declaration, 1);
22858 /* If this is an explicit function declaration then generate
22859 a DW_AT_explicit attribute. */
22860 if ((dwarf_version >= 3 || !dwarf_strict)
22861 && lang_hooks.decls.decl_dwarf_attribute (decl,
22862 DW_AT_explicit) == 1)
22863 add_AT_flag (subr_die, DW_AT_explicit, 1);
22865 /* If this is a C++11 deleted special function member then generate
22866 a DW_AT_deleted attribute. */
22867 if ((dwarf_version >= 5 || !dwarf_strict)
22868 && lang_hooks.decls.decl_dwarf_attribute (decl,
22869 DW_AT_deleted) == 1)
22870 add_AT_flag (subr_die, DW_AT_deleted, 1);
22872 /* If this is a C++11 defaulted special function member then
22873 generate a DW_AT_defaulted attribute. */
22874 if (dwarf_version >= 5 || !dwarf_strict)
22876 int defaulted
22877 = lang_hooks.decls.decl_dwarf_attribute (decl,
22878 DW_AT_defaulted);
22879 if (defaulted != -1)
22880 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22883 /* If this is a C++11 non-static member function with & ref-qualifier
22884 then generate a DW_AT_reference attribute. */
22885 if ((dwarf_version >= 5 || !dwarf_strict)
22886 && lang_hooks.decls.decl_dwarf_attribute (decl,
22887 DW_AT_reference) == 1)
22888 add_AT_flag (subr_die, DW_AT_reference, 1);
22890 /* If this is a C++11 non-static member function with &&
22891 ref-qualifier then generate a DW_AT_reference attribute. */
22892 if ((dwarf_version >= 5 || !dwarf_strict)
22893 && lang_hooks.decls.decl_dwarf_attribute (decl,
22894 DW_AT_rvalue_reference)
22895 == 1)
22896 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22899 /* For non DECL_EXTERNALs, if range information is available, fill
22900 the DIE with it. */
22901 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22903 HOST_WIDE_INT cfa_fb_offset;
22905 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22907 if (!crtl->has_bb_partition)
22909 dw_fde_ref fde = fun->fde;
22910 if (fde->dw_fde_begin)
22912 /* We have already generated the labels. */
22913 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22914 fde->dw_fde_end, false);
22916 else
22918 /* Create start/end labels and add the range. */
22919 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22920 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22921 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22922 current_function_funcdef_no);
22923 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22924 current_function_funcdef_no);
22925 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22926 false);
22929 #if VMS_DEBUGGING_INFO
22930 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22931 Section 2.3 Prologue and Epilogue Attributes:
22932 When a breakpoint is set on entry to a function, it is generally
22933 desirable for execution to be suspended, not on the very first
22934 instruction of the function, but rather at a point after the
22935 function's frame has been set up, after any language defined local
22936 declaration processing has been completed, and before execution of
22937 the first statement of the function begins. Debuggers generally
22938 cannot properly determine where this point is. Similarly for a
22939 breakpoint set on exit from a function. The prologue and epilogue
22940 attributes allow a compiler to communicate the location(s) to use. */
22943 if (fde->dw_fde_vms_end_prologue)
22944 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22945 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22947 if (fde->dw_fde_vms_begin_epilogue)
22948 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22949 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22951 #endif
22954 else
22956 /* Generate pubnames entries for the split function code ranges. */
22957 dw_fde_ref fde = fun->fde;
22959 if (fde->dw_fde_second_begin)
22961 if (dwarf_version >= 3 || !dwarf_strict)
22963 /* We should use ranges for non-contiguous code section
22964 addresses. Use the actual code range for the initial
22965 section, since the HOT/COLD labels might precede an
22966 alignment offset. */
22967 bool range_list_added = false;
22968 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22969 fde->dw_fde_end, &range_list_added,
22970 false);
22971 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22972 fde->dw_fde_second_end,
22973 &range_list_added, false);
22974 if (range_list_added)
22975 add_ranges (NULL);
22977 else
22979 /* There is no real support in DW2 for this .. so we make
22980 a work-around. First, emit the pub name for the segment
22981 containing the function label. Then make and emit a
22982 simplified subprogram DIE for the second segment with the
22983 name pre-fixed by __hot/cold_sect_of_. We use the same
22984 linkage name for the second die so that gdb will find both
22985 sections when given "b foo". */
22986 const char *name = NULL;
22987 tree decl_name = DECL_NAME (decl);
22988 dw_die_ref seg_die;
22990 /* Do the 'primary' section. */
22991 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22992 fde->dw_fde_end, false);
22994 /* Build a minimal DIE for the secondary section. */
22995 seg_die = new_die (DW_TAG_subprogram,
22996 subr_die->die_parent, decl);
22998 if (TREE_PUBLIC (decl))
22999 add_AT_flag (seg_die, DW_AT_external, 1);
23001 if (decl_name != NULL
23002 && IDENTIFIER_POINTER (decl_name) != NULL)
23004 name = dwarf2_name (decl, 1);
23005 if (! DECL_ARTIFICIAL (decl))
23006 add_src_coords_attributes (seg_die, decl);
23008 add_linkage_name (seg_die, decl);
23010 gcc_assert (name != NULL);
23011 add_pure_or_virtual_attribute (seg_die, decl);
23012 if (DECL_ARTIFICIAL (decl))
23013 add_AT_flag (seg_die, DW_AT_artificial, 1);
23015 name = concat ("__second_sect_of_", name, NULL);
23016 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
23017 fde->dw_fde_second_end, false);
23018 add_name_attribute (seg_die, name);
23019 if (want_pubnames ())
23020 add_pubname_string (name, seg_die);
23023 else
23024 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
23025 false);
23028 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
23030 /* We define the "frame base" as the function's CFA. This is more
23031 convenient for several reasons: (1) It's stable across the prologue
23032 and epilogue, which makes it better than just a frame pointer,
23033 (2) With dwarf3, there exists a one-byte encoding that allows us
23034 to reference the .debug_frame data by proxy, but failing that,
23035 (3) We can at least reuse the code inspection and interpretation
23036 code that determines the CFA position at various points in the
23037 function. */
23038 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
23040 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
23041 add_AT_loc (subr_die, DW_AT_frame_base, op);
23043 else
23045 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
23046 if (list->dw_loc_next)
23047 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
23048 else
23049 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
23052 /* Compute a displacement from the "steady-state frame pointer" to
23053 the CFA. The former is what all stack slots and argument slots
23054 will reference in the rtl; the latter is what we've told the
23055 debugger about. We'll need to adjust all frame_base references
23056 by this displacement. */
23057 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
23059 if (fun->static_chain_decl)
23061 /* DWARF requires here a location expression that computes the
23062 address of the enclosing subprogram's frame base. The machinery
23063 in tree-nested.c is supposed to store this specific address in the
23064 last field of the FRAME record. */
23065 const tree frame_type
23066 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
23067 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
23069 tree fb_expr
23070 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
23071 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
23072 fb_expr, fb_decl, NULL_TREE);
23074 add_AT_location_description (subr_die, DW_AT_static_link,
23075 loc_list_from_tree (fb_expr, 0, NULL));
23078 resolve_variable_values ();
23081 /* Generate child dies for template paramaters. */
23082 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
23083 gen_generic_params_dies (decl);
23085 /* Now output descriptions of the arguments for this function. This gets
23086 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
23087 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
23088 `...' at the end of the formal parameter list. In order to find out if
23089 there was a trailing ellipsis or not, we must instead look at the type
23090 associated with the FUNCTION_DECL. This will be a node of type
23091 FUNCTION_TYPE. If the chain of type nodes hanging off of this
23092 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
23093 an ellipsis at the end. */
23095 /* In the case where we are describing a mere function declaration, all we
23096 need to do here (and all we *can* do here) is to describe the *types* of
23097 its formal parameters. */
23098 if (debug_info_level <= DINFO_LEVEL_TERSE)
23100 else if (declaration)
23101 gen_formal_types_die (decl, subr_die);
23102 else
23104 /* Generate DIEs to represent all known formal parameters. */
23105 tree parm = DECL_ARGUMENTS (decl);
23106 tree generic_decl = early_dwarf
23107 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
23108 tree generic_decl_parm = generic_decl
23109 ? DECL_ARGUMENTS (generic_decl)
23110 : NULL;
23112 /* Now we want to walk the list of parameters of the function and
23113 emit their relevant DIEs.
23115 We consider the case of DECL being an instance of a generic function
23116 as well as it being a normal function.
23118 If DECL is an instance of a generic function we walk the
23119 parameters of the generic function declaration _and_ the parameters of
23120 DECL itself. This is useful because we want to emit specific DIEs for
23121 function parameter packs and those are declared as part of the
23122 generic function declaration. In that particular case,
23123 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
23124 That DIE has children DIEs representing the set of arguments
23125 of the pack. Note that the set of pack arguments can be empty.
23126 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
23127 children DIE.
23129 Otherwise, we just consider the parameters of DECL. */
23130 while (generic_decl_parm || parm)
23132 if (generic_decl_parm
23133 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
23134 gen_formal_parameter_pack_die (generic_decl_parm,
23135 parm, subr_die,
23136 &parm);
23137 else if (parm && !POINTER_BOUNDS_P (parm))
23139 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
23141 if (early_dwarf
23142 && parm == DECL_ARGUMENTS (decl)
23143 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
23144 && parm_die
23145 && (dwarf_version >= 3 || !dwarf_strict))
23146 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
23148 parm = DECL_CHAIN (parm);
23150 else if (parm)
23151 parm = DECL_CHAIN (parm);
23153 if (generic_decl_parm)
23154 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
23157 /* Decide whether we need an unspecified_parameters DIE at the end.
23158 There are 2 more cases to do this for: 1) the ansi ... declaration -
23159 this is detectable when the end of the arg list is not a
23160 void_type_node 2) an unprototyped function declaration (not a
23161 definition). This just means that we have no info about the
23162 parameters at all. */
23163 if (early_dwarf)
23165 if (prototype_p (TREE_TYPE (decl)))
23167 /* This is the prototyped case, check for.... */
23168 if (stdarg_p (TREE_TYPE (decl)))
23169 gen_unspecified_parameters_die (decl, subr_die);
23171 else if (DECL_INITIAL (decl) == NULL_TREE)
23172 gen_unspecified_parameters_die (decl, subr_die);
23176 if (subr_die != old_die)
23177 /* Add the calling convention attribute if requested. */
23178 add_calling_convention_attribute (subr_die, decl);
23180 /* Output Dwarf info for all of the stuff within the body of the function
23181 (if it has one - it may be just a declaration).
23183 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
23184 a function. This BLOCK actually represents the outermost binding contour
23185 for the function, i.e. the contour in which the function's formal
23186 parameters and labels get declared. Curiously, it appears that the front
23187 end doesn't actually put the PARM_DECL nodes for the current function onto
23188 the BLOCK_VARS list for this outer scope, but are strung off of the
23189 DECL_ARGUMENTS list for the function instead.
23191 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
23192 the LABEL_DECL nodes for the function however, and we output DWARF info
23193 for those in decls_for_scope. Just within the `outer_scope' there will be
23194 a BLOCK node representing the function's outermost pair of curly braces,
23195 and any blocks used for the base and member initializers of a C++
23196 constructor function. */
23197 tree outer_scope = DECL_INITIAL (decl);
23198 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
23200 int call_site_note_count = 0;
23201 int tail_call_site_note_count = 0;
23203 /* Emit a DW_TAG_variable DIE for a named return value. */
23204 if (DECL_NAME (DECL_RESULT (decl)))
23205 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
23207 /* The first time through decls_for_scope we will generate the
23208 DIEs for the locals. The second time, we fill in the
23209 location info. */
23210 decls_for_scope (outer_scope, subr_die);
23212 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
23214 struct call_arg_loc_node *ca_loc;
23215 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
23217 dw_die_ref die = NULL;
23218 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
23219 rtx arg, next_arg;
23221 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
23222 ? XEXP (ca_loc->call_arg_loc_note, 0)
23223 : NULL_RTX);
23224 arg; arg = next_arg)
23226 dw_loc_descr_ref reg, val;
23227 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
23228 dw_die_ref cdie, tdie = NULL;
23230 next_arg = XEXP (arg, 1);
23231 if (REG_P (XEXP (XEXP (arg, 0), 0))
23232 && next_arg
23233 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
23234 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
23235 && REGNO (XEXP (XEXP (arg, 0), 0))
23236 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
23237 next_arg = XEXP (next_arg, 1);
23238 if (mode == VOIDmode)
23240 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
23241 if (mode == VOIDmode)
23242 mode = GET_MODE (XEXP (arg, 0));
23244 if (mode == VOIDmode || mode == BLKmode)
23245 continue;
23246 /* Get dynamic information about call target only if we
23247 have no static information: we cannot generate both
23248 DW_AT_call_origin and DW_AT_call_target
23249 attributes. */
23250 if (ca_loc->symbol_ref == NULL_RTX)
23252 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
23254 tloc = XEXP (XEXP (arg, 0), 1);
23255 continue;
23257 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
23258 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
23260 tlocc = XEXP (XEXP (arg, 0), 1);
23261 continue;
23264 reg = NULL;
23265 if (REG_P (XEXP (XEXP (arg, 0), 0)))
23266 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
23267 VAR_INIT_STATUS_INITIALIZED);
23268 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
23270 rtx mem = XEXP (XEXP (arg, 0), 0);
23271 reg = mem_loc_descriptor (XEXP (mem, 0),
23272 get_address_mode (mem),
23273 GET_MODE (mem),
23274 VAR_INIT_STATUS_INITIALIZED);
23276 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
23277 == DEBUG_PARAMETER_REF)
23279 tree tdecl
23280 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
23281 tdie = lookup_decl_die (tdecl);
23282 if (tdie == NULL)
23283 continue;
23285 else
23286 continue;
23287 if (reg == NULL
23288 && GET_CODE (XEXP (XEXP (arg, 0), 0))
23289 != DEBUG_PARAMETER_REF)
23290 continue;
23291 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
23292 VOIDmode,
23293 VAR_INIT_STATUS_INITIALIZED);
23294 if (val == NULL)
23295 continue;
23296 if (die == NULL)
23297 die = gen_call_site_die (decl, subr_die, ca_loc);
23298 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
23299 NULL_TREE);
23300 if (reg != NULL)
23301 add_AT_loc (cdie, DW_AT_location, reg);
23302 else if (tdie != NULL)
23303 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
23304 tdie);
23305 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
23306 if (next_arg != XEXP (arg, 1))
23308 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
23309 if (mode == VOIDmode)
23310 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
23311 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
23312 0), 1),
23313 mode, VOIDmode,
23314 VAR_INIT_STATUS_INITIALIZED);
23315 if (val != NULL)
23316 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
23317 val);
23320 if (die == NULL
23321 && (ca_loc->symbol_ref || tloc))
23322 die = gen_call_site_die (decl, subr_die, ca_loc);
23323 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
23325 dw_loc_descr_ref tval = NULL;
23327 if (tloc != NULL_RTX)
23328 tval = mem_loc_descriptor (tloc,
23329 GET_MODE (tloc) == VOIDmode
23330 ? Pmode : GET_MODE (tloc),
23331 VOIDmode,
23332 VAR_INIT_STATUS_INITIALIZED);
23333 if (tval)
23334 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
23335 else if (tlocc != NULL_RTX)
23337 tval = mem_loc_descriptor (tlocc,
23338 GET_MODE (tlocc) == VOIDmode
23339 ? Pmode : GET_MODE (tlocc),
23340 VOIDmode,
23341 VAR_INIT_STATUS_INITIALIZED);
23342 if (tval)
23343 add_AT_loc (die,
23344 dwarf_AT (DW_AT_call_target_clobbered),
23345 tval);
23348 if (die != NULL)
23350 call_site_note_count++;
23351 if (ca_loc->tail_call_p)
23352 tail_call_site_note_count++;
23356 call_arg_locations = NULL;
23357 call_arg_loc_last = NULL;
23358 if (tail_call_site_count >= 0
23359 && tail_call_site_count == tail_call_site_note_count
23360 && (!dwarf_strict || dwarf_version >= 5))
23362 if (call_site_count >= 0
23363 && call_site_count == call_site_note_count)
23364 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
23365 else
23366 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
23368 call_site_count = -1;
23369 tail_call_site_count = -1;
23372 /* Mark used types after we have created DIEs for the functions scopes. */
23373 premark_used_types (DECL_STRUCT_FUNCTION (decl));
23376 /* Returns a hash value for X (which really is a die_struct). */
23378 hashval_t
23379 block_die_hasher::hash (die_struct *d)
23381 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
23384 /* Return nonzero if decl_id and die_parent of die_struct X is the same
23385 as decl_id and die_parent of die_struct Y. */
23387 bool
23388 block_die_hasher::equal (die_struct *x, die_struct *y)
23390 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
23393 /* Hold information about markers for inlined entry points. */
23394 struct GTY ((for_user)) inline_entry_data
23396 /* The block that's the inlined_function_outer_scope for an inlined
23397 function. */
23398 tree block;
23400 /* The label at the inlined entry point. */
23401 const char *label_pfx;
23402 unsigned int label_num;
23404 /* The view number to be used as the inlined entry point. */
23405 var_loc_view view;
23408 struct inline_entry_data_hasher : ggc_ptr_hash <inline_entry_data>
23410 typedef tree compare_type;
23411 static inline hashval_t hash (const inline_entry_data *);
23412 static inline bool equal (const inline_entry_data *, const_tree);
23415 /* Hash table routines for inline_entry_data. */
23417 inline hashval_t
23418 inline_entry_data_hasher::hash (const inline_entry_data *data)
23420 return htab_hash_pointer (data->block);
23423 inline bool
23424 inline_entry_data_hasher::equal (const inline_entry_data *data,
23425 const_tree block)
23427 return data->block == block;
23430 /* Inlined entry points pending DIE creation in this compilation unit. */
23432 static GTY(()) hash_table<inline_entry_data_hasher> *inline_entry_data_table;
23435 /* Return TRUE if DECL, which may have been previously generated as
23436 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
23437 true if decl (or its origin) is either an extern declaration or a
23438 class/namespace scoped declaration.
23440 The declare_in_namespace support causes us to get two DIEs for one
23441 variable, both of which are declarations. We want to avoid
23442 considering one to be a specification, so we must test for
23443 DECLARATION and DW_AT_declaration. */
23444 static inline bool
23445 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
23447 return (old_die && TREE_STATIC (decl) && !declaration
23448 && get_AT_flag (old_die, DW_AT_declaration) == 1);
23451 /* Return true if DECL is a local static. */
23453 static inline bool
23454 local_function_static (tree decl)
23456 gcc_assert (VAR_P (decl));
23457 return TREE_STATIC (decl)
23458 && DECL_CONTEXT (decl)
23459 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
23462 /* Generate a DIE to represent a declared data object.
23463 Either DECL or ORIGIN must be non-null. */
23465 static void
23466 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
23468 HOST_WIDE_INT off = 0;
23469 tree com_decl;
23470 tree decl_or_origin = decl ? decl : origin;
23471 tree ultimate_origin;
23472 dw_die_ref var_die;
23473 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
23474 bool declaration = (DECL_EXTERNAL (decl_or_origin)
23475 || class_or_namespace_scope_p (context_die));
23476 bool specialization_p = false;
23477 bool no_linkage_name = false;
23479 /* While C++ inline static data members have definitions inside of the
23480 class, force the first DIE to be a declaration, then let gen_member_die
23481 reparent it to the class context and call gen_variable_die again
23482 to create the outside of the class DIE for the definition. */
23483 if (!declaration
23484 && old_die == NULL
23485 && decl
23486 && DECL_CONTEXT (decl)
23487 && TYPE_P (DECL_CONTEXT (decl))
23488 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
23490 declaration = true;
23491 if (dwarf_version < 5)
23492 no_linkage_name = true;
23495 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23496 if (decl || ultimate_origin)
23497 origin = ultimate_origin;
23498 com_decl = fortran_common (decl_or_origin, &off);
23500 /* Symbol in common gets emitted as a child of the common block, in the form
23501 of a data member. */
23502 if (com_decl)
23504 dw_die_ref com_die;
23505 dw_loc_list_ref loc = NULL;
23506 die_node com_die_arg;
23508 var_die = lookup_decl_die (decl_or_origin);
23509 if (var_die)
23511 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
23513 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
23514 if (loc)
23516 if (off)
23518 /* Optimize the common case. */
23519 if (single_element_loc_list_p (loc)
23520 && loc->expr->dw_loc_opc == DW_OP_addr
23521 && loc->expr->dw_loc_next == NULL
23522 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
23523 == SYMBOL_REF)
23525 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23526 loc->expr->dw_loc_oprnd1.v.val_addr
23527 = plus_constant (GET_MODE (x), x , off);
23529 else
23530 loc_list_plus_const (loc, off);
23532 add_AT_location_description (var_die, DW_AT_location, loc);
23533 remove_AT (var_die, DW_AT_declaration);
23536 return;
23539 if (common_block_die_table == NULL)
23540 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
23542 com_die_arg.decl_id = DECL_UID (com_decl);
23543 com_die_arg.die_parent = context_die;
23544 com_die = common_block_die_table->find (&com_die_arg);
23545 if (! early_dwarf)
23546 loc = loc_list_from_tree (com_decl, 2, NULL);
23547 if (com_die == NULL)
23549 const char *cnam
23550 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
23551 die_node **slot;
23553 com_die = new_die (DW_TAG_common_block, context_die, decl);
23554 add_name_and_src_coords_attributes (com_die, com_decl);
23555 if (loc)
23557 add_AT_location_description (com_die, DW_AT_location, loc);
23558 /* Avoid sharing the same loc descriptor between
23559 DW_TAG_common_block and DW_TAG_variable. */
23560 loc = loc_list_from_tree (com_decl, 2, NULL);
23562 else if (DECL_EXTERNAL (decl_or_origin))
23563 add_AT_flag (com_die, DW_AT_declaration, 1);
23564 if (want_pubnames ())
23565 add_pubname_string (cnam, com_die); /* ??? needed? */
23566 com_die->decl_id = DECL_UID (com_decl);
23567 slot = common_block_die_table->find_slot (com_die, INSERT);
23568 *slot = com_die;
23570 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
23572 add_AT_location_description (com_die, DW_AT_location, loc);
23573 loc = loc_list_from_tree (com_decl, 2, NULL);
23574 remove_AT (com_die, DW_AT_declaration);
23576 var_die = new_die (DW_TAG_variable, com_die, decl);
23577 add_name_and_src_coords_attributes (var_die, decl_or_origin);
23578 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
23579 decl_quals (decl_or_origin), false,
23580 context_die);
23581 add_alignment_attribute (var_die, decl);
23582 add_AT_flag (var_die, DW_AT_external, 1);
23583 if (loc)
23585 if (off)
23587 /* Optimize the common case. */
23588 if (single_element_loc_list_p (loc)
23589 && loc->expr->dw_loc_opc == DW_OP_addr
23590 && loc->expr->dw_loc_next == NULL
23591 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
23593 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
23594 loc->expr->dw_loc_oprnd1.v.val_addr
23595 = plus_constant (GET_MODE (x), x, off);
23597 else
23598 loc_list_plus_const (loc, off);
23600 add_AT_location_description (var_die, DW_AT_location, loc);
23602 else if (DECL_EXTERNAL (decl_or_origin))
23603 add_AT_flag (var_die, DW_AT_declaration, 1);
23604 if (decl)
23605 equate_decl_number_to_die (decl, var_die);
23606 return;
23609 if (old_die)
23611 if (declaration)
23613 /* A declaration that has been previously dumped, needs no
23614 further annotations, since it doesn't need location on
23615 the second pass. */
23616 return;
23618 else if (decl_will_get_specification_p (old_die, decl, declaration)
23619 && !get_AT (old_die, DW_AT_specification))
23621 /* Fall-thru so we can make a new variable die along with a
23622 DW_AT_specification. */
23624 else if (origin && old_die->die_parent != context_die)
23626 /* If we will be creating an inlined instance, we need a
23627 new DIE that will get annotated with
23628 DW_AT_abstract_origin. */
23629 gcc_assert (!DECL_ABSTRACT_P (decl));
23631 else
23633 /* If a DIE was dumped early, it still needs location info.
23634 Skip to where we fill the location bits. */
23635 var_die = old_die;
23637 /* ??? In LTRANS we cannot annotate early created variably
23638 modified type DIEs without copying them and adjusting all
23639 references to them. Thus we dumped them again. Also add a
23640 reference to them but beware of -g0 compile and -g link
23641 in which case the reference will be already present. */
23642 tree type = TREE_TYPE (decl_or_origin);
23643 if (in_lto_p
23644 && ! get_AT (var_die, DW_AT_type)
23645 && variably_modified_type_p
23646 (type, decl_function_context (decl_or_origin)))
23648 if (decl_by_reference_p (decl_or_origin))
23649 add_type_attribute (var_die, TREE_TYPE (type),
23650 TYPE_UNQUALIFIED, false, context_die);
23651 else
23652 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
23653 false, context_die);
23656 goto gen_variable_die_location;
23660 /* For static data members, the declaration in the class is supposed
23661 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23662 also in DWARF2; the specification should still be DW_TAG_variable
23663 referencing the DW_TAG_member DIE. */
23664 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23665 var_die = new_die (DW_TAG_member, context_die, decl);
23666 else
23667 var_die = new_die (DW_TAG_variable, context_die, decl);
23669 if (origin != NULL)
23670 add_abstract_origin_attribute (var_die, origin);
23672 /* Loop unrolling can create multiple blocks that refer to the same
23673 static variable, so we must test for the DW_AT_declaration flag.
23675 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23676 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23677 sharing them.
23679 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23680 else if (decl_will_get_specification_p (old_die, decl, declaration))
23682 /* This is a definition of a C++ class level static. */
23683 add_AT_specification (var_die, old_die);
23684 specialization_p = true;
23685 if (DECL_NAME (decl))
23687 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23688 struct dwarf_file_data * file_index = lookup_filename (s.file);
23690 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23691 add_AT_file (var_die, DW_AT_decl_file, file_index);
23693 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23694 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23696 if (debug_column_info
23697 && s.column
23698 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23699 != (unsigned) s.column))
23700 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23702 if (old_die->die_tag == DW_TAG_member)
23703 add_linkage_name (var_die, decl);
23706 else
23707 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23709 if ((origin == NULL && !specialization_p)
23710 || (origin != NULL
23711 && !DECL_ABSTRACT_P (decl_or_origin)
23712 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23713 decl_function_context
23714 (decl_or_origin))))
23716 tree type = TREE_TYPE (decl_or_origin);
23718 if (decl_by_reference_p (decl_or_origin))
23719 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23720 context_die);
23721 else
23722 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23723 context_die);
23726 if (origin == NULL && !specialization_p)
23728 if (TREE_PUBLIC (decl))
23729 add_AT_flag (var_die, DW_AT_external, 1);
23731 if (DECL_ARTIFICIAL (decl))
23732 add_AT_flag (var_die, DW_AT_artificial, 1);
23734 add_alignment_attribute (var_die, decl);
23736 add_accessibility_attribute (var_die, decl);
23739 if (declaration)
23740 add_AT_flag (var_die, DW_AT_declaration, 1);
23742 if (decl && (DECL_ABSTRACT_P (decl)
23743 || !old_die || is_declaration_die (old_die)))
23744 equate_decl_number_to_die (decl, var_die);
23746 gen_variable_die_location:
23747 if (! declaration
23748 && (! DECL_ABSTRACT_P (decl_or_origin)
23749 /* Local static vars are shared between all clones/inlines,
23750 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23751 already set. */
23752 || (VAR_P (decl_or_origin)
23753 && TREE_STATIC (decl_or_origin)
23754 && DECL_RTL_SET_P (decl_or_origin))))
23756 if (early_dwarf)
23757 add_pubname (decl_or_origin, var_die);
23758 else
23759 add_location_or_const_value_attribute (var_die, decl_or_origin,
23760 decl == NULL);
23762 else
23763 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23765 if ((dwarf_version >= 4 || !dwarf_strict)
23766 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23767 DW_AT_const_expr) == 1
23768 && !get_AT (var_die, DW_AT_const_expr)
23769 && !specialization_p)
23770 add_AT_flag (var_die, DW_AT_const_expr, 1);
23772 if (!dwarf_strict)
23774 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23775 DW_AT_inline);
23776 if (inl != -1
23777 && !get_AT (var_die, DW_AT_inline)
23778 && !specialization_p)
23779 add_AT_unsigned (var_die, DW_AT_inline, inl);
23783 /* Generate a DIE to represent a named constant. */
23785 static void
23786 gen_const_die (tree decl, dw_die_ref context_die)
23788 dw_die_ref const_die;
23789 tree type = TREE_TYPE (decl);
23791 const_die = lookup_decl_die (decl);
23792 if (const_die)
23793 return;
23795 const_die = new_die (DW_TAG_constant, context_die, decl);
23796 equate_decl_number_to_die (decl, const_die);
23797 add_name_and_src_coords_attributes (const_die, decl);
23798 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23799 if (TREE_PUBLIC (decl))
23800 add_AT_flag (const_die, DW_AT_external, 1);
23801 if (DECL_ARTIFICIAL (decl))
23802 add_AT_flag (const_die, DW_AT_artificial, 1);
23803 tree_add_const_value_attribute_for_decl (const_die, decl);
23806 /* Generate a DIE to represent a label identifier. */
23808 static void
23809 gen_label_die (tree decl, dw_die_ref context_die)
23811 tree origin = decl_ultimate_origin (decl);
23812 dw_die_ref lbl_die = lookup_decl_die (decl);
23813 rtx insn;
23814 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23816 if (!lbl_die)
23818 lbl_die = new_die (DW_TAG_label, context_die, decl);
23819 equate_decl_number_to_die (decl, lbl_die);
23821 if (origin != NULL)
23822 add_abstract_origin_attribute (lbl_die, origin);
23823 else
23824 add_name_and_src_coords_attributes (lbl_die, decl);
23827 if (DECL_ABSTRACT_P (decl))
23828 equate_decl_number_to_die (decl, lbl_die);
23829 else if (! early_dwarf)
23831 insn = DECL_RTL_IF_SET (decl);
23833 /* Deleted labels are programmer specified labels which have been
23834 eliminated because of various optimizations. We still emit them
23835 here so that it is possible to put breakpoints on them. */
23836 if (insn
23837 && (LABEL_P (insn)
23838 || ((NOTE_P (insn)
23839 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23841 /* When optimization is enabled (via -O) some parts of the compiler
23842 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23843 represent source-level labels which were explicitly declared by
23844 the user. This really shouldn't be happening though, so catch
23845 it if it ever does happen. */
23846 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23848 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23849 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23851 else if (insn
23852 && NOTE_P (insn)
23853 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23854 && CODE_LABEL_NUMBER (insn) != -1)
23856 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23857 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23862 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23863 attributes to the DIE for a block STMT, to describe where the inlined
23864 function was called from. This is similar to add_src_coords_attributes. */
23866 static inline void
23867 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23869 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23871 if (dwarf_version >= 3 || !dwarf_strict)
23873 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23874 add_AT_unsigned (die, DW_AT_call_line, s.line);
23875 if (debug_column_info && s.column)
23876 add_AT_unsigned (die, DW_AT_call_column, s.column);
23881 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23882 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23884 static inline void
23885 add_high_low_attributes (tree stmt, dw_die_ref die)
23887 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23889 if (inline_entry_data **iedp
23890 = !inline_entry_data_table ? NULL
23891 : inline_entry_data_table->find_slot_with_hash (stmt,
23892 htab_hash_pointer (stmt),
23893 NO_INSERT))
23895 inline_entry_data *ied = *iedp;
23896 gcc_assert (MAY_HAVE_DEBUG_MARKER_INSNS);
23897 gcc_assert (debug_inline_points);
23898 gcc_assert (inlined_function_outer_scope_p (stmt));
23900 ASM_GENERATE_INTERNAL_LABEL (label, ied->label_pfx, ied->label_num);
23901 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23903 if (debug_variable_location_views && !ZERO_VIEW_P (ied->view)
23904 && !dwarf_strict)
23906 if (!output_asm_line_debug_info ())
23907 add_AT_unsigned (die, DW_AT_GNU_entry_view, ied->view);
23908 else
23910 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", ied->view);
23911 /* FIXME: this will resolve to a small number. Could we
23912 possibly emit smaller data? Ideally we'd emit a
23913 uleb128, but that would make the size of DIEs
23914 impossible for the compiler to compute, since it's
23915 the assembler that computes the value of the view
23916 label in this case. Ideally, we'd have a single form
23917 encompassing both the address and the view, and
23918 indirecting them through a table might make things
23919 easier, but even that would be more wasteful,
23920 space-wise, than what we have now. */
23921 add_AT_symview (die, DW_AT_GNU_entry_view, label);
23925 inline_entry_data_table->clear_slot (iedp);
23928 if (BLOCK_FRAGMENT_CHAIN (stmt)
23929 && (dwarf_version >= 3 || !dwarf_strict))
23931 tree chain, superblock = NULL_TREE;
23932 dw_die_ref pdie;
23933 dw_attr_node *attr = NULL;
23935 if (!debug_inline_points && inlined_function_outer_scope_p (stmt))
23937 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23938 BLOCK_NUMBER (stmt));
23939 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23942 /* Optimize duplicate .debug_ranges lists or even tails of
23943 lists. If this BLOCK has same ranges as its supercontext,
23944 lookup DW_AT_ranges attribute in the supercontext (and
23945 recursively so), verify that the ranges_table contains the
23946 right values and use it instead of adding a new .debug_range. */
23947 for (chain = stmt, pdie = die;
23948 BLOCK_SAME_RANGE (chain);
23949 chain = BLOCK_SUPERCONTEXT (chain))
23951 dw_attr_node *new_attr;
23953 pdie = pdie->die_parent;
23954 if (pdie == NULL)
23955 break;
23956 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23957 break;
23958 new_attr = get_AT (pdie, DW_AT_ranges);
23959 if (new_attr == NULL
23960 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23961 break;
23962 attr = new_attr;
23963 superblock = BLOCK_SUPERCONTEXT (chain);
23965 if (attr != NULL
23966 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23967 == BLOCK_NUMBER (superblock))
23968 && BLOCK_FRAGMENT_CHAIN (superblock))
23970 unsigned long off = attr->dw_attr_val.v.val_offset;
23971 unsigned long supercnt = 0, thiscnt = 0;
23972 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23973 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23975 ++supercnt;
23976 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23977 == BLOCK_NUMBER (chain));
23979 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23980 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23981 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23982 ++thiscnt;
23983 gcc_assert (supercnt >= thiscnt);
23984 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23985 false);
23986 note_rnglist_head (off + supercnt - thiscnt);
23987 return;
23990 unsigned int offset = add_ranges (stmt, true);
23991 add_AT_range_list (die, DW_AT_ranges, offset, false);
23992 note_rnglist_head (offset);
23994 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23995 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23998 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23999 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
24000 chain = BLOCK_FRAGMENT_CHAIN (chain);
24002 while (chain);
24003 add_ranges (NULL);
24005 else
24007 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
24008 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
24009 BLOCK_NUMBER (stmt));
24010 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
24011 BLOCK_NUMBER (stmt));
24012 add_AT_low_high_pc (die, label, label_high, false);
24016 /* Generate a DIE for a lexical block. */
24018 static void
24019 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
24021 dw_die_ref old_die = BLOCK_DIE (stmt);
24022 dw_die_ref stmt_die = NULL;
24023 if (!old_die)
24025 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24026 BLOCK_DIE (stmt) = stmt_die;
24029 if (BLOCK_ABSTRACT (stmt))
24031 if (old_die)
24033 /* This must have been generated early and it won't even
24034 need location information since it's a DW_AT_inline
24035 function. */
24036 if (flag_checking)
24037 for (dw_die_ref c = context_die; c; c = c->die_parent)
24038 if (c->die_tag == DW_TAG_inlined_subroutine
24039 || c->die_tag == DW_TAG_subprogram)
24041 gcc_assert (get_AT (c, DW_AT_inline));
24042 break;
24044 return;
24047 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
24049 /* If this is an inlined instance, create a new lexical die for
24050 anything below to attach DW_AT_abstract_origin to. */
24051 if (old_die)
24053 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
24054 BLOCK_DIE (stmt) = stmt_die;
24055 old_die = NULL;
24058 tree origin = block_ultimate_origin (stmt);
24059 if (origin != NULL_TREE && origin != stmt)
24060 add_abstract_origin_attribute (stmt_die, origin);
24063 if (old_die)
24064 stmt_die = old_die;
24066 /* A non abstract block whose blocks have already been reordered
24067 should have the instruction range for this block. If so, set the
24068 high/low attributes. */
24069 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
24071 gcc_assert (stmt_die);
24072 add_high_low_attributes (stmt, stmt_die);
24075 decls_for_scope (stmt, stmt_die);
24078 /* Generate a DIE for an inlined subprogram. */
24080 static void
24081 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
24083 tree decl;
24085 /* The instance of function that is effectively being inlined shall not
24086 be abstract. */
24087 gcc_assert (! BLOCK_ABSTRACT (stmt));
24089 decl = block_ultimate_origin (stmt);
24091 /* Make sure any inlined functions are known to be inlineable. */
24092 gcc_checking_assert (DECL_ABSTRACT_P (decl)
24093 || cgraph_function_possibly_inlined_p (decl));
24095 if (! BLOCK_ABSTRACT (stmt))
24097 dw_die_ref subr_die
24098 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
24100 if (call_arg_locations || debug_inline_points)
24101 BLOCK_DIE (stmt) = subr_die;
24102 add_abstract_origin_attribute (subr_die, decl);
24103 if (TREE_ASM_WRITTEN (stmt))
24104 add_high_low_attributes (stmt, subr_die);
24105 add_call_src_coords_attributes (stmt, subr_die);
24107 decls_for_scope (stmt, subr_die);
24111 /* Generate a DIE for a field in a record, or structure. CTX is required: see
24112 the comment for VLR_CONTEXT. */
24114 static void
24115 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
24117 dw_die_ref decl_die;
24119 if (TREE_TYPE (decl) == error_mark_node)
24120 return;
24122 decl_die = new_die (DW_TAG_member, context_die, decl);
24123 add_name_and_src_coords_attributes (decl_die, decl);
24124 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
24125 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
24126 context_die);
24128 if (DECL_BIT_FIELD_TYPE (decl))
24130 add_byte_size_attribute (decl_die, decl);
24131 add_bit_size_attribute (decl_die, decl);
24132 add_bit_offset_attribute (decl_die, decl, ctx);
24135 add_alignment_attribute (decl_die, decl);
24137 /* If we have a variant part offset, then we are supposed to process a member
24138 of a QUAL_UNION_TYPE, which is how we represent variant parts in
24139 trees. */
24140 gcc_assert (ctx->variant_part_offset == NULL_TREE
24141 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
24142 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
24143 add_data_member_location_attribute (decl_die, decl, ctx);
24145 if (DECL_ARTIFICIAL (decl))
24146 add_AT_flag (decl_die, DW_AT_artificial, 1);
24148 add_accessibility_attribute (decl_die, decl);
24150 /* Equate decl number to die, so that we can look up this decl later on. */
24151 equate_decl_number_to_die (decl, decl_die);
24154 /* Generate a DIE for a pointer to a member type. TYPE can be an
24155 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
24156 pointer to member function. */
24158 static void
24159 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
24161 if (lookup_type_die (type))
24162 return;
24164 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
24165 scope_die_for (type, context_die), type);
24167 equate_type_number_to_die (type, ptr_die);
24168 add_AT_die_ref (ptr_die, DW_AT_containing_type,
24169 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
24170 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
24171 context_die);
24172 add_alignment_attribute (ptr_die, type);
24174 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
24175 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
24177 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
24178 add_AT_loc (ptr_die, DW_AT_use_location, op);
24182 static char *producer_string;
24184 /* Return a heap allocated producer string including command line options
24185 if -grecord-gcc-switches. */
24187 static char *
24188 gen_producer_string (void)
24190 size_t j;
24191 auto_vec<const char *> switches;
24192 const char *language_string = lang_hooks.name;
24193 char *producer, *tail;
24194 const char *p;
24195 size_t len = dwarf_record_gcc_switches ? 0 : 3;
24196 size_t plen = strlen (language_string) + 1 + strlen (version_string);
24198 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
24199 switch (save_decoded_options[j].opt_index)
24201 case OPT_o:
24202 case OPT_d:
24203 case OPT_dumpbase:
24204 case OPT_dumpdir:
24205 case OPT_auxbase:
24206 case OPT_auxbase_strip:
24207 case OPT_quiet:
24208 case OPT_version:
24209 case OPT_v:
24210 case OPT_w:
24211 case OPT_L:
24212 case OPT_D:
24213 case OPT_I:
24214 case OPT_U:
24215 case OPT_SPECIAL_unknown:
24216 case OPT_SPECIAL_ignore:
24217 case OPT_SPECIAL_program_name:
24218 case OPT_SPECIAL_input_file:
24219 case OPT_grecord_gcc_switches:
24220 case OPT__output_pch_:
24221 case OPT_fdiagnostics_show_location_:
24222 case OPT_fdiagnostics_show_option:
24223 case OPT_fdiagnostics_show_caret:
24224 case OPT_fdiagnostics_color_:
24225 case OPT_fverbose_asm:
24226 case OPT____:
24227 case OPT__sysroot_:
24228 case OPT_nostdinc:
24229 case OPT_nostdinc__:
24230 case OPT_fpreprocessed:
24231 case OPT_fltrans_output_list_:
24232 case OPT_fresolution_:
24233 case OPT_fdebug_prefix_map_:
24234 case OPT_fmacro_prefix_map_:
24235 case OPT_ffile_prefix_map_:
24236 case OPT_fcompare_debug:
24237 case OPT_fchecking:
24238 case OPT_fchecking_:
24239 /* Ignore these. */
24240 continue;
24241 default:
24242 if (cl_options[save_decoded_options[j].opt_index].flags
24243 & CL_NO_DWARF_RECORD)
24244 continue;
24245 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
24246 == '-');
24247 switch (save_decoded_options[j].canonical_option[0][1])
24249 case 'M':
24250 case 'i':
24251 case 'W':
24252 continue;
24253 case 'f':
24254 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
24255 "dump", 4) == 0)
24256 continue;
24257 break;
24258 default:
24259 break;
24261 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
24262 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
24263 break;
24266 producer = XNEWVEC (char, plen + 1 + len + 1);
24267 tail = producer;
24268 sprintf (tail, "%s %s", language_string, version_string);
24269 tail += plen;
24271 FOR_EACH_VEC_ELT (switches, j, p)
24273 len = strlen (p);
24274 *tail = ' ';
24275 memcpy (tail + 1, p, len);
24276 tail += len + 1;
24279 *tail = '\0';
24280 return producer;
24283 /* Given a C and/or C++ language/version string return the "highest".
24284 C++ is assumed to be "higher" than C in this case. Used for merging
24285 LTO translation unit languages. */
24286 static const char *
24287 highest_c_language (const char *lang1, const char *lang2)
24289 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
24290 return "GNU C++17";
24291 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
24292 return "GNU C++14";
24293 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
24294 return "GNU C++11";
24295 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
24296 return "GNU C++98";
24298 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
24299 return "GNU C17";
24300 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
24301 return "GNU C11";
24302 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
24303 return "GNU C99";
24304 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
24305 return "GNU C89";
24307 gcc_unreachable ();
24311 /* Generate the DIE for the compilation unit. */
24313 static dw_die_ref
24314 gen_compile_unit_die (const char *filename)
24316 dw_die_ref die;
24317 const char *language_string = lang_hooks.name;
24318 int language;
24320 die = new_die (DW_TAG_compile_unit, NULL, NULL);
24322 if (filename)
24324 add_name_attribute (die, filename);
24325 /* Don't add cwd for <built-in>. */
24326 if (filename[0] != '<')
24327 add_comp_dir_attribute (die);
24330 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
24332 /* If our producer is LTO try to figure out a common language to use
24333 from the global list of translation units. */
24334 if (strcmp (language_string, "GNU GIMPLE") == 0)
24336 unsigned i;
24337 tree t;
24338 const char *common_lang = NULL;
24340 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
24342 if (!TRANSLATION_UNIT_LANGUAGE (t))
24343 continue;
24344 if (!common_lang)
24345 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
24346 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
24348 else if (strncmp (common_lang, "GNU C", 5) == 0
24349 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
24350 /* Mixing C and C++ is ok, use C++ in that case. */
24351 common_lang = highest_c_language (common_lang,
24352 TRANSLATION_UNIT_LANGUAGE (t));
24353 else
24355 /* Fall back to C. */
24356 common_lang = NULL;
24357 break;
24361 if (common_lang)
24362 language_string = common_lang;
24365 language = DW_LANG_C;
24366 if (strncmp (language_string, "GNU C", 5) == 0
24367 && ISDIGIT (language_string[5]))
24369 language = DW_LANG_C89;
24370 if (dwarf_version >= 3 || !dwarf_strict)
24372 if (strcmp (language_string, "GNU C89") != 0)
24373 language = DW_LANG_C99;
24375 if (dwarf_version >= 5 /* || !dwarf_strict */)
24376 if (strcmp (language_string, "GNU C11") == 0
24377 || strcmp (language_string, "GNU C17") == 0)
24378 language = DW_LANG_C11;
24381 else if (strncmp (language_string, "GNU C++", 7) == 0)
24383 language = DW_LANG_C_plus_plus;
24384 if (dwarf_version >= 5 /* || !dwarf_strict */)
24386 if (strcmp (language_string, "GNU C++11") == 0)
24387 language = DW_LANG_C_plus_plus_11;
24388 else if (strcmp (language_string, "GNU C++14") == 0)
24389 language = DW_LANG_C_plus_plus_14;
24390 else if (strcmp (language_string, "GNU C++17") == 0)
24391 /* For now. */
24392 language = DW_LANG_C_plus_plus_14;
24395 else if (strcmp (language_string, "GNU F77") == 0)
24396 language = DW_LANG_Fortran77;
24397 else if (dwarf_version >= 3 || !dwarf_strict)
24399 if (strcmp (language_string, "GNU Ada") == 0)
24400 language = DW_LANG_Ada95;
24401 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24403 language = DW_LANG_Fortran95;
24404 if (dwarf_version >= 5 /* || !dwarf_strict */)
24406 if (strcmp (language_string, "GNU Fortran2003") == 0)
24407 language = DW_LANG_Fortran03;
24408 else if (strcmp (language_string, "GNU Fortran2008") == 0)
24409 language = DW_LANG_Fortran08;
24412 else if (strcmp (language_string, "GNU Objective-C") == 0)
24413 language = DW_LANG_ObjC;
24414 else if (strcmp (language_string, "GNU Objective-C++") == 0)
24415 language = DW_LANG_ObjC_plus_plus;
24416 else if (dwarf_version >= 5 || !dwarf_strict)
24418 if (strcmp (language_string, "GNU Go") == 0)
24419 language = DW_LANG_Go;
24422 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
24423 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
24424 language = DW_LANG_Fortran90;
24426 add_AT_unsigned (die, DW_AT_language, language);
24428 switch (language)
24430 case DW_LANG_Fortran77:
24431 case DW_LANG_Fortran90:
24432 case DW_LANG_Fortran95:
24433 case DW_LANG_Fortran03:
24434 case DW_LANG_Fortran08:
24435 /* Fortran has case insensitive identifiers and the front-end
24436 lowercases everything. */
24437 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
24438 break;
24439 default:
24440 /* The default DW_ID_case_sensitive doesn't need to be specified. */
24441 break;
24443 return die;
24446 /* Generate the DIE for a base class. */
24448 static void
24449 gen_inheritance_die (tree binfo, tree access, tree type,
24450 dw_die_ref context_die)
24452 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
24453 struct vlr_context ctx = { type, NULL };
24455 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
24456 context_die);
24457 add_data_member_location_attribute (die, binfo, &ctx);
24459 if (BINFO_VIRTUAL_P (binfo))
24460 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
24462 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
24463 children, otherwise the default is DW_ACCESS_public. In DWARF2
24464 the default has always been DW_ACCESS_private. */
24465 if (access == access_public_node)
24467 if (dwarf_version == 2
24468 || context_die->die_tag == DW_TAG_class_type)
24469 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
24471 else if (access == access_protected_node)
24472 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
24473 else if (dwarf_version > 2
24474 && context_die->die_tag != DW_TAG_class_type)
24475 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
24478 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
24479 structure. */
24480 static bool
24481 is_variant_part (tree decl)
24483 return (TREE_CODE (decl) == FIELD_DECL
24484 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
24487 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
24488 return the FIELD_DECL. Return NULL_TREE otherwise. */
24490 static tree
24491 analyze_discr_in_predicate (tree operand, tree struct_type)
24493 bool continue_stripping = true;
24494 while (continue_stripping)
24495 switch (TREE_CODE (operand))
24497 CASE_CONVERT:
24498 operand = TREE_OPERAND (operand, 0);
24499 break;
24500 default:
24501 continue_stripping = false;
24502 break;
24505 /* Match field access to members of struct_type only. */
24506 if (TREE_CODE (operand) == COMPONENT_REF
24507 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
24508 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
24509 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
24510 return TREE_OPERAND (operand, 1);
24511 else
24512 return NULL_TREE;
24515 /* Check that SRC is a constant integer that can be represented as a native
24516 integer constant (either signed or unsigned). If so, store it into DEST and
24517 return true. Return false otherwise. */
24519 static bool
24520 get_discr_value (tree src, dw_discr_value *dest)
24522 tree discr_type = TREE_TYPE (src);
24524 if (lang_hooks.types.get_debug_type)
24526 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
24527 if (debug_type != NULL)
24528 discr_type = debug_type;
24531 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
24532 return false;
24534 /* Signedness can vary between the original type and the debug type. This
24535 can happen for character types in Ada for instance: the character type
24536 used for code generation can be signed, to be compatible with the C one,
24537 but from a debugger point of view, it must be unsigned. */
24538 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
24539 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
24541 if (is_orig_unsigned != is_debug_unsigned)
24542 src = fold_convert (discr_type, src);
24544 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
24545 return false;
24547 dest->pos = is_debug_unsigned;
24548 if (is_debug_unsigned)
24549 dest->v.uval = tree_to_uhwi (src);
24550 else
24551 dest->v.sval = tree_to_shwi (src);
24553 return true;
24556 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
24557 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
24558 store NULL_TREE in DISCR_DECL. Otherwise:
24560 - store the discriminant field in STRUCT_TYPE that controls the variant
24561 part to *DISCR_DECL
24563 - put in *DISCR_LISTS_P an array where for each variant, the item
24564 represents the corresponding matching list of discriminant values.
24566 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
24567 the above array.
24569 Note that when the array is allocated (i.e. when the analysis is
24570 successful), it is up to the caller to free the array. */
24572 static void
24573 analyze_variants_discr (tree variant_part_decl,
24574 tree struct_type,
24575 tree *discr_decl,
24576 dw_discr_list_ref **discr_lists_p,
24577 unsigned *discr_lists_length)
24579 tree variant_part_type = TREE_TYPE (variant_part_decl);
24580 tree variant;
24581 dw_discr_list_ref *discr_lists;
24582 unsigned i;
24584 /* Compute how many variants there are in this variant part. */
24585 *discr_lists_length = 0;
24586 for (variant = TYPE_FIELDS (variant_part_type);
24587 variant != NULL_TREE;
24588 variant = DECL_CHAIN (variant))
24589 ++*discr_lists_length;
24591 *discr_decl = NULL_TREE;
24592 *discr_lists_p
24593 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
24594 sizeof (**discr_lists_p));
24595 discr_lists = *discr_lists_p;
24597 /* And then analyze all variants to extract discriminant information for all
24598 of them. This analysis is conservative: as soon as we detect something we
24599 do not support, abort everything and pretend we found nothing. */
24600 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
24601 variant != NULL_TREE;
24602 variant = DECL_CHAIN (variant), ++i)
24604 tree match_expr = DECL_QUALIFIER (variant);
24606 /* Now, try to analyze the predicate and deduce a discriminant for
24607 it. */
24608 if (match_expr == boolean_true_node)
24609 /* Typically happens for the default variant: it matches all cases that
24610 previous variants rejected. Don't output any matching value for
24611 this one. */
24612 continue;
24614 /* The following loop tries to iterate over each discriminant
24615 possibility: single values or ranges. */
24616 while (match_expr != NULL_TREE)
24618 tree next_round_match_expr;
24619 tree candidate_discr = NULL_TREE;
24620 dw_discr_list_ref new_node = NULL;
24622 /* Possibilities are matched one after the other by nested
24623 TRUTH_ORIF_EXPR expressions. Process the current possibility and
24624 continue with the rest at next iteration. */
24625 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
24627 next_round_match_expr = TREE_OPERAND (match_expr, 0);
24628 match_expr = TREE_OPERAND (match_expr, 1);
24630 else
24631 next_round_match_expr = NULL_TREE;
24633 if (match_expr == boolean_false_node)
24634 /* This sub-expression matches nothing: just wait for the next
24635 one. */
24638 else if (TREE_CODE (match_expr) == EQ_EXPR)
24640 /* We are matching: <discr_field> == <integer_cst>
24641 This sub-expression matches a single value. */
24642 tree integer_cst = TREE_OPERAND (match_expr, 1);
24644 candidate_discr
24645 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
24646 struct_type);
24648 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24649 if (!get_discr_value (integer_cst,
24650 &new_node->dw_discr_lower_bound))
24651 goto abort;
24652 new_node->dw_discr_range = false;
24655 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
24657 /* We are matching:
24658 <discr_field> > <integer_cst>
24659 && <discr_field> < <integer_cst>.
24660 This sub-expression matches the range of values between the
24661 two matched integer constants. Note that comparisons can be
24662 inclusive or exclusive. */
24663 tree candidate_discr_1, candidate_discr_2;
24664 tree lower_cst, upper_cst;
24665 bool lower_cst_included, upper_cst_included;
24666 tree lower_op = TREE_OPERAND (match_expr, 0);
24667 tree upper_op = TREE_OPERAND (match_expr, 1);
24669 /* When the comparison is exclusive, the integer constant is not
24670 the discriminant range bound we are looking for: we will have
24671 to increment or decrement it. */
24672 if (TREE_CODE (lower_op) == GE_EXPR)
24673 lower_cst_included = true;
24674 else if (TREE_CODE (lower_op) == GT_EXPR)
24675 lower_cst_included = false;
24676 else
24677 goto abort;
24679 if (TREE_CODE (upper_op) == LE_EXPR)
24680 upper_cst_included = true;
24681 else if (TREE_CODE (upper_op) == LT_EXPR)
24682 upper_cst_included = false;
24683 else
24684 goto abort;
24686 /* Extract the discriminant from the first operand and check it
24687 is consistant with the same analysis in the second
24688 operand. */
24689 candidate_discr_1
24690 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
24691 struct_type);
24692 candidate_discr_2
24693 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
24694 struct_type);
24695 if (candidate_discr_1 == candidate_discr_2)
24696 candidate_discr = candidate_discr_1;
24697 else
24698 goto abort;
24700 /* Extract bounds from both. */
24701 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24702 lower_cst = TREE_OPERAND (lower_op, 1);
24703 upper_cst = TREE_OPERAND (upper_op, 1);
24705 if (!lower_cst_included)
24706 lower_cst
24707 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24708 build_int_cst (TREE_TYPE (lower_cst), 1));
24709 if (!upper_cst_included)
24710 upper_cst
24711 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24712 build_int_cst (TREE_TYPE (upper_cst), 1));
24714 if (!get_discr_value (lower_cst,
24715 &new_node->dw_discr_lower_bound)
24716 || !get_discr_value (upper_cst,
24717 &new_node->dw_discr_upper_bound))
24718 goto abort;
24720 new_node->dw_discr_range = true;
24723 else
24724 /* Unsupported sub-expression: we cannot determine the set of
24725 matching discriminant values. Abort everything. */
24726 goto abort;
24728 /* If the discriminant info is not consistant with what we saw so
24729 far, consider the analysis failed and abort everything. */
24730 if (candidate_discr == NULL_TREE
24731 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24732 goto abort;
24733 else
24734 *discr_decl = candidate_discr;
24736 if (new_node != NULL)
24738 new_node->dw_discr_next = discr_lists[i];
24739 discr_lists[i] = new_node;
24741 match_expr = next_round_match_expr;
24745 /* If we reach this point, we could match everything we were interested
24746 in. */
24747 return;
24749 abort:
24750 /* Clean all data structure and return no result. */
24751 free (*discr_lists_p);
24752 *discr_lists_p = NULL;
24753 *discr_decl = NULL_TREE;
24756 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24757 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24758 under CONTEXT_DIE.
24760 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24761 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24762 this type, which are record types, represent the available variants and each
24763 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24764 values are inferred from these attributes.
24766 In trees, the offsets for the fields inside these sub-records are relative
24767 to the variant part itself, whereas the corresponding DIEs should have
24768 offset attributes that are relative to the embedding record base address.
24769 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24770 must be an expression that computes the offset of the variant part to
24771 describe in DWARF. */
24773 static void
24774 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24775 dw_die_ref context_die)
24777 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24778 tree variant_part_offset = vlr_ctx->variant_part_offset;
24779 struct loc_descr_context ctx = {
24780 vlr_ctx->struct_type, /* context_type */
24781 NULL_TREE, /* base_decl */
24782 NULL, /* dpi */
24783 false, /* placeholder_arg */
24784 false /* placeholder_seen */
24787 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24788 NULL_TREE if there is no such field. */
24789 tree discr_decl = NULL_TREE;
24790 dw_discr_list_ref *discr_lists;
24791 unsigned discr_lists_length = 0;
24792 unsigned i;
24794 dw_die_ref dwarf_proc_die = NULL;
24795 dw_die_ref variant_part_die
24796 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24798 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24800 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24801 &discr_decl, &discr_lists, &discr_lists_length);
24803 if (discr_decl != NULL_TREE)
24805 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24807 if (discr_die)
24808 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24809 else
24810 /* We have no DIE for the discriminant, so just discard all
24811 discrimimant information in the output. */
24812 discr_decl = NULL_TREE;
24815 /* If the offset for this variant part is more complex than a constant,
24816 create a DWARF procedure for it so that we will not have to generate DWARF
24817 expressions for it for each member. */
24818 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24819 && (dwarf_version >= 3 || !dwarf_strict))
24821 const tree dwarf_proc_fndecl
24822 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24823 build_function_type (TREE_TYPE (variant_part_offset),
24824 NULL_TREE));
24825 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24826 const dw_loc_descr_ref dwarf_proc_body
24827 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24829 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24830 dwarf_proc_fndecl, context_die);
24831 if (dwarf_proc_die != NULL)
24832 variant_part_offset = dwarf_proc_call;
24835 /* Output DIEs for all variants. */
24836 i = 0;
24837 for (tree variant = TYPE_FIELDS (variant_part_type);
24838 variant != NULL_TREE;
24839 variant = DECL_CHAIN (variant), ++i)
24841 tree variant_type = TREE_TYPE (variant);
24842 dw_die_ref variant_die;
24844 /* All variants (i.e. members of a variant part) are supposed to be
24845 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24846 under these records. */
24847 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24849 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24850 equate_decl_number_to_die (variant, variant_die);
24852 /* Output discriminant values this variant matches, if any. */
24853 if (discr_decl == NULL || discr_lists[i] == NULL)
24854 /* In the case we have discriminant information at all, this is
24855 probably the default variant: as the standard says, don't
24856 output any discriminant value/list attribute. */
24858 else if (discr_lists[i]->dw_discr_next == NULL
24859 && !discr_lists[i]->dw_discr_range)
24860 /* If there is only one accepted value, don't bother outputting a
24861 list. */
24862 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24863 else
24864 add_discr_list (variant_die, discr_lists[i]);
24866 for (tree member = TYPE_FIELDS (variant_type);
24867 member != NULL_TREE;
24868 member = DECL_CHAIN (member))
24870 struct vlr_context vlr_sub_ctx = {
24871 vlr_ctx->struct_type, /* struct_type */
24872 NULL /* variant_part_offset */
24874 if (is_variant_part (member))
24876 /* All offsets for fields inside variant parts are relative to
24877 the top-level embedding RECORD_TYPE's base address. On the
24878 other hand, offsets in GCC's types are relative to the
24879 nested-most variant part. So we have to sum offsets each time
24880 we recurse. */
24882 vlr_sub_ctx.variant_part_offset
24883 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24884 variant_part_offset, byte_position (member));
24885 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24887 else
24889 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24890 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24895 free (discr_lists);
24898 /* Generate a DIE for a class member. */
24900 static void
24901 gen_member_die (tree type, dw_die_ref context_die)
24903 tree member;
24904 tree binfo = TYPE_BINFO (type);
24906 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24908 /* If this is not an incomplete type, output descriptions of each of its
24909 members. Note that as we output the DIEs necessary to represent the
24910 members of this record or union type, we will also be trying to output
24911 DIEs to represent the *types* of those members. However the `type'
24912 function (above) will specifically avoid generating type DIEs for member
24913 types *within* the list of member DIEs for this (containing) type except
24914 for those types (of members) which are explicitly marked as also being
24915 members of this (containing) type themselves. The g++ front- end can
24916 force any given type to be treated as a member of some other (containing)
24917 type by setting the TYPE_CONTEXT of the given (member) type to point to
24918 the TREE node representing the appropriate (containing) type. */
24920 /* First output info about the base classes. */
24921 if (binfo)
24923 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24924 int i;
24925 tree base;
24927 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24928 gen_inheritance_die (base,
24929 (accesses ? (*accesses)[i] : access_public_node),
24930 type,
24931 context_die);
24934 /* Now output info about the data members and type members. */
24935 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24937 struct vlr_context vlr_ctx = { type, NULL_TREE };
24938 bool static_inline_p
24939 = (TREE_STATIC (member)
24940 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24941 != -1));
24943 /* Ignore clones. */
24944 if (DECL_ABSTRACT_ORIGIN (member))
24945 continue;
24947 /* If we thought we were generating minimal debug info for TYPE
24948 and then changed our minds, some of the member declarations
24949 may have already been defined. Don't define them again, but
24950 do put them in the right order. */
24952 if (dw_die_ref child = lookup_decl_die (member))
24954 /* Handle inline static data members, which only have in-class
24955 declarations. */
24956 dw_die_ref ref = NULL;
24957 if (child->die_tag == DW_TAG_variable
24958 && child->die_parent == comp_unit_die ())
24960 ref = get_AT_ref (child, DW_AT_specification);
24961 /* For C++17 inline static data members followed by redundant
24962 out of class redeclaration, we might get here with
24963 child being the DIE created for the out of class
24964 redeclaration and with its DW_AT_specification being
24965 the DIE created for in-class definition. We want to
24966 reparent the latter, and don't want to create another
24967 DIE with DW_AT_specification in that case, because
24968 we already have one. */
24969 if (ref
24970 && static_inline_p
24971 && ref->die_tag == DW_TAG_variable
24972 && ref->die_parent == comp_unit_die ()
24973 && get_AT (ref, DW_AT_specification) == NULL)
24975 child = ref;
24976 ref = NULL;
24977 static_inline_p = false;
24981 if (child->die_tag == DW_TAG_variable
24982 && child->die_parent == comp_unit_die ()
24983 && ref == NULL)
24985 reparent_child (child, context_die);
24986 if (dwarf_version < 5)
24987 child->die_tag = DW_TAG_member;
24989 else
24990 splice_child_die (context_die, child);
24993 /* Do not generate standard DWARF for variant parts if we are generating
24994 the corresponding GNAT encodings: DIEs generated for both would
24995 conflict in our mappings. */
24996 else if (is_variant_part (member)
24997 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24999 vlr_ctx.variant_part_offset = byte_position (member);
25000 gen_variant_part (member, &vlr_ctx, context_die);
25002 else
25004 vlr_ctx.variant_part_offset = NULL_TREE;
25005 gen_decl_die (member, NULL, &vlr_ctx, context_die);
25008 /* For C++ inline static data members emit immediately a DW_TAG_variable
25009 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
25010 DW_AT_specification. */
25011 if (static_inline_p)
25013 int old_extern = DECL_EXTERNAL (member);
25014 DECL_EXTERNAL (member) = 0;
25015 gen_decl_die (member, NULL, NULL, comp_unit_die ());
25016 DECL_EXTERNAL (member) = old_extern;
25021 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
25022 is set, we pretend that the type was never defined, so we only get the
25023 member DIEs needed by later specification DIEs. */
25025 static void
25026 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
25027 enum debug_info_usage usage)
25029 if (TREE_ASM_WRITTEN (type))
25031 /* Fill in the bound of variable-length fields in late dwarf if
25032 still incomplete. */
25033 if (!early_dwarf && variably_modified_type_p (type, NULL))
25034 for (tree member = TYPE_FIELDS (type);
25035 member;
25036 member = DECL_CHAIN (member))
25037 fill_variable_array_bounds (TREE_TYPE (member));
25038 return;
25041 dw_die_ref type_die = lookup_type_die (type);
25042 dw_die_ref scope_die = 0;
25043 int nested = 0;
25044 int complete = (TYPE_SIZE (type)
25045 && (! TYPE_STUB_DECL (type)
25046 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
25047 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
25048 complete = complete && should_emit_struct_debug (type, usage);
25050 if (type_die && ! complete)
25051 return;
25053 if (TYPE_CONTEXT (type) != NULL_TREE
25054 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25055 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
25056 nested = 1;
25058 scope_die = scope_die_for (type, context_die);
25060 /* Generate child dies for template paramaters. */
25061 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
25062 schedule_generic_params_dies_gen (type);
25064 if (! type_die || (nested && is_cu_die (scope_die)))
25065 /* First occurrence of type or toplevel definition of nested class. */
25067 dw_die_ref old_die = type_die;
25069 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
25070 ? record_type_tag (type) : DW_TAG_union_type,
25071 scope_die, type);
25072 equate_type_number_to_die (type, type_die);
25073 if (old_die)
25074 add_AT_specification (type_die, old_die);
25075 else
25076 add_name_attribute (type_die, type_tag (type));
25078 else
25079 remove_AT (type_die, DW_AT_declaration);
25081 /* If this type has been completed, then give it a byte_size attribute and
25082 then give a list of members. */
25083 if (complete && !ns_decl)
25085 /* Prevent infinite recursion in cases where the type of some member of
25086 this type is expressed in terms of this type itself. */
25087 TREE_ASM_WRITTEN (type) = 1;
25088 add_byte_size_attribute (type_die, type);
25089 add_alignment_attribute (type_die, type);
25090 if (TYPE_STUB_DECL (type) != NULL_TREE)
25092 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
25093 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
25096 /* If the first reference to this type was as the return type of an
25097 inline function, then it may not have a parent. Fix this now. */
25098 if (type_die->die_parent == NULL)
25099 add_child_die (scope_die, type_die);
25101 push_decl_scope (type);
25102 gen_member_die (type, type_die);
25103 pop_decl_scope ();
25105 add_gnat_descriptive_type_attribute (type_die, type, context_die);
25106 if (TYPE_ARTIFICIAL (type))
25107 add_AT_flag (type_die, DW_AT_artificial, 1);
25109 /* GNU extension: Record what type our vtable lives in. */
25110 if (TYPE_VFIELD (type))
25112 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
25114 gen_type_die (vtype, context_die);
25115 add_AT_die_ref (type_die, DW_AT_containing_type,
25116 lookup_type_die (vtype));
25119 else
25121 add_AT_flag (type_die, DW_AT_declaration, 1);
25123 /* We don't need to do this for function-local types. */
25124 if (TYPE_STUB_DECL (type)
25125 && ! decl_function_context (TYPE_STUB_DECL (type)))
25126 vec_safe_push (incomplete_types, type);
25129 if (get_AT (type_die, DW_AT_name))
25130 add_pubtype (type, type_die);
25133 /* Generate a DIE for a subroutine _type_. */
25135 static void
25136 gen_subroutine_type_die (tree type, dw_die_ref context_die)
25138 tree return_type = TREE_TYPE (type);
25139 dw_die_ref subr_die
25140 = new_die (DW_TAG_subroutine_type,
25141 scope_die_for (type, context_die), type);
25143 equate_type_number_to_die (type, subr_die);
25144 add_prototyped_attribute (subr_die, type);
25145 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
25146 context_die);
25147 add_alignment_attribute (subr_die, type);
25148 gen_formal_types_die (type, subr_die);
25150 if (get_AT (subr_die, DW_AT_name))
25151 add_pubtype (type, subr_die);
25152 if ((dwarf_version >= 5 || !dwarf_strict)
25153 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
25154 add_AT_flag (subr_die, DW_AT_reference, 1);
25155 if ((dwarf_version >= 5 || !dwarf_strict)
25156 && lang_hooks.types.type_dwarf_attribute (type,
25157 DW_AT_rvalue_reference) != -1)
25158 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
25161 /* Generate a DIE for a type definition. */
25163 static void
25164 gen_typedef_die (tree decl, dw_die_ref context_die)
25166 dw_die_ref type_die;
25167 tree type;
25169 if (TREE_ASM_WRITTEN (decl))
25171 if (DECL_ORIGINAL_TYPE (decl))
25172 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
25173 return;
25176 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
25177 checks in process_scope_var and modified_type_die), this should be called
25178 only for original types. */
25179 gcc_assert (decl_ultimate_origin (decl) == NULL
25180 || decl_ultimate_origin (decl) == decl);
25182 TREE_ASM_WRITTEN (decl) = 1;
25183 type_die = new_die (DW_TAG_typedef, context_die, decl);
25185 add_name_and_src_coords_attributes (type_die, decl);
25186 if (DECL_ORIGINAL_TYPE (decl))
25188 type = DECL_ORIGINAL_TYPE (decl);
25189 if (type == error_mark_node)
25190 return;
25192 gcc_assert (type != TREE_TYPE (decl));
25193 equate_type_number_to_die (TREE_TYPE (decl), type_die);
25195 else
25197 type = TREE_TYPE (decl);
25198 if (type == error_mark_node)
25199 return;
25201 if (is_naming_typedef_decl (TYPE_NAME (type)))
25203 /* Here, we are in the case of decl being a typedef naming
25204 an anonymous type, e.g:
25205 typedef struct {...} foo;
25206 In that case TREE_TYPE (decl) is not a typedef variant
25207 type and TYPE_NAME of the anonymous type is set to the
25208 TYPE_DECL of the typedef. This construct is emitted by
25209 the C++ FE.
25211 TYPE is the anonymous struct named by the typedef
25212 DECL. As we need the DW_AT_type attribute of the
25213 DW_TAG_typedef to point to the DIE of TYPE, let's
25214 generate that DIE right away. add_type_attribute
25215 called below will then pick (via lookup_type_die) that
25216 anonymous struct DIE. */
25217 if (!TREE_ASM_WRITTEN (type))
25218 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
25220 /* This is a GNU Extension. We are adding a
25221 DW_AT_linkage_name attribute to the DIE of the
25222 anonymous struct TYPE. The value of that attribute
25223 is the name of the typedef decl naming the anonymous
25224 struct. This greatly eases the work of consumers of
25225 this debug info. */
25226 add_linkage_name_raw (lookup_type_die (type), decl);
25230 add_type_attribute (type_die, type, decl_quals (decl), false,
25231 context_die);
25233 if (is_naming_typedef_decl (decl))
25234 /* We want that all subsequent calls to lookup_type_die with
25235 TYPE in argument yield the DW_TAG_typedef we have just
25236 created. */
25237 equate_type_number_to_die (type, type_die);
25239 add_alignment_attribute (type_die, TREE_TYPE (decl));
25241 add_accessibility_attribute (type_die, decl);
25243 if (DECL_ABSTRACT_P (decl))
25244 equate_decl_number_to_die (decl, type_die);
25246 if (get_AT (type_die, DW_AT_name))
25247 add_pubtype (decl, type_die);
25250 /* Generate a DIE for a struct, class, enum or union type. */
25252 static void
25253 gen_tagged_type_die (tree type,
25254 dw_die_ref context_die,
25255 enum debug_info_usage usage)
25257 int need_pop;
25259 if (type == NULL_TREE
25260 || !is_tagged_type (type))
25261 return;
25263 if (TREE_ASM_WRITTEN (type))
25264 need_pop = 0;
25265 /* If this is a nested type whose containing class hasn't been written
25266 out yet, writing it out will cover this one, too. This does not apply
25267 to instantiations of member class templates; they need to be added to
25268 the containing class as they are generated. FIXME: This hurts the
25269 idea of combining type decls from multiple TUs, since we can't predict
25270 what set of template instantiations we'll get. */
25271 else if (TYPE_CONTEXT (type)
25272 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
25273 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
25275 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
25277 if (TREE_ASM_WRITTEN (type))
25278 return;
25280 /* If that failed, attach ourselves to the stub. */
25281 push_decl_scope (TYPE_CONTEXT (type));
25282 context_die = lookup_type_die (TYPE_CONTEXT (type));
25283 need_pop = 1;
25285 else if (TYPE_CONTEXT (type) != NULL_TREE
25286 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
25288 /* If this type is local to a function that hasn't been written
25289 out yet, use a NULL context for now; it will be fixed up in
25290 decls_for_scope. */
25291 context_die = lookup_decl_die (TYPE_CONTEXT (type));
25292 /* A declaration DIE doesn't count; nested types need to go in the
25293 specification. */
25294 if (context_die && is_declaration_die (context_die))
25295 context_die = NULL;
25296 need_pop = 0;
25298 else
25300 context_die = declare_in_namespace (type, context_die);
25301 need_pop = 0;
25304 if (TREE_CODE (type) == ENUMERAL_TYPE)
25306 /* This might have been written out by the call to
25307 declare_in_namespace. */
25308 if (!TREE_ASM_WRITTEN (type))
25309 gen_enumeration_type_die (type, context_die);
25311 else
25312 gen_struct_or_union_type_die (type, context_die, usage);
25314 if (need_pop)
25315 pop_decl_scope ();
25317 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
25318 it up if it is ever completed. gen_*_type_die will set it for us
25319 when appropriate. */
25322 /* Generate a type description DIE. */
25324 static void
25325 gen_type_die_with_usage (tree type, dw_die_ref context_die,
25326 enum debug_info_usage usage)
25328 struct array_descr_info info;
25330 if (type == NULL_TREE || type == error_mark_node)
25331 return;
25333 if (flag_checking && type)
25334 verify_type (type);
25336 if (TYPE_NAME (type) != NULL_TREE
25337 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
25338 && is_redundant_typedef (TYPE_NAME (type))
25339 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
25340 /* The DECL of this type is a typedef we don't want to emit debug
25341 info for but we want debug info for its underlying typedef.
25342 This can happen for e.g, the injected-class-name of a C++
25343 type. */
25344 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
25346 /* If TYPE is a typedef type variant, let's generate debug info
25347 for the parent typedef which TYPE is a type of. */
25348 if (typedef_variant_p (type))
25350 if (TREE_ASM_WRITTEN (type))
25351 return;
25353 tree name = TYPE_NAME (type);
25354 tree origin = decl_ultimate_origin (name);
25355 if (origin != NULL && origin != name)
25357 gen_decl_die (origin, NULL, NULL, context_die);
25358 return;
25361 /* Prevent broken recursion; we can't hand off to the same type. */
25362 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
25364 /* Give typedefs the right scope. */
25365 context_die = scope_die_for (type, context_die);
25367 TREE_ASM_WRITTEN (type) = 1;
25369 gen_decl_die (name, NULL, NULL, context_die);
25370 return;
25373 /* If type is an anonymous tagged type named by a typedef, let's
25374 generate debug info for the typedef. */
25375 if (is_naming_typedef_decl (TYPE_NAME (type)))
25377 /* Use the DIE of the containing namespace as the parent DIE of
25378 the type description DIE we want to generate. */
25379 if (DECL_CONTEXT (TYPE_NAME (type))
25380 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
25381 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
25383 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
25384 return;
25387 if (lang_hooks.types.get_debug_type)
25389 tree debug_type = lang_hooks.types.get_debug_type (type);
25391 if (debug_type != NULL_TREE && debug_type != type)
25393 gen_type_die_with_usage (debug_type, context_die, usage);
25394 return;
25398 /* We are going to output a DIE to represent the unqualified version
25399 of this type (i.e. without any const or volatile qualifiers) so
25400 get the main variant (i.e. the unqualified version) of this type
25401 now. (Vectors and arrays are special because the debugging info is in the
25402 cloned type itself. Similarly function/method types can contain extra
25403 ref-qualification). */
25404 if (TREE_CODE (type) == FUNCTION_TYPE
25405 || TREE_CODE (type) == METHOD_TYPE)
25407 /* For function/method types, can't use type_main_variant here,
25408 because that can have different ref-qualifiers for C++,
25409 but try to canonicalize. */
25410 tree main = TYPE_MAIN_VARIANT (type);
25411 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
25412 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
25413 && check_base_type (t, main)
25414 && check_lang_type (t, type))
25416 type = t;
25417 break;
25420 else if (TREE_CODE (type) != VECTOR_TYPE
25421 && TREE_CODE (type) != ARRAY_TYPE)
25422 type = type_main_variant (type);
25424 /* If this is an array type with hidden descriptor, handle it first. */
25425 if (!TREE_ASM_WRITTEN (type)
25426 && lang_hooks.types.get_array_descr_info)
25428 memset (&info, 0, sizeof (info));
25429 if (lang_hooks.types.get_array_descr_info (type, &info))
25431 /* Fortran sometimes emits array types with no dimension. */
25432 gcc_assert (info.ndimensions >= 0
25433 && (info.ndimensions
25434 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
25435 gen_descr_array_type_die (type, &info, context_die);
25436 TREE_ASM_WRITTEN (type) = 1;
25437 return;
25441 if (TREE_ASM_WRITTEN (type))
25443 /* Variable-length types may be incomplete even if
25444 TREE_ASM_WRITTEN. For such types, fall through to
25445 gen_array_type_die() and possibly fill in
25446 DW_AT_{upper,lower}_bound attributes. */
25447 if ((TREE_CODE (type) != ARRAY_TYPE
25448 && TREE_CODE (type) != RECORD_TYPE
25449 && TREE_CODE (type) != UNION_TYPE
25450 && TREE_CODE (type) != QUAL_UNION_TYPE)
25451 || !variably_modified_type_p (type, NULL))
25452 return;
25455 switch (TREE_CODE (type))
25457 case ERROR_MARK:
25458 break;
25460 case POINTER_TYPE:
25461 case REFERENCE_TYPE:
25462 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
25463 ensures that the gen_type_die recursion will terminate even if the
25464 type is recursive. Recursive types are possible in Ada. */
25465 /* ??? We could perhaps do this for all types before the switch
25466 statement. */
25467 TREE_ASM_WRITTEN (type) = 1;
25469 /* For these types, all that is required is that we output a DIE (or a
25470 set of DIEs) to represent the "basis" type. */
25471 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25472 DINFO_USAGE_IND_USE);
25473 break;
25475 case OFFSET_TYPE:
25476 /* This code is used for C++ pointer-to-data-member types.
25477 Output a description of the relevant class type. */
25478 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
25479 DINFO_USAGE_IND_USE);
25481 /* Output a description of the type of the object pointed to. */
25482 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25483 DINFO_USAGE_IND_USE);
25485 /* Now output a DIE to represent this pointer-to-data-member type
25486 itself. */
25487 gen_ptr_to_mbr_type_die (type, context_die);
25488 break;
25490 case FUNCTION_TYPE:
25491 /* Force out return type (in case it wasn't forced out already). */
25492 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25493 DINFO_USAGE_DIR_USE);
25494 gen_subroutine_type_die (type, context_die);
25495 break;
25497 case METHOD_TYPE:
25498 /* Force out return type (in case it wasn't forced out already). */
25499 gen_type_die_with_usage (TREE_TYPE (type), context_die,
25500 DINFO_USAGE_DIR_USE);
25501 gen_subroutine_type_die (type, context_die);
25502 break;
25504 case ARRAY_TYPE:
25505 case VECTOR_TYPE:
25506 gen_array_type_die (type, context_die);
25507 break;
25509 case ENUMERAL_TYPE:
25510 case RECORD_TYPE:
25511 case UNION_TYPE:
25512 case QUAL_UNION_TYPE:
25513 gen_tagged_type_die (type, context_die, usage);
25514 return;
25516 case VOID_TYPE:
25517 case INTEGER_TYPE:
25518 case REAL_TYPE:
25519 case FIXED_POINT_TYPE:
25520 case COMPLEX_TYPE:
25521 case BOOLEAN_TYPE:
25522 case POINTER_BOUNDS_TYPE:
25523 /* No DIEs needed for fundamental types. */
25524 break;
25526 case NULLPTR_TYPE:
25527 case LANG_TYPE:
25528 /* Just use DW_TAG_unspecified_type. */
25530 dw_die_ref type_die = lookup_type_die (type);
25531 if (type_die == NULL)
25533 tree name = TYPE_IDENTIFIER (type);
25534 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
25535 type);
25536 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
25537 equate_type_number_to_die (type, type_die);
25540 break;
25542 default:
25543 if (is_cxx_auto (type))
25545 tree name = TYPE_IDENTIFIER (type);
25546 dw_die_ref *die = (name == get_identifier ("auto")
25547 ? &auto_die : &decltype_auto_die);
25548 if (!*die)
25550 *die = new_die (DW_TAG_unspecified_type,
25551 comp_unit_die (), NULL_TREE);
25552 add_name_attribute (*die, IDENTIFIER_POINTER (name));
25554 equate_type_number_to_die (type, *die);
25555 break;
25557 gcc_unreachable ();
25560 TREE_ASM_WRITTEN (type) = 1;
25563 static void
25564 gen_type_die (tree type, dw_die_ref context_die)
25566 if (type != error_mark_node)
25568 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
25569 if (flag_checking)
25571 dw_die_ref die = lookup_type_die (type);
25572 if (die)
25573 check_die (die);
25578 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
25579 things which are local to the given block. */
25581 static void
25582 gen_block_die (tree stmt, dw_die_ref context_die)
25584 int must_output_die = 0;
25585 bool inlined_func;
25587 /* Ignore blocks that are NULL. */
25588 if (stmt == NULL_TREE)
25589 return;
25591 inlined_func = inlined_function_outer_scope_p (stmt);
25593 /* If the block is one fragment of a non-contiguous block, do not
25594 process the variables, since they will have been done by the
25595 origin block. Do process subblocks. */
25596 if (BLOCK_FRAGMENT_ORIGIN (stmt))
25598 tree sub;
25600 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
25601 gen_block_die (sub, context_die);
25603 return;
25606 /* Determine if we need to output any Dwarf DIEs at all to represent this
25607 block. */
25608 if (inlined_func)
25609 /* The outer scopes for inlinings *must* always be represented. We
25610 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
25611 must_output_die = 1;
25612 else
25614 /* Determine if this block directly contains any "significant"
25615 local declarations which we will need to output DIEs for. */
25616 if (debug_info_level > DINFO_LEVEL_TERSE)
25617 /* We are not in terse mode so *any* local declaration counts
25618 as being a "significant" one. */
25619 must_output_die = ((BLOCK_VARS (stmt) != NULL
25620 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
25621 && (TREE_USED (stmt)
25622 || TREE_ASM_WRITTEN (stmt)
25623 || BLOCK_ABSTRACT (stmt)));
25624 else if ((TREE_USED (stmt)
25625 || TREE_ASM_WRITTEN (stmt)
25626 || BLOCK_ABSTRACT (stmt))
25627 && !dwarf2out_ignore_block (stmt))
25628 must_output_die = 1;
25631 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
25632 DIE for any block which contains no significant local declarations at
25633 all. Rather, in such cases we just call `decls_for_scope' so that any
25634 needed Dwarf info for any sub-blocks will get properly generated. Note
25635 that in terse mode, our definition of what constitutes a "significant"
25636 local declaration gets restricted to include only inlined function
25637 instances and local (nested) function definitions. */
25638 if (must_output_die)
25640 if (inlined_func)
25642 /* If STMT block is abstract, that means we have been called
25643 indirectly from dwarf2out_abstract_function.
25644 That function rightfully marks the descendent blocks (of
25645 the abstract function it is dealing with) as being abstract,
25646 precisely to prevent us from emitting any
25647 DW_TAG_inlined_subroutine DIE as a descendent
25648 of an abstract function instance. So in that case, we should
25649 not call gen_inlined_subroutine_die.
25651 Later though, when cgraph asks dwarf2out to emit info
25652 for the concrete instance of the function decl into which
25653 the concrete instance of STMT got inlined, the later will lead
25654 to the generation of a DW_TAG_inlined_subroutine DIE. */
25655 if (! BLOCK_ABSTRACT (stmt))
25656 gen_inlined_subroutine_die (stmt, context_die);
25658 else
25659 gen_lexical_block_die (stmt, context_die);
25661 else
25662 decls_for_scope (stmt, context_die);
25665 /* Process variable DECL (or variable with origin ORIGIN) within
25666 block STMT and add it to CONTEXT_DIE. */
25667 static void
25668 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
25670 dw_die_ref die;
25671 tree decl_or_origin = decl ? decl : origin;
25673 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
25674 die = lookup_decl_die (decl_or_origin);
25675 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
25677 if (TYPE_DECL_IS_STUB (decl_or_origin))
25678 die = lookup_type_die (TREE_TYPE (decl_or_origin));
25679 else
25680 die = lookup_decl_die (decl_or_origin);
25681 /* Avoid re-creating the DIE late if it was optimized as unused early. */
25682 if (! die && ! early_dwarf)
25683 return;
25685 else
25686 die = NULL;
25688 /* Avoid creating DIEs for local typedefs and concrete static variables that
25689 will only be pruned later. */
25690 if ((origin || decl_ultimate_origin (decl))
25691 && (TREE_CODE (decl_or_origin) == TYPE_DECL
25692 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
25694 origin = decl_ultimate_origin (decl_or_origin);
25695 if (decl && VAR_P (decl) && die != NULL)
25697 die = lookup_decl_die (origin);
25698 if (die != NULL)
25699 equate_decl_number_to_die (decl, die);
25701 return;
25704 if (die != NULL && die->die_parent == NULL)
25705 add_child_die (context_die, die);
25706 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25708 if (early_dwarf)
25709 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25710 stmt, context_die);
25712 else
25714 if (decl && DECL_P (decl))
25716 die = lookup_decl_die (decl);
25718 /* Early created DIEs do not have a parent as the decls refer
25719 to the function as DECL_CONTEXT rather than the BLOCK. */
25720 if (die && die->die_parent == NULL)
25722 gcc_assert (in_lto_p);
25723 add_child_die (context_die, die);
25727 gen_decl_die (decl, origin, NULL, context_die);
25731 /* Generate all of the decls declared within a given scope and (recursively)
25732 all of its sub-blocks. */
25734 static void
25735 decls_for_scope (tree stmt, dw_die_ref context_die)
25737 tree decl;
25738 unsigned int i;
25739 tree subblocks;
25741 /* Ignore NULL blocks. */
25742 if (stmt == NULL_TREE)
25743 return;
25745 /* Output the DIEs to represent all of the data objects and typedefs
25746 declared directly within this block but not within any nested
25747 sub-blocks. Also, nested function and tag DIEs have been
25748 generated with a parent of NULL; fix that up now. We don't
25749 have to do this if we're at -g1. */
25750 if (debug_info_level > DINFO_LEVEL_TERSE)
25752 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25753 process_scope_var (stmt, decl, NULL_TREE, context_die);
25754 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25755 origin - avoid doing this twice as we have no good way to see
25756 if we've done it once already. */
25757 if (! early_dwarf)
25758 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25760 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25761 if (decl == current_function_decl)
25762 /* Ignore declarations of the current function, while they
25763 are declarations, gen_subprogram_die would treat them
25764 as definitions again, because they are equal to
25765 current_function_decl and endlessly recurse. */;
25766 else if (TREE_CODE (decl) == FUNCTION_DECL)
25767 process_scope_var (stmt, decl, NULL_TREE, context_die);
25768 else
25769 process_scope_var (stmt, NULL_TREE, decl, context_die);
25773 /* Even if we're at -g1, we need to process the subblocks in order to get
25774 inlined call information. */
25776 /* Output the DIEs to represent all sub-blocks (and the items declared
25777 therein) of this block. */
25778 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25779 subblocks != NULL;
25780 subblocks = BLOCK_CHAIN (subblocks))
25781 gen_block_die (subblocks, context_die);
25784 /* Is this a typedef we can avoid emitting? */
25786 bool
25787 is_redundant_typedef (const_tree decl)
25789 if (TYPE_DECL_IS_STUB (decl))
25790 return true;
25792 if (DECL_ARTIFICIAL (decl)
25793 && DECL_CONTEXT (decl)
25794 && is_tagged_type (DECL_CONTEXT (decl))
25795 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25796 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25797 /* Also ignore the artificial member typedef for the class name. */
25798 return true;
25800 return false;
25803 /* Return TRUE if TYPE is a typedef that names a type for linkage
25804 purposes. This kind of typedefs is produced by the C++ FE for
25805 constructs like:
25807 typedef struct {...} foo;
25809 In that case, there is no typedef variant type produced for foo.
25810 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25811 struct type. */
25813 static bool
25814 is_naming_typedef_decl (const_tree decl)
25816 if (decl == NULL_TREE
25817 || TREE_CODE (decl) != TYPE_DECL
25818 || DECL_NAMELESS (decl)
25819 || !is_tagged_type (TREE_TYPE (decl))
25820 || DECL_IS_BUILTIN (decl)
25821 || is_redundant_typedef (decl)
25822 /* It looks like Ada produces TYPE_DECLs that are very similar
25823 to C++ naming typedefs but that have different
25824 semantics. Let's be specific to c++ for now. */
25825 || !is_cxx (decl))
25826 return FALSE;
25828 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25829 && TYPE_NAME (TREE_TYPE (decl)) == decl
25830 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25831 != TYPE_NAME (TREE_TYPE (decl))));
25834 /* Looks up the DIE for a context. */
25836 static inline dw_die_ref
25837 lookup_context_die (tree context)
25839 if (context)
25841 /* Find die that represents this context. */
25842 if (TYPE_P (context))
25844 context = TYPE_MAIN_VARIANT (context);
25845 dw_die_ref ctx = lookup_type_die (context);
25846 if (!ctx)
25847 return NULL;
25848 return strip_naming_typedef (context, ctx);
25850 else
25851 return lookup_decl_die (context);
25853 return comp_unit_die ();
25856 /* Returns the DIE for a context. */
25858 static inline dw_die_ref
25859 get_context_die (tree context)
25861 if (context)
25863 /* Find die that represents this context. */
25864 if (TYPE_P (context))
25866 context = TYPE_MAIN_VARIANT (context);
25867 return strip_naming_typedef (context, force_type_die (context));
25869 else
25870 return force_decl_die (context);
25872 return comp_unit_die ();
25875 /* Returns the DIE for decl. A DIE will always be returned. */
25877 static dw_die_ref
25878 force_decl_die (tree decl)
25880 dw_die_ref decl_die;
25881 unsigned saved_external_flag;
25882 tree save_fn = NULL_TREE;
25883 decl_die = lookup_decl_die (decl);
25884 if (!decl_die)
25886 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25888 decl_die = lookup_decl_die (decl);
25889 if (decl_die)
25890 return decl_die;
25892 switch (TREE_CODE (decl))
25894 case FUNCTION_DECL:
25895 /* Clear current_function_decl, so that gen_subprogram_die thinks
25896 that this is a declaration. At this point, we just want to force
25897 declaration die. */
25898 save_fn = current_function_decl;
25899 current_function_decl = NULL_TREE;
25900 gen_subprogram_die (decl, context_die);
25901 current_function_decl = save_fn;
25902 break;
25904 case VAR_DECL:
25905 /* Set external flag to force declaration die. Restore it after
25906 gen_decl_die() call. */
25907 saved_external_flag = DECL_EXTERNAL (decl);
25908 DECL_EXTERNAL (decl) = 1;
25909 gen_decl_die (decl, NULL, NULL, context_die);
25910 DECL_EXTERNAL (decl) = saved_external_flag;
25911 break;
25913 case NAMESPACE_DECL:
25914 if (dwarf_version >= 3 || !dwarf_strict)
25915 dwarf2out_decl (decl);
25916 else
25917 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25918 decl_die = comp_unit_die ();
25919 break;
25921 case TRANSLATION_UNIT_DECL:
25922 decl_die = comp_unit_die ();
25923 break;
25925 default:
25926 gcc_unreachable ();
25929 /* We should be able to find the DIE now. */
25930 if (!decl_die)
25931 decl_die = lookup_decl_die (decl);
25932 gcc_assert (decl_die);
25935 return decl_die;
25938 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25939 always returned. */
25941 static dw_die_ref
25942 force_type_die (tree type)
25944 dw_die_ref type_die;
25946 type_die = lookup_type_die (type);
25947 if (!type_die)
25949 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25951 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25952 false, context_die);
25953 gcc_assert (type_die);
25955 return type_die;
25958 /* Force out any required namespaces to be able to output DECL,
25959 and return the new context_die for it, if it's changed. */
25961 static dw_die_ref
25962 setup_namespace_context (tree thing, dw_die_ref context_die)
25964 tree context = (DECL_P (thing)
25965 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25966 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25967 /* Force out the namespace. */
25968 context_die = force_decl_die (context);
25970 return context_die;
25973 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25974 type) within its namespace, if appropriate.
25976 For compatibility with older debuggers, namespace DIEs only contain
25977 declarations; all definitions are emitted at CU scope, with
25978 DW_AT_specification pointing to the declaration (like with class
25979 members). */
25981 static dw_die_ref
25982 declare_in_namespace (tree thing, dw_die_ref context_die)
25984 dw_die_ref ns_context;
25986 if (debug_info_level <= DINFO_LEVEL_TERSE)
25987 return context_die;
25989 /* External declarations in the local scope only need to be emitted
25990 once, not once in the namespace and once in the scope.
25992 This avoids declaring the `extern' below in the
25993 namespace DIE as well as in the innermost scope:
25995 namespace S
25997 int i=5;
25998 int foo()
26000 int i=8;
26001 extern int i;
26002 return i;
26006 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
26007 return context_die;
26009 /* If this decl is from an inlined function, then don't try to emit it in its
26010 namespace, as we will get confused. It would have already been emitted
26011 when the abstract instance of the inline function was emitted anyways. */
26012 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
26013 return context_die;
26015 ns_context = setup_namespace_context (thing, context_die);
26017 if (ns_context != context_die)
26019 if (is_fortran ())
26020 return ns_context;
26021 if (DECL_P (thing))
26022 gen_decl_die (thing, NULL, NULL, ns_context);
26023 else
26024 gen_type_die (thing, ns_context);
26026 return context_die;
26029 /* Generate a DIE for a namespace or namespace alias. */
26031 static void
26032 gen_namespace_die (tree decl, dw_die_ref context_die)
26034 dw_die_ref namespace_die;
26036 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
26037 they are an alias of. */
26038 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
26040 /* Output a real namespace or module. */
26041 context_die = setup_namespace_context (decl, comp_unit_die ());
26042 namespace_die = new_die (is_fortran ()
26043 ? DW_TAG_module : DW_TAG_namespace,
26044 context_die, decl);
26045 /* For Fortran modules defined in different CU don't add src coords. */
26046 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
26048 const char *name = dwarf2_name (decl, 0);
26049 if (name)
26050 add_name_attribute (namespace_die, name);
26052 else
26053 add_name_and_src_coords_attributes (namespace_die, decl);
26054 if (DECL_EXTERNAL (decl))
26055 add_AT_flag (namespace_die, DW_AT_declaration, 1);
26056 equate_decl_number_to_die (decl, namespace_die);
26058 else
26060 /* Output a namespace alias. */
26062 /* Force out the namespace we are an alias of, if necessary. */
26063 dw_die_ref origin_die
26064 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
26066 if (DECL_FILE_SCOPE_P (decl)
26067 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
26068 context_die = setup_namespace_context (decl, comp_unit_die ());
26069 /* Now create the namespace alias DIE. */
26070 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
26071 add_name_and_src_coords_attributes (namespace_die, decl);
26072 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
26073 equate_decl_number_to_die (decl, namespace_die);
26075 if ((dwarf_version >= 5 || !dwarf_strict)
26076 && lang_hooks.decls.decl_dwarf_attribute (decl,
26077 DW_AT_export_symbols) == 1)
26078 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
26080 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
26081 if (want_pubnames ())
26082 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
26085 /* Generate Dwarf debug information for a decl described by DECL.
26086 The return value is currently only meaningful for PARM_DECLs,
26087 for all other decls it returns NULL.
26089 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
26090 It can be NULL otherwise. */
26092 static dw_die_ref
26093 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
26094 dw_die_ref context_die)
26096 tree decl_or_origin = decl ? decl : origin;
26097 tree class_origin = NULL, ultimate_origin;
26099 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
26100 return NULL;
26102 /* Ignore pointer bounds decls. */
26103 if (DECL_P (decl_or_origin)
26104 && TREE_TYPE (decl_or_origin)
26105 && POINTER_BOUNDS_P (decl_or_origin))
26106 return NULL;
26108 switch (TREE_CODE (decl_or_origin))
26110 case ERROR_MARK:
26111 break;
26113 case CONST_DECL:
26114 if (!is_fortran () && !is_ada ())
26116 /* The individual enumerators of an enum type get output when we output
26117 the Dwarf representation of the relevant enum type itself. */
26118 break;
26121 /* Emit its type. */
26122 gen_type_die (TREE_TYPE (decl), context_die);
26124 /* And its containing namespace. */
26125 context_die = declare_in_namespace (decl, context_die);
26127 gen_const_die (decl, context_die);
26128 break;
26130 case FUNCTION_DECL:
26131 #if 0
26132 /* FIXME */
26133 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
26134 on local redeclarations of global functions. That seems broken. */
26135 if (current_function_decl != decl)
26136 /* This is only a declaration. */;
26137 #endif
26139 /* We should have abstract copies already and should not generate
26140 stray type DIEs in late LTO dumping. */
26141 if (! early_dwarf)
26144 /* If we're emitting a clone, emit info for the abstract instance. */
26145 else if (origin || DECL_ORIGIN (decl) != decl)
26146 dwarf2out_abstract_function (origin
26147 ? DECL_ORIGIN (origin)
26148 : DECL_ABSTRACT_ORIGIN (decl));
26150 /* If we're emitting a possibly inlined function emit it as
26151 abstract instance. */
26152 else if (cgraph_function_possibly_inlined_p (decl)
26153 && ! DECL_ABSTRACT_P (decl)
26154 && ! class_or_namespace_scope_p (context_die)
26155 /* dwarf2out_abstract_function won't emit a die if this is just
26156 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
26157 that case, because that works only if we have a die. */
26158 && DECL_INITIAL (decl) != NULL_TREE)
26159 dwarf2out_abstract_function (decl);
26161 /* Otherwise we're emitting the primary DIE for this decl. */
26162 else if (debug_info_level > DINFO_LEVEL_TERSE)
26164 /* Before we describe the FUNCTION_DECL itself, make sure that we
26165 have its containing type. */
26166 if (!origin)
26167 origin = decl_class_context (decl);
26168 if (origin != NULL_TREE)
26169 gen_type_die (origin, context_die);
26171 /* And its return type. */
26172 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
26174 /* And its virtual context. */
26175 if (DECL_VINDEX (decl) != NULL_TREE)
26176 gen_type_die (DECL_CONTEXT (decl), context_die);
26178 /* Make sure we have a member DIE for decl. */
26179 if (origin != NULL_TREE)
26180 gen_type_die_for_member (origin, decl, context_die);
26182 /* And its containing namespace. */
26183 context_die = declare_in_namespace (decl, context_die);
26186 /* Now output a DIE to represent the function itself. */
26187 if (decl)
26188 gen_subprogram_die (decl, context_die);
26189 break;
26191 case TYPE_DECL:
26192 /* If we are in terse mode, don't generate any DIEs to represent any
26193 actual typedefs. */
26194 if (debug_info_level <= DINFO_LEVEL_TERSE)
26195 break;
26197 /* In the special case of a TYPE_DECL node representing the declaration
26198 of some type tag, if the given TYPE_DECL is marked as having been
26199 instantiated from some other (original) TYPE_DECL node (e.g. one which
26200 was generated within the original definition of an inline function) we
26201 used to generate a special (abbreviated) DW_TAG_structure_type,
26202 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
26203 should be actually referencing those DIEs, as variable DIEs with that
26204 type would be emitted already in the abstract origin, so it was always
26205 removed during unused type prunning. Don't add anything in this
26206 case. */
26207 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
26208 break;
26210 if (is_redundant_typedef (decl))
26211 gen_type_die (TREE_TYPE (decl), context_die);
26212 else
26213 /* Output a DIE to represent the typedef itself. */
26214 gen_typedef_die (decl, context_die);
26215 break;
26217 case LABEL_DECL:
26218 if (debug_info_level >= DINFO_LEVEL_NORMAL)
26219 gen_label_die (decl, context_die);
26220 break;
26222 case VAR_DECL:
26223 case RESULT_DECL:
26224 /* If we are in terse mode, don't generate any DIEs to represent any
26225 variable declarations or definitions. */
26226 if (debug_info_level <= DINFO_LEVEL_TERSE)
26227 break;
26229 /* Avoid generating stray type DIEs during late dwarf dumping.
26230 All types have been dumped early. */
26231 if (early_dwarf
26232 /* ??? But in LTRANS we cannot annotate early created variably
26233 modified type DIEs without copying them and adjusting all
26234 references to them. Dump them again as happens for inlining
26235 which copies both the decl and the types. */
26236 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26237 in VLA bound information for example. */
26238 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26239 current_function_decl)))
26241 /* Output any DIEs that are needed to specify the type of this data
26242 object. */
26243 if (decl_by_reference_p (decl_or_origin))
26244 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26245 else
26246 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26249 if (early_dwarf)
26251 /* And its containing type. */
26252 class_origin = decl_class_context (decl_or_origin);
26253 if (class_origin != NULL_TREE)
26254 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
26256 /* And its containing namespace. */
26257 context_die = declare_in_namespace (decl_or_origin, context_die);
26260 /* Now output the DIE to represent the data object itself. This gets
26261 complicated because of the possibility that the VAR_DECL really
26262 represents an inlined instance of a formal parameter for an inline
26263 function. */
26264 ultimate_origin = decl_ultimate_origin (decl_or_origin);
26265 if (ultimate_origin != NULL_TREE
26266 && TREE_CODE (ultimate_origin) == PARM_DECL)
26267 gen_formal_parameter_die (decl, origin,
26268 true /* Emit name attribute. */,
26269 context_die);
26270 else
26271 gen_variable_die (decl, origin, context_die);
26272 break;
26274 case FIELD_DECL:
26275 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
26276 /* Ignore the nameless fields that are used to skip bits but handle C++
26277 anonymous unions and structs. */
26278 if (DECL_NAME (decl) != NULL_TREE
26279 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
26280 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
26282 gen_type_die (member_declared_type (decl), context_die);
26283 gen_field_die (decl, ctx, context_die);
26285 break;
26287 case PARM_DECL:
26288 /* Avoid generating stray type DIEs during late dwarf dumping.
26289 All types have been dumped early. */
26290 if (early_dwarf
26291 /* ??? But in LTRANS we cannot annotate early created variably
26292 modified type DIEs without copying them and adjusting all
26293 references to them. Dump them again as happens for inlining
26294 which copies both the decl and the types. */
26295 /* ??? And even non-LTO needs to re-visit type DIEs to fill
26296 in VLA bound information for example. */
26297 || (decl && variably_modified_type_p (TREE_TYPE (decl),
26298 current_function_decl)))
26300 if (DECL_BY_REFERENCE (decl_or_origin))
26301 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
26302 else
26303 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
26305 return gen_formal_parameter_die (decl, origin,
26306 true /* Emit name attribute. */,
26307 context_die);
26309 case NAMESPACE_DECL:
26310 if (dwarf_version >= 3 || !dwarf_strict)
26311 gen_namespace_die (decl, context_die);
26312 break;
26314 case IMPORTED_DECL:
26315 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
26316 DECL_CONTEXT (decl), context_die);
26317 break;
26319 case NAMELIST_DECL:
26320 gen_namelist_decl (DECL_NAME (decl), context_die,
26321 NAMELIST_DECL_ASSOCIATED_DECL (decl));
26322 break;
26324 default:
26325 /* Probably some frontend-internal decl. Assume we don't care. */
26326 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
26327 break;
26330 return NULL;
26333 /* Output initial debug information for global DECL. Called at the
26334 end of the parsing process.
26336 This is the initial debug generation process. As such, the DIEs
26337 generated may be incomplete. A later debug generation pass
26338 (dwarf2out_late_global_decl) will augment the information generated
26339 in this pass (e.g., with complete location info). */
26341 static void
26342 dwarf2out_early_global_decl (tree decl)
26344 set_early_dwarf s;
26346 /* gen_decl_die() will set DECL_ABSTRACT because
26347 cgraph_function_possibly_inlined_p() returns true. This is in
26348 turn will cause DW_AT_inline attributes to be set.
26350 This happens because at early dwarf generation, there is no
26351 cgraph information, causing cgraph_function_possibly_inlined_p()
26352 to return true. Trick cgraph_function_possibly_inlined_p()
26353 while we generate dwarf early. */
26354 bool save = symtab->global_info_ready;
26355 symtab->global_info_ready = true;
26357 /* We don't handle TYPE_DECLs. If required, they'll be reached via
26358 other DECLs and they can point to template types or other things
26359 that dwarf2out can't handle when done via dwarf2out_decl. */
26360 if (TREE_CODE (decl) != TYPE_DECL
26361 && TREE_CODE (decl) != PARM_DECL)
26363 if (TREE_CODE (decl) == FUNCTION_DECL)
26365 tree save_fndecl = current_function_decl;
26367 /* For nested functions, make sure we have DIEs for the parents first
26368 so that all nested DIEs are generated at the proper scope in the
26369 first shot. */
26370 tree context = decl_function_context (decl);
26371 if (context != NULL)
26373 dw_die_ref context_die = lookup_decl_die (context);
26374 current_function_decl = context;
26376 /* Avoid emitting DIEs multiple times, but still process CONTEXT
26377 enough so that it lands in its own context. This avoids type
26378 pruning issues later on. */
26379 if (context_die == NULL || is_declaration_die (context_die))
26380 dwarf2out_decl (context);
26383 /* Emit an abstract origin of a function first. This happens
26384 with C++ constructor clones for example and makes
26385 dwarf2out_abstract_function happy which requires the early
26386 DIE of the abstract instance to be present. */
26387 tree origin = DECL_ABSTRACT_ORIGIN (decl);
26388 dw_die_ref origin_die;
26389 if (origin != NULL
26390 /* Do not emit the DIE multiple times but make sure to
26391 process it fully here in case we just saw a declaration. */
26392 && ((origin_die = lookup_decl_die (origin)) == NULL
26393 || is_declaration_die (origin_die)))
26395 current_function_decl = origin;
26396 dwarf2out_decl (origin);
26399 /* Emit the DIE for decl but avoid doing that multiple times. */
26400 dw_die_ref old_die;
26401 if ((old_die = lookup_decl_die (decl)) == NULL
26402 || is_declaration_die (old_die))
26404 current_function_decl = decl;
26405 dwarf2out_decl (decl);
26408 current_function_decl = save_fndecl;
26410 else
26411 dwarf2out_decl (decl);
26413 symtab->global_info_ready = save;
26416 /* Return whether EXPR is an expression with the following pattern:
26417 INDIRECT_REF (NOP_EXPR (INTEGER_CST)). */
26419 static bool
26420 is_trivial_indirect_ref (tree expr)
26422 if (expr == NULL_TREE || TREE_CODE (expr) != INDIRECT_REF)
26423 return false;
26425 tree nop = TREE_OPERAND (expr, 0);
26426 if (nop == NULL_TREE || TREE_CODE (nop) != NOP_EXPR)
26427 return false;
26429 tree int_cst = TREE_OPERAND (nop, 0);
26430 return int_cst != NULL_TREE && TREE_CODE (int_cst) == INTEGER_CST;
26433 /* Output debug information for global decl DECL. Called from
26434 toplev.c after compilation proper has finished. */
26436 static void
26437 dwarf2out_late_global_decl (tree decl)
26439 /* Fill-in any location information we were unable to determine
26440 on the first pass. */
26441 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
26443 dw_die_ref die = lookup_decl_die (decl);
26445 /* We may have to generate early debug late for LTO in case debug
26446 was not enabled at compile-time or the target doesn't support
26447 the LTO early debug scheme. */
26448 if (! die && in_lto_p)
26450 dwarf2out_decl (decl);
26451 die = lookup_decl_die (decl);
26454 if (die)
26456 /* We get called via the symtab code invoking late_global_decl
26457 for symbols that are optimized out.
26459 Do not add locations for those, except if they have a
26460 DECL_VALUE_EXPR, in which case they are relevant for debuggers.
26461 Still don't add a location if the DECL_VALUE_EXPR is not a trivial
26462 INDIRECT_REF expression, as this could generate relocations to
26463 text symbols in LTO object files, which is invalid. */
26464 varpool_node *node = varpool_node::get (decl);
26465 if ((! node || ! node->definition)
26466 && ! (DECL_HAS_VALUE_EXPR_P (decl)
26467 && is_trivial_indirect_ref (DECL_VALUE_EXPR (decl))))
26468 tree_add_const_value_attribute_for_decl (die, decl);
26469 else
26470 add_location_or_const_value_attribute (die, decl, false);
26475 /* Output debug information for type decl DECL. Called from toplev.c
26476 and from language front ends (to record built-in types). */
26477 static void
26478 dwarf2out_type_decl (tree decl, int local)
26480 if (!local)
26482 set_early_dwarf s;
26483 dwarf2out_decl (decl);
26487 /* Output debug information for imported module or decl DECL.
26488 NAME is non-NULL name in the lexical block if the decl has been renamed.
26489 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
26490 that DECL belongs to.
26491 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
26492 static void
26493 dwarf2out_imported_module_or_decl_1 (tree decl,
26494 tree name,
26495 tree lexical_block,
26496 dw_die_ref lexical_block_die)
26498 expanded_location xloc;
26499 dw_die_ref imported_die = NULL;
26500 dw_die_ref at_import_die;
26502 if (TREE_CODE (decl) == IMPORTED_DECL)
26504 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
26505 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
26506 gcc_assert (decl);
26508 else
26509 xloc = expand_location (input_location);
26511 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
26513 at_import_die = force_type_die (TREE_TYPE (decl));
26514 /* For namespace N { typedef void T; } using N::T; base_type_die
26515 returns NULL, but DW_TAG_imported_declaration requires
26516 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
26517 if (!at_import_die)
26519 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
26520 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
26521 at_import_die = lookup_type_die (TREE_TYPE (decl));
26522 gcc_assert (at_import_die);
26525 else
26527 at_import_die = lookup_decl_die (decl);
26528 if (!at_import_die)
26530 /* If we're trying to avoid duplicate debug info, we may not have
26531 emitted the member decl for this field. Emit it now. */
26532 if (TREE_CODE (decl) == FIELD_DECL)
26534 tree type = DECL_CONTEXT (decl);
26536 if (TYPE_CONTEXT (type)
26537 && TYPE_P (TYPE_CONTEXT (type))
26538 && !should_emit_struct_debug (TYPE_CONTEXT (type),
26539 DINFO_USAGE_DIR_USE))
26540 return;
26541 gen_type_die_for_member (type, decl,
26542 get_context_die (TYPE_CONTEXT (type)));
26544 if (TREE_CODE (decl) == NAMELIST_DECL)
26545 at_import_die = gen_namelist_decl (DECL_NAME (decl),
26546 get_context_die (DECL_CONTEXT (decl)),
26547 NULL_TREE);
26548 else
26549 at_import_die = force_decl_die (decl);
26553 if (TREE_CODE (decl) == NAMESPACE_DECL)
26555 if (dwarf_version >= 3 || !dwarf_strict)
26556 imported_die = new_die (DW_TAG_imported_module,
26557 lexical_block_die,
26558 lexical_block);
26559 else
26560 return;
26562 else
26563 imported_die = new_die (DW_TAG_imported_declaration,
26564 lexical_block_die,
26565 lexical_block);
26567 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
26568 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
26569 if (debug_column_info && xloc.column)
26570 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
26571 if (name)
26572 add_AT_string (imported_die, DW_AT_name,
26573 IDENTIFIER_POINTER (name));
26574 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
26577 /* Output debug information for imported module or decl DECL.
26578 NAME is non-NULL name in context if the decl has been renamed.
26579 CHILD is true if decl is one of the renamed decls as part of
26580 importing whole module.
26581 IMPLICIT is set if this hook is called for an implicit import
26582 such as inline namespace. */
26584 static void
26585 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
26586 bool child, bool implicit)
26588 /* dw_die_ref at_import_die; */
26589 dw_die_ref scope_die;
26591 if (debug_info_level <= DINFO_LEVEL_TERSE)
26592 return;
26594 gcc_assert (decl);
26596 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
26597 should be enough, for DWARF4 and older even if we emit as extension
26598 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
26599 for the benefit of consumers unaware of DW_AT_export_symbols. */
26600 if (implicit
26601 && dwarf_version >= 5
26602 && lang_hooks.decls.decl_dwarf_attribute (decl,
26603 DW_AT_export_symbols) == 1)
26604 return;
26606 set_early_dwarf s;
26608 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
26609 We need decl DIE for reference and scope die. First, get DIE for the decl
26610 itself. */
26612 /* Get the scope die for decl context. Use comp_unit_die for global module
26613 or decl. If die is not found for non globals, force new die. */
26614 if (context
26615 && TYPE_P (context)
26616 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
26617 return;
26619 scope_die = get_context_die (context);
26621 if (child)
26623 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
26624 there is nothing we can do, here. */
26625 if (dwarf_version < 3 && dwarf_strict)
26626 return;
26628 gcc_assert (scope_die->die_child);
26629 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
26630 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
26631 scope_die = scope_die->die_child;
26634 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
26635 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
26638 /* Output debug information for namelists. */
26640 static dw_die_ref
26641 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
26643 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
26644 tree value;
26645 unsigned i;
26647 if (debug_info_level <= DINFO_LEVEL_TERSE)
26648 return NULL;
26650 gcc_assert (scope_die != NULL);
26651 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
26652 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
26654 /* If there are no item_decls, we have a nondefining namelist, e.g.
26655 with USE association; hence, set DW_AT_declaration. */
26656 if (item_decls == NULL_TREE)
26658 add_AT_flag (nml_die, DW_AT_declaration, 1);
26659 return nml_die;
26662 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
26664 nml_item_ref_die = lookup_decl_die (value);
26665 if (!nml_item_ref_die)
26666 nml_item_ref_die = force_decl_die (value);
26668 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
26669 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
26671 return nml_die;
26675 /* Write the debugging output for DECL and return the DIE. */
26677 static void
26678 dwarf2out_decl (tree decl)
26680 dw_die_ref context_die = comp_unit_die ();
26682 switch (TREE_CODE (decl))
26684 case ERROR_MARK:
26685 return;
26687 case FUNCTION_DECL:
26688 /* If we're a nested function, initially use a parent of NULL; if we're
26689 a plain function, this will be fixed up in decls_for_scope. If
26690 we're a method, it will be ignored, since we already have a DIE. */
26691 if (decl_function_context (decl)
26692 /* But if we're in terse mode, we don't care about scope. */
26693 && debug_info_level > DINFO_LEVEL_TERSE)
26694 context_die = NULL;
26695 break;
26697 case VAR_DECL:
26698 /* For local statics lookup proper context die. */
26699 if (local_function_static (decl))
26700 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26702 /* If we are in terse mode, don't generate any DIEs to represent any
26703 variable declarations or definitions. */
26704 if (debug_info_level <= DINFO_LEVEL_TERSE)
26705 return;
26706 break;
26708 case CONST_DECL:
26709 if (debug_info_level <= DINFO_LEVEL_TERSE)
26710 return;
26711 if (!is_fortran () && !is_ada ())
26712 return;
26713 if (TREE_STATIC (decl) && decl_function_context (decl))
26714 context_die = lookup_decl_die (DECL_CONTEXT (decl));
26715 break;
26717 case NAMESPACE_DECL:
26718 case IMPORTED_DECL:
26719 if (debug_info_level <= DINFO_LEVEL_TERSE)
26720 return;
26721 if (lookup_decl_die (decl) != NULL)
26722 return;
26723 break;
26725 case TYPE_DECL:
26726 /* Don't emit stubs for types unless they are needed by other DIEs. */
26727 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26728 return;
26730 /* Don't bother trying to generate any DIEs to represent any of the
26731 normal built-in types for the language we are compiling. */
26732 if (DECL_IS_BUILTIN (decl))
26733 return;
26735 /* If we are in terse mode, don't generate any DIEs for types. */
26736 if (debug_info_level <= DINFO_LEVEL_TERSE)
26737 return;
26739 /* If we're a function-scope tag, initially use a parent of NULL;
26740 this will be fixed up in decls_for_scope. */
26741 if (decl_function_context (decl))
26742 context_die = NULL;
26744 break;
26746 case NAMELIST_DECL:
26747 break;
26749 default:
26750 return;
26753 gen_decl_die (decl, NULL, NULL, context_die);
26755 if (flag_checking)
26757 dw_die_ref die = lookup_decl_die (decl);
26758 if (die)
26759 check_die (die);
26763 /* Write the debugging output for DECL. */
26765 static void
26766 dwarf2out_function_decl (tree decl)
26768 dwarf2out_decl (decl);
26769 call_arg_locations = NULL;
26770 call_arg_loc_last = NULL;
26771 call_site_count = -1;
26772 tail_call_site_count = -1;
26773 decl_loc_table->empty ();
26774 cached_dw_loc_list_table->empty ();
26777 /* Output a marker (i.e. a label) for the beginning of the generated code for
26778 a lexical block. */
26780 static void
26781 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26782 unsigned int blocknum)
26784 switch_to_section (current_function_section ());
26785 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26788 /* Output a marker (i.e. a label) for the end of the generated code for a
26789 lexical block. */
26791 static void
26792 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26794 switch_to_section (current_function_section ());
26795 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26798 /* Returns nonzero if it is appropriate not to emit any debugging
26799 information for BLOCK, because it doesn't contain any instructions.
26801 Don't allow this for blocks with nested functions or local classes
26802 as we would end up with orphans, and in the presence of scheduling
26803 we may end up calling them anyway. */
26805 static bool
26806 dwarf2out_ignore_block (const_tree block)
26808 tree decl;
26809 unsigned int i;
26811 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26812 if (TREE_CODE (decl) == FUNCTION_DECL
26813 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26814 return 0;
26815 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26817 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26818 if (TREE_CODE (decl) == FUNCTION_DECL
26819 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26820 return 0;
26823 return 1;
26826 /* Hash table routines for file_hash. */
26828 bool
26829 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26831 return filename_cmp (p1->filename, p2) == 0;
26834 hashval_t
26835 dwarf_file_hasher::hash (dwarf_file_data *p)
26837 return htab_hash_string (p->filename);
26840 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26841 dwarf2out.c) and return its "index". The index of each (known) filename is
26842 just a unique number which is associated with only that one filename. We
26843 need such numbers for the sake of generating labels (in the .debug_sfnames
26844 section) and references to those files numbers (in the .debug_srcinfo
26845 and .debug_macinfo sections). If the filename given as an argument is not
26846 found in our current list, add it to the list and assign it the next
26847 available unique index number. */
26849 static struct dwarf_file_data *
26850 lookup_filename (const char *file_name)
26852 struct dwarf_file_data * created;
26854 if (!file_name)
26855 return NULL;
26857 dwarf_file_data **slot
26858 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26859 INSERT);
26860 if (*slot)
26861 return *slot;
26863 created = ggc_alloc<dwarf_file_data> ();
26864 created->filename = file_name;
26865 created->emitted_number = 0;
26866 *slot = created;
26867 return created;
26870 /* If the assembler will construct the file table, then translate the compiler
26871 internal file table number into the assembler file table number, and emit
26872 a .file directive if we haven't already emitted one yet. The file table
26873 numbers are different because we prune debug info for unused variables and
26874 types, which may include filenames. */
26876 static int
26877 maybe_emit_file (struct dwarf_file_data * fd)
26879 if (! fd->emitted_number)
26881 if (last_emitted_file)
26882 fd->emitted_number = last_emitted_file->emitted_number + 1;
26883 else
26884 fd->emitted_number = 1;
26885 last_emitted_file = fd;
26887 if (output_asm_line_debug_info ())
26889 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26890 output_quoted_string (asm_out_file,
26891 remap_debug_filename (fd->filename));
26892 fputc ('\n', asm_out_file);
26896 return fd->emitted_number;
26899 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26900 That generation should happen after function debug info has been
26901 generated. The value of the attribute is the constant value of ARG. */
26903 static void
26904 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26906 die_arg_entry entry;
26908 if (!die || !arg)
26909 return;
26911 gcc_assert (early_dwarf);
26913 if (!tmpl_value_parm_die_table)
26914 vec_alloc (tmpl_value_parm_die_table, 32);
26916 entry.die = die;
26917 entry.arg = arg;
26918 vec_safe_push (tmpl_value_parm_die_table, entry);
26921 /* Return TRUE if T is an instance of generic type, FALSE
26922 otherwise. */
26924 static bool
26925 generic_type_p (tree t)
26927 if (t == NULL_TREE || !TYPE_P (t))
26928 return false;
26929 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26932 /* Schedule the generation of the generic parameter dies for the
26933 instance of generic type T. The proper generation itself is later
26934 done by gen_scheduled_generic_parms_dies. */
26936 static void
26937 schedule_generic_params_dies_gen (tree t)
26939 if (!generic_type_p (t))
26940 return;
26942 gcc_assert (early_dwarf);
26944 if (!generic_type_instances)
26945 vec_alloc (generic_type_instances, 256);
26947 vec_safe_push (generic_type_instances, t);
26950 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26951 by append_entry_to_tmpl_value_parm_die_table. This function must
26952 be called after function DIEs have been generated. */
26954 static void
26955 gen_remaining_tmpl_value_param_die_attribute (void)
26957 if (tmpl_value_parm_die_table)
26959 unsigned i, j;
26960 die_arg_entry *e;
26962 /* We do this in two phases - first get the cases we can
26963 handle during early-finish, preserving those we cannot
26964 (containing symbolic constants where we don't yet know
26965 whether we are going to output the referenced symbols).
26966 For those we try again at late-finish. */
26967 j = 0;
26968 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26970 if (!e->die->removed
26971 && !tree_add_const_value_attribute (e->die, e->arg))
26973 dw_loc_descr_ref loc = NULL;
26974 if (! early_dwarf
26975 && (dwarf_version >= 5 || !dwarf_strict))
26976 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26977 if (loc)
26978 add_AT_loc (e->die, DW_AT_location, loc);
26979 else
26980 (*tmpl_value_parm_die_table)[j++] = *e;
26983 tmpl_value_parm_die_table->truncate (j);
26987 /* Generate generic parameters DIEs for instances of generic types
26988 that have been previously scheduled by
26989 schedule_generic_params_dies_gen. This function must be called
26990 after all the types of the CU have been laid out. */
26992 static void
26993 gen_scheduled_generic_parms_dies (void)
26995 unsigned i;
26996 tree t;
26998 if (!generic_type_instances)
26999 return;
27001 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
27002 if (COMPLETE_TYPE_P (t))
27003 gen_generic_params_dies (t);
27005 generic_type_instances = NULL;
27009 /* Replace DW_AT_name for the decl with name. */
27011 static void
27012 dwarf2out_set_name (tree decl, tree name)
27014 dw_die_ref die;
27015 dw_attr_node *attr;
27016 const char *dname;
27018 die = TYPE_SYMTAB_DIE (decl);
27019 if (!die)
27020 return;
27022 dname = dwarf2_name (name, 0);
27023 if (!dname)
27024 return;
27026 attr = get_AT (die, DW_AT_name);
27027 if (attr)
27029 struct indirect_string_node *node;
27031 node = find_AT_string (dname);
27032 /* replace the string. */
27033 attr->dw_attr_val.v.val_str = node;
27036 else
27037 add_name_attribute (die, dname);
27040 /* True if before or during processing of the first function being emitted. */
27041 static bool in_first_function_p = true;
27042 /* True if loc_note during dwarf2out_var_location call might still be
27043 before first real instruction at address equal to .Ltext0. */
27044 static bool maybe_at_text_label_p = true;
27045 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
27046 static unsigned int first_loclabel_num_not_at_text_label;
27048 /* Look ahead for a real insn, or for a begin stmt marker. */
27050 static rtx_insn *
27051 dwarf2out_next_real_insn (rtx_insn *loc_note)
27053 rtx_insn *next_real = NEXT_INSN (loc_note);
27055 while (next_real)
27056 if (INSN_P (next_real))
27057 break;
27058 else
27059 next_real = NEXT_INSN (next_real);
27061 return next_real;
27064 /* Called by the final INSN scan whenever we see a var location. We
27065 use it to drop labels in the right places, and throw the location in
27066 our lookup table. */
27068 static void
27069 dwarf2out_var_location (rtx_insn *loc_note)
27071 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
27072 struct var_loc_node *newloc;
27073 rtx_insn *next_real, *next_note;
27074 rtx_insn *call_insn = NULL;
27075 static const char *last_label;
27076 static const char *last_postcall_label;
27077 static bool last_in_cold_section_p;
27078 static rtx_insn *expected_next_loc_note;
27079 tree decl;
27080 bool var_loc_p;
27081 var_loc_view view = 0;
27083 if (!NOTE_P (loc_note))
27085 if (CALL_P (loc_note))
27087 maybe_reset_location_view (loc_note, cur_line_info_table);
27088 call_site_count++;
27089 if (SIBLING_CALL_P (loc_note))
27090 tail_call_site_count++;
27091 if (find_reg_note (loc_note, REG_CALL_ARG_LOCATION, NULL_RTX))
27093 call_insn = loc_note;
27094 loc_note = NULL;
27095 var_loc_p = false;
27097 next_real = dwarf2out_next_real_insn (call_insn);
27098 next_note = NULL;
27099 cached_next_real_insn = NULL;
27100 goto create_label;
27102 if (optimize == 0 && !flag_var_tracking)
27104 /* When the var-tracking pass is not running, there is no note
27105 for indirect calls whose target is compile-time known. In this
27106 case, process such calls specifically so that we generate call
27107 sites for them anyway. */
27108 rtx x = PATTERN (loc_note);
27109 if (GET_CODE (x) == PARALLEL)
27110 x = XVECEXP (x, 0, 0);
27111 if (GET_CODE (x) == SET)
27112 x = SET_SRC (x);
27113 if (GET_CODE (x) == CALL)
27114 x = XEXP (x, 0);
27115 if (!MEM_P (x)
27116 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
27117 || !SYMBOL_REF_DECL (XEXP (x, 0))
27118 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
27119 != FUNCTION_DECL))
27121 call_insn = loc_note;
27122 loc_note = NULL;
27123 var_loc_p = false;
27125 next_real = dwarf2out_next_real_insn (call_insn);
27126 next_note = NULL;
27127 cached_next_real_insn = NULL;
27128 goto create_label;
27132 else if (!debug_variable_location_views)
27133 gcc_unreachable ();
27134 else
27135 maybe_reset_location_view (loc_note, cur_line_info_table);
27137 return;
27140 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
27141 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
27142 return;
27144 /* Optimize processing a large consecutive sequence of location
27145 notes so we don't spend too much time in next_real_insn. If the
27146 next insn is another location note, remember the next_real_insn
27147 calculation for next time. */
27148 next_real = cached_next_real_insn;
27149 if (next_real)
27151 if (expected_next_loc_note != loc_note)
27152 next_real = NULL;
27155 next_note = NEXT_INSN (loc_note);
27156 if (! next_note
27157 || next_note->deleted ()
27158 || ! NOTE_P (next_note)
27159 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
27160 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
27161 && NOTE_KIND (next_note) != NOTE_INSN_INLINE_ENTRY))
27162 next_note = NULL;
27164 if (! next_real)
27165 next_real = dwarf2out_next_real_insn (loc_note);
27167 if (next_note)
27169 expected_next_loc_note = next_note;
27170 cached_next_real_insn = next_real;
27172 else
27173 cached_next_real_insn = NULL;
27175 /* If there are no instructions which would be affected by this note,
27176 don't do anything. */
27177 if (var_loc_p
27178 && next_real == NULL_RTX
27179 && !NOTE_DURING_CALL_P (loc_note))
27180 return;
27182 create_label:
27184 if (next_real == NULL_RTX)
27185 next_real = get_last_insn ();
27187 /* If there were any real insns between note we processed last time
27188 and this note (or if it is the first note), clear
27189 last_{,postcall_}label so that they are not reused this time. */
27190 if (last_var_location_insn == NULL_RTX
27191 || last_var_location_insn != next_real
27192 || last_in_cold_section_p != in_cold_section_p)
27194 last_label = NULL;
27195 last_postcall_label = NULL;
27198 if (var_loc_p)
27200 const char *label
27201 = NOTE_DURING_CALL_P (loc_note) ? last_postcall_label : last_label;
27202 view = cur_line_info_table->view;
27203 decl = NOTE_VAR_LOCATION_DECL (loc_note);
27204 newloc = add_var_loc_to_decl (decl, loc_note, label, view);
27205 if (newloc == NULL)
27206 return;
27208 else
27210 decl = NULL_TREE;
27211 newloc = NULL;
27214 /* If there were no real insns between note we processed last time
27215 and this note, use the label we emitted last time. Otherwise
27216 create a new label and emit it. */
27217 if (last_label == NULL)
27219 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
27220 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
27221 loclabel_num++;
27222 last_label = ggc_strdup (loclabel);
27223 /* See if loclabel might be equal to .Ltext0. If yes,
27224 bump first_loclabel_num_not_at_text_label. */
27225 if (!have_multiple_function_sections
27226 && in_first_function_p
27227 && maybe_at_text_label_p)
27229 static rtx_insn *last_start;
27230 rtx_insn *insn;
27231 for (insn = loc_note; insn; insn = previous_insn (insn))
27232 if (insn == last_start)
27233 break;
27234 else if (!NONDEBUG_INSN_P (insn))
27235 continue;
27236 else
27238 rtx body = PATTERN (insn);
27239 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
27240 continue;
27241 /* Inline asm could occupy zero bytes. */
27242 else if (GET_CODE (body) == ASM_INPUT
27243 || asm_noperands (body) >= 0)
27244 continue;
27245 #ifdef HAVE_ATTR_length /* ??? We don't include insn-attr.h. */
27246 else if (HAVE_ATTR_length && get_attr_min_length (insn) == 0)
27247 continue;
27248 #endif
27249 else
27251 /* Assume insn has non-zero length. */
27252 maybe_at_text_label_p = false;
27253 break;
27256 if (maybe_at_text_label_p)
27258 last_start = loc_note;
27259 first_loclabel_num_not_at_text_label = loclabel_num;
27264 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
27265 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
27267 if (!var_loc_p)
27269 struct call_arg_loc_node *ca_loc
27270 = ggc_cleared_alloc<call_arg_loc_node> ();
27271 rtx_insn *prev = call_insn;
27273 ca_loc->call_arg_loc_note
27274 = find_reg_note (call_insn, REG_CALL_ARG_LOCATION, NULL_RTX);
27275 ca_loc->next = NULL;
27276 ca_loc->label = last_label;
27277 gcc_assert (prev
27278 && (CALL_P (prev)
27279 || (NONJUMP_INSN_P (prev)
27280 && GET_CODE (PATTERN (prev)) == SEQUENCE
27281 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
27282 if (!CALL_P (prev))
27283 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
27284 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
27286 /* Look for a SYMBOL_REF in the "prev" instruction. */
27287 rtx x = get_call_rtx_from (PATTERN (prev));
27288 if (x)
27290 /* Try to get the call symbol, if any. */
27291 if (MEM_P (XEXP (x, 0)))
27292 x = XEXP (x, 0);
27293 /* First, look for a memory access to a symbol_ref. */
27294 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
27295 && SYMBOL_REF_DECL (XEXP (x, 0))
27296 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
27297 ca_loc->symbol_ref = XEXP (x, 0);
27298 /* Otherwise, look at a compile-time known user-level function
27299 declaration. */
27300 else if (MEM_P (x)
27301 && MEM_EXPR (x)
27302 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
27303 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
27306 ca_loc->block = insn_scope (prev);
27307 if (call_arg_locations)
27308 call_arg_loc_last->next = ca_loc;
27309 else
27310 call_arg_locations = ca_loc;
27311 call_arg_loc_last = ca_loc;
27313 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
27315 newloc->label = last_label;
27316 newloc->view = view;
27318 else
27320 if (!last_postcall_label)
27322 sprintf (loclabel, "%s-1", last_label);
27323 last_postcall_label = ggc_strdup (loclabel);
27325 newloc->label = last_postcall_label;
27326 /* ??? This view is at last_label, not last_label-1, but we
27327 could only assume view at last_label-1 is zero if we could
27328 assume calls always have length greater than one. This is
27329 probably true in general, though there might be a rare
27330 exception to this rule, e.g. if a call insn is optimized out
27331 by target magic. Then, even the -1 in the label will be
27332 wrong, which might invalidate the range. Anyway, using view,
27333 though technically possibly incorrect, will work as far as
27334 ranges go: since L-1 is in the middle of the call insn,
27335 (L-1).0 and (L-1).V shouldn't make any difference, and having
27336 the loclist entry refer to the .loc entry might be useful, so
27337 leave it like this. */
27338 newloc->view = view;
27341 if (var_loc_p && flag_debug_asm)
27343 const char *name, *sep, *patstr;
27344 if (decl && DECL_NAME (decl))
27345 name = IDENTIFIER_POINTER (DECL_NAME (decl));
27346 else
27347 name = "";
27348 if (NOTE_VAR_LOCATION_LOC (loc_note))
27350 sep = " => ";
27351 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
27353 else
27355 sep = " ";
27356 patstr = "RESET";
27358 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
27359 name, sep, patstr);
27362 last_var_location_insn = next_real;
27363 last_in_cold_section_p = in_cold_section_p;
27366 /* Check whether BLOCK, a lexical block, is nested within OUTER, or is
27367 OUTER itself. If BOTHWAYS, check not only that BLOCK can reach
27368 OUTER through BLOCK_SUPERCONTEXT links, but also that there is a
27369 path from OUTER to BLOCK through BLOCK_SUBBLOCKs and
27370 BLOCK_FRAGMENT_ORIGIN links. */
27371 static bool
27372 block_within_block_p (tree block, tree outer, bool bothways)
27374 if (block == outer)
27375 return true;
27377 /* Quickly check that OUTER is up BLOCK's supercontext chain. */
27378 for (tree context = BLOCK_SUPERCONTEXT (block);
27379 context != outer;
27380 context = BLOCK_SUPERCONTEXT (context))
27381 if (!context || TREE_CODE (context) != BLOCK)
27382 return false;
27384 if (!bothways)
27385 return true;
27387 /* Now check that each block is actually referenced by its
27388 parent. */
27389 for (tree context = BLOCK_SUPERCONTEXT (block); ;
27390 context = BLOCK_SUPERCONTEXT (context))
27392 if (BLOCK_FRAGMENT_ORIGIN (context))
27394 gcc_assert (!BLOCK_SUBBLOCKS (context));
27395 context = BLOCK_FRAGMENT_ORIGIN (context);
27397 for (tree sub = BLOCK_SUBBLOCKS (context);
27398 sub != block;
27399 sub = BLOCK_CHAIN (sub))
27400 if (!sub)
27401 return false;
27402 if (context == outer)
27403 return true;
27404 else
27405 block = context;
27409 /* Called during final while assembling the marker of the entry point
27410 for an inlined function. */
27412 static void
27413 dwarf2out_inline_entry (tree block)
27415 gcc_assert (debug_inline_points);
27417 /* If we can't represent it, don't bother. */
27418 if (!(dwarf_version >= 3 || !dwarf_strict))
27419 return;
27421 gcc_assert (DECL_P (block_ultimate_origin (block)));
27423 /* Sanity check the block tree. This would catch a case in which
27424 BLOCK got removed from the tree reachable from the outermost
27425 lexical block, but got retained in markers. It would still link
27426 back to its parents, but some ancestor would be missing a link
27427 down the path to the sub BLOCK. If the block got removed, its
27428 BLOCK_NUMBER will not be a usable value. */
27429 if (flag_checking)
27430 gcc_assert (block_within_block_p (block,
27431 DECL_INITIAL (current_function_decl),
27432 true));
27434 gcc_assert (inlined_function_outer_scope_p (block));
27435 gcc_assert (!BLOCK_DIE (block));
27437 if (BLOCK_FRAGMENT_ORIGIN (block))
27438 block = BLOCK_FRAGMENT_ORIGIN (block);
27439 /* Can the entry point ever not be at the beginning of an
27440 unfragmented lexical block? */
27441 else if (!(BLOCK_FRAGMENT_CHAIN (block)
27442 || (cur_line_info_table
27443 && !ZERO_VIEW_P (cur_line_info_table->view))))
27444 return;
27446 if (!inline_entry_data_table)
27447 inline_entry_data_table
27448 = hash_table<inline_entry_data_hasher>::create_ggc (10);
27451 inline_entry_data **iedp
27452 = inline_entry_data_table->find_slot_with_hash (block,
27453 htab_hash_pointer (block),
27454 INSERT);
27455 if (*iedp)
27456 /* ??? Ideally, we'd record all entry points for the same inlined
27457 function (some may have been duplicated by e.g. unrolling), but
27458 we have no way to represent that ATM. */
27459 return;
27461 inline_entry_data *ied = *iedp = ggc_cleared_alloc<inline_entry_data> ();
27462 ied->block = block;
27463 ied->label_pfx = BLOCK_INLINE_ENTRY_LABEL;
27464 ied->label_num = BLOCK_NUMBER (block);
27465 if (cur_line_info_table)
27466 ied->view = cur_line_info_table->view;
27468 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27470 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_INLINE_ENTRY_LABEL,
27471 BLOCK_NUMBER (block));
27472 ASM_OUTPUT_LABEL (asm_out_file, label);
27475 /* Called from finalize_size_functions for size functions so that their body
27476 can be encoded in the debug info to describe the layout of variable-length
27477 structures. */
27479 static void
27480 dwarf2out_size_function (tree decl)
27482 function_to_dwarf_procedure (decl);
27485 /* Note in one location list that text section has changed. */
27488 var_location_switch_text_section_1 (var_loc_list **slot, void *)
27490 var_loc_list *list = *slot;
27491 if (list->first)
27492 list->last_before_switch
27493 = list->last->next ? list->last->next : list->last;
27494 return 1;
27497 /* Note in all location lists that text section has changed. */
27499 static void
27500 var_location_switch_text_section (void)
27502 if (decl_loc_table == NULL)
27503 return;
27505 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
27508 /* Create a new line number table. */
27510 static dw_line_info_table *
27511 new_line_info_table (void)
27513 dw_line_info_table *table;
27515 table = ggc_cleared_alloc<dw_line_info_table> ();
27516 table->file_num = 1;
27517 table->line_num = 1;
27518 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
27519 FORCE_RESET_NEXT_VIEW (table->view);
27520 table->symviews_since_reset = 0;
27522 return table;
27525 /* Lookup the "current" table into which we emit line info, so
27526 that we don't have to do it for every source line. */
27528 static void
27529 set_cur_line_info_table (section *sec)
27531 dw_line_info_table *table;
27533 if (sec == text_section)
27534 table = text_section_line_info;
27535 else if (sec == cold_text_section)
27537 table = cold_text_section_line_info;
27538 if (!table)
27540 cold_text_section_line_info = table = new_line_info_table ();
27541 table->end_label = cold_end_label;
27544 else
27546 const char *end_label;
27548 if (crtl->has_bb_partition)
27550 if (in_cold_section_p)
27551 end_label = crtl->subsections.cold_section_end_label;
27552 else
27553 end_label = crtl->subsections.hot_section_end_label;
27555 else
27557 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27558 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
27559 current_function_funcdef_no);
27560 end_label = ggc_strdup (label);
27563 table = new_line_info_table ();
27564 table->end_label = end_label;
27566 vec_safe_push (separate_line_info, table);
27569 if (output_asm_line_debug_info ())
27570 table->is_stmt = (cur_line_info_table
27571 ? cur_line_info_table->is_stmt
27572 : DWARF_LINE_DEFAULT_IS_STMT_START);
27573 cur_line_info_table = table;
27577 /* We need to reset the locations at the beginning of each
27578 function. We can't do this in the end_function hook, because the
27579 declarations that use the locations won't have been output when
27580 that hook is called. Also compute have_multiple_function_sections here. */
27582 static void
27583 dwarf2out_begin_function (tree fun)
27585 section *sec = function_section (fun);
27587 if (sec != text_section)
27588 have_multiple_function_sections = true;
27590 if (crtl->has_bb_partition && !cold_text_section)
27592 gcc_assert (current_function_decl == fun);
27593 cold_text_section = unlikely_text_section ();
27594 switch_to_section (cold_text_section);
27595 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
27596 switch_to_section (sec);
27599 dwarf2out_note_section_used ();
27600 call_site_count = 0;
27601 tail_call_site_count = 0;
27603 set_cur_line_info_table (sec);
27604 FORCE_RESET_NEXT_VIEW (cur_line_info_table->view);
27607 /* Helper function of dwarf2out_end_function, called only after emitting
27608 the very first function into assembly. Check if some .debug_loc range
27609 might end with a .LVL* label that could be equal to .Ltext0.
27610 In that case we must force using absolute addresses in .debug_loc ranges,
27611 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
27612 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
27613 list terminator.
27614 Set have_multiple_function_sections to true in that case and
27615 terminate htab traversal. */
27618 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
27620 var_loc_list *entry = *slot;
27621 struct var_loc_node *node;
27623 node = entry->first;
27624 if (node && node->next && node->next->label)
27626 unsigned int i;
27627 const char *label = node->next->label;
27628 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
27630 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
27632 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
27633 if (strcmp (label, loclabel) == 0)
27635 have_multiple_function_sections = true;
27636 return 0;
27640 return 1;
27643 /* Hook called after emitting a function into assembly.
27644 This does something only for the very first function emitted. */
27646 static void
27647 dwarf2out_end_function (unsigned int)
27649 if (in_first_function_p
27650 && !have_multiple_function_sections
27651 && first_loclabel_num_not_at_text_label
27652 && decl_loc_table)
27653 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
27654 in_first_function_p = false;
27655 maybe_at_text_label_p = false;
27658 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
27659 front-ends register a translation unit even before dwarf2out_init is
27660 called. */
27661 static tree main_translation_unit = NULL_TREE;
27663 /* Hook called by front-ends after they built their main translation unit.
27664 Associate comp_unit_die to UNIT. */
27666 static void
27667 dwarf2out_register_main_translation_unit (tree unit)
27669 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
27670 && main_translation_unit == NULL_TREE);
27671 main_translation_unit = unit;
27672 /* If dwarf2out_init has not been called yet, it will perform the association
27673 itself looking at main_translation_unit. */
27674 if (decl_die_table != NULL)
27675 equate_decl_number_to_die (unit, comp_unit_die ());
27678 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
27680 static void
27681 push_dw_line_info_entry (dw_line_info_table *table,
27682 enum dw_line_info_opcode opcode, unsigned int val)
27684 dw_line_info_entry e;
27685 e.opcode = opcode;
27686 e.val = val;
27687 vec_safe_push (table->entries, e);
27690 /* Output a label to mark the beginning of a source code line entry
27691 and record information relating to this source line, in
27692 'line_info_table' for later output of the .debug_line section. */
27693 /* ??? The discriminator parameter ought to be unsigned. */
27695 static void
27696 dwarf2out_source_line (unsigned int line, unsigned int column,
27697 const char *filename,
27698 int discriminator, bool is_stmt)
27700 unsigned int file_num;
27701 dw_line_info_table *table;
27702 static var_loc_view lvugid;
27704 if (debug_info_level < DINFO_LEVEL_TERSE)
27705 return;
27707 table = cur_line_info_table;
27709 if (line == 0)
27711 if (debug_variable_location_views
27712 && output_asm_line_debug_info ()
27713 && table && !RESETTING_VIEW_P (table->view))
27715 /* If we're using the assembler to compute view numbers, we
27716 can't issue a .loc directive for line zero, so we can't
27717 get a view number at this point. We might attempt to
27718 compute it from the previous view, or equate it to a
27719 subsequent view (though it might not be there!), but
27720 since we're omitting the line number entry, we might as
27721 well omit the view number as well. That means pretending
27722 it's a view number zero, which might very well turn out
27723 to be correct. ??? Extend the assembler so that the
27724 compiler could emit e.g. ".locview .LVU#", to output a
27725 view without changing line number information. We'd then
27726 have to count it in symviews_since_reset; when it's omitted,
27727 it doesn't count. */
27728 if (!zero_view_p)
27729 zero_view_p = BITMAP_GGC_ALLOC ();
27730 bitmap_set_bit (zero_view_p, table->view);
27731 if (flag_debug_asm)
27733 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27734 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27735 fprintf (asm_out_file, "\t%s line 0, omitted view ",
27736 ASM_COMMENT_START);
27737 assemble_name (asm_out_file, label);
27738 putc ('\n', asm_out_file);
27740 table->view = ++lvugid;
27742 return;
27745 /* The discriminator column was added in dwarf4. Simplify the below
27746 by simply removing it if we're not supposed to output it. */
27747 if (dwarf_version < 4 && dwarf_strict)
27748 discriminator = 0;
27750 if (!debug_column_info)
27751 column = 0;
27753 file_num = maybe_emit_file (lookup_filename (filename));
27755 /* ??? TODO: Elide duplicate line number entries. Traditionally,
27756 the debugger has used the second (possibly duplicate) line number
27757 at the beginning of the function to mark the end of the prologue.
27758 We could eliminate any other duplicates within the function. For
27759 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
27760 that second line number entry. */
27761 /* Recall that this end-of-prologue indication is *not* the same thing
27762 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
27763 to which the hook corresponds, follows the last insn that was
27764 emitted by gen_prologue. What we need is to precede the first insn
27765 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
27766 insn that corresponds to something the user wrote. These may be
27767 very different locations once scheduling is enabled. */
27769 if (0 && file_num == table->file_num
27770 && line == table->line_num
27771 && column == table->column_num
27772 && discriminator == table->discrim_num
27773 && is_stmt == table->is_stmt)
27774 return;
27776 switch_to_section (current_function_section ());
27778 /* If requested, emit something human-readable. */
27779 if (flag_debug_asm)
27781 if (debug_column_info)
27782 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
27783 filename, line, column);
27784 else
27785 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
27786 filename, line);
27789 if (output_asm_line_debug_info ())
27791 /* Emit the .loc directive understood by GNU as. */
27792 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
27793 file_num, line, is_stmt, discriminator */
27794 fputs ("\t.loc ", asm_out_file);
27795 fprint_ul (asm_out_file, file_num);
27796 putc (' ', asm_out_file);
27797 fprint_ul (asm_out_file, line);
27798 putc (' ', asm_out_file);
27799 fprint_ul (asm_out_file, column);
27801 if (is_stmt != table->is_stmt)
27803 fputs (" is_stmt ", asm_out_file);
27804 putc (is_stmt ? '1' : '0', asm_out_file);
27806 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
27808 gcc_assert (discriminator > 0);
27809 fputs (" discriminator ", asm_out_file);
27810 fprint_ul (asm_out_file, (unsigned long) discriminator);
27812 if (debug_variable_location_views)
27814 if (!RESETTING_VIEW_P (table->view))
27816 table->symviews_since_reset++;
27817 if (table->symviews_since_reset > symview_upper_bound)
27818 symview_upper_bound = table->symviews_since_reset;
27819 /* When we're using the assembler to compute view
27820 numbers, we output symbolic labels after "view" in
27821 .loc directives, and the assembler will set them for
27822 us, so that we can refer to the view numbers in
27823 location lists. The only exceptions are when we know
27824 a view will be zero: "-0" is a forced reset, used
27825 e.g. in the beginning of functions, whereas "0" tells
27826 the assembler to check that there was a PC change
27827 since the previous view, in a way that implicitly
27828 resets the next view. */
27829 fputs (" view ", asm_out_file);
27830 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27831 ASM_GENERATE_INTERNAL_LABEL (label, "LVU", table->view);
27832 assemble_name (asm_out_file, label);
27833 table->view = ++lvugid;
27835 else
27837 table->symviews_since_reset = 0;
27838 if (FORCE_RESETTING_VIEW_P (table->view))
27839 fputs (" view -0", asm_out_file);
27840 else
27841 fputs (" view 0", asm_out_file);
27842 /* Mark the present view as a zero view. Earlier debug
27843 binds may have already added its id to loclists to be
27844 emitted later, so we can't reuse the id for something
27845 else. However, it's good to know whether a view is
27846 known to be zero, because then we may be able to
27847 optimize out locviews that are all zeros, so take
27848 note of it in zero_view_p. */
27849 if (!zero_view_p)
27850 zero_view_p = BITMAP_GGC_ALLOC ();
27851 bitmap_set_bit (zero_view_p, lvugid);
27852 table->view = ++lvugid;
27855 putc ('\n', asm_out_file);
27857 else
27859 unsigned int label_num = ++line_info_label_num;
27861 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
27863 if (debug_variable_location_views && !RESETTING_VIEW_P (table->view))
27864 push_dw_line_info_entry (table, LI_adv_address, label_num);
27865 else
27866 push_dw_line_info_entry (table, LI_set_address, label_num);
27867 if (debug_variable_location_views)
27869 bool resetting = FORCE_RESETTING_VIEW_P (table->view);
27870 if (resetting)
27871 table->view = 0;
27873 if (flag_debug_asm)
27874 fprintf (asm_out_file, "\t%s view %s%d\n",
27875 ASM_COMMENT_START,
27876 resetting ? "-" : "",
27877 table->view);
27879 table->view++;
27881 if (file_num != table->file_num)
27882 push_dw_line_info_entry (table, LI_set_file, file_num);
27883 if (discriminator != table->discrim_num)
27884 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
27885 if (is_stmt != table->is_stmt)
27886 push_dw_line_info_entry (table, LI_negate_stmt, 0);
27887 push_dw_line_info_entry (table, LI_set_line, line);
27888 if (debug_column_info)
27889 push_dw_line_info_entry (table, LI_set_column, column);
27892 table->file_num = file_num;
27893 table->line_num = line;
27894 table->column_num = column;
27895 table->discrim_num = discriminator;
27896 table->is_stmt = is_stmt;
27897 table->in_use = true;
27900 /* Record the beginning of a new source file. */
27902 static void
27903 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
27905 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27907 macinfo_entry e;
27908 e.code = DW_MACINFO_start_file;
27909 e.lineno = lineno;
27910 e.info = ggc_strdup (filename);
27911 vec_safe_push (macinfo_table, e);
27915 /* Record the end of a source file. */
27917 static void
27918 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
27920 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27922 macinfo_entry e;
27923 e.code = DW_MACINFO_end_file;
27924 e.lineno = lineno;
27925 e.info = NULL;
27926 vec_safe_push (macinfo_table, e);
27930 /* Called from debug_define in toplev.c. The `buffer' parameter contains
27931 the tail part of the directive line, i.e. the part which is past the
27932 initial whitespace, #, whitespace, directive-name, whitespace part. */
27934 static void
27935 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
27936 const char *buffer ATTRIBUTE_UNUSED)
27938 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27940 macinfo_entry e;
27941 /* Insert a dummy first entry to be able to optimize the whole
27942 predefined macro block using DW_MACRO_import. */
27943 if (macinfo_table->is_empty () && lineno <= 1)
27945 e.code = 0;
27946 e.lineno = 0;
27947 e.info = NULL;
27948 vec_safe_push (macinfo_table, e);
27950 e.code = DW_MACINFO_define;
27951 e.lineno = lineno;
27952 e.info = ggc_strdup (buffer);
27953 vec_safe_push (macinfo_table, e);
27957 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
27958 the tail part of the directive line, i.e. the part which is past the
27959 initial whitespace, #, whitespace, directive-name, whitespace part. */
27961 static void
27962 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
27963 const char *buffer ATTRIBUTE_UNUSED)
27965 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27967 macinfo_entry e;
27968 /* Insert a dummy first entry to be able to optimize the whole
27969 predefined macro block using DW_MACRO_import. */
27970 if (macinfo_table->is_empty () && lineno <= 1)
27972 e.code = 0;
27973 e.lineno = 0;
27974 e.info = NULL;
27975 vec_safe_push (macinfo_table, e);
27977 e.code = DW_MACINFO_undef;
27978 e.lineno = lineno;
27979 e.info = ggc_strdup (buffer);
27980 vec_safe_push (macinfo_table, e);
27984 /* Helpers to manipulate hash table of CUs. */
27986 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27988 static inline hashval_t hash (const macinfo_entry *);
27989 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27992 inline hashval_t
27993 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27995 return htab_hash_string (entry->info);
27998 inline bool
27999 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
28000 const macinfo_entry *entry2)
28002 return !strcmp (entry1->info, entry2->info);
28005 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
28007 /* Output a single .debug_macinfo entry. */
28009 static void
28010 output_macinfo_op (macinfo_entry *ref)
28012 int file_num;
28013 size_t len;
28014 struct indirect_string_node *node;
28015 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28016 struct dwarf_file_data *fd;
28018 switch (ref->code)
28020 case DW_MACINFO_start_file:
28021 fd = lookup_filename (ref->info);
28022 file_num = maybe_emit_file (fd);
28023 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
28024 dw2_asm_output_data_uleb128 (ref->lineno,
28025 "Included from line number %lu",
28026 (unsigned long) ref->lineno);
28027 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
28028 break;
28029 case DW_MACINFO_end_file:
28030 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
28031 break;
28032 case DW_MACINFO_define:
28033 case DW_MACINFO_undef:
28034 len = strlen (ref->info) + 1;
28035 if (!dwarf_strict
28036 && len > DWARF_OFFSET_SIZE
28037 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28038 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28040 ref->code = ref->code == DW_MACINFO_define
28041 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
28042 output_macinfo_op (ref);
28043 return;
28045 dw2_asm_output_data (1, ref->code,
28046 ref->code == DW_MACINFO_define
28047 ? "Define macro" : "Undefine macro");
28048 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28049 (unsigned long) ref->lineno);
28050 dw2_asm_output_nstring (ref->info, -1, "The macro");
28051 break;
28052 case DW_MACRO_define_strp:
28053 case DW_MACRO_undef_strp:
28054 node = find_AT_string (ref->info);
28055 gcc_assert (node
28056 && (node->form == DW_FORM_strp
28057 || node->form == DW_FORM_GNU_str_index));
28058 dw2_asm_output_data (1, ref->code,
28059 ref->code == DW_MACRO_define_strp
28060 ? "Define macro strp"
28061 : "Undefine macro strp");
28062 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
28063 (unsigned long) ref->lineno);
28064 if (node->form == DW_FORM_strp)
28065 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
28066 debug_str_section, "The macro: \"%s\"",
28067 ref->info);
28068 else
28069 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
28070 ref->info);
28071 break;
28072 case DW_MACRO_import:
28073 dw2_asm_output_data (1, ref->code, "Import");
28074 ASM_GENERATE_INTERNAL_LABEL (label,
28075 DEBUG_MACRO_SECTION_LABEL,
28076 ref->lineno + macinfo_label_base);
28077 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
28078 break;
28079 default:
28080 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
28081 ASM_COMMENT_START, (unsigned long) ref->code);
28082 break;
28086 /* Attempt to make a sequence of define/undef macinfo ops shareable with
28087 other compilation unit .debug_macinfo sections. IDX is the first
28088 index of a define/undef, return the number of ops that should be
28089 emitted in a comdat .debug_macinfo section and emit
28090 a DW_MACRO_import entry referencing it.
28091 If the define/undef entry should be emitted normally, return 0. */
28093 static unsigned
28094 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
28095 macinfo_hash_type **macinfo_htab)
28097 macinfo_entry *first, *second, *cur, *inc;
28098 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
28099 unsigned char checksum[16];
28100 struct md5_ctx ctx;
28101 char *grp_name, *tail;
28102 const char *base;
28103 unsigned int i, count, encoded_filename_len, linebuf_len;
28104 macinfo_entry **slot;
28106 first = &(*macinfo_table)[idx];
28107 second = &(*macinfo_table)[idx + 1];
28109 /* Optimize only if there are at least two consecutive define/undef ops,
28110 and either all of them are before first DW_MACINFO_start_file
28111 with lineno {0,1} (i.e. predefined macro block), or all of them are
28112 in some included header file. */
28113 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
28114 return 0;
28115 if (vec_safe_is_empty (files))
28117 if (first->lineno > 1 || second->lineno > 1)
28118 return 0;
28120 else if (first->lineno == 0)
28121 return 0;
28123 /* Find the last define/undef entry that can be grouped together
28124 with first and at the same time compute md5 checksum of their
28125 codes, linenumbers and strings. */
28126 md5_init_ctx (&ctx);
28127 for (i = idx; macinfo_table->iterate (i, &cur); i++)
28128 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
28129 break;
28130 else if (vec_safe_is_empty (files) && cur->lineno > 1)
28131 break;
28132 else
28134 unsigned char code = cur->code;
28135 md5_process_bytes (&code, 1, &ctx);
28136 checksum_uleb128 (cur->lineno, &ctx);
28137 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
28139 md5_finish_ctx (&ctx, checksum);
28140 count = i - idx;
28142 /* From the containing include filename (if any) pick up just
28143 usable characters from its basename. */
28144 if (vec_safe_is_empty (files))
28145 base = "";
28146 else
28147 base = lbasename (files->last ().info);
28148 for (encoded_filename_len = 0, i = 0; base[i]; i++)
28149 if (ISIDNUM (base[i]) || base[i] == '.')
28150 encoded_filename_len++;
28151 /* Count . at the end. */
28152 if (encoded_filename_len)
28153 encoded_filename_len++;
28155 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
28156 linebuf_len = strlen (linebuf);
28158 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
28159 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
28160 + 16 * 2 + 1);
28161 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
28162 tail = grp_name + 4;
28163 if (encoded_filename_len)
28165 for (i = 0; base[i]; i++)
28166 if (ISIDNUM (base[i]) || base[i] == '.')
28167 *tail++ = base[i];
28168 *tail++ = '.';
28170 memcpy (tail, linebuf, linebuf_len);
28171 tail += linebuf_len;
28172 *tail++ = '.';
28173 for (i = 0; i < 16; i++)
28174 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
28176 /* Construct a macinfo_entry for DW_MACRO_import
28177 in the empty vector entry before the first define/undef. */
28178 inc = &(*macinfo_table)[idx - 1];
28179 inc->code = DW_MACRO_import;
28180 inc->lineno = 0;
28181 inc->info = ggc_strdup (grp_name);
28182 if (!*macinfo_htab)
28183 *macinfo_htab = new macinfo_hash_type (10);
28184 /* Avoid emitting duplicates. */
28185 slot = (*macinfo_htab)->find_slot (inc, INSERT);
28186 if (*slot != NULL)
28188 inc->code = 0;
28189 inc->info = NULL;
28190 /* If such an entry has been used before, just emit
28191 a DW_MACRO_import op. */
28192 inc = *slot;
28193 output_macinfo_op (inc);
28194 /* And clear all macinfo_entry in the range to avoid emitting them
28195 in the second pass. */
28196 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
28198 cur->code = 0;
28199 cur->info = NULL;
28202 else
28204 *slot = inc;
28205 inc->lineno = (*macinfo_htab)->elements ();
28206 output_macinfo_op (inc);
28208 return count;
28211 /* Save any strings needed by the macinfo table in the debug str
28212 table. All strings must be collected into the table by the time
28213 index_string is called. */
28215 static void
28216 save_macinfo_strings (void)
28218 unsigned len;
28219 unsigned i;
28220 macinfo_entry *ref;
28222 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
28224 switch (ref->code)
28226 /* Match the logic in output_macinfo_op to decide on
28227 indirect strings. */
28228 case DW_MACINFO_define:
28229 case DW_MACINFO_undef:
28230 len = strlen (ref->info) + 1;
28231 if (!dwarf_strict
28232 && len > DWARF_OFFSET_SIZE
28233 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
28234 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
28235 set_indirect_string (find_AT_string (ref->info));
28236 break;
28237 case DW_MACRO_define_strp:
28238 case DW_MACRO_undef_strp:
28239 set_indirect_string (find_AT_string (ref->info));
28240 break;
28241 default:
28242 break;
28247 /* Output macinfo section(s). */
28249 static void
28250 output_macinfo (const char *debug_line_label, bool early_lto_debug)
28252 unsigned i;
28253 unsigned long length = vec_safe_length (macinfo_table);
28254 macinfo_entry *ref;
28255 vec<macinfo_entry, va_gc> *files = NULL;
28256 macinfo_hash_type *macinfo_htab = NULL;
28257 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
28259 if (! length)
28260 return;
28262 /* output_macinfo* uses these interchangeably. */
28263 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
28264 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
28265 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
28266 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
28268 /* AIX Assembler inserts the length, so adjust the reference to match the
28269 offset expected by debuggers. */
28270 strcpy (dl_section_ref, debug_line_label);
28271 if (XCOFF_DEBUGGING_INFO)
28272 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
28274 /* For .debug_macro emit the section header. */
28275 if (!dwarf_strict || dwarf_version >= 5)
28277 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28278 "DWARF macro version number");
28279 if (DWARF_OFFSET_SIZE == 8)
28280 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
28281 else
28282 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
28283 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
28284 debug_line_section, NULL);
28287 /* In the first loop, it emits the primary .debug_macinfo section
28288 and after each emitted op the macinfo_entry is cleared.
28289 If a longer range of define/undef ops can be optimized using
28290 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
28291 the vector before the first define/undef in the range and the
28292 whole range of define/undef ops is not emitted and kept. */
28293 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28295 switch (ref->code)
28297 case DW_MACINFO_start_file:
28298 vec_safe_push (files, *ref);
28299 break;
28300 case DW_MACINFO_end_file:
28301 if (!vec_safe_is_empty (files))
28302 files->pop ();
28303 break;
28304 case DW_MACINFO_define:
28305 case DW_MACINFO_undef:
28306 if ((!dwarf_strict || dwarf_version >= 5)
28307 && HAVE_COMDAT_GROUP
28308 && vec_safe_length (files) != 1
28309 && i > 0
28310 && i + 1 < length
28311 && (*macinfo_table)[i - 1].code == 0)
28313 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
28314 if (count)
28316 i += count - 1;
28317 continue;
28320 break;
28321 case 0:
28322 /* A dummy entry may be inserted at the beginning to be able
28323 to optimize the whole block of predefined macros. */
28324 if (i == 0)
28325 continue;
28326 default:
28327 break;
28329 output_macinfo_op (ref);
28330 ref->info = NULL;
28331 ref->code = 0;
28334 if (!macinfo_htab)
28335 return;
28337 /* Save the number of transparent includes so we can adjust the
28338 label number for the fat LTO object DWARF. */
28339 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
28341 delete macinfo_htab;
28342 macinfo_htab = NULL;
28344 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
28345 terminate the current chain and switch to a new comdat .debug_macinfo
28346 section and emit the define/undef entries within it. */
28347 for (i = 0; macinfo_table->iterate (i, &ref); i++)
28348 switch (ref->code)
28350 case 0:
28351 continue;
28352 case DW_MACRO_import:
28354 char label[MAX_ARTIFICIAL_LABEL_BYTES];
28355 tree comdat_key = get_identifier (ref->info);
28356 /* Terminate the previous .debug_macinfo section. */
28357 dw2_asm_output_data (1, 0, "End compilation unit");
28358 targetm.asm_out.named_section (debug_macinfo_section_name,
28359 SECTION_DEBUG
28360 | SECTION_LINKONCE
28361 | (early_lto_debug
28362 ? SECTION_EXCLUDE : 0),
28363 comdat_key);
28364 ASM_GENERATE_INTERNAL_LABEL (label,
28365 DEBUG_MACRO_SECTION_LABEL,
28366 ref->lineno + macinfo_label_base);
28367 ASM_OUTPUT_LABEL (asm_out_file, label);
28368 ref->code = 0;
28369 ref->info = NULL;
28370 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
28371 "DWARF macro version number");
28372 if (DWARF_OFFSET_SIZE == 8)
28373 dw2_asm_output_data (1, 1, "Flags: 64-bit");
28374 else
28375 dw2_asm_output_data (1, 0, "Flags: 32-bit");
28377 break;
28378 case DW_MACINFO_define:
28379 case DW_MACINFO_undef:
28380 output_macinfo_op (ref);
28381 ref->code = 0;
28382 ref->info = NULL;
28383 break;
28384 default:
28385 gcc_unreachable ();
28388 macinfo_label_base += macinfo_label_base_adj;
28391 /* Initialize the various sections and labels for dwarf output and prefix
28392 them with PREFIX if non-NULL. Returns the generation (zero based
28393 number of times function was called). */
28395 static unsigned
28396 init_sections_and_labels (bool early_lto_debug)
28398 /* As we may get called multiple times have a generation count for
28399 labels. */
28400 static unsigned generation = 0;
28402 if (early_lto_debug)
28404 if (!dwarf_split_debug_info)
28406 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28407 SECTION_DEBUG | SECTION_EXCLUDE,
28408 NULL);
28409 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
28410 SECTION_DEBUG | SECTION_EXCLUDE,
28411 NULL);
28412 debug_macinfo_section_name
28413 = ((dwarf_strict && dwarf_version < 5)
28414 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
28415 debug_macinfo_section = get_section (debug_macinfo_section_name,
28416 SECTION_DEBUG
28417 | SECTION_EXCLUDE, NULL);
28419 else
28421 /* ??? Which of the following do we need early? */
28422 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
28423 SECTION_DEBUG | SECTION_EXCLUDE,
28424 NULL);
28425 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
28426 SECTION_DEBUG | SECTION_EXCLUDE,
28427 NULL);
28428 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
28429 SECTION_DEBUG
28430 | SECTION_EXCLUDE, NULL);
28431 debug_skeleton_abbrev_section
28432 = get_section (DEBUG_LTO_ABBREV_SECTION,
28433 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28434 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28435 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28436 generation);
28438 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28439 stay in the main .o, but the skeleton_line goes into the split
28440 off dwo. */
28441 debug_skeleton_line_section
28442 = get_section (DEBUG_LTO_LINE_SECTION,
28443 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28444 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28445 DEBUG_SKELETON_LINE_SECTION_LABEL,
28446 generation);
28447 debug_str_offsets_section
28448 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
28449 SECTION_DEBUG | SECTION_EXCLUDE,
28450 NULL);
28451 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28452 DEBUG_SKELETON_INFO_SECTION_LABEL,
28453 generation);
28454 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
28455 DEBUG_STR_DWO_SECTION_FLAGS,
28456 NULL);
28457 debug_macinfo_section_name
28458 = ((dwarf_strict && dwarf_version < 5)
28459 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
28460 debug_macinfo_section = get_section (debug_macinfo_section_name,
28461 SECTION_DEBUG | SECTION_EXCLUDE,
28462 NULL);
28464 /* For macro info and the file table we have to refer to a
28465 debug_line section. */
28466 debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
28467 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28468 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28469 DEBUG_LINE_SECTION_LABEL, generation);
28471 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
28472 DEBUG_STR_SECTION_FLAGS
28473 | SECTION_EXCLUDE, NULL);
28474 if (!dwarf_split_debug_info && !dwarf2out_as_loc_support)
28475 debug_line_str_section
28476 = get_section (DEBUG_LTO_LINE_STR_SECTION,
28477 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
28479 else
28481 if (!dwarf_split_debug_info)
28483 debug_info_section = get_section (DEBUG_INFO_SECTION,
28484 SECTION_DEBUG, NULL);
28485 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28486 SECTION_DEBUG, NULL);
28487 debug_loc_section = get_section (dwarf_version >= 5
28488 ? DEBUG_LOCLISTS_SECTION
28489 : DEBUG_LOC_SECTION,
28490 SECTION_DEBUG, NULL);
28491 debug_macinfo_section_name
28492 = ((dwarf_strict && dwarf_version < 5)
28493 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
28494 debug_macinfo_section = get_section (debug_macinfo_section_name,
28495 SECTION_DEBUG, NULL);
28497 else
28499 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
28500 SECTION_DEBUG | SECTION_EXCLUDE,
28501 NULL);
28502 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
28503 SECTION_DEBUG | SECTION_EXCLUDE,
28504 NULL);
28505 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
28506 SECTION_DEBUG, NULL);
28507 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
28508 SECTION_DEBUG, NULL);
28509 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
28510 SECTION_DEBUG, NULL);
28511 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
28512 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
28513 generation);
28515 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
28516 stay in the main .o, but the skeleton_line goes into the
28517 split off dwo. */
28518 debug_skeleton_line_section
28519 = get_section (DEBUG_DWO_LINE_SECTION,
28520 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28521 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
28522 DEBUG_SKELETON_LINE_SECTION_LABEL,
28523 generation);
28524 debug_str_offsets_section
28525 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
28526 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
28527 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
28528 DEBUG_SKELETON_INFO_SECTION_LABEL,
28529 generation);
28530 debug_loc_section = get_section (dwarf_version >= 5
28531 ? DEBUG_DWO_LOCLISTS_SECTION
28532 : DEBUG_DWO_LOC_SECTION,
28533 SECTION_DEBUG | SECTION_EXCLUDE,
28534 NULL);
28535 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
28536 DEBUG_STR_DWO_SECTION_FLAGS,
28537 NULL);
28538 debug_macinfo_section_name
28539 = ((dwarf_strict && dwarf_version < 5)
28540 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
28541 debug_macinfo_section = get_section (debug_macinfo_section_name,
28542 SECTION_DEBUG | SECTION_EXCLUDE,
28543 NULL);
28545 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
28546 SECTION_DEBUG, NULL);
28547 debug_line_section = get_section (DEBUG_LINE_SECTION,
28548 SECTION_DEBUG, NULL);
28549 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
28550 SECTION_DEBUG, NULL);
28551 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
28552 SECTION_DEBUG, NULL);
28553 debug_str_section = get_section (DEBUG_STR_SECTION,
28554 DEBUG_STR_SECTION_FLAGS, NULL);
28555 if (!dwarf_split_debug_info && !output_asm_line_debug_info ())
28556 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
28557 DEBUG_STR_SECTION_FLAGS, NULL);
28559 debug_ranges_section = get_section (dwarf_version >= 5
28560 ? DEBUG_RNGLISTS_SECTION
28561 : DEBUG_RANGES_SECTION,
28562 SECTION_DEBUG, NULL);
28563 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
28564 SECTION_DEBUG, NULL);
28567 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
28568 DEBUG_ABBREV_SECTION_LABEL, generation);
28569 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
28570 DEBUG_INFO_SECTION_LABEL, generation);
28571 info_section_emitted = false;
28572 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
28573 DEBUG_LINE_SECTION_LABEL, generation);
28574 /* There are up to 4 unique ranges labels per generation.
28575 See also output_rnglists. */
28576 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
28577 DEBUG_RANGES_SECTION_LABEL, generation * 4);
28578 if (dwarf_version >= 5 && dwarf_split_debug_info)
28579 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
28580 DEBUG_RANGES_SECTION_LABEL,
28581 1 + generation * 4);
28582 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
28583 DEBUG_ADDR_SECTION_LABEL, generation);
28584 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
28585 (dwarf_strict && dwarf_version < 5)
28586 ? DEBUG_MACINFO_SECTION_LABEL
28587 : DEBUG_MACRO_SECTION_LABEL, generation);
28588 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
28589 generation);
28591 ++generation;
28592 return generation - 1;
28595 /* Set up for Dwarf output at the start of compilation. */
28597 static void
28598 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
28600 /* Allocate the file_table. */
28601 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
28603 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28604 /* Allocate the decl_die_table. */
28605 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
28607 /* Allocate the decl_loc_table. */
28608 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
28610 /* Allocate the cached_dw_loc_list_table. */
28611 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
28613 /* Allocate the initial hunk of the decl_scope_table. */
28614 vec_alloc (decl_scope_table, 256);
28616 /* Allocate the initial hunk of the abbrev_die_table. */
28617 vec_alloc (abbrev_die_table, 256);
28618 /* Zero-th entry is allocated, but unused. */
28619 abbrev_die_table->quick_push (NULL);
28621 /* Allocate the dwarf_proc_stack_usage_map. */
28622 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
28624 /* Allocate the pubtypes and pubnames vectors. */
28625 vec_alloc (pubname_table, 32);
28626 vec_alloc (pubtype_table, 32);
28628 vec_alloc (incomplete_types, 64);
28630 vec_alloc (used_rtx_array, 32);
28632 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
28633 vec_alloc (macinfo_table, 64);
28634 #endif
28636 /* If front-ends already registered a main translation unit but we were not
28637 ready to perform the association, do this now. */
28638 if (main_translation_unit != NULL_TREE)
28639 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
28642 /* Called before compile () starts outputtting functions, variables
28643 and toplevel asms into assembly. */
28645 static void
28646 dwarf2out_assembly_start (void)
28648 if (text_section_line_info)
28649 return;
28651 #ifndef DWARF2_LINENO_DEBUGGING_INFO
28652 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
28653 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
28654 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
28655 COLD_TEXT_SECTION_LABEL, 0);
28656 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
28658 switch_to_section (text_section);
28659 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
28660 #endif
28662 /* Make sure the line number table for .text always exists. */
28663 text_section_line_info = new_line_info_table ();
28664 text_section_line_info->end_label = text_end_label;
28666 #ifdef DWARF2_LINENO_DEBUGGING_INFO
28667 cur_line_info_table = text_section_line_info;
28668 #endif
28670 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
28671 && dwarf2out_do_cfi_asm ()
28672 && !dwarf2out_do_eh_frame ())
28673 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
28676 /* A helper function for dwarf2out_finish called through
28677 htab_traverse. Assign a string its index. All strings must be
28678 collected into the table by the time index_string is called,
28679 because the indexing code relies on htab_traverse to traverse nodes
28680 in the same order for each run. */
28683 index_string (indirect_string_node **h, unsigned int *index)
28685 indirect_string_node *node = *h;
28687 find_string_form (node);
28688 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28690 gcc_assert (node->index == NO_INDEX_ASSIGNED);
28691 node->index = *index;
28692 *index += 1;
28694 return 1;
28697 /* A helper function for output_indirect_strings called through
28698 htab_traverse. Output the offset to a string and update the
28699 current offset. */
28702 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
28704 indirect_string_node *node = *h;
28706 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28708 /* Assert that this node has been assigned an index. */
28709 gcc_assert (node->index != NO_INDEX_ASSIGNED
28710 && node->index != NOT_INDEXED);
28711 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
28712 "indexed string 0x%x: %s", node->index, node->str);
28713 *offset += strlen (node->str) + 1;
28715 return 1;
28718 /* A helper function for dwarf2out_finish called through
28719 htab_traverse. Output the indexed string. */
28722 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
28724 struct indirect_string_node *node = *h;
28726 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
28728 /* Assert that the strings are output in the same order as their
28729 indexes were assigned. */
28730 gcc_assert (*cur_idx == node->index);
28731 assemble_string (node->str, strlen (node->str) + 1);
28732 *cur_idx += 1;
28734 return 1;
28737 /* A helper function for dwarf2out_finish called through
28738 htab_traverse. Emit one queued .debug_str string. */
28741 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
28743 struct indirect_string_node *node = *h;
28745 node->form = find_string_form (node);
28746 if (node->form == form && node->refcount > 0)
28748 ASM_OUTPUT_LABEL (asm_out_file, node->label);
28749 assemble_string (node->str, strlen (node->str) + 1);
28752 return 1;
28755 /* Output the indexed string table. */
28757 static void
28758 output_indirect_strings (void)
28760 switch_to_section (debug_str_section);
28761 if (!dwarf_split_debug_info)
28762 debug_str_hash->traverse<enum dwarf_form,
28763 output_indirect_string> (DW_FORM_strp);
28764 else
28766 unsigned int offset = 0;
28767 unsigned int cur_idx = 0;
28769 if (skeleton_debug_str_hash)
28770 skeleton_debug_str_hash->traverse<enum dwarf_form,
28771 output_indirect_string> (DW_FORM_strp);
28773 switch_to_section (debug_str_offsets_section);
28774 debug_str_hash->traverse_noresize
28775 <unsigned int *, output_index_string_offset> (&offset);
28776 switch_to_section (debug_str_dwo_section);
28777 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
28778 (&cur_idx);
28782 /* Callback for htab_traverse to assign an index to an entry in the
28783 table, and to write that entry to the .debug_addr section. */
28786 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
28788 addr_table_entry *entry = *slot;
28790 if (entry->refcount == 0)
28792 gcc_assert (entry->index == NO_INDEX_ASSIGNED
28793 || entry->index == NOT_INDEXED);
28794 return 1;
28797 gcc_assert (entry->index == *cur_index);
28798 (*cur_index)++;
28800 switch (entry->kind)
28802 case ate_kind_rtx:
28803 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
28804 "0x%x", entry->index);
28805 break;
28806 case ate_kind_rtx_dtprel:
28807 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
28808 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
28809 DWARF2_ADDR_SIZE,
28810 entry->addr.rtl);
28811 fputc ('\n', asm_out_file);
28812 break;
28813 case ate_kind_label:
28814 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
28815 "0x%x", entry->index);
28816 break;
28817 default:
28818 gcc_unreachable ();
28820 return 1;
28823 /* Produce the .debug_addr section. */
28825 static void
28826 output_addr_table (void)
28828 unsigned int index = 0;
28829 if (addr_index_table == NULL || addr_index_table->size () == 0)
28830 return;
28832 switch_to_section (debug_addr_section);
28833 addr_index_table
28834 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
28837 #if ENABLE_ASSERT_CHECKING
28838 /* Verify that all marks are clear. */
28840 static void
28841 verify_marks_clear (dw_die_ref die)
28843 dw_die_ref c;
28845 gcc_assert (! die->die_mark);
28846 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
28848 #endif /* ENABLE_ASSERT_CHECKING */
28850 /* Clear the marks for a die and its children.
28851 Be cool if the mark isn't set. */
28853 static void
28854 prune_unmark_dies (dw_die_ref die)
28856 dw_die_ref c;
28858 if (die->die_mark)
28859 die->die_mark = 0;
28860 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
28863 /* Given LOC that is referenced by a DIE we're marking as used, find all
28864 referenced DWARF procedures it references and mark them as used. */
28866 static void
28867 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
28869 for (; loc != NULL; loc = loc->dw_loc_next)
28870 switch (loc->dw_loc_opc)
28872 case DW_OP_implicit_pointer:
28873 case DW_OP_convert:
28874 case DW_OP_reinterpret:
28875 case DW_OP_GNU_implicit_pointer:
28876 case DW_OP_GNU_convert:
28877 case DW_OP_GNU_reinterpret:
28878 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
28879 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28880 break;
28881 case DW_OP_GNU_variable_value:
28882 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28884 dw_die_ref ref
28885 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28886 if (ref == NULL)
28887 break;
28888 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28889 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28890 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28892 /* FALLTHRU */
28893 case DW_OP_call2:
28894 case DW_OP_call4:
28895 case DW_OP_call_ref:
28896 case DW_OP_const_type:
28897 case DW_OP_GNU_const_type:
28898 case DW_OP_GNU_parameter_ref:
28899 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
28900 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
28901 break;
28902 case DW_OP_regval_type:
28903 case DW_OP_deref_type:
28904 case DW_OP_GNU_regval_type:
28905 case DW_OP_GNU_deref_type:
28906 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
28907 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
28908 break;
28909 case DW_OP_entry_value:
28910 case DW_OP_GNU_entry_value:
28911 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
28912 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
28913 break;
28914 default:
28915 break;
28919 /* Given DIE that we're marking as used, find any other dies
28920 it references as attributes and mark them as used. */
28922 static void
28923 prune_unused_types_walk_attribs (dw_die_ref die)
28925 dw_attr_node *a;
28926 unsigned ix;
28928 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28930 switch (AT_class (a))
28932 /* Make sure DWARF procedures referenced by location descriptions will
28933 get emitted. */
28934 case dw_val_class_loc:
28935 prune_unused_types_walk_loc_descr (AT_loc (a));
28936 break;
28937 case dw_val_class_loc_list:
28938 for (dw_loc_list_ref list = AT_loc_list (a);
28939 list != NULL;
28940 list = list->dw_loc_next)
28941 prune_unused_types_walk_loc_descr (list->expr);
28942 break;
28944 case dw_val_class_view_list:
28945 /* This points to a loc_list in another attribute, so it's
28946 already covered. */
28947 break;
28949 case dw_val_class_die_ref:
28950 /* A reference to another DIE.
28951 Make sure that it will get emitted.
28952 If it was broken out into a comdat group, don't follow it. */
28953 if (! AT_ref (a)->comdat_type_p
28954 || a->dw_attr == DW_AT_specification)
28955 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
28956 break;
28958 case dw_val_class_str:
28959 /* Set the string's refcount to 0 so that prune_unused_types_mark
28960 accounts properly for it. */
28961 a->dw_attr_val.v.val_str->refcount = 0;
28962 break;
28964 default:
28965 break;
28970 /* Mark the generic parameters and arguments children DIEs of DIE. */
28972 static void
28973 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28975 dw_die_ref c;
28977 if (die == NULL || die->die_child == NULL)
28978 return;
28979 c = die->die_child;
28982 if (is_template_parameter (c))
28983 prune_unused_types_mark (c, 1);
28984 c = c->die_sib;
28985 } while (c && c != die->die_child);
28988 /* Mark DIE as being used. If DOKIDS is true, then walk down
28989 to DIE's children. */
28991 static void
28992 prune_unused_types_mark (dw_die_ref die, int dokids)
28994 dw_die_ref c;
28996 if (die->die_mark == 0)
28998 /* We haven't done this node yet. Mark it as used. */
28999 die->die_mark = 1;
29000 /* If this is the DIE of a generic type instantiation,
29001 mark the children DIEs that describe its generic parms and
29002 args. */
29003 prune_unused_types_mark_generic_parms_dies (die);
29005 /* We also have to mark its parents as used.
29006 (But we don't want to mark our parent's kids due to this,
29007 unless it is a class.) */
29008 if (die->die_parent)
29009 prune_unused_types_mark (die->die_parent,
29010 class_scope_p (die->die_parent));
29012 /* Mark any referenced nodes. */
29013 prune_unused_types_walk_attribs (die);
29015 /* If this node is a specification,
29016 also mark the definition, if it exists. */
29017 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
29018 prune_unused_types_mark (die->die_definition, 1);
29021 if (dokids && die->die_mark != 2)
29023 /* We need to walk the children, but haven't done so yet.
29024 Remember that we've walked the kids. */
29025 die->die_mark = 2;
29027 /* If this is an array type, we need to make sure our
29028 kids get marked, even if they're types. If we're
29029 breaking out types into comdat sections, do this
29030 for all type definitions. */
29031 if (die->die_tag == DW_TAG_array_type
29032 || (use_debug_types
29033 && is_type_die (die) && ! is_declaration_die (die)))
29034 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
29035 else
29036 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29040 /* For local classes, look if any static member functions were emitted
29041 and if so, mark them. */
29043 static void
29044 prune_unused_types_walk_local_classes (dw_die_ref die)
29046 dw_die_ref c;
29048 if (die->die_mark == 2)
29049 return;
29051 switch (die->die_tag)
29053 case DW_TAG_structure_type:
29054 case DW_TAG_union_type:
29055 case DW_TAG_class_type:
29056 break;
29058 case DW_TAG_subprogram:
29059 if (!get_AT_flag (die, DW_AT_declaration)
29060 || die->die_definition != NULL)
29061 prune_unused_types_mark (die, 1);
29062 return;
29064 default:
29065 return;
29068 /* Mark children. */
29069 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
29072 /* Walk the tree DIE and mark types that we actually use. */
29074 static void
29075 prune_unused_types_walk (dw_die_ref die)
29077 dw_die_ref c;
29079 /* Don't do anything if this node is already marked and
29080 children have been marked as well. */
29081 if (die->die_mark == 2)
29082 return;
29084 switch (die->die_tag)
29086 case DW_TAG_structure_type:
29087 case DW_TAG_union_type:
29088 case DW_TAG_class_type:
29089 if (die->die_perennial_p)
29090 break;
29092 for (c = die->die_parent; c; c = c->die_parent)
29093 if (c->die_tag == DW_TAG_subprogram)
29094 break;
29096 /* Finding used static member functions inside of classes
29097 is needed just for local classes, because for other classes
29098 static member function DIEs with DW_AT_specification
29099 are emitted outside of the DW_TAG_*_type. If we ever change
29100 it, we'd need to call this even for non-local classes. */
29101 if (c)
29102 prune_unused_types_walk_local_classes (die);
29104 /* It's a type node --- don't mark it. */
29105 return;
29107 case DW_TAG_const_type:
29108 case DW_TAG_packed_type:
29109 case DW_TAG_pointer_type:
29110 case DW_TAG_reference_type:
29111 case DW_TAG_rvalue_reference_type:
29112 case DW_TAG_volatile_type:
29113 case DW_TAG_typedef:
29114 case DW_TAG_array_type:
29115 case DW_TAG_interface_type:
29116 case DW_TAG_friend:
29117 case DW_TAG_enumeration_type:
29118 case DW_TAG_subroutine_type:
29119 case DW_TAG_string_type:
29120 case DW_TAG_set_type:
29121 case DW_TAG_subrange_type:
29122 case DW_TAG_ptr_to_member_type:
29123 case DW_TAG_file_type:
29124 /* Type nodes are useful only when other DIEs reference them --- don't
29125 mark them. */
29126 /* FALLTHROUGH */
29128 case DW_TAG_dwarf_procedure:
29129 /* Likewise for DWARF procedures. */
29131 if (die->die_perennial_p)
29132 break;
29134 return;
29136 default:
29137 /* Mark everything else. */
29138 break;
29141 if (die->die_mark == 0)
29143 die->die_mark = 1;
29145 /* Now, mark any dies referenced from here. */
29146 prune_unused_types_walk_attribs (die);
29149 die->die_mark = 2;
29151 /* Mark children. */
29152 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
29155 /* Increment the string counts on strings referred to from DIE's
29156 attributes. */
29158 static void
29159 prune_unused_types_update_strings (dw_die_ref die)
29161 dw_attr_node *a;
29162 unsigned ix;
29164 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29165 if (AT_class (a) == dw_val_class_str)
29167 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
29168 s->refcount++;
29169 /* Avoid unnecessarily putting strings that are used less than
29170 twice in the hash table. */
29171 if (s->refcount
29172 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
29174 indirect_string_node **slot
29175 = debug_str_hash->find_slot_with_hash (s->str,
29176 htab_hash_string (s->str),
29177 INSERT);
29178 gcc_assert (*slot == NULL);
29179 *slot = s;
29184 /* Mark DIE and its children as removed. */
29186 static void
29187 mark_removed (dw_die_ref die)
29189 dw_die_ref c;
29190 die->removed = true;
29191 FOR_EACH_CHILD (die, c, mark_removed (c));
29194 /* Remove from the tree DIE any dies that aren't marked. */
29196 static void
29197 prune_unused_types_prune (dw_die_ref die)
29199 dw_die_ref c;
29201 gcc_assert (die->die_mark);
29202 prune_unused_types_update_strings (die);
29204 if (! die->die_child)
29205 return;
29207 c = die->die_child;
29208 do {
29209 dw_die_ref prev = c, next;
29210 for (c = c->die_sib; ! c->die_mark; c = next)
29211 if (c == die->die_child)
29213 /* No marked children between 'prev' and the end of the list. */
29214 if (prev == c)
29215 /* No marked children at all. */
29216 die->die_child = NULL;
29217 else
29219 prev->die_sib = c->die_sib;
29220 die->die_child = prev;
29222 c->die_sib = NULL;
29223 mark_removed (c);
29224 return;
29226 else
29228 next = c->die_sib;
29229 c->die_sib = NULL;
29230 mark_removed (c);
29233 if (c != prev->die_sib)
29234 prev->die_sib = c;
29235 prune_unused_types_prune (c);
29236 } while (c != die->die_child);
29239 /* Remove dies representing declarations that we never use. */
29241 static void
29242 prune_unused_types (void)
29244 unsigned int i;
29245 limbo_die_node *node;
29246 comdat_type_node *ctnode;
29247 pubname_entry *pub;
29248 dw_die_ref base_type;
29250 #if ENABLE_ASSERT_CHECKING
29251 /* All the marks should already be clear. */
29252 verify_marks_clear (comp_unit_die ());
29253 for (node = limbo_die_list; node; node = node->next)
29254 verify_marks_clear (node->die);
29255 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29256 verify_marks_clear (ctnode->root_die);
29257 #endif /* ENABLE_ASSERT_CHECKING */
29259 /* Mark types that are used in global variables. */
29260 premark_types_used_by_global_vars ();
29262 /* Set the mark on nodes that are actually used. */
29263 prune_unused_types_walk (comp_unit_die ());
29264 for (node = limbo_die_list; node; node = node->next)
29265 prune_unused_types_walk (node->die);
29266 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29268 prune_unused_types_walk (ctnode->root_die);
29269 prune_unused_types_mark (ctnode->type_die, 1);
29272 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
29273 are unusual in that they are pubnames that are the children of pubtypes.
29274 They should only be marked via their parent DW_TAG_enumeration_type die,
29275 not as roots in themselves. */
29276 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
29277 if (pub->die->die_tag != DW_TAG_enumerator)
29278 prune_unused_types_mark (pub->die, 1);
29279 for (i = 0; base_types.iterate (i, &base_type); i++)
29280 prune_unused_types_mark (base_type, 1);
29282 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
29283 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
29284 callees). */
29285 cgraph_node *cnode;
29286 FOR_EACH_FUNCTION (cnode)
29287 if (cnode->referred_to_p (false))
29289 dw_die_ref die = lookup_decl_die (cnode->decl);
29290 if (die == NULL || die->die_mark)
29291 continue;
29292 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
29293 if (e->caller != cnode
29294 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
29296 prune_unused_types_mark (die, 1);
29297 break;
29301 if (debug_str_hash)
29302 debug_str_hash->empty ();
29303 if (skeleton_debug_str_hash)
29304 skeleton_debug_str_hash->empty ();
29305 prune_unused_types_prune (comp_unit_die ());
29306 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
29308 node = *pnode;
29309 if (!node->die->die_mark)
29310 *pnode = node->next;
29311 else
29313 prune_unused_types_prune (node->die);
29314 pnode = &node->next;
29317 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29318 prune_unused_types_prune (ctnode->root_die);
29320 /* Leave the marks clear. */
29321 prune_unmark_dies (comp_unit_die ());
29322 for (node = limbo_die_list; node; node = node->next)
29323 prune_unmark_dies (node->die);
29324 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
29325 prune_unmark_dies (ctnode->root_die);
29328 /* Helpers to manipulate hash table of comdat type units. */
29330 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
29332 static inline hashval_t hash (const comdat_type_node *);
29333 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
29336 inline hashval_t
29337 comdat_type_hasher::hash (const comdat_type_node *type_node)
29339 hashval_t h;
29340 memcpy (&h, type_node->signature, sizeof (h));
29341 return h;
29344 inline bool
29345 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
29346 const comdat_type_node *type_node_2)
29348 return (! memcmp (type_node_1->signature, type_node_2->signature,
29349 DWARF_TYPE_SIGNATURE_SIZE));
29352 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
29353 to the location it would have been added, should we know its
29354 DECL_ASSEMBLER_NAME when we added other attributes. This will
29355 probably improve compactness of debug info, removing equivalent
29356 abbrevs, and hide any differences caused by deferring the
29357 computation of the assembler name, triggered by e.g. PCH. */
29359 static inline void
29360 move_linkage_attr (dw_die_ref die)
29362 unsigned ix = vec_safe_length (die->die_attr);
29363 dw_attr_node linkage = (*die->die_attr)[ix - 1];
29365 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
29366 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
29368 while (--ix > 0)
29370 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
29372 if (prev->dw_attr == DW_AT_decl_line
29373 || prev->dw_attr == DW_AT_decl_column
29374 || prev->dw_attr == DW_AT_name)
29375 break;
29378 if (ix != vec_safe_length (die->die_attr) - 1)
29380 die->die_attr->pop ();
29381 die->die_attr->quick_insert (ix, linkage);
29385 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
29386 referenced from typed stack ops and count how often they are used. */
29388 static void
29389 mark_base_types (dw_loc_descr_ref loc)
29391 dw_die_ref base_type = NULL;
29393 for (; loc; loc = loc->dw_loc_next)
29395 switch (loc->dw_loc_opc)
29397 case DW_OP_regval_type:
29398 case DW_OP_deref_type:
29399 case DW_OP_GNU_regval_type:
29400 case DW_OP_GNU_deref_type:
29401 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
29402 break;
29403 case DW_OP_convert:
29404 case DW_OP_reinterpret:
29405 case DW_OP_GNU_convert:
29406 case DW_OP_GNU_reinterpret:
29407 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
29408 continue;
29409 /* FALLTHRU */
29410 case DW_OP_const_type:
29411 case DW_OP_GNU_const_type:
29412 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
29413 break;
29414 case DW_OP_entry_value:
29415 case DW_OP_GNU_entry_value:
29416 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
29417 continue;
29418 default:
29419 continue;
29421 gcc_assert (base_type->die_parent == comp_unit_die ());
29422 if (base_type->die_mark)
29423 base_type->die_mark++;
29424 else
29426 base_types.safe_push (base_type);
29427 base_type->die_mark = 1;
29432 /* Comparison function for sorting marked base types. */
29434 static int
29435 base_type_cmp (const void *x, const void *y)
29437 dw_die_ref dx = *(const dw_die_ref *) x;
29438 dw_die_ref dy = *(const dw_die_ref *) y;
29439 unsigned int byte_size1, byte_size2;
29440 unsigned int encoding1, encoding2;
29441 unsigned int align1, align2;
29442 if (dx->die_mark > dy->die_mark)
29443 return -1;
29444 if (dx->die_mark < dy->die_mark)
29445 return 1;
29446 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
29447 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
29448 if (byte_size1 < byte_size2)
29449 return 1;
29450 if (byte_size1 > byte_size2)
29451 return -1;
29452 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
29453 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
29454 if (encoding1 < encoding2)
29455 return 1;
29456 if (encoding1 > encoding2)
29457 return -1;
29458 align1 = get_AT_unsigned (dx, DW_AT_alignment);
29459 align2 = get_AT_unsigned (dy, DW_AT_alignment);
29460 if (align1 < align2)
29461 return 1;
29462 if (align1 > align2)
29463 return -1;
29464 return 0;
29467 /* Move base types marked by mark_base_types as early as possible
29468 in the CU, sorted by decreasing usage count both to make the
29469 uleb128 references as small as possible and to make sure they
29470 will have die_offset already computed by calc_die_sizes when
29471 sizes of typed stack loc ops is computed. */
29473 static void
29474 move_marked_base_types (void)
29476 unsigned int i;
29477 dw_die_ref base_type, die, c;
29479 if (base_types.is_empty ())
29480 return;
29482 /* Sort by decreasing usage count, they will be added again in that
29483 order later on. */
29484 base_types.qsort (base_type_cmp);
29485 die = comp_unit_die ();
29486 c = die->die_child;
29489 dw_die_ref prev = c;
29490 c = c->die_sib;
29491 while (c->die_mark)
29493 remove_child_with_prev (c, prev);
29494 /* As base types got marked, there must be at least
29495 one node other than DW_TAG_base_type. */
29496 gcc_assert (die->die_child != NULL);
29497 c = prev->die_sib;
29500 while (c != die->die_child);
29501 gcc_assert (die->die_child);
29502 c = die->die_child;
29503 for (i = 0; base_types.iterate (i, &base_type); i++)
29505 base_type->die_mark = 0;
29506 base_type->die_sib = c->die_sib;
29507 c->die_sib = base_type;
29508 c = base_type;
29512 /* Helper function for resolve_addr, attempt to resolve
29513 one CONST_STRING, return true if successful. Similarly verify that
29514 SYMBOL_REFs refer to variables emitted in the current CU. */
29516 static bool
29517 resolve_one_addr (rtx *addr)
29519 rtx rtl = *addr;
29521 if (GET_CODE (rtl) == CONST_STRING)
29523 size_t len = strlen (XSTR (rtl, 0)) + 1;
29524 tree t = build_string (len, XSTR (rtl, 0));
29525 tree tlen = size_int (len - 1);
29526 TREE_TYPE (t)
29527 = build_array_type (char_type_node, build_index_type (tlen));
29528 rtl = lookup_constant_def (t);
29529 if (!rtl || !MEM_P (rtl))
29530 return false;
29531 rtl = XEXP (rtl, 0);
29532 if (GET_CODE (rtl) == SYMBOL_REF
29533 && SYMBOL_REF_DECL (rtl)
29534 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29535 return false;
29536 vec_safe_push (used_rtx_array, rtl);
29537 *addr = rtl;
29538 return true;
29541 if (GET_CODE (rtl) == SYMBOL_REF
29542 && SYMBOL_REF_DECL (rtl))
29544 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
29546 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
29547 return false;
29549 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
29550 return false;
29553 if (GET_CODE (rtl) == CONST)
29555 subrtx_ptr_iterator::array_type array;
29556 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
29557 if (!resolve_one_addr (*iter))
29558 return false;
29561 return true;
29564 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
29565 if possible, and create DW_TAG_dwarf_procedure that can be referenced
29566 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
29568 static rtx
29569 string_cst_pool_decl (tree t)
29571 rtx rtl = output_constant_def (t, 1);
29572 unsigned char *array;
29573 dw_loc_descr_ref l;
29574 tree decl;
29575 size_t len;
29576 dw_die_ref ref;
29578 if (!rtl || !MEM_P (rtl))
29579 return NULL_RTX;
29580 rtl = XEXP (rtl, 0);
29581 if (GET_CODE (rtl) != SYMBOL_REF
29582 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
29583 return NULL_RTX;
29585 decl = SYMBOL_REF_DECL (rtl);
29586 if (!lookup_decl_die (decl))
29588 len = TREE_STRING_LENGTH (t);
29589 vec_safe_push (used_rtx_array, rtl);
29590 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
29591 array = ggc_vec_alloc<unsigned char> (len);
29592 memcpy (array, TREE_STRING_POINTER (t), len);
29593 l = new_loc_descr (DW_OP_implicit_value, len, 0);
29594 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
29595 l->dw_loc_oprnd2.v.val_vec.length = len;
29596 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
29597 l->dw_loc_oprnd2.v.val_vec.array = array;
29598 add_AT_loc (ref, DW_AT_location, l);
29599 equate_decl_number_to_die (decl, ref);
29601 return rtl;
29604 /* Helper function of resolve_addr_in_expr. LOC is
29605 a DW_OP_addr followed by DW_OP_stack_value, either at the start
29606 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
29607 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
29608 with DW_OP_implicit_pointer if possible
29609 and return true, if unsuccessful, return false. */
29611 static bool
29612 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
29614 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
29615 HOST_WIDE_INT offset = 0;
29616 dw_die_ref ref = NULL;
29617 tree decl;
29619 if (GET_CODE (rtl) == CONST
29620 && GET_CODE (XEXP (rtl, 0)) == PLUS
29621 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
29623 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
29624 rtl = XEXP (XEXP (rtl, 0), 0);
29626 if (GET_CODE (rtl) == CONST_STRING)
29628 size_t len = strlen (XSTR (rtl, 0)) + 1;
29629 tree t = build_string (len, XSTR (rtl, 0));
29630 tree tlen = size_int (len - 1);
29632 TREE_TYPE (t)
29633 = build_array_type (char_type_node, build_index_type (tlen));
29634 rtl = string_cst_pool_decl (t);
29635 if (!rtl)
29636 return false;
29638 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
29640 decl = SYMBOL_REF_DECL (rtl);
29641 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
29643 ref = lookup_decl_die (decl);
29644 if (ref && (get_AT (ref, DW_AT_location)
29645 || get_AT (ref, DW_AT_const_value)))
29647 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
29648 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29649 loc->dw_loc_oprnd1.val_entry = NULL;
29650 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29651 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29652 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29653 loc->dw_loc_oprnd2.v.val_int = offset;
29654 return true;
29658 return false;
29661 /* Helper function for resolve_addr, handle one location
29662 expression, return false if at least one CONST_STRING or SYMBOL_REF in
29663 the location list couldn't be resolved. */
29665 static bool
29666 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
29668 dw_loc_descr_ref keep = NULL;
29669 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
29670 switch (loc->dw_loc_opc)
29672 case DW_OP_addr:
29673 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29675 if ((prev == NULL
29676 || prev->dw_loc_opc == DW_OP_piece
29677 || prev->dw_loc_opc == DW_OP_bit_piece)
29678 && loc->dw_loc_next
29679 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
29680 && (!dwarf_strict || dwarf_version >= 5)
29681 && optimize_one_addr_into_implicit_ptr (loc))
29682 break;
29683 return false;
29685 break;
29686 case DW_OP_GNU_addr_index:
29687 case DW_OP_GNU_const_index:
29688 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
29689 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
29691 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
29692 if (!resolve_one_addr (&rtl))
29693 return false;
29694 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
29695 loc->dw_loc_oprnd1.val_entry
29696 = add_addr_table_entry (rtl, ate_kind_rtx);
29698 break;
29699 case DW_OP_const4u:
29700 case DW_OP_const8u:
29701 if (loc->dtprel
29702 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
29703 return false;
29704 break;
29705 case DW_OP_plus_uconst:
29706 if (size_of_loc_descr (loc)
29707 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
29709 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
29711 dw_loc_descr_ref repl
29712 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
29713 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
29714 add_loc_descr (&repl, loc->dw_loc_next);
29715 *loc = *repl;
29717 break;
29718 case DW_OP_implicit_value:
29719 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
29720 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
29721 return false;
29722 break;
29723 case DW_OP_implicit_pointer:
29724 case DW_OP_GNU_implicit_pointer:
29725 case DW_OP_GNU_parameter_ref:
29726 case DW_OP_GNU_variable_value:
29727 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29729 dw_die_ref ref
29730 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
29731 if (ref == NULL)
29732 return false;
29733 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29734 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
29735 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
29737 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
29739 if (prev == NULL
29740 && loc->dw_loc_next == NULL
29741 && AT_class (a) == dw_val_class_loc)
29742 switch (a->dw_attr)
29744 /* Following attributes allow both exprloc and reference,
29745 so if the whole expression is DW_OP_GNU_variable_value
29746 alone we could transform it into reference. */
29747 case DW_AT_byte_size:
29748 case DW_AT_bit_size:
29749 case DW_AT_lower_bound:
29750 case DW_AT_upper_bound:
29751 case DW_AT_bit_stride:
29752 case DW_AT_count:
29753 case DW_AT_allocated:
29754 case DW_AT_associated:
29755 case DW_AT_byte_stride:
29756 a->dw_attr_val.val_class = dw_val_class_die_ref;
29757 a->dw_attr_val.val_entry = NULL;
29758 a->dw_attr_val.v.val_die_ref.die
29759 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29760 a->dw_attr_val.v.val_die_ref.external = 0;
29761 return true;
29762 default:
29763 break;
29765 if (dwarf_strict)
29766 return false;
29768 break;
29769 case DW_OP_const_type:
29770 case DW_OP_regval_type:
29771 case DW_OP_deref_type:
29772 case DW_OP_convert:
29773 case DW_OP_reinterpret:
29774 case DW_OP_GNU_const_type:
29775 case DW_OP_GNU_regval_type:
29776 case DW_OP_GNU_deref_type:
29777 case DW_OP_GNU_convert:
29778 case DW_OP_GNU_reinterpret:
29779 while (loc->dw_loc_next
29780 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
29781 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
29783 dw_die_ref base1, base2;
29784 unsigned enc1, enc2, size1, size2;
29785 if (loc->dw_loc_opc == DW_OP_regval_type
29786 || loc->dw_loc_opc == DW_OP_deref_type
29787 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29788 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29789 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
29790 else if (loc->dw_loc_oprnd1.val_class
29791 == dw_val_class_unsigned_const)
29792 break;
29793 else
29794 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
29795 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
29796 == dw_val_class_unsigned_const)
29797 break;
29798 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
29799 gcc_assert (base1->die_tag == DW_TAG_base_type
29800 && base2->die_tag == DW_TAG_base_type);
29801 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
29802 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
29803 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
29804 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
29805 if (size1 == size2
29806 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
29807 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
29808 && loc != keep)
29809 || enc1 == enc2))
29811 /* Optimize away next DW_OP_convert after
29812 adjusting LOC's base type die reference. */
29813 if (loc->dw_loc_opc == DW_OP_regval_type
29814 || loc->dw_loc_opc == DW_OP_deref_type
29815 || loc->dw_loc_opc == DW_OP_GNU_regval_type
29816 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
29817 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
29818 else
29819 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
29820 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
29821 continue;
29823 /* Don't change integer DW_OP_convert after e.g. floating
29824 point typed stack entry. */
29825 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
29826 keep = loc->dw_loc_next;
29827 break;
29829 break;
29830 default:
29831 break;
29833 return true;
29836 /* Helper function of resolve_addr. DIE had DW_AT_location of
29837 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
29838 and DW_OP_addr couldn't be resolved. resolve_addr has already
29839 removed the DW_AT_location attribute. This function attempts to
29840 add a new DW_AT_location attribute with DW_OP_implicit_pointer
29841 to it or DW_AT_const_value attribute, if possible. */
29843 static void
29844 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
29846 if (!VAR_P (decl)
29847 || lookup_decl_die (decl) != die
29848 || DECL_EXTERNAL (decl)
29849 || !TREE_STATIC (decl)
29850 || DECL_INITIAL (decl) == NULL_TREE
29851 || DECL_P (DECL_INITIAL (decl))
29852 || get_AT (die, DW_AT_const_value))
29853 return;
29855 tree init = DECL_INITIAL (decl);
29856 HOST_WIDE_INT offset = 0;
29857 /* For variables that have been optimized away and thus
29858 don't have a memory location, see if we can emit
29859 DW_AT_const_value instead. */
29860 if (tree_add_const_value_attribute (die, init))
29861 return;
29862 if (dwarf_strict && dwarf_version < 5)
29863 return;
29864 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
29865 and ADDR_EXPR refers to a decl that has DW_AT_location or
29866 DW_AT_const_value (but isn't addressable, otherwise
29867 resolving the original DW_OP_addr wouldn't fail), see if
29868 we can add DW_OP_implicit_pointer. */
29869 STRIP_NOPS (init);
29870 if (TREE_CODE (init) == POINTER_PLUS_EXPR
29871 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
29873 offset = tree_to_shwi (TREE_OPERAND (init, 1));
29874 init = TREE_OPERAND (init, 0);
29875 STRIP_NOPS (init);
29877 if (TREE_CODE (init) != ADDR_EXPR)
29878 return;
29879 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
29880 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
29881 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
29882 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
29883 && TREE_OPERAND (init, 0) != decl))
29885 dw_die_ref ref;
29886 dw_loc_descr_ref l;
29888 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
29890 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
29891 if (!rtl)
29892 return;
29893 decl = SYMBOL_REF_DECL (rtl);
29895 else
29896 decl = TREE_OPERAND (init, 0);
29897 ref = lookup_decl_die (decl);
29898 if (ref == NULL
29899 || (!get_AT (ref, DW_AT_location)
29900 && !get_AT (ref, DW_AT_const_value)))
29901 return;
29902 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
29903 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29904 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
29905 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29906 add_AT_loc (die, DW_AT_location, l);
29910 /* Return NULL if l is a DWARF expression, or first op that is not
29911 valid DWARF expression. */
29913 static dw_loc_descr_ref
29914 non_dwarf_expression (dw_loc_descr_ref l)
29916 while (l)
29918 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29919 return l;
29920 switch (l->dw_loc_opc)
29922 case DW_OP_regx:
29923 case DW_OP_implicit_value:
29924 case DW_OP_stack_value:
29925 case DW_OP_implicit_pointer:
29926 case DW_OP_GNU_implicit_pointer:
29927 case DW_OP_GNU_parameter_ref:
29928 case DW_OP_piece:
29929 case DW_OP_bit_piece:
29930 return l;
29931 default:
29932 break;
29934 l = l->dw_loc_next;
29936 return NULL;
29939 /* Return adjusted copy of EXPR:
29940 If it is empty DWARF expression, return it.
29941 If it is valid non-empty DWARF expression,
29942 return copy of EXPR with DW_OP_deref appended to it.
29943 If it is DWARF expression followed by DW_OP_reg{N,x}, return
29944 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
29945 If it is DWARF expression followed by DW_OP_stack_value, return
29946 copy of the DWARF expression without anything appended.
29947 Otherwise, return NULL. */
29949 static dw_loc_descr_ref
29950 copy_deref_exprloc (dw_loc_descr_ref expr)
29952 dw_loc_descr_ref tail = NULL;
29954 if (expr == NULL)
29955 return NULL;
29957 dw_loc_descr_ref l = non_dwarf_expression (expr);
29958 if (l && l->dw_loc_next)
29959 return NULL;
29961 if (l)
29963 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
29964 tail = new_loc_descr ((enum dwarf_location_atom)
29965 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
29966 0, 0);
29967 else
29968 switch (l->dw_loc_opc)
29970 case DW_OP_regx:
29971 tail = new_loc_descr (DW_OP_bregx,
29972 l->dw_loc_oprnd1.v.val_unsigned, 0);
29973 break;
29974 case DW_OP_stack_value:
29975 break;
29976 default:
29977 return NULL;
29980 else
29981 tail = new_loc_descr (DW_OP_deref, 0, 0);
29983 dw_loc_descr_ref ret = NULL, *p = &ret;
29984 while (expr != l)
29986 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29987 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29988 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29989 p = &(*p)->dw_loc_next;
29990 expr = expr->dw_loc_next;
29992 *p = tail;
29993 return ret;
29996 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29997 reference to a variable or argument, adjust it if needed and return:
29998 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29999 attribute if present should be removed
30000 0 keep the attribute perhaps with minor modifications, no need to rescan
30001 1 if the attribute has been successfully adjusted. */
30003 static int
30004 optimize_string_length (dw_attr_node *a)
30006 dw_loc_descr_ref l = AT_loc (a), lv;
30007 dw_die_ref die;
30008 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30010 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
30011 die = lookup_decl_die (decl);
30012 if (die)
30014 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30015 l->dw_loc_oprnd1.v.val_die_ref.die = die;
30016 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
30018 else
30019 return -1;
30021 else
30022 die = l->dw_loc_oprnd1.v.val_die_ref.die;
30024 /* DWARF5 allows reference class, so we can then reference the DIE.
30025 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
30026 if (l->dw_loc_next != NULL && dwarf_version >= 5)
30028 a->dw_attr_val.val_class = dw_val_class_die_ref;
30029 a->dw_attr_val.val_entry = NULL;
30030 a->dw_attr_val.v.val_die_ref.die = die;
30031 a->dw_attr_val.v.val_die_ref.external = 0;
30032 return 0;
30035 dw_attr_node *av = get_AT (die, DW_AT_location);
30036 dw_loc_list_ref d;
30037 bool non_dwarf_expr = false;
30039 if (av == NULL)
30040 return dwarf_strict ? -1 : 0;
30041 switch (AT_class (av))
30043 case dw_val_class_loc_list:
30044 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30045 if (d->expr && non_dwarf_expression (d->expr))
30046 non_dwarf_expr = true;
30047 break;
30048 case dw_val_class_view_list:
30049 gcc_unreachable ();
30050 case dw_val_class_loc:
30051 lv = AT_loc (av);
30052 if (lv == NULL)
30053 return dwarf_strict ? -1 : 0;
30054 if (non_dwarf_expression (lv))
30055 non_dwarf_expr = true;
30056 break;
30057 default:
30058 return dwarf_strict ? -1 : 0;
30061 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
30062 into DW_OP_call4 or DW_OP_GNU_variable_value into
30063 DW_OP_call4 DW_OP_deref, do so. */
30064 if (!non_dwarf_expr
30065 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
30067 l->dw_loc_opc = DW_OP_call4;
30068 if (l->dw_loc_next)
30069 l->dw_loc_next = NULL;
30070 else
30071 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
30072 return 0;
30075 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
30076 copy over the DW_AT_location attribute from die to a. */
30077 if (l->dw_loc_next != NULL)
30079 a->dw_attr_val = av->dw_attr_val;
30080 return 1;
30083 dw_loc_list_ref list, *p;
30084 switch (AT_class (av))
30086 case dw_val_class_loc_list:
30087 p = &list;
30088 list = NULL;
30089 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
30091 lv = copy_deref_exprloc (d->expr);
30092 if (lv)
30094 *p = new_loc_list (lv, d->begin, d->vbegin, d->end, d->vend, d->section);
30095 p = &(*p)->dw_loc_next;
30097 else if (!dwarf_strict && d->expr)
30098 return 0;
30100 if (list == NULL)
30101 return dwarf_strict ? -1 : 0;
30102 a->dw_attr_val.val_class = dw_val_class_loc_list;
30103 gen_llsym (list);
30104 *AT_loc_list_ptr (a) = list;
30105 return 1;
30106 case dw_val_class_loc:
30107 lv = copy_deref_exprloc (AT_loc (av));
30108 if (lv == NULL)
30109 return dwarf_strict ? -1 : 0;
30110 a->dw_attr_val.v.val_loc = lv;
30111 return 1;
30112 default:
30113 gcc_unreachable ();
30117 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
30118 an address in .rodata section if the string literal is emitted there,
30119 or remove the containing location list or replace DW_AT_const_value
30120 with DW_AT_location and empty location expression, if it isn't found
30121 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
30122 to something that has been emitted in the current CU. */
30124 static void
30125 resolve_addr (dw_die_ref die)
30127 dw_die_ref c;
30128 dw_attr_node *a;
30129 dw_loc_list_ref *curr, *start, loc;
30130 unsigned ix;
30131 bool remove_AT_byte_size = false;
30133 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30134 switch (AT_class (a))
30136 case dw_val_class_loc_list:
30137 start = curr = AT_loc_list_ptr (a);
30138 loc = *curr;
30139 gcc_assert (loc);
30140 /* The same list can be referenced more than once. See if we have
30141 already recorded the result from a previous pass. */
30142 if (loc->replaced)
30143 *curr = loc->dw_loc_next;
30144 else if (!loc->resolved_addr)
30146 /* As things stand, we do not expect or allow one die to
30147 reference a suffix of another die's location list chain.
30148 References must be identical or completely separate.
30149 There is therefore no need to cache the result of this
30150 pass on any list other than the first; doing so
30151 would lead to unnecessary writes. */
30152 while (*curr)
30154 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
30155 if (!resolve_addr_in_expr (a, (*curr)->expr))
30157 dw_loc_list_ref next = (*curr)->dw_loc_next;
30158 dw_loc_descr_ref l = (*curr)->expr;
30160 if (next && (*curr)->ll_symbol)
30162 gcc_assert (!next->ll_symbol);
30163 next->ll_symbol = (*curr)->ll_symbol;
30164 next->vl_symbol = (*curr)->vl_symbol;
30166 if (dwarf_split_debug_info)
30167 remove_loc_list_addr_table_entries (l);
30168 *curr = next;
30170 else
30172 mark_base_types ((*curr)->expr);
30173 curr = &(*curr)->dw_loc_next;
30176 if (loc == *start)
30177 loc->resolved_addr = 1;
30178 else
30180 loc->replaced = 1;
30181 loc->dw_loc_next = *start;
30184 if (!*start)
30186 remove_AT (die, a->dw_attr);
30187 ix--;
30189 break;
30190 case dw_val_class_view_list:
30192 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30193 gcc_checking_assert (dwarf2out_locviews_in_attribute ());
30194 dw_val_node *llnode
30195 = view_list_to_loc_list_val_node (&a->dw_attr_val);
30196 /* If we no longer have a loclist, or it no longer needs
30197 views, drop this attribute. */
30198 if (!llnode || !llnode->v.val_loc_list->vl_symbol)
30200 remove_AT (die, a->dw_attr);
30201 ix--;
30203 break;
30205 case dw_val_class_loc:
30207 dw_loc_descr_ref l = AT_loc (a);
30208 /* DW_OP_GNU_variable_value DW_OP_stack_value or
30209 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
30210 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
30211 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
30212 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
30213 with DW_FORM_ref referencing the same DIE as
30214 DW_OP_GNU_variable_value used to reference. */
30215 if (a->dw_attr == DW_AT_string_length
30216 && l
30217 && l->dw_loc_opc == DW_OP_GNU_variable_value
30218 && (l->dw_loc_next == NULL
30219 || (l->dw_loc_next->dw_loc_next == NULL
30220 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
30222 switch (optimize_string_length (a))
30224 case -1:
30225 remove_AT (die, a->dw_attr);
30226 ix--;
30227 /* If we drop DW_AT_string_length, we need to drop also
30228 DW_AT_{string_length_,}byte_size. */
30229 remove_AT_byte_size = true;
30230 continue;
30231 default:
30232 break;
30233 case 1:
30234 /* Even if we keep the optimized DW_AT_string_length,
30235 it might have changed AT_class, so process it again. */
30236 ix--;
30237 continue;
30240 /* For -gdwarf-2 don't attempt to optimize
30241 DW_AT_data_member_location containing
30242 DW_OP_plus_uconst - older consumers might
30243 rely on it being that op instead of a more complex,
30244 but shorter, location description. */
30245 if ((dwarf_version > 2
30246 || a->dw_attr != DW_AT_data_member_location
30247 || l == NULL
30248 || l->dw_loc_opc != DW_OP_plus_uconst
30249 || l->dw_loc_next != NULL)
30250 && !resolve_addr_in_expr (a, l))
30252 if (dwarf_split_debug_info)
30253 remove_loc_list_addr_table_entries (l);
30254 if (l != NULL
30255 && l->dw_loc_next == NULL
30256 && l->dw_loc_opc == DW_OP_addr
30257 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
30258 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
30259 && a->dw_attr == DW_AT_location)
30261 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
30262 remove_AT (die, a->dw_attr);
30263 ix--;
30264 optimize_location_into_implicit_ptr (die, decl);
30265 break;
30267 if (a->dw_attr == DW_AT_string_length)
30268 /* If we drop DW_AT_string_length, we need to drop also
30269 DW_AT_{string_length_,}byte_size. */
30270 remove_AT_byte_size = true;
30271 remove_AT (die, a->dw_attr);
30272 ix--;
30274 else
30275 mark_base_types (l);
30277 break;
30278 case dw_val_class_addr:
30279 if (a->dw_attr == DW_AT_const_value
30280 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
30282 if (AT_index (a) != NOT_INDEXED)
30283 remove_addr_table_entry (a->dw_attr_val.val_entry);
30284 remove_AT (die, a->dw_attr);
30285 ix--;
30287 if ((die->die_tag == DW_TAG_call_site
30288 && a->dw_attr == DW_AT_call_origin)
30289 || (die->die_tag == DW_TAG_GNU_call_site
30290 && a->dw_attr == DW_AT_abstract_origin))
30292 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
30293 dw_die_ref tdie = lookup_decl_die (tdecl);
30294 dw_die_ref cdie;
30295 if (tdie == NULL
30296 && DECL_EXTERNAL (tdecl)
30297 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
30298 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
30300 dw_die_ref pdie = cdie;
30301 /* Make sure we don't add these DIEs into type units.
30302 We could emit skeleton DIEs for context (namespaces,
30303 outer structs/classes) and a skeleton DIE for the
30304 innermost context with DW_AT_signature pointing to the
30305 type unit. See PR78835. */
30306 while (pdie && pdie->die_tag != DW_TAG_type_unit)
30307 pdie = pdie->die_parent;
30308 if (pdie == NULL)
30310 /* Creating a full DIE for tdecl is overly expensive and
30311 at this point even wrong when in the LTO phase
30312 as it can end up generating new type DIEs we didn't
30313 output and thus optimize_external_refs will crash. */
30314 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
30315 add_AT_flag (tdie, DW_AT_external, 1);
30316 add_AT_flag (tdie, DW_AT_declaration, 1);
30317 add_linkage_attr (tdie, tdecl);
30318 add_name_and_src_coords_attributes (tdie, tdecl, true);
30319 equate_decl_number_to_die (tdecl, tdie);
30322 if (tdie)
30324 a->dw_attr_val.val_class = dw_val_class_die_ref;
30325 a->dw_attr_val.v.val_die_ref.die = tdie;
30326 a->dw_attr_val.v.val_die_ref.external = 0;
30328 else
30330 if (AT_index (a) != NOT_INDEXED)
30331 remove_addr_table_entry (a->dw_attr_val.val_entry);
30332 remove_AT (die, a->dw_attr);
30333 ix--;
30336 break;
30337 default:
30338 break;
30341 if (remove_AT_byte_size)
30342 remove_AT (die, dwarf_version >= 5
30343 ? DW_AT_string_length_byte_size
30344 : DW_AT_byte_size);
30346 FOR_EACH_CHILD (die, c, resolve_addr (c));
30349 /* Helper routines for optimize_location_lists.
30350 This pass tries to share identical local lists in .debug_loc
30351 section. */
30353 /* Iteratively hash operands of LOC opcode into HSTATE. */
30355 static void
30356 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
30358 dw_val_ref val1 = &loc->dw_loc_oprnd1;
30359 dw_val_ref val2 = &loc->dw_loc_oprnd2;
30361 switch (loc->dw_loc_opc)
30363 case DW_OP_const4u:
30364 case DW_OP_const8u:
30365 if (loc->dtprel)
30366 goto hash_addr;
30367 /* FALLTHRU */
30368 case DW_OP_const1u:
30369 case DW_OP_const1s:
30370 case DW_OP_const2u:
30371 case DW_OP_const2s:
30372 case DW_OP_const4s:
30373 case DW_OP_const8s:
30374 case DW_OP_constu:
30375 case DW_OP_consts:
30376 case DW_OP_pick:
30377 case DW_OP_plus_uconst:
30378 case DW_OP_breg0:
30379 case DW_OP_breg1:
30380 case DW_OP_breg2:
30381 case DW_OP_breg3:
30382 case DW_OP_breg4:
30383 case DW_OP_breg5:
30384 case DW_OP_breg6:
30385 case DW_OP_breg7:
30386 case DW_OP_breg8:
30387 case DW_OP_breg9:
30388 case DW_OP_breg10:
30389 case DW_OP_breg11:
30390 case DW_OP_breg12:
30391 case DW_OP_breg13:
30392 case DW_OP_breg14:
30393 case DW_OP_breg15:
30394 case DW_OP_breg16:
30395 case DW_OP_breg17:
30396 case DW_OP_breg18:
30397 case DW_OP_breg19:
30398 case DW_OP_breg20:
30399 case DW_OP_breg21:
30400 case DW_OP_breg22:
30401 case DW_OP_breg23:
30402 case DW_OP_breg24:
30403 case DW_OP_breg25:
30404 case DW_OP_breg26:
30405 case DW_OP_breg27:
30406 case DW_OP_breg28:
30407 case DW_OP_breg29:
30408 case DW_OP_breg30:
30409 case DW_OP_breg31:
30410 case DW_OP_regx:
30411 case DW_OP_fbreg:
30412 case DW_OP_piece:
30413 case DW_OP_deref_size:
30414 case DW_OP_xderef_size:
30415 hstate.add_object (val1->v.val_int);
30416 break;
30417 case DW_OP_skip:
30418 case DW_OP_bra:
30420 int offset;
30422 gcc_assert (val1->val_class == dw_val_class_loc);
30423 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
30424 hstate.add_object (offset);
30426 break;
30427 case DW_OP_implicit_value:
30428 hstate.add_object (val1->v.val_unsigned);
30429 switch (val2->val_class)
30431 case dw_val_class_const:
30432 hstate.add_object (val2->v.val_int);
30433 break;
30434 case dw_val_class_vec:
30436 unsigned int elt_size = val2->v.val_vec.elt_size;
30437 unsigned int len = val2->v.val_vec.length;
30439 hstate.add_int (elt_size);
30440 hstate.add_int (len);
30441 hstate.add (val2->v.val_vec.array, len * elt_size);
30443 break;
30444 case dw_val_class_const_double:
30445 hstate.add_object (val2->v.val_double.low);
30446 hstate.add_object (val2->v.val_double.high);
30447 break;
30448 case dw_val_class_wide_int:
30449 hstate.add (val2->v.val_wide->get_val (),
30450 get_full_len (*val2->v.val_wide)
30451 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30452 break;
30453 case dw_val_class_addr:
30454 inchash::add_rtx (val2->v.val_addr, hstate);
30455 break;
30456 default:
30457 gcc_unreachable ();
30459 break;
30460 case DW_OP_bregx:
30461 case DW_OP_bit_piece:
30462 hstate.add_object (val1->v.val_int);
30463 hstate.add_object (val2->v.val_int);
30464 break;
30465 case DW_OP_addr:
30466 hash_addr:
30467 if (loc->dtprel)
30469 unsigned char dtprel = 0xd1;
30470 hstate.add_object (dtprel);
30472 inchash::add_rtx (val1->v.val_addr, hstate);
30473 break;
30474 case DW_OP_GNU_addr_index:
30475 case DW_OP_GNU_const_index:
30477 if (loc->dtprel)
30479 unsigned char dtprel = 0xd1;
30480 hstate.add_object (dtprel);
30482 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
30484 break;
30485 case DW_OP_implicit_pointer:
30486 case DW_OP_GNU_implicit_pointer:
30487 hstate.add_int (val2->v.val_int);
30488 break;
30489 case DW_OP_entry_value:
30490 case DW_OP_GNU_entry_value:
30491 hstate.add_object (val1->v.val_loc);
30492 break;
30493 case DW_OP_regval_type:
30494 case DW_OP_deref_type:
30495 case DW_OP_GNU_regval_type:
30496 case DW_OP_GNU_deref_type:
30498 unsigned int byte_size
30499 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
30500 unsigned int encoding
30501 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
30502 hstate.add_object (val1->v.val_int);
30503 hstate.add_object (byte_size);
30504 hstate.add_object (encoding);
30506 break;
30507 case DW_OP_convert:
30508 case DW_OP_reinterpret:
30509 case DW_OP_GNU_convert:
30510 case DW_OP_GNU_reinterpret:
30511 if (val1->val_class == dw_val_class_unsigned_const)
30513 hstate.add_object (val1->v.val_unsigned);
30514 break;
30516 /* FALLTHRU */
30517 case DW_OP_const_type:
30518 case DW_OP_GNU_const_type:
30520 unsigned int byte_size
30521 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
30522 unsigned int encoding
30523 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
30524 hstate.add_object (byte_size);
30525 hstate.add_object (encoding);
30526 if (loc->dw_loc_opc != DW_OP_const_type
30527 && loc->dw_loc_opc != DW_OP_GNU_const_type)
30528 break;
30529 hstate.add_object (val2->val_class);
30530 switch (val2->val_class)
30532 case dw_val_class_const:
30533 hstate.add_object (val2->v.val_int);
30534 break;
30535 case dw_val_class_vec:
30537 unsigned int elt_size = val2->v.val_vec.elt_size;
30538 unsigned int len = val2->v.val_vec.length;
30540 hstate.add_object (elt_size);
30541 hstate.add_object (len);
30542 hstate.add (val2->v.val_vec.array, len * elt_size);
30544 break;
30545 case dw_val_class_const_double:
30546 hstate.add_object (val2->v.val_double.low);
30547 hstate.add_object (val2->v.val_double.high);
30548 break;
30549 case dw_val_class_wide_int:
30550 hstate.add (val2->v.val_wide->get_val (),
30551 get_full_len (*val2->v.val_wide)
30552 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
30553 break;
30554 default:
30555 gcc_unreachable ();
30558 break;
30560 default:
30561 /* Other codes have no operands. */
30562 break;
30566 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
30568 static inline void
30569 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
30571 dw_loc_descr_ref l;
30572 bool sizes_computed = false;
30573 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
30574 size_of_locs (loc);
30576 for (l = loc; l != NULL; l = l->dw_loc_next)
30578 enum dwarf_location_atom opc = l->dw_loc_opc;
30579 hstate.add_object (opc);
30580 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
30582 size_of_locs (loc);
30583 sizes_computed = true;
30585 hash_loc_operands (l, hstate);
30589 /* Compute hash of the whole location list LIST_HEAD. */
30591 static inline void
30592 hash_loc_list (dw_loc_list_ref list_head)
30594 dw_loc_list_ref curr = list_head;
30595 inchash::hash hstate;
30597 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
30599 hstate.add (curr->begin, strlen (curr->begin) + 1);
30600 hstate.add (curr->end, strlen (curr->end) + 1);
30601 hstate.add_object (curr->vbegin);
30602 hstate.add_object (curr->vend);
30603 if (curr->section)
30604 hstate.add (curr->section, strlen (curr->section) + 1);
30605 hash_locs (curr->expr, hstate);
30607 list_head->hash = hstate.end ();
30610 /* Return true if X and Y opcodes have the same operands. */
30612 static inline bool
30613 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
30615 dw_val_ref valx1 = &x->dw_loc_oprnd1;
30616 dw_val_ref valx2 = &x->dw_loc_oprnd2;
30617 dw_val_ref valy1 = &y->dw_loc_oprnd1;
30618 dw_val_ref valy2 = &y->dw_loc_oprnd2;
30620 switch (x->dw_loc_opc)
30622 case DW_OP_const4u:
30623 case DW_OP_const8u:
30624 if (x->dtprel)
30625 goto hash_addr;
30626 /* FALLTHRU */
30627 case DW_OP_const1u:
30628 case DW_OP_const1s:
30629 case DW_OP_const2u:
30630 case DW_OP_const2s:
30631 case DW_OP_const4s:
30632 case DW_OP_const8s:
30633 case DW_OP_constu:
30634 case DW_OP_consts:
30635 case DW_OP_pick:
30636 case DW_OP_plus_uconst:
30637 case DW_OP_breg0:
30638 case DW_OP_breg1:
30639 case DW_OP_breg2:
30640 case DW_OP_breg3:
30641 case DW_OP_breg4:
30642 case DW_OP_breg5:
30643 case DW_OP_breg6:
30644 case DW_OP_breg7:
30645 case DW_OP_breg8:
30646 case DW_OP_breg9:
30647 case DW_OP_breg10:
30648 case DW_OP_breg11:
30649 case DW_OP_breg12:
30650 case DW_OP_breg13:
30651 case DW_OP_breg14:
30652 case DW_OP_breg15:
30653 case DW_OP_breg16:
30654 case DW_OP_breg17:
30655 case DW_OP_breg18:
30656 case DW_OP_breg19:
30657 case DW_OP_breg20:
30658 case DW_OP_breg21:
30659 case DW_OP_breg22:
30660 case DW_OP_breg23:
30661 case DW_OP_breg24:
30662 case DW_OP_breg25:
30663 case DW_OP_breg26:
30664 case DW_OP_breg27:
30665 case DW_OP_breg28:
30666 case DW_OP_breg29:
30667 case DW_OP_breg30:
30668 case DW_OP_breg31:
30669 case DW_OP_regx:
30670 case DW_OP_fbreg:
30671 case DW_OP_piece:
30672 case DW_OP_deref_size:
30673 case DW_OP_xderef_size:
30674 return valx1->v.val_int == valy1->v.val_int;
30675 case DW_OP_skip:
30676 case DW_OP_bra:
30677 /* If splitting debug info, the use of DW_OP_GNU_addr_index
30678 can cause irrelevant differences in dw_loc_addr. */
30679 gcc_assert (valx1->val_class == dw_val_class_loc
30680 && valy1->val_class == dw_val_class_loc
30681 && (dwarf_split_debug_info
30682 || x->dw_loc_addr == y->dw_loc_addr));
30683 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
30684 case DW_OP_implicit_value:
30685 if (valx1->v.val_unsigned != valy1->v.val_unsigned
30686 || valx2->val_class != valy2->val_class)
30687 return false;
30688 switch (valx2->val_class)
30690 case dw_val_class_const:
30691 return valx2->v.val_int == valy2->v.val_int;
30692 case dw_val_class_vec:
30693 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30694 && valx2->v.val_vec.length == valy2->v.val_vec.length
30695 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30696 valx2->v.val_vec.elt_size
30697 * valx2->v.val_vec.length) == 0;
30698 case dw_val_class_const_double:
30699 return valx2->v.val_double.low == valy2->v.val_double.low
30700 && valx2->v.val_double.high == valy2->v.val_double.high;
30701 case dw_val_class_wide_int:
30702 return *valx2->v.val_wide == *valy2->v.val_wide;
30703 case dw_val_class_addr:
30704 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
30705 default:
30706 gcc_unreachable ();
30708 case DW_OP_bregx:
30709 case DW_OP_bit_piece:
30710 return valx1->v.val_int == valy1->v.val_int
30711 && valx2->v.val_int == valy2->v.val_int;
30712 case DW_OP_addr:
30713 hash_addr:
30714 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
30715 case DW_OP_GNU_addr_index:
30716 case DW_OP_GNU_const_index:
30718 rtx ax1 = valx1->val_entry->addr.rtl;
30719 rtx ay1 = valy1->val_entry->addr.rtl;
30720 return rtx_equal_p (ax1, ay1);
30722 case DW_OP_implicit_pointer:
30723 case DW_OP_GNU_implicit_pointer:
30724 return valx1->val_class == dw_val_class_die_ref
30725 && valx1->val_class == valy1->val_class
30726 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
30727 && valx2->v.val_int == valy2->v.val_int;
30728 case DW_OP_entry_value:
30729 case DW_OP_GNU_entry_value:
30730 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
30731 case DW_OP_const_type:
30732 case DW_OP_GNU_const_type:
30733 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
30734 || valx2->val_class != valy2->val_class)
30735 return false;
30736 switch (valx2->val_class)
30738 case dw_val_class_const:
30739 return valx2->v.val_int == valy2->v.val_int;
30740 case dw_val_class_vec:
30741 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
30742 && valx2->v.val_vec.length == valy2->v.val_vec.length
30743 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
30744 valx2->v.val_vec.elt_size
30745 * valx2->v.val_vec.length) == 0;
30746 case dw_val_class_const_double:
30747 return valx2->v.val_double.low == valy2->v.val_double.low
30748 && valx2->v.val_double.high == valy2->v.val_double.high;
30749 case dw_val_class_wide_int:
30750 return *valx2->v.val_wide == *valy2->v.val_wide;
30751 default:
30752 gcc_unreachable ();
30754 case DW_OP_regval_type:
30755 case DW_OP_deref_type:
30756 case DW_OP_GNU_regval_type:
30757 case DW_OP_GNU_deref_type:
30758 return valx1->v.val_int == valy1->v.val_int
30759 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
30760 case DW_OP_convert:
30761 case DW_OP_reinterpret:
30762 case DW_OP_GNU_convert:
30763 case DW_OP_GNU_reinterpret:
30764 if (valx1->val_class != valy1->val_class)
30765 return false;
30766 if (valx1->val_class == dw_val_class_unsigned_const)
30767 return valx1->v.val_unsigned == valy1->v.val_unsigned;
30768 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30769 case DW_OP_GNU_parameter_ref:
30770 return valx1->val_class == dw_val_class_die_ref
30771 && valx1->val_class == valy1->val_class
30772 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
30773 default:
30774 /* Other codes have no operands. */
30775 return true;
30779 /* Return true if DWARF location expressions X and Y are the same. */
30781 static inline bool
30782 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
30784 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
30785 if (x->dw_loc_opc != y->dw_loc_opc
30786 || x->dtprel != y->dtprel
30787 || !compare_loc_operands (x, y))
30788 break;
30789 return x == NULL && y == NULL;
30792 /* Hashtable helpers. */
30794 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
30796 static inline hashval_t hash (const dw_loc_list_struct *);
30797 static inline bool equal (const dw_loc_list_struct *,
30798 const dw_loc_list_struct *);
30801 /* Return precomputed hash of location list X. */
30803 inline hashval_t
30804 loc_list_hasher::hash (const dw_loc_list_struct *x)
30806 return x->hash;
30809 /* Return true if location lists A and B are the same. */
30811 inline bool
30812 loc_list_hasher::equal (const dw_loc_list_struct *a,
30813 const dw_loc_list_struct *b)
30815 if (a == b)
30816 return 1;
30817 if (a->hash != b->hash)
30818 return 0;
30819 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
30820 if (strcmp (a->begin, b->begin) != 0
30821 || strcmp (a->end, b->end) != 0
30822 || (a->section == NULL) != (b->section == NULL)
30823 || (a->section && strcmp (a->section, b->section) != 0)
30824 || a->vbegin != b->vbegin || a->vend != b->vend
30825 || !compare_locs (a->expr, b->expr))
30826 break;
30827 return a == NULL && b == NULL;
30830 typedef hash_table<loc_list_hasher> loc_list_hash_type;
30833 /* Recursively optimize location lists referenced from DIE
30834 children and share them whenever possible. */
30836 static void
30837 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
30839 dw_die_ref c;
30840 dw_attr_node *a;
30841 unsigned ix;
30842 dw_loc_list_struct **slot;
30843 bool drop_locviews = false;
30844 bool has_locviews = false;
30846 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30847 if (AT_class (a) == dw_val_class_loc_list)
30849 dw_loc_list_ref list = AT_loc_list (a);
30850 /* TODO: perform some optimizations here, before hashing
30851 it and storing into the hash table. */
30852 hash_loc_list (list);
30853 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
30854 if (*slot == NULL)
30856 *slot = list;
30857 if (loc_list_has_views (list))
30858 gcc_assert (list->vl_symbol);
30859 else if (list->vl_symbol)
30861 drop_locviews = true;
30862 list->vl_symbol = NULL;
30865 else
30867 if (list->vl_symbol && !(*slot)->vl_symbol)
30868 drop_locviews = true;
30869 a->dw_attr_val.v.val_loc_list = *slot;
30872 else if (AT_class (a) == dw_val_class_view_list)
30874 gcc_checking_assert (a->dw_attr == DW_AT_GNU_locviews);
30875 has_locviews = true;
30879 if (drop_locviews && has_locviews)
30880 remove_AT (die, DW_AT_GNU_locviews);
30882 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
30886 /* Recursively assign each location list a unique index into the debug_addr
30887 section. */
30889 static void
30890 index_location_lists (dw_die_ref die)
30892 dw_die_ref c;
30893 dw_attr_node *a;
30894 unsigned ix;
30896 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30897 if (AT_class (a) == dw_val_class_loc_list)
30899 dw_loc_list_ref list = AT_loc_list (a);
30900 dw_loc_list_ref curr;
30901 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
30903 /* Don't index an entry that has already been indexed
30904 or won't be output. Make sure skip_loc_list_entry doesn't
30905 call size_of_locs, because that might cause circular dependency,
30906 index_location_lists requiring address table indexes to be
30907 computed, but adding new indexes through add_addr_table_entry
30908 and address table index computation requiring no new additions
30909 to the hash table. In the rare case of DWARF[234] >= 64KB
30910 location expression, we'll just waste unused address table entry
30911 for it. */
30912 if (curr->begin_entry != NULL
30913 || skip_loc_list_entry (curr))
30914 continue;
30916 curr->begin_entry
30917 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
30921 FOR_EACH_CHILD (die, c, index_location_lists (c));
30924 /* Optimize location lists referenced from DIE
30925 children and share them whenever possible. */
30927 static void
30928 optimize_location_lists (dw_die_ref die)
30930 loc_list_hash_type htab (500);
30931 optimize_location_lists_1 (die, &htab);
30934 /* Traverse the limbo die list, and add parent/child links. The only
30935 dies without parents that should be here are concrete instances of
30936 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
30937 For concrete instances, we can get the parent die from the abstract
30938 instance. */
30940 static void
30941 flush_limbo_die_list (void)
30943 limbo_die_node *node;
30945 /* get_context_die calls force_decl_die, which can put new DIEs on the
30946 limbo list in LTO mode when nested functions are put in a different
30947 partition than that of their parent function. */
30948 while ((node = limbo_die_list))
30950 dw_die_ref die = node->die;
30951 limbo_die_list = node->next;
30953 if (die->die_parent == NULL)
30955 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
30957 if (origin && origin->die_parent)
30958 add_child_die (origin->die_parent, die);
30959 else if (is_cu_die (die))
30961 else if (seen_error ())
30962 /* It's OK to be confused by errors in the input. */
30963 add_child_die (comp_unit_die (), die);
30964 else
30966 /* In certain situations, the lexical block containing a
30967 nested function can be optimized away, which results
30968 in the nested function die being orphaned. Likewise
30969 with the return type of that nested function. Force
30970 this to be a child of the containing function.
30972 It may happen that even the containing function got fully
30973 inlined and optimized out. In that case we are lost and
30974 assign the empty child. This should not be big issue as
30975 the function is likely unreachable too. */
30976 gcc_assert (node->created_for);
30978 if (DECL_P (node->created_for))
30979 origin = get_context_die (DECL_CONTEXT (node->created_for));
30980 else if (TYPE_P (node->created_for))
30981 origin = scope_die_for (node->created_for, comp_unit_die ());
30982 else
30983 origin = comp_unit_die ();
30985 add_child_die (origin, die);
30991 /* Reset DIEs so we can output them again. */
30993 static void
30994 reset_dies (dw_die_ref die)
30996 dw_die_ref c;
30998 /* Remove stuff we re-generate. */
30999 die->die_mark = 0;
31000 die->die_offset = 0;
31001 die->die_abbrev = 0;
31002 remove_AT (die, DW_AT_sibling);
31004 FOR_EACH_CHILD (die, c, reset_dies (c));
31007 /* Output stuff that dwarf requires at the end of every file,
31008 and generate the DWARF-2 debugging info. */
31010 static void
31011 dwarf2out_finish (const char *)
31013 comdat_type_node *ctnode;
31014 dw_die_ref main_comp_unit_die;
31015 unsigned char checksum[16];
31016 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31018 /* Flush out any latecomers to the limbo party. */
31019 flush_limbo_die_list ();
31021 if (inline_entry_data_table)
31022 gcc_assert (inline_entry_data_table->elements () == 0);
31024 if (flag_checking)
31026 verify_die (comp_unit_die ());
31027 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31028 verify_die (node->die);
31031 /* We shouldn't have any symbols with delayed asm names for
31032 DIEs generated after early finish. */
31033 gcc_assert (deferred_asm_name == NULL);
31035 gen_remaining_tmpl_value_param_die_attribute ();
31037 if (flag_generate_lto || flag_generate_offload)
31039 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
31041 /* Prune stuff so that dwarf2out_finish runs successfully
31042 for the fat part of the object. */
31043 reset_dies (comp_unit_die ());
31044 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31045 reset_dies (node->die);
31047 hash_table<comdat_type_hasher> comdat_type_table (100);
31048 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31050 comdat_type_node **slot
31051 = comdat_type_table.find_slot (ctnode, INSERT);
31053 /* Don't reset types twice. */
31054 if (*slot != HTAB_EMPTY_ENTRY)
31055 continue;
31057 /* Add a pointer to the line table for the main compilation unit
31058 so that the debugger can make sense of DW_AT_decl_file
31059 attributes. */
31060 if (debug_info_level >= DINFO_LEVEL_TERSE)
31061 reset_dies (ctnode->root_die);
31063 *slot = ctnode;
31066 /* Reset die CU symbol so we don't output it twice. */
31067 comp_unit_die ()->die_id.die_symbol = NULL;
31069 /* Remove DW_AT_macro and DW_AT_stmt_list from the early output. */
31070 remove_AT (comp_unit_die (), DW_AT_stmt_list);
31071 if (have_macinfo)
31072 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
31074 /* Remove indirect string decisions. */
31075 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
31078 #if ENABLE_ASSERT_CHECKING
31080 dw_die_ref die = comp_unit_die (), c;
31081 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
31083 #endif
31084 resolve_addr (comp_unit_die ());
31085 move_marked_base_types ();
31087 /* Initialize sections and labels used for actual assembler output. */
31088 unsigned generation = init_sections_and_labels (false);
31090 /* Traverse the DIE's and add sibling attributes to those DIE's that
31091 have children. */
31092 add_sibling_attributes (comp_unit_die ());
31093 limbo_die_node *node;
31094 for (node = cu_die_list; node; node = node->next)
31095 add_sibling_attributes (node->die);
31096 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31097 add_sibling_attributes (ctnode->root_die);
31099 /* When splitting DWARF info, we put some attributes in the
31100 skeleton compile_unit DIE that remains in the .o, while
31101 most attributes go in the DWO compile_unit_die. */
31102 if (dwarf_split_debug_info)
31104 limbo_die_node *cu;
31105 main_comp_unit_die = gen_compile_unit_die (NULL);
31106 if (dwarf_version >= 5)
31107 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
31108 cu = limbo_die_list;
31109 gcc_assert (cu->die == main_comp_unit_die);
31110 limbo_die_list = limbo_die_list->next;
31111 cu->next = cu_die_list;
31112 cu_die_list = cu;
31114 else
31115 main_comp_unit_die = comp_unit_die ();
31117 /* Output a terminator label for the .text section. */
31118 switch_to_section (text_section);
31119 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
31120 if (cold_text_section)
31122 switch_to_section (cold_text_section);
31123 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
31126 /* We can only use the low/high_pc attributes if all of the code was
31127 in .text. */
31128 if (!have_multiple_function_sections
31129 || (dwarf_version < 3 && dwarf_strict))
31131 /* Don't add if the CU has no associated code. */
31132 if (text_section_used)
31133 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
31134 text_end_label, true);
31136 else
31138 unsigned fde_idx;
31139 dw_fde_ref fde;
31140 bool range_list_added = false;
31142 if (text_section_used)
31143 add_ranges_by_labels (main_comp_unit_die, text_section_label,
31144 text_end_label, &range_list_added, true);
31145 if (cold_text_section_used)
31146 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
31147 cold_end_label, &range_list_added, true);
31149 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
31151 if (DECL_IGNORED_P (fde->decl))
31152 continue;
31153 if (!fde->in_std_section)
31154 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
31155 fde->dw_fde_end, &range_list_added,
31156 true);
31157 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
31158 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
31159 fde->dw_fde_second_end, &range_list_added,
31160 true);
31163 if (range_list_added)
31165 /* We need to give .debug_loc and .debug_ranges an appropriate
31166 "base address". Use zero so that these addresses become
31167 absolute. Historically, we've emitted the unexpected
31168 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
31169 Emit both to give time for other tools to adapt. */
31170 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
31171 if (! dwarf_strict && dwarf_version < 4)
31172 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
31174 add_ranges (NULL);
31178 /* AIX Assembler inserts the length, so adjust the reference to match the
31179 offset expected by debuggers. */
31180 strcpy (dl_section_ref, debug_line_section_label);
31181 if (XCOFF_DEBUGGING_INFO)
31182 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31184 if (debug_info_level >= DINFO_LEVEL_TERSE)
31185 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
31186 dl_section_ref);
31188 if (have_macinfo)
31189 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31190 macinfo_section_label);
31192 if (dwarf_split_debug_info)
31194 if (have_location_lists)
31196 if (dwarf_version >= 5)
31197 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
31198 loc_section_label);
31199 /* optimize_location_lists calculates the size of the lists,
31200 so index them first, and assign indices to the entries.
31201 Although optimize_location_lists will remove entries from
31202 the table, it only does so for duplicates, and therefore
31203 only reduces ref_counts to 1. */
31204 index_location_lists (comp_unit_die ());
31207 if (addr_index_table != NULL)
31209 unsigned int index = 0;
31210 addr_index_table
31211 ->traverse_noresize<unsigned int *, index_addr_table_entry>
31212 (&index);
31216 loc_list_idx = 0;
31217 if (have_location_lists)
31219 optimize_location_lists (comp_unit_die ());
31220 /* And finally assign indexes to the entries for -gsplit-dwarf. */
31221 if (dwarf_version >= 5 && dwarf_split_debug_info)
31222 assign_location_list_indexes (comp_unit_die ());
31225 save_macinfo_strings ();
31227 if (dwarf_split_debug_info)
31229 unsigned int index = 0;
31231 /* Add attributes common to skeleton compile_units and
31232 type_units. Because these attributes include strings, it
31233 must be done before freezing the string table. Top-level
31234 skeleton die attrs are added when the skeleton type unit is
31235 created, so ensure it is created by this point. */
31236 add_top_level_skeleton_die_attrs (main_comp_unit_die);
31237 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31240 /* Output all of the compilation units. We put the main one last so that
31241 the offsets are available to output_pubnames. */
31242 for (node = cu_die_list; node; node = node->next)
31243 output_comp_unit (node->die, 0, NULL);
31245 hash_table<comdat_type_hasher> comdat_type_table (100);
31246 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
31248 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31250 /* Don't output duplicate types. */
31251 if (*slot != HTAB_EMPTY_ENTRY)
31252 continue;
31254 /* Add a pointer to the line table for the main compilation unit
31255 so that the debugger can make sense of DW_AT_decl_file
31256 attributes. */
31257 if (debug_info_level >= DINFO_LEVEL_TERSE)
31258 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31259 (!dwarf_split_debug_info
31260 ? dl_section_ref
31261 : debug_skeleton_line_section_label));
31263 output_comdat_type_unit (ctnode);
31264 *slot = ctnode;
31267 if (dwarf_split_debug_info)
31269 int mark;
31270 struct md5_ctx ctx;
31272 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
31273 index_rnglists ();
31275 /* Compute a checksum of the comp_unit to use as the dwo_id. */
31276 md5_init_ctx (&ctx);
31277 mark = 0;
31278 die_checksum (comp_unit_die (), &ctx, &mark);
31279 unmark_all_dies (comp_unit_die ());
31280 md5_finish_ctx (&ctx, checksum);
31282 if (dwarf_version < 5)
31284 /* Use the first 8 bytes of the checksum as the dwo_id,
31285 and add it to both comp-unit DIEs. */
31286 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
31287 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
31290 /* Add the base offset of the ranges table to the skeleton
31291 comp-unit DIE. */
31292 if (!vec_safe_is_empty (ranges_table))
31294 if (dwarf_version >= 5)
31295 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
31296 ranges_base_label);
31297 else
31298 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
31299 ranges_section_label);
31302 switch_to_section (debug_addr_section);
31303 /* GNU DebugFission https://gcc.gnu.org/wiki/DebugFission
31304 which GCC uses to implement -gsplit-dwarf as DWARF GNU extension
31305 before DWARF5, didn't have a header for .debug_addr units.
31306 DWARF5 specifies a small header when address tables are used. */
31307 if (dwarf_version >= 5)
31309 unsigned long addrs_length
31310 = addr_index_table->elements () * DWARF2_ADDR_SIZE + 4;
31312 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31313 dw2_asm_output_data (4, 0xffffffff,
31314 "Escape value for 64-bit DWARF extension");
31315 dw2_asm_output_data (DWARF_OFFSET_SIZE, addrs_length,
31316 "Length of Address Unit");
31317 dw2_asm_output_data (2, 5, "DWARF addr version");
31318 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
31319 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
31321 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
31322 output_addr_table ();
31325 /* Output the main compilation unit if non-empty or if .debug_macinfo
31326 or .debug_macro will be emitted. */
31327 output_comp_unit (comp_unit_die (), have_macinfo,
31328 dwarf_split_debug_info ? checksum : NULL);
31330 if (dwarf_split_debug_info && info_section_emitted)
31331 output_skeleton_debug_sections (main_comp_unit_die, checksum);
31333 /* Output the abbreviation table. */
31334 if (vec_safe_length (abbrev_die_table) != 1)
31336 switch_to_section (debug_abbrev_section);
31337 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31338 output_abbrev_section ();
31341 /* Output location list section if necessary. */
31342 if (have_location_lists)
31344 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
31345 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
31346 /* Output the location lists info. */
31347 switch_to_section (debug_loc_section);
31348 if (dwarf_version >= 5)
31350 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
31351 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
31352 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
31353 dw2_asm_output_data (4, 0xffffffff,
31354 "Initial length escape value indicating "
31355 "64-bit DWARF extension");
31356 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
31357 "Length of Location Lists");
31358 ASM_OUTPUT_LABEL (asm_out_file, l1);
31359 output_dwarf_version ();
31360 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
31361 dw2_asm_output_data (1, 0, "Segment Size");
31362 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
31363 "Offset Entry Count");
31365 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
31366 if (dwarf_version >= 5 && dwarf_split_debug_info)
31368 unsigned int save_loc_list_idx = loc_list_idx;
31369 loc_list_idx = 0;
31370 output_loclists_offsets (comp_unit_die ());
31371 gcc_assert (save_loc_list_idx == loc_list_idx);
31373 output_location_lists (comp_unit_die ());
31374 if (dwarf_version >= 5)
31375 ASM_OUTPUT_LABEL (asm_out_file, l2);
31378 output_pubtables ();
31380 /* Output the address range information if a CU (.debug_info section)
31381 was emitted. We output an empty table even if we had no functions
31382 to put in it. This because the consumer has no way to tell the
31383 difference between an empty table that we omitted and failure to
31384 generate a table that would have contained data. */
31385 if (info_section_emitted)
31387 switch_to_section (debug_aranges_section);
31388 output_aranges ();
31391 /* Output ranges section if necessary. */
31392 if (!vec_safe_is_empty (ranges_table))
31394 if (dwarf_version >= 5)
31395 output_rnglists (generation);
31396 else
31397 output_ranges ();
31400 /* Have to end the macro section. */
31401 if (have_macinfo)
31403 switch_to_section (debug_macinfo_section);
31404 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31405 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
31406 : debug_skeleton_line_section_label, false);
31407 dw2_asm_output_data (1, 0, "End compilation unit");
31410 /* Output the source line correspondence table. We must do this
31411 even if there is no line information. Otherwise, on an empty
31412 translation unit, we will generate a present, but empty,
31413 .debug_info section. IRIX 6.5 `nm' will then complain when
31414 examining the file. This is done late so that any filenames
31415 used by the debug_info section are marked as 'used'. */
31416 switch_to_section (debug_line_section);
31417 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31418 if (! output_asm_line_debug_info ())
31419 output_line_info (false);
31421 if (dwarf_split_debug_info && info_section_emitted)
31423 switch_to_section (debug_skeleton_line_section);
31424 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
31425 output_line_info (true);
31428 /* If we emitted any indirect strings, output the string table too. */
31429 if (debug_str_hash || skeleton_debug_str_hash)
31430 output_indirect_strings ();
31431 if (debug_line_str_hash)
31433 switch_to_section (debug_line_str_section);
31434 const enum dwarf_form form = DW_FORM_line_strp;
31435 debug_line_str_hash->traverse<enum dwarf_form,
31436 output_indirect_string> (form);
31439 /* ??? Move lvugid out of dwarf2out_source_line and reset it too? */
31440 symview_upper_bound = 0;
31441 if (zero_view_p)
31442 bitmap_clear (zero_view_p);
31445 /* Returns a hash value for X (which really is a variable_value_struct). */
31447 inline hashval_t
31448 variable_value_hasher::hash (variable_value_struct *x)
31450 return (hashval_t) x->decl_id;
31453 /* Return nonzero if decl_id of variable_value_struct X is the same as
31454 UID of decl Y. */
31456 inline bool
31457 variable_value_hasher::equal (variable_value_struct *x, tree y)
31459 return x->decl_id == DECL_UID (y);
31462 /* Helper function for resolve_variable_value, handle
31463 DW_OP_GNU_variable_value in one location expression.
31464 Return true if exprloc has been changed into loclist. */
31466 static bool
31467 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
31469 dw_loc_descr_ref next;
31470 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
31472 next = loc->dw_loc_next;
31473 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
31474 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
31475 continue;
31477 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31478 if (DECL_CONTEXT (decl) != current_function_decl)
31479 continue;
31481 dw_die_ref ref = lookup_decl_die (decl);
31482 if (ref)
31484 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31485 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31486 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31487 continue;
31489 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
31490 if (l == NULL)
31491 continue;
31492 if (l->dw_loc_next)
31494 if (AT_class (a) != dw_val_class_loc)
31495 continue;
31496 switch (a->dw_attr)
31498 /* Following attributes allow both exprloc and loclist
31499 classes, so we can change them into a loclist. */
31500 case DW_AT_location:
31501 case DW_AT_string_length:
31502 case DW_AT_return_addr:
31503 case DW_AT_data_member_location:
31504 case DW_AT_frame_base:
31505 case DW_AT_segment:
31506 case DW_AT_static_link:
31507 case DW_AT_use_location:
31508 case DW_AT_vtable_elem_location:
31509 if (prev)
31511 prev->dw_loc_next = NULL;
31512 prepend_loc_descr_to_each (l, AT_loc (a));
31514 if (next)
31515 add_loc_descr_to_each (l, next);
31516 a->dw_attr_val.val_class = dw_val_class_loc_list;
31517 a->dw_attr_val.val_entry = NULL;
31518 a->dw_attr_val.v.val_loc_list = l;
31519 have_location_lists = true;
31520 return true;
31521 /* Following attributes allow both exprloc and reference,
31522 so if the whole expression is DW_OP_GNU_variable_value alone
31523 we could transform it into reference. */
31524 case DW_AT_byte_size:
31525 case DW_AT_bit_size:
31526 case DW_AT_lower_bound:
31527 case DW_AT_upper_bound:
31528 case DW_AT_bit_stride:
31529 case DW_AT_count:
31530 case DW_AT_allocated:
31531 case DW_AT_associated:
31532 case DW_AT_byte_stride:
31533 if (prev == NULL && next == NULL)
31534 break;
31535 /* FALLTHRU */
31536 default:
31537 if (dwarf_strict)
31538 continue;
31539 break;
31541 /* Create DW_TAG_variable that we can refer to. */
31542 gen_decl_die (decl, NULL_TREE, NULL,
31543 lookup_decl_die (current_function_decl));
31544 ref = lookup_decl_die (decl);
31545 if (ref)
31547 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31548 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31549 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31551 continue;
31553 if (prev)
31555 prev->dw_loc_next = l->expr;
31556 add_loc_descr (&prev->dw_loc_next, next);
31557 free_loc_descr (loc, NULL);
31558 next = prev->dw_loc_next;
31560 else
31562 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
31563 add_loc_descr (&loc, next);
31564 next = loc;
31566 loc = prev;
31568 return false;
31571 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
31573 static void
31574 resolve_variable_value (dw_die_ref die)
31576 dw_attr_node *a;
31577 dw_loc_list_ref loc;
31578 unsigned ix;
31580 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31581 switch (AT_class (a))
31583 case dw_val_class_loc:
31584 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
31585 break;
31586 /* FALLTHRU */
31587 case dw_val_class_loc_list:
31588 loc = AT_loc_list (a);
31589 gcc_assert (loc);
31590 for (; loc; loc = loc->dw_loc_next)
31591 resolve_variable_value_in_expr (a, loc->expr);
31592 break;
31593 default:
31594 break;
31598 /* Attempt to optimize DW_OP_GNU_variable_value refering to
31599 temporaries in the current function. */
31601 static void
31602 resolve_variable_values (void)
31604 if (!variable_value_hash || !current_function_decl)
31605 return;
31607 struct variable_value_struct *node
31608 = variable_value_hash->find_with_hash (current_function_decl,
31609 DECL_UID (current_function_decl));
31611 if (node == NULL)
31612 return;
31614 unsigned int i;
31615 dw_die_ref die;
31616 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
31617 resolve_variable_value (die);
31620 /* Helper function for note_variable_value, handle one location
31621 expression. */
31623 static void
31624 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
31626 for (; loc; loc = loc->dw_loc_next)
31627 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
31628 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
31630 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
31631 dw_die_ref ref = lookup_decl_die (decl);
31632 if (! ref && (flag_generate_lto || flag_generate_offload))
31634 /* ??? This is somewhat a hack because we do not create DIEs
31635 for variables not in BLOCK trees early but when generating
31636 early LTO output we need the dw_val_class_decl_ref to be
31637 fully resolved. For fat LTO objects we'd also like to
31638 undo this after LTO dwarf output. */
31639 gcc_assert (DECL_CONTEXT (decl));
31640 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
31641 gcc_assert (ctx != NULL);
31642 gen_decl_die (decl, NULL_TREE, NULL, ctx);
31643 ref = lookup_decl_die (decl);
31644 gcc_assert (ref != NULL);
31646 if (ref)
31648 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
31649 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
31650 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
31651 continue;
31653 if (VAR_P (decl)
31654 && DECL_CONTEXT (decl)
31655 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
31656 && lookup_decl_die (DECL_CONTEXT (decl)))
31658 if (!variable_value_hash)
31659 variable_value_hash
31660 = hash_table<variable_value_hasher>::create_ggc (10);
31662 tree fndecl = DECL_CONTEXT (decl);
31663 struct variable_value_struct *node;
31664 struct variable_value_struct **slot
31665 = variable_value_hash->find_slot_with_hash (fndecl,
31666 DECL_UID (fndecl),
31667 INSERT);
31668 if (*slot == NULL)
31670 node = ggc_cleared_alloc<variable_value_struct> ();
31671 node->decl_id = DECL_UID (fndecl);
31672 *slot = node;
31674 else
31675 node = *slot;
31677 vec_safe_push (node->dies, die);
31682 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
31683 with dw_val_class_decl_ref operand. */
31685 static void
31686 note_variable_value (dw_die_ref die)
31688 dw_die_ref c;
31689 dw_attr_node *a;
31690 dw_loc_list_ref loc;
31691 unsigned ix;
31693 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
31694 switch (AT_class (a))
31696 case dw_val_class_loc_list:
31697 loc = AT_loc_list (a);
31698 gcc_assert (loc);
31699 if (!loc->noted_variable_value)
31701 loc->noted_variable_value = 1;
31702 for (; loc; loc = loc->dw_loc_next)
31703 note_variable_value_in_expr (die, loc->expr);
31705 break;
31706 case dw_val_class_loc:
31707 note_variable_value_in_expr (die, AT_loc (a));
31708 break;
31709 default:
31710 break;
31713 /* Mark children. */
31714 FOR_EACH_CHILD (die, c, note_variable_value (c));
31717 /* Perform any cleanups needed after the early debug generation pass
31718 has run. */
31720 static void
31721 dwarf2out_early_finish (const char *filename)
31723 set_early_dwarf s;
31724 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
31726 /* PCH might result in DW_AT_producer string being restored from the
31727 header compilation, so always fill it with empty string initially
31728 and overwrite only here. */
31729 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
31730 producer_string = gen_producer_string ();
31731 producer->dw_attr_val.v.val_str->refcount--;
31732 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
31734 /* Add the name for the main input file now. We delayed this from
31735 dwarf2out_init to avoid complications with PCH. */
31736 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
31737 add_comp_dir_attribute (comp_unit_die ());
31739 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
31740 DW_AT_comp_dir into .debug_line_str section. */
31741 if (!dwarf2out_as_loc_support
31742 && dwarf_version >= 5
31743 && DWARF5_USE_DEBUG_LINE_STR)
31745 for (int i = 0; i < 2; i++)
31747 dw_attr_node *a = get_AT (comp_unit_die (),
31748 i ? DW_AT_comp_dir : DW_AT_name);
31749 if (a == NULL
31750 || AT_class (a) != dw_val_class_str
31751 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
31752 continue;
31754 if (! debug_line_str_hash)
31755 debug_line_str_hash
31756 = hash_table<indirect_string_hasher>::create_ggc (10);
31758 struct indirect_string_node *node
31759 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
31760 set_indirect_string (node);
31761 node->form = DW_FORM_line_strp;
31762 a->dw_attr_val.v.val_str->refcount--;
31763 a->dw_attr_val.v.val_str = node;
31767 /* With LTO early dwarf was really finished at compile-time, so make
31768 sure to adjust the phase after annotating the LTRANS CU DIE. */
31769 if (in_lto_p)
31771 early_dwarf_finished = true;
31772 return;
31775 /* Walk through the list of incomplete types again, trying once more to
31776 emit full debugging info for them. */
31777 retry_incomplete_types ();
31779 /* The point here is to flush out the limbo list so that it is empty
31780 and we don't need to stream it for LTO. */
31781 flush_limbo_die_list ();
31783 gen_scheduled_generic_parms_dies ();
31784 gen_remaining_tmpl_value_param_die_attribute ();
31786 /* Add DW_AT_linkage_name for all deferred DIEs. */
31787 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
31789 tree decl = node->created_for;
31790 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
31791 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
31792 ended up in deferred_asm_name before we knew it was
31793 constant and never written to disk. */
31794 && DECL_ASSEMBLER_NAME (decl))
31796 add_linkage_attr (node->die, decl);
31797 move_linkage_attr (node->die);
31800 deferred_asm_name = NULL;
31802 if (flag_eliminate_unused_debug_types)
31803 prune_unused_types ();
31805 /* Generate separate COMDAT sections for type DIEs. */
31806 if (use_debug_types)
31808 break_out_comdat_types (comp_unit_die ());
31810 /* Each new type_unit DIE was added to the limbo die list when created.
31811 Since these have all been added to comdat_type_list, clear the
31812 limbo die list. */
31813 limbo_die_list = NULL;
31815 /* For each new comdat type unit, copy declarations for incomplete
31816 types to make the new unit self-contained (i.e., no direct
31817 references to the main compile unit). */
31818 for (comdat_type_node *ctnode = comdat_type_list;
31819 ctnode != NULL; ctnode = ctnode->next)
31820 copy_decls_for_unworthy_types (ctnode->root_die);
31821 copy_decls_for_unworthy_types (comp_unit_die ());
31823 /* In the process of copying declarations from one unit to another,
31824 we may have left some declarations behind that are no longer
31825 referenced. Prune them. */
31826 prune_unused_types ();
31829 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
31830 with dw_val_class_decl_ref operand. */
31831 note_variable_value (comp_unit_die ());
31832 for (limbo_die_node *node = cu_die_list; node; node = node->next)
31833 note_variable_value (node->die);
31834 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
31835 ctnode = ctnode->next)
31836 note_variable_value (ctnode->root_die);
31837 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31838 note_variable_value (node->die);
31840 /* The AT_pubnames attribute needs to go in all skeleton dies, including
31841 both the main_cu and all skeleton TUs. Making this call unconditional
31842 would end up either adding a second copy of the AT_pubnames attribute, or
31843 requiring a special case in add_top_level_skeleton_die_attrs. */
31844 if (!dwarf_split_debug_info)
31845 add_AT_pubnames (comp_unit_die ());
31847 /* The early debug phase is now finished. */
31848 early_dwarf_finished = true;
31850 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
31851 if ((!flag_generate_lto && !flag_generate_offload)
31852 /* FIXME: Disable debug info generation for PE-COFF targets since the
31853 copy_lto_debug_sections operation of the simple object support in
31854 libiberty is not implemented for them yet. */
31855 || TARGET_PECOFF)
31856 return;
31858 /* Now as we are going to output for LTO initialize sections and labels
31859 to the LTO variants. We don't need a random-seed postfix as other
31860 LTO sections as linking the LTO debug sections into one in a partial
31861 link is fine. */
31862 init_sections_and_labels (true);
31864 /* The output below is modeled after dwarf2out_finish with all
31865 location related output removed and some LTO specific changes.
31866 Some refactoring might make both smaller and easier to match up. */
31868 /* Traverse the DIE's and add add sibling attributes to those DIE's
31869 that have children. */
31870 add_sibling_attributes (comp_unit_die ());
31871 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31872 add_sibling_attributes (node->die);
31873 for (comdat_type_node *ctnode = comdat_type_list;
31874 ctnode != NULL; ctnode = ctnode->next)
31875 add_sibling_attributes (ctnode->root_die);
31877 /* AIX Assembler inserts the length, so adjust the reference to match the
31878 offset expected by debuggers. */
31879 strcpy (dl_section_ref, debug_line_section_label);
31880 if (XCOFF_DEBUGGING_INFO)
31881 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
31883 if (debug_info_level >= DINFO_LEVEL_TERSE)
31884 add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list, dl_section_ref);
31886 if (have_macinfo)
31887 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
31888 macinfo_section_label);
31890 save_macinfo_strings ();
31892 if (dwarf_split_debug_info)
31894 unsigned int index = 0;
31895 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
31898 /* Output all of the compilation units. We put the main one last so that
31899 the offsets are available to output_pubnames. */
31900 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
31901 output_comp_unit (node->die, 0, NULL);
31903 hash_table<comdat_type_hasher> comdat_type_table (100);
31904 for (comdat_type_node *ctnode = comdat_type_list;
31905 ctnode != NULL; ctnode = ctnode->next)
31907 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
31909 /* Don't output duplicate types. */
31910 if (*slot != HTAB_EMPTY_ENTRY)
31911 continue;
31913 /* Add a pointer to the line table for the main compilation unit
31914 so that the debugger can make sense of DW_AT_decl_file
31915 attributes. */
31916 if (debug_info_level >= DINFO_LEVEL_TERSE)
31917 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
31918 (!dwarf_split_debug_info
31919 ? debug_line_section_label
31920 : debug_skeleton_line_section_label));
31922 output_comdat_type_unit (ctnode);
31923 *slot = ctnode;
31926 /* Stick a unique symbol to the main debuginfo section. */
31927 compute_comp_unit_symbol (comp_unit_die ());
31929 /* Output the main compilation unit. We always need it if only for
31930 the CU symbol. */
31931 output_comp_unit (comp_unit_die (), true, NULL);
31933 /* Output the abbreviation table. */
31934 if (vec_safe_length (abbrev_die_table) != 1)
31936 switch_to_section (debug_abbrev_section);
31937 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
31938 output_abbrev_section ();
31941 /* Have to end the macro section. */
31942 if (have_macinfo)
31944 /* We have to save macinfo state if we need to output it again
31945 for the FAT part of the object. */
31946 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
31947 if (flag_fat_lto_objects)
31948 macinfo_table = macinfo_table->copy ();
31950 switch_to_section (debug_macinfo_section);
31951 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
31952 output_macinfo (debug_line_section_label, true);
31953 dw2_asm_output_data (1, 0, "End compilation unit");
31955 if (flag_fat_lto_objects)
31957 vec_free (macinfo_table);
31958 macinfo_table = saved_macinfo_table;
31962 /* Emit a skeleton debug_line section. */
31963 switch_to_section (debug_line_section);
31964 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
31965 output_line_info (true);
31967 /* If we emitted any indirect strings, output the string table too. */
31968 if (debug_str_hash || skeleton_debug_str_hash)
31969 output_indirect_strings ();
31971 /* Switch back to the text section. */
31972 switch_to_section (text_section);
31975 /* Reset all state within dwarf2out.c so that we can rerun the compiler
31976 within the same process. For use by toplev::finalize. */
31978 void
31979 dwarf2out_c_finalize (void)
31981 last_var_location_insn = NULL;
31982 cached_next_real_insn = NULL;
31983 used_rtx_array = NULL;
31984 incomplete_types = NULL;
31985 decl_scope_table = NULL;
31986 debug_info_section = NULL;
31987 debug_skeleton_info_section = NULL;
31988 debug_abbrev_section = NULL;
31989 debug_skeleton_abbrev_section = NULL;
31990 debug_aranges_section = NULL;
31991 debug_addr_section = NULL;
31992 debug_macinfo_section = NULL;
31993 debug_line_section = NULL;
31994 debug_skeleton_line_section = NULL;
31995 debug_loc_section = NULL;
31996 debug_pubnames_section = NULL;
31997 debug_pubtypes_section = NULL;
31998 debug_str_section = NULL;
31999 debug_line_str_section = NULL;
32000 debug_str_dwo_section = NULL;
32001 debug_str_offsets_section = NULL;
32002 debug_ranges_section = NULL;
32003 debug_frame_section = NULL;
32004 fde_vec = NULL;
32005 debug_str_hash = NULL;
32006 debug_line_str_hash = NULL;
32007 skeleton_debug_str_hash = NULL;
32008 dw2_string_counter = 0;
32009 have_multiple_function_sections = false;
32010 text_section_used = false;
32011 cold_text_section_used = false;
32012 cold_text_section = NULL;
32013 current_unit_personality = NULL;
32015 early_dwarf = false;
32016 early_dwarf_finished = false;
32018 next_die_offset = 0;
32019 single_comp_unit_die = NULL;
32020 comdat_type_list = NULL;
32021 limbo_die_list = NULL;
32022 file_table = NULL;
32023 decl_die_table = NULL;
32024 common_block_die_table = NULL;
32025 decl_loc_table = NULL;
32026 call_arg_locations = NULL;
32027 call_arg_loc_last = NULL;
32028 call_site_count = -1;
32029 tail_call_site_count = -1;
32030 cached_dw_loc_list_table = NULL;
32031 abbrev_die_table = NULL;
32032 delete dwarf_proc_stack_usage_map;
32033 dwarf_proc_stack_usage_map = NULL;
32034 line_info_label_num = 0;
32035 cur_line_info_table = NULL;
32036 text_section_line_info = NULL;
32037 cold_text_section_line_info = NULL;
32038 separate_line_info = NULL;
32039 info_section_emitted = false;
32040 pubname_table = NULL;
32041 pubtype_table = NULL;
32042 macinfo_table = NULL;
32043 ranges_table = NULL;
32044 ranges_by_label = NULL;
32045 rnglist_idx = 0;
32046 have_location_lists = false;
32047 loclabel_num = 0;
32048 poc_label_num = 0;
32049 last_emitted_file = NULL;
32050 label_num = 0;
32051 tmpl_value_parm_die_table = NULL;
32052 generic_type_instances = NULL;
32053 frame_pointer_fb_offset = 0;
32054 frame_pointer_fb_offset_valid = false;
32055 base_types.release ();
32056 XDELETEVEC (producer_string);
32057 producer_string = NULL;
32060 #include "gt-dwarf2out.h"