PR target/83368
[official-gcc.git] / gcc / dwarf2out.c
blobfc206562abe8905ea558eecfc48f3a0ecaf806d6
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"
99 static void dwarf2out_source_line (unsigned int, unsigned int, const char *,
100 int, bool);
101 static rtx_insn *last_var_location_insn;
102 static rtx_insn *cached_next_real_insn;
103 static void dwarf2out_decl (tree);
105 #ifndef XCOFF_DEBUGGING_INFO
106 #define XCOFF_DEBUGGING_INFO 0
107 #endif
109 #ifndef HAVE_XCOFF_DWARF_EXTRAS
110 #define HAVE_XCOFF_DWARF_EXTRAS 0
111 #endif
113 #ifdef VMS_DEBUGGING_INFO
114 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
116 /* Define this macro to be a nonzero value if the directory specifications
117 which are output in the debug info should end with a separator. */
118 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
119 /* Define this macro to evaluate to a nonzero value if GCC should refrain
120 from generating indirect strings in DWARF2 debug information, for instance
121 if your target is stuck with an old version of GDB that is unable to
122 process them properly or uses VMS Debug. */
123 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
124 #else
125 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
126 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
127 #endif
129 /* ??? Poison these here until it can be done generically. They've been
130 totally replaced in this file; make sure it stays that way. */
131 #undef DWARF2_UNWIND_INFO
132 #undef DWARF2_FRAME_INFO
133 #if (GCC_VERSION >= 3000)
134 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
135 #endif
137 /* The size of the target's pointer type. */
138 #ifndef PTR_SIZE
139 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
140 #endif
142 /* Array of RTXes referenced by the debugging information, which therefore
143 must be kept around forever. */
144 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
146 /* A pointer to the base of a list of incomplete types which might be
147 completed at some later time. incomplete_types_list needs to be a
148 vec<tree, va_gc> *because we want to tell the garbage collector about
149 it. */
150 static GTY(()) vec<tree, va_gc> *incomplete_types;
152 /* A pointer to the base of a table of references to declaration
153 scopes. This table is a display which tracks the nesting
154 of declaration scopes at the current scope and containing
155 scopes. This table is used to find the proper place to
156 define type declaration DIE's. */
157 static GTY(()) vec<tree, va_gc> *decl_scope_table;
159 /* Pointers to various DWARF2 sections. */
160 static GTY(()) section *debug_info_section;
161 static GTY(()) section *debug_skeleton_info_section;
162 static GTY(()) section *debug_abbrev_section;
163 static GTY(()) section *debug_skeleton_abbrev_section;
164 static GTY(()) section *debug_aranges_section;
165 static GTY(()) section *debug_addr_section;
166 static GTY(()) section *debug_macinfo_section;
167 static const char *debug_macinfo_section_name;
168 static unsigned macinfo_label_base = 1;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_line_str_section;
176 static GTY(()) section *debug_str_dwo_section;
177 static GTY(()) section *debug_str_offsets_section;
178 static GTY(()) section *debug_ranges_section;
179 static GTY(()) section *debug_frame_section;
181 /* Maximum size (in bytes) of an artificially generated label. */
182 #define MAX_ARTIFICIAL_LABEL_BYTES 40
184 /* According to the (draft) DWARF 3 specification, the initial length
185 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
186 bytes are 0xffffffff, followed by the length stored in the next 8
187 bytes.
189 However, the SGI/MIPS ABI uses an initial length which is equal to
190 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
192 #ifndef DWARF_INITIAL_LENGTH_SIZE
193 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
194 #endif
196 #ifndef DWARF_INITIAL_LENGTH_SIZE_STR
197 #define DWARF_INITIAL_LENGTH_SIZE_STR (DWARF_OFFSET_SIZE == 4 ? "-4" : "-12")
198 #endif
200 /* Round SIZE up to the nearest BOUNDARY. */
201 #define DWARF_ROUND(SIZE,BOUNDARY) \
202 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
204 /* CIE identifier. */
205 #if HOST_BITS_PER_WIDE_INT >= 64
206 #define DWARF_CIE_ID \
207 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
208 #else
209 #define DWARF_CIE_ID DW_CIE_ID
210 #endif
213 /* A vector for a table that contains frame description
214 information for each routine. */
215 #define NOT_INDEXED (-1U)
216 #define NO_INDEX_ASSIGNED (-2U)
218 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
220 struct GTY((for_user)) indirect_string_node {
221 const char *str;
222 unsigned int refcount;
223 enum dwarf_form form;
224 char *label;
225 unsigned int index;
228 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
230 typedef const char *compare_type;
232 static hashval_t hash (indirect_string_node *);
233 static bool equal (indirect_string_node *, const char *);
236 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
238 static GTY (()) hash_table<indirect_string_hasher> *debug_line_str_hash;
240 /* With split_debug_info, both the comp_dir and dwo_name go in the
241 main object file, rather than the dwo, similar to the force_direct
242 parameter elsewhere but with additional complications:
244 1) The string is needed in both the main object file and the dwo.
245 That is, the comp_dir and dwo_name will appear in both places.
247 2) Strings can use four forms: DW_FORM_string, DW_FORM_strp,
248 DW_FORM_line_strp or DW_FORM_GNU_str_index.
250 3) GCC chooses the form to use late, depending on the size and
251 reference count.
253 Rather than forcing the all debug string handling functions and
254 callers to deal with these complications, simply use a separate,
255 special-cased string table for any attribute that should go in the
256 main object file. This limits the complexity to just the places
257 that need it. */
259 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
261 static GTY(()) int dw2_string_counter;
263 /* True if the compilation unit places functions in more than one section. */
264 static GTY(()) bool have_multiple_function_sections = false;
266 /* Whether the default text and cold text sections have been used at all. */
267 static GTY(()) bool text_section_used = false;
268 static GTY(()) bool cold_text_section_used = false;
270 /* The default cold text section. */
271 static GTY(()) section *cold_text_section;
273 /* The DIE for C++14 'auto' in a function return type. */
274 static GTY(()) dw_die_ref auto_die;
276 /* The DIE for C++14 'decltype(auto)' in a function return type. */
277 static GTY(()) dw_die_ref decltype_auto_die;
279 /* Forward declarations for functions defined in this file. */
281 static void output_call_frame_info (int);
282 static void dwarf2out_note_section_used (void);
284 /* Personality decl of current unit. Used only when assembler does not support
285 personality CFI. */
286 static GTY(()) rtx current_unit_personality;
288 /* Whether an eh_frame section is required. */
289 static GTY(()) bool do_eh_frame = false;
291 /* .debug_rnglists next index. */
292 static unsigned int rnglist_idx;
294 /* Data and reference forms for relocatable data. */
295 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
296 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
298 #ifndef DEBUG_FRAME_SECTION
299 #define DEBUG_FRAME_SECTION ".debug_frame"
300 #endif
302 #ifndef FUNC_BEGIN_LABEL
303 #define FUNC_BEGIN_LABEL "LFB"
304 #endif
306 #ifndef FUNC_END_LABEL
307 #define FUNC_END_LABEL "LFE"
308 #endif
310 #ifndef PROLOGUE_END_LABEL
311 #define PROLOGUE_END_LABEL "LPE"
312 #endif
314 #ifndef EPILOGUE_BEGIN_LABEL
315 #define EPILOGUE_BEGIN_LABEL "LEB"
316 #endif
318 #ifndef FRAME_BEGIN_LABEL
319 #define FRAME_BEGIN_LABEL "Lframe"
320 #endif
321 #define CIE_AFTER_SIZE_LABEL "LSCIE"
322 #define CIE_END_LABEL "LECIE"
323 #define FDE_LABEL "LSFDE"
324 #define FDE_AFTER_SIZE_LABEL "LASFDE"
325 #define FDE_END_LABEL "LEFDE"
326 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
327 #define LINE_NUMBER_END_LABEL "LELT"
328 #define LN_PROLOG_AS_LABEL "LASLTP"
329 #define LN_PROLOG_END_LABEL "LELTP"
330 #define DIE_LABEL_PREFIX "DW"
332 /* Match the base name of a file to the base name of a compilation unit. */
334 static int
335 matches_main_base (const char *path)
337 /* Cache the last query. */
338 static const char *last_path = NULL;
339 static int last_match = 0;
340 if (path != last_path)
342 const char *base;
343 int length = base_of_path (path, &base);
344 last_path = path;
345 last_match = (length == main_input_baselength
346 && memcmp (base, main_input_basename, length) == 0);
348 return last_match;
351 #ifdef DEBUG_DEBUG_STRUCT
353 static int
354 dump_struct_debug (tree type, enum debug_info_usage usage,
355 enum debug_struct_file criterion, int generic,
356 int matches, int result)
358 /* Find the type name. */
359 tree type_decl = TYPE_STUB_DECL (type);
360 tree t = type_decl;
361 const char *name = 0;
362 if (TREE_CODE (t) == TYPE_DECL)
363 t = DECL_NAME (t);
364 if (t)
365 name = IDENTIFIER_POINTER (t);
367 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
368 criterion,
369 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
370 matches ? "bas" : "hdr",
371 generic ? "gen" : "ord",
372 usage == DINFO_USAGE_DFN ? ";" :
373 usage == DINFO_USAGE_DIR_USE ? "." : "*",
374 result,
375 (void*) type_decl, name);
376 return result;
378 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
379 dump_struct_debug (type, usage, criterion, generic, matches, result)
381 #else
383 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
384 (result)
386 #endif
388 /* Get the number of HOST_WIDE_INTs needed to represent the precision
389 of the number. Some constants have a large uniform precision, so
390 we get the precision needed for the actual value of the number. */
392 static unsigned int
393 get_full_len (const wide_int &op)
395 int prec = wi::min_precision (op, UNSIGNED);
396 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
397 / HOST_BITS_PER_WIDE_INT);
400 static bool
401 should_emit_struct_debug (tree type, enum debug_info_usage usage)
403 enum debug_struct_file criterion;
404 tree type_decl;
405 bool generic = lang_hooks.types.generic_p (type);
407 if (generic)
408 criterion = debug_struct_generic[usage];
409 else
410 criterion = debug_struct_ordinary[usage];
412 if (criterion == DINFO_STRUCT_FILE_NONE)
413 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
414 if (criterion == DINFO_STRUCT_FILE_ANY)
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
417 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
419 if (type_decl != NULL)
421 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
422 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
424 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
425 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
428 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
431 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
432 switch to the data section instead, and write out a synthetic start label
433 for collect2 the first time around. */
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
438 if (eh_frame_section == 0)
440 int flags;
442 if (EH_TABLES_CAN_BE_READ_ONLY)
444 int fde_encoding;
445 int per_encoding;
446 int lsda_encoding;
448 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449 /*global=*/0);
450 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451 /*global=*/1);
452 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453 /*global=*/0);
454 flags = ((! flag_pic
455 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456 && (fde_encoding & 0x70) != DW_EH_PE_aligned
457 && (per_encoding & 0x70) != DW_EH_PE_absptr
458 && (per_encoding & 0x70) != DW_EH_PE_aligned
459 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461 ? 0 : SECTION_WRITE);
463 else
464 flags = SECTION_WRITE;
466 #ifdef EH_FRAME_SECTION_NAME
467 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469 eh_frame_section = ((flags == SECTION_WRITE)
470 ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
474 switch_to_section (eh_frame_section);
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477 /* We have no special eh_frame section. Emit special labels to guide
478 collect2. */
479 if (!back)
481 tree label = get_file_function_name ("F");
482 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483 targetm.asm_out.globalize_label (asm_out_file,
484 IDENTIFIER_POINTER (label));
485 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
487 #endif
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491 FOR_EH. */
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
496 if (for_eh)
497 switch_to_eh_frame_section (back);
498 else
500 if (!debug_frame_section)
501 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502 SECTION_DEBUG, NULL);
503 switch_to_section (debug_frame_section);
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
512 switch (cfi)
514 case DW_CFA_nop:
515 case DW_CFA_GNU_window_save:
516 case DW_CFA_remember_state:
517 case DW_CFA_restore_state:
518 return dw_cfi_oprnd_unused;
520 case DW_CFA_set_loc:
521 case DW_CFA_advance_loc1:
522 case DW_CFA_advance_loc2:
523 case DW_CFA_advance_loc4:
524 case DW_CFA_MIPS_advance_loc8:
525 return dw_cfi_oprnd_addr;
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended:
529 case DW_CFA_def_cfa:
530 case DW_CFA_offset_extended_sf:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_restore:
533 case DW_CFA_restore_extended:
534 case DW_CFA_undefined:
535 case DW_CFA_same_value:
536 case DW_CFA_def_cfa_register:
537 case DW_CFA_register:
538 case DW_CFA_expression:
539 case DW_CFA_val_expression:
540 return dw_cfi_oprnd_reg_num;
542 case DW_CFA_def_cfa_offset:
543 case DW_CFA_GNU_args_size:
544 case DW_CFA_def_cfa_offset_sf:
545 return dw_cfi_oprnd_offset;
547 case DW_CFA_def_cfa_expression:
548 return dw_cfi_oprnd_loc;
550 default:
551 gcc_unreachable ();
555 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
557 enum dw_cfi_oprnd_type
558 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
560 switch (cfi)
562 case DW_CFA_def_cfa:
563 case DW_CFA_def_cfa_sf:
564 case DW_CFA_offset:
565 case DW_CFA_offset_extended_sf:
566 case DW_CFA_offset_extended:
567 return dw_cfi_oprnd_offset;
569 case DW_CFA_register:
570 return dw_cfi_oprnd_reg_num;
572 case DW_CFA_expression:
573 case DW_CFA_val_expression:
574 return dw_cfi_oprnd_loc;
576 case DW_CFA_def_cfa_expression:
577 return dw_cfi_oprnd_cfa_loc;
579 default:
580 return dw_cfi_oprnd_unused;
584 /* Output one FDE. */
586 static void
587 output_fde (dw_fde_ref fde, bool for_eh, bool second,
588 char *section_start_label, int fde_encoding, char *augmentation,
589 bool any_lsda_needed, int lsda_encoding)
591 const char *begin, *end;
592 static unsigned int j;
593 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
595 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
596 /* empty */ 0);
597 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
598 for_eh + j);
599 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
601 if (!XCOFF_DEBUGGING_INFO || for_eh)
603 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
604 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
605 " indicating 64-bit DWARF extension");
606 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
607 "FDE Length");
609 ASM_OUTPUT_LABEL (asm_out_file, l1);
611 if (for_eh)
612 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
613 else
614 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
615 debug_frame_section, "FDE CIE offset");
617 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
618 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
620 if (for_eh)
622 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
623 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
624 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
625 "FDE initial location");
626 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
627 end, begin, "FDE address range");
629 else
631 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
632 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
635 if (augmentation[0])
637 if (any_lsda_needed)
639 int size = size_of_encoded_value (lsda_encoding);
641 if (lsda_encoding == DW_EH_PE_aligned)
643 int offset = ( 4 /* Length */
644 + 4 /* CIE offset */
645 + 2 * size_of_encoded_value (fde_encoding)
646 + 1 /* Augmentation size */ );
647 int pad = -offset & (PTR_SIZE - 1);
649 size += pad;
650 gcc_assert (size_of_uleb128 (size) == 1);
653 dw2_asm_output_data_uleb128 (size, "Augmentation size");
655 if (fde->uses_eh_lsda)
657 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
658 fde->funcdef_number);
659 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
660 gen_rtx_SYMBOL_REF (Pmode, l1),
661 false,
662 "Language Specific Data Area");
664 else
666 if (lsda_encoding == DW_EH_PE_aligned)
667 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
668 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
669 "Language Specific Data Area (none)");
672 else
673 dw2_asm_output_data_uleb128 (0, "Augmentation size");
676 /* Loop through the Call Frame Instructions associated with this FDE. */
677 fde->dw_fde_current_label = begin;
679 size_t from, until, i;
681 from = 0;
682 until = vec_safe_length (fde->dw_fde_cfi);
684 if (fde->dw_fde_second_begin == NULL)
686 else if (!second)
687 until = fde->dw_fde_switch_cfi_index;
688 else
689 from = fde->dw_fde_switch_cfi_index;
691 for (i = from; i < until; i++)
692 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
695 /* If we are to emit a ref/link from function bodies to their frame tables,
696 do it now. This is typically performed to make sure that tables
697 associated with functions are dragged with them and not discarded in
698 garbage collecting links. We need to do this on a per function basis to
699 cope with -ffunction-sections. */
701 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
702 /* Switch to the function section, emit the ref to the tables, and
703 switch *back* into the table section. */
704 switch_to_section (function_section (fde->decl));
705 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
706 switch_to_frame_table_section (for_eh, true);
707 #endif
709 /* Pad the FDE out to an address sized boundary. */
710 ASM_OUTPUT_ALIGN (asm_out_file,
711 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
712 ASM_OUTPUT_LABEL (asm_out_file, l2);
714 j += 2;
717 /* Return true if frame description entry FDE is needed for EH. */
719 static bool
720 fde_needed_for_eh_p (dw_fde_ref fde)
722 if (flag_asynchronous_unwind_tables)
723 return true;
725 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
726 return true;
728 if (fde->uses_eh_lsda)
729 return true;
731 /* If exceptions are enabled, we have collected nothrow info. */
732 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
733 return false;
735 return true;
738 /* Output the call frame information used to record information
739 that relates to calculating the frame pointer, and records the
740 location of saved registers. */
742 static void
743 output_call_frame_info (int for_eh)
745 unsigned int i;
746 dw_fde_ref fde;
747 dw_cfi_ref cfi;
748 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
749 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
750 bool any_lsda_needed = false;
751 char augmentation[6];
752 int augmentation_size;
753 int fde_encoding = DW_EH_PE_absptr;
754 int per_encoding = DW_EH_PE_absptr;
755 int lsda_encoding = DW_EH_PE_absptr;
756 int return_reg;
757 rtx personality = NULL;
758 int dw_cie_version;
760 /* Don't emit a CIE if there won't be any FDEs. */
761 if (!fde_vec)
762 return;
764 /* Nothing to do if the assembler's doing it all. */
765 if (dwarf2out_do_cfi_asm ())
766 return;
768 /* If we don't have any functions we'll want to unwind out of, don't emit
769 any EH unwind information. If we make FDEs linkonce, we may have to
770 emit an empty label for an FDE that wouldn't otherwise be emitted. We
771 want to avoid having an FDE kept around when the function it refers to
772 is discarded. Example where this matters: a primary function template
773 in C++ requires EH information, an explicit specialization doesn't. */
774 if (for_eh)
776 bool any_eh_needed = false;
778 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
780 if (fde->uses_eh_lsda)
781 any_eh_needed = any_lsda_needed = true;
782 else if (fde_needed_for_eh_p (fde))
783 any_eh_needed = true;
784 else if (TARGET_USES_WEAK_UNWIND_INFO)
785 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
788 if (!any_eh_needed)
789 return;
792 /* We're going to be generating comments, so turn on app. */
793 if (flag_debug_asm)
794 app_enable ();
796 /* Switch to the proper frame section, first time. */
797 switch_to_frame_table_section (for_eh, false);
799 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
800 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
802 /* Output the CIE. */
803 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
804 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
805 if (!XCOFF_DEBUGGING_INFO || for_eh)
807 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
808 dw2_asm_output_data (4, 0xffffffff,
809 "Initial length escape value indicating 64-bit DWARF extension");
810 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
811 "Length of Common Information Entry");
813 ASM_OUTPUT_LABEL (asm_out_file, l1);
815 /* Now that the CIE pointer is PC-relative for EH,
816 use 0 to identify the CIE. */
817 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
818 (for_eh ? 0 : DWARF_CIE_ID),
819 "CIE Identifier Tag");
821 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
822 use CIE version 1, unless that would produce incorrect results
823 due to overflowing the return register column. */
824 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
825 dw_cie_version = 1;
826 if (return_reg >= 256 || dwarf_version > 2)
827 dw_cie_version = 3;
828 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
830 augmentation[0] = 0;
831 augmentation_size = 0;
833 personality = current_unit_personality;
834 if (for_eh)
836 char *p;
838 /* Augmentation:
839 z Indicates that a uleb128 is present to size the
840 augmentation section.
841 L Indicates the encoding (and thus presence) of
842 an LSDA pointer in the FDE augmentation.
843 R Indicates a non-default pointer encoding for
844 FDE code pointers.
845 P Indicates the presence of an encoding + language
846 personality routine in the CIE augmentation. */
848 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
849 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
850 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
852 p = augmentation + 1;
853 if (personality)
855 *p++ = 'P';
856 augmentation_size += 1 + size_of_encoded_value (per_encoding);
857 assemble_external_libcall (personality);
859 if (any_lsda_needed)
861 *p++ = 'L';
862 augmentation_size += 1;
864 if (fde_encoding != DW_EH_PE_absptr)
866 *p++ = 'R';
867 augmentation_size += 1;
869 if (p > augmentation + 1)
871 augmentation[0] = 'z';
872 *p = '\0';
875 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
876 if (personality && per_encoding == DW_EH_PE_aligned)
878 int offset = ( 4 /* Length */
879 + 4 /* CIE Id */
880 + 1 /* CIE version */
881 + strlen (augmentation) + 1 /* Augmentation */
882 + size_of_uleb128 (1) /* Code alignment */
883 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
884 + 1 /* RA column */
885 + 1 /* Augmentation size */
886 + 1 /* Personality encoding */ );
887 int pad = -offset & (PTR_SIZE - 1);
889 augmentation_size += pad;
891 /* Augmentations should be small, so there's scarce need to
892 iterate for a solution. Die if we exceed one uleb128 byte. */
893 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
897 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
898 if (dw_cie_version >= 4)
900 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
901 dw2_asm_output_data (1, 0, "CIE Segment Size");
903 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
904 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
905 "CIE Data Alignment Factor");
907 if (dw_cie_version == 1)
908 dw2_asm_output_data (1, return_reg, "CIE RA Column");
909 else
910 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
912 if (augmentation[0])
914 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
915 if (personality)
917 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
918 eh_data_format_name (per_encoding));
919 dw2_asm_output_encoded_addr_rtx (per_encoding,
920 personality,
921 true, NULL);
924 if (any_lsda_needed)
925 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
926 eh_data_format_name (lsda_encoding));
928 if (fde_encoding != DW_EH_PE_absptr)
929 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
930 eh_data_format_name (fde_encoding));
933 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
934 output_cfi (cfi, NULL, for_eh);
936 /* Pad the CIE out to an address sized boundary. */
937 ASM_OUTPUT_ALIGN (asm_out_file,
938 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
939 ASM_OUTPUT_LABEL (asm_out_file, l2);
941 /* Loop through all of the FDE's. */
942 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
944 unsigned int k;
946 /* Don't emit EH unwind info for leaf functions that don't need it. */
947 if (for_eh && !fde_needed_for_eh_p (fde))
948 continue;
950 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
951 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
952 augmentation, any_lsda_needed, lsda_encoding);
955 if (for_eh && targetm.terminate_dw2_eh_frame_info)
956 dw2_asm_output_data (4, 0, "End of Table");
958 /* Turn off app to make assembly quicker. */
959 if (flag_debug_asm)
960 app_disable ();
963 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
965 static void
966 dwarf2out_do_cfi_startproc (bool second)
968 int enc;
969 rtx ref;
971 fprintf (asm_out_file, "\t.cfi_startproc\n");
973 /* .cfi_personality and .cfi_lsda are only relevant to DWARF2
974 eh unwinders. */
975 if (targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
976 return;
978 rtx personality = get_personality_function (current_function_decl);
980 if (personality)
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
983 ref = personality;
985 /* ??? The GAS support isn't entirely consistent. We have to
986 handle indirect support ourselves, but PC-relative is done
987 in the assembler. Further, the assembler can't handle any
988 of the weirder relocation types. */
989 if (enc & DW_EH_PE_indirect)
990 ref = dw2_force_const_mem (ref, true);
992 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
993 output_addr_const (asm_out_file, ref);
994 fputc ('\n', asm_out_file);
997 if (crtl->uses_eh_lsda)
999 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
1001 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1002 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1003 current_function_funcdef_no);
1004 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1005 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1007 if (enc & DW_EH_PE_indirect)
1008 ref = dw2_force_const_mem (ref, true);
1010 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1011 output_addr_const (asm_out_file, ref);
1012 fputc ('\n', asm_out_file);
1016 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1017 this allocation may be done before pass_final. */
1019 dw_fde_ref
1020 dwarf2out_alloc_current_fde (void)
1022 dw_fde_ref fde;
1024 fde = ggc_cleared_alloc<dw_fde_node> ();
1025 fde->decl = current_function_decl;
1026 fde->funcdef_number = current_function_funcdef_no;
1027 fde->fde_index = vec_safe_length (fde_vec);
1028 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1029 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1030 fde->nothrow = crtl->nothrow;
1031 fde->drap_reg = INVALID_REGNUM;
1032 fde->vdrap_reg = INVALID_REGNUM;
1034 /* Record the FDE associated with this function. */
1035 cfun->fde = fde;
1036 vec_safe_push (fde_vec, fde);
1038 return fde;
1041 /* Output a marker (i.e. a label) for the beginning of a function, before
1042 the prologue. */
1044 void
1045 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1046 unsigned int column ATTRIBUTE_UNUSED,
1047 const char *file ATTRIBUTE_UNUSED)
1049 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1050 char * dup_label;
1051 dw_fde_ref fde;
1052 section *fnsec;
1053 bool do_frame;
1055 current_function_func_begin_label = NULL;
1057 do_frame = dwarf2out_do_frame ();
1059 /* ??? current_function_func_begin_label is also used by except.c for
1060 call-site information. We must emit this label if it might be used. */
1061 if (!do_frame
1062 && (!flag_exceptions
1063 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1064 return;
1066 fnsec = function_section (current_function_decl);
1067 switch_to_section (fnsec);
1068 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1069 current_function_funcdef_no);
1070 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1071 current_function_funcdef_no);
1072 dup_label = xstrdup (label);
1073 current_function_func_begin_label = dup_label;
1075 /* We can elide FDE allocation if we're not emitting frame unwind info. */
1076 if (!do_frame)
1077 return;
1079 /* Unlike the debug version, the EH version of frame unwind info is a per-
1080 function setting so we need to record whether we need it for the unit. */
1081 do_eh_frame |= dwarf2out_do_eh_frame ();
1083 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1084 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1085 would include pass_dwarf2_frame. If we've not created the FDE yet,
1086 do so now. */
1087 fde = cfun->fde;
1088 if (fde == NULL)
1089 fde = dwarf2out_alloc_current_fde ();
1091 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1092 fde->dw_fde_begin = dup_label;
1093 fde->dw_fde_current_label = dup_label;
1094 fde->in_std_section = (fnsec == text_section
1095 || (cold_text_section && fnsec == cold_text_section));
1097 /* We only want to output line number information for the genuine dwarf2
1098 prologue case, not the eh frame case. */
1099 #ifdef DWARF2_DEBUGGING_INFO
1100 if (file)
1101 dwarf2out_source_line (line, column, file, 0, true);
1102 #endif
1104 if (dwarf2out_do_cfi_asm ())
1105 dwarf2out_do_cfi_startproc (false);
1106 else
1108 rtx personality = get_personality_function (current_function_decl);
1109 if (!current_unit_personality)
1110 current_unit_personality = personality;
1112 /* We cannot keep a current personality per function as without CFI
1113 asm, at the point where we emit the CFI data, there is no current
1114 function anymore. */
1115 if (personality && current_unit_personality != personality)
1116 sorry ("multiple EH personalities are supported only with assemblers "
1117 "supporting .cfi_personality directive");
1121 /* Output a marker (i.e. a label) for the end of the generated code
1122 for a function prologue. This gets called *after* the prologue code has
1123 been generated. */
1125 void
1126 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1127 const char *file ATTRIBUTE_UNUSED)
1129 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1131 /* Output a label to mark the endpoint of the code generated for this
1132 function. */
1133 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1134 current_function_funcdef_no);
1135 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1136 current_function_funcdef_no);
1137 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1140 /* Output a marker (i.e. a label) for the beginning of the generated code
1141 for a function epilogue. This gets called *before* the prologue code has
1142 been generated. */
1144 void
1145 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1146 const char *file ATTRIBUTE_UNUSED)
1148 dw_fde_ref fde = cfun->fde;
1149 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1151 if (fde->dw_fde_vms_begin_epilogue)
1152 return;
1154 /* Output a label to mark the endpoint of the code generated for this
1155 function. */
1156 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1157 current_function_funcdef_no);
1158 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1159 current_function_funcdef_no);
1160 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1163 /* Output a marker (i.e. a label) for the absolute end of the generated code
1164 for a function definition. This gets called *after* the epilogue code has
1165 been generated. */
1167 void
1168 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1169 const char *file ATTRIBUTE_UNUSED)
1171 dw_fde_ref fde;
1172 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1174 last_var_location_insn = NULL;
1175 cached_next_real_insn = NULL;
1177 if (dwarf2out_do_cfi_asm ())
1178 fprintf (asm_out_file, "\t.cfi_endproc\n");
1180 /* Output a label to mark the endpoint of the code generated for this
1181 function. */
1182 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1183 current_function_funcdef_no);
1184 ASM_OUTPUT_LABEL (asm_out_file, label);
1185 fde = cfun->fde;
1186 gcc_assert (fde != NULL);
1187 if (fde->dw_fde_second_begin == NULL)
1188 fde->dw_fde_end = xstrdup (label);
1191 void
1192 dwarf2out_frame_finish (void)
1194 /* Output call frame information. */
1195 if (targetm.debug_unwind_info () == UI_DWARF2)
1196 output_call_frame_info (0);
1198 /* Output another copy for the unwinder. */
1199 if (do_eh_frame)
1200 output_call_frame_info (1);
1203 /* Note that the current function section is being used for code. */
1205 static void
1206 dwarf2out_note_section_used (void)
1208 section *sec = current_function_section ();
1209 if (sec == text_section)
1210 text_section_used = true;
1211 else if (sec == cold_text_section)
1212 cold_text_section_used = true;
1215 static void var_location_switch_text_section (void);
1216 static void set_cur_line_info_table (section *);
1218 void
1219 dwarf2out_switch_text_section (void)
1221 section *sect;
1222 dw_fde_ref fde = cfun->fde;
1224 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1226 if (!in_cold_section_p)
1228 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1229 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1230 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1232 else
1234 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1235 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1236 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1238 have_multiple_function_sections = true;
1240 /* There is no need to mark used sections when not debugging. */
1241 if (cold_text_section != NULL)
1242 dwarf2out_note_section_used ();
1244 if (dwarf2out_do_cfi_asm ())
1245 fprintf (asm_out_file, "\t.cfi_endproc\n");
1247 /* Now do the real section switch. */
1248 sect = current_function_section ();
1249 switch_to_section (sect);
1251 fde->second_in_std_section
1252 = (sect == text_section
1253 || (cold_text_section && sect == cold_text_section));
1255 if (dwarf2out_do_cfi_asm ())
1256 dwarf2out_do_cfi_startproc (true);
1258 var_location_switch_text_section ();
1260 if (cold_text_section != NULL)
1261 set_cur_line_info_table (sect);
1264 /* And now, the subset of the debugging information support code necessary
1265 for emitting location expressions. */
1267 /* Data about a single source file. */
1268 struct GTY((for_user)) dwarf_file_data {
1269 const char * filename;
1270 int emitted_number;
1273 /* Describe an entry into the .debug_addr section. */
1275 enum ate_kind {
1276 ate_kind_rtx,
1277 ate_kind_rtx_dtprel,
1278 ate_kind_label
1281 struct GTY((for_user)) addr_table_entry {
1282 enum ate_kind kind;
1283 unsigned int refcount;
1284 unsigned int index;
1285 union addr_table_entry_struct_union
1287 rtx GTY ((tag ("0"))) rtl;
1288 char * GTY ((tag ("1"))) label;
1290 GTY ((desc ("%1.kind"))) addr;
1293 /* Location lists are ranges + location descriptions for that range,
1294 so you can track variables that are in different places over
1295 their entire life. */
1296 typedef struct GTY(()) dw_loc_list_struct {
1297 dw_loc_list_ref dw_loc_next;
1298 const char *begin; /* Label and addr_entry for start of range */
1299 addr_table_entry *begin_entry;
1300 const char *end; /* Label for end of range */
1301 char *ll_symbol; /* Label for beginning of location list.
1302 Only on head of list */
1303 const char *section; /* Section this loclist is relative to */
1304 dw_loc_descr_ref expr;
1305 hashval_t hash;
1306 /* True if all addresses in this and subsequent lists are known to be
1307 resolved. */
1308 bool resolved_addr;
1309 /* True if this list has been replaced by dw_loc_next. */
1310 bool replaced;
1311 /* True if it has been emitted into .debug_loc* / .debug_loclists*
1312 section. */
1313 unsigned char emitted : 1;
1314 /* True if hash field is index rather than hash value. */
1315 unsigned char num_assigned : 1;
1316 /* True if .debug_loclists.dwo offset has been emitted for it already. */
1317 unsigned char offset_emitted : 1;
1318 /* True if note_variable_value_in_expr has been called on it. */
1319 unsigned char noted_variable_value : 1;
1320 /* True if the range should be emitted even if begin and end
1321 are the same. */
1322 bool force;
1323 } dw_loc_list_node;
1325 static dw_loc_descr_ref int_loc_descriptor (poly_int64);
1326 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1328 /* Convert a DWARF stack opcode into its string name. */
1330 static const char *
1331 dwarf_stack_op_name (unsigned int op)
1333 const char *name = get_DW_OP_name (op);
1335 if (name != NULL)
1336 return name;
1338 return "OP_<unknown>";
1341 /* Return a pointer to a newly allocated location description. Location
1342 descriptions are simple expression terms that can be strung
1343 together to form more complicated location (address) descriptions. */
1345 static inline dw_loc_descr_ref
1346 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1347 unsigned HOST_WIDE_INT oprnd2)
1349 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1351 descr->dw_loc_opc = op;
1352 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1353 descr->dw_loc_oprnd1.val_entry = NULL;
1354 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1355 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1356 descr->dw_loc_oprnd2.val_entry = NULL;
1357 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1359 return descr;
1362 /* Add a location description term to a location description expression. */
1364 static inline void
1365 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1367 dw_loc_descr_ref *d;
1369 /* Find the end of the chain. */
1370 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1373 *d = descr;
1376 /* Compare two location operands for exact equality. */
1378 static bool
1379 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1381 if (a->val_class != b->val_class)
1382 return false;
1383 switch (a->val_class)
1385 case dw_val_class_none:
1386 return true;
1387 case dw_val_class_addr:
1388 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1390 case dw_val_class_offset:
1391 case dw_val_class_unsigned_const:
1392 case dw_val_class_const:
1393 case dw_val_class_unsigned_const_implicit:
1394 case dw_val_class_const_implicit:
1395 case dw_val_class_range_list:
1396 /* These are all HOST_WIDE_INT, signed or unsigned. */
1397 return a->v.val_unsigned == b->v.val_unsigned;
1399 case dw_val_class_loc:
1400 return a->v.val_loc == b->v.val_loc;
1401 case dw_val_class_loc_list:
1402 return a->v.val_loc_list == b->v.val_loc_list;
1403 case dw_val_class_die_ref:
1404 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1405 case dw_val_class_fde_ref:
1406 return a->v.val_fde_index == b->v.val_fde_index;
1407 case dw_val_class_lbl_id:
1408 case dw_val_class_lineptr:
1409 case dw_val_class_macptr:
1410 case dw_val_class_loclistsptr:
1411 case dw_val_class_high_pc:
1412 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1413 case dw_val_class_str:
1414 return a->v.val_str == b->v.val_str;
1415 case dw_val_class_flag:
1416 return a->v.val_flag == b->v.val_flag;
1417 case dw_val_class_file:
1418 case dw_val_class_file_implicit:
1419 return a->v.val_file == b->v.val_file;
1420 case dw_val_class_decl_ref:
1421 return a->v.val_decl_ref == b->v.val_decl_ref;
1423 case dw_val_class_const_double:
1424 return (a->v.val_double.high == b->v.val_double.high
1425 && a->v.val_double.low == b->v.val_double.low);
1427 case dw_val_class_wide_int:
1428 return *a->v.val_wide == *b->v.val_wide;
1430 case dw_val_class_vec:
1432 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1433 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1435 return (a_len == b_len
1436 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1439 case dw_val_class_data8:
1440 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1442 case dw_val_class_vms_delta:
1443 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1444 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1446 case dw_val_class_discr_value:
1447 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1448 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1449 case dw_val_class_discr_list:
1450 /* It makes no sense comparing two discriminant value lists. */
1451 return false;
1453 gcc_unreachable ();
1456 /* Compare two location atoms for exact equality. */
1458 static bool
1459 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1461 if (a->dw_loc_opc != b->dw_loc_opc)
1462 return false;
1464 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1465 address size, but since we always allocate cleared storage it
1466 should be zero for other types of locations. */
1467 if (a->dtprel != b->dtprel)
1468 return false;
1470 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1471 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1474 /* Compare two complete location expressions for exact equality. */
1476 bool
1477 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1479 while (1)
1481 if (a == b)
1482 return true;
1483 if (a == NULL || b == NULL)
1484 return false;
1485 if (!loc_descr_equal_p_1 (a, b))
1486 return false;
1488 a = a->dw_loc_next;
1489 b = b->dw_loc_next;
1494 /* Add a constant POLY_OFFSET to a location expression. */
1496 static void
1497 loc_descr_plus_const (dw_loc_descr_ref *list_head, poly_int64 poly_offset)
1499 dw_loc_descr_ref loc;
1500 HOST_WIDE_INT *p;
1502 gcc_assert (*list_head != NULL);
1504 if (known_eq (poly_offset, 0))
1505 return;
1507 /* Find the end of the chain. */
1508 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1511 HOST_WIDE_INT offset;
1512 if (!poly_offset.is_constant (&offset))
1514 loc->dw_loc_next = int_loc_descriptor (poly_offset);
1515 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
1516 return;
1519 p = NULL;
1520 if (loc->dw_loc_opc == DW_OP_fbreg
1521 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1522 p = &loc->dw_loc_oprnd1.v.val_int;
1523 else if (loc->dw_loc_opc == DW_OP_bregx)
1524 p = &loc->dw_loc_oprnd2.v.val_int;
1526 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1527 offset. Don't optimize if an signed integer overflow would happen. */
1528 if (p != NULL
1529 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1530 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1531 *p += offset;
1533 else if (offset > 0)
1534 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1536 else
1538 loc->dw_loc_next
1539 = uint_loc_descriptor (-(unsigned HOST_WIDE_INT) offset);
1540 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1544 /* Return a pointer to a newly allocated location description for
1545 REG and OFFSET. */
1547 static inline dw_loc_descr_ref
1548 new_reg_loc_descr (unsigned int reg, poly_int64 offset)
1550 HOST_WIDE_INT const_offset;
1551 if (offset.is_constant (&const_offset))
1553 if (reg <= 31)
1554 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1555 const_offset, 0);
1556 else
1557 return new_loc_descr (DW_OP_bregx, reg, const_offset);
1559 else
1561 dw_loc_descr_ref ret = new_reg_loc_descr (reg, 0);
1562 loc_descr_plus_const (&ret, offset);
1563 return ret;
1567 /* Add a constant OFFSET to a location list. */
1569 static void
1570 loc_list_plus_const (dw_loc_list_ref list_head, poly_int64 offset)
1572 dw_loc_list_ref d;
1573 for (d = list_head; d != NULL; d = d->dw_loc_next)
1574 loc_descr_plus_const (&d->expr, offset);
1577 #define DWARF_REF_SIZE \
1578 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1580 /* The number of bits that can be encoded by largest DW_FORM_dataN.
1581 In DWARF4 and earlier it is DW_FORM_data8 with 64 bits, in DWARF5
1582 DW_FORM_data16 with 128 bits. */
1583 #define DWARF_LARGEST_DATA_FORM_BITS \
1584 (dwarf_version >= 5 ? 128 : 64)
1586 /* Utility inline function for construction of ops that were GNU extension
1587 before DWARF 5. */
1588 static inline enum dwarf_location_atom
1589 dwarf_OP (enum dwarf_location_atom op)
1591 switch (op)
1593 case DW_OP_implicit_pointer:
1594 if (dwarf_version < 5)
1595 return DW_OP_GNU_implicit_pointer;
1596 break;
1598 case DW_OP_entry_value:
1599 if (dwarf_version < 5)
1600 return DW_OP_GNU_entry_value;
1601 break;
1603 case DW_OP_const_type:
1604 if (dwarf_version < 5)
1605 return DW_OP_GNU_const_type;
1606 break;
1608 case DW_OP_regval_type:
1609 if (dwarf_version < 5)
1610 return DW_OP_GNU_regval_type;
1611 break;
1613 case DW_OP_deref_type:
1614 if (dwarf_version < 5)
1615 return DW_OP_GNU_deref_type;
1616 break;
1618 case DW_OP_convert:
1619 if (dwarf_version < 5)
1620 return DW_OP_GNU_convert;
1621 break;
1623 case DW_OP_reinterpret:
1624 if (dwarf_version < 5)
1625 return DW_OP_GNU_reinterpret;
1626 break;
1628 default:
1629 break;
1631 return op;
1634 /* Similarly for attributes. */
1635 static inline enum dwarf_attribute
1636 dwarf_AT (enum dwarf_attribute at)
1638 switch (at)
1640 case DW_AT_call_return_pc:
1641 if (dwarf_version < 5)
1642 return DW_AT_low_pc;
1643 break;
1645 case DW_AT_call_tail_call:
1646 if (dwarf_version < 5)
1647 return DW_AT_GNU_tail_call;
1648 break;
1650 case DW_AT_call_origin:
1651 if (dwarf_version < 5)
1652 return DW_AT_abstract_origin;
1653 break;
1655 case DW_AT_call_target:
1656 if (dwarf_version < 5)
1657 return DW_AT_GNU_call_site_target;
1658 break;
1660 case DW_AT_call_target_clobbered:
1661 if (dwarf_version < 5)
1662 return DW_AT_GNU_call_site_target_clobbered;
1663 break;
1665 case DW_AT_call_parameter:
1666 if (dwarf_version < 5)
1667 return DW_AT_abstract_origin;
1668 break;
1670 case DW_AT_call_value:
1671 if (dwarf_version < 5)
1672 return DW_AT_GNU_call_site_value;
1673 break;
1675 case DW_AT_call_data_value:
1676 if (dwarf_version < 5)
1677 return DW_AT_GNU_call_site_data_value;
1678 break;
1680 case DW_AT_call_all_calls:
1681 if (dwarf_version < 5)
1682 return DW_AT_GNU_all_call_sites;
1683 break;
1685 case DW_AT_call_all_tail_calls:
1686 if (dwarf_version < 5)
1687 return DW_AT_GNU_all_tail_call_sites;
1688 break;
1690 case DW_AT_dwo_name:
1691 if (dwarf_version < 5)
1692 return DW_AT_GNU_dwo_name;
1693 break;
1695 default:
1696 break;
1698 return at;
1701 /* And similarly for tags. */
1702 static inline enum dwarf_tag
1703 dwarf_TAG (enum dwarf_tag tag)
1705 switch (tag)
1707 case DW_TAG_call_site:
1708 if (dwarf_version < 5)
1709 return DW_TAG_GNU_call_site;
1710 break;
1712 case DW_TAG_call_site_parameter:
1713 if (dwarf_version < 5)
1714 return DW_TAG_GNU_call_site_parameter;
1715 break;
1717 default:
1718 break;
1720 return tag;
1723 static unsigned long int get_base_type_offset (dw_die_ref);
1725 /* Return the size of a location descriptor. */
1727 static unsigned long
1728 size_of_loc_descr (dw_loc_descr_ref loc)
1730 unsigned long size = 1;
1732 switch (loc->dw_loc_opc)
1734 case DW_OP_addr:
1735 size += DWARF2_ADDR_SIZE;
1736 break;
1737 case DW_OP_GNU_addr_index:
1738 case DW_OP_GNU_const_index:
1739 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1740 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1741 break;
1742 case DW_OP_const1u:
1743 case DW_OP_const1s:
1744 size += 1;
1745 break;
1746 case DW_OP_const2u:
1747 case DW_OP_const2s:
1748 size += 2;
1749 break;
1750 case DW_OP_const4u:
1751 case DW_OP_const4s:
1752 size += 4;
1753 break;
1754 case DW_OP_const8u:
1755 case DW_OP_const8s:
1756 size += 8;
1757 break;
1758 case DW_OP_constu:
1759 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1760 break;
1761 case DW_OP_consts:
1762 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1763 break;
1764 case DW_OP_pick:
1765 size += 1;
1766 break;
1767 case DW_OP_plus_uconst:
1768 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1769 break;
1770 case DW_OP_skip:
1771 case DW_OP_bra:
1772 size += 2;
1773 break;
1774 case DW_OP_breg0:
1775 case DW_OP_breg1:
1776 case DW_OP_breg2:
1777 case DW_OP_breg3:
1778 case DW_OP_breg4:
1779 case DW_OP_breg5:
1780 case DW_OP_breg6:
1781 case DW_OP_breg7:
1782 case DW_OP_breg8:
1783 case DW_OP_breg9:
1784 case DW_OP_breg10:
1785 case DW_OP_breg11:
1786 case DW_OP_breg12:
1787 case DW_OP_breg13:
1788 case DW_OP_breg14:
1789 case DW_OP_breg15:
1790 case DW_OP_breg16:
1791 case DW_OP_breg17:
1792 case DW_OP_breg18:
1793 case DW_OP_breg19:
1794 case DW_OP_breg20:
1795 case DW_OP_breg21:
1796 case DW_OP_breg22:
1797 case DW_OP_breg23:
1798 case DW_OP_breg24:
1799 case DW_OP_breg25:
1800 case DW_OP_breg26:
1801 case DW_OP_breg27:
1802 case DW_OP_breg28:
1803 case DW_OP_breg29:
1804 case DW_OP_breg30:
1805 case DW_OP_breg31:
1806 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1807 break;
1808 case DW_OP_regx:
1809 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1810 break;
1811 case DW_OP_fbreg:
1812 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1813 break;
1814 case DW_OP_bregx:
1815 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1816 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1817 break;
1818 case DW_OP_piece:
1819 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1820 break;
1821 case DW_OP_bit_piece:
1822 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1823 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1824 break;
1825 case DW_OP_deref_size:
1826 case DW_OP_xderef_size:
1827 size += 1;
1828 break;
1829 case DW_OP_call2:
1830 size += 2;
1831 break;
1832 case DW_OP_call4:
1833 size += 4;
1834 break;
1835 case DW_OP_call_ref:
1836 case DW_OP_GNU_variable_value:
1837 size += DWARF_REF_SIZE;
1838 break;
1839 case DW_OP_implicit_value:
1840 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1841 + loc->dw_loc_oprnd1.v.val_unsigned;
1842 break;
1843 case DW_OP_implicit_pointer:
1844 case DW_OP_GNU_implicit_pointer:
1845 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1846 break;
1847 case DW_OP_entry_value:
1848 case DW_OP_GNU_entry_value:
1850 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1851 size += size_of_uleb128 (op_size) + op_size;
1852 break;
1854 case DW_OP_const_type:
1855 case DW_OP_GNU_const_type:
1857 unsigned long o
1858 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1859 size += size_of_uleb128 (o) + 1;
1860 switch (loc->dw_loc_oprnd2.val_class)
1862 case dw_val_class_vec:
1863 size += loc->dw_loc_oprnd2.v.val_vec.length
1864 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1865 break;
1866 case dw_val_class_const:
1867 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1868 break;
1869 case dw_val_class_const_double:
1870 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1871 break;
1872 case dw_val_class_wide_int:
1873 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1874 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1875 break;
1876 default:
1877 gcc_unreachable ();
1879 break;
1881 case DW_OP_regval_type:
1882 case DW_OP_GNU_regval_type:
1884 unsigned long o
1885 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1886 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1887 + size_of_uleb128 (o);
1889 break;
1890 case DW_OP_deref_type:
1891 case DW_OP_GNU_deref_type:
1893 unsigned long o
1894 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1895 size += 1 + size_of_uleb128 (o);
1897 break;
1898 case DW_OP_convert:
1899 case DW_OP_reinterpret:
1900 case DW_OP_GNU_convert:
1901 case DW_OP_GNU_reinterpret:
1902 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1903 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1904 else
1906 unsigned long o
1907 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1908 size += size_of_uleb128 (o);
1910 break;
1911 case DW_OP_GNU_parameter_ref:
1912 size += 4;
1913 break;
1914 default:
1915 break;
1918 return size;
1921 /* Return the size of a series of location descriptors. */
1923 unsigned long
1924 size_of_locs (dw_loc_descr_ref loc)
1926 dw_loc_descr_ref l;
1927 unsigned long size;
1929 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1930 field, to avoid writing to a PCH file. */
1931 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1933 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1934 break;
1935 size += size_of_loc_descr (l);
1937 if (! l)
1938 return size;
1940 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1942 l->dw_loc_addr = size;
1943 size += size_of_loc_descr (l);
1946 return size;
1949 /* Return the size of the value in a DW_AT_discr_value attribute. */
1951 static int
1952 size_of_discr_value (dw_discr_value *discr_value)
1954 if (discr_value->pos)
1955 return size_of_uleb128 (discr_value->v.uval);
1956 else
1957 return size_of_sleb128 (discr_value->v.sval);
1960 /* Return the size of the value in a DW_AT_discr_list attribute. */
1962 static int
1963 size_of_discr_list (dw_discr_list_ref discr_list)
1965 int size = 0;
1967 for (dw_discr_list_ref list = discr_list;
1968 list != NULL;
1969 list = list->dw_discr_next)
1971 /* One byte for the discriminant value descriptor, and then one or two
1972 LEB128 numbers, depending on whether it's a single case label or a
1973 range label. */
1974 size += 1;
1975 size += size_of_discr_value (&list->dw_discr_lower_bound);
1976 if (list->dw_discr_range != 0)
1977 size += size_of_discr_value (&list->dw_discr_upper_bound);
1979 return size;
1982 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1983 static void get_ref_die_offset_label (char *, dw_die_ref);
1984 static unsigned long int get_ref_die_offset (dw_die_ref);
1986 /* Output location description stack opcode's operands (if any).
1987 The for_eh_or_skip parameter controls whether register numbers are
1988 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1989 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1990 info). This should be suppressed for the cases that have not been converted
1991 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1993 static void
1994 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1996 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1997 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1999 switch (loc->dw_loc_opc)
2001 #ifdef DWARF2_DEBUGGING_INFO
2002 case DW_OP_const2u:
2003 case DW_OP_const2s:
2004 dw2_asm_output_data (2, val1->v.val_int, NULL);
2005 break;
2006 case DW_OP_const4u:
2007 if (loc->dtprel)
2009 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2010 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2011 val1->v.val_addr);
2012 fputc ('\n', asm_out_file);
2013 break;
2015 /* FALLTHRU */
2016 case DW_OP_const4s:
2017 dw2_asm_output_data (4, val1->v.val_int, NULL);
2018 break;
2019 case DW_OP_const8u:
2020 if (loc->dtprel)
2022 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2023 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2024 val1->v.val_addr);
2025 fputc ('\n', asm_out_file);
2026 break;
2028 /* FALLTHRU */
2029 case DW_OP_const8s:
2030 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2031 dw2_asm_output_data (8, val1->v.val_int, NULL);
2032 break;
2033 case DW_OP_skip:
2034 case DW_OP_bra:
2036 int offset;
2038 gcc_assert (val1->val_class == dw_val_class_loc);
2039 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2041 dw2_asm_output_data (2, offset, NULL);
2043 break;
2044 case DW_OP_implicit_value:
2045 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2046 switch (val2->val_class)
2048 case dw_val_class_const:
2049 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2050 break;
2051 case dw_val_class_vec:
2053 unsigned int elt_size = val2->v.val_vec.elt_size;
2054 unsigned int len = val2->v.val_vec.length;
2055 unsigned int i;
2056 unsigned char *p;
2058 if (elt_size > sizeof (HOST_WIDE_INT))
2060 elt_size /= 2;
2061 len *= 2;
2063 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2064 i < len;
2065 i++, p += elt_size)
2066 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2067 "fp or vector constant word %u", i);
2069 break;
2070 case dw_val_class_const_double:
2072 unsigned HOST_WIDE_INT first, second;
2074 if (WORDS_BIG_ENDIAN)
2076 first = val2->v.val_double.high;
2077 second = val2->v.val_double.low;
2079 else
2081 first = val2->v.val_double.low;
2082 second = val2->v.val_double.high;
2084 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2085 first, NULL);
2086 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2087 second, NULL);
2089 break;
2090 case dw_val_class_wide_int:
2092 int i;
2093 int len = get_full_len (*val2->v.val_wide);
2094 if (WORDS_BIG_ENDIAN)
2095 for (i = len - 1; i >= 0; --i)
2096 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2097 val2->v.val_wide->elt (i), NULL);
2098 else
2099 for (i = 0; i < len; ++i)
2100 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2101 val2->v.val_wide->elt (i), NULL);
2103 break;
2104 case dw_val_class_addr:
2105 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2106 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2107 break;
2108 default:
2109 gcc_unreachable ();
2111 break;
2112 #else
2113 case DW_OP_const2u:
2114 case DW_OP_const2s:
2115 case DW_OP_const4u:
2116 case DW_OP_const4s:
2117 case DW_OP_const8u:
2118 case DW_OP_const8s:
2119 case DW_OP_skip:
2120 case DW_OP_bra:
2121 case DW_OP_implicit_value:
2122 /* We currently don't make any attempt to make sure these are
2123 aligned properly like we do for the main unwind info, so
2124 don't support emitting things larger than a byte if we're
2125 only doing unwinding. */
2126 gcc_unreachable ();
2127 #endif
2128 case DW_OP_const1u:
2129 case DW_OP_const1s:
2130 dw2_asm_output_data (1, val1->v.val_int, NULL);
2131 break;
2132 case DW_OP_constu:
2133 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2134 break;
2135 case DW_OP_consts:
2136 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2137 break;
2138 case DW_OP_pick:
2139 dw2_asm_output_data (1, val1->v.val_int, NULL);
2140 break;
2141 case DW_OP_plus_uconst:
2142 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2143 break;
2144 case DW_OP_breg0:
2145 case DW_OP_breg1:
2146 case DW_OP_breg2:
2147 case DW_OP_breg3:
2148 case DW_OP_breg4:
2149 case DW_OP_breg5:
2150 case DW_OP_breg6:
2151 case DW_OP_breg7:
2152 case DW_OP_breg8:
2153 case DW_OP_breg9:
2154 case DW_OP_breg10:
2155 case DW_OP_breg11:
2156 case DW_OP_breg12:
2157 case DW_OP_breg13:
2158 case DW_OP_breg14:
2159 case DW_OP_breg15:
2160 case DW_OP_breg16:
2161 case DW_OP_breg17:
2162 case DW_OP_breg18:
2163 case DW_OP_breg19:
2164 case DW_OP_breg20:
2165 case DW_OP_breg21:
2166 case DW_OP_breg22:
2167 case DW_OP_breg23:
2168 case DW_OP_breg24:
2169 case DW_OP_breg25:
2170 case DW_OP_breg26:
2171 case DW_OP_breg27:
2172 case DW_OP_breg28:
2173 case DW_OP_breg29:
2174 case DW_OP_breg30:
2175 case DW_OP_breg31:
2176 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2177 break;
2178 case DW_OP_regx:
2180 unsigned r = val1->v.val_unsigned;
2181 if (for_eh_or_skip >= 0)
2182 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2183 gcc_assert (size_of_uleb128 (r)
2184 == size_of_uleb128 (val1->v.val_unsigned));
2185 dw2_asm_output_data_uleb128 (r, NULL);
2187 break;
2188 case DW_OP_fbreg:
2189 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2190 break;
2191 case DW_OP_bregx:
2193 unsigned r = val1->v.val_unsigned;
2194 if (for_eh_or_skip >= 0)
2195 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2196 gcc_assert (size_of_uleb128 (r)
2197 == size_of_uleb128 (val1->v.val_unsigned));
2198 dw2_asm_output_data_uleb128 (r, NULL);
2199 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2201 break;
2202 case DW_OP_piece:
2203 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2204 break;
2205 case DW_OP_bit_piece:
2206 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2207 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2208 break;
2209 case DW_OP_deref_size:
2210 case DW_OP_xderef_size:
2211 dw2_asm_output_data (1, val1->v.val_int, NULL);
2212 break;
2214 case DW_OP_addr:
2215 if (loc->dtprel)
2217 if (targetm.asm_out.output_dwarf_dtprel)
2219 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2220 DWARF2_ADDR_SIZE,
2221 val1->v.val_addr);
2222 fputc ('\n', asm_out_file);
2224 else
2225 gcc_unreachable ();
2227 else
2229 #ifdef DWARF2_DEBUGGING_INFO
2230 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2231 #else
2232 gcc_unreachable ();
2233 #endif
2235 break;
2237 case DW_OP_GNU_addr_index:
2238 case DW_OP_GNU_const_index:
2239 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2240 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2241 "(index into .debug_addr)");
2242 break;
2244 case DW_OP_call2:
2245 case DW_OP_call4:
2247 unsigned long die_offset
2248 = get_ref_die_offset (val1->v.val_die_ref.die);
2249 /* Make sure the offset has been computed and that we can encode it as
2250 an operand. */
2251 gcc_assert (die_offset > 0
2252 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2253 ? 0xffff
2254 : 0xffffffff));
2255 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2256 die_offset, NULL);
2258 break;
2260 case DW_OP_call_ref:
2261 case DW_OP_GNU_variable_value:
2263 char label[MAX_ARTIFICIAL_LABEL_BYTES
2264 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2265 gcc_assert (val1->val_class == dw_val_class_die_ref);
2266 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2267 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2269 break;
2271 case DW_OP_implicit_pointer:
2272 case DW_OP_GNU_implicit_pointer:
2274 char label[MAX_ARTIFICIAL_LABEL_BYTES
2275 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2276 gcc_assert (val1->val_class == dw_val_class_die_ref);
2277 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2278 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2279 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2281 break;
2283 case DW_OP_entry_value:
2284 case DW_OP_GNU_entry_value:
2285 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2286 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2287 break;
2289 case DW_OP_const_type:
2290 case DW_OP_GNU_const_type:
2292 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2293 gcc_assert (o);
2294 dw2_asm_output_data_uleb128 (o, NULL);
2295 switch (val2->val_class)
2297 case dw_val_class_const:
2298 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2299 dw2_asm_output_data (1, l, NULL);
2300 dw2_asm_output_data (l, val2->v.val_int, NULL);
2301 break;
2302 case dw_val_class_vec:
2304 unsigned int elt_size = val2->v.val_vec.elt_size;
2305 unsigned int len = val2->v.val_vec.length;
2306 unsigned int i;
2307 unsigned char *p;
2309 l = len * elt_size;
2310 dw2_asm_output_data (1, l, NULL);
2311 if (elt_size > sizeof (HOST_WIDE_INT))
2313 elt_size /= 2;
2314 len *= 2;
2316 for (i = 0, p = (unsigned char *) val2->v.val_vec.array;
2317 i < len;
2318 i++, p += elt_size)
2319 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2320 "fp or vector constant word %u", i);
2322 break;
2323 case dw_val_class_const_double:
2325 unsigned HOST_WIDE_INT first, second;
2326 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2328 dw2_asm_output_data (1, 2 * l, NULL);
2329 if (WORDS_BIG_ENDIAN)
2331 first = val2->v.val_double.high;
2332 second = val2->v.val_double.low;
2334 else
2336 first = val2->v.val_double.low;
2337 second = val2->v.val_double.high;
2339 dw2_asm_output_data (l, first, NULL);
2340 dw2_asm_output_data (l, second, NULL);
2342 break;
2343 case dw_val_class_wide_int:
2345 int i;
2346 int len = get_full_len (*val2->v.val_wide);
2347 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2349 dw2_asm_output_data (1, len * l, NULL);
2350 if (WORDS_BIG_ENDIAN)
2351 for (i = len - 1; i >= 0; --i)
2352 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2353 else
2354 for (i = 0; i < len; ++i)
2355 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2357 break;
2358 default:
2359 gcc_unreachable ();
2362 break;
2363 case DW_OP_regval_type:
2364 case DW_OP_GNU_regval_type:
2366 unsigned r = val1->v.val_unsigned;
2367 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2368 gcc_assert (o);
2369 if (for_eh_or_skip >= 0)
2371 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2372 gcc_assert (size_of_uleb128 (r)
2373 == size_of_uleb128 (val1->v.val_unsigned));
2375 dw2_asm_output_data_uleb128 (r, NULL);
2376 dw2_asm_output_data_uleb128 (o, NULL);
2378 break;
2379 case DW_OP_deref_type:
2380 case DW_OP_GNU_deref_type:
2382 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2383 gcc_assert (o);
2384 dw2_asm_output_data (1, val1->v.val_int, NULL);
2385 dw2_asm_output_data_uleb128 (o, NULL);
2387 break;
2388 case DW_OP_convert:
2389 case DW_OP_reinterpret:
2390 case DW_OP_GNU_convert:
2391 case DW_OP_GNU_reinterpret:
2392 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2393 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2394 else
2396 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2397 gcc_assert (o);
2398 dw2_asm_output_data_uleb128 (o, NULL);
2400 break;
2402 case DW_OP_GNU_parameter_ref:
2404 unsigned long o;
2405 gcc_assert (val1->val_class == dw_val_class_die_ref);
2406 o = get_ref_die_offset (val1->v.val_die_ref.die);
2407 dw2_asm_output_data (4, o, NULL);
2409 break;
2411 default:
2412 /* Other codes have no operands. */
2413 break;
2417 /* Output a sequence of location operations.
2418 The for_eh_or_skip parameter controls whether register numbers are
2419 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2420 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2421 info). This should be suppressed for the cases that have not been converted
2422 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2424 void
2425 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2427 for (; loc != NULL; loc = loc->dw_loc_next)
2429 enum dwarf_location_atom opc = loc->dw_loc_opc;
2430 /* Output the opcode. */
2431 if (for_eh_or_skip >= 0
2432 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2434 unsigned r = (opc - DW_OP_breg0);
2435 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2436 gcc_assert (r <= 31);
2437 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2439 else if (for_eh_or_skip >= 0
2440 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2442 unsigned r = (opc - DW_OP_reg0);
2443 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2444 gcc_assert (r <= 31);
2445 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2448 dw2_asm_output_data (1, opc,
2449 "%s", dwarf_stack_op_name (opc));
2451 /* Output the operand(s) (if any). */
2452 output_loc_operands (loc, for_eh_or_skip);
2456 /* Output location description stack opcode's operands (if any).
2457 The output is single bytes on a line, suitable for .cfi_escape. */
2459 static void
2460 output_loc_operands_raw (dw_loc_descr_ref loc)
2462 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2463 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2465 switch (loc->dw_loc_opc)
2467 case DW_OP_addr:
2468 case DW_OP_GNU_addr_index:
2469 case DW_OP_GNU_const_index:
2470 case DW_OP_implicit_value:
2471 /* We cannot output addresses in .cfi_escape, only bytes. */
2472 gcc_unreachable ();
2474 case DW_OP_const1u:
2475 case DW_OP_const1s:
2476 case DW_OP_pick:
2477 case DW_OP_deref_size:
2478 case DW_OP_xderef_size:
2479 fputc (',', asm_out_file);
2480 dw2_asm_output_data_raw (1, val1->v.val_int);
2481 break;
2483 case DW_OP_const2u:
2484 case DW_OP_const2s:
2485 fputc (',', asm_out_file);
2486 dw2_asm_output_data_raw (2, val1->v.val_int);
2487 break;
2489 case DW_OP_const4u:
2490 case DW_OP_const4s:
2491 fputc (',', asm_out_file);
2492 dw2_asm_output_data_raw (4, val1->v.val_int);
2493 break;
2495 case DW_OP_const8u:
2496 case DW_OP_const8s:
2497 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2498 fputc (',', asm_out_file);
2499 dw2_asm_output_data_raw (8, val1->v.val_int);
2500 break;
2502 case DW_OP_skip:
2503 case DW_OP_bra:
2505 int offset;
2507 gcc_assert (val1->val_class == dw_val_class_loc);
2508 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2510 fputc (',', asm_out_file);
2511 dw2_asm_output_data_raw (2, offset);
2513 break;
2515 case DW_OP_regx:
2517 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2518 gcc_assert (size_of_uleb128 (r)
2519 == size_of_uleb128 (val1->v.val_unsigned));
2520 fputc (',', asm_out_file);
2521 dw2_asm_output_data_uleb128_raw (r);
2523 break;
2525 case DW_OP_constu:
2526 case DW_OP_plus_uconst:
2527 case DW_OP_piece:
2528 fputc (',', asm_out_file);
2529 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2530 break;
2532 case DW_OP_bit_piece:
2533 fputc (',', asm_out_file);
2534 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2535 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2536 break;
2538 case DW_OP_consts:
2539 case DW_OP_breg0:
2540 case DW_OP_breg1:
2541 case DW_OP_breg2:
2542 case DW_OP_breg3:
2543 case DW_OP_breg4:
2544 case DW_OP_breg5:
2545 case DW_OP_breg6:
2546 case DW_OP_breg7:
2547 case DW_OP_breg8:
2548 case DW_OP_breg9:
2549 case DW_OP_breg10:
2550 case DW_OP_breg11:
2551 case DW_OP_breg12:
2552 case DW_OP_breg13:
2553 case DW_OP_breg14:
2554 case DW_OP_breg15:
2555 case DW_OP_breg16:
2556 case DW_OP_breg17:
2557 case DW_OP_breg18:
2558 case DW_OP_breg19:
2559 case DW_OP_breg20:
2560 case DW_OP_breg21:
2561 case DW_OP_breg22:
2562 case DW_OP_breg23:
2563 case DW_OP_breg24:
2564 case DW_OP_breg25:
2565 case DW_OP_breg26:
2566 case DW_OP_breg27:
2567 case DW_OP_breg28:
2568 case DW_OP_breg29:
2569 case DW_OP_breg30:
2570 case DW_OP_breg31:
2571 case DW_OP_fbreg:
2572 fputc (',', asm_out_file);
2573 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2574 break;
2576 case DW_OP_bregx:
2578 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2579 gcc_assert (size_of_uleb128 (r)
2580 == size_of_uleb128 (val1->v.val_unsigned));
2581 fputc (',', asm_out_file);
2582 dw2_asm_output_data_uleb128_raw (r);
2583 fputc (',', asm_out_file);
2584 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2586 break;
2588 case DW_OP_implicit_pointer:
2589 case DW_OP_entry_value:
2590 case DW_OP_const_type:
2591 case DW_OP_regval_type:
2592 case DW_OP_deref_type:
2593 case DW_OP_convert:
2594 case DW_OP_reinterpret:
2595 case DW_OP_GNU_implicit_pointer:
2596 case DW_OP_GNU_entry_value:
2597 case DW_OP_GNU_const_type:
2598 case DW_OP_GNU_regval_type:
2599 case DW_OP_GNU_deref_type:
2600 case DW_OP_GNU_convert:
2601 case DW_OP_GNU_reinterpret:
2602 case DW_OP_GNU_parameter_ref:
2603 gcc_unreachable ();
2604 break;
2606 default:
2607 /* Other codes have no operands. */
2608 break;
2612 void
2613 output_loc_sequence_raw (dw_loc_descr_ref loc)
2615 while (1)
2617 enum dwarf_location_atom opc = loc->dw_loc_opc;
2618 /* Output the opcode. */
2619 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2621 unsigned r = (opc - DW_OP_breg0);
2622 r = DWARF2_FRAME_REG_OUT (r, 1);
2623 gcc_assert (r <= 31);
2624 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2626 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2628 unsigned r = (opc - DW_OP_reg0);
2629 r = DWARF2_FRAME_REG_OUT (r, 1);
2630 gcc_assert (r <= 31);
2631 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2633 /* Output the opcode. */
2634 fprintf (asm_out_file, "%#x", opc);
2635 output_loc_operands_raw (loc);
2637 if (!loc->dw_loc_next)
2638 break;
2639 loc = loc->dw_loc_next;
2641 fputc (',', asm_out_file);
2645 /* This function builds a dwarf location descriptor sequence from a
2646 dw_cfa_location, adding the given OFFSET to the result of the
2647 expression. */
2649 struct dw_loc_descr_node *
2650 build_cfa_loc (dw_cfa_location *cfa, poly_int64 offset)
2652 struct dw_loc_descr_node *head, *tmp;
2654 offset += cfa->offset;
2656 if (cfa->indirect)
2658 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2659 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2660 head->dw_loc_oprnd1.val_entry = NULL;
2661 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2662 add_loc_descr (&head, tmp);
2663 loc_descr_plus_const (&head, offset);
2665 else
2666 head = new_reg_loc_descr (cfa->reg, offset);
2668 return head;
2671 /* This function builds a dwarf location descriptor sequence for
2672 the address at OFFSET from the CFA when stack is aligned to
2673 ALIGNMENT byte. */
2675 struct dw_loc_descr_node *
2676 build_cfa_aligned_loc (dw_cfa_location *cfa,
2677 poly_int64 offset, HOST_WIDE_INT alignment)
2679 struct dw_loc_descr_node *head;
2680 unsigned int dwarf_fp
2681 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2683 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2684 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2686 head = new_reg_loc_descr (dwarf_fp, 0);
2687 add_loc_descr (&head, int_loc_descriptor (alignment));
2688 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2689 loc_descr_plus_const (&head, offset);
2691 else
2692 head = new_reg_loc_descr (dwarf_fp, offset);
2693 return head;
2696 /* And now, the support for symbolic debugging information. */
2698 /* .debug_str support. */
2700 static void dwarf2out_init (const char *);
2701 static void dwarf2out_finish (const char *);
2702 static void dwarf2out_early_finish (const char *);
2703 static void dwarf2out_assembly_start (void);
2704 static void dwarf2out_define (unsigned int, const char *);
2705 static void dwarf2out_undef (unsigned int, const char *);
2706 static void dwarf2out_start_source_file (unsigned, const char *);
2707 static void dwarf2out_end_source_file (unsigned);
2708 static void dwarf2out_function_decl (tree);
2709 static void dwarf2out_begin_block (unsigned, unsigned);
2710 static void dwarf2out_end_block (unsigned, unsigned);
2711 static bool dwarf2out_ignore_block (const_tree);
2712 static void dwarf2out_early_global_decl (tree);
2713 static void dwarf2out_late_global_decl (tree);
2714 static void dwarf2out_type_decl (tree, int);
2715 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool, bool);
2716 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2717 dw_die_ref);
2718 static void dwarf2out_abstract_function (tree);
2719 static void dwarf2out_var_location (rtx_insn *);
2720 static void dwarf2out_size_function (tree);
2721 static void dwarf2out_begin_function (tree);
2722 static void dwarf2out_end_function (unsigned int);
2723 static void dwarf2out_register_main_translation_unit (tree unit);
2724 static void dwarf2out_set_name (tree, tree);
2725 static void dwarf2out_register_external_die (tree decl, const char *sym,
2726 unsigned HOST_WIDE_INT off);
2727 static bool dwarf2out_die_ref_for_decl (tree decl, const char **sym,
2728 unsigned HOST_WIDE_INT *off);
2730 /* The debug hooks structure. */
2732 const struct gcc_debug_hooks dwarf2_debug_hooks =
2734 dwarf2out_init,
2735 dwarf2out_finish,
2736 dwarf2out_early_finish,
2737 dwarf2out_assembly_start,
2738 dwarf2out_define,
2739 dwarf2out_undef,
2740 dwarf2out_start_source_file,
2741 dwarf2out_end_source_file,
2742 dwarf2out_begin_block,
2743 dwarf2out_end_block,
2744 dwarf2out_ignore_block,
2745 dwarf2out_source_line,
2746 dwarf2out_begin_prologue,
2747 #if VMS_DEBUGGING_INFO
2748 dwarf2out_vms_end_prologue,
2749 dwarf2out_vms_begin_epilogue,
2750 #else
2751 debug_nothing_int_charstar,
2752 debug_nothing_int_charstar,
2753 #endif
2754 dwarf2out_end_epilogue,
2755 dwarf2out_begin_function,
2756 dwarf2out_end_function, /* end_function */
2757 dwarf2out_register_main_translation_unit,
2758 dwarf2out_function_decl, /* function_decl */
2759 dwarf2out_early_global_decl,
2760 dwarf2out_late_global_decl,
2761 dwarf2out_type_decl, /* type_decl */
2762 dwarf2out_imported_module_or_decl,
2763 dwarf2out_die_ref_for_decl,
2764 dwarf2out_register_external_die,
2765 debug_nothing_tree, /* deferred_inline_function */
2766 /* The DWARF 2 backend tries to reduce debugging bloat by not
2767 emitting the abstract description of inline functions until
2768 something tries to reference them. */
2769 dwarf2out_abstract_function, /* outlining_inline_function */
2770 debug_nothing_rtx_code_label, /* label */
2771 debug_nothing_int, /* handle_pch */
2772 dwarf2out_var_location,
2773 debug_nothing_tree, /* inline_entry */
2774 dwarf2out_size_function, /* size_function */
2775 dwarf2out_switch_text_section,
2776 dwarf2out_set_name,
2777 1, /* start_end_main_source_file */
2778 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2781 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2783 dwarf2out_init,
2784 debug_nothing_charstar,
2785 debug_nothing_charstar,
2786 dwarf2out_assembly_start,
2787 debug_nothing_int_charstar,
2788 debug_nothing_int_charstar,
2789 debug_nothing_int_charstar,
2790 debug_nothing_int,
2791 debug_nothing_int_int, /* begin_block */
2792 debug_nothing_int_int, /* end_block */
2793 debug_true_const_tree, /* ignore_block */
2794 dwarf2out_source_line, /* source_line */
2795 debug_nothing_int_int_charstar, /* begin_prologue */
2796 debug_nothing_int_charstar, /* end_prologue */
2797 debug_nothing_int_charstar, /* begin_epilogue */
2798 debug_nothing_int_charstar, /* end_epilogue */
2799 debug_nothing_tree, /* begin_function */
2800 debug_nothing_int, /* end_function */
2801 debug_nothing_tree, /* register_main_translation_unit */
2802 debug_nothing_tree, /* function_decl */
2803 debug_nothing_tree, /* early_global_decl */
2804 debug_nothing_tree, /* late_global_decl */
2805 debug_nothing_tree_int, /* type_decl */
2806 debug_nothing_tree_tree_tree_bool_bool,/* imported_module_or_decl */
2807 debug_false_tree_charstarstar_uhwistar,/* die_ref_for_decl */
2808 debug_nothing_tree_charstar_uhwi, /* register_external_die */
2809 debug_nothing_tree, /* deferred_inline_function */
2810 debug_nothing_tree, /* outlining_inline_function */
2811 debug_nothing_rtx_code_label, /* label */
2812 debug_nothing_int, /* handle_pch */
2813 debug_nothing_rtx_insn, /* var_location */
2814 debug_nothing_tree, /* inline_entry */
2815 debug_nothing_tree, /* size_function */
2816 debug_nothing_void, /* switch_text_section */
2817 debug_nothing_tree_tree, /* set_name */
2818 0, /* start_end_main_source_file */
2819 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2822 /* NOTE: In the comments in this file, many references are made to
2823 "Debugging Information Entries". This term is abbreviated as `DIE'
2824 throughout the remainder of this file. */
2826 /* An internal representation of the DWARF output is built, and then
2827 walked to generate the DWARF debugging info. The walk of the internal
2828 representation is done after the entire program has been compiled.
2829 The types below are used to describe the internal representation. */
2831 /* Whether to put type DIEs into their own section .debug_types instead
2832 of making them part of the .debug_info section. Only supported for
2833 Dwarf V4 or higher and the user didn't disable them through
2834 -fno-debug-types-section. It is more efficient to put them in a
2835 separate comdat sections since the linker will then be able to
2836 remove duplicates. But not all tools support .debug_types sections
2837 yet. For Dwarf V5 or higher .debug_types doesn't exist any more,
2838 it is DW_UT_type unit type in .debug_info section. */
2840 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2842 /* Various DIE's use offsets relative to the beginning of the
2843 .debug_info section to refer to each other. */
2845 typedef long int dw_offset;
2847 struct comdat_type_node;
2849 /* The entries in the line_info table more-or-less mirror the opcodes
2850 that are used in the real dwarf line table. Arrays of these entries
2851 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2852 supported. */
2854 enum dw_line_info_opcode {
2855 /* Emit DW_LNE_set_address; the operand is the label index. */
2856 LI_set_address,
2858 /* Emit a row to the matrix with the given line. This may be done
2859 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2860 special opcodes. */
2861 LI_set_line,
2863 /* Emit a DW_LNS_set_file. */
2864 LI_set_file,
2866 /* Emit a DW_LNS_set_column. */
2867 LI_set_column,
2869 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2870 LI_negate_stmt,
2872 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2873 LI_set_prologue_end,
2874 LI_set_epilogue_begin,
2876 /* Emit a DW_LNE_set_discriminator. */
2877 LI_set_discriminator
2880 typedef struct GTY(()) dw_line_info_struct {
2881 enum dw_line_info_opcode opcode;
2882 unsigned int val;
2883 } dw_line_info_entry;
2886 struct GTY(()) dw_line_info_table {
2887 /* The label that marks the end of this section. */
2888 const char *end_label;
2890 /* The values for the last row of the matrix, as collected in the table.
2891 These are used to minimize the changes to the next row. */
2892 unsigned int file_num;
2893 unsigned int line_num;
2894 unsigned int column_num;
2895 int discrim_num;
2896 bool is_stmt;
2897 bool in_use;
2899 vec<dw_line_info_entry, va_gc> *entries;
2903 /* Each DIE attribute has a field specifying the attribute kind,
2904 a link to the next attribute in the chain, and an attribute value.
2905 Attributes are typically linked below the DIE they modify. */
2907 typedef struct GTY(()) dw_attr_struct {
2908 enum dwarf_attribute dw_attr;
2909 dw_val_node dw_attr_val;
2911 dw_attr_node;
2914 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2915 The children of each node form a circular list linked by
2916 die_sib. die_child points to the node *before* the "first" child node. */
2918 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2919 union die_symbol_or_type_node
2921 const char * GTY ((tag ("0"))) die_symbol;
2922 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2924 GTY ((desc ("%0.comdat_type_p"))) die_id;
2925 vec<dw_attr_node, va_gc> *die_attr;
2926 dw_die_ref die_parent;
2927 dw_die_ref die_child;
2928 dw_die_ref die_sib;
2929 dw_die_ref die_definition; /* ref from a specification to its definition */
2930 dw_offset die_offset;
2931 unsigned long die_abbrev;
2932 int die_mark;
2933 unsigned int decl_id;
2934 enum dwarf_tag die_tag;
2935 /* Die is used and must not be pruned as unused. */
2936 BOOL_BITFIELD die_perennial_p : 1;
2937 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2938 /* For an external ref to die_symbol if die_offset contains an extra
2939 offset to that symbol. */
2940 BOOL_BITFIELD with_offset : 1;
2941 /* Whether this DIE was removed from the DIE tree, for example via
2942 prune_unused_types. We don't consider those present from the
2943 DIE lookup routines. */
2944 BOOL_BITFIELD removed : 1;
2945 /* Lots of spare bits. */
2947 die_node;
2949 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2950 static bool early_dwarf;
2951 static bool early_dwarf_finished;
2952 struct set_early_dwarf {
2953 bool saved;
2954 set_early_dwarf () : saved(early_dwarf)
2956 gcc_assert (! early_dwarf_finished);
2957 early_dwarf = true;
2959 ~set_early_dwarf () { early_dwarf = saved; }
2962 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2963 #define FOR_EACH_CHILD(die, c, expr) do { \
2964 c = die->die_child; \
2965 if (c) do { \
2966 c = c->die_sib; \
2967 expr; \
2968 } while (c != die->die_child); \
2969 } while (0)
2971 /* The pubname structure */
2973 typedef struct GTY(()) pubname_struct {
2974 dw_die_ref die;
2975 const char *name;
2977 pubname_entry;
2980 struct GTY(()) dw_ranges {
2981 const char *label;
2982 /* If this is positive, it's a block number, otherwise it's a
2983 bitwise-negated index into dw_ranges_by_label. */
2984 int num;
2985 /* Index for the range list for DW_FORM_rnglistx. */
2986 unsigned int idx : 31;
2987 /* True if this range might be possibly in a different section
2988 from previous entry. */
2989 unsigned int maybe_new_sec : 1;
2992 /* A structure to hold a macinfo entry. */
2994 typedef struct GTY(()) macinfo_struct {
2995 unsigned char code;
2996 unsigned HOST_WIDE_INT lineno;
2997 const char *info;
2999 macinfo_entry;
3002 struct GTY(()) dw_ranges_by_label {
3003 const char *begin;
3004 const char *end;
3007 /* The comdat type node structure. */
3008 struct GTY(()) comdat_type_node
3010 dw_die_ref root_die;
3011 dw_die_ref type_die;
3012 dw_die_ref skeleton_die;
3013 char signature[DWARF_TYPE_SIGNATURE_SIZE];
3014 comdat_type_node *next;
3017 /* A list of DIEs for which we can't determine ancestry (parent_die
3018 field) just yet. Later in dwarf2out_finish we will fill in the
3019 missing bits. */
3020 typedef struct GTY(()) limbo_die_struct {
3021 dw_die_ref die;
3022 /* The tree for which this DIE was created. We use this to
3023 determine ancestry later. */
3024 tree created_for;
3025 struct limbo_die_struct *next;
3027 limbo_die_node;
3029 typedef struct skeleton_chain_struct
3031 dw_die_ref old_die;
3032 dw_die_ref new_die;
3033 struct skeleton_chain_struct *parent;
3035 skeleton_chain_node;
3037 /* Define a macro which returns nonzero for a TYPE_DECL which was
3038 implicitly generated for a type.
3040 Note that, unlike the C front-end (which generates a NULL named
3041 TYPE_DECL node for each complete tagged type, each array type,
3042 and each function type node created) the C++ front-end generates
3043 a _named_ TYPE_DECL node for each tagged type node created.
3044 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3045 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
3046 front-end, but for each type, tagged or not. */
3048 #define TYPE_DECL_IS_STUB(decl) \
3049 (DECL_NAME (decl) == NULL_TREE \
3050 || (DECL_ARTIFICIAL (decl) \
3051 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
3052 /* This is necessary for stub decls that \
3053 appear in nested inline functions. */ \
3054 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3055 && (decl_ultimate_origin (decl) \
3056 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3058 /* Information concerning the compilation unit's programming
3059 language, and compiler version. */
3061 /* Fixed size portion of the DWARF compilation unit header. */
3062 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3063 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE \
3064 + (dwarf_version >= 5 ? 4 : 3))
3066 /* Fixed size portion of the DWARF comdat type unit header. */
3067 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3068 (DWARF_COMPILE_UNIT_HEADER_SIZE \
3069 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE)
3071 /* Fixed size portion of the DWARF skeleton compilation unit header. */
3072 #define DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE \
3073 (DWARF_COMPILE_UNIT_HEADER_SIZE + (dwarf_version >= 5 ? 8 : 0))
3075 /* Fixed size portion of public names info. */
3076 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3078 /* Fixed size portion of the address range info. */
3079 #define DWARF_ARANGES_HEADER_SIZE \
3080 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3081 DWARF2_ADDR_SIZE * 2) \
3082 - DWARF_INITIAL_LENGTH_SIZE)
3084 /* Size of padding portion in the address range info. It must be
3085 aligned to twice the pointer size. */
3086 #define DWARF_ARANGES_PAD_SIZE \
3087 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3088 DWARF2_ADDR_SIZE * 2) \
3089 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3091 /* Use assembler line directives if available. */
3092 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3093 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3094 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3095 #else
3096 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3097 #endif
3098 #endif
3100 /* Minimum line offset in a special line info. opcode.
3101 This value was chosen to give a reasonable range of values. */
3102 #define DWARF_LINE_BASE -10
3104 /* First special line opcode - leave room for the standard opcodes. */
3105 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
3107 /* Range of line offsets in a special line info. opcode. */
3108 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
3110 /* Flag that indicates the initial value of the is_stmt_start flag.
3111 In the present implementation, we do not mark any lines as
3112 the beginning of a source statement, because that information
3113 is not made available by the GCC front-end. */
3114 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3116 /* Maximum number of operations per instruction bundle. */
3117 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3118 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3119 #endif
3121 /* This location is used by calc_die_sizes() to keep track
3122 the offset of each DIE within the .debug_info section. */
3123 static unsigned long next_die_offset;
3125 /* Record the root of the DIE's built for the current compilation unit. */
3126 static GTY(()) dw_die_ref single_comp_unit_die;
3128 /* A list of type DIEs that have been separated into comdat sections. */
3129 static GTY(()) comdat_type_node *comdat_type_list;
3131 /* A list of CU DIEs that have been separated. */
3132 static GTY(()) limbo_die_node *cu_die_list;
3134 /* A list of DIEs with a NULL parent waiting to be relocated. */
3135 static GTY(()) limbo_die_node *limbo_die_list;
3137 /* A list of DIEs for which we may have to generate
3138 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
3139 static GTY(()) limbo_die_node *deferred_asm_name;
3141 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
3143 typedef const char *compare_type;
3145 static hashval_t hash (dwarf_file_data *);
3146 static bool equal (dwarf_file_data *, const char *);
3149 /* Filenames referenced by this compilation unit. */
3150 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
3152 struct decl_die_hasher : ggc_ptr_hash<die_node>
3154 typedef tree compare_type;
3156 static hashval_t hash (die_node *);
3157 static bool equal (die_node *, tree);
3159 /* A hash table of references to DIE's that describe declarations.
3160 The key is a DECL_UID() which is a unique number identifying each decl. */
3161 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
3163 struct GTY ((for_user)) variable_value_struct {
3164 unsigned int decl_id;
3165 vec<dw_die_ref, va_gc> *dies;
3168 struct variable_value_hasher : ggc_ptr_hash<variable_value_struct>
3170 typedef tree compare_type;
3172 static hashval_t hash (variable_value_struct *);
3173 static bool equal (variable_value_struct *, tree);
3175 /* A hash table of DIEs that contain DW_OP_GNU_variable_value with
3176 dw_val_class_decl_ref class, indexed by FUNCTION_DECLs which is
3177 DECL_CONTEXT of the referenced VAR_DECLs. */
3178 static GTY (()) hash_table<variable_value_hasher> *variable_value_hash;
3180 struct block_die_hasher : ggc_ptr_hash<die_struct>
3182 static hashval_t hash (die_struct *);
3183 static bool equal (die_struct *, die_struct *);
3186 /* A hash table of references to DIE's that describe COMMON blocks.
3187 The key is DECL_UID() ^ die_parent. */
3188 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
3190 typedef struct GTY(()) die_arg_entry_struct {
3191 dw_die_ref die;
3192 tree arg;
3193 } die_arg_entry;
3196 /* Node of the variable location list. */
3197 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3198 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3199 EXPR_LIST chain. For small bitsizes, bitsize is encoded
3200 in mode of the EXPR_LIST node and first EXPR_LIST operand
3201 is either NOTE_INSN_VAR_LOCATION for a piece with a known
3202 location or NULL for padding. For larger bitsizes,
3203 mode is 0 and first operand is a CONCAT with bitsize
3204 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3205 NULL as second operand. */
3206 rtx GTY (()) loc;
3207 const char * GTY (()) label;
3208 struct var_loc_node * GTY (()) next;
3211 /* Variable location list. */
3212 struct GTY ((for_user)) var_loc_list_def {
3213 struct var_loc_node * GTY (()) first;
3215 /* Pointer to the last but one or last element of the
3216 chained list. If the list is empty, both first and
3217 last are NULL, if the list contains just one node
3218 or the last node certainly is not redundant, it points
3219 to the last node, otherwise points to the last but one.
3220 Do not mark it for GC because it is marked through the chain. */
3221 struct var_loc_node * GTY ((skip ("%h"))) last;
3223 /* Pointer to the last element before section switch,
3224 if NULL, either sections weren't switched or first
3225 is after section switch. */
3226 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3228 /* DECL_UID of the variable decl. */
3229 unsigned int decl_id;
3231 typedef struct var_loc_list_def var_loc_list;
3233 /* Call argument location list. */
3234 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3235 rtx GTY (()) call_arg_loc_note;
3236 const char * GTY (()) label;
3237 tree GTY (()) block;
3238 bool tail_call_p;
3239 rtx GTY (()) symbol_ref;
3240 struct call_arg_loc_node * GTY (()) next;
3244 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
3246 typedef const_tree compare_type;
3248 static hashval_t hash (var_loc_list *);
3249 static bool equal (var_loc_list *, const_tree);
3252 /* Table of decl location linked lists. */
3253 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
3255 /* Head and tail of call_arg_loc chain. */
3256 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3257 static struct call_arg_loc_node *call_arg_loc_last;
3259 /* Number of call sites in the current function. */
3260 static int call_site_count = -1;
3261 /* Number of tail call sites in the current function. */
3262 static int tail_call_site_count = -1;
3264 /* A cached location list. */
3265 struct GTY ((for_user)) cached_dw_loc_list_def {
3266 /* The DECL_UID of the decl that this entry describes. */
3267 unsigned int decl_id;
3269 /* The cached location list. */
3270 dw_loc_list_ref loc_list;
3272 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3274 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3277 typedef const_tree compare_type;
3279 static hashval_t hash (cached_dw_loc_list *);
3280 static bool equal (cached_dw_loc_list *, const_tree);
3283 /* Table of cached location lists. */
3284 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3286 /* A vector of references to DIE's that are uniquely identified by their tag,
3287 presence/absence of children DIE's, and list of attribute/value pairs. */
3288 static GTY(()) vec<dw_die_ref, va_gc> *abbrev_die_table;
3290 /* A hash map to remember the stack usage for DWARF procedures. The value
3291 stored is the stack size difference between before the DWARF procedure
3292 invokation and after it returned. In other words, for a DWARF procedure
3293 that consumes N stack slots and that pushes M ones, this stores M - N. */
3294 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3296 /* A global counter for generating labels for line number data. */
3297 static unsigned int line_info_label_num;
3299 /* The current table to which we should emit line number information
3300 for the current function. This will be set up at the beginning of
3301 assembly for the function. */
3302 static GTY(()) dw_line_info_table *cur_line_info_table;
3304 /* The two default tables of line number info. */
3305 static GTY(()) dw_line_info_table *text_section_line_info;
3306 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3308 /* The set of all non-default tables of line number info. */
3309 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3311 /* A flag to tell pubnames/types export if there is an info section to
3312 refer to. */
3313 static bool info_section_emitted;
3315 /* A pointer to the base of a table that contains a list of publicly
3316 accessible names. */
3317 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3319 /* A pointer to the base of a table that contains a list of publicly
3320 accessible types. */
3321 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3323 /* A pointer to the base of a table that contains a list of macro
3324 defines/undefines (and file start/end markers). */
3325 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3327 /* True if .debug_macinfo or .debug_macros section is going to be
3328 emitted. */
3329 #define have_macinfo \
3330 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3331 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3332 && !macinfo_table->is_empty ())
3334 /* Vector of dies for which we should generate .debug_ranges info. */
3335 static GTY (()) vec<dw_ranges, va_gc> *ranges_table;
3337 /* Vector of pairs of labels referenced in ranges_table. */
3338 static GTY (()) vec<dw_ranges_by_label, va_gc> *ranges_by_label;
3340 /* Whether we have location lists that need outputting */
3341 static GTY(()) bool have_location_lists;
3343 /* Unique label counter. */
3344 static GTY(()) unsigned int loclabel_num;
3346 /* Unique label counter for point-of-call tables. */
3347 static GTY(()) unsigned int poc_label_num;
3349 /* The last file entry emitted by maybe_emit_file(). */
3350 static GTY(()) struct dwarf_file_data * last_emitted_file;
3352 /* Number of internal labels generated by gen_internal_sym(). */
3353 static GTY(()) int label_num;
3355 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3357 /* Instances of generic types for which we need to generate debug
3358 info that describe their generic parameters and arguments. That
3359 generation needs to happen once all types are properly laid out so
3360 we do it at the end of compilation. */
3361 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3363 /* Offset from the "steady-state frame pointer" to the frame base,
3364 within the current function. */
3365 static poly_int64 frame_pointer_fb_offset;
3366 static bool frame_pointer_fb_offset_valid;
3368 static vec<dw_die_ref> base_types;
3370 /* Flags to represent a set of attribute classes for attributes that represent
3371 a scalar value (bounds, pointers, ...). */
3372 enum dw_scalar_form
3374 dw_scalar_form_constant = 0x01,
3375 dw_scalar_form_exprloc = 0x02,
3376 dw_scalar_form_reference = 0x04
3379 /* Forward declarations for functions defined in this file. */
3381 static int is_pseudo_reg (const_rtx);
3382 static tree type_main_variant (tree);
3383 static int is_tagged_type (const_tree);
3384 static const char *dwarf_tag_name (unsigned);
3385 static const char *dwarf_attr_name (unsigned);
3386 static const char *dwarf_form_name (unsigned);
3387 static tree decl_ultimate_origin (const_tree);
3388 static tree decl_class_context (tree);
3389 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3390 static inline enum dw_val_class AT_class (dw_attr_node *);
3391 static inline unsigned int AT_index (dw_attr_node *);
3392 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3393 static inline unsigned AT_flag (dw_attr_node *);
3394 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3395 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3396 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3397 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3398 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3399 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3400 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3401 unsigned int, unsigned char *);
3402 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3403 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3404 static inline const char *AT_string (dw_attr_node *);
3405 static enum dwarf_form AT_string_form (dw_attr_node *);
3406 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3407 static void add_AT_specification (dw_die_ref, dw_die_ref);
3408 static inline dw_die_ref AT_ref (dw_attr_node *);
3409 static inline int AT_ref_external (dw_attr_node *);
3410 static inline void set_AT_ref_external (dw_attr_node *, int);
3411 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3412 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3413 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3414 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3415 dw_loc_list_ref);
3416 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3417 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3418 static void remove_addr_table_entry (addr_table_entry *);
3419 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3420 static inline rtx AT_addr (dw_attr_node *);
3421 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3422 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3423 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3424 static void add_AT_loclistsptr (dw_die_ref, enum dwarf_attribute,
3425 const char *);
3426 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3427 unsigned HOST_WIDE_INT);
3428 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3429 unsigned long, bool);
3430 static inline const char *AT_lbl (dw_attr_node *);
3431 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3432 static const char *get_AT_low_pc (dw_die_ref);
3433 static const char *get_AT_hi_pc (dw_die_ref);
3434 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3435 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3436 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3437 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3438 static bool is_cxx (void);
3439 static bool is_cxx (const_tree);
3440 static bool is_fortran (void);
3441 static bool is_ada (void);
3442 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3443 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3444 static void add_child_die (dw_die_ref, dw_die_ref);
3445 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3446 static dw_die_ref lookup_type_die (tree);
3447 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3448 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3449 static void equate_type_number_to_die (tree, dw_die_ref);
3450 static dw_die_ref lookup_decl_die (tree);
3451 static var_loc_list *lookup_decl_loc (const_tree);
3452 static void equate_decl_number_to_die (tree, dw_die_ref);
3453 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3454 static void print_spaces (FILE *);
3455 static void print_die (dw_die_ref, FILE *);
3456 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3457 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3458 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3459 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3460 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3461 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3462 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3463 struct md5_ctx *, int *);
3464 struct checksum_attributes;
3465 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3466 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3467 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3468 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3469 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3470 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3471 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3472 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3473 static int is_type_die (dw_die_ref);
3474 static int is_comdat_die (dw_die_ref);
3475 static inline bool is_template_instantiation (dw_die_ref);
3476 static int is_declaration_die (dw_die_ref);
3477 static int should_move_die_to_comdat (dw_die_ref);
3478 static dw_die_ref clone_as_declaration (dw_die_ref);
3479 static dw_die_ref clone_die (dw_die_ref);
3480 static dw_die_ref clone_tree (dw_die_ref);
3481 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3482 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3483 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3484 static dw_die_ref generate_skeleton (dw_die_ref);
3485 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3486 dw_die_ref,
3487 dw_die_ref);
3488 static void break_out_comdat_types (dw_die_ref);
3489 static void copy_decls_for_unworthy_types (dw_die_ref);
3491 static void add_sibling_attributes (dw_die_ref);
3492 static void output_location_lists (dw_die_ref);
3493 static int constant_size (unsigned HOST_WIDE_INT);
3494 static unsigned long size_of_die (dw_die_ref);
3495 static void calc_die_sizes (dw_die_ref);
3496 static void calc_base_type_die_sizes (void);
3497 static void mark_dies (dw_die_ref);
3498 static void unmark_dies (dw_die_ref);
3499 static void unmark_all_dies (dw_die_ref);
3500 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3501 static unsigned long size_of_aranges (void);
3502 static enum dwarf_form value_format (dw_attr_node *);
3503 static void output_value_format (dw_attr_node *);
3504 static void output_abbrev_section (void);
3505 static void output_die_abbrevs (unsigned long, dw_die_ref);
3506 static void output_die (dw_die_ref);
3507 static void output_compilation_unit_header (enum dwarf_unit_type);
3508 static void output_comp_unit (dw_die_ref, int, const unsigned char *);
3509 static void output_comdat_type_unit (comdat_type_node *);
3510 static const char *dwarf2_name (tree, int);
3511 static void add_pubname (tree, dw_die_ref);
3512 static void add_enumerator_pubname (const char *, dw_die_ref);
3513 static void add_pubname_string (const char *, dw_die_ref);
3514 static void add_pubtype (tree, dw_die_ref);
3515 static void output_pubnames (vec<pubname_entry, va_gc> *);
3516 static void output_aranges (void);
3517 static unsigned int add_ranges (const_tree, bool = false);
3518 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3519 bool *, bool);
3520 static void output_ranges (void);
3521 static dw_line_info_table *new_line_info_table (void);
3522 static void output_line_info (bool);
3523 static void output_file_names (void);
3524 static dw_die_ref base_type_die (tree, bool);
3525 static int is_base_type (tree);
3526 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3527 static int decl_quals (const_tree);
3528 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3529 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3530 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3531 static int type_is_enum (const_tree);
3532 static unsigned int dbx_reg_number (const_rtx);
3533 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3534 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3535 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3536 enum var_init_status);
3537 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3538 enum var_init_status);
3539 static dw_loc_descr_ref based_loc_descr (rtx, poly_int64,
3540 enum var_init_status);
3541 static int is_based_loc (const_rtx);
3542 static bool resolve_one_addr (rtx *);
3543 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3544 enum var_init_status);
3545 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3546 enum var_init_status);
3547 struct loc_descr_context;
3548 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3549 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3550 static dw_loc_list_ref loc_list_from_tree (tree, int,
3551 struct loc_descr_context *);
3552 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3553 struct loc_descr_context *);
3554 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3555 static tree field_type (const_tree);
3556 static unsigned int simple_type_align_in_bits (const_tree);
3557 static unsigned int simple_decl_align_in_bits (const_tree);
3558 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3559 struct vlr_context;
3560 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3561 HOST_WIDE_INT *);
3562 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3563 dw_loc_list_ref);
3564 static void add_data_member_location_attribute (dw_die_ref, tree,
3565 struct vlr_context *);
3566 static bool add_const_value_attribute (dw_die_ref, rtx);
3567 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3568 static void insert_wide_int (const wide_int &, unsigned char *, int);
3569 static void insert_float (const_rtx, unsigned char *);
3570 static rtx rtl_for_decl_location (tree);
3571 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3572 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3573 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3574 static void add_name_attribute (dw_die_ref, const char *);
3575 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3576 static void add_comp_dir_attribute (dw_die_ref);
3577 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3578 struct loc_descr_context *);
3579 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3580 struct loc_descr_context *);
3581 static void add_subscript_info (dw_die_ref, tree, bool);
3582 static void add_byte_size_attribute (dw_die_ref, tree);
3583 static void add_alignment_attribute (dw_die_ref, tree);
3584 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3585 struct vlr_context *);
3586 static void add_bit_size_attribute (dw_die_ref, tree);
3587 static void add_prototyped_attribute (dw_die_ref, tree);
3588 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3589 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3590 static void add_src_coords_attributes (dw_die_ref, tree);
3591 static void add_name_and_src_coords_attributes (dw_die_ref, tree, bool = false);
3592 static void add_discr_value (dw_die_ref, dw_discr_value *);
3593 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3594 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3595 static void push_decl_scope (tree);
3596 static void pop_decl_scope (void);
3597 static dw_die_ref scope_die_for (tree, dw_die_ref);
3598 static inline int local_scope_p (dw_die_ref);
3599 static inline int class_scope_p (dw_die_ref);
3600 static inline int class_or_namespace_scope_p (dw_die_ref);
3601 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3602 static void add_calling_convention_attribute (dw_die_ref, tree);
3603 static const char *type_tag (const_tree);
3604 static tree member_declared_type (const_tree);
3605 #if 0
3606 static const char *decl_start_label (tree);
3607 #endif
3608 static void gen_array_type_die (tree, dw_die_ref);
3609 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3610 #if 0
3611 static void gen_entry_point_die (tree, dw_die_ref);
3612 #endif
3613 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3614 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3615 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3616 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3617 static void gen_formal_types_die (tree, dw_die_ref);
3618 static void gen_subprogram_die (tree, dw_die_ref);
3619 static void gen_variable_die (tree, tree, dw_die_ref);
3620 static void gen_const_die (tree, dw_die_ref);
3621 static void gen_label_die (tree, dw_die_ref);
3622 static void gen_lexical_block_die (tree, dw_die_ref);
3623 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3624 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3625 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3626 static dw_die_ref gen_compile_unit_die (const char *);
3627 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3628 static void gen_member_die (tree, dw_die_ref);
3629 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3630 enum debug_info_usage);
3631 static void gen_subroutine_type_die (tree, dw_die_ref);
3632 static void gen_typedef_die (tree, dw_die_ref);
3633 static void gen_type_die (tree, dw_die_ref);
3634 static void gen_block_die (tree, dw_die_ref);
3635 static void decls_for_scope (tree, dw_die_ref);
3636 static bool is_naming_typedef_decl (const_tree);
3637 static inline dw_die_ref get_context_die (tree);
3638 static void gen_namespace_die (tree, dw_die_ref);
3639 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3640 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3641 static dw_die_ref force_decl_die (tree);
3642 static dw_die_ref force_type_die (tree);
3643 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3644 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3645 static struct dwarf_file_data * lookup_filename (const char *);
3646 static void retry_incomplete_types (void);
3647 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3648 static void gen_generic_params_dies (tree);
3649 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3650 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3651 static void splice_child_die (dw_die_ref, dw_die_ref);
3652 static int file_info_cmp (const void *, const void *);
3653 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3654 const char *, const char *);
3655 static void output_loc_list (dw_loc_list_ref);
3656 static char *gen_internal_sym (const char *);
3657 static bool want_pubnames (void);
3659 static void prune_unmark_dies (dw_die_ref);
3660 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3661 static void prune_unused_types_mark (dw_die_ref, int);
3662 static void prune_unused_types_walk (dw_die_ref);
3663 static void prune_unused_types_walk_attribs (dw_die_ref);
3664 static void prune_unused_types_prune (dw_die_ref);
3665 static void prune_unused_types (void);
3666 static int maybe_emit_file (struct dwarf_file_data *fd);
3667 static inline const char *AT_vms_delta1 (dw_attr_node *);
3668 static inline const char *AT_vms_delta2 (dw_attr_node *);
3669 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3670 const char *, const char *);
3671 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3672 static void gen_remaining_tmpl_value_param_die_attribute (void);
3673 static bool generic_type_p (tree);
3674 static void schedule_generic_params_dies_gen (tree t);
3675 static void gen_scheduled_generic_parms_dies (void);
3676 static void resolve_variable_values (void);
3678 static const char *comp_dir_string (void);
3680 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3682 /* enum for tracking thread-local variables whose address is really an offset
3683 relative to the TLS pointer, which will need link-time relocation, but will
3684 not need relocation by the DWARF consumer. */
3686 enum dtprel_bool
3688 dtprel_false = 0,
3689 dtprel_true = 1
3692 /* Return the operator to use for an address of a variable. For dtprel_true, we
3693 use DW_OP_const*. For regular variables, which need both link-time
3694 relocation and consumer-level relocation (e.g., to account for shared objects
3695 loaded at a random address), we use DW_OP_addr*. */
3697 static inline enum dwarf_location_atom
3698 dw_addr_op (enum dtprel_bool dtprel)
3700 if (dtprel == dtprel_true)
3701 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3702 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3703 else
3704 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3707 /* Return a pointer to a newly allocated address location description. If
3708 dwarf_split_debug_info is true, then record the address with the appropriate
3709 relocation. */
3710 static inline dw_loc_descr_ref
3711 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3713 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3715 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3716 ref->dw_loc_oprnd1.v.val_addr = addr;
3717 ref->dtprel = dtprel;
3718 if (dwarf_split_debug_info)
3719 ref->dw_loc_oprnd1.val_entry
3720 = add_addr_table_entry (addr,
3721 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3722 else
3723 ref->dw_loc_oprnd1.val_entry = NULL;
3725 return ref;
3728 /* Section names used to hold DWARF debugging information. */
3730 #ifndef DEBUG_INFO_SECTION
3731 #define DEBUG_INFO_SECTION ".debug_info"
3732 #endif
3733 #ifndef DEBUG_DWO_INFO_SECTION
3734 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3735 #endif
3736 #ifndef DEBUG_LTO_INFO_SECTION
3737 #define DEBUG_LTO_INFO_SECTION ".gnu.debuglto_.debug_info"
3738 #endif
3739 #ifndef DEBUG_LTO_DWO_INFO_SECTION
3740 #define DEBUG_LTO_DWO_INFO_SECTION ".gnu.debuglto_.debug_info.dwo"
3741 #endif
3742 #ifndef DEBUG_ABBREV_SECTION
3743 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3744 #endif
3745 #ifndef DEBUG_LTO_ABBREV_SECTION
3746 #define DEBUG_LTO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev"
3747 #endif
3748 #ifndef DEBUG_DWO_ABBREV_SECTION
3749 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3750 #endif
3751 #ifndef DEBUG_LTO_DWO_ABBREV_SECTION
3752 #define DEBUG_LTO_DWO_ABBREV_SECTION ".gnu.debuglto_.debug_abbrev.dwo"
3753 #endif
3754 #ifndef DEBUG_ARANGES_SECTION
3755 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3756 #endif
3757 #ifndef DEBUG_ADDR_SECTION
3758 #define DEBUG_ADDR_SECTION ".debug_addr"
3759 #endif
3760 #ifndef DEBUG_MACINFO_SECTION
3761 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3762 #endif
3763 #ifndef DEBUG_LTO_MACINFO_SECTION
3764 #define DEBUG_LTO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo"
3765 #endif
3766 #ifndef DEBUG_DWO_MACINFO_SECTION
3767 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3768 #endif
3769 #ifndef DEBUG_LTO_DWO_MACINFO_SECTION
3770 #define DEBUG_LTO_DWO_MACINFO_SECTION ".gnu.debuglto_.debug_macinfo.dwo"
3771 #endif
3772 #ifndef DEBUG_MACRO_SECTION
3773 #define DEBUG_MACRO_SECTION ".debug_macro"
3774 #endif
3775 #ifndef DEBUG_LTO_MACRO_SECTION
3776 #define DEBUG_LTO_MACRO_SECTION ".gnu.debuglto_.debug_macro"
3777 #endif
3778 #ifndef DEBUG_DWO_MACRO_SECTION
3779 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3780 #endif
3781 #ifndef DEBUG_LTO_DWO_MACRO_SECTION
3782 #define DEBUG_LTO_DWO_MACRO_SECTION ".gnu.debuglto_.debug_macro.dwo"
3783 #endif
3784 #ifndef DEBUG_LINE_SECTION
3785 #define DEBUG_LINE_SECTION ".debug_line"
3786 #endif
3787 #ifndef DEBUG_LTO_LINE_SECTION
3788 #define DEBUG_LTO_LINE_SECTION ".gnu.debuglto_.debug_line"
3789 #endif
3790 #ifndef DEBUG_DWO_LINE_SECTION
3791 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3792 #endif
3793 #ifndef DEBUG_LTO_DWO_LINE_SECTION
3794 #define DEBUG_LTO_DWO_LINE_SECTION ".gnu.debuglto_.debug_line.dwo"
3795 #endif
3796 #ifndef DEBUG_LOC_SECTION
3797 #define DEBUG_LOC_SECTION ".debug_loc"
3798 #endif
3799 #ifndef DEBUG_DWO_LOC_SECTION
3800 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3801 #endif
3802 #ifndef DEBUG_LOCLISTS_SECTION
3803 #define DEBUG_LOCLISTS_SECTION ".debug_loclists"
3804 #endif
3805 #ifndef DEBUG_DWO_LOCLISTS_SECTION
3806 #define DEBUG_DWO_LOCLISTS_SECTION ".debug_loclists.dwo"
3807 #endif
3808 #ifndef DEBUG_PUBNAMES_SECTION
3809 #define DEBUG_PUBNAMES_SECTION \
3810 ((debug_generate_pub_sections == 2) \
3811 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3812 #endif
3813 #ifndef DEBUG_PUBTYPES_SECTION
3814 #define DEBUG_PUBTYPES_SECTION \
3815 ((debug_generate_pub_sections == 2) \
3816 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3817 #endif
3818 #ifndef DEBUG_STR_OFFSETS_SECTION
3819 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3820 #endif
3821 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3822 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3823 #endif
3824 #ifndef DEBUG_LTO_DWO_STR_OFFSETS_SECTION
3825 #define DEBUG_LTO_DWO_STR_OFFSETS_SECTION ".gnu.debuglto_.debug_str_offsets.dwo"
3826 #endif
3827 #ifndef DEBUG_STR_SECTION
3828 #define DEBUG_STR_SECTION ".debug_str"
3829 #endif
3830 #ifndef DEBUG_LTO_STR_SECTION
3831 #define DEBUG_LTO_STR_SECTION ".gnu.debuglto_.debug_str"
3832 #endif
3833 #ifndef DEBUG_STR_DWO_SECTION
3834 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3835 #endif
3836 #ifndef DEBUG_LTO_STR_DWO_SECTION
3837 #define DEBUG_LTO_STR_DWO_SECTION ".gnu.debuglto_.debug_str.dwo"
3838 #endif
3839 #ifndef DEBUG_RANGES_SECTION
3840 #define DEBUG_RANGES_SECTION ".debug_ranges"
3841 #endif
3842 #ifndef DEBUG_RNGLISTS_SECTION
3843 #define DEBUG_RNGLISTS_SECTION ".debug_rnglists"
3844 #endif
3845 #ifndef DEBUG_LINE_STR_SECTION
3846 #define DEBUG_LINE_STR_SECTION ".debug_line_str"
3847 #endif
3848 #ifndef DEBUG_LTO_LINE_STR_SECTION
3849 #define DEBUG_LTO_LINE_STR_SECTION ".gnu.debuglto_.debug_line_str"
3850 #endif
3852 /* Standard ELF section names for compiled code and data. */
3853 #ifndef TEXT_SECTION_NAME
3854 #define TEXT_SECTION_NAME ".text"
3855 #endif
3857 /* Section flags for .debug_str section. */
3858 #define DEBUG_STR_SECTION_FLAGS \
3859 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3860 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3861 : SECTION_DEBUG)
3863 /* Section flags for .debug_str.dwo section. */
3864 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3866 /* Attribute used to refer to the macro section. */
3867 #define DEBUG_MACRO_ATTRIBUTE (dwarf_version >= 5 ? DW_AT_macros \
3868 : dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros)
3870 /* Labels we insert at beginning sections we can reference instead of
3871 the section names themselves. */
3873 #ifndef TEXT_SECTION_LABEL
3874 #define TEXT_SECTION_LABEL "Ltext"
3875 #endif
3876 #ifndef COLD_TEXT_SECTION_LABEL
3877 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3878 #endif
3879 #ifndef DEBUG_LINE_SECTION_LABEL
3880 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3881 #endif
3882 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3883 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3884 #endif
3885 #ifndef DEBUG_INFO_SECTION_LABEL
3886 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3887 #endif
3888 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3889 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3890 #endif
3891 #ifndef DEBUG_ABBREV_SECTION_LABEL
3892 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3893 #endif
3894 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3895 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3896 #endif
3897 #ifndef DEBUG_ADDR_SECTION_LABEL
3898 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3899 #endif
3900 #ifndef DEBUG_LOC_SECTION_LABEL
3901 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3902 #endif
3903 #ifndef DEBUG_RANGES_SECTION_LABEL
3904 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3905 #endif
3906 #ifndef DEBUG_MACINFO_SECTION_LABEL
3907 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3908 #endif
3909 #ifndef DEBUG_MACRO_SECTION_LABEL
3910 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3911 #endif
3912 #define SKELETON_COMP_DIE_ABBREV 1
3913 #define SKELETON_TYPE_DIE_ABBREV 2
3915 /* Definitions of defaults for formats and names of various special
3916 (artificial) labels which may be generated within this file (when the -g
3917 options is used and DWARF2_DEBUGGING_INFO is in effect.
3918 If necessary, these may be overridden from within the tm.h file, but
3919 typically, overriding these defaults is unnecessary. */
3921 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3922 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3923 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3924 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3925 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3926 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3927 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3928 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3929 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3930 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3931 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3932 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3933 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3934 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3935 static char ranges_base_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3937 #ifndef TEXT_END_LABEL
3938 #define TEXT_END_LABEL "Letext"
3939 #endif
3940 #ifndef COLD_END_LABEL
3941 #define COLD_END_LABEL "Letext_cold"
3942 #endif
3943 #ifndef BLOCK_BEGIN_LABEL
3944 #define BLOCK_BEGIN_LABEL "LBB"
3945 #endif
3946 #ifndef BLOCK_END_LABEL
3947 #define BLOCK_END_LABEL "LBE"
3948 #endif
3949 #ifndef LINE_CODE_LABEL
3950 #define LINE_CODE_LABEL "LM"
3951 #endif
3954 /* Return the root of the DIE's built for the current compilation unit. */
3955 static dw_die_ref
3956 comp_unit_die (void)
3958 if (!single_comp_unit_die)
3959 single_comp_unit_die = gen_compile_unit_die (NULL);
3960 return single_comp_unit_die;
3963 /* We allow a language front-end to designate a function that is to be
3964 called to "demangle" any name before it is put into a DIE. */
3966 static const char *(*demangle_name_func) (const char *);
3968 void
3969 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3971 demangle_name_func = func;
3974 /* Test if rtl node points to a pseudo register. */
3976 static inline int
3977 is_pseudo_reg (const_rtx rtl)
3979 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3980 || (GET_CODE (rtl) == SUBREG
3981 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3984 /* Return a reference to a type, with its const and volatile qualifiers
3985 removed. */
3987 static inline tree
3988 type_main_variant (tree type)
3990 type = TYPE_MAIN_VARIANT (type);
3992 /* ??? There really should be only one main variant among any group of
3993 variants of a given type (and all of the MAIN_VARIANT values for all
3994 members of the group should point to that one type) but sometimes the C
3995 front-end messes this up for array types, so we work around that bug
3996 here. */
3997 if (TREE_CODE (type) == ARRAY_TYPE)
3998 while (type != TYPE_MAIN_VARIANT (type))
3999 type = TYPE_MAIN_VARIANT (type);
4001 return type;
4004 /* Return nonzero if the given type node represents a tagged type. */
4006 static inline int
4007 is_tagged_type (const_tree type)
4009 enum tree_code code = TREE_CODE (type);
4011 return (code == RECORD_TYPE || code == UNION_TYPE
4012 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4015 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
4017 static void
4018 get_ref_die_offset_label (char *label, dw_die_ref ref)
4020 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4023 /* Return die_offset of a DIE reference to a base type. */
4025 static unsigned long int
4026 get_base_type_offset (dw_die_ref ref)
4028 if (ref->die_offset)
4029 return ref->die_offset;
4030 if (comp_unit_die ()->die_abbrev)
4032 calc_base_type_die_sizes ();
4033 gcc_assert (ref->die_offset);
4035 return ref->die_offset;
4038 /* Return die_offset of a DIE reference other than base type. */
4040 static unsigned long int
4041 get_ref_die_offset (dw_die_ref ref)
4043 gcc_assert (ref->die_offset);
4044 return ref->die_offset;
4047 /* Convert a DIE tag into its string name. */
4049 static const char *
4050 dwarf_tag_name (unsigned int tag)
4052 const char *name = get_DW_TAG_name (tag);
4054 if (name != NULL)
4055 return name;
4057 return "DW_TAG_<unknown>";
4060 /* Convert a DWARF attribute code into its string name. */
4062 static const char *
4063 dwarf_attr_name (unsigned int attr)
4065 const char *name;
4067 switch (attr)
4069 #if VMS_DEBUGGING_INFO
4070 case DW_AT_HP_prologue:
4071 return "DW_AT_HP_prologue";
4072 #else
4073 case DW_AT_MIPS_loop_unroll_factor:
4074 return "DW_AT_MIPS_loop_unroll_factor";
4075 #endif
4077 #if VMS_DEBUGGING_INFO
4078 case DW_AT_HP_epilogue:
4079 return "DW_AT_HP_epilogue";
4080 #else
4081 case DW_AT_MIPS_stride:
4082 return "DW_AT_MIPS_stride";
4083 #endif
4086 name = get_DW_AT_name (attr);
4088 if (name != NULL)
4089 return name;
4091 return "DW_AT_<unknown>";
4094 /* Convert a DWARF value form code into its string name. */
4096 static const char *
4097 dwarf_form_name (unsigned int form)
4099 const char *name = get_DW_FORM_name (form);
4101 if (name != NULL)
4102 return name;
4104 return "DW_FORM_<unknown>";
4107 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
4108 instance of an inlined instance of a decl which is local to an inline
4109 function, so we have to trace all of the way back through the origin chain
4110 to find out what sort of node actually served as the original seed for the
4111 given block. */
4113 static tree
4114 decl_ultimate_origin (const_tree decl)
4116 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4117 return NULL_TREE;
4119 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
4120 we're trying to output the abstract instance of this function. */
4121 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4122 return NULL_TREE;
4124 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4125 most distant ancestor, this should never happen. */
4126 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4128 return DECL_ABSTRACT_ORIGIN (decl);
4131 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
4132 of a virtual function may refer to a base class, so we check the 'this'
4133 parameter. */
4135 static tree
4136 decl_class_context (tree decl)
4138 tree context = NULL_TREE;
4140 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4141 context = DECL_CONTEXT (decl);
4142 else
4143 context = TYPE_MAIN_VARIANT
4144 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4146 if (context && !TYPE_P (context))
4147 context = NULL_TREE;
4149 return context;
4152 /* Add an attribute/value pair to a DIE. */
4154 static inline void
4155 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
4157 /* Maybe this should be an assert? */
4158 if (die == NULL)
4159 return;
4161 if (flag_checking)
4163 /* Check we do not add duplicate attrs. Can't use get_AT here
4164 because that recurses to the specification/abstract origin DIE. */
4165 dw_attr_node *a;
4166 unsigned ix;
4167 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4168 gcc_assert (a->dw_attr != attr->dw_attr);
4171 vec_safe_reserve (die->die_attr, 1);
4172 vec_safe_push (die->die_attr, *attr);
4175 static inline enum dw_val_class
4176 AT_class (dw_attr_node *a)
4178 return a->dw_attr_val.val_class;
4181 /* Return the index for any attribute that will be referenced with a
4182 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
4183 are stored in dw_attr_val.v.val_str for reference counting
4184 pruning. */
4186 static inline unsigned int
4187 AT_index (dw_attr_node *a)
4189 if (AT_class (a) == dw_val_class_str)
4190 return a->dw_attr_val.v.val_str->index;
4191 else if (a->dw_attr_val.val_entry != NULL)
4192 return a->dw_attr_val.val_entry->index;
4193 return NOT_INDEXED;
4196 /* Add a flag value attribute to a DIE. */
4198 static inline void
4199 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4201 dw_attr_node attr;
4203 attr.dw_attr = attr_kind;
4204 attr.dw_attr_val.val_class = dw_val_class_flag;
4205 attr.dw_attr_val.val_entry = NULL;
4206 attr.dw_attr_val.v.val_flag = flag;
4207 add_dwarf_attr (die, &attr);
4210 static inline unsigned
4211 AT_flag (dw_attr_node *a)
4213 gcc_assert (a && AT_class (a) == dw_val_class_flag);
4214 return a->dw_attr_val.v.val_flag;
4217 /* Add a signed integer attribute value to a DIE. */
4219 static inline void
4220 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4222 dw_attr_node attr;
4224 attr.dw_attr = attr_kind;
4225 attr.dw_attr_val.val_class = dw_val_class_const;
4226 attr.dw_attr_val.val_entry = NULL;
4227 attr.dw_attr_val.v.val_int = int_val;
4228 add_dwarf_attr (die, &attr);
4231 static inline HOST_WIDE_INT
4232 AT_int (dw_attr_node *a)
4234 gcc_assert (a && (AT_class (a) == dw_val_class_const
4235 || AT_class (a) == dw_val_class_const_implicit));
4236 return a->dw_attr_val.v.val_int;
4239 /* Add an unsigned integer attribute value to a DIE. */
4241 static inline void
4242 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4243 unsigned HOST_WIDE_INT unsigned_val)
4245 dw_attr_node attr;
4247 attr.dw_attr = attr_kind;
4248 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4249 attr.dw_attr_val.val_entry = NULL;
4250 attr.dw_attr_val.v.val_unsigned = unsigned_val;
4251 add_dwarf_attr (die, &attr);
4254 static inline unsigned HOST_WIDE_INT
4255 AT_unsigned (dw_attr_node *a)
4257 gcc_assert (a && (AT_class (a) == dw_val_class_unsigned_const
4258 || AT_class (a) == dw_val_class_unsigned_const_implicit));
4259 return a->dw_attr_val.v.val_unsigned;
4262 /* Add an unsigned wide integer attribute value to a DIE. */
4264 static inline void
4265 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
4266 const wide_int& w)
4268 dw_attr_node attr;
4270 attr.dw_attr = attr_kind;
4271 attr.dw_attr_val.val_class = dw_val_class_wide_int;
4272 attr.dw_attr_val.val_entry = NULL;
4273 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
4274 *attr.dw_attr_val.v.val_wide = w;
4275 add_dwarf_attr (die, &attr);
4278 /* Add an unsigned double integer attribute value to a DIE. */
4280 static inline void
4281 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4282 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4284 dw_attr_node attr;
4286 attr.dw_attr = attr_kind;
4287 attr.dw_attr_val.val_class = dw_val_class_const_double;
4288 attr.dw_attr_val.val_entry = NULL;
4289 attr.dw_attr_val.v.val_double.high = high;
4290 attr.dw_attr_val.v.val_double.low = low;
4291 add_dwarf_attr (die, &attr);
4294 /* Add a floating point attribute value to a DIE and return it. */
4296 static inline void
4297 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4298 unsigned int length, unsigned int elt_size, unsigned char *array)
4300 dw_attr_node attr;
4302 attr.dw_attr = attr_kind;
4303 attr.dw_attr_val.val_class = dw_val_class_vec;
4304 attr.dw_attr_val.val_entry = NULL;
4305 attr.dw_attr_val.v.val_vec.length = length;
4306 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4307 attr.dw_attr_val.v.val_vec.array = array;
4308 add_dwarf_attr (die, &attr);
4311 /* Add an 8-byte data attribute value to a DIE. */
4313 static inline void
4314 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4315 unsigned char data8[8])
4317 dw_attr_node attr;
4319 attr.dw_attr = attr_kind;
4320 attr.dw_attr_val.val_class = dw_val_class_data8;
4321 attr.dw_attr_val.val_entry = NULL;
4322 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4323 add_dwarf_attr (die, &attr);
4326 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4327 dwarf_split_debug_info, address attributes in dies destined for the
4328 final executable have force_direct set to avoid using indexed
4329 references. */
4331 static inline void
4332 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4333 bool force_direct)
4335 dw_attr_node attr;
4336 char * lbl_id;
4338 lbl_id = xstrdup (lbl_low);
4339 attr.dw_attr = DW_AT_low_pc;
4340 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4341 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4342 if (dwarf_split_debug_info && !force_direct)
4343 attr.dw_attr_val.val_entry
4344 = add_addr_table_entry (lbl_id, ate_kind_label);
4345 else
4346 attr.dw_attr_val.val_entry = NULL;
4347 add_dwarf_attr (die, &attr);
4349 attr.dw_attr = DW_AT_high_pc;
4350 if (dwarf_version < 4)
4351 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4352 else
4353 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4354 lbl_id = xstrdup (lbl_high);
4355 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4356 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4357 && dwarf_split_debug_info && !force_direct)
4358 attr.dw_attr_val.val_entry
4359 = add_addr_table_entry (lbl_id, ate_kind_label);
4360 else
4361 attr.dw_attr_val.val_entry = NULL;
4362 add_dwarf_attr (die, &attr);
4365 /* Hash and equality functions for debug_str_hash. */
4367 hashval_t
4368 indirect_string_hasher::hash (indirect_string_node *x)
4370 return htab_hash_string (x->str);
4373 bool
4374 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4376 return strcmp (x1->str, x2) == 0;
4379 /* Add STR to the given string hash table. */
4381 static struct indirect_string_node *
4382 find_AT_string_in_table (const char *str,
4383 hash_table<indirect_string_hasher> *table)
4385 struct indirect_string_node *node;
4387 indirect_string_node **slot
4388 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4389 if (*slot == NULL)
4391 node = ggc_cleared_alloc<indirect_string_node> ();
4392 node->str = ggc_strdup (str);
4393 *slot = node;
4395 else
4396 node = *slot;
4398 node->refcount++;
4399 return node;
4402 /* Add STR to the indirect string hash table. */
4404 static struct indirect_string_node *
4405 find_AT_string (const char *str)
4407 if (! debug_str_hash)
4408 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4410 return find_AT_string_in_table (str, debug_str_hash);
4413 /* Add a string attribute value to a DIE. */
4415 static inline void
4416 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4418 dw_attr_node attr;
4419 struct indirect_string_node *node;
4421 node = find_AT_string (str);
4423 attr.dw_attr = attr_kind;
4424 attr.dw_attr_val.val_class = dw_val_class_str;
4425 attr.dw_attr_val.val_entry = NULL;
4426 attr.dw_attr_val.v.val_str = node;
4427 add_dwarf_attr (die, &attr);
4430 static inline const char *
4431 AT_string (dw_attr_node *a)
4433 gcc_assert (a && AT_class (a) == dw_val_class_str);
4434 return a->dw_attr_val.v.val_str->str;
4437 /* Call this function directly to bypass AT_string_form's logic to put
4438 the string inline in the die. */
4440 static void
4441 set_indirect_string (struct indirect_string_node *node)
4443 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4444 /* Already indirect is a no op. */
4445 if (node->form == DW_FORM_strp
4446 || node->form == DW_FORM_line_strp
4447 || node->form == DW_FORM_GNU_str_index)
4449 gcc_assert (node->label);
4450 return;
4452 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4453 ++dw2_string_counter;
4454 node->label = xstrdup (label);
4456 if (!dwarf_split_debug_info)
4458 node->form = DW_FORM_strp;
4459 node->index = NOT_INDEXED;
4461 else
4463 node->form = DW_FORM_GNU_str_index;
4464 node->index = NO_INDEX_ASSIGNED;
4468 /* A helper function for dwarf2out_finish, called to reset indirect
4469 string decisions done for early LTO dwarf output before fat object
4470 dwarf output. */
4473 reset_indirect_string (indirect_string_node **h, void *)
4475 struct indirect_string_node *node = *h;
4476 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4478 free (node->label);
4479 node->label = NULL;
4480 node->form = (dwarf_form) 0;
4481 node->index = 0;
4483 return 1;
4486 /* Find out whether a string should be output inline in DIE
4487 or out-of-line in .debug_str section. */
4489 static enum dwarf_form
4490 find_string_form (struct indirect_string_node *node)
4492 unsigned int len;
4494 if (node->form)
4495 return node->form;
4497 len = strlen (node->str) + 1;
4499 /* If the string is shorter or equal to the size of the reference, it is
4500 always better to put it inline. */
4501 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4502 return node->form = DW_FORM_string;
4504 /* If we cannot expect the linker to merge strings in .debug_str
4505 section, only put it into .debug_str if it is worth even in this
4506 single module. */
4507 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4508 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4509 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4510 return node->form = DW_FORM_string;
4512 set_indirect_string (node);
4514 return node->form;
4517 /* Find out whether the string referenced from the attribute should be
4518 output inline in DIE or out-of-line in .debug_str section. */
4520 static enum dwarf_form
4521 AT_string_form (dw_attr_node *a)
4523 gcc_assert (a && AT_class (a) == dw_val_class_str);
4524 return find_string_form (a->dw_attr_val.v.val_str);
4527 /* Add a DIE reference attribute value to a DIE. */
4529 static inline void
4530 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4532 dw_attr_node attr;
4533 gcc_checking_assert (targ_die != NULL);
4535 /* With LTO we can end up trying to reference something we didn't create
4536 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4537 if (targ_die == NULL)
4538 return;
4540 attr.dw_attr = attr_kind;
4541 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4542 attr.dw_attr_val.val_entry = NULL;
4543 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4544 attr.dw_attr_val.v.val_die_ref.external = 0;
4545 add_dwarf_attr (die, &attr);
4548 /* Change DIE reference REF to point to NEW_DIE instead. */
4550 static inline void
4551 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4553 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4554 ref->dw_attr_val.v.val_die_ref.die = new_die;
4555 ref->dw_attr_val.v.val_die_ref.external = 0;
4558 /* Add an AT_specification attribute to a DIE, and also make the back
4559 pointer from the specification to the definition. */
4561 static inline void
4562 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4564 add_AT_die_ref (die, DW_AT_specification, targ_die);
4565 gcc_assert (!targ_die->die_definition);
4566 targ_die->die_definition = die;
4569 static inline dw_die_ref
4570 AT_ref (dw_attr_node *a)
4572 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4573 return a->dw_attr_val.v.val_die_ref.die;
4576 static inline int
4577 AT_ref_external (dw_attr_node *a)
4579 if (a && AT_class (a) == dw_val_class_die_ref)
4580 return a->dw_attr_val.v.val_die_ref.external;
4582 return 0;
4585 static inline void
4586 set_AT_ref_external (dw_attr_node *a, int i)
4588 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4589 a->dw_attr_val.v.val_die_ref.external = i;
4592 /* Add an FDE reference attribute value to a DIE. */
4594 static inline void
4595 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4597 dw_attr_node attr;
4599 attr.dw_attr = attr_kind;
4600 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4601 attr.dw_attr_val.val_entry = NULL;
4602 attr.dw_attr_val.v.val_fde_index = targ_fde;
4603 add_dwarf_attr (die, &attr);
4606 /* Add a location description attribute value to a DIE. */
4608 static inline void
4609 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4611 dw_attr_node attr;
4613 attr.dw_attr = attr_kind;
4614 attr.dw_attr_val.val_class = dw_val_class_loc;
4615 attr.dw_attr_val.val_entry = NULL;
4616 attr.dw_attr_val.v.val_loc = loc;
4617 add_dwarf_attr (die, &attr);
4620 static inline dw_loc_descr_ref
4621 AT_loc (dw_attr_node *a)
4623 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4624 return a->dw_attr_val.v.val_loc;
4627 static inline void
4628 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4630 dw_attr_node attr;
4632 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4633 return;
4635 attr.dw_attr = attr_kind;
4636 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4637 attr.dw_attr_val.val_entry = NULL;
4638 attr.dw_attr_val.v.val_loc_list = loc_list;
4639 add_dwarf_attr (die, &attr);
4640 have_location_lists = true;
4643 static inline dw_loc_list_ref
4644 AT_loc_list (dw_attr_node *a)
4646 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4647 return a->dw_attr_val.v.val_loc_list;
4650 static inline dw_loc_list_ref *
4651 AT_loc_list_ptr (dw_attr_node *a)
4653 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4654 return &a->dw_attr_val.v.val_loc_list;
4657 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4659 static hashval_t hash (addr_table_entry *);
4660 static bool equal (addr_table_entry *, addr_table_entry *);
4663 /* Table of entries into the .debug_addr section. */
4665 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4667 /* Hash an address_table_entry. */
4669 hashval_t
4670 addr_hasher::hash (addr_table_entry *a)
4672 inchash::hash hstate;
4673 switch (a->kind)
4675 case ate_kind_rtx:
4676 hstate.add_int (0);
4677 break;
4678 case ate_kind_rtx_dtprel:
4679 hstate.add_int (1);
4680 break;
4681 case ate_kind_label:
4682 return htab_hash_string (a->addr.label);
4683 default:
4684 gcc_unreachable ();
4686 inchash::add_rtx (a->addr.rtl, hstate);
4687 return hstate.end ();
4690 /* Determine equality for two address_table_entries. */
4692 bool
4693 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4695 if (a1->kind != a2->kind)
4696 return 0;
4697 switch (a1->kind)
4699 case ate_kind_rtx:
4700 case ate_kind_rtx_dtprel:
4701 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4702 case ate_kind_label:
4703 return strcmp (a1->addr.label, a2->addr.label) == 0;
4704 default:
4705 gcc_unreachable ();
4709 /* Initialize an addr_table_entry. */
4711 void
4712 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4714 e->kind = kind;
4715 switch (kind)
4717 case ate_kind_rtx:
4718 case ate_kind_rtx_dtprel:
4719 e->addr.rtl = (rtx) addr;
4720 break;
4721 case ate_kind_label:
4722 e->addr.label = (char *) addr;
4723 break;
4725 e->refcount = 0;
4726 e->index = NO_INDEX_ASSIGNED;
4729 /* Add attr to the address table entry to the table. Defer setting an
4730 index until output time. */
4732 static addr_table_entry *
4733 add_addr_table_entry (void *addr, enum ate_kind kind)
4735 addr_table_entry *node;
4736 addr_table_entry finder;
4738 gcc_assert (dwarf_split_debug_info);
4739 if (! addr_index_table)
4740 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4741 init_addr_table_entry (&finder, kind, addr);
4742 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4744 if (*slot == HTAB_EMPTY_ENTRY)
4746 node = ggc_cleared_alloc<addr_table_entry> ();
4747 init_addr_table_entry (node, kind, addr);
4748 *slot = node;
4750 else
4751 node = *slot;
4753 node->refcount++;
4754 return node;
4757 /* Remove an entry from the addr table by decrementing its refcount.
4758 Strictly, decrementing the refcount would be enough, but the
4759 assertion that the entry is actually in the table has found
4760 bugs. */
4762 static void
4763 remove_addr_table_entry (addr_table_entry *entry)
4765 gcc_assert (dwarf_split_debug_info && addr_index_table);
4766 /* After an index is assigned, the table is frozen. */
4767 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4768 entry->refcount--;
4771 /* Given a location list, remove all addresses it refers to from the
4772 address_table. */
4774 static void
4775 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4777 for (; descr; descr = descr->dw_loc_next)
4778 if (descr->dw_loc_oprnd1.val_entry != NULL)
4780 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4781 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4785 /* A helper function for dwarf2out_finish called through
4786 htab_traverse. Assign an addr_table_entry its index. All entries
4787 must be collected into the table when this function is called,
4788 because the indexing code relies on htab_traverse to traverse nodes
4789 in the same order for each run. */
4792 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4794 addr_table_entry *node = *h;
4796 /* Don't index unreferenced nodes. */
4797 if (node->refcount == 0)
4798 return 1;
4800 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4801 node->index = *index;
4802 *index += 1;
4804 return 1;
4807 /* Add an address constant attribute value to a DIE. When using
4808 dwarf_split_debug_info, address attributes in dies destined for the
4809 final executable should be direct references--setting the parameter
4810 force_direct ensures this behavior. */
4812 static inline void
4813 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4814 bool force_direct)
4816 dw_attr_node attr;
4818 attr.dw_attr = attr_kind;
4819 attr.dw_attr_val.val_class = dw_val_class_addr;
4820 attr.dw_attr_val.v.val_addr = addr;
4821 if (dwarf_split_debug_info && !force_direct)
4822 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4823 else
4824 attr.dw_attr_val.val_entry = NULL;
4825 add_dwarf_attr (die, &attr);
4828 /* Get the RTX from to an address DIE attribute. */
4830 static inline rtx
4831 AT_addr (dw_attr_node *a)
4833 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4834 return a->dw_attr_val.v.val_addr;
4837 /* Add a file attribute value to a DIE. */
4839 static inline void
4840 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4841 struct dwarf_file_data *fd)
4843 dw_attr_node attr;
4845 attr.dw_attr = attr_kind;
4846 attr.dw_attr_val.val_class = dw_val_class_file;
4847 attr.dw_attr_val.val_entry = NULL;
4848 attr.dw_attr_val.v.val_file = fd;
4849 add_dwarf_attr (die, &attr);
4852 /* Get the dwarf_file_data from a file DIE attribute. */
4854 static inline struct dwarf_file_data *
4855 AT_file (dw_attr_node *a)
4857 gcc_assert (a && (AT_class (a) == dw_val_class_file
4858 || AT_class (a) == dw_val_class_file_implicit));
4859 return a->dw_attr_val.v.val_file;
4862 /* Add a vms delta attribute value to a DIE. */
4864 static inline void
4865 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4866 const char *lbl1, const char *lbl2)
4868 dw_attr_node attr;
4870 attr.dw_attr = attr_kind;
4871 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4872 attr.dw_attr_val.val_entry = NULL;
4873 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4874 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4875 add_dwarf_attr (die, &attr);
4878 /* Add a label identifier attribute value to a DIE. */
4880 static inline void
4881 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4882 const char *lbl_id)
4884 dw_attr_node attr;
4886 attr.dw_attr = attr_kind;
4887 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4888 attr.dw_attr_val.val_entry = NULL;
4889 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4890 if (dwarf_split_debug_info)
4891 attr.dw_attr_val.val_entry
4892 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4893 ate_kind_label);
4894 add_dwarf_attr (die, &attr);
4897 /* Add a section offset attribute value to a DIE, an offset into the
4898 debug_line section. */
4900 static inline void
4901 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4902 const char *label)
4904 dw_attr_node attr;
4906 attr.dw_attr = attr_kind;
4907 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4908 attr.dw_attr_val.val_entry = NULL;
4909 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4910 add_dwarf_attr (die, &attr);
4913 /* Add a section offset attribute value to a DIE, an offset into the
4914 debug_loclists section. */
4916 static inline void
4917 add_AT_loclistsptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4918 const char *label)
4920 dw_attr_node attr;
4922 attr.dw_attr = attr_kind;
4923 attr.dw_attr_val.val_class = dw_val_class_loclistsptr;
4924 attr.dw_attr_val.val_entry = NULL;
4925 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4926 add_dwarf_attr (die, &attr);
4929 /* Add a section offset attribute value to a DIE, an offset into the
4930 debug_macinfo section. */
4932 static inline void
4933 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4934 const char *label)
4936 dw_attr_node attr;
4938 attr.dw_attr = attr_kind;
4939 attr.dw_attr_val.val_class = dw_val_class_macptr;
4940 attr.dw_attr_val.val_entry = NULL;
4941 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4942 add_dwarf_attr (die, &attr);
4945 /* Add an offset attribute value to a DIE. */
4947 static inline void
4948 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4949 unsigned HOST_WIDE_INT offset)
4951 dw_attr_node attr;
4953 attr.dw_attr = attr_kind;
4954 attr.dw_attr_val.val_class = dw_val_class_offset;
4955 attr.dw_attr_val.val_entry = NULL;
4956 attr.dw_attr_val.v.val_offset = offset;
4957 add_dwarf_attr (die, &attr);
4960 /* Add a range_list attribute value to a DIE. When using
4961 dwarf_split_debug_info, address attributes in dies destined for the
4962 final executable should be direct references--setting the parameter
4963 force_direct ensures this behavior. */
4965 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4966 #define RELOCATED_OFFSET (NULL)
4968 static void
4969 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4970 long unsigned int offset, bool force_direct)
4972 dw_attr_node attr;
4974 attr.dw_attr = attr_kind;
4975 attr.dw_attr_val.val_class = dw_val_class_range_list;
4976 /* For the range_list attribute, use val_entry to store whether the
4977 offset should follow split-debug-info or normal semantics. This
4978 value is read in output_range_list_offset. */
4979 if (dwarf_split_debug_info && !force_direct)
4980 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4981 else
4982 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4983 attr.dw_attr_val.v.val_offset = offset;
4984 add_dwarf_attr (die, &attr);
4987 /* Return the start label of a delta attribute. */
4989 static inline const char *
4990 AT_vms_delta1 (dw_attr_node *a)
4992 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4993 return a->dw_attr_val.v.val_vms_delta.lbl1;
4996 /* Return the end label of a delta attribute. */
4998 static inline const char *
4999 AT_vms_delta2 (dw_attr_node *a)
5001 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5002 return a->dw_attr_val.v.val_vms_delta.lbl2;
5005 static inline const char *
5006 AT_lbl (dw_attr_node *a)
5008 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5009 || AT_class (a) == dw_val_class_lineptr
5010 || AT_class (a) == dw_val_class_macptr
5011 || AT_class (a) == dw_val_class_loclistsptr
5012 || AT_class (a) == dw_val_class_high_pc));
5013 return a->dw_attr_val.v.val_lbl_id;
5016 /* Get the attribute of type attr_kind. */
5018 static dw_attr_node *
5019 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5021 dw_attr_node *a;
5022 unsigned ix;
5023 dw_die_ref spec = NULL;
5025 if (! die)
5026 return NULL;
5028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5029 if (a->dw_attr == attr_kind)
5030 return a;
5031 else if (a->dw_attr == DW_AT_specification
5032 || a->dw_attr == DW_AT_abstract_origin)
5033 spec = AT_ref (a);
5035 if (spec)
5036 return get_AT (spec, attr_kind);
5038 return NULL;
5041 /* Returns the parent of the declaration of DIE. */
5043 static dw_die_ref
5044 get_die_parent (dw_die_ref die)
5046 dw_die_ref t;
5048 if (!die)
5049 return NULL;
5051 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
5052 || (t = get_AT_ref (die, DW_AT_specification)))
5053 die = t;
5055 return die->die_parent;
5058 /* Return the "low pc" attribute value, typically associated with a subprogram
5059 DIE. Return null if the "low pc" attribute is either not present, or if it
5060 cannot be represented as an assembler label identifier. */
5062 static inline const char *
5063 get_AT_low_pc (dw_die_ref die)
5065 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
5067 return a ? AT_lbl (a) : NULL;
5070 /* Return the "high pc" attribute value, typically associated with a subprogram
5071 DIE. Return null if the "high pc" attribute is either not present, or if it
5072 cannot be represented as an assembler label identifier. */
5074 static inline const char *
5075 get_AT_hi_pc (dw_die_ref die)
5077 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
5079 return a ? AT_lbl (a) : NULL;
5082 /* Return the value of the string attribute designated by ATTR_KIND, or
5083 NULL if it is not present. */
5085 static inline const char *
5086 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5088 dw_attr_node *a = get_AT (die, attr_kind);
5090 return a ? AT_string (a) : NULL;
5093 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5094 if it is not present. */
5096 static inline int
5097 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5099 dw_attr_node *a = get_AT (die, attr_kind);
5101 return a ? AT_flag (a) : 0;
5104 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5105 if it is not present. */
5107 static inline unsigned
5108 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5110 dw_attr_node *a = get_AT (die, attr_kind);
5112 return a ? AT_unsigned (a) : 0;
5115 static inline dw_die_ref
5116 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5118 dw_attr_node *a = get_AT (die, attr_kind);
5120 return a ? AT_ref (a) : NULL;
5123 static inline struct dwarf_file_data *
5124 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5126 dw_attr_node *a = get_AT (die, attr_kind);
5128 return a ? AT_file (a) : NULL;
5131 /* Return TRUE if the language is C++. */
5133 static inline bool
5134 is_cxx (void)
5136 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5138 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
5139 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
5142 /* Return TRUE if DECL was created by the C++ frontend. */
5144 static bool
5145 is_cxx (const_tree decl)
5147 if (in_lto_p)
5149 const_tree context = get_ultimate_context (decl);
5150 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5151 return strncmp (TRANSLATION_UNIT_LANGUAGE (context), "GNU C++", 7) == 0;
5153 return is_cxx ();
5156 /* Return TRUE if the language is Fortran. */
5158 static inline bool
5159 is_fortran (void)
5161 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5163 return (lang == DW_LANG_Fortran77
5164 || lang == DW_LANG_Fortran90
5165 || lang == DW_LANG_Fortran95
5166 || lang == DW_LANG_Fortran03
5167 || lang == DW_LANG_Fortran08);
5170 static inline bool
5171 is_fortran (const_tree decl)
5173 if (in_lto_p)
5175 const_tree context = get_ultimate_context (decl);
5176 if (context && TRANSLATION_UNIT_LANGUAGE (context))
5177 return (strncmp (TRANSLATION_UNIT_LANGUAGE (context),
5178 "GNU Fortran", 11) == 0
5179 || strcmp (TRANSLATION_UNIT_LANGUAGE (context),
5180 "GNU F77") == 0);
5182 return is_fortran ();
5185 /* Return TRUE if the language is Ada. */
5187 static inline bool
5188 is_ada (void)
5190 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5192 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5195 /* Remove the specified attribute if present. Return TRUE if removal
5196 was successful. */
5198 static bool
5199 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5201 dw_attr_node *a;
5202 unsigned ix;
5204 if (! die)
5205 return false;
5207 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5208 if (a->dw_attr == attr_kind)
5210 if (AT_class (a) == dw_val_class_str)
5211 if (a->dw_attr_val.v.val_str->refcount)
5212 a->dw_attr_val.v.val_str->refcount--;
5214 /* vec::ordered_remove should help reduce the number of abbrevs
5215 that are needed. */
5216 die->die_attr->ordered_remove (ix);
5217 return true;
5219 return false;
5222 /* Remove CHILD from its parent. PREV must have the property that
5223 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
5225 static void
5226 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5228 gcc_assert (child->die_parent == prev->die_parent);
5229 gcc_assert (prev->die_sib == child);
5230 if (prev == child)
5232 gcc_assert (child->die_parent->die_child == child);
5233 prev = NULL;
5235 else
5236 prev->die_sib = child->die_sib;
5237 if (child->die_parent->die_child == child)
5238 child->die_parent->die_child = prev;
5239 child->die_sib = NULL;
5242 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
5243 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
5245 static void
5246 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5248 dw_die_ref parent = old_child->die_parent;
5250 gcc_assert (parent == prev->die_parent);
5251 gcc_assert (prev->die_sib == old_child);
5253 new_child->die_parent = parent;
5254 if (prev == old_child)
5256 gcc_assert (parent->die_child == old_child);
5257 new_child->die_sib = new_child;
5259 else
5261 prev->die_sib = new_child;
5262 new_child->die_sib = old_child->die_sib;
5264 if (old_child->die_parent->die_child == old_child)
5265 old_child->die_parent->die_child = new_child;
5266 old_child->die_sib = NULL;
5269 /* Move all children from OLD_PARENT to NEW_PARENT. */
5271 static void
5272 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5274 dw_die_ref c;
5275 new_parent->die_child = old_parent->die_child;
5276 old_parent->die_child = NULL;
5277 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5280 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
5281 matches TAG. */
5283 static void
5284 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5286 dw_die_ref c;
5288 c = die->die_child;
5289 if (c) do {
5290 dw_die_ref prev = c;
5291 c = c->die_sib;
5292 while (c->die_tag == tag)
5294 remove_child_with_prev (c, prev);
5295 c->die_parent = NULL;
5296 /* Might have removed every child. */
5297 if (die->die_child == NULL)
5298 return;
5299 c = prev->die_sib;
5301 } while (c != die->die_child);
5304 /* Add a CHILD_DIE as the last child of DIE. */
5306 static void
5307 add_child_die (dw_die_ref die, dw_die_ref child_die)
5309 /* FIXME this should probably be an assert. */
5310 if (! die || ! child_die)
5311 return;
5312 gcc_assert (die != child_die);
5314 child_die->die_parent = die;
5315 if (die->die_child)
5317 child_die->die_sib = die->die_child->die_sib;
5318 die->die_child->die_sib = child_die;
5320 else
5321 child_die->die_sib = child_die;
5322 die->die_child = child_die;
5325 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
5327 static void
5328 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
5329 dw_die_ref after_die)
5331 gcc_assert (die
5332 && child_die
5333 && after_die
5334 && die->die_child
5335 && die != child_die);
5337 child_die->die_parent = die;
5338 child_die->die_sib = after_die->die_sib;
5339 after_die->die_sib = child_die;
5340 if (die->die_child == after_die)
5341 die->die_child = child_die;
5344 /* Unassociate CHILD from its parent, and make its parent be
5345 NEW_PARENT. */
5347 static void
5348 reparent_child (dw_die_ref child, dw_die_ref new_parent)
5350 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
5351 if (p->die_sib == child)
5353 remove_child_with_prev (child, p);
5354 break;
5356 add_child_die (new_parent, child);
5359 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5360 is the specification, to the end of PARENT's list of children.
5361 This is done by removing and re-adding it. */
5363 static void
5364 splice_child_die (dw_die_ref parent, dw_die_ref child)
5366 /* We want the declaration DIE from inside the class, not the
5367 specification DIE at toplevel. */
5368 if (child->die_parent != parent)
5370 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5372 if (tmp)
5373 child = tmp;
5376 gcc_assert (child->die_parent == parent
5377 || (child->die_parent
5378 == get_AT_ref (parent, DW_AT_specification)));
5380 reparent_child (child, parent);
5383 /* Create and return a new die with TAG_VALUE as tag. */
5385 static inline dw_die_ref
5386 new_die_raw (enum dwarf_tag tag_value)
5388 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5389 die->die_tag = tag_value;
5390 return die;
5393 /* Create and return a new die with a parent of PARENT_DIE. If
5394 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5395 associated tree T must be supplied to determine parenthood
5396 later. */
5398 static inline dw_die_ref
5399 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5401 dw_die_ref die = new_die_raw (tag_value);
5403 if (parent_die != NULL)
5404 add_child_die (parent_die, die);
5405 else
5407 limbo_die_node *limbo_node;
5409 /* No DIEs created after early dwarf should end up in limbo,
5410 because the limbo list should not persist past LTO
5411 streaming. */
5412 if (tag_value != DW_TAG_compile_unit
5413 /* These are allowed because they're generated while
5414 breaking out COMDAT units late. */
5415 && tag_value != DW_TAG_type_unit
5416 && tag_value != DW_TAG_skeleton_unit
5417 && !early_dwarf
5418 /* Allow nested functions to live in limbo because they will
5419 only temporarily live there, as decls_for_scope will fix
5420 them up. */
5421 && (TREE_CODE (t) != FUNCTION_DECL
5422 || !decl_function_context (t))
5423 /* Same as nested functions above but for types. Types that
5424 are local to a function will be fixed in
5425 decls_for_scope. */
5426 && (!RECORD_OR_UNION_TYPE_P (t)
5427 || !TYPE_CONTEXT (t)
5428 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5429 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5430 especially in the ltrans stage, but once we implement LTO
5431 dwarf streaming, we should remove this exception. */
5432 && !in_lto_p)
5434 fprintf (stderr, "symbol ended up in limbo too late:");
5435 debug_generic_stmt (t);
5436 gcc_unreachable ();
5439 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5440 limbo_node->die = die;
5441 limbo_node->created_for = t;
5442 limbo_node->next = limbo_die_list;
5443 limbo_die_list = limbo_node;
5446 return die;
5449 /* Return the DIE associated with the given type specifier. */
5451 static inline dw_die_ref
5452 lookup_type_die (tree type)
5454 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5455 if (die && die->removed)
5457 TYPE_SYMTAB_DIE (type) = NULL;
5458 return NULL;
5460 return die;
5463 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5464 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5465 anonymous type instead the one of the naming typedef. */
5467 static inline dw_die_ref
5468 strip_naming_typedef (tree type, dw_die_ref type_die)
5470 if (type
5471 && TREE_CODE (type) == RECORD_TYPE
5472 && type_die
5473 && type_die->die_tag == DW_TAG_typedef
5474 && is_naming_typedef_decl (TYPE_NAME (type)))
5475 type_die = get_AT_ref (type_die, DW_AT_type);
5476 return type_die;
5479 /* Like lookup_type_die, but if type is an anonymous type named by a
5480 typedef[1], return the DIE of the anonymous type instead the one of
5481 the naming typedef. This is because in gen_typedef_die, we did
5482 equate the anonymous struct named by the typedef with the DIE of
5483 the naming typedef. So by default, lookup_type_die on an anonymous
5484 struct yields the DIE of the naming typedef.
5486 [1]: Read the comment of is_naming_typedef_decl to learn about what
5487 a naming typedef is. */
5489 static inline dw_die_ref
5490 lookup_type_die_strip_naming_typedef (tree type)
5492 dw_die_ref die = lookup_type_die (type);
5493 return strip_naming_typedef (type, die);
5496 /* Equate a DIE to a given type specifier. */
5498 static inline void
5499 equate_type_number_to_die (tree type, dw_die_ref type_die)
5501 TYPE_SYMTAB_DIE (type) = type_die;
5504 /* Returns a hash value for X (which really is a die_struct). */
5506 inline hashval_t
5507 decl_die_hasher::hash (die_node *x)
5509 return (hashval_t) x->decl_id;
5512 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5514 inline bool
5515 decl_die_hasher::equal (die_node *x, tree y)
5517 return (x->decl_id == DECL_UID (y));
5520 /* Return the DIE associated with a given declaration. */
5522 static inline dw_die_ref
5523 lookup_decl_die (tree decl)
5525 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5526 NO_INSERT);
5527 if (!die)
5528 return NULL;
5529 if ((*die)->removed)
5531 decl_die_table->clear_slot (die);
5532 return NULL;
5534 return *die;
5538 /* For DECL which might have early dwarf output query a SYMBOL + OFFSET
5539 style reference. Return true if we found one refering to a DIE for
5540 DECL, otherwise return false. */
5542 static bool
5543 dwarf2out_die_ref_for_decl (tree decl, const char **sym,
5544 unsigned HOST_WIDE_INT *off)
5546 dw_die_ref die;
5548 if (flag_wpa && !decl_die_table)
5549 return false;
5551 if (TREE_CODE (decl) == BLOCK)
5552 die = BLOCK_DIE (decl);
5553 else
5554 die = lookup_decl_die (decl);
5555 if (!die)
5556 return false;
5558 /* During WPA stage we currently use DIEs to store the
5559 decl <-> label + offset map. That's quite inefficient but it
5560 works for now. */
5561 if (flag_wpa)
5563 dw_die_ref ref = get_AT_ref (die, DW_AT_abstract_origin);
5564 if (!ref)
5566 gcc_assert (die == comp_unit_die ());
5567 return false;
5569 *off = ref->die_offset;
5570 *sym = ref->die_id.die_symbol;
5571 return true;
5574 /* Similar to get_ref_die_offset_label, but using the "correct"
5575 label. */
5576 *off = die->die_offset;
5577 while (die->die_parent)
5578 die = die->die_parent;
5579 /* For the containing CU DIE we compute a die_symbol in
5580 compute_comp_unit_symbol. */
5581 gcc_assert (die->die_tag == DW_TAG_compile_unit
5582 && die->die_id.die_symbol != NULL);
5583 *sym = die->die_id.die_symbol;
5584 return true;
5587 /* Add a reference of kind ATTR_KIND to a DIE at SYMBOL + OFFSET to DIE. */
5589 static void
5590 add_AT_external_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind,
5591 const char *symbol, HOST_WIDE_INT offset)
5593 /* Create a fake DIE that contains the reference. Don't use
5594 new_die because we don't want to end up in the limbo list. */
5595 dw_die_ref ref = new_die_raw (die->die_tag);
5596 ref->die_id.die_symbol = IDENTIFIER_POINTER (get_identifier (symbol));
5597 ref->die_offset = offset;
5598 ref->with_offset = 1;
5599 add_AT_die_ref (die, attr_kind, ref);
5602 /* Create a DIE for DECL if required and add a reference to a DIE
5603 at SYMBOL + OFFSET which contains attributes dumped early. */
5605 static void
5606 dwarf2out_register_external_die (tree decl, const char *sym,
5607 unsigned HOST_WIDE_INT off)
5609 if (debug_info_level == DINFO_LEVEL_NONE)
5610 return;
5612 if (flag_wpa && !decl_die_table)
5613 decl_die_table = hash_table<decl_die_hasher>::create_ggc (1000);
5615 dw_die_ref die
5616 = TREE_CODE (decl) == BLOCK ? BLOCK_DIE (decl) : lookup_decl_die (decl);
5617 gcc_assert (!die);
5619 tree ctx;
5620 dw_die_ref parent = NULL;
5621 /* Need to lookup a DIE for the decls context - the containing
5622 function or translation unit. */
5623 if (TREE_CODE (decl) == BLOCK)
5625 ctx = BLOCK_SUPERCONTEXT (decl);
5626 /* ??? We do not output DIEs for all scopes thus skip as
5627 many DIEs as needed. */
5628 while (TREE_CODE (ctx) == BLOCK
5629 && !BLOCK_DIE (ctx))
5630 ctx = BLOCK_SUPERCONTEXT (ctx);
5632 else
5633 ctx = DECL_CONTEXT (decl);
5634 while (ctx && TYPE_P (ctx))
5635 ctx = TYPE_CONTEXT (ctx);
5636 if (ctx)
5638 if (TREE_CODE (ctx) == BLOCK)
5639 parent = BLOCK_DIE (ctx);
5640 else if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
5641 /* Keep the 1:1 association during WPA. */
5642 && !flag_wpa)
5643 /* Otherwise all late annotations go to the main CU which
5644 imports the original CUs. */
5645 parent = comp_unit_die ();
5646 else if (TREE_CODE (ctx) == FUNCTION_DECL
5647 && TREE_CODE (decl) != PARM_DECL
5648 && TREE_CODE (decl) != BLOCK)
5649 /* Leave function local entities parent determination to when
5650 we process scope vars. */
5652 else
5653 parent = lookup_decl_die (ctx);
5655 else
5656 /* In some cases the FEs fail to set DECL_CONTEXT properly.
5657 Handle this case gracefully by globalizing stuff. */
5658 parent = comp_unit_die ();
5659 /* Create a DIE "stub". */
5660 switch (TREE_CODE (decl))
5662 case TRANSLATION_UNIT_DECL:
5663 if (! flag_wpa)
5665 die = comp_unit_die ();
5666 dw_die_ref import = new_die (DW_TAG_imported_unit, die, NULL_TREE);
5667 add_AT_external_die_ref (import, DW_AT_import, sym, off);
5668 /* We re-target all CU decls to the LTRANS CU DIE, so no need
5669 to create a DIE for the original CUs. */
5670 return;
5672 /* Keep the 1:1 association during WPA. */
5673 die = new_die (DW_TAG_compile_unit, NULL, decl);
5674 break;
5675 case NAMESPACE_DECL:
5676 if (is_fortran (decl))
5677 die = new_die (DW_TAG_module, parent, decl);
5678 else
5679 die = new_die (DW_TAG_namespace, parent, decl);
5680 break;
5681 case FUNCTION_DECL:
5682 die = new_die (DW_TAG_subprogram, parent, decl);
5683 break;
5684 case VAR_DECL:
5685 die = new_die (DW_TAG_variable, parent, decl);
5686 break;
5687 case RESULT_DECL:
5688 die = new_die (DW_TAG_variable, parent, decl);
5689 break;
5690 case PARM_DECL:
5691 die = new_die (DW_TAG_formal_parameter, parent, decl);
5692 break;
5693 case CONST_DECL:
5694 die = new_die (DW_TAG_constant, parent, decl);
5695 break;
5696 case LABEL_DECL:
5697 die = new_die (DW_TAG_label, parent, decl);
5698 break;
5699 case BLOCK:
5700 die = new_die (DW_TAG_lexical_block, parent, decl);
5701 break;
5702 default:
5703 gcc_unreachable ();
5705 if (TREE_CODE (decl) == BLOCK)
5706 BLOCK_DIE (decl) = die;
5707 else
5708 equate_decl_number_to_die (decl, die);
5710 /* Add a reference to the DIE providing early debug at $sym + off. */
5711 add_AT_external_die_ref (die, DW_AT_abstract_origin, sym, off);
5714 /* Returns a hash value for X (which really is a var_loc_list). */
5716 inline hashval_t
5717 decl_loc_hasher::hash (var_loc_list *x)
5719 return (hashval_t) x->decl_id;
5722 /* Return nonzero if decl_id of var_loc_list X is the same as
5723 UID of decl *Y. */
5725 inline bool
5726 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5728 return (x->decl_id == DECL_UID (y));
5731 /* Return the var_loc list associated with a given declaration. */
5733 static inline var_loc_list *
5734 lookup_decl_loc (const_tree decl)
5736 if (!decl_loc_table)
5737 return NULL;
5738 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5741 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5743 inline hashval_t
5744 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5746 return (hashval_t) x->decl_id;
5749 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5750 UID of decl *Y. */
5752 inline bool
5753 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5755 return (x->decl_id == DECL_UID (y));
5758 /* Equate a DIE to a particular declaration. */
5760 static void
5761 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5763 unsigned int decl_id = DECL_UID (decl);
5765 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5766 decl_die->decl_id = decl_id;
5769 /* Return how many bits covers PIECE EXPR_LIST. */
5771 static HOST_WIDE_INT
5772 decl_piece_bitsize (rtx piece)
5774 int ret = (int) GET_MODE (piece);
5775 if (ret)
5776 return ret;
5777 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5778 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5779 return INTVAL (XEXP (XEXP (piece, 0), 0));
5782 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5784 static rtx *
5785 decl_piece_varloc_ptr (rtx piece)
5787 if ((int) GET_MODE (piece))
5788 return &XEXP (piece, 0);
5789 else
5790 return &XEXP (XEXP (piece, 0), 1);
5793 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5794 Next is the chain of following piece nodes. */
5796 static rtx_expr_list *
5797 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5799 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5800 return alloc_EXPR_LIST (bitsize, loc_note, next);
5801 else
5802 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5803 GEN_INT (bitsize),
5804 loc_note), next);
5807 /* Return rtx that should be stored into loc field for
5808 LOC_NOTE and BITPOS/BITSIZE. */
5810 static rtx
5811 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5812 HOST_WIDE_INT bitsize)
5814 if (bitsize != -1)
5816 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5817 if (bitpos != 0)
5818 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5820 return loc_note;
5823 /* This function either modifies location piece list *DEST in
5824 place (if SRC and INNER is NULL), or copies location piece list
5825 *SRC to *DEST while modifying it. Location BITPOS is modified
5826 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5827 not copied and if needed some padding around it is added.
5828 When modifying in place, DEST should point to EXPR_LIST where
5829 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5830 to the start of the whole list and INNER points to the EXPR_LIST
5831 where earlier pieces cover PIECE_BITPOS bits. */
5833 static void
5834 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5835 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5836 HOST_WIDE_INT bitsize, rtx loc_note)
5838 HOST_WIDE_INT diff;
5839 bool copy = inner != NULL;
5841 if (copy)
5843 /* First copy all nodes preceding the current bitpos. */
5844 while (src != inner)
5846 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5847 decl_piece_bitsize (*src), NULL_RTX);
5848 dest = &XEXP (*dest, 1);
5849 src = &XEXP (*src, 1);
5852 /* Add padding if needed. */
5853 if (bitpos != piece_bitpos)
5855 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5856 copy ? NULL_RTX : *dest);
5857 dest = &XEXP (*dest, 1);
5859 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5861 gcc_assert (!copy);
5862 /* A piece with correct bitpos and bitsize already exist,
5863 just update the location for it and return. */
5864 *decl_piece_varloc_ptr (*dest) = loc_note;
5865 return;
5867 /* Add the piece that changed. */
5868 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5869 dest = &XEXP (*dest, 1);
5870 /* Skip over pieces that overlap it. */
5871 diff = bitpos - piece_bitpos + bitsize;
5872 if (!copy)
5873 src = dest;
5874 while (diff > 0 && *src)
5876 rtx piece = *src;
5877 diff -= decl_piece_bitsize (piece);
5878 if (copy)
5879 src = &XEXP (piece, 1);
5880 else
5882 *src = XEXP (piece, 1);
5883 free_EXPR_LIST_node (piece);
5886 /* Add padding if needed. */
5887 if (diff < 0 && *src)
5889 if (!copy)
5890 dest = src;
5891 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5892 dest = &XEXP (*dest, 1);
5894 if (!copy)
5895 return;
5896 /* Finally copy all nodes following it. */
5897 while (*src)
5899 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5900 decl_piece_bitsize (*src), NULL_RTX);
5901 dest = &XEXP (*dest, 1);
5902 src = &XEXP (*src, 1);
5906 /* Add a variable location node to the linked list for DECL. */
5908 static struct var_loc_node *
5909 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5911 unsigned int decl_id;
5912 var_loc_list *temp;
5913 struct var_loc_node *loc = NULL;
5914 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5916 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5918 tree realdecl = DECL_DEBUG_EXPR (decl);
5919 if (handled_component_p (realdecl)
5920 || (TREE_CODE (realdecl) == MEM_REF
5921 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5923 bool reverse;
5924 tree innerdecl = get_ref_base_and_extent_hwi (realdecl, &bitpos,
5925 &bitsize, &reverse);
5926 if (!innerdecl
5927 || !DECL_P (innerdecl)
5928 || DECL_IGNORED_P (innerdecl)
5929 || TREE_STATIC (innerdecl)
5930 || bitsize == 0
5931 || bitpos + bitsize > 256)
5932 return NULL;
5933 decl = innerdecl;
5937 decl_id = DECL_UID (decl);
5938 var_loc_list **slot
5939 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5940 if (*slot == NULL)
5942 temp = ggc_cleared_alloc<var_loc_list> ();
5943 temp->decl_id = decl_id;
5944 *slot = temp;
5946 else
5947 temp = *slot;
5949 /* For PARM_DECLs try to keep around the original incoming value,
5950 even if that means we'll emit a zero-range .debug_loc entry. */
5951 if (temp->last
5952 && temp->first == temp->last
5953 && TREE_CODE (decl) == PARM_DECL
5954 && NOTE_P (temp->first->loc)
5955 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5956 && DECL_INCOMING_RTL (decl)
5957 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5958 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5959 == GET_CODE (DECL_INCOMING_RTL (decl))
5960 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5961 && (bitsize != -1
5962 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5963 NOTE_VAR_LOCATION_LOC (loc_note))
5964 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5965 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5967 loc = ggc_cleared_alloc<var_loc_node> ();
5968 temp->first->next = loc;
5969 temp->last = loc;
5970 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5972 else if (temp->last)
5974 struct var_loc_node *last = temp->last, *unused = NULL;
5975 rtx *piece_loc = NULL, last_loc_note;
5976 HOST_WIDE_INT piece_bitpos = 0;
5977 if (last->next)
5979 last = last->next;
5980 gcc_assert (last->next == NULL);
5982 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5984 piece_loc = &last->loc;
5987 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5988 if (piece_bitpos + cur_bitsize > bitpos)
5989 break;
5990 piece_bitpos += cur_bitsize;
5991 piece_loc = &XEXP (*piece_loc, 1);
5993 while (*piece_loc);
5995 /* TEMP->LAST here is either pointer to the last but one or
5996 last element in the chained list, LAST is pointer to the
5997 last element. */
5998 if (label && strcmp (last->label, label) == 0)
6000 /* For SRA optimized variables if there weren't any real
6001 insns since last note, just modify the last node. */
6002 if (piece_loc != NULL)
6004 adjust_piece_list (piece_loc, NULL, NULL,
6005 bitpos, piece_bitpos, bitsize, loc_note);
6006 return NULL;
6008 /* If the last note doesn't cover any instructions, remove it. */
6009 if (temp->last != last)
6011 temp->last->next = NULL;
6012 unused = last;
6013 last = temp->last;
6014 gcc_assert (strcmp (last->label, label) != 0);
6016 else
6018 gcc_assert (temp->first == temp->last
6019 || (temp->first->next == temp->last
6020 && TREE_CODE (decl) == PARM_DECL));
6021 memset (temp->last, '\0', sizeof (*temp->last));
6022 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6023 return temp->last;
6026 if (bitsize == -1 && NOTE_P (last->loc))
6027 last_loc_note = last->loc;
6028 else if (piece_loc != NULL
6029 && *piece_loc != NULL_RTX
6030 && piece_bitpos == bitpos
6031 && decl_piece_bitsize (*piece_loc) == bitsize)
6032 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6033 else
6034 last_loc_note = NULL_RTX;
6035 /* If the current location is the same as the end of the list,
6036 and either both or neither of the locations is uninitialized,
6037 we have nothing to do. */
6038 if (last_loc_note == NULL_RTX
6039 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6040 NOTE_VAR_LOCATION_LOC (loc_note)))
6041 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6042 != NOTE_VAR_LOCATION_STATUS (loc_note))
6043 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6044 == VAR_INIT_STATUS_UNINITIALIZED)
6045 || (NOTE_VAR_LOCATION_STATUS (loc_note)
6046 == VAR_INIT_STATUS_UNINITIALIZED))))
6048 /* Add LOC to the end of list and update LAST. If the last
6049 element of the list has been removed above, reuse its
6050 memory for the new node, otherwise allocate a new one. */
6051 if (unused)
6053 loc = unused;
6054 memset (loc, '\0', sizeof (*loc));
6056 else
6057 loc = ggc_cleared_alloc<var_loc_node> ();
6058 if (bitsize == -1 || piece_loc == NULL)
6059 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6060 else
6061 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6062 bitpos, piece_bitpos, bitsize, loc_note);
6063 last->next = loc;
6064 /* Ensure TEMP->LAST will point either to the new last but one
6065 element of the chain, or to the last element in it. */
6066 if (last != temp->last)
6067 temp->last = last;
6069 else if (unused)
6070 ggc_free (unused);
6072 else
6074 loc = ggc_cleared_alloc<var_loc_node> ();
6075 temp->first = loc;
6076 temp->last = loc;
6077 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6079 return loc;
6082 /* Keep track of the number of spaces used to indent the
6083 output of the debugging routines that print the structure of
6084 the DIE internal representation. */
6085 static int print_indent;
6087 /* Indent the line the number of spaces given by print_indent. */
6089 static inline void
6090 print_spaces (FILE *outfile)
6092 fprintf (outfile, "%*s", print_indent, "");
6095 /* Print a type signature in hex. */
6097 static inline void
6098 print_signature (FILE *outfile, char *sig)
6100 int i;
6102 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6103 fprintf (outfile, "%02x", sig[i] & 0xff);
6106 static inline void
6107 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
6109 if (discr_value->pos)
6110 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
6111 else
6112 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
6115 static void print_loc_descr (dw_loc_descr_ref, FILE *);
6117 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
6118 RECURSE, output location descriptor operations. */
6120 static void
6121 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
6123 switch (val->val_class)
6125 case dw_val_class_addr:
6126 fprintf (outfile, "address");
6127 break;
6128 case dw_val_class_offset:
6129 fprintf (outfile, "offset");
6130 break;
6131 case dw_val_class_loc:
6132 fprintf (outfile, "location descriptor");
6133 if (val->v.val_loc == NULL)
6134 fprintf (outfile, " -> <null>\n");
6135 else if (recurse)
6137 fprintf (outfile, ":\n");
6138 print_indent += 4;
6139 print_loc_descr (val->v.val_loc, outfile);
6140 print_indent -= 4;
6142 else
6143 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
6144 break;
6145 case dw_val_class_loc_list:
6146 fprintf (outfile, "location list -> label:%s",
6147 val->v.val_loc_list->ll_symbol);
6148 break;
6149 case dw_val_class_range_list:
6150 fprintf (outfile, "range list");
6151 break;
6152 case dw_val_class_const:
6153 case dw_val_class_const_implicit:
6154 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
6155 break;
6156 case dw_val_class_unsigned_const:
6157 case dw_val_class_unsigned_const_implicit:
6158 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
6159 break;
6160 case dw_val_class_const_double:
6161 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
6162 HOST_WIDE_INT_PRINT_UNSIGNED")",
6163 val->v.val_double.high,
6164 val->v.val_double.low);
6165 break;
6166 case dw_val_class_wide_int:
6168 int i = val->v.val_wide->get_len ();
6169 fprintf (outfile, "constant (");
6170 gcc_assert (i > 0);
6171 if (val->v.val_wide->elt (i - 1) == 0)
6172 fprintf (outfile, "0x");
6173 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
6174 val->v.val_wide->elt (--i));
6175 while (--i >= 0)
6176 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
6177 val->v.val_wide->elt (i));
6178 fprintf (outfile, ")");
6179 break;
6181 case dw_val_class_vec:
6182 fprintf (outfile, "floating-point or vector constant");
6183 break;
6184 case dw_val_class_flag:
6185 fprintf (outfile, "%u", val->v.val_flag);
6186 break;
6187 case dw_val_class_die_ref:
6188 if (val->v.val_die_ref.die != NULL)
6190 dw_die_ref die = val->v.val_die_ref.die;
6192 if (die->comdat_type_p)
6194 fprintf (outfile, "die -> signature: ");
6195 print_signature (outfile,
6196 die->die_id.die_type_node->signature);
6198 else if (die->die_id.die_symbol)
6200 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
6201 if (die->with_offset)
6202 fprintf (outfile, " + %ld", die->die_offset);
6204 else
6205 fprintf (outfile, "die -> %ld", die->die_offset);
6206 fprintf (outfile, " (%p)", (void *) die);
6208 else
6209 fprintf (outfile, "die -> <null>");
6210 break;
6211 case dw_val_class_vms_delta:
6212 fprintf (outfile, "delta: @slotcount(%s-%s)",
6213 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
6214 break;
6215 case dw_val_class_lbl_id:
6216 case dw_val_class_lineptr:
6217 case dw_val_class_macptr:
6218 case dw_val_class_loclistsptr:
6219 case dw_val_class_high_pc:
6220 fprintf (outfile, "label: %s", val->v.val_lbl_id);
6221 break;
6222 case dw_val_class_str:
6223 if (val->v.val_str->str != NULL)
6224 fprintf (outfile, "\"%s\"", val->v.val_str->str);
6225 else
6226 fprintf (outfile, "<null>");
6227 break;
6228 case dw_val_class_file:
6229 case dw_val_class_file_implicit:
6230 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
6231 val->v.val_file->emitted_number);
6232 break;
6233 case dw_val_class_data8:
6235 int i;
6237 for (i = 0; i < 8; i++)
6238 fprintf (outfile, "%02x", val->v.val_data8[i]);
6239 break;
6241 case dw_val_class_discr_value:
6242 print_discr_value (outfile, &val->v.val_discr_value);
6243 break;
6244 case dw_val_class_discr_list:
6245 for (dw_discr_list_ref node = val->v.val_discr_list;
6246 node != NULL;
6247 node = node->dw_discr_next)
6249 if (node->dw_discr_range)
6251 fprintf (outfile, " .. ");
6252 print_discr_value (outfile, &node->dw_discr_lower_bound);
6253 print_discr_value (outfile, &node->dw_discr_upper_bound);
6255 else
6256 print_discr_value (outfile, &node->dw_discr_lower_bound);
6258 if (node->dw_discr_next != NULL)
6259 fprintf (outfile, " | ");
6261 default:
6262 break;
6266 /* Likewise, for a DIE attribute. */
6268 static void
6269 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
6271 print_dw_val (&a->dw_attr_val, recurse, outfile);
6275 /* Print the list of operands in the LOC location description to OUTFILE. This
6276 routine is a debugging aid only. */
6278 static void
6279 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
6281 dw_loc_descr_ref l = loc;
6283 if (loc == NULL)
6285 print_spaces (outfile);
6286 fprintf (outfile, "<null>\n");
6287 return;
6290 for (l = loc; l != NULL; l = l->dw_loc_next)
6292 print_spaces (outfile);
6293 fprintf (outfile, "(%p) %s",
6294 (void *) l,
6295 dwarf_stack_op_name (l->dw_loc_opc));
6296 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
6298 fprintf (outfile, " ");
6299 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
6301 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
6303 fprintf (outfile, ", ");
6304 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
6306 fprintf (outfile, "\n");
6310 /* Print the information associated with a given DIE, and its children.
6311 This routine is a debugging aid only. */
6313 static void
6314 print_die (dw_die_ref die, FILE *outfile)
6316 dw_attr_node *a;
6317 dw_die_ref c;
6318 unsigned ix;
6320 print_spaces (outfile);
6321 fprintf (outfile, "DIE %4ld: %s (%p)\n",
6322 die->die_offset, dwarf_tag_name (die->die_tag),
6323 (void*) die);
6324 print_spaces (outfile);
6325 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
6326 fprintf (outfile, " offset: %ld", die->die_offset);
6327 fprintf (outfile, " mark: %d\n", die->die_mark);
6329 if (die->comdat_type_p)
6331 print_spaces (outfile);
6332 fprintf (outfile, " signature: ");
6333 print_signature (outfile, die->die_id.die_type_node->signature);
6334 fprintf (outfile, "\n");
6337 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6339 print_spaces (outfile);
6340 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
6342 print_attribute (a, true, outfile);
6343 fprintf (outfile, "\n");
6346 if (die->die_child != NULL)
6348 print_indent += 4;
6349 FOR_EACH_CHILD (die, c, print_die (c, outfile));
6350 print_indent -= 4;
6352 if (print_indent == 0)
6353 fprintf (outfile, "\n");
6356 /* Print the list of operations in the LOC location description. */
6358 DEBUG_FUNCTION void
6359 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
6361 print_loc_descr (loc, stderr);
6364 /* Print the information collected for a given DIE. */
6366 DEBUG_FUNCTION void
6367 debug_dwarf_die (dw_die_ref die)
6369 print_die (die, stderr);
6372 DEBUG_FUNCTION void
6373 debug (die_struct &ref)
6375 print_die (&ref, stderr);
6378 DEBUG_FUNCTION void
6379 debug (die_struct *ptr)
6381 if (ptr)
6382 debug (*ptr);
6383 else
6384 fprintf (stderr, "<nil>\n");
6388 /* Print all DWARF information collected for the compilation unit.
6389 This routine is a debugging aid only. */
6391 DEBUG_FUNCTION void
6392 debug_dwarf (void)
6394 print_indent = 0;
6395 print_die (comp_unit_die (), stderr);
6398 /* Verify the DIE tree structure. */
6400 DEBUG_FUNCTION void
6401 verify_die (dw_die_ref die)
6403 gcc_assert (!die->die_mark);
6404 if (die->die_parent == NULL
6405 && die->die_sib == NULL)
6406 return;
6407 /* Verify the die_sib list is cyclic. */
6408 dw_die_ref x = die;
6411 x->die_mark = 1;
6412 x = x->die_sib;
6414 while (x && !x->die_mark);
6415 gcc_assert (x == die);
6416 x = die;
6419 /* Verify all dies have the same parent. */
6420 gcc_assert (x->die_parent == die->die_parent);
6421 if (x->die_child)
6423 /* Verify the child has the proper parent and recurse. */
6424 gcc_assert (x->die_child->die_parent == x);
6425 verify_die (x->die_child);
6427 x->die_mark = 0;
6428 x = x->die_sib;
6430 while (x && x->die_mark);
6433 /* Sanity checks on DIEs. */
6435 static void
6436 check_die (dw_die_ref die)
6438 unsigned ix;
6439 dw_attr_node *a;
6440 bool inline_found = false;
6441 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
6442 int n_decl_line = 0, n_decl_column = 0, n_decl_file = 0;
6443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6445 switch (a->dw_attr)
6447 case DW_AT_inline:
6448 if (a->dw_attr_val.v.val_unsigned)
6449 inline_found = true;
6450 break;
6451 case DW_AT_location:
6452 ++n_location;
6453 break;
6454 case DW_AT_low_pc:
6455 ++n_low_pc;
6456 break;
6457 case DW_AT_high_pc:
6458 ++n_high_pc;
6459 break;
6460 case DW_AT_artificial:
6461 ++n_artificial;
6462 break;
6463 case DW_AT_decl_column:
6464 ++n_decl_column;
6465 break;
6466 case DW_AT_decl_line:
6467 ++n_decl_line;
6468 break;
6469 case DW_AT_decl_file:
6470 ++n_decl_file;
6471 break;
6472 default:
6473 break;
6476 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
6477 || n_decl_column > 1 || n_decl_line > 1 || n_decl_file > 1)
6479 fprintf (stderr, "Duplicate attributes in DIE:\n");
6480 debug_dwarf_die (die);
6481 gcc_unreachable ();
6483 if (inline_found)
6485 /* A debugging information entry that is a member of an abstract
6486 instance tree [that has DW_AT_inline] should not contain any
6487 attributes which describe aspects of the subroutine which vary
6488 between distinct inlined expansions or distinct out-of-line
6489 expansions. */
6490 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6491 gcc_assert (a->dw_attr != DW_AT_low_pc
6492 && a->dw_attr != DW_AT_high_pc
6493 && a->dw_attr != DW_AT_location
6494 && a->dw_attr != DW_AT_frame_base
6495 && a->dw_attr != DW_AT_call_all_calls
6496 && a->dw_attr != DW_AT_GNU_all_call_sites);
6500 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6501 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6502 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6504 /* Calculate the checksum of a location expression. */
6506 static inline void
6507 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6509 int tem;
6510 inchash::hash hstate;
6511 hashval_t hash;
6513 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6514 CHECKSUM (tem);
6515 hash_loc_operands (loc, hstate);
6516 hash = hstate.end();
6517 CHECKSUM (hash);
6520 /* Calculate the checksum of an attribute. */
6522 static void
6523 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
6525 dw_loc_descr_ref loc;
6526 rtx r;
6528 CHECKSUM (at->dw_attr);
6530 /* We don't care that this was compiled with a different compiler
6531 snapshot; if the output is the same, that's what matters. */
6532 if (at->dw_attr == DW_AT_producer)
6533 return;
6535 switch (AT_class (at))
6537 case dw_val_class_const:
6538 case dw_val_class_const_implicit:
6539 CHECKSUM (at->dw_attr_val.v.val_int);
6540 break;
6541 case dw_val_class_unsigned_const:
6542 case dw_val_class_unsigned_const_implicit:
6543 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6544 break;
6545 case dw_val_class_const_double:
6546 CHECKSUM (at->dw_attr_val.v.val_double);
6547 break;
6548 case dw_val_class_wide_int:
6549 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6550 get_full_len (*at->dw_attr_val.v.val_wide)
6551 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6552 break;
6553 case dw_val_class_vec:
6554 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6555 (at->dw_attr_val.v.val_vec.length
6556 * at->dw_attr_val.v.val_vec.elt_size));
6557 break;
6558 case dw_val_class_flag:
6559 CHECKSUM (at->dw_attr_val.v.val_flag);
6560 break;
6561 case dw_val_class_str:
6562 CHECKSUM_STRING (AT_string (at));
6563 break;
6565 case dw_val_class_addr:
6566 r = AT_addr (at);
6567 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6568 CHECKSUM_STRING (XSTR (r, 0));
6569 break;
6571 case dw_val_class_offset:
6572 CHECKSUM (at->dw_attr_val.v.val_offset);
6573 break;
6575 case dw_val_class_loc:
6576 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6577 loc_checksum (loc, ctx);
6578 break;
6580 case dw_val_class_die_ref:
6581 die_checksum (AT_ref (at), ctx, mark);
6582 break;
6584 case dw_val_class_fde_ref:
6585 case dw_val_class_vms_delta:
6586 case dw_val_class_lbl_id:
6587 case dw_val_class_lineptr:
6588 case dw_val_class_macptr:
6589 case dw_val_class_loclistsptr:
6590 case dw_val_class_high_pc:
6591 break;
6593 case dw_val_class_file:
6594 case dw_val_class_file_implicit:
6595 CHECKSUM_STRING (AT_file (at)->filename);
6596 break;
6598 case dw_val_class_data8:
6599 CHECKSUM (at->dw_attr_val.v.val_data8);
6600 break;
6602 default:
6603 break;
6607 /* Calculate the checksum of a DIE. */
6609 static void
6610 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6612 dw_die_ref c;
6613 dw_attr_node *a;
6614 unsigned ix;
6616 /* To avoid infinite recursion. */
6617 if (die->die_mark)
6619 CHECKSUM (die->die_mark);
6620 return;
6622 die->die_mark = ++(*mark);
6624 CHECKSUM (die->die_tag);
6626 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6627 attr_checksum (a, ctx, mark);
6629 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6632 #undef CHECKSUM
6633 #undef CHECKSUM_BLOCK
6634 #undef CHECKSUM_STRING
6636 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6637 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6638 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6639 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6640 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6641 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6642 #define CHECKSUM_ATTR(FOO) \
6643 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6645 /* Calculate the checksum of a number in signed LEB128 format. */
6647 static void
6648 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6650 unsigned char byte;
6651 bool more;
6653 while (1)
6655 byte = (value & 0x7f);
6656 value >>= 7;
6657 more = !((value == 0 && (byte & 0x40) == 0)
6658 || (value == -1 && (byte & 0x40) != 0));
6659 if (more)
6660 byte |= 0x80;
6661 CHECKSUM (byte);
6662 if (!more)
6663 break;
6667 /* Calculate the checksum of a number in unsigned LEB128 format. */
6669 static void
6670 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6672 while (1)
6674 unsigned char byte = (value & 0x7f);
6675 value >>= 7;
6676 if (value != 0)
6677 /* More bytes to follow. */
6678 byte |= 0x80;
6679 CHECKSUM (byte);
6680 if (value == 0)
6681 break;
6685 /* Checksum the context of the DIE. This adds the names of any
6686 surrounding namespaces or structures to the checksum. */
6688 static void
6689 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6691 const char *name;
6692 dw_die_ref spec;
6693 int tag = die->die_tag;
6695 if (tag != DW_TAG_namespace
6696 && tag != DW_TAG_structure_type
6697 && tag != DW_TAG_class_type)
6698 return;
6700 name = get_AT_string (die, DW_AT_name);
6702 spec = get_AT_ref (die, DW_AT_specification);
6703 if (spec != NULL)
6704 die = spec;
6706 if (die->die_parent != NULL)
6707 checksum_die_context (die->die_parent, ctx);
6709 CHECKSUM_ULEB128 ('C');
6710 CHECKSUM_ULEB128 (tag);
6711 if (name != NULL)
6712 CHECKSUM_STRING (name);
6715 /* Calculate the checksum of a location expression. */
6717 static inline void
6718 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6720 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6721 were emitted as a DW_FORM_sdata instead of a location expression. */
6722 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6724 CHECKSUM_ULEB128 (DW_FORM_sdata);
6725 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6726 return;
6729 /* Otherwise, just checksum the raw location expression. */
6730 while (loc != NULL)
6732 inchash::hash hstate;
6733 hashval_t hash;
6735 CHECKSUM_ULEB128 (loc->dtprel);
6736 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6737 hash_loc_operands (loc, hstate);
6738 hash = hstate.end ();
6739 CHECKSUM (hash);
6740 loc = loc->dw_loc_next;
6744 /* Calculate the checksum of an attribute. */
6746 static void
6747 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6748 struct md5_ctx *ctx, int *mark)
6750 dw_loc_descr_ref loc;
6751 rtx r;
6753 if (AT_class (at) == dw_val_class_die_ref)
6755 dw_die_ref target_die = AT_ref (at);
6757 /* For pointer and reference types, we checksum only the (qualified)
6758 name of the target type (if there is a name). For friend entries,
6759 we checksum only the (qualified) name of the target type or function.
6760 This allows the checksum to remain the same whether the target type
6761 is complete or not. */
6762 if ((at->dw_attr == DW_AT_type
6763 && (tag == DW_TAG_pointer_type
6764 || tag == DW_TAG_reference_type
6765 || tag == DW_TAG_rvalue_reference_type
6766 || tag == DW_TAG_ptr_to_member_type))
6767 || (at->dw_attr == DW_AT_friend
6768 && tag == DW_TAG_friend))
6770 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6772 if (name_attr != NULL)
6774 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6776 if (decl == NULL)
6777 decl = target_die;
6778 CHECKSUM_ULEB128 ('N');
6779 CHECKSUM_ULEB128 (at->dw_attr);
6780 if (decl->die_parent != NULL)
6781 checksum_die_context (decl->die_parent, ctx);
6782 CHECKSUM_ULEB128 ('E');
6783 CHECKSUM_STRING (AT_string (name_attr));
6784 return;
6788 /* For all other references to another DIE, we check to see if the
6789 target DIE has already been visited. If it has, we emit a
6790 backward reference; if not, we descend recursively. */
6791 if (target_die->die_mark > 0)
6793 CHECKSUM_ULEB128 ('R');
6794 CHECKSUM_ULEB128 (at->dw_attr);
6795 CHECKSUM_ULEB128 (target_die->die_mark);
6797 else
6799 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6801 if (decl == NULL)
6802 decl = target_die;
6803 target_die->die_mark = ++(*mark);
6804 CHECKSUM_ULEB128 ('T');
6805 CHECKSUM_ULEB128 (at->dw_attr);
6806 if (decl->die_parent != NULL)
6807 checksum_die_context (decl->die_parent, ctx);
6808 die_checksum_ordered (target_die, ctx, mark);
6810 return;
6813 CHECKSUM_ULEB128 ('A');
6814 CHECKSUM_ULEB128 (at->dw_attr);
6816 switch (AT_class (at))
6818 case dw_val_class_const:
6819 case dw_val_class_const_implicit:
6820 CHECKSUM_ULEB128 (DW_FORM_sdata);
6821 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6822 break;
6824 case dw_val_class_unsigned_const:
6825 case dw_val_class_unsigned_const_implicit:
6826 CHECKSUM_ULEB128 (DW_FORM_sdata);
6827 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6828 break;
6830 case dw_val_class_const_double:
6831 CHECKSUM_ULEB128 (DW_FORM_block);
6832 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6833 CHECKSUM (at->dw_attr_val.v.val_double);
6834 break;
6836 case dw_val_class_wide_int:
6837 CHECKSUM_ULEB128 (DW_FORM_block);
6838 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6839 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
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;
6845 case dw_val_class_vec:
6846 CHECKSUM_ULEB128 (DW_FORM_block);
6847 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6848 * at->dw_attr_val.v.val_vec.elt_size);
6849 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6850 (at->dw_attr_val.v.val_vec.length
6851 * at->dw_attr_val.v.val_vec.elt_size));
6852 break;
6854 case dw_val_class_flag:
6855 CHECKSUM_ULEB128 (DW_FORM_flag);
6856 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6857 break;
6859 case dw_val_class_str:
6860 CHECKSUM_ULEB128 (DW_FORM_string);
6861 CHECKSUM_STRING (AT_string (at));
6862 break;
6864 case dw_val_class_addr:
6865 r = AT_addr (at);
6866 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6867 CHECKSUM_ULEB128 (DW_FORM_string);
6868 CHECKSUM_STRING (XSTR (r, 0));
6869 break;
6871 case dw_val_class_offset:
6872 CHECKSUM_ULEB128 (DW_FORM_sdata);
6873 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6874 break;
6876 case dw_val_class_loc:
6877 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6878 loc_checksum_ordered (loc, ctx);
6879 break;
6881 case dw_val_class_fde_ref:
6882 case dw_val_class_lbl_id:
6883 case dw_val_class_lineptr:
6884 case dw_val_class_macptr:
6885 case dw_val_class_loclistsptr:
6886 case dw_val_class_high_pc:
6887 break;
6889 case dw_val_class_file:
6890 case dw_val_class_file_implicit:
6891 CHECKSUM_ULEB128 (DW_FORM_string);
6892 CHECKSUM_STRING (AT_file (at)->filename);
6893 break;
6895 case dw_val_class_data8:
6896 CHECKSUM (at->dw_attr_val.v.val_data8);
6897 break;
6899 default:
6900 break;
6904 struct checksum_attributes
6906 dw_attr_node *at_name;
6907 dw_attr_node *at_type;
6908 dw_attr_node *at_friend;
6909 dw_attr_node *at_accessibility;
6910 dw_attr_node *at_address_class;
6911 dw_attr_node *at_alignment;
6912 dw_attr_node *at_allocated;
6913 dw_attr_node *at_artificial;
6914 dw_attr_node *at_associated;
6915 dw_attr_node *at_binary_scale;
6916 dw_attr_node *at_bit_offset;
6917 dw_attr_node *at_bit_size;
6918 dw_attr_node *at_bit_stride;
6919 dw_attr_node *at_byte_size;
6920 dw_attr_node *at_byte_stride;
6921 dw_attr_node *at_const_value;
6922 dw_attr_node *at_containing_type;
6923 dw_attr_node *at_count;
6924 dw_attr_node *at_data_location;
6925 dw_attr_node *at_data_member_location;
6926 dw_attr_node *at_decimal_scale;
6927 dw_attr_node *at_decimal_sign;
6928 dw_attr_node *at_default_value;
6929 dw_attr_node *at_digit_count;
6930 dw_attr_node *at_discr;
6931 dw_attr_node *at_discr_list;
6932 dw_attr_node *at_discr_value;
6933 dw_attr_node *at_encoding;
6934 dw_attr_node *at_endianity;
6935 dw_attr_node *at_explicit;
6936 dw_attr_node *at_is_optional;
6937 dw_attr_node *at_location;
6938 dw_attr_node *at_lower_bound;
6939 dw_attr_node *at_mutable;
6940 dw_attr_node *at_ordering;
6941 dw_attr_node *at_picture_string;
6942 dw_attr_node *at_prototyped;
6943 dw_attr_node *at_small;
6944 dw_attr_node *at_segment;
6945 dw_attr_node *at_string_length;
6946 dw_attr_node *at_string_length_bit_size;
6947 dw_attr_node *at_string_length_byte_size;
6948 dw_attr_node *at_threads_scaled;
6949 dw_attr_node *at_upper_bound;
6950 dw_attr_node *at_use_location;
6951 dw_attr_node *at_use_UTF8;
6952 dw_attr_node *at_variable_parameter;
6953 dw_attr_node *at_virtuality;
6954 dw_attr_node *at_visibility;
6955 dw_attr_node *at_vtable_elem_location;
6958 /* Collect the attributes that we will want to use for the checksum. */
6960 static void
6961 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6963 dw_attr_node *a;
6964 unsigned ix;
6966 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6968 switch (a->dw_attr)
6970 case DW_AT_name:
6971 attrs->at_name = a;
6972 break;
6973 case DW_AT_type:
6974 attrs->at_type = a;
6975 break;
6976 case DW_AT_friend:
6977 attrs->at_friend = a;
6978 break;
6979 case DW_AT_accessibility:
6980 attrs->at_accessibility = a;
6981 break;
6982 case DW_AT_address_class:
6983 attrs->at_address_class = a;
6984 break;
6985 case DW_AT_alignment:
6986 attrs->at_alignment = a;
6987 break;
6988 case DW_AT_allocated:
6989 attrs->at_allocated = a;
6990 break;
6991 case DW_AT_artificial:
6992 attrs->at_artificial = a;
6993 break;
6994 case DW_AT_associated:
6995 attrs->at_associated = a;
6996 break;
6997 case DW_AT_binary_scale:
6998 attrs->at_binary_scale = a;
6999 break;
7000 case DW_AT_bit_offset:
7001 attrs->at_bit_offset = a;
7002 break;
7003 case DW_AT_bit_size:
7004 attrs->at_bit_size = a;
7005 break;
7006 case DW_AT_bit_stride:
7007 attrs->at_bit_stride = a;
7008 break;
7009 case DW_AT_byte_size:
7010 attrs->at_byte_size = a;
7011 break;
7012 case DW_AT_byte_stride:
7013 attrs->at_byte_stride = a;
7014 break;
7015 case DW_AT_const_value:
7016 attrs->at_const_value = a;
7017 break;
7018 case DW_AT_containing_type:
7019 attrs->at_containing_type = a;
7020 break;
7021 case DW_AT_count:
7022 attrs->at_count = a;
7023 break;
7024 case DW_AT_data_location:
7025 attrs->at_data_location = a;
7026 break;
7027 case DW_AT_data_member_location:
7028 attrs->at_data_member_location = a;
7029 break;
7030 case DW_AT_decimal_scale:
7031 attrs->at_decimal_scale = a;
7032 break;
7033 case DW_AT_decimal_sign:
7034 attrs->at_decimal_sign = a;
7035 break;
7036 case DW_AT_default_value:
7037 attrs->at_default_value = a;
7038 break;
7039 case DW_AT_digit_count:
7040 attrs->at_digit_count = a;
7041 break;
7042 case DW_AT_discr:
7043 attrs->at_discr = a;
7044 break;
7045 case DW_AT_discr_list:
7046 attrs->at_discr_list = a;
7047 break;
7048 case DW_AT_discr_value:
7049 attrs->at_discr_value = a;
7050 break;
7051 case DW_AT_encoding:
7052 attrs->at_encoding = a;
7053 break;
7054 case DW_AT_endianity:
7055 attrs->at_endianity = a;
7056 break;
7057 case DW_AT_explicit:
7058 attrs->at_explicit = a;
7059 break;
7060 case DW_AT_is_optional:
7061 attrs->at_is_optional = a;
7062 break;
7063 case DW_AT_location:
7064 attrs->at_location = a;
7065 break;
7066 case DW_AT_lower_bound:
7067 attrs->at_lower_bound = a;
7068 break;
7069 case DW_AT_mutable:
7070 attrs->at_mutable = a;
7071 break;
7072 case DW_AT_ordering:
7073 attrs->at_ordering = a;
7074 break;
7075 case DW_AT_picture_string:
7076 attrs->at_picture_string = a;
7077 break;
7078 case DW_AT_prototyped:
7079 attrs->at_prototyped = a;
7080 break;
7081 case DW_AT_small:
7082 attrs->at_small = a;
7083 break;
7084 case DW_AT_segment:
7085 attrs->at_segment = a;
7086 break;
7087 case DW_AT_string_length:
7088 attrs->at_string_length = a;
7089 break;
7090 case DW_AT_string_length_bit_size:
7091 attrs->at_string_length_bit_size = a;
7092 break;
7093 case DW_AT_string_length_byte_size:
7094 attrs->at_string_length_byte_size = a;
7095 break;
7096 case DW_AT_threads_scaled:
7097 attrs->at_threads_scaled = a;
7098 break;
7099 case DW_AT_upper_bound:
7100 attrs->at_upper_bound = a;
7101 break;
7102 case DW_AT_use_location:
7103 attrs->at_use_location = a;
7104 break;
7105 case DW_AT_use_UTF8:
7106 attrs->at_use_UTF8 = a;
7107 break;
7108 case DW_AT_variable_parameter:
7109 attrs->at_variable_parameter = a;
7110 break;
7111 case DW_AT_virtuality:
7112 attrs->at_virtuality = a;
7113 break;
7114 case DW_AT_visibility:
7115 attrs->at_visibility = a;
7116 break;
7117 case DW_AT_vtable_elem_location:
7118 attrs->at_vtable_elem_location = a;
7119 break;
7120 default:
7121 break;
7126 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
7128 static void
7129 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7131 dw_die_ref c;
7132 dw_die_ref decl;
7133 struct checksum_attributes attrs;
7135 CHECKSUM_ULEB128 ('D');
7136 CHECKSUM_ULEB128 (die->die_tag);
7138 memset (&attrs, 0, sizeof (attrs));
7140 decl = get_AT_ref (die, DW_AT_specification);
7141 if (decl != NULL)
7142 collect_checksum_attributes (&attrs, decl);
7143 collect_checksum_attributes (&attrs, die);
7145 CHECKSUM_ATTR (attrs.at_name);
7146 CHECKSUM_ATTR (attrs.at_accessibility);
7147 CHECKSUM_ATTR (attrs.at_address_class);
7148 CHECKSUM_ATTR (attrs.at_allocated);
7149 CHECKSUM_ATTR (attrs.at_artificial);
7150 CHECKSUM_ATTR (attrs.at_associated);
7151 CHECKSUM_ATTR (attrs.at_binary_scale);
7152 CHECKSUM_ATTR (attrs.at_bit_offset);
7153 CHECKSUM_ATTR (attrs.at_bit_size);
7154 CHECKSUM_ATTR (attrs.at_bit_stride);
7155 CHECKSUM_ATTR (attrs.at_byte_size);
7156 CHECKSUM_ATTR (attrs.at_byte_stride);
7157 CHECKSUM_ATTR (attrs.at_const_value);
7158 CHECKSUM_ATTR (attrs.at_containing_type);
7159 CHECKSUM_ATTR (attrs.at_count);
7160 CHECKSUM_ATTR (attrs.at_data_location);
7161 CHECKSUM_ATTR (attrs.at_data_member_location);
7162 CHECKSUM_ATTR (attrs.at_decimal_scale);
7163 CHECKSUM_ATTR (attrs.at_decimal_sign);
7164 CHECKSUM_ATTR (attrs.at_default_value);
7165 CHECKSUM_ATTR (attrs.at_digit_count);
7166 CHECKSUM_ATTR (attrs.at_discr);
7167 CHECKSUM_ATTR (attrs.at_discr_list);
7168 CHECKSUM_ATTR (attrs.at_discr_value);
7169 CHECKSUM_ATTR (attrs.at_encoding);
7170 CHECKSUM_ATTR (attrs.at_endianity);
7171 CHECKSUM_ATTR (attrs.at_explicit);
7172 CHECKSUM_ATTR (attrs.at_is_optional);
7173 CHECKSUM_ATTR (attrs.at_location);
7174 CHECKSUM_ATTR (attrs.at_lower_bound);
7175 CHECKSUM_ATTR (attrs.at_mutable);
7176 CHECKSUM_ATTR (attrs.at_ordering);
7177 CHECKSUM_ATTR (attrs.at_picture_string);
7178 CHECKSUM_ATTR (attrs.at_prototyped);
7179 CHECKSUM_ATTR (attrs.at_small);
7180 CHECKSUM_ATTR (attrs.at_segment);
7181 CHECKSUM_ATTR (attrs.at_string_length);
7182 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
7183 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
7184 CHECKSUM_ATTR (attrs.at_threads_scaled);
7185 CHECKSUM_ATTR (attrs.at_upper_bound);
7186 CHECKSUM_ATTR (attrs.at_use_location);
7187 CHECKSUM_ATTR (attrs.at_use_UTF8);
7188 CHECKSUM_ATTR (attrs.at_variable_parameter);
7189 CHECKSUM_ATTR (attrs.at_virtuality);
7190 CHECKSUM_ATTR (attrs.at_visibility);
7191 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
7192 CHECKSUM_ATTR (attrs.at_type);
7193 CHECKSUM_ATTR (attrs.at_friend);
7194 CHECKSUM_ATTR (attrs.at_alignment);
7196 /* Checksum the child DIEs. */
7197 c = die->die_child;
7198 if (c) do {
7199 dw_attr_node *name_attr;
7201 c = c->die_sib;
7202 name_attr = get_AT (c, DW_AT_name);
7203 if (is_template_instantiation (c))
7205 /* Ignore instantiations of member type and function templates. */
7207 else if (name_attr != NULL
7208 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
7210 /* Use a shallow checksum for named nested types and member
7211 functions. */
7212 CHECKSUM_ULEB128 ('S');
7213 CHECKSUM_ULEB128 (c->die_tag);
7214 CHECKSUM_STRING (AT_string (name_attr));
7216 else
7218 /* Use a deep checksum for other children. */
7219 /* Mark this DIE so it gets processed when unmarking. */
7220 if (c->die_mark == 0)
7221 c->die_mark = -1;
7222 die_checksum_ordered (c, ctx, mark);
7224 } while (c != die->die_child);
7226 CHECKSUM_ULEB128 (0);
7229 /* Add a type name and tag to a hash. */
7230 static void
7231 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
7233 CHECKSUM_ULEB128 (tag);
7234 CHECKSUM_STRING (name);
7237 #undef CHECKSUM
7238 #undef CHECKSUM_STRING
7239 #undef CHECKSUM_ATTR
7240 #undef CHECKSUM_LEB128
7241 #undef CHECKSUM_ULEB128
7243 /* Generate the type signature for DIE. This is computed by generating an
7244 MD5 checksum over the DIE's tag, its relevant attributes, and its
7245 children. Attributes that are references to other DIEs are processed
7246 by recursion, using the MARK field to prevent infinite recursion.
7247 If the DIE is nested inside a namespace or another type, we also
7248 need to include that context in the signature. The lower 64 bits
7249 of the resulting MD5 checksum comprise the signature. */
7251 static void
7252 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
7254 int mark;
7255 const char *name;
7256 unsigned char checksum[16];
7257 struct md5_ctx ctx;
7258 dw_die_ref decl;
7259 dw_die_ref parent;
7261 name = get_AT_string (die, DW_AT_name);
7262 decl = get_AT_ref (die, DW_AT_specification);
7263 parent = get_die_parent (die);
7265 /* First, compute a signature for just the type name (and its surrounding
7266 context, if any. This is stored in the type unit DIE for link-time
7267 ODR (one-definition rule) checking. */
7269 if (is_cxx () && name != NULL)
7271 md5_init_ctx (&ctx);
7273 /* Checksum the names of surrounding namespaces and structures. */
7274 if (parent != NULL)
7275 checksum_die_context (parent, &ctx);
7277 /* Checksum the current DIE. */
7278 die_odr_checksum (die->die_tag, name, &ctx);
7279 md5_finish_ctx (&ctx, checksum);
7281 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
7284 /* Next, compute the complete type signature. */
7286 md5_init_ctx (&ctx);
7287 mark = 1;
7288 die->die_mark = mark;
7290 /* Checksum the names of surrounding namespaces and structures. */
7291 if (parent != NULL)
7292 checksum_die_context (parent, &ctx);
7294 /* Checksum the DIE and its children. */
7295 die_checksum_ordered (die, &ctx, &mark);
7296 unmark_all_dies (die);
7297 md5_finish_ctx (&ctx, checksum);
7299 /* Store the signature in the type node and link the type DIE and the
7300 type node together. */
7301 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7302 DWARF_TYPE_SIGNATURE_SIZE);
7303 die->comdat_type_p = true;
7304 die->die_id.die_type_node = type_node;
7305 type_node->type_die = die;
7307 /* If the DIE is a specification, link its declaration to the type node
7308 as well. */
7309 if (decl != NULL)
7311 decl->comdat_type_p = true;
7312 decl->die_id.die_type_node = type_node;
7316 /* Do the location expressions look same? */
7317 static inline int
7318 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7320 return loc1->dw_loc_opc == loc2->dw_loc_opc
7321 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7322 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7325 /* Do the values look the same? */
7326 static int
7327 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7329 dw_loc_descr_ref loc1, loc2;
7330 rtx r1, r2;
7332 if (v1->val_class != v2->val_class)
7333 return 0;
7335 switch (v1->val_class)
7337 case dw_val_class_const:
7338 case dw_val_class_const_implicit:
7339 return v1->v.val_int == v2->v.val_int;
7340 case dw_val_class_unsigned_const:
7341 case dw_val_class_unsigned_const_implicit:
7342 return v1->v.val_unsigned == v2->v.val_unsigned;
7343 case dw_val_class_const_double:
7344 return v1->v.val_double.high == v2->v.val_double.high
7345 && v1->v.val_double.low == v2->v.val_double.low;
7346 case dw_val_class_wide_int:
7347 return *v1->v.val_wide == *v2->v.val_wide;
7348 case dw_val_class_vec:
7349 if (v1->v.val_vec.length != v2->v.val_vec.length
7350 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7351 return 0;
7352 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7353 v1->v.val_vec.length * v1->v.val_vec.elt_size))
7354 return 0;
7355 return 1;
7356 case dw_val_class_flag:
7357 return v1->v.val_flag == v2->v.val_flag;
7358 case dw_val_class_str:
7359 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
7361 case dw_val_class_addr:
7362 r1 = v1->v.val_addr;
7363 r2 = v2->v.val_addr;
7364 if (GET_CODE (r1) != GET_CODE (r2))
7365 return 0;
7366 return !rtx_equal_p (r1, r2);
7368 case dw_val_class_offset:
7369 return v1->v.val_offset == v2->v.val_offset;
7371 case dw_val_class_loc:
7372 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7373 loc1 && loc2;
7374 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7375 if (!same_loc_p (loc1, loc2, mark))
7376 return 0;
7377 return !loc1 && !loc2;
7379 case dw_val_class_die_ref:
7380 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7382 case dw_val_class_fde_ref:
7383 case dw_val_class_vms_delta:
7384 case dw_val_class_lbl_id:
7385 case dw_val_class_lineptr:
7386 case dw_val_class_macptr:
7387 case dw_val_class_loclistsptr:
7388 case dw_val_class_high_pc:
7389 return 1;
7391 case dw_val_class_file:
7392 case dw_val_class_file_implicit:
7393 return v1->v.val_file == v2->v.val_file;
7395 case dw_val_class_data8:
7396 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7398 default:
7399 return 1;
7403 /* Do the attributes look the same? */
7405 static int
7406 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
7408 if (at1->dw_attr != at2->dw_attr)
7409 return 0;
7411 /* We don't care that this was compiled with a different compiler
7412 snapshot; if the output is the same, that's what matters. */
7413 if (at1->dw_attr == DW_AT_producer)
7414 return 1;
7416 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7419 /* Do the dies look the same? */
7421 static int
7422 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7424 dw_die_ref c1, c2;
7425 dw_attr_node *a1;
7426 unsigned ix;
7428 /* To avoid infinite recursion. */
7429 if (die1->die_mark)
7430 return die1->die_mark == die2->die_mark;
7431 die1->die_mark = die2->die_mark = ++(*mark);
7433 if (die1->die_tag != die2->die_tag)
7434 return 0;
7436 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
7437 return 0;
7439 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
7440 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
7441 return 0;
7443 c1 = die1->die_child;
7444 c2 = die2->die_child;
7445 if (! c1)
7447 if (c2)
7448 return 0;
7450 else
7451 for (;;)
7453 if (!same_die_p (c1, c2, mark))
7454 return 0;
7455 c1 = c1->die_sib;
7456 c2 = c2->die_sib;
7457 if (c1 == die1->die_child)
7459 if (c2 == die2->die_child)
7460 break;
7461 else
7462 return 0;
7466 return 1;
7469 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7470 children, and set die_symbol. */
7472 static void
7473 compute_comp_unit_symbol (dw_die_ref unit_die)
7475 const char *die_name = get_AT_string (unit_die, DW_AT_name);
7476 const char *base = die_name ? lbasename (die_name) : "anonymous";
7477 char *name = XALLOCAVEC (char, strlen (base) + 64);
7478 char *p;
7479 int i, mark;
7480 unsigned char checksum[16];
7481 struct md5_ctx ctx;
7483 /* Compute the checksum of the DIE, then append part of it as hex digits to
7484 the name filename of the unit. */
7486 md5_init_ctx (&ctx);
7487 mark = 0;
7488 die_checksum (unit_die, &ctx, &mark);
7489 unmark_all_dies (unit_die);
7490 md5_finish_ctx (&ctx, checksum);
7492 /* When we this for comp_unit_die () we have a DW_AT_name that might
7493 not start with a letter but with anything valid for filenames and
7494 clean_symbol_name doesn't fix that up. Prepend 'g' if the first
7495 character is not a letter. */
7496 sprintf (name, "%s%s.", ISALPHA (*base) ? "" : "g", base);
7497 clean_symbol_name (name);
7499 p = name + strlen (name);
7500 for (i = 0; i < 4; i++)
7502 sprintf (p, "%.2x", checksum[i]);
7503 p += 2;
7506 unit_die->die_id.die_symbol = xstrdup (name);
7509 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
7511 static int
7512 is_type_die (dw_die_ref die)
7514 switch (die->die_tag)
7516 case DW_TAG_array_type:
7517 case DW_TAG_class_type:
7518 case DW_TAG_interface_type:
7519 case DW_TAG_enumeration_type:
7520 case DW_TAG_pointer_type:
7521 case DW_TAG_reference_type:
7522 case DW_TAG_rvalue_reference_type:
7523 case DW_TAG_string_type:
7524 case DW_TAG_structure_type:
7525 case DW_TAG_subroutine_type:
7526 case DW_TAG_union_type:
7527 case DW_TAG_ptr_to_member_type:
7528 case DW_TAG_set_type:
7529 case DW_TAG_subrange_type:
7530 case DW_TAG_base_type:
7531 case DW_TAG_const_type:
7532 case DW_TAG_file_type:
7533 case DW_TAG_packed_type:
7534 case DW_TAG_volatile_type:
7535 case DW_TAG_typedef:
7536 return 1;
7537 default:
7538 return 0;
7542 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7543 Basically, we want to choose the bits that are likely to be shared between
7544 compilations (types) and leave out the bits that are specific to individual
7545 compilations (functions). */
7547 static int
7548 is_comdat_die (dw_die_ref c)
7550 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7551 we do for stabs. The advantage is a greater likelihood of sharing between
7552 objects that don't include headers in the same order (and therefore would
7553 put the base types in a different comdat). jason 8/28/00 */
7555 if (c->die_tag == DW_TAG_base_type)
7556 return 0;
7558 if (c->die_tag == DW_TAG_pointer_type
7559 || c->die_tag == DW_TAG_reference_type
7560 || c->die_tag == DW_TAG_rvalue_reference_type
7561 || c->die_tag == DW_TAG_const_type
7562 || c->die_tag == DW_TAG_volatile_type)
7564 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7566 return t ? is_comdat_die (t) : 0;
7569 return is_type_die (c);
7572 /* Returns true iff C is a compile-unit DIE. */
7574 static inline bool
7575 is_cu_die (dw_die_ref c)
7577 return c && (c->die_tag == DW_TAG_compile_unit
7578 || c->die_tag == DW_TAG_skeleton_unit);
7581 /* Returns true iff C is a unit DIE of some sort. */
7583 static inline bool
7584 is_unit_die (dw_die_ref c)
7586 return c && (c->die_tag == DW_TAG_compile_unit
7587 || c->die_tag == DW_TAG_partial_unit
7588 || c->die_tag == DW_TAG_type_unit
7589 || c->die_tag == DW_TAG_skeleton_unit);
7592 /* Returns true iff C is a namespace DIE. */
7594 static inline bool
7595 is_namespace_die (dw_die_ref c)
7597 return c && c->die_tag == DW_TAG_namespace;
7600 /* Returns true iff C is a class or structure DIE. */
7602 static inline bool
7603 is_class_die (dw_die_ref c)
7605 return c && (c->die_tag == DW_TAG_class_type
7606 || c->die_tag == DW_TAG_structure_type);
7609 /* Return non-zero if this DIE is a template parameter. */
7611 static inline bool
7612 is_template_parameter (dw_die_ref die)
7614 switch (die->die_tag)
7616 case DW_TAG_template_type_param:
7617 case DW_TAG_template_value_param:
7618 case DW_TAG_GNU_template_template_param:
7619 case DW_TAG_GNU_template_parameter_pack:
7620 return true;
7621 default:
7622 return false;
7626 /* Return non-zero if this DIE represents a template instantiation. */
7628 static inline bool
7629 is_template_instantiation (dw_die_ref die)
7631 dw_die_ref c;
7633 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7634 return false;
7635 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7636 return false;
7639 static char *
7640 gen_internal_sym (const char *prefix)
7642 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7644 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7645 return xstrdup (buf);
7648 /* Return non-zero if this DIE is a declaration. */
7650 static int
7651 is_declaration_die (dw_die_ref die)
7653 dw_attr_node *a;
7654 unsigned ix;
7656 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7657 if (a->dw_attr == DW_AT_declaration)
7658 return 1;
7660 return 0;
7663 /* Return non-zero if this DIE is nested inside a subprogram. */
7665 static int
7666 is_nested_in_subprogram (dw_die_ref die)
7668 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7670 if (decl == NULL)
7671 decl = die;
7672 return local_scope_p (decl);
7675 /* Return non-zero if this DIE contains a defining declaration of a
7676 subprogram. */
7678 static int
7679 contains_subprogram_definition (dw_die_ref die)
7681 dw_die_ref c;
7683 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7684 return 1;
7685 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7686 return 0;
7689 /* Return non-zero if this is a type DIE that should be moved to a
7690 COMDAT .debug_types section or .debug_info section with DW_UT_*type
7691 unit type. */
7693 static int
7694 should_move_die_to_comdat (dw_die_ref die)
7696 switch (die->die_tag)
7698 case DW_TAG_class_type:
7699 case DW_TAG_structure_type:
7700 case DW_TAG_enumeration_type:
7701 case DW_TAG_union_type:
7702 /* Don't move declarations, inlined instances, types nested in a
7703 subprogram, or types that contain subprogram definitions. */
7704 if (is_declaration_die (die)
7705 || get_AT (die, DW_AT_abstract_origin)
7706 || is_nested_in_subprogram (die)
7707 || contains_subprogram_definition (die))
7708 return 0;
7709 return 1;
7710 case DW_TAG_array_type:
7711 case DW_TAG_interface_type:
7712 case DW_TAG_pointer_type:
7713 case DW_TAG_reference_type:
7714 case DW_TAG_rvalue_reference_type:
7715 case DW_TAG_string_type:
7716 case DW_TAG_subroutine_type:
7717 case DW_TAG_ptr_to_member_type:
7718 case DW_TAG_set_type:
7719 case DW_TAG_subrange_type:
7720 case DW_TAG_base_type:
7721 case DW_TAG_const_type:
7722 case DW_TAG_file_type:
7723 case DW_TAG_packed_type:
7724 case DW_TAG_volatile_type:
7725 case DW_TAG_typedef:
7726 default:
7727 return 0;
7731 /* Make a clone of DIE. */
7733 static dw_die_ref
7734 clone_die (dw_die_ref die)
7736 dw_die_ref clone = new_die_raw (die->die_tag);
7737 dw_attr_node *a;
7738 unsigned ix;
7740 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7741 add_dwarf_attr (clone, a);
7743 return clone;
7746 /* Make a clone of the tree rooted at DIE. */
7748 static dw_die_ref
7749 clone_tree (dw_die_ref die)
7751 dw_die_ref c;
7752 dw_die_ref clone = clone_die (die);
7754 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7756 return clone;
7759 /* Make a clone of DIE as a declaration. */
7761 static dw_die_ref
7762 clone_as_declaration (dw_die_ref die)
7764 dw_die_ref clone;
7765 dw_die_ref decl;
7766 dw_attr_node *a;
7767 unsigned ix;
7769 /* If the DIE is already a declaration, just clone it. */
7770 if (is_declaration_die (die))
7771 return clone_die (die);
7773 /* If the DIE is a specification, just clone its declaration DIE. */
7774 decl = get_AT_ref (die, DW_AT_specification);
7775 if (decl != NULL)
7777 clone = clone_die (decl);
7778 if (die->comdat_type_p)
7779 add_AT_die_ref (clone, DW_AT_signature, die);
7780 return clone;
7783 clone = new_die_raw (die->die_tag);
7785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7787 /* We don't want to copy over all attributes.
7788 For example we don't want DW_AT_byte_size because otherwise we will no
7789 longer have a declaration and GDB will treat it as a definition. */
7791 switch (a->dw_attr)
7793 case DW_AT_abstract_origin:
7794 case DW_AT_artificial:
7795 case DW_AT_containing_type:
7796 case DW_AT_external:
7797 case DW_AT_name:
7798 case DW_AT_type:
7799 case DW_AT_virtuality:
7800 case DW_AT_linkage_name:
7801 case DW_AT_MIPS_linkage_name:
7802 add_dwarf_attr (clone, a);
7803 break;
7804 case DW_AT_byte_size:
7805 case DW_AT_alignment:
7806 default:
7807 break;
7811 if (die->comdat_type_p)
7812 add_AT_die_ref (clone, DW_AT_signature, die);
7814 add_AT_flag (clone, DW_AT_declaration, 1);
7815 return clone;
7819 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7821 struct decl_table_entry
7823 dw_die_ref orig;
7824 dw_die_ref copy;
7827 /* Helpers to manipulate hash table of copied declarations. */
7829 /* Hashtable helpers. */
7831 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7833 typedef die_struct *compare_type;
7834 static inline hashval_t hash (const decl_table_entry *);
7835 static inline bool equal (const decl_table_entry *, const die_struct *);
7838 inline hashval_t
7839 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7841 return htab_hash_pointer (entry->orig);
7844 inline bool
7845 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7846 const die_struct *entry2)
7848 return entry1->orig == entry2;
7851 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7853 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7854 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7855 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7856 to check if the ancestor has already been copied into UNIT. */
7858 static dw_die_ref
7859 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7860 decl_hash_type *decl_table)
7862 dw_die_ref parent = die->die_parent;
7863 dw_die_ref new_parent = unit;
7864 dw_die_ref copy;
7865 decl_table_entry **slot = NULL;
7866 struct decl_table_entry *entry = NULL;
7868 if (decl_table)
7870 /* Check if the entry has already been copied to UNIT. */
7871 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7872 INSERT);
7873 if (*slot != HTAB_EMPTY_ENTRY)
7875 entry = *slot;
7876 return entry->copy;
7879 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7880 entry = XCNEW (struct decl_table_entry);
7881 entry->orig = die;
7882 entry->copy = NULL;
7883 *slot = entry;
7886 if (parent != NULL)
7888 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7889 if (spec != NULL)
7890 parent = spec;
7891 if (!is_unit_die (parent))
7892 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7895 copy = clone_as_declaration (die);
7896 add_child_die (new_parent, copy);
7898 if (decl_table)
7900 /* Record the pointer to the copy. */
7901 entry->copy = copy;
7904 return copy;
7906 /* Copy the declaration context to the new type unit DIE. This includes
7907 any surrounding namespace or type declarations. If the DIE has an
7908 AT_specification attribute, it also includes attributes and children
7909 attached to the specification, and returns a pointer to the original
7910 parent of the declaration DIE. Returns NULL otherwise. */
7912 static dw_die_ref
7913 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7915 dw_die_ref decl;
7916 dw_die_ref new_decl;
7917 dw_die_ref orig_parent = NULL;
7919 decl = get_AT_ref (die, DW_AT_specification);
7920 if (decl == NULL)
7921 decl = die;
7922 else
7924 unsigned ix;
7925 dw_die_ref c;
7926 dw_attr_node *a;
7928 /* The original DIE will be changed to a declaration, and must
7929 be moved to be a child of the original declaration DIE. */
7930 orig_parent = decl->die_parent;
7932 /* Copy the type node pointer from the new DIE to the original
7933 declaration DIE so we can forward references later. */
7934 decl->comdat_type_p = true;
7935 decl->die_id.die_type_node = die->die_id.die_type_node;
7937 remove_AT (die, DW_AT_specification);
7939 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7941 if (a->dw_attr != DW_AT_name
7942 && a->dw_attr != DW_AT_declaration
7943 && a->dw_attr != DW_AT_external)
7944 add_dwarf_attr (die, a);
7947 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7950 if (decl->die_parent != NULL
7951 && !is_unit_die (decl->die_parent))
7953 new_decl = copy_ancestor_tree (unit, decl, NULL);
7954 if (new_decl != NULL)
7956 remove_AT (new_decl, DW_AT_signature);
7957 add_AT_specification (die, new_decl);
7961 return orig_parent;
7964 /* Generate the skeleton ancestor tree for the given NODE, then clone
7965 the DIE and add the clone into the tree. */
7967 static void
7968 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7970 if (node->new_die != NULL)
7971 return;
7973 node->new_die = clone_as_declaration (node->old_die);
7975 if (node->parent != NULL)
7977 generate_skeleton_ancestor_tree (node->parent);
7978 add_child_die (node->parent->new_die, node->new_die);
7982 /* Generate a skeleton tree of DIEs containing any declarations that are
7983 found in the original tree. We traverse the tree looking for declaration
7984 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7986 static void
7987 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7989 skeleton_chain_node node;
7990 dw_die_ref c;
7991 dw_die_ref first;
7992 dw_die_ref prev = NULL;
7993 dw_die_ref next = NULL;
7995 node.parent = parent;
7997 first = c = parent->old_die->die_child;
7998 if (c)
7999 next = c->die_sib;
8000 if (c) do {
8001 if (prev == NULL || prev->die_sib == c)
8002 prev = c;
8003 c = next;
8004 next = (c == first ? NULL : c->die_sib);
8005 node.old_die = c;
8006 node.new_die = NULL;
8007 if (is_declaration_die (c))
8009 if (is_template_instantiation (c))
8011 /* Instantiated templates do not need to be cloned into the
8012 type unit. Just move the DIE and its children back to
8013 the skeleton tree (in the main CU). */
8014 remove_child_with_prev (c, prev);
8015 add_child_die (parent->new_die, c);
8016 c = prev;
8018 else if (c->comdat_type_p)
8020 /* This is the skeleton of earlier break_out_comdat_types
8021 type. Clone the existing DIE, but keep the children
8022 under the original (which is in the main CU). */
8023 dw_die_ref clone = clone_die (c);
8025 replace_child (c, clone, prev);
8026 generate_skeleton_ancestor_tree (parent);
8027 add_child_die (parent->new_die, c);
8028 c = clone;
8029 continue;
8031 else
8033 /* Clone the existing DIE, move the original to the skeleton
8034 tree (which is in the main CU), and put the clone, with
8035 all the original's children, where the original came from
8036 (which is about to be moved to the type unit). */
8037 dw_die_ref clone = clone_die (c);
8038 move_all_children (c, clone);
8040 /* If the original has a DW_AT_object_pointer attribute,
8041 it would now point to a child DIE just moved to the
8042 cloned tree, so we need to remove that attribute from
8043 the original. */
8044 remove_AT (c, DW_AT_object_pointer);
8046 replace_child (c, clone, prev);
8047 generate_skeleton_ancestor_tree (parent);
8048 add_child_die (parent->new_die, c);
8049 node.old_die = clone;
8050 node.new_die = c;
8051 c = clone;
8054 generate_skeleton_bottom_up (&node);
8055 } while (next != NULL);
8058 /* Wrapper function for generate_skeleton_bottom_up. */
8060 static dw_die_ref
8061 generate_skeleton (dw_die_ref die)
8063 skeleton_chain_node node;
8065 node.old_die = die;
8066 node.new_die = NULL;
8067 node.parent = NULL;
8069 /* If this type definition is nested inside another type,
8070 and is not an instantiation of a template, always leave
8071 at least a declaration in its place. */
8072 if (die->die_parent != NULL
8073 && is_type_die (die->die_parent)
8074 && !is_template_instantiation (die))
8075 node.new_die = clone_as_declaration (die);
8077 generate_skeleton_bottom_up (&node);
8078 return node.new_die;
8081 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
8082 declaration. The original DIE is moved to a new compile unit so that
8083 existing references to it follow it to the new location. If any of the
8084 original DIE's descendants is a declaration, we need to replace the
8085 original DIE with a skeleton tree and move the declarations back into the
8086 skeleton tree. */
8088 static dw_die_ref
8089 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
8090 dw_die_ref prev)
8092 dw_die_ref skeleton, orig_parent;
8094 /* Copy the declaration context to the type unit DIE. If the returned
8095 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
8096 that DIE. */
8097 orig_parent = copy_declaration_context (unit, child);
8099 skeleton = generate_skeleton (child);
8100 if (skeleton == NULL)
8101 remove_child_with_prev (child, prev);
8102 else
8104 skeleton->comdat_type_p = true;
8105 skeleton->die_id.die_type_node = child->die_id.die_type_node;
8107 /* If the original DIE was a specification, we need to put
8108 the skeleton under the parent DIE of the declaration.
8109 This leaves the original declaration in the tree, but
8110 it will be pruned later since there are no longer any
8111 references to it. */
8112 if (orig_parent != NULL)
8114 remove_child_with_prev (child, prev);
8115 add_child_die (orig_parent, skeleton);
8117 else
8118 replace_child (child, skeleton, prev);
8121 return skeleton;
8124 static void
8125 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8126 comdat_type_node *type_node,
8127 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
8129 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
8130 procedure, put it under TYPE_NODE and return the copy. Continue looking for
8131 DWARF procedure references in the DW_AT_location attribute. */
8133 static dw_die_ref
8134 copy_dwarf_procedure (dw_die_ref die,
8135 comdat_type_node *type_node,
8136 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8138 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
8140 /* DWARF procedures are not supposed to have children... */
8141 gcc_assert (die->die_child == NULL);
8143 /* ... and they are supposed to have only one attribute: DW_AT_location. */
8144 gcc_assert (vec_safe_length (die->die_attr) == 1
8145 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
8147 /* Do not copy more than once DWARF procedures. */
8148 bool existed;
8149 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
8150 if (existed)
8151 return die_copy;
8153 die_copy = clone_die (die);
8154 add_child_die (type_node->root_die, die_copy);
8155 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
8156 return die_copy;
8159 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
8160 procedures in DIE's attributes. */
8162 static void
8163 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
8164 comdat_type_node *type_node,
8165 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8167 dw_attr_node *a;
8168 unsigned i;
8170 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
8172 dw_loc_descr_ref loc;
8174 if (a->dw_attr_val.val_class != dw_val_class_loc)
8175 continue;
8177 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
8179 switch (loc->dw_loc_opc)
8181 case DW_OP_call2:
8182 case DW_OP_call4:
8183 case DW_OP_call_ref:
8184 gcc_assert (loc->dw_loc_oprnd1.val_class
8185 == dw_val_class_die_ref);
8186 loc->dw_loc_oprnd1.v.val_die_ref.die
8187 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
8188 type_node,
8189 copied_dwarf_procs);
8191 default:
8192 break;
8198 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
8199 rewrite references to point to the copies.
8201 References are looked for in DIE's attributes and recursively in all its
8202 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
8203 mapping from old DWARF procedures to their copy. It is used not to copy
8204 twice the same DWARF procedure under TYPE_NODE. */
8206 static void
8207 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
8208 comdat_type_node *type_node,
8209 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
8211 dw_die_ref c;
8213 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
8214 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
8215 type_node,
8216 copied_dwarf_procs));
8219 /* Traverse the DIE and set up additional .debug_types or .debug_info
8220 DW_UT_*type sections for each type worthy of being placed in a COMDAT
8221 section. */
8223 static void
8224 break_out_comdat_types (dw_die_ref die)
8226 dw_die_ref c;
8227 dw_die_ref first;
8228 dw_die_ref prev = NULL;
8229 dw_die_ref next = NULL;
8230 dw_die_ref unit = NULL;
8232 first = c = die->die_child;
8233 if (c)
8234 next = c->die_sib;
8235 if (c) do {
8236 if (prev == NULL || prev->die_sib == c)
8237 prev = c;
8238 c = next;
8239 next = (c == first ? NULL : c->die_sib);
8240 if (should_move_die_to_comdat (c))
8242 dw_die_ref replacement;
8243 comdat_type_node *type_node;
8245 /* Break out nested types into their own type units. */
8246 break_out_comdat_types (c);
8248 /* Create a new type unit DIE as the root for the new tree, and
8249 add it to the list of comdat types. */
8250 unit = new_die (DW_TAG_type_unit, NULL, NULL);
8251 add_AT_unsigned (unit, DW_AT_language,
8252 get_AT_unsigned (comp_unit_die (), DW_AT_language));
8253 type_node = ggc_cleared_alloc<comdat_type_node> ();
8254 type_node->root_die = unit;
8255 type_node->next = comdat_type_list;
8256 comdat_type_list = type_node;
8258 /* Generate the type signature. */
8259 generate_type_signature (c, type_node);
8261 /* Copy the declaration context, attributes, and children of the
8262 declaration into the new type unit DIE, then remove this DIE
8263 from the main CU (or replace it with a skeleton if necessary). */
8264 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
8265 type_node->skeleton_die = replacement;
8267 /* Add the DIE to the new compunit. */
8268 add_child_die (unit, c);
8270 /* Types can reference DWARF procedures for type size or data location
8271 expressions. Calls in DWARF expressions cannot target procedures
8272 that are not in the same section. So we must copy DWARF procedures
8273 along with this type and then rewrite references to them. */
8274 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
8275 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
8277 if (replacement != NULL)
8278 c = replacement;
8280 else if (c->die_tag == DW_TAG_namespace
8281 || c->die_tag == DW_TAG_class_type
8282 || c->die_tag == DW_TAG_structure_type
8283 || c->die_tag == DW_TAG_union_type)
8285 /* Look for nested types that can be broken out. */
8286 break_out_comdat_types (c);
8288 } while (next != NULL);
8291 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
8292 Enter all the cloned children into the hash table decl_table. */
8294 static dw_die_ref
8295 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
8297 dw_die_ref c;
8298 dw_die_ref clone;
8299 struct decl_table_entry *entry;
8300 decl_table_entry **slot;
8302 if (die->die_tag == DW_TAG_subprogram)
8303 clone = clone_as_declaration (die);
8304 else
8305 clone = clone_die (die);
8307 slot = decl_table->find_slot_with_hash (die,
8308 htab_hash_pointer (die), INSERT);
8310 /* Assert that DIE isn't in the hash table yet. If it would be there
8311 before, the ancestors would be necessarily there as well, therefore
8312 clone_tree_partial wouldn't be called. */
8313 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
8315 entry = XCNEW (struct decl_table_entry);
8316 entry->orig = die;
8317 entry->copy = clone;
8318 *slot = entry;
8320 if (die->die_tag != DW_TAG_subprogram)
8321 FOR_EACH_CHILD (die, c,
8322 add_child_die (clone, clone_tree_partial (c, decl_table)));
8324 return clone;
8327 /* Walk the DIE and its children, looking for references to incomplete
8328 or trivial types that are unmarked (i.e., that are not in the current
8329 type_unit). */
8331 static void
8332 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
8334 dw_die_ref c;
8335 dw_attr_node *a;
8336 unsigned ix;
8338 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8340 if (AT_class (a) == dw_val_class_die_ref)
8342 dw_die_ref targ = AT_ref (a);
8343 decl_table_entry **slot;
8344 struct decl_table_entry *entry;
8346 if (targ->die_mark != 0 || targ->comdat_type_p)
8347 continue;
8349 slot = decl_table->find_slot_with_hash (targ,
8350 htab_hash_pointer (targ),
8351 INSERT);
8353 if (*slot != HTAB_EMPTY_ENTRY)
8355 /* TARG has already been copied, so we just need to
8356 modify the reference to point to the copy. */
8357 entry = *slot;
8358 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8360 else
8362 dw_die_ref parent = unit;
8363 dw_die_ref copy = clone_die (targ);
8365 /* Record in DECL_TABLE that TARG has been copied.
8366 Need to do this now, before the recursive call,
8367 because DECL_TABLE may be expanded and SLOT
8368 would no longer be a valid pointer. */
8369 entry = XCNEW (struct decl_table_entry);
8370 entry->orig = targ;
8371 entry->copy = copy;
8372 *slot = entry;
8374 /* If TARG is not a declaration DIE, we need to copy its
8375 children. */
8376 if (!is_declaration_die (targ))
8378 FOR_EACH_CHILD (
8379 targ, c,
8380 add_child_die (copy,
8381 clone_tree_partial (c, decl_table)));
8384 /* Make sure the cloned tree is marked as part of the
8385 type unit. */
8386 mark_dies (copy);
8388 /* If TARG has surrounding context, copy its ancestor tree
8389 into the new type unit. */
8390 if (targ->die_parent != NULL
8391 && !is_unit_die (targ->die_parent))
8392 parent = copy_ancestor_tree (unit, targ->die_parent,
8393 decl_table);
8395 add_child_die (parent, copy);
8396 a->dw_attr_val.v.val_die_ref.die = copy;
8398 /* Make sure the newly-copied DIE is walked. If it was
8399 installed in a previously-added context, it won't
8400 get visited otherwise. */
8401 if (parent != unit)
8403 /* Find the highest point of the newly-added tree,
8404 mark each node along the way, and walk from there. */
8405 parent->die_mark = 1;
8406 while (parent->die_parent
8407 && parent->die_parent->die_mark == 0)
8409 parent = parent->die_parent;
8410 parent->die_mark = 1;
8412 copy_decls_walk (unit, parent, decl_table);
8418 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8421 /* Copy declarations for "unworthy" types into the new comdat section.
8422 Incomplete types, modified types, and certain other types aren't broken
8423 out into comdat sections of their own, so they don't have a signature,
8424 and we need to copy the declaration into the same section so that we
8425 don't have an external reference. */
8427 static void
8428 copy_decls_for_unworthy_types (dw_die_ref unit)
8430 mark_dies (unit);
8431 decl_hash_type decl_table (10);
8432 copy_decls_walk (unit, unit, &decl_table);
8433 unmark_dies (unit);
8436 /* Traverse the DIE and add a sibling attribute if it may have the
8437 effect of speeding up access to siblings. To save some space,
8438 avoid generating sibling attributes for DIE's without children. */
8440 static void
8441 add_sibling_attributes (dw_die_ref die)
8443 dw_die_ref c;
8445 if (! die->die_child)
8446 return;
8448 if (die->die_parent && die != die->die_parent->die_child)
8449 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8451 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8454 /* Output all location lists for the DIE and its children. */
8456 static void
8457 output_location_lists (dw_die_ref die)
8459 dw_die_ref c;
8460 dw_attr_node *a;
8461 unsigned ix;
8463 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8464 if (AT_class (a) == dw_val_class_loc_list)
8465 output_loc_list (AT_loc_list (a));
8467 FOR_EACH_CHILD (die, c, output_location_lists (c));
8470 /* During assign_location_list_indexes and output_loclists_offset the
8471 current index, after it the number of assigned indexes (i.e. how
8472 large the .debug_loclists* offset table should be). */
8473 static unsigned int loc_list_idx;
8475 /* Output all location list offsets for the DIE and its children. */
8477 static void
8478 output_loclists_offsets (dw_die_ref die)
8480 dw_die_ref c;
8481 dw_attr_node *a;
8482 unsigned ix;
8484 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8485 if (AT_class (a) == dw_val_class_loc_list)
8487 dw_loc_list_ref l = AT_loc_list (a);
8488 if (l->offset_emitted)
8489 continue;
8490 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l->ll_symbol,
8491 loc_section_label, NULL);
8492 gcc_assert (l->hash == loc_list_idx);
8493 loc_list_idx++;
8494 l->offset_emitted = true;
8497 FOR_EACH_CHILD (die, c, output_loclists_offsets (c));
8500 /* Recursively set indexes of location lists. */
8502 static void
8503 assign_location_list_indexes (dw_die_ref die)
8505 dw_die_ref c;
8506 dw_attr_node *a;
8507 unsigned ix;
8509 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8510 if (AT_class (a) == dw_val_class_loc_list)
8512 dw_loc_list_ref list = AT_loc_list (a);
8513 if (!list->num_assigned)
8515 list->num_assigned = true;
8516 list->hash = loc_list_idx++;
8520 FOR_EACH_CHILD (die, c, assign_location_list_indexes (c));
8523 /* We want to limit the number of external references, because they are
8524 larger than local references: a relocation takes multiple words, and
8525 even a sig8 reference is always eight bytes, whereas a local reference
8526 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8527 So if we encounter multiple external references to the same type DIE, we
8528 make a local typedef stub for it and redirect all references there.
8530 This is the element of the hash table for keeping track of these
8531 references. */
8533 struct external_ref
8535 dw_die_ref type;
8536 dw_die_ref stub;
8537 unsigned n_refs;
8540 /* Hashtable helpers. */
8542 struct external_ref_hasher : free_ptr_hash <external_ref>
8544 static inline hashval_t hash (const external_ref *);
8545 static inline bool equal (const external_ref *, const external_ref *);
8548 inline hashval_t
8549 external_ref_hasher::hash (const external_ref *r)
8551 dw_die_ref die = r->type;
8552 hashval_t h = 0;
8554 /* We can't use the address of the DIE for hashing, because
8555 that will make the order of the stub DIEs non-deterministic. */
8556 if (! die->comdat_type_p)
8557 /* We have a symbol; use it to compute a hash. */
8558 h = htab_hash_string (die->die_id.die_symbol);
8559 else
8561 /* We have a type signature; use a subset of the bits as the hash.
8562 The 8-byte signature is at least as large as hashval_t. */
8563 comdat_type_node *type_node = die->die_id.die_type_node;
8564 memcpy (&h, type_node->signature, sizeof (h));
8566 return h;
8569 inline bool
8570 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8572 return r1->type == r2->type;
8575 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8577 /* Return a pointer to the external_ref for references to DIE. */
8579 static struct external_ref *
8580 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8582 struct external_ref ref, *ref_p;
8583 external_ref **slot;
8585 ref.type = die;
8586 slot = map->find_slot (&ref, INSERT);
8587 if (*slot != HTAB_EMPTY_ENTRY)
8588 return *slot;
8590 ref_p = XCNEW (struct external_ref);
8591 ref_p->type = die;
8592 *slot = ref_p;
8593 return ref_p;
8596 /* Subroutine of optimize_external_refs, below.
8598 If we see a type skeleton, record it as our stub. If we see external
8599 references, remember how many we've seen. */
8601 static void
8602 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8604 dw_die_ref c;
8605 dw_attr_node *a;
8606 unsigned ix;
8607 struct external_ref *ref_p;
8609 if (is_type_die (die)
8610 && (c = get_AT_ref (die, DW_AT_signature)))
8612 /* This is a local skeleton; use it for local references. */
8613 ref_p = lookup_external_ref (map, c);
8614 ref_p->stub = die;
8617 /* Scan the DIE references, and remember any that refer to DIEs from
8618 other CUs (i.e. those which are not marked). */
8619 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8620 if (AT_class (a) == dw_val_class_die_ref
8621 && (c = AT_ref (a))->die_mark == 0
8622 && is_type_die (c))
8624 ref_p = lookup_external_ref (map, c);
8625 ref_p->n_refs++;
8628 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8631 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8632 points to an external_ref, DATA is the CU we're processing. If we don't
8633 already have a local stub, and we have multiple refs, build a stub. */
8636 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8638 struct external_ref *ref_p = *slot;
8640 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8642 /* We have multiple references to this type, so build a small stub.
8643 Both of these forms are a bit dodgy from the perspective of the
8644 DWARF standard, since technically they should have names. */
8645 dw_die_ref cu = data;
8646 dw_die_ref type = ref_p->type;
8647 dw_die_ref stub = NULL;
8649 if (type->comdat_type_p)
8651 /* If we refer to this type via sig8, use AT_signature. */
8652 stub = new_die (type->die_tag, cu, NULL_TREE);
8653 add_AT_die_ref (stub, DW_AT_signature, type);
8655 else
8657 /* Otherwise, use a typedef with no name. */
8658 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8659 add_AT_die_ref (stub, DW_AT_type, type);
8662 stub->die_mark++;
8663 ref_p->stub = stub;
8665 return 1;
8668 /* DIE is a unit; look through all the DIE references to see if there are
8669 any external references to types, and if so, create local stubs for
8670 them which will be applied in build_abbrev_table. This is useful because
8671 references to local DIEs are smaller. */
8673 static external_ref_hash_type *
8674 optimize_external_refs (dw_die_ref die)
8676 external_ref_hash_type *map = new external_ref_hash_type (10);
8677 optimize_external_refs_1 (die, map);
8678 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8679 return map;
8682 /* The following 3 variables are temporaries that are computed only during the
8683 build_abbrev_table call and used and released during the following
8684 optimize_abbrev_table call. */
8686 /* First abbrev_id that can be optimized based on usage. */
8687 static unsigned int abbrev_opt_start;
8689 /* Maximum abbrev_id of a base type plus one (we can't optimize DIEs with
8690 abbrev_id smaller than this, because they must be already sized
8691 during build_abbrev_table). */
8692 static unsigned int abbrev_opt_base_type_end;
8694 /* Vector of usage counts during build_abbrev_table. Indexed by
8695 abbrev_id - abbrev_opt_start. */
8696 static vec<unsigned int> abbrev_usage_count;
8698 /* Vector of all DIEs added with die_abbrev >= abbrev_opt_start. */
8699 static vec<dw_die_ref> sorted_abbrev_dies;
8701 /* The format of each DIE (and its attribute value pairs) is encoded in an
8702 abbreviation table. This routine builds the abbreviation table and assigns
8703 a unique abbreviation id for each abbreviation entry. The children of each
8704 die are visited recursively. */
8706 static void
8707 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8709 unsigned int abbrev_id = 0;
8710 dw_die_ref c;
8711 dw_attr_node *a;
8712 unsigned ix;
8713 dw_die_ref abbrev;
8715 /* Scan the DIE references, and replace any that refer to
8716 DIEs from other CUs (i.e. those which are not marked) with
8717 the local stubs we built in optimize_external_refs. */
8718 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8719 if (AT_class (a) == dw_val_class_die_ref
8720 && (c = AT_ref (a))->die_mark == 0)
8722 struct external_ref *ref_p;
8723 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8725 ref_p = lookup_external_ref (extern_map, c);
8726 if (ref_p->stub && ref_p->stub != die)
8727 change_AT_die_ref (a, ref_p->stub);
8728 else
8729 /* We aren't changing this reference, so mark it external. */
8730 set_AT_ref_external (a, 1);
8733 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
8735 dw_attr_node *die_a, *abbrev_a;
8736 unsigned ix;
8737 bool ok = true;
8739 if (abbrev_id == 0)
8740 continue;
8741 if (abbrev->die_tag != die->die_tag)
8742 continue;
8743 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8744 continue;
8746 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8747 continue;
8749 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8751 abbrev_a = &(*abbrev->die_attr)[ix];
8752 if ((abbrev_a->dw_attr != die_a->dw_attr)
8753 || (value_format (abbrev_a) != value_format (die_a)))
8755 ok = false;
8756 break;
8759 if (ok)
8760 break;
8763 if (abbrev_id >= vec_safe_length (abbrev_die_table))
8765 vec_safe_push (abbrev_die_table, die);
8766 if (abbrev_opt_start)
8767 abbrev_usage_count.safe_push (0);
8769 if (abbrev_opt_start && abbrev_id >= abbrev_opt_start)
8771 abbrev_usage_count[abbrev_id - abbrev_opt_start]++;
8772 sorted_abbrev_dies.safe_push (die);
8775 die->die_abbrev = abbrev_id;
8776 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8779 /* Callback function for sorted_abbrev_dies vector sorting. We sort
8780 by die_abbrev's usage count, from the most commonly used
8781 abbreviation to the least. */
8783 static int
8784 die_abbrev_cmp (const void *p1, const void *p2)
8786 dw_die_ref die1 = *(const dw_die_ref *) p1;
8787 dw_die_ref die2 = *(const dw_die_ref *) p2;
8789 gcc_checking_assert (die1->die_abbrev >= abbrev_opt_start);
8790 gcc_checking_assert (die2->die_abbrev >= abbrev_opt_start);
8792 if (die1->die_abbrev >= abbrev_opt_base_type_end
8793 && die2->die_abbrev >= abbrev_opt_base_type_end)
8795 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8796 > abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8797 return -1;
8798 if (abbrev_usage_count[die1->die_abbrev - abbrev_opt_start]
8799 < abbrev_usage_count[die2->die_abbrev - abbrev_opt_start])
8800 return 1;
8803 /* Stabilize the sort. */
8804 if (die1->die_abbrev < die2->die_abbrev)
8805 return -1;
8806 if (die1->die_abbrev > die2->die_abbrev)
8807 return 1;
8809 return 0;
8812 /* Convert dw_val_class_const and dw_val_class_unsigned_const class attributes
8813 of DIEs in between sorted_abbrev_dies[first_id] and abbrev_dies[end_id - 1]
8814 into dw_val_class_const_implicit or
8815 dw_val_class_unsigned_const_implicit. */
8817 static void
8818 optimize_implicit_const (unsigned int first_id, unsigned int end,
8819 vec<bool> &implicit_consts)
8821 /* It never makes sense if there is just one DIE using the abbreviation. */
8822 if (end < first_id + 2)
8823 return;
8825 dw_attr_node *a;
8826 unsigned ix, i;
8827 dw_die_ref die = sorted_abbrev_dies[first_id];
8828 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8829 if (implicit_consts[ix])
8831 enum dw_val_class new_class = dw_val_class_none;
8832 switch (AT_class (a))
8834 case dw_val_class_unsigned_const:
8835 if ((HOST_WIDE_INT) AT_unsigned (a) < 0)
8836 continue;
8838 /* The .debug_abbrev section will grow by
8839 size_of_sleb128 (AT_unsigned (a)) and we avoid the constants
8840 in all the DIEs using that abbreviation. */
8841 if (constant_size (AT_unsigned (a)) * (end - first_id)
8842 <= (unsigned) size_of_sleb128 (AT_unsigned (a)))
8843 continue;
8845 new_class = dw_val_class_unsigned_const_implicit;
8846 break;
8848 case dw_val_class_const:
8849 new_class = dw_val_class_const_implicit;
8850 break;
8852 case dw_val_class_file:
8853 new_class = dw_val_class_file_implicit;
8854 break;
8856 default:
8857 continue;
8859 for (i = first_id; i < end; i++)
8860 (*sorted_abbrev_dies[i]->die_attr)[ix].dw_attr_val.val_class
8861 = new_class;
8865 /* Attempt to optimize abbreviation table from abbrev_opt_start
8866 abbreviation above. */
8868 static void
8869 optimize_abbrev_table (void)
8871 if (abbrev_opt_start
8872 && vec_safe_length (abbrev_die_table) > abbrev_opt_start
8873 && (dwarf_version >= 5 || vec_safe_length (abbrev_die_table) > 127))
8875 auto_vec<bool, 32> implicit_consts;
8876 sorted_abbrev_dies.qsort (die_abbrev_cmp);
8878 unsigned int abbrev_id = abbrev_opt_start - 1;
8879 unsigned int first_id = ~0U;
8880 unsigned int last_abbrev_id = 0;
8881 unsigned int i;
8882 dw_die_ref die;
8883 if (abbrev_opt_base_type_end > abbrev_opt_start)
8884 abbrev_id = abbrev_opt_base_type_end - 1;
8885 /* Reassign abbreviation ids from abbrev_opt_start above, so that
8886 most commonly used abbreviations come first. */
8887 FOR_EACH_VEC_ELT (sorted_abbrev_dies, i, die)
8889 dw_attr_node *a;
8890 unsigned ix;
8892 /* If calc_base_type_die_sizes has been called, the CU and
8893 base types after it can't be optimized, because we've already
8894 calculated their DIE offsets. We've sorted them first. */
8895 if (die->die_abbrev < abbrev_opt_base_type_end)
8896 continue;
8897 if (die->die_abbrev != last_abbrev_id)
8899 last_abbrev_id = die->die_abbrev;
8900 if (dwarf_version >= 5 && first_id != ~0U)
8901 optimize_implicit_const (first_id, i, implicit_consts);
8902 abbrev_id++;
8903 (*abbrev_die_table)[abbrev_id] = die;
8904 if (dwarf_version >= 5)
8906 first_id = i;
8907 implicit_consts.truncate (0);
8909 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8910 switch (AT_class (a))
8912 case dw_val_class_const:
8913 case dw_val_class_unsigned_const:
8914 case dw_val_class_file:
8915 implicit_consts.safe_push (true);
8916 break;
8917 default:
8918 implicit_consts.safe_push (false);
8919 break;
8923 else if (dwarf_version >= 5)
8925 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8926 if (!implicit_consts[ix])
8927 continue;
8928 else
8930 dw_attr_node *other_a
8931 = &(*(*abbrev_die_table)[abbrev_id]->die_attr)[ix];
8932 if (!dw_val_equal_p (&a->dw_attr_val,
8933 &other_a->dw_attr_val))
8934 implicit_consts[ix] = false;
8937 die->die_abbrev = abbrev_id;
8939 gcc_assert (abbrev_id == vec_safe_length (abbrev_die_table) - 1);
8940 if (dwarf_version >= 5 && first_id != ~0U)
8941 optimize_implicit_const (first_id, i, implicit_consts);
8944 abbrev_opt_start = 0;
8945 abbrev_opt_base_type_end = 0;
8946 abbrev_usage_count.release ();
8947 sorted_abbrev_dies.release ();
8950 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8952 static int
8953 constant_size (unsigned HOST_WIDE_INT value)
8955 int log;
8957 if (value == 0)
8958 log = 0;
8959 else
8960 log = floor_log2 (value);
8962 log = log / 8;
8963 log = 1 << (floor_log2 (log) + 1);
8965 return log;
8968 /* Return the size of a DIE as it is represented in the
8969 .debug_info section. */
8971 static unsigned long
8972 size_of_die (dw_die_ref die)
8974 unsigned long size = 0;
8975 dw_attr_node *a;
8976 unsigned ix;
8977 enum dwarf_form form;
8979 size += size_of_uleb128 (die->die_abbrev);
8980 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8982 switch (AT_class (a))
8984 case dw_val_class_addr:
8985 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8987 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8988 size += size_of_uleb128 (AT_index (a));
8990 else
8991 size += DWARF2_ADDR_SIZE;
8992 break;
8993 case dw_val_class_offset:
8994 size += DWARF_OFFSET_SIZE;
8995 break;
8996 case dw_val_class_loc:
8998 unsigned long lsize = size_of_locs (AT_loc (a));
9000 /* Block length. */
9001 if (dwarf_version >= 4)
9002 size += size_of_uleb128 (lsize);
9003 else
9004 size += constant_size (lsize);
9005 size += lsize;
9007 break;
9008 case dw_val_class_loc_list:
9009 if (dwarf_split_debug_info && dwarf_version >= 5)
9011 gcc_assert (AT_loc_list (a)->num_assigned);
9012 size += size_of_uleb128 (AT_loc_list (a)->hash);
9014 else
9015 size += DWARF_OFFSET_SIZE;
9016 break;
9017 case dw_val_class_range_list:
9018 if (value_format (a) == DW_FORM_rnglistx)
9020 gcc_assert (rnglist_idx);
9021 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9022 size += size_of_uleb128 (r->idx);
9024 else
9025 size += DWARF_OFFSET_SIZE;
9026 break;
9027 case dw_val_class_const:
9028 size += size_of_sleb128 (AT_int (a));
9029 break;
9030 case dw_val_class_unsigned_const:
9032 int csize = constant_size (AT_unsigned (a));
9033 if (dwarf_version == 3
9034 && a->dw_attr == DW_AT_data_member_location
9035 && csize >= 4)
9036 size += size_of_uleb128 (AT_unsigned (a));
9037 else
9038 size += csize;
9040 break;
9041 case dw_val_class_const_implicit:
9042 case dw_val_class_unsigned_const_implicit:
9043 case dw_val_class_file_implicit:
9044 /* These occupy no size in the DIE, just an extra sleb128 in
9045 .debug_abbrev. */
9046 break;
9047 case dw_val_class_const_double:
9048 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
9049 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
9050 size++; /* block */
9051 break;
9052 case dw_val_class_wide_int:
9053 size += (get_full_len (*a->dw_attr_val.v.val_wide)
9054 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
9055 if (get_full_len (*a->dw_attr_val.v.val_wide)
9056 * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
9057 size++; /* block */
9058 break;
9059 case dw_val_class_vec:
9060 size += constant_size (a->dw_attr_val.v.val_vec.length
9061 * a->dw_attr_val.v.val_vec.elt_size)
9062 + a->dw_attr_val.v.val_vec.length
9063 * a->dw_attr_val.v.val_vec.elt_size; /* block */
9064 break;
9065 case dw_val_class_flag:
9066 if (dwarf_version >= 4)
9067 /* Currently all add_AT_flag calls pass in 1 as last argument,
9068 so DW_FORM_flag_present can be used. If that ever changes,
9069 we'll need to use DW_FORM_flag and have some optimization
9070 in build_abbrev_table that will change those to
9071 DW_FORM_flag_present if it is set to 1 in all DIEs using
9072 the same abbrev entry. */
9073 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9074 else
9075 size += 1;
9076 break;
9077 case dw_val_class_die_ref:
9078 if (AT_ref_external (a))
9080 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
9081 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
9082 is sized by target address length, whereas in DWARF3
9083 it's always sized as an offset. */
9084 if (use_debug_types)
9085 size += DWARF_TYPE_SIGNATURE_SIZE;
9086 else if (dwarf_version == 2)
9087 size += DWARF2_ADDR_SIZE;
9088 else
9089 size += DWARF_OFFSET_SIZE;
9091 else
9092 size += DWARF_OFFSET_SIZE;
9093 break;
9094 case dw_val_class_fde_ref:
9095 size += DWARF_OFFSET_SIZE;
9096 break;
9097 case dw_val_class_lbl_id:
9098 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9100 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
9101 size += size_of_uleb128 (AT_index (a));
9103 else
9104 size += DWARF2_ADDR_SIZE;
9105 break;
9106 case dw_val_class_lineptr:
9107 case dw_val_class_macptr:
9108 case dw_val_class_loclistsptr:
9109 size += DWARF_OFFSET_SIZE;
9110 break;
9111 case dw_val_class_str:
9112 form = AT_string_form (a);
9113 if (form == DW_FORM_strp || form == DW_FORM_line_strp)
9114 size += DWARF_OFFSET_SIZE;
9115 else if (form == DW_FORM_GNU_str_index)
9116 size += size_of_uleb128 (AT_index (a));
9117 else
9118 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9119 break;
9120 case dw_val_class_file:
9121 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9122 break;
9123 case dw_val_class_data8:
9124 size += 8;
9125 break;
9126 case dw_val_class_vms_delta:
9127 size += DWARF_OFFSET_SIZE;
9128 break;
9129 case dw_val_class_high_pc:
9130 size += DWARF2_ADDR_SIZE;
9131 break;
9132 case dw_val_class_discr_value:
9133 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
9134 break;
9135 case dw_val_class_discr_list:
9137 unsigned block_size = size_of_discr_list (AT_discr_list (a));
9139 /* This is a block, so we have the block length and then its
9140 data. */
9141 size += constant_size (block_size) + block_size;
9143 break;
9144 default:
9145 gcc_unreachable ();
9149 return size;
9152 /* Size the debugging information associated with a given DIE. Visits the
9153 DIE's children recursively. Updates the global variable next_die_offset, on
9154 each time through. Uses the current value of next_die_offset to update the
9155 die_offset field in each DIE. */
9157 static void
9158 calc_die_sizes (dw_die_ref die)
9160 dw_die_ref c;
9162 gcc_assert (die->die_offset == 0
9163 || (unsigned long int) die->die_offset == next_die_offset);
9164 die->die_offset = next_die_offset;
9165 next_die_offset += size_of_die (die);
9167 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9169 if (die->die_child != NULL)
9170 /* Count the null byte used to terminate sibling lists. */
9171 next_die_offset += 1;
9174 /* Size just the base type children at the start of the CU.
9175 This is needed because build_abbrev needs to size locs
9176 and sizing of type based stack ops needs to know die_offset
9177 values for the base types. */
9179 static void
9180 calc_base_type_die_sizes (void)
9182 unsigned long die_offset = (dwarf_split_debug_info
9183 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
9184 : DWARF_COMPILE_UNIT_HEADER_SIZE);
9185 unsigned int i;
9186 dw_die_ref base_type;
9187 #if ENABLE_ASSERT_CHECKING
9188 dw_die_ref prev = comp_unit_die ()->die_child;
9189 #endif
9191 die_offset += size_of_die (comp_unit_die ());
9192 for (i = 0; base_types.iterate (i, &base_type); i++)
9194 #if ENABLE_ASSERT_CHECKING
9195 gcc_assert (base_type->die_offset == 0
9196 && prev->die_sib == base_type
9197 && base_type->die_child == NULL
9198 && base_type->die_abbrev);
9199 prev = base_type;
9200 #endif
9201 if (abbrev_opt_start
9202 && base_type->die_abbrev >= abbrev_opt_base_type_end)
9203 abbrev_opt_base_type_end = base_type->die_abbrev + 1;
9204 base_type->die_offset = die_offset;
9205 die_offset += size_of_die (base_type);
9209 /* Set the marks for a die and its children. We do this so
9210 that we know whether or not a reference needs to use FORM_ref_addr; only
9211 DIEs in the same CU will be marked. We used to clear out the offset
9212 and use that as the flag, but ran into ordering problems. */
9214 static void
9215 mark_dies (dw_die_ref die)
9217 dw_die_ref c;
9219 gcc_assert (!die->die_mark);
9221 die->die_mark = 1;
9222 FOR_EACH_CHILD (die, c, mark_dies (c));
9225 /* Clear the marks for a die and its children. */
9227 static void
9228 unmark_dies (dw_die_ref die)
9230 dw_die_ref c;
9232 if (! use_debug_types)
9233 gcc_assert (die->die_mark);
9235 die->die_mark = 0;
9236 FOR_EACH_CHILD (die, c, unmark_dies (c));
9239 /* Clear the marks for a die, its children and referred dies. */
9241 static void
9242 unmark_all_dies (dw_die_ref die)
9244 dw_die_ref c;
9245 dw_attr_node *a;
9246 unsigned ix;
9248 if (!die->die_mark)
9249 return;
9250 die->die_mark = 0;
9252 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9254 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9255 if (AT_class (a) == dw_val_class_die_ref)
9256 unmark_all_dies (AT_ref (a));
9259 /* Calculate if the entry should appear in the final output file. It may be
9260 from a pruned a type. */
9262 static bool
9263 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
9265 /* By limiting gnu pubnames to definitions only, gold can generate a
9266 gdb index without entries for declarations, which don't include
9267 enough information to be useful. */
9268 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
9269 return false;
9271 if (table == pubname_table)
9273 /* Enumerator names are part of the pubname table, but the
9274 parent DW_TAG_enumeration_type die may have been pruned.
9275 Don't output them if that is the case. */
9276 if (p->die->die_tag == DW_TAG_enumerator &&
9277 (p->die->die_parent == NULL
9278 || !p->die->die_parent->die_perennial_p))
9279 return false;
9281 /* Everything else in the pubname table is included. */
9282 return true;
9285 /* The pubtypes table shouldn't include types that have been
9286 pruned. */
9287 return (p->die->die_offset != 0
9288 || !flag_eliminate_unused_debug_types);
9291 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9292 generated for the compilation unit. */
9294 static unsigned long
9295 size_of_pubnames (vec<pubname_entry, va_gc> *names)
9297 unsigned long size;
9298 unsigned i;
9299 pubname_entry *p;
9300 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
9302 size = DWARF_PUBNAMES_HEADER_SIZE;
9303 FOR_EACH_VEC_ELT (*names, i, p)
9304 if (include_pubname_in_output (names, p))
9305 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
9307 size += DWARF_OFFSET_SIZE;
9308 return size;
9311 /* Return the size of the information in the .debug_aranges section. */
9313 static unsigned long
9314 size_of_aranges (void)
9316 unsigned long size;
9318 size = DWARF_ARANGES_HEADER_SIZE;
9320 /* Count the address/length pair for this compilation unit. */
9321 if (text_section_used)
9322 size += 2 * DWARF2_ADDR_SIZE;
9323 if (cold_text_section_used)
9324 size += 2 * DWARF2_ADDR_SIZE;
9325 if (have_multiple_function_sections)
9327 unsigned fde_idx;
9328 dw_fde_ref fde;
9330 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9332 if (DECL_IGNORED_P (fde->decl))
9333 continue;
9334 if (!fde->in_std_section)
9335 size += 2 * DWARF2_ADDR_SIZE;
9336 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9337 size += 2 * DWARF2_ADDR_SIZE;
9341 /* Count the two zero words used to terminated the address range table. */
9342 size += 2 * DWARF2_ADDR_SIZE;
9343 return size;
9346 /* Select the encoding of an attribute value. */
9348 static enum dwarf_form
9349 value_format (dw_attr_node *a)
9351 switch (AT_class (a))
9353 case dw_val_class_addr:
9354 /* Only very few attributes allow DW_FORM_addr. */
9355 switch (a->dw_attr)
9357 case DW_AT_low_pc:
9358 case DW_AT_high_pc:
9359 case DW_AT_entry_pc:
9360 case DW_AT_trampoline:
9361 return (AT_index (a) == NOT_INDEXED
9362 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9363 default:
9364 break;
9366 switch (DWARF2_ADDR_SIZE)
9368 case 1:
9369 return DW_FORM_data1;
9370 case 2:
9371 return DW_FORM_data2;
9372 case 4:
9373 return DW_FORM_data4;
9374 case 8:
9375 return DW_FORM_data8;
9376 default:
9377 gcc_unreachable ();
9379 case dw_val_class_loc_list:
9380 if (dwarf_split_debug_info
9381 && dwarf_version >= 5
9382 && AT_loc_list (a)->num_assigned)
9383 return DW_FORM_loclistx;
9384 /* FALLTHRU */
9385 case dw_val_class_range_list:
9386 /* For range lists in DWARF 5, use DW_FORM_rnglistx from .debug_info.dwo
9387 but in .debug_info use DW_FORM_sec_offset, which is shorter if we
9388 care about sizes of .debug* sections in shared libraries and
9389 executables and don't take into account relocations that affect just
9390 relocatable objects - for DW_FORM_rnglistx we'd have to emit offset
9391 table in the .debug_rnglists section. */
9392 if (dwarf_split_debug_info
9393 && dwarf_version >= 5
9394 && AT_class (a) == dw_val_class_range_list
9395 && rnglist_idx
9396 && a->dw_attr_val.val_entry != RELOCATED_OFFSET)
9397 return DW_FORM_rnglistx;
9398 if (dwarf_version >= 4)
9399 return DW_FORM_sec_offset;
9400 /* FALLTHRU */
9401 case dw_val_class_vms_delta:
9402 case dw_val_class_offset:
9403 switch (DWARF_OFFSET_SIZE)
9405 case 4:
9406 return DW_FORM_data4;
9407 case 8:
9408 return DW_FORM_data8;
9409 default:
9410 gcc_unreachable ();
9412 case dw_val_class_loc:
9413 if (dwarf_version >= 4)
9414 return DW_FORM_exprloc;
9415 switch (constant_size (size_of_locs (AT_loc (a))))
9417 case 1:
9418 return DW_FORM_block1;
9419 case 2:
9420 return DW_FORM_block2;
9421 case 4:
9422 return DW_FORM_block4;
9423 default:
9424 gcc_unreachable ();
9426 case dw_val_class_const:
9427 return DW_FORM_sdata;
9428 case dw_val_class_unsigned_const:
9429 switch (constant_size (AT_unsigned (a)))
9431 case 1:
9432 return DW_FORM_data1;
9433 case 2:
9434 return DW_FORM_data2;
9435 case 4:
9436 /* In DWARF3 DW_AT_data_member_location with
9437 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
9438 constant, so we need to use DW_FORM_udata if we need
9439 a large constant. */
9440 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9441 return DW_FORM_udata;
9442 return DW_FORM_data4;
9443 case 8:
9444 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
9445 return DW_FORM_udata;
9446 return DW_FORM_data8;
9447 default:
9448 gcc_unreachable ();
9450 case dw_val_class_const_implicit:
9451 case dw_val_class_unsigned_const_implicit:
9452 case dw_val_class_file_implicit:
9453 return DW_FORM_implicit_const;
9454 case dw_val_class_const_double:
9455 switch (HOST_BITS_PER_WIDE_INT)
9457 case 8:
9458 return DW_FORM_data2;
9459 case 16:
9460 return DW_FORM_data4;
9461 case 32:
9462 return DW_FORM_data8;
9463 case 64:
9464 if (dwarf_version >= 5)
9465 return DW_FORM_data16;
9466 /* FALLTHRU */
9467 default:
9468 return DW_FORM_block1;
9470 case dw_val_class_wide_int:
9471 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
9473 case 8:
9474 return DW_FORM_data1;
9475 case 16:
9476 return DW_FORM_data2;
9477 case 32:
9478 return DW_FORM_data4;
9479 case 64:
9480 return DW_FORM_data8;
9481 case 128:
9482 if (dwarf_version >= 5)
9483 return DW_FORM_data16;
9484 /* FALLTHRU */
9485 default:
9486 return DW_FORM_block1;
9488 case dw_val_class_vec:
9489 switch (constant_size (a->dw_attr_val.v.val_vec.length
9490 * a->dw_attr_val.v.val_vec.elt_size))
9492 case 1:
9493 return DW_FORM_block1;
9494 case 2:
9495 return DW_FORM_block2;
9496 case 4:
9497 return DW_FORM_block4;
9498 default:
9499 gcc_unreachable ();
9501 case dw_val_class_flag:
9502 if (dwarf_version >= 4)
9504 /* Currently all add_AT_flag calls pass in 1 as last argument,
9505 so DW_FORM_flag_present can be used. If that ever changes,
9506 we'll need to use DW_FORM_flag and have some optimization
9507 in build_abbrev_table that will change those to
9508 DW_FORM_flag_present if it is set to 1 in all DIEs using
9509 the same abbrev entry. */
9510 gcc_assert (a->dw_attr_val.v.val_flag == 1);
9511 return DW_FORM_flag_present;
9513 return DW_FORM_flag;
9514 case dw_val_class_die_ref:
9515 if (AT_ref_external (a))
9516 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
9517 else
9518 return DW_FORM_ref;
9519 case dw_val_class_fde_ref:
9520 return DW_FORM_data;
9521 case dw_val_class_lbl_id:
9522 return (AT_index (a) == NOT_INDEXED
9523 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
9524 case dw_val_class_lineptr:
9525 case dw_val_class_macptr:
9526 case dw_val_class_loclistsptr:
9527 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
9528 case dw_val_class_str:
9529 return AT_string_form (a);
9530 case dw_val_class_file:
9531 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
9533 case 1:
9534 return DW_FORM_data1;
9535 case 2:
9536 return DW_FORM_data2;
9537 case 4:
9538 return DW_FORM_data4;
9539 default:
9540 gcc_unreachable ();
9543 case dw_val_class_data8:
9544 return DW_FORM_data8;
9546 case dw_val_class_high_pc:
9547 switch (DWARF2_ADDR_SIZE)
9549 case 1:
9550 return DW_FORM_data1;
9551 case 2:
9552 return DW_FORM_data2;
9553 case 4:
9554 return DW_FORM_data4;
9555 case 8:
9556 return DW_FORM_data8;
9557 default:
9558 gcc_unreachable ();
9561 case dw_val_class_discr_value:
9562 return (a->dw_attr_val.v.val_discr_value.pos
9563 ? DW_FORM_udata
9564 : DW_FORM_sdata);
9565 case dw_val_class_discr_list:
9566 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
9568 case 1:
9569 return DW_FORM_block1;
9570 case 2:
9571 return DW_FORM_block2;
9572 case 4:
9573 return DW_FORM_block4;
9574 default:
9575 gcc_unreachable ();
9578 default:
9579 gcc_unreachable ();
9583 /* Output the encoding of an attribute value. */
9585 static void
9586 output_value_format (dw_attr_node *a)
9588 enum dwarf_form form = value_format (a);
9590 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
9593 /* Given a die and id, produce the appropriate abbreviations. */
9595 static void
9596 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
9598 unsigned ix;
9599 dw_attr_node *a_attr;
9601 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
9602 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
9603 dwarf_tag_name (abbrev->die_tag));
9605 if (abbrev->die_child != NULL)
9606 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
9607 else
9608 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
9610 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
9612 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
9613 dwarf_attr_name (a_attr->dw_attr));
9614 output_value_format (a_attr);
9615 if (value_format (a_attr) == DW_FORM_implicit_const)
9617 if (AT_class (a_attr) == dw_val_class_file_implicit)
9619 int f = maybe_emit_file (a_attr->dw_attr_val.v.val_file);
9620 const char *filename = a_attr->dw_attr_val.v.val_file->filename;
9621 dw2_asm_output_data_sleb128 (f, "(%s)", filename);
9623 else
9624 dw2_asm_output_data_sleb128 (a_attr->dw_attr_val.v.val_int, NULL);
9628 dw2_asm_output_data (1, 0, NULL);
9629 dw2_asm_output_data (1, 0, NULL);
9633 /* Output the .debug_abbrev section which defines the DIE abbreviation
9634 table. */
9636 static void
9637 output_abbrev_section (void)
9639 unsigned int abbrev_id;
9640 dw_die_ref abbrev;
9642 FOR_EACH_VEC_SAFE_ELT (abbrev_die_table, abbrev_id, abbrev)
9643 if (abbrev_id != 0)
9644 output_die_abbrevs (abbrev_id, abbrev);
9646 /* Terminate the table. */
9647 dw2_asm_output_data (1, 0, NULL);
9650 /* Return a new location list, given the begin and end range, and the
9651 expression. */
9653 static inline dw_loc_list_ref
9654 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9655 const char *section)
9657 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9659 retlist->begin = begin;
9660 retlist->begin_entry = NULL;
9661 retlist->end = end;
9662 retlist->expr = expr;
9663 retlist->section = section;
9665 return retlist;
9668 /* Generate a new internal symbol for this location list node, if it
9669 hasn't got one yet. */
9671 static inline void
9672 gen_llsym (dw_loc_list_ref list)
9674 gcc_assert (!list->ll_symbol);
9675 list->ll_symbol = gen_internal_sym ("LLST");
9678 /* Output the location list given to us. */
9680 static void
9681 output_loc_list (dw_loc_list_ref list_head)
9683 if (list_head->emitted)
9684 return;
9685 list_head->emitted = true;
9687 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9689 dw_loc_list_ref curr = list_head;
9690 const char *last_section = NULL;
9691 const char *base_label = NULL;
9693 /* Walk the location list, and output each range + expression. */
9694 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9696 unsigned long size;
9697 /* Don't output an entry that starts and ends at the same address. */
9698 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9699 continue;
9700 size = size_of_locs (curr->expr);
9701 /* If the expression is too large, drop it on the floor. We could
9702 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9703 in the expression, but >= 64KB expressions for a single value
9704 in a single range are unlikely very useful. */
9705 if (dwarf_version < 5 && size > 0xffff)
9706 continue;
9707 if (dwarf_version >= 5)
9709 if (dwarf_split_debug_info)
9711 /* For -gsplit-dwarf, emit DW_LLE_starx_length, which has
9712 uleb128 index into .debug_addr and uleb128 length. */
9713 dw2_asm_output_data (1, DW_LLE_startx_length,
9714 "DW_LLE_startx_length (%s)",
9715 list_head->ll_symbol);
9716 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9717 "Location list range start index "
9718 "(%s)", curr->begin);
9719 /* FIXME: This will ICE ifndef HAVE_AS_LEB128.
9720 For that case we probably need to emit DW_LLE_startx_endx,
9721 but we'd need 2 .debug_addr entries rather than just one. */
9722 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9723 "Location list length (%s)",
9724 list_head->ll_symbol);
9726 else if (!have_multiple_function_sections && HAVE_AS_LEB128)
9728 /* If all code is in .text section, the base address is
9729 already provided by the CU attributes. Use
9730 DW_LLE_offset_pair where both addresses are uleb128 encoded
9731 offsets against that base. */
9732 dw2_asm_output_data (1, DW_LLE_offset_pair,
9733 "DW_LLE_offset_pair (%s)",
9734 list_head->ll_symbol);
9735 dw2_asm_output_delta_uleb128 (curr->begin, curr->section,
9736 "Location list begin address (%s)",
9737 list_head->ll_symbol);
9738 dw2_asm_output_delta_uleb128 (curr->end, curr->section,
9739 "Location list end address (%s)",
9740 list_head->ll_symbol);
9742 else if (HAVE_AS_LEB128)
9744 /* Otherwise, find out how many consecutive entries could share
9745 the same base entry. If just one, emit DW_LLE_start_length,
9746 otherwise emit DW_LLE_base_address for the base address
9747 followed by a series of DW_LLE_offset_pair. */
9748 if (last_section == NULL || curr->section != last_section)
9750 dw_loc_list_ref curr2;
9751 for (curr2 = curr->dw_loc_next; curr2 != NULL;
9752 curr2 = curr2->dw_loc_next)
9754 if (strcmp (curr2->begin, curr2->end) == 0
9755 && !curr2->force)
9756 continue;
9757 break;
9759 if (curr2 == NULL || curr->section != curr2->section)
9760 last_section = NULL;
9761 else
9763 last_section = curr->section;
9764 base_label = curr->begin;
9765 dw2_asm_output_data (1, DW_LLE_base_address,
9766 "DW_LLE_base_address (%s)",
9767 list_head->ll_symbol);
9768 dw2_asm_output_addr (DWARF2_ADDR_SIZE, base_label,
9769 "Base address (%s)",
9770 list_head->ll_symbol);
9773 /* Only one entry with the same base address. Use
9774 DW_LLE_start_length with absolute address and uleb128
9775 length. */
9776 if (last_section == NULL)
9778 dw2_asm_output_data (1, DW_LLE_start_length,
9779 "DW_LLE_start_length (%s)",
9780 list_head->ll_symbol);
9781 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9782 "Location list begin address (%s)",
9783 list_head->ll_symbol);
9784 dw2_asm_output_delta_uleb128 (curr->end, curr->begin,
9785 "Location list length "
9786 "(%s)", list_head->ll_symbol);
9788 /* Otherwise emit DW_LLE_offset_pair, relative to above emitted
9789 DW_LLE_base_address. */
9790 else
9792 dw2_asm_output_data (1, DW_LLE_offset_pair,
9793 "DW_LLE_offset_pair (%s)",
9794 list_head->ll_symbol);
9795 dw2_asm_output_delta_uleb128 (curr->begin, base_label,
9796 "Location list begin address "
9797 "(%s)", list_head->ll_symbol);
9798 dw2_asm_output_delta_uleb128 (curr->end, base_label,
9799 "Location list end address "
9800 "(%s)", list_head->ll_symbol);
9803 /* The assembler does not support .uleb128 directive. Emit
9804 DW_LLE_start_end with a pair of absolute addresses. */
9805 else
9807 dw2_asm_output_data (1, DW_LLE_start_end,
9808 "DW_LLE_start_end (%s)",
9809 list_head->ll_symbol);
9810 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9811 "Location list begin address (%s)",
9812 list_head->ll_symbol);
9813 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9814 "Location list end address (%s)",
9815 list_head->ll_symbol);
9818 else if (dwarf_split_debug_info)
9820 /* For -gsplit-dwarf -gdwarf-{2,3,4} emit index into .debug_addr
9821 and 4 byte length. */
9822 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9823 "Location list start/length entry (%s)",
9824 list_head->ll_symbol);
9825 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9826 "Location list range start index (%s)",
9827 curr->begin);
9828 /* The length field is 4 bytes. If we ever need to support
9829 an 8-byte length, we can add a new DW_LLE code or fall back
9830 to DW_LLE_GNU_start_end_entry. */
9831 dw2_asm_output_delta (4, curr->end, curr->begin,
9832 "Location list range length (%s)",
9833 list_head->ll_symbol);
9835 else if (!have_multiple_function_sections)
9837 /* Pair of relative addresses against start of text section. */
9838 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9839 "Location list begin address (%s)",
9840 list_head->ll_symbol);
9841 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9842 "Location list end address (%s)",
9843 list_head->ll_symbol);
9845 else
9847 /* Pair of absolute addresses. */
9848 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9849 "Location list begin address (%s)",
9850 list_head->ll_symbol);
9851 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9852 "Location list end address (%s)",
9853 list_head->ll_symbol);
9856 /* Output the block length for this list of location operations. */
9857 if (dwarf_version >= 5)
9858 dw2_asm_output_data_uleb128 (size, "Location expression size");
9859 else
9861 gcc_assert (size <= 0xffff);
9862 dw2_asm_output_data (2, size, "Location expression size");
9865 output_loc_sequence (curr->expr, -1);
9868 /* And finally list termination. */
9869 if (dwarf_version >= 5)
9870 dw2_asm_output_data (1, DW_LLE_end_of_list,
9871 "DW_LLE_end_of_list (%s)", list_head->ll_symbol);
9872 else if (dwarf_split_debug_info)
9873 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9874 "Location list terminator (%s)",
9875 list_head->ll_symbol);
9876 else
9878 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9879 "Location list terminator begin (%s)",
9880 list_head->ll_symbol);
9881 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9882 "Location list terminator end (%s)",
9883 list_head->ll_symbol);
9887 /* Output a range_list offset into the .debug_ranges or .debug_rnglists
9888 section. Emit a relocated reference if val_entry is NULL, otherwise,
9889 emit an indirect reference. */
9891 static void
9892 output_range_list_offset (dw_attr_node *a)
9894 const char *name = dwarf_attr_name (a->dw_attr);
9896 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9898 if (dwarf_version >= 5)
9900 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9901 dw2_asm_output_offset (DWARF_OFFSET_SIZE, r->label,
9902 debug_ranges_section, "%s", name);
9904 else
9906 char *p = strchr (ranges_section_label, '\0');
9907 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
9908 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE);
9909 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9910 debug_ranges_section, "%s", name);
9911 *p = '\0';
9914 else if (dwarf_version >= 5)
9916 dw_ranges *r = &(*ranges_table)[a->dw_attr_val.v.val_offset];
9917 gcc_assert (rnglist_idx);
9918 dw2_asm_output_data_uleb128 (r->idx, "%s", name);
9920 else
9921 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9922 a->dw_attr_val.v.val_offset * 2 * DWARF2_ADDR_SIZE,
9923 "%s (offset from %s)", name, ranges_section_label);
9926 /* Output the offset into the debug_loc section. */
9928 static void
9929 output_loc_list_offset (dw_attr_node *a)
9931 char *sym = AT_loc_list (a)->ll_symbol;
9933 gcc_assert (sym);
9934 if (!dwarf_split_debug_info)
9935 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9936 "%s", dwarf_attr_name (a->dw_attr));
9937 else if (dwarf_version >= 5)
9939 gcc_assert (AT_loc_list (a)->num_assigned);
9940 dw2_asm_output_data_uleb128 (AT_loc_list (a)->hash, "%s (%s)",
9941 dwarf_attr_name (a->dw_attr),
9942 sym);
9944 else
9945 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9946 "%s", dwarf_attr_name (a->dw_attr));
9949 /* Output an attribute's index or value appropriately. */
9951 static void
9952 output_attr_index_or_value (dw_attr_node *a)
9954 const char *name = dwarf_attr_name (a->dw_attr);
9956 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9958 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9959 return;
9961 switch (AT_class (a))
9963 case dw_val_class_addr:
9964 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9965 break;
9966 case dw_val_class_high_pc:
9967 case dw_val_class_lbl_id:
9968 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9969 break;
9970 default:
9971 gcc_unreachable ();
9975 /* Output a type signature. */
9977 static inline void
9978 output_signature (const char *sig, const char *name)
9980 int i;
9982 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9983 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9986 /* Output a discriminant value. */
9988 static inline void
9989 output_discr_value (dw_discr_value *discr_value, const char *name)
9991 if (discr_value->pos)
9992 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9993 else
9994 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9997 /* Output the DIE and its attributes. Called recursively to generate
9998 the definitions of each child DIE. */
10000 static void
10001 output_die (dw_die_ref die)
10003 dw_attr_node *a;
10004 dw_die_ref c;
10005 unsigned long size;
10006 unsigned ix;
10008 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10009 (unsigned long)die->die_offset,
10010 dwarf_tag_name (die->die_tag));
10012 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
10014 const char *name = dwarf_attr_name (a->dw_attr);
10016 switch (AT_class (a))
10018 case dw_val_class_addr:
10019 output_attr_index_or_value (a);
10020 break;
10022 case dw_val_class_offset:
10023 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10024 "%s", name);
10025 break;
10027 case dw_val_class_range_list:
10028 output_range_list_offset (a);
10029 break;
10031 case dw_val_class_loc:
10032 size = size_of_locs (AT_loc (a));
10034 /* Output the block length for this list of location operations. */
10035 if (dwarf_version >= 4)
10036 dw2_asm_output_data_uleb128 (size, "%s", name);
10037 else
10038 dw2_asm_output_data (constant_size (size), size, "%s", name);
10040 output_loc_sequence (AT_loc (a), -1);
10041 break;
10043 case dw_val_class_const:
10044 /* ??? It would be slightly more efficient to use a scheme like is
10045 used for unsigned constants below, but gdb 4.x does not sign
10046 extend. Gdb 5.x does sign extend. */
10047 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10048 break;
10050 case dw_val_class_unsigned_const:
10052 int csize = constant_size (AT_unsigned (a));
10053 if (dwarf_version == 3
10054 && a->dw_attr == DW_AT_data_member_location
10055 && csize >= 4)
10056 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
10057 else
10058 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
10060 break;
10062 case dw_val_class_const_implicit:
10063 if (flag_debug_asm)
10064 fprintf (asm_out_file, "\t\t\t%s %s ("
10065 HOST_WIDE_INT_PRINT_DEC ")\n",
10066 ASM_COMMENT_START, name, AT_int (a));
10067 break;
10069 case dw_val_class_unsigned_const_implicit:
10070 if (flag_debug_asm)
10071 fprintf (asm_out_file, "\t\t\t%s %s ("
10072 HOST_WIDE_INT_PRINT_HEX ")\n",
10073 ASM_COMMENT_START, name, AT_unsigned (a));
10074 break;
10076 case dw_val_class_const_double:
10078 unsigned HOST_WIDE_INT first, second;
10080 if (HOST_BITS_PER_WIDE_INT >= DWARF_LARGEST_DATA_FORM_BITS)
10081 dw2_asm_output_data (1,
10082 HOST_BITS_PER_DOUBLE_INT
10083 / HOST_BITS_PER_CHAR,
10084 NULL);
10086 if (WORDS_BIG_ENDIAN)
10088 first = a->dw_attr_val.v.val_double.high;
10089 second = a->dw_attr_val.v.val_double.low;
10091 else
10093 first = a->dw_attr_val.v.val_double.low;
10094 second = a->dw_attr_val.v.val_double.high;
10097 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10098 first, "%s", name);
10099 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10100 second, NULL);
10102 break;
10104 case dw_val_class_wide_int:
10106 int i;
10107 int len = get_full_len (*a->dw_attr_val.v.val_wide);
10108 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10109 if (len * HOST_BITS_PER_WIDE_INT > DWARF_LARGEST_DATA_FORM_BITS)
10110 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide)
10111 * l, NULL);
10113 if (WORDS_BIG_ENDIAN)
10114 for (i = len - 1; i >= 0; --i)
10116 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10117 "%s", name);
10118 name = "";
10120 else
10121 for (i = 0; i < len; ++i)
10123 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
10124 "%s", name);
10125 name = "";
10128 break;
10130 case dw_val_class_vec:
10132 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10133 unsigned int len = a->dw_attr_val.v.val_vec.length;
10134 unsigned int i;
10135 unsigned char *p;
10137 dw2_asm_output_data (constant_size (len * elt_size),
10138 len * elt_size, "%s", name);
10139 if (elt_size > sizeof (HOST_WIDE_INT))
10141 elt_size /= 2;
10142 len *= 2;
10144 for (i = 0, p = (unsigned char *) a->dw_attr_val.v.val_vec.array;
10145 i < len;
10146 i++, p += elt_size)
10147 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10148 "fp or vector constant word %u", i);
10149 break;
10152 case dw_val_class_flag:
10153 if (dwarf_version >= 4)
10155 /* Currently all add_AT_flag calls pass in 1 as last argument,
10156 so DW_FORM_flag_present can be used. If that ever changes,
10157 we'll need to use DW_FORM_flag and have some optimization
10158 in build_abbrev_table that will change those to
10159 DW_FORM_flag_present if it is set to 1 in all DIEs using
10160 the same abbrev entry. */
10161 gcc_assert (AT_flag (a) == 1);
10162 if (flag_debug_asm)
10163 fprintf (asm_out_file, "\t\t\t%s %s\n",
10164 ASM_COMMENT_START, name);
10165 break;
10167 dw2_asm_output_data (1, AT_flag (a), "%s", name);
10168 break;
10170 case dw_val_class_loc_list:
10171 output_loc_list_offset (a);
10172 break;
10174 case dw_val_class_die_ref:
10175 if (AT_ref_external (a))
10177 if (AT_ref (a)->comdat_type_p)
10179 comdat_type_node *type_node
10180 = AT_ref (a)->die_id.die_type_node;
10182 gcc_assert (type_node);
10183 output_signature (type_node->signature, name);
10185 else
10187 const char *sym = AT_ref (a)->die_id.die_symbol;
10188 int size;
10190 gcc_assert (sym);
10191 /* In DWARF2, DW_FORM_ref_addr is sized by target address
10192 length, whereas in DWARF3 it's always sized as an
10193 offset. */
10194 if (dwarf_version == 2)
10195 size = DWARF2_ADDR_SIZE;
10196 else
10197 size = DWARF_OFFSET_SIZE;
10198 /* ??? We cannot unconditionally output die_offset if
10199 non-zero - others might create references to those
10200 DIEs via symbols.
10201 And we do not clear its DIE offset after outputting it
10202 (and the label refers to the actual DIEs, not the
10203 DWARF CU unit header which is when using label + offset
10204 would be the correct thing to do).
10205 ??? This is the reason for the with_offset flag. */
10206 if (AT_ref (a)->with_offset)
10207 dw2_asm_output_offset (size, sym, AT_ref (a)->die_offset,
10208 debug_info_section, "%s", name);
10209 else
10210 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10211 name);
10214 else
10216 gcc_assert (AT_ref (a)->die_offset);
10217 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10218 "%s", name);
10220 break;
10222 case dw_val_class_fde_ref:
10224 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
10226 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10227 a->dw_attr_val.v.val_fde_index * 2);
10228 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10229 "%s", name);
10231 break;
10233 case dw_val_class_vms_delta:
10234 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
10235 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
10236 AT_vms_delta2 (a), AT_vms_delta1 (a),
10237 "%s", name);
10238 #else
10239 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
10240 AT_vms_delta2 (a), AT_vms_delta1 (a),
10241 "%s", name);
10242 #endif
10243 break;
10245 case dw_val_class_lbl_id:
10246 output_attr_index_or_value (a);
10247 break;
10249 case dw_val_class_lineptr:
10250 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10251 debug_line_section, "%s", name);
10252 break;
10254 case dw_val_class_macptr:
10255 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10256 debug_macinfo_section, "%s", name);
10257 break;
10259 case dw_val_class_loclistsptr:
10260 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10261 debug_loc_section, "%s", name);
10262 break;
10264 case dw_val_class_str:
10265 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
10266 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10267 a->dw_attr_val.v.val_str->label,
10268 debug_str_section,
10269 "%s: \"%s\"", name, AT_string (a));
10270 else if (a->dw_attr_val.v.val_str->form == DW_FORM_line_strp)
10271 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10272 a->dw_attr_val.v.val_str->label,
10273 debug_line_str_section,
10274 "%s: \"%s\"", name, AT_string (a));
10275 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
10276 dw2_asm_output_data_uleb128 (AT_index (a),
10277 "%s: \"%s\"", name, AT_string (a));
10278 else
10279 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10280 break;
10282 case dw_val_class_file:
10284 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10286 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10287 a->dw_attr_val.v.val_file->filename);
10288 break;
10291 case dw_val_class_file_implicit:
10292 if (flag_debug_asm)
10293 fprintf (asm_out_file, "\t\t\t%s %s (%d, %s)\n",
10294 ASM_COMMENT_START, name,
10295 maybe_emit_file (a->dw_attr_val.v.val_file),
10296 a->dw_attr_val.v.val_file->filename);
10297 break;
10299 case dw_val_class_data8:
10301 int i;
10303 for (i = 0; i < 8; i++)
10304 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10305 i == 0 ? "%s" : NULL, name);
10306 break;
10309 case dw_val_class_high_pc:
10310 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
10311 get_AT_low_pc (die), "DW_AT_high_pc");
10312 break;
10314 case dw_val_class_discr_value:
10315 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
10316 break;
10318 case dw_val_class_discr_list:
10320 dw_discr_list_ref list = AT_discr_list (a);
10321 const int size = size_of_discr_list (list);
10323 /* This is a block, so output its length first. */
10324 dw2_asm_output_data (constant_size (size), size,
10325 "%s: block size", name);
10327 for (; list != NULL; list = list->dw_discr_next)
10329 /* One byte for the discriminant value descriptor, and then as
10330 many LEB128 numbers as required. */
10331 if (list->dw_discr_range)
10332 dw2_asm_output_data (1, DW_DSC_range,
10333 "%s: DW_DSC_range", name);
10334 else
10335 dw2_asm_output_data (1, DW_DSC_label,
10336 "%s: DW_DSC_label", name);
10338 output_discr_value (&list->dw_discr_lower_bound, name);
10339 if (list->dw_discr_range)
10340 output_discr_value (&list->dw_discr_upper_bound, name);
10342 break;
10345 default:
10346 gcc_unreachable ();
10350 FOR_EACH_CHILD (die, c, output_die (c));
10352 /* Add null byte to terminate sibling list. */
10353 if (die->die_child != NULL)
10354 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
10355 (unsigned long) die->die_offset);
10358 /* Output the compilation unit that appears at the beginning of the
10359 .debug_info section, and precedes the DIE descriptions. */
10361 static void
10362 output_compilation_unit_header (enum dwarf_unit_type ut)
10364 if (!XCOFF_DEBUGGING_INFO)
10366 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10367 dw2_asm_output_data (4, 0xffffffff,
10368 "Initial length escape value indicating 64-bit DWARF extension");
10369 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10370 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10371 "Length of Compilation Unit Info");
10374 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10375 if (dwarf_version >= 5)
10377 const char *name;
10378 switch (ut)
10380 case DW_UT_compile: name = "DW_UT_compile"; break;
10381 case DW_UT_type: name = "DW_UT_type"; break;
10382 case DW_UT_split_compile: name = "DW_UT_split_compile"; break;
10383 case DW_UT_split_type: name = "DW_UT_split_type"; break;
10384 default: gcc_unreachable ();
10386 dw2_asm_output_data (1, ut, "%s", name);
10387 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10389 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10390 debug_abbrev_section,
10391 "Offset Into Abbrev. Section");
10392 if (dwarf_version < 5)
10393 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10396 /* Output the compilation unit DIE and its children. */
10398 static void
10399 output_comp_unit (dw_die_ref die, int output_if_empty,
10400 const unsigned char *dwo_id)
10402 const char *secname, *oldsym;
10403 char *tmp;
10405 /* Unless we are outputting main CU, we may throw away empty ones. */
10406 if (!output_if_empty && die->die_child == NULL)
10407 return;
10409 /* Even if there are no children of this DIE, we must output the information
10410 about the compilation unit. Otherwise, on an empty translation unit, we
10411 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
10412 will then complain when examining the file. First mark all the DIEs in
10413 this CU so we know which get local refs. */
10414 mark_dies (die);
10416 external_ref_hash_type *extern_map = optimize_external_refs (die);
10418 /* For now, optimize only the main CU, in order to optimize the rest
10419 we'd need to see all of them earlier. Leave the rest for post-linking
10420 tools like DWZ. */
10421 if (die == comp_unit_die ())
10422 abbrev_opt_start = vec_safe_length (abbrev_die_table);
10424 build_abbrev_table (die, extern_map);
10426 optimize_abbrev_table ();
10428 delete extern_map;
10430 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10431 next_die_offset = (dwo_id
10432 ? DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10433 : DWARF_COMPILE_UNIT_HEADER_SIZE);
10434 calc_die_sizes (die);
10436 oldsym = die->die_id.die_symbol;
10437 if (oldsym && die->comdat_type_p)
10439 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10441 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10442 secname = tmp;
10443 die->die_id.die_symbol = NULL;
10444 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10446 else
10448 switch_to_section (debug_info_section);
10449 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
10450 info_section_emitted = true;
10453 /* For LTO cross unit DIE refs we want a symbol on the start of the
10454 debuginfo section, not on the CU DIE. */
10455 if ((flag_generate_lto || flag_generate_offload) && oldsym)
10457 /* ??? No way to get visibility assembled without a decl. */
10458 tree decl = build_decl (UNKNOWN_LOCATION, VAR_DECL,
10459 get_identifier (oldsym), char_type_node);
10460 TREE_PUBLIC (decl) = true;
10461 TREE_STATIC (decl) = true;
10462 DECL_ARTIFICIAL (decl) = true;
10463 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
10464 DECL_VISIBILITY_SPECIFIED (decl) = true;
10465 targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
10466 #ifdef ASM_WEAKEN_LABEL
10467 /* We prefer a .weak because that handles duplicates from duplicate
10468 archive members in a graceful way. */
10469 ASM_WEAKEN_LABEL (asm_out_file, oldsym);
10470 #else
10471 targetm.asm_out.globalize_label (asm_out_file, oldsym);
10472 #endif
10473 ASM_OUTPUT_LABEL (asm_out_file, oldsym);
10476 /* Output debugging information. */
10477 output_compilation_unit_header (dwo_id
10478 ? DW_UT_split_compile : DW_UT_compile);
10479 if (dwarf_version >= 5)
10481 if (dwo_id != NULL)
10482 for (int i = 0; i < 8; i++)
10483 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10485 output_die (die);
10487 /* Leave the marks on the main CU, so we can check them in
10488 output_pubnames. */
10489 if (oldsym)
10491 unmark_dies (die);
10492 die->die_id.die_symbol = oldsym;
10496 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
10497 and .debug_pubtypes. This is configured per-target, but can be
10498 overridden by the -gpubnames or -gno-pubnames options. */
10500 static inline bool
10501 want_pubnames (void)
10503 if (debug_info_level <= DINFO_LEVEL_TERSE)
10504 return false;
10505 if (debug_generate_pub_sections != -1)
10506 return debug_generate_pub_sections;
10507 return targetm.want_debug_pub_sections;
10510 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
10512 static void
10513 add_AT_pubnames (dw_die_ref die)
10515 if (want_pubnames ())
10516 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
10519 /* Add a string attribute value to a skeleton DIE. */
10521 static inline void
10522 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
10523 const char *str)
10525 dw_attr_node attr;
10526 struct indirect_string_node *node;
10528 if (! skeleton_debug_str_hash)
10529 skeleton_debug_str_hash
10530 = hash_table<indirect_string_hasher>::create_ggc (10);
10532 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
10533 find_string_form (node);
10534 if (node->form == DW_FORM_GNU_str_index)
10535 node->form = DW_FORM_strp;
10537 attr.dw_attr = attr_kind;
10538 attr.dw_attr_val.val_class = dw_val_class_str;
10539 attr.dw_attr_val.val_entry = NULL;
10540 attr.dw_attr_val.v.val_str = node;
10541 add_dwarf_attr (die, &attr);
10544 /* Helper function to generate top-level dies for skeleton debug_info and
10545 debug_types. */
10547 static void
10548 add_top_level_skeleton_die_attrs (dw_die_ref die)
10550 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
10551 const char *comp_dir = comp_dir_string ();
10553 add_skeleton_AT_string (die, dwarf_AT (DW_AT_dwo_name), dwo_file_name);
10554 if (comp_dir != NULL)
10555 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
10556 add_AT_pubnames (die);
10557 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
10560 /* Output skeleton debug sections that point to the dwo file. */
10562 static void
10563 output_skeleton_debug_sections (dw_die_ref comp_unit,
10564 const unsigned char *dwo_id)
10566 /* These attributes will be found in the full debug_info section. */
10567 remove_AT (comp_unit, DW_AT_producer);
10568 remove_AT (comp_unit, DW_AT_language);
10570 switch_to_section (debug_skeleton_info_section);
10571 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
10573 /* Produce the skeleton compilation-unit header. This one differs enough from
10574 a normal CU header that it's better not to call output_compilation_unit
10575 header. */
10576 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10577 dw2_asm_output_data (4, 0xffffffff,
10578 "Initial length escape value indicating 64-bit "
10579 "DWARF extension");
10581 dw2_asm_output_data (DWARF_OFFSET_SIZE,
10582 DWARF_COMPILE_UNIT_SKELETON_HEADER_SIZE
10583 - DWARF_INITIAL_LENGTH_SIZE
10584 + size_of_die (comp_unit),
10585 "Length of Compilation Unit Info");
10586 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
10587 if (dwarf_version >= 5)
10589 dw2_asm_output_data (1, DW_UT_skeleton, "DW_UT_skeleton");
10590 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10592 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
10593 debug_skeleton_abbrev_section,
10594 "Offset Into Abbrev. Section");
10595 if (dwarf_version < 5)
10596 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10597 else
10598 for (int i = 0; i < 8; i++)
10599 dw2_asm_output_data (1, dwo_id[i], i == 0 ? "DWO id" : NULL);
10601 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
10602 output_die (comp_unit);
10604 /* Build the skeleton debug_abbrev section. */
10605 switch_to_section (debug_skeleton_abbrev_section);
10606 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
10608 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
10610 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
10613 /* Output a comdat type unit DIE and its children. */
10615 static void
10616 output_comdat_type_unit (comdat_type_node *node)
10618 const char *secname;
10619 char *tmp;
10620 int i;
10621 #if defined (OBJECT_FORMAT_ELF)
10622 tree comdat_key;
10623 #endif
10625 /* First mark all the DIEs in this CU so we know which get local refs. */
10626 mark_dies (node->root_die);
10628 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
10630 build_abbrev_table (node->root_die, extern_map);
10632 delete extern_map;
10633 extern_map = NULL;
10635 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
10636 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10637 calc_die_sizes (node->root_die);
10639 #if defined (OBJECT_FORMAT_ELF)
10640 if (dwarf_version >= 5)
10642 if (!dwarf_split_debug_info)
10643 secname = ".debug_info";
10644 else
10645 secname = ".debug_info.dwo";
10647 else if (!dwarf_split_debug_info)
10648 secname = ".debug_types";
10649 else
10650 secname = ".debug_types.dwo";
10652 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10653 sprintf (tmp, dwarf_version >= 5 ? "wi." : "wt.");
10654 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10655 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10656 comdat_key = get_identifier (tmp);
10657 targetm.asm_out.named_section (secname,
10658 SECTION_DEBUG | SECTION_LINKONCE,
10659 comdat_key);
10660 #else
10661 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10662 sprintf (tmp, (dwarf_version >= 5
10663 ? ".gnu.linkonce.wi." : ".gnu.linkonce.wt."));
10664 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10665 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10666 secname = tmp;
10667 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10668 #endif
10670 /* Output debugging information. */
10671 output_compilation_unit_header (dwarf_split_debug_info
10672 ? DW_UT_split_type : DW_UT_type);
10673 output_signature (node->signature, "Type Signature");
10674 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10675 "Offset to Type DIE");
10676 output_die (node->root_die);
10678 unmark_dies (node->root_die);
10681 /* Return the DWARF2/3 pubname associated with a decl. */
10683 static const char *
10684 dwarf2_name (tree decl, int scope)
10686 if (DECL_NAMELESS (decl))
10687 return NULL;
10688 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10691 /* Add a new entry to .debug_pubnames if appropriate. */
10693 static void
10694 add_pubname_string (const char *str, dw_die_ref die)
10696 pubname_entry e;
10698 e.die = die;
10699 e.name = xstrdup (str);
10700 vec_safe_push (pubname_table, e);
10703 static void
10704 add_pubname (tree decl, dw_die_ref die)
10706 if (!want_pubnames ())
10707 return;
10709 /* Don't add items to the table when we expect that the consumer will have
10710 just read the enclosing die. For example, if the consumer is looking at a
10711 class_member, it will either be inside the class already, or will have just
10712 looked up the class to find the member. Either way, searching the class is
10713 faster than searching the index. */
10714 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
10715 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10717 const char *name = dwarf2_name (decl, 1);
10719 if (name)
10720 add_pubname_string (name, die);
10724 /* Add an enumerator to the pubnames section. */
10726 static void
10727 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
10729 pubname_entry e;
10731 gcc_assert (scope_name);
10732 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
10733 e.die = die;
10734 vec_safe_push (pubname_table, e);
10737 /* Add a new entry to .debug_pubtypes if appropriate. */
10739 static void
10740 add_pubtype (tree decl, dw_die_ref die)
10742 pubname_entry e;
10744 if (!want_pubnames ())
10745 return;
10747 if ((TREE_PUBLIC (decl)
10748 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
10749 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10751 tree scope = NULL;
10752 const char *scope_name = "";
10753 const char *sep = is_cxx () ? "::" : ".";
10754 const char *name;
10756 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
10757 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
10759 scope_name = lang_hooks.dwarf_name (scope, 1);
10760 if (scope_name != NULL && scope_name[0] != '\0')
10761 scope_name = concat (scope_name, sep, NULL);
10762 else
10763 scope_name = "";
10766 if (TYPE_P (decl))
10767 name = type_tag (decl);
10768 else
10769 name = lang_hooks.dwarf_name (decl, 1);
10771 /* If we don't have a name for the type, there's no point in adding
10772 it to the table. */
10773 if (name != NULL && name[0] != '\0')
10775 e.die = die;
10776 e.name = concat (scope_name, name, NULL);
10777 vec_safe_push (pubtype_table, e);
10780 /* Although it might be more consistent to add the pubinfo for the
10781 enumerators as their dies are created, they should only be added if the
10782 enum type meets the criteria above. So rather than re-check the parent
10783 enum type whenever an enumerator die is created, just output them all
10784 here. This isn't protected by the name conditional because anonymous
10785 enums don't have names. */
10786 if (die->die_tag == DW_TAG_enumeration_type)
10788 dw_die_ref c;
10790 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
10795 /* Output a single entry in the pubnames table. */
10797 static void
10798 output_pubname (dw_offset die_offset, pubname_entry *entry)
10800 dw_die_ref die = entry->die;
10801 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
10803 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
10805 if (debug_generate_pub_sections == 2)
10807 /* This logic follows gdb's method for determining the value of the flag
10808 byte. */
10809 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
10810 switch (die->die_tag)
10812 case DW_TAG_typedef:
10813 case DW_TAG_base_type:
10814 case DW_TAG_subrange_type:
10815 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10816 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10817 break;
10818 case DW_TAG_enumerator:
10819 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10820 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10821 if (!is_cxx ())
10822 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10823 break;
10824 case DW_TAG_subprogram:
10825 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10826 GDB_INDEX_SYMBOL_KIND_FUNCTION);
10827 if (!is_ada ())
10828 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10829 break;
10830 case DW_TAG_constant:
10831 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10832 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10833 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10834 break;
10835 case DW_TAG_variable:
10836 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
10837 GDB_INDEX_SYMBOL_KIND_VARIABLE);
10838 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
10839 break;
10840 case DW_TAG_namespace:
10841 case DW_TAG_imported_declaration:
10842 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10843 break;
10844 case DW_TAG_class_type:
10845 case DW_TAG_interface_type:
10846 case DW_TAG_structure_type:
10847 case DW_TAG_union_type:
10848 case DW_TAG_enumeration_type:
10849 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
10850 if (!is_cxx ())
10851 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
10852 break;
10853 default:
10854 /* An unusual tag. Leave the flag-byte empty. */
10855 break;
10857 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
10858 "GDB-index flags");
10861 dw2_asm_output_nstring (entry->name, -1, "external name");
10865 /* Output the public names table used to speed up access to externally
10866 visible names; or the public types table used to find type definitions. */
10868 static void
10869 output_pubnames (vec<pubname_entry, va_gc> *names)
10871 unsigned i;
10872 unsigned long pubnames_length = size_of_pubnames (names);
10873 pubname_entry *pub;
10875 if (!XCOFF_DEBUGGING_INFO)
10877 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10878 dw2_asm_output_data (4, 0xffffffff,
10879 "Initial length escape value indicating 64-bit DWARF extension");
10880 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10881 "Pub Info Length");
10884 /* Version number for pubnames/pubtypes is independent of dwarf version. */
10885 dw2_asm_output_data (2, 2, "DWARF Version");
10887 if (dwarf_split_debug_info)
10888 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10889 debug_skeleton_info_section,
10890 "Offset of Compilation Unit Info");
10891 else
10892 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10893 debug_info_section,
10894 "Offset of Compilation Unit Info");
10895 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10896 "Compilation Unit Length");
10898 FOR_EACH_VEC_ELT (*names, i, pub)
10900 if (include_pubname_in_output (names, pub))
10902 dw_offset die_offset = pub->die->die_offset;
10904 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10905 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10906 gcc_assert (pub->die->die_mark);
10908 /* If we're putting types in their own .debug_types sections,
10909 the .debug_pubtypes table will still point to the compile
10910 unit (not the type unit), so we want to use the offset of
10911 the skeleton DIE (if there is one). */
10912 if (pub->die->comdat_type_p && names == pubtype_table)
10914 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10916 if (type_node != NULL)
10917 die_offset = (type_node->skeleton_die != NULL
10918 ? type_node->skeleton_die->die_offset
10919 : comp_unit_die ()->die_offset);
10922 output_pubname (die_offset, pub);
10926 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10929 /* Output public names and types tables if necessary. */
10931 static void
10932 output_pubtables (void)
10934 if (!want_pubnames () || !info_section_emitted)
10935 return;
10937 switch_to_section (debug_pubnames_section);
10938 output_pubnames (pubname_table);
10939 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10940 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10941 simply won't look for the section. */
10942 switch_to_section (debug_pubtypes_section);
10943 output_pubnames (pubtype_table);
10947 /* Output the information that goes into the .debug_aranges table.
10948 Namely, define the beginning and ending address range of the
10949 text section generated for this compilation unit. */
10951 static void
10952 output_aranges (void)
10954 unsigned i;
10955 unsigned long aranges_length = size_of_aranges ();
10957 if (!XCOFF_DEBUGGING_INFO)
10959 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10960 dw2_asm_output_data (4, 0xffffffff,
10961 "Initial length escape value indicating 64-bit DWARF extension");
10962 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10963 "Length of Address Ranges Info");
10966 /* Version number for aranges is still 2, even up to DWARF5. */
10967 dw2_asm_output_data (2, 2, "DWARF Version");
10968 if (dwarf_split_debug_info)
10969 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10970 debug_skeleton_info_section,
10971 "Offset of Compilation Unit Info");
10972 else
10973 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10974 debug_info_section,
10975 "Offset of Compilation Unit Info");
10976 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10977 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10979 /* We need to align to twice the pointer size here. */
10980 if (DWARF_ARANGES_PAD_SIZE)
10982 /* Pad using a 2 byte words so that padding is correct for any
10983 pointer size. */
10984 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10985 2 * DWARF2_ADDR_SIZE);
10986 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10987 dw2_asm_output_data (2, 0, NULL);
10990 /* It is necessary not to output these entries if the sections were
10991 not used; if the sections were not used, the length will be 0 and
10992 the address may end up as 0 if the section is discarded by ld
10993 --gc-sections, leaving an invalid (0, 0) entry that can be
10994 confused with the terminator. */
10995 if (text_section_used)
10997 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10998 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10999 text_section_label, "Length");
11001 if (cold_text_section_used)
11003 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11004 "Address");
11005 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11006 cold_text_section_label, "Length");
11009 if (have_multiple_function_sections)
11011 unsigned fde_idx;
11012 dw_fde_ref fde;
11014 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
11016 if (DECL_IGNORED_P (fde->decl))
11017 continue;
11018 if (!fde->in_std_section)
11020 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11021 "Address");
11022 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11023 fde->dw_fde_begin, "Length");
11025 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11027 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11028 "Address");
11029 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11030 fde->dw_fde_second_begin, "Length");
11035 /* Output the terminator words. */
11036 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11037 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11040 /* Add a new entry to .debug_ranges. Return its index into
11041 ranges_table vector. */
11043 static unsigned int
11044 add_ranges_num (int num, bool maybe_new_sec)
11046 dw_ranges r = { NULL, num, 0, maybe_new_sec };
11047 vec_safe_push (ranges_table, r);
11048 return vec_safe_length (ranges_table) - 1;
11051 /* Add a new entry to .debug_ranges corresponding to a block, or a
11052 range terminator if BLOCK is NULL. MAYBE_NEW_SEC is true if
11053 this entry might be in a different section from previous range. */
11055 static unsigned int
11056 add_ranges (const_tree block, bool maybe_new_sec)
11058 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0, maybe_new_sec);
11061 /* Note that (*rnglist_table)[offset] is either a head of a rnglist
11062 chain, or middle entry of a chain that will be directly referred to. */
11064 static void
11065 note_rnglist_head (unsigned int offset)
11067 if (dwarf_version < 5 || (*ranges_table)[offset].label)
11068 return;
11069 (*ranges_table)[offset].label = gen_internal_sym ("LLRL");
11072 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
11073 When using dwarf_split_debug_info, address attributes in dies destined
11074 for the final executable should be direct references--setting the
11075 parameter force_direct ensures this behavior. */
11077 static void
11078 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11079 bool *added, bool force_direct)
11081 unsigned int in_use = vec_safe_length (ranges_by_label);
11082 unsigned int offset;
11083 dw_ranges_by_label rbl = { begin, end };
11084 vec_safe_push (ranges_by_label, rbl);
11085 offset = add_ranges_num (-(int)in_use - 1, true);
11086 if (!*added)
11088 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
11089 *added = true;
11090 note_rnglist_head (offset);
11094 /* Emit .debug_ranges section. */
11096 static void
11097 output_ranges (void)
11099 unsigned i;
11100 static const char *const start_fmt = "Offset %#x";
11101 const char *fmt = start_fmt;
11102 dw_ranges *r;
11104 switch_to_section (debug_ranges_section);
11105 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11106 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11108 int block_num = r->num;
11110 if (block_num > 0)
11112 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11113 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11115 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11116 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11118 /* If all code is in the text section, then the compilation
11119 unit base address defaults to DW_AT_low_pc, which is the
11120 base of the text section. */
11121 if (!have_multiple_function_sections)
11123 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11124 text_section_label,
11125 fmt, i * 2 * DWARF2_ADDR_SIZE);
11126 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11127 text_section_label, NULL);
11130 /* Otherwise, the compilation unit base address is zero,
11131 which allows us to use absolute addresses, and not worry
11132 about whether the target supports cross-section
11133 arithmetic. */
11134 else
11136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11137 fmt, i * 2 * DWARF2_ADDR_SIZE);
11138 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11141 fmt = NULL;
11144 /* Negative block_num stands for an index into ranges_by_label. */
11145 else if (block_num < 0)
11147 int lab_idx = - block_num - 1;
11149 if (!have_multiple_function_sections)
11151 gcc_unreachable ();
11152 #if 0
11153 /* If we ever use add_ranges_by_labels () for a single
11154 function section, all we have to do is to take out
11155 the #if 0 above. */
11156 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11157 (*ranges_by_label)[lab_idx].begin,
11158 text_section_label,
11159 fmt, i * 2 * DWARF2_ADDR_SIZE);
11160 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11161 (*ranges_by_label)[lab_idx].end,
11162 text_section_label, NULL);
11163 #endif
11165 else
11167 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11168 (*ranges_by_label)[lab_idx].begin,
11169 fmt, i * 2 * DWARF2_ADDR_SIZE);
11170 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11171 (*ranges_by_label)[lab_idx].end,
11172 NULL);
11175 else
11177 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11178 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11179 fmt = start_fmt;
11184 /* Non-zero if .debug_line_str should be used for .debug_line section
11185 strings or strings that are likely shareable with those. */
11186 #define DWARF5_USE_DEBUG_LINE_STR \
11187 (!DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET \
11188 && (DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) != 0 \
11189 /* FIXME: there is no .debug_line_str.dwo section, \
11190 for -gsplit-dwarf we should use DW_FORM_strx instead. */ \
11191 && !dwarf_split_debug_info)
11193 /* Assign .debug_rnglists indexes. */
11195 static void
11196 index_rnglists (void)
11198 unsigned i;
11199 dw_ranges *r;
11201 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11202 if (r->label)
11203 r->idx = rnglist_idx++;
11206 /* Emit .debug_rnglists section. */
11208 static void
11209 output_rnglists (unsigned generation)
11211 unsigned i;
11212 dw_ranges *r;
11213 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
11214 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
11215 char basebuf[MAX_ARTIFICIAL_LABEL_BYTES];
11217 switch_to_section (debug_ranges_section);
11218 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
11219 /* There are up to 4 unique ranges labels per generation.
11220 See also init_sections_and_labels. */
11221 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_RANGES_SECTION_LABEL,
11222 2 + generation * 4);
11223 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_RANGES_SECTION_LABEL,
11224 3 + generation * 4);
11225 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11226 dw2_asm_output_data (4, 0xffffffff,
11227 "Initial length escape value indicating "
11228 "64-bit DWARF extension");
11229 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11230 "Length of Range Lists");
11231 ASM_OUTPUT_LABEL (asm_out_file, l1);
11232 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11233 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
11234 dw2_asm_output_data (1, 0, "Segment Size");
11235 /* Emit the offset table only for -gsplit-dwarf. If we don't care
11236 about relocation sizes and primarily care about the size of .debug*
11237 sections in linked shared libraries and executables, then
11238 the offset table plus corresponding DW_FORM_rnglistx uleb128 indexes
11239 into it are usually larger than just DW_FORM_sec_offset offsets
11240 into the .debug_rnglists section. */
11241 dw2_asm_output_data (4, dwarf_split_debug_info ? rnglist_idx : 0,
11242 "Offset Entry Count");
11243 if (dwarf_split_debug_info)
11245 ASM_OUTPUT_LABEL (asm_out_file, ranges_base_label);
11246 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11247 if (r->label)
11248 dw2_asm_output_delta (DWARF_OFFSET_SIZE, r->label,
11249 ranges_base_label, NULL);
11252 const char *lab = "";
11253 unsigned int len = vec_safe_length (ranges_table);
11254 const char *base = NULL;
11255 FOR_EACH_VEC_SAFE_ELT (ranges_table, i, r)
11257 int block_num = r->num;
11259 if (r->label)
11261 ASM_OUTPUT_LABEL (asm_out_file, r->label);
11262 lab = r->label;
11264 if (HAVE_AS_LEB128 && (r->label || r->maybe_new_sec))
11265 base = NULL;
11266 if (block_num > 0)
11268 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11269 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11271 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11272 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11274 if (HAVE_AS_LEB128)
11276 /* If all code is in the text section, then the compilation
11277 unit base address defaults to DW_AT_low_pc, which is the
11278 base of the text section. */
11279 if (!have_multiple_function_sections)
11281 dw2_asm_output_data (1, DW_RLE_offset_pair,
11282 "DW_RLE_offset_pair (%s)", lab);
11283 dw2_asm_output_delta_uleb128 (blabel, text_section_label,
11284 "Range begin address (%s)", lab);
11285 dw2_asm_output_delta_uleb128 (elabel, text_section_label,
11286 "Range end address (%s)", lab);
11287 continue;
11289 if (base == NULL)
11291 dw_ranges *r2 = NULL;
11292 if (i < len - 1)
11293 r2 = &(*ranges_table)[i + 1];
11294 if (r2
11295 && r2->num != 0
11296 && r2->label == NULL
11297 && !r2->maybe_new_sec)
11299 dw2_asm_output_data (1, DW_RLE_base_address,
11300 "DW_RLE_base_address (%s)", lab);
11301 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11302 "Base address (%s)", lab);
11303 strcpy (basebuf, blabel);
11304 base = basebuf;
11307 if (base)
11309 dw2_asm_output_data (1, DW_RLE_offset_pair,
11310 "DW_RLE_offset_pair (%s)", lab);
11311 dw2_asm_output_delta_uleb128 (blabel, base,
11312 "Range begin address (%s)", lab);
11313 dw2_asm_output_delta_uleb128 (elabel, base,
11314 "Range end address (%s)", lab);
11315 continue;
11317 dw2_asm_output_data (1, DW_RLE_start_length,
11318 "DW_RLE_start_length (%s)", lab);
11319 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11320 "Range begin address (%s)", lab);
11321 dw2_asm_output_delta_uleb128 (elabel, blabel,
11322 "Range length (%s)", lab);
11324 else
11326 dw2_asm_output_data (1, DW_RLE_start_end,
11327 "DW_RLE_start_end (%s)", lab);
11328 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11329 "Range begin address (%s)", lab);
11330 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11331 "Range end address (%s)", lab);
11335 /* Negative block_num stands for an index into ranges_by_label. */
11336 else if (block_num < 0)
11338 int lab_idx = - block_num - 1;
11339 const char *blabel = (*ranges_by_label)[lab_idx].begin;
11340 const char *elabel = (*ranges_by_label)[lab_idx].end;
11342 if (!have_multiple_function_sections)
11343 gcc_unreachable ();
11344 if (HAVE_AS_LEB128)
11346 dw2_asm_output_data (1, DW_RLE_start_length,
11347 "DW_RLE_start_length (%s)", lab);
11348 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11349 "Range begin address (%s)", lab);
11350 dw2_asm_output_delta_uleb128 (elabel, blabel,
11351 "Range length (%s)", lab);
11353 else
11355 dw2_asm_output_data (1, DW_RLE_start_end,
11356 "DW_RLE_start_end (%s)", lab);
11357 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11358 "Range begin address (%s)", lab);
11359 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel,
11360 "Range end address (%s)", lab);
11363 else
11364 dw2_asm_output_data (1, DW_RLE_end_of_list,
11365 "DW_RLE_end_of_list (%s)", lab);
11367 ASM_OUTPUT_LABEL (asm_out_file, l2);
11370 /* Data structure containing information about input files. */
11371 struct file_info
11373 const char *path; /* Complete file name. */
11374 const char *fname; /* File name part. */
11375 int length; /* Length of entire string. */
11376 struct dwarf_file_data * file_idx; /* Index in input file table. */
11377 int dir_idx; /* Index in directory table. */
11380 /* Data structure containing information about directories with source
11381 files. */
11382 struct dir_info
11384 const char *path; /* Path including directory name. */
11385 int length; /* Path length. */
11386 int prefix; /* Index of directory entry which is a prefix. */
11387 int count; /* Number of files in this directory. */
11388 int dir_idx; /* Index of directory used as base. */
11391 /* Callback function for file_info comparison. We sort by looking at
11392 the directories in the path. */
11394 static int
11395 file_info_cmp (const void *p1, const void *p2)
11397 const struct file_info *const s1 = (const struct file_info *) p1;
11398 const struct file_info *const s2 = (const struct file_info *) p2;
11399 const unsigned char *cp1;
11400 const unsigned char *cp2;
11402 /* Take care of file names without directories. We need to make sure that
11403 we return consistent values to qsort since some will get confused if
11404 we return the same value when identical operands are passed in opposite
11405 orders. So if neither has a directory, return 0 and otherwise return
11406 1 or -1 depending on which one has the directory. */
11407 if ((s1->path == s1->fname || s2->path == s2->fname))
11408 return (s2->path == s2->fname) - (s1->path == s1->fname);
11410 cp1 = (const unsigned char *) s1->path;
11411 cp2 = (const unsigned char *) s2->path;
11413 while (1)
11415 ++cp1;
11416 ++cp2;
11417 /* Reached the end of the first path? If so, handle like above. */
11418 if ((cp1 == (const unsigned char *) s1->fname)
11419 || (cp2 == (const unsigned char *) s2->fname))
11420 return ((cp2 == (const unsigned char *) s2->fname)
11421 - (cp1 == (const unsigned char *) s1->fname));
11423 /* Character of current path component the same? */
11424 else if (*cp1 != *cp2)
11425 return *cp1 - *cp2;
11429 struct file_name_acquire_data
11431 struct file_info *files;
11432 int used_files;
11433 int max_files;
11436 /* Traversal function for the hash table. */
11439 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
11441 struct dwarf_file_data *d = *slot;
11442 struct file_info *fi;
11443 const char *f;
11445 gcc_assert (fnad->max_files >= d->emitted_number);
11447 if (! d->emitted_number)
11448 return 1;
11450 gcc_assert (fnad->max_files != fnad->used_files);
11452 fi = fnad->files + fnad->used_files++;
11454 /* Skip all leading "./". */
11455 f = d->filename;
11456 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11457 f += 2;
11459 /* Create a new array entry. */
11460 fi->path = f;
11461 fi->length = strlen (f);
11462 fi->file_idx = d;
11464 /* Search for the file name part. */
11465 f = strrchr (f, DIR_SEPARATOR);
11466 #if defined (DIR_SEPARATOR_2)
11468 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11470 if (g != NULL)
11472 if (f == NULL || f < g)
11473 f = g;
11476 #endif
11478 fi->fname = f == NULL ? fi->path : f + 1;
11479 return 1;
11482 /* Helper function for output_file_names. Emit a FORM encoded
11483 string STR, with assembly comment start ENTRY_KIND and
11484 index IDX */
11486 static void
11487 output_line_string (enum dwarf_form form, const char *str,
11488 const char *entry_kind, unsigned int idx)
11490 switch (form)
11492 case DW_FORM_string:
11493 dw2_asm_output_nstring (str, -1, "%s: %#x", entry_kind, idx);
11494 break;
11495 case DW_FORM_line_strp:
11496 if (!debug_line_str_hash)
11497 debug_line_str_hash
11498 = hash_table<indirect_string_hasher>::create_ggc (10);
11500 struct indirect_string_node *node;
11501 node = find_AT_string_in_table (str, debug_line_str_hash);
11502 set_indirect_string (node);
11503 node->form = form;
11504 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
11505 debug_line_str_section, "%s: %#x: \"%s\"",
11506 entry_kind, 0, node->str);
11507 break;
11508 default:
11509 gcc_unreachable ();
11513 /* Output the directory table and the file name table. We try to minimize
11514 the total amount of memory needed. A heuristic is used to avoid large
11515 slowdowns with many input files. */
11517 static void
11518 output_file_names (void)
11520 struct file_name_acquire_data fnad;
11521 int numfiles;
11522 struct file_info *files;
11523 struct dir_info *dirs;
11524 int *saved;
11525 int *savehere;
11526 int *backmap;
11527 int ndirs;
11528 int idx_offset;
11529 int i;
11531 if (!last_emitted_file)
11533 if (dwarf_version >= 5)
11535 dw2_asm_output_data (1, 0, "Directory entry format count");
11536 dw2_asm_output_data_uleb128 (0, "Directories count");
11537 dw2_asm_output_data (1, 0, "File name entry format count");
11538 dw2_asm_output_data_uleb128 (0, "File names count");
11540 else
11542 dw2_asm_output_data (1, 0, "End directory table");
11543 dw2_asm_output_data (1, 0, "End file name table");
11545 return;
11548 numfiles = last_emitted_file->emitted_number;
11550 /* Allocate the various arrays we need. */
11551 files = XALLOCAVEC (struct file_info, numfiles);
11552 dirs = XALLOCAVEC (struct dir_info, numfiles);
11554 fnad.files = files;
11555 fnad.used_files = 0;
11556 fnad.max_files = numfiles;
11557 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
11558 gcc_assert (fnad.used_files == fnad.max_files);
11560 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11562 /* Find all the different directories used. */
11563 dirs[0].path = files[0].path;
11564 dirs[0].length = files[0].fname - files[0].path;
11565 dirs[0].prefix = -1;
11566 dirs[0].count = 1;
11567 dirs[0].dir_idx = 0;
11568 files[0].dir_idx = 0;
11569 ndirs = 1;
11571 for (i = 1; i < numfiles; i++)
11572 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11573 && memcmp (dirs[ndirs - 1].path, files[i].path,
11574 dirs[ndirs - 1].length) == 0)
11576 /* Same directory as last entry. */
11577 files[i].dir_idx = ndirs - 1;
11578 ++dirs[ndirs - 1].count;
11580 else
11582 int j;
11584 /* This is a new directory. */
11585 dirs[ndirs].path = files[i].path;
11586 dirs[ndirs].length = files[i].fname - files[i].path;
11587 dirs[ndirs].count = 1;
11588 dirs[ndirs].dir_idx = ndirs;
11589 files[i].dir_idx = ndirs;
11591 /* Search for a prefix. */
11592 dirs[ndirs].prefix = -1;
11593 for (j = 0; j < ndirs; j++)
11594 if (dirs[j].length < dirs[ndirs].length
11595 && dirs[j].length > 1
11596 && (dirs[ndirs].prefix == -1
11597 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11598 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11599 dirs[ndirs].prefix = j;
11601 ++ndirs;
11604 /* Now to the actual work. We have to find a subset of the directories which
11605 allow expressing the file name using references to the directory table
11606 with the least amount of characters. We do not do an exhaustive search
11607 where we would have to check out every combination of every single
11608 possible prefix. Instead we use a heuristic which provides nearly optimal
11609 results in most cases and never is much off. */
11610 saved = XALLOCAVEC (int, ndirs);
11611 savehere = XALLOCAVEC (int, ndirs);
11613 memset (saved, '\0', ndirs * sizeof (saved[0]));
11614 for (i = 0; i < ndirs; i++)
11616 int j;
11617 int total;
11619 /* We can always save some space for the current directory. But this
11620 does not mean it will be enough to justify adding the directory. */
11621 savehere[i] = dirs[i].length;
11622 total = (savehere[i] - saved[i]) * dirs[i].count;
11624 for (j = i + 1; j < ndirs; j++)
11626 savehere[j] = 0;
11627 if (saved[j] < dirs[i].length)
11629 /* Determine whether the dirs[i] path is a prefix of the
11630 dirs[j] path. */
11631 int k;
11633 k = dirs[j].prefix;
11634 while (k != -1 && k != (int) i)
11635 k = dirs[k].prefix;
11637 if (k == (int) i)
11639 /* Yes it is. We can possibly save some memory by
11640 writing the filenames in dirs[j] relative to
11641 dirs[i]. */
11642 savehere[j] = dirs[i].length;
11643 total += (savehere[j] - saved[j]) * dirs[j].count;
11648 /* Check whether we can save enough to justify adding the dirs[i]
11649 directory. */
11650 if (total > dirs[i].length + 1)
11652 /* It's worthwhile adding. */
11653 for (j = i; j < ndirs; j++)
11654 if (savehere[j] > 0)
11656 /* Remember how much we saved for this directory so far. */
11657 saved[j] = savehere[j];
11659 /* Remember the prefix directory. */
11660 dirs[j].dir_idx = i;
11665 /* Emit the directory name table. */
11666 idx_offset = dirs[0].length > 0 ? 1 : 0;
11667 enum dwarf_form str_form = DW_FORM_string;
11668 enum dwarf_form idx_form = DW_FORM_udata;
11669 if (dwarf_version >= 5)
11671 const char *comp_dir = comp_dir_string ();
11672 if (comp_dir == NULL)
11673 comp_dir = "";
11674 dw2_asm_output_data (1, 1, "Directory entry format count");
11675 if (DWARF5_USE_DEBUG_LINE_STR)
11676 str_form = DW_FORM_line_strp;
11677 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11678 dw2_asm_output_data_uleb128 (str_form, "%s",
11679 get_DW_FORM_name (str_form));
11680 dw2_asm_output_data_uleb128 (ndirs + idx_offset, "Directories count");
11681 if (str_form == DW_FORM_string)
11683 dw2_asm_output_nstring (comp_dir, -1, "Directory Entry: %#x", 0);
11684 for (i = 1 - idx_offset; i < ndirs; i++)
11685 dw2_asm_output_nstring (dirs[i].path,
11686 dirs[i].length
11687 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11688 "Directory Entry: %#x", i + idx_offset);
11690 else
11692 output_line_string (str_form, comp_dir, "Directory Entry", 0);
11693 for (i = 1 - idx_offset; i < ndirs; i++)
11695 const char *str
11696 = ggc_alloc_string (dirs[i].path,
11697 dirs[i].length
11698 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR);
11699 output_line_string (str_form, str, "Directory Entry",
11700 (unsigned) i + idx_offset);
11704 else
11706 for (i = 1 - idx_offset; i < ndirs; i++)
11707 dw2_asm_output_nstring (dirs[i].path,
11708 dirs[i].length
11709 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11710 "Directory Entry: %#x", i + idx_offset);
11712 dw2_asm_output_data (1, 0, "End directory table");
11715 /* We have to emit them in the order of emitted_number since that's
11716 used in the debug info generation. To do this efficiently we
11717 generate a back-mapping of the indices first. */
11718 backmap = XALLOCAVEC (int, numfiles);
11719 for (i = 0; i < numfiles; i++)
11720 backmap[files[i].file_idx->emitted_number - 1] = i;
11722 if (dwarf_version >= 5)
11724 const char *filename0 = get_AT_string (comp_unit_die (), DW_AT_name);
11725 if (filename0 == NULL)
11726 filename0 = "";
11727 /* DW_LNCT_directory_index can use DW_FORM_udata, DW_FORM_data1 and
11728 DW_FORM_data2. Choose one based on the number of directories
11729 and how much space would they occupy in each encoding.
11730 If we have at most 256 directories, all indexes fit into
11731 a single byte, so DW_FORM_data1 is most compact (if there
11732 are at most 128 directories, DW_FORM_udata would be as
11733 compact as that, but not shorter and slower to decode). */
11734 if (ndirs + idx_offset <= 256)
11735 idx_form = DW_FORM_data1;
11736 /* If there are more than 65536 directories, we have to use
11737 DW_FORM_udata, DW_FORM_data2 can't refer to them.
11738 Otherwise, compute what space would occupy if all the indexes
11739 used DW_FORM_udata - sum - and compare that to how large would
11740 be DW_FORM_data2 encoding, and pick the more efficient one. */
11741 else if (ndirs + idx_offset <= 65536)
11743 unsigned HOST_WIDE_INT sum = 1;
11744 for (i = 0; i < numfiles; i++)
11746 int file_idx = backmap[i];
11747 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11748 sum += size_of_uleb128 (dir_idx);
11750 if (sum >= HOST_WIDE_INT_UC (2) * (numfiles + 1))
11751 idx_form = DW_FORM_data2;
11753 #ifdef VMS_DEBUGGING_INFO
11754 dw2_asm_output_data (1, 4, "File name entry format count");
11755 #else
11756 dw2_asm_output_data (1, 2, "File name entry format count");
11757 #endif
11758 dw2_asm_output_data_uleb128 (DW_LNCT_path, "DW_LNCT_path");
11759 dw2_asm_output_data_uleb128 (str_form, "%s",
11760 get_DW_FORM_name (str_form));
11761 dw2_asm_output_data_uleb128 (DW_LNCT_directory_index,
11762 "DW_LNCT_directory_index");
11763 dw2_asm_output_data_uleb128 (idx_form, "%s",
11764 get_DW_FORM_name (idx_form));
11765 #ifdef VMS_DEBUGGING_INFO
11766 dw2_asm_output_data_uleb128 (DW_LNCT_timestamp, "DW_LNCT_timestamp");
11767 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11768 dw2_asm_output_data_uleb128 (DW_LNCT_size, "DW_LNCT_size");
11769 dw2_asm_output_data_uleb128 (DW_FORM_udata, "DW_FORM_udata");
11770 #endif
11771 dw2_asm_output_data_uleb128 (numfiles + 1, "File names count");
11773 output_line_string (str_form, filename0, "File Entry", 0);
11775 /* Include directory index. */
11776 if (idx_form != DW_FORM_udata)
11777 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11778 0, NULL);
11779 else
11780 dw2_asm_output_data_uleb128 (0, NULL);
11782 #ifdef VMS_DEBUGGING_INFO
11783 dw2_asm_output_data_uleb128 (0, NULL);
11784 dw2_asm_output_data_uleb128 (0, NULL);
11785 #endif
11788 /* Now write all the file names. */
11789 for (i = 0; i < numfiles; i++)
11791 int file_idx = backmap[i];
11792 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11794 #ifdef VMS_DEBUGGING_INFO
11795 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11797 /* Setting these fields can lead to debugger miscomparisons,
11798 but VMS Debug requires them to be set correctly. */
11800 int ver;
11801 long long cdt;
11802 long siz;
11803 int maxfilelen = (strlen (files[file_idx].path)
11804 + dirs[dir_idx].length
11805 + MAX_VMS_VERSION_LEN + 1);
11806 char *filebuf = XALLOCAVEC (char, maxfilelen);
11808 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11809 snprintf (filebuf, maxfilelen, "%s;%d",
11810 files[file_idx].path + dirs[dir_idx].length, ver);
11812 output_line_string (str_form, filebuf, "File Entry", (unsigned) i + 1);
11814 /* Include directory index. */
11815 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11816 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11817 dir_idx + idx_offset, NULL);
11818 else
11819 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11821 /* Modification time. */
11822 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11823 &cdt, 0, 0, 0) == 0)
11824 ? cdt : 0, NULL);
11826 /* File length in bytes. */
11827 dw2_asm_output_data_uleb128 ((vms_file_stats_name (files[file_idx].path,
11828 0, &siz, 0, 0) == 0)
11829 ? siz : 0, NULL);
11830 #else
11831 output_line_string (str_form,
11832 files[file_idx].path + dirs[dir_idx].length,
11833 "File Entry", (unsigned) i + 1);
11835 /* Include directory index. */
11836 if (dwarf_version >= 5 && idx_form != DW_FORM_udata)
11837 dw2_asm_output_data (idx_form == DW_FORM_data1 ? 1 : 2,
11838 dir_idx + idx_offset, NULL);
11839 else
11840 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11842 if (dwarf_version >= 5)
11843 continue;
11845 /* Modification time. */
11846 dw2_asm_output_data_uleb128 (0, NULL);
11848 /* File length in bytes. */
11849 dw2_asm_output_data_uleb128 (0, NULL);
11850 #endif /* VMS_DEBUGGING_INFO */
11853 if (dwarf_version < 5)
11854 dw2_asm_output_data (1, 0, "End file name table");
11858 /* Output one line number table into the .debug_line section. */
11860 static void
11861 output_one_line_info_table (dw_line_info_table *table)
11863 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11864 unsigned int current_line = 1;
11865 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
11866 dw_line_info_entry *ent;
11867 size_t i;
11869 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
11871 switch (ent->opcode)
11873 case LI_set_address:
11874 /* ??? Unfortunately, we have little choice here currently, and
11875 must always use the most general form. GCC does not know the
11876 address delta itself, so we can't use DW_LNS_advance_pc. Many
11877 ports do have length attributes which will give an upper bound
11878 on the address range. We could perhaps use length attributes
11879 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
11880 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
11882 /* This can handle any delta. This takes
11883 4+DWARF2_ADDR_SIZE bytes. */
11884 dw2_asm_output_data (1, 0, "set address %s", line_label);
11885 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11886 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11887 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11888 break;
11890 case LI_set_line:
11891 if (ent->val == current_line)
11893 /* We still need to start a new row, so output a copy insn. */
11894 dw2_asm_output_data (1, DW_LNS_copy,
11895 "copy line %u", current_line);
11897 else
11899 int line_offset = ent->val - current_line;
11900 int line_delta = line_offset - DWARF_LINE_BASE;
11902 current_line = ent->val;
11903 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11905 /* This can handle deltas from -10 to 234, using the current
11906 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
11907 This takes 1 byte. */
11908 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11909 "line %u", current_line);
11911 else
11913 /* This can handle any delta. This takes at least 4 bytes,
11914 depending on the value being encoded. */
11915 dw2_asm_output_data (1, DW_LNS_advance_line,
11916 "advance to line %u", current_line);
11917 dw2_asm_output_data_sleb128 (line_offset, NULL);
11918 dw2_asm_output_data (1, DW_LNS_copy, NULL);
11921 break;
11923 case LI_set_file:
11924 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
11925 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11926 break;
11928 case LI_set_column:
11929 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
11930 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
11931 break;
11933 case LI_negate_stmt:
11934 current_is_stmt = !current_is_stmt;
11935 dw2_asm_output_data (1, DW_LNS_negate_stmt,
11936 "is_stmt %d", current_is_stmt);
11937 break;
11939 case LI_set_prologue_end:
11940 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
11941 "set prologue end");
11942 break;
11944 case LI_set_epilogue_begin:
11945 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
11946 "set epilogue begin");
11947 break;
11949 case LI_set_discriminator:
11950 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
11951 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
11952 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
11953 dw2_asm_output_data_uleb128 (ent->val, NULL);
11954 break;
11958 /* Emit debug info for the address of the end of the table. */
11959 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
11960 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11961 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11962 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
11964 dw2_asm_output_data (1, 0, "end sequence");
11965 dw2_asm_output_data_uleb128 (1, NULL);
11966 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11969 /* Output the source line number correspondence information. This
11970 information goes into the .debug_line section. */
11972 static void
11973 output_line_info (bool prologue_only)
11975 static unsigned int generation;
11976 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
11977 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
11978 bool saw_one = false;
11979 int opc;
11981 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
11982 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
11983 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
11984 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
11986 if (!XCOFF_DEBUGGING_INFO)
11988 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11989 dw2_asm_output_data (4, 0xffffffff,
11990 "Initial length escape value indicating 64-bit DWARF extension");
11991 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11992 "Length of Source Line Info");
11995 ASM_OUTPUT_LABEL (asm_out_file, l1);
11997 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
11998 if (dwarf_version >= 5)
12000 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
12001 dw2_asm_output_data (1, 0, "Segment Size");
12003 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12004 ASM_OUTPUT_LABEL (asm_out_file, p1);
12006 /* Define the architecture-dependent minimum instruction length (in bytes).
12007 In this implementation of DWARF, this field is used for information
12008 purposes only. Since GCC generates assembly language, we have no
12009 a priori knowledge of how many instruction bytes are generated for each
12010 source line, and therefore can use only the DW_LNE_set_address and
12011 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
12012 this as '1', which is "correct enough" for all architectures,
12013 and don't let the target override. */
12014 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12016 if (dwarf_version >= 4)
12017 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12018 "Maximum Operations Per Instruction");
12019 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12020 "Default is_stmt_start flag");
12021 dw2_asm_output_data (1, DWARF_LINE_BASE,
12022 "Line Base Value (Special Opcodes)");
12023 dw2_asm_output_data (1, DWARF_LINE_RANGE,
12024 "Line Range Value (Special Opcodes)");
12025 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12026 "Special Opcode Base");
12028 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12030 int n_op_args;
12031 switch (opc)
12033 case DW_LNS_advance_pc:
12034 case DW_LNS_advance_line:
12035 case DW_LNS_set_file:
12036 case DW_LNS_set_column:
12037 case DW_LNS_fixed_advance_pc:
12038 case DW_LNS_set_isa:
12039 n_op_args = 1;
12040 break;
12041 default:
12042 n_op_args = 0;
12043 break;
12046 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12047 opc, n_op_args);
12050 /* Write out the information about the files we use. */
12051 output_file_names ();
12052 ASM_OUTPUT_LABEL (asm_out_file, p2);
12053 if (prologue_only)
12055 /* Output the marker for the end of the line number info. */
12056 ASM_OUTPUT_LABEL (asm_out_file, l2);
12057 return;
12060 if (separate_line_info)
12062 dw_line_info_table *table;
12063 size_t i;
12065 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
12066 if (table->in_use)
12068 output_one_line_info_table (table);
12069 saw_one = true;
12072 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12074 output_one_line_info_table (cold_text_section_line_info);
12075 saw_one = true;
12078 /* ??? Some Darwin linkers crash on a .debug_line section with no
12079 sequences. Further, merely a DW_LNE_end_sequence entry is not
12080 sufficient -- the address column must also be initialized.
12081 Make sure to output at least one set_address/end_sequence pair,
12082 choosing .text since that section is always present. */
12083 if (text_section_line_info->in_use || !saw_one)
12084 output_one_line_info_table (text_section_line_info);
12086 /* Output the marker for the end of the line number info. */
12087 ASM_OUTPUT_LABEL (asm_out_file, l2);
12090 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
12092 static inline bool
12093 need_endianity_attribute_p (bool reverse)
12095 return reverse && (dwarf_version >= 3 || !dwarf_strict);
12098 /* Given a pointer to a tree node for some base type, return a pointer to
12099 a DIE that describes the given type. REVERSE is true if the type is
12100 to be interpreted in the reverse storage order wrt the target order.
12102 This routine must only be called for GCC type nodes that correspond to
12103 Dwarf base (fundamental) types. */
12105 static dw_die_ref
12106 base_type_die (tree type, bool reverse)
12108 dw_die_ref base_type_result;
12109 enum dwarf_type encoding;
12110 bool fpt_used = false;
12111 struct fixed_point_type_info fpt_info;
12112 tree type_bias = NULL_TREE;
12114 /* If this is a subtype that should not be emitted as a subrange type,
12115 use the base type. See subrange_type_for_debug_p. */
12116 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12117 type = TREE_TYPE (type);
12119 switch (TREE_CODE (type))
12121 case INTEGER_TYPE:
12122 if ((dwarf_version >= 4 || !dwarf_strict)
12123 && TYPE_NAME (type)
12124 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12125 && DECL_IS_BUILTIN (TYPE_NAME (type))
12126 && DECL_NAME (TYPE_NAME (type)))
12128 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12129 if (strcmp (name, "char16_t") == 0
12130 || strcmp (name, "char32_t") == 0)
12132 encoding = DW_ATE_UTF;
12133 break;
12136 if ((dwarf_version >= 3 || !dwarf_strict)
12137 && lang_hooks.types.get_fixed_point_type_info)
12139 memset (&fpt_info, 0, sizeof (fpt_info));
12140 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
12142 fpt_used = true;
12143 encoding = ((TYPE_UNSIGNED (type))
12144 ? DW_ATE_unsigned_fixed
12145 : DW_ATE_signed_fixed);
12146 break;
12149 if (TYPE_STRING_FLAG (type))
12151 if (TYPE_UNSIGNED (type))
12152 encoding = DW_ATE_unsigned_char;
12153 else
12154 encoding = DW_ATE_signed_char;
12156 else if (TYPE_UNSIGNED (type))
12157 encoding = DW_ATE_unsigned;
12158 else
12159 encoding = DW_ATE_signed;
12161 if (!dwarf_strict
12162 && lang_hooks.types.get_type_bias)
12163 type_bias = lang_hooks.types.get_type_bias (type);
12164 break;
12166 case REAL_TYPE:
12167 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12169 if (dwarf_version >= 3 || !dwarf_strict)
12170 encoding = DW_ATE_decimal_float;
12171 else
12172 encoding = DW_ATE_lo_user;
12174 else
12175 encoding = DW_ATE_float;
12176 break;
12178 case FIXED_POINT_TYPE:
12179 if (!(dwarf_version >= 3 || !dwarf_strict))
12180 encoding = DW_ATE_lo_user;
12181 else if (TYPE_UNSIGNED (type))
12182 encoding = DW_ATE_unsigned_fixed;
12183 else
12184 encoding = DW_ATE_signed_fixed;
12185 break;
12187 /* Dwarf2 doesn't know anything about complex ints, so use
12188 a user defined type for it. */
12189 case COMPLEX_TYPE:
12190 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12191 encoding = DW_ATE_complex_float;
12192 else
12193 encoding = DW_ATE_lo_user;
12194 break;
12196 case BOOLEAN_TYPE:
12197 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
12198 encoding = DW_ATE_boolean;
12199 break;
12201 default:
12202 /* No other TREE_CODEs are Dwarf fundamental types. */
12203 gcc_unreachable ();
12206 base_type_result = new_die_raw (DW_TAG_base_type);
12208 add_AT_unsigned (base_type_result, DW_AT_byte_size,
12209 int_size_in_bytes (type));
12210 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12212 if (need_endianity_attribute_p (reverse))
12213 add_AT_unsigned (base_type_result, DW_AT_endianity,
12214 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
12216 add_alignment_attribute (base_type_result, type);
12218 if (fpt_used)
12220 switch (fpt_info.scale_factor_kind)
12222 case fixed_point_scale_factor_binary:
12223 add_AT_int (base_type_result, DW_AT_binary_scale,
12224 fpt_info.scale_factor.binary);
12225 break;
12227 case fixed_point_scale_factor_decimal:
12228 add_AT_int (base_type_result, DW_AT_decimal_scale,
12229 fpt_info.scale_factor.decimal);
12230 break;
12232 case fixed_point_scale_factor_arbitrary:
12233 /* Arbitrary scale factors cannot be described in standard DWARF,
12234 yet. */
12235 if (!dwarf_strict)
12237 /* Describe the scale factor as a rational constant. */
12238 const dw_die_ref scale_factor
12239 = new_die (DW_TAG_constant, comp_unit_die (), type);
12241 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
12242 fpt_info.scale_factor.arbitrary.numerator);
12243 add_AT_int (scale_factor, DW_AT_GNU_denominator,
12244 fpt_info.scale_factor.arbitrary.denominator);
12246 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
12248 break;
12250 default:
12251 gcc_unreachable ();
12255 if (type_bias)
12256 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
12257 dw_scalar_form_constant
12258 | dw_scalar_form_exprloc
12259 | dw_scalar_form_reference,
12260 NULL);
12262 return base_type_result;
12265 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
12266 named 'auto' in its type: return true for it, false otherwise. */
12268 static inline bool
12269 is_cxx_auto (tree type)
12271 if (is_cxx ())
12273 tree name = TYPE_IDENTIFIER (type);
12274 if (name == get_identifier ("auto")
12275 || name == get_identifier ("decltype(auto)"))
12276 return true;
12278 return false;
12281 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12282 given input type is a Dwarf "fundamental" type. Otherwise return null. */
12284 static inline int
12285 is_base_type (tree type)
12287 switch (TREE_CODE (type))
12289 case INTEGER_TYPE:
12290 case REAL_TYPE:
12291 case FIXED_POINT_TYPE:
12292 case COMPLEX_TYPE:
12293 case BOOLEAN_TYPE:
12294 case POINTER_BOUNDS_TYPE:
12295 return 1;
12297 case VOID_TYPE:
12298 case ARRAY_TYPE:
12299 case RECORD_TYPE:
12300 case UNION_TYPE:
12301 case QUAL_UNION_TYPE:
12302 case ENUMERAL_TYPE:
12303 case FUNCTION_TYPE:
12304 case METHOD_TYPE:
12305 case POINTER_TYPE:
12306 case REFERENCE_TYPE:
12307 case NULLPTR_TYPE:
12308 case OFFSET_TYPE:
12309 case LANG_TYPE:
12310 case VECTOR_TYPE:
12311 return 0;
12313 default:
12314 if (is_cxx_auto (type))
12315 return 0;
12316 gcc_unreachable ();
12319 return 0;
12322 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12323 node, return the size in bits for the type if it is a constant, or else
12324 return the alignment for the type if the type's size is not constant, or
12325 else return BITS_PER_WORD if the type actually turns out to be an
12326 ERROR_MARK node. */
12328 static inline unsigned HOST_WIDE_INT
12329 simple_type_size_in_bits (const_tree type)
12331 if (TREE_CODE (type) == ERROR_MARK)
12332 return BITS_PER_WORD;
12333 else if (TYPE_SIZE (type) == NULL_TREE)
12334 return 0;
12335 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
12336 return tree_to_uhwi (TYPE_SIZE (type));
12337 else
12338 return TYPE_ALIGN (type);
12341 /* Similarly, but return an offset_int instead of UHWI. */
12343 static inline offset_int
12344 offset_int_type_size_in_bits (const_tree type)
12346 if (TREE_CODE (type) == ERROR_MARK)
12347 return BITS_PER_WORD;
12348 else if (TYPE_SIZE (type) == NULL_TREE)
12349 return 0;
12350 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12351 return wi::to_offset (TYPE_SIZE (type));
12352 else
12353 return TYPE_ALIGN (type);
12356 /* Given a pointer to a tree node for a subrange type, return a pointer
12357 to a DIE that describes the given type. */
12359 static dw_die_ref
12360 subrange_type_die (tree type, tree low, tree high, tree bias,
12361 dw_die_ref context_die)
12363 dw_die_ref subrange_die;
12364 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12366 if (context_die == NULL)
12367 context_die = comp_unit_die ();
12369 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12371 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12373 /* The size of the subrange type and its base type do not match,
12374 so we need to generate a size attribute for the subrange type. */
12375 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12378 add_alignment_attribute (subrange_die, type);
12380 if (low)
12381 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
12382 if (high)
12383 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
12384 if (bias && !dwarf_strict)
12385 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
12386 dw_scalar_form_constant
12387 | dw_scalar_form_exprloc
12388 | dw_scalar_form_reference,
12389 NULL);
12391 return subrange_die;
12394 /* Returns the (const and/or volatile) cv_qualifiers associated with
12395 the decl node. This will normally be augmented with the
12396 cv_qualifiers of the underlying type in add_type_attribute. */
12398 static int
12399 decl_quals (const_tree decl)
12401 return ((TREE_READONLY (decl)
12402 /* The C++ front-end correctly marks reference-typed
12403 variables as readonly, but from a language (and debug
12404 info) standpoint they are not const-qualified. */
12405 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
12406 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
12407 | (TREE_THIS_VOLATILE (decl)
12408 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
12411 /* Determine the TYPE whose qualifiers match the largest strict subset
12412 of the given TYPE_QUALS, and return its qualifiers. Ignore all
12413 qualifiers outside QUAL_MASK. */
12415 static int
12416 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
12418 tree t;
12419 int best_rank = 0, best_qual = 0, max_rank;
12421 type_quals &= qual_mask;
12422 max_rank = popcount_hwi (type_quals) - 1;
12424 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
12425 t = TYPE_NEXT_VARIANT (t))
12427 int q = TYPE_QUALS (t) & qual_mask;
12429 if ((q & type_quals) == q && q != type_quals
12430 && check_base_type (t, type))
12432 int rank = popcount_hwi (q);
12434 if (rank > best_rank)
12436 best_rank = rank;
12437 best_qual = q;
12442 return best_qual;
12445 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
12446 static const dwarf_qual_info_t dwarf_qual_info[] =
12448 { TYPE_QUAL_CONST, DW_TAG_const_type },
12449 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
12450 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
12451 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
12453 static const unsigned int dwarf_qual_info_size
12454 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
12456 /* If DIE is a qualified DIE of some base DIE with the same parent,
12457 return the base DIE, otherwise return NULL. Set MASK to the
12458 qualifiers added compared to the returned DIE. */
12460 static dw_die_ref
12461 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
12463 unsigned int i;
12464 for (i = 0; i < dwarf_qual_info_size; i++)
12465 if (die->die_tag == dwarf_qual_info[i].t)
12466 break;
12467 if (i == dwarf_qual_info_size)
12468 return NULL;
12469 if (vec_safe_length (die->die_attr) != 1)
12470 return NULL;
12471 dw_die_ref type = get_AT_ref (die, DW_AT_type);
12472 if (type == NULL || type->die_parent != die->die_parent)
12473 return NULL;
12474 *mask |= dwarf_qual_info[i].q;
12475 if (depth)
12477 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
12478 if (ret)
12479 return ret;
12481 return type;
12484 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12485 entry that chains the modifiers specified by CV_QUALS in front of the
12486 given type. REVERSE is true if the type is to be interpreted in the
12487 reverse storage order wrt the target order. */
12489 static dw_die_ref
12490 modified_type_die (tree type, int cv_quals, bool reverse,
12491 dw_die_ref context_die)
12493 enum tree_code code = TREE_CODE (type);
12494 dw_die_ref mod_type_die;
12495 dw_die_ref sub_die = NULL;
12496 tree item_type = NULL;
12497 tree qualified_type;
12498 tree name, low, high;
12499 dw_die_ref mod_scope;
12500 /* Only these cv-qualifiers are currently handled. */
12501 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
12502 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC |
12503 ENCODE_QUAL_ADDR_SPACE(~0U));
12504 const bool reverse_base_type
12505 = need_endianity_attribute_p (reverse) && is_base_type (type);
12507 if (code == ERROR_MARK)
12508 return NULL;
12510 if (lang_hooks.types.get_debug_type)
12512 tree debug_type = lang_hooks.types.get_debug_type (type);
12514 if (debug_type != NULL_TREE && debug_type != type)
12515 return modified_type_die (debug_type, cv_quals, reverse, context_die);
12518 cv_quals &= cv_qual_mask;
12520 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
12521 tag modifier (and not an attribute) old consumers won't be able
12522 to handle it. */
12523 if (dwarf_version < 3)
12524 cv_quals &= ~TYPE_QUAL_RESTRICT;
12526 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
12527 if (dwarf_version < 5)
12528 cv_quals &= ~TYPE_QUAL_ATOMIC;
12530 /* See if we already have the appropriately qualified variant of
12531 this type. */
12532 qualified_type = get_qualified_type (type, cv_quals);
12534 if (qualified_type == sizetype)
12536 /* Try not to expose the internal sizetype type's name. */
12537 if (TYPE_NAME (qualified_type)
12538 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12540 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12542 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12543 && (TYPE_PRECISION (t)
12544 == TYPE_PRECISION (qualified_type))
12545 && (TYPE_UNSIGNED (t)
12546 == TYPE_UNSIGNED (qualified_type)));
12547 qualified_type = t;
12549 else if (qualified_type == sizetype
12550 && TREE_CODE (sizetype) == TREE_CODE (size_type_node)
12551 && TYPE_PRECISION (sizetype) == TYPE_PRECISION (size_type_node)
12552 && TYPE_UNSIGNED (sizetype) == TYPE_UNSIGNED (size_type_node))
12553 qualified_type = size_type_node;
12556 /* If we do, then we can just use its DIE, if it exists. */
12557 if (qualified_type)
12559 mod_type_die = lookup_type_die (qualified_type);
12561 /* DW_AT_endianity doesn't come from a qualifier on the type, so it is
12562 dealt with specially: the DIE with the attribute, if it exists, is
12563 placed immediately after the regular DIE for the same base type. */
12564 if (mod_type_die
12565 && (!reverse_base_type
12566 || ((mod_type_die = mod_type_die->die_sib) != NULL
12567 && get_AT_unsigned (mod_type_die, DW_AT_endianity))))
12568 return mod_type_die;
12571 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12573 /* Handle C typedef types. */
12574 if (name
12575 && TREE_CODE (name) == TYPE_DECL
12576 && DECL_ORIGINAL_TYPE (name)
12577 && !DECL_ARTIFICIAL (name))
12579 tree dtype = TREE_TYPE (name);
12581 /* Skip the typedef for base types with DW_AT_endianity, no big deal. */
12582 if (qualified_type == dtype && !reverse_base_type)
12584 tree origin = decl_ultimate_origin (name);
12586 /* Typedef variants that have an abstract origin don't get their own
12587 type DIE (see gen_typedef_die), so fall back on the ultimate
12588 abstract origin instead. */
12589 if (origin != NULL && origin != name)
12590 return modified_type_die (TREE_TYPE (origin), cv_quals, reverse,
12591 context_die);
12593 /* For a named type, use the typedef. */
12594 gen_type_die (qualified_type, context_die);
12595 return lookup_type_die (qualified_type);
12597 else
12599 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
12600 dquals &= cv_qual_mask;
12601 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
12602 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
12603 /* cv-unqualified version of named type. Just use
12604 the unnamed type to which it refers. */
12605 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
12606 reverse, context_die);
12607 /* Else cv-qualified version of named type; fall through. */
12611 mod_scope = scope_die_for (type, context_die);
12613 if (cv_quals)
12615 int sub_quals = 0, first_quals = 0;
12616 unsigned i;
12617 dw_die_ref first = NULL, last = NULL;
12619 /* Determine a lesser qualified type that most closely matches
12620 this one. Then generate DW_TAG_* entries for the remaining
12621 qualifiers. */
12622 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
12623 cv_qual_mask);
12624 if (sub_quals && use_debug_types)
12626 bool needed = false;
12627 /* If emitting type units, make sure the order of qualifiers
12628 is canonical. Thus, start from unqualified type if
12629 an earlier qualifier is missing in sub_quals, but some later
12630 one is present there. */
12631 for (i = 0; i < dwarf_qual_info_size; i++)
12632 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12633 needed = true;
12634 else if (needed && (dwarf_qual_info[i].q & cv_quals))
12636 sub_quals = 0;
12637 break;
12640 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
12641 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
12643 /* As not all intermediate qualified DIEs have corresponding
12644 tree types, ensure that qualified DIEs in the same scope
12645 as their DW_AT_type are emitted after their DW_AT_type,
12646 only with other qualified DIEs for the same type possibly
12647 in between them. Determine the range of such qualified
12648 DIEs now (first being the base type, last being corresponding
12649 last qualified DIE for it). */
12650 unsigned int count = 0;
12651 first = qualified_die_p (mod_type_die, &first_quals,
12652 dwarf_qual_info_size);
12653 if (first == NULL)
12654 first = mod_type_die;
12655 gcc_assert ((first_quals & ~sub_quals) == 0);
12656 for (count = 0, last = first;
12657 count < (1U << dwarf_qual_info_size);
12658 count++, last = last->die_sib)
12660 int quals = 0;
12661 if (last == mod_scope->die_child)
12662 break;
12663 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
12664 != first)
12665 break;
12669 for (i = 0; i < dwarf_qual_info_size; i++)
12670 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
12672 dw_die_ref d;
12673 if (first && first != last)
12675 for (d = first->die_sib; ; d = d->die_sib)
12677 int quals = 0;
12678 qualified_die_p (d, &quals, dwarf_qual_info_size);
12679 if (quals == (first_quals | dwarf_qual_info[i].q))
12680 break;
12681 if (d == last)
12683 d = NULL;
12684 break;
12687 if (d)
12689 mod_type_die = d;
12690 continue;
12693 if (first)
12695 d = new_die_raw (dwarf_qual_info[i].t);
12696 add_child_die_after (mod_scope, d, last);
12697 last = d;
12699 else
12700 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
12701 if (mod_type_die)
12702 add_AT_die_ref (d, DW_AT_type, mod_type_die);
12703 mod_type_die = d;
12704 first_quals |= dwarf_qual_info[i].q;
12707 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
12709 dwarf_tag tag = DW_TAG_pointer_type;
12710 if (code == REFERENCE_TYPE)
12712 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12713 tag = DW_TAG_rvalue_reference_type;
12714 else
12715 tag = DW_TAG_reference_type;
12717 mod_type_die = new_die (tag, mod_scope, type);
12719 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12720 simple_type_size_in_bits (type) / BITS_PER_UNIT);
12721 add_alignment_attribute (mod_type_die, type);
12722 item_type = TREE_TYPE (type);
12724 addr_space_t as = TYPE_ADDR_SPACE (item_type);
12725 if (!ADDR_SPACE_GENERIC_P (as))
12727 int action = targetm.addr_space.debug (as);
12728 if (action >= 0)
12730 /* Positive values indicate an address_class. */
12731 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
12733 else
12735 /* Negative values indicate an (inverted) segment base reg. */
12736 dw_loc_descr_ref d
12737 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
12738 add_AT_loc (mod_type_die, DW_AT_segment, d);
12742 else if (code == INTEGER_TYPE
12743 && TREE_TYPE (type) != NULL_TREE
12744 && subrange_type_for_debug_p (type, &low, &high))
12746 tree bias = NULL_TREE;
12747 if (lang_hooks.types.get_type_bias)
12748 bias = lang_hooks.types.get_type_bias (type);
12749 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
12750 item_type = TREE_TYPE (type);
12752 else if (is_base_type (type))
12754 mod_type_die = base_type_die (type, reverse);
12756 /* The DIE with DW_AT_endianity is placed right after the naked DIE. */
12757 if (reverse_base_type)
12759 dw_die_ref after_die
12760 = modified_type_die (type, cv_quals, false, context_die);
12761 add_child_die_after (comp_unit_die (), mod_type_die, after_die);
12763 else
12764 add_child_die (comp_unit_die (), mod_type_die);
12766 add_pubtype (type, mod_type_die);
12768 else
12770 gen_type_die (type, context_die);
12772 /* We have to get the type_main_variant here (and pass that to the
12773 `lookup_type_die' routine) because the ..._TYPE node we have
12774 might simply be a *copy* of some original type node (where the
12775 copy was created to help us keep track of typedef names) and
12776 that copy might have a different TYPE_UID from the original
12777 ..._TYPE node. */
12778 if (TREE_CODE (type) == FUNCTION_TYPE
12779 || TREE_CODE (type) == METHOD_TYPE)
12781 /* For function/method types, can't just use type_main_variant here,
12782 because that can have different ref-qualifiers for C++,
12783 but try to canonicalize. */
12784 tree main = TYPE_MAIN_VARIANT (type);
12785 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
12786 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
12787 && check_base_type (t, main)
12788 && check_lang_type (t, type))
12789 return lookup_type_die (t);
12790 return lookup_type_die (type);
12792 else if (TREE_CODE (type) != VECTOR_TYPE
12793 && TREE_CODE (type) != ARRAY_TYPE)
12794 return lookup_type_die (type_main_variant (type));
12795 else
12796 /* Vectors have the debugging information in the type,
12797 not the main variant. */
12798 return lookup_type_die (type);
12801 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
12802 don't output a DW_TAG_typedef, since there isn't one in the
12803 user's program; just attach a DW_AT_name to the type.
12804 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12805 if the base type already has the same name. */
12806 if (name
12807 && ((TREE_CODE (name) != TYPE_DECL
12808 && (qualified_type == TYPE_MAIN_VARIANT (type)
12809 || (cv_quals == TYPE_UNQUALIFIED)))
12810 || (TREE_CODE (name) == TYPE_DECL
12811 && TREE_TYPE (name) == qualified_type
12812 && DECL_NAME (name))))
12814 if (TREE_CODE (name) == TYPE_DECL)
12815 /* Could just call add_name_and_src_coords_attributes here,
12816 but since this is a builtin type it doesn't have any
12817 useful source coordinates anyway. */
12818 name = DECL_NAME (name);
12819 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12821 /* This probably indicates a bug. */
12822 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12824 name = TYPE_IDENTIFIER (type);
12825 add_name_attribute (mod_type_die,
12826 name ? IDENTIFIER_POINTER (name) : "__unknown__");
12829 if (qualified_type && !reverse_base_type)
12830 equate_type_number_to_die (qualified_type, mod_type_die);
12832 if (item_type)
12833 /* We must do this after the equate_type_number_to_die call, in case
12834 this is a recursive type. This ensures that the modified_type_die
12835 recursion will terminate even if the type is recursive. Recursive
12836 types are possible in Ada. */
12837 sub_die = modified_type_die (item_type,
12838 TYPE_QUALS_NO_ADDR_SPACE (item_type),
12839 reverse,
12840 context_die);
12842 if (sub_die != NULL)
12843 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12845 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
12846 if (TYPE_ARTIFICIAL (type))
12847 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
12849 return mod_type_die;
12852 /* Generate DIEs for the generic parameters of T.
12853 T must be either a generic type or a generic function.
12854 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
12856 static void
12857 gen_generic_params_dies (tree t)
12859 tree parms, args;
12860 int parms_num, i;
12861 dw_die_ref die = NULL;
12862 int non_default;
12864 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12865 return;
12867 if (TYPE_P (t))
12868 die = lookup_type_die (t);
12869 else if (DECL_P (t))
12870 die = lookup_decl_die (t);
12872 gcc_assert (die);
12874 parms = lang_hooks.get_innermost_generic_parms (t);
12875 if (!parms)
12876 /* T has no generic parameter. It means T is neither a generic type
12877 or function. End of story. */
12878 return;
12880 parms_num = TREE_VEC_LENGTH (parms);
12881 args = lang_hooks.get_innermost_generic_args (t);
12882 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
12883 non_default = int_cst_value (TREE_CHAIN (args));
12884 else
12885 non_default = TREE_VEC_LENGTH (args);
12886 for (i = 0; i < parms_num; i++)
12888 tree parm, arg, arg_pack_elems;
12889 dw_die_ref parm_die;
12891 parm = TREE_VEC_ELT (parms, i);
12892 arg = TREE_VEC_ELT (args, i);
12893 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12894 gcc_assert (parm && TREE_VALUE (parm) && arg);
12896 if (parm && TREE_VALUE (parm) && arg)
12898 /* If PARM represents a template parameter pack,
12899 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12900 by DW_TAG_template_*_parameter DIEs for the argument
12901 pack elements of ARG. Note that ARG would then be
12902 an argument pack. */
12903 if (arg_pack_elems)
12904 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
12905 arg_pack_elems,
12906 die);
12907 else
12908 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
12909 true /* emit name */, die);
12910 if (i >= non_default)
12911 add_AT_flag (parm_die, DW_AT_default_value, 1);
12916 /* Create and return a DIE for PARM which should be
12917 the representation of a generic type parameter.
12918 For instance, in the C++ front end, PARM would be a template parameter.
12919 ARG is the argument to PARM.
12920 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12921 name of the PARM.
12922 PARENT_DIE is the parent DIE which the new created DIE should be added to,
12923 as a child node. */
12925 static dw_die_ref
12926 generic_parameter_die (tree parm, tree arg,
12927 bool emit_name_p,
12928 dw_die_ref parent_die)
12930 dw_die_ref tmpl_die = NULL;
12931 const char *name = NULL;
12933 if (!parm || !DECL_NAME (parm) || !arg)
12934 return NULL;
12936 /* We support non-type generic parameters and arguments,
12937 type generic parameters and arguments, as well as
12938 generic generic parameters (a.k.a. template template parameters in C++)
12939 and arguments. */
12940 if (TREE_CODE (parm) == PARM_DECL)
12941 /* PARM is a nontype generic parameter */
12942 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12943 else if (TREE_CODE (parm) == TYPE_DECL)
12944 /* PARM is a type generic parameter. */
12945 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12946 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12947 /* PARM is a generic generic parameter.
12948 Its DIE is a GNU extension. It shall have a
12949 DW_AT_name attribute to represent the name of the template template
12950 parameter, and a DW_AT_GNU_template_name attribute to represent the
12951 name of the template template argument. */
12952 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12953 parent_die, parm);
12954 else
12955 gcc_unreachable ();
12957 if (tmpl_die)
12959 tree tmpl_type;
12961 /* If PARM is a generic parameter pack, it means we are
12962 emitting debug info for a template argument pack element.
12963 In other terms, ARG is a template argument pack element.
12964 In that case, we don't emit any DW_AT_name attribute for
12965 the die. */
12966 if (emit_name_p)
12968 name = IDENTIFIER_POINTER (DECL_NAME (parm));
12969 gcc_assert (name);
12970 add_AT_string (tmpl_die, DW_AT_name, name);
12973 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12975 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12976 TMPL_DIE should have a child DW_AT_type attribute that is set
12977 to the type of the argument to PARM, which is ARG.
12978 If PARM is a type generic parameter, TMPL_DIE should have a
12979 child DW_AT_type that is set to ARG. */
12980 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12981 add_type_attribute (tmpl_die, tmpl_type,
12982 (TREE_THIS_VOLATILE (tmpl_type)
12983 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
12984 false, parent_die);
12986 else
12988 /* So TMPL_DIE is a DIE representing a
12989 a generic generic template parameter, a.k.a template template
12990 parameter in C++ and arg is a template. */
12992 /* The DW_AT_GNU_template_name attribute of the DIE must be set
12993 to the name of the argument. */
12994 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12995 if (name)
12996 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12999 if (TREE_CODE (parm) == PARM_DECL)
13000 /* So PARM is a non-type generic parameter.
13001 DWARF3 5.6.8 says we must set a DW_AT_const_value child
13002 attribute of TMPL_DIE which value represents the value
13003 of ARG.
13004 We must be careful here:
13005 The value of ARG might reference some function decls.
13006 We might currently be emitting debug info for a generic
13007 type and types are emitted before function decls, we don't
13008 know if the function decls referenced by ARG will actually be
13009 emitted after cgraph computations.
13010 So must defer the generation of the DW_AT_const_value to
13011 after cgraph is ready. */
13012 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13015 return tmpl_die;
13018 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
13019 PARM_PACK must be a template parameter pack. The returned DIE
13020 will be child DIE of PARENT_DIE. */
13022 static dw_die_ref
13023 template_parameter_pack_die (tree parm_pack,
13024 tree parm_pack_args,
13025 dw_die_ref parent_die)
13027 dw_die_ref die;
13028 int j;
13030 gcc_assert (parent_die && parm_pack);
13032 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13033 add_name_and_src_coords_attributes (die, parm_pack);
13034 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13035 generic_parameter_die (parm_pack,
13036 TREE_VEC_ELT (parm_pack_args, j),
13037 false /* Don't emit DW_AT_name */,
13038 die);
13039 return die;
13042 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13043 an enumerated type. */
13045 static inline int
13046 type_is_enum (const_tree type)
13048 return TREE_CODE (type) == ENUMERAL_TYPE;
13051 /* Return the DBX register number described by a given RTL node. */
13053 static unsigned int
13054 dbx_reg_number (const_rtx rtl)
13056 unsigned regno = REGNO (rtl);
13058 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13060 #ifdef LEAF_REG_REMAP
13061 if (crtl->uses_only_leaf_regs)
13063 int leaf_reg = LEAF_REG_REMAP (regno);
13064 if (leaf_reg != -1)
13065 regno = (unsigned) leaf_reg;
13067 #endif
13069 regno = DBX_REGISTER_NUMBER (regno);
13070 gcc_assert (regno != INVALID_REGNUM);
13071 return regno;
13074 /* Optionally add a DW_OP_piece term to a location description expression.
13075 DW_OP_piece is only added if the location description expression already
13076 doesn't end with DW_OP_piece. */
13078 static void
13079 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13081 dw_loc_descr_ref loc;
13083 if (*list_head != NULL)
13085 /* Find the end of the chain. */
13086 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13089 if (loc->dw_loc_opc != DW_OP_piece)
13090 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13094 /* Return a location descriptor that designates a machine register or
13095 zero if there is none. */
13097 static dw_loc_descr_ref
13098 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13100 rtx regs;
13102 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13103 return 0;
13105 /* We only use "frame base" when we're sure we're talking about the
13106 post-prologue local stack frame. We do this by *not* running
13107 register elimination until this point, and recognizing the special
13108 argument pointer and soft frame pointer rtx's.
13109 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
13110 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13111 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13113 dw_loc_descr_ref result = NULL;
13115 if (dwarf_version >= 4 || !dwarf_strict)
13117 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13118 initialized);
13119 if (result)
13120 add_loc_descr (&result,
13121 new_loc_descr (DW_OP_stack_value, 0, 0));
13123 return result;
13126 regs = targetm.dwarf_register_span (rtl);
13128 if (REG_NREGS (rtl) > 1 || regs)
13129 return multiple_reg_loc_descriptor (rtl, regs, initialized);
13130 else
13132 unsigned int dbx_regnum = dbx_reg_number (rtl);
13133 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13134 return 0;
13135 return one_reg_loc_descriptor (dbx_regnum, initialized);
13139 /* Return a location descriptor that designates a machine register for
13140 a given hard register number. */
13142 static dw_loc_descr_ref
13143 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13145 dw_loc_descr_ref reg_loc_descr;
13147 if (regno <= 31)
13148 reg_loc_descr
13149 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13150 else
13151 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13153 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13154 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13156 return reg_loc_descr;
13159 /* Given an RTL of a register, return a location descriptor that
13160 designates a value that spans more than one register. */
13162 static dw_loc_descr_ref
13163 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13164 enum var_init_status initialized)
13166 int size, i;
13167 dw_loc_descr_ref loc_result = NULL;
13169 /* Simple, contiguous registers. */
13170 if (regs == NULL_RTX)
13172 unsigned reg = REGNO (rtl);
13173 int nregs;
13175 #ifdef LEAF_REG_REMAP
13176 if (crtl->uses_only_leaf_regs)
13178 int leaf_reg = LEAF_REG_REMAP (reg);
13179 if (leaf_reg != -1)
13180 reg = (unsigned) leaf_reg;
13182 #endif
13184 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13185 nregs = REG_NREGS (rtl);
13187 /* At present we only track constant-sized pieces. */
13188 if (!GET_MODE_SIZE (GET_MODE (rtl)).is_constant (&size))
13189 return NULL;
13190 size /= nregs;
13192 loc_result = NULL;
13193 while (nregs--)
13195 dw_loc_descr_ref t;
13197 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13198 VAR_INIT_STATUS_INITIALIZED);
13199 add_loc_descr (&loc_result, t);
13200 add_loc_descr_op_piece (&loc_result, size);
13201 ++reg;
13203 return loc_result;
13206 /* Now onto stupid register sets in non contiguous locations. */
13208 gcc_assert (GET_CODE (regs) == PARALLEL);
13210 /* At present we only track constant-sized pieces. */
13211 if (!GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0))).is_constant (&size))
13212 return NULL;
13213 loc_result = NULL;
13215 for (i = 0; i < XVECLEN (regs, 0); ++i)
13217 dw_loc_descr_ref t;
13219 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
13220 VAR_INIT_STATUS_INITIALIZED);
13221 add_loc_descr (&loc_result, t);
13222 add_loc_descr_op_piece (&loc_result, size);
13225 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13226 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13227 return loc_result;
13230 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
13232 /* Return a location descriptor that designates a constant i,
13233 as a compound operation from constant (i >> shift), constant shift
13234 and DW_OP_shl. */
13236 static dw_loc_descr_ref
13237 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13239 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
13240 add_loc_descr (&ret, int_loc_descriptor (shift));
13241 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13242 return ret;
13245 /* Return a location descriptor that designates constant POLY_I. */
13247 static dw_loc_descr_ref
13248 int_loc_descriptor (poly_int64 poly_i)
13250 enum dwarf_location_atom op;
13252 HOST_WIDE_INT i;
13253 if (!poly_i.is_constant (&i))
13255 /* Create location descriptions for the non-constant part and
13256 add any constant offset at the end. */
13257 dw_loc_descr_ref ret = NULL;
13258 HOST_WIDE_INT constant = poly_i.coeffs[0];
13259 for (unsigned int j = 1; j < NUM_POLY_INT_COEFFS; ++j)
13261 HOST_WIDE_INT coeff = poly_i.coeffs[j];
13262 if (coeff != 0)
13264 dw_loc_descr_ref start = ret;
13265 unsigned int factor;
13266 int bias;
13267 unsigned int regno = targetm.dwarf_poly_indeterminate_value
13268 (j, &factor, &bias);
13270 /* Add COEFF * ((REGNO / FACTOR) - BIAS) to the value:
13271 add COEFF * (REGNO / FACTOR) now and subtract
13272 COEFF * BIAS from the final constant part. */
13273 constant -= coeff * bias;
13274 add_loc_descr (&ret, new_reg_loc_descr (regno, 0));
13275 if (coeff % factor == 0)
13276 coeff /= factor;
13277 else
13279 int amount = exact_log2 (factor);
13280 gcc_assert (amount >= 0);
13281 add_loc_descr (&ret, int_loc_descriptor (amount));
13282 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13284 if (coeff != 1)
13286 add_loc_descr (&ret, int_loc_descriptor (coeff));
13287 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13289 if (start)
13290 add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
13293 loc_descr_plus_const (&ret, constant);
13294 return ret;
13297 /* Pick the smallest representation of a constant, rather than just
13298 defaulting to the LEB encoding. */
13299 if (i >= 0)
13301 int clz = clz_hwi (i);
13302 int ctz = ctz_hwi (i);
13303 if (i <= 31)
13304 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13305 else if (i <= 0xff)
13306 op = DW_OP_const1u;
13307 else if (i <= 0xffff)
13308 op = DW_OP_const2u;
13309 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13310 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13311 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
13312 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
13313 while DW_OP_const4u is 5 bytes. */
13314 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
13315 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13316 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13317 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
13318 while DW_OP_const4u is 5 bytes. */
13319 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13321 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13322 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13323 <= 4)
13325 /* As i >= 2**31, the double cast above will yield a negative number.
13326 Since wrapping is defined in DWARF expressions we can output big
13327 positive integers as small negative ones, regardless of the size
13328 of host wide ints.
13330 Here, since the evaluator will handle 32-bit values and since i >=
13331 2**31, we know it's going to be interpreted as a negative literal:
13332 store it this way if we can do better than 5 bytes this way. */
13333 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13335 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13336 op = DW_OP_const4u;
13338 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
13339 least 6 bytes: see if we can do better before falling back to it. */
13340 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13341 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13342 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
13343 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
13344 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13345 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
13346 >= HOST_BITS_PER_WIDE_INT)
13347 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
13348 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
13349 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
13350 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13351 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13352 && size_of_uleb128 (i) > 6)
13353 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
13354 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
13355 else
13356 op = DW_OP_constu;
13358 else
13360 if (i >= -0x80)
13361 op = DW_OP_const1s;
13362 else if (i >= -0x8000)
13363 op = DW_OP_const2s;
13364 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13366 if (size_of_int_loc_descriptor (i) < 5)
13368 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13369 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13370 return ret;
13372 op = DW_OP_const4s;
13374 else
13376 if (size_of_int_loc_descriptor (i)
13377 < (unsigned long) 1 + size_of_sleb128 (i))
13379 dw_loc_descr_ref ret = int_loc_descriptor (-i);
13380 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13381 return ret;
13383 op = DW_OP_consts;
13387 return new_loc_descr (op, i, 0);
13390 /* Likewise, for unsigned constants. */
13392 static dw_loc_descr_ref
13393 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
13395 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
13396 const unsigned HOST_WIDE_INT max_uint
13397 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
13399 /* If possible, use the clever signed constants handling. */
13400 if (i <= max_int)
13401 return int_loc_descriptor ((HOST_WIDE_INT) i);
13403 /* Here, we are left with positive numbers that cannot be represented as
13404 HOST_WIDE_INT, i.e.:
13405 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
13407 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
13408 whereas may be better to output a negative integer: thanks to integer
13409 wrapping, we know that:
13410 x = x - 2 ** DWARF2_ADDR_SIZE
13411 = x - 2 * (max (HOST_WIDE_INT) + 1)
13412 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
13413 small negative integers. Let's try that in cases it will clearly improve
13414 the encoding: there is no gain turning DW_OP_const4u into
13415 DW_OP_const4s. */
13416 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
13417 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
13418 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
13420 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
13422 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
13423 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
13424 const HOST_WIDE_INT second_shift
13425 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
13427 /* So we finally have:
13428 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
13429 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
13430 return int_loc_descriptor (second_shift);
13433 /* Last chance: fallback to a simple constant operation. */
13434 return new_loc_descr
13435 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13436 ? DW_OP_const4u
13437 : DW_OP_const8u,
13438 i, 0);
13441 /* Generate and return a location description that computes the unsigned
13442 comparison of the two stack top entries (a OP b where b is the top-most
13443 entry and a is the second one). The KIND of comparison can be LT_EXPR,
13444 LE_EXPR, GT_EXPR or GE_EXPR. */
13446 static dw_loc_descr_ref
13447 uint_comparison_loc_list (enum tree_code kind)
13449 enum dwarf_location_atom op, flip_op;
13450 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
13452 switch (kind)
13454 case LT_EXPR:
13455 op = DW_OP_lt;
13456 break;
13457 case LE_EXPR:
13458 op = DW_OP_le;
13459 break;
13460 case GT_EXPR:
13461 op = DW_OP_gt;
13462 break;
13463 case GE_EXPR:
13464 op = DW_OP_ge;
13465 break;
13466 default:
13467 gcc_unreachable ();
13470 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13471 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
13473 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
13474 possible to perform unsigned comparisons: we just have to distinguish
13475 three cases:
13477 1. when a and b have the same sign (as signed integers); then we should
13478 return: a OP(signed) b;
13480 2. when a is a negative signed integer while b is a positive one, then a
13481 is a greater unsigned integer than b; likewise when a and b's roles
13482 are flipped.
13484 So first, compare the sign of the two operands. */
13485 ret = new_loc_descr (DW_OP_over, 0, 0);
13486 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13487 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
13488 /* If they have different signs (i.e. they have different sign bits), then
13489 the stack top value has now the sign bit set and thus it's smaller than
13490 zero. */
13491 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
13492 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
13493 add_loc_descr (&ret, bra_node);
13495 /* We are in case 1. At this point, we know both operands have the same
13496 sign, to it's safe to use the built-in signed comparison. */
13497 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13498 add_loc_descr (&ret, jmp_node);
13500 /* We are in case 2. Here, we know both operands do not have the same sign,
13501 so we have to flip the signed comparison. */
13502 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
13503 tmp = new_loc_descr (flip_op, 0, 0);
13504 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13505 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
13506 add_loc_descr (&ret, tmp);
13508 /* This dummy operation is necessary to make the two branches join. */
13509 tmp = new_loc_descr (DW_OP_nop, 0, 0);
13510 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13511 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
13512 add_loc_descr (&ret, tmp);
13514 return ret;
13517 /* Likewise, but takes the location description lists (might be destructive on
13518 them). Return NULL if either is NULL or if concatenation fails. */
13520 static dw_loc_list_ref
13521 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
13522 enum tree_code kind)
13524 if (left == NULL || right == NULL)
13525 return NULL;
13527 add_loc_list (&left, right);
13528 if (left == NULL)
13529 return NULL;
13531 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
13532 return left;
13535 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
13536 without actually allocating it. */
13538 static unsigned long
13539 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
13541 return size_of_int_loc_descriptor (i >> shift)
13542 + size_of_int_loc_descriptor (shift)
13543 + 1;
13546 /* Return size_of_locs (int_loc_descriptor (i)) without
13547 actually allocating it. */
13549 static unsigned long
13550 size_of_int_loc_descriptor (HOST_WIDE_INT i)
13552 unsigned long s;
13554 if (i >= 0)
13556 int clz, ctz;
13557 if (i <= 31)
13558 return 1;
13559 else if (i <= 0xff)
13560 return 2;
13561 else if (i <= 0xffff)
13562 return 3;
13563 clz = clz_hwi (i);
13564 ctz = ctz_hwi (i);
13565 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
13566 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
13567 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13568 - clz - 5);
13569 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13570 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
13571 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13572 - clz - 8);
13573 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
13574 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
13575 <= 4)
13576 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
13577 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
13578 return 5;
13579 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13580 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
13581 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
13582 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13583 - clz - 8);
13584 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
13585 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
13586 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13587 - clz - 16);
13588 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
13589 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
13590 && s > 6)
13591 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
13592 - clz - 32);
13593 else
13594 return 1 + s;
13596 else
13598 if (i >= -0x80)
13599 return 2;
13600 else if (i >= -0x8000)
13601 return 3;
13602 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
13604 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13606 s = size_of_int_loc_descriptor (-i) + 1;
13607 if (s < 5)
13608 return s;
13610 return 5;
13612 else
13614 unsigned long r = 1 + size_of_sleb128 (i);
13615 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
13617 s = size_of_int_loc_descriptor (-i) + 1;
13618 if (s < r)
13619 return s;
13621 return r;
13626 /* Return loc description representing "address" of integer value.
13627 This can appear only as toplevel expression. */
13629 static dw_loc_descr_ref
13630 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13632 int litsize;
13633 dw_loc_descr_ref loc_result = NULL;
13635 if (!(dwarf_version >= 4 || !dwarf_strict))
13636 return NULL;
13638 litsize = size_of_int_loc_descriptor (i);
13639 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13640 is more compact. For DW_OP_stack_value we need:
13641 litsize + 1 (DW_OP_stack_value)
13642 and for DW_OP_implicit_value:
13643 1 (DW_OP_implicit_value) + 1 (length) + size. */
13644 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13646 loc_result = int_loc_descriptor (i);
13647 add_loc_descr (&loc_result,
13648 new_loc_descr (DW_OP_stack_value, 0, 0));
13649 return loc_result;
13652 loc_result = new_loc_descr (DW_OP_implicit_value,
13653 size, 0);
13654 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13655 loc_result->dw_loc_oprnd2.v.val_int = i;
13656 return loc_result;
13659 /* Return a location descriptor that designates a base+offset location. */
13661 static dw_loc_descr_ref
13662 based_loc_descr (rtx reg, poly_int64 offset,
13663 enum var_init_status initialized)
13665 unsigned int regno;
13666 dw_loc_descr_ref result;
13667 dw_fde_ref fde = cfun->fde;
13669 /* We only use "frame base" when we're sure we're talking about the
13670 post-prologue local stack frame. We do this by *not* running
13671 register elimination until this point, and recognizing the special
13672 argument pointer and soft frame pointer rtx's. */
13673 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13675 rtx elim = (ira_use_lra_p
13676 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
13677 : eliminate_regs (reg, VOIDmode, NULL_RTX));
13679 if (elim != reg)
13681 elim = strip_offset_and_add (elim, &offset);
13682 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13683 && (elim == hard_frame_pointer_rtx
13684 || elim == stack_pointer_rtx))
13685 || elim == (frame_pointer_needed
13686 ? hard_frame_pointer_rtx
13687 : stack_pointer_rtx));
13689 /* If drap register is used to align stack, use frame
13690 pointer + offset to access stack variables. If stack
13691 is aligned without drap, use stack pointer + offset to
13692 access stack variables. */
13693 if (crtl->stack_realign_tried
13694 && reg == frame_pointer_rtx)
13696 int base_reg
13697 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13698 ? HARD_FRAME_POINTER_REGNUM
13699 : REGNO (elim));
13700 return new_reg_loc_descr (base_reg, offset);
13703 gcc_assert (frame_pointer_fb_offset_valid);
13704 offset += frame_pointer_fb_offset;
13705 HOST_WIDE_INT const_offset;
13706 if (offset.is_constant (&const_offset))
13707 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
13708 else
13710 dw_loc_descr_ref ret = new_loc_descr (DW_OP_fbreg, 0, 0);
13711 loc_descr_plus_const (&ret, offset);
13712 return ret;
13717 regno = REGNO (reg);
13718 #ifdef LEAF_REG_REMAP
13719 if (crtl->uses_only_leaf_regs)
13721 int leaf_reg = LEAF_REG_REMAP (regno);
13722 if (leaf_reg != -1)
13723 regno = (unsigned) leaf_reg;
13725 #endif
13726 regno = DWARF_FRAME_REGNUM (regno);
13728 HOST_WIDE_INT const_offset;
13729 if (!optimize && fde
13730 && (fde->drap_reg == regno || fde->vdrap_reg == regno)
13731 && offset.is_constant (&const_offset))
13733 /* Use cfa+offset to represent the location of arguments passed
13734 on the stack when drap is used to align stack.
13735 Only do this when not optimizing, for optimized code var-tracking
13736 is supposed to track where the arguments live and the register
13737 used as vdrap or drap in some spot might be used for something
13738 else in other part of the routine. */
13739 return new_loc_descr (DW_OP_fbreg, const_offset, 0);
13742 result = new_reg_loc_descr (regno, offset);
13744 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13745 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13747 return result;
13750 /* Return true if this RTL expression describes a base+offset calculation. */
13752 static inline int
13753 is_based_loc (const_rtx rtl)
13755 return (GET_CODE (rtl) == PLUS
13756 && ((REG_P (XEXP (rtl, 0))
13757 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13758 && CONST_INT_P (XEXP (rtl, 1)))));
13761 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13762 failed. */
13764 static dw_loc_descr_ref
13765 tls_mem_loc_descriptor (rtx mem)
13767 tree base;
13768 dw_loc_descr_ref loc_result;
13770 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
13771 return NULL;
13773 base = get_base_address (MEM_EXPR (mem));
13774 if (base == NULL
13775 || !VAR_P (base)
13776 || !DECL_THREAD_LOCAL_P (base))
13777 return NULL;
13779 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
13780 if (loc_result == NULL)
13781 return NULL;
13783 if (maybe_ne (MEM_OFFSET (mem), 0))
13784 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
13786 return loc_result;
13789 /* Output debug info about reason why we failed to expand expression as dwarf
13790 expression. */
13792 static void
13793 expansion_failed (tree expr, rtx rtl, char const *reason)
13795 if (dump_file && (dump_flags & TDF_DETAILS))
13797 fprintf (dump_file, "Failed to expand as dwarf: ");
13798 if (expr)
13799 print_generic_expr (dump_file, expr, dump_flags);
13800 if (rtl)
13802 fprintf (dump_file, "\n");
13803 print_rtl (dump_file, rtl);
13805 fprintf (dump_file, "\nReason: %s\n", reason);
13809 /* Helper function for const_ok_for_output. */
13811 static bool
13812 const_ok_for_output_1 (rtx rtl)
13814 if (targetm.const_not_ok_for_debug_p (rtl))
13816 if (GET_CODE (rtl) != UNSPEC)
13818 expansion_failed (NULL_TREE, rtl,
13819 "Expression rejected for debug by the backend.\n");
13820 return false;
13823 /* If delegitimize_address couldn't do anything with the UNSPEC, and
13824 the target hook doesn't explicitly allow it in debug info, assume
13825 we can't express it in the debug info. */
13826 /* Don't complain about TLS UNSPECs, those are just too hard to
13827 delegitimize. Note this could be a non-decl SYMBOL_REF such as
13828 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
13829 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
13830 if (flag_checking
13831 && (XVECLEN (rtl, 0) == 0
13832 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13833 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
13834 inform (current_function_decl
13835 ? DECL_SOURCE_LOCATION (current_function_decl)
13836 : UNKNOWN_LOCATION,
13837 #if NUM_UNSPEC_VALUES > 0
13838 "non-delegitimized UNSPEC %s (%d) found in variable location",
13839 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13840 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13841 XINT (rtl, 1));
13842 #else
13843 "non-delegitimized UNSPEC %d found in variable location",
13844 XINT (rtl, 1));
13845 #endif
13846 expansion_failed (NULL_TREE, rtl,
13847 "UNSPEC hasn't been delegitimized.\n");
13848 return false;
13851 if (CONST_POLY_INT_P (rtl))
13852 return false;
13854 if (targetm.const_not_ok_for_debug_p (rtl))
13856 expansion_failed (NULL_TREE, rtl,
13857 "Expression rejected for debug by the backend.\n");
13858 return false;
13861 /* FIXME: Refer to PR60655. It is possible for simplification
13862 of rtl expressions in var tracking to produce such expressions.
13863 We should really identify / validate expressions
13864 enclosed in CONST that can be handled by assemblers on various
13865 targets and only handle legitimate cases here. */
13866 switch (GET_CODE (rtl))
13868 case SYMBOL_REF:
13869 break;
13870 case NOT:
13871 case NEG:
13872 return false;
13873 default:
13874 return true;
13877 if (CONSTANT_POOL_ADDRESS_P (rtl))
13879 bool marked;
13880 get_pool_constant_mark (rtl, &marked);
13881 /* If all references to this pool constant were optimized away,
13882 it was not output and thus we can't represent it. */
13883 if (!marked)
13885 expansion_failed (NULL_TREE, rtl,
13886 "Constant was removed from constant pool.\n");
13887 return false;
13891 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13892 return false;
13894 /* Avoid references to external symbols in debug info, on several targets
13895 the linker might even refuse to link when linking a shared library,
13896 and in many other cases the relocations for .debug_info/.debug_loc are
13897 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
13898 to be defined within the same shared library or executable are fine. */
13899 if (SYMBOL_REF_EXTERNAL_P (rtl))
13901 tree decl = SYMBOL_REF_DECL (rtl);
13903 if (decl == NULL || !targetm.binds_local_p (decl))
13905 expansion_failed (NULL_TREE, rtl,
13906 "Symbol not defined in current TU.\n");
13907 return false;
13911 return true;
13914 /* Return true if constant RTL can be emitted in DW_OP_addr or
13915 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
13916 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
13918 static bool
13919 const_ok_for_output (rtx rtl)
13921 if (GET_CODE (rtl) == SYMBOL_REF)
13922 return const_ok_for_output_1 (rtl);
13924 if (GET_CODE (rtl) == CONST)
13926 subrtx_var_iterator::array_type array;
13927 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
13928 if (!const_ok_for_output_1 (*iter))
13929 return false;
13930 return true;
13933 return true;
13936 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13937 if possible, NULL otherwise. */
13939 static dw_die_ref
13940 base_type_for_mode (machine_mode mode, bool unsignedp)
13942 dw_die_ref type_die;
13943 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13945 if (type == NULL)
13946 return NULL;
13947 switch (TREE_CODE (type))
13949 case INTEGER_TYPE:
13950 case REAL_TYPE:
13951 break;
13952 default:
13953 return NULL;
13955 type_die = lookup_type_die (type);
13956 if (!type_die)
13957 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
13958 comp_unit_die ());
13959 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13960 return NULL;
13961 return type_die;
13964 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13965 type matching MODE, or, if MODE is narrower than or as wide as
13966 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
13967 possible. */
13969 static dw_loc_descr_ref
13970 convert_descriptor_to_mode (scalar_int_mode mode, dw_loc_descr_ref op)
13972 machine_mode outer_mode = mode;
13973 dw_die_ref type_die;
13974 dw_loc_descr_ref cvt;
13976 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13978 add_loc_descr (&op, new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0));
13979 return op;
13981 type_die = base_type_for_mode (outer_mode, 1);
13982 if (type_die == NULL)
13983 return NULL;
13984 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
13985 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13986 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13987 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13988 add_loc_descr (&op, cvt);
13989 return op;
13992 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
13994 static dw_loc_descr_ref
13995 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13996 dw_loc_descr_ref op1)
13998 dw_loc_descr_ref ret = op0;
13999 add_loc_descr (&ret, op1);
14000 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14001 if (STORE_FLAG_VALUE != 1)
14003 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
14004 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
14006 return ret;
14009 /* Subroutine of scompare_loc_descriptor for the case in which we're
14010 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14011 and in which OP_MODE is bigger than DWARF2_ADDR_SIZE. */
14013 static dw_loc_descr_ref
14014 scompare_loc_descriptor_wide (enum dwarf_location_atom op,
14015 scalar_int_mode op_mode,
14016 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14018 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14019 dw_loc_descr_ref cvt;
14021 if (type_die == NULL)
14022 return NULL;
14023 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14024 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14025 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14026 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14027 add_loc_descr (&op0, cvt);
14028 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14029 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14030 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14031 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14032 add_loc_descr (&op1, cvt);
14033 return compare_loc_descriptor (op, op0, op1);
14036 /* Subroutine of scompare_loc_descriptor for the case in which we're
14037 comparing two scalar integer operands OP0 and OP1 that have mode OP_MODE,
14038 and in which OP_MODE is smaller than DWARF2_ADDR_SIZE. */
14040 static dw_loc_descr_ref
14041 scompare_loc_descriptor_narrow (enum dwarf_location_atom op, rtx rtl,
14042 scalar_int_mode op_mode,
14043 dw_loc_descr_ref op0, dw_loc_descr_ref op1)
14045 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14046 /* For eq/ne, if the operands are known to be zero-extended,
14047 there is no need to do the fancy shifting up. */
14048 if (op == DW_OP_eq || op == DW_OP_ne)
14050 dw_loc_descr_ref last0, last1;
14051 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14053 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14055 /* deref_size zero extends, and for constants we can check
14056 whether they are zero extended or not. */
14057 if (((last0->dw_loc_opc == DW_OP_deref_size
14058 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14059 || (CONST_INT_P (XEXP (rtl, 0))
14060 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14061 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14062 && ((last1->dw_loc_opc == DW_OP_deref_size
14063 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14064 || (CONST_INT_P (XEXP (rtl, 1))
14065 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14066 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14067 return compare_loc_descriptor (op, op0, op1);
14069 /* EQ/NE comparison against constant in narrower type than
14070 DWARF2_ADDR_SIZE can be performed either as
14071 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
14072 DW_OP_{eq,ne}
14074 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
14075 DW_OP_{eq,ne}. Pick whatever is shorter. */
14076 if (CONST_INT_P (XEXP (rtl, 1))
14077 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
14078 && (size_of_int_loc_descriptor (shift) + 1
14079 + size_of_int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift)
14080 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
14081 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14082 & GET_MODE_MASK (op_mode))))
14084 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
14085 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14086 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
14087 & GET_MODE_MASK (op_mode));
14088 return compare_loc_descriptor (op, op0, op1);
14091 add_loc_descr (&op0, int_loc_descriptor (shift));
14092 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14093 if (CONST_INT_P (XEXP (rtl, 1)))
14094 op1 = int_loc_descriptor (UINTVAL (XEXP (rtl, 1)) << shift);
14095 else
14097 add_loc_descr (&op1, int_loc_descriptor (shift));
14098 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14100 return compare_loc_descriptor (op, op0, op1);
14103 /* Return location descriptor for unsigned comparison OP RTL. */
14105 static dw_loc_descr_ref
14106 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14107 machine_mode mem_mode)
14109 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14110 dw_loc_descr_ref op0, op1;
14112 if (op_mode == VOIDmode)
14113 op_mode = GET_MODE (XEXP (rtl, 1));
14114 if (op_mode == VOIDmode)
14115 return NULL;
14117 scalar_int_mode int_op_mode;
14118 if (dwarf_strict
14119 && dwarf_version < 5
14120 && (!is_a <scalar_int_mode> (op_mode, &int_op_mode)
14121 || GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE))
14122 return NULL;
14124 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14125 VAR_INIT_STATUS_INITIALIZED);
14126 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14127 VAR_INIT_STATUS_INITIALIZED);
14129 if (op0 == NULL || op1 == NULL)
14130 return NULL;
14132 if (is_a <scalar_int_mode> (op_mode, &int_op_mode))
14134 if (GET_MODE_SIZE (int_op_mode) < DWARF2_ADDR_SIZE)
14135 return scompare_loc_descriptor_narrow (op, rtl, int_op_mode, op0, op1);
14137 if (GET_MODE_SIZE (int_op_mode) > DWARF2_ADDR_SIZE)
14138 return scompare_loc_descriptor_wide (op, int_op_mode, op0, op1);
14140 return compare_loc_descriptor (op, op0, op1);
14143 /* Return location descriptor for unsigned comparison OP RTL. */
14145 static dw_loc_descr_ref
14146 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14147 machine_mode mem_mode)
14149 dw_loc_descr_ref op0, op1;
14151 machine_mode test_op_mode = GET_MODE (XEXP (rtl, 0));
14152 if (test_op_mode == VOIDmode)
14153 test_op_mode = GET_MODE (XEXP (rtl, 1));
14155 scalar_int_mode op_mode;
14156 if (!is_a <scalar_int_mode> (test_op_mode, &op_mode))
14157 return NULL;
14159 if (dwarf_strict
14160 && dwarf_version < 5
14161 && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14162 return NULL;
14164 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14165 VAR_INIT_STATUS_INITIALIZED);
14166 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14167 VAR_INIT_STATUS_INITIALIZED);
14169 if (op0 == NULL || op1 == NULL)
14170 return NULL;
14172 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14174 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14175 dw_loc_descr_ref last0, last1;
14176 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14178 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14180 if (CONST_INT_P (XEXP (rtl, 0)))
14181 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14182 /* deref_size zero extends, so no need to mask it again. */
14183 else if (last0->dw_loc_opc != DW_OP_deref_size
14184 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14186 add_loc_descr (&op0, int_loc_descriptor (mask));
14187 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14189 if (CONST_INT_P (XEXP (rtl, 1)))
14190 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14191 /* deref_size zero extends, so no need to mask it again. */
14192 else if (last1->dw_loc_opc != DW_OP_deref_size
14193 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14195 add_loc_descr (&op1, int_loc_descriptor (mask));
14196 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14199 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14201 HOST_WIDE_INT bias = 1;
14202 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14203 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14204 if (CONST_INT_P (XEXP (rtl, 1)))
14205 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14206 + INTVAL (XEXP (rtl, 1)));
14207 else
14208 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14209 bias, 0));
14211 return compare_loc_descriptor (op, op0, op1);
14214 /* Return location descriptor for {U,S}{MIN,MAX}. */
14216 static dw_loc_descr_ref
14217 minmax_loc_descriptor (rtx rtl, machine_mode mode,
14218 machine_mode mem_mode)
14220 enum dwarf_location_atom op;
14221 dw_loc_descr_ref op0, op1, ret;
14222 dw_loc_descr_ref bra_node, drop_node;
14224 scalar_int_mode int_mode;
14225 if (dwarf_strict
14226 && dwarf_version < 5
14227 && (!is_a <scalar_int_mode> (mode, &int_mode)
14228 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE))
14229 return NULL;
14231 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14232 VAR_INIT_STATUS_INITIALIZED);
14233 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14234 VAR_INIT_STATUS_INITIALIZED);
14236 if (op0 == NULL || op1 == NULL)
14237 return NULL;
14239 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14240 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14241 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14242 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14244 /* Checked by the caller. */
14245 int_mode = as_a <scalar_int_mode> (mode);
14246 if (GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14248 HOST_WIDE_INT mask = GET_MODE_MASK (int_mode);
14249 add_loc_descr (&op0, int_loc_descriptor (mask));
14250 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14251 add_loc_descr (&op1, int_loc_descriptor (mask));
14252 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14254 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14256 HOST_WIDE_INT bias = 1;
14257 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14258 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14259 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14262 else if (is_a <scalar_int_mode> (mode, &int_mode)
14263 && GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE)
14265 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (int_mode)) * BITS_PER_UNIT;
14266 add_loc_descr (&op0, int_loc_descriptor (shift));
14267 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14268 add_loc_descr (&op1, int_loc_descriptor (shift));
14269 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14271 else if (is_a <scalar_int_mode> (mode, &int_mode)
14272 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14274 dw_die_ref type_die = base_type_for_mode (int_mode, 0);
14275 dw_loc_descr_ref cvt;
14276 if (type_die == NULL)
14277 return NULL;
14278 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14279 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14280 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14281 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14282 add_loc_descr (&op0, cvt);
14283 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14284 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14285 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14286 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14287 add_loc_descr (&op1, cvt);
14290 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14291 op = DW_OP_lt;
14292 else
14293 op = DW_OP_gt;
14294 ret = op0;
14295 add_loc_descr (&ret, op1);
14296 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14297 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14298 add_loc_descr (&ret, bra_node);
14299 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14300 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14301 add_loc_descr (&ret, drop_node);
14302 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14303 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14304 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14305 && is_a <scalar_int_mode> (mode, &int_mode)
14306 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14307 ret = convert_descriptor_to_mode (int_mode, ret);
14308 return ret;
14311 /* Helper function for mem_loc_descriptor. Perform OP binary op,
14312 but after converting arguments to type_die, afterwards
14313 convert back to unsigned. */
14315 static dw_loc_descr_ref
14316 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14317 scalar_int_mode mode, machine_mode mem_mode)
14319 dw_loc_descr_ref cvt, op0, op1;
14321 if (type_die == NULL)
14322 return NULL;
14323 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14324 VAR_INIT_STATUS_INITIALIZED);
14325 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14326 VAR_INIT_STATUS_INITIALIZED);
14327 if (op0 == NULL || op1 == NULL)
14328 return NULL;
14329 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14330 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14331 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14332 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14333 add_loc_descr (&op0, cvt);
14334 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14335 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14336 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14337 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14338 add_loc_descr (&op1, cvt);
14339 add_loc_descr (&op0, op1);
14340 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14341 return convert_descriptor_to_mode (mode, op0);
14344 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14345 const0 is DW_OP_lit0 or corresponding typed constant,
14346 const1 is DW_OP_lit1 or corresponding typed constant
14347 and constMSB is constant with just the MSB bit set
14348 for the mode):
14349 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14350 L1: const0 DW_OP_swap
14351 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14352 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14353 L3: DW_OP_drop
14354 L4: DW_OP_nop
14356 CTZ is similar:
14357 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14358 L1: const0 DW_OP_swap
14359 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14360 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14361 L3: DW_OP_drop
14362 L4: DW_OP_nop
14364 FFS is similar:
14365 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14366 L1: const1 DW_OP_swap
14367 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14368 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14369 L3: DW_OP_drop
14370 L4: DW_OP_nop */
14372 static dw_loc_descr_ref
14373 clz_loc_descriptor (rtx rtl, scalar_int_mode mode,
14374 machine_mode mem_mode)
14376 dw_loc_descr_ref op0, ret, tmp;
14377 HOST_WIDE_INT valv;
14378 dw_loc_descr_ref l1jump, l1label;
14379 dw_loc_descr_ref l2jump, l2label;
14380 dw_loc_descr_ref l3jump, l3label;
14381 dw_loc_descr_ref l4jump, l4label;
14382 rtx msb;
14384 if (GET_MODE (XEXP (rtl, 0)) != mode)
14385 return NULL;
14387 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14388 VAR_INIT_STATUS_INITIALIZED);
14389 if (op0 == NULL)
14390 return NULL;
14391 ret = op0;
14392 if (GET_CODE (rtl) == CLZ)
14394 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14395 valv = GET_MODE_BITSIZE (mode);
14397 else if (GET_CODE (rtl) == FFS)
14398 valv = 0;
14399 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14400 valv = GET_MODE_BITSIZE (mode);
14401 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14402 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14403 add_loc_descr (&ret, l1jump);
14404 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14405 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14406 VAR_INIT_STATUS_INITIALIZED);
14407 if (tmp == NULL)
14408 return NULL;
14409 add_loc_descr (&ret, tmp);
14410 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14411 add_loc_descr (&ret, l4jump);
14412 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14413 ? const1_rtx : const0_rtx,
14414 mode, mem_mode,
14415 VAR_INIT_STATUS_INITIALIZED);
14416 if (l1label == NULL)
14417 return NULL;
14418 add_loc_descr (&ret, l1label);
14419 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14420 l2label = new_loc_descr (DW_OP_dup, 0, 0);
14421 add_loc_descr (&ret, l2label);
14422 if (GET_CODE (rtl) != CLZ)
14423 msb = const1_rtx;
14424 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14425 msb = GEN_INT (HOST_WIDE_INT_1U
14426 << (GET_MODE_BITSIZE (mode) - 1));
14427 else
14428 msb = immed_wide_int_const
14429 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
14430 GET_MODE_PRECISION (mode)), mode);
14431 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14432 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14433 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14434 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14435 else
14436 tmp = mem_loc_descriptor (msb, mode, mem_mode,
14437 VAR_INIT_STATUS_INITIALIZED);
14438 if (tmp == NULL)
14439 return NULL;
14440 add_loc_descr (&ret, tmp);
14441 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14442 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14443 add_loc_descr (&ret, l3jump);
14444 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14445 VAR_INIT_STATUS_INITIALIZED);
14446 if (tmp == NULL)
14447 return NULL;
14448 add_loc_descr (&ret, tmp);
14449 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14450 ? DW_OP_shl : DW_OP_shr, 0, 0));
14451 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14452 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14453 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14454 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14455 add_loc_descr (&ret, l2jump);
14456 l3label = new_loc_descr (DW_OP_drop, 0, 0);
14457 add_loc_descr (&ret, l3label);
14458 l4label = new_loc_descr (DW_OP_nop, 0, 0);
14459 add_loc_descr (&ret, l4label);
14460 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14461 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14462 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14463 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14464 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14465 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14466 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14467 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14468 return ret;
14471 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14472 const1 is DW_OP_lit1 or corresponding typed constant):
14473 const0 DW_OP_swap
14474 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14475 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14476 L2: DW_OP_drop
14478 PARITY is similar:
14479 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14480 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14481 L2: DW_OP_drop */
14483 static dw_loc_descr_ref
14484 popcount_loc_descriptor (rtx rtl, scalar_int_mode mode,
14485 machine_mode mem_mode)
14487 dw_loc_descr_ref op0, ret, tmp;
14488 dw_loc_descr_ref l1jump, l1label;
14489 dw_loc_descr_ref l2jump, l2label;
14491 if (GET_MODE (XEXP (rtl, 0)) != mode)
14492 return NULL;
14494 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14495 VAR_INIT_STATUS_INITIALIZED);
14496 if (op0 == NULL)
14497 return NULL;
14498 ret = op0;
14499 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14500 VAR_INIT_STATUS_INITIALIZED);
14501 if (tmp == NULL)
14502 return NULL;
14503 add_loc_descr (&ret, tmp);
14504 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14505 l1label = new_loc_descr (DW_OP_dup, 0, 0);
14506 add_loc_descr (&ret, l1label);
14507 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14508 add_loc_descr (&ret, l2jump);
14509 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14510 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14511 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14512 VAR_INIT_STATUS_INITIALIZED);
14513 if (tmp == NULL)
14514 return NULL;
14515 add_loc_descr (&ret, tmp);
14516 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14517 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14518 ? DW_OP_plus : DW_OP_xor, 0, 0));
14519 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14520 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14521 VAR_INIT_STATUS_INITIALIZED);
14522 add_loc_descr (&ret, tmp);
14523 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14524 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14525 add_loc_descr (&ret, l1jump);
14526 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14527 add_loc_descr (&ret, l2label);
14528 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14529 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14530 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14531 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14532 return ret;
14535 /* BSWAP (constS is initial shift count, either 56 or 24):
14536 constS const0
14537 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14538 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14539 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14540 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14541 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
14543 static dw_loc_descr_ref
14544 bswap_loc_descriptor (rtx rtl, scalar_int_mode mode,
14545 machine_mode mem_mode)
14547 dw_loc_descr_ref op0, ret, tmp;
14548 dw_loc_descr_ref l1jump, l1label;
14549 dw_loc_descr_ref l2jump, l2label;
14551 if (BITS_PER_UNIT != 8
14552 || (GET_MODE_BITSIZE (mode) != 32
14553 && GET_MODE_BITSIZE (mode) != 64))
14554 return NULL;
14556 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14557 VAR_INIT_STATUS_INITIALIZED);
14558 if (op0 == NULL)
14559 return NULL;
14561 ret = op0;
14562 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14563 mode, mem_mode,
14564 VAR_INIT_STATUS_INITIALIZED);
14565 if (tmp == NULL)
14566 return NULL;
14567 add_loc_descr (&ret, tmp);
14568 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14569 VAR_INIT_STATUS_INITIALIZED);
14570 if (tmp == NULL)
14571 return NULL;
14572 add_loc_descr (&ret, tmp);
14573 l1label = new_loc_descr (DW_OP_pick, 2, 0);
14574 add_loc_descr (&ret, l1label);
14575 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14576 mode, mem_mode,
14577 VAR_INIT_STATUS_INITIALIZED);
14578 add_loc_descr (&ret, tmp);
14579 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14580 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14581 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14582 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14583 VAR_INIT_STATUS_INITIALIZED);
14584 if (tmp == NULL)
14585 return NULL;
14586 add_loc_descr (&ret, tmp);
14587 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14588 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14589 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14590 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14591 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14592 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14593 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14594 VAR_INIT_STATUS_INITIALIZED);
14595 add_loc_descr (&ret, tmp);
14596 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14597 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14598 add_loc_descr (&ret, l2jump);
14599 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14600 VAR_INIT_STATUS_INITIALIZED);
14601 add_loc_descr (&ret, tmp);
14602 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14603 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14604 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14605 add_loc_descr (&ret, l1jump);
14606 l2label = new_loc_descr (DW_OP_drop, 0, 0);
14607 add_loc_descr (&ret, l2label);
14608 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14609 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14610 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14611 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14612 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14613 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14614 return ret;
14617 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14618 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14619 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14620 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14622 ROTATERT is similar:
14623 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14624 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14625 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
14627 static dw_loc_descr_ref
14628 rotate_loc_descriptor (rtx rtl, scalar_int_mode mode,
14629 machine_mode mem_mode)
14631 rtx rtlop1 = XEXP (rtl, 1);
14632 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14633 int i;
14635 if (is_narrower_int_mode (GET_MODE (rtlop1), mode))
14636 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14637 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14638 VAR_INIT_STATUS_INITIALIZED);
14639 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14640 VAR_INIT_STATUS_INITIALIZED);
14641 if (op0 == NULL || op1 == NULL)
14642 return NULL;
14643 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14644 for (i = 0; i < 2; i++)
14646 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14647 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14648 mode, mem_mode,
14649 VAR_INIT_STATUS_INITIALIZED);
14650 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14651 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14652 ? DW_OP_const4u
14653 : HOST_BITS_PER_WIDE_INT == 64
14654 ? DW_OP_const8u : DW_OP_constu,
14655 GET_MODE_MASK (mode), 0);
14656 else
14657 mask[i] = NULL;
14658 if (mask[i] == NULL)
14659 return NULL;
14660 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14662 ret = op0;
14663 add_loc_descr (&ret, op1);
14664 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14665 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14666 if (GET_CODE (rtl) == ROTATERT)
14668 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14669 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14670 GET_MODE_BITSIZE (mode), 0));
14672 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14673 if (mask[0] != NULL)
14674 add_loc_descr (&ret, mask[0]);
14675 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14676 if (mask[1] != NULL)
14678 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14679 add_loc_descr (&ret, mask[1]);
14680 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14682 if (GET_CODE (rtl) == ROTATE)
14684 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14685 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14686 GET_MODE_BITSIZE (mode), 0));
14688 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14689 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14690 return ret;
14693 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
14694 for DEBUG_PARAMETER_REF RTL. */
14696 static dw_loc_descr_ref
14697 parameter_ref_descriptor (rtx rtl)
14699 dw_loc_descr_ref ret;
14700 dw_die_ref ref;
14702 if (dwarf_strict)
14703 return NULL;
14704 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14705 /* With LTO during LTRANS we get the late DIE that refers to the early
14706 DIE, thus we add another indirection here. This seems to confuse
14707 gdb enough to make gcc.dg/guality/pr68860-1.c FAIL with LTO. */
14708 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14709 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14710 if (ref)
14712 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14713 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14714 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14716 else
14718 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14719 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14721 return ret;
14724 /* The following routine converts the RTL for a variable or parameter
14725 (resident in memory) into an equivalent Dwarf representation of a
14726 mechanism for getting the address of that same variable onto the top of a
14727 hypothetical "address evaluation" stack.
14729 When creating memory location descriptors, we are effectively transforming
14730 the RTL for a memory-resident object into its Dwarf postfix expression
14731 equivalent. This routine recursively descends an RTL tree, turning
14732 it into Dwarf postfix code as it goes.
14734 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14736 MEM_MODE is the mode of the memory reference, needed to handle some
14737 autoincrement addressing modes.
14739 Return 0 if we can't represent the location. */
14741 dw_loc_descr_ref
14742 mem_loc_descriptor (rtx rtl, machine_mode mode,
14743 machine_mode mem_mode,
14744 enum var_init_status initialized)
14746 dw_loc_descr_ref mem_loc_result = NULL;
14747 enum dwarf_location_atom op;
14748 dw_loc_descr_ref op0, op1;
14749 rtx inner = NULL_RTX;
14750 poly_int64 offset;
14752 if (mode == VOIDmode)
14753 mode = GET_MODE (rtl);
14755 /* Note that for a dynamically sized array, the location we will generate a
14756 description of here will be the lowest numbered location which is
14757 actually within the array. That's *not* necessarily the same as the
14758 zeroth element of the array. */
14760 rtl = targetm.delegitimize_address (rtl);
14762 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14763 return NULL;
14765 scalar_int_mode int_mode, inner_mode, op1_mode;
14766 switch (GET_CODE (rtl))
14768 case POST_INC:
14769 case POST_DEC:
14770 case POST_MODIFY:
14771 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14773 case SUBREG:
14774 /* The case of a subreg may arise when we have a local (register)
14775 variable or a formal (register) parameter which doesn't quite fill
14776 up an entire register. For now, just assume that it is
14777 legitimate to make the Dwarf info refer to the whole register which
14778 contains the given subreg. */
14779 if (!subreg_lowpart_p (rtl))
14780 break;
14781 inner = SUBREG_REG (rtl);
14782 /* FALLTHRU */
14783 case TRUNCATE:
14784 if (inner == NULL_RTX)
14785 inner = XEXP (rtl, 0);
14786 if (is_a <scalar_int_mode> (mode, &int_mode)
14787 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14788 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14789 #ifdef POINTERS_EXTEND_UNSIGNED
14790 || (int_mode == Pmode && mem_mode != VOIDmode)
14791 #endif
14793 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE)
14795 mem_loc_result = mem_loc_descriptor (inner,
14796 inner_mode,
14797 mem_mode, initialized);
14798 break;
14800 if (dwarf_strict && dwarf_version < 5)
14801 break;
14802 if (is_a <scalar_int_mode> (mode, &int_mode)
14803 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
14804 ? GET_MODE_SIZE (int_mode) <= GET_MODE_SIZE (inner_mode)
14805 : known_eq (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
14807 dw_die_ref type_die;
14808 dw_loc_descr_ref cvt;
14810 mem_loc_result = mem_loc_descriptor (inner,
14811 GET_MODE (inner),
14812 mem_mode, initialized);
14813 if (mem_loc_result == NULL)
14814 break;
14815 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14816 if (type_die == NULL)
14818 mem_loc_result = NULL;
14819 break;
14821 if (maybe_ne (GET_MODE_SIZE (mode), GET_MODE_SIZE (GET_MODE (inner))))
14822 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14823 else
14824 cvt = new_loc_descr (dwarf_OP (DW_OP_reinterpret), 0, 0);
14825 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14826 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14827 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14828 add_loc_descr (&mem_loc_result, cvt);
14829 if (is_a <scalar_int_mode> (mode, &int_mode)
14830 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14832 /* Convert it to untyped afterwards. */
14833 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14834 add_loc_descr (&mem_loc_result, cvt);
14837 break;
14839 case REG:
14840 if (!is_a <scalar_int_mode> (mode, &int_mode)
14841 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
14842 && rtl != arg_pointer_rtx
14843 && rtl != frame_pointer_rtx
14844 #ifdef POINTERS_EXTEND_UNSIGNED
14845 && (int_mode != Pmode || mem_mode == VOIDmode)
14846 #endif
14849 dw_die_ref type_die;
14850 unsigned int dbx_regnum;
14852 if (dwarf_strict && dwarf_version < 5)
14853 break;
14854 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14855 break;
14856 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14857 if (type_die == NULL)
14858 break;
14860 dbx_regnum = dbx_reg_number (rtl);
14861 if (dbx_regnum == IGNORED_DWARF_REGNUM)
14862 break;
14863 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_regval_type),
14864 dbx_regnum, 0);
14865 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14866 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14867 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14868 break;
14870 /* Whenever a register number forms a part of the description of the
14871 method for calculating the (dynamic) address of a memory resident
14872 object, DWARF rules require the register number be referred to as
14873 a "base register". This distinction is not based in any way upon
14874 what category of register the hardware believes the given register
14875 belongs to. This is strictly DWARF terminology we're dealing with
14876 here. Note that in cases where the location of a memory-resident
14877 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14878 OP_CONST (0)) the actual DWARF location descriptor that we generate
14879 may just be OP_BASEREG (basereg). This may look deceptively like
14880 the object in question was allocated to a register (rather than in
14881 memory) so DWARF consumers need to be aware of the subtle
14882 distinction between OP_REG and OP_BASEREG. */
14883 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14884 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14885 else if (stack_realign_drap
14886 && crtl->drap_reg
14887 && crtl->args.internal_arg_pointer == rtl
14888 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14890 /* If RTL is internal_arg_pointer, which has been optimized
14891 out, use DRAP instead. */
14892 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14893 VAR_INIT_STATUS_INITIALIZED);
14895 break;
14897 case SIGN_EXTEND:
14898 case ZERO_EXTEND:
14899 if (!is_a <scalar_int_mode> (mode, &int_mode)
14900 || !is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode))
14901 break;
14902 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
14903 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14904 if (op0 == 0)
14905 break;
14906 else if (GET_CODE (rtl) == ZERO_EXTEND
14907 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
14908 && GET_MODE_BITSIZE (inner_mode) < HOST_BITS_PER_WIDE_INT
14909 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14910 to expand zero extend as two shifts instead of
14911 masking. */
14912 && GET_MODE_SIZE (inner_mode) <= 4)
14914 mem_loc_result = op0;
14915 add_loc_descr (&mem_loc_result,
14916 int_loc_descriptor (GET_MODE_MASK (inner_mode)));
14917 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14919 else if (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE)
14921 int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (inner_mode);
14922 shift *= BITS_PER_UNIT;
14923 if (GET_CODE (rtl) == SIGN_EXTEND)
14924 op = DW_OP_shra;
14925 else
14926 op = DW_OP_shr;
14927 mem_loc_result = op0;
14928 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14929 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14930 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14931 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14933 else if (!dwarf_strict || dwarf_version >= 5)
14935 dw_die_ref type_die1, type_die2;
14936 dw_loc_descr_ref cvt;
14938 type_die1 = base_type_for_mode (inner_mode,
14939 GET_CODE (rtl) == ZERO_EXTEND);
14940 if (type_die1 == NULL)
14941 break;
14942 type_die2 = base_type_for_mode (int_mode, 1);
14943 if (type_die2 == NULL)
14944 break;
14945 mem_loc_result = op0;
14946 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14947 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14948 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14949 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14950 add_loc_descr (&mem_loc_result, cvt);
14951 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
14952 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14953 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14954 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14955 add_loc_descr (&mem_loc_result, cvt);
14957 break;
14959 case MEM:
14961 rtx new_rtl = avoid_constant_pool_reference (rtl);
14962 if (new_rtl != rtl)
14964 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
14965 initialized);
14966 if (mem_loc_result != NULL)
14967 return mem_loc_result;
14970 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14971 get_address_mode (rtl), mode,
14972 VAR_INIT_STATUS_INITIALIZED);
14973 if (mem_loc_result == NULL)
14974 mem_loc_result = tls_mem_loc_descriptor (rtl);
14975 if (mem_loc_result != NULL)
14977 if (!is_a <scalar_int_mode> (mode, &int_mode)
14978 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
14980 dw_die_ref type_die;
14981 dw_loc_descr_ref deref;
14982 HOST_WIDE_INT size;
14984 if (dwarf_strict && dwarf_version < 5)
14985 return NULL;
14986 if (!GET_MODE_SIZE (mode).is_constant (&size))
14987 return NULL;
14988 type_die
14989 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14990 if (type_die == NULL)
14991 return NULL;
14992 deref = new_loc_descr (dwarf_OP (DW_OP_deref_type), size, 0);
14993 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14994 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14995 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14996 add_loc_descr (&mem_loc_result, deref);
14998 else if (GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE)
14999 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
15000 else
15001 add_loc_descr (&mem_loc_result,
15002 new_loc_descr (DW_OP_deref_size,
15003 GET_MODE_SIZE (int_mode), 0));
15005 break;
15007 case LO_SUM:
15008 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
15010 case LABEL_REF:
15011 /* Some ports can transform a symbol ref into a label ref, because
15012 the symbol ref is too far away and has to be dumped into a constant
15013 pool. */
15014 case CONST:
15015 case SYMBOL_REF:
15016 if (!is_a <scalar_int_mode> (mode, &int_mode)
15017 || (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE
15018 #ifdef POINTERS_EXTEND_UNSIGNED
15019 && (int_mode != Pmode || mem_mode == VOIDmode)
15020 #endif
15022 break;
15023 if (GET_CODE (rtl) == SYMBOL_REF
15024 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
15026 dw_loc_descr_ref temp;
15028 /* If this is not defined, we have no way to emit the data. */
15029 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
15030 break;
15032 temp = new_addr_loc_descr (rtl, dtprel_true);
15034 /* We check for DWARF 5 here because gdb did not implement
15035 DW_OP_form_tls_address until after 7.12. */
15036 mem_loc_result = new_loc_descr ((dwarf_version >= 5
15037 ? DW_OP_form_tls_address
15038 : DW_OP_GNU_push_tls_address),
15039 0, 0);
15040 add_loc_descr (&mem_loc_result, temp);
15042 break;
15045 if (!const_ok_for_output (rtl))
15047 if (GET_CODE (rtl) == CONST)
15048 switch (GET_CODE (XEXP (rtl, 0)))
15050 case NOT:
15051 op = DW_OP_not;
15052 goto try_const_unop;
15053 case NEG:
15054 op = DW_OP_neg;
15055 goto try_const_unop;
15056 try_const_unop:
15057 rtx arg;
15058 arg = XEXP (XEXP (rtl, 0), 0);
15059 if (!CONSTANT_P (arg))
15060 arg = gen_rtx_CONST (int_mode, arg);
15061 op0 = mem_loc_descriptor (arg, int_mode, mem_mode,
15062 initialized);
15063 if (op0)
15065 mem_loc_result = op0;
15066 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15068 break;
15069 default:
15070 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), int_mode,
15071 mem_mode, initialized);
15072 break;
15074 break;
15077 symref:
15078 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
15079 vec_safe_push (used_rtx_array, rtl);
15080 break;
15082 case CONCAT:
15083 case CONCATN:
15084 case VAR_LOCATION:
15085 case DEBUG_IMPLICIT_PTR:
15086 expansion_failed (NULL_TREE, rtl,
15087 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
15088 return 0;
15090 case ENTRY_VALUE:
15091 if (dwarf_strict && dwarf_version < 5)
15092 return NULL;
15093 if (REG_P (ENTRY_VALUE_EXP (rtl)))
15095 if (!is_a <scalar_int_mode> (mode, &int_mode)
15096 || GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15097 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15098 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15099 else
15101 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
15102 if (dbx_regnum == IGNORED_DWARF_REGNUM)
15103 return NULL;
15104 op0 = one_reg_loc_descriptor (dbx_regnum,
15105 VAR_INIT_STATUS_INITIALIZED);
15108 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
15109 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
15111 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
15112 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15113 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
15114 return NULL;
15116 else
15117 gcc_unreachable ();
15118 if (op0 == NULL)
15119 return NULL;
15120 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_entry_value), 0, 0);
15121 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
15122 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
15123 break;
15125 case DEBUG_PARAMETER_REF:
15126 mem_loc_result = parameter_ref_descriptor (rtl);
15127 break;
15129 case PRE_MODIFY:
15130 /* Extract the PLUS expression nested inside and fall into
15131 PLUS code below. */
15132 rtl = XEXP (rtl, 1);
15133 goto plus;
15135 case PRE_INC:
15136 case PRE_DEC:
15137 /* Turn these into a PLUS expression and fall into the PLUS code
15138 below. */
15139 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15140 gen_int_mode (GET_CODE (rtl) == PRE_INC
15141 ? GET_MODE_UNIT_SIZE (mem_mode)
15142 : -GET_MODE_UNIT_SIZE (mem_mode),
15143 mode));
15145 /* fall through */
15147 case PLUS:
15148 plus:
15149 if (is_based_loc (rtl)
15150 && is_a <scalar_int_mode> (mode, &int_mode)
15151 && (GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15152 || XEXP (rtl, 0) == arg_pointer_rtx
15153 || XEXP (rtl, 0) == frame_pointer_rtx))
15154 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15155 INTVAL (XEXP (rtl, 1)),
15156 VAR_INIT_STATUS_INITIALIZED);
15157 else
15159 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15160 VAR_INIT_STATUS_INITIALIZED);
15161 if (mem_loc_result == 0)
15162 break;
15164 if (CONST_INT_P (XEXP (rtl, 1))
15165 && (GET_MODE_SIZE (as_a <scalar_int_mode> (mode))
15166 <= DWARF2_ADDR_SIZE))
15167 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15168 else
15170 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15171 VAR_INIT_STATUS_INITIALIZED);
15172 if (op1 == 0)
15173 return NULL;
15174 add_loc_descr (&mem_loc_result, op1);
15175 add_loc_descr (&mem_loc_result,
15176 new_loc_descr (DW_OP_plus, 0, 0));
15179 break;
15181 /* If a pseudo-reg is optimized away, it is possible for it to
15182 be replaced with a MEM containing a multiply or shift. */
15183 case MINUS:
15184 op = DW_OP_minus;
15185 goto do_binop;
15187 case MULT:
15188 op = DW_OP_mul;
15189 goto do_binop;
15191 case DIV:
15192 if ((!dwarf_strict || dwarf_version >= 5)
15193 && is_a <scalar_int_mode> (mode, &int_mode)
15194 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15196 mem_loc_result = typed_binop (DW_OP_div, rtl,
15197 base_type_for_mode (mode, 0),
15198 int_mode, mem_mode);
15199 break;
15201 op = DW_OP_div;
15202 goto do_binop;
15204 case UMOD:
15205 op = DW_OP_mod;
15206 goto do_binop;
15208 case ASHIFT:
15209 op = DW_OP_shl;
15210 goto do_shift;
15212 case ASHIFTRT:
15213 op = DW_OP_shra;
15214 goto do_shift;
15216 case LSHIFTRT:
15217 op = DW_OP_shr;
15218 goto do_shift;
15220 do_shift:
15221 if (!is_a <scalar_int_mode> (mode, &int_mode))
15222 break;
15223 op0 = mem_loc_descriptor (XEXP (rtl, 0), int_mode, mem_mode,
15224 VAR_INIT_STATUS_INITIALIZED);
15226 rtx rtlop1 = XEXP (rtl, 1);
15227 if (is_a <scalar_int_mode> (GET_MODE (rtlop1), &op1_mode)
15228 && GET_MODE_BITSIZE (op1_mode) < GET_MODE_BITSIZE (int_mode))
15229 rtlop1 = gen_rtx_ZERO_EXTEND (int_mode, rtlop1);
15230 op1 = mem_loc_descriptor (rtlop1, int_mode, mem_mode,
15231 VAR_INIT_STATUS_INITIALIZED);
15234 if (op0 == 0 || op1 == 0)
15235 break;
15237 mem_loc_result = op0;
15238 add_loc_descr (&mem_loc_result, op1);
15239 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15240 break;
15242 case AND:
15243 op = DW_OP_and;
15244 goto do_binop;
15246 case IOR:
15247 op = DW_OP_or;
15248 goto do_binop;
15250 case XOR:
15251 op = DW_OP_xor;
15252 goto do_binop;
15254 do_binop:
15255 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15256 VAR_INIT_STATUS_INITIALIZED);
15257 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15258 VAR_INIT_STATUS_INITIALIZED);
15260 if (op0 == 0 || op1 == 0)
15261 break;
15263 mem_loc_result = op0;
15264 add_loc_descr (&mem_loc_result, op1);
15265 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15266 break;
15268 case MOD:
15269 if ((!dwarf_strict || dwarf_version >= 5)
15270 && is_a <scalar_int_mode> (mode, &int_mode)
15271 && GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15273 mem_loc_result = typed_binop (DW_OP_mod, rtl,
15274 base_type_for_mode (mode, 0),
15275 int_mode, mem_mode);
15276 break;
15279 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15280 VAR_INIT_STATUS_INITIALIZED);
15281 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15282 VAR_INIT_STATUS_INITIALIZED);
15284 if (op0 == 0 || op1 == 0)
15285 break;
15287 mem_loc_result = op0;
15288 add_loc_descr (&mem_loc_result, op1);
15289 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15290 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15291 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15292 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15293 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15294 break;
15296 case UDIV:
15297 if ((!dwarf_strict || dwarf_version >= 5)
15298 && is_a <scalar_int_mode> (mode, &int_mode))
15300 if (GET_MODE_SIZE (int_mode) > DWARF2_ADDR_SIZE)
15302 op = DW_OP_div;
15303 goto do_binop;
15305 mem_loc_result = typed_binop (DW_OP_div, rtl,
15306 base_type_for_mode (int_mode, 1),
15307 int_mode, mem_mode);
15309 break;
15311 case NOT:
15312 op = DW_OP_not;
15313 goto do_unop;
15315 case ABS:
15316 op = DW_OP_abs;
15317 goto do_unop;
15319 case NEG:
15320 op = DW_OP_neg;
15321 goto do_unop;
15323 do_unop:
15324 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15325 VAR_INIT_STATUS_INITIALIZED);
15327 if (op0 == 0)
15328 break;
15330 mem_loc_result = op0;
15331 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15332 break;
15334 case CONST_INT:
15335 if (!is_a <scalar_int_mode> (mode, &int_mode)
15336 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15337 #ifdef POINTERS_EXTEND_UNSIGNED
15338 || (int_mode == Pmode
15339 && mem_mode != VOIDmode
15340 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15341 #endif
15344 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15345 break;
15347 if ((!dwarf_strict || dwarf_version >= 5)
15348 && (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT
15349 || GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_DOUBLE_INT))
15351 dw_die_ref type_die = base_type_for_mode (int_mode, 1);
15352 scalar_int_mode amode;
15353 if (type_die == NULL)
15354 return NULL;
15355 if (INTVAL (rtl) >= 0
15356 && (int_mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT, 0)
15357 .exists (&amode))
15358 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
15359 /* const DW_OP_convert <XXX> vs.
15360 DW_OP_const_type <XXX, 1, const>. */
15361 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
15362 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (int_mode))
15364 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15365 op0 = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15366 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15367 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15368 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
15369 add_loc_descr (&mem_loc_result, op0);
15370 return mem_loc_result;
15372 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0,
15373 INTVAL (rtl));
15374 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15375 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15376 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15377 if (GET_MODE_BITSIZE (int_mode) == HOST_BITS_PER_WIDE_INT)
15378 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15379 else
15381 mem_loc_result->dw_loc_oprnd2.val_class
15382 = dw_val_class_const_double;
15383 mem_loc_result->dw_loc_oprnd2.v.val_double
15384 = double_int::from_shwi (INTVAL (rtl));
15387 break;
15389 case CONST_DOUBLE:
15390 if (!dwarf_strict || dwarf_version >= 5)
15392 dw_die_ref type_die;
15394 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
15395 CONST_DOUBLE rtx could represent either a large integer
15396 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
15397 the value is always a floating point constant.
15399 When it is an integer, a CONST_DOUBLE is used whenever
15400 the constant requires 2 HWIs to be adequately represented.
15401 We output CONST_DOUBLEs as blocks. */
15402 if (mode == VOIDmode
15403 || (GET_MODE (rtl) == VOIDmode
15404 && maybe_ne (GET_MODE_BITSIZE (mode),
15405 HOST_BITS_PER_DOUBLE_INT)))
15406 break;
15407 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15408 if (type_die == NULL)
15409 return NULL;
15410 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15411 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15412 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15413 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15414 #if TARGET_SUPPORTS_WIDE_INT == 0
15415 if (!SCALAR_FLOAT_MODE_P (mode))
15417 mem_loc_result->dw_loc_oprnd2.val_class
15418 = dw_val_class_const_double;
15419 mem_loc_result->dw_loc_oprnd2.v.val_double
15420 = rtx_to_double_int (rtl);
15422 else
15423 #endif
15425 scalar_float_mode float_mode = as_a <scalar_float_mode> (mode);
15426 unsigned int length = GET_MODE_SIZE (float_mode);
15427 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15429 insert_float (rtl, array);
15430 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15431 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15432 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15433 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15436 break;
15438 case CONST_WIDE_INT:
15439 if (!dwarf_strict || dwarf_version >= 5)
15441 dw_die_ref type_die;
15443 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
15444 if (type_die == NULL)
15445 return NULL;
15446 mem_loc_result = new_loc_descr (dwarf_OP (DW_OP_const_type), 0, 0);
15447 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15448 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15449 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15450 mem_loc_result->dw_loc_oprnd2.val_class
15451 = dw_val_class_wide_int;
15452 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
15453 *mem_loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, mode);
15455 break;
15457 case CONST_POLY_INT:
15458 mem_loc_result = int_loc_descriptor (rtx_to_poly_int64 (rtl));
15459 break;
15461 case EQ:
15462 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15463 break;
15465 case GE:
15466 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15467 break;
15469 case GT:
15470 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15471 break;
15473 case LE:
15474 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15475 break;
15477 case LT:
15478 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15479 break;
15481 case NE:
15482 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15483 break;
15485 case GEU:
15486 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15487 break;
15489 case GTU:
15490 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15491 break;
15493 case LEU:
15494 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15495 break;
15497 case LTU:
15498 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15499 break;
15501 case UMIN:
15502 case UMAX:
15503 if (!SCALAR_INT_MODE_P (mode))
15504 break;
15505 /* FALLTHRU */
15506 case SMIN:
15507 case SMAX:
15508 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15509 break;
15511 case ZERO_EXTRACT:
15512 case SIGN_EXTRACT:
15513 if (CONST_INT_P (XEXP (rtl, 1))
15514 && CONST_INT_P (XEXP (rtl, 2))
15515 && is_a <scalar_int_mode> (mode, &int_mode)
15516 && is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &inner_mode)
15517 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
15518 && GET_MODE_SIZE (inner_mode) <= DWARF2_ADDR_SIZE
15519 && ((unsigned) INTVAL (XEXP (rtl, 1))
15520 + (unsigned) INTVAL (XEXP (rtl, 2))
15521 <= GET_MODE_BITSIZE (int_mode)))
15523 int shift, size;
15524 op0 = mem_loc_descriptor (XEXP (rtl, 0), inner_mode,
15525 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15526 if (op0 == 0)
15527 break;
15528 if (GET_CODE (rtl) == SIGN_EXTRACT)
15529 op = DW_OP_shra;
15530 else
15531 op = DW_OP_shr;
15532 mem_loc_result = op0;
15533 size = INTVAL (XEXP (rtl, 1));
15534 shift = INTVAL (XEXP (rtl, 2));
15535 if (BITS_BIG_ENDIAN)
15536 shift = GET_MODE_BITSIZE (inner_mode) - shift - size;
15537 if (shift + size != (int) DWARF2_ADDR_SIZE)
15539 add_loc_descr (&mem_loc_result,
15540 int_loc_descriptor (DWARF2_ADDR_SIZE
15541 - shift - size));
15542 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15544 if (size != (int) DWARF2_ADDR_SIZE)
15546 add_loc_descr (&mem_loc_result,
15547 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15548 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15551 break;
15553 case IF_THEN_ELSE:
15555 dw_loc_descr_ref op2, bra_node, drop_node;
15556 op0 = mem_loc_descriptor (XEXP (rtl, 0),
15557 GET_MODE (XEXP (rtl, 0)) == VOIDmode
15558 ? word_mode : GET_MODE (XEXP (rtl, 0)),
15559 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15560 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15561 VAR_INIT_STATUS_INITIALIZED);
15562 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15563 VAR_INIT_STATUS_INITIALIZED);
15564 if (op0 == NULL || op1 == NULL || op2 == NULL)
15565 break;
15567 mem_loc_result = op1;
15568 add_loc_descr (&mem_loc_result, op2);
15569 add_loc_descr (&mem_loc_result, op0);
15570 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15571 add_loc_descr (&mem_loc_result, bra_node);
15572 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15573 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15574 add_loc_descr (&mem_loc_result, drop_node);
15575 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15576 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15578 break;
15580 case FLOAT_EXTEND:
15581 case FLOAT_TRUNCATE:
15582 case FLOAT:
15583 case UNSIGNED_FLOAT:
15584 case FIX:
15585 case UNSIGNED_FIX:
15586 if (!dwarf_strict || dwarf_version >= 5)
15588 dw_die_ref type_die;
15589 dw_loc_descr_ref cvt;
15591 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15592 mem_mode, VAR_INIT_STATUS_INITIALIZED);
15593 if (op0 == NULL)
15594 break;
15595 if (is_a <scalar_int_mode> (GET_MODE (XEXP (rtl, 0)), &int_mode)
15596 && (GET_CODE (rtl) == FLOAT
15597 || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
15599 type_die = base_type_for_mode (int_mode,
15600 GET_CODE (rtl) == UNSIGNED_FLOAT);
15601 if (type_die == NULL)
15602 break;
15603 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15604 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15605 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15606 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15607 add_loc_descr (&op0, cvt);
15609 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15610 if (type_die == NULL)
15611 break;
15612 cvt = new_loc_descr (dwarf_OP (DW_OP_convert), 0, 0);
15613 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15614 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15615 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15616 add_loc_descr (&op0, cvt);
15617 if (is_a <scalar_int_mode> (mode, &int_mode)
15618 && (GET_CODE (rtl) == FIX
15619 || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
15621 op0 = convert_descriptor_to_mode (int_mode, op0);
15622 if (op0 == NULL)
15623 break;
15625 mem_loc_result = op0;
15627 break;
15629 case CLZ:
15630 case CTZ:
15631 case FFS:
15632 if (is_a <scalar_int_mode> (mode, &int_mode))
15633 mem_loc_result = clz_loc_descriptor (rtl, int_mode, mem_mode);
15634 break;
15636 case POPCOUNT:
15637 case PARITY:
15638 if (is_a <scalar_int_mode> (mode, &int_mode))
15639 mem_loc_result = popcount_loc_descriptor (rtl, int_mode, mem_mode);
15640 break;
15642 case BSWAP:
15643 if (is_a <scalar_int_mode> (mode, &int_mode))
15644 mem_loc_result = bswap_loc_descriptor (rtl, int_mode, mem_mode);
15645 break;
15647 case ROTATE:
15648 case ROTATERT:
15649 if (is_a <scalar_int_mode> (mode, &int_mode))
15650 mem_loc_result = rotate_loc_descriptor (rtl, int_mode, mem_mode);
15651 break;
15653 case COMPARE:
15654 /* In theory, we could implement the above. */
15655 /* DWARF cannot represent the unsigned compare operations
15656 natively. */
15657 case SS_MULT:
15658 case US_MULT:
15659 case SS_DIV:
15660 case US_DIV:
15661 case SS_PLUS:
15662 case US_PLUS:
15663 case SS_MINUS:
15664 case US_MINUS:
15665 case SS_NEG:
15666 case US_NEG:
15667 case SS_ABS:
15668 case SS_ASHIFT:
15669 case US_ASHIFT:
15670 case SS_TRUNCATE:
15671 case US_TRUNCATE:
15672 case UNORDERED:
15673 case ORDERED:
15674 case UNEQ:
15675 case UNGE:
15676 case UNGT:
15677 case UNLE:
15678 case UNLT:
15679 case LTGT:
15680 case FRACT_CONVERT:
15681 case UNSIGNED_FRACT_CONVERT:
15682 case SAT_FRACT:
15683 case UNSIGNED_SAT_FRACT:
15684 case SQRT:
15685 case ASM_OPERANDS:
15686 case VEC_MERGE:
15687 case VEC_SELECT:
15688 case VEC_CONCAT:
15689 case VEC_DUPLICATE:
15690 case VEC_SERIES:
15691 case UNSPEC:
15692 case HIGH:
15693 case FMA:
15694 case STRICT_LOW_PART:
15695 case CONST_VECTOR:
15696 case CONST_FIXED:
15697 case CLRSB:
15698 case CLOBBER:
15699 /* If delegitimize_address couldn't do anything with the UNSPEC, we
15700 can't express it in the debug info. This can happen e.g. with some
15701 TLS UNSPECs. */
15702 break;
15704 case CONST_STRING:
15705 resolve_one_addr (&rtl);
15706 goto symref;
15708 /* RTL sequences inside PARALLEL record a series of DWARF operations for
15709 the expression. An UNSPEC rtx represents a raw DWARF operation,
15710 new_loc_descr is called for it to build the operation directly.
15711 Otherwise mem_loc_descriptor is called recursively. */
15712 case PARALLEL:
15714 int index = 0;
15715 dw_loc_descr_ref exp_result = NULL;
15717 for (; index < XVECLEN (rtl, 0); index++)
15719 rtx elem = XVECEXP (rtl, 0, index);
15720 if (GET_CODE (elem) == UNSPEC)
15722 /* Each DWARF operation UNSPEC contain two operands, if
15723 one operand is not used for the operation, const0_rtx is
15724 passed. */
15725 gcc_assert (XVECLEN (elem, 0) == 2);
15727 HOST_WIDE_INT dw_op = XINT (elem, 1);
15728 HOST_WIDE_INT oprnd1 = INTVAL (XVECEXP (elem, 0, 0));
15729 HOST_WIDE_INT oprnd2 = INTVAL (XVECEXP (elem, 0, 1));
15730 exp_result
15731 = new_loc_descr ((enum dwarf_location_atom) dw_op, oprnd1,
15732 oprnd2);
15734 else
15735 exp_result
15736 = mem_loc_descriptor (elem, mode, mem_mode,
15737 VAR_INIT_STATUS_INITIALIZED);
15739 if (!mem_loc_result)
15740 mem_loc_result = exp_result;
15741 else
15742 add_loc_descr (&mem_loc_result, exp_result);
15745 break;
15748 default:
15749 if (flag_checking)
15751 print_rtl (stderr, rtl);
15752 gcc_unreachable ();
15754 break;
15757 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15758 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15760 return mem_loc_result;
15763 /* Return a descriptor that describes the concatenation of two locations.
15764 This is typically a complex variable. */
15766 static dw_loc_descr_ref
15767 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15769 /* At present we only track constant-sized pieces. */
15770 unsigned int size0, size1;
15771 if (!GET_MODE_SIZE (GET_MODE (x0)).is_constant (&size0)
15772 || !GET_MODE_SIZE (GET_MODE (x1)).is_constant (&size1))
15773 return 0;
15775 dw_loc_descr_ref cc_loc_result = NULL;
15776 dw_loc_descr_ref x0_ref
15777 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15778 dw_loc_descr_ref x1_ref
15779 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15781 if (x0_ref == 0 || x1_ref == 0)
15782 return 0;
15784 cc_loc_result = x0_ref;
15785 add_loc_descr_op_piece (&cc_loc_result, size0);
15787 add_loc_descr (&cc_loc_result, x1_ref);
15788 add_loc_descr_op_piece (&cc_loc_result, size1);
15790 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15791 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15793 return cc_loc_result;
15796 /* Return a descriptor that describes the concatenation of N
15797 locations. */
15799 static dw_loc_descr_ref
15800 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15802 unsigned int i;
15803 dw_loc_descr_ref cc_loc_result = NULL;
15804 unsigned int n = XVECLEN (concatn, 0);
15805 unsigned int size;
15807 for (i = 0; i < n; ++i)
15809 dw_loc_descr_ref ref;
15810 rtx x = XVECEXP (concatn, 0, i);
15812 /* At present we only track constant-sized pieces. */
15813 if (!GET_MODE_SIZE (GET_MODE (x)).is_constant (&size))
15814 return NULL;
15816 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15817 if (ref == NULL)
15818 return NULL;
15820 add_loc_descr (&cc_loc_result, ref);
15821 add_loc_descr_op_piece (&cc_loc_result, size);
15824 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15825 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15827 return cc_loc_result;
15830 /* Helper function for loc_descriptor. Return DW_OP_implicit_pointer
15831 for DEBUG_IMPLICIT_PTR RTL. */
15833 static dw_loc_descr_ref
15834 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15836 dw_loc_descr_ref ret;
15837 dw_die_ref ref;
15839 if (dwarf_strict && dwarf_version < 5)
15840 return NULL;
15841 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15842 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15843 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15844 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15845 ret = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
15846 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15847 if (ref)
15849 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15850 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15851 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15853 else
15855 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15856 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15858 return ret;
15861 /* Output a proper Dwarf location descriptor for a variable or parameter
15862 which is either allocated in a register or in a memory location. For a
15863 register, we just generate an OP_REG and the register number. For a
15864 memory location we provide a Dwarf postfix expression describing how to
15865 generate the (dynamic) address of the object onto the address stack.
15867 MODE is mode of the decl if this loc_descriptor is going to be used in
15868 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15869 allowed, VOIDmode otherwise.
15871 If we don't know how to describe it, return 0. */
15873 static dw_loc_descr_ref
15874 loc_descriptor (rtx rtl, machine_mode mode,
15875 enum var_init_status initialized)
15877 dw_loc_descr_ref loc_result = NULL;
15878 scalar_int_mode int_mode;
15880 switch (GET_CODE (rtl))
15882 case SUBREG:
15883 /* The case of a subreg may arise when we have a local (register)
15884 variable or a formal (register) parameter which doesn't quite fill
15885 up an entire register. For now, just assume that it is
15886 legitimate to make the Dwarf info refer to the whole register which
15887 contains the given subreg. */
15888 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15889 loc_result = loc_descriptor (SUBREG_REG (rtl),
15890 GET_MODE (SUBREG_REG (rtl)), initialized);
15891 else
15892 goto do_default;
15893 break;
15895 case REG:
15896 loc_result = reg_loc_descriptor (rtl, initialized);
15897 break;
15899 case MEM:
15900 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15901 GET_MODE (rtl), initialized);
15902 if (loc_result == NULL)
15903 loc_result = tls_mem_loc_descriptor (rtl);
15904 if (loc_result == NULL)
15906 rtx new_rtl = avoid_constant_pool_reference (rtl);
15907 if (new_rtl != rtl)
15908 loc_result = loc_descriptor (new_rtl, mode, initialized);
15910 break;
15912 case CONCAT:
15913 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15914 initialized);
15915 break;
15917 case CONCATN:
15918 loc_result = concatn_loc_descriptor (rtl, initialized);
15919 break;
15921 case VAR_LOCATION:
15922 /* Single part. */
15923 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15925 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15926 if (GET_CODE (loc) == EXPR_LIST)
15927 loc = XEXP (loc, 0);
15928 loc_result = loc_descriptor (loc, mode, initialized);
15929 break;
15932 rtl = XEXP (rtl, 1);
15933 /* FALLTHRU */
15935 case PARALLEL:
15937 rtvec par_elems = XVEC (rtl, 0);
15938 int num_elem = GET_NUM_ELEM (par_elems);
15939 machine_mode mode;
15940 int i, size;
15942 /* Create the first one, so we have something to add to. */
15943 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15944 VOIDmode, initialized);
15945 if (loc_result == NULL)
15946 return NULL;
15947 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15948 /* At present we only track constant-sized pieces. */
15949 if (!GET_MODE_SIZE (mode).is_constant (&size))
15950 return NULL;
15951 add_loc_descr_op_piece (&loc_result, size);
15952 for (i = 1; i < num_elem; i++)
15954 dw_loc_descr_ref temp;
15956 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15957 VOIDmode, initialized);
15958 if (temp == NULL)
15959 return NULL;
15960 add_loc_descr (&loc_result, temp);
15961 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15962 /* At present we only track constant-sized pieces. */
15963 if (!GET_MODE_SIZE (mode).is_constant (&size))
15964 return NULL;
15965 add_loc_descr_op_piece (&loc_result, size);
15968 break;
15970 case CONST_INT:
15971 if (mode != VOIDmode && mode != BLKmode)
15973 int_mode = as_a <scalar_int_mode> (mode);
15974 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (int_mode),
15975 INTVAL (rtl));
15977 break;
15979 case CONST_DOUBLE:
15980 if (mode == VOIDmode)
15981 mode = GET_MODE (rtl);
15983 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15985 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15987 /* Note that a CONST_DOUBLE rtx could represent either an integer
15988 or a floating-point constant. A CONST_DOUBLE is used whenever
15989 the constant requires more than one word in order to be
15990 adequately represented. We output CONST_DOUBLEs as blocks. */
15991 scalar_mode smode = as_a <scalar_mode> (mode);
15992 loc_result = new_loc_descr (DW_OP_implicit_value,
15993 GET_MODE_SIZE (smode), 0);
15994 #if TARGET_SUPPORTS_WIDE_INT == 0
15995 if (!SCALAR_FLOAT_MODE_P (smode))
15997 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15998 loc_result->dw_loc_oprnd2.v.val_double
15999 = rtx_to_double_int (rtl);
16001 else
16002 #endif
16004 unsigned int length = GET_MODE_SIZE (smode);
16005 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
16007 insert_float (rtl, array);
16008 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16009 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
16010 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
16011 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16014 break;
16016 case CONST_WIDE_INT:
16017 if (mode == VOIDmode)
16018 mode = GET_MODE (rtl);
16020 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16022 int_mode = as_a <scalar_int_mode> (mode);
16023 loc_result = new_loc_descr (DW_OP_implicit_value,
16024 GET_MODE_SIZE (int_mode), 0);
16025 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
16026 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
16027 *loc_result->dw_loc_oprnd2.v.val_wide = rtx_mode_t (rtl, int_mode);
16029 break;
16031 case CONST_VECTOR:
16032 if (mode == VOIDmode)
16033 mode = GET_MODE (rtl);
16035 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
16037 unsigned int length;
16038 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
16039 return NULL;
16041 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
16042 unsigned char *array
16043 = ggc_vec_alloc<unsigned char> (length * elt_size);
16044 unsigned int i;
16045 unsigned char *p;
16046 machine_mode imode = GET_MODE_INNER (mode);
16048 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
16049 switch (GET_MODE_CLASS (mode))
16051 case MODE_VECTOR_INT:
16052 for (i = 0, p = array; i < length; i++, p += elt_size)
16054 rtx elt = CONST_VECTOR_ELT (rtl, i);
16055 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
16057 break;
16059 case MODE_VECTOR_FLOAT:
16060 for (i = 0, p = array; i < length; i++, p += elt_size)
16062 rtx elt = CONST_VECTOR_ELT (rtl, i);
16063 insert_float (elt, p);
16065 break;
16067 default:
16068 gcc_unreachable ();
16071 loc_result = new_loc_descr (DW_OP_implicit_value,
16072 length * elt_size, 0);
16073 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
16074 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
16075 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
16076 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
16078 break;
16080 case CONST:
16081 if (mode == VOIDmode
16082 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
16083 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
16084 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
16086 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
16087 break;
16089 /* FALLTHROUGH */
16090 case SYMBOL_REF:
16091 if (!const_ok_for_output (rtl))
16092 break;
16093 /* FALLTHROUGH */
16094 case LABEL_REF:
16095 if (is_a <scalar_int_mode> (mode, &int_mode)
16096 && GET_MODE_SIZE (int_mode) == DWARF2_ADDR_SIZE
16097 && (dwarf_version >= 4 || !dwarf_strict))
16099 loc_result = new_addr_loc_descr (rtl, dtprel_false);
16100 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16101 vec_safe_push (used_rtx_array, rtl);
16103 break;
16105 case DEBUG_IMPLICIT_PTR:
16106 loc_result = implicit_ptr_descriptor (rtl, 0);
16107 break;
16109 case PLUS:
16110 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
16111 && CONST_INT_P (XEXP (rtl, 1)))
16113 loc_result
16114 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
16115 break;
16117 /* FALLTHRU */
16118 do_default:
16119 default:
16120 if ((is_a <scalar_int_mode> (mode, &int_mode)
16121 && GET_MODE (rtl) == int_mode
16122 && GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE
16123 && dwarf_version >= 4)
16124 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
16126 /* Value expression. */
16127 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
16128 if (loc_result)
16129 add_loc_descr (&loc_result,
16130 new_loc_descr (DW_OP_stack_value, 0, 0));
16132 break;
16135 return loc_result;
16138 /* We need to figure out what section we should use as the base for the
16139 address ranges where a given location is valid.
16140 1. If this particular DECL has a section associated with it, use that.
16141 2. If this function has a section associated with it, use that.
16142 3. Otherwise, use the text section.
16143 XXX: If you split a variable across multiple sections, we won't notice. */
16145 static const char *
16146 secname_for_decl (const_tree decl)
16148 const char *secname;
16150 if (VAR_OR_FUNCTION_DECL_P (decl)
16151 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
16152 && DECL_SECTION_NAME (decl))
16153 secname = DECL_SECTION_NAME (decl);
16154 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
16155 secname = DECL_SECTION_NAME (current_function_decl);
16156 else if (cfun && in_cold_section_p)
16157 secname = crtl->subsections.cold_section_label;
16158 else
16159 secname = text_section_label;
16161 return secname;
16164 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
16166 static bool
16167 decl_by_reference_p (tree decl)
16169 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
16170 || VAR_P (decl))
16171 && DECL_BY_REFERENCE (decl));
16174 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16175 for VARLOC. */
16177 static dw_loc_descr_ref
16178 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
16179 enum var_init_status initialized)
16181 int have_address = 0;
16182 dw_loc_descr_ref descr;
16183 machine_mode mode;
16185 if (want_address != 2)
16187 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
16188 /* Single part. */
16189 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16191 varloc = PAT_VAR_LOCATION_LOC (varloc);
16192 if (GET_CODE (varloc) == EXPR_LIST)
16193 varloc = XEXP (varloc, 0);
16194 mode = GET_MODE (varloc);
16195 if (MEM_P (varloc))
16197 rtx addr = XEXP (varloc, 0);
16198 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
16199 mode, initialized);
16200 if (descr)
16201 have_address = 1;
16202 else
16204 rtx x = avoid_constant_pool_reference (varloc);
16205 if (x != varloc)
16206 descr = mem_loc_descriptor (x, mode, VOIDmode,
16207 initialized);
16210 else
16211 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
16213 else
16214 return 0;
16216 else
16218 if (GET_CODE (varloc) == VAR_LOCATION)
16219 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
16220 else
16221 mode = DECL_MODE (loc);
16222 descr = loc_descriptor (varloc, mode, initialized);
16223 have_address = 1;
16226 if (!descr)
16227 return 0;
16229 if (want_address == 2 && !have_address
16230 && (dwarf_version >= 4 || !dwarf_strict))
16232 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16234 expansion_failed (loc, NULL_RTX,
16235 "DWARF address size mismatch");
16236 return 0;
16238 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
16239 have_address = 1;
16241 /* Show if we can't fill the request for an address. */
16242 if (want_address && !have_address)
16244 expansion_failed (loc, NULL_RTX,
16245 "Want address and only have value");
16246 return 0;
16249 /* If we've got an address and don't want one, dereference. */
16250 if (!want_address && have_address)
16252 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16253 enum dwarf_location_atom op;
16255 if (size > DWARF2_ADDR_SIZE || size == -1)
16257 expansion_failed (loc, NULL_RTX,
16258 "DWARF address size mismatch");
16259 return 0;
16261 else if (size == DWARF2_ADDR_SIZE)
16262 op = DW_OP_deref;
16263 else
16264 op = DW_OP_deref_size;
16266 add_loc_descr (&descr, new_loc_descr (op, size, 0));
16269 return descr;
16272 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16273 if it is not possible. */
16275 static dw_loc_descr_ref
16276 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16278 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16279 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16280 else if (dwarf_version >= 3 || !dwarf_strict)
16281 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16282 else
16283 return NULL;
16286 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
16287 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
16289 static dw_loc_descr_ref
16290 dw_sra_loc_expr (tree decl, rtx loc)
16292 rtx p;
16293 unsigned HOST_WIDE_INT padsize = 0;
16294 dw_loc_descr_ref descr, *descr_tail;
16295 unsigned HOST_WIDE_INT decl_size;
16296 rtx varloc;
16297 enum var_init_status initialized;
16299 if (DECL_SIZE (decl) == NULL
16300 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
16301 return NULL;
16303 decl_size = tree_to_uhwi (DECL_SIZE (decl));
16304 descr = NULL;
16305 descr_tail = &descr;
16307 for (p = loc; p; p = XEXP (p, 1))
16309 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
16310 rtx loc_note = *decl_piece_varloc_ptr (p);
16311 dw_loc_descr_ref cur_descr;
16312 dw_loc_descr_ref *tail, last = NULL;
16313 unsigned HOST_WIDE_INT opsize = 0;
16315 if (loc_note == NULL_RTX
16316 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16318 padsize += bitsize;
16319 continue;
16321 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16322 varloc = NOTE_VAR_LOCATION (loc_note);
16323 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16324 if (cur_descr == NULL)
16326 padsize += bitsize;
16327 continue;
16330 /* Check that cur_descr either doesn't use
16331 DW_OP_*piece operations, or their sum is equal
16332 to bitsize. Otherwise we can't embed it. */
16333 for (tail = &cur_descr; *tail != NULL;
16334 tail = &(*tail)->dw_loc_next)
16335 if ((*tail)->dw_loc_opc == DW_OP_piece)
16337 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16338 * BITS_PER_UNIT;
16339 last = *tail;
16341 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16343 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16344 last = *tail;
16347 if (last != NULL && opsize != bitsize)
16349 padsize += bitsize;
16350 /* Discard the current piece of the descriptor and release any
16351 addr_table entries it uses. */
16352 remove_loc_list_addr_table_entries (cur_descr);
16353 continue;
16356 /* If there is a hole, add DW_OP_*piece after empty DWARF
16357 expression, which means that those bits are optimized out. */
16358 if (padsize)
16360 if (padsize > decl_size)
16362 remove_loc_list_addr_table_entries (cur_descr);
16363 goto discard_descr;
16365 decl_size -= padsize;
16366 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16367 if (*descr_tail == NULL)
16369 remove_loc_list_addr_table_entries (cur_descr);
16370 goto discard_descr;
16372 descr_tail = &(*descr_tail)->dw_loc_next;
16373 padsize = 0;
16375 *descr_tail = cur_descr;
16376 descr_tail = tail;
16377 if (bitsize > decl_size)
16378 goto discard_descr;
16379 decl_size -= bitsize;
16380 if (last == NULL)
16382 HOST_WIDE_INT offset = 0;
16383 if (GET_CODE (varloc) == VAR_LOCATION
16384 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16386 varloc = PAT_VAR_LOCATION_LOC (varloc);
16387 if (GET_CODE (varloc) == EXPR_LIST)
16388 varloc = XEXP (varloc, 0);
16392 if (GET_CODE (varloc) == CONST
16393 || GET_CODE (varloc) == SIGN_EXTEND
16394 || GET_CODE (varloc) == ZERO_EXTEND)
16395 varloc = XEXP (varloc, 0);
16396 else if (GET_CODE (varloc) == SUBREG)
16397 varloc = SUBREG_REG (varloc);
16398 else
16399 break;
16401 while (1);
16402 /* DW_OP_bit_size offset should be zero for register
16403 or implicit location descriptions and empty location
16404 descriptions, but for memory addresses needs big endian
16405 adjustment. */
16406 if (MEM_P (varloc))
16408 unsigned HOST_WIDE_INT memsize;
16409 if (!poly_uint64 (MEM_SIZE (varloc)).is_constant (&memsize))
16410 goto discard_descr;
16411 memsize *= BITS_PER_UNIT;
16412 if (memsize != bitsize)
16414 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16415 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16416 goto discard_descr;
16417 if (memsize < bitsize)
16418 goto discard_descr;
16419 if (BITS_BIG_ENDIAN)
16420 offset = memsize - bitsize;
16424 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16425 if (*descr_tail == NULL)
16426 goto discard_descr;
16427 descr_tail = &(*descr_tail)->dw_loc_next;
16431 /* If there were any non-empty expressions, add padding till the end of
16432 the decl. */
16433 if (descr != NULL && decl_size != 0)
16435 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16436 if (*descr_tail == NULL)
16437 goto discard_descr;
16439 return descr;
16441 discard_descr:
16442 /* Discard the descriptor and release any addr_table entries it uses. */
16443 remove_loc_list_addr_table_entries (descr);
16444 return NULL;
16447 /* Return the dwarf representation of the location list LOC_LIST of
16448 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
16449 function. */
16451 static dw_loc_list_ref
16452 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16454 const char *endname, *secname;
16455 rtx varloc;
16456 enum var_init_status initialized;
16457 struct var_loc_node *node;
16458 dw_loc_descr_ref descr;
16459 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16460 dw_loc_list_ref list = NULL;
16461 dw_loc_list_ref *listp = &list;
16463 /* Now that we know what section we are using for a base,
16464 actually construct the list of locations.
16465 The first location information is what is passed to the
16466 function that creates the location list, and the remaining
16467 locations just get added on to that list.
16468 Note that we only know the start address for a location
16469 (IE location changes), so to build the range, we use
16470 the range [current location start, next location start].
16471 This means we have to special case the last node, and generate
16472 a range of [last location start, end of function label]. */
16474 if (cfun && crtl->has_bb_partition)
16476 bool save_in_cold_section_p = in_cold_section_p;
16477 in_cold_section_p = first_function_block_is_cold;
16478 if (loc_list->last_before_switch == NULL)
16479 in_cold_section_p = !in_cold_section_p;
16480 secname = secname_for_decl (decl);
16481 in_cold_section_p = save_in_cold_section_p;
16483 else
16484 secname = secname_for_decl (decl);
16486 for (node = loc_list->first; node; node = node->next)
16488 bool range_across_switch = false;
16489 if (GET_CODE (node->loc) == EXPR_LIST
16490 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16492 if (GET_CODE (node->loc) == EXPR_LIST)
16494 descr = NULL;
16495 /* This requires DW_OP_{,bit_}piece, which is not usable
16496 inside DWARF expressions. */
16497 if (want_address == 2)
16498 descr = dw_sra_loc_expr (decl, node->loc);
16500 else
16502 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16503 varloc = NOTE_VAR_LOCATION (node->loc);
16504 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16506 if (descr)
16508 /* If section switch happens in between node->label
16509 and node->next->label (or end of function) and
16510 we can't emit it as a single entry list,
16511 emit two ranges, first one ending at the end
16512 of first partition and second one starting at the
16513 beginning of second partition. */
16514 if (node == loc_list->last_before_switch
16515 && (node != loc_list->first || loc_list->first->next)
16516 && current_function_decl)
16518 endname = cfun->fde->dw_fde_end;
16519 range_across_switch = true;
16521 /* The variable has a location between NODE->LABEL and
16522 NODE->NEXT->LABEL. */
16523 else if (node->next)
16524 endname = node->next->label;
16525 /* If the variable has a location at the last label
16526 it keeps its location until the end of function. */
16527 else if (!current_function_decl)
16528 endname = text_end_label;
16529 else
16531 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16532 current_function_funcdef_no);
16533 endname = ggc_strdup (label_id);
16536 *listp = new_loc_list (descr, node->label, endname, secname);
16537 if (TREE_CODE (decl) == PARM_DECL
16538 && node == loc_list->first
16539 && NOTE_P (node->loc)
16540 && strcmp (node->label, endname) == 0)
16541 (*listp)->force = true;
16542 listp = &(*listp)->dw_loc_next;
16546 if (cfun
16547 && crtl->has_bb_partition
16548 && node == loc_list->last_before_switch)
16550 bool save_in_cold_section_p = in_cold_section_p;
16551 in_cold_section_p = !first_function_block_is_cold;
16552 secname = secname_for_decl (decl);
16553 in_cold_section_p = save_in_cold_section_p;
16556 if (range_across_switch)
16558 if (GET_CODE (node->loc) == EXPR_LIST)
16559 descr = dw_sra_loc_expr (decl, node->loc);
16560 else
16562 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16563 varloc = NOTE_VAR_LOCATION (node->loc);
16564 descr = dw_loc_list_1 (decl, varloc, want_address,
16565 initialized);
16567 gcc_assert (descr);
16568 /* The variable has a location between NODE->LABEL and
16569 NODE->NEXT->LABEL. */
16570 if (node->next)
16571 endname = node->next->label;
16572 else
16573 endname = cfun->fde->dw_fde_second_end;
16574 *listp = new_loc_list (descr, cfun->fde->dw_fde_second_begin,
16575 endname, secname);
16576 listp = &(*listp)->dw_loc_next;
16580 /* Try to avoid the overhead of a location list emitting a location
16581 expression instead, but only if we didn't have more than one
16582 location entry in the first place. If some entries were not
16583 representable, we don't want to pretend a single entry that was
16584 applies to the entire scope in which the variable is
16585 available. */
16586 if (list && loc_list->first->next)
16587 gen_llsym (list);
16589 return list;
16592 /* Return if the loc_list has only single element and thus can be represented
16593 as location description. */
16595 static bool
16596 single_element_loc_list_p (dw_loc_list_ref list)
16598 gcc_assert (!list->dw_loc_next || list->ll_symbol);
16599 return !list->ll_symbol;
16602 /* Duplicate a single element of location list. */
16604 static inline dw_loc_descr_ref
16605 copy_loc_descr (dw_loc_descr_ref ref)
16607 dw_loc_descr_ref copy = ggc_alloc<dw_loc_descr_node> ();
16608 memcpy (copy, ref, sizeof (dw_loc_descr_node));
16609 return copy;
16612 /* To each location in list LIST append loc descr REF. */
16614 static void
16615 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16617 dw_loc_descr_ref copy;
16618 add_loc_descr (&list->expr, ref);
16619 list = list->dw_loc_next;
16620 while (list)
16622 copy = copy_loc_descr (ref);
16623 add_loc_descr (&list->expr, copy);
16624 while (copy->dw_loc_next)
16625 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16626 list = list->dw_loc_next;
16630 /* To each location in list LIST prepend loc descr REF. */
16632 static void
16633 prepend_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16635 dw_loc_descr_ref copy;
16636 dw_loc_descr_ref ref_end = list->expr;
16637 add_loc_descr (&ref, list->expr);
16638 list->expr = ref;
16639 list = list->dw_loc_next;
16640 while (list)
16642 dw_loc_descr_ref end = list->expr;
16643 list->expr = copy = copy_loc_descr (ref);
16644 while (copy->dw_loc_next != ref_end)
16645 copy = copy->dw_loc_next = copy_loc_descr (copy->dw_loc_next);
16646 copy->dw_loc_next = end;
16647 list = list->dw_loc_next;
16651 /* Given two lists RET and LIST
16652 produce location list that is result of adding expression in LIST
16653 to expression in RET on each position in program.
16654 Might be destructive on both RET and LIST.
16656 TODO: We handle only simple cases of RET or LIST having at most one
16657 element. General case would involve sorting the lists in program order
16658 and merging them that will need some additional work.
16659 Adding that will improve quality of debug info especially for SRA-ed
16660 structures. */
16662 static void
16663 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16665 if (!list)
16666 return;
16667 if (!*ret)
16669 *ret = list;
16670 return;
16672 if (!list->dw_loc_next)
16674 add_loc_descr_to_each (*ret, list->expr);
16675 return;
16677 if (!(*ret)->dw_loc_next)
16679 prepend_loc_descr_to_each (list, (*ret)->expr);
16680 *ret = list;
16681 return;
16683 expansion_failed (NULL_TREE, NULL_RTX,
16684 "Don't know how to merge two non-trivial"
16685 " location lists.\n");
16686 *ret = NULL;
16687 return;
16690 /* LOC is constant expression. Try a luck, look it up in constant
16691 pool and return its loc_descr of its address. */
16693 static dw_loc_descr_ref
16694 cst_pool_loc_descr (tree loc)
16696 /* Get an RTL for this, if something has been emitted. */
16697 rtx rtl = lookup_constant_def (loc);
16699 if (!rtl || !MEM_P (rtl))
16701 gcc_assert (!rtl);
16702 return 0;
16704 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16706 /* TODO: We might get more coverage if we was actually delaying expansion
16707 of all expressions till end of compilation when constant pools are fully
16708 populated. */
16709 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16711 expansion_failed (loc, NULL_RTX,
16712 "CST value in contant pool but not marked.");
16713 return 0;
16715 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16716 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16719 /* Return dw_loc_list representing address of addr_expr LOC
16720 by looking for inner INDIRECT_REF expression and turning
16721 it into simple arithmetics.
16723 See loc_list_from_tree for the meaning of CONTEXT. */
16725 static dw_loc_list_ref
16726 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
16727 loc_descr_context *context)
16729 tree obj, offset;
16730 poly_int64 bitsize, bitpos, bytepos;
16731 machine_mode mode;
16732 int unsignedp, reversep, volatilep = 0;
16733 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16735 obj = get_inner_reference (TREE_OPERAND (loc, 0),
16736 &bitsize, &bitpos, &offset, &mode,
16737 &unsignedp, &reversep, &volatilep);
16738 STRIP_NOPS (obj);
16739 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
16741 expansion_failed (loc, NULL_RTX, "bitfield access");
16742 return 0;
16744 if (!INDIRECT_REF_P (obj))
16746 expansion_failed (obj,
16747 NULL_RTX, "no indirect ref in inner refrence");
16748 return 0;
16750 if (!offset && known_eq (bitpos, 0))
16751 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
16752 context);
16753 else if (toplev
16754 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16755 && (dwarf_version >= 4 || !dwarf_strict))
16757 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
16758 if (!list_ret)
16759 return 0;
16760 if (offset)
16762 /* Variable offset. */
16763 list_ret1 = loc_list_from_tree (offset, 0, context);
16764 if (list_ret1 == 0)
16765 return 0;
16766 add_loc_list (&list_ret, list_ret1);
16767 if (!list_ret)
16768 return 0;
16769 add_loc_descr_to_each (list_ret,
16770 new_loc_descr (DW_OP_plus, 0, 0));
16772 HOST_WIDE_INT value;
16773 if (bytepos.is_constant (&value) && value > 0)
16774 add_loc_descr_to_each (list_ret,
16775 new_loc_descr (DW_OP_plus_uconst, value, 0));
16776 else if (maybe_ne (bytepos, 0))
16777 loc_list_plus_const (list_ret, bytepos);
16778 add_loc_descr_to_each (list_ret,
16779 new_loc_descr (DW_OP_stack_value, 0, 0));
16781 return list_ret;
16784 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
16785 all operations from LOC are nops, move to the last one. Insert in NOPS all
16786 operations that are skipped. */
16788 static void
16789 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
16790 hash_set<dw_loc_descr_ref> &nops)
16792 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
16794 nops.add (loc);
16795 loc = loc->dw_loc_next;
16799 /* Helper for loc_descr_without_nops: free the location description operation
16800 P. */
16802 bool
16803 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
16805 ggc_free (loc);
16806 return true;
16809 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
16810 finishes LOC. */
16812 static void
16813 loc_descr_without_nops (dw_loc_descr_ref &loc)
16815 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
16816 return;
16818 /* Set of all DW_OP_nop operations we remove. */
16819 hash_set<dw_loc_descr_ref> nops;
16821 /* First, strip all prefix NOP operations in order to keep the head of the
16822 operations list. */
16823 loc_descr_to_next_no_nop (loc, nops);
16825 for (dw_loc_descr_ref cur = loc; cur != NULL;)
16827 /* For control flow operations: strip "prefix" nops in destination
16828 labels. */
16829 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
16830 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
16831 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
16832 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
16834 /* Do the same for the operations that follow, then move to the next
16835 iteration. */
16836 if (cur->dw_loc_next != NULL)
16837 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
16838 cur = cur->dw_loc_next;
16841 nops.traverse<void *, free_loc_descr> (NULL);
16845 struct dwarf_procedure_info;
16847 /* Helper structure for location descriptions generation. */
16848 struct loc_descr_context
16850 /* The type that is implicitly referenced by DW_OP_push_object_address, or
16851 NULL_TREE if DW_OP_push_object_address in invalid for this location
16852 description. This is used when processing PLACEHOLDER_EXPR nodes. */
16853 tree context_type;
16854 /* The ..._DECL node that should be translated as a
16855 DW_OP_push_object_address operation. */
16856 tree base_decl;
16857 /* Information about the DWARF procedure we are currently generating. NULL if
16858 we are not generating a DWARF procedure. */
16859 struct dwarf_procedure_info *dpi;
16860 /* True if integral PLACEHOLDER_EXPR stands for the first argument passed
16861 by consumer. Used for DW_TAG_generic_subrange attributes. */
16862 bool placeholder_arg;
16863 /* True if PLACEHOLDER_EXPR has been seen. */
16864 bool placeholder_seen;
16867 /* DWARF procedures generation
16869 DWARF expressions (aka. location descriptions) are used to encode variable
16870 things such as sizes or offsets. Such computations can have redundant parts
16871 that can be factorized in order to reduce the size of the output debug
16872 information. This is the whole point of DWARF procedures.
16874 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
16875 already factorized into functions ("size functions") in order to handle very
16876 big and complex types. Such functions are quite simple: they have integral
16877 arguments, they return an integral result and their body contains only a
16878 return statement with arithmetic expressions. This is the only kind of
16879 function we are interested in translating into DWARF procedures, here.
16881 DWARF expressions and DWARF procedure are executed using a stack, so we have
16882 to define some calling convention for them to interact. Let's say that:
16884 - Before calling a DWARF procedure, DWARF expressions must push on the stack
16885 all arguments in reverse order (right-to-left) so that when the DWARF
16886 procedure execution starts, the first argument is the top of the stack.
16888 - Then, when returning, the DWARF procedure must have consumed all arguments
16889 on the stack, must have pushed the result and touched nothing else.
16891 - Each integral argument and the result are integral types can be hold in a
16892 single stack slot.
16894 - We call "frame offset" the number of stack slots that are "under DWARF
16895 procedure control": it includes the arguments slots, the temporaries and
16896 the result slot. Thus, it is equal to the number of arguments when the
16897 procedure execution starts and must be equal to one (the result) when it
16898 returns. */
16900 /* Helper structure used when generating operations for a DWARF procedure. */
16901 struct dwarf_procedure_info
16903 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
16904 currently translated. */
16905 tree fndecl;
16906 /* The number of arguments FNDECL takes. */
16907 unsigned args_count;
16910 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
16911 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
16912 equate it to this DIE. */
16914 static dw_die_ref
16915 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
16916 dw_die_ref parent_die)
16918 dw_die_ref dwarf_proc_die;
16920 if ((dwarf_version < 3 && dwarf_strict)
16921 || location == NULL)
16922 return NULL;
16924 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
16925 if (fndecl)
16926 equate_decl_number_to_die (fndecl, dwarf_proc_die);
16927 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
16928 return dwarf_proc_die;
16931 /* Return whether TYPE is a supported type as a DWARF procedure argument
16932 type or return type (we handle only scalar types and pointer types that
16933 aren't wider than the DWARF expression evaluation stack. */
16935 static bool
16936 is_handled_procedure_type (tree type)
16938 return ((INTEGRAL_TYPE_P (type)
16939 || TREE_CODE (type) == OFFSET_TYPE
16940 || TREE_CODE (type) == POINTER_TYPE)
16941 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
16944 /* Helper for resolve_args_picking: do the same but stop when coming across
16945 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
16946 offset *before* evaluating the corresponding operation. */
16948 static bool
16949 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
16950 struct dwarf_procedure_info *dpi,
16951 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
16953 /* The "frame_offset" identifier is already used to name a macro... */
16954 unsigned frame_offset_ = initial_frame_offset;
16955 dw_loc_descr_ref l;
16957 for (l = loc; l != NULL;)
16959 bool existed;
16960 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
16962 /* If we already met this node, there is nothing to compute anymore. */
16963 if (existed)
16965 /* Make sure that the stack size is consistent wherever the execution
16966 flow comes from. */
16967 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
16968 break;
16970 l_frame_offset = frame_offset_;
16972 /* If needed, relocate the picking offset with respect to the frame
16973 offset. */
16974 if (l->frame_offset_rel)
16976 unsigned HOST_WIDE_INT off;
16977 switch (l->dw_loc_opc)
16979 case DW_OP_pick:
16980 off = l->dw_loc_oprnd1.v.val_unsigned;
16981 break;
16982 case DW_OP_dup:
16983 off = 0;
16984 break;
16985 case DW_OP_over:
16986 off = 1;
16987 break;
16988 default:
16989 gcc_unreachable ();
16991 /* frame_offset_ is the size of the current stack frame, including
16992 incoming arguments. Besides, the arguments are pushed
16993 right-to-left. Thus, in order to access the Nth argument from
16994 this operation node, the picking has to skip temporaries *plus*
16995 one stack slot per argument (0 for the first one, 1 for the second
16996 one, etc.).
16998 The targetted argument number (N) is already set as the operand,
16999 and the number of temporaries can be computed with:
17000 frame_offsets_ - dpi->args_count */
17001 off += frame_offset_ - dpi->args_count;
17003 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
17004 if (off > 255)
17005 return false;
17007 if (off == 0)
17009 l->dw_loc_opc = DW_OP_dup;
17010 l->dw_loc_oprnd1.v.val_unsigned = 0;
17012 else if (off == 1)
17014 l->dw_loc_opc = DW_OP_over;
17015 l->dw_loc_oprnd1.v.val_unsigned = 0;
17017 else
17019 l->dw_loc_opc = DW_OP_pick;
17020 l->dw_loc_oprnd1.v.val_unsigned = off;
17024 /* Update frame_offset according to the effect the current operation has
17025 on the stack. */
17026 switch (l->dw_loc_opc)
17028 case DW_OP_deref:
17029 case DW_OP_swap:
17030 case DW_OP_rot:
17031 case DW_OP_abs:
17032 case DW_OP_neg:
17033 case DW_OP_not:
17034 case DW_OP_plus_uconst:
17035 case DW_OP_skip:
17036 case DW_OP_reg0:
17037 case DW_OP_reg1:
17038 case DW_OP_reg2:
17039 case DW_OP_reg3:
17040 case DW_OP_reg4:
17041 case DW_OP_reg5:
17042 case DW_OP_reg6:
17043 case DW_OP_reg7:
17044 case DW_OP_reg8:
17045 case DW_OP_reg9:
17046 case DW_OP_reg10:
17047 case DW_OP_reg11:
17048 case DW_OP_reg12:
17049 case DW_OP_reg13:
17050 case DW_OP_reg14:
17051 case DW_OP_reg15:
17052 case DW_OP_reg16:
17053 case DW_OP_reg17:
17054 case DW_OP_reg18:
17055 case DW_OP_reg19:
17056 case DW_OP_reg20:
17057 case DW_OP_reg21:
17058 case DW_OP_reg22:
17059 case DW_OP_reg23:
17060 case DW_OP_reg24:
17061 case DW_OP_reg25:
17062 case DW_OP_reg26:
17063 case DW_OP_reg27:
17064 case DW_OP_reg28:
17065 case DW_OP_reg29:
17066 case DW_OP_reg30:
17067 case DW_OP_reg31:
17068 case DW_OP_bregx:
17069 case DW_OP_piece:
17070 case DW_OP_deref_size:
17071 case DW_OP_nop:
17072 case DW_OP_bit_piece:
17073 case DW_OP_implicit_value:
17074 case DW_OP_stack_value:
17075 break;
17077 case DW_OP_addr:
17078 case DW_OP_const1u:
17079 case DW_OP_const1s:
17080 case DW_OP_const2u:
17081 case DW_OP_const2s:
17082 case DW_OP_const4u:
17083 case DW_OP_const4s:
17084 case DW_OP_const8u:
17085 case DW_OP_const8s:
17086 case DW_OP_constu:
17087 case DW_OP_consts:
17088 case DW_OP_dup:
17089 case DW_OP_over:
17090 case DW_OP_pick:
17091 case DW_OP_lit0:
17092 case DW_OP_lit1:
17093 case DW_OP_lit2:
17094 case DW_OP_lit3:
17095 case DW_OP_lit4:
17096 case DW_OP_lit5:
17097 case DW_OP_lit6:
17098 case DW_OP_lit7:
17099 case DW_OP_lit8:
17100 case DW_OP_lit9:
17101 case DW_OP_lit10:
17102 case DW_OP_lit11:
17103 case DW_OP_lit12:
17104 case DW_OP_lit13:
17105 case DW_OP_lit14:
17106 case DW_OP_lit15:
17107 case DW_OP_lit16:
17108 case DW_OP_lit17:
17109 case DW_OP_lit18:
17110 case DW_OP_lit19:
17111 case DW_OP_lit20:
17112 case DW_OP_lit21:
17113 case DW_OP_lit22:
17114 case DW_OP_lit23:
17115 case DW_OP_lit24:
17116 case DW_OP_lit25:
17117 case DW_OP_lit26:
17118 case DW_OP_lit27:
17119 case DW_OP_lit28:
17120 case DW_OP_lit29:
17121 case DW_OP_lit30:
17122 case DW_OP_lit31:
17123 case DW_OP_breg0:
17124 case DW_OP_breg1:
17125 case DW_OP_breg2:
17126 case DW_OP_breg3:
17127 case DW_OP_breg4:
17128 case DW_OP_breg5:
17129 case DW_OP_breg6:
17130 case DW_OP_breg7:
17131 case DW_OP_breg8:
17132 case DW_OP_breg9:
17133 case DW_OP_breg10:
17134 case DW_OP_breg11:
17135 case DW_OP_breg12:
17136 case DW_OP_breg13:
17137 case DW_OP_breg14:
17138 case DW_OP_breg15:
17139 case DW_OP_breg16:
17140 case DW_OP_breg17:
17141 case DW_OP_breg18:
17142 case DW_OP_breg19:
17143 case DW_OP_breg20:
17144 case DW_OP_breg21:
17145 case DW_OP_breg22:
17146 case DW_OP_breg23:
17147 case DW_OP_breg24:
17148 case DW_OP_breg25:
17149 case DW_OP_breg26:
17150 case DW_OP_breg27:
17151 case DW_OP_breg28:
17152 case DW_OP_breg29:
17153 case DW_OP_breg30:
17154 case DW_OP_breg31:
17155 case DW_OP_fbreg:
17156 case DW_OP_push_object_address:
17157 case DW_OP_call_frame_cfa:
17158 case DW_OP_GNU_variable_value:
17159 ++frame_offset_;
17160 break;
17162 case DW_OP_drop:
17163 case DW_OP_xderef:
17164 case DW_OP_and:
17165 case DW_OP_div:
17166 case DW_OP_minus:
17167 case DW_OP_mod:
17168 case DW_OP_mul:
17169 case DW_OP_or:
17170 case DW_OP_plus:
17171 case DW_OP_shl:
17172 case DW_OP_shr:
17173 case DW_OP_shra:
17174 case DW_OP_xor:
17175 case DW_OP_bra:
17176 case DW_OP_eq:
17177 case DW_OP_ge:
17178 case DW_OP_gt:
17179 case DW_OP_le:
17180 case DW_OP_lt:
17181 case DW_OP_ne:
17182 case DW_OP_regx:
17183 case DW_OP_xderef_size:
17184 --frame_offset_;
17185 break;
17187 case DW_OP_call2:
17188 case DW_OP_call4:
17189 case DW_OP_call_ref:
17191 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
17192 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
17194 if (stack_usage == NULL)
17195 return false;
17196 frame_offset_ += *stack_usage;
17197 break;
17200 case DW_OP_implicit_pointer:
17201 case DW_OP_entry_value:
17202 case DW_OP_const_type:
17203 case DW_OP_regval_type:
17204 case DW_OP_deref_type:
17205 case DW_OP_convert:
17206 case DW_OP_reinterpret:
17207 case DW_OP_form_tls_address:
17208 case DW_OP_GNU_push_tls_address:
17209 case DW_OP_GNU_uninit:
17210 case DW_OP_GNU_encoded_addr:
17211 case DW_OP_GNU_implicit_pointer:
17212 case DW_OP_GNU_entry_value:
17213 case DW_OP_GNU_const_type:
17214 case DW_OP_GNU_regval_type:
17215 case DW_OP_GNU_deref_type:
17216 case DW_OP_GNU_convert:
17217 case DW_OP_GNU_reinterpret:
17218 case DW_OP_GNU_parameter_ref:
17219 /* loc_list_from_tree will probably not output these operations for
17220 size functions, so assume they will not appear here. */
17221 /* Fall through... */
17223 default:
17224 gcc_unreachable ();
17227 /* Now, follow the control flow (except subroutine calls). */
17228 switch (l->dw_loc_opc)
17230 case DW_OP_bra:
17231 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
17232 frame_offsets))
17233 return false;
17234 /* Fall through. */
17236 case DW_OP_skip:
17237 l = l->dw_loc_oprnd1.v.val_loc;
17238 break;
17240 case DW_OP_stack_value:
17241 return true;
17243 default:
17244 l = l->dw_loc_next;
17245 break;
17249 return true;
17252 /* Make a DFS over operations reachable through LOC (i.e. follow branch
17253 operations) in order to resolve the operand of DW_OP_pick operations that
17254 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
17255 offset *before* LOC is executed. Return if all relocations were
17256 successful. */
17258 static bool
17259 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
17260 struct dwarf_procedure_info *dpi)
17262 /* Associate to all visited operations the frame offset *before* evaluating
17263 this operation. */
17264 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
17266 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
17267 frame_offsets);
17270 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
17271 Return NULL if it is not possible. */
17273 static dw_die_ref
17274 function_to_dwarf_procedure (tree fndecl)
17276 struct loc_descr_context ctx;
17277 struct dwarf_procedure_info dpi;
17278 dw_die_ref dwarf_proc_die;
17279 tree tree_body = DECL_SAVED_TREE (fndecl);
17280 dw_loc_descr_ref loc_body, epilogue;
17282 tree cursor;
17283 unsigned i;
17285 /* Do not generate multiple DWARF procedures for the same function
17286 declaration. */
17287 dwarf_proc_die = lookup_decl_die (fndecl);
17288 if (dwarf_proc_die != NULL)
17289 return dwarf_proc_die;
17291 /* DWARF procedures are available starting with the DWARFv3 standard. */
17292 if (dwarf_version < 3 && dwarf_strict)
17293 return NULL;
17295 /* We handle only functions for which we still have a body, that return a
17296 supported type and that takes arguments with supported types. Note that
17297 there is no point translating functions that return nothing. */
17298 if (tree_body == NULL_TREE
17299 || DECL_RESULT (fndecl) == NULL_TREE
17300 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
17301 return NULL;
17303 for (cursor = DECL_ARGUMENTS (fndecl);
17304 cursor != NULL_TREE;
17305 cursor = TREE_CHAIN (cursor))
17306 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
17307 return NULL;
17309 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
17310 if (TREE_CODE (tree_body) != RETURN_EXPR)
17311 return NULL;
17312 tree_body = TREE_OPERAND (tree_body, 0);
17313 if (TREE_CODE (tree_body) != MODIFY_EXPR
17314 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
17315 return NULL;
17316 tree_body = TREE_OPERAND (tree_body, 1);
17318 /* Try to translate the body expression itself. Note that this will probably
17319 cause an infinite recursion if its call graph has a cycle. This is very
17320 unlikely for size functions, however, so don't bother with such things at
17321 the moment. */
17322 ctx.context_type = NULL_TREE;
17323 ctx.base_decl = NULL_TREE;
17324 ctx.dpi = &dpi;
17325 ctx.placeholder_arg = false;
17326 ctx.placeholder_seen = false;
17327 dpi.fndecl = fndecl;
17328 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
17329 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
17330 if (!loc_body)
17331 return NULL;
17333 /* After evaluating all operands in "loc_body", we should still have on the
17334 stack all arguments plus the desired function result (top of the stack).
17335 Generate code in order to keep only the result in our stack frame. */
17336 epilogue = NULL;
17337 for (i = 0; i < dpi.args_count; ++i)
17339 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
17340 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
17341 op_couple->dw_loc_next->dw_loc_next = epilogue;
17342 epilogue = op_couple;
17344 add_loc_descr (&loc_body, epilogue);
17345 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
17346 return NULL;
17348 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
17349 because they are considered useful. Now there is an epilogue, they are
17350 not anymore, so give it another try. */
17351 loc_descr_without_nops (loc_body);
17353 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
17354 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
17355 though, given that size functions do not come from source, so they should
17356 not have a dedicated DW_TAG_subprogram DIE. */
17357 dwarf_proc_die
17358 = new_dwarf_proc_die (loc_body, fndecl,
17359 get_context_die (DECL_CONTEXT (fndecl)));
17361 /* The called DWARF procedure consumes one stack slot per argument and
17362 returns one stack slot. */
17363 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
17365 return dwarf_proc_die;
17369 /* Generate Dwarf location list representing LOC.
17370 If WANT_ADDRESS is false, expression computing LOC will be computed
17371 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
17372 if WANT_ADDRESS is 2, expression computing address useable in location
17373 will be returned (i.e. DW_OP_reg can be used
17374 to refer to register values).
17376 CONTEXT provides information to customize the location descriptions
17377 generation. Its context_type field specifies what type is implicitly
17378 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
17379 will not be generated.
17381 Its DPI field determines whether we are generating a DWARF expression for a
17382 DWARF procedure, so PARM_DECL references are processed specifically.
17384 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
17385 and dpi fields were null. */
17387 static dw_loc_list_ref
17388 loc_list_from_tree_1 (tree loc, int want_address,
17389 struct loc_descr_context *context)
17391 dw_loc_descr_ref ret = NULL, ret1 = NULL;
17392 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
17393 int have_address = 0;
17394 enum dwarf_location_atom op;
17396 /* ??? Most of the time we do not take proper care for sign/zero
17397 extending the values properly. Hopefully this won't be a real
17398 problem... */
17400 if (context != NULL
17401 && context->base_decl == loc
17402 && want_address == 0)
17404 if (dwarf_version >= 3 || !dwarf_strict)
17405 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
17406 NULL, NULL, NULL);
17407 else
17408 return NULL;
17411 switch (TREE_CODE (loc))
17413 case ERROR_MARK:
17414 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
17415 return 0;
17417 case PLACEHOLDER_EXPR:
17418 /* This case involves extracting fields from an object to determine the
17419 position of other fields. It is supposed to appear only as the first
17420 operand of COMPONENT_REF nodes and to reference precisely the type
17421 that the context allows. */
17422 if (context != NULL
17423 && TREE_TYPE (loc) == context->context_type
17424 && want_address >= 1)
17426 if (dwarf_version >= 3 || !dwarf_strict)
17428 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
17429 have_address = 1;
17430 break;
17432 else
17433 return NULL;
17435 /* For DW_TAG_generic_subrange attributes, PLACEHOLDER_EXPR stands for
17436 the single argument passed by consumer. */
17437 else if (context != NULL
17438 && context->placeholder_arg
17439 && INTEGRAL_TYPE_P (TREE_TYPE (loc))
17440 && want_address == 0)
17442 ret = new_loc_descr (DW_OP_pick, 0, 0);
17443 ret->frame_offset_rel = 1;
17444 context->placeholder_seen = true;
17445 break;
17447 else
17448 expansion_failed (loc, NULL_RTX,
17449 "PLACEHOLDER_EXPR for an unexpected type");
17450 break;
17452 case CALL_EXPR:
17454 const int nargs = call_expr_nargs (loc);
17455 tree callee = get_callee_fndecl (loc);
17456 int i;
17457 dw_die_ref dwarf_proc;
17459 if (callee == NULL_TREE)
17460 goto call_expansion_failed;
17462 /* We handle only functions that return an integer. */
17463 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
17464 goto call_expansion_failed;
17466 dwarf_proc = function_to_dwarf_procedure (callee);
17467 if (dwarf_proc == NULL)
17468 goto call_expansion_failed;
17470 /* Evaluate arguments right-to-left so that the first argument will
17471 be the top-most one on the stack. */
17472 for (i = nargs - 1; i >= 0; --i)
17474 dw_loc_descr_ref loc_descr
17475 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
17476 context);
17478 if (loc_descr == NULL)
17479 goto call_expansion_failed;
17481 add_loc_descr (&ret, loc_descr);
17484 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
17485 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17486 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
17487 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
17488 add_loc_descr (&ret, ret1);
17489 break;
17491 call_expansion_failed:
17492 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
17493 /* There are no opcodes for these operations. */
17494 return 0;
17497 case PREINCREMENT_EXPR:
17498 case PREDECREMENT_EXPR:
17499 case POSTINCREMENT_EXPR:
17500 case POSTDECREMENT_EXPR:
17501 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
17502 /* There are no opcodes for these operations. */
17503 return 0;
17505 case ADDR_EXPR:
17506 /* If we already want an address, see if there is INDIRECT_REF inside
17507 e.g. for &this->field. */
17508 if (want_address)
17510 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
17511 (loc, want_address == 2, context);
17512 if (list_ret)
17513 have_address = 1;
17514 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
17515 && (ret = cst_pool_loc_descr (loc)))
17516 have_address = 1;
17518 /* Otherwise, process the argument and look for the address. */
17519 if (!list_ret && !ret)
17520 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
17521 else
17523 if (want_address)
17524 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
17525 return NULL;
17527 break;
17529 case VAR_DECL:
17530 if (DECL_THREAD_LOCAL_P (loc))
17532 rtx rtl;
17533 enum dwarf_location_atom tls_op;
17534 enum dtprel_bool dtprel = dtprel_false;
17536 if (targetm.have_tls)
17538 /* If this is not defined, we have no way to emit the
17539 data. */
17540 if (!targetm.asm_out.output_dwarf_dtprel)
17541 return 0;
17543 /* The way DW_OP_GNU_push_tls_address is specified, we
17544 can only look up addresses of objects in the current
17545 module. We used DW_OP_addr as first op, but that's
17546 wrong, because DW_OP_addr is relocated by the debug
17547 info consumer, while DW_OP_GNU_push_tls_address
17548 operand shouldn't be. */
17549 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
17550 return 0;
17551 dtprel = dtprel_true;
17552 /* We check for DWARF 5 here because gdb did not implement
17553 DW_OP_form_tls_address until after 7.12. */
17554 tls_op = (dwarf_version >= 5 ? DW_OP_form_tls_address
17555 : DW_OP_GNU_push_tls_address);
17557 else
17559 if (!targetm.emutls.debug_form_tls_address
17560 || !(dwarf_version >= 3 || !dwarf_strict))
17561 return 0;
17562 /* We stuffed the control variable into the DECL_VALUE_EXPR
17563 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
17564 no longer appear in gimple code. We used the control
17565 variable in specific so that we could pick it up here. */
17566 loc = DECL_VALUE_EXPR (loc);
17567 tls_op = DW_OP_form_tls_address;
17570 rtl = rtl_for_decl_location (loc);
17571 if (rtl == NULL_RTX)
17572 return 0;
17574 if (!MEM_P (rtl))
17575 return 0;
17576 rtl = XEXP (rtl, 0);
17577 if (! CONSTANT_P (rtl))
17578 return 0;
17580 ret = new_addr_loc_descr (rtl, dtprel);
17581 ret1 = new_loc_descr (tls_op, 0, 0);
17582 add_loc_descr (&ret, ret1);
17584 have_address = 1;
17585 break;
17587 /* FALLTHRU */
17589 case PARM_DECL:
17590 if (context != NULL && context->dpi != NULL
17591 && DECL_CONTEXT (loc) == context->dpi->fndecl)
17593 /* We are generating code for a DWARF procedure and we want to access
17594 one of its arguments: find the appropriate argument offset and let
17595 the resolve_args_picking pass compute the offset that complies
17596 with the stack frame size. */
17597 unsigned i = 0;
17598 tree cursor;
17600 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
17601 cursor != NULL_TREE && cursor != loc;
17602 cursor = TREE_CHAIN (cursor), ++i)
17604 /* If we are translating a DWARF procedure, all referenced parameters
17605 must belong to the current function. */
17606 gcc_assert (cursor != NULL_TREE);
17608 ret = new_loc_descr (DW_OP_pick, i, 0);
17609 ret->frame_offset_rel = 1;
17610 break;
17612 /* FALLTHRU */
17614 case RESULT_DECL:
17615 if (DECL_HAS_VALUE_EXPR_P (loc))
17616 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
17617 want_address, context);
17618 /* FALLTHRU */
17620 case FUNCTION_DECL:
17622 rtx rtl;
17623 var_loc_list *loc_list = lookup_decl_loc (loc);
17625 if (loc_list && loc_list->first)
17627 list_ret = dw_loc_list (loc_list, loc, want_address);
17628 have_address = want_address != 0;
17629 break;
17631 rtl = rtl_for_decl_location (loc);
17632 if (rtl == NULL_RTX)
17634 if (TREE_CODE (loc) != FUNCTION_DECL
17635 && early_dwarf
17636 && current_function_decl
17637 && want_address != 1
17638 && ! DECL_IGNORED_P (loc)
17639 && (INTEGRAL_TYPE_P (TREE_TYPE (loc))
17640 || POINTER_TYPE_P (TREE_TYPE (loc)))
17641 && DECL_CONTEXT (loc) == current_function_decl
17642 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (loc)))
17643 <= DWARF2_ADDR_SIZE))
17645 dw_die_ref ref = lookup_decl_die (loc);
17646 ret = new_loc_descr (DW_OP_GNU_variable_value, 0, 0);
17647 if (ref)
17649 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
17650 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
17651 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
17653 else
17655 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
17656 ret->dw_loc_oprnd1.v.val_decl_ref = loc;
17658 break;
17660 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
17661 return 0;
17663 else if (CONST_INT_P (rtl))
17665 HOST_WIDE_INT val = INTVAL (rtl);
17666 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17667 val &= GET_MODE_MASK (DECL_MODE (loc));
17668 ret = int_loc_descriptor (val);
17670 else if (GET_CODE (rtl) == CONST_STRING)
17672 expansion_failed (loc, NULL_RTX, "CONST_STRING");
17673 return 0;
17675 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
17676 ret = new_addr_loc_descr (rtl, dtprel_false);
17677 else
17679 machine_mode mode, mem_mode;
17681 /* Certain constructs can only be represented at top-level. */
17682 if (want_address == 2)
17684 ret = loc_descriptor (rtl, VOIDmode,
17685 VAR_INIT_STATUS_INITIALIZED);
17686 have_address = 1;
17688 else
17690 mode = GET_MODE (rtl);
17691 mem_mode = VOIDmode;
17692 if (MEM_P (rtl))
17694 mem_mode = mode;
17695 mode = get_address_mode (rtl);
17696 rtl = XEXP (rtl, 0);
17697 have_address = 1;
17699 ret = mem_loc_descriptor (rtl, mode, mem_mode,
17700 VAR_INIT_STATUS_INITIALIZED);
17702 if (!ret)
17703 expansion_failed (loc, rtl,
17704 "failed to produce loc descriptor for rtl");
17707 break;
17709 case MEM_REF:
17710 if (!integer_zerop (TREE_OPERAND (loc, 1)))
17712 have_address = 1;
17713 goto do_plus;
17715 /* Fallthru. */
17716 case INDIRECT_REF:
17717 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17718 have_address = 1;
17719 break;
17721 case TARGET_MEM_REF:
17722 case SSA_NAME:
17723 case DEBUG_EXPR_DECL:
17724 return NULL;
17726 case COMPOUND_EXPR:
17727 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
17728 context);
17730 CASE_CONVERT:
17731 case VIEW_CONVERT_EXPR:
17732 case SAVE_EXPR:
17733 case MODIFY_EXPR:
17734 case NON_LVALUE_EXPR:
17735 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
17736 context);
17738 case COMPONENT_REF:
17739 case BIT_FIELD_REF:
17740 case ARRAY_REF:
17741 case ARRAY_RANGE_REF:
17742 case REALPART_EXPR:
17743 case IMAGPART_EXPR:
17745 tree obj, offset;
17746 poly_int64 bitsize, bitpos, bytepos;
17747 machine_mode mode;
17748 int unsignedp, reversep, volatilep = 0;
17750 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
17751 &unsignedp, &reversep, &volatilep);
17753 gcc_assert (obj != loc);
17755 list_ret = loc_list_from_tree_1 (obj,
17756 want_address == 2
17757 && known_eq (bitpos, 0)
17758 && !offset ? 2 : 1,
17759 context);
17760 /* TODO: We can extract value of the small expression via shifting even
17761 for nonzero bitpos. */
17762 if (list_ret == 0)
17763 return 0;
17764 if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
17765 || !multiple_p (bitsize, BITS_PER_UNIT))
17767 expansion_failed (loc, NULL_RTX,
17768 "bitfield access");
17769 return 0;
17772 if (offset != NULL_TREE)
17774 /* Variable offset. */
17775 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
17776 if (list_ret1 == 0)
17777 return 0;
17778 add_loc_list (&list_ret, list_ret1);
17779 if (!list_ret)
17780 return 0;
17781 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
17784 HOST_WIDE_INT value;
17785 if (bytepos.is_constant (&value) && value > 0)
17786 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst,
17787 value, 0));
17788 else if (maybe_ne (bytepos, 0))
17789 loc_list_plus_const (list_ret, bytepos);
17791 have_address = 1;
17792 break;
17795 case INTEGER_CST:
17796 if ((want_address || !tree_fits_shwi_p (loc))
17797 && (ret = cst_pool_loc_descr (loc)))
17798 have_address = 1;
17799 else if (want_address == 2
17800 && tree_fits_shwi_p (loc)
17801 && (ret = address_of_int_loc_descriptor
17802 (int_size_in_bytes (TREE_TYPE (loc)),
17803 tree_to_shwi (loc))))
17804 have_address = 1;
17805 else if (tree_fits_shwi_p (loc))
17806 ret = int_loc_descriptor (tree_to_shwi (loc));
17807 else if (tree_fits_uhwi_p (loc))
17808 ret = uint_loc_descriptor (tree_to_uhwi (loc));
17809 else
17811 expansion_failed (loc, NULL_RTX,
17812 "Integer operand is not host integer");
17813 return 0;
17815 break;
17817 case CONSTRUCTOR:
17818 case REAL_CST:
17819 case STRING_CST:
17820 case COMPLEX_CST:
17821 if ((ret = cst_pool_loc_descr (loc)))
17822 have_address = 1;
17823 else if (TREE_CODE (loc) == CONSTRUCTOR)
17825 tree type = TREE_TYPE (loc);
17826 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
17827 unsigned HOST_WIDE_INT offset = 0;
17828 unsigned HOST_WIDE_INT cnt;
17829 constructor_elt *ce;
17831 if (TREE_CODE (type) == RECORD_TYPE)
17833 /* This is very limited, but it's enough to output
17834 pointers to member functions, as long as the
17835 referenced function is defined in the current
17836 translation unit. */
17837 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
17839 tree val = ce->value;
17841 tree field = ce->index;
17843 if (val)
17844 STRIP_NOPS (val);
17846 if (!field || DECL_BIT_FIELD (field))
17848 expansion_failed (loc, NULL_RTX,
17849 "bitfield in record type constructor");
17850 size = offset = (unsigned HOST_WIDE_INT)-1;
17851 ret = NULL;
17852 break;
17855 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17856 unsigned HOST_WIDE_INT pos = int_byte_position (field);
17857 gcc_assert (pos + fieldsize <= size);
17858 if (pos < offset)
17860 expansion_failed (loc, NULL_RTX,
17861 "out-of-order fields in record constructor");
17862 size = offset = (unsigned HOST_WIDE_INT)-1;
17863 ret = NULL;
17864 break;
17866 if (pos > offset)
17868 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
17869 add_loc_descr (&ret, ret1);
17870 offset = pos;
17872 if (val && fieldsize != 0)
17874 ret1 = loc_descriptor_from_tree (val, want_address, context);
17875 if (!ret1)
17877 expansion_failed (loc, NULL_RTX,
17878 "unsupported expression in field");
17879 size = offset = (unsigned HOST_WIDE_INT)-1;
17880 ret = NULL;
17881 break;
17883 add_loc_descr (&ret, ret1);
17885 if (fieldsize)
17887 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
17888 add_loc_descr (&ret, ret1);
17889 offset = pos + fieldsize;
17893 if (offset != size)
17895 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
17896 add_loc_descr (&ret, ret1);
17897 offset = size;
17900 have_address = !!want_address;
17902 else
17903 expansion_failed (loc, NULL_RTX,
17904 "constructor of non-record type");
17906 else
17907 /* We can construct small constants here using int_loc_descriptor. */
17908 expansion_failed (loc, NULL_RTX,
17909 "constructor or constant not in constant pool");
17910 break;
17912 case TRUTH_AND_EXPR:
17913 case TRUTH_ANDIF_EXPR:
17914 case BIT_AND_EXPR:
17915 op = DW_OP_and;
17916 goto do_binop;
17918 case TRUTH_XOR_EXPR:
17919 case BIT_XOR_EXPR:
17920 op = DW_OP_xor;
17921 goto do_binop;
17923 case TRUTH_OR_EXPR:
17924 case TRUTH_ORIF_EXPR:
17925 case BIT_IOR_EXPR:
17926 op = DW_OP_or;
17927 goto do_binop;
17929 case FLOOR_DIV_EXPR:
17930 case CEIL_DIV_EXPR:
17931 case ROUND_DIV_EXPR:
17932 case TRUNC_DIV_EXPR:
17933 case EXACT_DIV_EXPR:
17934 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17935 return 0;
17936 op = DW_OP_div;
17937 goto do_binop;
17939 case MINUS_EXPR:
17940 op = DW_OP_minus;
17941 goto do_binop;
17943 case FLOOR_MOD_EXPR:
17944 case CEIL_MOD_EXPR:
17945 case ROUND_MOD_EXPR:
17946 case TRUNC_MOD_EXPR:
17947 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
17949 op = DW_OP_mod;
17950 goto do_binop;
17952 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17953 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
17954 if (list_ret == 0 || list_ret1 == 0)
17955 return 0;
17957 add_loc_list (&list_ret, list_ret1);
17958 if (list_ret == 0)
17959 return 0;
17960 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17961 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
17962 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
17963 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
17964 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
17965 break;
17967 case MULT_EXPR:
17968 op = DW_OP_mul;
17969 goto do_binop;
17971 case LSHIFT_EXPR:
17972 op = DW_OP_shl;
17973 goto do_binop;
17975 case RSHIFT_EXPR:
17976 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
17977 goto do_binop;
17979 case POINTER_PLUS_EXPR:
17980 case PLUS_EXPR:
17981 do_plus:
17982 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
17984 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
17985 smarter to encode their opposite. The DW_OP_plus_uconst operation
17986 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
17987 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
17988 bytes, Y being the size of the operation that pushes the opposite
17989 of the addend. So let's choose the smallest representation. */
17990 const tree tree_addend = TREE_OPERAND (loc, 1);
17991 offset_int wi_addend;
17992 HOST_WIDE_INT shwi_addend;
17993 dw_loc_descr_ref loc_naddend;
17995 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
17996 if (list_ret == 0)
17997 return 0;
17999 /* Try to get the literal to push. It is the opposite of the addend,
18000 so as we rely on wrapping during DWARF evaluation, first decode
18001 the literal as a "DWARF-sized" signed number. */
18002 wi_addend = wi::to_offset (tree_addend);
18003 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
18004 shwi_addend = wi_addend.to_shwi ();
18005 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
18006 ? int_loc_descriptor (-shwi_addend)
18007 : NULL;
18009 if (loc_naddend != NULL
18010 && ((unsigned) size_of_uleb128 (shwi_addend)
18011 > size_of_loc_descr (loc_naddend)))
18013 add_loc_descr_to_each (list_ret, loc_naddend);
18014 add_loc_descr_to_each (list_ret,
18015 new_loc_descr (DW_OP_minus, 0, 0));
18017 else
18019 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
18021 loc_naddend = loc_cur;
18022 loc_cur = loc_cur->dw_loc_next;
18023 ggc_free (loc_naddend);
18025 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
18027 break;
18030 op = DW_OP_plus;
18031 goto do_binop;
18033 case LE_EXPR:
18034 op = DW_OP_le;
18035 goto do_comp_binop;
18037 case GE_EXPR:
18038 op = DW_OP_ge;
18039 goto do_comp_binop;
18041 case LT_EXPR:
18042 op = DW_OP_lt;
18043 goto do_comp_binop;
18045 case GT_EXPR:
18046 op = DW_OP_gt;
18047 goto do_comp_binop;
18049 do_comp_binop:
18050 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
18052 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
18053 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
18054 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
18055 TREE_CODE (loc));
18056 break;
18058 else
18059 goto do_binop;
18061 case EQ_EXPR:
18062 op = DW_OP_eq;
18063 goto do_binop;
18065 case NE_EXPR:
18066 op = DW_OP_ne;
18067 goto do_binop;
18069 do_binop:
18070 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18071 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
18072 if (list_ret == 0 || list_ret1 == 0)
18073 return 0;
18075 add_loc_list (&list_ret, list_ret1);
18076 if (list_ret == 0)
18077 return 0;
18078 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18079 break;
18081 case TRUTH_NOT_EXPR:
18082 case BIT_NOT_EXPR:
18083 op = DW_OP_not;
18084 goto do_unop;
18086 case ABS_EXPR:
18087 op = DW_OP_abs;
18088 goto do_unop;
18090 case NEGATE_EXPR:
18091 op = DW_OP_neg;
18092 goto do_unop;
18094 do_unop:
18095 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18096 if (list_ret == 0)
18097 return 0;
18099 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
18100 break;
18102 case MIN_EXPR:
18103 case MAX_EXPR:
18105 const enum tree_code code =
18106 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
18108 loc = build3 (COND_EXPR, TREE_TYPE (loc),
18109 build2 (code, integer_type_node,
18110 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
18111 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
18114 /* fall through */
18116 case COND_EXPR:
18118 dw_loc_descr_ref lhs
18119 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
18120 dw_loc_list_ref rhs
18121 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
18122 dw_loc_descr_ref bra_node, jump_node, tmp;
18124 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
18125 if (list_ret == 0 || lhs == 0 || rhs == 0)
18126 return 0;
18128 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
18129 add_loc_descr_to_each (list_ret, bra_node);
18131 add_loc_list (&list_ret, rhs);
18132 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
18133 add_loc_descr_to_each (list_ret, jump_node);
18135 add_loc_descr_to_each (list_ret, lhs);
18136 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18137 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
18139 /* ??? Need a node to point the skip at. Use a nop. */
18140 tmp = new_loc_descr (DW_OP_nop, 0, 0);
18141 add_loc_descr_to_each (list_ret, tmp);
18142 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
18143 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
18145 break;
18147 case FIX_TRUNC_EXPR:
18148 return 0;
18150 default:
18151 /* Leave front-end specific codes as simply unknown. This comes
18152 up, for instance, with the C STMT_EXPR. */
18153 if ((unsigned int) TREE_CODE (loc)
18154 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
18156 expansion_failed (loc, NULL_RTX,
18157 "language specific tree node");
18158 return 0;
18161 /* Otherwise this is a generic code; we should just lists all of
18162 these explicitly. We forgot one. */
18163 if (flag_checking)
18164 gcc_unreachable ();
18166 /* In a release build, we want to degrade gracefully: better to
18167 generate incomplete debugging information than to crash. */
18168 return NULL;
18171 if (!ret && !list_ret)
18172 return 0;
18174 if (want_address == 2 && !have_address
18175 && (dwarf_version >= 4 || !dwarf_strict))
18177 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
18179 expansion_failed (loc, NULL_RTX,
18180 "DWARF address size mismatch");
18181 return 0;
18183 if (ret)
18184 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
18185 else
18186 add_loc_descr_to_each (list_ret,
18187 new_loc_descr (DW_OP_stack_value, 0, 0));
18188 have_address = 1;
18190 /* Show if we can't fill the request for an address. */
18191 if (want_address && !have_address)
18193 expansion_failed (loc, NULL_RTX,
18194 "Want address and only have value");
18195 return 0;
18198 gcc_assert (!ret || !list_ret);
18200 /* If we've got an address and don't want one, dereference. */
18201 if (!want_address && have_address)
18203 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
18205 if (size > DWARF2_ADDR_SIZE || size == -1)
18207 expansion_failed (loc, NULL_RTX,
18208 "DWARF address size mismatch");
18209 return 0;
18211 else if (size == DWARF2_ADDR_SIZE)
18212 op = DW_OP_deref;
18213 else
18214 op = DW_OP_deref_size;
18216 if (ret)
18217 add_loc_descr (&ret, new_loc_descr (op, size, 0));
18218 else
18219 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
18221 if (ret)
18222 list_ret = new_loc_list (ret, NULL, NULL, NULL);
18224 return list_ret;
18227 /* Likewise, but strip useless DW_OP_nop operations in the resulting
18228 expressions. */
18230 static dw_loc_list_ref
18231 loc_list_from_tree (tree loc, int want_address,
18232 struct loc_descr_context *context)
18234 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
18236 for (dw_loc_list_ref loc_cur = result;
18237 loc_cur != NULL; loc_cur = loc_cur->dw_loc_next)
18238 loc_descr_without_nops (loc_cur->expr);
18239 return result;
18242 /* Same as above but return only single location expression. */
18243 static dw_loc_descr_ref
18244 loc_descriptor_from_tree (tree loc, int want_address,
18245 struct loc_descr_context *context)
18247 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
18248 if (!ret)
18249 return NULL;
18250 if (ret->dw_loc_next)
18252 expansion_failed (loc, NULL_RTX,
18253 "Location list where only loc descriptor needed");
18254 return NULL;
18256 return ret->expr;
18259 /* Given a value, round it up to the lowest multiple of `boundary'
18260 which is not less than the value itself. */
18262 static inline HOST_WIDE_INT
18263 ceiling (HOST_WIDE_INT value, unsigned int boundary)
18265 return (((value + boundary - 1) / boundary) * boundary);
18268 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
18269 pointer to the declared type for the relevant field variable, or return
18270 `integer_type_node' if the given node turns out to be an
18271 ERROR_MARK node. */
18273 static inline tree
18274 field_type (const_tree decl)
18276 tree type;
18278 if (TREE_CODE (decl) == ERROR_MARK)
18279 return integer_type_node;
18281 type = DECL_BIT_FIELD_TYPE (decl);
18282 if (type == NULL_TREE)
18283 type = TREE_TYPE (decl);
18285 return type;
18288 /* Given a pointer to a tree node, return the alignment in bits for
18289 it, or else return BITS_PER_WORD if the node actually turns out to
18290 be an ERROR_MARK node. */
18292 static inline unsigned
18293 simple_type_align_in_bits (const_tree type)
18295 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
18298 static inline unsigned
18299 simple_decl_align_in_bits (const_tree decl)
18301 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
18304 /* Return the result of rounding T up to ALIGN. */
18306 static inline offset_int
18307 round_up_to_align (const offset_int &t, unsigned int align)
18309 return wi::udiv_trunc (t + align - 1, align) * align;
18312 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
18313 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
18314 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
18315 if we fail to return the size in one of these two forms. */
18317 static dw_loc_descr_ref
18318 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
18320 tree tree_size;
18321 struct loc_descr_context ctx;
18323 /* Return a constant integer in priority, if possible. */
18324 *cst_size = int_size_in_bytes (type);
18325 if (*cst_size != -1)
18326 return NULL;
18328 ctx.context_type = const_cast<tree> (type);
18329 ctx.base_decl = NULL_TREE;
18330 ctx.dpi = NULL;
18331 ctx.placeholder_arg = false;
18332 ctx.placeholder_seen = false;
18334 type = TYPE_MAIN_VARIANT (type);
18335 tree_size = TYPE_SIZE_UNIT (type);
18336 return ((tree_size != NULL_TREE)
18337 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
18338 : NULL);
18341 /* Helper structure for RECORD_TYPE processing. */
18342 struct vlr_context
18344 /* Root RECORD_TYPE. It is needed to generate data member location
18345 descriptions in variable-length records (VLR), but also to cope with
18346 variants, which are composed of nested structures multiplexed with
18347 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
18348 function processing a FIELD_DECL, it is required to be non null. */
18349 tree struct_type;
18350 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
18351 QUAL_UNION_TYPE), this holds an expression that computes the offset for
18352 this variant part as part of the root record (in storage units). For
18353 regular records, it must be NULL_TREE. */
18354 tree variant_part_offset;
18357 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
18358 addressed byte of the "containing object" for the given FIELD_DECL. If
18359 possible, return a native constant through CST_OFFSET (in which case NULL is
18360 returned); otherwise return a DWARF expression that computes the offset.
18362 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
18363 that offset is, either because the argument turns out to be a pointer to an
18364 ERROR_MARK node, or because the offset expression is too complex for us.
18366 CTX is required: see the comment for VLR_CONTEXT. */
18368 static dw_loc_descr_ref
18369 field_byte_offset (const_tree decl, struct vlr_context *ctx,
18370 HOST_WIDE_INT *cst_offset)
18372 tree tree_result;
18373 dw_loc_list_ref loc_result;
18375 *cst_offset = 0;
18377 if (TREE_CODE (decl) == ERROR_MARK)
18378 return NULL;
18379 else
18380 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
18382 /* We cannot handle variable bit offsets at the moment, so abort if it's the
18383 case. */
18384 if (TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST)
18385 return NULL;
18387 #ifdef PCC_BITFIELD_TYPE_MATTERS
18388 /* We used to handle only constant offsets in all cases. Now, we handle
18389 properly dynamic byte offsets only when PCC bitfield type doesn't
18390 matter. */
18391 if (PCC_BITFIELD_TYPE_MATTERS
18392 && TREE_CODE (DECL_FIELD_OFFSET (decl)) == INTEGER_CST)
18394 offset_int object_offset_in_bits;
18395 offset_int object_offset_in_bytes;
18396 offset_int bitpos_int;
18397 tree type;
18398 tree field_size_tree;
18399 offset_int deepest_bitpos;
18400 offset_int field_size_in_bits;
18401 unsigned int type_align_in_bits;
18402 unsigned int decl_align_in_bits;
18403 offset_int type_size_in_bits;
18405 bitpos_int = wi::to_offset (bit_position (decl));
18406 type = field_type (decl);
18407 type_size_in_bits = offset_int_type_size_in_bits (type);
18408 type_align_in_bits = simple_type_align_in_bits (type);
18410 field_size_tree = DECL_SIZE (decl);
18412 /* The size could be unspecified if there was an error, or for
18413 a flexible array member. */
18414 if (!field_size_tree)
18415 field_size_tree = bitsize_zero_node;
18417 /* If the size of the field is not constant, use the type size. */
18418 if (TREE_CODE (field_size_tree) == INTEGER_CST)
18419 field_size_in_bits = wi::to_offset (field_size_tree);
18420 else
18421 field_size_in_bits = type_size_in_bits;
18423 decl_align_in_bits = simple_decl_align_in_bits (decl);
18425 /* The GCC front-end doesn't make any attempt to keep track of the
18426 starting bit offset (relative to the start of the containing
18427 structure type) of the hypothetical "containing object" for a
18428 bit-field. Thus, when computing the byte offset value for the
18429 start of the "containing object" of a bit-field, we must deduce
18430 this information on our own. This can be rather tricky to do in
18431 some cases. For example, handling the following structure type
18432 definition when compiling for an i386/i486 target (which only
18433 aligns long long's to 32-bit boundaries) can be very tricky:
18435 struct S { int field1; long long field2:31; };
18437 Fortunately, there is a simple rule-of-thumb which can be used
18438 in such cases. When compiling for an i386/i486, GCC will
18439 allocate 8 bytes for the structure shown above. It decides to
18440 do this based upon one simple rule for bit-field allocation.
18441 GCC allocates each "containing object" for each bit-field at
18442 the first (i.e. lowest addressed) legitimate alignment boundary
18443 (based upon the required minimum alignment for the declared
18444 type of the field) which it can possibly use, subject to the
18445 condition that there is still enough available space remaining
18446 in the containing object (when allocated at the selected point)
18447 to fully accommodate all of the bits of the bit-field itself.
18449 This simple rule makes it obvious why GCC allocates 8 bytes for
18450 each object of the structure type shown above. When looking
18451 for a place to allocate the "containing object" for `field2',
18452 the compiler simply tries to allocate a 64-bit "containing
18453 object" at each successive 32-bit boundary (starting at zero)
18454 until it finds a place to allocate that 64- bit field such that
18455 at least 31 contiguous (and previously unallocated) bits remain
18456 within that selected 64 bit field. (As it turns out, for the
18457 example above, the compiler finds it is OK to allocate the
18458 "containing object" 64-bit field at bit-offset zero within the
18459 structure type.)
18461 Here we attempt to work backwards from the limited set of facts
18462 we're given, and we try to deduce from those facts, where GCC
18463 must have believed that the containing object started (within
18464 the structure type). The value we deduce is then used (by the
18465 callers of this routine) to generate DW_AT_location and
18466 DW_AT_bit_offset attributes for fields (both bit-fields and, in
18467 the case of DW_AT_location, regular fields as well). */
18469 /* Figure out the bit-distance from the start of the structure to
18470 the "deepest" bit of the bit-field. */
18471 deepest_bitpos = bitpos_int + field_size_in_bits;
18473 /* This is the tricky part. Use some fancy footwork to deduce
18474 where the lowest addressed bit of the containing object must
18475 be. */
18476 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18478 /* Round up to type_align by default. This works best for
18479 bitfields. */
18480 object_offset_in_bits
18481 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
18483 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
18485 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
18487 /* Round up to decl_align instead. */
18488 object_offset_in_bits
18489 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
18492 object_offset_in_bytes
18493 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
18494 if (ctx->variant_part_offset == NULL_TREE)
18496 *cst_offset = object_offset_in_bytes.to_shwi ();
18497 return NULL;
18499 tree_result = wide_int_to_tree (sizetype, object_offset_in_bytes);
18501 else
18502 #endif /* PCC_BITFIELD_TYPE_MATTERS */
18503 tree_result = byte_position (decl);
18505 if (ctx->variant_part_offset != NULL_TREE)
18506 tree_result = fold_build2 (PLUS_EXPR, TREE_TYPE (tree_result),
18507 ctx->variant_part_offset, tree_result);
18509 /* If the byte offset is a constant, it's simplier to handle a native
18510 constant rather than a DWARF expression. */
18511 if (TREE_CODE (tree_result) == INTEGER_CST)
18513 *cst_offset = wi::to_offset (tree_result).to_shwi ();
18514 return NULL;
18516 struct loc_descr_context loc_ctx = {
18517 ctx->struct_type, /* context_type */
18518 NULL_TREE, /* base_decl */
18519 NULL, /* dpi */
18520 false, /* placeholder_arg */
18521 false /* placeholder_seen */
18523 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
18525 /* We want a DWARF expression: abort if we only have a location list with
18526 multiple elements. */
18527 if (!loc_result || !single_element_loc_list_p (loc_result))
18528 return NULL;
18529 else
18530 return loc_result->expr;
18533 /* The following routines define various Dwarf attributes and any data
18534 associated with them. */
18536 /* Add a location description attribute value to a DIE.
18538 This emits location attributes suitable for whole variables and
18539 whole parameters. Note that the location attributes for struct fields are
18540 generated by the routine `data_member_location_attribute' below. */
18542 static inline void
18543 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
18544 dw_loc_list_ref descr)
18546 if (descr == 0)
18547 return;
18548 if (single_element_loc_list_p (descr))
18549 add_AT_loc (die, attr_kind, descr->expr);
18550 else
18551 add_AT_loc_list (die, attr_kind, descr);
18554 /* Add DW_AT_accessibility attribute to DIE if needed. */
18556 static void
18557 add_accessibility_attribute (dw_die_ref die, tree decl)
18559 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18560 children, otherwise the default is DW_ACCESS_public. In DWARF2
18561 the default has always been DW_ACCESS_public. */
18562 if (TREE_PROTECTED (decl))
18563 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18564 else if (TREE_PRIVATE (decl))
18566 if (dwarf_version == 2
18567 || die->die_parent == NULL
18568 || die->die_parent->die_tag != DW_TAG_class_type)
18569 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18571 else if (dwarf_version > 2
18572 && die->die_parent
18573 && die->die_parent->die_tag == DW_TAG_class_type)
18574 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18577 /* Attach the specialized form of location attribute used for data members of
18578 struct and union types. In the special case of a FIELD_DECL node which
18579 represents a bit-field, the "offset" part of this special location
18580 descriptor must indicate the distance in bytes from the lowest-addressed
18581 byte of the containing struct or union type to the lowest-addressed byte of
18582 the "containing object" for the bit-field. (See the `field_byte_offset'
18583 function above).
18585 For any given bit-field, the "containing object" is a hypothetical object
18586 (of some integral or enum type) within which the given bit-field lives. The
18587 type of this hypothetical "containing object" is always the same as the
18588 declared type of the individual bit-field itself (for GCC anyway... the
18589 DWARF spec doesn't actually mandate this). Note that it is the size (in
18590 bytes) of the hypothetical "containing object" which will be given in the
18591 DW_AT_byte_size attribute for this bit-field. (See the
18592 `byte_size_attribute' function below.) It is also used when calculating the
18593 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
18594 function below.)
18596 CTX is required: see the comment for VLR_CONTEXT. */
18598 static void
18599 add_data_member_location_attribute (dw_die_ref die,
18600 tree decl,
18601 struct vlr_context *ctx)
18603 HOST_WIDE_INT offset;
18604 dw_loc_descr_ref loc_descr = 0;
18606 if (TREE_CODE (decl) == TREE_BINFO)
18608 /* We're working on the TAG_inheritance for a base class. */
18609 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
18611 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
18612 aren't at a fixed offset from all (sub)objects of the same
18613 type. We need to extract the appropriate offset from our
18614 vtable. The following dwarf expression means
18616 BaseAddr = ObAddr + *((*ObAddr) - Offset)
18618 This is specific to the V3 ABI, of course. */
18620 dw_loc_descr_ref tmp;
18622 /* Make a copy of the object address. */
18623 tmp = new_loc_descr (DW_OP_dup, 0, 0);
18624 add_loc_descr (&loc_descr, tmp);
18626 /* Extract the vtable address. */
18627 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18628 add_loc_descr (&loc_descr, tmp);
18630 /* Calculate the address of the offset. */
18631 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
18632 gcc_assert (offset < 0);
18634 tmp = int_loc_descriptor (-offset);
18635 add_loc_descr (&loc_descr, tmp);
18636 tmp = new_loc_descr (DW_OP_minus, 0, 0);
18637 add_loc_descr (&loc_descr, tmp);
18639 /* Extract the offset. */
18640 tmp = new_loc_descr (DW_OP_deref, 0, 0);
18641 add_loc_descr (&loc_descr, tmp);
18643 /* Add it to the object address. */
18644 tmp = new_loc_descr (DW_OP_plus, 0, 0);
18645 add_loc_descr (&loc_descr, tmp);
18647 else
18648 offset = tree_to_shwi (BINFO_OFFSET (decl));
18650 else
18652 loc_descr = field_byte_offset (decl, ctx, &offset);
18654 /* If loc_descr is available then we know the field offset is dynamic.
18655 However, GDB does not handle dynamic field offsets very well at the
18656 moment. */
18657 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
18659 loc_descr = NULL;
18660 offset = 0;
18663 /* Data member location evalutation starts with the base address on the
18664 stack. Compute the field offset and add it to this base address. */
18665 else if (loc_descr != NULL)
18666 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
18669 if (! loc_descr)
18671 /* While DW_AT_data_bit_offset has been added already in DWARF4,
18672 e.g. GDB only added support to it in November 2016. For DWARF5
18673 we need newer debug info consumers anyway. We might change this
18674 to dwarf_version >= 4 once most consumers catched up. */
18675 if (dwarf_version >= 5
18676 && TREE_CODE (decl) == FIELD_DECL
18677 && DECL_BIT_FIELD_TYPE (decl))
18679 tree off = bit_position (decl);
18680 if (tree_fits_uhwi_p (off) && get_AT (die, DW_AT_bit_size))
18682 remove_AT (die, DW_AT_byte_size);
18683 remove_AT (die, DW_AT_bit_offset);
18684 add_AT_unsigned (die, DW_AT_data_bit_offset, tree_to_uhwi (off));
18685 return;
18688 if (dwarf_version > 2)
18690 /* Don't need to output a location expression, just the constant. */
18691 if (offset < 0)
18692 add_AT_int (die, DW_AT_data_member_location, offset);
18693 else
18694 add_AT_unsigned (die, DW_AT_data_member_location, offset);
18695 return;
18697 else
18699 enum dwarf_location_atom op;
18701 /* The DWARF2 standard says that we should assume that the structure
18702 address is already on the stack, so we can specify a structure
18703 field address by using DW_OP_plus_uconst. */
18704 op = DW_OP_plus_uconst;
18705 loc_descr = new_loc_descr (op, offset, 0);
18709 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
18712 /* Writes integer values to dw_vec_const array. */
18714 static void
18715 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
18717 while (size != 0)
18719 *dest++ = val & 0xff;
18720 val >>= 8;
18721 --size;
18725 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
18727 static HOST_WIDE_INT
18728 extract_int (const unsigned char *src, unsigned int size)
18730 HOST_WIDE_INT val = 0;
18732 src += size;
18733 while (size != 0)
18735 val <<= 8;
18736 val |= *--src & 0xff;
18737 --size;
18739 return val;
18742 /* Writes wide_int values to dw_vec_const array. */
18744 static void
18745 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
18747 int i;
18749 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
18751 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
18752 return;
18755 /* We'd have to extend this code to support odd sizes. */
18756 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
18758 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
18760 if (WORDS_BIG_ENDIAN)
18761 for (i = n - 1; i >= 0; i--)
18763 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18764 dest += sizeof (HOST_WIDE_INT);
18766 else
18767 for (i = 0; i < n; i++)
18769 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
18770 dest += sizeof (HOST_WIDE_INT);
18774 /* Writes floating point values to dw_vec_const array. */
18776 static void
18777 insert_float (const_rtx rtl, unsigned char *array)
18779 long val[4];
18780 int i;
18781 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18783 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), mode);
18785 /* real_to_target puts 32-bit pieces in each long. Pack them. */
18786 for (i = 0; i < GET_MODE_SIZE (mode) / 4; i++)
18788 insert_int (val[i], 4, array);
18789 array += 4;
18793 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
18794 does not have a "location" either in memory or in a register. These
18795 things can arise in GNU C when a constant is passed as an actual parameter
18796 to an inlined function. They can also arise in C++ where declared
18797 constants do not necessarily get memory "homes". */
18799 static bool
18800 add_const_value_attribute (dw_die_ref die, rtx rtl)
18802 switch (GET_CODE (rtl))
18804 case CONST_INT:
18806 HOST_WIDE_INT val = INTVAL (rtl);
18808 if (val < 0)
18809 add_AT_int (die, DW_AT_const_value, val);
18810 else
18811 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
18813 return true;
18815 case CONST_WIDE_INT:
18817 wide_int w1 = rtx_mode_t (rtl, MAX_MODE_INT);
18818 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
18819 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
18820 wide_int w = wi::zext (w1, prec);
18821 add_AT_wide (die, DW_AT_const_value, w);
18823 return true;
18825 case CONST_DOUBLE:
18826 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
18827 floating-point constant. A CONST_DOUBLE is used whenever the
18828 constant requires more than one word in order to be adequately
18829 represented. */
18830 if (TARGET_SUPPORTS_WIDE_INT == 0
18831 && !SCALAR_FLOAT_MODE_P (GET_MODE (rtl)))
18832 add_AT_double (die, DW_AT_const_value,
18833 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
18834 else
18836 scalar_float_mode mode = as_a <scalar_float_mode> (GET_MODE (rtl));
18837 unsigned int length = GET_MODE_SIZE (mode);
18838 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
18840 insert_float (rtl, array);
18841 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
18843 return true;
18845 case CONST_VECTOR:
18847 unsigned int length;
18848 if (!CONST_VECTOR_NUNITS (rtl).is_constant (&length))
18849 return false;
18851 machine_mode mode = GET_MODE (rtl);
18852 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
18853 unsigned char *array
18854 = ggc_vec_alloc<unsigned char> (length * elt_size);
18855 unsigned int i;
18856 unsigned char *p;
18857 machine_mode imode = GET_MODE_INNER (mode);
18859 switch (GET_MODE_CLASS (mode))
18861 case MODE_VECTOR_INT:
18862 for (i = 0, p = array; i < length; i++, p += elt_size)
18864 rtx elt = CONST_VECTOR_ELT (rtl, i);
18865 insert_wide_int (rtx_mode_t (elt, imode), p, elt_size);
18867 break;
18869 case MODE_VECTOR_FLOAT:
18870 for (i = 0, p = array; i < length; i++, p += elt_size)
18872 rtx elt = CONST_VECTOR_ELT (rtl, i);
18873 insert_float (elt, p);
18875 break;
18877 default:
18878 gcc_unreachable ();
18881 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
18883 return true;
18885 case CONST_STRING:
18886 if (dwarf_version >= 4 || !dwarf_strict)
18888 dw_loc_descr_ref loc_result;
18889 resolve_one_addr (&rtl);
18890 rtl_addr:
18891 loc_result = new_addr_loc_descr (rtl, dtprel_false);
18892 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
18893 add_AT_loc (die, DW_AT_location, loc_result);
18894 vec_safe_push (used_rtx_array, rtl);
18895 return true;
18897 return false;
18899 case CONST:
18900 if (CONSTANT_P (XEXP (rtl, 0)))
18901 return add_const_value_attribute (die, XEXP (rtl, 0));
18902 /* FALLTHROUGH */
18903 case SYMBOL_REF:
18904 if (!const_ok_for_output (rtl))
18905 return false;
18906 /* FALLTHROUGH */
18907 case LABEL_REF:
18908 if (dwarf_version >= 4 || !dwarf_strict)
18909 goto rtl_addr;
18910 return false;
18912 case PLUS:
18913 /* In cases where an inlined instance of an inline function is passed
18914 the address of an `auto' variable (which is local to the caller) we
18915 can get a situation where the DECL_RTL of the artificial local
18916 variable (for the inlining) which acts as a stand-in for the
18917 corresponding formal parameter (of the inline function) will look
18918 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
18919 exactly a compile-time constant expression, but it isn't the address
18920 of the (artificial) local variable either. Rather, it represents the
18921 *value* which the artificial local variable always has during its
18922 lifetime. We currently have no way to represent such quasi-constant
18923 values in Dwarf, so for now we just punt and generate nothing. */
18924 return false;
18926 case HIGH:
18927 case CONST_FIXED:
18928 return false;
18930 case MEM:
18931 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
18932 && MEM_READONLY_P (rtl)
18933 && GET_MODE (rtl) == BLKmode)
18935 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
18936 return true;
18938 return false;
18940 default:
18941 /* No other kinds of rtx should be possible here. */
18942 gcc_unreachable ();
18944 return false;
18947 /* Determine whether the evaluation of EXPR references any variables
18948 or functions which aren't otherwise used (and therefore may not be
18949 output). */
18950 static tree
18951 reference_to_unused (tree * tp, int * walk_subtrees,
18952 void * data ATTRIBUTE_UNUSED)
18954 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
18955 *walk_subtrees = 0;
18957 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
18958 && ! TREE_ASM_WRITTEN (*tp))
18959 return *tp;
18960 /* ??? The C++ FE emits debug information for using decls, so
18961 putting gcc_unreachable here falls over. See PR31899. For now
18962 be conservative. */
18963 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
18964 return *tp;
18965 else if (VAR_P (*tp))
18967 varpool_node *node = varpool_node::get (*tp);
18968 if (!node || !node->definition)
18969 return *tp;
18971 else if (TREE_CODE (*tp) == FUNCTION_DECL
18972 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
18974 /* The call graph machinery must have finished analyzing,
18975 optimizing and gimplifying the CU by now.
18976 So if *TP has no call graph node associated
18977 to it, it means *TP will not be emitted. */
18978 if (!cgraph_node::get (*tp))
18979 return *tp;
18981 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
18982 return *tp;
18984 return NULL_TREE;
18987 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
18988 for use in a later add_const_value_attribute call. */
18990 static rtx
18991 rtl_for_decl_init (tree init, tree type)
18993 rtx rtl = NULL_RTX;
18995 STRIP_NOPS (init);
18997 /* If a variable is initialized with a string constant without embedded
18998 zeros, build CONST_STRING. */
18999 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
19001 tree enttype = TREE_TYPE (type);
19002 tree domain = TYPE_DOMAIN (type);
19003 scalar_int_mode mode;
19005 if (is_int_mode (TYPE_MODE (enttype), &mode)
19006 && GET_MODE_SIZE (mode) == 1
19007 && domain
19008 && integer_zerop (TYPE_MIN_VALUE (domain))
19009 && compare_tree_int (TYPE_MAX_VALUE (domain),
19010 TREE_STRING_LENGTH (init) - 1) == 0
19011 && ((size_t) TREE_STRING_LENGTH (init)
19012 == strlen (TREE_STRING_POINTER (init)) + 1))
19014 rtl = gen_rtx_CONST_STRING (VOIDmode,
19015 ggc_strdup (TREE_STRING_POINTER (init)));
19016 rtl = gen_rtx_MEM (BLKmode, rtl);
19017 MEM_READONLY_P (rtl) = 1;
19020 /* Other aggregates, and complex values, could be represented using
19021 CONCAT: FIXME! */
19022 else if (AGGREGATE_TYPE_P (type)
19023 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
19024 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
19025 || TREE_CODE (type) == COMPLEX_TYPE)
19027 /* Vectors only work if their mode is supported by the target.
19028 FIXME: generic vectors ought to work too. */
19029 else if (TREE_CODE (type) == VECTOR_TYPE
19030 && !VECTOR_MODE_P (TYPE_MODE (type)))
19032 /* If the initializer is something that we know will expand into an
19033 immediate RTL constant, expand it now. We must be careful not to
19034 reference variables which won't be output. */
19035 else if (initializer_constant_valid_p (init, type)
19036 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
19038 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
19039 possible. */
19040 if (TREE_CODE (type) == VECTOR_TYPE)
19041 switch (TREE_CODE (init))
19043 case VECTOR_CST:
19044 break;
19045 case CONSTRUCTOR:
19046 if (TREE_CONSTANT (init))
19048 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
19049 bool constant_p = true;
19050 tree value;
19051 unsigned HOST_WIDE_INT ix;
19053 /* Even when ctor is constant, it might contain non-*_CST
19054 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
19055 belong into VECTOR_CST nodes. */
19056 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
19057 if (!CONSTANT_CLASS_P (value))
19059 constant_p = false;
19060 break;
19063 if (constant_p)
19065 init = build_vector_from_ctor (type, elts);
19066 break;
19069 /* FALLTHRU */
19071 default:
19072 return NULL;
19075 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
19077 /* If expand_expr returns a MEM, it wasn't immediate. */
19078 gcc_assert (!rtl || !MEM_P (rtl));
19081 return rtl;
19084 /* Generate RTL for the variable DECL to represent its location. */
19086 static rtx
19087 rtl_for_decl_location (tree decl)
19089 rtx rtl;
19091 /* Here we have to decide where we are going to say the parameter "lives"
19092 (as far as the debugger is concerned). We only have a couple of
19093 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
19095 DECL_RTL normally indicates where the parameter lives during most of the
19096 activation of the function. If optimization is enabled however, this
19097 could be either NULL or else a pseudo-reg. Both of those cases indicate
19098 that the parameter doesn't really live anywhere (as far as the code
19099 generation parts of GCC are concerned) during most of the function's
19100 activation. That will happen (for example) if the parameter is never
19101 referenced within the function.
19103 We could just generate a location descriptor here for all non-NULL
19104 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
19105 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
19106 where DECL_RTL is NULL or is a pseudo-reg.
19108 Note however that we can only get away with using DECL_INCOMING_RTL as
19109 a backup substitute for DECL_RTL in certain limited cases. In cases
19110 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
19111 we can be sure that the parameter was passed using the same type as it is
19112 declared to have within the function, and that its DECL_INCOMING_RTL
19113 points us to a place where a value of that type is passed.
19115 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
19116 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
19117 because in these cases DECL_INCOMING_RTL points us to a value of some
19118 type which is *different* from the type of the parameter itself. Thus,
19119 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
19120 such cases, the debugger would end up (for example) trying to fetch a
19121 `float' from a place which actually contains the first part of a
19122 `double'. That would lead to really incorrect and confusing
19123 output at debug-time.
19125 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
19126 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
19127 are a couple of exceptions however. On little-endian machines we can
19128 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
19129 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
19130 an integral type that is smaller than TREE_TYPE (decl). These cases arise
19131 when (on a little-endian machine) a non-prototyped function has a
19132 parameter declared to be of type `short' or `char'. In such cases,
19133 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
19134 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
19135 passed `int' value. If the debugger then uses that address to fetch
19136 a `short' or a `char' (on a little-endian machine) the result will be
19137 the correct data, so we allow for such exceptional cases below.
19139 Note that our goal here is to describe the place where the given formal
19140 parameter lives during most of the function's activation (i.e. between the
19141 end of the prologue and the start of the epilogue). We'll do that as best
19142 as we can. Note however that if the given formal parameter is modified
19143 sometime during the execution of the function, then a stack backtrace (at
19144 debug-time) will show the function as having been called with the *new*
19145 value rather than the value which was originally passed in. This happens
19146 rarely enough that it is not a major problem, but it *is* a problem, and
19147 I'd like to fix it.
19149 A future version of dwarf2out.c may generate two additional attributes for
19150 any given DW_TAG_formal_parameter DIE which will describe the "passed
19151 type" and the "passed location" for the given formal parameter in addition
19152 to the attributes we now generate to indicate the "declared type" and the
19153 "active location" for each parameter. This additional set of attributes
19154 could be used by debuggers for stack backtraces. Separately, note that
19155 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
19156 This happens (for example) for inlined-instances of inline function formal
19157 parameters which are never referenced. This really shouldn't be
19158 happening. All PARM_DECL nodes should get valid non-NULL
19159 DECL_INCOMING_RTL values. FIXME. */
19161 /* Use DECL_RTL as the "location" unless we find something better. */
19162 rtl = DECL_RTL_IF_SET (decl);
19164 /* When generating abstract instances, ignore everything except
19165 constants, symbols living in memory, and symbols living in
19166 fixed registers. */
19167 if (! reload_completed)
19169 if (rtl
19170 && (CONSTANT_P (rtl)
19171 || (MEM_P (rtl)
19172 && CONSTANT_P (XEXP (rtl, 0)))
19173 || (REG_P (rtl)
19174 && VAR_P (decl)
19175 && TREE_STATIC (decl))))
19177 rtl = targetm.delegitimize_address (rtl);
19178 return rtl;
19180 rtl = NULL_RTX;
19182 else if (TREE_CODE (decl) == PARM_DECL)
19184 if (rtl == NULL_RTX
19185 || is_pseudo_reg (rtl)
19186 || (MEM_P (rtl)
19187 && is_pseudo_reg (XEXP (rtl, 0))
19188 && DECL_INCOMING_RTL (decl)
19189 && MEM_P (DECL_INCOMING_RTL (decl))
19190 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
19192 tree declared_type = TREE_TYPE (decl);
19193 tree passed_type = DECL_ARG_TYPE (decl);
19194 machine_mode dmode = TYPE_MODE (declared_type);
19195 machine_mode pmode = TYPE_MODE (passed_type);
19197 /* This decl represents a formal parameter which was optimized out.
19198 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
19199 all cases where (rtl == NULL_RTX) just below. */
19200 if (dmode == pmode)
19201 rtl = DECL_INCOMING_RTL (decl);
19202 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
19203 && SCALAR_INT_MODE_P (dmode)
19204 && known_le (GET_MODE_SIZE (dmode), GET_MODE_SIZE (pmode))
19205 && DECL_INCOMING_RTL (decl))
19207 rtx inc = DECL_INCOMING_RTL (decl);
19208 if (REG_P (inc))
19209 rtl = inc;
19210 else if (MEM_P (inc))
19212 if (BYTES_BIG_ENDIAN)
19213 rtl = adjust_address_nv (inc, dmode,
19214 GET_MODE_SIZE (pmode)
19215 - GET_MODE_SIZE (dmode));
19216 else
19217 rtl = inc;
19222 /* If the parm was passed in registers, but lives on the stack, then
19223 make a big endian correction if the mode of the type of the
19224 parameter is not the same as the mode of the rtl. */
19225 /* ??? This is the same series of checks that are made in dbxout.c before
19226 we reach the big endian correction code there. It isn't clear if all
19227 of these checks are necessary here, but keeping them all is the safe
19228 thing to do. */
19229 else if (MEM_P (rtl)
19230 && XEXP (rtl, 0) != const0_rtx
19231 && ! CONSTANT_P (XEXP (rtl, 0))
19232 /* Not passed in memory. */
19233 && !MEM_P (DECL_INCOMING_RTL (decl))
19234 /* Not passed by invisible reference. */
19235 && (!REG_P (XEXP (rtl, 0))
19236 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
19237 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
19238 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
19239 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
19240 #endif
19242 /* Big endian correction check. */
19243 && BYTES_BIG_ENDIAN
19244 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
19245 && known_lt (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))),
19246 UNITS_PER_WORD))
19248 machine_mode addr_mode = get_address_mode (rtl);
19249 poly_int64 offset = (UNITS_PER_WORD
19250 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
19252 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19253 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19256 else if (VAR_P (decl)
19257 && rtl
19258 && MEM_P (rtl)
19259 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl)))
19261 machine_mode addr_mode = get_address_mode (rtl);
19262 poly_int64 offset = byte_lowpart_offset (TYPE_MODE (TREE_TYPE (decl)),
19263 GET_MODE (rtl));
19265 /* If a variable is declared "register" yet is smaller than
19266 a register, then if we store the variable to memory, it
19267 looks like we're storing a register-sized value, when in
19268 fact we are not. We need to adjust the offset of the
19269 storage location to reflect the actual value's bytes,
19270 else gdb will not be able to display it. */
19271 if (maybe_ne (offset, 0))
19272 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
19273 plus_constant (addr_mode, XEXP (rtl, 0), offset));
19276 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
19277 and will have been substituted directly into all expressions that use it.
19278 C does not have such a concept, but C++ and other languages do. */
19279 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
19280 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
19282 if (rtl)
19283 rtl = targetm.delegitimize_address (rtl);
19285 /* If we don't look past the constant pool, we risk emitting a
19286 reference to a constant pool entry that isn't referenced from
19287 code, and thus is not emitted. */
19288 if (rtl)
19289 rtl = avoid_constant_pool_reference (rtl);
19291 /* Try harder to get a rtl. If this symbol ends up not being emitted
19292 in the current CU, resolve_addr will remove the expression referencing
19293 it. */
19294 if (rtl == NULL_RTX
19295 && VAR_P (decl)
19296 && !DECL_EXTERNAL (decl)
19297 && TREE_STATIC (decl)
19298 && DECL_NAME (decl)
19299 && !DECL_HARD_REGISTER (decl)
19300 && DECL_MODE (decl) != VOIDmode)
19302 rtl = make_decl_rtl_for_debug (decl);
19303 if (!MEM_P (rtl)
19304 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
19305 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
19306 rtl = NULL_RTX;
19309 return rtl;
19312 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
19313 returned. If so, the decl for the COMMON block is returned, and the
19314 value is the offset into the common block for the symbol. */
19316 static tree
19317 fortran_common (tree decl, HOST_WIDE_INT *value)
19319 tree val_expr, cvar;
19320 machine_mode mode;
19321 poly_int64 bitsize, bitpos;
19322 tree offset;
19323 HOST_WIDE_INT cbitpos;
19324 int unsignedp, reversep, volatilep = 0;
19326 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
19327 it does not have a value (the offset into the common area), or if it
19328 is thread local (as opposed to global) then it isn't common, and shouldn't
19329 be handled as such. */
19330 if (!VAR_P (decl)
19331 || !TREE_STATIC (decl)
19332 || !DECL_HAS_VALUE_EXPR_P (decl)
19333 || !is_fortran ())
19334 return NULL_TREE;
19336 val_expr = DECL_VALUE_EXPR (decl);
19337 if (TREE_CODE (val_expr) != COMPONENT_REF)
19338 return NULL_TREE;
19340 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
19341 &unsignedp, &reversep, &volatilep);
19343 if (cvar == NULL_TREE
19344 || !VAR_P (cvar)
19345 || DECL_ARTIFICIAL (cvar)
19346 || !TREE_PUBLIC (cvar)
19347 /* We don't expect to have to cope with variable offsets,
19348 since at present all static data must have a constant size. */
19349 || !bitpos.is_constant (&cbitpos))
19350 return NULL_TREE;
19352 *value = 0;
19353 if (offset != NULL)
19355 if (!tree_fits_shwi_p (offset))
19356 return NULL_TREE;
19357 *value = tree_to_shwi (offset);
19359 if (cbitpos != 0)
19360 *value += cbitpos / BITS_PER_UNIT;
19362 return cvar;
19365 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
19366 data attribute for a variable or a parameter. We generate the
19367 DW_AT_const_value attribute only in those cases where the given variable
19368 or parameter does not have a true "location" either in memory or in a
19369 register. This can happen (for example) when a constant is passed as an
19370 actual argument in a call to an inline function. (It's possible that
19371 these things can crop up in other ways also.) Note that one type of
19372 constant value which can be passed into an inlined function is a constant
19373 pointer. This can happen for example if an actual argument in an inlined
19374 function call evaluates to a compile-time constant address.
19376 CACHE_P is true if it is worth caching the location list for DECL,
19377 so that future calls can reuse it rather than regenerate it from scratch.
19378 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
19379 since we will need to refer to them each time the function is inlined. */
19381 static bool
19382 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
19384 rtx rtl;
19385 dw_loc_list_ref list;
19386 var_loc_list *loc_list;
19387 cached_dw_loc_list *cache;
19389 if (early_dwarf)
19390 return false;
19392 if (TREE_CODE (decl) == ERROR_MARK)
19393 return false;
19395 if (get_AT (die, DW_AT_location)
19396 || get_AT (die, DW_AT_const_value))
19397 return true;
19399 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
19400 || TREE_CODE (decl) == RESULT_DECL);
19402 /* Try to get some constant RTL for this decl, and use that as the value of
19403 the location. */
19405 rtl = rtl_for_decl_location (decl);
19406 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19407 && add_const_value_attribute (die, rtl))
19408 return true;
19410 /* See if we have single element location list that is equivalent to
19411 a constant value. That way we are better to use add_const_value_attribute
19412 rather than expanding constant value equivalent. */
19413 loc_list = lookup_decl_loc (decl);
19414 if (loc_list
19415 && loc_list->first
19416 && loc_list->first->next == NULL
19417 && NOTE_P (loc_list->first->loc)
19418 && NOTE_VAR_LOCATION (loc_list->first->loc)
19419 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
19421 struct var_loc_node *node;
19423 node = loc_list->first;
19424 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
19425 if (GET_CODE (rtl) == EXPR_LIST)
19426 rtl = XEXP (rtl, 0);
19427 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
19428 && add_const_value_attribute (die, rtl))
19429 return true;
19431 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
19432 list several times. See if we've already cached the contents. */
19433 list = NULL;
19434 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
19435 cache_p = false;
19436 if (cache_p)
19438 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
19439 if (cache)
19440 list = cache->loc_list;
19442 if (list == NULL)
19444 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
19445 NULL);
19446 /* It is usually worth caching this result if the decl is from
19447 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
19448 if (cache_p && list && list->dw_loc_next)
19450 cached_dw_loc_list **slot
19451 = cached_dw_loc_list_table->find_slot_with_hash (decl,
19452 DECL_UID (decl),
19453 INSERT);
19454 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
19455 cache->decl_id = DECL_UID (decl);
19456 cache->loc_list = list;
19457 *slot = cache;
19460 if (list)
19462 add_AT_location_description (die, DW_AT_location, list);
19463 return true;
19465 /* None of that worked, so it must not really have a location;
19466 try adding a constant value attribute from the DECL_INITIAL. */
19467 return tree_add_const_value_attribute_for_decl (die, decl);
19470 /* Helper function for tree_add_const_value_attribute. Natively encode
19471 initializer INIT into an array. Return true if successful. */
19473 static bool
19474 native_encode_initializer (tree init, unsigned char *array, int size)
19476 tree type;
19478 if (init == NULL_TREE)
19479 return false;
19481 STRIP_NOPS (init);
19482 switch (TREE_CODE (init))
19484 case STRING_CST:
19485 type = TREE_TYPE (init);
19486 if (TREE_CODE (type) == ARRAY_TYPE)
19488 tree enttype = TREE_TYPE (type);
19489 scalar_int_mode mode;
19491 if (!is_int_mode (TYPE_MODE (enttype), &mode)
19492 || GET_MODE_SIZE (mode) != 1)
19493 return false;
19494 if (int_size_in_bytes (type) != size)
19495 return false;
19496 if (size > TREE_STRING_LENGTH (init))
19498 memcpy (array, TREE_STRING_POINTER (init),
19499 TREE_STRING_LENGTH (init));
19500 memset (array + TREE_STRING_LENGTH (init),
19501 '\0', size - TREE_STRING_LENGTH (init));
19503 else
19504 memcpy (array, TREE_STRING_POINTER (init), size);
19505 return true;
19507 return false;
19508 case CONSTRUCTOR:
19509 type = TREE_TYPE (init);
19510 if (int_size_in_bytes (type) != size)
19511 return false;
19512 if (TREE_CODE (type) == ARRAY_TYPE)
19514 HOST_WIDE_INT min_index;
19515 unsigned HOST_WIDE_INT cnt;
19516 int curpos = 0, fieldsize;
19517 constructor_elt *ce;
19519 if (TYPE_DOMAIN (type) == NULL_TREE
19520 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
19521 return false;
19523 fieldsize = int_size_in_bytes (TREE_TYPE (type));
19524 if (fieldsize <= 0)
19525 return false;
19527 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
19528 memset (array, '\0', size);
19529 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19531 tree val = ce->value;
19532 tree index = ce->index;
19533 int pos = curpos;
19534 if (index && TREE_CODE (index) == RANGE_EXPR)
19535 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
19536 * fieldsize;
19537 else if (index)
19538 pos = (tree_to_shwi (index) - min_index) * fieldsize;
19540 if (val)
19542 STRIP_NOPS (val);
19543 if (!native_encode_initializer (val, array + pos, fieldsize))
19544 return false;
19546 curpos = pos + fieldsize;
19547 if (index && TREE_CODE (index) == RANGE_EXPR)
19549 int count = tree_to_shwi (TREE_OPERAND (index, 1))
19550 - tree_to_shwi (TREE_OPERAND (index, 0));
19551 while (count-- > 0)
19553 if (val)
19554 memcpy (array + curpos, array + pos, fieldsize);
19555 curpos += fieldsize;
19558 gcc_assert (curpos <= size);
19560 return true;
19562 else if (TREE_CODE (type) == RECORD_TYPE
19563 || TREE_CODE (type) == UNION_TYPE)
19565 tree field = NULL_TREE;
19566 unsigned HOST_WIDE_INT cnt;
19567 constructor_elt *ce;
19569 if (int_size_in_bytes (type) != size)
19570 return false;
19572 if (TREE_CODE (type) == RECORD_TYPE)
19573 field = TYPE_FIELDS (type);
19575 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
19577 tree val = ce->value;
19578 int pos, fieldsize;
19580 if (ce->index != 0)
19581 field = ce->index;
19583 if (val)
19584 STRIP_NOPS (val);
19586 if (field == NULL_TREE || DECL_BIT_FIELD (field))
19587 return false;
19589 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
19590 && TYPE_DOMAIN (TREE_TYPE (field))
19591 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
19592 return false;
19593 else if (DECL_SIZE_UNIT (field) == NULL_TREE
19594 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
19595 return false;
19596 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
19597 pos = int_byte_position (field);
19598 gcc_assert (pos + fieldsize <= size);
19599 if (val && fieldsize != 0
19600 && !native_encode_initializer (val, array + pos, fieldsize))
19601 return false;
19603 return true;
19605 return false;
19606 case VIEW_CONVERT_EXPR:
19607 case NON_LVALUE_EXPR:
19608 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
19609 default:
19610 return native_encode_expr (init, array, size) == size;
19614 /* Attach a DW_AT_const_value attribute to DIE. The value of the
19615 attribute is the const value T. */
19617 static bool
19618 tree_add_const_value_attribute (dw_die_ref die, tree t)
19620 tree init;
19621 tree type = TREE_TYPE (t);
19622 rtx rtl;
19624 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
19625 return false;
19627 init = t;
19628 gcc_assert (!DECL_P (init));
19630 if (TREE_CODE (init) == INTEGER_CST)
19632 if (tree_fits_uhwi_p (init))
19634 add_AT_unsigned (die, DW_AT_const_value, tree_to_uhwi (init));
19635 return true;
19637 if (tree_fits_shwi_p (init))
19639 add_AT_int (die, DW_AT_const_value, tree_to_shwi (init));
19640 return true;
19643 if (! early_dwarf)
19645 rtl = rtl_for_decl_init (init, type);
19646 if (rtl)
19647 return add_const_value_attribute (die, rtl);
19649 /* If the host and target are sane, try harder. */
19650 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
19651 && initializer_constant_valid_p (init, type))
19653 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
19654 if (size > 0 && (int) size == size)
19656 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
19658 if (native_encode_initializer (init, array, size))
19660 add_AT_vec (die, DW_AT_const_value, size, 1, array);
19661 return true;
19663 ggc_free (array);
19666 return false;
19669 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
19670 attribute is the const value of T, where T is an integral constant
19671 variable with static storage duration
19672 (so it can't be a PARM_DECL or a RESULT_DECL). */
19674 static bool
19675 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
19678 if (!decl
19679 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
19680 || (VAR_P (decl) && !TREE_STATIC (decl)))
19681 return false;
19683 if (TREE_READONLY (decl)
19684 && ! TREE_THIS_VOLATILE (decl)
19685 && DECL_INITIAL (decl))
19686 /* OK */;
19687 else
19688 return false;
19690 /* Don't add DW_AT_const_value if abstract origin already has one. */
19691 if (get_AT (var_die, DW_AT_const_value))
19692 return false;
19694 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
19697 /* Convert the CFI instructions for the current function into a
19698 location list. This is used for DW_AT_frame_base when we targeting
19699 a dwarf2 consumer that does not support the dwarf3
19700 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
19701 expressions. */
19703 static dw_loc_list_ref
19704 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
19706 int ix;
19707 dw_fde_ref fde;
19708 dw_loc_list_ref list, *list_tail;
19709 dw_cfi_ref cfi;
19710 dw_cfa_location last_cfa, next_cfa;
19711 const char *start_label, *last_label, *section;
19712 dw_cfa_location remember;
19714 fde = cfun->fde;
19715 gcc_assert (fde != NULL);
19717 section = secname_for_decl (current_function_decl);
19718 list_tail = &list;
19719 list = NULL;
19721 memset (&next_cfa, 0, sizeof (next_cfa));
19722 next_cfa.reg = INVALID_REGNUM;
19723 remember = next_cfa;
19725 start_label = fde->dw_fde_begin;
19727 /* ??? Bald assumption that the CIE opcode list does not contain
19728 advance opcodes. */
19729 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
19730 lookup_cfa_1 (cfi, &next_cfa, &remember);
19732 last_cfa = next_cfa;
19733 last_label = start_label;
19735 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
19737 /* If the first partition contained no CFI adjustments, the
19738 CIE opcodes apply to the whole first partition. */
19739 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19740 fde->dw_fde_begin, fde->dw_fde_end, section);
19741 list_tail =&(*list_tail)->dw_loc_next;
19742 start_label = last_label = fde->dw_fde_second_begin;
19745 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
19747 switch (cfi->dw_cfi_opc)
19749 case DW_CFA_set_loc:
19750 case DW_CFA_advance_loc1:
19751 case DW_CFA_advance_loc2:
19752 case DW_CFA_advance_loc4:
19753 if (!cfa_equal_p (&last_cfa, &next_cfa))
19755 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19756 start_label, last_label, section);
19758 list_tail = &(*list_tail)->dw_loc_next;
19759 last_cfa = next_cfa;
19760 start_label = last_label;
19762 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
19763 break;
19765 case DW_CFA_advance_loc:
19766 /* The encoding is complex enough that we should never emit this. */
19767 gcc_unreachable ();
19769 default:
19770 lookup_cfa_1 (cfi, &next_cfa, &remember);
19771 break;
19773 if (ix + 1 == fde->dw_fde_switch_cfi_index)
19775 if (!cfa_equal_p (&last_cfa, &next_cfa))
19777 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19778 start_label, last_label, section);
19780 list_tail = &(*list_tail)->dw_loc_next;
19781 last_cfa = next_cfa;
19782 start_label = last_label;
19784 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19785 start_label, fde->dw_fde_end, section);
19786 list_tail = &(*list_tail)->dw_loc_next;
19787 start_label = last_label = fde->dw_fde_second_begin;
19791 if (!cfa_equal_p (&last_cfa, &next_cfa))
19793 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
19794 start_label, last_label, section);
19795 list_tail = &(*list_tail)->dw_loc_next;
19796 start_label = last_label;
19799 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
19800 start_label,
19801 fde->dw_fde_second_begin
19802 ? fde->dw_fde_second_end : fde->dw_fde_end,
19803 section);
19805 if (list && list->dw_loc_next)
19806 gen_llsym (list);
19808 return list;
19811 /* Compute a displacement from the "steady-state frame pointer" to the
19812 frame base (often the same as the CFA), and store it in
19813 frame_pointer_fb_offset. OFFSET is added to the displacement
19814 before the latter is negated. */
19816 static void
19817 compute_frame_pointer_to_fb_displacement (poly_int64 offset)
19819 rtx reg, elim;
19821 #ifdef FRAME_POINTER_CFA_OFFSET
19822 reg = frame_pointer_rtx;
19823 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
19824 #else
19825 reg = arg_pointer_rtx;
19826 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
19827 #endif
19829 elim = (ira_use_lra_p
19830 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
19831 : eliminate_regs (reg, VOIDmode, NULL_RTX));
19832 elim = strip_offset_and_add (elim, &offset);
19834 frame_pointer_fb_offset = -offset;
19836 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
19837 in which to eliminate. This is because it's stack pointer isn't
19838 directly accessible as a register within the ISA. To work around
19839 this, assume that while we cannot provide a proper value for
19840 frame_pointer_fb_offset, we won't need one either. */
19841 frame_pointer_fb_offset_valid
19842 = ((SUPPORTS_STACK_ALIGNMENT
19843 && (elim == hard_frame_pointer_rtx
19844 || elim == stack_pointer_rtx))
19845 || elim == (frame_pointer_needed
19846 ? hard_frame_pointer_rtx
19847 : stack_pointer_rtx));
19850 /* Generate a DW_AT_name attribute given some string value to be included as
19851 the value of the attribute. */
19853 static void
19854 add_name_attribute (dw_die_ref die, const char *name_string)
19856 if (name_string != NULL && *name_string != 0)
19858 if (demangle_name_func)
19859 name_string = (*demangle_name_func) (name_string);
19861 add_AT_string (die, DW_AT_name, name_string);
19865 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
19866 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
19867 of TYPE accordingly.
19869 ??? This is a temporary measure until after we're able to generate
19870 regular DWARF for the complex Ada type system. */
19872 static void
19873 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
19874 dw_die_ref context_die)
19876 tree dtype;
19877 dw_die_ref dtype_die;
19879 if (!lang_hooks.types.descriptive_type)
19880 return;
19882 dtype = lang_hooks.types.descriptive_type (type);
19883 if (!dtype)
19884 return;
19886 dtype_die = lookup_type_die (dtype);
19887 if (!dtype_die)
19889 gen_type_die (dtype, context_die);
19890 dtype_die = lookup_type_die (dtype);
19891 gcc_assert (dtype_die);
19894 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
19897 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
19899 static const char *
19900 comp_dir_string (void)
19902 const char *wd;
19903 char *wd1;
19904 static const char *cached_wd = NULL;
19906 if (cached_wd != NULL)
19907 return cached_wd;
19909 wd = get_src_pwd ();
19910 if (wd == NULL)
19911 return NULL;
19913 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
19915 int wdlen;
19917 wdlen = strlen (wd);
19918 wd1 = ggc_vec_alloc<char> (wdlen + 2);
19919 strcpy (wd1, wd);
19920 wd1 [wdlen] = DIR_SEPARATOR;
19921 wd1 [wdlen + 1] = 0;
19922 wd = wd1;
19925 cached_wd = remap_debug_filename (wd);
19926 return cached_wd;
19929 /* Generate a DW_AT_comp_dir attribute for DIE. */
19931 static void
19932 add_comp_dir_attribute (dw_die_ref die)
19934 const char * wd = comp_dir_string ();
19935 if (wd != NULL)
19936 add_AT_string (die, DW_AT_comp_dir, wd);
19939 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
19940 pointer computation, ...), output a representation for that bound according
19941 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
19942 loc_list_from_tree for the meaning of CONTEXT. */
19944 static void
19945 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
19946 int forms, struct loc_descr_context *context)
19948 dw_die_ref context_die, decl_die;
19949 dw_loc_list_ref list;
19950 bool strip_conversions = true;
19951 bool placeholder_seen = false;
19953 while (strip_conversions)
19954 switch (TREE_CODE (value))
19956 case ERROR_MARK:
19957 case SAVE_EXPR:
19958 return;
19960 CASE_CONVERT:
19961 case VIEW_CONVERT_EXPR:
19962 value = TREE_OPERAND (value, 0);
19963 break;
19965 default:
19966 strip_conversions = false;
19967 break;
19970 /* If possible and permitted, output the attribute as a constant. */
19971 if ((forms & dw_scalar_form_constant) != 0
19972 && TREE_CODE (value) == INTEGER_CST)
19974 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
19976 /* If HOST_WIDE_INT is big enough then represent the bound as
19977 a constant value. We need to choose a form based on
19978 whether the type is signed or unsigned. We cannot just
19979 call add_AT_unsigned if the value itself is positive
19980 (add_AT_unsigned might add the unsigned value encoded as
19981 DW_FORM_data[1248]). Some DWARF consumers will lookup the
19982 bounds type and then sign extend any unsigned values found
19983 for signed types. This is needed only for
19984 DW_AT_{lower,upper}_bound, since for most other attributes,
19985 consumers will treat DW_FORM_data[1248] as unsigned values,
19986 regardless of the underlying type. */
19987 if (prec <= HOST_BITS_PER_WIDE_INT
19988 || tree_fits_uhwi_p (value))
19990 if (TYPE_UNSIGNED (TREE_TYPE (value)))
19991 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
19992 else
19993 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
19995 else
19996 /* Otherwise represent the bound as an unsigned value with
19997 the precision of its type. The precision and signedness
19998 of the type will be necessary to re-interpret it
19999 unambiguously. */
20000 add_AT_wide (die, attr, wi::to_wide (value));
20001 return;
20004 /* Otherwise, if it's possible and permitted too, output a reference to
20005 another DIE. */
20006 if ((forms & dw_scalar_form_reference) != 0)
20008 tree decl = NULL_TREE;
20010 /* Some type attributes reference an outer type. For instance, the upper
20011 bound of an array may reference an embedding record (this happens in
20012 Ada). */
20013 if (TREE_CODE (value) == COMPONENT_REF
20014 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
20015 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
20016 decl = TREE_OPERAND (value, 1);
20018 else if (VAR_P (value)
20019 || TREE_CODE (value) == PARM_DECL
20020 || TREE_CODE (value) == RESULT_DECL)
20021 decl = value;
20023 if (decl != NULL_TREE)
20025 dw_die_ref decl_die = lookup_decl_die (decl);
20027 /* ??? Can this happen, or should the variable have been bound
20028 first? Probably it can, since I imagine that we try to create
20029 the types of parameters in the order in which they exist in
20030 the list, and won't have created a forward reference to a
20031 later parameter. */
20032 if (decl_die != NULL)
20034 add_AT_die_ref (die, attr, decl_die);
20035 return;
20040 /* Last chance: try to create a stack operation procedure to evaluate the
20041 value. Do nothing if even that is not possible or permitted. */
20042 if ((forms & dw_scalar_form_exprloc) == 0)
20043 return;
20045 list = loc_list_from_tree (value, 2, context);
20046 if (context && context->placeholder_arg)
20048 placeholder_seen = context->placeholder_seen;
20049 context->placeholder_seen = false;
20051 if (list == NULL || single_element_loc_list_p (list))
20053 /* If this attribute is not a reference nor constant, it is
20054 a DWARF expression rather than location description. For that
20055 loc_list_from_tree (value, 0, &context) is needed. */
20056 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
20057 if (list2 && single_element_loc_list_p (list2))
20059 if (placeholder_seen)
20061 struct dwarf_procedure_info dpi;
20062 dpi.fndecl = NULL_TREE;
20063 dpi.args_count = 1;
20064 if (!resolve_args_picking (list2->expr, 1, &dpi))
20065 return;
20067 add_AT_loc (die, attr, list2->expr);
20068 return;
20072 /* If that failed to give a single element location list, fall back to
20073 outputting this as a reference... still if permitted. */
20074 if (list == NULL
20075 || (forms & dw_scalar_form_reference) == 0
20076 || placeholder_seen)
20077 return;
20079 if (current_function_decl == 0)
20080 context_die = comp_unit_die ();
20081 else
20082 context_die = lookup_decl_die (current_function_decl);
20084 decl_die = new_die (DW_TAG_variable, context_die, value);
20085 add_AT_flag (decl_die, DW_AT_artificial, 1);
20086 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
20087 context_die);
20088 add_AT_location_description (decl_die, DW_AT_location, list);
20089 add_AT_die_ref (die, attr, decl_die);
20092 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
20093 default. */
20095 static int
20096 lower_bound_default (void)
20098 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20100 case DW_LANG_C:
20101 case DW_LANG_C89:
20102 case DW_LANG_C99:
20103 case DW_LANG_C11:
20104 case DW_LANG_C_plus_plus:
20105 case DW_LANG_C_plus_plus_11:
20106 case DW_LANG_C_plus_plus_14:
20107 case DW_LANG_ObjC:
20108 case DW_LANG_ObjC_plus_plus:
20109 return 0;
20110 case DW_LANG_Fortran77:
20111 case DW_LANG_Fortran90:
20112 case DW_LANG_Fortran95:
20113 case DW_LANG_Fortran03:
20114 case DW_LANG_Fortran08:
20115 return 1;
20116 case DW_LANG_UPC:
20117 case DW_LANG_D:
20118 case DW_LANG_Python:
20119 return dwarf_version >= 4 ? 0 : -1;
20120 case DW_LANG_Ada95:
20121 case DW_LANG_Ada83:
20122 case DW_LANG_Cobol74:
20123 case DW_LANG_Cobol85:
20124 case DW_LANG_Modula2:
20125 case DW_LANG_PLI:
20126 return dwarf_version >= 4 ? 1 : -1;
20127 default:
20128 return -1;
20132 /* Given a tree node describing an array bound (either lower or upper) output
20133 a representation for that bound. */
20135 static void
20136 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
20137 tree bound, struct loc_descr_context *context)
20139 int dflt;
20141 while (1)
20142 switch (TREE_CODE (bound))
20144 /* Strip all conversions. */
20145 CASE_CONVERT:
20146 case VIEW_CONVERT_EXPR:
20147 bound = TREE_OPERAND (bound, 0);
20148 break;
20150 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
20151 are even omitted when they are the default. */
20152 case INTEGER_CST:
20153 /* If the value for this bound is the default one, we can even omit the
20154 attribute. */
20155 if (bound_attr == DW_AT_lower_bound
20156 && tree_fits_shwi_p (bound)
20157 && (dflt = lower_bound_default ()) != -1
20158 && tree_to_shwi (bound) == dflt)
20159 return;
20161 /* FALLTHRU */
20163 default:
20164 /* Because of the complex interaction there can be with other GNAT
20165 encodings, GDB isn't ready yet to handle proper DWARF description
20166 for self-referencial subrange bounds: let GNAT encodings do the
20167 magic in such a case. */
20168 if (is_ada ()
20169 && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
20170 && contains_placeholder_p (bound))
20171 return;
20173 add_scalar_info (subrange_die, bound_attr, bound,
20174 dw_scalar_form_constant
20175 | dw_scalar_form_exprloc
20176 | dw_scalar_form_reference,
20177 context);
20178 return;
20182 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
20183 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
20184 Note that the block of subscript information for an array type also
20185 includes information about the element type of the given array type.
20187 This function reuses previously set type and bound information if
20188 available. */
20190 static void
20191 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
20193 unsigned dimension_number;
20194 tree lower, upper;
20195 dw_die_ref child = type_die->die_child;
20197 for (dimension_number = 0;
20198 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
20199 type = TREE_TYPE (type), dimension_number++)
20201 tree domain = TYPE_DOMAIN (type);
20203 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
20204 break;
20206 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
20207 and (in GNU C only) variable bounds. Handle all three forms
20208 here. */
20210 /* Find and reuse a previously generated DW_TAG_subrange_type if
20211 available.
20213 For multi-dimensional arrays, as we iterate through the
20214 various dimensions in the enclosing for loop above, we also
20215 iterate through the DIE children and pick at each
20216 DW_TAG_subrange_type previously generated (if available).
20217 Each child DW_TAG_subrange_type DIE describes the range of
20218 the current dimension. At this point we should have as many
20219 DW_TAG_subrange_type's as we have dimensions in the
20220 array. */
20221 dw_die_ref subrange_die = NULL;
20222 if (child)
20223 while (1)
20225 child = child->die_sib;
20226 if (child->die_tag == DW_TAG_subrange_type)
20227 subrange_die = child;
20228 if (child == type_die->die_child)
20230 /* If we wrapped around, stop looking next time. */
20231 child = NULL;
20232 break;
20234 if (child->die_tag == DW_TAG_subrange_type)
20235 break;
20237 if (!subrange_die)
20238 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
20240 if (domain)
20242 /* We have an array type with specified bounds. */
20243 lower = TYPE_MIN_VALUE (domain);
20244 upper = TYPE_MAX_VALUE (domain);
20246 /* Define the index type. */
20247 if (TREE_TYPE (domain)
20248 && !get_AT (subrange_die, DW_AT_type))
20250 /* ??? This is probably an Ada unnamed subrange type. Ignore the
20251 TREE_TYPE field. We can't emit debug info for this
20252 because it is an unnamed integral type. */
20253 if (TREE_CODE (domain) == INTEGER_TYPE
20254 && TYPE_NAME (domain) == NULL_TREE
20255 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
20256 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
20258 else
20259 add_type_attribute (subrange_die, TREE_TYPE (domain),
20260 TYPE_UNQUALIFIED, false, type_die);
20263 /* ??? If upper is NULL, the array has unspecified length,
20264 but it does have a lower bound. This happens with Fortran
20265 dimension arr(N:*)
20266 Since the debugger is definitely going to need to know N
20267 to produce useful results, go ahead and output the lower
20268 bound solo, and hope the debugger can cope. */
20270 if (!get_AT (subrange_die, DW_AT_lower_bound))
20271 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
20272 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
20273 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
20276 /* Otherwise we have an array type with an unspecified length. The
20277 DWARF-2 spec does not say how to handle this; let's just leave out the
20278 bounds. */
20282 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
20284 static void
20285 add_byte_size_attribute (dw_die_ref die, tree tree_node)
20287 dw_die_ref decl_die;
20288 HOST_WIDE_INT size;
20289 dw_loc_descr_ref size_expr = NULL;
20291 switch (TREE_CODE (tree_node))
20293 case ERROR_MARK:
20294 size = 0;
20295 break;
20296 case ENUMERAL_TYPE:
20297 case RECORD_TYPE:
20298 case UNION_TYPE:
20299 case QUAL_UNION_TYPE:
20300 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
20301 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
20303 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
20304 return;
20306 size_expr = type_byte_size (tree_node, &size);
20307 break;
20308 case FIELD_DECL:
20309 /* For a data member of a struct or union, the DW_AT_byte_size is
20310 generally given as the number of bytes normally allocated for an
20311 object of the *declared* type of the member itself. This is true
20312 even for bit-fields. */
20313 size = int_size_in_bytes (field_type (tree_node));
20314 break;
20315 default:
20316 gcc_unreachable ();
20319 /* Support for dynamically-sized objects was introduced by DWARFv3.
20320 At the moment, GDB does not handle variable byte sizes very well,
20321 though. */
20322 if ((dwarf_version >= 3 || !dwarf_strict)
20323 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
20324 && size_expr != NULL)
20325 add_AT_loc (die, DW_AT_byte_size, size_expr);
20327 /* Note that `size' might be -1 when we get to this point. If it is, that
20328 indicates that the byte size of the entity in question is variable and
20329 that we could not generate a DWARF expression that computes it. */
20330 if (size >= 0)
20331 add_AT_unsigned (die, DW_AT_byte_size, size);
20334 /* Add a DW_AT_alignment attribute to DIE with TREE_NODE's non-default
20335 alignment. */
20337 static void
20338 add_alignment_attribute (dw_die_ref die, tree tree_node)
20340 if (dwarf_version < 5 && dwarf_strict)
20341 return;
20343 unsigned align;
20345 if (DECL_P (tree_node))
20347 if (!DECL_USER_ALIGN (tree_node))
20348 return;
20350 align = DECL_ALIGN_UNIT (tree_node);
20352 else if (TYPE_P (tree_node))
20354 if (!TYPE_USER_ALIGN (tree_node))
20355 return;
20357 align = TYPE_ALIGN_UNIT (tree_node);
20359 else
20360 gcc_unreachable ();
20362 add_AT_unsigned (die, DW_AT_alignment, align);
20365 /* For a FIELD_DECL node which represents a bit-field, output an attribute
20366 which specifies the distance in bits from the highest order bit of the
20367 "containing object" for the bit-field to the highest order bit of the
20368 bit-field itself.
20370 For any given bit-field, the "containing object" is a hypothetical object
20371 (of some integral or enum type) within which the given bit-field lives. The
20372 type of this hypothetical "containing object" is always the same as the
20373 declared type of the individual bit-field itself. The determination of the
20374 exact location of the "containing object" for a bit-field is rather
20375 complicated. It's handled by the `field_byte_offset' function (above).
20377 CTX is required: see the comment for VLR_CONTEXT.
20379 Note that it is the size (in bytes) of the hypothetical "containing object"
20380 which will be given in the DW_AT_byte_size attribute for this bit-field.
20381 (See `byte_size_attribute' above). */
20383 static inline void
20384 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
20386 HOST_WIDE_INT object_offset_in_bytes;
20387 tree original_type = DECL_BIT_FIELD_TYPE (decl);
20388 HOST_WIDE_INT bitpos_int;
20389 HOST_WIDE_INT highest_order_object_bit_offset;
20390 HOST_WIDE_INT highest_order_field_bit_offset;
20391 HOST_WIDE_INT bit_offset;
20393 field_byte_offset (decl, ctx, &object_offset_in_bytes);
20395 /* Must be a field and a bit field. */
20396 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
20398 /* We can't yet handle bit-fields whose offsets are variable, so if we
20399 encounter such things, just return without generating any attribute
20400 whatsoever. Likewise for variable or too large size. */
20401 if (! tree_fits_shwi_p (bit_position (decl))
20402 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
20403 return;
20405 bitpos_int = int_bit_position (decl);
20407 /* Note that the bit offset is always the distance (in bits) from the
20408 highest-order bit of the "containing object" to the highest-order bit of
20409 the bit-field itself. Since the "high-order end" of any object or field
20410 is different on big-endian and little-endian machines, the computation
20411 below must take account of these differences. */
20412 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
20413 highest_order_field_bit_offset = bitpos_int;
20415 if (! BYTES_BIG_ENDIAN)
20417 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
20418 highest_order_object_bit_offset +=
20419 simple_type_size_in_bits (original_type);
20422 bit_offset
20423 = (! BYTES_BIG_ENDIAN
20424 ? highest_order_object_bit_offset - highest_order_field_bit_offset
20425 : highest_order_field_bit_offset - highest_order_object_bit_offset);
20427 if (bit_offset < 0)
20428 add_AT_int (die, DW_AT_bit_offset, bit_offset);
20429 else
20430 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
20433 /* For a FIELD_DECL node which represents a bit field, output an attribute
20434 which specifies the length in bits of the given field. */
20436 static inline void
20437 add_bit_size_attribute (dw_die_ref die, tree decl)
20439 /* Must be a field and a bit field. */
20440 gcc_assert (TREE_CODE (decl) == FIELD_DECL
20441 && DECL_BIT_FIELD_TYPE (decl));
20443 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
20444 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
20447 /* If the compiled language is ANSI C, then add a 'prototyped'
20448 attribute, if arg types are given for the parameters of a function. */
20450 static inline void
20451 add_prototyped_attribute (dw_die_ref die, tree func_type)
20453 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
20455 case DW_LANG_C:
20456 case DW_LANG_C89:
20457 case DW_LANG_C99:
20458 case DW_LANG_C11:
20459 case DW_LANG_ObjC:
20460 if (prototype_p (func_type))
20461 add_AT_flag (die, DW_AT_prototyped, 1);
20462 break;
20463 default:
20464 break;
20468 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
20469 by looking in the type declaration, the object declaration equate table or
20470 the block mapping. */
20472 static inline dw_die_ref
20473 add_abstract_origin_attribute (dw_die_ref die, tree origin)
20475 dw_die_ref origin_die = NULL;
20477 if (DECL_P (origin))
20479 dw_die_ref c;
20480 origin_die = lookup_decl_die (origin);
20481 /* "Unwrap" the decls DIE which we put in the imported unit context.
20482 We are looking for the abstract copy here. */
20483 if (in_lto_p
20484 && origin_die
20485 && (c = get_AT_ref (origin_die, DW_AT_abstract_origin))
20486 /* ??? Identify this better. */
20487 && c->with_offset)
20488 origin_die = c;
20490 else if (TYPE_P (origin))
20491 origin_die = lookup_type_die (origin);
20492 else if (TREE_CODE (origin) == BLOCK)
20493 origin_die = BLOCK_DIE (origin);
20495 /* XXX: Functions that are never lowered don't always have correct block
20496 trees (in the case of java, they simply have no block tree, in some other
20497 languages). For these functions, there is nothing we can really do to
20498 output correct debug info for inlined functions in all cases. Rather
20499 than die, we'll just produce deficient debug info now, in that we will
20500 have variables without a proper abstract origin. In the future, when all
20501 functions are lowered, we should re-add a gcc_assert (origin_die)
20502 here. */
20504 if (origin_die)
20505 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
20506 return origin_die;
20509 /* We do not currently support the pure_virtual attribute. */
20511 static inline void
20512 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
20514 if (DECL_VINDEX (func_decl))
20516 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20518 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
20519 add_AT_loc (die, DW_AT_vtable_elem_location,
20520 new_loc_descr (DW_OP_constu,
20521 tree_to_shwi (DECL_VINDEX (func_decl)),
20522 0));
20524 /* GNU extension: Record what type this method came from originally. */
20525 if (debug_info_level > DINFO_LEVEL_TERSE
20526 && DECL_CONTEXT (func_decl))
20527 add_AT_die_ref (die, DW_AT_containing_type,
20528 lookup_type_die (DECL_CONTEXT (func_decl)));
20532 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
20533 given decl. This used to be a vendor extension until after DWARF 4
20534 standardized it. */
20536 static void
20537 add_linkage_attr (dw_die_ref die, tree decl)
20539 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20541 /* Mimic what assemble_name_raw does with a leading '*'. */
20542 if (name[0] == '*')
20543 name = &name[1];
20545 if (dwarf_version >= 4)
20546 add_AT_string (die, DW_AT_linkage_name, name);
20547 else
20548 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
20551 /* Add source coordinate attributes for the given decl. */
20553 static void
20554 add_src_coords_attributes (dw_die_ref die, tree decl)
20556 expanded_location s;
20558 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
20559 return;
20560 s = expand_location (DECL_SOURCE_LOCATION (decl));
20561 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
20562 add_AT_unsigned (die, DW_AT_decl_line, s.line);
20563 if (debug_column_info && s.column)
20564 add_AT_unsigned (die, DW_AT_decl_column, s.column);
20567 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
20569 static void
20570 add_linkage_name_raw (dw_die_ref die, tree decl)
20572 /* Defer until we have an assembler name set. */
20573 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
20575 limbo_die_node *asm_name;
20577 asm_name = ggc_cleared_alloc<limbo_die_node> ();
20578 asm_name->die = die;
20579 asm_name->created_for = decl;
20580 asm_name->next = deferred_asm_name;
20581 deferred_asm_name = asm_name;
20583 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20584 add_linkage_attr (die, decl);
20587 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
20589 static void
20590 add_linkage_name (dw_die_ref die, tree decl)
20592 if (debug_info_level > DINFO_LEVEL_NONE
20593 && VAR_OR_FUNCTION_DECL_P (decl)
20594 && TREE_PUBLIC (decl)
20595 && !(VAR_P (decl) && DECL_REGISTER (decl))
20596 && die->die_tag != DW_TAG_member)
20597 add_linkage_name_raw (die, decl);
20600 /* Add a DW_AT_name attribute and source coordinate attribute for the
20601 given decl, but only if it actually has a name. */
20603 static void
20604 add_name_and_src_coords_attributes (dw_die_ref die, tree decl,
20605 bool no_linkage_name)
20607 tree decl_name;
20609 decl_name = DECL_NAME (decl);
20610 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
20612 const char *name = dwarf2_name (decl, 0);
20613 if (name)
20614 add_name_attribute (die, name);
20615 if (! DECL_ARTIFICIAL (decl))
20616 add_src_coords_attributes (die, decl);
20618 if (!no_linkage_name)
20619 add_linkage_name (die, decl);
20622 #ifdef VMS_DEBUGGING_INFO
20623 /* Get the function's name, as described by its RTL. This may be different
20624 from the DECL_NAME name used in the source file. */
20625 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
20627 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
20628 XEXP (DECL_RTL (decl), 0), false);
20629 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
20631 #endif /* VMS_DEBUGGING_INFO */
20634 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
20636 static void
20637 add_discr_value (dw_die_ref die, dw_discr_value *value)
20639 dw_attr_node attr;
20641 attr.dw_attr = DW_AT_discr_value;
20642 attr.dw_attr_val.val_class = dw_val_class_discr_value;
20643 attr.dw_attr_val.val_entry = NULL;
20644 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
20645 if (value->pos)
20646 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
20647 else
20648 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
20649 add_dwarf_attr (die, &attr);
20652 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
20654 static void
20655 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
20657 dw_attr_node attr;
20659 attr.dw_attr = DW_AT_discr_list;
20660 attr.dw_attr_val.val_class = dw_val_class_discr_list;
20661 attr.dw_attr_val.val_entry = NULL;
20662 attr.dw_attr_val.v.val_discr_list = discr_list;
20663 add_dwarf_attr (die, &attr);
20666 static inline dw_discr_list_ref
20667 AT_discr_list (dw_attr_node *attr)
20669 return attr->dw_attr_val.v.val_discr_list;
20672 #ifdef VMS_DEBUGGING_INFO
20673 /* Output the debug main pointer die for VMS */
20675 void
20676 dwarf2out_vms_debug_main_pointer (void)
20678 char label[MAX_ARTIFICIAL_LABEL_BYTES];
20679 dw_die_ref die;
20681 /* Allocate the VMS debug main subprogram die. */
20682 die = new_die_raw (DW_TAG_subprogram);
20683 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
20684 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
20685 current_function_funcdef_no);
20686 add_AT_lbl_id (die, DW_AT_entry_pc, label);
20688 /* Make it the first child of comp_unit_die (). */
20689 die->die_parent = comp_unit_die ();
20690 if (comp_unit_die ()->die_child)
20692 die->die_sib = comp_unit_die ()->die_child->die_sib;
20693 comp_unit_die ()->die_child->die_sib = die;
20695 else
20697 die->die_sib = die;
20698 comp_unit_die ()->die_child = die;
20701 #endif /* VMS_DEBUGGING_INFO */
20703 /* Push a new declaration scope. */
20705 static void
20706 push_decl_scope (tree scope)
20708 vec_safe_push (decl_scope_table, scope);
20711 /* Pop a declaration scope. */
20713 static inline void
20714 pop_decl_scope (void)
20716 decl_scope_table->pop ();
20719 /* walk_tree helper function for uses_local_type, below. */
20721 static tree
20722 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
20724 if (!TYPE_P (*tp))
20725 *walk_subtrees = 0;
20726 else
20728 tree name = TYPE_NAME (*tp);
20729 if (name && DECL_P (name) && decl_function_context (name))
20730 return *tp;
20732 return NULL_TREE;
20735 /* If TYPE involves a function-local type (including a local typedef to a
20736 non-local type), returns that type; otherwise returns NULL_TREE. */
20738 static tree
20739 uses_local_type (tree type)
20741 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
20742 return used;
20745 /* Return the DIE for the scope that immediately contains this type.
20746 Non-named types that do not involve a function-local type get global
20747 scope. Named types nested in namespaces or other types get their
20748 containing scope. All other types (i.e. function-local named types) get
20749 the current active scope. */
20751 static dw_die_ref
20752 scope_die_for (tree t, dw_die_ref context_die)
20754 dw_die_ref scope_die = NULL;
20755 tree containing_scope;
20757 /* Non-types always go in the current scope. */
20758 gcc_assert (TYPE_P (t));
20760 /* Use the scope of the typedef, rather than the scope of the type
20761 it refers to. */
20762 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
20763 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
20764 else
20765 containing_scope = TYPE_CONTEXT (t);
20767 /* Use the containing namespace if there is one. */
20768 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
20770 if (context_die == lookup_decl_die (containing_scope))
20771 /* OK */;
20772 else if (debug_info_level > DINFO_LEVEL_TERSE)
20773 context_die = get_context_die (containing_scope);
20774 else
20775 containing_scope = NULL_TREE;
20778 /* Ignore function type "scopes" from the C frontend. They mean that
20779 a tagged type is local to a parmlist of a function declarator, but
20780 that isn't useful to DWARF. */
20781 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
20782 containing_scope = NULL_TREE;
20784 if (SCOPE_FILE_SCOPE_P (containing_scope))
20786 /* If T uses a local type keep it local as well, to avoid references
20787 to function-local DIEs from outside the function. */
20788 if (current_function_decl && uses_local_type (t))
20789 scope_die = context_die;
20790 else
20791 scope_die = comp_unit_die ();
20793 else if (TYPE_P (containing_scope))
20795 /* For types, we can just look up the appropriate DIE. */
20796 if (debug_info_level > DINFO_LEVEL_TERSE)
20797 scope_die = get_context_die (containing_scope);
20798 else
20800 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
20801 if (scope_die == NULL)
20802 scope_die = comp_unit_die ();
20805 else
20806 scope_die = context_die;
20808 return scope_die;
20811 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
20813 static inline int
20814 local_scope_p (dw_die_ref context_die)
20816 for (; context_die; context_die = context_die->die_parent)
20817 if (context_die->die_tag == DW_TAG_inlined_subroutine
20818 || context_die->die_tag == DW_TAG_subprogram)
20819 return 1;
20821 return 0;
20824 /* Returns nonzero if CONTEXT_DIE is a class. */
20826 static inline int
20827 class_scope_p (dw_die_ref context_die)
20829 return (context_die
20830 && (context_die->die_tag == DW_TAG_structure_type
20831 || context_die->die_tag == DW_TAG_class_type
20832 || context_die->die_tag == DW_TAG_interface_type
20833 || context_die->die_tag == DW_TAG_union_type));
20836 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
20837 whether or not to treat a DIE in this context as a declaration. */
20839 static inline int
20840 class_or_namespace_scope_p (dw_die_ref context_die)
20842 return (class_scope_p (context_die)
20843 || (context_die && context_die->die_tag == DW_TAG_namespace));
20846 /* Many forms of DIEs require a "type description" attribute. This
20847 routine locates the proper "type descriptor" die for the type given
20848 by 'type' plus any additional qualifiers given by 'cv_quals', and
20849 adds a DW_AT_type attribute below the given die. */
20851 static void
20852 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
20853 bool reverse, dw_die_ref context_die)
20855 enum tree_code code = TREE_CODE (type);
20856 dw_die_ref type_die = NULL;
20858 /* ??? If this type is an unnamed subrange type of an integral, floating-point
20859 or fixed-point type, use the inner type. This is because we have no
20860 support for unnamed types in base_type_die. This can happen if this is
20861 an Ada subrange type. Correct solution is emit a subrange type die. */
20862 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
20863 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
20864 type = TREE_TYPE (type), code = TREE_CODE (type);
20866 if (code == ERROR_MARK
20867 /* Handle a special case. For functions whose return type is void, we
20868 generate *no* type attribute. (Note that no object may have type
20869 `void', so this only applies to function return types). */
20870 || code == VOID_TYPE)
20871 return;
20873 type_die = modified_type_die (type,
20874 cv_quals | TYPE_QUALS (type),
20875 reverse,
20876 context_die);
20878 if (type_die != NULL)
20879 add_AT_die_ref (object_die, DW_AT_type, type_die);
20882 /* Given an object die, add the calling convention attribute for the
20883 function call type. */
20884 static void
20885 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
20887 enum dwarf_calling_convention value = DW_CC_normal;
20889 value = ((enum dwarf_calling_convention)
20890 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
20892 if (is_fortran ()
20893 && id_equal (DECL_ASSEMBLER_NAME (decl), "MAIN__"))
20895 /* DWARF 2 doesn't provide a way to identify a program's source-level
20896 entry point. DW_AT_calling_convention attributes are only meant
20897 to describe functions' calling conventions. However, lacking a
20898 better way to signal the Fortran main program, we used this for
20899 a long time, following existing custom. Now, DWARF 4 has
20900 DW_AT_main_subprogram, which we add below, but some tools still
20901 rely on the old way, which we thus keep. */
20902 value = DW_CC_program;
20904 if (dwarf_version >= 4 || !dwarf_strict)
20905 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
20908 /* Only add the attribute if the backend requests it, and
20909 is not DW_CC_normal. */
20910 if (value && (value != DW_CC_normal))
20911 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
20914 /* Given a tree pointer to a struct, class, union, or enum type node, return
20915 a pointer to the (string) tag name for the given type, or zero if the type
20916 was declared without a tag. */
20918 static const char *
20919 type_tag (const_tree type)
20921 const char *name = 0;
20923 if (TYPE_NAME (type) != 0)
20925 tree t = 0;
20927 /* Find the IDENTIFIER_NODE for the type name. */
20928 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
20929 && !TYPE_NAMELESS (type))
20930 t = TYPE_NAME (type);
20932 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
20933 a TYPE_DECL node, regardless of whether or not a `typedef' was
20934 involved. */
20935 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20936 && ! DECL_IGNORED_P (TYPE_NAME (type)))
20938 /* We want to be extra verbose. Don't call dwarf_name if
20939 DECL_NAME isn't set. The default hook for decl_printable_name
20940 doesn't like that, and in this context it's correct to return
20941 0, instead of "<anonymous>" or the like. */
20942 if (DECL_NAME (TYPE_NAME (type))
20943 && !DECL_NAMELESS (TYPE_NAME (type)))
20944 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
20947 /* Now get the name as a string, or invent one. */
20948 if (!name && t != 0)
20949 name = IDENTIFIER_POINTER (t);
20952 return (name == 0 || *name == '\0') ? 0 : name;
20955 /* Return the type associated with a data member, make a special check
20956 for bit field types. */
20958 static inline tree
20959 member_declared_type (const_tree member)
20961 return (DECL_BIT_FIELD_TYPE (member)
20962 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
20965 /* Get the decl's label, as described by its RTL. This may be different
20966 from the DECL_NAME name used in the source file. */
20968 #if 0
20969 static const char *
20970 decl_start_label (tree decl)
20972 rtx x;
20973 const char *fnname;
20975 x = DECL_RTL (decl);
20976 gcc_assert (MEM_P (x));
20978 x = XEXP (x, 0);
20979 gcc_assert (GET_CODE (x) == SYMBOL_REF);
20981 fnname = XSTR (x, 0);
20982 return fnname;
20984 #endif
20986 /* For variable-length arrays that have been previously generated, but
20987 may be incomplete due to missing subscript info, fill the subscript
20988 info. Return TRUE if this is one of those cases. */
20989 static bool
20990 fill_variable_array_bounds (tree type)
20992 if (TREE_ASM_WRITTEN (type)
20993 && TREE_CODE (type) == ARRAY_TYPE
20994 && variably_modified_type_p (type, NULL))
20996 dw_die_ref array_die = lookup_type_die (type);
20997 if (!array_die)
20998 return false;
20999 add_subscript_info (array_die, type, !is_ada ());
21000 return true;
21002 return false;
21005 /* These routines generate the internal representation of the DIE's for
21006 the compilation unit. Debugging information is collected by walking
21007 the declaration trees passed in from dwarf2out_decl(). */
21009 static void
21010 gen_array_type_die (tree type, dw_die_ref context_die)
21012 dw_die_ref array_die;
21014 /* GNU compilers represent multidimensional array types as sequences of one
21015 dimensional array types whose element types are themselves array types.
21016 We sometimes squish that down to a single array_type DIE with multiple
21017 subscripts in the Dwarf debugging info. The draft Dwarf specification
21018 say that we are allowed to do this kind of compression in C, because
21019 there is no difference between an array of arrays and a multidimensional
21020 array. We don't do this for Ada to remain as close as possible to the
21021 actual representation, which is especially important against the language
21022 flexibilty wrt arrays of variable size. */
21024 bool collapse_nested_arrays = !is_ada ();
21026 if (fill_variable_array_bounds (type))
21027 return;
21029 dw_die_ref scope_die = scope_die_for (type, context_die);
21030 tree element_type;
21032 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
21033 DW_TAG_string_type doesn't have DW_AT_type attribute). */
21034 if (TYPE_STRING_FLAG (type)
21035 && TREE_CODE (type) == ARRAY_TYPE
21036 && is_fortran ()
21037 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
21039 HOST_WIDE_INT size;
21041 array_die = new_die (DW_TAG_string_type, scope_die, type);
21042 add_name_attribute (array_die, type_tag (type));
21043 equate_type_number_to_die (type, array_die);
21044 size = int_size_in_bytes (type);
21045 if (size >= 0)
21046 add_AT_unsigned (array_die, DW_AT_byte_size, size);
21047 /* ??? We can't annotate types late, but for LTO we may not
21048 generate a location early either (gfortran.dg/save_6.f90). */
21049 else if (! (early_dwarf && (flag_generate_lto || flag_generate_offload))
21050 && TYPE_DOMAIN (type) != NULL_TREE
21051 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
21053 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
21054 tree rszdecl = szdecl;
21056 size = int_size_in_bytes (TREE_TYPE (szdecl));
21057 if (!DECL_P (szdecl))
21059 if (TREE_CODE (szdecl) == INDIRECT_REF
21060 && DECL_P (TREE_OPERAND (szdecl, 0)))
21062 rszdecl = TREE_OPERAND (szdecl, 0);
21063 if (int_size_in_bytes (TREE_TYPE (rszdecl))
21064 != DWARF2_ADDR_SIZE)
21065 size = 0;
21067 else
21068 size = 0;
21070 if (size > 0)
21072 dw_loc_list_ref loc
21073 = loc_list_from_tree (rszdecl, szdecl == rszdecl ? 2 : 0,
21074 NULL);
21075 if (loc)
21077 add_AT_location_description (array_die, DW_AT_string_length,
21078 loc);
21079 if (size != DWARF2_ADDR_SIZE)
21080 add_AT_unsigned (array_die, dwarf_version >= 5
21081 ? DW_AT_string_length_byte_size
21082 : DW_AT_byte_size, size);
21086 return;
21089 array_die = new_die (DW_TAG_array_type, scope_die, type);
21090 add_name_attribute (array_die, type_tag (type));
21091 equate_type_number_to_die (type, array_die);
21093 if (TREE_CODE (type) == VECTOR_TYPE)
21094 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
21096 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
21097 if (is_fortran ()
21098 && TREE_CODE (type) == ARRAY_TYPE
21099 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
21100 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
21101 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21103 #if 0
21104 /* We default the array ordering. Debuggers will probably do the right
21105 things even if DW_AT_ordering is not present. It's not even an issue
21106 until we start to get into multidimensional arrays anyway. If a debugger
21107 is ever caught doing the Wrong Thing for multi-dimensional arrays,
21108 then we'll have to put the DW_AT_ordering attribute back in. (But if
21109 and when we find out that we need to put these in, we will only do so
21110 for multidimensional arrays. */
21111 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21112 #endif
21114 if (TREE_CODE (type) == VECTOR_TYPE)
21116 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
21117 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
21118 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
21119 add_bound_info (subrange_die, DW_AT_upper_bound,
21120 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
21122 else
21123 add_subscript_info (array_die, type, collapse_nested_arrays);
21125 /* Add representation of the type of the elements of this array type and
21126 emit the corresponding DIE if we haven't done it already. */
21127 element_type = TREE_TYPE (type);
21128 if (collapse_nested_arrays)
21129 while (TREE_CODE (element_type) == ARRAY_TYPE)
21131 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
21132 break;
21133 element_type = TREE_TYPE (element_type);
21136 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
21137 TREE_CODE (type) == ARRAY_TYPE
21138 && TYPE_REVERSE_STORAGE_ORDER (type),
21139 context_die);
21141 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21142 if (TYPE_ARTIFICIAL (type))
21143 add_AT_flag (array_die, DW_AT_artificial, 1);
21145 if (get_AT (array_die, DW_AT_name))
21146 add_pubtype (type, array_die);
21148 add_alignment_attribute (array_die, type);
21151 /* This routine generates DIE for array with hidden descriptor, details
21152 are filled into *info by a langhook. */
21154 static void
21155 gen_descr_array_type_die (tree type, struct array_descr_info *info,
21156 dw_die_ref context_die)
21158 const dw_die_ref scope_die = scope_die_for (type, context_die);
21159 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
21160 struct loc_descr_context context = { type, info->base_decl, NULL,
21161 false, false };
21162 enum dwarf_tag subrange_tag = DW_TAG_subrange_type;
21163 int dim;
21165 add_name_attribute (array_die, type_tag (type));
21166 equate_type_number_to_die (type, array_die);
21168 if (info->ndimensions > 1)
21169 switch (info->ordering)
21171 case array_descr_ordering_row_major:
21172 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
21173 break;
21174 case array_descr_ordering_column_major:
21175 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
21176 break;
21177 default:
21178 break;
21181 if (dwarf_version >= 3 || !dwarf_strict)
21183 if (info->data_location)
21184 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
21185 dw_scalar_form_exprloc, &context);
21186 if (info->associated)
21187 add_scalar_info (array_die, DW_AT_associated, info->associated,
21188 dw_scalar_form_constant
21189 | dw_scalar_form_exprloc
21190 | dw_scalar_form_reference, &context);
21191 if (info->allocated)
21192 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
21193 dw_scalar_form_constant
21194 | dw_scalar_form_exprloc
21195 | dw_scalar_form_reference, &context);
21196 if (info->stride)
21198 const enum dwarf_attribute attr
21199 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
21200 const int forms
21201 = (info->stride_in_bits)
21202 ? dw_scalar_form_constant
21203 : (dw_scalar_form_constant
21204 | dw_scalar_form_exprloc
21205 | dw_scalar_form_reference);
21207 add_scalar_info (array_die, attr, info->stride, forms, &context);
21210 if (dwarf_version >= 5)
21212 if (info->rank)
21214 add_scalar_info (array_die, DW_AT_rank, info->rank,
21215 dw_scalar_form_constant
21216 | dw_scalar_form_exprloc, &context);
21217 subrange_tag = DW_TAG_generic_subrange;
21218 context.placeholder_arg = true;
21222 add_gnat_descriptive_type_attribute (array_die, type, context_die);
21224 for (dim = 0; dim < info->ndimensions; dim++)
21226 dw_die_ref subrange_die = new_die (subrange_tag, array_die, NULL);
21228 if (info->dimen[dim].bounds_type)
21229 add_type_attribute (subrange_die,
21230 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
21231 false, context_die);
21232 if (info->dimen[dim].lower_bound)
21233 add_bound_info (subrange_die, DW_AT_lower_bound,
21234 info->dimen[dim].lower_bound, &context);
21235 if (info->dimen[dim].upper_bound)
21236 add_bound_info (subrange_die, DW_AT_upper_bound,
21237 info->dimen[dim].upper_bound, &context);
21238 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
21239 add_scalar_info (subrange_die, DW_AT_byte_stride,
21240 info->dimen[dim].stride,
21241 dw_scalar_form_constant
21242 | dw_scalar_form_exprloc
21243 | dw_scalar_form_reference,
21244 &context);
21247 gen_type_die (info->element_type, context_die);
21248 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
21249 TREE_CODE (type) == ARRAY_TYPE
21250 && TYPE_REVERSE_STORAGE_ORDER (type),
21251 context_die);
21253 if (get_AT (array_die, DW_AT_name))
21254 add_pubtype (type, array_die);
21256 add_alignment_attribute (array_die, type);
21259 #if 0
21260 static void
21261 gen_entry_point_die (tree decl, dw_die_ref context_die)
21263 tree origin = decl_ultimate_origin (decl);
21264 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
21266 if (origin != NULL)
21267 add_abstract_origin_attribute (decl_die, origin);
21268 else
21270 add_name_and_src_coords_attributes (decl_die, decl);
21271 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
21272 TYPE_UNQUALIFIED, false, context_die);
21275 if (DECL_ABSTRACT_P (decl))
21276 equate_decl_number_to_die (decl, decl_die);
21277 else
21278 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
21280 #endif
21282 /* Walk through the list of incomplete types again, trying once more to
21283 emit full debugging info for them. */
21285 static void
21286 retry_incomplete_types (void)
21288 set_early_dwarf s;
21289 int i;
21291 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
21292 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
21293 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
21294 vec_safe_truncate (incomplete_types, 0);
21297 /* Determine what tag to use for a record type. */
21299 static enum dwarf_tag
21300 record_type_tag (tree type)
21302 if (! lang_hooks.types.classify_record)
21303 return DW_TAG_structure_type;
21305 switch (lang_hooks.types.classify_record (type))
21307 case RECORD_IS_STRUCT:
21308 return DW_TAG_structure_type;
21310 case RECORD_IS_CLASS:
21311 return DW_TAG_class_type;
21313 case RECORD_IS_INTERFACE:
21314 if (dwarf_version >= 3 || !dwarf_strict)
21315 return DW_TAG_interface_type;
21316 return DW_TAG_structure_type;
21318 default:
21319 gcc_unreachable ();
21323 /* Generate a DIE to represent an enumeration type. Note that these DIEs
21324 include all of the information about the enumeration values also. Each
21325 enumerated type name/value is listed as a child of the enumerated type
21326 DIE. */
21328 static dw_die_ref
21329 gen_enumeration_type_die (tree type, dw_die_ref context_die)
21331 dw_die_ref type_die = lookup_type_die (type);
21333 if (type_die == NULL)
21335 type_die = new_die (DW_TAG_enumeration_type,
21336 scope_die_for (type, context_die), type);
21337 equate_type_number_to_die (type, type_die);
21338 add_name_attribute (type_die, type_tag (type));
21339 if (dwarf_version >= 4 || !dwarf_strict)
21341 if (ENUM_IS_SCOPED (type))
21342 add_AT_flag (type_die, DW_AT_enum_class, 1);
21343 if (ENUM_IS_OPAQUE (type))
21344 add_AT_flag (type_die, DW_AT_declaration, 1);
21346 if (!dwarf_strict)
21347 add_AT_unsigned (type_die, DW_AT_encoding,
21348 TYPE_UNSIGNED (type)
21349 ? DW_ATE_unsigned
21350 : DW_ATE_signed);
21352 else if (! TYPE_SIZE (type))
21353 return type_die;
21354 else
21355 remove_AT (type_die, DW_AT_declaration);
21357 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
21358 given enum type is incomplete, do not generate the DW_AT_byte_size
21359 attribute or the DW_AT_element_list attribute. */
21360 if (TYPE_SIZE (type))
21362 tree link;
21364 TREE_ASM_WRITTEN (type) = 1;
21365 add_byte_size_attribute (type_die, type);
21366 add_alignment_attribute (type_die, type);
21367 if (dwarf_version >= 3 || !dwarf_strict)
21369 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
21370 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
21371 context_die);
21373 if (TYPE_STUB_DECL (type) != NULL_TREE)
21375 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21376 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21379 /* If the first reference to this type was as the return type of an
21380 inline function, then it may not have a parent. Fix this now. */
21381 if (type_die->die_parent == NULL)
21382 add_child_die (scope_die_for (type, context_die), type_die);
21384 for (link = TYPE_VALUES (type);
21385 link != NULL; link = TREE_CHAIN (link))
21387 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
21388 tree value = TREE_VALUE (link);
21390 add_name_attribute (enum_die,
21391 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
21393 if (TREE_CODE (value) == CONST_DECL)
21394 value = DECL_INITIAL (value);
21396 if (simple_type_size_in_bits (TREE_TYPE (value))
21397 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
21399 /* For constant forms created by add_AT_unsigned DWARF
21400 consumers (GDB, elfutils, etc.) always zero extend
21401 the value. Only when the actual value is negative
21402 do we need to use add_AT_int to generate a constant
21403 form that can represent negative values. */
21404 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
21405 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
21406 add_AT_unsigned (enum_die, DW_AT_const_value,
21407 (unsigned HOST_WIDE_INT) val);
21408 else
21409 add_AT_int (enum_die, DW_AT_const_value, val);
21411 else
21412 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
21413 that here. TODO: This should be re-worked to use correct
21414 signed/unsigned double tags for all cases. */
21415 add_AT_wide (enum_die, DW_AT_const_value, wi::to_wide (value));
21418 add_gnat_descriptive_type_attribute (type_die, type, context_die);
21419 if (TYPE_ARTIFICIAL (type))
21420 add_AT_flag (type_die, DW_AT_artificial, 1);
21422 else
21423 add_AT_flag (type_die, DW_AT_declaration, 1);
21425 add_pubtype (type, type_die);
21427 return type_die;
21430 /* Generate a DIE to represent either a real live formal parameter decl or to
21431 represent just the type of some formal parameter position in some function
21432 type.
21434 Note that this routine is a bit unusual because its argument may be a
21435 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
21436 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
21437 node. If it's the former then this function is being called to output a
21438 DIE to represent a formal parameter object (or some inlining thereof). If
21439 it's the latter, then this function is only being called to output a
21440 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
21441 argument type of some subprogram type.
21442 If EMIT_NAME_P is true, name and source coordinate attributes
21443 are emitted. */
21445 static dw_die_ref
21446 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
21447 dw_die_ref context_die)
21449 tree node_or_origin = node ? node : origin;
21450 tree ultimate_origin;
21451 dw_die_ref parm_die = NULL;
21453 if (DECL_P (node_or_origin))
21455 parm_die = lookup_decl_die (node);
21457 /* If the contexts differ, we may not be talking about the same
21458 thing.
21459 ??? When in LTO the DIE parent is the "abstract" copy and the
21460 context_die is the specification "copy". But this whole block
21461 should eventually be no longer needed. */
21462 if (parm_die && parm_die->die_parent != context_die && !in_lto_p)
21464 if (!DECL_ABSTRACT_P (node))
21466 /* This can happen when creating an inlined instance, in
21467 which case we need to create a new DIE that will get
21468 annotated with DW_AT_abstract_origin. */
21469 parm_die = NULL;
21471 else
21472 gcc_unreachable ();
21475 if (parm_die && parm_die->die_parent == NULL)
21477 /* Check that parm_die already has the right attributes that
21478 we would have added below. If any attributes are
21479 missing, fall through to add them. */
21480 if (! DECL_ABSTRACT_P (node_or_origin)
21481 && !get_AT (parm_die, DW_AT_location)
21482 && !get_AT (parm_die, DW_AT_const_value))
21483 /* We are missing location info, and are about to add it. */
21485 else
21487 add_child_die (context_die, parm_die);
21488 return parm_die;
21493 /* If we have a previously generated DIE, use it, unless this is an
21494 concrete instance (origin != NULL), in which case we need a new
21495 DIE with a corresponding DW_AT_abstract_origin. */
21496 bool reusing_die;
21497 if (parm_die && origin == NULL)
21498 reusing_die = true;
21499 else
21501 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
21502 reusing_die = false;
21505 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
21507 case tcc_declaration:
21508 ultimate_origin = decl_ultimate_origin (node_or_origin);
21509 if (node || ultimate_origin)
21510 origin = ultimate_origin;
21512 if (reusing_die)
21513 goto add_location;
21515 if (origin != NULL)
21516 add_abstract_origin_attribute (parm_die, origin);
21517 else if (emit_name_p)
21518 add_name_and_src_coords_attributes (parm_die, node);
21519 if (origin == NULL
21520 || (! DECL_ABSTRACT_P (node_or_origin)
21521 && variably_modified_type_p (TREE_TYPE (node_or_origin),
21522 decl_function_context
21523 (node_or_origin))))
21525 tree type = TREE_TYPE (node_or_origin);
21526 if (decl_by_reference_p (node_or_origin))
21527 add_type_attribute (parm_die, TREE_TYPE (type),
21528 TYPE_UNQUALIFIED,
21529 false, context_die);
21530 else
21531 add_type_attribute (parm_die, type,
21532 decl_quals (node_or_origin),
21533 false, context_die);
21535 if (origin == NULL && DECL_ARTIFICIAL (node))
21536 add_AT_flag (parm_die, DW_AT_artificial, 1);
21537 add_location:
21538 if (node && node != origin)
21539 equate_decl_number_to_die (node, parm_die);
21540 if (! DECL_ABSTRACT_P (node_or_origin))
21541 add_location_or_const_value_attribute (parm_die, node_or_origin,
21542 node == NULL);
21544 break;
21546 case tcc_type:
21547 /* We were called with some kind of a ..._TYPE node. */
21548 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
21549 context_die);
21550 break;
21552 default:
21553 gcc_unreachable ();
21556 return parm_die;
21559 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
21560 children DW_TAG_formal_parameter DIEs representing the arguments of the
21561 parameter pack.
21563 PARM_PACK must be a function parameter pack.
21564 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
21565 must point to the subsequent arguments of the function PACK_ARG belongs to.
21566 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
21567 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
21568 following the last one for which a DIE was generated. */
21570 static dw_die_ref
21571 gen_formal_parameter_pack_die (tree parm_pack,
21572 tree pack_arg,
21573 dw_die_ref subr_die,
21574 tree *next_arg)
21576 tree arg;
21577 dw_die_ref parm_pack_die;
21579 gcc_assert (parm_pack
21580 && lang_hooks.function_parameter_pack_p (parm_pack)
21581 && subr_die);
21583 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
21584 add_src_coords_attributes (parm_pack_die, parm_pack);
21586 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
21588 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
21589 parm_pack))
21590 break;
21591 gen_formal_parameter_die (arg, NULL,
21592 false /* Don't emit name attribute. */,
21593 parm_pack_die);
21595 if (next_arg)
21596 *next_arg = arg;
21597 return parm_pack_die;
21600 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
21601 at the end of an (ANSI prototyped) formal parameters list. */
21603 static void
21604 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
21606 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
21609 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
21610 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
21611 parameters as specified in some function type specification (except for
21612 those which appear as part of a function *definition*). */
21614 static void
21615 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
21617 tree link;
21618 tree formal_type = NULL;
21619 tree first_parm_type;
21620 tree arg;
21622 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
21624 arg = DECL_ARGUMENTS (function_or_method_type);
21625 function_or_method_type = TREE_TYPE (function_or_method_type);
21627 else
21628 arg = NULL_TREE;
21630 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
21632 /* Make our first pass over the list of formal parameter types and output a
21633 DW_TAG_formal_parameter DIE for each one. */
21634 for (link = first_parm_type; link; )
21636 dw_die_ref parm_die;
21638 formal_type = TREE_VALUE (link);
21639 if (formal_type == void_type_node)
21640 break;
21642 /* Output a (nameless) DIE to represent the formal parameter itself. */
21643 if (!POINTER_BOUNDS_TYPE_P (formal_type))
21645 parm_die = gen_formal_parameter_die (formal_type, NULL,
21646 true /* Emit name attribute. */,
21647 context_die);
21648 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
21649 && link == first_parm_type)
21651 add_AT_flag (parm_die, DW_AT_artificial, 1);
21652 if (dwarf_version >= 3 || !dwarf_strict)
21653 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
21655 else if (arg && DECL_ARTIFICIAL (arg))
21656 add_AT_flag (parm_die, DW_AT_artificial, 1);
21659 link = TREE_CHAIN (link);
21660 if (arg)
21661 arg = DECL_CHAIN (arg);
21664 /* If this function type has an ellipsis, add a
21665 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
21666 if (formal_type != void_type_node)
21667 gen_unspecified_parameters_die (function_or_method_type, context_die);
21669 /* Make our second (and final) pass over the list of formal parameter types
21670 and output DIEs to represent those types (as necessary). */
21671 for (link = TYPE_ARG_TYPES (function_or_method_type);
21672 link && TREE_VALUE (link);
21673 link = TREE_CHAIN (link))
21674 gen_type_die (TREE_VALUE (link), context_die);
21677 /* We want to generate the DIE for TYPE so that we can generate the
21678 die for MEMBER, which has been defined; we will need to refer back
21679 to the member declaration nested within TYPE. If we're trying to
21680 generate minimal debug info for TYPE, processing TYPE won't do the
21681 trick; we need to attach the member declaration by hand. */
21683 static void
21684 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
21686 gen_type_die (type, context_die);
21688 /* If we're trying to avoid duplicate debug info, we may not have
21689 emitted the member decl for this function. Emit it now. */
21690 if (TYPE_STUB_DECL (type)
21691 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
21692 && ! lookup_decl_die (member))
21694 dw_die_ref type_die;
21695 gcc_assert (!decl_ultimate_origin (member));
21697 push_decl_scope (type);
21698 type_die = lookup_type_die_strip_naming_typedef (type);
21699 if (TREE_CODE (member) == FUNCTION_DECL)
21700 gen_subprogram_die (member, type_die);
21701 else if (TREE_CODE (member) == FIELD_DECL)
21703 /* Ignore the nameless fields that are used to skip bits but handle
21704 C++ anonymous unions and structs. */
21705 if (DECL_NAME (member) != NULL_TREE
21706 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
21707 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
21709 struct vlr_context vlr_ctx = {
21710 DECL_CONTEXT (member), /* struct_type */
21711 NULL_TREE /* variant_part_offset */
21713 gen_type_die (member_declared_type (member), type_die);
21714 gen_field_die (member, &vlr_ctx, type_die);
21717 else
21718 gen_variable_die (member, NULL_TREE, type_die);
21720 pop_decl_scope ();
21724 /* Forward declare these functions, because they are mutually recursive
21725 with their set_block_* pairing functions. */
21726 static void set_decl_origin_self (tree);
21728 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
21729 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
21730 that it points to the node itself, thus indicating that the node is its
21731 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
21732 the given node is NULL, recursively descend the decl/block tree which
21733 it is the root of, and for each other ..._DECL or BLOCK node contained
21734 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
21735 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
21736 values to point to themselves. */
21738 static void
21739 set_block_origin_self (tree stmt)
21741 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
21743 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
21746 tree local_decl;
21748 for (local_decl = BLOCK_VARS (stmt);
21749 local_decl != NULL_TREE;
21750 local_decl = DECL_CHAIN (local_decl))
21751 /* Do not recurse on nested functions since the inlining status
21752 of parent and child can be different as per the DWARF spec. */
21753 if (TREE_CODE (local_decl) != FUNCTION_DECL
21754 && !DECL_EXTERNAL (local_decl))
21755 set_decl_origin_self (local_decl);
21759 tree subblock;
21761 for (subblock = BLOCK_SUBBLOCKS (stmt);
21762 subblock != NULL_TREE;
21763 subblock = BLOCK_CHAIN (subblock))
21764 set_block_origin_self (subblock); /* Recurse. */
21769 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
21770 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
21771 node to so that it points to the node itself, thus indicating that the
21772 node represents its own (abstract) origin. Additionally, if the
21773 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
21774 the decl/block tree of which the given node is the root of, and for
21775 each other ..._DECL or BLOCK node contained therein whose
21776 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
21777 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
21778 point to themselves. */
21780 static void
21781 set_decl_origin_self (tree decl)
21783 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
21785 DECL_ABSTRACT_ORIGIN (decl) = decl;
21786 if (TREE_CODE (decl) == FUNCTION_DECL)
21788 tree arg;
21790 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
21791 DECL_ABSTRACT_ORIGIN (arg) = arg;
21792 if (DECL_INITIAL (decl) != NULL_TREE
21793 && DECL_INITIAL (decl) != error_mark_node)
21794 set_block_origin_self (DECL_INITIAL (decl));
21799 /* Mark the early DIE for DECL as the abstract instance. */
21801 static void
21802 dwarf2out_abstract_function (tree decl)
21804 dw_die_ref old_die;
21806 /* Make sure we have the actual abstract inline, not a clone. */
21807 decl = DECL_ORIGIN (decl);
21809 if (DECL_IGNORED_P (decl))
21810 return;
21812 old_die = lookup_decl_die (decl);
21813 /* With early debug we always have an old DIE unless we are in LTO
21814 and the user did not compile but only link with debug. */
21815 if (in_lto_p && ! old_die)
21816 return;
21817 gcc_assert (old_die != NULL);
21818 if (get_AT (old_die, DW_AT_inline)
21819 || get_AT (old_die, DW_AT_abstract_origin))
21820 /* We've already generated the abstract instance. */
21821 return;
21823 /* Go ahead and put DW_AT_inline on the DIE. */
21824 if (DECL_DECLARED_INLINE_P (decl))
21826 if (cgraph_function_possibly_inlined_p (decl))
21827 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_inlined);
21828 else
21829 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_declared_not_inlined);
21831 else
21833 if (cgraph_function_possibly_inlined_p (decl))
21834 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_inlined);
21835 else
21836 add_AT_unsigned (old_die, DW_AT_inline, DW_INL_not_inlined);
21839 if (DECL_DECLARED_INLINE_P (decl)
21840 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
21841 add_AT_flag (old_die, DW_AT_artificial, 1);
21843 set_decl_origin_self (decl);
21846 /* Helper function of premark_used_types() which gets called through
21847 htab_traverse.
21849 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21850 marked as unused by prune_unused_types. */
21852 bool
21853 premark_used_types_helper (tree const &type, void *)
21855 dw_die_ref die;
21857 die = lookup_type_die (type);
21858 if (die != NULL)
21859 die->die_perennial_p = 1;
21860 return true;
21863 /* Helper function of premark_types_used_by_global_vars which gets called
21864 through htab_traverse.
21866 Marks the DIE of a given type in *SLOT as perennial, so it never gets
21867 marked as unused by prune_unused_types. The DIE of the type is marked
21868 only if the global variable using the type will actually be emitted. */
21871 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
21872 void *)
21874 struct types_used_by_vars_entry *entry;
21875 dw_die_ref die;
21877 entry = (struct types_used_by_vars_entry *) *slot;
21878 gcc_assert (entry->type != NULL
21879 && entry->var_decl != NULL);
21880 die = lookup_type_die (entry->type);
21881 if (die)
21883 /* Ask cgraph if the global variable really is to be emitted.
21884 If yes, then we'll keep the DIE of ENTRY->TYPE. */
21885 varpool_node *node = varpool_node::get (entry->var_decl);
21886 if (node && node->definition)
21888 die->die_perennial_p = 1;
21889 /* Keep the parent DIEs as well. */
21890 while ((die = die->die_parent) && die->die_perennial_p == 0)
21891 die->die_perennial_p = 1;
21894 return 1;
21897 /* Mark all members of used_types_hash as perennial. */
21899 static void
21900 premark_used_types (struct function *fun)
21902 if (fun && fun->used_types_hash)
21903 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
21906 /* Mark all members of types_used_by_vars_entry as perennial. */
21908 static void
21909 premark_types_used_by_global_vars (void)
21911 if (types_used_by_vars_hash)
21912 types_used_by_vars_hash
21913 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
21916 /* Generate a DW_TAG_call_site DIE in function DECL under SUBR_DIE
21917 for CA_LOC call arg loc node. */
21919 static dw_die_ref
21920 gen_call_site_die (tree decl, dw_die_ref subr_die,
21921 struct call_arg_loc_node *ca_loc)
21923 dw_die_ref stmt_die = NULL, die;
21924 tree block = ca_loc->block;
21926 while (block
21927 && block != DECL_INITIAL (decl)
21928 && TREE_CODE (block) == BLOCK)
21930 stmt_die = BLOCK_DIE (block);
21931 if (stmt_die)
21932 break;
21933 block = BLOCK_SUPERCONTEXT (block);
21935 if (stmt_die == NULL)
21936 stmt_die = subr_die;
21937 die = new_die (dwarf_TAG (DW_TAG_call_site), stmt_die, NULL_TREE);
21938 add_AT_lbl_id (die, dwarf_AT (DW_AT_call_return_pc), ca_loc->label);
21939 if (ca_loc->tail_call_p)
21940 add_AT_flag (die, dwarf_AT (DW_AT_call_tail_call), 1);
21941 if (ca_loc->symbol_ref)
21943 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
21944 if (tdie)
21945 add_AT_die_ref (die, dwarf_AT (DW_AT_call_origin), tdie);
21946 else
21947 add_AT_addr (die, dwarf_AT (DW_AT_call_origin), ca_loc->symbol_ref,
21948 false);
21950 return die;
21953 /* Generate a DIE to represent a declared function (either file-scope or
21954 block-local). */
21956 static void
21957 gen_subprogram_die (tree decl, dw_die_ref context_die)
21959 tree origin = decl_ultimate_origin (decl);
21960 dw_die_ref subr_die;
21961 dw_die_ref old_die = lookup_decl_die (decl);
21963 /* This function gets called multiple times for different stages of
21964 the debug process. For example, for func() in this code:
21966 namespace S
21968 void func() { ... }
21971 ...we get called 4 times. Twice in early debug and twice in
21972 late debug:
21974 Early debug
21975 -----------
21977 1. Once while generating func() within the namespace. This is
21978 the declaration. The declaration bit below is set, as the
21979 context is the namespace.
21981 A new DIE will be generated with DW_AT_declaration set.
21983 2. Once for func() itself. This is the specification. The
21984 declaration bit below is clear as the context is the CU.
21986 We will use the cached DIE from (1) to create a new DIE with
21987 DW_AT_specification pointing to the declaration in (1).
21989 Late debug via rest_of_handle_final()
21990 -------------------------------------
21992 3. Once generating func() within the namespace. This is also the
21993 declaration, as in (1), but this time we will early exit below
21994 as we have a cached DIE and a declaration needs no additional
21995 annotations (no locations), as the source declaration line
21996 info is enough.
21998 4. Once for func() itself. As in (2), this is the specification,
21999 but this time we will re-use the cached DIE, and just annotate
22000 it with the location information that should now be available.
22002 For something without namespaces, but with abstract instances, we
22003 are also called a multiple times:
22005 class Base
22007 public:
22008 Base (); // constructor declaration (1)
22011 Base::Base () { } // constructor specification (2)
22013 Early debug
22014 -----------
22016 1. Once for the Base() constructor by virtue of it being a
22017 member of the Base class. This is done via
22018 rest_of_type_compilation.
22020 This is a declaration, so a new DIE will be created with
22021 DW_AT_declaration.
22023 2. Once for the Base() constructor definition, but this time
22024 while generating the abstract instance of the base
22025 constructor (__base_ctor) which is being generated via early
22026 debug of reachable functions.
22028 Even though we have a cached version of the declaration (1),
22029 we will create a DW_AT_specification of the declaration DIE
22030 in (1).
22032 3. Once for the __base_ctor itself, but this time, we generate
22033 an DW_AT_abstract_origin version of the DW_AT_specification in
22034 (2).
22036 Late debug via rest_of_handle_final
22037 -----------------------------------
22039 4. One final time for the __base_ctor (which will have a cached
22040 DIE with DW_AT_abstract_origin created in (3). This time,
22041 we will just annotate the location information now
22042 available.
22044 int declaration = (current_function_decl != decl
22045 || class_or_namespace_scope_p (context_die));
22047 /* A declaration that has been previously dumped needs no
22048 additional information. */
22049 if (old_die && declaration)
22050 return;
22052 /* Now that the C++ front end lazily declares artificial member fns, we
22053 might need to retrofit the declaration into its class. */
22054 if (!declaration && !origin && !old_die
22055 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
22056 && !class_or_namespace_scope_p (context_die)
22057 && debug_info_level > DINFO_LEVEL_TERSE)
22058 old_die = force_decl_die (decl);
22060 /* A concrete instance, tag a new DIE with DW_AT_abstract_origin. */
22061 if (origin != NULL)
22063 gcc_assert (!declaration || local_scope_p (context_die));
22065 /* Fixup die_parent for the abstract instance of a nested
22066 inline function. */
22067 if (old_die && old_die->die_parent == NULL)
22068 add_child_die (context_die, old_die);
22070 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
22072 /* If we have a DW_AT_abstract_origin we have a working
22073 cached version. */
22074 subr_die = old_die;
22076 else
22078 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22079 add_abstract_origin_attribute (subr_die, origin);
22080 /* This is where the actual code for a cloned function is.
22081 Let's emit linkage name attribute for it. This helps
22082 debuggers to e.g, set breakpoints into
22083 constructors/destructors when the user asks "break
22084 K::K". */
22085 add_linkage_name (subr_die, decl);
22088 /* A cached copy, possibly from early dwarf generation. Reuse as
22089 much as possible. */
22090 else if (old_die)
22092 if (!get_AT_flag (old_die, DW_AT_declaration)
22093 /* We can have a normal definition following an inline one in the
22094 case of redefinition of GNU C extern inlines.
22095 It seems reasonable to use AT_specification in this case. */
22096 && !get_AT (old_die, DW_AT_inline))
22098 /* Detect and ignore this case, where we are trying to output
22099 something we have already output. */
22100 if (get_AT (old_die, DW_AT_low_pc)
22101 || get_AT (old_die, DW_AT_ranges))
22102 return;
22104 /* If we have no location information, this must be a
22105 partially generated DIE from early dwarf generation.
22106 Fall through and generate it. */
22109 /* If the definition comes from the same place as the declaration,
22110 maybe use the old DIE. We always want the DIE for this function
22111 that has the *_pc attributes to be under comp_unit_die so the
22112 debugger can find it. We also need to do this for abstract
22113 instances of inlines, since the spec requires the out-of-line copy
22114 to have the same parent. For local class methods, this doesn't
22115 apply; we just use the old DIE. */
22116 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
22117 struct dwarf_file_data * file_index = lookup_filename (s.file);
22118 if ((is_cu_die (old_die->die_parent)
22119 /* This condition fixes the inconsistency/ICE with the
22120 following Fortran test (or some derivative thereof) while
22121 building libgfortran:
22123 module some_m
22124 contains
22125 logical function funky (FLAG)
22126 funky = .true.
22127 end function
22128 end module
22130 || (old_die->die_parent
22131 && old_die->die_parent->die_tag == DW_TAG_module)
22132 || context_die == NULL)
22133 && (DECL_ARTIFICIAL (decl)
22134 /* The location attributes may be in the abstract origin
22135 which in the case of LTO might be not available to
22136 look at. */
22137 || get_AT (old_die, DW_AT_abstract_origin)
22138 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
22139 && (get_AT_unsigned (old_die, DW_AT_decl_line)
22140 == (unsigned) s.line)
22141 && (!debug_column_info
22142 || s.column == 0
22143 || (get_AT_unsigned (old_die, DW_AT_decl_column)
22144 == (unsigned) s.column)))))
22146 subr_die = old_die;
22148 /* Clear out the declaration attribute, but leave the
22149 parameters so they can be augmented with location
22150 information later. Unless this was a declaration, in
22151 which case, wipe out the nameless parameters and recreate
22152 them further down. */
22153 if (remove_AT (subr_die, DW_AT_declaration))
22156 remove_AT (subr_die, DW_AT_object_pointer);
22157 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
22160 /* Make a specification pointing to the previously built
22161 declaration. */
22162 else
22164 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22165 add_AT_specification (subr_die, old_die);
22166 add_pubname (decl, subr_die);
22167 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
22168 add_AT_file (subr_die, DW_AT_decl_file, file_index);
22169 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
22170 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
22171 if (debug_column_info
22172 && s.column
22173 && (get_AT_unsigned (old_die, DW_AT_decl_column)
22174 != (unsigned) s.column))
22175 add_AT_unsigned (subr_die, DW_AT_decl_column, s.column);
22177 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
22178 emit the real type on the definition die. */
22179 if (is_cxx () && debug_info_level > DINFO_LEVEL_TERSE)
22181 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
22182 if (die == auto_die || die == decltype_auto_die)
22183 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22184 TYPE_UNQUALIFIED, false, context_die);
22187 /* When we process the method declaration, we haven't seen
22188 the out-of-class defaulted definition yet, so we have to
22189 recheck now. */
22190 if ((dwarf_version >= 5 || ! dwarf_strict)
22191 && !get_AT (subr_die, DW_AT_defaulted))
22193 int defaulted
22194 = lang_hooks.decls.decl_dwarf_attribute (decl,
22195 DW_AT_defaulted);
22196 if (defaulted != -1)
22198 /* Other values must have been handled before. */
22199 gcc_assert (defaulted == DW_DEFAULTED_out_of_class);
22200 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22205 /* Create a fresh DIE for anything else. */
22206 else
22208 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
22210 if (TREE_PUBLIC (decl))
22211 add_AT_flag (subr_die, DW_AT_external, 1);
22213 add_name_and_src_coords_attributes (subr_die, decl);
22214 add_pubname (decl, subr_die);
22215 if (debug_info_level > DINFO_LEVEL_TERSE)
22217 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
22218 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
22219 TYPE_UNQUALIFIED, false, context_die);
22222 add_pure_or_virtual_attribute (subr_die, decl);
22223 if (DECL_ARTIFICIAL (decl))
22224 add_AT_flag (subr_die, DW_AT_artificial, 1);
22226 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
22227 add_AT_flag (subr_die, DW_AT_noreturn, 1);
22229 add_alignment_attribute (subr_die, decl);
22231 add_accessibility_attribute (subr_die, decl);
22234 /* Unless we have an existing non-declaration DIE, equate the new
22235 DIE. */
22236 if (!old_die || is_declaration_die (old_die))
22237 equate_decl_number_to_die (decl, subr_die);
22239 if (declaration)
22241 if (!old_die || !get_AT (old_die, DW_AT_inline))
22243 add_AT_flag (subr_die, DW_AT_declaration, 1);
22245 /* If this is an explicit function declaration then generate
22246 a DW_AT_explicit attribute. */
22247 if ((dwarf_version >= 3 || !dwarf_strict)
22248 && lang_hooks.decls.decl_dwarf_attribute (decl,
22249 DW_AT_explicit) == 1)
22250 add_AT_flag (subr_die, DW_AT_explicit, 1);
22252 /* If this is a C++11 deleted special function member then generate
22253 a DW_AT_deleted attribute. */
22254 if ((dwarf_version >= 5 || !dwarf_strict)
22255 && lang_hooks.decls.decl_dwarf_attribute (decl,
22256 DW_AT_deleted) == 1)
22257 add_AT_flag (subr_die, DW_AT_deleted, 1);
22259 /* If this is a C++11 defaulted special function member then
22260 generate a DW_AT_defaulted attribute. */
22261 if (dwarf_version >= 5 || !dwarf_strict)
22263 int defaulted
22264 = lang_hooks.decls.decl_dwarf_attribute (decl,
22265 DW_AT_defaulted);
22266 if (defaulted != -1)
22267 add_AT_unsigned (subr_die, DW_AT_defaulted, defaulted);
22270 /* If this is a C++11 non-static member function with & ref-qualifier
22271 then generate a DW_AT_reference attribute. */
22272 if ((dwarf_version >= 5 || !dwarf_strict)
22273 && lang_hooks.decls.decl_dwarf_attribute (decl,
22274 DW_AT_reference) == 1)
22275 add_AT_flag (subr_die, DW_AT_reference, 1);
22277 /* If this is a C++11 non-static member function with &&
22278 ref-qualifier then generate a DW_AT_reference attribute. */
22279 if ((dwarf_version >= 5 || !dwarf_strict)
22280 && lang_hooks.decls.decl_dwarf_attribute (decl,
22281 DW_AT_rvalue_reference)
22282 == 1)
22283 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
22286 /* For non DECL_EXTERNALs, if range information is available, fill
22287 the DIE with it. */
22288 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
22290 HOST_WIDE_INT cfa_fb_offset;
22292 struct function *fun = DECL_STRUCT_FUNCTION (decl);
22294 if (!crtl->has_bb_partition)
22296 dw_fde_ref fde = fun->fde;
22297 if (fde->dw_fde_begin)
22299 /* We have already generated the labels. */
22300 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22301 fde->dw_fde_end, false);
22303 else
22305 /* Create start/end labels and add the range. */
22306 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
22307 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
22308 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
22309 current_function_funcdef_no);
22310 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
22311 current_function_funcdef_no);
22312 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
22313 false);
22316 #if VMS_DEBUGGING_INFO
22317 /* HP OpenVMS Industry Standard 64: DWARF Extensions
22318 Section 2.3 Prologue and Epilogue Attributes:
22319 When a breakpoint is set on entry to a function, it is generally
22320 desirable for execution to be suspended, not on the very first
22321 instruction of the function, but rather at a point after the
22322 function's frame has been set up, after any language defined local
22323 declaration processing has been completed, and before execution of
22324 the first statement of the function begins. Debuggers generally
22325 cannot properly determine where this point is. Similarly for a
22326 breakpoint set on exit from a function. The prologue and epilogue
22327 attributes allow a compiler to communicate the location(s) to use. */
22330 if (fde->dw_fde_vms_end_prologue)
22331 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
22332 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
22334 if (fde->dw_fde_vms_begin_epilogue)
22335 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
22336 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
22338 #endif
22341 else
22343 /* Generate pubnames entries for the split function code ranges. */
22344 dw_fde_ref fde = fun->fde;
22346 if (fde->dw_fde_second_begin)
22348 if (dwarf_version >= 3 || !dwarf_strict)
22350 /* We should use ranges for non-contiguous code section
22351 addresses. Use the actual code range for the initial
22352 section, since the HOT/COLD labels might precede an
22353 alignment offset. */
22354 bool range_list_added = false;
22355 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
22356 fde->dw_fde_end, &range_list_added,
22357 false);
22358 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
22359 fde->dw_fde_second_end,
22360 &range_list_added, false);
22361 if (range_list_added)
22362 add_ranges (NULL);
22364 else
22366 /* There is no real support in DW2 for this .. so we make
22367 a work-around. First, emit the pub name for the segment
22368 containing the function label. Then make and emit a
22369 simplified subprogram DIE for the second segment with the
22370 name pre-fixed by __hot/cold_sect_of_. We use the same
22371 linkage name for the second die so that gdb will find both
22372 sections when given "b foo". */
22373 const char *name = NULL;
22374 tree decl_name = DECL_NAME (decl);
22375 dw_die_ref seg_die;
22377 /* Do the 'primary' section. */
22378 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
22379 fde->dw_fde_end, false);
22381 /* Build a minimal DIE for the secondary section. */
22382 seg_die = new_die (DW_TAG_subprogram,
22383 subr_die->die_parent, decl);
22385 if (TREE_PUBLIC (decl))
22386 add_AT_flag (seg_die, DW_AT_external, 1);
22388 if (decl_name != NULL
22389 && IDENTIFIER_POINTER (decl_name) != NULL)
22391 name = dwarf2_name (decl, 1);
22392 if (! DECL_ARTIFICIAL (decl))
22393 add_src_coords_attributes (seg_die, decl);
22395 add_linkage_name (seg_die, decl);
22397 gcc_assert (name != NULL);
22398 add_pure_or_virtual_attribute (seg_die, decl);
22399 if (DECL_ARTIFICIAL (decl))
22400 add_AT_flag (seg_die, DW_AT_artificial, 1);
22402 name = concat ("__second_sect_of_", name, NULL);
22403 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
22404 fde->dw_fde_second_end, false);
22405 add_name_attribute (seg_die, name);
22406 if (want_pubnames ())
22407 add_pubname_string (name, seg_die);
22410 else
22411 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
22412 false);
22415 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
22417 /* We define the "frame base" as the function's CFA. This is more
22418 convenient for several reasons: (1) It's stable across the prologue
22419 and epilogue, which makes it better than just a frame pointer,
22420 (2) With dwarf3, there exists a one-byte encoding that allows us
22421 to reference the .debug_frame data by proxy, but failing that,
22422 (3) We can at least reuse the code inspection and interpretation
22423 code that determines the CFA position at various points in the
22424 function. */
22425 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
22427 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
22428 add_AT_loc (subr_die, DW_AT_frame_base, op);
22430 else
22432 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
22433 if (list->dw_loc_next)
22434 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
22435 else
22436 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
22439 /* Compute a displacement from the "steady-state frame pointer" to
22440 the CFA. The former is what all stack slots and argument slots
22441 will reference in the rtl; the latter is what we've told the
22442 debugger about. We'll need to adjust all frame_base references
22443 by this displacement. */
22444 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
22446 if (fun->static_chain_decl)
22448 /* DWARF requires here a location expression that computes the
22449 address of the enclosing subprogram's frame base. The machinery
22450 in tree-nested.c is supposed to store this specific address in the
22451 last field of the FRAME record. */
22452 const tree frame_type
22453 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
22454 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
22456 tree fb_expr
22457 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
22458 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
22459 fb_expr, fb_decl, NULL_TREE);
22461 add_AT_location_description (subr_die, DW_AT_static_link,
22462 loc_list_from_tree (fb_expr, 0, NULL));
22465 resolve_variable_values ();
22468 /* Generate child dies for template paramaters. */
22469 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
22470 gen_generic_params_dies (decl);
22472 /* Now output descriptions of the arguments for this function. This gets
22473 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
22474 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
22475 `...' at the end of the formal parameter list. In order to find out if
22476 there was a trailing ellipsis or not, we must instead look at the type
22477 associated with the FUNCTION_DECL. This will be a node of type
22478 FUNCTION_TYPE. If the chain of type nodes hanging off of this
22479 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
22480 an ellipsis at the end. */
22482 /* In the case where we are describing a mere function declaration, all we
22483 need to do here (and all we *can* do here) is to describe the *types* of
22484 its formal parameters. */
22485 if (debug_info_level <= DINFO_LEVEL_TERSE)
22487 else if (declaration)
22488 gen_formal_types_die (decl, subr_die);
22489 else
22491 /* Generate DIEs to represent all known formal parameters. */
22492 tree parm = DECL_ARGUMENTS (decl);
22493 tree generic_decl = early_dwarf
22494 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
22495 tree generic_decl_parm = generic_decl
22496 ? DECL_ARGUMENTS (generic_decl)
22497 : NULL;
22499 /* Now we want to walk the list of parameters of the function and
22500 emit their relevant DIEs.
22502 We consider the case of DECL being an instance of a generic function
22503 as well as it being a normal function.
22505 If DECL is an instance of a generic function we walk the
22506 parameters of the generic function declaration _and_ the parameters of
22507 DECL itself. This is useful because we want to emit specific DIEs for
22508 function parameter packs and those are declared as part of the
22509 generic function declaration. In that particular case,
22510 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
22511 That DIE has children DIEs representing the set of arguments
22512 of the pack. Note that the set of pack arguments can be empty.
22513 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
22514 children DIE.
22516 Otherwise, we just consider the parameters of DECL. */
22517 while (generic_decl_parm || parm)
22519 if (generic_decl_parm
22520 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
22521 gen_formal_parameter_pack_die (generic_decl_parm,
22522 parm, subr_die,
22523 &parm);
22524 else if (parm && !POINTER_BOUNDS_P (parm))
22526 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
22528 if (early_dwarf
22529 && parm == DECL_ARGUMENTS (decl)
22530 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
22531 && parm_die
22532 && (dwarf_version >= 3 || !dwarf_strict))
22533 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
22535 parm = DECL_CHAIN (parm);
22537 else if (parm)
22538 parm = DECL_CHAIN (parm);
22540 if (generic_decl_parm)
22541 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
22544 /* Decide whether we need an unspecified_parameters DIE at the end.
22545 There are 2 more cases to do this for: 1) the ansi ... declaration -
22546 this is detectable when the end of the arg list is not a
22547 void_type_node 2) an unprototyped function declaration (not a
22548 definition). This just means that we have no info about the
22549 parameters at all. */
22550 if (early_dwarf)
22552 if (prototype_p (TREE_TYPE (decl)))
22554 /* This is the prototyped case, check for.... */
22555 if (stdarg_p (TREE_TYPE (decl)))
22556 gen_unspecified_parameters_die (decl, subr_die);
22558 else if (DECL_INITIAL (decl) == NULL_TREE)
22559 gen_unspecified_parameters_die (decl, subr_die);
22563 if (subr_die != old_die)
22564 /* Add the calling convention attribute if requested. */
22565 add_calling_convention_attribute (subr_die, decl);
22567 /* Output Dwarf info for all of the stuff within the body of the function
22568 (if it has one - it may be just a declaration).
22570 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
22571 a function. This BLOCK actually represents the outermost binding contour
22572 for the function, i.e. the contour in which the function's formal
22573 parameters and labels get declared. Curiously, it appears that the front
22574 end doesn't actually put the PARM_DECL nodes for the current function onto
22575 the BLOCK_VARS list for this outer scope, but are strung off of the
22576 DECL_ARGUMENTS list for the function instead.
22578 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
22579 the LABEL_DECL nodes for the function however, and we output DWARF info
22580 for those in decls_for_scope. Just within the `outer_scope' there will be
22581 a BLOCK node representing the function's outermost pair of curly braces,
22582 and any blocks used for the base and member initializers of a C++
22583 constructor function. */
22584 tree outer_scope = DECL_INITIAL (decl);
22585 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
22587 int call_site_note_count = 0;
22588 int tail_call_site_note_count = 0;
22590 /* Emit a DW_TAG_variable DIE for a named return value. */
22591 if (DECL_NAME (DECL_RESULT (decl)))
22592 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
22594 /* The first time through decls_for_scope we will generate the
22595 DIEs for the locals. The second time, we fill in the
22596 location info. */
22597 decls_for_scope (outer_scope, subr_die);
22599 if (call_arg_locations && (!dwarf_strict || dwarf_version >= 5))
22601 struct call_arg_loc_node *ca_loc;
22602 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
22604 dw_die_ref die = NULL;
22605 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
22606 rtx arg, next_arg;
22608 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
22609 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
22610 : NULL_RTX);
22611 arg; arg = next_arg)
22613 dw_loc_descr_ref reg, val;
22614 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
22615 dw_die_ref cdie, tdie = NULL;
22617 next_arg = XEXP (arg, 1);
22618 if (REG_P (XEXP (XEXP (arg, 0), 0))
22619 && next_arg
22620 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
22621 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
22622 && REGNO (XEXP (XEXP (arg, 0), 0))
22623 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
22624 next_arg = XEXP (next_arg, 1);
22625 if (mode == VOIDmode)
22627 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
22628 if (mode == VOIDmode)
22629 mode = GET_MODE (XEXP (arg, 0));
22631 if (mode == VOIDmode || mode == BLKmode)
22632 continue;
22633 /* Get dynamic information about call target only if we
22634 have no static information: we cannot generate both
22635 DW_AT_call_origin and DW_AT_call_target
22636 attributes. */
22637 if (ca_loc->symbol_ref == NULL_RTX)
22639 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
22641 tloc = XEXP (XEXP (arg, 0), 1);
22642 continue;
22644 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
22645 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
22647 tlocc = XEXP (XEXP (arg, 0), 1);
22648 continue;
22651 reg = NULL;
22652 if (REG_P (XEXP (XEXP (arg, 0), 0)))
22653 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
22654 VAR_INIT_STATUS_INITIALIZED);
22655 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
22657 rtx mem = XEXP (XEXP (arg, 0), 0);
22658 reg = mem_loc_descriptor (XEXP (mem, 0),
22659 get_address_mode (mem),
22660 GET_MODE (mem),
22661 VAR_INIT_STATUS_INITIALIZED);
22663 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
22664 == DEBUG_PARAMETER_REF)
22666 tree tdecl
22667 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
22668 tdie = lookup_decl_die (tdecl);
22669 if (tdie == NULL)
22670 continue;
22672 else
22673 continue;
22674 if (reg == NULL
22675 && GET_CODE (XEXP (XEXP (arg, 0), 0))
22676 != DEBUG_PARAMETER_REF)
22677 continue;
22678 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
22679 VOIDmode,
22680 VAR_INIT_STATUS_INITIALIZED);
22681 if (val == NULL)
22682 continue;
22683 if (die == NULL)
22684 die = gen_call_site_die (decl, subr_die, ca_loc);
22685 cdie = new_die (dwarf_TAG (DW_TAG_call_site_parameter), die,
22686 NULL_TREE);
22687 if (reg != NULL)
22688 add_AT_loc (cdie, DW_AT_location, reg);
22689 else if (tdie != NULL)
22690 add_AT_die_ref (cdie, dwarf_AT (DW_AT_call_parameter),
22691 tdie);
22692 add_AT_loc (cdie, dwarf_AT (DW_AT_call_value), val);
22693 if (next_arg != XEXP (arg, 1))
22695 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
22696 if (mode == VOIDmode)
22697 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
22698 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
22699 0), 1),
22700 mode, VOIDmode,
22701 VAR_INIT_STATUS_INITIALIZED);
22702 if (val != NULL)
22703 add_AT_loc (cdie, dwarf_AT (DW_AT_call_data_value),
22704 val);
22707 if (die == NULL
22708 && (ca_loc->symbol_ref || tloc))
22709 die = gen_call_site_die (decl, subr_die, ca_loc);
22710 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
22712 dw_loc_descr_ref tval = NULL;
22714 if (tloc != NULL_RTX)
22715 tval = mem_loc_descriptor (tloc,
22716 GET_MODE (tloc) == VOIDmode
22717 ? Pmode : GET_MODE (tloc),
22718 VOIDmode,
22719 VAR_INIT_STATUS_INITIALIZED);
22720 if (tval)
22721 add_AT_loc (die, dwarf_AT (DW_AT_call_target), tval);
22722 else if (tlocc != NULL_RTX)
22724 tval = mem_loc_descriptor (tlocc,
22725 GET_MODE (tlocc) == VOIDmode
22726 ? Pmode : GET_MODE (tlocc),
22727 VOIDmode,
22728 VAR_INIT_STATUS_INITIALIZED);
22729 if (tval)
22730 add_AT_loc (die,
22731 dwarf_AT (DW_AT_call_target_clobbered),
22732 tval);
22735 if (die != NULL)
22737 call_site_note_count++;
22738 if (ca_loc->tail_call_p)
22739 tail_call_site_note_count++;
22743 call_arg_locations = NULL;
22744 call_arg_loc_last = NULL;
22745 if (tail_call_site_count >= 0
22746 && tail_call_site_count == tail_call_site_note_count
22747 && (!dwarf_strict || dwarf_version >= 5))
22749 if (call_site_count >= 0
22750 && call_site_count == call_site_note_count)
22751 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_calls), 1);
22752 else
22753 add_AT_flag (subr_die, dwarf_AT (DW_AT_call_all_tail_calls), 1);
22755 call_site_count = -1;
22756 tail_call_site_count = -1;
22759 /* Mark used types after we have created DIEs for the functions scopes. */
22760 premark_used_types (DECL_STRUCT_FUNCTION (decl));
22763 /* Returns a hash value for X (which really is a die_struct). */
22765 hashval_t
22766 block_die_hasher::hash (die_struct *d)
22768 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
22771 /* Return nonzero if decl_id and die_parent of die_struct X is the same
22772 as decl_id and die_parent of die_struct Y. */
22774 bool
22775 block_die_hasher::equal (die_struct *x, die_struct *y)
22777 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
22780 /* Return TRUE if DECL, which may have been previously generated as
22781 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
22782 true if decl (or its origin) is either an extern declaration or a
22783 class/namespace scoped declaration.
22785 The declare_in_namespace support causes us to get two DIEs for one
22786 variable, both of which are declarations. We want to avoid
22787 considering one to be a specification, so we must test for
22788 DECLARATION and DW_AT_declaration. */
22789 static inline bool
22790 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
22792 return (old_die && TREE_STATIC (decl) && !declaration
22793 && get_AT_flag (old_die, DW_AT_declaration) == 1);
22796 /* Return true if DECL is a local static. */
22798 static inline bool
22799 local_function_static (tree decl)
22801 gcc_assert (VAR_P (decl));
22802 return TREE_STATIC (decl)
22803 && DECL_CONTEXT (decl)
22804 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
22807 /* Generate a DIE to represent a declared data object.
22808 Either DECL or ORIGIN must be non-null. */
22810 static void
22811 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
22813 HOST_WIDE_INT off = 0;
22814 tree com_decl;
22815 tree decl_or_origin = decl ? decl : origin;
22816 tree ultimate_origin;
22817 dw_die_ref var_die;
22818 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
22819 bool declaration = (DECL_EXTERNAL (decl_or_origin)
22820 || class_or_namespace_scope_p (context_die));
22821 bool specialization_p = false;
22822 bool no_linkage_name = false;
22824 /* While C++ inline static data members have definitions inside of the
22825 class, force the first DIE to be a declaration, then let gen_member_die
22826 reparent it to the class context and call gen_variable_die again
22827 to create the outside of the class DIE for the definition. */
22828 if (!declaration
22829 && old_die == NULL
22830 && decl
22831 && DECL_CONTEXT (decl)
22832 && TYPE_P (DECL_CONTEXT (decl))
22833 && lang_hooks.decls.decl_dwarf_attribute (decl, DW_AT_inline) != -1)
22835 declaration = true;
22836 if (dwarf_version < 5)
22837 no_linkage_name = true;
22840 ultimate_origin = decl_ultimate_origin (decl_or_origin);
22841 if (decl || ultimate_origin)
22842 origin = ultimate_origin;
22843 com_decl = fortran_common (decl_or_origin, &off);
22845 /* Symbol in common gets emitted as a child of the common block, in the form
22846 of a data member. */
22847 if (com_decl)
22849 dw_die_ref com_die;
22850 dw_loc_list_ref loc = NULL;
22851 die_node com_die_arg;
22853 var_die = lookup_decl_die (decl_or_origin);
22854 if (var_die)
22856 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
22858 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
22859 if (loc)
22861 if (off)
22863 /* Optimize the common case. */
22864 if (single_element_loc_list_p (loc)
22865 && loc->expr->dw_loc_opc == DW_OP_addr
22866 && loc->expr->dw_loc_next == NULL
22867 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
22868 == SYMBOL_REF)
22870 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22871 loc->expr->dw_loc_oprnd1.v.val_addr
22872 = plus_constant (GET_MODE (x), x , off);
22874 else
22875 loc_list_plus_const (loc, off);
22877 add_AT_location_description (var_die, DW_AT_location, loc);
22878 remove_AT (var_die, DW_AT_declaration);
22881 return;
22884 if (common_block_die_table == NULL)
22885 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
22887 com_die_arg.decl_id = DECL_UID (com_decl);
22888 com_die_arg.die_parent = context_die;
22889 com_die = common_block_die_table->find (&com_die_arg);
22890 if (! early_dwarf)
22891 loc = loc_list_from_tree (com_decl, 2, NULL);
22892 if (com_die == NULL)
22894 const char *cnam
22895 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
22896 die_node **slot;
22898 com_die = new_die (DW_TAG_common_block, context_die, decl);
22899 add_name_and_src_coords_attributes (com_die, com_decl);
22900 if (loc)
22902 add_AT_location_description (com_die, DW_AT_location, loc);
22903 /* Avoid sharing the same loc descriptor between
22904 DW_TAG_common_block and DW_TAG_variable. */
22905 loc = loc_list_from_tree (com_decl, 2, NULL);
22907 else if (DECL_EXTERNAL (decl_or_origin))
22908 add_AT_flag (com_die, DW_AT_declaration, 1);
22909 if (want_pubnames ())
22910 add_pubname_string (cnam, com_die); /* ??? needed? */
22911 com_die->decl_id = DECL_UID (com_decl);
22912 slot = common_block_die_table->find_slot (com_die, INSERT);
22913 *slot = com_die;
22915 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
22917 add_AT_location_description (com_die, DW_AT_location, loc);
22918 loc = loc_list_from_tree (com_decl, 2, NULL);
22919 remove_AT (com_die, DW_AT_declaration);
22921 var_die = new_die (DW_TAG_variable, com_die, decl);
22922 add_name_and_src_coords_attributes (var_die, decl_or_origin);
22923 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
22924 decl_quals (decl_or_origin), false,
22925 context_die);
22926 add_alignment_attribute (var_die, decl);
22927 add_AT_flag (var_die, DW_AT_external, 1);
22928 if (loc)
22930 if (off)
22932 /* Optimize the common case. */
22933 if (single_element_loc_list_p (loc)
22934 && loc->expr->dw_loc_opc == DW_OP_addr
22935 && loc->expr->dw_loc_next == NULL
22936 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
22938 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
22939 loc->expr->dw_loc_oprnd1.v.val_addr
22940 = plus_constant (GET_MODE (x), x, off);
22942 else
22943 loc_list_plus_const (loc, off);
22945 add_AT_location_description (var_die, DW_AT_location, loc);
22947 else if (DECL_EXTERNAL (decl_or_origin))
22948 add_AT_flag (var_die, DW_AT_declaration, 1);
22949 if (decl)
22950 equate_decl_number_to_die (decl, var_die);
22951 return;
22954 if (old_die)
22956 if (declaration)
22958 /* A declaration that has been previously dumped, needs no
22959 further annotations, since it doesn't need location on
22960 the second pass. */
22961 return;
22963 else if (decl_will_get_specification_p (old_die, decl, declaration)
22964 && !get_AT (old_die, DW_AT_specification))
22966 /* Fall-thru so we can make a new variable die along with a
22967 DW_AT_specification. */
22969 else if (origin && old_die->die_parent != context_die)
22971 /* If we will be creating an inlined instance, we need a
22972 new DIE that will get annotated with
22973 DW_AT_abstract_origin. Clear things so we can get a
22974 new DIE. */
22975 gcc_assert (!DECL_ABSTRACT_P (decl));
22976 old_die = NULL;
22978 else
22980 /* If a DIE was dumped early, it still needs location info.
22981 Skip to where we fill the location bits. */
22982 var_die = old_die;
22984 /* ??? In LTRANS we cannot annotate early created variably
22985 modified type DIEs without copying them and adjusting all
22986 references to them. Thus we dumped them again, also add a
22987 reference to them. */
22988 tree type = TREE_TYPE (decl_or_origin);
22989 if (in_lto_p
22990 && variably_modified_type_p
22991 (type, decl_function_context (decl_or_origin)))
22993 if (decl_by_reference_p (decl_or_origin))
22994 add_type_attribute (var_die, TREE_TYPE (type),
22995 TYPE_UNQUALIFIED, false, context_die);
22996 else
22997 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
22998 false, context_die);
23001 goto gen_variable_die_location;
23005 /* For static data members, the declaration in the class is supposed
23006 to have DW_TAG_member tag in DWARF{3,4} and we emit it for compatibility
23007 also in DWARF2; the specification should still be DW_TAG_variable
23008 referencing the DW_TAG_member DIE. */
23009 if (declaration && class_scope_p (context_die) && dwarf_version < 5)
23010 var_die = new_die (DW_TAG_member, context_die, decl);
23011 else
23012 var_die = new_die (DW_TAG_variable, context_die, decl);
23014 if (origin != NULL)
23015 add_abstract_origin_attribute (var_die, origin);
23017 /* Loop unrolling can create multiple blocks that refer to the same
23018 static variable, so we must test for the DW_AT_declaration flag.
23020 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
23021 copy decls and set the DECL_ABSTRACT_P flag on them instead of
23022 sharing them.
23024 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
23025 else if (decl_will_get_specification_p (old_die, decl, declaration))
23027 /* This is a definition of a C++ class level static. */
23028 add_AT_specification (var_die, old_die);
23029 specialization_p = true;
23030 if (DECL_NAME (decl))
23032 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
23033 struct dwarf_file_data * file_index = lookup_filename (s.file);
23035 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
23036 add_AT_file (var_die, DW_AT_decl_file, file_index);
23038 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
23039 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
23041 if (debug_column_info
23042 && s.column
23043 && (get_AT_unsigned (old_die, DW_AT_decl_column)
23044 != (unsigned) s.column))
23045 add_AT_unsigned (var_die, DW_AT_decl_column, s.column);
23047 if (old_die->die_tag == DW_TAG_member)
23048 add_linkage_name (var_die, decl);
23051 else
23052 add_name_and_src_coords_attributes (var_die, decl, no_linkage_name);
23054 if ((origin == NULL && !specialization_p)
23055 || (origin != NULL
23056 && !DECL_ABSTRACT_P (decl_or_origin)
23057 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
23058 decl_function_context
23059 (decl_or_origin))))
23061 tree type = TREE_TYPE (decl_or_origin);
23063 if (decl_by_reference_p (decl_or_origin))
23064 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23065 context_die);
23066 else
23067 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
23068 context_die);
23071 if (origin == NULL && !specialization_p)
23073 if (TREE_PUBLIC (decl))
23074 add_AT_flag (var_die, DW_AT_external, 1);
23076 if (DECL_ARTIFICIAL (decl))
23077 add_AT_flag (var_die, DW_AT_artificial, 1);
23079 add_alignment_attribute (var_die, decl);
23081 add_accessibility_attribute (var_die, decl);
23084 if (declaration)
23085 add_AT_flag (var_die, DW_AT_declaration, 1);
23087 if (decl && (DECL_ABSTRACT_P (decl)
23088 || !old_die || is_declaration_die (old_die)))
23089 equate_decl_number_to_die (decl, var_die);
23091 gen_variable_die_location:
23092 if (! declaration
23093 && (! DECL_ABSTRACT_P (decl_or_origin)
23094 /* Local static vars are shared between all clones/inlines,
23095 so emit DW_AT_location on the abstract DIE if DECL_RTL is
23096 already set. */
23097 || (VAR_P (decl_or_origin)
23098 && TREE_STATIC (decl_or_origin)
23099 && DECL_RTL_SET_P (decl_or_origin))))
23101 if (early_dwarf)
23102 add_pubname (decl_or_origin, var_die);
23103 else
23104 add_location_or_const_value_attribute (var_die, decl_or_origin,
23105 decl == NULL);
23107 else
23108 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
23110 if ((dwarf_version >= 4 || !dwarf_strict)
23111 && lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23112 DW_AT_const_expr) == 1
23113 && !get_AT (var_die, DW_AT_const_expr)
23114 && !specialization_p)
23115 add_AT_flag (var_die, DW_AT_const_expr, 1);
23117 if (!dwarf_strict)
23119 int inl = lang_hooks.decls.decl_dwarf_attribute (decl_or_origin,
23120 DW_AT_inline);
23121 if (inl != -1
23122 && !get_AT (var_die, DW_AT_inline)
23123 && !specialization_p)
23124 add_AT_unsigned (var_die, DW_AT_inline, inl);
23128 /* Generate a DIE to represent a named constant. */
23130 static void
23131 gen_const_die (tree decl, dw_die_ref context_die)
23133 dw_die_ref const_die;
23134 tree type = TREE_TYPE (decl);
23136 const_die = lookup_decl_die (decl);
23137 if (const_die)
23138 return;
23140 const_die = new_die (DW_TAG_constant, context_die, decl);
23141 equate_decl_number_to_die (decl, const_die);
23142 add_name_and_src_coords_attributes (const_die, decl);
23143 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
23144 if (TREE_PUBLIC (decl))
23145 add_AT_flag (const_die, DW_AT_external, 1);
23146 if (DECL_ARTIFICIAL (decl))
23147 add_AT_flag (const_die, DW_AT_artificial, 1);
23148 tree_add_const_value_attribute_for_decl (const_die, decl);
23151 /* Generate a DIE to represent a label identifier. */
23153 static void
23154 gen_label_die (tree decl, dw_die_ref context_die)
23156 tree origin = decl_ultimate_origin (decl);
23157 dw_die_ref lbl_die = lookup_decl_die (decl);
23158 rtx insn;
23159 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23161 if (!lbl_die)
23163 lbl_die = new_die (DW_TAG_label, context_die, decl);
23164 equate_decl_number_to_die (decl, lbl_die);
23166 if (origin != NULL)
23167 add_abstract_origin_attribute (lbl_die, origin);
23168 else
23169 add_name_and_src_coords_attributes (lbl_die, decl);
23172 if (DECL_ABSTRACT_P (decl))
23173 equate_decl_number_to_die (decl, lbl_die);
23174 else if (! early_dwarf)
23176 insn = DECL_RTL_IF_SET (decl);
23178 /* Deleted labels are programmer specified labels which have been
23179 eliminated because of various optimizations. We still emit them
23180 here so that it is possible to put breakpoints on them. */
23181 if (insn
23182 && (LABEL_P (insn)
23183 || ((NOTE_P (insn)
23184 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
23186 /* When optimization is enabled (via -O) some parts of the compiler
23187 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
23188 represent source-level labels which were explicitly declared by
23189 the user. This really shouldn't be happening though, so catch
23190 it if it ever does happen. */
23191 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
23193 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
23194 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23196 else if (insn
23197 && NOTE_P (insn)
23198 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
23199 && CODE_LABEL_NUMBER (insn) != -1)
23201 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
23202 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
23207 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
23208 attributes to the DIE for a block STMT, to describe where the inlined
23209 function was called from. This is similar to add_src_coords_attributes. */
23211 static inline void
23212 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
23214 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
23216 if (dwarf_version >= 3 || !dwarf_strict)
23218 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
23219 add_AT_unsigned (die, DW_AT_call_line, s.line);
23220 if (debug_column_info && s.column)
23221 add_AT_unsigned (die, DW_AT_call_column, s.column);
23226 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
23227 Add low_pc and high_pc attributes to the DIE for a block STMT. */
23229 static inline void
23230 add_high_low_attributes (tree stmt, dw_die_ref die)
23232 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23234 if (BLOCK_FRAGMENT_CHAIN (stmt)
23235 && (dwarf_version >= 3 || !dwarf_strict))
23237 tree chain, superblock = NULL_TREE;
23238 dw_die_ref pdie;
23239 dw_attr_node *attr = NULL;
23241 if (inlined_function_outer_scope_p (stmt))
23243 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23244 BLOCK_NUMBER (stmt));
23245 add_AT_lbl_id (die, DW_AT_entry_pc, label);
23248 /* Optimize duplicate .debug_ranges lists or even tails of
23249 lists. If this BLOCK has same ranges as its supercontext,
23250 lookup DW_AT_ranges attribute in the supercontext (and
23251 recursively so), verify that the ranges_table contains the
23252 right values and use it instead of adding a new .debug_range. */
23253 for (chain = stmt, pdie = die;
23254 BLOCK_SAME_RANGE (chain);
23255 chain = BLOCK_SUPERCONTEXT (chain))
23257 dw_attr_node *new_attr;
23259 pdie = pdie->die_parent;
23260 if (pdie == NULL)
23261 break;
23262 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
23263 break;
23264 new_attr = get_AT (pdie, DW_AT_ranges);
23265 if (new_attr == NULL
23266 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
23267 break;
23268 attr = new_attr;
23269 superblock = BLOCK_SUPERCONTEXT (chain);
23271 if (attr != NULL
23272 && ((*ranges_table)[attr->dw_attr_val.v.val_offset].num
23273 == BLOCK_NUMBER (superblock))
23274 && BLOCK_FRAGMENT_CHAIN (superblock))
23276 unsigned long off = attr->dw_attr_val.v.val_offset;
23277 unsigned long supercnt = 0, thiscnt = 0;
23278 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
23279 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23281 ++supercnt;
23282 gcc_checking_assert ((*ranges_table)[off + supercnt].num
23283 == BLOCK_NUMBER (chain));
23285 gcc_checking_assert ((*ranges_table)[off + supercnt + 1].num == 0);
23286 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
23287 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
23288 ++thiscnt;
23289 gcc_assert (supercnt >= thiscnt);
23290 add_AT_range_list (die, DW_AT_ranges, off + supercnt - thiscnt,
23291 false);
23292 note_rnglist_head (off + supercnt - thiscnt);
23293 return;
23296 unsigned int offset = add_ranges (stmt, true);
23297 add_AT_range_list (die, DW_AT_ranges, offset, false);
23298 note_rnglist_head (offset);
23300 bool prev_in_cold = BLOCK_IN_COLD_SECTION_P (stmt);
23301 chain = BLOCK_FRAGMENT_CHAIN (stmt);
23304 add_ranges (chain, prev_in_cold != BLOCK_IN_COLD_SECTION_P (chain));
23305 prev_in_cold = BLOCK_IN_COLD_SECTION_P (chain);
23306 chain = BLOCK_FRAGMENT_CHAIN (chain);
23308 while (chain);
23309 add_ranges (NULL);
23311 else
23313 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
23314 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
23315 BLOCK_NUMBER (stmt));
23316 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
23317 BLOCK_NUMBER (stmt));
23318 add_AT_low_high_pc (die, label, label_high, false);
23322 /* Generate a DIE for a lexical block. */
23324 static void
23325 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
23327 dw_die_ref old_die = BLOCK_DIE (stmt);
23328 dw_die_ref stmt_die = NULL;
23329 if (!old_die)
23331 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23332 BLOCK_DIE (stmt) = stmt_die;
23335 if (BLOCK_ABSTRACT (stmt))
23337 if (old_die)
23339 /* This must have been generated early and it won't even
23340 need location information since it's a DW_AT_inline
23341 function. */
23342 if (flag_checking)
23343 for (dw_die_ref c = context_die; c; c = c->die_parent)
23344 if (c->die_tag == DW_TAG_inlined_subroutine
23345 || c->die_tag == DW_TAG_subprogram)
23347 gcc_assert (get_AT (c, DW_AT_inline));
23348 break;
23350 return;
23353 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
23355 /* If this is an inlined instance, create a new lexical die for
23356 anything below to attach DW_AT_abstract_origin to. */
23357 if (old_die)
23359 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
23360 BLOCK_DIE (stmt) = stmt_die;
23361 old_die = NULL;
23364 tree origin = block_ultimate_origin (stmt);
23365 if (origin != NULL_TREE && origin != stmt)
23366 add_abstract_origin_attribute (stmt_die, origin);
23369 if (old_die)
23370 stmt_die = old_die;
23372 /* A non abstract block whose blocks have already been reordered
23373 should have the instruction range for this block. If so, set the
23374 high/low attributes. */
23375 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
23377 gcc_assert (stmt_die);
23378 add_high_low_attributes (stmt, stmt_die);
23381 decls_for_scope (stmt, stmt_die);
23384 /* Generate a DIE for an inlined subprogram. */
23386 static void
23387 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
23389 tree decl;
23391 /* The instance of function that is effectively being inlined shall not
23392 be abstract. */
23393 gcc_assert (! BLOCK_ABSTRACT (stmt));
23395 decl = block_ultimate_origin (stmt);
23397 /* Make sure any inlined functions are known to be inlineable. */
23398 gcc_checking_assert (DECL_ABSTRACT_P (decl)
23399 || cgraph_function_possibly_inlined_p (decl));
23401 if (! BLOCK_ABSTRACT (stmt))
23403 dw_die_ref subr_die
23404 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
23406 if (call_arg_locations)
23407 BLOCK_DIE (stmt) = subr_die;
23408 add_abstract_origin_attribute (subr_die, decl);
23409 if (TREE_ASM_WRITTEN (stmt))
23410 add_high_low_attributes (stmt, subr_die);
23411 add_call_src_coords_attributes (stmt, subr_die);
23413 decls_for_scope (stmt, subr_die);
23417 /* Generate a DIE for a field in a record, or structure. CTX is required: see
23418 the comment for VLR_CONTEXT. */
23420 static void
23421 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
23423 dw_die_ref decl_die;
23425 if (TREE_TYPE (decl) == error_mark_node)
23426 return;
23428 decl_die = new_die (DW_TAG_member, context_die, decl);
23429 add_name_and_src_coords_attributes (decl_die, decl);
23430 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
23431 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
23432 context_die);
23434 if (DECL_BIT_FIELD_TYPE (decl))
23436 add_byte_size_attribute (decl_die, decl);
23437 add_bit_size_attribute (decl_die, decl);
23438 add_bit_offset_attribute (decl_die, decl, ctx);
23441 add_alignment_attribute (decl_die, decl);
23443 /* If we have a variant part offset, then we are supposed to process a member
23444 of a QUAL_UNION_TYPE, which is how we represent variant parts in
23445 trees. */
23446 gcc_assert (ctx->variant_part_offset == NULL_TREE
23447 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
23448 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
23449 add_data_member_location_attribute (decl_die, decl, ctx);
23451 if (DECL_ARTIFICIAL (decl))
23452 add_AT_flag (decl_die, DW_AT_artificial, 1);
23454 add_accessibility_attribute (decl_die, decl);
23456 /* Equate decl number to die, so that we can look up this decl later on. */
23457 equate_decl_number_to_die (decl, decl_die);
23460 /* Generate a DIE for a pointer to a member type. TYPE can be an
23461 OFFSET_TYPE, for a pointer to data member, or a RECORD_TYPE, for a
23462 pointer to member function. */
23464 static void
23465 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
23467 if (lookup_type_die (type))
23468 return;
23470 dw_die_ref ptr_die = new_die (DW_TAG_ptr_to_member_type,
23471 scope_die_for (type, context_die), type);
23473 equate_type_number_to_die (type, ptr_die);
23474 add_AT_die_ref (ptr_die, DW_AT_containing_type,
23475 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
23476 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
23477 context_die);
23478 add_alignment_attribute (ptr_die, type);
23480 if (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
23481 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)
23483 dw_loc_descr_ref op = new_loc_descr (DW_OP_plus, 0, 0);
23484 add_AT_loc (ptr_die, DW_AT_use_location, op);
23488 static char *producer_string;
23490 /* Return a heap allocated producer string including command line options
23491 if -grecord-gcc-switches. */
23493 static char *
23494 gen_producer_string (void)
23496 size_t j;
23497 auto_vec<const char *> switches;
23498 const char *language_string = lang_hooks.name;
23499 char *producer, *tail;
23500 const char *p;
23501 size_t len = dwarf_record_gcc_switches ? 0 : 3;
23502 size_t plen = strlen (language_string) + 1 + strlen (version_string);
23504 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
23505 switch (save_decoded_options[j].opt_index)
23507 case OPT_o:
23508 case OPT_d:
23509 case OPT_dumpbase:
23510 case OPT_dumpdir:
23511 case OPT_auxbase:
23512 case OPT_auxbase_strip:
23513 case OPT_quiet:
23514 case OPT_version:
23515 case OPT_v:
23516 case OPT_w:
23517 case OPT_L:
23518 case OPT_D:
23519 case OPT_I:
23520 case OPT_U:
23521 case OPT_SPECIAL_unknown:
23522 case OPT_SPECIAL_ignore:
23523 case OPT_SPECIAL_program_name:
23524 case OPT_SPECIAL_input_file:
23525 case OPT_grecord_gcc_switches:
23526 case OPT__output_pch_:
23527 case OPT_fdiagnostics_show_location_:
23528 case OPT_fdiagnostics_show_option:
23529 case OPT_fdiagnostics_show_caret:
23530 case OPT_fdiagnostics_color_:
23531 case OPT_fverbose_asm:
23532 case OPT____:
23533 case OPT__sysroot_:
23534 case OPT_nostdinc:
23535 case OPT_nostdinc__:
23536 case OPT_fpreprocessed:
23537 case OPT_fltrans_output_list_:
23538 case OPT_fresolution_:
23539 case OPT_fdebug_prefix_map_:
23540 case OPT_fcompare_debug:
23541 /* Ignore these. */
23542 continue;
23543 default:
23544 if (cl_options[save_decoded_options[j].opt_index].flags
23545 & CL_NO_DWARF_RECORD)
23546 continue;
23547 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
23548 == '-');
23549 switch (save_decoded_options[j].canonical_option[0][1])
23551 case 'M':
23552 case 'i':
23553 case 'W':
23554 continue;
23555 case 'f':
23556 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
23557 "dump", 4) == 0)
23558 continue;
23559 break;
23560 default:
23561 break;
23563 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
23564 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
23565 break;
23568 producer = XNEWVEC (char, plen + 1 + len + 1);
23569 tail = producer;
23570 sprintf (tail, "%s %s", language_string, version_string);
23571 tail += plen;
23573 FOR_EACH_VEC_ELT (switches, j, p)
23575 len = strlen (p);
23576 *tail = ' ';
23577 memcpy (tail + 1, p, len);
23578 tail += len + 1;
23581 *tail = '\0';
23582 return producer;
23585 /* Given a C and/or C++ language/version string return the "highest".
23586 C++ is assumed to be "higher" than C in this case. Used for merging
23587 LTO translation unit languages. */
23588 static const char *
23589 highest_c_language (const char *lang1, const char *lang2)
23591 if (strcmp ("GNU C++17", lang1) == 0 || strcmp ("GNU C++17", lang2) == 0)
23592 return "GNU C++17";
23593 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
23594 return "GNU C++14";
23595 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
23596 return "GNU C++11";
23597 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
23598 return "GNU C++98";
23600 if (strcmp ("GNU C17", lang1) == 0 || strcmp ("GNU C17", lang2) == 0)
23601 return "GNU C17";
23602 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
23603 return "GNU C11";
23604 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
23605 return "GNU C99";
23606 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
23607 return "GNU C89";
23609 gcc_unreachable ();
23613 /* Generate the DIE for the compilation unit. */
23615 static dw_die_ref
23616 gen_compile_unit_die (const char *filename)
23618 dw_die_ref die;
23619 const char *language_string = lang_hooks.name;
23620 int language;
23622 die = new_die (DW_TAG_compile_unit, NULL, NULL);
23624 if (filename)
23626 add_name_attribute (die, filename);
23627 /* Don't add cwd for <built-in>. */
23628 if (filename[0] != '<')
23629 add_comp_dir_attribute (die);
23632 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
23634 /* If our producer is LTO try to figure out a common language to use
23635 from the global list of translation units. */
23636 if (strcmp (language_string, "GNU GIMPLE") == 0)
23638 unsigned i;
23639 tree t;
23640 const char *common_lang = NULL;
23642 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
23644 if (!TRANSLATION_UNIT_LANGUAGE (t))
23645 continue;
23646 if (!common_lang)
23647 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
23648 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
23650 else if (strncmp (common_lang, "GNU C", 5) == 0
23651 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
23652 /* Mixing C and C++ is ok, use C++ in that case. */
23653 common_lang = highest_c_language (common_lang,
23654 TRANSLATION_UNIT_LANGUAGE (t));
23655 else
23657 /* Fall back to C. */
23658 common_lang = NULL;
23659 break;
23663 if (common_lang)
23664 language_string = common_lang;
23667 language = DW_LANG_C;
23668 if (strncmp (language_string, "GNU C", 5) == 0
23669 && ISDIGIT (language_string[5]))
23671 language = DW_LANG_C89;
23672 if (dwarf_version >= 3 || !dwarf_strict)
23674 if (strcmp (language_string, "GNU C89") != 0)
23675 language = DW_LANG_C99;
23677 if (dwarf_version >= 5 /* || !dwarf_strict */)
23678 if (strcmp (language_string, "GNU C11") == 0
23679 || strcmp (language_string, "GNU C17") == 0)
23680 language = DW_LANG_C11;
23683 else if (strncmp (language_string, "GNU C++", 7) == 0)
23685 language = DW_LANG_C_plus_plus;
23686 if (dwarf_version >= 5 /* || !dwarf_strict */)
23688 if (strcmp (language_string, "GNU C++11") == 0)
23689 language = DW_LANG_C_plus_plus_11;
23690 else if (strcmp (language_string, "GNU C++14") == 0)
23691 language = DW_LANG_C_plus_plus_14;
23692 else if (strcmp (language_string, "GNU C++17") == 0)
23693 /* For now. */
23694 language = DW_LANG_C_plus_plus_14;
23697 else if (strcmp (language_string, "GNU F77") == 0)
23698 language = DW_LANG_Fortran77;
23699 else if (dwarf_version >= 3 || !dwarf_strict)
23701 if (strcmp (language_string, "GNU Ada") == 0)
23702 language = DW_LANG_Ada95;
23703 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23705 language = DW_LANG_Fortran95;
23706 if (dwarf_version >= 5 /* || !dwarf_strict */)
23708 if (strcmp (language_string, "GNU Fortran2003") == 0)
23709 language = DW_LANG_Fortran03;
23710 else if (strcmp (language_string, "GNU Fortran2008") == 0)
23711 language = DW_LANG_Fortran08;
23714 else if (strcmp (language_string, "GNU Objective-C") == 0)
23715 language = DW_LANG_ObjC;
23716 else if (strcmp (language_string, "GNU Objective-C++") == 0)
23717 language = DW_LANG_ObjC_plus_plus;
23718 else if (dwarf_version >= 5 || !dwarf_strict)
23720 if (strcmp (language_string, "GNU Go") == 0)
23721 language = DW_LANG_Go;
23724 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
23725 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
23726 language = DW_LANG_Fortran90;
23728 add_AT_unsigned (die, DW_AT_language, language);
23730 switch (language)
23732 case DW_LANG_Fortran77:
23733 case DW_LANG_Fortran90:
23734 case DW_LANG_Fortran95:
23735 case DW_LANG_Fortran03:
23736 case DW_LANG_Fortran08:
23737 /* Fortran has case insensitive identifiers and the front-end
23738 lowercases everything. */
23739 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
23740 break;
23741 default:
23742 /* The default DW_ID_case_sensitive doesn't need to be specified. */
23743 break;
23745 return die;
23748 /* Generate the DIE for a base class. */
23750 static void
23751 gen_inheritance_die (tree binfo, tree access, tree type,
23752 dw_die_ref context_die)
23754 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
23755 struct vlr_context ctx = { type, NULL };
23757 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
23758 context_die);
23759 add_data_member_location_attribute (die, binfo, &ctx);
23761 if (BINFO_VIRTUAL_P (binfo))
23762 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
23764 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
23765 children, otherwise the default is DW_ACCESS_public. In DWARF2
23766 the default has always been DW_ACCESS_private. */
23767 if (access == access_public_node)
23769 if (dwarf_version == 2
23770 || context_die->die_tag == DW_TAG_class_type)
23771 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
23773 else if (access == access_protected_node)
23774 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
23775 else if (dwarf_version > 2
23776 && context_die->die_tag != DW_TAG_class_type)
23777 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
23780 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
23781 structure. */
23782 static bool
23783 is_variant_part (tree decl)
23785 return (TREE_CODE (decl) == FIELD_DECL
23786 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
23789 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
23790 return the FIELD_DECL. Return NULL_TREE otherwise. */
23792 static tree
23793 analyze_discr_in_predicate (tree operand, tree struct_type)
23795 bool continue_stripping = true;
23796 while (continue_stripping)
23797 switch (TREE_CODE (operand))
23799 CASE_CONVERT:
23800 operand = TREE_OPERAND (operand, 0);
23801 break;
23802 default:
23803 continue_stripping = false;
23804 break;
23807 /* Match field access to members of struct_type only. */
23808 if (TREE_CODE (operand) == COMPONENT_REF
23809 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
23810 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
23811 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
23812 return TREE_OPERAND (operand, 1);
23813 else
23814 return NULL_TREE;
23817 /* Check that SRC is a constant integer that can be represented as a native
23818 integer constant (either signed or unsigned). If so, store it into DEST and
23819 return true. Return false otherwise. */
23821 static bool
23822 get_discr_value (tree src, dw_discr_value *dest)
23824 tree discr_type = TREE_TYPE (src);
23826 if (lang_hooks.types.get_debug_type)
23828 tree debug_type = lang_hooks.types.get_debug_type (discr_type);
23829 if (debug_type != NULL)
23830 discr_type = debug_type;
23833 if (TREE_CODE (src) != INTEGER_CST || !INTEGRAL_TYPE_P (discr_type))
23834 return false;
23836 /* Signedness can vary between the original type and the debug type. This
23837 can happen for character types in Ada for instance: the character type
23838 used for code generation can be signed, to be compatible with the C one,
23839 but from a debugger point of view, it must be unsigned. */
23840 bool is_orig_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
23841 bool is_debug_unsigned = TYPE_UNSIGNED (discr_type);
23843 if (is_orig_unsigned != is_debug_unsigned)
23844 src = fold_convert (discr_type, src);
23846 if (!(is_debug_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
23847 return false;
23849 dest->pos = is_debug_unsigned;
23850 if (is_debug_unsigned)
23851 dest->v.uval = tree_to_uhwi (src);
23852 else
23853 dest->v.sval = tree_to_shwi (src);
23855 return true;
23858 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
23859 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
23860 store NULL_TREE in DISCR_DECL. Otherwise:
23862 - store the discriminant field in STRUCT_TYPE that controls the variant
23863 part to *DISCR_DECL
23865 - put in *DISCR_LISTS_P an array where for each variant, the item
23866 represents the corresponding matching list of discriminant values.
23868 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
23869 the above array.
23871 Note that when the array is allocated (i.e. when the analysis is
23872 successful), it is up to the caller to free the array. */
23874 static void
23875 analyze_variants_discr (tree variant_part_decl,
23876 tree struct_type,
23877 tree *discr_decl,
23878 dw_discr_list_ref **discr_lists_p,
23879 unsigned *discr_lists_length)
23881 tree variant_part_type = TREE_TYPE (variant_part_decl);
23882 tree variant;
23883 dw_discr_list_ref *discr_lists;
23884 unsigned i;
23886 /* Compute how many variants there are in this variant part. */
23887 *discr_lists_length = 0;
23888 for (variant = TYPE_FIELDS (variant_part_type);
23889 variant != NULL_TREE;
23890 variant = DECL_CHAIN (variant))
23891 ++*discr_lists_length;
23893 *discr_decl = NULL_TREE;
23894 *discr_lists_p
23895 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
23896 sizeof (**discr_lists_p));
23897 discr_lists = *discr_lists_p;
23899 /* And then analyze all variants to extract discriminant information for all
23900 of them. This analysis is conservative: as soon as we detect something we
23901 do not support, abort everything and pretend we found nothing. */
23902 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
23903 variant != NULL_TREE;
23904 variant = DECL_CHAIN (variant), ++i)
23906 tree match_expr = DECL_QUALIFIER (variant);
23908 /* Now, try to analyze the predicate and deduce a discriminant for
23909 it. */
23910 if (match_expr == boolean_true_node)
23911 /* Typically happens for the default variant: it matches all cases that
23912 previous variants rejected. Don't output any matching value for
23913 this one. */
23914 continue;
23916 /* The following loop tries to iterate over each discriminant
23917 possibility: single values or ranges. */
23918 while (match_expr != NULL_TREE)
23920 tree next_round_match_expr;
23921 tree candidate_discr = NULL_TREE;
23922 dw_discr_list_ref new_node = NULL;
23924 /* Possibilities are matched one after the other by nested
23925 TRUTH_ORIF_EXPR expressions. Process the current possibility and
23926 continue with the rest at next iteration. */
23927 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
23929 next_round_match_expr = TREE_OPERAND (match_expr, 0);
23930 match_expr = TREE_OPERAND (match_expr, 1);
23932 else
23933 next_round_match_expr = NULL_TREE;
23935 if (match_expr == boolean_false_node)
23936 /* This sub-expression matches nothing: just wait for the next
23937 one. */
23940 else if (TREE_CODE (match_expr) == EQ_EXPR)
23942 /* We are matching: <discr_field> == <integer_cst>
23943 This sub-expression matches a single value. */
23944 tree integer_cst = TREE_OPERAND (match_expr, 1);
23946 candidate_discr
23947 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
23948 struct_type);
23950 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
23951 if (!get_discr_value (integer_cst,
23952 &new_node->dw_discr_lower_bound))
23953 goto abort;
23954 new_node->dw_discr_range = false;
23957 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
23959 /* We are matching:
23960 <discr_field> > <integer_cst>
23961 && <discr_field> < <integer_cst>.
23962 This sub-expression matches the range of values between the
23963 two matched integer constants. Note that comparisons can be
23964 inclusive or exclusive. */
23965 tree candidate_discr_1, candidate_discr_2;
23966 tree lower_cst, upper_cst;
23967 bool lower_cst_included, upper_cst_included;
23968 tree lower_op = TREE_OPERAND (match_expr, 0);
23969 tree upper_op = TREE_OPERAND (match_expr, 1);
23971 /* When the comparison is exclusive, the integer constant is not
23972 the discriminant range bound we are looking for: we will have
23973 to increment or decrement it. */
23974 if (TREE_CODE (lower_op) == GE_EXPR)
23975 lower_cst_included = true;
23976 else if (TREE_CODE (lower_op) == GT_EXPR)
23977 lower_cst_included = false;
23978 else
23979 goto abort;
23981 if (TREE_CODE (upper_op) == LE_EXPR)
23982 upper_cst_included = true;
23983 else if (TREE_CODE (upper_op) == LT_EXPR)
23984 upper_cst_included = false;
23985 else
23986 goto abort;
23988 /* Extract the discriminant from the first operand and check it
23989 is consistant with the same analysis in the second
23990 operand. */
23991 candidate_discr_1
23992 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
23993 struct_type);
23994 candidate_discr_2
23995 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
23996 struct_type);
23997 if (candidate_discr_1 == candidate_discr_2)
23998 candidate_discr = candidate_discr_1;
23999 else
24000 goto abort;
24002 /* Extract bounds from both. */
24003 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
24004 lower_cst = TREE_OPERAND (lower_op, 1);
24005 upper_cst = TREE_OPERAND (upper_op, 1);
24007 if (!lower_cst_included)
24008 lower_cst
24009 = fold_build2 (PLUS_EXPR, TREE_TYPE (lower_cst), lower_cst,
24010 build_int_cst (TREE_TYPE (lower_cst), 1));
24011 if (!upper_cst_included)
24012 upper_cst
24013 = fold_build2 (MINUS_EXPR, TREE_TYPE (upper_cst), upper_cst,
24014 build_int_cst (TREE_TYPE (upper_cst), 1));
24016 if (!get_discr_value (lower_cst,
24017 &new_node->dw_discr_lower_bound)
24018 || !get_discr_value (upper_cst,
24019 &new_node->dw_discr_upper_bound))
24020 goto abort;
24022 new_node->dw_discr_range = true;
24025 else
24026 /* Unsupported sub-expression: we cannot determine the set of
24027 matching discriminant values. Abort everything. */
24028 goto abort;
24030 /* If the discriminant info is not consistant with what we saw so
24031 far, consider the analysis failed and abort everything. */
24032 if (candidate_discr == NULL_TREE
24033 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
24034 goto abort;
24035 else
24036 *discr_decl = candidate_discr;
24038 if (new_node != NULL)
24040 new_node->dw_discr_next = discr_lists[i];
24041 discr_lists[i] = new_node;
24043 match_expr = next_round_match_expr;
24047 /* If we reach this point, we could match everything we were interested
24048 in. */
24049 return;
24051 abort:
24052 /* Clean all data structure and return no result. */
24053 free (*discr_lists_p);
24054 *discr_lists_p = NULL;
24055 *discr_decl = NULL_TREE;
24058 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
24059 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
24060 under CONTEXT_DIE.
24062 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
24063 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
24064 this type, which are record types, represent the available variants and each
24065 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
24066 values are inferred from these attributes.
24068 In trees, the offsets for the fields inside these sub-records are relative
24069 to the variant part itself, whereas the corresponding DIEs should have
24070 offset attributes that are relative to the embedding record base address.
24071 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
24072 must be an expression that computes the offset of the variant part to
24073 describe in DWARF. */
24075 static void
24076 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
24077 dw_die_ref context_die)
24079 const tree variant_part_type = TREE_TYPE (variant_part_decl);
24080 tree variant_part_offset = vlr_ctx->variant_part_offset;
24081 struct loc_descr_context ctx = {
24082 vlr_ctx->struct_type, /* context_type */
24083 NULL_TREE, /* base_decl */
24084 NULL, /* dpi */
24085 false, /* placeholder_arg */
24086 false /* placeholder_seen */
24089 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
24090 NULL_TREE if there is no such field. */
24091 tree discr_decl = NULL_TREE;
24092 dw_discr_list_ref *discr_lists;
24093 unsigned discr_lists_length = 0;
24094 unsigned i;
24096 dw_die_ref dwarf_proc_die = NULL;
24097 dw_die_ref variant_part_die
24098 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
24100 equate_decl_number_to_die (variant_part_decl, variant_part_die);
24102 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
24103 &discr_decl, &discr_lists, &discr_lists_length);
24105 if (discr_decl != NULL_TREE)
24107 dw_die_ref discr_die = lookup_decl_die (discr_decl);
24109 if (discr_die)
24110 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
24111 else
24112 /* We have no DIE for the discriminant, so just discard all
24113 discrimimant information in the output. */
24114 discr_decl = NULL_TREE;
24117 /* If the offset for this variant part is more complex than a constant,
24118 create a DWARF procedure for it so that we will not have to generate DWARF
24119 expressions for it for each member. */
24120 if (TREE_CODE (variant_part_offset) != INTEGER_CST
24121 && (dwarf_version >= 3 || !dwarf_strict))
24123 const tree dwarf_proc_fndecl
24124 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
24125 build_function_type (TREE_TYPE (variant_part_offset),
24126 NULL_TREE));
24127 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
24128 const dw_loc_descr_ref dwarf_proc_body
24129 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
24131 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
24132 dwarf_proc_fndecl, context_die);
24133 if (dwarf_proc_die != NULL)
24134 variant_part_offset = dwarf_proc_call;
24137 /* Output DIEs for all variants. */
24138 i = 0;
24139 for (tree variant = TYPE_FIELDS (variant_part_type);
24140 variant != NULL_TREE;
24141 variant = DECL_CHAIN (variant), ++i)
24143 tree variant_type = TREE_TYPE (variant);
24144 dw_die_ref variant_die;
24146 /* All variants (i.e. members of a variant part) are supposed to be
24147 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
24148 under these records. */
24149 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
24151 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
24152 equate_decl_number_to_die (variant, variant_die);
24154 /* Output discriminant values this variant matches, if any. */
24155 if (discr_decl == NULL || discr_lists[i] == NULL)
24156 /* In the case we have discriminant information at all, this is
24157 probably the default variant: as the standard says, don't
24158 output any discriminant value/list attribute. */
24160 else if (discr_lists[i]->dw_discr_next == NULL
24161 && !discr_lists[i]->dw_discr_range)
24162 /* If there is only one accepted value, don't bother outputting a
24163 list. */
24164 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
24165 else
24166 add_discr_list (variant_die, discr_lists[i]);
24168 for (tree member = TYPE_FIELDS (variant_type);
24169 member != NULL_TREE;
24170 member = DECL_CHAIN (member))
24172 struct vlr_context vlr_sub_ctx = {
24173 vlr_ctx->struct_type, /* struct_type */
24174 NULL /* variant_part_offset */
24176 if (is_variant_part (member))
24178 /* All offsets for fields inside variant parts are relative to
24179 the top-level embedding RECORD_TYPE's base address. On the
24180 other hand, offsets in GCC's types are relative to the
24181 nested-most variant part. So we have to sum offsets each time
24182 we recurse. */
24184 vlr_sub_ctx.variant_part_offset
24185 = fold_build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
24186 variant_part_offset, byte_position (member));
24187 gen_variant_part (member, &vlr_sub_ctx, variant_die);
24189 else
24191 vlr_sub_ctx.variant_part_offset = variant_part_offset;
24192 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
24197 free (discr_lists);
24200 /* Generate a DIE for a class member. */
24202 static void
24203 gen_member_die (tree type, dw_die_ref context_die)
24205 tree member;
24206 tree binfo = TYPE_BINFO (type);
24208 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
24210 /* If this is not an incomplete type, output descriptions of each of its
24211 members. Note that as we output the DIEs necessary to represent the
24212 members of this record or union type, we will also be trying to output
24213 DIEs to represent the *types* of those members. However the `type'
24214 function (above) will specifically avoid generating type DIEs for member
24215 types *within* the list of member DIEs for this (containing) type except
24216 for those types (of members) which are explicitly marked as also being
24217 members of this (containing) type themselves. The g++ front- end can
24218 force any given type to be treated as a member of some other (containing)
24219 type by setting the TYPE_CONTEXT of the given (member) type to point to
24220 the TREE node representing the appropriate (containing) type. */
24222 /* First output info about the base classes. */
24223 if (binfo)
24225 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
24226 int i;
24227 tree base;
24229 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
24230 gen_inheritance_die (base,
24231 (accesses ? (*accesses)[i] : access_public_node),
24232 type,
24233 context_die);
24236 /* Now output info about the data members and type members. */
24237 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
24239 struct vlr_context vlr_ctx = { type, NULL_TREE };
24240 bool static_inline_p
24241 = (TREE_STATIC (member)
24242 && (lang_hooks.decls.decl_dwarf_attribute (member, DW_AT_inline)
24243 != -1));
24245 /* Ignore clones. */
24246 if (DECL_ABSTRACT_ORIGIN (member))
24247 continue;
24249 /* If we thought we were generating minimal debug info for TYPE
24250 and then changed our minds, some of the member declarations
24251 may have already been defined. Don't define them again, but
24252 do put them in the right order. */
24254 if (dw_die_ref child = lookup_decl_die (member))
24256 /* Handle inline static data members, which only have in-class
24257 declarations. */
24258 dw_die_ref ref = NULL;
24259 if (child->die_tag == DW_TAG_variable
24260 && child->die_parent == comp_unit_die ())
24262 ref = get_AT_ref (child, DW_AT_specification);
24263 /* For C++17 inline static data members followed by redundant
24264 out of class redeclaration, we might get here with
24265 child being the DIE created for the out of class
24266 redeclaration and with its DW_AT_specification being
24267 the DIE created for in-class definition. We want to
24268 reparent the latter, and don't want to create another
24269 DIE with DW_AT_specification in that case, because
24270 we already have one. */
24271 if (ref
24272 && static_inline_p
24273 && ref->die_tag == DW_TAG_variable
24274 && ref->die_parent == comp_unit_die ()
24275 && get_AT (ref, DW_AT_specification) == NULL)
24277 child = ref;
24278 ref = NULL;
24279 static_inline_p = false;
24283 if (child->die_tag == DW_TAG_variable
24284 && child->die_parent == comp_unit_die ()
24285 && ref == NULL)
24287 reparent_child (child, context_die);
24288 if (dwarf_version < 5)
24289 child->die_tag = DW_TAG_member;
24291 else
24292 splice_child_die (context_die, child);
24295 /* Do not generate standard DWARF for variant parts if we are generating
24296 the corresponding GNAT encodings: DIEs generated for both would
24297 conflict in our mappings. */
24298 else if (is_variant_part (member)
24299 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
24301 vlr_ctx.variant_part_offset = byte_position (member);
24302 gen_variant_part (member, &vlr_ctx, context_die);
24304 else
24306 vlr_ctx.variant_part_offset = NULL_TREE;
24307 gen_decl_die (member, NULL, &vlr_ctx, context_die);
24310 /* For C++ inline static data members emit immediately a DW_TAG_variable
24311 DIE that will refer to that DW_TAG_member/DW_TAG_variable through
24312 DW_AT_specification. */
24313 if (static_inline_p)
24315 int old_extern = DECL_EXTERNAL (member);
24316 DECL_EXTERNAL (member) = 0;
24317 gen_decl_die (member, NULL, NULL, comp_unit_die ());
24318 DECL_EXTERNAL (member) = old_extern;
24323 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
24324 is set, we pretend that the type was never defined, so we only get the
24325 member DIEs needed by later specification DIEs. */
24327 static void
24328 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
24329 enum debug_info_usage usage)
24331 if (TREE_ASM_WRITTEN (type))
24333 /* Fill in the bound of variable-length fields in late dwarf if
24334 still incomplete. */
24335 if (!early_dwarf && variably_modified_type_p (type, NULL))
24336 for (tree member = TYPE_FIELDS (type);
24337 member;
24338 member = DECL_CHAIN (member))
24339 fill_variable_array_bounds (TREE_TYPE (member));
24340 return;
24343 dw_die_ref type_die = lookup_type_die (type);
24344 dw_die_ref scope_die = 0;
24345 int nested = 0;
24346 int complete = (TYPE_SIZE (type)
24347 && (! TYPE_STUB_DECL (type)
24348 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
24349 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
24350 complete = complete && should_emit_struct_debug (type, usage);
24352 if (type_die && ! complete)
24353 return;
24355 if (TYPE_CONTEXT (type) != NULL_TREE
24356 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24357 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
24358 nested = 1;
24360 scope_die = scope_die_for (type, context_die);
24362 /* Generate child dies for template paramaters. */
24363 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
24364 schedule_generic_params_dies_gen (type);
24366 if (! type_die || (nested && is_cu_die (scope_die)))
24367 /* First occurrence of type or toplevel definition of nested class. */
24369 dw_die_ref old_die = type_die;
24371 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
24372 ? record_type_tag (type) : DW_TAG_union_type,
24373 scope_die, type);
24374 equate_type_number_to_die (type, type_die);
24375 if (old_die)
24376 add_AT_specification (type_die, old_die);
24377 else
24378 add_name_attribute (type_die, type_tag (type));
24380 else
24381 remove_AT (type_die, DW_AT_declaration);
24383 /* If this type has been completed, then give it a byte_size attribute and
24384 then give a list of members. */
24385 if (complete && !ns_decl)
24387 /* Prevent infinite recursion in cases where the type of some member of
24388 this type is expressed in terms of this type itself. */
24389 TREE_ASM_WRITTEN (type) = 1;
24390 add_byte_size_attribute (type_die, type);
24391 add_alignment_attribute (type_die, type);
24392 if (TYPE_STUB_DECL (type) != NULL_TREE)
24394 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
24395 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
24398 /* If the first reference to this type was as the return type of an
24399 inline function, then it may not have a parent. Fix this now. */
24400 if (type_die->die_parent == NULL)
24401 add_child_die (scope_die, type_die);
24403 push_decl_scope (type);
24404 gen_member_die (type, type_die);
24405 pop_decl_scope ();
24407 add_gnat_descriptive_type_attribute (type_die, type, context_die);
24408 if (TYPE_ARTIFICIAL (type))
24409 add_AT_flag (type_die, DW_AT_artificial, 1);
24411 /* GNU extension: Record what type our vtable lives in. */
24412 if (TYPE_VFIELD (type))
24414 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
24416 gen_type_die (vtype, context_die);
24417 add_AT_die_ref (type_die, DW_AT_containing_type,
24418 lookup_type_die (vtype));
24421 else
24423 add_AT_flag (type_die, DW_AT_declaration, 1);
24425 /* We don't need to do this for function-local types. */
24426 if (TYPE_STUB_DECL (type)
24427 && ! decl_function_context (TYPE_STUB_DECL (type)))
24428 vec_safe_push (incomplete_types, type);
24431 if (get_AT (type_die, DW_AT_name))
24432 add_pubtype (type, type_die);
24435 /* Generate a DIE for a subroutine _type_. */
24437 static void
24438 gen_subroutine_type_die (tree type, dw_die_ref context_die)
24440 tree return_type = TREE_TYPE (type);
24441 dw_die_ref subr_die
24442 = new_die (DW_TAG_subroutine_type,
24443 scope_die_for (type, context_die), type);
24445 equate_type_number_to_die (type, subr_die);
24446 add_prototyped_attribute (subr_die, type);
24447 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
24448 context_die);
24449 add_alignment_attribute (subr_die, type);
24450 gen_formal_types_die (type, subr_die);
24452 if (get_AT (subr_die, DW_AT_name))
24453 add_pubtype (type, subr_die);
24454 if ((dwarf_version >= 5 || !dwarf_strict)
24455 && lang_hooks.types.type_dwarf_attribute (type, DW_AT_reference) != -1)
24456 add_AT_flag (subr_die, DW_AT_reference, 1);
24457 if ((dwarf_version >= 5 || !dwarf_strict)
24458 && lang_hooks.types.type_dwarf_attribute (type,
24459 DW_AT_rvalue_reference) != -1)
24460 add_AT_flag (subr_die, DW_AT_rvalue_reference, 1);
24463 /* Generate a DIE for a type definition. */
24465 static void
24466 gen_typedef_die (tree decl, dw_die_ref context_die)
24468 dw_die_ref type_die;
24469 tree type;
24471 if (TREE_ASM_WRITTEN (decl))
24473 if (DECL_ORIGINAL_TYPE (decl))
24474 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
24475 return;
24478 /* As we avoid creating DIEs for local typedefs (see decl_ultimate_origin
24479 checks in process_scope_var and modified_type_die), this should be called
24480 only for original types. */
24481 gcc_assert (decl_ultimate_origin (decl) == NULL
24482 || decl_ultimate_origin (decl) == decl);
24484 TREE_ASM_WRITTEN (decl) = 1;
24485 type_die = new_die (DW_TAG_typedef, context_die, decl);
24487 add_name_and_src_coords_attributes (type_die, decl);
24488 if (DECL_ORIGINAL_TYPE (decl))
24490 type = DECL_ORIGINAL_TYPE (decl);
24491 if (type == error_mark_node)
24492 return;
24494 gcc_assert (type != TREE_TYPE (decl));
24495 equate_type_number_to_die (TREE_TYPE (decl), type_die);
24497 else
24499 type = TREE_TYPE (decl);
24500 if (type == error_mark_node)
24501 return;
24503 if (is_naming_typedef_decl (TYPE_NAME (type)))
24505 /* Here, we are in the case of decl being a typedef naming
24506 an anonymous type, e.g:
24507 typedef struct {...} foo;
24508 In that case TREE_TYPE (decl) is not a typedef variant
24509 type and TYPE_NAME of the anonymous type is set to the
24510 TYPE_DECL of the typedef. This construct is emitted by
24511 the C++ FE.
24513 TYPE is the anonymous struct named by the typedef
24514 DECL. As we need the DW_AT_type attribute of the
24515 DW_TAG_typedef to point to the DIE of TYPE, let's
24516 generate that DIE right away. add_type_attribute
24517 called below will then pick (via lookup_type_die) that
24518 anonymous struct DIE. */
24519 if (!TREE_ASM_WRITTEN (type))
24520 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
24522 /* This is a GNU Extension. We are adding a
24523 DW_AT_linkage_name attribute to the DIE of the
24524 anonymous struct TYPE. The value of that attribute
24525 is the name of the typedef decl naming the anonymous
24526 struct. This greatly eases the work of consumers of
24527 this debug info. */
24528 add_linkage_name_raw (lookup_type_die (type), decl);
24532 add_type_attribute (type_die, type, decl_quals (decl), false,
24533 context_die);
24535 if (is_naming_typedef_decl (decl))
24536 /* We want that all subsequent calls to lookup_type_die with
24537 TYPE in argument yield the DW_TAG_typedef we have just
24538 created. */
24539 equate_type_number_to_die (type, type_die);
24541 add_alignment_attribute (type_die, TREE_TYPE (decl));
24543 add_accessibility_attribute (type_die, decl);
24545 if (DECL_ABSTRACT_P (decl))
24546 equate_decl_number_to_die (decl, type_die);
24548 if (get_AT (type_die, DW_AT_name))
24549 add_pubtype (decl, type_die);
24552 /* Generate a DIE for a struct, class, enum or union type. */
24554 static void
24555 gen_tagged_type_die (tree type,
24556 dw_die_ref context_die,
24557 enum debug_info_usage usage)
24559 int need_pop;
24561 if (type == NULL_TREE
24562 || !is_tagged_type (type))
24563 return;
24565 if (TREE_ASM_WRITTEN (type))
24566 need_pop = 0;
24567 /* If this is a nested type whose containing class hasn't been written
24568 out yet, writing it out will cover this one, too. This does not apply
24569 to instantiations of member class templates; they need to be added to
24570 the containing class as they are generated. FIXME: This hurts the
24571 idea of combining type decls from multiple TUs, since we can't predict
24572 what set of template instantiations we'll get. */
24573 else if (TYPE_CONTEXT (type)
24574 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
24575 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
24577 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
24579 if (TREE_ASM_WRITTEN (type))
24580 return;
24582 /* If that failed, attach ourselves to the stub. */
24583 push_decl_scope (TYPE_CONTEXT (type));
24584 context_die = lookup_type_die (TYPE_CONTEXT (type));
24585 need_pop = 1;
24587 else if (TYPE_CONTEXT (type) != NULL_TREE
24588 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
24590 /* If this type is local to a function that hasn't been written
24591 out yet, use a NULL context for now; it will be fixed up in
24592 decls_for_scope. */
24593 context_die = lookup_decl_die (TYPE_CONTEXT (type));
24594 /* A declaration DIE doesn't count; nested types need to go in the
24595 specification. */
24596 if (context_die && is_declaration_die (context_die))
24597 context_die = NULL;
24598 need_pop = 0;
24600 else
24602 context_die = declare_in_namespace (type, context_die);
24603 need_pop = 0;
24606 if (TREE_CODE (type) == ENUMERAL_TYPE)
24608 /* This might have been written out by the call to
24609 declare_in_namespace. */
24610 if (!TREE_ASM_WRITTEN (type))
24611 gen_enumeration_type_die (type, context_die);
24613 else
24614 gen_struct_or_union_type_die (type, context_die, usage);
24616 if (need_pop)
24617 pop_decl_scope ();
24619 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
24620 it up if it is ever completed. gen_*_type_die will set it for us
24621 when appropriate. */
24624 /* Generate a type description DIE. */
24626 static void
24627 gen_type_die_with_usage (tree type, dw_die_ref context_die,
24628 enum debug_info_usage usage)
24630 struct array_descr_info info;
24632 if (type == NULL_TREE || type == error_mark_node)
24633 return;
24635 if (flag_checking && type)
24636 verify_type (type);
24638 if (TYPE_NAME (type) != NULL_TREE
24639 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
24640 && is_redundant_typedef (TYPE_NAME (type))
24641 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
24642 /* The DECL of this type is a typedef we don't want to emit debug
24643 info for but we want debug info for its underlying typedef.
24644 This can happen for e.g, the injected-class-name of a C++
24645 type. */
24646 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
24648 /* If TYPE is a typedef type variant, let's generate debug info
24649 for the parent typedef which TYPE is a type of. */
24650 if (typedef_variant_p (type))
24652 if (TREE_ASM_WRITTEN (type))
24653 return;
24655 tree name = TYPE_NAME (type);
24656 tree origin = decl_ultimate_origin (name);
24657 if (origin != NULL && origin != name)
24659 gen_decl_die (origin, NULL, NULL, context_die);
24660 return;
24663 /* Prevent broken recursion; we can't hand off to the same type. */
24664 gcc_assert (DECL_ORIGINAL_TYPE (name) != type);
24666 /* Give typedefs the right scope. */
24667 context_die = scope_die_for (type, context_die);
24669 TREE_ASM_WRITTEN (type) = 1;
24671 gen_decl_die (name, NULL, NULL, context_die);
24672 return;
24675 /* If type is an anonymous tagged type named by a typedef, let's
24676 generate debug info for the typedef. */
24677 if (is_naming_typedef_decl (TYPE_NAME (type)))
24679 /* Use the DIE of the containing namespace as the parent DIE of
24680 the type description DIE we want to generate. */
24681 if (DECL_CONTEXT (TYPE_NAME (type))
24682 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
24683 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
24685 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
24686 return;
24689 if (lang_hooks.types.get_debug_type)
24691 tree debug_type = lang_hooks.types.get_debug_type (type);
24693 if (debug_type != NULL_TREE && debug_type != type)
24695 gen_type_die_with_usage (debug_type, context_die, usage);
24696 return;
24700 /* We are going to output a DIE to represent the unqualified version
24701 of this type (i.e. without any const or volatile qualifiers) so
24702 get the main variant (i.e. the unqualified version) of this type
24703 now. (Vectors and arrays are special because the debugging info is in the
24704 cloned type itself. Similarly function/method types can contain extra
24705 ref-qualification). */
24706 if (TREE_CODE (type) == FUNCTION_TYPE
24707 || TREE_CODE (type) == METHOD_TYPE)
24709 /* For function/method types, can't use type_main_variant here,
24710 because that can have different ref-qualifiers for C++,
24711 but try to canonicalize. */
24712 tree main = TYPE_MAIN_VARIANT (type);
24713 for (tree t = main; t; t = TYPE_NEXT_VARIANT (t))
24714 if (TYPE_QUALS_NO_ADDR_SPACE (t) == 0
24715 && check_base_type (t, main)
24716 && check_lang_type (t, type))
24718 type = t;
24719 break;
24722 else if (TREE_CODE (type) != VECTOR_TYPE
24723 && TREE_CODE (type) != ARRAY_TYPE)
24724 type = type_main_variant (type);
24726 /* If this is an array type with hidden descriptor, handle it first. */
24727 if (!TREE_ASM_WRITTEN (type)
24728 && lang_hooks.types.get_array_descr_info)
24730 memset (&info, 0, sizeof (info));
24731 if (lang_hooks.types.get_array_descr_info (type, &info))
24733 /* Fortran sometimes emits array types with no dimension. */
24734 gcc_assert (info.ndimensions >= 0
24735 && (info.ndimensions
24736 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
24737 gen_descr_array_type_die (type, &info, context_die);
24738 TREE_ASM_WRITTEN (type) = 1;
24739 return;
24743 if (TREE_ASM_WRITTEN (type))
24745 /* Variable-length types may be incomplete even if
24746 TREE_ASM_WRITTEN. For such types, fall through to
24747 gen_array_type_die() and possibly fill in
24748 DW_AT_{upper,lower}_bound attributes. */
24749 if ((TREE_CODE (type) != ARRAY_TYPE
24750 && TREE_CODE (type) != RECORD_TYPE
24751 && TREE_CODE (type) != UNION_TYPE
24752 && TREE_CODE (type) != QUAL_UNION_TYPE)
24753 || !variably_modified_type_p (type, NULL))
24754 return;
24757 switch (TREE_CODE (type))
24759 case ERROR_MARK:
24760 break;
24762 case POINTER_TYPE:
24763 case REFERENCE_TYPE:
24764 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
24765 ensures that the gen_type_die recursion will terminate even if the
24766 type is recursive. Recursive types are possible in Ada. */
24767 /* ??? We could perhaps do this for all types before the switch
24768 statement. */
24769 TREE_ASM_WRITTEN (type) = 1;
24771 /* For these types, all that is required is that we output a DIE (or a
24772 set of DIEs) to represent the "basis" type. */
24773 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24774 DINFO_USAGE_IND_USE);
24775 break;
24777 case OFFSET_TYPE:
24778 /* This code is used for C++ pointer-to-data-member types.
24779 Output a description of the relevant class type. */
24780 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
24781 DINFO_USAGE_IND_USE);
24783 /* Output a description of the type of the object pointed to. */
24784 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24785 DINFO_USAGE_IND_USE);
24787 /* Now output a DIE to represent this pointer-to-data-member type
24788 itself. */
24789 gen_ptr_to_mbr_type_die (type, context_die);
24790 break;
24792 case FUNCTION_TYPE:
24793 /* Force out return type (in case it wasn't forced out already). */
24794 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24795 DINFO_USAGE_DIR_USE);
24796 gen_subroutine_type_die (type, context_die);
24797 break;
24799 case METHOD_TYPE:
24800 /* Force out return type (in case it wasn't forced out already). */
24801 gen_type_die_with_usage (TREE_TYPE (type), context_die,
24802 DINFO_USAGE_DIR_USE);
24803 gen_subroutine_type_die (type, context_die);
24804 break;
24806 case ARRAY_TYPE:
24807 case VECTOR_TYPE:
24808 gen_array_type_die (type, context_die);
24809 break;
24811 case ENUMERAL_TYPE:
24812 case RECORD_TYPE:
24813 case UNION_TYPE:
24814 case QUAL_UNION_TYPE:
24815 gen_tagged_type_die (type, context_die, usage);
24816 return;
24818 case VOID_TYPE:
24819 case INTEGER_TYPE:
24820 case REAL_TYPE:
24821 case FIXED_POINT_TYPE:
24822 case COMPLEX_TYPE:
24823 case BOOLEAN_TYPE:
24824 case POINTER_BOUNDS_TYPE:
24825 /* No DIEs needed for fundamental types. */
24826 break;
24828 case NULLPTR_TYPE:
24829 case LANG_TYPE:
24830 /* Just use DW_TAG_unspecified_type. */
24832 dw_die_ref type_die = lookup_type_die (type);
24833 if (type_die == NULL)
24835 tree name = TYPE_IDENTIFIER (type);
24836 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
24837 type);
24838 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
24839 equate_type_number_to_die (type, type_die);
24842 break;
24844 default:
24845 if (is_cxx_auto (type))
24847 tree name = TYPE_IDENTIFIER (type);
24848 dw_die_ref *die = (name == get_identifier ("auto")
24849 ? &auto_die : &decltype_auto_die);
24850 if (!*die)
24852 *die = new_die (DW_TAG_unspecified_type,
24853 comp_unit_die (), NULL_TREE);
24854 add_name_attribute (*die, IDENTIFIER_POINTER (name));
24856 equate_type_number_to_die (type, *die);
24857 break;
24859 gcc_unreachable ();
24862 TREE_ASM_WRITTEN (type) = 1;
24865 static void
24866 gen_type_die (tree type, dw_die_ref context_die)
24868 if (type != error_mark_node)
24870 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
24871 if (flag_checking)
24873 dw_die_ref die = lookup_type_die (type);
24874 if (die)
24875 check_die (die);
24880 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
24881 things which are local to the given block. */
24883 static void
24884 gen_block_die (tree stmt, dw_die_ref context_die)
24886 int must_output_die = 0;
24887 bool inlined_func;
24889 /* Ignore blocks that are NULL. */
24890 if (stmt == NULL_TREE)
24891 return;
24893 inlined_func = inlined_function_outer_scope_p (stmt);
24895 /* If the block is one fragment of a non-contiguous block, do not
24896 process the variables, since they will have been done by the
24897 origin block. Do process subblocks. */
24898 if (BLOCK_FRAGMENT_ORIGIN (stmt))
24900 tree sub;
24902 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
24903 gen_block_die (sub, context_die);
24905 return;
24908 /* Determine if we need to output any Dwarf DIEs at all to represent this
24909 block. */
24910 if (inlined_func)
24911 /* The outer scopes for inlinings *must* always be represented. We
24912 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
24913 must_output_die = 1;
24914 else
24916 /* Determine if this block directly contains any "significant"
24917 local declarations which we will need to output DIEs for. */
24918 if (debug_info_level > DINFO_LEVEL_TERSE)
24919 /* We are not in terse mode so *any* local declaration counts
24920 as being a "significant" one. */
24921 must_output_die = ((BLOCK_VARS (stmt) != NULL
24922 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
24923 && (TREE_USED (stmt)
24924 || TREE_ASM_WRITTEN (stmt)
24925 || BLOCK_ABSTRACT (stmt)));
24926 else if ((TREE_USED (stmt)
24927 || TREE_ASM_WRITTEN (stmt)
24928 || BLOCK_ABSTRACT (stmt))
24929 && !dwarf2out_ignore_block (stmt))
24930 must_output_die = 1;
24933 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
24934 DIE for any block which contains no significant local declarations at
24935 all. Rather, in such cases we just call `decls_for_scope' so that any
24936 needed Dwarf info for any sub-blocks will get properly generated. Note
24937 that in terse mode, our definition of what constitutes a "significant"
24938 local declaration gets restricted to include only inlined function
24939 instances and local (nested) function definitions. */
24940 if (must_output_die)
24942 if (inlined_func)
24944 /* If STMT block is abstract, that means we have been called
24945 indirectly from dwarf2out_abstract_function.
24946 That function rightfully marks the descendent blocks (of
24947 the abstract function it is dealing with) as being abstract,
24948 precisely to prevent us from emitting any
24949 DW_TAG_inlined_subroutine DIE as a descendent
24950 of an abstract function instance. So in that case, we should
24951 not call gen_inlined_subroutine_die.
24953 Later though, when cgraph asks dwarf2out to emit info
24954 for the concrete instance of the function decl into which
24955 the concrete instance of STMT got inlined, the later will lead
24956 to the generation of a DW_TAG_inlined_subroutine DIE. */
24957 if (! BLOCK_ABSTRACT (stmt))
24958 gen_inlined_subroutine_die (stmt, context_die);
24960 else
24961 gen_lexical_block_die (stmt, context_die);
24963 else
24964 decls_for_scope (stmt, context_die);
24967 /* Process variable DECL (or variable with origin ORIGIN) within
24968 block STMT and add it to CONTEXT_DIE. */
24969 static void
24970 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
24972 dw_die_ref die;
24973 tree decl_or_origin = decl ? decl : origin;
24975 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
24976 die = lookup_decl_die (decl_or_origin);
24977 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
24979 if (TYPE_DECL_IS_STUB (decl_or_origin))
24980 die = lookup_type_die (TREE_TYPE (decl_or_origin));
24981 else
24982 die = lookup_decl_die (decl_or_origin);
24983 /* Avoid re-creating the DIE late if it was optimized as unused early. */
24984 if (! die && ! early_dwarf)
24985 return;
24987 else
24988 die = NULL;
24990 /* Avoid creating DIEs for local typedefs and concrete static variables that
24991 will only be pruned later. */
24992 if ((origin || decl_ultimate_origin (decl))
24993 && (TREE_CODE (decl_or_origin) == TYPE_DECL
24994 || (VAR_P (decl_or_origin) && TREE_STATIC (decl_or_origin))))
24996 origin = decl_ultimate_origin (decl_or_origin);
24997 if (decl && VAR_P (decl) && die != NULL)
24999 die = lookup_decl_die (origin);
25000 if (die != NULL)
25001 equate_decl_number_to_die (decl, die);
25003 return;
25006 if (die != NULL && die->die_parent == NULL)
25007 add_child_die (context_die, die);
25008 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
25010 if (early_dwarf)
25011 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
25012 stmt, context_die);
25014 else
25016 if (decl && DECL_P (decl))
25018 die = lookup_decl_die (decl);
25020 /* Early created DIEs do not have a parent as the decls refer
25021 to the function as DECL_CONTEXT rather than the BLOCK. */
25022 if (die && die->die_parent == NULL)
25024 gcc_assert (in_lto_p);
25025 add_child_die (context_die, die);
25029 gen_decl_die (decl, origin, NULL, context_die);
25033 /* Generate all of the decls declared within a given scope and (recursively)
25034 all of its sub-blocks. */
25036 static void
25037 decls_for_scope (tree stmt, dw_die_ref context_die)
25039 tree decl;
25040 unsigned int i;
25041 tree subblocks;
25043 /* Ignore NULL blocks. */
25044 if (stmt == NULL_TREE)
25045 return;
25047 /* Output the DIEs to represent all of the data objects and typedefs
25048 declared directly within this block but not within any nested
25049 sub-blocks. Also, nested function and tag DIEs have been
25050 generated with a parent of NULL; fix that up now. We don't
25051 have to do this if we're at -g1. */
25052 if (debug_info_level > DINFO_LEVEL_TERSE)
25054 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
25055 process_scope_var (stmt, decl, NULL_TREE, context_die);
25056 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
25057 origin - avoid doing this twice as we have no good way to see
25058 if we've done it once already. */
25059 if (! early_dwarf)
25060 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
25062 decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
25063 if (decl == current_function_decl)
25064 /* Ignore declarations of the current function, while they
25065 are declarations, gen_subprogram_die would treat them
25066 as definitions again, because they are equal to
25067 current_function_decl and endlessly recurse. */;
25068 else if (TREE_CODE (decl) == FUNCTION_DECL)
25069 process_scope_var (stmt, decl, NULL_TREE, context_die);
25070 else
25071 process_scope_var (stmt, NULL_TREE, decl, context_die);
25075 /* Even if we're at -g1, we need to process the subblocks in order to get
25076 inlined call information. */
25078 /* Output the DIEs to represent all sub-blocks (and the items declared
25079 therein) of this block. */
25080 for (subblocks = BLOCK_SUBBLOCKS (stmt);
25081 subblocks != NULL;
25082 subblocks = BLOCK_CHAIN (subblocks))
25083 gen_block_die (subblocks, context_die);
25086 /* Is this a typedef we can avoid emitting? */
25088 bool
25089 is_redundant_typedef (const_tree decl)
25091 if (TYPE_DECL_IS_STUB (decl))
25092 return true;
25094 if (DECL_ARTIFICIAL (decl)
25095 && DECL_CONTEXT (decl)
25096 && is_tagged_type (DECL_CONTEXT (decl))
25097 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
25098 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
25099 /* Also ignore the artificial member typedef for the class name. */
25100 return true;
25102 return false;
25105 /* Return TRUE if TYPE is a typedef that names a type for linkage
25106 purposes. This kind of typedefs is produced by the C++ FE for
25107 constructs like:
25109 typedef struct {...} foo;
25111 In that case, there is no typedef variant type produced for foo.
25112 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
25113 struct type. */
25115 static bool
25116 is_naming_typedef_decl (const_tree decl)
25118 if (decl == NULL_TREE
25119 || TREE_CODE (decl) != TYPE_DECL
25120 || DECL_NAMELESS (decl)
25121 || !is_tagged_type (TREE_TYPE (decl))
25122 || DECL_IS_BUILTIN (decl)
25123 || is_redundant_typedef (decl)
25124 /* It looks like Ada produces TYPE_DECLs that are very similar
25125 to C++ naming typedefs but that have different
25126 semantics. Let's be specific to c++ for now. */
25127 || !is_cxx (decl))
25128 return FALSE;
25130 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
25131 && TYPE_NAME (TREE_TYPE (decl)) == decl
25132 && (TYPE_STUB_DECL (TREE_TYPE (decl))
25133 != TYPE_NAME (TREE_TYPE (decl))));
25136 /* Looks up the DIE for a context. */
25138 static inline dw_die_ref
25139 lookup_context_die (tree context)
25141 if (context)
25143 /* Find die that represents this context. */
25144 if (TYPE_P (context))
25146 context = TYPE_MAIN_VARIANT (context);
25147 dw_die_ref ctx = lookup_type_die (context);
25148 if (!ctx)
25149 return NULL;
25150 return strip_naming_typedef (context, ctx);
25152 else
25153 return lookup_decl_die (context);
25155 return comp_unit_die ();
25158 /* Returns the DIE for a context. */
25160 static inline dw_die_ref
25161 get_context_die (tree context)
25163 if (context)
25165 /* Find die that represents this context. */
25166 if (TYPE_P (context))
25168 context = TYPE_MAIN_VARIANT (context);
25169 return strip_naming_typedef (context, force_type_die (context));
25171 else
25172 return force_decl_die (context);
25174 return comp_unit_die ();
25177 /* Returns the DIE for decl. A DIE will always be returned. */
25179 static dw_die_ref
25180 force_decl_die (tree decl)
25182 dw_die_ref decl_die;
25183 unsigned saved_external_flag;
25184 tree save_fn = NULL_TREE;
25185 decl_die = lookup_decl_die (decl);
25186 if (!decl_die)
25188 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
25190 decl_die = lookup_decl_die (decl);
25191 if (decl_die)
25192 return decl_die;
25194 switch (TREE_CODE (decl))
25196 case FUNCTION_DECL:
25197 /* Clear current_function_decl, so that gen_subprogram_die thinks
25198 that this is a declaration. At this point, we just want to force
25199 declaration die. */
25200 save_fn = current_function_decl;
25201 current_function_decl = NULL_TREE;
25202 gen_subprogram_die (decl, context_die);
25203 current_function_decl = save_fn;
25204 break;
25206 case VAR_DECL:
25207 /* Set external flag to force declaration die. Restore it after
25208 gen_decl_die() call. */
25209 saved_external_flag = DECL_EXTERNAL (decl);
25210 DECL_EXTERNAL (decl) = 1;
25211 gen_decl_die (decl, NULL, NULL, context_die);
25212 DECL_EXTERNAL (decl) = saved_external_flag;
25213 break;
25215 case NAMESPACE_DECL:
25216 if (dwarf_version >= 3 || !dwarf_strict)
25217 dwarf2out_decl (decl);
25218 else
25219 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
25220 decl_die = comp_unit_die ();
25221 break;
25223 case TRANSLATION_UNIT_DECL:
25224 decl_die = comp_unit_die ();
25225 break;
25227 default:
25228 gcc_unreachable ();
25231 /* We should be able to find the DIE now. */
25232 if (!decl_die)
25233 decl_die = lookup_decl_die (decl);
25234 gcc_assert (decl_die);
25237 return decl_die;
25240 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
25241 always returned. */
25243 static dw_die_ref
25244 force_type_die (tree type)
25246 dw_die_ref type_die;
25248 type_die = lookup_type_die (type);
25249 if (!type_die)
25251 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
25253 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
25254 false, context_die);
25255 gcc_assert (type_die);
25257 return type_die;
25260 /* Force out any required namespaces to be able to output DECL,
25261 and return the new context_die for it, if it's changed. */
25263 static dw_die_ref
25264 setup_namespace_context (tree thing, dw_die_ref context_die)
25266 tree context = (DECL_P (thing)
25267 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
25268 if (context && TREE_CODE (context) == NAMESPACE_DECL)
25269 /* Force out the namespace. */
25270 context_die = force_decl_die (context);
25272 return context_die;
25275 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
25276 type) within its namespace, if appropriate.
25278 For compatibility with older debuggers, namespace DIEs only contain
25279 declarations; all definitions are emitted at CU scope, with
25280 DW_AT_specification pointing to the declaration (like with class
25281 members). */
25283 static dw_die_ref
25284 declare_in_namespace (tree thing, dw_die_ref context_die)
25286 dw_die_ref ns_context;
25288 if (debug_info_level <= DINFO_LEVEL_TERSE)
25289 return context_die;
25291 /* External declarations in the local scope only need to be emitted
25292 once, not once in the namespace and once in the scope.
25294 This avoids declaring the `extern' below in the
25295 namespace DIE as well as in the innermost scope:
25297 namespace S
25299 int i=5;
25300 int foo()
25302 int i=8;
25303 extern int i;
25304 return i;
25308 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
25309 return context_die;
25311 /* If this decl is from an inlined function, then don't try to emit it in its
25312 namespace, as we will get confused. It would have already been emitted
25313 when the abstract instance of the inline function was emitted anyways. */
25314 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
25315 return context_die;
25317 ns_context = setup_namespace_context (thing, context_die);
25319 if (ns_context != context_die)
25321 if (is_fortran ())
25322 return ns_context;
25323 if (DECL_P (thing))
25324 gen_decl_die (thing, NULL, NULL, ns_context);
25325 else
25326 gen_type_die (thing, ns_context);
25328 return context_die;
25331 /* Generate a DIE for a namespace or namespace alias. */
25333 static void
25334 gen_namespace_die (tree decl, dw_die_ref context_die)
25336 dw_die_ref namespace_die;
25338 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
25339 they are an alias of. */
25340 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
25342 /* Output a real namespace or module. */
25343 context_die = setup_namespace_context (decl, comp_unit_die ());
25344 namespace_die = new_die (is_fortran ()
25345 ? DW_TAG_module : DW_TAG_namespace,
25346 context_die, decl);
25347 /* For Fortran modules defined in different CU don't add src coords. */
25348 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
25350 const char *name = dwarf2_name (decl, 0);
25351 if (name)
25352 add_name_attribute (namespace_die, name);
25354 else
25355 add_name_and_src_coords_attributes (namespace_die, decl);
25356 if (DECL_EXTERNAL (decl))
25357 add_AT_flag (namespace_die, DW_AT_declaration, 1);
25358 equate_decl_number_to_die (decl, namespace_die);
25360 else
25362 /* Output a namespace alias. */
25364 /* Force out the namespace we are an alias of, if necessary. */
25365 dw_die_ref origin_die
25366 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
25368 if (DECL_FILE_SCOPE_P (decl)
25369 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
25370 context_die = setup_namespace_context (decl, comp_unit_die ());
25371 /* Now create the namespace alias DIE. */
25372 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
25373 add_name_and_src_coords_attributes (namespace_die, decl);
25374 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
25375 equate_decl_number_to_die (decl, namespace_die);
25377 if ((dwarf_version >= 5 || !dwarf_strict)
25378 && lang_hooks.decls.decl_dwarf_attribute (decl,
25379 DW_AT_export_symbols) == 1)
25380 add_AT_flag (namespace_die, DW_AT_export_symbols, 1);
25382 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
25383 if (want_pubnames ())
25384 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
25387 /* Generate Dwarf debug information for a decl described by DECL.
25388 The return value is currently only meaningful for PARM_DECLs,
25389 for all other decls it returns NULL.
25391 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
25392 It can be NULL otherwise. */
25394 static dw_die_ref
25395 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
25396 dw_die_ref context_die)
25398 tree decl_or_origin = decl ? decl : origin;
25399 tree class_origin = NULL, ultimate_origin;
25401 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
25402 return NULL;
25404 /* Ignore pointer bounds decls. */
25405 if (DECL_P (decl_or_origin)
25406 && TREE_TYPE (decl_or_origin)
25407 && POINTER_BOUNDS_P (decl_or_origin))
25408 return NULL;
25410 switch (TREE_CODE (decl_or_origin))
25412 case ERROR_MARK:
25413 break;
25415 case CONST_DECL:
25416 if (!is_fortran () && !is_ada ())
25418 /* The individual enumerators of an enum type get output when we output
25419 the Dwarf representation of the relevant enum type itself. */
25420 break;
25423 /* Emit its type. */
25424 gen_type_die (TREE_TYPE (decl), context_die);
25426 /* And its containing namespace. */
25427 context_die = declare_in_namespace (decl, context_die);
25429 gen_const_die (decl, context_die);
25430 break;
25432 case FUNCTION_DECL:
25433 #if 0
25434 /* FIXME */
25435 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
25436 on local redeclarations of global functions. That seems broken. */
25437 if (current_function_decl != decl)
25438 /* This is only a declaration. */;
25439 #endif
25441 /* We should have abstract copies already and should not generate
25442 stray type DIEs in late LTO dumping. */
25443 if (! early_dwarf)
25446 /* If we're emitting a clone, emit info for the abstract instance. */
25447 else if (origin || DECL_ORIGIN (decl) != decl)
25448 dwarf2out_abstract_function (origin
25449 ? DECL_ORIGIN (origin)
25450 : DECL_ABSTRACT_ORIGIN (decl));
25452 /* If we're emitting a possibly inlined function emit it as
25453 abstract instance. */
25454 else if (cgraph_function_possibly_inlined_p (decl)
25455 && ! DECL_ABSTRACT_P (decl)
25456 && ! class_or_namespace_scope_p (context_die)
25457 /* dwarf2out_abstract_function won't emit a die if this is just
25458 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
25459 that case, because that works only if we have a die. */
25460 && DECL_INITIAL (decl) != NULL_TREE)
25461 dwarf2out_abstract_function (decl);
25463 /* Otherwise we're emitting the primary DIE for this decl. */
25464 else if (debug_info_level > DINFO_LEVEL_TERSE)
25466 /* Before we describe the FUNCTION_DECL itself, make sure that we
25467 have its containing type. */
25468 if (!origin)
25469 origin = decl_class_context (decl);
25470 if (origin != NULL_TREE)
25471 gen_type_die (origin, context_die);
25473 /* And its return type. */
25474 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
25476 /* And its virtual context. */
25477 if (DECL_VINDEX (decl) != NULL_TREE)
25478 gen_type_die (DECL_CONTEXT (decl), context_die);
25480 /* Make sure we have a member DIE for decl. */
25481 if (origin != NULL_TREE)
25482 gen_type_die_for_member (origin, decl, context_die);
25484 /* And its containing namespace. */
25485 context_die = declare_in_namespace (decl, context_die);
25488 /* Now output a DIE to represent the function itself. */
25489 if (decl)
25490 gen_subprogram_die (decl, context_die);
25491 break;
25493 case TYPE_DECL:
25494 /* If we are in terse mode, don't generate any DIEs to represent any
25495 actual typedefs. */
25496 if (debug_info_level <= DINFO_LEVEL_TERSE)
25497 break;
25499 /* In the special case of a TYPE_DECL node representing the declaration
25500 of some type tag, if the given TYPE_DECL is marked as having been
25501 instantiated from some other (original) TYPE_DECL node (e.g. one which
25502 was generated within the original definition of an inline function) we
25503 used to generate a special (abbreviated) DW_TAG_structure_type,
25504 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
25505 should be actually referencing those DIEs, as variable DIEs with that
25506 type would be emitted already in the abstract origin, so it was always
25507 removed during unused type prunning. Don't add anything in this
25508 case. */
25509 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
25510 break;
25512 if (is_redundant_typedef (decl))
25513 gen_type_die (TREE_TYPE (decl), context_die);
25514 else
25515 /* Output a DIE to represent the typedef itself. */
25516 gen_typedef_die (decl, context_die);
25517 break;
25519 case LABEL_DECL:
25520 if (debug_info_level >= DINFO_LEVEL_NORMAL)
25521 gen_label_die (decl, context_die);
25522 break;
25524 case VAR_DECL:
25525 case RESULT_DECL:
25526 /* If we are in terse mode, don't generate any DIEs to represent any
25527 variable declarations or definitions. */
25528 if (debug_info_level <= DINFO_LEVEL_TERSE)
25529 break;
25531 /* Avoid generating stray type DIEs during late dwarf dumping.
25532 All types have been dumped early. */
25533 if (early_dwarf
25534 /* ??? But in LTRANS we cannot annotate early created variably
25535 modified type DIEs without copying them and adjusting all
25536 references to them. Dump them again as happens for inlining
25537 which copies both the decl and the types. */
25538 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25539 in VLA bound information for example. */
25540 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25541 current_function_decl)))
25543 /* Output any DIEs that are needed to specify the type of this data
25544 object. */
25545 if (decl_by_reference_p (decl_or_origin))
25546 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25547 else
25548 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25551 if (early_dwarf)
25553 /* And its containing type. */
25554 class_origin = decl_class_context (decl_or_origin);
25555 if (class_origin != NULL_TREE)
25556 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
25558 /* And its containing namespace. */
25559 context_die = declare_in_namespace (decl_or_origin, context_die);
25562 /* Now output the DIE to represent the data object itself. This gets
25563 complicated because of the possibility that the VAR_DECL really
25564 represents an inlined instance of a formal parameter for an inline
25565 function. */
25566 ultimate_origin = decl_ultimate_origin (decl_or_origin);
25567 if (ultimate_origin != NULL_TREE
25568 && TREE_CODE (ultimate_origin) == PARM_DECL)
25569 gen_formal_parameter_die (decl, origin,
25570 true /* Emit name attribute. */,
25571 context_die);
25572 else
25573 gen_variable_die (decl, origin, context_die);
25574 break;
25576 case FIELD_DECL:
25577 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
25578 /* Ignore the nameless fields that are used to skip bits but handle C++
25579 anonymous unions and structs. */
25580 if (DECL_NAME (decl) != NULL_TREE
25581 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
25582 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
25584 gen_type_die (member_declared_type (decl), context_die);
25585 gen_field_die (decl, ctx, context_die);
25587 break;
25589 case PARM_DECL:
25590 /* Avoid generating stray type DIEs during late dwarf dumping.
25591 All types have been dumped early. */
25592 if (early_dwarf
25593 /* ??? But in LTRANS we cannot annotate early created variably
25594 modified type DIEs without copying them and adjusting all
25595 references to them. Dump them again as happens for inlining
25596 which copies both the decl and the types. */
25597 /* ??? And even non-LTO needs to re-visit type DIEs to fill
25598 in VLA bound information for example. */
25599 || (decl && variably_modified_type_p (TREE_TYPE (decl),
25600 current_function_decl)))
25602 if (DECL_BY_REFERENCE (decl_or_origin))
25603 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
25604 else
25605 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
25607 return gen_formal_parameter_die (decl, origin,
25608 true /* Emit name attribute. */,
25609 context_die);
25611 case NAMESPACE_DECL:
25612 if (dwarf_version >= 3 || !dwarf_strict)
25613 gen_namespace_die (decl, context_die);
25614 break;
25616 case IMPORTED_DECL:
25617 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
25618 DECL_CONTEXT (decl), context_die);
25619 break;
25621 case NAMELIST_DECL:
25622 gen_namelist_decl (DECL_NAME (decl), context_die,
25623 NAMELIST_DECL_ASSOCIATED_DECL (decl));
25624 break;
25626 default:
25627 /* Probably some frontend-internal decl. Assume we don't care. */
25628 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
25629 break;
25632 return NULL;
25635 /* Output initial debug information for global DECL. Called at the
25636 end of the parsing process.
25638 This is the initial debug generation process. As such, the DIEs
25639 generated may be incomplete. A later debug generation pass
25640 (dwarf2out_late_global_decl) will augment the information generated
25641 in this pass (e.g., with complete location info). */
25643 static void
25644 dwarf2out_early_global_decl (tree decl)
25646 set_early_dwarf s;
25648 /* gen_decl_die() will set DECL_ABSTRACT because
25649 cgraph_function_possibly_inlined_p() returns true. This is in
25650 turn will cause DW_AT_inline attributes to be set.
25652 This happens because at early dwarf generation, there is no
25653 cgraph information, causing cgraph_function_possibly_inlined_p()
25654 to return true. Trick cgraph_function_possibly_inlined_p()
25655 while we generate dwarf early. */
25656 bool save = symtab->global_info_ready;
25657 symtab->global_info_ready = true;
25659 /* We don't handle TYPE_DECLs. If required, they'll be reached via
25660 other DECLs and they can point to template types or other things
25661 that dwarf2out can't handle when done via dwarf2out_decl. */
25662 if (TREE_CODE (decl) != TYPE_DECL
25663 && TREE_CODE (decl) != PARM_DECL)
25665 if (TREE_CODE (decl) == FUNCTION_DECL)
25667 tree save_fndecl = current_function_decl;
25669 /* For nested functions, make sure we have DIEs for the parents first
25670 so that all nested DIEs are generated at the proper scope in the
25671 first shot. */
25672 tree context = decl_function_context (decl);
25673 if (context != NULL)
25675 dw_die_ref context_die = lookup_decl_die (context);
25676 current_function_decl = context;
25678 /* Avoid emitting DIEs multiple times, but still process CONTEXT
25679 enough so that it lands in its own context. This avoids type
25680 pruning issues later on. */
25681 if (context_die == NULL || is_declaration_die (context_die))
25682 dwarf2out_decl (context);
25685 /* Emit an abstract origin of a function first. This happens
25686 with C++ constructor clones for example and makes
25687 dwarf2out_abstract_function happy which requires the early
25688 DIE of the abstract instance to be present. */
25689 tree origin = DECL_ABSTRACT_ORIGIN (decl);
25690 dw_die_ref origin_die;
25691 if (origin != NULL
25692 /* Do not emit the DIE multiple times but make sure to
25693 process it fully here in case we just saw a declaration. */
25694 && ((origin_die = lookup_decl_die (origin)) == NULL
25695 || is_declaration_die (origin_die)))
25697 current_function_decl = origin;
25698 dwarf2out_decl (origin);
25701 /* Emit the DIE for decl but avoid doing that multiple times. */
25702 dw_die_ref old_die;
25703 if ((old_die = lookup_decl_die (decl)) == NULL
25704 || is_declaration_die (old_die))
25706 current_function_decl = decl;
25707 dwarf2out_decl (decl);
25710 current_function_decl = save_fndecl;
25712 else
25713 dwarf2out_decl (decl);
25715 symtab->global_info_ready = save;
25718 /* Output debug information for global decl DECL. Called from
25719 toplev.c after compilation proper has finished. */
25721 static void
25722 dwarf2out_late_global_decl (tree decl)
25724 /* Fill-in any location information we were unable to determine
25725 on the first pass. */
25726 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
25728 dw_die_ref die = lookup_decl_die (decl);
25730 /* We may have to generate early debug late for LTO in case debug
25731 was not enabled at compile-time or the target doesn't support
25732 the LTO early debug scheme. */
25733 if (! die && in_lto_p)
25735 dwarf2out_decl (decl);
25736 die = lookup_decl_die (decl);
25739 if (die)
25741 /* We get called via the symtab code invoking late_global_decl
25742 for symbols that are optimized out. Do not add locations
25743 for those, except if they have a DECL_VALUE_EXPR, in which case
25744 they are relevant for debuggers. */
25745 varpool_node *node = varpool_node::get (decl);
25746 if ((! node || ! node->definition) && ! DECL_HAS_VALUE_EXPR_P (decl))
25747 tree_add_const_value_attribute_for_decl (die, decl);
25748 else
25749 add_location_or_const_value_attribute (die, decl, false);
25754 /* Output debug information for type decl DECL. Called from toplev.c
25755 and from language front ends (to record built-in types). */
25756 static void
25757 dwarf2out_type_decl (tree decl, int local)
25759 if (!local)
25761 set_early_dwarf s;
25762 dwarf2out_decl (decl);
25766 /* Output debug information for imported module or decl DECL.
25767 NAME is non-NULL name in the lexical block if the decl has been renamed.
25768 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
25769 that DECL belongs to.
25770 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
25771 static void
25772 dwarf2out_imported_module_or_decl_1 (tree decl,
25773 tree name,
25774 tree lexical_block,
25775 dw_die_ref lexical_block_die)
25777 expanded_location xloc;
25778 dw_die_ref imported_die = NULL;
25779 dw_die_ref at_import_die;
25781 if (TREE_CODE (decl) == IMPORTED_DECL)
25783 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
25784 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
25785 gcc_assert (decl);
25787 else
25788 xloc = expand_location (input_location);
25790 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
25792 at_import_die = force_type_die (TREE_TYPE (decl));
25793 /* For namespace N { typedef void T; } using N::T; base_type_die
25794 returns NULL, but DW_TAG_imported_declaration requires
25795 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
25796 if (!at_import_die)
25798 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
25799 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
25800 at_import_die = lookup_type_die (TREE_TYPE (decl));
25801 gcc_assert (at_import_die);
25804 else
25806 at_import_die = lookup_decl_die (decl);
25807 if (!at_import_die)
25809 /* If we're trying to avoid duplicate debug info, we may not have
25810 emitted the member decl for this field. Emit it now. */
25811 if (TREE_CODE (decl) == FIELD_DECL)
25813 tree type = DECL_CONTEXT (decl);
25815 if (TYPE_CONTEXT (type)
25816 && TYPE_P (TYPE_CONTEXT (type))
25817 && !should_emit_struct_debug (TYPE_CONTEXT (type),
25818 DINFO_USAGE_DIR_USE))
25819 return;
25820 gen_type_die_for_member (type, decl,
25821 get_context_die (TYPE_CONTEXT (type)));
25823 if (TREE_CODE (decl) == NAMELIST_DECL)
25824 at_import_die = gen_namelist_decl (DECL_NAME (decl),
25825 get_context_die (DECL_CONTEXT (decl)),
25826 NULL_TREE);
25827 else
25828 at_import_die = force_decl_die (decl);
25832 if (TREE_CODE (decl) == NAMESPACE_DECL)
25834 if (dwarf_version >= 3 || !dwarf_strict)
25835 imported_die = new_die (DW_TAG_imported_module,
25836 lexical_block_die,
25837 lexical_block);
25838 else
25839 return;
25841 else
25842 imported_die = new_die (DW_TAG_imported_declaration,
25843 lexical_block_die,
25844 lexical_block);
25846 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
25847 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
25848 if (debug_column_info && xloc.column)
25849 add_AT_unsigned (imported_die, DW_AT_decl_column, xloc.column);
25850 if (name)
25851 add_AT_string (imported_die, DW_AT_name,
25852 IDENTIFIER_POINTER (name));
25853 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
25856 /* Output debug information for imported module or decl DECL.
25857 NAME is non-NULL name in context if the decl has been renamed.
25858 CHILD is true if decl is one of the renamed decls as part of
25859 importing whole module.
25860 IMPLICIT is set if this hook is called for an implicit import
25861 such as inline namespace. */
25863 static void
25864 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
25865 bool child, bool implicit)
25867 /* dw_die_ref at_import_die; */
25868 dw_die_ref scope_die;
25870 if (debug_info_level <= DINFO_LEVEL_TERSE)
25871 return;
25873 gcc_assert (decl);
25875 /* For DWARF5, just DW_AT_export_symbols on the DW_TAG_namespace
25876 should be enough, for DWARF4 and older even if we emit as extension
25877 DW_AT_export_symbols add the implicit DW_TAG_imported_module anyway
25878 for the benefit of consumers unaware of DW_AT_export_symbols. */
25879 if (implicit
25880 && dwarf_version >= 5
25881 && lang_hooks.decls.decl_dwarf_attribute (decl,
25882 DW_AT_export_symbols) == 1)
25883 return;
25885 set_early_dwarf s;
25887 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
25888 We need decl DIE for reference and scope die. First, get DIE for the decl
25889 itself. */
25891 /* Get the scope die for decl context. Use comp_unit_die for global module
25892 or decl. If die is not found for non globals, force new die. */
25893 if (context
25894 && TYPE_P (context)
25895 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
25896 return;
25898 scope_die = get_context_die (context);
25900 if (child)
25902 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
25903 there is nothing we can do, here. */
25904 if (dwarf_version < 3 && dwarf_strict)
25905 return;
25907 gcc_assert (scope_die->die_child);
25908 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
25909 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
25910 scope_die = scope_die->die_child;
25913 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
25914 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
25917 /* Output debug information for namelists. */
25919 static dw_die_ref
25920 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
25922 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
25923 tree value;
25924 unsigned i;
25926 if (debug_info_level <= DINFO_LEVEL_TERSE)
25927 return NULL;
25929 gcc_assert (scope_die != NULL);
25930 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
25931 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
25933 /* If there are no item_decls, we have a nondefining namelist, e.g.
25934 with USE association; hence, set DW_AT_declaration. */
25935 if (item_decls == NULL_TREE)
25937 add_AT_flag (nml_die, DW_AT_declaration, 1);
25938 return nml_die;
25941 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
25943 nml_item_ref_die = lookup_decl_die (value);
25944 if (!nml_item_ref_die)
25945 nml_item_ref_die = force_decl_die (value);
25947 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
25948 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
25950 return nml_die;
25954 /* Write the debugging output for DECL and return the DIE. */
25956 static void
25957 dwarf2out_decl (tree decl)
25959 dw_die_ref context_die = comp_unit_die ();
25961 switch (TREE_CODE (decl))
25963 case ERROR_MARK:
25964 return;
25966 case FUNCTION_DECL:
25967 /* If we're a nested function, initially use a parent of NULL; if we're
25968 a plain function, this will be fixed up in decls_for_scope. If
25969 we're a method, it will be ignored, since we already have a DIE. */
25970 if (decl_function_context (decl)
25971 /* But if we're in terse mode, we don't care about scope. */
25972 && debug_info_level > DINFO_LEVEL_TERSE)
25973 context_die = NULL;
25974 break;
25976 case VAR_DECL:
25977 /* For local statics lookup proper context die. */
25978 if (local_function_static (decl))
25979 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25981 /* If we are in terse mode, don't generate any DIEs to represent any
25982 variable declarations or definitions. */
25983 if (debug_info_level <= DINFO_LEVEL_TERSE)
25984 return;
25985 break;
25987 case CONST_DECL:
25988 if (debug_info_level <= DINFO_LEVEL_TERSE)
25989 return;
25990 if (!is_fortran () && !is_ada ())
25991 return;
25992 if (TREE_STATIC (decl) && decl_function_context (decl))
25993 context_die = lookup_decl_die (DECL_CONTEXT (decl));
25994 break;
25996 case NAMESPACE_DECL:
25997 case IMPORTED_DECL:
25998 if (debug_info_level <= DINFO_LEVEL_TERSE)
25999 return;
26000 if (lookup_decl_die (decl) != NULL)
26001 return;
26002 break;
26004 case TYPE_DECL:
26005 /* Don't emit stubs for types unless they are needed by other DIEs. */
26006 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
26007 return;
26009 /* Don't bother trying to generate any DIEs to represent any of the
26010 normal built-in types for the language we are compiling. */
26011 if (DECL_IS_BUILTIN (decl))
26012 return;
26014 /* If we are in terse mode, don't generate any DIEs for types. */
26015 if (debug_info_level <= DINFO_LEVEL_TERSE)
26016 return;
26018 /* If we're a function-scope tag, initially use a parent of NULL;
26019 this will be fixed up in decls_for_scope. */
26020 if (decl_function_context (decl))
26021 context_die = NULL;
26023 break;
26025 case NAMELIST_DECL:
26026 break;
26028 default:
26029 return;
26032 gen_decl_die (decl, NULL, NULL, context_die);
26034 if (flag_checking)
26036 dw_die_ref die = lookup_decl_die (decl);
26037 if (die)
26038 check_die (die);
26042 /* Write the debugging output for DECL. */
26044 static void
26045 dwarf2out_function_decl (tree decl)
26047 dwarf2out_decl (decl);
26048 call_arg_locations = NULL;
26049 call_arg_loc_last = NULL;
26050 call_site_count = -1;
26051 tail_call_site_count = -1;
26052 decl_loc_table->empty ();
26053 cached_dw_loc_list_table->empty ();
26056 /* Output a marker (i.e. a label) for the beginning of the generated code for
26057 a lexical block. */
26059 static void
26060 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
26061 unsigned int blocknum)
26063 switch_to_section (current_function_section ());
26064 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
26067 /* Output a marker (i.e. a label) for the end of the generated code for a
26068 lexical block. */
26070 static void
26071 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
26073 switch_to_section (current_function_section ());
26074 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
26077 /* Returns nonzero if it is appropriate not to emit any debugging
26078 information for BLOCK, because it doesn't contain any instructions.
26080 Don't allow this for blocks with nested functions or local classes
26081 as we would end up with orphans, and in the presence of scheduling
26082 we may end up calling them anyway. */
26084 static bool
26085 dwarf2out_ignore_block (const_tree block)
26087 tree decl;
26088 unsigned int i;
26090 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
26091 if (TREE_CODE (decl) == FUNCTION_DECL
26092 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26093 return 0;
26094 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
26096 decl = BLOCK_NONLOCALIZED_VAR (block, i);
26097 if (TREE_CODE (decl) == FUNCTION_DECL
26098 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
26099 return 0;
26102 return 1;
26105 /* Hash table routines for file_hash. */
26107 bool
26108 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
26110 return filename_cmp (p1->filename, p2) == 0;
26113 hashval_t
26114 dwarf_file_hasher::hash (dwarf_file_data *p)
26116 return htab_hash_string (p->filename);
26119 /* Lookup FILE_NAME (in the list of filenames that we know about here in
26120 dwarf2out.c) and return its "index". The index of each (known) filename is
26121 just a unique number which is associated with only that one filename. We
26122 need such numbers for the sake of generating labels (in the .debug_sfnames
26123 section) and references to those files numbers (in the .debug_srcinfo
26124 and .debug_macinfo sections). If the filename given as an argument is not
26125 found in our current list, add it to the list and assign it the next
26126 available unique index number. */
26128 static struct dwarf_file_data *
26129 lookup_filename (const char *file_name)
26131 struct dwarf_file_data * created;
26133 if (!file_name)
26134 return NULL;
26136 dwarf_file_data **slot
26137 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
26138 INSERT);
26139 if (*slot)
26140 return *slot;
26142 created = ggc_alloc<dwarf_file_data> ();
26143 created->filename = file_name;
26144 created->emitted_number = 0;
26145 *slot = created;
26146 return created;
26149 /* If the assembler will construct the file table, then translate the compiler
26150 internal file table number into the assembler file table number, and emit
26151 a .file directive if we haven't already emitted one yet. The file table
26152 numbers are different because we prune debug info for unused variables and
26153 types, which may include filenames. */
26155 static int
26156 maybe_emit_file (struct dwarf_file_data * fd)
26158 if (! fd->emitted_number)
26160 if (last_emitted_file)
26161 fd->emitted_number = last_emitted_file->emitted_number + 1;
26162 else
26163 fd->emitted_number = 1;
26164 last_emitted_file = fd;
26166 if (DWARF2_ASM_LINE_DEBUG_INFO)
26168 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
26169 output_quoted_string (asm_out_file,
26170 remap_debug_filename (fd->filename));
26171 fputc ('\n', asm_out_file);
26175 return fd->emitted_number;
26178 /* Schedule generation of a DW_AT_const_value attribute to DIE.
26179 That generation should happen after function debug info has been
26180 generated. The value of the attribute is the constant value of ARG. */
26182 static void
26183 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
26185 die_arg_entry entry;
26187 if (!die || !arg)
26188 return;
26190 gcc_assert (early_dwarf);
26192 if (!tmpl_value_parm_die_table)
26193 vec_alloc (tmpl_value_parm_die_table, 32);
26195 entry.die = die;
26196 entry.arg = arg;
26197 vec_safe_push (tmpl_value_parm_die_table, entry);
26200 /* Return TRUE if T is an instance of generic type, FALSE
26201 otherwise. */
26203 static bool
26204 generic_type_p (tree t)
26206 if (t == NULL_TREE || !TYPE_P (t))
26207 return false;
26208 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
26211 /* Schedule the generation of the generic parameter dies for the
26212 instance of generic type T. The proper generation itself is later
26213 done by gen_scheduled_generic_parms_dies. */
26215 static void
26216 schedule_generic_params_dies_gen (tree t)
26218 if (!generic_type_p (t))
26219 return;
26221 gcc_assert (early_dwarf);
26223 if (!generic_type_instances)
26224 vec_alloc (generic_type_instances, 256);
26226 vec_safe_push (generic_type_instances, t);
26229 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
26230 by append_entry_to_tmpl_value_parm_die_table. This function must
26231 be called after function DIEs have been generated. */
26233 static void
26234 gen_remaining_tmpl_value_param_die_attribute (void)
26236 if (tmpl_value_parm_die_table)
26238 unsigned i, j;
26239 die_arg_entry *e;
26241 /* We do this in two phases - first get the cases we can
26242 handle during early-finish, preserving those we cannot
26243 (containing symbolic constants where we don't yet know
26244 whether we are going to output the referenced symbols).
26245 For those we try again at late-finish. */
26246 j = 0;
26247 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
26249 if (!e->die->removed
26250 && !tree_add_const_value_attribute (e->die, e->arg))
26252 dw_loc_descr_ref loc = NULL;
26253 if (! early_dwarf
26254 && (dwarf_version >= 5 || !dwarf_strict))
26255 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
26256 if (loc)
26257 add_AT_loc (e->die, DW_AT_location, loc);
26258 else
26259 (*tmpl_value_parm_die_table)[j++] = *e;
26262 tmpl_value_parm_die_table->truncate (j);
26266 /* Generate generic parameters DIEs for instances of generic types
26267 that have been previously scheduled by
26268 schedule_generic_params_dies_gen. This function must be called
26269 after all the types of the CU have been laid out. */
26271 static void
26272 gen_scheduled_generic_parms_dies (void)
26274 unsigned i;
26275 tree t;
26277 if (!generic_type_instances)
26278 return;
26280 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
26281 if (COMPLETE_TYPE_P (t))
26282 gen_generic_params_dies (t);
26284 generic_type_instances = NULL;
26288 /* Replace DW_AT_name for the decl with name. */
26290 static void
26291 dwarf2out_set_name (tree decl, tree name)
26293 dw_die_ref die;
26294 dw_attr_node *attr;
26295 const char *dname;
26297 die = TYPE_SYMTAB_DIE (decl);
26298 if (!die)
26299 return;
26301 dname = dwarf2_name (name, 0);
26302 if (!dname)
26303 return;
26305 attr = get_AT (die, DW_AT_name);
26306 if (attr)
26308 struct indirect_string_node *node;
26310 node = find_AT_string (dname);
26311 /* replace the string. */
26312 attr->dw_attr_val.v.val_str = node;
26315 else
26316 add_name_attribute (die, dname);
26319 /* True if before or during processing of the first function being emitted. */
26320 static bool in_first_function_p = true;
26321 /* True if loc_note during dwarf2out_var_location call might still be
26322 before first real instruction at address equal to .Ltext0. */
26323 static bool maybe_at_text_label_p = true;
26324 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
26325 static unsigned int first_loclabel_num_not_at_text_label;
26327 /* Look ahead for a real insn, or for a begin stmt marker. */
26329 static rtx_insn *
26330 dwarf2out_next_real_insn (rtx_insn *loc_note)
26332 rtx_insn *next_real = NEXT_INSN (loc_note);
26334 while (next_real)
26335 if (INSN_P (next_real))
26336 break;
26337 else
26338 next_real = NEXT_INSN (next_real);
26340 return next_real;
26343 /* Called by the final INSN scan whenever we see a var location. We
26344 use it to drop labels in the right places, and throw the location in
26345 our lookup table. */
26347 static void
26348 dwarf2out_var_location (rtx_insn *loc_note)
26350 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
26351 struct var_loc_node *newloc;
26352 rtx_insn *next_real, *next_note;
26353 rtx_insn *call_insn = NULL;
26354 static const char *last_label;
26355 static const char *last_postcall_label;
26356 static bool last_in_cold_section_p;
26357 static rtx_insn *expected_next_loc_note;
26358 tree decl;
26359 bool var_loc_p;
26361 if (!NOTE_P (loc_note))
26363 if (CALL_P (loc_note))
26365 call_site_count++;
26366 if (SIBLING_CALL_P (loc_note))
26367 tail_call_site_count++;
26368 if (optimize == 0 && !flag_var_tracking)
26370 /* When the var-tracking pass is not running, there is no note
26371 for indirect calls whose target is compile-time known. In this
26372 case, process such calls specifically so that we generate call
26373 sites for them anyway. */
26374 rtx x = PATTERN (loc_note);
26375 if (GET_CODE (x) == PARALLEL)
26376 x = XVECEXP (x, 0, 0);
26377 if (GET_CODE (x) == SET)
26378 x = SET_SRC (x);
26379 if (GET_CODE (x) == CALL)
26380 x = XEXP (x, 0);
26381 if (!MEM_P (x)
26382 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
26383 || !SYMBOL_REF_DECL (XEXP (x, 0))
26384 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
26385 != FUNCTION_DECL))
26387 call_insn = loc_note;
26388 loc_note = NULL;
26389 var_loc_p = false;
26391 next_real = dwarf2out_next_real_insn (call_insn);
26392 next_note = NULL;
26393 cached_next_real_insn = NULL;
26394 goto create_label;
26398 return;
26401 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
26402 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
26403 return;
26405 /* Optimize processing a large consecutive sequence of location
26406 notes so we don't spend too much time in next_real_insn. If the
26407 next insn is another location note, remember the next_real_insn
26408 calculation for next time. */
26409 next_real = cached_next_real_insn;
26410 if (next_real)
26412 if (expected_next_loc_note != loc_note)
26413 next_real = NULL;
26416 next_note = NEXT_INSN (loc_note);
26417 if (! next_note
26418 || next_note->deleted ()
26419 || ! NOTE_P (next_note)
26420 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
26421 && NOTE_KIND (next_note) != NOTE_INSN_BEGIN_STMT
26422 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
26423 next_note = NULL;
26425 if (! next_real)
26426 next_real = dwarf2out_next_real_insn (loc_note);
26428 if (next_note)
26430 expected_next_loc_note = next_note;
26431 cached_next_real_insn = next_real;
26433 else
26434 cached_next_real_insn = NULL;
26436 /* If there are no instructions which would be affected by this note,
26437 don't do anything. */
26438 if (var_loc_p
26439 && next_real == NULL_RTX
26440 && !NOTE_DURING_CALL_P (loc_note))
26441 return;
26443 create_label:
26445 if (next_real == NULL_RTX)
26446 next_real = get_last_insn ();
26448 /* If there were any real insns between note we processed last time
26449 and this note (or if it is the first note), clear
26450 last_{,postcall_}label so that they are not reused this time. */
26451 if (last_var_location_insn == NULL_RTX
26452 || last_var_location_insn != next_real
26453 || last_in_cold_section_p != in_cold_section_p)
26455 last_label = NULL;
26456 last_postcall_label = NULL;
26459 if (var_loc_p)
26461 decl = NOTE_VAR_LOCATION_DECL (loc_note);
26462 newloc = add_var_loc_to_decl (decl, loc_note,
26463 NOTE_DURING_CALL_P (loc_note)
26464 ? last_postcall_label : last_label);
26465 if (newloc == NULL)
26466 return;
26468 else
26470 decl = NULL_TREE;
26471 newloc = NULL;
26474 /* If there were no real insns between note we processed last time
26475 and this note, use the label we emitted last time. Otherwise
26476 create a new label and emit it. */
26477 if (last_label == NULL)
26479 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
26480 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
26481 loclabel_num++;
26482 last_label = ggc_strdup (loclabel);
26483 /* See if loclabel might be equal to .Ltext0. If yes,
26484 bump first_loclabel_num_not_at_text_label. */
26485 if (!have_multiple_function_sections
26486 && in_first_function_p
26487 && maybe_at_text_label_p)
26489 static rtx_insn *last_start;
26490 rtx_insn *insn;
26491 for (insn = loc_note; insn; insn = previous_insn (insn))
26492 if (insn == last_start)
26493 break;
26494 else if (!NONDEBUG_INSN_P (insn))
26495 continue;
26496 else
26498 rtx body = PATTERN (insn);
26499 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
26500 continue;
26501 /* Inline asm could occupy zero bytes. */
26502 else if (GET_CODE (body) == ASM_INPUT
26503 || asm_noperands (body) >= 0)
26504 continue;
26505 #ifdef HAVE_attr_length
26506 else if (get_attr_min_length (insn) == 0)
26507 continue;
26508 #endif
26509 else
26511 /* Assume insn has non-zero length. */
26512 maybe_at_text_label_p = false;
26513 break;
26516 if (maybe_at_text_label_p)
26518 last_start = loc_note;
26519 first_loclabel_num_not_at_text_label = loclabel_num;
26524 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
26525 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
26527 if (!var_loc_p)
26529 struct call_arg_loc_node *ca_loc
26530 = ggc_cleared_alloc<call_arg_loc_node> ();
26531 rtx_insn *prev
26532 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
26534 ca_loc->call_arg_loc_note = loc_note;
26535 ca_loc->next = NULL;
26536 ca_loc->label = last_label;
26537 gcc_assert (prev
26538 && (CALL_P (prev)
26539 || (NONJUMP_INSN_P (prev)
26540 && GET_CODE (PATTERN (prev)) == SEQUENCE
26541 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
26542 if (!CALL_P (prev))
26543 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
26544 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
26546 /* Look for a SYMBOL_REF in the "prev" instruction. */
26547 rtx x = get_call_rtx_from (PATTERN (prev));
26548 if (x)
26550 /* Try to get the call symbol, if any. */
26551 if (MEM_P (XEXP (x, 0)))
26552 x = XEXP (x, 0);
26553 /* First, look for a memory access to a symbol_ref. */
26554 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
26555 && SYMBOL_REF_DECL (XEXP (x, 0))
26556 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
26557 ca_loc->symbol_ref = XEXP (x, 0);
26558 /* Otherwise, look at a compile-time known user-level function
26559 declaration. */
26560 else if (MEM_P (x)
26561 && MEM_EXPR (x)
26562 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
26563 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
26566 ca_loc->block = insn_scope (prev);
26567 if (call_arg_locations)
26568 call_arg_loc_last->next = ca_loc;
26569 else
26570 call_arg_locations = ca_loc;
26571 call_arg_loc_last = ca_loc;
26573 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
26574 newloc->label = last_label;
26575 else
26577 if (!last_postcall_label)
26579 sprintf (loclabel, "%s-1", last_label);
26580 last_postcall_label = ggc_strdup (loclabel);
26582 newloc->label = last_postcall_label;
26585 if (var_loc_p && flag_debug_asm)
26587 const char *name, *sep, *patstr;
26588 if (decl && DECL_NAME (decl))
26589 name = IDENTIFIER_POINTER (DECL_NAME (decl));
26590 else
26591 name = "";
26592 if (NOTE_VAR_LOCATION_LOC (loc_note))
26594 sep = " => ";
26595 patstr = str_pattern_slim (NOTE_VAR_LOCATION_LOC (loc_note));
26597 else
26599 sep = " ";
26600 patstr = "RESET";
26602 fprintf (asm_out_file, "\t%s DEBUG %s%s%s\n", ASM_COMMENT_START,
26603 name, sep, patstr);
26606 last_var_location_insn = next_real;
26607 last_in_cold_section_p = in_cold_section_p;
26610 /* Called from finalize_size_functions for size functions so that their body
26611 can be encoded in the debug info to describe the layout of variable-length
26612 structures. */
26614 static void
26615 dwarf2out_size_function (tree decl)
26617 function_to_dwarf_procedure (decl);
26620 /* Note in one location list that text section has changed. */
26623 var_location_switch_text_section_1 (var_loc_list **slot, void *)
26625 var_loc_list *list = *slot;
26626 if (list->first)
26627 list->last_before_switch
26628 = list->last->next ? list->last->next : list->last;
26629 return 1;
26632 /* Note in all location lists that text section has changed. */
26634 static void
26635 var_location_switch_text_section (void)
26637 if (decl_loc_table == NULL)
26638 return;
26640 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
26643 /* Create a new line number table. */
26645 static dw_line_info_table *
26646 new_line_info_table (void)
26648 dw_line_info_table *table;
26650 table = ggc_cleared_alloc<dw_line_info_table> ();
26651 table->file_num = 1;
26652 table->line_num = 1;
26653 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
26655 return table;
26658 /* Lookup the "current" table into which we emit line info, so
26659 that we don't have to do it for every source line. */
26661 static void
26662 set_cur_line_info_table (section *sec)
26664 dw_line_info_table *table;
26666 if (sec == text_section)
26667 table = text_section_line_info;
26668 else if (sec == cold_text_section)
26670 table = cold_text_section_line_info;
26671 if (!table)
26673 cold_text_section_line_info = table = new_line_info_table ();
26674 table->end_label = cold_end_label;
26677 else
26679 const char *end_label;
26681 if (crtl->has_bb_partition)
26683 if (in_cold_section_p)
26684 end_label = crtl->subsections.cold_section_end_label;
26685 else
26686 end_label = crtl->subsections.hot_section_end_label;
26688 else
26690 char label[MAX_ARTIFICIAL_LABEL_BYTES];
26691 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
26692 current_function_funcdef_no);
26693 end_label = ggc_strdup (label);
26696 table = new_line_info_table ();
26697 table->end_label = end_label;
26699 vec_safe_push (separate_line_info, table);
26702 if (DWARF2_ASM_LINE_DEBUG_INFO)
26703 table->is_stmt = (cur_line_info_table
26704 ? cur_line_info_table->is_stmt
26705 : DWARF_LINE_DEFAULT_IS_STMT_START);
26706 cur_line_info_table = table;
26710 /* We need to reset the locations at the beginning of each
26711 function. We can't do this in the end_function hook, because the
26712 declarations that use the locations won't have been output when
26713 that hook is called. Also compute have_multiple_function_sections here. */
26715 static void
26716 dwarf2out_begin_function (tree fun)
26718 section *sec = function_section (fun);
26720 if (sec != text_section)
26721 have_multiple_function_sections = true;
26723 if (crtl->has_bb_partition && !cold_text_section)
26725 gcc_assert (current_function_decl == fun);
26726 cold_text_section = unlikely_text_section ();
26727 switch_to_section (cold_text_section);
26728 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
26729 switch_to_section (sec);
26732 dwarf2out_note_section_used ();
26733 call_site_count = 0;
26734 tail_call_site_count = 0;
26736 set_cur_line_info_table (sec);
26739 /* Helper function of dwarf2out_end_function, called only after emitting
26740 the very first function into assembly. Check if some .debug_loc range
26741 might end with a .LVL* label that could be equal to .Ltext0.
26742 In that case we must force using absolute addresses in .debug_loc ranges,
26743 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
26744 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
26745 list terminator.
26746 Set have_multiple_function_sections to true in that case and
26747 terminate htab traversal. */
26750 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
26752 var_loc_list *entry = *slot;
26753 struct var_loc_node *node;
26755 node = entry->first;
26756 if (node && node->next && node->next->label)
26758 unsigned int i;
26759 const char *label = node->next->label;
26760 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
26762 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
26764 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
26765 if (strcmp (label, loclabel) == 0)
26767 have_multiple_function_sections = true;
26768 return 0;
26772 return 1;
26775 /* Hook called after emitting a function into assembly.
26776 This does something only for the very first function emitted. */
26778 static void
26779 dwarf2out_end_function (unsigned int)
26781 if (in_first_function_p
26782 && !have_multiple_function_sections
26783 && first_loclabel_num_not_at_text_label
26784 && decl_loc_table)
26785 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
26786 in_first_function_p = false;
26787 maybe_at_text_label_p = false;
26790 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
26791 front-ends register a translation unit even before dwarf2out_init is
26792 called. */
26793 static tree main_translation_unit = NULL_TREE;
26795 /* Hook called by front-ends after they built their main translation unit.
26796 Associate comp_unit_die to UNIT. */
26798 static void
26799 dwarf2out_register_main_translation_unit (tree unit)
26801 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
26802 && main_translation_unit == NULL_TREE);
26803 main_translation_unit = unit;
26804 /* If dwarf2out_init has not been called yet, it will perform the association
26805 itself looking at main_translation_unit. */
26806 if (decl_die_table != NULL)
26807 equate_decl_number_to_die (unit, comp_unit_die ());
26810 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
26812 static void
26813 push_dw_line_info_entry (dw_line_info_table *table,
26814 enum dw_line_info_opcode opcode, unsigned int val)
26816 dw_line_info_entry e;
26817 e.opcode = opcode;
26818 e.val = val;
26819 vec_safe_push (table->entries, e);
26822 /* Output a label to mark the beginning of a source code line entry
26823 and record information relating to this source line, in
26824 'line_info_table' for later output of the .debug_line section. */
26825 /* ??? The discriminator parameter ought to be unsigned. */
26827 static void
26828 dwarf2out_source_line (unsigned int line, unsigned int column,
26829 const char *filename,
26830 int discriminator, bool is_stmt)
26832 unsigned int file_num;
26833 dw_line_info_table *table;
26835 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
26836 return;
26838 /* The discriminator column was added in dwarf4. Simplify the below
26839 by simply removing it if we're not supposed to output it. */
26840 if (dwarf_version < 4 && dwarf_strict)
26841 discriminator = 0;
26843 if (!debug_column_info)
26844 column = 0;
26846 table = cur_line_info_table;
26847 file_num = maybe_emit_file (lookup_filename (filename));
26849 /* ??? TODO: Elide duplicate line number entries. Traditionally,
26850 the debugger has used the second (possibly duplicate) line number
26851 at the beginning of the function to mark the end of the prologue.
26852 We could eliminate any other duplicates within the function. For
26853 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
26854 that second line number entry. */
26855 /* Recall that this end-of-prologue indication is *not* the same thing
26856 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
26857 to which the hook corresponds, follows the last insn that was
26858 emitted by gen_prologue. What we need is to precede the first insn
26859 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
26860 insn that corresponds to something the user wrote. These may be
26861 very different locations once scheduling is enabled. */
26863 if (0 && file_num == table->file_num
26864 && line == table->line_num
26865 && column == table->column_num
26866 && discriminator == table->discrim_num
26867 && is_stmt == table->is_stmt)
26868 return;
26870 switch_to_section (current_function_section ());
26872 /* If requested, emit something human-readable. */
26873 if (flag_debug_asm)
26875 if (debug_column_info)
26876 fprintf (asm_out_file, "\t%s %s:%d:%d\n", ASM_COMMENT_START,
26877 filename, line, column);
26878 else
26879 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
26880 filename, line);
26883 if (DWARF2_ASM_LINE_DEBUG_INFO)
26885 /* Emit the .loc directive understood by GNU as. */
26886 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
26887 file_num, line, is_stmt, discriminator */
26888 fputs ("\t.loc ", asm_out_file);
26889 fprint_ul (asm_out_file, file_num);
26890 putc (' ', asm_out_file);
26891 fprint_ul (asm_out_file, line);
26892 putc (' ', asm_out_file);
26893 fprint_ul (asm_out_file, column);
26895 if (is_stmt != table->is_stmt)
26897 fputs (" is_stmt ", asm_out_file);
26898 putc (is_stmt ? '1' : '0', asm_out_file);
26900 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
26902 gcc_assert (discriminator > 0);
26903 fputs (" discriminator ", asm_out_file);
26904 fprint_ul (asm_out_file, (unsigned long) discriminator);
26906 putc ('\n', asm_out_file);
26908 else
26910 unsigned int label_num = ++line_info_label_num;
26912 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
26914 push_dw_line_info_entry (table, LI_set_address, label_num);
26915 if (file_num != table->file_num)
26916 push_dw_line_info_entry (table, LI_set_file, file_num);
26917 if (discriminator != table->discrim_num)
26918 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
26919 if (is_stmt != table->is_stmt)
26920 push_dw_line_info_entry (table, LI_negate_stmt, 0);
26921 push_dw_line_info_entry (table, LI_set_line, line);
26922 if (debug_column_info)
26923 push_dw_line_info_entry (table, LI_set_column, column);
26926 table->file_num = file_num;
26927 table->line_num = line;
26928 table->column_num = column;
26929 table->discrim_num = discriminator;
26930 table->is_stmt = is_stmt;
26931 table->in_use = true;
26934 /* Record the beginning of a new source file. */
26936 static void
26937 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
26939 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26941 macinfo_entry e;
26942 e.code = DW_MACINFO_start_file;
26943 e.lineno = lineno;
26944 e.info = ggc_strdup (filename);
26945 vec_safe_push (macinfo_table, e);
26949 /* Record the end of a source file. */
26951 static void
26952 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
26954 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26956 macinfo_entry e;
26957 e.code = DW_MACINFO_end_file;
26958 e.lineno = lineno;
26959 e.info = NULL;
26960 vec_safe_push (macinfo_table, e);
26964 /* Called from debug_define in toplev.c. The `buffer' parameter contains
26965 the tail part of the directive line, i.e. the part which is past the
26966 initial whitespace, #, whitespace, directive-name, whitespace part. */
26968 static void
26969 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
26970 const char *buffer ATTRIBUTE_UNUSED)
26972 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
26974 macinfo_entry e;
26975 /* Insert a dummy first entry to be able to optimize the whole
26976 predefined macro block using DW_MACRO_import. */
26977 if (macinfo_table->is_empty () && lineno <= 1)
26979 e.code = 0;
26980 e.lineno = 0;
26981 e.info = NULL;
26982 vec_safe_push (macinfo_table, e);
26984 e.code = DW_MACINFO_define;
26985 e.lineno = lineno;
26986 e.info = ggc_strdup (buffer);
26987 vec_safe_push (macinfo_table, e);
26991 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
26992 the tail part of the directive line, i.e. the part which is past the
26993 initial whitespace, #, whitespace, directive-name, whitespace part. */
26995 static void
26996 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
26997 const char *buffer ATTRIBUTE_UNUSED)
26999 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27001 macinfo_entry e;
27002 /* Insert a dummy first entry to be able to optimize the whole
27003 predefined macro block using DW_MACRO_import. */
27004 if (macinfo_table->is_empty () && lineno <= 1)
27006 e.code = 0;
27007 e.lineno = 0;
27008 e.info = NULL;
27009 vec_safe_push (macinfo_table, e);
27011 e.code = DW_MACINFO_undef;
27012 e.lineno = lineno;
27013 e.info = ggc_strdup (buffer);
27014 vec_safe_push (macinfo_table, e);
27018 /* Helpers to manipulate hash table of CUs. */
27020 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
27022 static inline hashval_t hash (const macinfo_entry *);
27023 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
27026 inline hashval_t
27027 macinfo_entry_hasher::hash (const macinfo_entry *entry)
27029 return htab_hash_string (entry->info);
27032 inline bool
27033 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
27034 const macinfo_entry *entry2)
27036 return !strcmp (entry1->info, entry2->info);
27039 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
27041 /* Output a single .debug_macinfo entry. */
27043 static void
27044 output_macinfo_op (macinfo_entry *ref)
27046 int file_num;
27047 size_t len;
27048 struct indirect_string_node *node;
27049 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27050 struct dwarf_file_data *fd;
27052 switch (ref->code)
27054 case DW_MACINFO_start_file:
27055 fd = lookup_filename (ref->info);
27056 file_num = maybe_emit_file (fd);
27057 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
27058 dw2_asm_output_data_uleb128 (ref->lineno,
27059 "Included from line number %lu",
27060 (unsigned long) ref->lineno);
27061 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
27062 break;
27063 case DW_MACINFO_end_file:
27064 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
27065 break;
27066 case DW_MACINFO_define:
27067 case DW_MACINFO_undef:
27068 len = strlen (ref->info) + 1;
27069 if (!dwarf_strict
27070 && len > DWARF_OFFSET_SIZE
27071 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27072 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27074 ref->code = ref->code == DW_MACINFO_define
27075 ? DW_MACRO_define_strp : DW_MACRO_undef_strp;
27076 output_macinfo_op (ref);
27077 return;
27079 dw2_asm_output_data (1, ref->code,
27080 ref->code == DW_MACINFO_define
27081 ? "Define macro" : "Undefine macro");
27082 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27083 (unsigned long) ref->lineno);
27084 dw2_asm_output_nstring (ref->info, -1, "The macro");
27085 break;
27086 case DW_MACRO_define_strp:
27087 case DW_MACRO_undef_strp:
27088 node = find_AT_string (ref->info);
27089 gcc_assert (node
27090 && (node->form == DW_FORM_strp
27091 || node->form == DW_FORM_GNU_str_index));
27092 dw2_asm_output_data (1, ref->code,
27093 ref->code == DW_MACRO_define_strp
27094 ? "Define macro strp"
27095 : "Undefine macro strp");
27096 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
27097 (unsigned long) ref->lineno);
27098 if (node->form == DW_FORM_strp)
27099 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
27100 debug_str_section, "The macro: \"%s\"",
27101 ref->info);
27102 else
27103 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
27104 ref->info);
27105 break;
27106 case DW_MACRO_import:
27107 dw2_asm_output_data (1, ref->code, "Import");
27108 ASM_GENERATE_INTERNAL_LABEL (label,
27109 DEBUG_MACRO_SECTION_LABEL,
27110 ref->lineno + macinfo_label_base);
27111 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
27112 break;
27113 default:
27114 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
27115 ASM_COMMENT_START, (unsigned long) ref->code);
27116 break;
27120 /* Attempt to make a sequence of define/undef macinfo ops shareable with
27121 other compilation unit .debug_macinfo sections. IDX is the first
27122 index of a define/undef, return the number of ops that should be
27123 emitted in a comdat .debug_macinfo section and emit
27124 a DW_MACRO_import entry referencing it.
27125 If the define/undef entry should be emitted normally, return 0. */
27127 static unsigned
27128 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
27129 macinfo_hash_type **macinfo_htab)
27131 macinfo_entry *first, *second, *cur, *inc;
27132 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
27133 unsigned char checksum[16];
27134 struct md5_ctx ctx;
27135 char *grp_name, *tail;
27136 const char *base;
27137 unsigned int i, count, encoded_filename_len, linebuf_len;
27138 macinfo_entry **slot;
27140 first = &(*macinfo_table)[idx];
27141 second = &(*macinfo_table)[idx + 1];
27143 /* Optimize only if there are at least two consecutive define/undef ops,
27144 and either all of them are before first DW_MACINFO_start_file
27145 with lineno {0,1} (i.e. predefined macro block), or all of them are
27146 in some included header file. */
27147 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
27148 return 0;
27149 if (vec_safe_is_empty (files))
27151 if (first->lineno > 1 || second->lineno > 1)
27152 return 0;
27154 else if (first->lineno == 0)
27155 return 0;
27157 /* Find the last define/undef entry that can be grouped together
27158 with first and at the same time compute md5 checksum of their
27159 codes, linenumbers and strings. */
27160 md5_init_ctx (&ctx);
27161 for (i = idx; macinfo_table->iterate (i, &cur); i++)
27162 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
27163 break;
27164 else if (vec_safe_is_empty (files) && cur->lineno > 1)
27165 break;
27166 else
27168 unsigned char code = cur->code;
27169 md5_process_bytes (&code, 1, &ctx);
27170 checksum_uleb128 (cur->lineno, &ctx);
27171 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
27173 md5_finish_ctx (&ctx, checksum);
27174 count = i - idx;
27176 /* From the containing include filename (if any) pick up just
27177 usable characters from its basename. */
27178 if (vec_safe_is_empty (files))
27179 base = "";
27180 else
27181 base = lbasename (files->last ().info);
27182 for (encoded_filename_len = 0, i = 0; base[i]; i++)
27183 if (ISIDNUM (base[i]) || base[i] == '.')
27184 encoded_filename_len++;
27185 /* Count . at the end. */
27186 if (encoded_filename_len)
27187 encoded_filename_len++;
27189 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
27190 linebuf_len = strlen (linebuf);
27192 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
27193 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
27194 + 16 * 2 + 1);
27195 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
27196 tail = grp_name + 4;
27197 if (encoded_filename_len)
27199 for (i = 0; base[i]; i++)
27200 if (ISIDNUM (base[i]) || base[i] == '.')
27201 *tail++ = base[i];
27202 *tail++ = '.';
27204 memcpy (tail, linebuf, linebuf_len);
27205 tail += linebuf_len;
27206 *tail++ = '.';
27207 for (i = 0; i < 16; i++)
27208 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
27210 /* Construct a macinfo_entry for DW_MACRO_import
27211 in the empty vector entry before the first define/undef. */
27212 inc = &(*macinfo_table)[idx - 1];
27213 inc->code = DW_MACRO_import;
27214 inc->lineno = 0;
27215 inc->info = ggc_strdup (grp_name);
27216 if (!*macinfo_htab)
27217 *macinfo_htab = new macinfo_hash_type (10);
27218 /* Avoid emitting duplicates. */
27219 slot = (*macinfo_htab)->find_slot (inc, INSERT);
27220 if (*slot != NULL)
27222 inc->code = 0;
27223 inc->info = NULL;
27224 /* If such an entry has been used before, just emit
27225 a DW_MACRO_import op. */
27226 inc = *slot;
27227 output_macinfo_op (inc);
27228 /* And clear all macinfo_entry in the range to avoid emitting them
27229 in the second pass. */
27230 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
27232 cur->code = 0;
27233 cur->info = NULL;
27236 else
27238 *slot = inc;
27239 inc->lineno = (*macinfo_htab)->elements ();
27240 output_macinfo_op (inc);
27242 return count;
27245 /* Save any strings needed by the macinfo table in the debug str
27246 table. All strings must be collected into the table by the time
27247 index_string is called. */
27249 static void
27250 save_macinfo_strings (void)
27252 unsigned len;
27253 unsigned i;
27254 macinfo_entry *ref;
27256 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
27258 switch (ref->code)
27260 /* Match the logic in output_macinfo_op to decide on
27261 indirect strings. */
27262 case DW_MACINFO_define:
27263 case DW_MACINFO_undef:
27264 len = strlen (ref->info) + 1;
27265 if (!dwarf_strict
27266 && len > DWARF_OFFSET_SIZE
27267 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
27268 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
27269 set_indirect_string (find_AT_string (ref->info));
27270 break;
27271 case DW_MACRO_define_strp:
27272 case DW_MACRO_undef_strp:
27273 set_indirect_string (find_AT_string (ref->info));
27274 break;
27275 default:
27276 break;
27281 /* Output macinfo section(s). */
27283 static void
27284 output_macinfo (const char *debug_line_label, bool early_lto_debug)
27286 unsigned i;
27287 unsigned long length = vec_safe_length (macinfo_table);
27288 macinfo_entry *ref;
27289 vec<macinfo_entry, va_gc> *files = NULL;
27290 macinfo_hash_type *macinfo_htab = NULL;
27291 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
27293 if (! length)
27294 return;
27296 /* output_macinfo* uses these interchangeably. */
27297 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_define
27298 && (int) DW_MACINFO_undef == (int) DW_MACRO_undef
27299 && (int) DW_MACINFO_start_file == (int) DW_MACRO_start_file
27300 && (int) DW_MACINFO_end_file == (int) DW_MACRO_end_file);
27302 /* AIX Assembler inserts the length, so adjust the reference to match the
27303 offset expected by debuggers. */
27304 strcpy (dl_section_ref, debug_line_label);
27305 if (XCOFF_DEBUGGING_INFO)
27306 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
27308 /* For .debug_macro emit the section header. */
27309 if (!dwarf_strict || dwarf_version >= 5)
27311 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27312 "DWARF macro version number");
27313 if (DWARF_OFFSET_SIZE == 8)
27314 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
27315 else
27316 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
27317 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_label,
27318 debug_line_section, NULL);
27321 /* In the first loop, it emits the primary .debug_macinfo section
27322 and after each emitted op the macinfo_entry is cleared.
27323 If a longer range of define/undef ops can be optimized using
27324 DW_MACRO_import, the DW_MACRO_import op is emitted and kept in
27325 the vector before the first define/undef in the range and the
27326 whole range of define/undef ops is not emitted and kept. */
27327 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27329 switch (ref->code)
27331 case DW_MACINFO_start_file:
27332 vec_safe_push (files, *ref);
27333 break;
27334 case DW_MACINFO_end_file:
27335 if (!vec_safe_is_empty (files))
27336 files->pop ();
27337 break;
27338 case DW_MACINFO_define:
27339 case DW_MACINFO_undef:
27340 if ((!dwarf_strict || dwarf_version >= 5)
27341 && HAVE_COMDAT_GROUP
27342 && vec_safe_length (files) != 1
27343 && i > 0
27344 && i + 1 < length
27345 && (*macinfo_table)[i - 1].code == 0)
27347 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
27348 if (count)
27350 i += count - 1;
27351 continue;
27354 break;
27355 case 0:
27356 /* A dummy entry may be inserted at the beginning to be able
27357 to optimize the whole block of predefined macros. */
27358 if (i == 0)
27359 continue;
27360 default:
27361 break;
27363 output_macinfo_op (ref);
27364 ref->info = NULL;
27365 ref->code = 0;
27368 if (!macinfo_htab)
27369 return;
27371 /* Save the number of transparent includes so we can adjust the
27372 label number for the fat LTO object DWARF. */
27373 unsigned macinfo_label_base_adj = macinfo_htab->elements ();
27375 delete macinfo_htab;
27376 macinfo_htab = NULL;
27378 /* If any DW_MACRO_import were used, on those DW_MACRO_import entries
27379 terminate the current chain and switch to a new comdat .debug_macinfo
27380 section and emit the define/undef entries within it. */
27381 for (i = 0; macinfo_table->iterate (i, &ref); i++)
27382 switch (ref->code)
27384 case 0:
27385 continue;
27386 case DW_MACRO_import:
27388 char label[MAX_ARTIFICIAL_LABEL_BYTES];
27389 tree comdat_key = get_identifier (ref->info);
27390 /* Terminate the previous .debug_macinfo section. */
27391 dw2_asm_output_data (1, 0, "End compilation unit");
27392 targetm.asm_out.named_section (debug_macinfo_section_name,
27393 SECTION_DEBUG
27394 | SECTION_LINKONCE
27395 | (early_lto_debug
27396 ? SECTION_EXCLUDE : 0),
27397 comdat_key);
27398 ASM_GENERATE_INTERNAL_LABEL (label,
27399 DEBUG_MACRO_SECTION_LABEL,
27400 ref->lineno + macinfo_label_base);
27401 ASM_OUTPUT_LABEL (asm_out_file, label);
27402 ref->code = 0;
27403 ref->info = NULL;
27404 dw2_asm_output_data (2, dwarf_version >= 5 ? 5 : 4,
27405 "DWARF macro version number");
27406 if (DWARF_OFFSET_SIZE == 8)
27407 dw2_asm_output_data (1, 1, "Flags: 64-bit");
27408 else
27409 dw2_asm_output_data (1, 0, "Flags: 32-bit");
27411 break;
27412 case DW_MACINFO_define:
27413 case DW_MACINFO_undef:
27414 output_macinfo_op (ref);
27415 ref->code = 0;
27416 ref->info = NULL;
27417 break;
27418 default:
27419 gcc_unreachable ();
27422 macinfo_label_base += macinfo_label_base_adj;
27425 /* Initialize the various sections and labels for dwarf output and prefix
27426 them with PREFIX if non-NULL. Returns the generation (zero based
27427 number of times function was called). */
27429 static unsigned
27430 init_sections_and_labels (bool early_lto_debug)
27432 /* As we may get called multiple times have a generation count for
27433 labels. */
27434 static unsigned generation = 0;
27436 if (early_lto_debug)
27438 if (!dwarf_split_debug_info)
27440 debug_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27441 SECTION_DEBUG | SECTION_EXCLUDE,
27442 NULL);
27443 debug_abbrev_section = get_section (DEBUG_LTO_ABBREV_SECTION,
27444 SECTION_DEBUG | SECTION_EXCLUDE,
27445 NULL);
27446 debug_macinfo_section_name
27447 = ((dwarf_strict && dwarf_version < 5)
27448 ? DEBUG_LTO_MACINFO_SECTION : DEBUG_LTO_MACRO_SECTION);
27449 debug_macinfo_section = get_section (debug_macinfo_section_name,
27450 SECTION_DEBUG
27451 | SECTION_EXCLUDE, NULL);
27452 /* For macro info we have to refer to a debug_line section, so
27453 similar to split-dwarf emit a skeleton one for early debug. */
27454 debug_skeleton_line_section
27455 = get_section (DEBUG_LTO_LINE_SECTION,
27456 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27457 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27458 DEBUG_SKELETON_LINE_SECTION_LABEL,
27459 generation);
27461 else
27463 /* ??? Which of the following do we need early? */
27464 debug_info_section = get_section (DEBUG_LTO_DWO_INFO_SECTION,
27465 SECTION_DEBUG | SECTION_EXCLUDE,
27466 NULL);
27467 debug_abbrev_section = get_section (DEBUG_LTO_DWO_ABBREV_SECTION,
27468 SECTION_DEBUG | SECTION_EXCLUDE,
27469 NULL);
27470 debug_skeleton_info_section = get_section (DEBUG_LTO_INFO_SECTION,
27471 SECTION_DEBUG
27472 | SECTION_EXCLUDE, NULL);
27473 debug_skeleton_abbrev_section
27474 = get_section (DEBUG_LTO_ABBREV_SECTION,
27475 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27476 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27477 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27478 generation);
27480 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27481 stay in the main .o, but the skeleton_line goes into the split
27482 off dwo. */
27483 debug_skeleton_line_section
27484 = get_section (DEBUG_LTO_LINE_SECTION,
27485 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27486 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27487 DEBUG_SKELETON_LINE_SECTION_LABEL,
27488 generation);
27489 debug_str_offsets_section
27490 = get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
27491 SECTION_DEBUG | SECTION_EXCLUDE,
27492 NULL);
27493 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27494 DEBUG_SKELETON_INFO_SECTION_LABEL,
27495 generation);
27496 debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
27497 DEBUG_STR_DWO_SECTION_FLAGS,
27498 NULL);
27499 debug_macinfo_section_name
27500 = ((dwarf_strict && dwarf_version < 5)
27501 ? DEBUG_LTO_DWO_MACINFO_SECTION : DEBUG_LTO_DWO_MACRO_SECTION);
27502 debug_macinfo_section = get_section (debug_macinfo_section_name,
27503 SECTION_DEBUG | SECTION_EXCLUDE,
27504 NULL);
27506 debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
27507 DEBUG_STR_SECTION_FLAGS
27508 | SECTION_EXCLUDE, NULL);
27509 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27510 debug_line_str_section
27511 = get_section (DEBUG_LTO_LINE_STR_SECTION,
27512 DEBUG_STR_SECTION_FLAGS | SECTION_EXCLUDE, NULL);
27514 else
27516 if (!dwarf_split_debug_info)
27518 debug_info_section = get_section (DEBUG_INFO_SECTION,
27519 SECTION_DEBUG, NULL);
27520 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27521 SECTION_DEBUG, NULL);
27522 debug_loc_section = get_section (dwarf_version >= 5
27523 ? DEBUG_LOCLISTS_SECTION
27524 : DEBUG_LOC_SECTION,
27525 SECTION_DEBUG, NULL);
27526 debug_macinfo_section_name
27527 = ((dwarf_strict && dwarf_version < 5)
27528 ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION);
27529 debug_macinfo_section = get_section (debug_macinfo_section_name,
27530 SECTION_DEBUG, NULL);
27532 else
27534 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
27535 SECTION_DEBUG | SECTION_EXCLUDE,
27536 NULL);
27537 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
27538 SECTION_DEBUG | SECTION_EXCLUDE,
27539 NULL);
27540 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
27541 SECTION_DEBUG, NULL);
27542 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
27543 SECTION_DEBUG, NULL);
27544 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
27545 SECTION_DEBUG, NULL);
27546 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
27547 DEBUG_SKELETON_ABBREV_SECTION_LABEL,
27548 generation);
27550 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections
27551 stay in the main .o, but the skeleton_line goes into the
27552 split off dwo. */
27553 debug_skeleton_line_section
27554 = get_section (DEBUG_DWO_LINE_SECTION,
27555 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27556 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
27557 DEBUG_SKELETON_LINE_SECTION_LABEL,
27558 generation);
27559 debug_str_offsets_section
27560 = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
27561 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
27562 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
27563 DEBUG_SKELETON_INFO_SECTION_LABEL,
27564 generation);
27565 debug_loc_section = get_section (dwarf_version >= 5
27566 ? DEBUG_DWO_LOCLISTS_SECTION
27567 : DEBUG_DWO_LOC_SECTION,
27568 SECTION_DEBUG | SECTION_EXCLUDE,
27569 NULL);
27570 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
27571 DEBUG_STR_DWO_SECTION_FLAGS,
27572 NULL);
27573 debug_macinfo_section_name
27574 = ((dwarf_strict && dwarf_version < 5)
27575 ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION);
27576 debug_macinfo_section = get_section (debug_macinfo_section_name,
27577 SECTION_DEBUG | SECTION_EXCLUDE,
27578 NULL);
27580 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
27581 SECTION_DEBUG, NULL);
27582 debug_line_section = get_section (DEBUG_LINE_SECTION,
27583 SECTION_DEBUG, NULL);
27584 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
27585 SECTION_DEBUG, NULL);
27586 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
27587 SECTION_DEBUG, NULL);
27588 debug_str_section = get_section (DEBUG_STR_SECTION,
27589 DEBUG_STR_SECTION_FLAGS, NULL);
27590 if (!dwarf_split_debug_info && !DWARF2_ASM_LINE_DEBUG_INFO)
27591 debug_line_str_section = get_section (DEBUG_LINE_STR_SECTION,
27592 DEBUG_STR_SECTION_FLAGS, NULL);
27593 debug_ranges_section = get_section (dwarf_version >= 5
27594 ? DEBUG_RNGLISTS_SECTION
27595 : DEBUG_RANGES_SECTION,
27596 SECTION_DEBUG, NULL);
27597 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
27598 SECTION_DEBUG, NULL);
27601 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
27602 DEBUG_ABBREV_SECTION_LABEL, generation);
27603 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
27604 DEBUG_INFO_SECTION_LABEL, generation);
27605 info_section_emitted = false;
27606 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
27607 DEBUG_LINE_SECTION_LABEL, generation);
27608 /* There are up to 4 unique ranges labels per generation.
27609 See also output_rnglists. */
27610 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
27611 DEBUG_RANGES_SECTION_LABEL, generation * 4);
27612 if (dwarf_version >= 5 && dwarf_split_debug_info)
27613 ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
27614 DEBUG_RANGES_SECTION_LABEL,
27615 1 + generation * 4);
27616 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
27617 DEBUG_ADDR_SECTION_LABEL, generation);
27618 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
27619 (dwarf_strict && dwarf_version < 5)
27620 ? DEBUG_MACINFO_SECTION_LABEL
27621 : DEBUG_MACRO_SECTION_LABEL, generation);
27622 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
27623 generation);
27625 ++generation;
27626 return generation - 1;
27629 /* Set up for Dwarf output at the start of compilation. */
27631 static void
27632 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
27634 /* Allocate the file_table. */
27635 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
27637 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27638 /* Allocate the decl_die_table. */
27639 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
27641 /* Allocate the decl_loc_table. */
27642 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
27644 /* Allocate the cached_dw_loc_list_table. */
27645 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
27647 /* Allocate the initial hunk of the decl_scope_table. */
27648 vec_alloc (decl_scope_table, 256);
27650 /* Allocate the initial hunk of the abbrev_die_table. */
27651 vec_alloc (abbrev_die_table, 256);
27652 /* Zero-th entry is allocated, but unused. */
27653 abbrev_die_table->quick_push (NULL);
27655 /* Allocate the dwarf_proc_stack_usage_map. */
27656 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
27658 /* Allocate the pubtypes and pubnames vectors. */
27659 vec_alloc (pubname_table, 32);
27660 vec_alloc (pubtype_table, 32);
27662 vec_alloc (incomplete_types, 64);
27664 vec_alloc (used_rtx_array, 32);
27666 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
27667 vec_alloc (macinfo_table, 64);
27668 #endif
27670 /* If front-ends already registered a main translation unit but we were not
27671 ready to perform the association, do this now. */
27672 if (main_translation_unit != NULL_TREE)
27673 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
27676 /* Called before compile () starts outputtting functions, variables
27677 and toplevel asms into assembly. */
27679 static void
27680 dwarf2out_assembly_start (void)
27682 if (text_section_line_info)
27683 return;
27685 #ifndef DWARF2_LINENO_DEBUGGING_INFO
27686 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
27687 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
27688 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
27689 COLD_TEXT_SECTION_LABEL, 0);
27690 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
27692 switch_to_section (text_section);
27693 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
27694 #endif
27696 /* Make sure the line number table for .text always exists. */
27697 text_section_line_info = new_line_info_table ();
27698 text_section_line_info->end_label = text_end_label;
27700 #ifdef DWARF2_LINENO_DEBUGGING_INFO
27701 cur_line_info_table = text_section_line_info;
27702 #endif
27704 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
27705 && dwarf2out_do_cfi_asm ()
27706 && !dwarf2out_do_eh_frame ())
27707 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
27710 /* A helper function for dwarf2out_finish called through
27711 htab_traverse. Assign a string its index. All strings must be
27712 collected into the table by the time index_string is called,
27713 because the indexing code relies on htab_traverse to traverse nodes
27714 in the same order for each run. */
27717 index_string (indirect_string_node **h, unsigned int *index)
27719 indirect_string_node *node = *h;
27721 find_string_form (node);
27722 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27724 gcc_assert (node->index == NO_INDEX_ASSIGNED);
27725 node->index = *index;
27726 *index += 1;
27728 return 1;
27731 /* A helper function for output_indirect_strings called through
27732 htab_traverse. Output the offset to a string and update the
27733 current offset. */
27736 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
27738 indirect_string_node *node = *h;
27740 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27742 /* Assert that this node has been assigned an index. */
27743 gcc_assert (node->index != NO_INDEX_ASSIGNED
27744 && node->index != NOT_INDEXED);
27745 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
27746 "indexed string 0x%x: %s", node->index, node->str);
27747 *offset += strlen (node->str) + 1;
27749 return 1;
27752 /* A helper function for dwarf2out_finish called through
27753 htab_traverse. Output the indexed string. */
27756 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
27758 struct indirect_string_node *node = *h;
27760 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
27762 /* Assert that the strings are output in the same order as their
27763 indexes were assigned. */
27764 gcc_assert (*cur_idx == node->index);
27765 assemble_string (node->str, strlen (node->str) + 1);
27766 *cur_idx += 1;
27768 return 1;
27771 /* A helper function for dwarf2out_finish called through
27772 htab_traverse. Emit one queued .debug_str string. */
27775 output_indirect_string (indirect_string_node **h, enum dwarf_form form)
27777 struct indirect_string_node *node = *h;
27779 node->form = find_string_form (node);
27780 if (node->form == form && node->refcount > 0)
27782 ASM_OUTPUT_LABEL (asm_out_file, node->label);
27783 assemble_string (node->str, strlen (node->str) + 1);
27786 return 1;
27789 /* Output the indexed string table. */
27791 static void
27792 output_indirect_strings (void)
27794 switch_to_section (debug_str_section);
27795 if (!dwarf_split_debug_info)
27796 debug_str_hash->traverse<enum dwarf_form,
27797 output_indirect_string> (DW_FORM_strp);
27798 else
27800 unsigned int offset = 0;
27801 unsigned int cur_idx = 0;
27803 if (skeleton_debug_str_hash)
27804 skeleton_debug_str_hash->traverse<enum dwarf_form,
27805 output_indirect_string> (DW_FORM_strp);
27807 switch_to_section (debug_str_offsets_section);
27808 debug_str_hash->traverse_noresize
27809 <unsigned int *, output_index_string_offset> (&offset);
27810 switch_to_section (debug_str_dwo_section);
27811 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
27812 (&cur_idx);
27816 /* Callback for htab_traverse to assign an index to an entry in the
27817 table, and to write that entry to the .debug_addr section. */
27820 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
27822 addr_table_entry *entry = *slot;
27824 if (entry->refcount == 0)
27826 gcc_assert (entry->index == NO_INDEX_ASSIGNED
27827 || entry->index == NOT_INDEXED);
27828 return 1;
27831 gcc_assert (entry->index == *cur_index);
27832 (*cur_index)++;
27834 switch (entry->kind)
27836 case ate_kind_rtx:
27837 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
27838 "0x%x", entry->index);
27839 break;
27840 case ate_kind_rtx_dtprel:
27841 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
27842 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
27843 DWARF2_ADDR_SIZE,
27844 entry->addr.rtl);
27845 fputc ('\n', asm_out_file);
27846 break;
27847 case ate_kind_label:
27848 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
27849 "0x%x", entry->index);
27850 break;
27851 default:
27852 gcc_unreachable ();
27854 return 1;
27857 /* Produce the .debug_addr section. */
27859 static void
27860 output_addr_table (void)
27862 unsigned int index = 0;
27863 if (addr_index_table == NULL || addr_index_table->size () == 0)
27864 return;
27866 switch_to_section (debug_addr_section);
27867 addr_index_table
27868 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
27871 #if ENABLE_ASSERT_CHECKING
27872 /* Verify that all marks are clear. */
27874 static void
27875 verify_marks_clear (dw_die_ref die)
27877 dw_die_ref c;
27879 gcc_assert (! die->die_mark);
27880 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
27882 #endif /* ENABLE_ASSERT_CHECKING */
27884 /* Clear the marks for a die and its children.
27885 Be cool if the mark isn't set. */
27887 static void
27888 prune_unmark_dies (dw_die_ref die)
27890 dw_die_ref c;
27892 if (die->die_mark)
27893 die->die_mark = 0;
27894 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
27897 /* Given LOC that is referenced by a DIE we're marking as used, find all
27898 referenced DWARF procedures it references and mark them as used. */
27900 static void
27901 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
27903 for (; loc != NULL; loc = loc->dw_loc_next)
27904 switch (loc->dw_loc_opc)
27906 case DW_OP_implicit_pointer:
27907 case DW_OP_convert:
27908 case DW_OP_reinterpret:
27909 case DW_OP_GNU_implicit_pointer:
27910 case DW_OP_GNU_convert:
27911 case DW_OP_GNU_reinterpret:
27912 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
27913 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27914 break;
27915 case DW_OP_GNU_variable_value:
27916 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
27918 dw_die_ref ref
27919 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
27920 if (ref == NULL)
27921 break;
27922 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
27923 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
27924 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
27926 /* FALLTHRU */
27927 case DW_OP_call2:
27928 case DW_OP_call4:
27929 case DW_OP_call_ref:
27930 case DW_OP_const_type:
27931 case DW_OP_GNU_const_type:
27932 case DW_OP_GNU_parameter_ref:
27933 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
27934 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
27935 break;
27936 case DW_OP_regval_type:
27937 case DW_OP_deref_type:
27938 case DW_OP_GNU_regval_type:
27939 case DW_OP_GNU_deref_type:
27940 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
27941 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
27942 break;
27943 case DW_OP_entry_value:
27944 case DW_OP_GNU_entry_value:
27945 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
27946 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
27947 break;
27948 default:
27949 break;
27953 /* Given DIE that we're marking as used, find any other dies
27954 it references as attributes and mark them as used. */
27956 static void
27957 prune_unused_types_walk_attribs (dw_die_ref die)
27959 dw_attr_node *a;
27960 unsigned ix;
27962 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27964 switch (AT_class (a))
27966 /* Make sure DWARF procedures referenced by location descriptions will
27967 get emitted. */
27968 case dw_val_class_loc:
27969 prune_unused_types_walk_loc_descr (AT_loc (a));
27970 break;
27971 case dw_val_class_loc_list:
27972 for (dw_loc_list_ref list = AT_loc_list (a);
27973 list != NULL;
27974 list = list->dw_loc_next)
27975 prune_unused_types_walk_loc_descr (list->expr);
27976 break;
27978 case dw_val_class_die_ref:
27979 /* A reference to another DIE.
27980 Make sure that it will get emitted.
27981 If it was broken out into a comdat group, don't follow it. */
27982 if (! AT_ref (a)->comdat_type_p
27983 || a->dw_attr == DW_AT_specification)
27984 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
27985 break;
27987 case dw_val_class_str:
27988 /* Set the string's refcount to 0 so that prune_unused_types_mark
27989 accounts properly for it. */
27990 a->dw_attr_val.v.val_str->refcount = 0;
27991 break;
27993 default:
27994 break;
27999 /* Mark the generic parameters and arguments children DIEs of DIE. */
28001 static void
28002 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
28004 dw_die_ref c;
28006 if (die == NULL || die->die_child == NULL)
28007 return;
28008 c = die->die_child;
28011 if (is_template_parameter (c))
28012 prune_unused_types_mark (c, 1);
28013 c = c->die_sib;
28014 } while (c && c != die->die_child);
28017 /* Mark DIE as being used. If DOKIDS is true, then walk down
28018 to DIE's children. */
28020 static void
28021 prune_unused_types_mark (dw_die_ref die, int dokids)
28023 dw_die_ref c;
28025 if (die->die_mark == 0)
28027 /* We haven't done this node yet. Mark it as used. */
28028 die->die_mark = 1;
28029 /* If this is the DIE of a generic type instantiation,
28030 mark the children DIEs that describe its generic parms and
28031 args. */
28032 prune_unused_types_mark_generic_parms_dies (die);
28034 /* We also have to mark its parents as used.
28035 (But we don't want to mark our parent's kids due to this,
28036 unless it is a class.) */
28037 if (die->die_parent)
28038 prune_unused_types_mark (die->die_parent,
28039 class_scope_p (die->die_parent));
28041 /* Mark any referenced nodes. */
28042 prune_unused_types_walk_attribs (die);
28044 /* If this node is a specification,
28045 also mark the definition, if it exists. */
28046 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
28047 prune_unused_types_mark (die->die_definition, 1);
28050 if (dokids && die->die_mark != 2)
28052 /* We need to walk the children, but haven't done so yet.
28053 Remember that we've walked the kids. */
28054 die->die_mark = 2;
28056 /* If this is an array type, we need to make sure our
28057 kids get marked, even if they're types. If we're
28058 breaking out types into comdat sections, do this
28059 for all type definitions. */
28060 if (die->die_tag == DW_TAG_array_type
28061 || (use_debug_types
28062 && is_type_die (die) && ! is_declaration_die (die)))
28063 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
28064 else
28065 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28069 /* For local classes, look if any static member functions were emitted
28070 and if so, mark them. */
28072 static void
28073 prune_unused_types_walk_local_classes (dw_die_ref die)
28075 dw_die_ref c;
28077 if (die->die_mark == 2)
28078 return;
28080 switch (die->die_tag)
28082 case DW_TAG_structure_type:
28083 case DW_TAG_union_type:
28084 case DW_TAG_class_type:
28085 break;
28087 case DW_TAG_subprogram:
28088 if (!get_AT_flag (die, DW_AT_declaration)
28089 || die->die_definition != NULL)
28090 prune_unused_types_mark (die, 1);
28091 return;
28093 default:
28094 return;
28097 /* Mark children. */
28098 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
28101 /* Walk the tree DIE and mark types that we actually use. */
28103 static void
28104 prune_unused_types_walk (dw_die_ref die)
28106 dw_die_ref c;
28108 /* Don't do anything if this node is already marked and
28109 children have been marked as well. */
28110 if (die->die_mark == 2)
28111 return;
28113 switch (die->die_tag)
28115 case DW_TAG_structure_type:
28116 case DW_TAG_union_type:
28117 case DW_TAG_class_type:
28118 if (die->die_perennial_p)
28119 break;
28121 for (c = die->die_parent; c; c = c->die_parent)
28122 if (c->die_tag == DW_TAG_subprogram)
28123 break;
28125 /* Finding used static member functions inside of classes
28126 is needed just for local classes, because for other classes
28127 static member function DIEs with DW_AT_specification
28128 are emitted outside of the DW_TAG_*_type. If we ever change
28129 it, we'd need to call this even for non-local classes. */
28130 if (c)
28131 prune_unused_types_walk_local_classes (die);
28133 /* It's a type node --- don't mark it. */
28134 return;
28136 case DW_TAG_const_type:
28137 case DW_TAG_packed_type:
28138 case DW_TAG_pointer_type:
28139 case DW_TAG_reference_type:
28140 case DW_TAG_rvalue_reference_type:
28141 case DW_TAG_volatile_type:
28142 case DW_TAG_typedef:
28143 case DW_TAG_array_type:
28144 case DW_TAG_interface_type:
28145 case DW_TAG_friend:
28146 case DW_TAG_enumeration_type:
28147 case DW_TAG_subroutine_type:
28148 case DW_TAG_string_type:
28149 case DW_TAG_set_type:
28150 case DW_TAG_subrange_type:
28151 case DW_TAG_ptr_to_member_type:
28152 case DW_TAG_file_type:
28153 /* Type nodes are useful only when other DIEs reference them --- don't
28154 mark them. */
28155 /* FALLTHROUGH */
28157 case DW_TAG_dwarf_procedure:
28158 /* Likewise for DWARF procedures. */
28160 if (die->die_perennial_p)
28161 break;
28163 return;
28165 default:
28166 /* Mark everything else. */
28167 break;
28170 if (die->die_mark == 0)
28172 die->die_mark = 1;
28174 /* Now, mark any dies referenced from here. */
28175 prune_unused_types_walk_attribs (die);
28178 die->die_mark = 2;
28180 /* Mark children. */
28181 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
28184 /* Increment the string counts on strings referred to from DIE's
28185 attributes. */
28187 static void
28188 prune_unused_types_update_strings (dw_die_ref die)
28190 dw_attr_node *a;
28191 unsigned ix;
28193 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
28194 if (AT_class (a) == dw_val_class_str)
28196 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
28197 s->refcount++;
28198 /* Avoid unnecessarily putting strings that are used less than
28199 twice in the hash table. */
28200 if (s->refcount
28201 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
28203 indirect_string_node **slot
28204 = debug_str_hash->find_slot_with_hash (s->str,
28205 htab_hash_string (s->str),
28206 INSERT);
28207 gcc_assert (*slot == NULL);
28208 *slot = s;
28213 /* Mark DIE and its children as removed. */
28215 static void
28216 mark_removed (dw_die_ref die)
28218 dw_die_ref c;
28219 die->removed = true;
28220 FOR_EACH_CHILD (die, c, mark_removed (c));
28223 /* Remove from the tree DIE any dies that aren't marked. */
28225 static void
28226 prune_unused_types_prune (dw_die_ref die)
28228 dw_die_ref c;
28230 gcc_assert (die->die_mark);
28231 prune_unused_types_update_strings (die);
28233 if (! die->die_child)
28234 return;
28236 c = die->die_child;
28237 do {
28238 dw_die_ref prev = c, next;
28239 for (c = c->die_sib; ! c->die_mark; c = next)
28240 if (c == die->die_child)
28242 /* No marked children between 'prev' and the end of the list. */
28243 if (prev == c)
28244 /* No marked children at all. */
28245 die->die_child = NULL;
28246 else
28248 prev->die_sib = c->die_sib;
28249 die->die_child = prev;
28251 c->die_sib = NULL;
28252 mark_removed (c);
28253 return;
28255 else
28257 next = c->die_sib;
28258 c->die_sib = NULL;
28259 mark_removed (c);
28262 if (c != prev->die_sib)
28263 prev->die_sib = c;
28264 prune_unused_types_prune (c);
28265 } while (c != die->die_child);
28268 /* Remove dies representing declarations that we never use. */
28270 static void
28271 prune_unused_types (void)
28273 unsigned int i;
28274 limbo_die_node *node;
28275 comdat_type_node *ctnode;
28276 pubname_entry *pub;
28277 dw_die_ref base_type;
28279 #if ENABLE_ASSERT_CHECKING
28280 /* All the marks should already be clear. */
28281 verify_marks_clear (comp_unit_die ());
28282 for (node = limbo_die_list; node; node = node->next)
28283 verify_marks_clear (node->die);
28284 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28285 verify_marks_clear (ctnode->root_die);
28286 #endif /* ENABLE_ASSERT_CHECKING */
28288 /* Mark types that are used in global variables. */
28289 premark_types_used_by_global_vars ();
28291 /* Set the mark on nodes that are actually used. */
28292 prune_unused_types_walk (comp_unit_die ());
28293 for (node = limbo_die_list; node; node = node->next)
28294 prune_unused_types_walk (node->die);
28295 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28297 prune_unused_types_walk (ctnode->root_die);
28298 prune_unused_types_mark (ctnode->type_die, 1);
28301 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
28302 are unusual in that they are pubnames that are the children of pubtypes.
28303 They should only be marked via their parent DW_TAG_enumeration_type die,
28304 not as roots in themselves. */
28305 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
28306 if (pub->die->die_tag != DW_TAG_enumerator)
28307 prune_unused_types_mark (pub->die, 1);
28308 for (i = 0; base_types.iterate (i, &base_type); i++)
28309 prune_unused_types_mark (base_type, 1);
28311 /* For -fvar-tracking-assignments, also set the mark on nodes that could be
28312 referenced by DW_TAG_call_site DW_AT_call_origin (i.e. direct call
28313 callees). */
28314 cgraph_node *cnode;
28315 FOR_EACH_FUNCTION (cnode)
28316 if (cnode->referred_to_p (false))
28318 dw_die_ref die = lookup_decl_die (cnode->decl);
28319 if (die == NULL || die->die_mark)
28320 continue;
28321 for (cgraph_edge *e = cnode->callers; e; e = e->next_caller)
28322 if (e->caller != cnode
28323 && opt_for_fn (e->caller->decl, flag_var_tracking_assignments))
28325 prune_unused_types_mark (die, 1);
28326 break;
28330 if (debug_str_hash)
28331 debug_str_hash->empty ();
28332 if (skeleton_debug_str_hash)
28333 skeleton_debug_str_hash->empty ();
28334 prune_unused_types_prune (comp_unit_die ());
28335 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
28337 node = *pnode;
28338 if (!node->die->die_mark)
28339 *pnode = node->next;
28340 else
28342 prune_unused_types_prune (node->die);
28343 pnode = &node->next;
28346 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28347 prune_unused_types_prune (ctnode->root_die);
28349 /* Leave the marks clear. */
28350 prune_unmark_dies (comp_unit_die ());
28351 for (node = limbo_die_list; node; node = node->next)
28352 prune_unmark_dies (node->die);
28353 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
28354 prune_unmark_dies (ctnode->root_die);
28357 /* Helpers to manipulate hash table of comdat type units. */
28359 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
28361 static inline hashval_t hash (const comdat_type_node *);
28362 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
28365 inline hashval_t
28366 comdat_type_hasher::hash (const comdat_type_node *type_node)
28368 hashval_t h;
28369 memcpy (&h, type_node->signature, sizeof (h));
28370 return h;
28373 inline bool
28374 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
28375 const comdat_type_node *type_node_2)
28377 return (! memcmp (type_node_1->signature, type_node_2->signature,
28378 DWARF_TYPE_SIGNATURE_SIZE));
28381 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
28382 to the location it would have been added, should we know its
28383 DECL_ASSEMBLER_NAME when we added other attributes. This will
28384 probably improve compactness of debug info, removing equivalent
28385 abbrevs, and hide any differences caused by deferring the
28386 computation of the assembler name, triggered by e.g. PCH. */
28388 static inline void
28389 move_linkage_attr (dw_die_ref die)
28391 unsigned ix = vec_safe_length (die->die_attr);
28392 dw_attr_node linkage = (*die->die_attr)[ix - 1];
28394 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
28395 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
28397 while (--ix > 0)
28399 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
28401 if (prev->dw_attr == DW_AT_decl_line
28402 || prev->dw_attr == DW_AT_decl_column
28403 || prev->dw_attr == DW_AT_name)
28404 break;
28407 if (ix != vec_safe_length (die->die_attr) - 1)
28409 die->die_attr->pop ();
28410 die->die_attr->quick_insert (ix, linkage);
28414 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
28415 referenced from typed stack ops and count how often they are used. */
28417 static void
28418 mark_base_types (dw_loc_descr_ref loc)
28420 dw_die_ref base_type = NULL;
28422 for (; loc; loc = loc->dw_loc_next)
28424 switch (loc->dw_loc_opc)
28426 case DW_OP_regval_type:
28427 case DW_OP_deref_type:
28428 case DW_OP_GNU_regval_type:
28429 case DW_OP_GNU_deref_type:
28430 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
28431 break;
28432 case DW_OP_convert:
28433 case DW_OP_reinterpret:
28434 case DW_OP_GNU_convert:
28435 case DW_OP_GNU_reinterpret:
28436 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
28437 continue;
28438 /* FALLTHRU */
28439 case DW_OP_const_type:
28440 case DW_OP_GNU_const_type:
28441 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
28442 break;
28443 case DW_OP_entry_value:
28444 case DW_OP_GNU_entry_value:
28445 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
28446 continue;
28447 default:
28448 continue;
28450 gcc_assert (base_type->die_parent == comp_unit_die ());
28451 if (base_type->die_mark)
28452 base_type->die_mark++;
28453 else
28455 base_types.safe_push (base_type);
28456 base_type->die_mark = 1;
28461 /* Comparison function for sorting marked base types. */
28463 static int
28464 base_type_cmp (const void *x, const void *y)
28466 dw_die_ref dx = *(const dw_die_ref *) x;
28467 dw_die_ref dy = *(const dw_die_ref *) y;
28468 unsigned int byte_size1, byte_size2;
28469 unsigned int encoding1, encoding2;
28470 unsigned int align1, align2;
28471 if (dx->die_mark > dy->die_mark)
28472 return -1;
28473 if (dx->die_mark < dy->die_mark)
28474 return 1;
28475 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
28476 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
28477 if (byte_size1 < byte_size2)
28478 return 1;
28479 if (byte_size1 > byte_size2)
28480 return -1;
28481 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
28482 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
28483 if (encoding1 < encoding2)
28484 return 1;
28485 if (encoding1 > encoding2)
28486 return -1;
28487 align1 = get_AT_unsigned (dx, DW_AT_alignment);
28488 align2 = get_AT_unsigned (dy, DW_AT_alignment);
28489 if (align1 < align2)
28490 return 1;
28491 if (align1 > align2)
28492 return -1;
28493 return 0;
28496 /* Move base types marked by mark_base_types as early as possible
28497 in the CU, sorted by decreasing usage count both to make the
28498 uleb128 references as small as possible and to make sure they
28499 will have die_offset already computed by calc_die_sizes when
28500 sizes of typed stack loc ops is computed. */
28502 static void
28503 move_marked_base_types (void)
28505 unsigned int i;
28506 dw_die_ref base_type, die, c;
28508 if (base_types.is_empty ())
28509 return;
28511 /* Sort by decreasing usage count, they will be added again in that
28512 order later on. */
28513 base_types.qsort (base_type_cmp);
28514 die = comp_unit_die ();
28515 c = die->die_child;
28518 dw_die_ref prev = c;
28519 c = c->die_sib;
28520 while (c->die_mark)
28522 remove_child_with_prev (c, prev);
28523 /* As base types got marked, there must be at least
28524 one node other than DW_TAG_base_type. */
28525 gcc_assert (die->die_child != NULL);
28526 c = prev->die_sib;
28529 while (c != die->die_child);
28530 gcc_assert (die->die_child);
28531 c = die->die_child;
28532 for (i = 0; base_types.iterate (i, &base_type); i++)
28534 base_type->die_mark = 0;
28535 base_type->die_sib = c->die_sib;
28536 c->die_sib = base_type;
28537 c = base_type;
28541 /* Helper function for resolve_addr, attempt to resolve
28542 one CONST_STRING, return true if successful. Similarly verify that
28543 SYMBOL_REFs refer to variables emitted in the current CU. */
28545 static bool
28546 resolve_one_addr (rtx *addr)
28548 rtx rtl = *addr;
28550 if (GET_CODE (rtl) == CONST_STRING)
28552 size_t len = strlen (XSTR (rtl, 0)) + 1;
28553 tree t = build_string (len, XSTR (rtl, 0));
28554 tree tlen = size_int (len - 1);
28555 TREE_TYPE (t)
28556 = build_array_type (char_type_node, build_index_type (tlen));
28557 rtl = lookup_constant_def (t);
28558 if (!rtl || !MEM_P (rtl))
28559 return false;
28560 rtl = XEXP (rtl, 0);
28561 if (GET_CODE (rtl) == SYMBOL_REF
28562 && SYMBOL_REF_DECL (rtl)
28563 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28564 return false;
28565 vec_safe_push (used_rtx_array, rtl);
28566 *addr = rtl;
28567 return true;
28570 if (GET_CODE (rtl) == SYMBOL_REF
28571 && SYMBOL_REF_DECL (rtl))
28573 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
28575 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
28576 return false;
28578 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
28579 return false;
28582 if (GET_CODE (rtl) == CONST)
28584 subrtx_ptr_iterator::array_type array;
28585 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
28586 if (!resolve_one_addr (*iter))
28587 return false;
28590 return true;
28593 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
28594 if possible, and create DW_TAG_dwarf_procedure that can be referenced
28595 from DW_OP_implicit_pointer if the string hasn't been seen yet. */
28597 static rtx
28598 string_cst_pool_decl (tree t)
28600 rtx rtl = output_constant_def (t, 1);
28601 unsigned char *array;
28602 dw_loc_descr_ref l;
28603 tree decl;
28604 size_t len;
28605 dw_die_ref ref;
28607 if (!rtl || !MEM_P (rtl))
28608 return NULL_RTX;
28609 rtl = XEXP (rtl, 0);
28610 if (GET_CODE (rtl) != SYMBOL_REF
28611 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
28612 return NULL_RTX;
28614 decl = SYMBOL_REF_DECL (rtl);
28615 if (!lookup_decl_die (decl))
28617 len = TREE_STRING_LENGTH (t);
28618 vec_safe_push (used_rtx_array, rtl);
28619 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
28620 array = ggc_vec_alloc<unsigned char> (len);
28621 memcpy (array, TREE_STRING_POINTER (t), len);
28622 l = new_loc_descr (DW_OP_implicit_value, len, 0);
28623 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
28624 l->dw_loc_oprnd2.v.val_vec.length = len;
28625 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
28626 l->dw_loc_oprnd2.v.val_vec.array = array;
28627 add_AT_loc (ref, DW_AT_location, l);
28628 equate_decl_number_to_die (decl, ref);
28630 return rtl;
28633 /* Helper function of resolve_addr_in_expr. LOC is
28634 a DW_OP_addr followed by DW_OP_stack_value, either at the start
28635 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
28636 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
28637 with DW_OP_implicit_pointer if possible
28638 and return true, if unsuccessful, return false. */
28640 static bool
28641 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
28643 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
28644 HOST_WIDE_INT offset = 0;
28645 dw_die_ref ref = NULL;
28646 tree decl;
28648 if (GET_CODE (rtl) == CONST
28649 && GET_CODE (XEXP (rtl, 0)) == PLUS
28650 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
28652 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
28653 rtl = XEXP (XEXP (rtl, 0), 0);
28655 if (GET_CODE (rtl) == CONST_STRING)
28657 size_t len = strlen (XSTR (rtl, 0)) + 1;
28658 tree t = build_string (len, XSTR (rtl, 0));
28659 tree tlen = size_int (len - 1);
28661 TREE_TYPE (t)
28662 = build_array_type (char_type_node, build_index_type (tlen));
28663 rtl = string_cst_pool_decl (t);
28664 if (!rtl)
28665 return false;
28667 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
28669 decl = SYMBOL_REF_DECL (rtl);
28670 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
28672 ref = lookup_decl_die (decl);
28673 if (ref && (get_AT (ref, DW_AT_location)
28674 || get_AT (ref, DW_AT_const_value)))
28676 loc->dw_loc_opc = dwarf_OP (DW_OP_implicit_pointer);
28677 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28678 loc->dw_loc_oprnd1.val_entry = NULL;
28679 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28680 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28681 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28682 loc->dw_loc_oprnd2.v.val_int = offset;
28683 return true;
28687 return false;
28690 /* Helper function for resolve_addr, handle one location
28691 expression, return false if at least one CONST_STRING or SYMBOL_REF in
28692 the location list couldn't be resolved. */
28694 static bool
28695 resolve_addr_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
28697 dw_loc_descr_ref keep = NULL;
28698 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
28699 switch (loc->dw_loc_opc)
28701 case DW_OP_addr:
28702 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28704 if ((prev == NULL
28705 || prev->dw_loc_opc == DW_OP_piece
28706 || prev->dw_loc_opc == DW_OP_bit_piece)
28707 && loc->dw_loc_next
28708 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
28709 && (!dwarf_strict || dwarf_version >= 5)
28710 && optimize_one_addr_into_implicit_ptr (loc))
28711 break;
28712 return false;
28714 break;
28715 case DW_OP_GNU_addr_index:
28716 case DW_OP_GNU_const_index:
28717 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
28718 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
28720 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
28721 if (!resolve_one_addr (&rtl))
28722 return false;
28723 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
28724 loc->dw_loc_oprnd1.val_entry
28725 = add_addr_table_entry (rtl, ate_kind_rtx);
28727 break;
28728 case DW_OP_const4u:
28729 case DW_OP_const8u:
28730 if (loc->dtprel
28731 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
28732 return false;
28733 break;
28734 case DW_OP_plus_uconst:
28735 if (size_of_loc_descr (loc)
28736 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
28738 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
28740 dw_loc_descr_ref repl
28741 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
28742 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
28743 add_loc_descr (&repl, loc->dw_loc_next);
28744 *loc = *repl;
28746 break;
28747 case DW_OP_implicit_value:
28748 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
28749 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
28750 return false;
28751 break;
28752 case DW_OP_implicit_pointer:
28753 case DW_OP_GNU_implicit_pointer:
28754 case DW_OP_GNU_parameter_ref:
28755 case DW_OP_GNU_variable_value:
28756 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
28758 dw_die_ref ref
28759 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
28760 if (ref == NULL)
28761 return false;
28762 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28763 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
28764 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
28766 if (loc->dw_loc_opc == DW_OP_GNU_variable_value)
28768 if (prev == NULL
28769 && loc->dw_loc_next == NULL
28770 && AT_class (a) == dw_val_class_loc)
28771 switch (a->dw_attr)
28773 /* Following attributes allow both exprloc and reference,
28774 so if the whole expression is DW_OP_GNU_variable_value
28775 alone we could transform it into reference. */
28776 case DW_AT_byte_size:
28777 case DW_AT_bit_size:
28778 case DW_AT_lower_bound:
28779 case DW_AT_upper_bound:
28780 case DW_AT_bit_stride:
28781 case DW_AT_count:
28782 case DW_AT_allocated:
28783 case DW_AT_associated:
28784 case DW_AT_byte_stride:
28785 a->dw_attr_val.val_class = dw_val_class_die_ref;
28786 a->dw_attr_val.val_entry = NULL;
28787 a->dw_attr_val.v.val_die_ref.die
28788 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28789 a->dw_attr_val.v.val_die_ref.external = 0;
28790 return true;
28791 default:
28792 break;
28794 if (dwarf_strict)
28795 return false;
28797 break;
28798 case DW_OP_const_type:
28799 case DW_OP_regval_type:
28800 case DW_OP_deref_type:
28801 case DW_OP_convert:
28802 case DW_OP_reinterpret:
28803 case DW_OP_GNU_const_type:
28804 case DW_OP_GNU_regval_type:
28805 case DW_OP_GNU_deref_type:
28806 case DW_OP_GNU_convert:
28807 case DW_OP_GNU_reinterpret:
28808 while (loc->dw_loc_next
28809 && (loc->dw_loc_next->dw_loc_opc == DW_OP_convert
28810 || loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert))
28812 dw_die_ref base1, base2;
28813 unsigned enc1, enc2, size1, size2;
28814 if (loc->dw_loc_opc == DW_OP_regval_type
28815 || loc->dw_loc_opc == DW_OP_deref_type
28816 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28817 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28818 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
28819 else if (loc->dw_loc_oprnd1.val_class
28820 == dw_val_class_unsigned_const)
28821 break;
28822 else
28823 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
28824 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
28825 == dw_val_class_unsigned_const)
28826 break;
28827 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
28828 gcc_assert (base1->die_tag == DW_TAG_base_type
28829 && base2->die_tag == DW_TAG_base_type);
28830 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
28831 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
28832 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
28833 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
28834 if (size1 == size2
28835 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
28836 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
28837 && loc != keep)
28838 || enc1 == enc2))
28840 /* Optimize away next DW_OP_convert after
28841 adjusting LOC's base type die reference. */
28842 if (loc->dw_loc_opc == DW_OP_regval_type
28843 || loc->dw_loc_opc == DW_OP_deref_type
28844 || loc->dw_loc_opc == DW_OP_GNU_regval_type
28845 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
28846 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
28847 else
28848 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
28849 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
28850 continue;
28852 /* Don't change integer DW_OP_convert after e.g. floating
28853 point typed stack entry. */
28854 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
28855 keep = loc->dw_loc_next;
28856 break;
28858 break;
28859 default:
28860 break;
28862 return true;
28865 /* Helper function of resolve_addr. DIE had DW_AT_location of
28866 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
28867 and DW_OP_addr couldn't be resolved. resolve_addr has already
28868 removed the DW_AT_location attribute. This function attempts to
28869 add a new DW_AT_location attribute with DW_OP_implicit_pointer
28870 to it or DW_AT_const_value attribute, if possible. */
28872 static void
28873 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
28875 if (!VAR_P (decl)
28876 || lookup_decl_die (decl) != die
28877 || DECL_EXTERNAL (decl)
28878 || !TREE_STATIC (decl)
28879 || DECL_INITIAL (decl) == NULL_TREE
28880 || DECL_P (DECL_INITIAL (decl))
28881 || get_AT (die, DW_AT_const_value))
28882 return;
28884 tree init = DECL_INITIAL (decl);
28885 HOST_WIDE_INT offset = 0;
28886 /* For variables that have been optimized away and thus
28887 don't have a memory location, see if we can emit
28888 DW_AT_const_value instead. */
28889 if (tree_add_const_value_attribute (die, init))
28890 return;
28891 if (dwarf_strict && dwarf_version < 5)
28892 return;
28893 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
28894 and ADDR_EXPR refers to a decl that has DW_AT_location or
28895 DW_AT_const_value (but isn't addressable, otherwise
28896 resolving the original DW_OP_addr wouldn't fail), see if
28897 we can add DW_OP_implicit_pointer. */
28898 STRIP_NOPS (init);
28899 if (TREE_CODE (init) == POINTER_PLUS_EXPR
28900 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
28902 offset = tree_to_shwi (TREE_OPERAND (init, 1));
28903 init = TREE_OPERAND (init, 0);
28904 STRIP_NOPS (init);
28906 if (TREE_CODE (init) != ADDR_EXPR)
28907 return;
28908 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
28909 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
28910 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
28911 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
28912 && TREE_OPERAND (init, 0) != decl))
28914 dw_die_ref ref;
28915 dw_loc_descr_ref l;
28917 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
28919 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
28920 if (!rtl)
28921 return;
28922 decl = SYMBOL_REF_DECL (rtl);
28924 else
28925 decl = TREE_OPERAND (init, 0);
28926 ref = lookup_decl_die (decl);
28927 if (ref == NULL
28928 || (!get_AT (ref, DW_AT_location)
28929 && !get_AT (ref, DW_AT_const_value)))
28930 return;
28931 l = new_loc_descr (dwarf_OP (DW_OP_implicit_pointer), 0, offset);
28932 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
28933 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
28934 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
28935 add_AT_loc (die, DW_AT_location, l);
28939 /* Return NULL if l is a DWARF expression, or first op that is not
28940 valid DWARF expression. */
28942 static dw_loc_descr_ref
28943 non_dwarf_expression (dw_loc_descr_ref l)
28945 while (l)
28947 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28948 return l;
28949 switch (l->dw_loc_opc)
28951 case DW_OP_regx:
28952 case DW_OP_implicit_value:
28953 case DW_OP_stack_value:
28954 case DW_OP_implicit_pointer:
28955 case DW_OP_GNU_implicit_pointer:
28956 case DW_OP_GNU_parameter_ref:
28957 case DW_OP_piece:
28958 case DW_OP_bit_piece:
28959 return l;
28960 default:
28961 break;
28963 l = l->dw_loc_next;
28965 return NULL;
28968 /* Return adjusted copy of EXPR:
28969 If it is empty DWARF expression, return it.
28970 If it is valid non-empty DWARF expression,
28971 return copy of EXPR with DW_OP_deref appended to it.
28972 If it is DWARF expression followed by DW_OP_reg{N,x}, return
28973 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended.
28974 If it is DWARF expression followed by DW_OP_stack_value, return
28975 copy of the DWARF expression without anything appended.
28976 Otherwise, return NULL. */
28978 static dw_loc_descr_ref
28979 copy_deref_exprloc (dw_loc_descr_ref expr)
28981 dw_loc_descr_ref tail = NULL;
28983 if (expr == NULL)
28984 return NULL;
28986 dw_loc_descr_ref l = non_dwarf_expression (expr);
28987 if (l && l->dw_loc_next)
28988 return NULL;
28990 if (l)
28992 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
28993 tail = new_loc_descr ((enum dwarf_location_atom)
28994 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
28995 0, 0);
28996 else
28997 switch (l->dw_loc_opc)
28999 case DW_OP_regx:
29000 tail = new_loc_descr (DW_OP_bregx,
29001 l->dw_loc_oprnd1.v.val_unsigned, 0);
29002 break;
29003 case DW_OP_stack_value:
29004 break;
29005 default:
29006 return NULL;
29009 else
29010 tail = new_loc_descr (DW_OP_deref, 0, 0);
29012 dw_loc_descr_ref ret = NULL, *p = &ret;
29013 while (expr != l)
29015 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
29016 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
29017 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
29018 p = &(*p)->dw_loc_next;
29019 expr = expr->dw_loc_next;
29021 *p = tail;
29022 return ret;
29025 /* For DW_AT_string_length attribute with DW_OP_GNU_variable_value
29026 reference to a variable or argument, adjust it if needed and return:
29027 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
29028 attribute if present should be removed
29029 0 keep the attribute perhaps with minor modifications, no need to rescan
29030 1 if the attribute has been successfully adjusted. */
29032 static int
29033 optimize_string_length (dw_attr_node *a)
29035 dw_loc_descr_ref l = AT_loc (a), lv;
29036 dw_die_ref die;
29037 if (l->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
29039 tree decl = l->dw_loc_oprnd1.v.val_decl_ref;
29040 die = lookup_decl_die (decl);
29041 if (die)
29043 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
29044 l->dw_loc_oprnd1.v.val_die_ref.die = die;
29045 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
29047 else
29048 return -1;
29050 else
29051 die = l->dw_loc_oprnd1.v.val_die_ref.die;
29053 /* DWARF5 allows reference class, so we can then reference the DIE.
29054 Only do this for DW_OP_GNU_variable_value DW_OP_stack_value. */
29055 if (l->dw_loc_next != NULL && dwarf_version >= 5)
29057 a->dw_attr_val.val_class = dw_val_class_die_ref;
29058 a->dw_attr_val.val_entry = NULL;
29059 a->dw_attr_val.v.val_die_ref.die = die;
29060 a->dw_attr_val.v.val_die_ref.external = 0;
29061 return 0;
29064 dw_attr_node *av = get_AT (die, DW_AT_location);
29065 dw_loc_list_ref d;
29066 bool non_dwarf_expr = false;
29068 if (av == NULL)
29069 return dwarf_strict ? -1 : 0;
29070 switch (AT_class (av))
29072 case dw_val_class_loc_list:
29073 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29074 if (d->expr && non_dwarf_expression (d->expr))
29075 non_dwarf_expr = true;
29076 break;
29077 case dw_val_class_loc:
29078 lv = AT_loc (av);
29079 if (lv == NULL)
29080 return dwarf_strict ? -1 : 0;
29081 if (non_dwarf_expression (lv))
29082 non_dwarf_expr = true;
29083 break;
29084 default:
29085 return dwarf_strict ? -1 : 0;
29088 /* If it is safe to transform DW_OP_GNU_variable_value DW_OP_stack_value
29089 into DW_OP_call4 or DW_OP_GNU_variable_value into
29090 DW_OP_call4 DW_OP_deref, do so. */
29091 if (!non_dwarf_expr
29092 && (l->dw_loc_next != NULL || AT_class (av) == dw_val_class_loc))
29094 l->dw_loc_opc = DW_OP_call4;
29095 if (l->dw_loc_next)
29096 l->dw_loc_next = NULL;
29097 else
29098 l->dw_loc_next = new_loc_descr (DW_OP_deref, 0, 0);
29099 return 0;
29102 /* For DW_OP_GNU_variable_value DW_OP_stack_value, we can just
29103 copy over the DW_AT_location attribute from die to a. */
29104 if (l->dw_loc_next != NULL)
29106 a->dw_attr_val = av->dw_attr_val;
29107 return 1;
29110 dw_loc_list_ref list, *p;
29111 switch (AT_class (av))
29113 case dw_val_class_loc_list:
29114 p = &list;
29115 list = NULL;
29116 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
29118 lv = copy_deref_exprloc (d->expr);
29119 if (lv)
29121 *p = new_loc_list (lv, d->begin, d->end, d->section);
29122 p = &(*p)->dw_loc_next;
29124 else if (!dwarf_strict && d->expr)
29125 return 0;
29127 if (list == NULL)
29128 return dwarf_strict ? -1 : 0;
29129 a->dw_attr_val.val_class = dw_val_class_loc_list;
29130 gen_llsym (list);
29131 *AT_loc_list_ptr (a) = list;
29132 return 1;
29133 case dw_val_class_loc:
29134 lv = copy_deref_exprloc (AT_loc (av));
29135 if (lv == NULL)
29136 return dwarf_strict ? -1 : 0;
29137 a->dw_attr_val.v.val_loc = lv;
29138 return 1;
29139 default:
29140 gcc_unreachable ();
29144 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
29145 an address in .rodata section if the string literal is emitted there,
29146 or remove the containing location list or replace DW_AT_const_value
29147 with DW_AT_location and empty location expression, if it isn't found
29148 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
29149 to something that has been emitted in the current CU. */
29151 static void
29152 resolve_addr (dw_die_ref die)
29154 dw_die_ref c;
29155 dw_attr_node *a;
29156 dw_loc_list_ref *curr, *start, loc;
29157 unsigned ix;
29158 bool remove_AT_byte_size = false;
29160 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29161 switch (AT_class (a))
29163 case dw_val_class_loc_list:
29164 start = curr = AT_loc_list_ptr (a);
29165 loc = *curr;
29166 gcc_assert (loc);
29167 /* The same list can be referenced more than once. See if we have
29168 already recorded the result from a previous pass. */
29169 if (loc->replaced)
29170 *curr = loc->dw_loc_next;
29171 else if (!loc->resolved_addr)
29173 /* As things stand, we do not expect or allow one die to
29174 reference a suffix of another die's location list chain.
29175 References must be identical or completely separate.
29176 There is therefore no need to cache the result of this
29177 pass on any list other than the first; doing so
29178 would lead to unnecessary writes. */
29179 while (*curr)
29181 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
29182 if (!resolve_addr_in_expr (a, (*curr)->expr))
29184 dw_loc_list_ref next = (*curr)->dw_loc_next;
29185 dw_loc_descr_ref l = (*curr)->expr;
29187 if (next && (*curr)->ll_symbol)
29189 gcc_assert (!next->ll_symbol);
29190 next->ll_symbol = (*curr)->ll_symbol;
29192 if (dwarf_split_debug_info)
29193 remove_loc_list_addr_table_entries (l);
29194 *curr = next;
29196 else
29198 mark_base_types ((*curr)->expr);
29199 curr = &(*curr)->dw_loc_next;
29202 if (loc == *start)
29203 loc->resolved_addr = 1;
29204 else
29206 loc->replaced = 1;
29207 loc->dw_loc_next = *start;
29210 if (!*start)
29212 remove_AT (die, a->dw_attr);
29213 ix--;
29215 break;
29216 case dw_val_class_loc:
29218 dw_loc_descr_ref l = AT_loc (a);
29219 /* DW_OP_GNU_variable_value DW_OP_stack_value or
29220 DW_OP_GNU_variable_value in DW_AT_string_length can be converted
29221 into DW_OP_call4 or DW_OP_call4 DW_OP_deref, which is standard
29222 DWARF4 unlike DW_OP_GNU_variable_value. Or for DWARF5
29223 DW_OP_GNU_variable_value DW_OP_stack_value can be replaced
29224 with DW_FORM_ref referencing the same DIE as
29225 DW_OP_GNU_variable_value used to reference. */
29226 if (a->dw_attr == DW_AT_string_length
29227 && l
29228 && l->dw_loc_opc == DW_OP_GNU_variable_value
29229 && (l->dw_loc_next == NULL
29230 || (l->dw_loc_next->dw_loc_next == NULL
29231 && l->dw_loc_next->dw_loc_opc == DW_OP_stack_value)))
29233 switch (optimize_string_length (a))
29235 case -1:
29236 remove_AT (die, a->dw_attr);
29237 ix--;
29238 /* If we drop DW_AT_string_length, we need to drop also
29239 DW_AT_{string_length_,}byte_size. */
29240 remove_AT_byte_size = true;
29241 continue;
29242 default:
29243 break;
29244 case 1:
29245 /* Even if we keep the optimized DW_AT_string_length,
29246 it might have changed AT_class, so process it again. */
29247 ix--;
29248 continue;
29251 /* For -gdwarf-2 don't attempt to optimize
29252 DW_AT_data_member_location containing
29253 DW_OP_plus_uconst - older consumers might
29254 rely on it being that op instead of a more complex,
29255 but shorter, location description. */
29256 if ((dwarf_version > 2
29257 || a->dw_attr != DW_AT_data_member_location
29258 || l == NULL
29259 || l->dw_loc_opc != DW_OP_plus_uconst
29260 || l->dw_loc_next != NULL)
29261 && !resolve_addr_in_expr (a, l))
29263 if (dwarf_split_debug_info)
29264 remove_loc_list_addr_table_entries (l);
29265 if (l != NULL
29266 && l->dw_loc_next == NULL
29267 && l->dw_loc_opc == DW_OP_addr
29268 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
29269 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
29270 && a->dw_attr == DW_AT_location)
29272 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
29273 remove_AT (die, a->dw_attr);
29274 ix--;
29275 optimize_location_into_implicit_ptr (die, decl);
29276 break;
29278 if (a->dw_attr == DW_AT_string_length)
29279 /* If we drop DW_AT_string_length, we need to drop also
29280 DW_AT_{string_length_,}byte_size. */
29281 remove_AT_byte_size = true;
29282 remove_AT (die, a->dw_attr);
29283 ix--;
29285 else
29286 mark_base_types (l);
29288 break;
29289 case dw_val_class_addr:
29290 if (a->dw_attr == DW_AT_const_value
29291 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
29293 if (AT_index (a) != NOT_INDEXED)
29294 remove_addr_table_entry (a->dw_attr_val.val_entry);
29295 remove_AT (die, a->dw_attr);
29296 ix--;
29298 if ((die->die_tag == DW_TAG_call_site
29299 && a->dw_attr == DW_AT_call_origin)
29300 || (die->die_tag == DW_TAG_GNU_call_site
29301 && a->dw_attr == DW_AT_abstract_origin))
29303 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
29304 dw_die_ref tdie = lookup_decl_die (tdecl);
29305 dw_die_ref cdie;
29306 if (tdie == NULL
29307 && DECL_EXTERNAL (tdecl)
29308 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
29309 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
29311 dw_die_ref pdie = cdie;
29312 /* Make sure we don't add these DIEs into type units.
29313 We could emit skeleton DIEs for context (namespaces,
29314 outer structs/classes) and a skeleton DIE for the
29315 innermost context with DW_AT_signature pointing to the
29316 type unit. See PR78835. */
29317 while (pdie && pdie->die_tag != DW_TAG_type_unit)
29318 pdie = pdie->die_parent;
29319 if (pdie == NULL)
29321 /* Creating a full DIE for tdecl is overly expensive and
29322 at this point even wrong when in the LTO phase
29323 as it can end up generating new type DIEs we didn't
29324 output and thus optimize_external_refs will crash. */
29325 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
29326 add_AT_flag (tdie, DW_AT_external, 1);
29327 add_AT_flag (tdie, DW_AT_declaration, 1);
29328 add_linkage_attr (tdie, tdecl);
29329 add_name_and_src_coords_attributes (tdie, tdecl, true);
29330 equate_decl_number_to_die (tdecl, tdie);
29333 if (tdie)
29335 a->dw_attr_val.val_class = dw_val_class_die_ref;
29336 a->dw_attr_val.v.val_die_ref.die = tdie;
29337 a->dw_attr_val.v.val_die_ref.external = 0;
29339 else
29341 if (AT_index (a) != NOT_INDEXED)
29342 remove_addr_table_entry (a->dw_attr_val.val_entry);
29343 remove_AT (die, a->dw_attr);
29344 ix--;
29347 break;
29348 default:
29349 break;
29352 if (remove_AT_byte_size)
29353 remove_AT (die, dwarf_version >= 5
29354 ? DW_AT_string_length_byte_size
29355 : DW_AT_byte_size);
29357 FOR_EACH_CHILD (die, c, resolve_addr (c));
29360 /* Helper routines for optimize_location_lists.
29361 This pass tries to share identical local lists in .debug_loc
29362 section. */
29364 /* Iteratively hash operands of LOC opcode into HSTATE. */
29366 static void
29367 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
29369 dw_val_ref val1 = &loc->dw_loc_oprnd1;
29370 dw_val_ref val2 = &loc->dw_loc_oprnd2;
29372 switch (loc->dw_loc_opc)
29374 case DW_OP_const4u:
29375 case DW_OP_const8u:
29376 if (loc->dtprel)
29377 goto hash_addr;
29378 /* FALLTHRU */
29379 case DW_OP_const1u:
29380 case DW_OP_const1s:
29381 case DW_OP_const2u:
29382 case DW_OP_const2s:
29383 case DW_OP_const4s:
29384 case DW_OP_const8s:
29385 case DW_OP_constu:
29386 case DW_OP_consts:
29387 case DW_OP_pick:
29388 case DW_OP_plus_uconst:
29389 case DW_OP_breg0:
29390 case DW_OP_breg1:
29391 case DW_OP_breg2:
29392 case DW_OP_breg3:
29393 case DW_OP_breg4:
29394 case DW_OP_breg5:
29395 case DW_OP_breg6:
29396 case DW_OP_breg7:
29397 case DW_OP_breg8:
29398 case DW_OP_breg9:
29399 case DW_OP_breg10:
29400 case DW_OP_breg11:
29401 case DW_OP_breg12:
29402 case DW_OP_breg13:
29403 case DW_OP_breg14:
29404 case DW_OP_breg15:
29405 case DW_OP_breg16:
29406 case DW_OP_breg17:
29407 case DW_OP_breg18:
29408 case DW_OP_breg19:
29409 case DW_OP_breg20:
29410 case DW_OP_breg21:
29411 case DW_OP_breg22:
29412 case DW_OP_breg23:
29413 case DW_OP_breg24:
29414 case DW_OP_breg25:
29415 case DW_OP_breg26:
29416 case DW_OP_breg27:
29417 case DW_OP_breg28:
29418 case DW_OP_breg29:
29419 case DW_OP_breg30:
29420 case DW_OP_breg31:
29421 case DW_OP_regx:
29422 case DW_OP_fbreg:
29423 case DW_OP_piece:
29424 case DW_OP_deref_size:
29425 case DW_OP_xderef_size:
29426 hstate.add_object (val1->v.val_int);
29427 break;
29428 case DW_OP_skip:
29429 case DW_OP_bra:
29431 int offset;
29433 gcc_assert (val1->val_class == dw_val_class_loc);
29434 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
29435 hstate.add_object (offset);
29437 break;
29438 case DW_OP_implicit_value:
29439 hstate.add_object (val1->v.val_unsigned);
29440 switch (val2->val_class)
29442 case dw_val_class_const:
29443 hstate.add_object (val2->v.val_int);
29444 break;
29445 case dw_val_class_vec:
29447 unsigned int elt_size = val2->v.val_vec.elt_size;
29448 unsigned int len = val2->v.val_vec.length;
29450 hstate.add_int (elt_size);
29451 hstate.add_int (len);
29452 hstate.add (val2->v.val_vec.array, len * elt_size);
29454 break;
29455 case dw_val_class_const_double:
29456 hstate.add_object (val2->v.val_double.low);
29457 hstate.add_object (val2->v.val_double.high);
29458 break;
29459 case dw_val_class_wide_int:
29460 hstate.add (val2->v.val_wide->get_val (),
29461 get_full_len (*val2->v.val_wide)
29462 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29463 break;
29464 case dw_val_class_addr:
29465 inchash::add_rtx (val2->v.val_addr, hstate);
29466 break;
29467 default:
29468 gcc_unreachable ();
29470 break;
29471 case DW_OP_bregx:
29472 case DW_OP_bit_piece:
29473 hstate.add_object (val1->v.val_int);
29474 hstate.add_object (val2->v.val_int);
29475 break;
29476 case DW_OP_addr:
29477 hash_addr:
29478 if (loc->dtprel)
29480 unsigned char dtprel = 0xd1;
29481 hstate.add_object (dtprel);
29483 inchash::add_rtx (val1->v.val_addr, hstate);
29484 break;
29485 case DW_OP_GNU_addr_index:
29486 case DW_OP_GNU_const_index:
29488 if (loc->dtprel)
29490 unsigned char dtprel = 0xd1;
29491 hstate.add_object (dtprel);
29493 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
29495 break;
29496 case DW_OP_implicit_pointer:
29497 case DW_OP_GNU_implicit_pointer:
29498 hstate.add_int (val2->v.val_int);
29499 break;
29500 case DW_OP_entry_value:
29501 case DW_OP_GNU_entry_value:
29502 hstate.add_object (val1->v.val_loc);
29503 break;
29504 case DW_OP_regval_type:
29505 case DW_OP_deref_type:
29506 case DW_OP_GNU_regval_type:
29507 case DW_OP_GNU_deref_type:
29509 unsigned int byte_size
29510 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
29511 unsigned int encoding
29512 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
29513 hstate.add_object (val1->v.val_int);
29514 hstate.add_object (byte_size);
29515 hstate.add_object (encoding);
29517 break;
29518 case DW_OP_convert:
29519 case DW_OP_reinterpret:
29520 case DW_OP_GNU_convert:
29521 case DW_OP_GNU_reinterpret:
29522 if (val1->val_class == dw_val_class_unsigned_const)
29524 hstate.add_object (val1->v.val_unsigned);
29525 break;
29527 /* FALLTHRU */
29528 case DW_OP_const_type:
29529 case DW_OP_GNU_const_type:
29531 unsigned int byte_size
29532 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
29533 unsigned int encoding
29534 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
29535 hstate.add_object (byte_size);
29536 hstate.add_object (encoding);
29537 if (loc->dw_loc_opc != DW_OP_const_type
29538 && loc->dw_loc_opc != DW_OP_GNU_const_type)
29539 break;
29540 hstate.add_object (val2->val_class);
29541 switch (val2->val_class)
29543 case dw_val_class_const:
29544 hstate.add_object (val2->v.val_int);
29545 break;
29546 case dw_val_class_vec:
29548 unsigned int elt_size = val2->v.val_vec.elt_size;
29549 unsigned int len = val2->v.val_vec.length;
29551 hstate.add_object (elt_size);
29552 hstate.add_object (len);
29553 hstate.add (val2->v.val_vec.array, len * elt_size);
29555 break;
29556 case dw_val_class_const_double:
29557 hstate.add_object (val2->v.val_double.low);
29558 hstate.add_object (val2->v.val_double.high);
29559 break;
29560 case dw_val_class_wide_int:
29561 hstate.add (val2->v.val_wide->get_val (),
29562 get_full_len (*val2->v.val_wide)
29563 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
29564 break;
29565 default:
29566 gcc_unreachable ();
29569 break;
29571 default:
29572 /* Other codes have no operands. */
29573 break;
29577 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
29579 static inline void
29580 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
29582 dw_loc_descr_ref l;
29583 bool sizes_computed = false;
29584 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
29585 size_of_locs (loc);
29587 for (l = loc; l != NULL; l = l->dw_loc_next)
29589 enum dwarf_location_atom opc = l->dw_loc_opc;
29590 hstate.add_object (opc);
29591 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
29593 size_of_locs (loc);
29594 sizes_computed = true;
29596 hash_loc_operands (l, hstate);
29600 /* Compute hash of the whole location list LIST_HEAD. */
29602 static inline void
29603 hash_loc_list (dw_loc_list_ref list_head)
29605 dw_loc_list_ref curr = list_head;
29606 inchash::hash hstate;
29608 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
29610 hstate.add (curr->begin, strlen (curr->begin) + 1);
29611 hstate.add (curr->end, strlen (curr->end) + 1);
29612 if (curr->section)
29613 hstate.add (curr->section, strlen (curr->section) + 1);
29614 hash_locs (curr->expr, hstate);
29616 list_head->hash = hstate.end ();
29619 /* Return true if X and Y opcodes have the same operands. */
29621 static inline bool
29622 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
29624 dw_val_ref valx1 = &x->dw_loc_oprnd1;
29625 dw_val_ref valx2 = &x->dw_loc_oprnd2;
29626 dw_val_ref valy1 = &y->dw_loc_oprnd1;
29627 dw_val_ref valy2 = &y->dw_loc_oprnd2;
29629 switch (x->dw_loc_opc)
29631 case DW_OP_const4u:
29632 case DW_OP_const8u:
29633 if (x->dtprel)
29634 goto hash_addr;
29635 /* FALLTHRU */
29636 case DW_OP_const1u:
29637 case DW_OP_const1s:
29638 case DW_OP_const2u:
29639 case DW_OP_const2s:
29640 case DW_OP_const4s:
29641 case DW_OP_const8s:
29642 case DW_OP_constu:
29643 case DW_OP_consts:
29644 case DW_OP_pick:
29645 case DW_OP_plus_uconst:
29646 case DW_OP_breg0:
29647 case DW_OP_breg1:
29648 case DW_OP_breg2:
29649 case DW_OP_breg3:
29650 case DW_OP_breg4:
29651 case DW_OP_breg5:
29652 case DW_OP_breg6:
29653 case DW_OP_breg7:
29654 case DW_OP_breg8:
29655 case DW_OP_breg9:
29656 case DW_OP_breg10:
29657 case DW_OP_breg11:
29658 case DW_OP_breg12:
29659 case DW_OP_breg13:
29660 case DW_OP_breg14:
29661 case DW_OP_breg15:
29662 case DW_OP_breg16:
29663 case DW_OP_breg17:
29664 case DW_OP_breg18:
29665 case DW_OP_breg19:
29666 case DW_OP_breg20:
29667 case DW_OP_breg21:
29668 case DW_OP_breg22:
29669 case DW_OP_breg23:
29670 case DW_OP_breg24:
29671 case DW_OP_breg25:
29672 case DW_OP_breg26:
29673 case DW_OP_breg27:
29674 case DW_OP_breg28:
29675 case DW_OP_breg29:
29676 case DW_OP_breg30:
29677 case DW_OP_breg31:
29678 case DW_OP_regx:
29679 case DW_OP_fbreg:
29680 case DW_OP_piece:
29681 case DW_OP_deref_size:
29682 case DW_OP_xderef_size:
29683 return valx1->v.val_int == valy1->v.val_int;
29684 case DW_OP_skip:
29685 case DW_OP_bra:
29686 /* If splitting debug info, the use of DW_OP_GNU_addr_index
29687 can cause irrelevant differences in dw_loc_addr. */
29688 gcc_assert (valx1->val_class == dw_val_class_loc
29689 && valy1->val_class == dw_val_class_loc
29690 && (dwarf_split_debug_info
29691 || x->dw_loc_addr == y->dw_loc_addr));
29692 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
29693 case DW_OP_implicit_value:
29694 if (valx1->v.val_unsigned != valy1->v.val_unsigned
29695 || valx2->val_class != valy2->val_class)
29696 return false;
29697 switch (valx2->val_class)
29699 case dw_val_class_const:
29700 return valx2->v.val_int == valy2->v.val_int;
29701 case dw_val_class_vec:
29702 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29703 && valx2->v.val_vec.length == valy2->v.val_vec.length
29704 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29705 valx2->v.val_vec.elt_size
29706 * valx2->v.val_vec.length) == 0;
29707 case dw_val_class_const_double:
29708 return valx2->v.val_double.low == valy2->v.val_double.low
29709 && valx2->v.val_double.high == valy2->v.val_double.high;
29710 case dw_val_class_wide_int:
29711 return *valx2->v.val_wide == *valy2->v.val_wide;
29712 case dw_val_class_addr:
29713 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
29714 default:
29715 gcc_unreachable ();
29717 case DW_OP_bregx:
29718 case DW_OP_bit_piece:
29719 return valx1->v.val_int == valy1->v.val_int
29720 && valx2->v.val_int == valy2->v.val_int;
29721 case DW_OP_addr:
29722 hash_addr:
29723 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
29724 case DW_OP_GNU_addr_index:
29725 case DW_OP_GNU_const_index:
29727 rtx ax1 = valx1->val_entry->addr.rtl;
29728 rtx ay1 = valy1->val_entry->addr.rtl;
29729 return rtx_equal_p (ax1, ay1);
29731 case DW_OP_implicit_pointer:
29732 case DW_OP_GNU_implicit_pointer:
29733 return valx1->val_class == dw_val_class_die_ref
29734 && valx1->val_class == valy1->val_class
29735 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
29736 && valx2->v.val_int == valy2->v.val_int;
29737 case DW_OP_entry_value:
29738 case DW_OP_GNU_entry_value:
29739 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
29740 case DW_OP_const_type:
29741 case DW_OP_GNU_const_type:
29742 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
29743 || valx2->val_class != valy2->val_class)
29744 return false;
29745 switch (valx2->val_class)
29747 case dw_val_class_const:
29748 return valx2->v.val_int == valy2->v.val_int;
29749 case dw_val_class_vec:
29750 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
29751 && valx2->v.val_vec.length == valy2->v.val_vec.length
29752 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
29753 valx2->v.val_vec.elt_size
29754 * valx2->v.val_vec.length) == 0;
29755 case dw_val_class_const_double:
29756 return valx2->v.val_double.low == valy2->v.val_double.low
29757 && valx2->v.val_double.high == valy2->v.val_double.high;
29758 case dw_val_class_wide_int:
29759 return *valx2->v.val_wide == *valy2->v.val_wide;
29760 default:
29761 gcc_unreachable ();
29763 case DW_OP_regval_type:
29764 case DW_OP_deref_type:
29765 case DW_OP_GNU_regval_type:
29766 case DW_OP_GNU_deref_type:
29767 return valx1->v.val_int == valy1->v.val_int
29768 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
29769 case DW_OP_convert:
29770 case DW_OP_reinterpret:
29771 case DW_OP_GNU_convert:
29772 case DW_OP_GNU_reinterpret:
29773 if (valx1->val_class != valy1->val_class)
29774 return false;
29775 if (valx1->val_class == dw_val_class_unsigned_const)
29776 return valx1->v.val_unsigned == valy1->v.val_unsigned;
29777 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29778 case DW_OP_GNU_parameter_ref:
29779 return valx1->val_class == dw_val_class_die_ref
29780 && valx1->val_class == valy1->val_class
29781 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
29782 default:
29783 /* Other codes have no operands. */
29784 return true;
29788 /* Return true if DWARF location expressions X and Y are the same. */
29790 static inline bool
29791 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
29793 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
29794 if (x->dw_loc_opc != y->dw_loc_opc
29795 || x->dtprel != y->dtprel
29796 || !compare_loc_operands (x, y))
29797 break;
29798 return x == NULL && y == NULL;
29801 /* Hashtable helpers. */
29803 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
29805 static inline hashval_t hash (const dw_loc_list_struct *);
29806 static inline bool equal (const dw_loc_list_struct *,
29807 const dw_loc_list_struct *);
29810 /* Return precomputed hash of location list X. */
29812 inline hashval_t
29813 loc_list_hasher::hash (const dw_loc_list_struct *x)
29815 return x->hash;
29818 /* Return true if location lists A and B are the same. */
29820 inline bool
29821 loc_list_hasher::equal (const dw_loc_list_struct *a,
29822 const dw_loc_list_struct *b)
29824 if (a == b)
29825 return 1;
29826 if (a->hash != b->hash)
29827 return 0;
29828 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
29829 if (strcmp (a->begin, b->begin) != 0
29830 || strcmp (a->end, b->end) != 0
29831 || (a->section == NULL) != (b->section == NULL)
29832 || (a->section && strcmp (a->section, b->section) != 0)
29833 || !compare_locs (a->expr, b->expr))
29834 break;
29835 return a == NULL && b == NULL;
29838 typedef hash_table<loc_list_hasher> loc_list_hash_type;
29841 /* Recursively optimize location lists referenced from DIE
29842 children and share them whenever possible. */
29844 static void
29845 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
29847 dw_die_ref c;
29848 dw_attr_node *a;
29849 unsigned ix;
29850 dw_loc_list_struct **slot;
29852 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29853 if (AT_class (a) == dw_val_class_loc_list)
29855 dw_loc_list_ref list = AT_loc_list (a);
29856 /* TODO: perform some optimizations here, before hashing
29857 it and storing into the hash table. */
29858 hash_loc_list (list);
29859 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
29860 if (*slot == NULL)
29861 *slot = list;
29862 else
29863 a->dw_attr_val.v.val_loc_list = *slot;
29866 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
29870 /* Recursively assign each location list a unique index into the debug_addr
29871 section. */
29873 static void
29874 index_location_lists (dw_die_ref die)
29876 dw_die_ref c;
29877 dw_attr_node *a;
29878 unsigned ix;
29880 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
29881 if (AT_class (a) == dw_val_class_loc_list)
29883 dw_loc_list_ref list = AT_loc_list (a);
29884 dw_loc_list_ref curr;
29885 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
29887 /* Don't index an entry that has already been indexed
29888 or won't be output. */
29889 if (curr->begin_entry != NULL
29890 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
29891 continue;
29893 curr->begin_entry
29894 = add_addr_table_entry (xstrdup (curr->begin), ate_kind_label);
29898 FOR_EACH_CHILD (die, c, index_location_lists (c));
29901 /* Optimize location lists referenced from DIE
29902 children and share them whenever possible. */
29904 static void
29905 optimize_location_lists (dw_die_ref die)
29907 loc_list_hash_type htab (500);
29908 optimize_location_lists_1 (die, &htab);
29911 /* Traverse the limbo die list, and add parent/child links. The only
29912 dies without parents that should be here are concrete instances of
29913 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
29914 For concrete instances, we can get the parent die from the abstract
29915 instance. */
29917 static void
29918 flush_limbo_die_list (void)
29920 limbo_die_node *node;
29922 /* get_context_die calls force_decl_die, which can put new DIEs on the
29923 limbo list in LTO mode when nested functions are put in a different
29924 partition than that of their parent function. */
29925 while ((node = limbo_die_list))
29927 dw_die_ref die = node->die;
29928 limbo_die_list = node->next;
29930 if (die->die_parent == NULL)
29932 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
29934 if (origin && origin->die_parent)
29935 add_child_die (origin->die_parent, die);
29936 else if (is_cu_die (die))
29938 else if (seen_error ())
29939 /* It's OK to be confused by errors in the input. */
29940 add_child_die (comp_unit_die (), die);
29941 else
29943 /* In certain situations, the lexical block containing a
29944 nested function can be optimized away, which results
29945 in the nested function die being orphaned. Likewise
29946 with the return type of that nested function. Force
29947 this to be a child of the containing function.
29949 It may happen that even the containing function got fully
29950 inlined and optimized out. In that case we are lost and
29951 assign the empty child. This should not be big issue as
29952 the function is likely unreachable too. */
29953 gcc_assert (node->created_for);
29955 if (DECL_P (node->created_for))
29956 origin = get_context_die (DECL_CONTEXT (node->created_for));
29957 else if (TYPE_P (node->created_for))
29958 origin = scope_die_for (node->created_for, comp_unit_die ());
29959 else
29960 origin = comp_unit_die ();
29962 add_child_die (origin, die);
29968 /* Reset DIEs so we can output them again. */
29970 static void
29971 reset_dies (dw_die_ref die)
29973 dw_die_ref c;
29975 /* Remove stuff we re-generate. */
29976 die->die_mark = 0;
29977 die->die_offset = 0;
29978 die->die_abbrev = 0;
29979 remove_AT (die, DW_AT_sibling);
29981 FOR_EACH_CHILD (die, c, reset_dies (c));
29984 /* Output stuff that dwarf requires at the end of every file,
29985 and generate the DWARF-2 debugging info. */
29987 static void
29988 dwarf2out_finish (const char *)
29990 comdat_type_node *ctnode;
29991 dw_die_ref main_comp_unit_die;
29992 unsigned char checksum[16];
29993 char dl_section_ref[MAX_ARTIFICIAL_LABEL_BYTES];
29995 /* Flush out any latecomers to the limbo party. */
29996 flush_limbo_die_list ();
29998 if (flag_checking)
30000 verify_die (comp_unit_die ());
30001 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30002 verify_die (node->die);
30005 /* We shouldn't have any symbols with delayed asm names for
30006 DIEs generated after early finish. */
30007 gcc_assert (deferred_asm_name == NULL);
30009 gen_remaining_tmpl_value_param_die_attribute ();
30011 if (flag_generate_lto || flag_generate_offload)
30013 gcc_assert (flag_fat_lto_objects || flag_generate_offload);
30015 /* Prune stuff so that dwarf2out_finish runs successfully
30016 for the fat part of the object. */
30017 reset_dies (comp_unit_die ());
30018 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30019 reset_dies (node->die);
30021 hash_table<comdat_type_hasher> comdat_type_table (100);
30022 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30024 comdat_type_node **slot
30025 = comdat_type_table.find_slot (ctnode, INSERT);
30027 /* Don't reset types twice. */
30028 if (*slot != HTAB_EMPTY_ENTRY)
30029 continue;
30031 /* Add a pointer to the line table for the main compilation unit
30032 so that the debugger can make sense of DW_AT_decl_file
30033 attributes. */
30034 if (debug_info_level >= DINFO_LEVEL_TERSE)
30035 reset_dies (ctnode->root_die);
30037 *slot = ctnode;
30040 /* Reset die CU symbol so we don't output it twice. */
30041 comp_unit_die ()->die_id.die_symbol = NULL;
30043 /* Remove DW_AT_macro from the early output. */
30044 if (have_macinfo)
30045 remove_AT (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE);
30047 /* Remove indirect string decisions. */
30048 debug_str_hash->traverse<void *, reset_indirect_string> (NULL);
30051 #if ENABLE_ASSERT_CHECKING
30053 dw_die_ref die = comp_unit_die (), c;
30054 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
30056 #endif
30057 resolve_addr (comp_unit_die ());
30058 move_marked_base_types ();
30060 /* Initialize sections and labels used for actual assembler output. */
30061 unsigned generation = init_sections_and_labels (false);
30063 /* Traverse the DIE's and add sibling attributes to those DIE's that
30064 have children. */
30065 add_sibling_attributes (comp_unit_die ());
30066 limbo_die_node *node;
30067 for (node = cu_die_list; node; node = node->next)
30068 add_sibling_attributes (node->die);
30069 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30070 add_sibling_attributes (ctnode->root_die);
30072 /* When splitting DWARF info, we put some attributes in the
30073 skeleton compile_unit DIE that remains in the .o, while
30074 most attributes go in the DWO compile_unit_die. */
30075 if (dwarf_split_debug_info)
30077 limbo_die_node *cu;
30078 main_comp_unit_die = gen_compile_unit_die (NULL);
30079 if (dwarf_version >= 5)
30080 main_comp_unit_die->die_tag = DW_TAG_skeleton_unit;
30081 cu = limbo_die_list;
30082 gcc_assert (cu->die == main_comp_unit_die);
30083 limbo_die_list = limbo_die_list->next;
30084 cu->next = cu_die_list;
30085 cu_die_list = cu;
30087 else
30088 main_comp_unit_die = comp_unit_die ();
30090 /* Output a terminator label for the .text section. */
30091 switch_to_section (text_section);
30092 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
30093 if (cold_text_section)
30095 switch_to_section (cold_text_section);
30096 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
30099 /* We can only use the low/high_pc attributes if all of the code was
30100 in .text. */
30101 if (!have_multiple_function_sections
30102 || (dwarf_version < 3 && dwarf_strict))
30104 /* Don't add if the CU has no associated code. */
30105 if (text_section_used)
30106 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
30107 text_end_label, true);
30109 else
30111 unsigned fde_idx;
30112 dw_fde_ref fde;
30113 bool range_list_added = false;
30115 if (text_section_used)
30116 add_ranges_by_labels (main_comp_unit_die, text_section_label,
30117 text_end_label, &range_list_added, true);
30118 if (cold_text_section_used)
30119 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
30120 cold_end_label, &range_list_added, true);
30122 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
30124 if (DECL_IGNORED_P (fde->decl))
30125 continue;
30126 if (!fde->in_std_section)
30127 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
30128 fde->dw_fde_end, &range_list_added,
30129 true);
30130 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
30131 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
30132 fde->dw_fde_second_end, &range_list_added,
30133 true);
30136 if (range_list_added)
30138 /* We need to give .debug_loc and .debug_ranges an appropriate
30139 "base address". Use zero so that these addresses become
30140 absolute. Historically, we've emitted the unexpected
30141 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
30142 Emit both to give time for other tools to adapt. */
30143 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
30144 if (! dwarf_strict && dwarf_version < 4)
30145 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
30147 add_ranges (NULL);
30151 /* AIX Assembler inserts the length, so adjust the reference to match the
30152 offset expected by debuggers. */
30153 strcpy (dl_section_ref, debug_line_section_label);
30154 if (XCOFF_DEBUGGING_INFO)
30155 strcat (dl_section_ref, DWARF_INITIAL_LENGTH_SIZE_STR);
30157 if (debug_info_level >= DINFO_LEVEL_TERSE)
30158 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
30159 dl_section_ref);
30161 if (have_macinfo)
30162 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30163 macinfo_section_label);
30165 if (dwarf_split_debug_info)
30167 if (have_location_lists)
30169 if (dwarf_version >= 5)
30170 add_AT_loclistsptr (comp_unit_die (), DW_AT_loclists_base,
30171 loc_section_label);
30172 /* optimize_location_lists calculates the size of the lists,
30173 so index them first, and assign indices to the entries.
30174 Although optimize_location_lists will remove entries from
30175 the table, it only does so for duplicates, and therefore
30176 only reduces ref_counts to 1. */
30177 index_location_lists (comp_unit_die ());
30180 if (addr_index_table != NULL)
30182 unsigned int index = 0;
30183 addr_index_table
30184 ->traverse_noresize<unsigned int *, index_addr_table_entry>
30185 (&index);
30189 loc_list_idx = 0;
30190 if (have_location_lists)
30192 optimize_location_lists (comp_unit_die ());
30193 /* And finally assign indexes to the entries for -gsplit-dwarf. */
30194 if (dwarf_version >= 5 && dwarf_split_debug_info)
30195 assign_location_list_indexes (comp_unit_die ());
30198 save_macinfo_strings ();
30200 if (dwarf_split_debug_info)
30202 unsigned int index = 0;
30204 /* Add attributes common to skeleton compile_units and
30205 type_units. Because these attributes include strings, it
30206 must be done before freezing the string table. Top-level
30207 skeleton die attrs are added when the skeleton type unit is
30208 created, so ensure it is created by this point. */
30209 add_top_level_skeleton_die_attrs (main_comp_unit_die);
30210 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30213 /* Output all of the compilation units. We put the main one last so that
30214 the offsets are available to output_pubnames. */
30215 for (node = cu_die_list; node; node = node->next)
30216 output_comp_unit (node->die, 0, NULL);
30218 hash_table<comdat_type_hasher> comdat_type_table (100);
30219 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
30221 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30223 /* Don't output duplicate types. */
30224 if (*slot != HTAB_EMPTY_ENTRY)
30225 continue;
30227 /* Add a pointer to the line table for the main compilation unit
30228 so that the debugger can make sense of DW_AT_decl_file
30229 attributes. */
30230 if (debug_info_level >= DINFO_LEVEL_TERSE)
30231 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30232 (!dwarf_split_debug_info
30233 ? dl_section_ref
30234 : debug_skeleton_line_section_label));
30236 output_comdat_type_unit (ctnode);
30237 *slot = ctnode;
30240 if (dwarf_split_debug_info)
30242 int mark;
30243 struct md5_ctx ctx;
30245 if (dwarf_version >= 5 && !vec_safe_is_empty (ranges_table))
30246 index_rnglists ();
30248 /* Compute a checksum of the comp_unit to use as the dwo_id. */
30249 md5_init_ctx (&ctx);
30250 mark = 0;
30251 die_checksum (comp_unit_die (), &ctx, &mark);
30252 unmark_all_dies (comp_unit_die ());
30253 md5_finish_ctx (&ctx, checksum);
30255 if (dwarf_version < 5)
30257 /* Use the first 8 bytes of the checksum as the dwo_id,
30258 and add it to both comp-unit DIEs. */
30259 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
30260 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
30263 /* Add the base offset of the ranges table to the skeleton
30264 comp-unit DIE. */
30265 if (!vec_safe_is_empty (ranges_table))
30267 if (dwarf_version >= 5)
30268 add_AT_lineptr (main_comp_unit_die, DW_AT_rnglists_base,
30269 ranges_base_label);
30270 else
30271 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
30272 ranges_section_label);
30275 switch_to_section (debug_addr_section);
30276 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
30277 output_addr_table ();
30280 /* Output the main compilation unit if non-empty or if .debug_macinfo
30281 or .debug_macro will be emitted. */
30282 output_comp_unit (comp_unit_die (), have_macinfo,
30283 dwarf_split_debug_info ? checksum : NULL);
30285 if (dwarf_split_debug_info && info_section_emitted)
30286 output_skeleton_debug_sections (main_comp_unit_die, checksum);
30288 /* Output the abbreviation table. */
30289 if (vec_safe_length (abbrev_die_table) != 1)
30291 switch_to_section (debug_abbrev_section);
30292 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30293 output_abbrev_section ();
30296 /* Output location list section if necessary. */
30297 if (have_location_lists)
30299 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
30300 char l2[MAX_ARTIFICIAL_LABEL_BYTES];
30301 /* Output the location lists info. */
30302 switch_to_section (debug_loc_section);
30303 if (dwarf_version >= 5)
30305 ASM_GENERATE_INTERNAL_LABEL (l1, DEBUG_LOC_SECTION_LABEL, 1);
30306 ASM_GENERATE_INTERNAL_LABEL (l2, DEBUG_LOC_SECTION_LABEL, 2);
30307 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
30308 dw2_asm_output_data (4, 0xffffffff,
30309 "Initial length escape value indicating "
30310 "64-bit DWARF extension");
30311 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
30312 "Length of Location Lists");
30313 ASM_OUTPUT_LABEL (asm_out_file, l1);
30314 dw2_asm_output_data (2, dwarf_version, "DWARF Version");
30315 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Address Size");
30316 dw2_asm_output_data (1, 0, "Segment Size");
30317 dw2_asm_output_data (4, dwarf_split_debug_info ? loc_list_idx : 0,
30318 "Offset Entry Count");
30320 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
30321 if (dwarf_version >= 5 && dwarf_split_debug_info)
30323 unsigned int save_loc_list_idx = loc_list_idx;
30324 loc_list_idx = 0;
30325 output_loclists_offsets (comp_unit_die ());
30326 gcc_assert (save_loc_list_idx == loc_list_idx);
30328 output_location_lists (comp_unit_die ());
30329 if (dwarf_version >= 5)
30330 ASM_OUTPUT_LABEL (asm_out_file, l2);
30333 output_pubtables ();
30335 /* Output the address range information if a CU (.debug_info section)
30336 was emitted. We output an empty table even if we had no functions
30337 to put in it. This because the consumer has no way to tell the
30338 difference between an empty table that we omitted and failure to
30339 generate a table that would have contained data. */
30340 if (info_section_emitted)
30342 switch_to_section (debug_aranges_section);
30343 output_aranges ();
30346 /* Output ranges section if necessary. */
30347 if (!vec_safe_is_empty (ranges_table))
30349 if (dwarf_version >= 5)
30350 output_rnglists (generation);
30351 else
30352 output_ranges ();
30355 /* Have to end the macro section. */
30356 if (have_macinfo)
30358 switch_to_section (debug_macinfo_section);
30359 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30360 output_macinfo (!dwarf_split_debug_info ? debug_line_section_label
30361 : debug_skeleton_line_section_label, false);
30362 dw2_asm_output_data (1, 0, "End compilation unit");
30365 /* Output the source line correspondence table. We must do this
30366 even if there is no line information. Otherwise, on an empty
30367 translation unit, we will generate a present, but empty,
30368 .debug_info section. IRIX 6.5 `nm' will then complain when
30369 examining the file. This is done late so that any filenames
30370 used by the debug_info section are marked as 'used'. */
30371 switch_to_section (debug_line_section);
30372 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
30373 if (! DWARF2_ASM_LINE_DEBUG_INFO)
30374 output_line_info (false);
30376 if (dwarf_split_debug_info && info_section_emitted)
30378 switch_to_section (debug_skeleton_line_section);
30379 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30380 output_line_info (true);
30383 /* If we emitted any indirect strings, output the string table too. */
30384 if (debug_str_hash || skeleton_debug_str_hash)
30385 output_indirect_strings ();
30386 if (debug_line_str_hash)
30388 switch_to_section (debug_line_str_section);
30389 const enum dwarf_form form = DW_FORM_line_strp;
30390 debug_line_str_hash->traverse<enum dwarf_form,
30391 output_indirect_string> (form);
30395 /* Returns a hash value for X (which really is a variable_value_struct). */
30397 inline hashval_t
30398 variable_value_hasher::hash (variable_value_struct *x)
30400 return (hashval_t) x->decl_id;
30403 /* Return nonzero if decl_id of variable_value_struct X is the same as
30404 UID of decl Y. */
30406 inline bool
30407 variable_value_hasher::equal (variable_value_struct *x, tree y)
30409 return x->decl_id == DECL_UID (y);
30412 /* Helper function for resolve_variable_value, handle
30413 DW_OP_GNU_variable_value in one location expression.
30414 Return true if exprloc has been changed into loclist. */
30416 static bool
30417 resolve_variable_value_in_expr (dw_attr_node *a, dw_loc_descr_ref loc)
30419 dw_loc_descr_ref next;
30420 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = next)
30422 next = loc->dw_loc_next;
30423 if (loc->dw_loc_opc != DW_OP_GNU_variable_value
30424 || loc->dw_loc_oprnd1.val_class != dw_val_class_decl_ref)
30425 continue;
30427 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30428 if (DECL_CONTEXT (decl) != current_function_decl)
30429 continue;
30431 dw_die_ref ref = lookup_decl_die (decl);
30432 if (ref)
30434 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30435 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30436 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30437 continue;
30439 dw_loc_list_ref l = loc_list_from_tree (decl, 0, NULL);
30440 if (l == NULL)
30441 continue;
30442 if (l->dw_loc_next)
30444 if (AT_class (a) != dw_val_class_loc)
30445 continue;
30446 switch (a->dw_attr)
30448 /* Following attributes allow both exprloc and loclist
30449 classes, so we can change them into a loclist. */
30450 case DW_AT_location:
30451 case DW_AT_string_length:
30452 case DW_AT_return_addr:
30453 case DW_AT_data_member_location:
30454 case DW_AT_frame_base:
30455 case DW_AT_segment:
30456 case DW_AT_static_link:
30457 case DW_AT_use_location:
30458 case DW_AT_vtable_elem_location:
30459 if (prev)
30461 prev->dw_loc_next = NULL;
30462 prepend_loc_descr_to_each (l, AT_loc (a));
30464 if (next)
30465 add_loc_descr_to_each (l, next);
30466 a->dw_attr_val.val_class = dw_val_class_loc_list;
30467 a->dw_attr_val.val_entry = NULL;
30468 a->dw_attr_val.v.val_loc_list = l;
30469 have_location_lists = true;
30470 return true;
30471 /* Following attributes allow both exprloc and reference,
30472 so if the whole expression is DW_OP_GNU_variable_value alone
30473 we could transform it into reference. */
30474 case DW_AT_byte_size:
30475 case DW_AT_bit_size:
30476 case DW_AT_lower_bound:
30477 case DW_AT_upper_bound:
30478 case DW_AT_bit_stride:
30479 case DW_AT_count:
30480 case DW_AT_allocated:
30481 case DW_AT_associated:
30482 case DW_AT_byte_stride:
30483 if (prev == NULL && next == NULL)
30484 break;
30485 /* FALLTHRU */
30486 default:
30487 if (dwarf_strict)
30488 continue;
30489 break;
30491 /* Create DW_TAG_variable that we can refer to. */
30492 gen_decl_die (decl, NULL_TREE, NULL,
30493 lookup_decl_die (current_function_decl));
30494 ref = lookup_decl_die (decl);
30495 if (ref)
30497 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30498 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30499 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30501 continue;
30503 if (prev)
30505 prev->dw_loc_next = l->expr;
30506 add_loc_descr (&prev->dw_loc_next, next);
30507 free_loc_descr (loc, NULL);
30508 next = prev->dw_loc_next;
30510 else
30512 memcpy (loc, l->expr, sizeof (dw_loc_descr_node));
30513 add_loc_descr (&loc, next);
30514 next = loc;
30516 loc = prev;
30518 return false;
30521 /* Attempt to resolve DW_OP_GNU_variable_value using loc_list_from_tree. */
30523 static void
30524 resolve_variable_value (dw_die_ref die)
30526 dw_attr_node *a;
30527 dw_loc_list_ref loc;
30528 unsigned ix;
30530 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30531 switch (AT_class (a))
30533 case dw_val_class_loc:
30534 if (!resolve_variable_value_in_expr (a, AT_loc (a)))
30535 break;
30536 /* FALLTHRU */
30537 case dw_val_class_loc_list:
30538 loc = AT_loc_list (a);
30539 gcc_assert (loc);
30540 for (; loc; loc = loc->dw_loc_next)
30541 resolve_variable_value_in_expr (a, loc->expr);
30542 break;
30543 default:
30544 break;
30548 /* Attempt to optimize DW_OP_GNU_variable_value refering to
30549 temporaries in the current function. */
30551 static void
30552 resolve_variable_values (void)
30554 if (!variable_value_hash || !current_function_decl)
30555 return;
30557 struct variable_value_struct *node
30558 = variable_value_hash->find_with_hash (current_function_decl,
30559 DECL_UID (current_function_decl));
30561 if (node == NULL)
30562 return;
30564 unsigned int i;
30565 dw_die_ref die;
30566 FOR_EACH_VEC_SAFE_ELT (node->dies, i, die)
30567 resolve_variable_value (die);
30570 /* Helper function for note_variable_value, handle one location
30571 expression. */
30573 static void
30574 note_variable_value_in_expr (dw_die_ref die, dw_loc_descr_ref loc)
30576 for (; loc; loc = loc->dw_loc_next)
30577 if (loc->dw_loc_opc == DW_OP_GNU_variable_value
30578 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
30580 tree decl = loc->dw_loc_oprnd1.v.val_decl_ref;
30581 dw_die_ref ref = lookup_decl_die (decl);
30582 if (! ref && (flag_generate_lto || flag_generate_offload))
30584 /* ??? This is somewhat a hack because we do not create DIEs
30585 for variables not in BLOCK trees early but when generating
30586 early LTO output we need the dw_val_class_decl_ref to be
30587 fully resolved. For fat LTO objects we'd also like to
30588 undo this after LTO dwarf output. */
30589 gcc_assert (DECL_CONTEXT (decl));
30590 dw_die_ref ctx = lookup_decl_die (DECL_CONTEXT (decl));
30591 gcc_assert (ctx != NULL);
30592 gen_decl_die (decl, NULL_TREE, NULL, ctx);
30593 ref = lookup_decl_die (decl);
30594 gcc_assert (ref != NULL);
30596 if (ref)
30598 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
30599 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
30600 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
30601 continue;
30603 if (VAR_P (decl)
30604 && DECL_CONTEXT (decl)
30605 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
30606 && lookup_decl_die (DECL_CONTEXT (decl)))
30608 if (!variable_value_hash)
30609 variable_value_hash
30610 = hash_table<variable_value_hasher>::create_ggc (10);
30612 tree fndecl = DECL_CONTEXT (decl);
30613 struct variable_value_struct *node;
30614 struct variable_value_struct **slot
30615 = variable_value_hash->find_slot_with_hash (fndecl,
30616 DECL_UID (fndecl),
30617 INSERT);
30618 if (*slot == NULL)
30620 node = ggc_cleared_alloc<variable_value_struct> ();
30621 node->decl_id = DECL_UID (fndecl);
30622 *slot = node;
30624 else
30625 node = *slot;
30627 vec_safe_push (node->dies, die);
30632 /* Walk the tree DIE and note DIEs with DW_OP_GNU_variable_value still
30633 with dw_val_class_decl_ref operand. */
30635 static void
30636 note_variable_value (dw_die_ref die)
30638 dw_die_ref c;
30639 dw_attr_node *a;
30640 dw_loc_list_ref loc;
30641 unsigned ix;
30643 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
30644 switch (AT_class (a))
30646 case dw_val_class_loc_list:
30647 loc = AT_loc_list (a);
30648 gcc_assert (loc);
30649 if (!loc->noted_variable_value)
30651 loc->noted_variable_value = 1;
30652 for (; loc; loc = loc->dw_loc_next)
30653 note_variable_value_in_expr (die, loc->expr);
30655 break;
30656 case dw_val_class_loc:
30657 note_variable_value_in_expr (die, AT_loc (a));
30658 break;
30659 default:
30660 break;
30663 /* Mark children. */
30664 FOR_EACH_CHILD (die, c, note_variable_value (c));
30667 /* Perform any cleanups needed after the early debug generation pass
30668 has run. */
30670 static void
30671 dwarf2out_early_finish (const char *filename)
30673 set_early_dwarf s;
30675 /* PCH might result in DW_AT_producer string being restored from the
30676 header compilation, so always fill it with empty string initially
30677 and overwrite only here. */
30678 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
30679 producer_string = gen_producer_string ();
30680 producer->dw_attr_val.v.val_str->refcount--;
30681 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
30683 /* Add the name for the main input file now. We delayed this from
30684 dwarf2out_init to avoid complications with PCH. */
30685 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
30686 add_comp_dir_attribute (comp_unit_die ());
30688 /* When emitting DWARF5 .debug_line_str, move DW_AT_name and
30689 DW_AT_comp_dir into .debug_line_str section. */
30690 if (!DWARF2_ASM_LINE_DEBUG_INFO
30691 && dwarf_version >= 5
30692 && DWARF5_USE_DEBUG_LINE_STR)
30694 for (int i = 0; i < 2; i++)
30696 dw_attr_node *a = get_AT (comp_unit_die (),
30697 i ? DW_AT_comp_dir : DW_AT_name);
30698 if (a == NULL
30699 || AT_class (a) != dw_val_class_str
30700 || strlen (AT_string (a)) + 1 <= DWARF_OFFSET_SIZE)
30701 continue;
30703 if (! debug_line_str_hash)
30704 debug_line_str_hash
30705 = hash_table<indirect_string_hasher>::create_ggc (10);
30707 struct indirect_string_node *node
30708 = find_AT_string_in_table (AT_string (a), debug_line_str_hash);
30709 set_indirect_string (node);
30710 node->form = DW_FORM_line_strp;
30711 a->dw_attr_val.v.val_str->refcount--;
30712 a->dw_attr_val.v.val_str = node;
30716 /* With LTO early dwarf was really finished at compile-time, so make
30717 sure to adjust the phase after annotating the LTRANS CU DIE. */
30718 if (in_lto_p)
30720 early_dwarf_finished = true;
30721 return;
30724 /* Walk through the list of incomplete types again, trying once more to
30725 emit full debugging info for them. */
30726 retry_incomplete_types ();
30728 /* The point here is to flush out the limbo list so that it is empty
30729 and we don't need to stream it for LTO. */
30730 flush_limbo_die_list ();
30732 gen_scheduled_generic_parms_dies ();
30733 gen_remaining_tmpl_value_param_die_attribute ();
30735 /* Add DW_AT_linkage_name for all deferred DIEs. */
30736 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
30738 tree decl = node->created_for;
30739 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
30740 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
30741 ended up in deferred_asm_name before we knew it was
30742 constant and never written to disk. */
30743 && DECL_ASSEMBLER_NAME (decl))
30745 add_linkage_attr (node->die, decl);
30746 move_linkage_attr (node->die);
30749 deferred_asm_name = NULL;
30751 if (flag_eliminate_unused_debug_types)
30752 prune_unused_types ();
30754 /* Generate separate COMDAT sections for type DIEs. */
30755 if (use_debug_types)
30757 break_out_comdat_types (comp_unit_die ());
30759 /* Each new type_unit DIE was added to the limbo die list when created.
30760 Since these have all been added to comdat_type_list, clear the
30761 limbo die list. */
30762 limbo_die_list = NULL;
30764 /* For each new comdat type unit, copy declarations for incomplete
30765 types to make the new unit self-contained (i.e., no direct
30766 references to the main compile unit). */
30767 for (comdat_type_node *ctnode = comdat_type_list;
30768 ctnode != NULL; ctnode = ctnode->next)
30769 copy_decls_for_unworthy_types (ctnode->root_die);
30770 copy_decls_for_unworthy_types (comp_unit_die ());
30772 /* In the process of copying declarations from one unit to another,
30773 we may have left some declarations behind that are no longer
30774 referenced. Prune them. */
30775 prune_unused_types ();
30778 /* Traverse the DIE's and note DIEs with DW_OP_GNU_variable_value still
30779 with dw_val_class_decl_ref operand. */
30780 note_variable_value (comp_unit_die ());
30781 for (limbo_die_node *node = cu_die_list; node; node = node->next)
30782 note_variable_value (node->die);
30783 for (comdat_type_node *ctnode = comdat_type_list; ctnode != NULL;
30784 ctnode = ctnode->next)
30785 note_variable_value (ctnode->root_die);
30786 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30787 note_variable_value (node->die);
30789 /* The AT_pubnames attribute needs to go in all skeleton dies, including
30790 both the main_cu and all skeleton TUs. Making this call unconditional
30791 would end up either adding a second copy of the AT_pubnames attribute, or
30792 requiring a special case in add_top_level_skeleton_die_attrs. */
30793 if (!dwarf_split_debug_info)
30794 add_AT_pubnames (comp_unit_die ());
30796 /* The early debug phase is now finished. */
30797 early_dwarf_finished = true;
30799 /* Do not generate DWARF assembler now when not producing LTO bytecode. */
30800 if (!flag_generate_lto && !flag_generate_offload)
30801 return;
30803 /* Now as we are going to output for LTO initialize sections and labels
30804 to the LTO variants. We don't need a random-seed postfix as other
30805 LTO sections as linking the LTO debug sections into one in a partial
30806 link is fine. */
30807 init_sections_and_labels (true);
30809 /* The output below is modeled after dwarf2out_finish with all
30810 location related output removed and some LTO specific changes.
30811 Some refactoring might make both smaller and easier to match up. */
30813 /* Traverse the DIE's and add add sibling attributes to those DIE's
30814 that have children. */
30815 add_sibling_attributes (comp_unit_die ());
30816 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30817 add_sibling_attributes (node->die);
30818 for (comdat_type_node *ctnode = comdat_type_list;
30819 ctnode != NULL; ctnode = ctnode->next)
30820 add_sibling_attributes (ctnode->root_die);
30822 if (have_macinfo)
30823 add_AT_macptr (comp_unit_die (), DEBUG_MACRO_ATTRIBUTE,
30824 macinfo_section_label);
30826 save_macinfo_strings ();
30828 if (dwarf_split_debug_info)
30830 unsigned int index = 0;
30831 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
30834 /* Output all of the compilation units. We put the main one last so that
30835 the offsets are available to output_pubnames. */
30836 for (limbo_die_node *node = limbo_die_list; node; node = node->next)
30837 output_comp_unit (node->die, 0, NULL);
30839 hash_table<comdat_type_hasher> comdat_type_table (100);
30840 for (comdat_type_node *ctnode = comdat_type_list;
30841 ctnode != NULL; ctnode = ctnode->next)
30843 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
30845 /* Don't output duplicate types. */
30846 if (*slot != HTAB_EMPTY_ENTRY)
30847 continue;
30849 /* Add a pointer to the line table for the main compilation unit
30850 so that the debugger can make sense of DW_AT_decl_file
30851 attributes. */
30852 if (debug_info_level >= DINFO_LEVEL_TERSE)
30853 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
30854 (!dwarf_split_debug_info
30855 ? debug_line_section_label
30856 : debug_skeleton_line_section_label));
30858 output_comdat_type_unit (ctnode);
30859 *slot = ctnode;
30862 /* Stick a unique symbol to the main debuginfo section. */
30863 compute_comp_unit_symbol (comp_unit_die ());
30865 /* Output the main compilation unit. We always need it if only for
30866 the CU symbol. */
30867 output_comp_unit (comp_unit_die (), true, NULL);
30869 /* Output the abbreviation table. */
30870 if (vec_safe_length (abbrev_die_table) != 1)
30872 switch_to_section (debug_abbrev_section);
30873 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
30874 output_abbrev_section ();
30877 /* Have to end the macro section. */
30878 if (have_macinfo)
30880 /* We have to save macinfo state if we need to output it again
30881 for the FAT part of the object. */
30882 vec<macinfo_entry, va_gc> *saved_macinfo_table = macinfo_table;
30883 if (flag_fat_lto_objects)
30884 macinfo_table = macinfo_table->copy ();
30886 switch_to_section (debug_macinfo_section);
30887 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
30888 output_macinfo (debug_skeleton_line_section_label, true);
30889 dw2_asm_output_data (1, 0, "End compilation unit");
30891 /* Emit a skeleton debug_line section. */
30892 switch_to_section (debug_skeleton_line_section);
30893 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
30894 output_line_info (true);
30896 if (flag_fat_lto_objects)
30898 vec_free (macinfo_table);
30899 macinfo_table = saved_macinfo_table;
30904 /* If we emitted any indirect strings, output the string table too. */
30905 if (debug_str_hash || skeleton_debug_str_hash)
30906 output_indirect_strings ();
30908 /* Switch back to the text section. */
30909 switch_to_section (text_section);
30912 /* Reset all state within dwarf2out.c so that we can rerun the compiler
30913 within the same process. For use by toplev::finalize. */
30915 void
30916 dwarf2out_c_finalize (void)
30918 last_var_location_insn = NULL;
30919 cached_next_real_insn = NULL;
30920 used_rtx_array = NULL;
30921 incomplete_types = NULL;
30922 decl_scope_table = NULL;
30923 debug_info_section = NULL;
30924 debug_skeleton_info_section = NULL;
30925 debug_abbrev_section = NULL;
30926 debug_skeleton_abbrev_section = NULL;
30927 debug_aranges_section = NULL;
30928 debug_addr_section = NULL;
30929 debug_macinfo_section = NULL;
30930 debug_line_section = NULL;
30931 debug_skeleton_line_section = NULL;
30932 debug_loc_section = NULL;
30933 debug_pubnames_section = NULL;
30934 debug_pubtypes_section = NULL;
30935 debug_str_section = NULL;
30936 debug_line_str_section = NULL;
30937 debug_str_dwo_section = NULL;
30938 debug_str_offsets_section = NULL;
30939 debug_ranges_section = NULL;
30940 debug_frame_section = NULL;
30941 fde_vec = NULL;
30942 debug_str_hash = NULL;
30943 debug_line_str_hash = NULL;
30944 skeleton_debug_str_hash = NULL;
30945 dw2_string_counter = 0;
30946 have_multiple_function_sections = false;
30947 text_section_used = false;
30948 cold_text_section_used = false;
30949 cold_text_section = NULL;
30950 current_unit_personality = NULL;
30952 early_dwarf = false;
30953 early_dwarf_finished = false;
30955 next_die_offset = 0;
30956 single_comp_unit_die = NULL;
30957 comdat_type_list = NULL;
30958 limbo_die_list = NULL;
30959 file_table = NULL;
30960 decl_die_table = NULL;
30961 common_block_die_table = NULL;
30962 decl_loc_table = NULL;
30963 call_arg_locations = NULL;
30964 call_arg_loc_last = NULL;
30965 call_site_count = -1;
30966 tail_call_site_count = -1;
30967 cached_dw_loc_list_table = NULL;
30968 abbrev_die_table = NULL;
30969 delete dwarf_proc_stack_usage_map;
30970 dwarf_proc_stack_usage_map = NULL;
30971 line_info_label_num = 0;
30972 cur_line_info_table = NULL;
30973 text_section_line_info = NULL;
30974 cold_text_section_line_info = NULL;
30975 separate_line_info = NULL;
30976 info_section_emitted = false;
30977 pubname_table = NULL;
30978 pubtype_table = NULL;
30979 macinfo_table = NULL;
30980 ranges_table = NULL;
30981 ranges_by_label = NULL;
30982 rnglist_idx = 0;
30983 have_location_lists = false;
30984 loclabel_num = 0;
30985 poc_label_num = 0;
30986 last_emitted_file = NULL;
30987 label_num = 0;
30988 tmpl_value_parm_die_table = NULL;
30989 generic_type_instances = NULL;
30990 frame_pointer_fb_offset = 0;
30991 frame_pointer_fb_offset_valid = false;
30992 base_types.release ();
30993 XDELETEVEC (producer_string);
30994 producer_string = NULL;
30997 #include "gt-dwarf2out.h"